Example #1
0
        public void TestSplit()
        {
            var          mol       = smilesParser.ParseSmiles("C1CC1C2CCC2");
            SpanningTree st        = new SpanningTree(mol);
            IRingSet     rings     = st.GetAllRings();
            IBond        splitBond = null;

            for (int i = 0; i < mol.Bonds.Count; i++)
            {
                if (rings.GetRings(mol.Bonds[i]).Count() == 0)
                {
                    splitBond = mol.Bonds[i];
                    break;
                }
            }
            var             frags       = FragmentUtils.SplitMolecule(mol, splitBond);
            SmilesGenerator sg          = new SmilesGenerator();
            var             uniqueFrags = new HashSet <string>();

            foreach (var frag in frags)
            {
                uniqueFrags.Add(sg.Create(frag));
            }
            Assert.AreEqual(2, uniqueFrags.Count);
            // You can put the fragments back together with a ring closure and dot
            // [CH]12CC1.[CH]12CCC1
            Assert.IsTrue(uniqueFrags.IsSupersetOf(new[] { "[CH]1CC1", "[CH]1CCC1" }));
        }
Example #2
0
        private List <IAtomContainer> Run(IAtomContainer atomContainer)
        {
            var fragments = new List <IAtomContainer>();

            if (atomContainer.Bonds.Count < 3)
            {
                return(fragments);
            }
            var splitableBonds = GetSplitableBonds(atomContainer);

            if (splitableBonds.Count == 0)
            {
                return(fragments);
            }
            Debug.WriteLine("Got " + splitableBonds.Count + " splittable bonds");

            string tmpSmiles;

            foreach (var bond in splitableBonds)
            {
                var parts = FragmentUtils.SplitMolecule(atomContainer, bond);
                // make sure we don't add the same fragment twice
                foreach (var partContainer in parts)
                {
                    AtomContainerManipulator.ClearAtomConfigurations(partContainer);
                    foreach (var atom in partContainer.Atoms)
                    {
                        atom.ImplicitHydrogenCount = null;
                    }
                    AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(partContainer);
                    CDK.HydrogenAdder.AddImplicitHydrogens(partContainer);
                    Aromaticity.CDKLegacy.Apply(partContainer);
                    tmpSmiles = smilesGenerator.Create(partContainer);
                    if (partContainer.Atoms.Count >= MinimumFragmentSize && !fragMap.ContainsKey(tmpSmiles))
                    {
                        fragments.Add(partContainer);
                        fragMap[tmpSmiles] = partContainer;
                    }
                }
            }

            // try and partition the fragments
            var tmp = new List <IAtomContainer>(fragments);

            foreach (var fragment in fragments)
            {
                if (fragment.Bonds.Count < 3 || fragment.Atoms.Count < MinimumFragmentSize)
                {
                    continue;
                }
                if (GetSplitableBonds(fragment).Count == 0)
                {
                    continue;
                }

                var frags = Run(fragment);
                if (frags.Count == 0)
                {
                    continue;
                }

                foreach (var frag in frags)
                {
                    if (frag.Bonds.Count < 3)
                    {
                        continue;
                    }
                    AtomContainerManipulator.ClearAtomConfigurations(frag);
                    foreach (var atom in frag.Atoms)
                    {
                        atom.ImplicitHydrogenCount = null;
                    }
                    AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(frag);
                    CDK.HydrogenAdder.AddImplicitHydrogens(frag);
                    Aromaticity.CDKLegacy.Apply(frag);
                    tmpSmiles = smilesGenerator.Create(frag);
                    if (frag.Atoms.Count >= MinimumFragmentSize && !fragMap.ContainsKey(tmpSmiles))
                    {
                        tmp.Add(frag);
                        fragMap[tmpSmiles] = frag;
                    }
                }
            }
            fragments = new List <IAtomContainer>(tmp);
            return(fragments);
        }