public static string RDKit_Smiles(string text,
                                          object isomericSmiles = null, bool kekuleSmiles   = false,
                                          object rootedAtAtom   = null, object canonical    = null,
                                          bool allBondsExplicit = false, bool allHsExplicit = false,
                                          bool doRandom         = false)
        {
            var _isomericSmiles = ExcelDnaUtility.ToBoolean(isomericSmiles, true);
            var _rootedAtAtom   = ExcelDnaUtility.ToInt32(rootedAtAtom, -1);
            var _canonical      = ExcelDnaUtility.ToBoolean(canonical, true);

            if (_isomericSmiles &&
                !kekuleSmiles &&
                _rootedAtAtom == -1 &&
                _canonical &&
                !allBondsExplicit &&
                !allHsExplicit &&
                !doRandom)
            {
                return(RDKit_CalcDesc(text, "RDKit_Smiles", mol => RDKit.Chem.MolToSmiles(mol)));
            }
            {
                ROMol mol;
                if (kekuleSmiles)
                {
                    var _mol = RWMol.MolFromSmiles(text);
                    RDKFuncs.Kekulize(_mol);
                    mol = _mol;
                }
                else
                {
                    mol = Parse(text);
                }
                return(RDKit.Chem.MolToSmiles(mol, _isomericSmiles, kekuleSmiles, _rootedAtAtom, _canonical, allBondsExplicit, allHsExplicit, doRandom));
            }
        }
        /// <summary>
        /// Tries to parses text as SMILES, InChI, or MolBlock and returns RDKit ROMol.
        /// </summary>
        /// <param name="molIdent">Text expression of molecule like SMILES, InChI, or MolBlock.</param>
        /// <returns>RDKit ROMol.</returns>
        public static ROMol Parse(string molIdent)
        {
            if (molIdent == null)
            {
                throw new ArgumentNullException(nameof(molIdent));
            }

            var mol = MolecularCache.GetOrAdd(molIdent, a_ident =>
            {
                string notationType = null;
                ROMol a_mol;

                a_mol = RWMol.MolFromSmiles(molIdent);
                if (a_mol != null)
                {
                    notationType = "SMILES";
                    goto L_Found;
                }

                using (var rv = new ExtraInchiReturnValues())
                {
                    a_mol = RDKFuncs.InchiToMol(molIdent, rv);
                    if (a_mol != null)
                    {
                        notationType = "InChI";
                        goto L_Found;
                    }
                }

                a_mol = RWMol.MolFromMolBlock(molIdent);
                if (a_mol != null)
                {
                    RDKFuncs.assignStereochemistryFrom3D(a_mol);
                    notationType = "MolBlock";
                    goto L_Found;
                }

                L_Found:
                if (a_mol == null)
                {
                    a_mol = nullMol;
                }
                else
                {
                    if (notationType != null)
                    {
                        a_mol.setProp("source", notationType);
                    }
                }

                return(a_mol);
            });

            if (object.ReferenceEquals(mol, nullMol))
            {
                return(null);
            }

            return(mol);
        }
Ejemplo n.º 3
0
    static void Main()
    {
        // ----- Object creation -----

        Console.WriteLine("Creating some objects:");

        ROMol m1 = RWMol.MolFromSmiles("c1ccccc1");

        Console.WriteLine(" mol: " + m1 + " " + m1.getNumAtoms());
        ROMol m2 = RWMol.MolFromSmiles("c1ccccn1");

        Console.WriteLine(" smi: " + m1 + " " + m1.MolToSmiles());
        Console.WriteLine(" smi2: " + m2 + " " + m2.MolToSmiles());


        ExplicitBitVect fp1 = RDKFuncs.LayeredFingerprintMol(m1);
        ExplicitBitVect fp2 = RDKFuncs.LayeredFingerprintMol(m2);

        Console.WriteLine(" sim: " + RDKFuncs.TanimotoSimilarityEBV(fp1, fp2));

        //rxnTest();
        //smiTest();
        morganTest();

        Console.WriteLine("Goodbye");
    }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var smiles = "c1ccccc1C";
            var mol    = RWMol.MolFromSmiles(smiles);
            var re_smi = RDKFuncs.MolToSmiles(mol);

            Console.WriteLine($"Hello toluene, {re_smi}.");
        }
Ejemplo n.º 5
0
 static void BulkMemoryLeakTest()
 {
     Console.WriteLine("Bulk memory leak test");
     for (uint i = 0; i < 10000; ++i)
     {
         ROMol m4 = RWMol.MolFromSmiles("Clc1cccc(N2CCN(CCC3CCC(CC3)NC(=O)c3cccs3)CC2)c1Cl");
         if ((i % 1000) == 0)
         {
             Console.WriteLine(" Done: " + i);
         }
         m4.Dispose();
         //GC.Collect();
     }
 }
Ejemplo n.º 6
0
    // static void rxnTest() {
    //     Console.WriteLine( "Reaction tests" );
    //     var rxn = ChemicalReaction.ReactionFromSmarts("[N:1][C:2].[OH][C:3]=[O:4]>>[C:2][N:1][C:3]=[O:4]");
    //     var amine = RWMol.MolFromSmiles("CCCN");
    //     var acid = RWMol.MolFromSmiles("C1CC1CC(=O)O");
    //     ROMol[] rs = {amine,acid};
    //     ROMol_Vect rv = new ROMol_Vect(rs);
    //     for(var i=0;i<100000;i++){
    //         var ps=rxn.runReactants(rv);
    //         if(i%100 == 0) {
    //             Console.WriteLine( "\t{0}", i );
    //         }
    //     }
    //     Console.WriteLine( "Goodbye" );
    // }
    // static void smiTest() {
    //     Console.WriteLine( "repeatedly from smiles" );
    //     for(var i=0;i<1000000;i++){
    //         ROMol m1=RDKFuncs.MolFromSmiles("c1ccccc1");
    //         if(i%1000 == 0) {
    //             Console.WriteLine( "\t{0}", i );
    //         }
    //     }

    //     Console.WriteLine( "Goodbye" );
    // }

    static void morganTest()
    {
        // ----- Object creation -----

        Console.WriteLine("Creating some objects:");

        ROMol m1 = RWMol.MolFromSmiles("c1ccccc1");

        Console.WriteLine(" mol: " + m1 + " " + m1.getNumAtoms());
        ROMol m2 = RWMol.MolFromSmiles("c1ccccn1");

        var fp1 = RDKFuncs.MorganFingerprintMol(m1, 2);
        var fp2 = RDKFuncs.MorganFingerprintMol(m2, 2);

        Console.WriteLine(" sim: " + RDKFuncs.DiceSimilarity(fp1, fp2));
    }
        public TempFile GenerateTemporary(string text, double width, double height)
        {
            var min = Math.Min(width, height);

            if (min < Config.MinimumEdgePixels)
            {
                double scale = Config.MinimumEdgePixels / min;
                width  *= scale;
                height *= scale;
            }

            RWMol mol = null;

            mol = Chem.MolFromSmiles(text);
            if (mol == null)
            {
                mol = Chem.MolFromSmarts(text);
            }
            if (mol == null)
            {
                return(null);
            }

            var tempFile = new TempFile("." + Config.ImageType);
            var filename = tempFile.FileName;

            MolToFile(mol, filename, new Tuple <int, int>((int)width, (int)height));

            if (filename.EndsWith(".svg"))
            {
                string svg;
                using (var r = new StreamReader(filename))
                {
                    svg = r.ReadToEnd();
                }
                if (ImageBGIsTransparent())
                {
                    using (var r = new StreamWriter(filename))
                    {
                        r.Write(re_rect_tag.Replace(svg, ""));
                    }
                }
            }

            return(tempFile);
        }
Ejemplo n.º 8
0
    public void TestDrawMolecule()
    {
        var mol    = RWMol.MolFromSmiles("c1ccc(C)c(C)c1C");
        var drawer = new MolDraw2DCairo(300, 300, -1, -1, true);

        drawer.drawOptions().addAtomIndices = true;
        drawer.drawMolecule(mol);
        drawer.finishDrawing();
        var png1 = drawer.getImage().ToArray();

        drawer.writeDrawingText("test.png");
        byte[] png2 = File.ReadAllBytes("test.png");
        Assert.Equal(png1.Length, png2.Length);
        for (int i = 0; i < png1.Length; i++)
        {
            Assert.Equal(png1[i], png2[i]);
        }
        File.Delete("test.png");
    }
Ejemplo n.º 9
0
        static void Demo()
        {
            var toluene           = RWMol.MolFromSmiles("Cc1ccccc1");
            var mol1              = RWMol.MolFromMolFile(Path.Combine("Data", "input.mol"));
            var stringWithMolData = new StreamReader(Path.Combine("Data", "input.mol")).ReadToEnd();
            var mol2              = RWMol.MolFromMolBlock(stringWithMolData);

            using (var suppl = new SDMolSupplier(Path.Combine("Data", "5ht3ligs.sdf")))
            {
                while (!suppl.atEnd())
                {
                    var mol = suppl.next();
                    if (mol == null)
                    {
                        continue;
                    }
                    Console.WriteLine(mol.getAtoms().Count);

                    using (var maccs = RDKFuncs.MACCSFingerprintMol(mol))
                    {
                        Console.WriteLine(ToString(maccs));
                    }
                }
            }

            using (var gzsuppl = new ForwardSDMolSupplier(new gzstream("Data/actives_5ht3.sdf.gz")))
            {
                while (!gzsuppl.atEnd())
                {
                    var mol = gzsuppl.next();
                    if (mol == null)
                    {
                        continue;
                    }
                    Console.WriteLine(mol.getAtoms().Count);
                    using (var maccs = RDKFuncs.MACCSFingerprintMol(mol))
                    {
                        Console.WriteLine(ToString(maccs));
                    }
                }
            }
        }
Ejemplo n.º 10
0
        static void MakePicture(string smiles, string filename)
        {
            int width  = 200;
            int height = 200;

            RWMol mol = null;

            mol = RWMol.MolFromSmiles(smiles);
            if (mol == null)
            {
                mol = RWMol.MolFromSmarts(smiles);
            }
            if (mol == null)
            {
                throw new Exception($"Cannot recognize: '{smiles}'");
            }

            RDKFuncs.prepareMolForDrawing(mol);
            if (filename.EndsWith(".svg"))
            {
                var view = new MolDraw2DSVG(width, height);
                view.drawMolecule(mol);
                view.finishDrawing();
                using (var w = new StreamWriter(filename))
                {
                    w.Write(view.getDrawingText());
                    Console.WriteLine($"{filename} is drawn.");
                }
            }
            else if (filename.EndsWith(".png"))
            {
                var view = new MolDraw2DCairo(width, height);
                view.drawMolecule(mol, Path.GetFileNameWithoutExtension(filename));
                view.finishDrawing();
                view.writeDrawingText(filename);
            }
            else
            {
                throw new Exception($"Not supported: {filename}");
            }
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            // ----- Object creation -----

            Console.WriteLine("Creating some objects:");

            ROMol m1 = RWMol.MolFromSmiles("c1ccccc1");

            Console.WriteLine(" mol: " + m1 + " " + m1.getNumAtoms());
            ROMol m2 = RWMol.MolFromSmiles("c1ccccn1");

            Console.WriteLine(" smi: " + m1 + " " + m1.MolToSmiles());
            Console.WriteLine(" smi2: " + m2 + " " + m2.MolToSmiles());


            ExplicitBitVect fp1 = RDKFuncs.LayeredFingerprintMol(m1);
            ExplicitBitVect fp2 = RDKFuncs.LayeredFingerprintMol(m2);

            Console.WriteLine(" sim: " + RDKFuncs.TanimotoSimilarityEBV(fp1, fp2));

            //rxnTest();
            //smiTest();
            //morganTest();

            ROMol m3     = RWMol.MolFromSmiles("c1ccccc1");
            uint  nAtoms = m3.getNumAtoms(true);

            Console.WriteLine("Bulk memory leak test");
            for (uint i = 0; i < 10000; ++i)
            {
                ROMol m4 = RWMol.MolFromSmiles("Clc1cccc(N2CCN(CCC3CCC(CC3)NC(=O)c3cccs3)CC2)c1Cl");
                if ((i % 1000) == 0)
                {
                    Console.WriteLine(" Done: " + i);
                }
                m4.Dispose();
                //GC.Collect();
            }

            Console.WriteLine("Goodbye");
        }
Ejemplo n.º 12
0
        static void CreateSomeObjects()
        {
            // ----- Object creation -----
            Console.WriteLine("Creating some objects:");

            ROMol m1 = RWMol.MolFromSmiles("c1ccccc1");

            Console.WriteLine(" mol: " + m1 + " " + m1.getNumAtoms());
            ROMol m2 = RWMol.MolFromSmiles("c1ccccn1");

            Console.WriteLine(" smi: " + m1 + " " + m1.MolToSmiles());
            Console.WriteLine(" smi2: " + m2 + " " + m2.MolToSmiles());

            ExplicitBitVect fp1 = RDKFuncs.LayeredFingerprintMol(m1);
            ExplicitBitVect fp2 = RDKFuncs.LayeredFingerprintMol(m2);

            Console.WriteLine(" sim: " + RDKFuncs.TanimotoSimilarityEBV(fp1, fp2));

            const string SMILES_benzene = "c1ccccc1";
            ROMol        m3             = RWMol.MolFromSmiles(SMILES_benzene);
            uint         nAtoms         = m3.getNumAtoms(true);

            Console.WriteLine($"The number of atoms in {SMILES_benzene} is {nAtoms}.");
        }
Ejemplo n.º 13
0
 public static void CleanUp(RWMol mol)
 {
     RDKFuncs.cleanUp(mol);
 }
Ejemplo n.º 14
0
 public static void AssignRadicals(RWMol mol)
 {
     RDKFuncs.assignRadicals(mol);
 }
Ejemplo n.º 15
0
 public static void SetAromaticity(RWMol mol, AromaticityModel model = AromaticityModel.AROMATICITY_DEFAULT)
 {
     RDKFuncs.setAromaticity(mol, model);
 }
Ejemplo n.º 16
0
 public static void PrepareMolForDrawing(RWMol mol, bool kekulize = true, bool addChiralHs = true, bool wedgeBonds = true, bool forceCoords = false)
 {
     RDKFuncs.prepareMolForDrawing(mol, kekulize, addChiralHs, wedgeBonds, forceCoords);
 }
Ejemplo n.º 17
0
            /// <summary>
            /// Generates a drawing of a molecule and writes it to a file.
            /// </summary>
            /// <seealso href="https://github.com/rdkit/rdkit/blob/1649029367a8e517cf1913a5b80b91813874e002/rdkit/Chem/Draw/__init__.py#L265-L300"/>
            public static void MolToFile(
                RWMol mol,
                string filename,
                Tuple <int, int> size     = null,
                bool kekulize             = true,
                bool wedgeBonds           = true,
                string imageType          = null,
                bool fitImage             = false,
                string legend             = "",
                Int_Vect highlight_atoms  = null,
                Int_Vect highlight_bonds  = null,
                DrawColour highlightColor = null
                )
            {
                if (size == null)
                {
                    size = new Tuple <int, int>(300, 300);
                }

                if (imageType == null)
                {
                    imageType = Path.GetExtension(filename).Substring(1).ToLowerInvariant();
                }

                try
                {
                    RdMolDraw2D.PrepareMolForDrawing(mol, kekulize: kekulize, wedgeBonds: wedgeBonds);
                }
                catch (Exception)
                {
                    RdMolDraw2D.PrepareMolForDrawing(mol, kekulize: false, wedgeBonds: wedgeBonds);
                }
                MolDraw2D d2d;

                switch (imageType)
                {
                case "png":
                    d2d = new MolDraw2DCairo(size.Item1, size.Item2);
                    break;

                case "svg":
                    d2d = new MolDraw2DSVG(size.Item1, size.Item2);
                    break;

                default:
                    throw new Exception($"{imageType} is not supported.");
                }
                if (highlightColor != null)
                {
                    d2d.DrawOptions().highlightColour = highlightColor;
                }
                d2d.DrawOptions().prepareMolsBeforeDrawing = false;
                d2d.DrawMolecule(mol, legend, highlight_atoms, highlight_bonds);
                d2d.FinishDrawing();
                switch (d2d)
                {
                case MolDraw2DCairo d:
                    d.WriteDrawingText(filename);
                    break;

                case MolDraw2DSVG d:
                    using (var w = new StreamWriter(filename))
                    {
                        w.Write(d.GetDrawingText());
                    }
                    break;
                }
            }
Ejemplo n.º 18
0
 public static RWMol MolFromSequence(string text, bool sanitize = true, SequenceFlavor flavor = 0)
 {
     return(RWMol.MolFromSequence(text, sanitize, (int)flavor));
 }
Ejemplo n.º 19
0
 public static void Kekulize(RWMol mol, bool clearAromaticFlags = false)
 {
     RDKFuncs.Kekulize(mol, clearAromaticFlags);
 }
Ejemplo n.º 20
0
 // TODO: Add strictParsing
 public static RWMol MolFromMolFile(string molFileName, bool sanitize = true, bool removeHs = true)
 {
     return(RWMol.MolFromMolFile(molFileName, sanitize, removeHs));
 }
Ejemplo n.º 21
0
 // TODO: Add strictParsing
 public static RWMol MolFromMolBlock(string molBlock, bool sanitize = true, bool removeHs = true)
 {
     return(RWMol.MolFromMolBlock(molBlock, sanitize, removeHs));
 }
Ejemplo n.º 22
0
 public static RWMol MolFromMol2File(string molFileName, bool sanitize = true, bool removeHs = true, bool cleanupSubstructures = true)
 {
     return(RWMol.MolFromMol2File(molFileName, sanitize, removeHs, Mol2Type.CORINA, cleanupSubstructures));
 }
Ejemplo n.º 23
0
 public static RWMol MolFromHELM(string text, bool sanitize = true)
 {
     return(RWMol.MolFromHELM(text, sanitize));
 }
Ejemplo n.º 24
0
 static void ErrorHandle()
 {
     var invalid = RWMol.MolFromSmiles("dfep3js2g");
 }
Ejemplo n.º 25
0
 public static RWMol MolFromSmiles(string smiles, bool sanitize = true)
 {
     return(RWMol.MolFromSmiles(smiles, 0, sanitize));
 }
Ejemplo n.º 26
0
 public static RWMol MolFromSmarts(string smiles, bool mergeHs = false)
 {
     return(RWMol.MolFromSmarts(smiles, 0, mergeHs));
 }
Ejemplo n.º 27
0
        //
        // rdmolops
        //

        public static void AddHs(RWMol mol, bool explicitOnly = false, bool addCoords     = false,
                                 UInt_Vect onlyOnAtoms        = null, bool addResidueInfo = false)
        {
            RDKFuncs.addHs(mol, explicitOnly, addCoords, onlyOnAtoms, addResidueInfo);
        }
Ejemplo n.º 28
0
 public static RWMol MolFromPDBFile(string molFileName, bool sanitize = true,
                                    bool removeHs = true, PDBFlavors flavor = 0, bool proximityBonding = true)
 {
     return(RWMol.MolFromPDBBlock(molFileName, sanitize, removeHs, (uint)flavor, proximityBonding));
 }
Ejemplo n.º 29
0
 public static SanitizeFlags SanitizeMol(RWMol mol, SanitizeFlags sanitizeOps = SanitizeFlags.SANITIZE_ALL)
 {
     return((SanitizeFlags)RDKFuncs.sanitizeMol(mol, (int)sanitizeOps));
 }