Search in sources :

Example 16 with ChemFile

use of org.openscience.cdk.ChemFile in project cdk by cdk.

the class MoleculeFactory method loadMolecule.

public static IAtomContainer loadMolecule(String inFile) {
    ChemFile chemFile;
    IChemSequence chemSequence;
    IChemModel chemModel;
    IAtomContainerSet setOfMolecules;
    IAtomContainer molecule = null;
    try (FileInputStream fis = new FileInputStream(inFile);
        MDLReader mr = new MDLReader(fis)) {
        chemFile = (ChemFile) mr.read((ChemObject) new ChemFile());
        mr.close();
        chemSequence = chemFile.getChemSequence(0);
        chemModel = chemSequence.getChemModel(0);
        setOfMolecules = chemModel.getMoleculeSet();
        molecule = setOfMolecules.getAtomContainer(0);
        for (int i = 0; i < molecule.getAtomCount(); i++) {
            molecule.getAtom(i).setPoint2d(null);
        }
    } catch (CDKException | IOException exc) {
        // we just return null if something went wrong
        logger.error("An exception occurred while loading a molecule: " + inFile);
        logger.debug(exc);
    }
    return molecule;
}
Also used : MDLReader(org.openscience.cdk.io.MDLReader) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) ChemFile(org.openscience.cdk.ChemFile) IChemModel(org.openscience.cdk.interfaces.IChemModel) IOException(java.io.IOException) IChemSequence(org.openscience.cdk.interfaces.IChemSequence) FileInputStream(java.io.FileInputStream)

Example 17 with ChemFile

use of org.openscience.cdk.ChemFile in project cdk by cdk.

the class Mol2ReaderTest method testMultiMol.

@Test
public void testMultiMol() throws Exception {
    Assume.assumeTrue(runSlowTests());
    String filename = "actives.mol2";
    logger.info("Testing: ", filename);
    InputStream ins = this.getClass().getResourceAsStream(filename);
    Mol2Reader reader = new Mol2Reader(ins);
    IChemFile chemFile = reader.read(new ChemFile());
    reader.close();
    List<IAtomContainer> mols = ChemFileManipulator.getAllAtomContainers(chemFile);
    Assert.assertEquals(30, mols.size());
    Assert.assertEquals(25, mols.get(0).getAtomCount());
    Assert.assertEquals(24, mols.get(29).getAtomCount());
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) IChemFile(org.openscience.cdk.interfaces.IChemFile) ChemFile(org.openscience.cdk.ChemFile) IChemFile(org.openscience.cdk.interfaces.IChemFile) SimpleChemObjectReaderTest(org.openscience.cdk.test.io.SimpleChemObjectReaderTest) SlowTest(org.openscience.cdk.test.SlowTest) Test(org.junit.Test)

Example 18 with ChemFile

use of org.openscience.cdk.ChemFile in project cdk by cdk.

the class PCCompoundASNReaderTest method testReading.

@Test
public void testReading() throws Exception {
    String filename = "cid1.asn";
    logger.info("Testing: " + filename);
    InputStream ins = this.getClass().getResourceAsStream(filename);
    PCCompoundASNReader reader = new PCCompoundASNReader(ins);
    IChemFile cFile = reader.read(new ChemFile());
    reader.close();
    List<IAtomContainer> containers = ChemFileManipulator.getAllAtomContainers(cFile);
    Assert.assertEquals(1, containers.size());
    Assert.assertTrue(containers.get(0) instanceof IAtomContainer);
    IAtomContainer molecule = containers.get(0);
    Assert.assertNotNull(molecule);
    // check atom stuff
    Assert.assertEquals(31, molecule.getAtomCount());
    Assert.assertNotNull(molecule.getAtom(3));
    Assert.assertEquals("O", molecule.getAtom(3).getSymbol());
    Assert.assertNotNull(molecule.getAtom(4));
    Assert.assertEquals("N", molecule.getAtom(4).getSymbol());
    // check bond stuff
    Assert.assertEquals(30, molecule.getBondCount());
    Assert.assertNotNull(molecule.getBond(3));
    Assert.assertEquals(molecule.getAtom(2), molecule.getBond(3).getBegin());
    Assert.assertEquals(molecule.getAtom(11), molecule.getBond(3).getEnd());
    // some extracted props
    Assert.assertEquals("InChI=1/C9H17NO4/c1-7(11)14-8(5-9(12)13)6-10(2,3)4/h8H,5-6H2,1-4H3", molecule.getProperty(CDKConstants.INCHI));
    Assert.assertEquals("CC(=O)OC(CC(=O)[O-])C[N+](C)(C)C", molecule.getProperty(CDKConstants.SMILES));
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) InputStream(java.io.InputStream) IChemFile(org.openscience.cdk.interfaces.IChemFile) IChemFile(org.openscience.cdk.interfaces.IChemFile) ChemFile(org.openscience.cdk.ChemFile) SimpleChemObjectReaderTest(org.openscience.cdk.test.io.SimpleChemObjectReaderTest) Test(org.junit.Test)

Example 19 with ChemFile

use of org.openscience.cdk.ChemFile in project cdk by cdk.

the class PMPReaderTest method testTwoAceticAcid.

@Test
public void testTwoAceticAcid() throws Exception {
    String filename = "two_aceticacid.pmp";
    InputStream ins = this.getClass().getResourceAsStream(filename);
    PMPReader reader = new PMPReader(ins);
    ChemFile chemFile = (ChemFile) reader.read((ChemObject) new ChemFile());
    reader.close();
    Assert.assertNotNull(chemFile);
    Assert.assertEquals(1, chemFile.getChemSequenceCount());
    IChemSequence seq = chemFile.getChemSequence(0);
    Assert.assertNotNull(seq);
    Assert.assertEquals(2, seq.getChemModelCount());
    IChemModel model = seq.getChemModel(0);
    Assert.assertNotNull(model);
    ICrystal crystal = model.getCrystal();
    Assert.assertNotNull(crystal);
    Assert.assertEquals(32, crystal.getAtomCount());
    Assert.assertEquals(28, crystal.getBondCount());
    model = seq.getChemModel(1);
    Assert.assertNotNull(model);
    crystal = model.getCrystal();
    Assert.assertNotNull(crystal);
    Assert.assertEquals(32, crystal.getAtomCount());
    Assert.assertEquals(28, crystal.getBondCount());
}
Also used : ICrystal(org.openscience.cdk.interfaces.ICrystal) ChemObject(org.openscience.cdk.ChemObject) InputStream(java.io.InputStream) ChemFile(org.openscience.cdk.ChemFile) IChemModel(org.openscience.cdk.interfaces.IChemModel) IChemSequence(org.openscience.cdk.interfaces.IChemSequence) SimpleChemObjectReaderTest(org.openscience.cdk.test.io.SimpleChemObjectReaderTest) Test(org.junit.Test)

Example 20 with ChemFile

use of org.openscience.cdk.ChemFile in project cdk by cdk.

the class ReaderFactoryTest method testReadGzWithGzipDetection.

@Test
public void testReadGzWithGzipDetection() throws Exception {
    String filename = "org/openscience/cdk/io/bf3.xyz.gz";
    InputStream input = this.getClass().getClassLoader().getResourceAsStream(filename);
    // ok, if format ok, try instantiating a reader
    ISimpleChemObjectReader reader = factory.createReader(input);
    Assert.assertNotNull(reader);
    Assert.assertEquals(((IChemFormat) XYZFormat.getInstance()).getReaderClassName(), reader.getClass().getName());
    // now try reading something from it
    IChemFile chemFile = reader.read(new ChemFile());
    IAtomContainer molecule = new AtomContainer();
    for (IAtomContainer container : ChemFileManipulator.getAllAtomContainers(chemFile)) {
        molecule.add(container);
    }
    Assert.assertNotNull(molecule);
    Assert.assertEquals(4, molecule.getAtomCount());
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IChemFile(org.openscience.cdk.interfaces.IChemFile) ChemFile(org.openscience.cdk.ChemFile) IChemFile(org.openscience.cdk.interfaces.IChemFile) Test(org.junit.Test)

Aggregations

ChemFile (org.openscience.cdk.ChemFile)188 InputStream (java.io.InputStream)171 Test (org.junit.Test)171 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)154 IChemFile (org.openscience.cdk.interfaces.IChemFile)112 ChemObject (org.openscience.cdk.ChemObject)68 SimpleChemObjectReaderTest (org.openscience.cdk.test.io.SimpleChemObjectReaderTest)66 IChemSequence (org.openscience.cdk.interfaces.IChemSequence)57 IChemModel (org.openscience.cdk.interfaces.IChemModel)55 MDLV2000Reader (org.openscience.cdk.io.MDLV2000Reader)52 CMLReader (org.openscience.cdk.io.CMLReader)41 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)33 ByteArrayInputStream (java.io.ByteArrayInputStream)30 ISimpleChemObjectReader (org.openscience.cdk.io.ISimpleChemObjectReader)25 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)23 List (java.util.List)21 IAtom (org.openscience.cdk.interfaces.IAtom)19 SlowTest (org.openscience.cdk.test.SlowTest)18 DoubleArrayResult (org.openscience.cdk.qsar.result.DoubleArrayResult)15 IReaction (org.openscience.cdk.interfaces.IReaction)11