Debian Codegraph @

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

raw
1 package org.guha.rcdk.view;
2
3 import org.openscience.cdk.DefaultChemObjectBuilder;
4 import org.openscience.cdk.exception.CDKException;
5 import org.openscience.cdk.interfaces.IAtomContainer;
6 import org.openscience.cdk.smiles.SmilesParser;
7
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10
11 /**
12 * Generate a chemical structure depiction in a graphic image format.
13 *
14 * @author Rajarshi Guha
15 */
16 public 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 }