Skip to content
Permalink
Browse files

Signature symmetry class viewer app

  • Loading branch information...
gilleain committed May 13, 2010
1 parent 2f8def0 commit e09ff2ec4a9470ad9f58259f269e5c79f07f7dbe
@@ -0,0 +1,70 @@
package app;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.vecmath.Point2d;

import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.renderer.RendererModel;
import org.openscience.cdk.renderer.elements.ElementGroup;
import org.openscience.cdk.renderer.elements.IRenderingElement;
import org.openscience.cdk.renderer.elements.OvalElement;
import org.openscience.cdk.renderer.generators.IAtomContainerGenerator;
import org.openscience.cdk.renderer.generators.IGeneratorParameter;
import org.openscience.cdk.signature.Orbit;

public class AtomSymmetryClassGenerator implements IAtomContainerGenerator {

private Map<Orbit, Color> orbitColorMap;

public AtomSymmetryClassGenerator() {
orbitColorMap = new HashMap<Orbit, Color>();
}

public void setOrbits(List<Orbit> orbits) {
orbitColorMap.clear();
int i = 0;
for (Orbit o : orbits) {
orbitColorMap.put(o, getColorForIndex(i));
i++;
}

}

public Color getColorForIndex(int i) {
switch (i) {
case 0: return Color.RED;
case 1: return Color.ORANGE;
case 2: return Color.YELLOW;
case 3: return Color.GREEN;
case 4: return Color.BLUE;
case 5: return Color.MAGENTA;
default: return Color.BLACK;
}
}

public IRenderingElement generate(IAtomContainer atomContainer,
RendererModel model) {
ElementGroup elements = new ElementGroup();
for (Orbit o : orbitColorMap.keySet()) {
Color color = orbitColorMap.get(o);
System.out.println("setting color " + color);
for (int i : o) {
IAtom a = atomContainer.getAtom(i);
Point2d p = a.getPoint2d();
elements.add(new OvalElement(p.x, p.y, 0.25, color));
}
}
return elements;
}

public List<IGeneratorParameter<?>> getParameters() {
return new ArrayList<IGeneratorParameter<?>>();
}

}
@@ -19,12 +19,9 @@
import org.openscience.cdk.layout.StructureDiagramGenerator;
import org.openscience.cdk.renderer.AtomContainerRenderer;
import org.openscience.cdk.renderer.RendererModel;
import org.openscience.cdk.renderer.elements.IRenderingElement;
import org.openscience.cdk.renderer.font.AWTFontManager;
import org.openscience.cdk.renderer.generators.BasicAtomGenerator;
import org.openscience.cdk.renderer.generators.BoundsGenerator;
import org.openscience.cdk.renderer.generators.IAtomContainerGenerator;
import org.openscience.cdk.renderer.generators.IGeneratorParameter;
import org.openscience.cdk.renderer.selection.IChemObjectSelection;
import org.openscience.cdk.renderer.visitor.AWTDrawVisitor;

@@ -43,7 +40,14 @@
private BasicAtomGenerator basicAtomGenerator;

public MoleculePanel(int panelWidth, int panelHeight) {
renderer = new AtomContainerRenderer(getGenerators(), new AWTFontManager());
this(panelWidth, panelHeight, new ArrayList<IAtomContainerGenerator>());
}

public MoleculePanel(int panelWidth, int panelHeight,
List<IAtomContainerGenerator> initialGenerators) {
initialGenerators.addAll(getGenerators());
renderer = new AtomContainerRenderer(
initialGenerators, new AWTFontManager());
setRenderingParameters();
sdg = new StructureDiagramGenerator();
this.setPreferredSize(new Dimension(panelWidth, panelHeight));
@@ -76,16 +80,6 @@ private void setRenderingParameters() {
private List<IAtomContainerGenerator> getGenerators() {
List<IAtomContainerGenerator> generators =
new ArrayList<IAtomContainerGenerator>();
generators.add(new IAtomContainerGenerator() {
public IRenderingElement generate(IAtomContainer ac, RendererModel m) {
return new BoundsGenerator().generate((IMolecule)ac, m);
}

public List<IGeneratorParameter<?>> getParameters() {
return new ArrayList<IGeneratorParameter<?>>();
}

});
// generators.add(new RingGenerator());
// generators.add(new BasicBondGenerator());
generators.add(new TmpBondGenerator());
@@ -146,6 +140,10 @@ public void setMolecule(IMolecule molecule) {
repaint();
}

public void setMoleculeWithoutLayout(IMolecule molecule) {
this.molecule = molecule;
repaint();
}

private IMolecule diagramGenerate(IMolecule molecule) {
this.sdg.setMolecule(molecule, true);
@@ -0,0 +1,130 @@
package app;

import java.awt.BorderLayout;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.openscience.cdk.Molecule;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.io.ISimpleChemObjectReader;
import org.openscience.cdk.io.MDLReader;
import org.openscience.cdk.io.ReaderFactory;
import org.openscience.cdk.renderer.generators.IAtomContainerGenerator;
import org.openscience.cdk.signature.MoleculeSignature;
import org.openscience.cdk.signature.Orbit;

public class SignatureClassViewer extends JFrame implements ListSelectionListener {

private MoleculePanel moleculePanel;

private TreeThumbViewer treeThumbViewer;

private Map<String, Orbit> orbitMap;

private AtomSymmetryClassGenerator atomSymmetryClassGenerator;

public SignatureClassViewer(String[] args) {
setLayout(new BorderLayout());

List<IAtomContainerGenerator> initialGenerators =
new ArrayList<IAtomContainerGenerator>();
atomSymmetryClassGenerator = new AtomSymmetryClassGenerator();
initialGenerators.add(atomSymmetryClassGenerator);

moleculePanel = new MoleculePanel(700, 700, initialGenerators);
add(moleculePanel, BorderLayout.CENTER);

treeThumbViewer = new TreeThumbViewer(700, 700);
add(treeThumbViewer, BorderLayout.EAST);
treeThumbViewer.addSelectionListener(this);

orbitMap = new HashMap<String, Orbit>();

String filename;
if (args.length != 0) {
filename = args[0];
loadFile(filename);
}
pack();
setVisible(true);
}

public void loadFile(String filename) {
try {
ReaderFactory factory = new ReaderFactory();
ISimpleChemObjectReader reader =
new MDLReader(new FileReader(filename));
// factory.createReader(new FileReader(filename));
if (reader == null) return;
IMolecule molecule = reader.read(new Molecule());
System.out.println("read");
moleculePanel.setMoleculeWithoutLayout(molecule);
for (int i = 0; i < molecule.getAtomCount(); i++) {
List<IAtom> connected =
molecule.getConnectedAtomsList(molecule.getAtom(i));
System.out.print(connected.size() + " " + (i + 1) + " ");
for (IAtom a : connected) {
int j = molecule.getAtomNumber(a) + 1;
System.out.print(j + ",");
}
System.out.println();
}
makeSignatures(molecule);


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CDKException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void makeSignatures(IMolecule molecule) {
MoleculeSignature molSig = new MoleculeSignature(molecule);
List<Orbit> orbits = molSig.calculateOrbits();
orbitMap.clear();
for (Orbit o : orbits) {
String sig = o.getLabel();
treeThumbViewer.addSignature(sig);
orbitMap.put(sig, o);
}
}

public void displaySelectedOrbits(List<Orbit> orbits) {
atomSymmetryClassGenerator.setOrbits(orbits);
repaint();
}


public static void main(String[] args) {
// new SignatureClassViewer(args);
new SignatureClassViewer(new String[] { "/Users/maclean/fullerene_28.mol"});
}

public void valueChanged(ListSelectionEvent e) {
List<String> selectedList = treeThumbViewer.getSelected();
List<Orbit> selectedOrbits = new ArrayList<Orbit>();
for (String selected : selectedList) {
System.out.println("selected " + orbitMap.get(selected));
selectedOrbits.add(orbitMap.get(selected));
}
displaySelectedOrbits(selectedOrbits);
}

}
@@ -0,0 +1,54 @@
package app;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

import signature.AbstractVertexSignature;
import signature.ColoredTree;
import signature.display.DisplayableColoredTree;

public class SignatureTreeListCellRenderer extends JPanel implements
ListCellRenderer {

private String signature;

public SignatureTreeListCellRenderer(int w, int h) {
setPreferredSize(new Dimension(w, h));
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof String) {
signature = (String) value;
}

if (isSelected) {
setBackground(Color.RED);
setForeground(Color.WHITE);
} else {
setBackground(Color.WHITE);
setForeground(Color.BLACK);
}

return this;
}

public void paint(Graphics g) {
super.paint(g);
if (signature != null) {
int w = getWidth() - 2;
int h = getHeight() - 2;
ColoredTree tree = AbstractVertexSignature.parse(signature);
DisplayableColoredTree displayTree = new DisplayableColoredTree(w, h);
displayTree.makeFromColoredTree(tree);
displayTree.paint(g);
}
}

}
@@ -0,0 +1,67 @@
package app;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;

public class TreeThumbViewer extends JPanel {

public int numberOfPanels;

public int thumbWidth;

public int thumbHeight;

private JScrollPane scrollPane;

private JList list;

public TreeThumbViewer(int w, int h) {
numberOfPanels = 0;
thumbWidth = 300;
thumbHeight = 200;
list = new JList(new DefaultListModel());
list.setCellRenderer(new SignatureTreeListCellRenderer(
thumbWidth, thumbHeight));
scrollPane = new JScrollPane(list,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

add(scrollPane);
Dimension d = new Dimension(w, h);
scrollPane.setPreferredSize(d);
setPreferredSize(d);
}

public void addSelectionListener(ListSelectionListener listener) {
list.addListSelectionListener(listener);
}

public List<String> getSelected() {
List<String> selected = new ArrayList<String>();
for (Object o : list.getSelectedValues()) {
if (o instanceof String) {
selected.add((String) o);
}
}
return selected;
}

public void addSignature(String signature) {
((DefaultListModel)list.getModel()).addElement(signature);
repaint();
}

public void clear() {
((DefaultListModel)list.getModel()).clear();
repaint();
}

}

0 comments on commit e09ff2e

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.