summaryrefslogtreecommitdiff
path: root/application.rb
blob: 68bb8d284953b6aba3e0d96660b7c6e37c69ddf8 (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
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'

    @@pug_uri = "http://pubchem.ncbi.nlm.nih.gov/rest/pug/"

    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
      haml :compound
    end

    get '/search/?' do
      #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
        elsif cids.size > 1
          @compounds = cids.collect{|cid| PubChemCompound.new cid }
          haml :select
        end
      rescue
        haml :not_found
      end
    end

    get '/cid/:cid/targets/?' do
      @assays = session[:compound].targets
      haml :targets, :layout => false
    end

    get '/cid/:cid/nontargets/?' do
      @assays = session[:compound].non_targets
      haml :targets, :layout => false
    end

    get '/cid/:cid/other_active_assays/?' do
      @assays = session[:compound].active_assays - session[:compound].targets
      haml :assays, :layout => false
    end

    get '/cid/:cid/other_inactive_assays/?' do
      @assays = session[:compound].inactive_assays - session[:compound].non_targets
      haml :assays, :layout => false
    end

    get '/cid/:cid/predicted_targets/?' do
      @assays = session[:compound].predicted_targets
      haml :predicted_targets, :layout => false
    end

    get '/cid/:cid/predicted_nontargets/?' do
      @assays = session[:compound].predicted_non_targets
      haml :predicted_targets, :layout => false
    end

    get '/cid/:cid/other_predicted_active_assays/?' do
      @assays = session[:compound].predicted_active_assays - session[:compound].predicted_targets
      haml :predicted_assays, :layout => false
    end

    get '/cid/:cid/other_predicted_inactive_assays/?' do
      @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])).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|
        pred[:fp_targets].each do |gi,t|
          @fp << {
            "CID" => pred[:cid],
            "Target GI" => gi,
            "p_active" => t[:p][:active].first, 
            "p_inactive" => t[:p][:inactive].first, 
            :assays => t[:measured],
            :neighbors => t[:neighbors]
          }
        end
      end
      @fp.sort!{|a,b| b["p_active"] <=> a["p_active"]}
      haml :fp
    end
  end
end