public void writeChemModel(IAtomContainerSet som, String file, String endFix) { System.out.println("WRITE Molecules:" + som.getAtomContainerCount()); String molfile = file + endFix; try { FileOutputStream fout = new FileOutputStream(molfile); MDLV2000Writer mdlw = new MDLV2000Writer(fout); mdlw.write(som); mdlw.close(); } catch (CDKException | IOException ex2) { System.out.println("IOError:cannot write file due to:" + ex2.toString()); } }
@Test public void testAccepts() throws Exception { MDLV2000Writer reader = new MDLV2000Writer(); Assert.assertTrue(reader.accepts(ChemFile.class)); Assert.assertTrue(reader.accepts(ChemModel.class)); Assert.assertTrue(reader.accepts(AtomContainer.class)); }
customizeJob(); try { if (object instanceof IChemFile) { writeChemFile((IChemFile) object); return; } else if (object instanceof IChemModel) { sequence.addChemModel((IChemModel) object); file.addChemSequence(sequence); writeChemFile((IChemFile) file); return; } else if (object instanceof IAtomContainer) { writeMolecule((IAtomContainer) object); return;
@Test public void roundtripAtomParityExpH() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/tetrahedral-parity-withExpH.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString(" 0.0000 0.0000 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0\n")); } }
MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(benzene); mdlWriter.close(); Assert.assertTrue(writer.toString().indexOf("1 2 1 0 0 0 0") != -1); mdlWriter = new MDLV2000Writer(writer); Properties prop = new Properties(); prop.setProperty("WriteAromaticBondTypes", "true"); PropertiesListener listener = new PropertiesListener(prop); mdlWriter.addChemObjectIOListener(listener); mdlWriter.customizeJob(); mdlWriter.write(benzene); mdlWriter.close(); Assert.assertTrue(writer.toString().indexOf("1 2 4 0 0 0 0") != -1);
@Test public void ignoreCarbon12() throws Exception { IAtomContainer mol = builder.newAtomContainer(); IAtom atom = builder.newAtom(); atom.setSymbol("C"); atom.setMassNumber(12); mol.addAtom(atom); StringWriter sw = new StringWriter(); try (MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.getSetting(MDLV2000Writer.OptWriteMajorIsotopes) .setSetting("false"); mdlw.write(mol); } assertThat(sw.toString(), not(containsString("M ISO 1 1 12"))); }
@Test public void aromaticBondTypesEnabled() throws Exception { IAtomContainer mol = builder.newInstance(IAtomContainer.class); mol.addAtom(builder.newInstance(IAtom.class, "C")); mol.addAtom(builder.newInstance(IAtom.class, "C")); IBond bond = builder.newInstance(IBond.class, mol.getAtom(0), mol.getAtom(1), Order.UNSET); bond.setIsAromatic(true); mol.addBond(bond); StringWriter sw = new StringWriter(); try (MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.setWriteAromaticBondTypes(true); mdlw.write(mol); } assertThat(sw.toString(), containsString(" 1 2 4 0 0 0 0 \n")); }
mdlWriter = new MDLV3000Writer(stringWriter); else mdlWriter = new MDLV2000Writer(stringWriter);
try (MDLV2000Writer mdlWriter = new MDLV2000Writer(sw)) { mdlWriter.writeMolecule(tocopy); } catch (Exception ex) { logger.error("Could not write molecule to string: ", ex.getMessage());
private void initIOSettings() { paramWriteData = addSetting(new BooleanIOSetting("writeProperties", IOSetting.Importance.LOW, "Should molecule properties be written as non-structural data", "true")); paramWriteV3000 = addSetting(new BooleanIOSetting("writeV3000", IOSetting.Importance.LOW, "Write all records as V3000", "false")); addSettings(new MDLV2000Writer().getSettings()); addSettings(new MDLV3000Writer().getSettings()); }
@Override public boolean accepts(Class<? extends IChemObject> classObject) { Class<?>[] interfaces = classObject.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (IAtomContainer.class.equals(interfaces[i])) return true; if (IChemFile.class.equals(interfaces[i])) return true; if (IChemModel.class.equals(interfaces[i])) return true; } if (IAtomContainer.class.equals(classObject)) return true; if (IChemFile.class.equals(classObject)) return true; if (IChemModel.class.equals(classObject)) return true; Class superClass = classObject.getSuperclass(); if (superClass != null) return this.accepts(superClass); return false; }
private void writeChemFile(IChemFile file) throws Exception { IAtomContainer bigPile = file.getBuilder().newInstance(IAtomContainer.class); for (IAtomContainer container : ChemFileManipulator.getAllAtomContainers(file)) { bigPile.add(container); if (container.getTitle() != null) { if (bigPile.getTitle() != null) bigPile.setTitle(bigPile.getTitle() + "; " + container.getTitle()); else bigPile.setTitle(container.getTitle()); } if (container.getProperty(CDKConstants.REMARK) != null) { if (bigPile.getProperty(CDKConstants.REMARK) != null) bigPile.setProperty(CDKConstants.REMARK, bigPile.getProperty(CDKConstants.REMARK) + "; " + container.getProperty(CDKConstants.REMARK)); else bigPile.setProperty(CDKConstants.REMARK, container.getProperty(CDKConstants.REMARK)); } } writeMolecule(bigPile); }
@Test public void roundtripAtomParityImplH() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/tetrahedral-parity-withImplH.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString(" 0.0000 0.0000 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0\n")); } }
@Test public void testWriteValence() throws Exception { StringWriter writer = new StringWriter(); IAtomContainer molecule = TestMoleculeFactory.makeAlphaPinene(); molecule.getAtom(0).setValency(1); molecule.getAtom(1).setValency(0); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); Properties customSettings = new Properties(); customSettings.setProperty("WriteQueryFormatValencies", "true"); mdlWriter.addChemObjectIOListener(new PropertiesListener(customSettings)); mdlWriter.write(molecule); mdlWriter.close(); String output = writer.toString(); Assert.assertTrue(output.indexOf("0 0 0 0 0 1 0 0 0 0 0 0") != -1); Assert.assertTrue(output.indexOf("0 0 0 0 0 15 0 0 0 0 0 0") != -1); }
@Test public void skipDefaultProps() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/tetrahedral-parity-withImplH.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.getSetting(MDLV2000Writer.OptWriteDefaultProperties) .setSetting("false"); mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString("\n" + " 5 4 0 0 1 0 0 0 0 0999 V2000\n" + " 0.0000 0.0000 0.0000 C 0 0 1 0 0 0\n" + " 0.0000 0.0000 0.0000 C 0 0\n" + " 0.0000 0.0000 0.0000 C 0 0\n" + " 0.0000 0.0000 0.0000 O 0 0\n" + " 0.0000 0.0000 0.0000 C 0 0\n" + " 1 2 1 0\n" + " 2 3 1 0\n" + " 1 4 1 0\n" + " 1 5 1 0\n" + "M END")); } } }
@BeforeClass public static void setup() { builder = DefaultChemObjectBuilder.getInstance(); setChemObjectIO(new MDLV2000Writer()); }
/** * Produces a CTAB block for an atomContainer, without the header lines. * @param atomContainer * @return CTAB block * @throws CDKException */ private String getCTAB(IAtomContainer atomContainer) throws CDKException { StringWriter strWriter = new StringWriter(); MDLV2000Writer mdlWriter = new MDLV2000Writer(strWriter); mdlWriter.write(atomContainer); try { mdlWriter.close(); } catch (IOException exception) { // FIXME } String ctab = strWriter.toString(); //strip of the individual header, as we have one super header instead. for (int line = 1; line <= 3; line++) { ctab = ctab.substring(ctab.indexOf(LSEP) + (LSEP.length())); } return ctab; }
@Test public void sgroupExpandedAbbreviationRoundTrip() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/triphenyl-phosphate-expanded.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString("M STY 3 1 SUP 2 SUP 3 SUP\n")); assertThat(output, containsString("M SDS EXP 1 1")); } }
@Test public void testForce2DCoordinates() throws Exception { StringWriter writer = new StringWriter(); IAtomContainer molecule = builder.newInstance(IAtomContainer.class); IAtom atom = builder.newInstance(IAtom.class, "C"); atom.setPoint2d(new Point2d(1.0, 2.0)); atom.setPoint3d(new Point3d(3.0, 4.0, 5.0)); molecule.addAtom(atom); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); Properties prop = new Properties(); prop.setProperty("ForceWriteAs2DCoordinates", "true"); PropertiesListener listener = new PropertiesListener(prop); mdlWriter.addChemObjectIOListener(listener); mdlWriter.customizeJob(); mdlWriter.write(molecule); mdlWriter.close(); String output = writer.toString(); // the current behavior is that if both 2D and 3D coordinates // are available, the 3D is outputted, and the 2D not Assert.assertTrue(output.contains("1.0")); Assert.assertTrue(output.contains("2.0")); }
cow = new MDLV2000Writer(new FileWriter(outFile)); cow.write(model);