SMSDNormalizer extends AtomContainerManipulator { public static IAtomContainer removeHydrogensAndPreserveAtomID(IAtomContainer atomContainer) { Map<IAtom, IAtom> map = new HashMap<IAtom, IAtom>(); List<IAtom> remove = new ArrayList<IAtom>(); IAtomContainer mol = null; if (atomContainer.getBondCount() > 0) { mol = atomContainer.getBuilder().newInstance(IAtomContainer.class); int count = atomContainer.getAtomCount(); for (int i = 0; i < count; i++) { IAtom atom = atomContainer.getAtom(i); if (!atom.getSymbol().equals("H")) { IAtom clonedAtom = null; try { clonedAtom = (IAtom) atom.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } clonedAtom.setID(atom.getID()); clonedAtom.setFlags(atom.getFlags()); int countH = 0; if (atom.getImplicitHydrogenCount() != null) { countH = atom.getImplicitHydrogenCount(); } clonedAtom.setImplicitHydrogenCount(countH); mol.addAtom(clonedAtom); map.put(atom, clonedAtom); } else { remove.add(atom); } } mol = cloneAndMarkNonHBonds(mol, atomContainer, remove, map); mol = reComputeHydrogens(mol, atomContainer, remove, map); } else { mol = atomContainer.getBuilder().newInstance(IAtomContainer.class, atomContainer); mol.addProperties(atomContainer.getProperties()); mol.setFlags(atomContainer.getFlags()); if (atomContainer.getID() != null) { mol.setID(atomContainer.getID()); } if (atomContainer.getAtom(0).getSymbol().equalsIgnoreCase("H")) { System.err.println("WARNING: single hydrogen atom removal not supported!"); } } return mol; }  static IAtomContainer makeDeepCopy(IAtomContainer container); static void aromatizeMolecule(IAtomContainer mol); static int getExplicitHydrogenCount(IAtomContainer atomContainer, IAtom atom); static int getImplicitHydrogenCount(IAtomContainer atomContainer, IAtom atom); static int getHydrogenCount(IAtomContainer atomContainer, IAtom atom); static IAtomContainer removeHydrogensAndPreserveAtomID(IAtomContainer atomContainer); static IAtomContainer convertExplicitToImplicitHydrogens(IAtomContainer atomContainer); static void percieveAtomTypesAndConfigureAtoms(IAtomContainer container);  }
@Test public void testRemoveHydrogensAndPreserveAtomID() throws InvalidSmilesException { String rawMolSmiles = "[H]POOSC(Br)C(Cl)C(F)I"; SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance()); IAtomContainer atomContainer = sp.parseSmiles(rawMolSmiles); IAtom beforeAtom = null; IAtom afterAtom = null; for (IAtom a : atomContainer.atoms()) { if (a.getSymbol().equalsIgnoreCase("P")) { beforeAtom = a; a.setID("TEST"); break; } } IAtomContainer result = SMSDNormalizer.removeHydrogensAndPreserveAtomID(atomContainer); for (IAtom a : result.atoms()) { if (a.getSymbol().equalsIgnoreCase("P")) { afterAtom = a; break; } } assertEquals(afterAtom.getID(), beforeAtom.getID()); }