public void TestRemoveHydrogensAndPreserveAtomID()
        {
            string rawMolSmiles  = "[H]POOSC(Br)C(Cl)C(F)I";
            var    sp            = CDK.SmilesParser;
            var    atomContainer = sp.ParseSmiles(rawMolSmiles);
            IAtom  beforeAtom    = null;
            IAtom  afterAtom     = null;

            foreach (var a in atomContainer.Atoms)
            {
                if (string.Equals(a.Symbol, "P", StringComparison.OrdinalIgnoreCase))
                {
                    beforeAtom = a;
                    a.Id       = "TEST";
                    break;
                }
            }
            IAtomContainer result = ExtAtomContainerManipulator.RemoveHydrogensExceptSingleAndPreserveAtomID(atomContainer);

            foreach (var a in result.Atoms)
            {
                if (string.Equals(a.Symbol, "P", StringComparison.OrdinalIgnoreCase))
                {
                    afterAtom = a;
                    break;
                }
            }

            Assert.AreEqual(afterAtom.Id, beforeAtom.Id);
        }
Beispiel #2
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);
        }
        public void TestConvertExplicitToImplicitHydrogens()
        {
            string         rawMolSmiles  = "[H]POOSC(Br)C(Cl)C(F)I";
            var            sp            = CDK.SmilesParser;
            var            atomContainer = sp.ParseSmiles(rawMolSmiles);
            int            expResult     = 11;
            IAtomContainer result        = ExtAtomContainerManipulator.ConvertExplicitToImplicitHydrogens(atomContainer);

            Assert.AreEqual(expResult, result.Atoms.Count);
        }
Beispiel #5
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);
            }
        }
Beispiel #6
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;
        }
        public void TestAromatizeMolecule()
        {
            string rawMolSmiles = "C1=CC2=C(C=C1)C=CC=C2";
            var    sp           = CDK.SmilesParser;
            var    mol          = sp.ParseSmiles(rawMolSmiles);

            ExtAtomContainerManipulator.AromatizeMolecule(mol);
            int count = 0;

            foreach (var b in mol.Bonds)
            {
                if (b.IsAromatic && b.Order.Equals(BondOrder.Double))
                {
                    count++;
                }
            }
            Assert.AreEqual(5, count);
        }
        public void TestGetHydrogenCount()
        {
            string rawMolSmiles  = "[H]POOSC(Br)C(Cl)C(F)I";
            var    sp            = CDK.SmilesParser;
            var    atomContainer = sp.ParseSmiles(rawMolSmiles);
            IAtom  atom          = null;

            foreach (var a in atomContainer.Atoms)
            {
                if (string.Equals(a.Symbol, "P", StringComparison.OrdinalIgnoreCase))
                {
                    atom = a;
                    break;
                }
            }
            int expResult = 2;
            int result    = ExtAtomContainerManipulator.GetHydrogenCount(atomContainer, atom);

            Assert.AreEqual(expResult, result);
        }
        public void TestMakeDeepCopy()
        {
            string rawMolSmiles = "[H]POOSC(Br)C(Cl)C(F)I";
            var    sp           = CDK.SmilesParser;
            var    container    = sp.ParseSmiles(rawMolSmiles);

            int counter = 0;

            foreach (var a in container.Atoms)
            {
                a.Id = (counter++).ToString();
            }

            IAtomContainer result = ExtAtomContainerManipulator.MakeDeepCopy(container);

            for (int i = 0; i < result.Atoms.Count; i++)
            {
                Assert.AreEqual(result.Atoms[i].Symbol, container.Atoms[i].Symbol);
                Assert.AreEqual(result.Atoms[i].Id, container.Atoms[i].Id);
            }
        }
Beispiel #10
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;
                    }
                }
            }
        }