opentox-ruby-api-wrapper 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,13 +10,9 @@ begin
10
10
  gem.email = "helma@in-silico.ch"
11
11
  gem.homepage = "http://github.com/helma/opentox-ruby-api-wrapper"
12
12
  gem.authors = ["Christoph Helma"]
13
- gem.add_dependency "rest-client"
14
- gem.add_dependency "sinatra"
15
- gem.add_dependency "rack"
16
- gem.add_dependency "rack-contrib"
17
- gem.add_dependency "thin"
18
- gem.add_dependency "emk-sinatra-url-for"
19
- gem.add_dependency "cehoffman-sinatra-respond_to"
13
+ ["sinatra", "rest-client", "rack", "rack-contrib", "thin", "emk-sinatra-url-for", "cehoffman-sinatra-respond_to", "dm-more", "dm-core"].each do |dep|
14
+ gem.add_dependency dep
15
+ end
20
16
  gem.add_development_dependency "cucumber"
21
17
  gem.files = FileList["[A-Z]*", "{bin,generators,lib,test}/**/*", 'lib/jeweler/templates/.gitignore']
22
18
  gem.files.include %w(lib/tasks/owl.rb, lib/environment.rb, lib/algorithm.rb, lib/compound.rb, lib/dataset.rb, lib/model.rb, lib/utils.rb, lib/templates/*)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.3
1
+ 1.2.4
@@ -1,7 +1,7 @@
1
1
  module OpenTox
2
2
  module Algorithm
3
3
 
4
- class Fminer #< OpenTox
4
+ class Fminer
5
5
  include Owl
6
6
 
7
7
  def initialize
@@ -20,7 +20,7 @@ module OpenTox
20
20
  end
21
21
  end
22
22
 
23
- class Lazar #< OpenTox
23
+ class Lazar
24
24
  include Owl
25
25
 
26
26
  def initialize
@@ -37,10 +37,14 @@ module OpenTox
37
37
  { :scope => "mandatory", :value => "feature_generation_uri" }
38
38
  }
39
39
  end
40
+
41
+ def self.create_model(params)
42
+ @uri = RestClient.post File.join(@@config[:services]["opentox-algorithm"], "lazar"), :dataset_uri => params[:dataset_uri], :feature_uri => params[:feature_uri], :feature_generation_uri => File.join(@@config[:services]["opentox-algorithm"], "fminer")
43
+ end
44
+
40
45
  end
41
46
 
42
47
  class Similarity
43
-
44
48
  def self.weighted_tanimoto(fp_a,fp_b,p)
45
49
  common_features = fp_a & fp_b
46
50
  all_features = fp_a + fp_b
@@ -54,7 +58,6 @@ module OpenTox
54
58
  0.0
55
59
  end
56
60
  end
57
-
58
61
  end
59
62
 
60
63
  end
@@ -34,6 +34,14 @@ module OpenTox
34
34
  obconversion(@inchi,'inchi','sdf')
35
35
  end
36
36
 
37
+ def image
38
+ RestClient.get("#{@@cactus_uri}#{@inchi}/image")
39
+ end
40
+
41
+ def image_uri
42
+ "#{@@cactus_uri}#{@inchi}/image"
43
+ end
44
+
37
45
  # Matchs a smarts string
38
46
  def match?(smarts)
39
47
  obconversion = OpenBabel::OBConversion.new
@@ -47,8 +47,9 @@ module OpenTox
47
47
  @model.add tuple, OT['complexValue'], complex_value
48
48
  @model.add complex_value, RDF['type'], OT["FeatureValue"]
49
49
  @model.add complex_value, OT['feature'], f
50
- @model.add complex_value, OT['value'], value.to_s
51
- end
50
+ @model.add complex_value, OT['value'], value.to_s
51
+ end
52
+
52
53
  tuple
53
54
  end
54
55
 
@@ -77,7 +78,7 @@ module OpenTox
77
78
  end
78
79
 
79
80
  def self.create(data, content_type = 'application/rdf+xml')
80
- uri = RestClient.post @@config[:services]["opentox-dataset"], data, :content_type => content_type
81
+ uri = RestClient.post @@config[:services]["opentox-dataset"], data, :content_type => content_type
81
82
  dataset = Dataset.new
82
83
  dataset.read uri.to_s
83
84
  dataset
@@ -85,9 +86,9 @@ module OpenTox
85
86
 
86
87
  def self.find(uri)
87
88
  begin
88
- dataset = Dataset.new
89
- data = RestClient.get uri, :accept => 'application/rdf+xml' # check if the resource is available
90
- dataset.rdf = data
89
+ dataset = Dataset.new
90
+ data = RestClient.get uri, :accept => 'application/rdf+xml' # check if the resource is available
91
+ dataset.rdf = data
91
92
  dataset
92
93
  rescue
93
94
  nil
@@ -157,46 +158,6 @@ module OpenTox
157
158
  features
158
159
  end
159
160
 
160
- =begin
161
- def tuples
162
- tuples = []
163
- @model.subjects(RDF['type'], OT["Tuple"]).each do |t|
164
- tuple = {}
165
- compounds = []
166
- @model.subjects(OT['values'], t).each do |data_entry|
167
- compound_node = @model.object(data_entry,OT['compound'])
168
- compounds << @model.object(compound_node, DC["identifier"]).to_s
169
- end
170
- @model.find(t, OT['tuple'],nil) do |s,p,pair|
171
- feature_node = @model.object(pair, OT['feature'])
172
- feature_name = @model.object(feature_node, DC['title']).to_s
173
- value_node = @model.object(pair, OT['value'])
174
- value = @model.object(value_node, OT['value']).to_s
175
- value = value.to_f if value.match(/^[\d\.]+$/)
176
- tuple[feature_name.to_sym] = value
177
- end
178
- tuple[:compounds] = compounds
179
- tuples << tuple
180
- end
181
- tuples
182
- end
183
-
184
- def tuple(compound_uri)
185
- compound_node = @model.subject(DC["identifier"],compound_uri)
186
- #puts compound_uri
187
- @model.subjects(OT['compound'], compound_node).each do |data_entry|
188
- values_node = @model.object(data_entry, OT['values'])
189
- @model.find(values_node, OT['tuple'], nil) do |s,p,tuple|
190
- @model.find(tuple, OT['feature'], nil) do |s,p,feature|
191
- name = @model.object(feature,DC['title']).to_s
192
- #puts name
193
- end
194
- end
195
- #puts values_node
196
- end
197
- end
198
- =end
199
-
200
161
  def compounds
201
162
  compounds = []
202
163
  @model.subjects(RDF['type'], OT["Compound"]).each do |compound_node|
@@ -222,8 +183,7 @@ module OpenTox
222
183
  :source => self.source,
223
184
  :identifier => self.identifier,
224
185
  :compounds => self.compounds.collect{|c| c.to_s.to_s.sub(/^\[(.*)\]$/,'\1')},
225
- :features => self.features.collect{|f| f.to_s },
226
- :data_entries => self.data_entries,
186
+ :features => self.features.collect{|f| f.to_s }
227
187
  }.to_yaml
228
188
  end
229
189
 
@@ -1,43 +1,31 @@
1
1
  module OpenTox
2
2
  module Model
3
-
3
+
4
4
  class Lazar
5
5
  include Owl
6
6
 
7
- attr_accessor :dataset, :predictions
8
-
9
7
  # Create a new prediction model from a dataset
10
- def initialize(yaml)
11
- super()
12
- id = File.basename(yaml,'.yaml')
13
- # TODO Untyped Individual: http://localhost:4003/lazar/{id} ????
14
- @lazar = YAML.load_file yaml
15
- self.uri = File.join(@@config[:services]["opentox-model"],'lazar',id)
16
- self.title = "lazar model for #{@lazar[:endpoint]}"
8
+ def initialize
9
+ super
17
10
  self.source = "http://github.com/helma/opentox-model"
18
- self.parameters = {
19
- "Dataset URI" => { :scope => "mandatory", :value => "dataset_uri=#{@lazar[:activity_dataset]}" },
20
- "Feature URI for dependent variable" => { :scope => "mandatory", :value => "feature_uri=#{@lazar[:endpoint]}" },
21
- "Feature generation URI" => { :scope => "mandatory", :value => "feature_generation_uri=" } #TODO write to yaml
22
- }
23
11
  self.algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
24
- self.trainingDataset = @lazar[:activity_dataset]
25
- self.dependentVariables = @lazar[:endpoint]
26
- self.independentVariables = "http://localhost:4002/fminer#BBRC_representative" # TODO read this from dataset
27
- self.predictedVariables = @lazar[:endpoint] #+ " lazar prediction"
28
- @dataset = OpenTox::Dataset.new
29
- @predictions = {}
12
+ self.independentVariables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative") # TODO read this from dataset
30
13
  end
31
14
 
32
- def self.find(uri)
33
- =begin
34
- begin
35
- YAML.load(RestClient.get uri)
36
- Lazar.new uri
37
- rescue
38
- halt 404, "Model #{uri} not found."
39
- end
40
- =end
15
+ def self.from_yaml(yaml)
16
+ yaml = YAML.load yaml
17
+ lazar = Lazar.new
18
+ lazar.title = "lazar model for #{yaml[:endpoint]}"
19
+ lazar.parameters = {
20
+ "Dataset URI" => { :scope => "mandatory", :value => "dataset_uri=#{yaml[:activity_dataset]}" },
21
+ "Feature URI for dependent variable" => { :scope => "mandatory", :value => "feature_uri=#{yaml[:endpoint]}" },
22
+ "Feature generation URI" => { :scope => "mandatory", :value => "feature_generation_uri=#{File.join(@@config[:services]["opentox-algorithm"],"fminer")}"} #TODO write to yaml
23
+ }
24
+ lazar.algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
25
+ lazar.trainingDataset = yaml[:activity_dataset]
26
+ lazar.dependentVariables = yaml[:endpoint]
27
+ lazar.predictedVariables = yaml[:endpoint] #+ " lazar prediction"
28
+ lazar
41
29
  end
42
30
 
43
31
  def self.find_all
@@ -49,65 +37,6 @@ module OpenTox
49
37
  RestClient.post(@uri, :compound_uri => compound.uri)
50
38
  end
51
39
 
52
- def database_activity?(compound_uri)
53
- # find database activities
54
- db_activities = @lazar[:activities][compound_uri]
55
- if db_activities
56
- c = @dataset.find_or_create_compound(compound_uri)
57
- f = @dataset.find_or_create_feature(@lazar[:endpoint])
58
- v = db_activities.join(',')
59
- @dataset.add c,f,v
60
- @predictions[compound_uri] = { @lazar[:endpoint] => {:measured_activities => db_activities}}
61
- true
62
- else
63
- false
64
- end
65
- end
66
-
67
- def classify(compound_uri)
68
-
69
- compound = OpenTox::Compound.new(:uri => compound_uri)
70
- compound_matches = compound.match @lazar[:features]
71
-
72
- conf = 0.0
73
- neighbors = []
74
- classification = nil
75
-
76
- @lazar[:fingerprints].each do |uri,matches|
77
-
78
- sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(compound_matches,matches,@lazar[:p_values])
79
- if sim > 0.3
80
- neighbors << uri
81
- @lazar[:activities][uri].each do |act|
82
- case act.to_s
83
- when 'true'
84
- conf += OpenTox::Utils.gauss(sim)
85
- when 'false'
86
- conf -= OpenTox::Utils.gauss(sim)
87
- end
88
- end
89
- end
90
- end
91
-
92
- conf = conf/neighbors.size
93
- if conf > 0.0
94
- classification = true
95
- elsif conf < 0.0
96
- classification = false
97
- end
98
-
99
- compound = @dataset.find_or_create_compound(compound_uri)
100
- feature = @dataset.find_or_create_feature(@lazar[:endpoint])
101
- tuple = @dataset.create_tuple(feature,{ 'lazar#classification' => classification, 'lazar#confidence' => conf})
102
- @dataset.add_tuple compound,tuple
103
- @predictions[compound_uri] = { @lazar[:endpoint] => { :lazar_prediction => {
104
- :classification => classification,
105
- :confidence => conf,
106
- :neighbors => neighbors,
107
- :features => compound_matches
108
- } } }
109
- end
110
-
111
40
  def self.base_uri
112
41
  @@config[:services]["opentox-model"]
113
42
  end
data/lib/owl.rb CHANGED
@@ -12,7 +12,7 @@ module OpenTox
12
12
 
13
13
  # read OT Ontology
14
14
  #@parser.parse_into_model(@model,"http://opentox.org/data/documents/development/RDF%20files/OpenToxOntology/at_download/file")
15
- @parser.parse_string_into_model(@model,File.read(File.join(File.dirname(__FILE__),"opentox.owl")),'/')
15
+ #@parser.parse_string_into_model(@model,File.read(File.join(File.dirname(__FILE__),"opentox.owl")),'/')
16
16
  # reate an anonymous resource for metadata
17
17
  # this has to be rewritten with an URI as soon as the resource has been saved at an definitive location
18
18
  tmp = @model.create_resource
@@ -1,20 +1,21 @@
1
1
  module OpenTox
2
2
 
3
- class Task #< OpenTox
3
+ class Task
4
+
5
+ attr_accessor :uri
4
6
 
5
7
  def initialize(uri)
6
- super(uri)
8
+ #super()
9
+ @uri = uri
7
10
  end
8
11
 
9
- #def self.create(uri)
10
12
  def self.create
11
- puts @@config[:services]["opentox-task"]
12
- uri = RestClient.post @@config[:services]["opentox-task"], ''#, :dataset_uri => uri
13
+ uri = RestClient.post @@config[:services]["opentox-task"], nil
13
14
  Task.new(uri)
14
15
  end
15
16
 
16
- def self.find(params)
17
- Task.new(params[:uri])
17
+ def self.find(uri)
18
+ Task.new(uri)
18
19
  end
19
20
 
20
21
  def self.base_uri
@@ -22,32 +23,32 @@ module OpenTox
22
23
  end
23
24
 
24
25
  def start
25
- RestClient.put @uri, :status => 'started'
26
+ RestClient.put File.join(@uri,'started'), nil
26
27
  end
27
28
 
28
- def stop
29
- RestClient.put @uri, :status => 'stopped'
29
+ def cancel
30
+ RestClient.put File.join(@uri,'cancelled'), nil
30
31
  end
31
32
 
32
- def completed
33
- RestClient.put @uri, :status => 'completed'
33
+ def completed(uri)
34
+ RestClient.put File.join(@uri,'completed'), :resource => uri
34
35
  end
35
36
 
36
37
  def status
37
38
  RestClient.get File.join(@uri, 'status')
38
39
  end
39
-
40
- def completed?
41
- self.status == 'completed'
40
+
41
+ def resource
42
+ RestClient.get File.join(@uri, 'resource')
42
43
  end
43
44
 
44
- def resource
45
- RestClient.get @uri
45
+ def completed?
46
+ self.status.to_s == 'completed'
46
47
  end
47
48
 
48
49
  def wait_for_completion
49
50
  until self.completed?
50
- sleep 1
51
+ sleep 0.1
51
52
  end
52
53
  end
53
54
 
@@ -1,24 +1,4 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'opentox-ruby-api-wrapper.rb')
2
- require File.join(File.dirname(__FILE__), 'redis.rb')
3
-
4
- namespace :redis do
5
-
6
- desc "Flush Redis testing database"
7
- task :flush do
8
- require 'redis'
9
- r = Redis.new :db => 2
10
- r.flush_db
11
- end
12
-
13
- desc "Flush all Redis databases"
14
- task :flushall do
15
- require 'redis'
16
- r = Redis.new
17
- r.flush_all
18
- end
19
-
20
-
21
- end
22
2
 
23
3
  namespace :opentox do
24
4
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentox-ruby-api-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Helma
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-21 00:00:00 +01:00
12
+ date: 2010-01-09 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rest-client
16
+ name: sinatra
17
17
  type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
@@ -23,7 +23,7 @@ dependencies:
23
23
  version: "0"
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
- name: sinatra
26
+ name: rest-client
27
27
  type: :runtime
28
28
  version_requirement:
29
29
  version_requirements: !ruby/object:Gem::Requirement
@@ -82,6 +82,26 @@ dependencies:
82
82
  - !ruby/object:Gem::Version
83
83
  version: "0"
84
84
  version:
85
+ - !ruby/object:Gem::Dependency
86
+ name: dm-more
87
+ type: :runtime
88
+ version_requirement:
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
95
+ - !ruby/object:Gem::Dependency
96
+ name: dm-core
97
+ type: :runtime
98
+ version_requirement:
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ version:
85
105
  - !ruby/object:Gem::Dependency
86
106
  name: cucumber
87
107
  type: :development