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

http://github.com/rajarshi/cdkr · Java · 106 lines · 83 code · 17 blank · 6 comment · 13 complexity · 7dfc94091ad978bfb4662acbcec32145 MD5 · raw file

  1. package org.guha.rcdk.view;
  2. import org.guha.rcdk.util.Misc;
  3. import org.guha.rcdk.view.panels.MoleculeCell;
  4. import org.openscience.cdk.DefaultChemObjectBuilder;
  5. import org.openscience.cdk.exception.CDKException;
  6. import org.openscience.cdk.interfaces.IAtomContainer;
  7. import org.openscience.cdk.layout.StructureDiagramGenerator;
  8. import org.openscience.cdk.smiles.SmilesParser;
  9. import javax.swing.*;
  10. import java.awt.*;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. /**
  15. * A one line summary.
  16. *
  17. * @author Rajarshi Guha
  18. */
  19. public class MoleculeDisplay extends JPanel {
  20. int ncol = 2;
  21. int nrow = 2;
  22. int sep = 2;
  23. int width = 200;
  24. int height = 200;
  25. int nmol = 0;
  26. GridLayout layout;
  27. public MoleculeDisplay() {
  28. layout = new GridLayout(nrow, ncol, sep, sep);
  29. setLayout(layout);
  30. }
  31. public void setParams(String paramString) throws CDKException {
  32. parseParamString(paramString);
  33. }
  34. private void parseParamString(String paramString) throws CDKException {
  35. String[] lines = paramString.split("\n");
  36. for (String s : lines) {
  37. String[] toks = s.split("=");
  38. if (toks.length != 2) throw new CDKException("Invalid parameter string");
  39. String varName = toks[0].trim();
  40. String varValue = toks[1].trim();
  41. if (varName.equals("ncol")) ncol = Integer.parseInt(varValue);
  42. else if (varName.equals("nrow")) nrow = Integer.parseInt(varValue);
  43. else if (varName.equals("sep")) sep = Integer.parseInt(varValue);
  44. else if (varName.equals("width")) width = Integer.parseInt(varValue);
  45. else if (varName.equals("height")) height = Integer.parseInt(varValue);
  46. }
  47. layout = new GridLayout(nrow, ncol, sep, sep);
  48. }
  49. public void addMolecule(IAtomContainer molecule) throws IOException, CDKException {
  50. MoleculeCell cell = new MoleculeCell(molecule, Misc.getDefaultDepictor());
  51. add(cell);
  52. }
  53. public void addLabel(String label) {
  54. add(new JLabel(label));
  55. }
  56. public int getMoleculeCount() {
  57. return nmol;
  58. }
  59. public static void main(String[] args) throws CDKException, IOException {
  60. MoleculeDisplay md = new MoleculeDisplay();
  61. SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
  62. String[] smiles = {"CC(C)N(C(=O)CCl)c1ccccc1", "CC(C)N(C(=O)CCl)C1=CC=CC=C1", "CC(C)N(C(=O)CCl)C1CC=CCC1"};
  63. List<IAtomContainer> mols = new ArrayList<IAtomContainer>();
  64. IAtomContainer mol;
  65. for (String s : smiles) {
  66. mol = sp.parseSmiles(s);
  67. StructureDiagramGenerator sdg = new StructureDiagramGenerator();
  68. sdg.setMolecule(mol);
  69. try {
  70. sdg.generateCoordinates();
  71. } catch (Exception e) {
  72. }
  73. mol = sdg.getMolecule();
  74. mols.add(mol);
  75. }
  76. // md.setParams("ncol=2\nnrow=2");
  77. md.addMolecule(mols.get(0));
  78. md.addMolecule(mols.get(1));
  79. md.addMolecule(mols.get(2));
  80. md.addLabel("Foo");
  81. md.addLabel("Bar");
  82. md.addLabel("Baz");
  83. JFrame f = new JFrame("Molecule Display");
  84. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85. f.getContentPane().add(md);
  86. f.pack();
  87. f.setVisible(true);
  88. }
  89. }