diff options
author | Christoph Helma <helma@in-silico.ch> | 2012-12-07 11:42:12 +0100 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2012-12-07 11:42:12 +0100 |
commit | 17e783b5d159f205f0de59c6c522f5de5b8c9a6e (patch) | |
tree | 0b9f3602770ec9388b109b8c83e832a61216878a | |
parent | c38f9f2e4c2dec4ef163c6c76d64f5dbf4974a5a (diff) |
working version without proxy
-rw-r--r-- | Gemfile | 2 | ||||
-rw-r--r-- | application.rb | 128 | ||||
-rw-r--r-- | config.ru | 6 | ||||
-rw-r--r-- | pubchem.rb | 28 | ||||
-rw-r--r-- | views/assays.haml | 13 | ||||
-rw-r--r-- | views/compound.haml | 65 | ||||
-rw-r--r-- | views/layout.haml | 7 | ||||
-rw-r--r-- | views/neighbors.haml | 58 | ||||
-rw-r--r-- | views/nontargets.haml | 3 | ||||
-rw-r--r-- | views/not_found.haml | 2 | ||||
-rw-r--r-- | views/predicted_assays.haml | 19 | ||||
-rw-r--r-- | views/predicted_targets.haml | 21 | ||||
-rw-r--r-- | views/targets.haml | 17 |
13 files changed, 186 insertions, 183 deletions
@@ -2,5 +2,7 @@ source :gemcutter gemspec gem "haml" gem "dalli" +gem "rack-cache" +gem "rest-client-components" gem "opentox-server", :path => "../opentox-server" gem "opentox-client", :path => "../opentox-client" diff --git a/application.rb b/application.rb index a3d46c3..68bb8d2 100644 --- a/application.rb +++ b/application.rb @@ -1,70 +1,36 @@ require "./pubchem.rb" require 'rack/session/dalli' +require "rack/cache" module OpenTox class Application < Service set :static, true set :root, File.dirname(__FILE__) also_reload './pubchem.rb' - #enable :sessions - use Rack::Session::Dalli, :cache => Dalli::Client.new @@pug_uri = "http://pubchem.ncbi.nlm.nih.gov/rest/pug/" - helpers do - -=begin - def pubchem_search url - attempts = 0 - result = nil - begin - attempts += 1 - json = RestClient.get url, :timeout => 90000000 - result = JSON.parse json - while result["Waiting"] do - sleep 2 - listkey = result["Waiting"]["ListKey"] - result = pubchem_search File.join(@pug_uri, "compound", "listkey", listkey, "cids", "JSON") - end - rescue - if $!.message =~ /Timeout/i and attempts < 4 - sleep 2 - retry - elsif $!.message =~ /Timeout/i and attempts >= 4 - File.open("timeouts","a+"){|f| f.puts url} - puts url - puts $!.message - elsif $!.message.match /404/ - #not_found_error #TODO - else - puts url - puts $!.message - end - end - end -=end - - def image_uri cid - File.join @@pug_uri, "compound", "cid", cid, "PNG"#?record_type=3d&image_size=small" - end - + before do + #cache_control :public, :max_age => 3600 end before '/cid/:cid/*' do + #cache_control :public, :max_age => 3600 session[:compound] = PubChemCompound.new params[:cid] unless session[:compound] and session[:compound].cid == params[:cid] end get '/?' do + #cache_control :public, :no_cache haml :index end get '/cid/:cid/?' do - session[:compound] = PubChemCompound.new params[:cid] haml :compound end get '/search/?' do - #begin - cids = RestClientWrapper.get(File.join(@@pug_uri,"compound","name",URI.escape(params[:name]),"cids","TXT")).split("\n") + #cache_control :public, :no_cache + begin + cids = RestClient.get(File.join(@@pug_uri,"compound","name",CGI.escape(params[:name]),"cids","TXT")).split("\n") if cids.size == 1 session[:compound] = PubChemCompound.new cids.first haml :compound @@ -72,96 +38,74 @@ module OpenTox @compounds = cids.collect{|cid| PubChemCompound.new cid } haml :select end - #rescue - #haml :not_found - #end + rescue + haml :not_found + end end get '/cid/:cid/targets/?' do - if params[:cid] == session[:compound].cid - @assays = session[:compound].targets - else - @assays = PubChemCompound.new(params[:cid]).targets - end + @assays = session[:compound].targets haml :targets, :layout => false end get '/cid/:cid/nontargets/?' do - if params[:cid] == session[:compound].cid - @assays = session[:compound].non_targets - else - @assays = PubChemCompound.new(params[:cid]).non_targets - end + @assays = session[:compound].non_targets haml :targets, :layout => false end get '/cid/:cid/other_active_assays/?' do - if params[:cid] == session[:compound].cid - @assays = session[:compound].active_assays - session[:compound].targets - else - compound = PubChemCompound.new(params[:cid]) - @assays = compound.active_assays - compound.targets - end + @assays = session[:compound].active_assays - session[:compound].targets haml :assays, :layout => false end get '/cid/:cid/other_inactive_assays/?' do - if params[:cid] == session[:compound].cid - @assays = session[:compound].inactive_assays - session[:compound].non_targets - else - compound = PubChemCompound.new(params[:cid]) - @assays = compound.inactive_assays - compound.non_targets - end + @assays = session[:compound].inactive_assays - session[:compound].non_targets haml :assays, :layout => false end get '/cid/:cid/predicted_targets/?' do - if params[:cid] == session[:compound].cid - @assays = session[:compound].predicted_targets - else - @assays = PubChemCompound.new(params[:cid]).predicted_targets - end + @assays = session[:compound].predicted_targets haml :predicted_targets, :layout => false end get '/cid/:cid/predicted_nontargets/?' do - if params[:cid] == session[:compound].cid - @assays = session[:compound].predicted_non_targets - else - @assays = PubChemCompound.new(params[:cid]).predicted_non_targets - end + @assays = session[:compound].predicted_non_targets haml :predicted_targets, :layout => false end get '/cid/:cid/other_predicted_active_assays/?' do - if params[:cid] == session[:compound].cid - @assays = session[:compound].predicted_active_assays - session[:compound].predicted_targets - else - compound = PubChemCompound.new(params[:cid]) - @assays = compound.predicted_active_assays - compound.predicted_targets - end + @assays = session[:compound].predicted_active_assays - session[:compound].predicted_targets haml :predicted_assays, :layout => false end get '/cid/:cid/other_predicted_inactive_assays/?' do - if params[:cid] == session[:compound].cid - @assays = session[:compound].predicted_inactive_assays - session[:compound].predicted_non_targets - else - compound = PubChemCompound.new(params[:cid]) - @assays = compound.predicted_inactive_assays - compound.predicted_non_targets - end - haml :assays, :layout => false + @assays = session[:compound].predicted_inactive_assays - session[:compound].predicted_non_targets + haml :predicted_assays, :layout => false end get '/cid/:cid/neighbors/?' do haml :neighbors, :layout => false end - get '/cid/:cid/cosine/:cid2/?' do - session[:compound].cosine(PubChemCompound.new(params[:cid2])).to_s + session[:compound].cosine(PubChemCompound.new(params[:cid2])).round(3).to_s end +=begin + get '/aid/:aid/?' do + puts File.join(@@pug_uri, "assay", "aid", params[:aid].to_s, "description", "JSON") + json = RestClient.get File.join(@@pug_uri, "assay", "aid", params[:aid].to_s, "description", "JSON") + @description = JSON.parse(json)["PC_AssayContainer"][0]["assay"]["descr"] + haml :assay_description, :layout => false + end + + get '/pubchem_proxy/*' do |path| + puts path.inspect + puts "http://pubchem.ncbi.nlm.nih.gov/rest/pug/#{path}" + RestClientWrapper.get "http://pubchem.ncbi.nlm.nih.gov/rest/pug/#{path}" + end +=end + get '/fp/?' do @fp = [] YAML.load_file("false_positives.yaml").each do |pred| @@ -1,6 +1,10 @@ SERVICE = "aop" require 'bundler' Bundler.require -timeout = 600 require './application.rb' +use Rack::Session::Dalli, :cache => Dalli::Client.new +#use Rack::Cache, +# :verbose => true, +# :metastore => "memcached://127.0.0.1:11211/meta", +# :entitystore => "memcached://127.0.0.1:11211/body" run OpenTox::Application @@ -1,6 +1,13 @@ require '../opentox-client/lib/opentox-client.rb' require 'json' require 'base64' +require 'restclient/components' +require 'rack/cache' +#RestClient.enable Rack::Cache, :verbose => true#, :allow_reload => true, :allow_revalidate => true +RestClient.enable Rack::Cache, + :verbose => true, + :metastore => 'file:/tmp/cache/meta', + :entitystore => 'file:/tmp/cache/body' def Math.gauss(x, sigma = 0.3) d = 1.0 - x.to_f @@ -15,10 +22,11 @@ module OpenTox attr_accessor :similarity, :p, :assays def initialize cid=nil - @pug_uri = "http://pubchem.ncbi.nlm.nih.gov/rest/pug/" + #@pug_uri = "http://pubchem.ncbi.nlm.nih.gov/rest/pug/" + @pug_uri = "http://localhost:8081/" @cid = cid @assays = nil - @similarity_threshold = 85 + @similarity_threshold = 90 @neighbors = nil @predicted_assays = nil #@predicted_targets = nil @@ -42,7 +50,7 @@ module OpenTox pug_uri = "http://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name" compounds = [] session[:name] = name - cid = RestClientWrapper.get(File.join(pug_uri,URI.escape(name),"cids","TXT")) + cid = RestClient.get(File.join(pug_uri,URI.escape(name),"cids","TXT")) #puts response #response.split("\n") do |cid| puts cid @@ -53,6 +61,10 @@ module OpenTox compounds end + def name + RestClient.get File.join(@pug_uri, "compound", "cid", cid.to_s, "property", "IUPACName","TXT").chomp + end + def neighbors unless @neighbors @neighbors = [] @@ -63,11 +75,11 @@ module OpenTox result = pubchem_search File.join(@pug_uri, "compound", "listkey", listkey, "cids", "JSON") #result = pubchem_search File.join(@pug_uri, "compound", "listkey", listkey, "assaysummary", "JSON") end - puts "Neighbor CIDs received" + puts "#{result["IdentifierList"]["CID"].size} Neighbor CIDs received" result["IdentifierList"]["CID"].each do |cid| unless cid.to_s == @cid.to_s c = PubChemCompound.new cid.to_s - @neighbors << c if c.assays #and !(c.targets + c.non_targets).empty? + @neighbors << c if c.assays and !c.assays.empty? end end if result and result["IdentifierList"] =begin @@ -122,7 +134,7 @@ module OpenTox neighbors.collect{|n| n.assays.collect{|a| a["AID"]}}.flatten.compact.uniq.each do |aid| predicted_assay = {"AID" => aid} neighbors.each do |neighbor| - if similarity(neighbor) > 0.5 # avoid downweighting + if similarity(neighbor) and similarity(neighbor) > 0.5 # avoid downweighting search = neighbor.assays.select{|a| a["AID"] == aid} search.each do |assay| predicted_assay["Target GI"] ||= assay["Target GI"] @@ -168,11 +180,11 @@ module OpenTox end def predicted_targets - predicted_active_assays.select{|a| a[:target_gi]} if predicted_assays + predicted_active_assays.select{|a| a["Target GI"]} if predicted_assays end def predicted_non_targets - inactive_assays.select{|a| a[:target_gi]} if predicted_assays + predicted_inactive_assays.select{|a| a["Target GI"]} if predicted_assays end def to_smiles diff --git a/views/assays.haml b/views/assays.haml index e995842..510234d 100644 --- a/views/assays.haml +++ b/views/assays.haml @@ -1,6 +1,7 @@ -%ul - - @assays.each do |assay| - %li - %a{:href => "/aid/#{assay["AID"]}"} #{assay['Assay Name']} - AID: - = assay["AID"] +%dl + - @assays.sort{|a,b| a["Assay Name"] <=> b["Assay Name"]}.each do |assay| + %dt + %a{:href => "http://pubchem.ncbi.nlm.nih.gov/assay/assay.cgi?aid=#{assay["AID"]}"} #{assay['Assay Name']} + %dd + Assay ID: + %a{:href => "http://pubchem.ncbi.nlm.nih.gov/assay/assay.cgi?aid=#{assay["AID"]}"} #{assay['AID']} diff --git a/views/compound.haml b/views/compound.haml index 7614142..de4cf77 100644 --- a/views/compound.haml +++ b/views/compound.haml @@ -1,46 +1,65 @@ -:javascript - $(document).ready(function() { - display(".targets", "/cid/#{session[:compound].cid}/targets"); - //display(".nontargets", "/cid/#{session[:compound].cid}/targets"); - display(".active_assays", "/cid/#{session[:compound].cid}/other_active_assays"); - //display(".inactive_assays", "/cid/#{session[:compound].cid}/other_active_assays"); - display(".predicted_targets", "/cid/#{session[:compound].cid}/predicted_targets"); - display(".predicted_active_assays", "/cid/#{session[:compound].cid}/other_predicted_active_assays"); - display(".neighbors", "/cid/#{session[:compound].cid}/neighbors"); - }); - %script{:type => "text/javascript", :src => "sorttable.js"} %table{:class => "sortable"} %tr %th Structure - %th %th Targets (experimental data) - %th Other assays (experimental data) + %th Other active assays (experimental data) %tr %td{:valign => "top"} + %br= session[:compound].name %img{:src => session[:compound].image_uri} - %td %td{:valign => "top"} .targets - -# %h2 Non-Targets - .nontargets + :javascript + display(".targets", "/cid/#{session[:compound].cid}/targets"); %td{:valign => "top"} .active_assays - -# %h2 Inactive assays - .inactive_assays + :javascript + display(".active_assays", "/cid/#{session[:compound].cid}/other_active_assays"); %tr %th - %th %th Targets (predicted) - %th Other assays (predicted) + %th Other active assays (predicted) %tr %td - %td %td{:valign => "top"} .predicted_targets + :javascript + display(".predicted_targets", "/cid/#{session[:compound].cid}/predicted_targets"); %td{:valign => "top"} .predicted_active_assays + :javascript + display(".predicted_active_assays", "/cid/#{session[:compound].cid}/other_predicted_active_assays"); + %tr + %th + %th Non-targets (experimental data) + %th Other inactive assays (experimental data) + %tr + %td + %td{:valign => "top"} + .nontargets + :javascript + display(".nontargets", "/cid/#{session[:compound].cid}/nontargets"); + %td{:valign => "top"} + .inactive_assays + :javascript + display(".inactive_assays", "/cid/#{session[:compound].cid}/other_inactive_assays"); + %tr + %th + %th Non-targets (predicted) + %th Other inactive assays (predicted) + %tr + %td + %td{:valign => "top"} + .predicted_nontargets + :javascript + display(".predicted_nontargets", "/cid/#{session[:compound].cid}/predicted_nontargets"); + %td{:valign => "top"} + .predicted_inactive_assays + :javascript + display(".predicted_inactive_assays", "/cid/#{session[:compound].cid}/other_predicted_inactive_assays"); -%h2 Neighbors +%h2 Read across compounds .neighbors - +:javascript + display(".neighbors", "/cid/#{session[:compound].cid}/neighbors"); diff --git a/views/layout.haml b/views/layout.haml index 8641692..40c6747 100644 --- a/views/layout.haml +++ b/views/layout.haml @@ -6,6 +6,7 @@ function display(element,uri) { $(element).html("<img src=\"/spinning-wait-icons/wait30trans.gif\" alt=\"Searching PubChem\">"); $.ajax({ + cache: true, url: uri, success: function(data){ $(element).html(data); @@ -20,7 +21,7 @@ %h1 adverse outcome pathways %form{:name => "form", :action => '/search', :method => "GET"} %fieldset - %label{:for => 'identifier'} Compound name: - %input{:type => 'text', :name => 'name', :id => 'name', :size => '60'} - %input{ :type => "submit", :value => "Predict" } + %label{:for => 'name'} Compound name: + %input{:type => 'text', :name => 'name', :id => 'name'} + %input{ :type => "submit", :value => "Search" } = yield diff --git a/views/neighbors.haml b/views/neighbors.haml index 54905ce..7b9c79d 100644 --- a/views/neighbors.haml +++ b/views/neighbors.haml @@ -1,43 +1,37 @@ -%script{:type => "text/javascript", :src => "sorttable.js"} -%table{:class => "sortable"} +%table - session[:compound].neighbors[0..10].each do |compound| %tr + %th Structure + %th Similarity + %th Targets (experimental data) + %th Other active assays (experimental data) + + %tr %td{:valign => "top"} + %br= compound.name %img{:src => compound.image_uri} %td{:id => "sim#{compound.cid}", :valign => "top"} - %img{:src => "/spinning-wait-icons/wait30trans.gif"} :javascript - $.ajax({ - url: "/cid/#{session[:compound].cid}/cosine/#{compound.cid}", - success: function(data){ - $("#sim#{compound.cid}").html(data); - }, - error: function(data,textStatus,message){ - $("#sim#{compound.cid}").html(message); - } - }); + display("#sim#{compound.cid}", "/cid/#{session[:compound].cid}/cosine/#{compound.cid}"); %td{:id => "targets#{compound.cid}", :valign => "top"} - %img{:src => "/spinning-wait-icons/wait30trans.gif"} :javascript - $.ajax({ - url: "/cid/#{compound.cid}/targets", - success: function(data){ - $("#targets#{compound.cid}").html(data); - }, - error: function(data,textStatus,message){ - $("#targets#{compound.cid}").html(message); - } - }); + display("#targets#{compound.cid}", "/cid/#{compound.cid}/targets"); %td{:id => "assays#{compound.cid}", :valign => "top"} - %img{:src => "/spinning-wait-icons/wait30trans.gif"} :javascript - $.ajax({ - url: "/cid/#{compound.cid}/other_active_assays", - success: function(data){ - $("#assays#{compound.cid}").html(data); - }, - error: function(data,textStatus,message){ - $("#assays#{compound.cid}").html(message); - } - }); + display("#assays#{compound.cid}", "/cid/#{compound.cid}/other_active_assays"); + + %tr + %th + %th + %th Non-targets (experimental data) + %th Other inactive assays (experimental data) + + %tr + %td %td + %td{:id => "targets#{compound.cid}", :valign => "top"} + :javascript + display("#targets#{compound.cid}", "/cid/#{compound.cid}/nontargets"); + %td{:id => "assays#{compound.cid}", :valign => "top"} + :javascript + display("#assays#{compound.cid}", "/cid/#{compound.cid}/other_inactive_assays"); diff --git a/views/nontargets.haml b/views/nontargets.haml new file mode 100644 index 0000000..154b5da --- /dev/null +++ b/views/nontargets.haml @@ -0,0 +1,3 @@ +%pre + =# session[:compound].non_targets.to_yaml + diff --git a/views/not_found.haml b/views/not_found.haml new file mode 100644 index 0000000..5903559 --- /dev/null +++ b/views/not_found.haml @@ -0,0 +1,2 @@ +No compound found that matches name += "\"#{params[:name]}\"." diff --git a/views/predicted_assays.haml b/views/predicted_assays.haml index e995842..991c267 100644 --- a/views/predicted_assays.haml +++ b/views/predicted_assays.haml @@ -1,6 +1,13 @@ -%ul - - @assays.each do |assay| - %li - %a{:href => "/aid/#{assay["AID"]}"} #{assay['Assay Name']} - AID: - = assay["AID"] +%dl + - @assays.sort{|a,b| [b[:p_active],b[:p_inactive]].max <=> [a[:p_active],a[:p_inactive]].max}.each do |assay| + %dt + %a{:href => "http://pubchem.ncbi.nlm.nih.gov/assay/assay.cgi?aid=#{assay["AID"]}"} #{assay['Assay Name']} + %dd + Assay ID: + %a{:href => "http://pubchem.ncbi.nlm.nih.gov/assay/assay.cgi?aid=#{assay["AID"]}"} #{assay['AID']} + %dd + p_active: + = assay[:p_active].to_f.round(3) + %dd + p_inactive: + = assay[:p_inactive].to_f.round(3) diff --git a/views/predicted_targets.haml b/views/predicted_targets.haml index 1a172ff..523a217 100644 --- a/views/predicted_targets.haml +++ b/views/predicted_targets.haml @@ -1,6 +1,15 @@ -%ul - - @assays.each do |assay| - %li - %a{:href => "/aid/#{assay["AID"]}"} #{assay['Target Name']} - Target GI: - = assay['Target GI'] +%dl + - @assays.sort{|a,b| [b[:p_active],b[:p_inactive]].max <=> [a[:p_active],a[:p_inactive]].max}.each do |assay| + %dt= assay["Target Name"] + %dd + Target GeneID: + = assay["Target GI"] + %dd + Assay ID: + %a{:href => "http://pubchem.ncbi.nlm.nih.gov/assay/assay.cgi?aid=#{assay["AID"]}"} #{assay['AID']} + %dd + p_active: + = assay[:p_active].to_f.round(3) + %dd + p_inactive: + = assay[:p_inactive].to_f.round(3) diff --git a/views/targets.haml b/views/targets.haml index 1a172ff..9622abd 100644 --- a/views/targets.haml +++ b/views/targets.haml @@ -1,6 +1,11 @@ -%ul - - @assays.each do |assay| - %li - %a{:href => "/aid/#{assay["AID"]}"} #{assay['Target Name']} - Target GI: - = assay['Target GI'] +%dl + - @assays.collect{|a| [a["Target Name"],a["Target GI"]]}.uniq.sort{|a,b| a[0] <=> b[0]}.each do |target| + %dt= target.first + %dd + Target GeneID: + = target.last + %dd + Assay IDs: + - @assays.select{|a| a["Target GI"] == target.last}.each do |assay| + %a{:href => "http://pubchem.ncbi.nlm.nih.gov/assay/assay.cgi?aid=#{assay["AID"]}"} #{assay['AID']} + |