PageRenderTime 142ms CodeModel.GetById 50ms app.highlight 4ms RepoModel.GetById 87ms app.codeStats 0ms

/rcdkjar/src/org/guha/rcdk/view/MoleculeImage.java

http://github.com/rajarshi/cdkr
Java | 52 lines | 29 code | 8 blank | 15 comment | 0 complexity | 54fa24c61da90215d99edc2abed0ffb2 MD5 | raw file
 1package org.guha.rcdk.view;
 2
 3import org.openscience.cdk.DefaultChemObjectBuilder;
 4import org.openscience.cdk.exception.CDKException;
 5import org.openscience.cdk.interfaces.IAtomContainer;
 6import org.openscience.cdk.smiles.SmilesParser;
 7
 8import java.io.FileOutputStream;
 9import java.io.IOException;
10
11/**
12 * Generate a chemical structure depiction in a graphic image format.
13 *
14 * @author Rajarshi Guha
15 */
16public class MoleculeImage {
17    private IAtomContainer molecule;
18    private RcdkDepictor depictor;
19
20    public MoleculeImage(IAtomContainer molecule, RcdkDepictor depictor) throws Exception {
21        this.molecule = molecule;
22        this.depictor = depictor;
23    }
24
25    /**
26     * Get image as a byte array.
27     *
28     * @param width  output width
29     * @param height output height
30     * @param fmt    image format (png, jpeg, svg, pdf, gif)
31     * @return
32     * @throws IOException
33     * @throws CDKException
34     */
35    public byte[] getBytes(int width, int height, String fmt) throws IOException, CDKException {
36
37        depictor.setWidth(width);
38        depictor.setHeight(height);
39        return depictor.getFormat(molecule, fmt);
40    }
41
42    public static void main(String[] args) throws Exception {
43        SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
44        IAtomContainer mol = sp.parseSmiles("c1ccccc1CC(=O)C1COCNC1");
45
46        RcdkDepictor depictor = new RcdkDepictor(300, 300, 1.3, "cow", "off", "reagents", true, false, 100, "");
47        MoleculeImage mi = new MoleculeImage(mol, depictor);
48        byte[] bytes = mi.getBytes(300, 300, "png");
49        FileOutputStream fos = new FileOutputStream("test.png");
50        fos.write(bytes);
51    }
52}