示例#1
0
 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;
                        }
                    }
                }
            }
        }
示例#3
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);
            }
        }
示例#4
0
        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);
        }
示例#5
0
 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);
 }
示例#6
0
        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));
        }
        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);
        }
		private bool preprocessMolecule(IAtomContainer original)
		{
			//prepare atom weights
			var atomWeightCanBeCalculated = prepareAtomWeights(original);
			// If the SDF contains an "R" or something, we don't know its mass, so this whole endevour is fruitless
			// (since we won't be able to match the fragment masses to the peaks).
			if (!atomWeightCanBeCalculated)
			{
				return false;
			}

			//mark all the bonds and atoms with numbers --> identify them later on        
			originalMolecule = markAllBonds(original);

			//do ring detection with the original molecule
			var allRingsFinder = new AllRingsFinder();

			// Steve: Set a really large timeout, because we don't want to crash just because it took a long time.
			// The size limit of 7 below should stop it looping forever.
			allRingsFinder.setTimeout(int.MaxValue);
			// TODO: Steve: The 7 is a max ring size - I added this to prevent it getting in to infinite loops (7 comes from MetFrag
			// where it is used in some other random class). Don't know if we need to change this??
			allRings = allRingsFinder.findAllRings(originalMolecule, Integer.valueOf(7));
			aromaticBonds = new List<IBond>();

			CDKHueckelAromaticityDetector.detectAromaticity(originalMolecule);

			foreach (var bond in originalMolecule.bonds().ToWindowsEnumerable<IBond>())
			{
				//lets see if it is a ring and aromatic
				var rings = allRings.getRings(bond);
				//don't split up aromatic rings...see constructor for option
				for (var i = 0; i < rings.getAtomContainerCount(); i++)
				{
					var aromatic = AromaticityCalculator.isAromatic((IRing)rings.getAtomContainer(i), originalMolecule);
					if (aromatic)
					{
						aromaticBonds.Add(bond);
						break;
					}
				}
			}
			return true;
		}
示例#9
0
        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);
            }
        }
示例#10
0
        /// <summary>
        /// Generates a fingerprint of the default size for the given AtomContainer.
        /// </summary>
        /// <param name="container">The AtomContainer for which a Fingerprint is generated</param>
        /// <param name="ringFinder">An instance of <see cref="AllRingsFinder"/></param>
        /// <exception cref="CDKException">if there is a timeout in ring or aromaticity perception</exception>
        /// <returns>A <see cref="BitArray"/> representing the fingerprint</returns>
        public IBitFingerprint GetBitFingerprint(IAtomContainer container, AllRingsFinder ringFinder)
        {
            Debug.WriteLine("Entering Fingerprinter");
            Debug.WriteLine("Starting Aromaticity Detection");
            var before = DateTime.Now.Ticks;

            if (!HasPseudoAtom(container.Atoms))
            {
                AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(container);
                Aromaticity.CDKLegacy.Apply(container);
            }
            var after = DateTime.Now.Ticks;

            Debug.WriteLine($"time for aromaticity calculation: {after - before} ticks");
            Debug.WriteLine("Finished Aromaticity Detection");
            BitArray bitSet = new BitArray(size);

            EncodePaths(container, SearchDepth, bitSet, size);

            return(new BitSetFingerprint(bitSet));
        }
        /// <summary>  Retrieves the set of all rings and performs an aromaticity detection based
        /// on Hueckels 4n + 2 rule. An AllRingsFinder with customized timeout may be
        /// assigned to this method.
        /// </summary>
        /// <param name="removeAromatictyFlags"> When true, we leaves ChemObjects that
        /// are already marked as aromatic as they are
        /// </param>
        /// <param name="atomContainer">         AtomContainer to be searched for
        /// </param>
        /// <param name="arf">                   AllRingsFinder to be employed for the
        /// ringsearch. Use this to customize the
        /// AllRingsFinder timeout feature
        /// rings
        /// </param>
        /// <returns>			True, if molecule has aromatic features
        /// </returns>
        /// <exception cref="CDKException">     Thrown in case of errors or an
        /// AllRingsFinder timeout
        /// </exception>
        public static bool detectAromaticity(IAtomContainer atomContainer, bool removeAromatictyFlags, AllRingsFinder arf)
        {
            //logger.debug("Entered Aromaticity Detection");
            //logger.debug("Starting AllRingsFinder");
            long before = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            if (arf == null)
            {
                arf = new AllRingsFinder();
                arf.setTimeout(timeout);
            }
            ringSet = arf.findAllRings(atomContainer);
            long after = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            //logger.debug("time for finding all rings: " + (after - before) + " milliseconds");
            //logger.debug("Finished AllRingsFinder");
            if (ringSet.AtomContainerCount > 0)
            {
                return(detectAromaticity(atomContainer, ringSet, removeAromatictyFlags));
            }
            return(false);
        }
示例#12
0
        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>
        /// 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);
        }
示例#15
0
 /// <summary>
 /// Constructor for the DeduceBondSystemTool object.
 /// </summary>
 public DeduceBondSystemTool()
 {
     allRingsFinder = new AllRingsFinder();
 }
示例#16
0
 /// <summary>
 /// Constructor for the DeduceBondSystemTool object accepting a custom <see cref="AllRingsFinder"/>.
 /// </summary>
 /// <param name="ringFinder">a custom <see cref="AllRingsFinder"/>.</param>
 public DeduceBondSystemTool(AllRingsFinder ringFinder)
 {
     allRingsFinder = ringFinder;
 }
示例#17
0
        /// <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;
                    }
                }
            }
        }