summaryrefslogtreecommitdiff
path: root/application.rb
blob: 9191a522f801d47200d6af1a5b358652f3c13a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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__)),"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}"

require 'rubygems'
require 'rjb'
gem "opentox-ruby-api-wrapper", "= 1.6.1"
require 'opentox-ruby-api-wrapper'

#set :lock, true # avoid JVM memory allocation problems
# -Xmx64m

get "/display/activating/(.+)$" 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_activating(params["smarts"])
  s.show
end

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"
  Rjb.load(nil,["-Xmx64m"])# avoid JVM memory allocation problems
  s = Rjb::import('Structure').new(smiles,150)
  s.match(smarts)
  s.show
end

get %r{/smiles/(.+)} do |smiles| 
  content_type "image/png"
  attachment "#{smiles}.png"
  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.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
  inchi = URI.unescape request.env['REQUEST_URI'].sub(/^\//,'').sub(/.*compound\//,'') # hack to avoid sinatra's URI/CGI unescaping, splitting, ..."
  case request.env['HTTP_ACCEPT']
  when "*/*"
    response['Content-Type'] = "chemical/x-daylight-smiles"
    OpenTox::Compound.new(:inchi => inchi).smiles
  when "chemical/x-daylight-smiles"
    response['Content-Type'] = "chemical/x-daylight-smiles"
    OpenTox::Compound.new(:inchi => inchi).smiles
  when "chemical/x-inchi"
    response['Content-Type'] = "chemical/x-inchi"
    inchi 
  when "chemical/x-mdl-sdfile"
    response['Content-Type'] = "chemical/x-mdl-sdfile"
    OpenTox::Compound.new(:inchi => inchi).sdf
  when "image/gif"
    response['Content-Type'] = "image/gif"
    OpenTox::Compound.new(:inchi => inchi).gif
  when "image/png"
    response['Content-Type'] = "image/png"
    OpenTox::Compound.new(:inchi => inchi).png
  when "text/plain"
    response['Content-Type'] = "text/plain"
    uri = File.join @@cactus_uri,inchi,"names"
    RestClient.get(uri).body
  else
    halt 400, "Unsupported MIME type '#{request.env['HTTP_ACCEPT']}'"
  end
end

post '/?' do 

  input = request.env["rack.input"].read
  response['Content-Type'] = 'text/uri-list'
  begin
    case request.content_type
    when /chemical\/x-daylight-smiles/
      OpenTox::Compound.new(:smiles => input).uri + "\n"
    when /chemical\/x-inchi/
      OpenTox::Compound.new(:inchi => input).uri + "\n"
    when /chemical\/x-mdl-sdfile|chemical\/x-mdl-molfile/
      OpenTox::Compound.new(:sdf => input).uri + "\n"
    when /text\/plain/
      OpenTox::Compound.new(:name => input).uri + "\n"
    else
      status 400
      "Unsupported MIME type '#{request.content_type}'"
    end
  rescue
    status 400
    "Cannot process request '#{input}' for content type '#{request.content_type}'"
  end
end