Merge branch 'release/v3.2.0'
[compound] / application.rb
1 # Java environment
2 ENV["JAVA_HOME"] = "/usr/lib/jvm/java-6-sun" unless ENV["JAVA_HOME"]
3 java_dir = File.join File.expand_path(File.dirname(__FILE__)),"java"
4 cdk = File.join java_dir, "cdk-1.3.5.jar"
5 jchempaint = File.join java_dir, "cdk-jchempaint-15.jar"
6 ENV["CLASSPATH"] = "#{ENV["CLASSPATH"]}:#{java_dir}:#{cdk}:#{jchempaint}"
7
8 require 'rubygems'
9 require 'rjb'
10 gem "opentox-ruby", "~> 4"
11 require 'opentox-ruby'
12
13 before do
14   @inchi = URI.unescape request.env['REQUEST_URI'].sub(/^\//,'').sub(/.*compound\//,'').sub(/\/smarts.*$/,'').sub(/\/image/,'').sub(/\?.*$/,'') # hack to avoid sinatra's URI/CGI unescaping, splitting, ..."
15   puts @inchi
16 end
17
18 # Display activating (red) and deactivating (green) substructures. Overlaps betwen activating and deactivating structures are marked in yellow.
19 # @example
20 #   curl http://webservices.in-silico.ch/compound/compound/InChI=1S/C6H5NO2/c8-7(9)6-4-2-1-3-5-6/h1-5H/smarts/activating/cN/ccN/deactivating/cc" > img.png
21 # @return [image/png] Image with highlighted substructures
22 get %r{/(.+)/smarts/activating/(.*)/deactivating/(.*)$} do |inchi,activating,deactivating| 
23   smiles = OpenTox::Compound.from_inchi(@inchi).to_smiles
24   activating = activating.to_s.split(/\//).collect{|s| s.gsub(/"/,'')}
25   deactivating = deactivating.to_s.split(/\//).collect{|s| s.gsub(/"/,'')}
26   content_type "image/png"
27   attachment "#{smiles}.png"
28   begin
29     Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
30     s = Rjb::import('Structure').new(smiles,150)
31     s.match_deactivating(deactivating)
32     s.match_activating(activating)
33     s.show
34   rescue => e
35     LOGGER.warn e.message
36   end
37 end
38
39 def png_from_smiles(smiles)
40   begin
41     Rjb.load(nil,["-Xmx64m","-Djava.awt.headless=true"])# avoid JVM memory allocation problems
42     Rjb::import('Structure').new(smiles,150).show
43   rescue
44     LOGGER.warn e.message
45   end
46 end
47
48 # Get png image
49 # @return [image/png] Image data
50 get %r{/(.+)/image} do |inchi| # catches all remaining get requests
51   smiles = OpenTox::Compound.from_inchi(@inchi).to_smiles
52   content_type "image/png"
53   attachment "#{smiles}.png"
54   png_from_smiles(smiles)
55 end
56
57 # Get compound representation
58 # @param [optinal, HEADER] Accept one of `chemical/x-daylight-smiles, chemical/x-inchi, chemical/x-mdl-sdfile, chemical/x-mdl-molfile, text/plain, image/gif, image/png`, defaults to chemical/x-daylight-smiles
59 # @example Get smiles
60 #   curl http://webservices.in-silico.ch/compound/InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H
61 # @example Get all known names 
62 #   curl -H "Accept:text/plain" http://webservices.in-silico.ch/compound/InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H
63 # @return [chemical/x-daylight-smiles, chemical/x-inchi, chemical/x-mdl-sdfile, chemical/x-mdl-molfile, text/plain, image/gif, image/png] Compound representation
64 get %r{/(.+)} do |inchi| # catches all remaining get requests
65   #inchi = URI.unescape request.env['REQUEST_URI'].sub(/^\//,'').sub(/.*compound\//,'') # hack to avoid sinatra's URI/CGI unescaping, splitting, ..."
66   case request.env['HTTP_ACCEPT']
67   when "*/*"
68     response['Content-Type'] = "chemical/x-daylight-smiles"
69     OpenTox::Compound.from_inchi(@inchi).to_smiles
70   when "chemical/x-daylight-smiles"
71     response['Content-Type'] = "chemical/x-daylight-smiles"
72     OpenTox::Compound.from_inchi(@inchi).to_smiles
73   when "chemical/x-inchi"
74     response['Content-Type'] = "chemical/x-inchi"
75     @inchi 
76   when "chemical/x-mdl-sdfile"
77     response['Content-Type'] = "chemical/x-mdl-sdfile"
78     OpenTox::Compound.from_inchi(@inchi).to_sdf
79   when "image/gif"
80     response['Content-Type'] = "image/gif"
81     OpenTox::Compound.from_inchi(@inchi).to_gif
82   when "image/png"
83     response['Content-Type'] = "image/png"
84     png_from_smiles(OpenTox::Compound.from_inchi(@inchi).to_smiles)
85   when "text/plain"
86     response['Content-Type'] = "text/plain"
87     uri = File.join @@cactus_uri,@inchi,"names"
88     RestClient.get(uri).body
89   else
90     raise OpenTox::BadRequestError.new "Unsupported MIME type '#{request.env['HTTP_ACCEPT']}'"
91   end
92 end
93
94 # Create a new compound URI (compounds are not saved at the compound service)
95 # @param [HEADER] Content-type one of `chemical/x-daylight-smiles, chemical/x-inchi, chemical/x-mdl-sdfile, chemical/x-mdl-molfile, text/plain`
96 # @example Create compound from Smiles string
97 #   curl -X POST -H "Content-type:chemical/x-daylight-smiles" --data "c1ccccc1" http://webservices.in-silico.ch/compound
98 # @example Create compound from name, uses an external lookup service and should work also with trade names, CAS numbers, ...
99 #   curl -X POST -H "Content-type:text/plain" --data "Benzene" http://webservices.in-silico.ch/compound
100 # @param [BODY] - string with identifier/data in selected Content-type
101 # @return [text/uri-list] compound URI
102 post '/?' do 
103
104   input = request.env["rack.input"].read
105   response['Content-Type'] = 'text/uri-list'
106   begin
107     case request.content_type
108     when /chemical\/x-daylight-smiles/
109       OpenTox::Compound.from_smiles(input).uri + "\n"
110     when /chemical\/x-inchi/
111       OpenTox::Compound.from_inchi(input).uri + "\n"
112     when /chemical\/x-mdl-sdfile|chemical\/x-mdl-molfile/
113       OpenTox::Compound.from_sdf(input).uri + "\n"
114     when /text\/plain/
115       OpenTox::Compound.from_name(input).uri + "\n"
116     else
117       status 400
118       "Unsupported MIME type '#{request.content_type}'"
119     end
120   rescue
121     status 400
122     "Cannot process request '#{input}' for content type '#{request.content_type}'"
123   end
124 end