Convertor { public CMLBond cdkBondToCMLBond(IBond cdkBond) { CMLBond cmlBond = new CMLBond(); this.checkPrefix(cmlBond); if (cdkBond.getID() == null || cdkBond.getID().length() == 0) { cmlBond.setId("b" + cdkBond.hashCode()); } else { cmlBond.setId(cdkBond.getID()); } String[] atomRefArray = new String[cdkBond.getAtomCount()]; for (int i = 0; i < cdkBond.getAtomCount(); i++) { String atomID = cdkBond.getAtom(i).getID(); if (atomID == null || atomID.length() == 0) { atomRefArray[i] = "a" + Integer.valueOf(cdkBond.getAtom(i).hashCode()).toString(); } else { atomRefArray[i] = atomID; } } if (atomRefArray.length == 2) { cmlBond.setAtomRefs2(atomRefArray); } else { cmlBond.setAtomRefs(atomRefArray); } IBond.Order border = cdkBond.getOrder(); if (border == Order.SINGLE) { cmlBond.setOrder("S"); } else if (border == Order.DOUBLE) { cmlBond.setOrder("D"); } else if (border == Order.TRIPLE) { cmlBond.setOrder("T"); } else { CMLScalar scalar = new CMLScalar(); this.checkPrefix(scalar); scalar.setDictRef("cdk:bondOrder"); scalar.setTitle("order"); scalar.setValue(cdkBond.getOrder().numeric()); cmlBond.appendChild(scalar); } if (cdkBond.getFlag(CDKConstants.ISAROMATIC)) { CMLBondType bType = new CMLBondType(); bType.setDictRef("cdk:aromaticBond"); cmlBond.appendChild(bType); } if (cdkBond.getStereo() == IBond.Stereo.UP || cdkBond.getStereo() == IBond.Stereo.DOWN) { CMLBondStereo bondStereo = new CMLBondStereo(); this.checkPrefix(bondStereo); if (cdkBond.getStereo() == IBond.Stereo.UP) { bondStereo.setDictRef("cml:W"); bondStereo.setXMLContent("W"); } else { bondStereo.setDictRef("cml:H"); bondStereo.setXMLContent("H"); } cmlBond.appendChild(bondStereo); } if (cdkBond.getProperties().size() > 0) writeProperties(cdkBond, cmlBond); Iterator<String> elements = customizers.keySet().iterator(); while (elements.hasNext()) { ICMLCustomizer customizer = customizers.get(elements.next()); try { customizer.customize(cdkBond, cmlBond); } catch (Exception exception) { logger.error("Error while customizing CML output with customizer: ", customizer.getClass().getName()); logger.debug(exception); } } return cmlBond; } Convertor(boolean useCMLIDs, String prefix); void registerCustomizer(ICMLCustomizer customizer); CMLCml cdkChemFileToCMLList(IChemFile file); CMLList cdkChemSequenceToCMLList(IChemSequence sequence); CMLList cdkChemModelToCMLList(IChemModel model); CMLCml cdkReactionSchemeToCMLReactionSchemeAndMoleculeList(IReactionScheme cdkScheme); CMLReactionScheme cdkReactionSchemeToCMLReactionScheme(IReactionScheme cdkScheme); CMLReactionStep cdkReactionToCMLReactionStep(IReaction reaction); CMLReactionList cdkReactionSetToCMLReactionList(IReactionSet reactionSet); CMLMoleculeList cdkAtomContainerSetToCMLList(IAtomContainerSet moleculeSet); CMLReaction cdkReactionToCMLReaction(IReaction reaction); CMLMolecule cdkCrystalToCMLMolecule(ICrystal crystal); CMLMolecule cdkPDBPolymerToCMLMolecule(IPDBPolymer pdbPolymer); CMLMolecule cdkMonomerToCMLMolecule(IMonomer monomer); CMLMolecule cdkAtomContainerToCMLMolecule(IAtomContainer structure); CMLAtom cdkAtomToCMLAtom(IAtom cdkAtom); CMLAtom cdkAtomToCMLAtom(IAtomContainer container, IAtom cdkAtom); CMLBond cdkBondToCMLBond(IBond cdkBond); final static String NS_CML; }
@Test public void testCdkBondToCMLBond_Wedge() throws IOException { IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance(); IBond bond = builder.newInstance(IBond.class); bond.setOrder(IBond.Order.SINGLE); bond.setStereo(IBond.Stereo.UP); Convertor convertor = new Convertor(true, null); CMLBond cmlBond = convertor.cdkBondToCMLBond(bond); ByteArrayOutputStream out = new ByteArrayOutputStream(); Serializer serializer = new Serializer(out, "UTF-8"); serializer.write(new Document(cmlBond)); out.close(); String expected = "<bondStereo dictRef=\"cml:W\">W</bondStereo>"; String actual = new String(out.toByteArray()); Assert.assertTrue(actual.contains(expected)); }
@Test public void testCdkBondToCMLBond_Hatch() throws IOException { IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance(); IBond bond = builder.newInstance(IBond.class); bond.setOrder(IBond.Order.SINGLE); bond.setStereo(IBond.Stereo.DOWN); Convertor convertor = new Convertor(true, null); CMLBond cmlBond = convertor.cdkBondToCMLBond(bond); ByteArrayOutputStream out = new ByteArrayOutputStream(); Serializer serializer = new Serializer(out, "UTF-8"); serializer.write(new Document(cmlBond)); out.close(); String expected = "<bondStereo dictRef=\"cml:H\">H</bondStereo>"; String actual = new String(out.toByteArray()); Assert.assertTrue(actual.contains(expected)); }