PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

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