summaryrefslogtreecommitdiff
path: root/pubchem.rb
blob: d16f4b4f2b6e28eb061be155928ac9993d3b3782 (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
require '../opentox-client/lib/opentox-client.rb'
require 'json'
require 'base64'

def Math.gauss(x, sigma = 0.3) 
  d = 1.0 - x.to_f
  Math.exp(-(d*d)/(2*sigma*sigma))
end

module OpenTox

  class PubChemCompound < Compound
 
    attr_accessor :cid
    @@pug_proxy = "http://localhost:8081/"
    
    def initialize cid
      @cid = cid.to_s
    end

    def fingerprint
      JSON.parse RestClient.get(File.join(@@pug_proxy,"cid",@cid,"fingerprint"))
    end

    def self.from_name name
      cids = JSON.parse(RestClient.get(File.join(@@pug_proxy,"name",CGI.escape(name))))
      if cids.size == 1
        PubChemCompound.new cids.first
      elsif cids.empty?
        nil
      else
        cids.collect{|cid| PubChemCompound.new cid}
      end
    end

    def name
      RestClient.get(File.join(@@pug_proxy,"cid",cid,"name")).chomp.sub(/^"/,'').sub(/"$/,'')
    end

    def neighbors
      JSON.parse(RestClient.get(File.join(@@pug_proxy,"cid",@cid,"neighbors"))).collect{|n| PubChemCompound.new(n) }
    end

    def assays
      JSON.parse RestClient.get(File.join(@@pug_proxy,"cid",cid,"assays"))
    end

    def active_assays
      assays.select{|a| a["Activity Outcome"] == "active"} if assays
    end

    def inactive_assays
      assays.select{|a| a["Activity Outcome"] == "inactive"} if assays
    end

    def targets
      active_assays.select{|a| a["Target GI"]} if assays
    end

    def non_targets
      inactive_assays.select{|a| a["Target GI"]} if assays
    end

    def predicted_assays
      JSON.parse RestClient.get(File.join(@@pug_proxy,"cid",cid,"predictions"))
    end

    def predicted_active_assays
      predicted_assays.select{|a| a["p_active"] > a["p_inactive"]} if predicted_assays
    end

    def predicted_inactive_assays
      predicted_assays.select{|a| a["p_active"] < a["p_inactive"]} if predicted_assays
    end

    def predicted_targets
      predicted_active_assays.select{|a| a["Target GI"]} if predicted_assays
    end

    def predicted_non_targets
      predicted_inactive_assays.select{|a| a["Target GI"]} if predicted_assays
    end

    def image_uri
      File.join @@pug_proxy, "cid", @cid, "image"
    end

    def similarity compound
      cosine compound
    end

    def cosine compound
      RestClient.get(File.join(@@pug_proxy,"cid",@cid,"cosine",compound.cid)).to_f
    end
  end
end