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()); } }
/** * * @param mol * @param out * @throws IOException * @throws IllegalArgumentException * @throws CDKException */ void writeMolToMolfile(IAtomContainer mol, Writer out) throws IOException, IllegalArgumentException, CDKException { try (MDLV2000Writer writer = new MDLV2000Writer(out)) { writer.write(DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class, mol)); writer.close(); } }
@Test public void testAtomParity() throws CDKException, IOException { InputStream in = ClassLoader.getSystemResourceAsStream("data/mdl/mol_testAtomParity.mol"); MDLV2000Reader reader = new MDLV2000Reader(in); IAtomContainer molecule = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class); molecule = reader.read(molecule); reader.close(); StringWriter sw = new StringWriter(); MDLV2000Writer writer = new MDLV2000Writer(sw); writer.write(molecule); writer.close(); System.out.println(sw.toString()); Assert.assertTrue(sw.toString().contains( " -1.1749 0.1436 0.0000 C 0 0 1 0 0 0 0 0 0 0 0 0")); }
@Test public void testSingleSingletRadical() throws Exception { InputStream in = ClassLoader.getSystemResourceAsStream("data/mdl/singleSingletRadical.mol"); MDLV2000Reader reader = new MDLV2000Reader(in); IAtomContainer molecule = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class); molecule = reader.read(molecule); reader.close(); StringWriter sw = new StringWriter(); MDLV2000Writer writer = new MDLV2000Writer(sw); writer.write(molecule); writer.close(); String[] lines = sw.toString().split("\n"); assertThat("incorrect file length", lines.length, is(9)); assertThat("incorrect radical output", lines[7], is("M RAD 1 2 1")); }
@Test public void testSingleDoubletRadical() throws Exception { InputStream in = ClassLoader.getSystemResourceAsStream("data/mdl/singleDoubletRadical.mol"); MDLV2000Reader reader = new MDLV2000Reader(in); IAtomContainer molecule = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class); molecule = reader.read(molecule); reader.close(); StringWriter sw = new StringWriter(); MDLV2000Writer writer = new MDLV2000Writer(sw); writer.write(molecule); writer.close(); String[] lines = sw.toString().split("\n"); assertThat("incorrect file length", lines.length, is(9)); assertThat("incorrect radical output", lines[7], is("M RAD 1 2 2")); }
@Test public void testSingleTripletRadical() throws Exception { InputStream in = ClassLoader.getSystemResourceAsStream("data/mdl/singleTripletRadical.mol"); MDLV2000Reader reader = new MDLV2000Reader(in); IAtomContainer molecule = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class); molecule = reader.read(molecule); reader.close(); StringWriter sw = new StringWriter(); MDLV2000Writer writer = new MDLV2000Writer(sw); writer.write(molecule); writer.close(); String[] lines = sw.toString().split("\n"); assertThat("incorrect file length", lines.length, is(9)); assertThat("incorrect radical output", lines[7], is("M RAD 1 2 1")); }
@Test public void testMultipleRadicals() throws Exception { InputStream in = ClassLoader.getSystemResourceAsStream("data/mdl/multipleRadicals.mol"); MDLV2000Reader reader = new MDLV2000Reader(in); IAtomContainer molecule = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class); molecule = reader.read(molecule); reader.close(); StringWriter sw = new StringWriter(); MDLV2000Writer writer = new MDLV2000Writer(sw); writer.write(molecule); writer.close(); String[] lines = sw.toString().split("\n"); assertThat("incorrect file length", lines.length, is(24)); assertThat("incorrect radical output on line 22", lines[21], is("M RAD 8 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2")); assertThat("incorrect radical output on line 23", lines[22], is("M RAD 1 9 2")); }
@Test public void nonDefaultValence_fe_iii() throws Exception { IAtomContainer container = new AtomContainer(); IAtom fe1 = new Atom("Fe"); fe1.setImplicitHydrogenCount(3); container.addAtom(fe1); StringWriter writer = new StringWriter(); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(container); mdlWriter.close(); String output = writer.toString(); Assert.assertTrue(output.contains("Fe 0 0 0 0 0 3 0 0 0 0 0 0")); }
@Test public void testWritePseudoAtoms() throws Exception { InputStream in = ClassLoader.getSystemResourceAsStream("data/mdl/pseudoatoms.sdf"); MDLV2000Reader reader = new MDLV2000Reader(in); IAtomContainer molecule = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class); molecule = reader.read(molecule); reader.close(); StringWriter writer = new StringWriter(); MDLV2000Writer mwriter = new MDLV2000Writer(writer); mwriter.write(molecule); mwriter.close(); String output = writer.toString(); Assert.assertTrue(output.indexOf("Gln") != -1); Assert.assertTrue(output.indexOf("Leu") != -1); }
@Test public void testUndefinedStereo() throws Exception { IAtomContainer mol = TestMoleculeFactory.makeAlphaPinene(); mol.getBond(0).setStereo(IBond.Stereo.UP_OR_DOWN); mol.getBond(1).setStereo(IBond.Stereo.E_OR_Z); StringWriter writer = new StringWriter(); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(mol); mdlWriter.close(); String output = writer.toString(); Assert.assertTrue(output.indexOf("1 2 2 4 0 0 0") > -1); Assert.assertTrue(output.indexOf("2 3 1 3 0 0 0") > -1); }
@Test public void testAlias_TruncatedLabel() throws Exception { IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance(); IAtomContainer container = builder.newInstance(IAtomContainer.class); String label = "This is a very long label - almost too long. it should be cut here -> and the rest is truncated"; container.addAtom(builder.newInstance(IPseudoAtom.class, label)); StringWriter sw = new StringWriter(); MDLV2000Writer writer = new MDLV2000Writer(sw); writer.write(container); writer.close(); String output = sw.toString(); Assert.assertTrue(output.contains("This is a very long label - almost too long. it should be cut here ->")); // make sure the full label wasn't output Assert.assertFalse(output.contains(label)); }
/** * Tests if non-valid atom atom mappings are ignored by the reader. */ @Test public void testWriteInvalidAtomAtomMapping() throws Exception { StringWriter writer = new StringWriter(); IAtomContainer molecule = TestMoleculeFactory.makeAlphaPinene(); molecule.getAtom(0).setProperty(CDKConstants.ATOM_ATOM_MAPPING, "1a"); molecule.getAtom(1).setProperty(CDKConstants.ATOM_ATOM_MAPPING, "15"); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(molecule); mdlWriter.close(); String output = writer.toString(); Pattern p = Pattern.compile(".*V2000.* 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 " + "0 0.* 0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 15 0 0.*", Pattern.MULTILINE | Pattern.DOTALL); Matcher m = p.matcher(output); Assert.assertTrue(m.matches()); }
@Test public void testWriteAtomAtomMapping() throws Exception { StringWriter writer = new StringWriter(); IAtomContainer molecule = TestMoleculeFactory.makeAlphaPinene(); molecule.getAtom(0).setProperty(CDKConstants.ATOM_ATOM_MAPPING, 1); molecule.getAtom(1).setProperty(CDKConstants.ATOM_ATOM_MAPPING, 15); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(molecule); mdlWriter.close(); String output = writer.toString(); Assert.assertTrue(output.indexOf("0 0 0 0 0 0 0 0 0 1 0 0") != -1); Assert.assertTrue(output.indexOf("0 0 0 0 0 0 0 0 0 15 0 0") != -1); }
@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); }
/** * @cdk.bug 1212219 */ @Test public void testBug1212219() throws Exception { StringWriter writer = new StringWriter(); IAtomContainer molecule = new AtomContainer(); Atom atom = new Atom("C"); atom.setMassNumber(14); molecule.addAtom(atom); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(molecule); mdlWriter.close(); String output = writer.toString(); //logger.debug("MDL output for testBug1212219: " + output); Assert.assertTrue(output.indexOf("M ISO 1 1 14") != -1); }
/** * Tests if String atom atom mappings are parsed correctly */ @Test public void testWriteStringAtomAtomMapping() throws Exception { StringWriter writer = new StringWriter(); IAtomContainer molecule = TestMoleculeFactory.makeAlphaPinene(); molecule.getAtom(0).setProperty(CDKConstants.ATOM_ATOM_MAPPING, "1"); molecule.getAtom(1).setProperty(CDKConstants.ATOM_ATOM_MAPPING, "15"); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(molecule); mdlWriter.close(); String output = writer.toString(); Assert.assertTrue(output.contains("0 0 0 0 0 0 0 0 0 1 0 0")); Assert.assertTrue(output.contains("0 0 0 0 0 0 0 0 0 15 0 0")); }
@Test public void testNullFormalCharge() throws Exception { StringWriter writer = new StringWriter(); IAtomContainer molecule = builder.newInstance(IAtomContainer.class); IAtom atom = builder.newInstance(IAtom.class, "C"); atom.setFormalCharge(null); molecule.addAtom(atom); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(molecule); mdlWriter.close(); String output = writer.toString(); // test ensures that the writer does not throw an exception on // null formal charges, so a mere assert on output being non-zero // length is enough Assert.assertNotNull(output); Assert.assertNotSame(0, output.length()); }
/** * @cdk.bug 890456 * @cdk.bug 1524466 */ @Test public void testBug890456() throws Exception { StringWriter writer = new StringWriter(); IAtomContainer molecule = new AtomContainer(); molecule.addAtom(new PseudoAtom("*")); molecule.addAtom(new Atom("C")); molecule.addAtom(new Atom("C")); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(molecule); mdlWriter.close(); Assert.assertTrue(writer.toString().indexOf("M END") != -1); }
@Test(expected = CDKException.class) public void testUnsupportedBondOrder() throws Exception { IAtomContainer molecule = new AtomContainer(); molecule.addAtom(new Atom("C")); molecule.addAtom(new Atom("C")); molecule.addBond(new Bond(molecule.getAtom(0), molecule.getAtom(1), Order.QUADRUPLE)); MDLV2000Writer mdlWriter = new MDLV2000Writer(new StringWriter()); mdlWriter.write(molecule); mdlWriter.close(); }
@Test public void testTwoFragmentsWithTitle() throws Exception { IAtomContainer mol1 = TestMoleculeFactory.makeAlphaPinene(); mol1.setTitle("title1"); IAtomContainer mol2 = TestMoleculeFactory.makeAlphaPinene(); mol2.setTitle("title2"); IChemModel model = mol1.getBuilder().newInstance(IChemModel.class); model.setMoleculeSet(mol1.getBuilder().newInstance(IAtomContainerSet.class)); model.getMoleculeSet().addAtomContainer(mol1); model.getMoleculeSet().addAtomContainer(mol2); StringWriter writer = new StringWriter(); MDLV2000Writer mdlWriter = new MDLV2000Writer(writer); mdlWriter.write(model); mdlWriter.close(); String output = writer.toString(); Assert.assertTrue(output.contains("title1; title2")); }