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
|
#['rubygems', 'rest_client', 'spork', 'helper' ].each do |lib|
['rubygems', 'rest_client', 'spork' ].each do |lib|
require lib
end
ENV['OPENTOX_COMPOUND'] = 'http://webservices.in-silico.ch/compound/v0/' unless ENV['OPENTOX_COMPOUND']
ENV['OPENTOX_FEATURE'] = 'http://webservices.in-silico.ch/feature/v0/' unless ENV['OPENTOX_FEATURE']
ENV['OPENTOX_DATASET'] = 'http://webservices.in-silico.ch/dataset/v0/' unless ENV['OPENTOX_DATASET']
ENV['OPENTOX_FMINER'] = 'http://webservices.in-silico.ch/fminer/v0/' unless ENV['OPENTOX_FMINER']
ENV['OPENTOX_LAZAR'] = 'http://webservices.in-silico.ch/lazar/v0/' unless ENV['OPENTOX_LAZAR']
module OpenTox
class OpenTox
attr_reader :uri
# Escape all nonword characters
def uri_escape(string)
URI.escape(string, /[^\w]/)
end
def finished?
YAML.load(RestClient.get(@uri))[:finished]
end
def finished
print "closing "
puts @uri + '/finished'
RestClient.post @uri + '/finished'
end
def destroy
RestClient.delete @uri
end
end
class Compound < OpenTox
# Initialize with <tt>:uri => uri</tt>, <tt>:smiles => smiles</tt> or <tt>:name => name</tt> (name can be also an InChI/InChiKey, CAS number, etc)
def initialize(params)
if params[:uri]
@uri = params[:uri].to_s
elsif params[:smiles]
@uri = RestClient.post ENV['OPENTOX_COMPOUND'] ,:smiles => uri_escape(params[:smiles])
elsif params[:name]
@uri = RestClient.post ENV['OPENTOX_COMPOUND'] ,:name => uri_escape(params[:name])
end
end
# Get the (canonical) smiles
def smiles
RestClient.get @uri
end
# Matchs a smarts string
def match?(smarts)
if RestClient.get(@uri + '/match/' + uri_escape(smarts)) == 'true'
true
else
false
end
end
# Match an array of smarts features, returns matching features
def match(smarts_features)
smarts_features.collect{ |smarts| smarts if self.match?(smarts.name) }.compact
end
end
class Feature < OpenTox
# Initialize with <tt>:uri => uri</tt>, or <tt>:name => name, :values => hash_of_property_names_and_values</tt>
def initialize(params)
if params[:uri]
@uri = params[:uri].to_s
else
@uri = ENV['OPENTOX_FEATURE'] + uri_escape(params[:name])
params[:values].each do |k,v|
@uri += '/' + k.to_s + '/' + v.to_s
end
end
end
# Get the value of a property
def value(property)
RestClient.get @uri + '/' + property
end
# Get the name of the feature
def name
RestClient.get @uri + '/name'
end
end
class Dataset < OpenTox
# Initialize with <tt>:uri => uri</tt> or <tt>:name => name</tt> (creates a new dataset)
def initialize(params)
if params[:uri]
@uri = params[:uri].to_s
elsif params[:name] and params[:filename]
@uri = `curl -X POST -F file=@#{params[:filename]} -F name="#{params[:name]}" #{ENV['OPENTOX_DATASET']}`
elsif params[:name]
@uri = RestClient.post ENV['OPENTOX_DATASET'], :name => params[:name]
end
end
def finished
print "closing "
puts @uri + '/finished'
RestClient.post(@uri + '/finished',nil)
end
# Get the dataset name
def name
RestClient.get @uri + '/name'
end
# Get all compounds from a dataset
def compounds
RestClient.get(@uri + '/compounds').split("\n").collect{ |c| Compound.new(:uri => c) }
end
# Get all compounds and features from a dataset, returns a hash with compound_uris as keys and arrays of features as values
def all_compounds_and_features
YAML.load(RestClient.get(@uri + '/compounds/features'))
end
# Get all features from a dataset
def all_features
RestClient.get(@uri + '/features').split("\n").collect{|f| Feature.new(:uri => f)}
end
# Get all features for a compound
def features(compound)
RestClient.get(@uri + '/compound/' + uri_escape(compound.uri) + '/features').split("\n").collect{|f| Feature.new(:uri => f) }
end
# Add a compound and a feature to a dataset
def add(compound,feature)
RestClient.post @uri, :compound_uri => compound.uri, :feature_uri => feature.uri
end
end
class Fminer < OpenTox
# Create a new dataset with BBRC features
def initialize(training_dataset)
@dataset_uri = RestClient.post ENV['OPENTOX_FMINER'], :dataset_uri => training_dataset.uri
end
def dataset
Dataset.new(:uri => @dataset_uri)
end
end
class Lazar < OpenTox
# Create a new prediction model from a dataset
def initialize(params)
if params[:uri]
@uri = params[:uri]
elsif params[:dataset_uri]
@uri = RestClient.post ENV['OPENTOX_LAZAR'] + 'models' , :dataset_uri => params[:dataset_uri]
end
end
# Predict a compound
def predict(compound)
RestClient.post @uri, :compound_uri => compound.uri
end
end
end
|