Search in sources :

Example 1 with CMLWriter

use of org.openscience.cdk.io.CMLWriter in project ambit-mirror by ideaconsult.

the class TestCML method writeCML.

protected String writeCML(IAtomContainer mol) throws Exception {
    StringWriter output = new StringWriter();
    CMLWriter cmlwriter = new CMLWriter(output);
    cmlwriter.write(mol);
    cmlwriter.close();
    return output.toString();
}
Also used : StringWriter(java.io.StringWriter) CMLWriter(org.openscience.cdk.io.CMLWriter)

Example 2 with CMLWriter

use of org.openscience.cdk.io.CMLWriter 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 3 with CMLWriter

use of org.openscience.cdk.io.CMLWriter 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)

Example 4 with CMLWriter

use of org.openscience.cdk.io.CMLWriter in project cdk by cdk.

the class SmilesGeneratorTest method testSFBug1014344.

/**
 * @cdk.bug 1014344
 */
@Category(SlowTest.class)
// MDL -> CML (slow) -> SMILES round tripping
@Test
public void testSFBug1014344() throws Exception {
    String filename = "bug1014344-1.mol";
    InputStream ins = this.getClass().getResourceAsStream(filename);
    MDLReader reader = new MDLReader(ins, Mode.STRICT);
    IAtomContainer mol1 = reader.read(new AtomContainer());
    addImplicitHydrogens(mol1);
    SmilesGenerator sg = new SmilesGenerator();
    String molSmiles = sg.create(mol1);
    StringWriter output = new StringWriter();
    CMLWriter cmlWriter = new CMLWriter(output);
    cmlWriter.write(mol1);
    CMLReader cmlreader = new CMLReader(new ByteArrayInputStream(output.toString().getBytes()));
    IAtomContainer mol2 = ((IChemFile) cmlreader.read(new ChemFile())).getChemSequence(0).getChemModel(0).getMoleculeSet().getAtomContainer(0);
    addImplicitHydrogens(mol2);
    String cmlSmiles = sg.create(new AtomContainer(mol2));
    Assert.assertEquals(molSmiles, cmlSmiles);
}
Also used : MDLReader(org.openscience.cdk.io.MDLReader) CMLReader(org.openscience.cdk.io.CMLReader) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) StringWriter(java.io.StringWriter) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.AtomContainer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ChemFile(org.openscience.cdk.ChemFile) IChemFile(org.openscience.cdk.interfaces.IChemFile) CMLWriter(org.openscience.cdk.io.CMLWriter) Category(org.junit.experimental.categories.Category) SlowTest(org.openscience.cdk.test.SlowTest) Test(org.junit.Test)

Example 5 with CMLWriter

use of org.openscience.cdk.io.CMLWriter in project cdk by cdk.

the class CML2WriterTest method testHydrogenCount.

/**
 * Test example with one explicit carbon, and four implicit hydrogens.
 *
 * @cdk.bug 1655045
 */
@Test
public void testHydrogenCount() throws Exception {
    StringWriter writer = new StringWriter();
    // methane
    IAtomContainer molecule = new AtomContainer();
    molecule.addAtom(molecule.getBuilder().newInstance(IAtom.class, Elements.CARBON));
    molecule.getAtom(0).setImplicitHydrogenCount(4);
    CMLWriter cmlWriter = new CMLWriter(writer);
    cmlWriter.write(molecule);
    cmlWriter.close();
    logger.debug("****************************** testHydrogenCount()");
    logger.debug(writer.toString());
    logger.debug("******************************");
    Assert.assertTrue(writer.toString().contains("hydrogenCount=\"4\""));
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) StringWriter(java.io.StringWriter) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) AtomContainer(org.openscience.cdk.silent.AtomContainer) IAtom(org.openscience.cdk.interfaces.IAtom) CMLWriter(org.openscience.cdk.io.CMLWriter) Test(org.junit.Test)

Aggregations

StringWriter (java.io.StringWriter)27 CMLWriter (org.openscience.cdk.io.CMLWriter)27 Test (org.junit.Test)22 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)19 IChemFile (org.openscience.cdk.interfaces.IChemFile)7 IAtom (org.openscience.cdk.interfaces.IAtom)6 IReaction (org.openscience.cdk.interfaces.IReaction)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ChemFile (org.openscience.cdk.ChemFile)4 IReactionScheme (org.openscience.cdk.interfaces.IReactionScheme)4 CMLReader (org.openscience.cdk.io.CMLReader)4 AtomContainer (org.openscience.cdk.silent.AtomContainer)4 InputStream (java.io.InputStream)3 AtomContainer (org.openscience.cdk.AtomContainer)3 ReactionScheme (org.openscience.cdk.ReactionScheme)3 EmptyMoleculeException (ambit2.base.exceptions.EmptyMoleculeException)2 Atom (org.openscience.cdk.Atom)2 CDKException (org.openscience.cdk.exception.CDKException)2 IChemModel (org.openscience.cdk.interfaces.IChemModel)2 MDLReader (org.openscience.cdk.io.MDLReader)2