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