SMILESReader extends DefaultChemObjectReader { @Override public <T extends IChemObject> T read(T object) throws CDKException { sp = new SmilesParser(object.getBuilder()); if (object instanceof IAtomContainerSet) { return (T) readAtomContainerSet((IAtomContainerSet) object); } else if (object instanceof IChemFile) { IChemFile file = (IChemFile) object; IChemSequence sequence = file.getBuilder().newInstance(IChemSequence.class); IChemModel chemModel = file.getBuilder().newInstance(IChemModel.class); chemModel.setMoleculeSet(readAtomContainerSet(file.getBuilder().newInstance(IAtomContainerSet.class))); sequence.addChemModel(chemModel); file.addChemSequence(sequence); return (T) file; } else { throw new CDKException("Only supported is reading of MoleculeSet objects."); } } SMILESReader(Reader input);  SMILESReader(InputStream input);  SMILESReader(); @Override IResourceFormat getFormat(); @Override void setReader(Reader input); @Override void setReader(InputStream input); @Override boolean accepts(Class<? extends IChemObject> classObject); @Override T read(T object); @Override void close();  }
@Test public void testReading() throws Exception { String filename = "data/smiles/smiles.smi"; logger.info("Testing: " + filename); InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); SMILESReader reader = new SMILESReader(ins); IAtomContainerSet som = reader.read(new AtomContainerSet()); Assert.assertEquals(8, som.getAtomContainerCount()); }
@Test public void testReadingSmiFile_1() throws Exception { String filename = "data/smiles/smiles.smi"; logger.info("Testing: " + filename); InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); SMILESReader reader = new SMILESReader(ins); IAtomContainerSet som = reader.read(new AtomContainerSet()); String name = null; IAtomContainer thisMol = som.getAtomContainer(0); name = ((String) thisMol.getProperty("SMIdbNAME")).toString(); Assert.assertEquals("benzene", name); }
@Test public void testReadingSmiFile_2() throws Exception { String filename = "data/smiles/smiles.smi"; logger.info("Testing: " + filename); InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); SMILESReader reader = new SMILESReader(ins); IAtomContainerSet som = reader.read(new AtomContainerSet()); IAtomContainer thisMol = som.getAtomContainer(1); Assert.assertNull(thisMol.getProperty("SMIdbNAME")); }
@Test public void testReadingSmiFile_3() throws Exception { String filename = "data/smiles/test3.smi"; logger.info("Testing: " + filename); InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); SMILESReader reader = new SMILESReader(ins); IAtomContainerSet som = reader.read(new AtomContainerSet()); Assert.assertEquals(5, som.getAtomContainerCount()); }
@Test public void badSmilesLine() throws CDKException { IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance(); String input = "C\nn1cccc1\nc1ccccc1\n"; DefaultChemObjectReader cor = new SMILESReader(new StringReader(input)); IAtomContainerSet mols = cor.read(bldr.newInstance(IAtomContainerSet.class)); assertThat(mols.getAtomContainerCount(), is(3)); assertThat(mols.getAtomContainer(0).getAtomCount(), is(1)); assertThat(mols.getAtomContainer(0).getProperty(IteratingSMILESReader.BAD_SMILES_INPUT), nullValue()); assertThat(mols.getAtomContainer(1).getAtomCount(), is(0)); assertThat(mols.getAtomContainer(1).getProperty(IteratingSMILESReader.BAD_SMILES_INPUT), notNullValue()); assertThat(mols.getAtomContainer(2).getAtomCount(), is(6)); assertThat(mols.getAtomContainer(2).getProperty(IteratingSMILESReader.BAD_SMILES_INPUT), nullValue()); }