public void TestMakeAtomContainer() { var builder = CDK.Builder; IAtom atom = builder.NewAtom("C"); IAtom exclude = builder.NewAtom("C"); IAtom a1 = builder.NewAtom("C"); IAtom a2 = builder.NewAtom("C"); IBond[] bonds = new IBond[] { builder.NewBond(atom, exclude), builder.NewBond(a1, a2), builder.NewBond(a1, atom), builder.NewBond(a2, exclude) }; IAtomContainer part = FragmentUtils.MakeAtomContainer(atom, bonds, exclude); Assert.AreEqual(3, part.Atoms.Count); Assert.AreEqual(2, part.Bonds.Count); Assert.IsTrue(part.Contains(atom)); Assert.IsTrue(part.Contains(a1)); Assert.IsTrue(part.Contains(a2)); Assert.IsFalse(part.Contains(exclude)); Assert.IsTrue(part.Contains(bonds[1])); Assert.IsTrue(part.Contains(bonds[2])); }
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" })); }
public void TestTraversal_Chain() { var builder = CDK.Builder; IAtom[] atoms = new IAtom[] { builder.NewAtom("C"), builder.NewAtom("C"), builder.NewAtom("C"), builder.NewAtom("C"), builder.NewAtom("C"), builder.NewAtom("C") }; IBond[] bonds = new IBond[] { builder.NewBond(atoms[0], atoms[1]), builder.NewBond(atoms[1], atoms[2]), builder.NewBond(atoms[2], atoms[3]), builder.NewBond(atoms[3], atoms[4]), builder.NewBond(atoms[4], atoms[5]) }; IAtomContainer m = builder.NewAtomContainer(); m.SetAtoms(atoms); m.SetBonds(bonds); List <IBond> accumulator = new List <IBond>(); // traverse from one end FragmentUtils.Traverse(m, atoms[0], accumulator); Assert.AreEqual(5, accumulator.Count); Assert.AreEqual(bonds[0], accumulator[0]); Assert.AreEqual(bonds[1], accumulator[1]); Assert.AreEqual(bonds[2], accumulator[2]); Assert.AreEqual(bonds[3], accumulator[3]); Assert.AreEqual(bonds[4], accumulator[4]); // traverse from the middle accumulator.Clear(); FragmentUtils.Traverse(m, atoms[3], accumulator); Assert.AreEqual(5, accumulator.Count); Assert.AreEqual(bonds[2], accumulator[0]); Assert.AreEqual(bonds[1], accumulator[1]); Assert.AreEqual(bonds[0], accumulator[2]); Assert.AreEqual(bonds[3], accumulator[3]); Assert.AreEqual(bonds[4], accumulator[4]); }
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); }