Exemple #1
0
        /// <summary>
        /// Creates a new instance of MolHandler
        /// </summary>
        /// <param name="molFile">atomContainer file name</param>
        public MolHandler(string molFile, bool removeHydrogen, bool cleanMolecule)
        {
            MDLReader molRead = null;

            this.removeHydrogen = removeHydrogen;
            try
            {
                Stream readMolecule = null;

                readMolecule       = new FileStream(molFile, FileMode.Open, FileAccess.Read);
                molRead            = new MDLReader(new StreamReader(readMolecule));
                this.atomContainer = (IAtomContainer)molRead.Read(new AtomContainer());
                molRead.Close();
                readMolecule.Close();
                /* Remove Hydrogen by Asad */
                if (removeHydrogen)
                {
                    atomContainer = ExtAtomContainerManipulator.RemoveHydrogensExceptSingleAndPreserveAtomID(atomContainer);
                }
                if (cleanMolecule)
                {
                    if (!IsPseudoAtoms())
                    {
                        atomContainer = canonLabeler.GetCanonicalMolecule(atomContainer);
                    }
                    // percieve atoms, set valency etc
                    ExtAtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(atomContainer);
                    //Add implicit Hydrogens
                    var adder = CDK.HydrogenAdder;
                    adder.AddImplicitHydrogens(atomContainer);
                    // figure out which atoms are in aromatic rings:
                    Aromaticity.CDKLegacy.Apply(atomContainer);
                    BondTools.MakeUpDownBonds(atomContainer);
                }
            }
            catch (IOException ex)
            {
                Debug.WriteLine(ex);
            }
            catch (CDKException e)
            {
                Console.Error.WriteLine(e);
            }
            finally
            {
                if (molRead != null)
                {
                    try
                    {
                        molRead.Close();
                    }
                    catch (IOException ioe)
                    {
                        Trace.TraceWarning("Couldn't close molReader: ", ioe.Message);
                        Debug.WriteLine(ioe);
                    }
                }
            }
        }
        public void TestPercieveAtomTypesAndConfigureAtoms()
        {
            string rawMolSmiles  = "[H]POOSC(Br)C(Cl)C(F)I";
            var    sp            = CDK.SmilesParser;
            var    atomContainer = sp.ParseSmiles(rawMolSmiles);

            ExtAtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(atomContainer);
            Assert.IsNotNull(atomContainer);
        }
        /// <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);
            }
        }
Exemple #4
0
        /// <summary>
        /// Creates a new instance of MolHandler
        /// <param name="container">Molecule AtomContainer</param>
        /// </summary>
        public MolHandler(IAtomContainer container, bool removeHydrogen, bool cleanMolecule)
        {
            string molID = container.Id;

            this.removeHydrogen = removeHydrogen;
            this.atomContainer  = container;
            if (removeHydrogen)
            {
                try
                {
                    this.atomContainer = ExtAtomContainerManipulator
                                         .RemoveHydrogensExceptSingleAndPreserveAtomID(atomContainer);
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.Message);
                }
            }
            else
            {
                this.atomContainer = container.Builder.NewAtomContainer(atomContainer);
            }

            if (cleanMolecule)
            {
                try
                {
                    if (!IsPseudoAtoms())
                    {
                        atomContainer = canonLabeler.GetCanonicalMolecule(atomContainer);
                    }
                    // percieve atoms, set valency etc
                    ExtAtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(atomContainer);
                    //Add implicit Hydrogens
                    var adder = CDK.HydrogenAdder;
                    adder.AddImplicitHydrogens(atomContainer);
                    // figure out which atoms are in aromatic rings:
                    Aromaticity.CDKLegacy.Apply(atomContainer);
                }
                catch (CDKException ex)
                {
                    Trace.TraceError(ex.Message);
                }
            }
            atomContainer.Id = molID;
        }
Exemple #5
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;
                    }
                }
            }
        }