Search in sources :

Example 1 with IChemFile

use of org.openscience.cdk.interfaces.IChemFile in project ambit-mirror by ideaconsult.

the class MoleculeTools method readCMLMolecule.

public static IAtomContainer readCMLMolecule(InputStream in) throws Exception {
    IAtomContainer mol = null;
    CMLReader reader = new CMLReader(in);
    IChemFile obj = null;
    obj = (IChemFile) reader.read(newChemFile(SilentChemObjectBuilder.getInstance()));
    int n = obj.getChemSequenceCount();
    if (n > 1)
        logger.finest("> 1 sequence in a record");
    for (int j = 0; j < n; j++) {
        IChemSequence seq = obj.getChemSequence(j);
        int m = seq.getChemModelCount();
        if (m > 1)
            logger.finest("> 1 model in a record");
        for (int k = 0; k < m; k++) {
            IChemModel mod = seq.getChemModel(k);
            IAtomContainerSet som = mod.getMoleculeSet();
            if (som.getAtomContainerCount() > 1)
                logger.finest("> 1 molecule in a record");
            for (int l = 0; l < som.getAtomContainerCount(); l++) {
                mol = som.getAtomContainer(l);
                return mol;
            }
        }
    }
    reader = null;
    return mol;
}
Also used : CMLReader(org.openscience.cdk.io.CMLReader) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IChemFile(org.openscience.cdk.interfaces.IChemFile) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IChemModel(org.openscience.cdk.interfaces.IChemModel) IChemSequence(org.openscience.cdk.interfaces.IChemSequence)

Example 2 with IChemFile

use of org.openscience.cdk.interfaces.IChemFile in project ambit-mirror by ideaconsult.

the class TestCML method parseCMLString.

public static IChemFile parseCMLString(String cmlString) throws Exception {
    IChemFile chemFile = null;
    CMLReader reader = new CMLReader(new ByteArrayInputStream(cmlString.getBytes()));
    chemFile = (IChemFile) reader.read(new org.openscience.cdk.ChemFile());
    return chemFile;
}
Also used : CMLReader(org.openscience.cdk.io.CMLReader) ByteArrayInputStream(java.io.ByteArrayInputStream) IChemFile(org.openscience.cdk.interfaces.IChemFile)

Example 3 with IChemFile

use of org.openscience.cdk.interfaces.IChemFile in project ambit-mirror by ideaconsult.

the class TestCML method testCML_singlePropertyForAllAtoms.

@Test
public void testCML_singlePropertyForAllAtoms() throws Exception {
    String smiles = "CCCC";
    IAtomContainer mol = parser.parseSmiles(smiles);
    for (int i = 0; i < mol.getAtomCount(); i++) {
        String id = Integer.toString(i + 1);
        mol.getAtom(i).setProperty("Prop" + id, id);
        mol.getAtom(i).setID(id);
    }
    printAtomProperties(mol);
    for (int i = 0; i < mol.getAtomCount(); i++) {
        String id = Integer.toString(i + 1);
        Assert.assertEquals(id, mol.getAtom(i).getProperty("Prop" + id));
        Assert.assertEquals(1, mol.getAtom(i).getProperties().size());
    }
    String cmlcode = writeCML(mol);
    logger.info(cmlcode);
    IChemFile chemFile = parseCMLString(cmlcode);
    IAtomContainer mol2 = chemFile.getChemSequence(0).getChemModel(0).getMoleculeSet().getAtomContainer(0);
    printAtomProperties(mol2);
    for (int i = 0; i < mol2.getAtomCount(); i++) {
        String id = mol2.getAtom(i).getID();
        Assert.assertEquals(id, mol2.getAtom(i).getProperty("Prop" + id));
        Assert.assertEquals(1, mol2.getAtom(i).getProperties().size());
    }
    logger.info(writeCML(mol2));
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IChemFile(org.openscience.cdk.interfaces.IChemFile) Test(org.junit.Test)

Example 4 with IChemFile

use of org.openscience.cdk.interfaces.IChemFile in project ambit-mirror by ideaconsult.

the class TestCML method testCML1.

public static void testCML1(String smiles) throws Exception {
    IAtomContainer mol = SmartsHelper.getMoleculeFromSmiles(smiles);
    mol.getAtom(0).setProperty("Prop1", "Test1");
    mol.getAtom(0).setProperty("Prop2", "Test2");
    printAtomProperties(mol.getAtom(0));
    try {
        System.out.println("Writing " + smiles + " to CML ");
        StringWriter output = new StringWriter();
        CMLWriter cmlwriter = new CMLWriter(output);
        cmlwriter.write(mol);
        cmlwriter.close();
        String cmlcode = output.toString();
        System.out.println(cmlcode);
        IChemFile chemFile = parseCMLString(cmlcode);
        IAtomContainer mol2 = chemFile.getChemSequence(0).getChemModel(0).getMoleculeSet().getAtomContainer(0);
        printAtomProperties(mol2.getAtom(0));
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) StringWriter(java.io.StringWriter) IChemFile(org.openscience.cdk.interfaces.IChemFile) CMLWriter(org.openscience.cdk.io.CMLWriter)

Example 5 with IChemFile

use of org.openscience.cdk.interfaces.IChemFile in project ambit-mirror by ideaconsult.

the class TestCML method testCML2.

public static void testCML2(String smiles) throws Exception {
    IAtomContainer mol = SmartsHelper.getMoleculeFromSmiles(smiles);
    printAromaticity(mol);
    try {
        System.out.println("Writing " + smiles + " to CML ");
        StringWriter output = new StringWriter();
        CMLWriter cmlwriter = new CMLWriter(output);
        cmlwriter.write(mol);
        cmlwriter.close();
        String cmlcode = output.toString();
        System.out.println(cmlcode);
        IChemFile chemFile = parseCMLString(cmlcode);
        IAtomContainer mol2 = chemFile.getChemSequence(0).getChemModel(0).getMoleculeSet().getAtomContainer(0);
        printAromaticity(mol2);
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) StringWriter(java.io.StringWriter) IChemFile(org.openscience.cdk.interfaces.IChemFile) CMLWriter(org.openscience.cdk.io.CMLWriter)

Aggregations

IChemFile (org.openscience.cdk.interfaces.IChemFile)239 Test (org.junit.Test)205 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)174 InputStream (java.io.InputStream)108 IChemSequence (org.openscience.cdk.interfaces.IChemSequence)75 CMLReader (org.openscience.cdk.io.CMLReader)72 ChemFile (org.openscience.cdk.ChemFile)70 IChemModel (org.openscience.cdk.interfaces.IChemModel)67 IAtom (org.openscience.cdk.interfaces.IAtom)54 SimpleChemObjectReaderTest (org.openscience.cdk.test.io.SimpleChemObjectReaderTest)37 ByteArrayInputStream (java.io.ByteArrayInputStream)29 IBond (org.openscience.cdk.interfaces.IBond)24 SlowTest (org.openscience.cdk.test.SlowTest)22 IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)19 ChemFile (org.openscience.cdk.silent.ChemFile)18 IReaction (org.openscience.cdk.interfaces.IReaction)15 MDLV2000Reader (org.openscience.cdk.io.MDLV2000Reader)14 CDKException (org.openscience.cdk.exception.CDKException)12 StringWriter (java.io.StringWriter)10 IOException (java.io.IOException)9