Exemple #1
0
        public void SaturateRingSystems(IAtomContainer atomContainer)
        {
            var   rs0      = Cycles.FindSSSR(atomContainer.Builder.NewAtomContainer(atomContainer)).ToRingSet();
            var   ringSets = RingPartitioner.PartitionRings(rs0);
            IAtom atom     = null;

            foreach (var rs in ringSets)
            {
                var containers = RingSetManipulator.GetAllAtomContainers(rs);
                foreach (var ac in containers)
                {
                    var temp = new int[ac.Atoms.Count];
                    for (int g = 0; g < ac.Atoms.Count; g++)
                    {
                        atom    = ac.Atoms[g];
                        temp[g] = atom.ImplicitHydrogenCount.Value;
                        atom.ImplicitHydrogenCount = (atomContainer.GetConnectedBonds(atom).Count() - ac.GetConnectedBonds(atom).Count() - temp[g]);
                    }
                    Saturate(ac);
                    for (int g = 0; g < ac.Atoms.Count; g++)
                    {
                        atom = ac.Atoms[g];
                        atom.ImplicitHydrogenCount = temp[g];
                    }
                }
            }
        }
        public virtual void  saturateRingSystems(IAtomContainer atomContainer)
        {
            IRingSet rs = new SSSRFinder(atomContainer.Builder.newMolecule(atomContainer)).findSSSR();

            System.Collections.ArrayList ringSets = RingPartitioner.partitionRings(rs);
            IAtomContainer ac   = null;
            IAtom          atom = null;

            int[] temp;
            for (int f = 0; f < ringSets.Count; f++)
            {
                rs   = (IRingSet)ringSets[f];
                ac   = RingSetManipulator.getAllInOneContainer(rs);
                temp = new int[ac.AtomCount];
                for (int g = 0; g < ac.AtomCount; g++)
                {
                    atom    = ac.getAtomAt(g);
                    temp[g] = atom.getHydrogenCount();
                    atom.setHydrogenCount(atomContainer.getBondCount(atom) - ac.getBondCount(atom) - temp[g]);
                }
                saturate(ac);
                for (int g = 0; g < ac.AtomCount; g++)
                {
                    atom = ac.getAtomAt(g);
                    atom.setHydrogenCount(temp[g]);
                }
            }
        }
        public void TestgetBitFingerprintIAtomContainerIRingSetList()
        {
            var fingerprinter = new ExtendedFingerprinter();

            Assert.IsNotNull(fingerprinter);

            var mol    = TestMoleculeFactory.MakeIndole();
            var rs     = Cycles.FindSSSR(mol).ToRingSet();
            var rslist = RingPartitioner.PartitionRings(rs);
            var bs     = fingerprinter.GetBitFingerprint(mol, rs, rslist).AsBitSet();
            var frag1  = TestMoleculeFactory.MakePyrrole();
            var bs1    = fingerprinter.GetBitFingerprint(frag1).AsBitSet();

            Assert.IsTrue(FingerprinterTool.IsSubset(bs, bs1));
            Assert.IsFalse(FingerprinterTool.IsSubset(bs1, bs));
        }
        public void TestMapTemplates_IAtomContainer_Double()
        {
            var ac   = TestMoleculeFactory.MakeBicycloRings();
            var th3d = TemplateHandler3D.Instance;
            var ffc  = new ForceFieldConfigurator();

            ffc.SetForceFieldConfigurator("mm2");
            var ringSetMolecule         = ffc.AssignAtomTyps(ac);
            var ringSystems             = RingPartitioner.PartitionRings(ringSetMolecule);
            var largestRingSet          = RingSetManipulator.GetLargestRingSet(ringSystems);
            var largestRingSetContainer = RingSetManipulator.GetAllInOneContainer(largestRingSet);

            th3d.MapTemplates(largestRingSetContainer, largestRingSetContainer.Atoms.Count);
            for (int i = 0; i < ac.Atoms.Count; i++)
            {
                Assert.IsNotNull(ac.Atoms[i].Point3D);
            }
            ModelBuilder3DTest.CheckAverageBondLength(ac);
        }
        /// <summary>
        /// Generates a fingerprint of the default size for the given
        /// AtomContainer, using path and ring metrics. It contains the
        /// informations from <see cref="Fingerprinter.GetBitFingerprint(IAtomContainer)"/> and bits which tell if the structure
        /// has 0 rings, 1 or less rings, 2 or less rings ... 10 or less rings and
        /// bits which tell if there is a fused ring system with 1,2...8 or more
        /// rings in it. The RingSet used is passed via rs parameter. This must be
        /// a smallesSetOfSmallestRings. The List must be a list of all ring
        /// systems in the molecule.
        /// </summary>
        /// <param name="atomContainer">The AtomContainer for which a Fingerprint is generated</param>
        /// <param name="ringSet">An SSSR RingSet of ac (if not available, use <see cref="GetBitFingerprint(IAtomContainer)"/>, which does the calculation)</param>
        /// <param name="rslist">A list of all ring systems in ac</param>
        /// <exception cref="CDKException">for example if input can not be cloned.</exception>
        /// <returns>a BitArray representing the fingerprint</returns>
        public IBitFingerprint GetBitFingerprint(IAtomContainer atomContainer, IRingSet ringSet, IEnumerable <IRingSet> rslist)
        {
            var container = (IAtomContainer)atomContainer.Clone();

            var fingerprint = fingerprinter.GetBitFingerprint(container);
            var size        = this.Length;
            var weight      = MolecularFormulaManipulator.GetTotalNaturalAbundance(MolecularFormulaManipulator.GetMolecularFormula(container));

            for (int i = 1; i < 11; i++)
            {
                if (weight > (100 * i))
                {
                    fingerprint.Set(size - 26 + i); // 26 := RESERVED_BITS+1
                }
            }
            if (ringSet == null)
            {
                ringSet = Cycles.FindSSSR(container).ToRingSet();
                rslist  = RingPartitioner.PartitionRings(ringSet);
            }
            for (int i = 0; i < 7; i++)
            {
                if (ringSet.Count > i)
                {
                    fingerprint.Set(size - 15 + i); // 15 := RESERVED_BITS+1+10 mass bits
                }
            }
            int maximumringsystemsize = 0;

            foreach (var rs in rslist)
            {
                if (rs.Count > maximumringsystemsize)
                {
                    maximumringsystemsize = rs.Count;
                }
            }
            for (int i = 0; i < maximumringsystemsize && i < 9; i++)
            {
                fingerprint.Set(size - 8 + i - 3);
            }
            return(fingerprint);
        }
        public void TestMapTemplatesCyclicMol2()
        {
            TemplateHandler3D  tmphandler3d = TemplateHandler3D.Instance;
            string             cyclicMolSmi = "CC(C)(C)NC(=O)C1CN(CCN1CC(CC(Cc1ccccc1)C(=O)NC1c2ccccc2CC1O)O)Cc1cccnc1";
            IChemObjectBuilder builder      = ChemObjectBuilder.Instance;
            SmilesParser       smiparser    = new SmilesParser(builder);
            var molecule = smiparser.ParseSmiles(cyclicMolSmi);
            ForceFieldConfigurator forcefconf = new ForceFieldConfigurator();

            forcefconf.SetForceFieldConfigurator("mmff94");
            IRingSet       rings                  = forcefconf.AssignAtomTyps(molecule);
            var            ringSystems            = RingPartitioner.PartitionRings(rings);
            IRingSet       largestRingSet         = RingSetManipulator.GetLargestRingSet(ringSystems);
            IAtomContainer allAtomsInOneContainer = RingSetManipulator.GetAllInOneContainer(largestRingSet);

            tmphandler3d.MapTemplates(allAtomsInOneContainer, allAtomsInOneContainer.Atoms.Count);
            for (int j = 0; j < allAtomsInOneContainer.Atoms.Count; j++)
            {
                Assert.IsNotNull(allAtomsInOneContainer.Atoms[j].Point3D);
            }
        }
        public static void ExtractUniqueRingSystemsFromFile(string dataFile)
        {
            Console.Out.WriteLine("****** EXTRACT UNIQUE RING SYSTEMS ******");
            Console.Out.WriteLine($"From file: {dataFile}");

            Dictionary <string, string> hashRingSystems = new Dictionary <string, string>();
            SmilesGenerator             smilesGenerator = new SmilesGenerator();

            int            counterRings       = 0;
            int            counterMolecules   = 0;
            int            counterUniqueRings = 0;
            IRingSet       ringSet            = null;
            string         key = "";
            IAtomContainer ac  = null;

            string molfile = dataFile + "_UniqueRings";

            try
            {
                using (var fout = new FileStream(molfile, FileMode.Create))
                    using (var mdlw = new MDLV2000Writer(fout))
                    {
                        try
                        {
                            Console.Out.WriteLine("Start...");
                            using (var fin = new StreamReader(dataFile))
                                using (var imdl = new EnumerableSDFReader(fin, builder))
                                {
                                    Console.Out.WriteLine("Read File in..");

                                    foreach (var m in imdl)
                                    {
                                        counterMolecules = counterMolecules + 1;

                                        IRingSet ringSetM = Cycles.FindSSSR(m).ToRingSet();

                                        if (counterMolecules % 1000 == 0)
                                        {
                                            Console.Out.WriteLine("Molecules:" + counterMolecules);
                                        }

                                        if (ringSetM.Count > 0)
                                        {
                                            var ringSystems = RingPartitioner.PartitionRings(ringSetM);

                                            for (int i = 0; i < ringSystems.Count; i++)
                                            {
                                                ringSet = (IRingSet)ringSystems[i];
                                                ac      = builder.NewAtomContainer();
                                                var containers = RingSetManipulator.GetAllAtomContainers(ringSet);
                                                foreach (var container in containers)
                                                {
                                                    ac.Add(container);
                                                }
                                                counterRings = counterRings + 1;
                                                // Only connection is important
                                                for (int j = 0; j < ac.Atoms.Count; j++)
                                                {
                                                    (ac.Atoms[j]).Symbol = "C";
                                                }

                                                try
                                                {
                                                    key = smilesGenerator.Create(builder.NewAtomContainer(ac));
                                                }
                                                catch (CDKException e)
                                                {
                                                    Trace.TraceError(e.Message);
                                                    return;
                                                }

                                                if (hashRingSystems.ContainsKey(key))
                                                {
                                                }
                                                else
                                                {
                                                    counterUniqueRings = counterUniqueRings + 1; hashRingSystems[key] = "1";
                                                    try
                                                    {
                                                        mdlw.Write(builder.NewAtomContainer(ac));
                                                    }
                                                    catch (Exception emdl)
                                                    {
                                                        if (!(emdl is ArgumentException || emdl is CDKException))
                                                        {
                                                            throw;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                        }
                        catch (Exception exc)
                        {
                            Console.Out.WriteLine($"Could not read Molecules from file {dataFile} due to: {exc.Message}");
                        }
                    }
            }
            catch (Exception ex2)
            {
                Console.Out.WriteLine($"IOError:cannot write file due to: {ex2.ToString()}");
            }
            Console.Out.WriteLine($"READY Molecules:{counterMolecules} RingSystems:{counterRings} UniqueRingsSystem:{counterUniqueRings}");
            Console.Out.WriteLine($"HashtableKeys:{hashRingSystems.Count}");
        }
Exemple #8
0
        /// <summary>
        /// Generate 3D coordinates with force field information.
        /// </summary>
        public IAtomContainer Generate3DCoordinates(IAtomContainer molecule, bool clone)
        {
            var originalAtomTypeNames = molecule.Atoms.Select(n => n.AtomTypeName).ToArray();

            Debug.WriteLine("******** GENERATE COORDINATES ********");
            foreach (var atom in molecule.Atoms)
            {
                atom.IsPlaced  = false;
                atom.IsVisited = false;
            }
            //CHECK FOR CONNECTIVITY!
            Debug.WriteLine($"#atoms>{molecule.Atoms.Count}");
            if (!ConnectivityChecker.IsConnected(molecule))
            {
                throw new CDKException("Molecule is NOT connected, could not layout.");
            }

            // setup helper classes
            AtomPlacer   atomPlacer = new AtomPlacer();
            AtomPlacer3D ap3d       = new AtomPlacer3D(parameterSet);
            AtomTetrahedralLigandPlacer3D atlp3d = new AtomTetrahedralLigandPlacer3D(parameterSet);

            if (clone)
            {
                molecule = (IAtomContainer)molecule.Clone();
            }
            atomPlacer.Molecule = molecule;

            if (ap3d.NumberOfUnplacedHeavyAtoms(molecule) == 1)
            {
                Debug.WriteLine("Only one Heavy Atom");
                ap3d.GetUnplacedHeavyAtom(molecule).Point3D = new Vector3(0.0, 0.0, 0.0);
                try
                {
                    atlp3d.Add3DCoordinatesForSinglyBondedLigands(molecule);
                }
                catch (CDKException ex3)
                {
                    Trace.TraceError($"PlaceSubstitutensERROR: Cannot place substitutents due to:{ex3.Message}");
                    Debug.WriteLine(ex3);
                    throw new CDKException("PlaceSubstitutensERROR: Cannot place substitutents due to:" + ex3.Message,
                                           ex3);
                }
                return(molecule);
            }
            //Assing Atoms to Rings,Aliphatic and Atomtype
            IRingSet ringSetMolecule   = ffc.AssignAtomTyps(molecule);
            IRingSet largestRingSet    = null;
            int      numberOfRingAtoms = 0;

            IReadOnlyList <IRingSet> ringSystems = null;

            if (ringSetMolecule.Count > 0)
            {
                if (templateHandler == null)
                {
                    throw new CDKException("You are trying to generate coordinates for a molecule with rings, but you have no template handler set. Please do SetTemplateHandler() before generation!");
                }
                ringSystems    = RingPartitioner.PartitionRings(ringSetMolecule);
                largestRingSet = RingSetManipulator.GetLargestRingSet(ringSystems);
                IAtomContainer largestRingSetContainer = RingSetManipulator.GetAllInOneContainer(largestRingSet);
                numberOfRingAtoms = largestRingSetContainer.Atoms.Count;
                templateHandler.MapTemplates(largestRingSetContainer, numberOfRingAtoms);
                if (!CheckAllRingAtomsHasCoordinates(largestRingSetContainer))
                {
                    throw new CDKException("RingAtomLayoutError: Not every ring atom is placed! Molecule cannot be layout.");
                }

                SetAtomsToPlace(largestRingSetContainer);
                SearchAndPlaceBranches(molecule, largestRingSetContainer, ap3d, atlp3d, atomPlacer);
                largestRingSet = null;
            }
            else
            {
                //Debug.WriteLine("****** Start of handling aliphatic molecule ******");
                IAtomContainer ac = AtomPlacer.GetInitialLongestChain(molecule);
                SetAtomsToUnVisited(molecule);
                SetAtomsToUnplaced(molecule);
                ap3d.PlaceAliphaticHeavyChain(molecule, ac);
                //ZMatrixApproach
                ap3d.ZMatrixChainToCartesian(molecule, false);
                SearchAndPlaceBranches(molecule, ac, ap3d, atlp3d, atomPlacer);
            }
            LayoutMolecule(ringSystems, molecule, ap3d, atlp3d, atomPlacer);
            //Debug.WriteLine("******* PLACE SUBSTITUENTS ******");
            try
            {
                atlp3d.Add3DCoordinatesForSinglyBondedLigands(molecule);
            }
            catch (CDKException ex3)
            {
                Trace.TraceError($"PlaceSubstitutensERROR: Cannot place substitutents due to:{ex3.Message}");
                Debug.WriteLine(ex3);
                throw new CDKException("PlaceSubstitutensERROR: Cannot place substitutents due to:" + ex3.Message, ex3);
            }
            // restore the original atom type names
            for (int i = 0; i < originalAtomTypeNames.Length; i++)
            {
                molecule.Atoms[i].AtomTypeName = originalAtomTypeNames[i];
            }

            return(molecule);
        }