Normalizer { public static boolean normalize(IAtomContainer ac, Document doc) throws InvalidSmilesException, CDKException { NodeList nl = doc.getElementsByTagName("replace-set"); SmilesParser sp = new SmilesParser(ac.getBuilder()); boolean change = false; for (int i = 0; i < nl.getLength(); i++) { Element child = (Element) nl.item(i); NodeList replaces = child.getElementsByTagName("replace"); NodeList replacement = child.getElementsByTagName("replacement"); String replacementstring = replacement.item(0).getFirstChild().getNodeValue(); if (replacementstring.indexOf('\n') > -1 || replacementstring.length() < 1) { replacementstring = replacement.item(0).getFirstChild().getNextSibling().getNodeValue(); } IAtomContainer replacementStructure = sp.parseSmiles(replacementstring); for (int k = 0; k < replaces.getLength(); k++) { Element replace = (Element) replaces.item(k); String replacestring = replace.getFirstChild().getNodeValue(); if (replacestring.indexOf('\n') > -1 || replacestring.length() < 1) { replacestring = replace.getFirstChild().getNextSibling().getNodeValue(); } IAtomContainer replaceStructure = sp.parseSmiles(replacestring); List<RMap> l = null; UniversalIsomorphismTester universalIsomorphismTester = new UniversalIsomorphismTester(); while ((l = universalIsomorphismTester.getSubgraphMap(ac, replaceStructure)) != null) { List<RMap> l2 = universalIsomorphismTester.makeAtomsMapOfBondsMap(l, ac, replaceStructure); Iterator<RMap> bondit = l.iterator(); while (bondit.hasNext()) { RMap rmap = bondit.next(); IBond acbond = ac.getBond(rmap.getId1()); IBond replacebond = replacementStructure.getBond(rmap.getId2()); acbond.setOrder(replacebond.getOrder()); change = true; } Iterator<RMap> atomit = l2.iterator(); while (atomit.hasNext()) { RMap rmap = atomit.next(); IAtom acatom = ac.getAtom(rmap.getId1()); IAtom replaceatom = replacementStructure.getAtom(rmap.getId2()); acatom.setFormalCharge(replaceatom.getFormalCharge()); change = true; } } } } return (change); }  static boolean normalize(IAtomContainer ac, Document doc);  }
@Test public void testNormalize() throws Exception { IAtomContainer ac = new AtomContainer(); ac.addAtom(new Atom("C")); ac.addAtom(new Atom("N")); ac.addAtom(new Atom("O")); ac.addAtom(new Atom("O")); ac.addBond(new Bond(ac.getAtom(0), ac.getAtom(1))); ac.addBond(new Bond(ac.getAtom(1), ac.getAtom(2), IBond.Order.DOUBLE)); ac.addBond(new Bond(ac.getAtom(1), ac.getAtom(3), IBond.Order.DOUBLE)); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element set = doc.createElement("replace-set"); doc.appendChild(set); Element replace = doc.createElement("replace"); set.appendChild(replace); replace.appendChild(doc.createTextNode("O=N=O")); Element replacement = doc.createElement("replacement"); set.appendChild(replacement); replacement.appendChild(doc.createTextNode("[O-][N+]=O")); Normalizer.normalize(ac, doc); Assert.assertTrue(ac.getBond(1).getOrder() == IBond.Order.SINGLE ^ ac.getBond(2).getOrder() == IBond.Order.SINGLE); }