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); }
/// <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); } } } }
/// <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; }