opentox-ruby-api-wrapper 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/VERSION +1 -1
  2. data/lib/algorithm.rb +2 -31
  3. data/lib/dataset.rb +21 -38
  4. data/lib/model.rb +27 -5
  5. data/lib/owl.rb +134 -67
  6. data/lib/task.rb +6 -24
  7. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.1
1
+ 1.4.0
data/lib/algorithm.rb CHANGED
@@ -4,18 +4,6 @@ module OpenTox
4
4
  module Algorithm
5
5
 
6
6
  class Fminer
7
- include Owl
8
-
9
- def initialize
10
- super
11
- self.uri = File.join(@@config[:services]["opentox-algorithm"],'fminer')
12
- self.title = "fminer"
13
- self.source = "http://github.com/amaunz/libfminer"
14
- self.parameters = {
15
- "Dataset URI" => { :scope => "mandatory", :value => "dataset_uri" },
16
- "Feature URI for dependent variable" => { :scope => "mandatory", :value => "feature_uri" }
17
- }
18
- end
19
7
 
20
8
  def self.create_feature_dataset(params)
21
9
  LOGGER.debug File.basename(__FILE__) + ": creating feature dataset"
@@ -29,29 +17,12 @@ module OpenTox
29
17
  end
30
18
 
31
19
  class Lazar
32
- include Owl
33
-
34
- def initialize
35
- super
36
- self.uri = File.join(@@config[:services]["opentox-algorithm"],'lazar')
37
- self.title = "lazar"
38
- self.source = "http://github.com/helma/opentox-algorithm"
39
- self.parameters = {
40
- "Dataset URI" =>
41
- { :scope => "mandatory", :value => "dataset_uri" },
42
- "Feature URI for dependent variable" =>
43
- { :scope => "mandatory", :value => "feature_uri" },
44
- "Feature generation URI" =>
45
- { :scope => "mandatory", :value => "feature_generation_uri" }
46
- }
47
- end
48
20
 
49
21
  def self.create_model(params)
50
22
  LOGGER.debug params
51
23
  LOGGER.debug File.basename(__FILE__) + ": creating model"
52
- #@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")
53
- resource = RestClient::Resource.new(File.join(@@config[:services]["opentox-algorithm"], "lazar"), :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
54
- @uri = resource.post :dataset_uri => params[:dataset_uri], :feature_uri => params[:feature_uri], :feature_generation_uri => File.join(@@config[:services]["opentox-algorithm"], "fminer")
24
+ resource = RestClient::Resource.new(File.join(@@config[:services]["opentox-algorithm"], "lazar"), :user => @@users[:users].keys[0], :password => @@users[:users].values[0], :content_type => "application/x-yaml")
25
+ @uri = resource.post(:dataset_uri => params[:dataset_uri], :feature_uri => params[:feature_uri], :feature_generation_uri => File.join(@@config[:services]["opentox-algorithm"], "fminer")).chomp
55
26
  end
56
27
 
57
28
  def self.uri
data/lib/dataset.rb CHANGED
@@ -3,12 +3,27 @@ LOGGER.progname = File.expand_path(__FILE__)
3
3
  module OpenTox
4
4
 
5
5
  class Dataset
6
- include Owl
6
+
7
+ attr_accessor :uri, :title, :source, :identifier, :data, :features, :compounds
7
8
 
8
9
  def initialize
9
- super
10
+ @data = {}
11
+ @features = []
12
+ @compounds = []
13
+ end
14
+
15
+ def self.find(uri)
16
+ YAML.load RestClient.get(uri, :accept => 'application/x-yaml').to_s
17
+ end
18
+
19
+
20
+ def save
21
+ @features.uniq!
22
+ @compounds.uniq!
23
+ RestClient::Resource.new(@@config[:services]["opentox-dataset"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]).post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s
10
24
  end
11
25
 
26
+ =begin
12
27
  # create/add to entry from uris or Redland::Resources
13
28
  def add(compound,feature,value)
14
29
  compound = self.find_or_create_compound compound unless compound.class == Redland::Resource
@@ -87,17 +102,7 @@ module OpenTox
87
102
  resource = RestClient::Resource.new(@@config[:services]["opentox-dataset"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
88
103
  uri = resource.post data, :content_type => content_type
89
104
  dataset = Dataset.new
90
- dataset.read uri.to_s
91
- dataset
92
- end
93
-
94
- def self.find(uri)
95
- dataset = Dataset.new
96
- LOGGER.debug "Getting data from #{uri}"
97
- data = `curl "#{uri}"`
98
- #LOGGER.debug data
99
- #data = RestClient.get(uri, :accept => 'application/rdf+xml') # unclear why this does not work for complex uris, Dataset.find works from irb
100
- dataset.rdf = data
105
+ dataset.read uri.chomp.to_s
101
106
  dataset
102
107
  end
103
108
 
@@ -165,35 +170,13 @@ module OpenTox
165
170
  resource.delete
166
171
  end
167
172
 
168
- def save
169
- LOGGER.debug "Saving dataset"
170
- #task_uri = RestClient.post(@@config[:services]["opentox-dataset"], self.rdf, :content_type => "application/rdf+xml").to_s
171
- task_uri = RestClient::Resource.new(@@config[:services]["opentox-dataset"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]).post(self.rdf, :content_type => "application/rdf+xml").to_s
172
- task = OpenTox::Task.find(task_uri)
173
- LOGGER.debug "Waiting for task #{task_uri}"
174
- task.wait_for_completion
175
- LOGGER.debug "Dataset task #{task_uri} completed"
176
- if task.failed?
177
- LOGGER.error "Saving dataset failed"
178
- task.failed
179
- exit
180
- end
181
- task.resource
173
+ def to_owl
182
174
  end
183
175
 
184
- def to_yaml
185
- {
186
- :uri => self.uri,
187
- :opentox_class => self.owl_class,
188
- :title => self.title,
189
- :source => self.source,
190
- :identifier => self.identifier,
191
- :compounds => self.compounds.collect{|c| c.to_s.to_s.sub(/^\[(.*)\]$/,'\1')},
192
- :features => self.features.collect{|f| f.to_s },
193
- :data => self.data
194
- }.to_yaml
176
+ def from_owl
195
177
  end
196
178
 
179
+ =end
197
180
  end
198
181
 
199
182
  end
data/lib/model.rb CHANGED
@@ -2,6 +2,31 @@ module OpenTox
2
2
  module Model
3
3
 
4
4
  class Lazar
5
+
6
+ attr_accessor :dependent_variable, :activity_dataset_uri, :feature_dataset_uri, :effects, :activities, :p_values, :fingerprints, :features, :algorithm
7
+
8
+ def initialize
9
+ @source = "http://github.com/helma/opentox-model"
10
+ @algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
11
+ #@independent_variables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative")
12
+ @features = []
13
+ @effects = {}
14
+ @activities = {}
15
+ @p_values = {}
16
+ @fingerprints = {}
17
+ end
18
+
19
+ def save
20
+ @features.uniq!
21
+ resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
22
+ resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s
23
+ end
24
+
25
+
26
+ def self.find_all
27
+ RestClient.get(@@config[:services]["opentox-model"]).chomp.split("\n")
28
+ end
29
+ =begin
5
30
  include Owl
6
31
 
7
32
  # Create a new prediction model from a dataset
@@ -28,10 +53,6 @@ module OpenTox
28
53
  lazar
29
54
  end
30
55
 
31
- def self.find_all
32
- RestClient.get(@@config[:services]["opentox-model"]).split("\n")
33
- end
34
-
35
56
  def self.find(uri)
36
57
  yaml = RestClient.get(uri, :accept => "application/x-yaml")
37
58
  OpenTox::Model::Lazar.from_yaml(yaml)
@@ -50,7 +71,7 @@ module OpenTox
50
71
 
51
72
  def self.create(data)
52
73
  resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
53
- resource.post(data, :content_type => "application/x-yaml").to_s
74
+ resource.post(data, :content_type => "application/x-yaml").chomp.to_s
54
75
  end
55
76
 
56
77
  def delete
@@ -101,6 +122,7 @@ module OpenTox
101
122
  @model.add me, OT['predictedVariables'], Redland::Uri.new(predictedVariables) # untyped individual comes from this line, why??
102
123
  @model.add Redland::Uri.new(predictedVariables), RDF['type'], OT['Feature']
103
124
  end
125
+ =end
104
126
  end
105
127
  end
106
128
  end
data/lib/owl.rb CHANGED
@@ -1,31 +1,127 @@
1
1
  module OpenTox
2
2
 
3
- module Owl
3
+ class Owl
4
4
 
5
- attr_reader :uri
6
-
7
- def initialize
5
+ attr_reader :uri, :ot_class
8
6
 
7
+ def initialize(ot_class,uri)
9
8
  @model = Redland::Model.new Redland::MemoryStore.new
10
9
  @parser = Redland::Parser.new
11
- @serializer = Redland::Serializer.ntriples
12
-
13
- # read OT Ontology
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")),'/')
16
- # reate an anonymous resource for metadata
17
- # this has to be rewritten with an URI as soon as the resource has been saved at an definitive location
18
- tmp = @model.create_resource
19
- @model.add tmp, RDF['type'], OT[self.owl_class]
10
+ @ot_class = ot_class
11
+ @uri = Redland::Uri.new(uri.chomp)
12
+ @model.add @uri, RDF['type'], OT[@ot_class]
13
+ @model.add @uri, DC['identifier'], @uri
14
+ end
15
+
16
+ def method_missing(name, *args)
17
+ methods = ['title', 'source', 'identifier', 'algorithm', 'independentVariables', 'dependentVariables', 'predictedVariables', 'date','trainingDataset' ]
18
+ if methods.include? name.to_s.sub(/=/,'')
19
+ if /=/ =~ name.to_s # setter
20
+ name = name.to_s.sub(/=/,'')
21
+ begin # delete existing entry
22
+ t = @model.object(@uri, DC[name])
23
+ @model.delete @uri, DC[name], t
24
+ rescue
25
+ end
26
+ @model.add @uri, DC[name], args.first
27
+ else # getter
28
+ @model.object(@uri, DC['title']).to_s
29
+ end
30
+ else
31
+ raise "Method '#{name.to_s}' not found."
32
+ end
33
+ end
34
+
35
+ def parameters=(params)
36
+ params.each do |name, settings|
37
+ parameter = @model.create_resource
38
+ @model.add parameter, RDF['type'], OT['Parameter']
39
+ @model.add parameter, DC['title'], name
40
+ @model.add parameter, OT['paramScope'], settings[:scope]
41
+ @model.add parameter, OT['paramValue'], settings[:value]
42
+ end
43
+ end
44
+
45
+ def add_data_entries(compound_uri,features)
46
+ # add compound
47
+ compound = @model.subject(DC["identifier"], compound_uri)
48
+ if compound.nil?
49
+ compound = @model.create_resource(compound_uri)
50
+ @model.add compound, RDF['type'], OT["Compound"]
51
+ @model.add compound, DC["identifier"], compound_uri
52
+ end
53
+ features.each do |f|
54
+ f.each do |feature_uri,value|
55
+ # add feature
56
+ feature = find_or_create_feature feature_uri
57
+ if value.class.to_s == 'Hash'
58
+ # create tuple
59
+ tuple = @model.create_resource
60
+ @model.add tuple, RDF['type'], OT["Tuple"]
61
+ @model.add tuple, OT['feature'], feature
62
+ value.each do |uri,v|
63
+ f = find_or_create_feature uri
64
+ complex_value = @model.create_resource
65
+ @model.add tuple, OT['complexValue'], complex_value
66
+ @model.add complex_value, RDF['type'], OT["FeatureValue"]
67
+ @model.add complex_value, OT['feature'], f
68
+ @model.add complex_value, OT['value'], v.to_s
69
+ end
70
+ # add data entry
71
+ data_entry = @model.subject OT['compound'], compound
72
+ if data_entry.nil?
73
+ data_entry = @model.create_resource
74
+ @model.add @uri, OT['dataEntry'], data_entry
75
+ @model.add data_entry, RDF['type'], OT["DataEntry"]
76
+ @model.add data_entry, OT['compound'], compound
77
+ end
78
+ @model.add data_entry, OT['values'], tuple
79
+ else
80
+ data_entry = @model.subject OT['compound'], compound
81
+ if data_entry.nil?
82
+ data_entry = @model.create_resource
83
+ @model.add @uri, OT['dataEntry'], data_entry
84
+ @model.add data_entry, RDF['type'], OT["DataEntry"]
85
+ @model.add data_entry, OT['compound'], compound
86
+ end
87
+ values = @model.create_resource
88
+ @model.add data_entry, OT['values'], values
89
+ @model.add values, RDF['type'], OT['FeatureValue']
90
+ @model.add values, OT['feature'], feature
91
+ @model.add values, OT['value'], value.to_s
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ def find_or_create_feature(feature_uri)
98
+ feature = @model.subject(DC["identifier"], feature_uri)
99
+ if feature.nil?
100
+ feature = @model.create_resource(feature_uri)
101
+ @model.add feature, RDF['type'], OT["Feature"]
102
+ @model.add feature, DC["identifier"], feature_uri
103
+ @model.add feature, DC["title"], File.basename(feature_uri).split(/#/)[1]
104
+ @model.add feature, DC['source'], feature_uri
105
+ end
106
+ feature
107
+ end
108
+
109
+ def rdf
110
+ @model.to_string
111
+ end
112
+
113
+ =begin
114
+
115
+ def to_ntriples
116
+ @serializer.model_to_string(Redland::Uri.new(@uri), @model)
20
117
  end
21
118
 
22
119
  def uri=(uri)
23
- @uri = uri
24
- uri = Redland::Uri.new(uri)
120
+ @uri = uri.chomp
25
121
  # rewrite uri
26
- @model.subjects(RDF['type'],OT[self.owl_class]).each do |me|
27
- @model.delete(me,RDF['type'],OT[self.owl_class])
28
- @model.add(uri,RDF['type'],OT[self.owl_class])
122
+ @model.subjects(RDF['type'],OT[@ot_class]).each do |me|
123
+ @model.delete(me,RDF['type'],OT[@ot_class])
124
+ @model.add(uri,RDF['type'],OT[@ot_class])
29
125
  id = @model.object(me, DC['identifier'])
30
126
  @model.delete me, DC['identifier'], id
31
127
  # find/replace metadata
@@ -37,17 +133,18 @@ module OpenTox
37
133
  end
38
134
  end
39
135
 
40
- def title
41
- # I have no idea, why 2 subjects are returned
42
- # iterating over all subjects leads to memory allocation problems
43
- # SPARQL queries also do not work
44
- #me = @model.subjects(RDF['type'],OT[self.owl_class])[1]
45
- me = @model.subject(RDF['type'],OT[self.owl_class])
46
- @model.object(me, DC['title']).to_s
136
+ def read(uri)
137
+ @parser.parse_into_model(@model,uri)
138
+ @uri = uri
139
+ end
140
+
141
+ def identifier
142
+ me = @model.subject(RDF['type'],OT[@ot_class])
143
+ @model.object(me, DC['identifier']).to_s unless me.nil?
47
144
  end
48
145
 
49
146
  def title=(title)
50
- me = @model.subject(RDF['type'],OT[self.owl_class])
147
+ me = @model.subject(RDF['type'],OT[@ot_class])
51
148
  begin
52
149
  t = @model.object(me, DC['title'])
53
150
  @model.delete me, DC['title'], t
@@ -56,13 +153,8 @@ module OpenTox
56
153
  @model.add me, DC['title'], title
57
154
  end
58
155
 
59
- def source
60
- me = @model.subject(RDF['type'],OT[self.owl_class])
61
- @model.object(me, DC['source']).to_s unless me.nil?
62
- end
63
-
64
156
  def source=(source)
65
- me = @model.subject(RDF['type'],OT[self.owl_class])
157
+ me = @model.subject(RDF['type'],OT[@ot_class])
66
158
  begin
67
159
  t = @model.object(me, DC['source'])
68
160
  @model.delete me, DC['source'], t
@@ -71,44 +163,19 @@ module OpenTox
71
163
  @model.add me, DC['source'], source
72
164
  end
73
165
 
74
- def identifier
75
- me = @model.subject(RDF['type'],OT[self.owl_class])
76
- @model.object(me, DC['identifier']).to_s unless me.nil?
77
- end
78
-
79
- def owl_class
80
- self.class.to_s.sub(/^OpenTox::/,'').sub(/::.*$/,'')
81
- end
82
-
83
- def read(uri)
84
- @parser.parse_into_model(@model,uri)
85
- @uri = uri
86
- end
87
-
88
- def rdf=(rdf)
89
- @uri = '/' unless @uri
90
- @parser.parse_string_into_model(@model,rdf,@uri)
91
- end
92
-
93
- def rdf
94
- @model.to_string
95
- end
96
-
97
- def to_ntriples
98
- @serializer.model_to_string(Redland::Uri.new(@uri), @model)
166
+ def title
167
+ # I have no idea, why 2 subjects are returned
168
+ # iterating over all subjects leads to memory allocation problems
169
+ # SPARQL queries also do not work
170
+ #me = @model.subjects(RDF['type'],OT[@ot_class])[1]
171
+ me = @model.subject(RDF['type'],OT[@ot_class])
172
+ @model.object(me, DC['title']).to_s
99
173
  end
100
174
 
101
- def parameters=(params)
102
- params.each do |name, settings|
103
- parameter = @model.create_resource
104
- @model.add parameter, RDF['type'], OT['Parameter']
105
- @model.add parameter, DC['title'], name
106
- @model.add parameter, OT['paramScope'], settings[:scope]
107
- @model.add parameter, OT['paramValue'], settings[:value]
108
- end
175
+ def source
176
+ me = @model.subject(RDF['type'],OT[@ot_class])
177
+ @model.object(me, DC['source']).to_s unless me.nil?
109
178
  end
110
-
111
- =begin
112
179
  def create_owl_statement(name,value)
113
180
  r = @model.create_resource
114
181
  dc_class = DC[name.gsub(/^[a-z]/) { |a| a.upcase }] # capitalize only the first letter
data/lib/task.rb CHANGED
@@ -7,16 +7,13 @@ module OpenTox
7
7
  attr_accessor :uri
8
8
 
9
9
  def initialize(uri)
10
- #super()
11
- @uri = uri
10
+ @uri = uri.chomp
12
11
  end
13
12
 
14
13
  def self.create
15
- #uri = RestClient.post @@config[:services]["opentox-task"], {}
16
14
  resource = RestClient::Resource.new(@@config[:services]["opentox-task"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
17
- #uri = resource.post(nil)
18
- uri = resource.post({})
19
- Task.new(uri)
15
+ uri = resource.post({}).chomp
16
+ Task.new(uri.chomp)
20
17
  end
21
18
 
22
19
  def self.find(uri)
@@ -28,7 +25,7 @@ module OpenTox
28
25
  end
29
26
 
30
27
  def self.all
31
- task_uris = RestClient.get(@@config[:services]["opentox-task"]).split(/\n/)
28
+ task_uris = RestClient.get(@@config[:services]["opentox-task"]).chomp.split(/\n/)
32
29
  task_uris.collect{|uri| Task.new(uri)}
33
30
  end
34
31
 
@@ -64,22 +61,7 @@ module OpenTox
64
61
  resource.put :resource => uri
65
62
  end
66
63
 
67
- =begin
68
- def started
69
- RestClient.put File.join(@uri,'started'), {}
70
- end
71
-
72
- def cancel
73
- RestClient.put File.join(@uri,'cancelled'), {}
74
- end
75
-
76
- def completed(uri)
77
- RestClient.put File.join(@uri,'completed'), :resource => uri
78
- end
79
-
80
- =end
81
64
  def failed
82
- #RestClient.put File.join(@uri,'failed'), {}
83
65
  resource = RestClient::Resource.new(File.join(@uri,'failed'), :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
84
66
  resource.put({})
85
67
  end
@@ -104,9 +86,9 @@ module OpenTox
104
86
  self.status.to_s == 'failed'
105
87
  end
106
88
 
107
- def wait_for_completion
89
+ def wait_for_completion(dur=0.1)
108
90
  until self.completed? or self.failed?
109
- sleep 1
91
+ sleep dur
110
92
  end
111
93
  end
112
94
 
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.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Helma
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-02 00:00:00 +01:00
12
+ date: 2010-03-18 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency