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 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 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")); } }
@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 sgroupCopolymerRoundTrip() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-ran-copolymer.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString("M SST 1 1 RAN")); assertThat(output, containsString("M STY 3 1 COP 2 SRU 3 SRU")); } }
@Test public void sgroupParentAtomListRoundTrip() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/ChEBI_81539.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString("M STY 5 1 MUL 2 SRU")); assertThat(output, containsString("M SPA 1 12")); } }
@Test public void sgroupBracketStylesRoundTrip() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-sru-bracketstyles.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString("M STY 2 1 SRU 2 SRU")); assertThat(output, containsString("M SBT 1 1 1")); } }
@Test public void sgroupUnorderedMixtureRoundTrip() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-unord-mixture.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString("M STY 3 1 COM 2 COM 3 MIX")); assertThat(output, containsString("M SPL 1 1 3")); } }
@Test public void sgroupRepeatUnitRoundTrip() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-sru.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString("M STY 1 1 SRU")); assertThat(output, containsString("M SMT 1 n")); assertThat(output, containsString("M SCN 1 1 HT")); } }
@Test public void sgroupOrderedMixtureRoundTrip() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-ord-mixture.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mdlr.read(new AtomContainer())); String output = sw.toString(); assertThat(output, containsString("M STY 3 1 COM 2 COM 3 FOR")); assertThat(output, containsString("M SNC 1 1 1")); assertThat(output, containsString("M SNC 1 2 2")); } }
@Test public void roundtripAtomParityImplModified() throws Exception { StringWriter sw = new StringWriter(); try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/tetrahedral-parity-withImplH.mol")); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { AtomContainer mol = mdlr.read(new AtomContainer()); ITetrahedralChirality tc = (ITetrahedralChirality) mol.stereoElements().iterator().next(); tc.setStereo(tc.getStereo().invert()); mdlw.write(mol); String output = sw.toString(); assertThat(output, containsString(" 0.0000 0.0000 0.0000 C 0 0 2 0 0 0 0 0 0 0 0 0\n")); } }
@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 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 writeCarbon12() 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.write(mol); } assertThat(sw.toString(), containsString("M ISO 1 1 12")); }
@Test public void writeChargeAtomProps() throws Exception { IAtomContainer mol = builder.newAtomContainer(); IAtom atom = builder.newAtom(); atom.setSymbol("C"); atom.setFormalCharge(+1); mol.addAtom(atom); StringWriter sw = new StringWriter(); try (MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mol); } assertThat(sw.toString(), containsString("C 0 3")); }
@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 writeDimensionField3D() throws Exception { IAtomContainer mol = builder.newAtomContainer(); IAtom atom = builder.newAtom(); atom.setSymbol("C"); atom.setImplicitHydrogenCount(4); atom.setPoint3d(new Point3d(0.5, 0.5, 0.1)); mol.addAtom(atom); StringWriter sw = new StringWriter(); try (MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { mdlw.write(mol); } assertThat(sw.toString(), containsString("3D")); }
@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 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")); }
@Test(expected = CDKException.class) public void aromaticBondTypes() 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.write(mol); } }