private List<IMapping> cloneMappings(IReaction reaction, Map<IAtom, IAtom> atomAtomMap) { // clone the mappings int numberOfMappings = reaction.getMappingCount(); List<IMapping> map = new ArrayList<IMapping>(); for (int mappingIndex = 0; mappingIndex < numberOfMappings; mappingIndex++) { IMapping mapping = reaction.getMapping(mappingIndex); IChemObject keyChemObj0 = mapping.getChemObject(0); IChemObject keyChemObj1 = mapping.getChemObject(1); IChemObject co0 = (IChemObject) atomAtomMap.get(keyChemObj0); IChemObject co1 = (IChemObject) atomAtomMap.get(keyChemObj1); map.add(new Mapping(co0, co1)); } return map; }
/** * * @param mapping * @param atomAtomMap * @return */ protected IMapping cloneMapping( IMapping mapping, Map<IAtom, IAtom> atomAtomMap) { IChemObject keyChemObj0 = mapping.getChemObject(0); IChemObject keyChemObj1 = mapping.getChemObject(1); IChemObject co0 = atomAtomMap.get(keyChemObj0); IChemObject co1 = atomAtomMap.get(keyChemObj1); // THIS IS STUPID : BLAME THE IDIOT WHO FAILED TO PUT SET METHODS IN // IMAPPING (OR IREACTION, FOR THAT MATTER) if (co0 == null) { co0 = new ChemObject(); } if (co1 == null) { co1 = new ChemObject(); } return new Mapping(co0, co1); }
@BeforeClass public static void setUp() { setTestObjectBuilder(new ITestObjectBuilder() { @Override public IChemObject newTestObject() { return new Mapping(new Atom(), new Atom()); } }); }
/** * Clones this <code>Reaction</code> and its content. * * @return The cloned object */ @Override public Object clone() throws CloneNotSupportedException { Reaction clone = (Reaction) super.clone(); // clone the reactants, products and agents clone.reactants = (IAtomContainerSet) reactants.clone(); clone.agents = (IAtomContainerSet) agents.clone(); clone.products = (IAtomContainerSet) products.clone(); // create a Map of corresponding atoms for molecules (key: original Atom, // value: clone Atom) Map<IAtom, IAtom> atomatom = new Hashtable<IAtom, IAtom>(); for (int i = 0; i < reactants.getAtomContainerCount(); ++i) { IAtomContainer mol = reactants.getAtomContainer(i); IAtomContainer mol2 = clone.reactants.getAtomContainer(i); for (int j = 0; j < mol.getAtomCount(); ++j) atomatom.put(mol.getAtom(j), mol2.getAtom(j)); } // clone the maps clone.map = new Mapping[map.length]; for (int f = 0; f < mappingCount; f++) { clone.map[f] = new Mapping(atomatom.get(map[f].getChemObject(0)), atomatom.get(map[f].getChemObject(1))); } return clone; }
/** * Clones this <code>Reaction</code> and its content. * * @return The cloned object */ @Override public Object clone() throws CloneNotSupportedException { Reaction clone = (Reaction) super.clone(); // clone the reactants, products and agents clone.reactants = (IAtomContainerSet) reactants.clone(); clone.agents = (IAtomContainerSet) agents.clone(); clone.products = (IAtomContainerSet) products.clone(); // create a Map of corresponding atoms for molecules (key: original Atom, // value: clone Atom) Map<IAtom, IAtom> atomatom = new Hashtable<IAtom, IAtom>(); for (int i = 0; i < reactants.getAtomContainerCount(); ++i) { IAtomContainer mol = reactants.getAtomContainer(i); IAtomContainer mol2 = clone.reactants.getAtomContainer(i); for (int j = 0; j < mol.getAtomCount(); ++j) atomatom.put(mol.getAtom(j), mol2.getAtom(j)); } // clone the maps clone.map = new Mapping[map.length]; for (int f = 0; f < mappingCount; f++) { clone.map[f] = new Mapping(atomatom.get(map[f].getChemObject(0)), atomatom.get(map[f].getChemObject(1))); } return clone; }
map.getKey().setFlag(MAPPED, true); map.getValue().setFlag(MAPPED, true); compressedReaction.addMapping(new Mapping(map.getKey(), map.getValue()));
@Override public IChemObject newTestObject() { return new Mapping(new Atom(), new Atom()); } });
@Test public void testMapping_IChemObject_IChemObject() { IChemObject object = newChemObject(); IMapping mapping = new Mapping(object.getBuilder().newInstance(IAtom.class), object.getBuilder().newInstance( IAtom.class)); Assert.assertNotNull(mapping); }
@Test public void testRoundtrip() throws Exception { IReaction reaction = builder.newInstance(IReaction.class); IAtomContainer hydroxide = builder.newInstance(IAtomContainer.class); hydroxide.addAtom(builder.newInstance(IAtom.class, "O")); reaction.addReactant(hydroxide); IAtomContainer proton = builder.newInstance(IAtomContainer.class); proton.addAtom(builder.newInstance(IAtom.class, "H")); reaction.addReactant(proton); IAtomContainer water = builder.newInstance(IAtomContainer.class); water.addAtom(builder.newInstance(IAtom.class, "O")); reaction.addProduct(water); reaction.addMapping(new Mapping(hydroxide.getAtom(0), water.getAtom(0))); // now serialize to MDL RXN StringWriter writer = new StringWriter(10000); String file = ""; MDLRXNWriter mdlWriter = new MDLRXNWriter(writer); mdlWriter.write(reaction); mdlWriter.close(); file = writer.toString(); Assert.assertTrue(file.length() > 0); // now deserialize the MDL RXN output IReaction reaction2 = builder.newInstance(IReaction.class); MDLRXNReader reader = new MDLRXNReader(new StringReader(file)); reaction2 = (IReaction) reader.read(reaction2); reader.close(); Assert.assertEquals(2, reaction2.getReactantCount()); Assert.assertEquals(1, reaction2.getProductCount()); Assert.assertEquals(1, reaction2.getMappingCount()); }
clone.addMapping(new Mapping(atomatom.get(mapping.getChemObject(0)), atomatom.get(mapping.getChemObject(1))));