summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2010-08-06 13:32:35 +0200
committerChristoph Helma <helma@in-silico.ch>2010-08-06 13:32:35 +0200
commit1512ccc16307fc214328e894f25d417d8c851605 (patch)
tree7bd257adcc0e841e9b3cf5f671c2306ab93cad40
parentd381360584e3cde2a66a2a478c674266253b86f1 (diff)
java libs moved from public to root
-rw-r--r--application.rb77
-rw-r--r--java/Makefile (renamed from public/java/Makefile)0
-rw-r--r--java/Structure.java56
-rw-r--r--java/cdk-1.3.5.jarbin0 -> 9978895 bytes
-rw-r--r--java/cdk-jchempaint-15.jarbin0 -> 1890835 bytes
-rw-r--r--public/java/Structure.java164
6 files changed, 107 insertions, 190 deletions
diff --git a/application.rb b/application.rb
index e0e469d..09e447a 100644
--- a/application.rb
+++ b/application.rb
@@ -1,6 +1,6 @@
# Java environment
ENV["JAVA_HOME"] = "/usr/lib/jvm/java-6-sun" unless ENV["JAVA_HOME"]
-java_dir = File.join File.expand_path(File.dirname(__FILE__)),"public/java"
+java_dir = File.join File.expand_path(File.dirname(__FILE__)),"java"
cdk = File.join java_dir, "cdk-1.3.5.jar"
jchempaint = File.join java_dir, "cdk-jchempaint-15.jar"
ENV["CLASSPATH"] = "#{ENV["CLASSPATH"]}:#{java_dir}:#{cdk}:#{jchempaint}"
@@ -10,19 +10,78 @@ require 'rjb'
gem "opentox-ruby-api-wrapper", "= 1.6.0"
require 'opentox-ruby-api-wrapper'
-post "/display" do
+#set :lock, true # avoid JVM memory allocation problems
+# -Xmx64m
+
+get "/display/activating/(.+)$" do
content_type "image/png"
attachment "#{params["smiles"]}.png"
- s = Rjb::import('Structure').new(params["smiles"],200)
+ Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
+ s = Rjb::import('Structure').new(params["smiles"],150)
s.match_activating(params["smarts"])
- s.match_deactivating(["CC"])
s.show
end
-get %r{/smiles/(.+)/smarts/(.*)} do |smiles,smarts|
+post "/display/deactivating" do
+ content_type "image/png"
+ attachment "#{params["smiles"]}.png"
+ Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
+ s = Rjb::import('Structure').new(params["smiles"],150)
+ s.match_deactivating(params["smarts"])
+ s.show
+end
+
+get %r{/smiles/(.+)/smarts/activating/(.*)/deactivating/(.*)/highlight/(.*)$} do |smiles,activating,deactivating,smarts|
+ activating = activating.to_s.split(/\//).collect{|s| s.gsub(/"/,'')}
+ deactivating = deactivating.to_s.split(/\//).collect{|s| s.gsub(/"/,'')}
+ content_type "image/png"
+ attachment "#{smiles}.png"
+ Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
+ s = Rjb::import('Structure').new(smiles,150)
+ s.match_deactivating(deactivating) unless deactivating.empty?
+ s.match_activating(activating) unless activating.empty?
+ s.match(smarts)
+ s.show
+ #s = nil
+end
+
+get %r{/smiles/(.+)/smarts/activating/(.*)/deactivating/(.*)$} do |smiles,activating,deactivating|
+ activating = activating.to_s.split(/\//).collect{|s| s.gsub(/"/,'')}
+ deactivating = deactivating.to_s.split(/\//).collect{|s| s.gsub(/"/,'')}
+ content_type "image/png"
+ attachment "#{smiles}.png"
+ Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
+ s = Rjb::import('Structure').new(smiles,150)
+ s.match_deactivating(deactivating)
+ s.match_activating(activating)
+ s.show
+ #s = nil
+end
+
+get %r{/smiles/(.+)/smarts/(.*)/(.*activating)$} do |smiles,allsmarts,effect|
+ LOGGER.debug "String:"
+ LOGGER.debug allsmarts
+ smarts = allsmarts.to_s.split(/\//)
+ smarts.collect!{|s| s.gsub(/"/,'')}
+ LOGGER.debug "Smarts:"
+ LOGGER.debug smarts.to_yaml
+ content_type "image/png"
+ attachment "#{smiles}.png"
+ Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
+ s = Rjb::import('Structure').new(smiles,150)
+ if effect == "activating"
+ s.match_activating(smarts)
+ elsif effect == "deactivating"
+ s.match_deactivating(smarts)
+ end
+ s.show
+end
+
+get %r{/smiles/(.+)/smarts/(.*)$} do |smiles,smarts|
content_type "image/png"
attachment "#{smiles}.png"
- s = Rjb::import('Structure').new(smiles,200)
+ Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
+ s = Rjb::import('Structure').new(smiles,150)
s.match(smarts)
s.show
end
@@ -30,14 +89,16 @@ end
get %r{/smiles/(.+)} do |smiles|
content_type "image/png"
attachment "#{smiles}.png"
- Rjb::import('Structure').new(smiles,200).show
+ Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
+ Rjb::import('Structure').new(smiles,150).show
end
get %r{/(.+)/image} do |inchi| # catches all remaining get requests
smiles = OpenTox::Compound.new(:inchi => inchi).smiles
content_type "image/png"
attachment "#{smiles}.png"
- Rjb::import('Structure').new(smiles,200).show
+ Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
+ Rjb::import('Structure').new(smiles,150).show
end
get %r{/(.+)} do |inchi| # catches all remaining get requests
diff --git a/public/java/Makefile b/java/Makefile
index bffc3d8..bffc3d8 100644
--- a/public/java/Makefile
+++ b/java/Makefile
diff --git a/java/Structure.java b/java/Structure.java
index d1ac188..af0f3b4 100644
--- a/java/Structure.java
+++ b/java/Structure.java
@@ -2,6 +2,7 @@ import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
+import java.lang.Math;
import java.io.*;
import java.awt.*;
@@ -35,6 +36,11 @@ public class Structure{
StructureDiagramGenerator sdg = new StructureDiagramGenerator();
SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
+
+ List<IChemObject> matchingBonds = new ArrayList<IChemObject>();
+ List<IChemObject> activatingBonds = new ArrayList<IChemObject>();
+ List<IChemObject> deactivatingBonds = new ArrayList<IChemObject>();
+
Map<IChemObject,Color> coloredMatches = new HashMap<IChemObject,Color>();
Renderer renderer;
@@ -75,7 +81,17 @@ public class Structure{
public byte[] show() {
try {
+ // set colors
+ for (int i = 0; i < matchingBonds.size(); i++) {
+ float red = 0;
+ float green = 0;
+ IChemObject bond = (IChemObject) matchingBonds.get(i);
+ if (activatingBonds.contains(bond)) { red = 1; };
+ if (deactivatingBonds.contains(bond)) { green = 1; };
+ coloredMatches.put((IChemObject) bond , new Color(red,green,0));
+ }
renderer.getRenderer2DModel().set( RendererModel.ColorHash.class, coloredMatches );
+ // render and write
renderer.paintMoleculeSet(moleculeSet, new AWTDrawVisitor(g2), drawArea, true);
ImageIO.write(image, "png", out);
}
@@ -86,14 +102,16 @@ public class Structure{
private void layout() {
try {
Rectangle2D last = new Rectangle(0,0);
- for (int i = 0; i < moleculeSet.getMoleculeCount(); i++) {
+ //for (int i = 0; i < moleculeSet.getMoleculeCount(); i++) {
+ // reverse iteration to show small molecules at the right side
+ for (int i = moleculeSet.getMoleculeCount()-1; i >= 0 ; i--) {
IAtomContainer mol = moleculeSet.getMolecule(i);
sdg.setMolecule((IMolecule) mol);
sdg.generateCoordinates();
mol = sdg.getMolecule();
- GeometryTools.translateAllPositive(mol);
// get size of previous mol and shift to the right
- last = GeometryTools.shiftContainer(mol, GeometryTools.getRectangle2D(mol), last, 5);
+ // gives nasty results for single atom molecules, but works otherwise
+ last = GeometryTools.shiftContainer(mol, GeometryTools.getRectangle2D(mol), last, 0);
coordinated_mols[i] = (IMolecule) mol;
}
moleculeSet.setMolecules(coordinated_mols);
@@ -103,25 +121,22 @@ public class Structure{
public void match_activating(String[] smarts) {
for (int i = 0; i < smarts.length; i++) {
- match(smarts[i],Color.RED);
+ match(smarts[i],true);
}
}
public void match_deactivating(String[] smarts) {
for (int i = 0; i < smarts.length; i++) {
- match(smarts[i],Color.GREEN);
+ match(smarts[i],false);
}
}
- public void match(String smarts) { match(smarts, Color.BLUE); }
-
- public void match(String smarts, Color color) {
+ public void match(String smarts) { match(smarts, true); }
- IAtom a1 = new Atom();
- IAtom a2 = new Atom();
- IBond bond = new Bond();
+ public void match(String smarts, Boolean active) {
try {
+ int count;
SMARTSQueryTool querytool = new SMARTSQueryTool(smarts);
// iterate over molecule set
for (int i = 0; i < moleculeSet.getMoleculeCount(); i++) {
@@ -131,19 +146,25 @@ public class Structure{
boolean status = querytool.matches(mol);
if (status) {
List matches = querytool.getUniqueMatchingAtoms();
- //System.out.print("Matches: ");
- //System.out.println(matches);
// iterate over all matches
for (int j = 0; j < matches.size(); j++) {
List atomIndices = (List) matches.get(j);
// itrate over all atoms
for (int k = 0; k < atomIndices.size(); k++) {
- a1 = mol.getAtom( (Integer) atomIndices.get(k));
+ IAtom a1 = mol.getAtom( (Integer) atomIndices.get(k));
// find bonds
for (int l = k + 1; l < atomIndices.size(); l++) {
- a2 = mol.getAtom( (Integer) atomIndices.get(l));
- bond = mol.getBond(a1,a2);
- if (bond != null) { coloredMatches.put((IChemObject) bond , color); }
+ IAtom a2 = mol.getAtom( (Integer) atomIndices.get(l));
+ IChemObject bond = (IChemObject) mol.getBond(a1,a2);
+ if (bond != null) {
+ // collect all/active/inactive bonds
+ if (!matchingBonds.contains(bond)) { matchingBonds.add(bond); }
+ if (active) {
+ if (!activatingBonds.contains(bond)) { activatingBonds.add(bond); }
+ } else {
+ if (!deactivatingBonds.contains(bond)) { deactivatingBonds.add(bond); }
+ }
+ }
}
}
}
@@ -152,7 +173,6 @@ public class Structure{
}
catch (Exception exc) { exc.printStackTrace(); }
-
}
}
diff --git a/java/cdk-1.3.5.jar b/java/cdk-1.3.5.jar
new file mode 100644
index 0000000..7d80934
--- /dev/null
+++ b/java/cdk-1.3.5.jar
Binary files differ
diff --git a/java/cdk-jchempaint-15.jar b/java/cdk-jchempaint-15.jar
new file mode 100644
index 0000000..7078c2b
--- /dev/null
+++ b/java/cdk-jchempaint-15.jar
Binary files differ
diff --git a/public/java/Structure.java b/public/java/Structure.java
deleted file mode 100644
index c46ea05..0000000
--- a/public/java/Structure.java
+++ /dev/null
@@ -1,164 +0,0 @@
-import java.util.List;
-import java.util.*;
-import java.io.*;
-
-import java.awt.*;
-import java.awt.image.*;
-import java.awt.geom.*;
-
-import javax.imageio.*;
-
-import org.openscience.cdk.*;
-import org.openscience.cdk.interfaces.*;
-import org.openscience.cdk.layout.*;
-import org.openscience.cdk.renderer.*;
-import org.openscience.cdk.renderer.font.*;
-import org.openscience.cdk.renderer.generators.*;
-import org.openscience.cdk.renderer.visitor.*;
-import org.openscience.cdk.renderer.selection.*;
-import org.openscience.cdk.templates.*;
-import org.openscience.cdk.smiles.*;
-import org.openscience.cdk.smiles.smarts.*;
-import org.openscience.cdk.graph.*;
-import org.openscience.cdk.geometry.*;
-
-public class Structure{
-
- int size;
- List generators = new ArrayList();
-
- IMolecule molecule = new Molecule();
- IMoleculeSet moleculeSet;
- IMolecule[] coordinated_mols;
-
- StructureDiagramGenerator sdg = new StructureDiagramGenerator();
- SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
- Renderer renderer;
-
- BufferedImage image;
- Rectangle drawArea;
- Graphics2D g2;
-
- LogicalSelection selection = new LogicalSelection(LogicalSelection.Type.ALL);
- MoleculeSet selectionMoleculeSet = new MoleculeSet();
- ChemModel selectionChemModel = new ChemModel();
-
- ByteArrayOutputStream out = new ByteArrayOutputStream();
-
- public Structure (String smiles, int imageSize) {
-
- size = imageSize;
-
- // generators make the image elements
- generators.add(new BasicSceneGenerator());
- generators.add(new RingGenerator());
- generators.add(new BasicBondGenerator());
- generators.add(new BasicAtomGenerator());
- generators.add(new SelectBondGenerator());
- generators.add(new SelectAtomGenerator());
-
- renderer = new Renderer(generators, new AWTFontManager());
- renderer.getRenderer2DModel().set( SelectBondGenerator.SelectionBondColor.class,Color.RED);
-
- try { molecule = sp.parseSmiles(smiles); }
- catch (Exception ex) { ex.printStackTrace(); }
-
- moleculeSet = ConnectivityChecker.partitionIntoMolecules(molecule);
- coordinated_mols = new IMolecule[moleculeSet.getMoleculeCount()];
-
- drawArea = new Rectangle(size, size);
- image = new BufferedImage(size, size , BufferedImage.TYPE_INT_RGB);
-
- g2 = (Graphics2D)image.getGraphics();
- g2.setColor(Color.WHITE);
- g2.fillRect(0, 0, size, size);
-
- layout();
-
- }
-
- public byte[] show() {
- try {
- // create a fake ChemModel to make the LogicalSelection happy
- selectionChemModel.setMoleculeSet(selectionMoleculeSet);
- selection.select(selectionChemModel);
- renderer.getRenderer2DModel().setSelection(selection);
- renderer.paintMoleculeSet(moleculeSet, new AWTDrawVisitor(g2), drawArea, true);
- ImageIO.write(image, "png", out);
- }
- catch (Exception ex) { ex.printStackTrace(); }
- return out.toByteArray();
- }
-
- private void layout() {
- try {
- Rectangle2D last = new Rectangle(0,0);
- for (int i = 0; i < moleculeSet.getMoleculeCount(); i++) {
- IAtomContainer mol = moleculeSet.getMolecule(i);
- sdg.setMolecule((IMolecule) mol);
- sdg.generateCoordinates();
- mol = sdg.getMolecule();
- GeometryTools.translateAllPositive(mol);
- // get size of previous mol and shift to the right
- last = GeometryTools.shiftContainer(mol, GeometryTools.getRectangle2D(mol), last,2);
- coordinated_mols[i] = (IMolecule) mol;
- }
- moleculeSet.setMolecules(coordinated_mols);
- }
- catch (Exception ex) { ex.printStackTrace(); }
- }
-
- public void match_activating(String[] smarts) {
- for (int i = 0; i < smarts.length; i++) {
- match(smarts[i]);
- }
- }
-
- public void match_deactivating(String[] smarts) {
- renderer.getRenderer2DModel().set( SelectBondGenerator.SelectionBondColor.class,Color.GREEN);
- for (int i = 0; i < smarts.length; i++) {
- match(smarts[i]);
- }
- }
-
- public void match(String smarts) {
-
- try {
- SMARTSQueryTool querytool = new SMARTSQueryTool(smarts);
- // iterate over molecule set
- for (int i = 0; i < moleculeSet.getMoleculeCount(); i++) {
- IAtomContainer mol = moleculeSet.getMolecule(i);
- ChemModel fragment = new ChemModel();
- // match smarts
- boolean status = querytool.matches(mol);
- if (status) {
- List matches = querytool.getUniqueMatchingAtoms();
- //System.out.print("Matches: ");
- //System.out.println(matches);
- // iterate over all matches
- for (int j = 0; j < matches.size(); j++) {
- IAtomContainer selectionContainer = new AtomContainer();
- List atomIndices = (List) matches.get(j);
- // itrate over all atoms
- for (int k = 0; k < atomIndices.size(); k++) {
- IAtom a1 = mol.getAtom( (Integer) atomIndices.get(k));
- if (!selectionContainer.contains(a1)) { selectionContainer.addAtom(a1); }
- // find bonds
- for (int l = k + 1; l < atomIndices.size(); l++) {
- IAtom a2 = mol.getAtom( (Integer) atomIndices.get(l));
- IBond bond = mol.getBond(a1,a2);
- if (bond != null) { selectionContainer.addBond(bond); }
- }
- }
- // create a fake MoleculeSet to make the LogicalSelection happy
- selectionMoleculeSet.addMolecule(new Molecule(selectionContainer));
- }
- }
- }
-
- }
- catch (Exception exc) { exc.printStackTrace(); }
-
- }
-
-}