@Override public boolean accepts(Class<? extends IChemObject> classObject) { if (IReaction.class.equals(classObject)) return true; if (IReactionSet.class.equals(classObject)) return true; Class<?>[] interfaces = classObject.getInterfaces(); for (Class<?> anInterface : interfaces) { if (IReaction.class.equals(anInterface)) return true; if (IReactionSet.class.equals(anInterface)) return true; } Class superClass = classObject.getSuperclass(); if (superClass != null) return this.accepts(superClass); return false; }
@Override public void setWriter(OutputStream output) throws CDKException { setWriter(new OutputStreamWriter(output)); }
/** * Writes a IChemObject to the MDL RXN file formated output. * It can only output ChemObjects of type Reaction * * @param object class must be of type Molecule or MoleculeSet. * * @see org.openscience.cdk.ChemFile */ @Override public void write(IChemObject object) throws CDKException { if (object instanceof IReactionSet) { writeReactionSet((IReactionSet) object); } else if (object instanceof IReaction) { writeReaction((IReaction) object); } else { throw new CDKException("Only supported is writing ReactionSet, Reaction objects."); } }
@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()); }
@Test public void testAccepts() throws Exception { MDLRXNWriter reader = new MDLRXNWriter(); Assert.assertTrue(reader.accepts(Reaction.class)); }
protected File saveAsRXN(IChemModel model, File outFile) throws IOException, CDKException { if(model.getMoleculeSet()!=null && model.getMoleculeSet().getAtomContainerCount()>0){ String error = GT.get("Problems handling data"); String message = GT.get("{0} files cannot contain extra molecules. You painted molecules outside the reaction(s), which will not be in the file. Continue?", "RXN"); int answer = JOptionPane.showConfirmDialog(jcpPanel, message, error, JOptionPane.YES_NO_OPTION); if(answer == JOptionPane.NO_OPTION) return null; } if(model.getReactionSet()==null || model.getReactionSet().getReactionCount()==0){ String error = GT.get("Problems handling data"); String message = GT.get("RXN can only save reactions. You have no reactions painted!"); JOptionPane.showMessageDialog(jcpPanel, message, error, JOptionPane.WARNING_MESSAGE); return null; } logger.info("Saving the contents in an rxn file..."); String fileName = outFile.toString(); if (!fileName.endsWith(".rxn")) { fileName += ".rxn"; outFile = new File(fileName); } outFile=new File(fileName); cow = new MDLRXNWriter(new FileWriter(outFile)); cow.write(model.getReactionSet()); cow.close(); if(jcpPanel instanceof JChemPaintPanel) ((JChemPaintPanel)jcpPanel).setTitle(jcpPanel.getChemModel().getID()); return outFile; }
line += formatMDLInt(reactantCount, 3); line += formatMDLInt(productCount, 3); writer.write(line); writer.write('\n'); i++; writeAtomContainerSet(reaction.getReactants()); writeAtomContainerSet(reaction.getProducts());
/** * Writes an array of Reaction to an OutputStream in MDL rdf format. * * @param reactions Array of Reactions that is written to an OutputStream */ private void writeReactionSet(IReactionSet reactions) throws CDKException { for (IReaction iReaction : reactions.reactions()) { writeReaction(iReaction); } }
MDLRXNWriter mdlWriter = new MDLRXNWriter(writer); mdlWriter.write(reactionSet); mdlWriter.close(); file = writer.toString();
@BeforeClass public static void setup() { builder = DefaultChemObjectBuilder.getInstance(); setChemObjectIO(new MDLRXNWriter()); }
MDLRXNWriter mdlWriter = new MDLRXNWriter(writer); mdlWriter.write(reactionSet); mdlWriter.close(); file = writer.toString();
@BeforeClass public static void setup() { builder = DefaultChemObjectBuilder.getInstance(); setChemObjectIO(new MDLRXNWriter()); }