summaryrefslogtreecommitdiff
path: root/lib/opentox-ruby-api-wrapper.rb
blob: 4ee6fd3ce9260df3db54798de07a79d6597f9a24 (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
['rubygems', 'rest_client', 'spork', 'environment'].each do |lib|
	require lib
end

module OpenTox

	class OpenTox
		attr_reader :uri

		# Escape all nonword characters
		def uri_escape(string)
			URI.escape(string, /[^\w]/)
		end

		# Returns true if object creation has finished (for asynchronous processes)
		def finished?
			YAML.load(RestClient.get(@uri))[:finished]
		end

		# Get the object name
		def name
			RestClient.get @uri + '/name'
		end

		# Deletes an object
		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 @services['opentox-compound'] ,:smiles => uri_escape(params[:smiles])
			elsif params[:name]
				@uri = RestClient.post @services['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 = @services['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

	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]}" #{@services['opentox-dataset']}`
			elsif params[:name] 
				@uri = RestClient.post @services['opentox-dataset'], :name => params[:name]
			end
		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 feature_uris as values
		def all_compounds_and_features_uris
			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.put @uri, :compound_uri => compound.uri, :feature_uri => feature.uri
		end

		# Tell the dataset that it is complete
		def close
			RestClient.put @uri, :finished => 'true'
		end

	end

	class Fminer < OpenTox

		# Create a new dataset with BBRC features
		def initialize(training_dataset)
			@dataset_uri = RestClient.post @services['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 @services['opentox-lazar']+ 'models' , :dataset_uri => params[:dataset_uri]
			end
		end

		# Predict a compound
		def predict(compound)
			LazarPrediction.new(:uri => RestClient.post(@uri, :compound_uri => compound.uri))
		end

	end

	class LazarPrediction < OpenTox

		def initialize(params)
			if params[:uri]
				@uri = params[:uri]
			end
		end

		def classification
			YAML.load(RestClient.get @uri)[:classification]
		end

		def confidence
			YAML.load(RestClient.get @uri)[:confidence]
		end

		def neighbors
			RestClient.get @uri + '/neighbors' 
		end

		def features
			RestClient.get @uri + '/features' 
		end

	end

end