summaryrefslogtreecommitdiff
path: root/application.rb
blob: 9162fcffd07018437d30ad4f041f77ccccaa9dbb (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
require "./pug.rb"
require "./pubchem.rb"
module OpenTox
  class Application < Sinatra::Base

    set :static, true
    set :root, File.dirname(__FILE__)

    configure :development do
      register Sinatra::Reloader
      also_reload './pubchem.rb'
    end

    before '/cid/:cid/*' do
      @compound = PubChemCompound.new params[:cid] 
    end

    get '/?' do
      haml :index
    end

    get '/cid/:cid/?' do
      haml :compound
    end

    get '/search/?' do
      @compounds = PubChemCompound.from_name params[:name]
      if @compounds.nil?
        haml :not_found
      elsif @compounds.is_a? Array
        haml :select
      else
        @compound = @compounds
        haml :compound
      end
    end

    get '/cid/:cid/targets/?' do
      @assays = @compound.targets
      if @assays.empty?
        "<br><em>No PubChem data</em></br>"
      else
        haml :targets, :layout => false
      end
    end

    get '/cid/:cid/nontargets/?' do
      @assays = @compound.non_targets
      if @assays.empty?
        "<br><em>No PubChem data</em></br>"
      else
        haml :targets, :layout => false
      end
    end

    get '/cid/:cid/other_active_assays/?' do
      @assays = @compound.active_assays - @compound.targets
      if @assays.empty?
        "<br><em>No PubChem data</em></br>"
      else
        haml :assays, :layout => false
      end
    end

    get '/cid/:cid/other_inactive_assays/?' do
      @assays = @compound.inactive_assays - @compound.non_targets
      if @assays.empty?
        "<br><em>No PubChem data</em></br>"
      else
        haml :assays, :layout => false
      end
    end

    get '/cid/:cid/predicted_targets/?' do
      @assays = @compound.predicted_targets
      puts @assays.inspect
      haml :predicted_targets, :layout => false
    end

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

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

    get '/cid/:cid/other_predicted_inactive_assays/?' do
      @assays = @compound.predicted_inactive_assays - @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
      @compound.cosine(PubChemCompound.new(params[:cid2])).round(3).to_s
    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