summaryrefslogtreecommitdiff
path: root/pug.rb
blob: 6a8b102e94a91d7f2e96ba98e892b1e1cdbaf89e (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
require "json"
require 'base64'
require 'sinatra/base'
require "sinatra/reloader" 
require "rest-client"
require 'memcache'

class Application < Sinatra::Base

  # doc @ http://pubchem.ncbi.nlm.nih.gov/pug_rest/
  @@pug_uri = "http://pubchem.ncbi.nlm.nih.gov/rest/pug/"
  @@similarity_threshold = 90
  @@max_neighbors = 100
  
  CACHE = MemCache.new 'localhost:11211'

  helpers do

    def local route
      status, headers, body = call env.merge("PATH_INFO" => route)
      begin
        Float body[0]
      rescue
        JSON.parse body[0]
      end
    end

    def pubchem_search url
      attempts = 0
      begin
        attempts += 1
        puts url
        json = RestClient.get url, :timeout => 90000000
        JSON.parse json
      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
          nil
        elsif $!.message.match /404/
          nil
        else
          puts url
          puts $!.message
          nil
        end
      end
    end

    def fingerprint cid
      local("/cid/#{cid}/fingerprint")
    end

    def neighbors cid
      local "/cid/#{cid}/neighbors"
    end

    def assays cid
      local "/cid/#{cid}/assays"
    end

    def similarity cid1, cid2
      local("/cid/#{cid1}/cosine/#{cid2}")
    end

    def cosine fp1, fp2
      if fp1 and fp2
        m11 = 0.0
        m01 = 0.0
        m10 = 0.0
        m00 = 0.0
        fp1.each_index do |i|
          m11 += 1 if (fp1[i] and fp2[i])
          m01 += 1 if (!fp1[i] and fp2[i]) 
          m10 += 1 if (fp1[i] and !fp2[i]) 
          m00 += 1 if (!fp1[i] and !fp2[i]) 
        end
        m11/((m01+m11)*(m10+m11))**0.5
      end
    end
  end

  before do
    @result = CACHE.get request.path
    halt 200, @result unless @result.nil? # should be 304, but this does not work with local()
  end

  after do
    CACHE.add request.path, @result, 7200
  end

  get '/cid/:cid/name' do
    @result = RestClient.get(File.join(@@pug_uri, "compound", "cid", params[:cid], "property", "IUPACName","TXT")).chomp
  end

  get '/cid/:cid/fingerprint' do
    # ftp://ftp.ncbi.nlm.nih.gov/pubchem/specifications/pubchem_fingerprints.txt
    # it seems that only SDF formats contain fingerprints 
    sdf_lines = RestClient.get(File.join(@@pug_uri, "compound", "cid", params[:cid], "SDF")).split("\n")
    index = sdf_lines.index(sdf_lines.grep(/PUBCHEM_CACTVS_SUBSKEYS/).first)
    @result = Base64.decode64(sdf_lines[index+1])[4..-1].unpack("B*").first[0..-8].split(//).collect{|c| c == "1"}.to_json
  end

  get '/cid/:cid/assays' do
    assays = []
    result = pubchem_search File.join(@@pug_uri, "compound", "cid", params[:cid], "assaysummary", "JSON")
    if result and result["Table"]
      columns = result["Table"]["Columns"]["Column"]
      result["Table"]["Row"].collect{|cell| cell.values.flatten}.each do |row|
        assay = {}
        row.each_with_index do |cell,i|
          assay[columns[i]] = cell unless cell.empty? or columns[i] == "CID" 
        end
        assays << assay unless assay.empty?
      end
    end
    @result = assays.to_json
  end

  get '/cid/:cid/neighbors' do
    result = pubchem_search File.join(@@pug_uri, "compound", "similarity", "cid", params[:cid], "JSON")+"?Threshold=#{@@similarity_threshold}&MaxRecords=#{@@max_neighbors}"
    while result["Waiting"] do
      sleep 2
      listkey = result["Waiting"]["ListKey"]
      result = pubchem_search File.join(@@pug_uri, "compound", "listkey", listkey, "cids", "JSON")
    end
    result["IdentifierList"]["CID"].delete params[:cid].to_i
    @result = result["IdentifierList"]["CID"].to_json
  end

  get '/name/:name' do
    @result = RestClient.get(File.join(@@pug_uri,"compound","name",CGI.escape(params[:name]),"cids","TXT")).split("\n").to_json
  end

  get '/cid/:cid/image' do
    @result = RestClient.get File.join(@@pug_uri, "compound", "cid", params[:cid], "PNG")
  end

  get '/cid/:cid1/cosine/:cid2' do
    fp1 = fingerprint params[:cid1]
    fp2 = fingerprint params[:cid2]
    @result = cosine(fp1, fp2).to_json
  end

  get '/cid/:cid/predictions' do
    assays = {}
    assay_details = {}
    neighbors(params[:cid]).each do |cid|
      neighbor_assays = assays cid
      unless neighbor_assays.empty?
        neighbor_assays.each do |assay|
          if assay["Activity Outcome"] == "active" or assay["Activity Outcome"] == "inactive"
            assays[assay["AID"]] ||= []
            assays[assay["AID"]] << [cid,similarity(params[:cid],cid),assay["Activity Outcome"]]
            assay_details[assay["AID"]] ||= {}
            ["Target GI", "Target Name", "Assay Name"].each do |d|
              assay_details[assay["AID"]][d] = assay[d] #if assay[d]
            end
          end
        end
      end
    end
    predictions = []
    assays.each do |aid,neighbors|
      prediction = {"AID" => aid}
      neighbors.each do |neighbor|
        cid = neighbor[0]
        sim = neighbor[1]
        activity = neighbor[2]
        if activity == "active"
          prediction[:p_active] ? prediction[:p_active] = prediction[:p_active]*sim : prediction[:p_active] = sim
          prediction[:p_inactive] ? prediction[:p_inactive] = prediction[:p_inactive]*(1-sim) : prediction[:p_inactive] = 1-sim
        elsif activity == "inactive"
          prediction[:p_active] ? prediction[:p_active] = prediction[:p_active]*(1-sim) : prediction[:p_active] = 1-sim
          prediction[:p_inactive] ? prediction[:p_inactive] = prediction[:p_inactive]*sim : prediction[:p_inactive] = sim
        end
        ["Target GI", "Target Name", "Assay Name"].each do |d|
          prediction[d] = assay_details[aid][d] if assay_details[aid][d]
        end
      end
      predictions << prediction
    end
    @result = predictions.to_json
  end

#  get '/*' do |path|
#    RestClient.get File.join(@@pug_uri,path), params
#  end
end