From 1512ccc16307fc214328e894f25d417d8c851605 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 6 Aug 2010 13:32:35 +0200 Subject: java libs moved from public to root --- application.rb | 77 ++++++++++++++++++--- java/Makefile | 6 ++ java/Structure.java | 56 +++++++++++----- java/cdk-1.3.5.jar | Bin 0 -> 9978895 bytes java/cdk-jchempaint-15.jar | Bin 0 -> 1890835 bytes public/java/Makefile | 6 -- public/java/Structure.java | 164 --------------------------------------------- 7 files changed, 113 insertions(+), 196 deletions(-) create mode 100644 java/Makefile create mode 100644 java/cdk-1.3.5.jar create mode 100644 java/cdk-jchempaint-15.jar delete mode 100644 public/java/Makefile delete mode 100644 public/java/Structure.java 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/java/Makefile b/java/Makefile new file mode 100644 index 0000000..bffc3d8 --- /dev/null +++ b/java/Makefile @@ -0,0 +1,6 @@ +all: Structure.class + +%.class: %.java + javac -classpath ./:./cdk-1.3.5.jar:./cdk-jchempaint-15.jar $< + #javac -classpath ./:./cdk-1.2.5.jar $< + #javac -Xlint:deprecation -Xlint:unchecked -classpath ./:./cdk-1.3.5.jar:./cdk-jchempaint-15.jar $< 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 matchingBonds = new ArrayList(); + List activatingBonds = new ArrayList(); + List deactivatingBonds = new ArrayList(); + Map coloredMatches = new HashMap(); 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 Binary files /dev/null and b/java/cdk-1.3.5.jar differ diff --git a/java/cdk-jchempaint-15.jar b/java/cdk-jchempaint-15.jar new file mode 100644 index 0000000..7078c2b Binary files /dev/null and b/java/cdk-jchempaint-15.jar differ diff --git a/public/java/Makefile b/public/java/Makefile deleted file mode 100644 index bffc3d8..0000000 --- a/public/java/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -all: Structure.class - -%.class: %.java - javac -classpath ./:./cdk-1.3.5.jar:./cdk-jchempaint-15.jar $< - #javac -classpath ./:./cdk-1.2.5.jar $< - #javac -Xlint:deprecation -Xlint:unchecked -classpath ./:./cdk-1.3.5.jar:./cdk-jchempaint-15.jar $< 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(); } - - } - -} -- cgit v1.2.3