public void TestGetBondCount() { IAtomContainer mol = TestMoleculeFactory.MakeAdenine(); AllRingsFinder arf = new AllRingsFinder(); IRingSet ringSet = arf.FindAllRings(mol); Assert.AreEqual(3, ringSet.Count); Assert.AreEqual(20, RingSetManipulator.GetBondCount(ringSet)); mol = TestMoleculeFactory.MakeBiphenyl(); ringSet = arf.FindAllRings(mol); Assert.AreEqual(2, ringSet.Count); Assert.AreEqual(12, RingSetManipulator.GetBondCount(ringSet)); }
public static void Main() { { IChemObjectSet <IAtomContainer> ms = null; #region AllRingsFinder arf = new AllRingsFinder(); foreach (var m in ms) { try { IRingSet rs = arf.FindAllRings(m); } catch (CDKException) { // molecule was too complex, handle error } } #endregion } { #region UsingThreshold // using static NCDK.RingSearches.AllRingsFinder.Threshold; AllRingsFinder arf = AllRingsFinder.UsingThreshold(Threshold.PubChem99); #endregion } }
/// <summary> /// Set the active center for this molecule. /// The active center will be those which correspond to a ring /// with pi electrons with resonance. /// </summary> /// FIXME REACT: It could be possible that a ring is a super ring of others small rings /// <param name="reactant">The molecule to set the activity</param> private static void SetActiveCenters(IAtomContainer reactant) { var arf = new AllRingsFinder(); var ringSet = arf.FindAllRings(reactant); for (int ir = 0; ir < ringSet.Count; ir++) { var ring = ringSet[ir]; //only rings with even number of atoms int nrAtoms = ring.Atoms.Count; if (nrAtoms % 2 == 0) { int nrSingleBonds = 0; foreach (var bond in ring.Bonds) { if (bond.Order == BondOrder.Single) { nrSingleBonds++; } } //if exactly half (nrAtoms/2==nrSingleBonds) if (nrSingleBonds != 0 && nrAtoms / 2 == nrSingleBonds) { foreach (var bond in ring.Bonds) { bond.IsReactiveCenter = true; } } } } }
/// <summary> /// Determines if, according to the algorithms implemented in this class, the given /// AtomContainer has properly distributed double bonds. /// </summary> /// <param name="m"><see cref="IAtomContainer"/> to check the bond orders for.</param> /// <returns>true, if bond orders are properly distributed</returns> /// <exception cref="CDKException">thrown when something went wrong</exception> public bool IsOK(IAtomContainer m) { // OK, we take advantage here from the fact that this class does not take // into account rings larger than 7 atoms. See FixAromaticBondOrders(). var rs = allRingsFinder.FindAllRings(m, 7); StoreRingSystem(m, rs); var StructureOK = IsStructureOK(m); var irs = RemoveExtraRings(m); if (irs == null) { throw new CDKException("error in AllRingsFinder.findAllRings"); } var count = GetBadCount(m, irs); return(StructureOK && count == 0); }
/// <summary> /// Fixes Aromaticity of the molecule /// i.e. need to find rings and aromaticity again since added H's /// <param name="mol"></param> /// </summary> public static void Configure(IAtomContainer mol) { // need to find rings and aromaticity again since added H's IRingSet ringSet = null; try { var arf = new AllRingsFinder(); ringSet = arf.FindAllRings(mol); } catch (Exception e) { Console.Out.WriteLine(e.StackTrace); } try { // figure out which atoms are in aromatic rings: var cdk = CDK.HydrogenAdder; ExtAtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol); cdk.AddImplicitHydrogens(mol); Aromaticity.CDKLegacy.Apply(mol); // figure out which rings are aromatic: RingSetManipulator.MarkAromaticRings(ringSet); // figure out which simple (non cycles) rings are aromatic: // only atoms in 6 membered rings are aromatic // determine largest ring that each atom is a part of foreach (var atom in mol.Atoms) { atom.IsAromatic = false; foreach (var ring in ringSet) { if (!ring.IsAromatic) { continue; } bool haveatom = ring.Contains(atom); //Debug.WriteLine("haveatom="+haveatom); if (haveatom && ring.Atoms.Count == 6) { atom.IsAromatic = true; } } } } catch (Exception e) { Console.Out.WriteLine(e.StackTrace); } }
public void TestGetLargestRingSetListIRingSet() { var list = new List <IRingSet> { ringset }; var mol = TestMoleculeFactory.MakeBiphenyl(); var arf = new AllRingsFinder(); var ringSet = arf.FindAllRings(mol); list.Add(ringSet); Assert.AreEqual(2, RingSetManipulator.GetLargestRingSet(list).Count); }
private static IRingSet GetRings(IAtomContainer mol) { try { var arf = new AllRingsFinder(); var rs = arf.FindAllRings(mol); return(rs); } catch (Exception e) { Console.Out.WriteLine($"Could not find all rings: {e.Message}"); } return(null); }
IRingSet GetRings() { IRingSet rs = null; try { AllRingsFinder arf = new AllRingsFinder(); rs = arf.FindAllRings(mol); } catch (Exception e) { Console.Out.WriteLine("Could not find all rings: " + e.Message); } return(rs); }
public void MarkAromatic() { IAtomContainer mol = TestMoleculeFactory.MakeBiphenyl(); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol); Aromaticity.CDKLegacy.Apply(mol); AllRingsFinder arf = new AllRingsFinder(); IRingSet ringSet = arf.FindAllRings(mol); Assert.AreEqual(2, ringSet.Count); RingSetManipulator.MarkAromaticRings(ringSet); for (int i = 0; i < ringSet.Count; i++) { IRing ring = (IRing)ringSet[i]; Assert.IsTrue(ring.IsAromatic); } }
public static void FixAromaticityForXLogP(IAtomContainer m) { // need to find rings and aromaticity again since added H's IRingSet rs = null; try { AllRingsFinder arf = new AllRingsFinder(); rs = arf.FindAllRings(m); // SSSRFinder s = new SSSRFinder(m); // srs = s.FindEssentialRings(); } catch (Exception e) { Console.Out.WriteLine(e.StackTrace); } try { // figure out which atoms are in aromatic rings: AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(m); Aromaticity.CDKLegacy.Apply(m); // figure out which rings are aromatic: RingSetManipulator.MarkAromaticRings(rs); // figure out which simple (non cycles) rings are aromatic: // HueckelAromaticityDetector.DetectAromaticity(m, srs); } catch (Exception e) { Console.Out.WriteLine(e.StackTrace); } // only atoms in 6 membered rings are aromatic // determine largest ring that each atom is a part of for (int i = 0; i <= m.Atoms.Count - 1; i++) { m.Atoms[i].IsAromatic = false; for (int j = 0; j <= rs.Count - 1; j++) { //Debug.WriteLine(i+"\t"+j); IRing r = (IRing)rs[j]; if (!r.IsAromatic) { goto continue_jloop; } bool haveatom = r.Contains(m.Atoms[i]); //Debug.WriteLine("haveatom="+haveatom); if (haveatom && r.Atoms.Count == 6) { m.Atoms[i].IsAromatic = true; } continue_jloop: ; } } }
internal IReactionSet Initiate(IChemObjectSet <IAtomContainer> reactants, IChemObjectSet <IAtomContainer> agents, int length, bool checkPrev, AtomCheck atomCheck) { CheckInitiateParams(reactants, agents); IReactionSet setOfReactions = reactants.Builder.NewReactionSet(); IAtomContainer reactant = reactants[0]; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactant); Aromaticity.CDKLegacy.Apply(reactant); AllRingsFinder arf = new AllRingsFinder(); IRingSet ringSet = arf.FindAllRings(reactant); for (int ir = 0; ir < ringSet.Count; ir++) { IRing ring = (IRing)ringSet[ir]; for (int jr = 0; jr < ring.Atoms.Count; jr++) { IAtom aring = ring.Atoms[jr]; aring.IsInRing = true; } } // if the parameter hasActiveCenter is not fixed yet, set the active centers IParameterReaction ipr = base.GetParameterClass(typeof(SetReactionCenter)); if (ipr != null && !ipr.IsSetParameter) { SetActiveCenters(reactant, length, checkPrev, atomCheck); } HOSECodeGenerator hcg = new HOSECodeGenerator(); foreach (var atomi in reactant.Atoms) { if (atomi.IsReactiveCenter && reactant.GetConnectedSingleElectrons(atomi).Count() == 1) { IEnumerable <IAtom> atom1s = null; if (checkPrev) { hcg.GetSpheres(reactant, atomi, length - 1, true); atom1s = hcg.GetNodesInSphere(length - 1); } hcg.GetSpheres(reactant, atomi, length, true); foreach (var atoml in hcg.GetNodesInSphere(length)) { if (atoml != null && atoml.IsReactiveCenter && !atoml.IsInRing && (atoml.FormalCharge ?? 0) == 0 && !atoml.AtomicNumber.Equals(AtomicNumbers.H) && reactant.GetMaximumBondOrder(atoml) == BondOrder.Single) { foreach (var atomR in reactant.GetConnectedAtoms(atoml)) { if (atom1s != null && atom1s.Contains(atomR)) { continue; } if (reactant.GetBond(atomR, atoml).IsReactiveCenter && atomR.IsReactiveCenter && atomCheck(atomR)) { var atomList = new List <IAtom> { atomR, atomi, atoml }; var bondList = new List <IBond> { reactant.GetBond(atomR, atoml) }; var moleculeSet = reactant.Builder.NewChemObjectSet <IAtomContainer>(); moleculeSet.Add(reactant); var reaction = Mechanism.Initiate(moleculeSet, atomList, bondList); if (reaction == null) { continue; } else { setOfReactions.Add(reaction); } } } } } } } return(setOfReactions); }
/// <summary> /// This function finds rings and uses aromaticity detection code to /// aromatize the molecule. /// </summary> /// <param name="mol">input molecule</param> public static void AromatizeMolecule(IAtomContainer mol) { // need to find rings and aromaticity again since added H's IRingSet ringSet = null; try { AllRingsFinder arf = new AllRingsFinder(); ringSet = arf.FindAllRings(mol); // SSSRFinder s = new SSSRFinder(atomContainer); // srs = s.FindEssentialRings(); } catch (Exception e) { Console.Out.WriteLine(e.StackTrace); } try { // figure out which atoms are in aromatic rings: // PrintAtoms(atomContainer); ExtAtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol); // PrintAtoms(atomContainer); Aromaticity.CDKLegacy.Apply(mol); // PrintAtoms(atomContainer); // figure out which rings are aromatic: RingSetManipulator.MarkAromaticRings(ringSet); // PrintAtoms(atomContainer); // figure out which simple (non cycles) rings are aromatic: // HueckelAromaticityDetector.DetectAromaticity(atomContainer, srs); } catch (Exception e) { Console.Out.WriteLine(e.StackTrace); } // only atoms in 6 membered rings are aromatic // determine largest ring that each atom is atom part of for (int i = 0; i <= mol.Atoms.Count - 1; i++) { mol.Atoms[i].IsAromatic = false; foreach (var ring in ringSet) { if (!ring.IsAromatic) { continue; } bool haveatom = ring.Contains(mol.Atoms[i]); //Debug.WriteLine("haveatom="+haveatom); if (haveatom && ring.Atoms.Count == 6) { mol.Atoms[i].IsAromatic = true; } } } }
/// <summary> /// Initiate process. /// It is needed to call the addExplicitHydrogensToSatisfyValency /// from the class tools.HydrogenAdder. /// </summary> /// <exception cref="CDKException"> Description of the Exception</exception> /// <param name="reactants">reactants of the reaction.</param> /// <param name="agents">agents of the reaction (Must be in this case null).</param> public IReactionSet Initiate(IChemObjectSet <IAtomContainer> reactants, IChemObjectSet <IAtomContainer> agents) { CheckInitiateParams(reactants, agents); var setOfReactions = reactants.Builder.NewReactionSet(); var reactant = reactants[0]; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactant); // if the parameter hasActiveCenter is not fixed yet, set the active centers var ipr = base.GetParameterClass(typeof(SetReactionCenter)); if (ipr != null && !ipr.IsSetParameter) { SetActiveCenters(reactant); } var arf = new AllRingsFinder(); var ringSet = arf.FindAllRings((IAtomContainer)reactant); for (int ir = 0; ir < ringSet.Count; ir++) { var ring = ringSet[ir]; //only rings with even number of atoms int nrAtoms = ring.Atoms.Count; if (nrAtoms % 2 == 0) { int nrSingleBonds = 0; foreach (var bond in ring.Bonds) { if (bond.Order == BondOrder.Single) { nrSingleBonds++; } } //if exactly half (nrAtoms/2==nrSingleBonds) if (nrSingleBonds != 0 && nrAtoms / 2 == nrSingleBonds) { bool ringCompletActive = false; foreach (var bond in ring.Bonds) { if (bond.IsReactiveCenter) { ringCompletActive = true; } else { ringCompletActive = false; break; } } if (!ringCompletActive) { continue; } IReaction reaction = reactants.Builder.NewReaction(); reaction.Reactants.Add(reactant); IAtomContainer reactantCloned; reactantCloned = (IAtomContainer)reactant.Clone(); foreach (var bondi in ring.Bonds) { int bondiP = reactant.Bonds.IndexOf(bondi); if (bondi.Order == BondOrder.Single) { BondManipulator.IncreaseBondOrder(reactantCloned.Bonds[bondiP]); } else { BondManipulator.DecreaseBondOrder(reactantCloned.Bonds[bondiP]); } } reaction.Products.Add(reactantCloned); setOfReactions.Add(reaction); } } } return(setOfReactions); }