MDLV2000Reader extends DefaultChemObjectReader { @SuppressWarnings("unchecked") @Override public <T extends IChemObject> T read(T object) throws CDKException { if (object instanceof IAtomContainer) { return (T) readAtomContainer((IAtomContainer) object); } else if (object instanceof IChemFile) { return (T) readChemFile((IChemFile) object); } else if (object instanceof IChemModel) { return (T) readChemModel((IChemModel) object); } else { throw new CDKException("Only supported are ChemFile and Molecule."); } } MDLV2000Reader();  MDLV2000Reader(InputStream in);  MDLV2000Reader(InputStream in, Mode mode);  MDLV2000Reader(Reader in);  MDLV2000Reader(Reader in, Mode mode); @Override IResourceFormat getFormat(); @Override void setReader(Reader input); @Override void setReader(InputStream input); @SuppressWarnings("unchecked") @Override boolean accepts(Class<? extends IChemObject> classObject); @SuppressWarnings("unchecked") @Override T read(T object); @Override void close(); void customizeJob();  }
@Test(expected=CDKException.class) public void testAcdChemSketchLabel_Strict() throws Exception { String filename = "data/mdl/chemsketch-all-labelled.mol"; InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); MDLV2000Reader reader = new MDLV2000Reader(ins, Mode.STRICT); reader.read(new AtomContainer()); }
@Test public void testSgroupAbbreviation() throws Exception { try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-abbrv.mol"))) { final IAtomContainer container = mdlr.read(new AtomContainer()); List<Sgroup> sgroups = container.getProperty(CDKConstants.CTAB_SGROUPS); assertNotNull(sgroups); assertThat(sgroups.size(), is(1)); Sgroup sgroup = sgroups.get(0); assertThat(sgroup.getType(), is(SgroupType.CtabAbbreviation)); assertThat(sgroup.getSubscript(), is("Cs2CO3")); assertThat(sgroup.getAtoms().size(), is(6)); } }
@Test public void testSgroupRepeatUnit() throws Exception { try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-sru.mol"))) { IAtomContainer container = mdlr.read(new AtomContainer()); List<Sgroup> sgroups = container.getProperty(CDKConstants.CTAB_SGROUPS); assertNotNull(sgroups); assertThat(sgroups.size(), is(1)); Sgroup sgroup = sgroups.get(0); assertThat(sgroup.getType(), is(SgroupType.CtabStructureRepeatUnit)); assertThat(sgroup.getSubscript(), is("n")); assertThat((String) sgroup.getValue(SgroupKey.CtabConnectivity), is("HT")); assertThat(sgroup.getAtoms().size(), is(10)); assertThat(sgroup.getBonds().size(), is(2)); List<SgroupBracket> brackets = sgroup.getValue(SgroupKey.CtabBracket); assertThat(brackets.size(), is(2)); assertThat(brackets.get(0).getFirstPoint().x, closeTo(2.2579, 0.001)); assertThat(brackets.get(0).getFirstPoint().y, closeTo(-0.8756, 0.001)); assertThat(brackets.get(0).getSecondPoint().x, closeTo(1.7735, 0.001)); assertThat(brackets.get(0).getSecondPoint().y, closeTo(-1.6600, 0.001)); assertThat(brackets.get(1).getFirstPoint().x, closeTo(-0.9910, 0.001)); assertThat(brackets.get(1).getFirstPoint().y, closeTo(-1.7247, 0.001)); assertThat(brackets.get(1).getSecondPoint().x, closeTo(-0.4960, 0.001)); assertThat(brackets.get(1).getSecondPoint().y, closeTo(-0.8673, 0.001)); } }
@Test public void testSgroupUnorderedMixture() throws Exception { try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-unord-mixture.mol"))) { IAtomContainer container = mdlr.read(new AtomContainer()); List<Sgroup> sgroups = container.getProperty(CDKConstants.CTAB_SGROUPS); assertNotNull(sgroups); assertThat(sgroups.size(), is(3)); Sgroup sgroup = sgroups.get(0); assertThat(sgroup.getType(), is(SgroupType.CtabComponent)); assertThat(sgroup.getParents().size(), is(1)); assertThat(sgroup.getParents(), hasItem(sgroups.get(2))); sgroup = sgroups.get(1); assertThat(sgroup.getType(), is(SgroupType.CtabComponent)); assertThat(sgroup.getParents().size(), is(1)); assertThat(sgroup.getParents(), hasItem(sgroups.get(2))); sgroup = sgroups.get(2); assertThat(sgroup.getType(), is(SgroupType.CtabMixture)); assertThat(sgroup.getParents().size(), is(0)); } }
@Test public void testSgroupExpandedAbbreviation() throws Exception { try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/triphenyl-phosphate-expanded.mol"))) { IAtomContainer container = mdlr.read(new AtomContainer()); List<Sgroup> sgroups = container.getProperty(CDKConstants.CTAB_SGROUPS); assertNotNull(sgroups); assertThat(sgroups.size(), is(3)); Sgroup sgroup = sgroups.get(0); assertThat(sgroup.getType(), is(SgroupType.CtabAbbreviation)); assertThat(sgroup.getSubscript(), is("Ph")); assertNotNull(sgroup.getValue(SgroupKey.CtabExpansion)); sgroup = sgroups.get(1); assertThat(sgroup.getType(), is(SgroupType.CtabAbbreviation)); assertThat(sgroup.getSubscript(), is("Ph")); assertNotNull(sgroup.getValue(SgroupKey.CtabExpansion)); sgroup = sgroups.get(2); assertThat(sgroup.getType(), is(SgroupType.CtabAbbreviation)); assertThat(sgroup.getSubscript(), is("Ph")); assertNotNull(sgroup.getValue(SgroupKey.CtabExpansion)); } }
@Test(expected = CDKException.class) public void testSgroupInvalidConnectInStrictMode() throws Exception { try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-sru-bad-scn.mol"))) { mdlr.setReaderMode(Mode.STRICT); IAtomContainer container = mdlr.read(new AtomContainer()); } }
@Test(expected = CDKException.class) public void testSgroupDefOrderInStrictMode() throws Exception { try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-sru-bad-def.mol"))) { mdlr.setReaderMode(Mode.STRICT); IAtomContainer container = mdlr.read(new AtomContainer()); } }
@Test public void testSgroupBracketStyle() throws Exception { try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/sgroup-sru-bracketstyles.mol"))) { IAtomContainer container = mdlr.read(new AtomContainer()); List<Sgroup> sgroups = container.getProperty(CDKConstants.CTAB_SGROUPS); assertNotNull(sgroups); assertThat(sgroups.size(), is(2)); Sgroup sgroup = sgroups.get(0); assertThat(sgroup.getType(), is(SgroupType.CtabStructureRepeatUnit)); assertThat((Integer) sgroup.getValue(SgroupKey.CtabBracketStyle), is(1)); sgroup = sgroups.get(1); assertThat(sgroup.getType(), is(SgroupType.CtabStructureRepeatUnit)); assertThat((Integer) sgroup.getValue(SgroupKey.CtabBracketStyle), is(1)); } }
@Test public void testReading0DStereochemistry() throws Exception { try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/tetrahedral-parity-withImplH.mol"))) { IAtomContainer container = mdlr.read(new AtomContainer()); Iterable<IStereoElement> selements = container.stereoElements(); Iterator<IStereoElement> siter = selements.iterator(); assertTrue(siter.hasNext()); IStereoElement se = siter.next(); assertThat(se, is(instanceOf(ITetrahedralChirality.class))); assertThat(((ITetrahedralChirality) se).getStereo(), is(ITetrahedralChirality.Stereo.CLOCKWISE)); assertThat(((ITetrahedralChirality) se).getLigands(), is(new IAtom[]{container.getAtom(1), container.getAtom(3), container.getAtom(4), container.getAtom(0)})); assertFalse(siter.hasNext()); } }
@Test public void testReading0DStereochemistryWithHydrogen() throws Exception { try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("/data/mdl/tetrahedral-parity-withExpH.mol"))) { IAtomContainer container = mdlr.read(new AtomContainer()); Iterable<IStereoElement> selements = container.stereoElements(); Iterator<IStereoElement> siter = selements.iterator(); assertTrue(siter.hasNext()); IStereoElement se = siter.next(); assertThat(se, is(instanceOf(ITetrahedralChirality.class))); assertThat(((ITetrahedralChirality) se).getStereo(), is(ITetrahedralChirality.Stereo.ANTI_CLOCKWISE)); assertThat(((ITetrahedralChirality) se).getLigands(), is(new IAtom[]{container.getAtom(0), container.getAtom(2), container.getAtom(3), container.getAtom(4)})); assertFalse(siter.hasNext()); } }
@Test(expected = CDKException.class) public void seaborgiumMassDelta() throws Exception { try (InputStream in = getClass().getResourceAsStream("seaborgium.mol"); MDLV2000Reader mdlr = new MDLV2000Reader(in, Mode.STRICT)) { IAtomContainer mol = mdlr.read(new AtomContainer()); } }
@Test public void seaborgiumAbsMass() throws Exception { try (InputStream in = getClass().getResourceAsStream("seaborgium_abs.mol"); MDLV2000Reader mdlr = new MDLV2000Reader(in, Mode.STRICT)) { IAtomContainer mol = mdlr.read(new AtomContainer()); assertThat(mol.getAtom(0).getMassNumber(), is(261)); } }
@Test public void testMassDiff() throws Exception { String mdl = "deuterium.mol\n" + "\n" + "\n" + " 1 0 0 0 0 1 V2000\n" + " 0.0000 0.0000 0.0000 H +1 0 0 0 0\n" + "M END\n"; try (StringReader in = new StringReader(mdl)) { MDLV2000Reader reader = new MDLV2000Reader(new StringReader(mdl), Mode.STRICT); IAtomContainer mol = reader.read(new AtomContainer()); IAtom atom = mol.getAtom(0); Assert.assertEquals(1, atom.getAtomicNumber().intValue()); Assert.assertEquals(2, atom.getMassNumber().intValue()); } }
@Test public void testBadAtomCoordinateFormat() throws Exception { final String mol = "\n" + "\n" + "\n" + " 17 18 0 0 0 0 1 V2000\n" + " -2.31474 0.50167 -3.30968 C 0 0 0 0 0 0\n" + " -2.43523 -0.19508 -2.16895 C 0 0 0 0 0 0\n" + " -1.14825 1.44681 -3.13364 C 0 0 0 0 0 0\n" + " -1.34937 0.28780 -1.23468 C 0 0 0 0 0 0\n" + " -1.31596 1.76332 -1.64443 C 0 0 0 0 0 0\n" + " 0.00411 -0.20225 -1.79002 C 0 0 0 0 0 0\n" + " 0.14340 0.60304 -3.11198 C 0 0 0 0 0 0\n" + " -1.14836 2.30981 -3.79997 H 0 0 0 0 0 0\n" + " 0.23461 -0.05636 -3.98172 H 0 0 0 0 0 0\n" + " 1.03045 1.24486 -3.06787 H 0 0 0 0 0 0\n" + " 0.82300 0.05318 -1.10813 H 0 0 0 0 0 0\n" + " 0.02443 -1.28271 -1.96799 H 0 0 0 0 0 0\n" + " -1.53255 0.09851 -0.17666 H 0 0 0 0 0 0\n" + " -0.46658 2.31096 -1.22026 H 0 0 0 0 0 0\n" + " -2.24286 2.30318 -1.41306 H 0 0 0 0 0 0\n" + " -3.13662 -0.99036 -1.96785 H 0 0 0 0 0 0\n" + " -2.90004 0.37818 -4.20802 H 0 0 0 0 0 0\n" + " 1 2 2 0 0 0\n" + " 1 3 1 0 0 0\n" + " 2 4 1 0 0 0\n" + " 4 5 1 0 0 0\n" + " 3 5 1 0 0 0\n" + " 4 6 1 0 0 0\n" + " 6 7 1 0 0 0\n" + " 7 3 1 0 0 0\n" + " 3 8 1 0 0 0\n" + " 7 9 1 0 0 0\n" + " 7 10 1 0 0 0\n" + " 6 11 1 0 0 0\n" + " 6 12 1 0 0 0\n" + " 4 13 1 0 0 0\n" + " 5 14 1 0 0 0\n" + " 5 15 1 0 0 0\n" + " 2 16 1 0 0 0\n" + " 1 17 1 0 0 0\n" + "M END\n" + "\n"; final MDLV2000Reader mdlv2000Reader = new MDLV2000Reader(new ByteArrayInputStream(mol.getBytes(StandardCharsets.UTF_8))); mdlv2000Reader.setReaderMode(IChemObjectReader.Mode.RELAXED); final org.openscience.cdk.silent.AtomContainer atomContainer = mdlv2000Reader.read(new org.openscience.cdk.silent.AtomContainer()); Assert.assertEquals(17, atomContainer.getAtomCount()); }
@Test public void test() throws Exception { String input = "\n" + "Structure query\n" + "\n" + " 1 0 0 0 0 0 0 0 0 0999 V2000\n" + " 2430.7100 2427.0000 0.0000 C 0 0 0 0 0 0\n" + "A 1\n" + "Blah\n" + "M END"; IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance(); try (MDLV2000Reader mdlr = new MDLV2000Reader(new StringReader(input))) { IAtomContainer mol = mdlr.read(bldr.newAtomContainer()); assertThat(mol.getAtom(0), instanceOf(IPseudoAtom.class)); assertThat(((IPseudoAtom)mol.getAtom(0)).getLabel(), is("Blah")); } }
@Test public void atomList() throws Exception { try (InputStream in = getClass().getResourceAsStream("query_atomlist.mol"); MDLV2000Reader mdlr = new MDLV2000Reader(in)) { IQueryAtomContainer mol = mdlr.read(new QueryAtomContainer(SilentChemObjectBuilder.getInstance())); IAtom deref = AtomRef.deref(mol.getAtom(0)); assertThat(deref, CoreMatchers.<IAtom>instanceOf(QueryAtom.class)); QueryAtom queryAtom = (QueryAtom) deref; Expr expr = queryAtom.getExpression(); Expr expected = new Expr(Expr.Type.ELEMENT, 9) .or(new Expr(Expr.Type.ELEMENT, 7)) .or(new Expr(Expr.Type.ELEMENT, 8)); assertThat(expr, is(expected)); } }
@Test public void legacyAtomList() throws Exception { try (InputStream in = getClass().getResourceAsStream("query_legacyatomlist.mol"); MDLV2000Reader mdlr = new MDLV2000Reader(in)) { IQueryAtomContainer mol = mdlr.read(new QueryAtomContainer(SilentChemObjectBuilder.getInstance())); IAtom deref = AtomRef.deref(mol.getAtom(0)); assertThat(deref, CoreMatchers.<IAtom>instanceOf(QueryAtom.class)); QueryAtom queryAtom = (QueryAtom) deref; Expr expr = queryAtom.getExpression(); Expr expected = new Expr(Expr.Type.ELEMENT, 9) .or(new Expr(Expr.Type.ELEMENT, 7)) .or(new Expr(Expr.Type.ELEMENT, 8)); assertThat(expr, is(expected)); } }
@Test public void notatomList() throws Exception { try (InputStream in = getClass().getResourceAsStream("query_notatomlist.mol"); MDLV2000Reader mdlr = new MDLV2000Reader(in)) { IQueryAtomContainer mol = mdlr.read(new QueryAtomContainer(SilentChemObjectBuilder.getInstance())); IAtom deref = AtomRef.deref(mol.getAtom(0)); assertThat(deref, CoreMatchers.<IAtom>instanceOf(QueryAtom.class)); QueryAtom queryAtom = (QueryAtom) deref; Expr expr = queryAtom.getExpression(); Expr expected = new Expr(Expr.Type.ELEMENT, 9) .or(new Expr(Expr.Type.ELEMENT, 7)) .or(new Expr(Expr.Type.ELEMENT, 8)); expected = expected.negate(); assertThat(expr, is(expected)); } }
@Test public void legacynotatomList() throws Exception { try (InputStream in = getClass().getResourceAsStream("query_legacynotatomlist.mol"); MDLV2000Reader mdlr = new MDLV2000Reader(in)) { IQueryAtomContainer mol = mdlr.read(new QueryAtomContainer(SilentChemObjectBuilder.getInstance())); IAtom deref = AtomRef.deref(mol.getAtom(0)); assertThat(deref, CoreMatchers.<IAtom>instanceOf(QueryAtom.class)); QueryAtom queryAtom = (QueryAtom) deref; Expr expr = queryAtom.getExpression(); Expr expected = new Expr(Expr.Type.ELEMENT, 9) .or(new Expr(Expr.Type.ELEMENT, 7)) .or(new Expr(Expr.Type.ELEMENT, 8)); expected = expected.negate(); assertThat(expr, is(expected)); } }
@Test public void sgroupsAbbrRoundTrip() throws IOException, CDKException { StringWriter sw = new StringWriter(); try (InputStream in = getClass().getResourceAsStream("sgroup-sup.mol3"); MDLV3000Reader mdlr = new MDLV3000Reader(in); MDLV2000Writer mdlw = new MDLV2000Writer(sw)) { IAtomContainer mol = SilentChemObjectBuilder.getInstance().newAtomContainer(); mol = mdlr.read(mol); mdlw.write(mol); } assertThat(sw.toString(), containsString("M SAL 1 2 2 3")); }
@Test public void checkFuseBondWithFewerBondsThanAtoms() throws IOException, CDKException { try (InputStream in = getClass().getResourceAsStream("potentialLateFuse.mol"); MDLV2000Reader reader = new MDLV2000Reader(in)) { IAtomContainer mol = reader.read(SilentChemObjectBuilder.getInstance().newAtomContainer()); assertThat(mol.getAtomCount(), is(108)); } }
@Test public void atomlistWithAtomContainer() throws Exception { try (InputStream in = getClass().getResourceAsStream("query_notatomlist.mol"); MDLV2000Reader mdlr = new MDLV2000Reader(in)) { IAtomContainer mol = mdlr.read(SilentChemObjectBuilder.getInstance().newAtomContainer()); IAtom deref = AtomRef.deref(mol.getAtom(0)); assertThat(deref, CoreMatchers.<IAtom>instanceOf(QueryAtom.class)); } }
@Test public void dataSgroup() { String path = "/data/mdl/hbr_acoh_mix.mol"; try (InputStream in = getClass().getResourceAsStream(path)) { MDLV2000Reader mdlr = new MDLV2000Reader(in); IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance(); IAtomContainer mol = mdlr.read(builder.newAtomContainer()); List<Sgroup> sgroups = mol.getProperty(CDKConstants.CTAB_SGROUPS); Sgroup dataSgroup = null; for (Sgroup sgroup : sgroups) { if (sgroup.getType() == SgroupType.CtabData) { dataSgroup = sgroup; break; } } assertNotNull(dataSgroup); assertThat(dataSgroup.<String>getValue(SgroupKey.DataFieldName), CoreMatchers.is("WEIGHT_PERCENT")); assertThat(dataSgroup.<String>getValue(SgroupKey.DataFieldUnits), CoreMatchers.is("%")); assertThat(dataSgroup.<String>getValue(SgroupKey.DataFieldFormat), CoreMatchers.is("N")); assertThat(dataSgroup.<String>getValue(SgroupKey.Data), CoreMatchers.is("33%")); } catch (IOException | CDKException e) { e.printStackTrace(); } }