opentox-ruby-api-wrapper 1.6.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -26,7 +26,8 @@ begin
26
26
  "spreadsheet",
27
27
  "google-spreadsheet-ruby",
28
28
  "tmail",
29
- "rinruby"
29
+ "rinruby",
30
+ "rjb"
30
31
  ].each { |dep| gem.add_dependency dep }
31
32
  [ "dm-core",
32
33
  'dm-serializer',
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.6.0
1
+ 1.6.1
@@ -22,9 +22,7 @@ module OpenTox
22
22
  LOGGER.debug params
23
23
  LOGGER.debug File.basename(__FILE__) + ": creating model"
24
24
  LOGGER.debug File.join(@@config[:services]["opentox-algorithm"], "lazar")
25
- #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")
26
25
  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")
27
- #@uri = resource.post(:dataset_uri => params[:dataset_uri], :feature_uri => params[:feature_uri], :feature_generation_uri => File.join(@@config[:services]["opentox-algorithm"], "fminer")).chomp
28
26
  @uri = resource.post(:dataset_uri => params[:dataset_uri], :prediction_feature => params[:prediction_feature], :feature_generation_uri => File.join(@@config[:services]["opentox-algorithm"], "fminer")).body.chomp
29
27
  end
30
28
 
@@ -37,12 +35,12 @@ module OpenTox
37
35
  class Similarity
38
36
  def self.weighted_tanimoto(fp_a,fp_b,p)
39
37
  common_features = fp_a & fp_b
40
- all_features = fp_a + fp_b
38
+ all_features = (fp_a + fp_b).uniq
41
39
  common_p_sum = 0.0
42
40
  if common_features.size > 0
43
- common_features.each{|f| common_p_sum += p[f]}
41
+ common_features.each{|f| common_p_sum += OpenTox::Utils.gauss(p[f])}
44
42
  all_p_sum = 0.0
45
- all_features.each{|f| all_p_sum += p[f]}
43
+ all_features.each{|f| all_p_sum += OpenTox::Utils.gauss(p[f])}
46
44
  common_p_sum/all_p_sum
47
45
  else
48
46
  0.0
@@ -52,12 +52,30 @@ module OpenTox
52
52
  end
53
53
 
54
54
  def png
55
- RestClientWrapper.get("#{@@ambit_uri}#{smiles}")
55
+ RestClientWrapper.get(File.join @uri, "image")
56
56
  end
57
57
 
58
+ def names
59
+ begin
60
+ RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/names")
61
+ rescue
62
+ "not available"
63
+ end
64
+ end
65
+
66
+ def display_smarts_uri(activating, deactivating, highlight = nil)
67
+ LOGGER.debug activating.to_yaml unless activating.nil?
68
+ activating_smarts = URI.encode "\"#{activating.join("\"/\"")}\""
69
+ deactivating_smarts = URI.encode "\"#{deactivating.join("\"/\"")}\""
70
+ if highlight.nil?
71
+ File.join @@config[:services]["opentox-compound"], "smiles", URI.encode(smiles), "smarts/activating", URI.encode(activating_smarts),"deactivating", URI.encode(deactivating_smarts)
72
+ else
73
+ File.join @@config[:services]["opentox-compound"], "smiles", URI.encode(smiles), "smarts/activating", URI.encode(activating_smarts),"deactivating", URI.encode(deactivating_smarts), "highlight", URI.encode(highlight)
74
+ end
75
+ end
76
+
58
77
  def image_uri
59
- "#{@@ambit_uri}#{smiles}"
60
- #"#{@@cactus_uri}#{@inchi}/image"
78
+ File.join @uri, "image"
61
79
  end
62
80
 
63
81
  # Matchs a smarts string
@@ -2,50 +2,64 @@ LOGGER.progname = File.expand_path(__FILE__)
2
2
 
3
3
  module OpenTox
4
4
 
5
- class Dataset
5
+ class Dataset
6
6
 
7
- attr_accessor :uri, :title, :creator, :data, :features, :compounds
7
+ attr_accessor :uri, :title, :creator, :data, :features, :compounds
8
8
 
9
- def initialize
10
- @data = {}
11
- @features = []
12
- @compounds = []
13
- end
14
-
15
- def self.find(uri, accept_header=nil)
9
+ def initialize( owl=nil )
10
+ @data = {}
11
+ @features = []
12
+ @compounds = []
13
+
14
+ # creates dataset object from Opentox::Owl object
15
+ # use Dataset.find( <uri> ) to load dataset from rdf-supporting datasetservice
16
+ # note: does not load all feature values, as this is time consuming
17
+ if owl
18
+ raise "invalid param" unless owl.is_a?(OpenTox::Owl)
19
+ @title = owl.get("title")
20
+ @creator = owl.get("creator")
21
+ @uri = owl.uri
22
+ # when loading a dataset from owl, only compound- and feature-uris are loaded
23
+ owl.load_dataset(@compounds, @features)
24
+ # all features are marked as dirty
25
+ # as soon as a feature-value is requested all values for this feature are loaded from the rdf
26
+ @dirty_features = @features.dclone
27
+ @owl = owl
28
+ end
29
+ end
30
+
31
+ def self.find(uri, accept_header=nil)
16
32
 
17
33
  unless accept_header
18
34
  #if uri.match(@@config[:services]["opentox-dataset"]) || uri=~ /188.40.32.88/ || uri =~ /informatik/
19
- if !@@config[:accept_headers]["opentox-dataset"].grep(/yaml/).empty?
35
+ if (uri.match(@@config[:services]["opentox-dataset"]) || uri =~ /in-silico.ch/) && !@@config[:accept_headers]["opentox-dataset"].grep(/yaml/).empty?
20
36
  accept_header = 'application/x-yaml'
21
37
  else
22
38
  accept_header = "application/rdf+xml"
23
39
  end
24
40
  end
41
+
25
42
  case accept_header
26
43
  when "application/x-yaml"
27
- d = YAML.load RestClientWrapper.get(uri.to_s.strip, :accept => 'application/x-yaml').to_s
44
+ d = YAML.load RestClientWrapper.get(uri.to_s.strip, :accept => 'application/x-yaml').to_s
28
45
  d.uri = uri unless d.uri
29
- when "application/rdf+xml"
30
- owl = OpenTox::Owl.from_uri(uri.to_s.strip, "Dataset")
31
-
32
- d = Dataset.new
33
- d.title = owl.get("title")
34
- d.creator = owl.get("creator")
35
- d.uri = owl.uri
36
-
37
- # when loading a dataset from owl, only compound- and feature-uris are loaded
38
- owl.load_dataset(d.compounds, d.features)
39
- # all features are marked as dirty, loaded dynamically later
40
- d.init_dirty_features(owl)
41
-
42
- d.compounds.uniq!
43
- d.features.uniq!
46
+ when "application/rdf+xml"
47
+ owl = OpenTox::Owl.from_uri(uri.to_s.strip, "Dataset")
48
+ d = Dataset.new(owl)
44
49
  else
45
50
  raise "cannot get datset with accept header: "+accept_header.to_s
46
- end
47
- return d
48
- end
51
+ end
52
+ d
53
+ end
54
+
55
+ # converts a dataset represented in owl to yaml
56
+ # (uses a temporary dataset)
57
+ # note: to_yaml is overwritten, loads complete owl dataset values
58
+ def self.owl_to_yaml( owl_data, uri)
59
+ owl = OpenTox::Owl.from_data(owl_data, uri, "Dataset")
60
+ d = Dataset.new(owl)
61
+ d.to_yaml
62
+ end
49
63
 
50
64
  # creates a new dataset, using only those compounsd specified in new_compounds
51
65
  # returns uri of new dataset
@@ -86,8 +100,8 @@ module OpenTox
86
100
  def get_predicted_class(compound, feature)
87
101
  v = get_value(compound, feature)
88
102
  if v.is_a?(Hash)
89
- k = v.keys.grep(/classification/).first
90
- unless k.empty?
103
+ k = v.keys.grep(/classification/).first
104
+ unless k.empty?
91
105
  #if v.has_key?(:classification)
92
106
  return v[k]
93
107
  else
@@ -109,8 +123,8 @@ module OpenTox
109
123
  def get_predicted_regression(compound, feature)
110
124
  v = get_value(compound, feature)
111
125
  if v.is_a?(Hash)
112
- k = v.keys.grep(/regression/).first
113
- unless k.empty?
126
+ k = v.keys.grep(/regression/).first
127
+ unless k.empty?
114
128
  return v[k]
115
129
  else
116
130
  return "no regression key"
@@ -131,11 +145,11 @@ module OpenTox
131
145
  def get_prediction_confidence(compound, feature)
132
146
  v = get_value(compound, feature)
133
147
  if v.is_a?(Hash)
134
- k = v.keys.grep(/confidence/).first
135
- unless k.empty?
148
+ k = v.keys.grep(/confidence/).first
149
+ unless k.empty?
136
150
  #if v.has_key?(:confidence)
137
151
  return v[k].abs
138
- #return v["http://ot-dev.in-silico.ch/model/lazar#confidence"].abs
152
+ #return v["http://ot-dev.in-silico.ch/model/lazar#confidence"].abs
139
153
  else
140
154
  # PENDING: return nil isntead of raising an exception
141
155
  raise "no confidence key"
@@ -143,12 +157,6 @@ module OpenTox
143
157
  else
144
158
  LOGGER.warn "no confidence for compound: "+compound.to_s+", feature: "+feature.to_s
145
159
  return 1
146
- # raise "prediction confidence value is not a hash value\n"+
147
- # "value "+v.to_s+"\n"+
148
- # "value-class "+v.class.to_s+"\n"+
149
- # "dataset "+@uri.to_s+"\n"+
150
- # "compound "+compound.to_s+"\n"+
151
- # "feature "+feature.to_s+"\n"
152
160
  end
153
161
  end
154
162
 
@@ -159,7 +167,7 @@ module OpenTox
159
167
  end
160
168
 
161
169
  v = @data[compound]
162
- raise "no values for compound "+compound.to_s if v==nil
170
+ return nil if v == nil # missing values for all features
163
171
  if v.is_a?(Array)
164
172
  # PENDING: why using an array here?
165
173
  v.each do |e|
@@ -171,7 +179,7 @@ module OpenTox
171
179
  raise "invalid internal value type"
172
180
  end
173
181
  end
174
- raise "feature value no found: "+feature.to_s
182
+ return nil #missing value
175
183
  else
176
184
  raise "value is not an array\n"+
177
185
  "value "+v.to_s+"\n"+
@@ -186,31 +194,33 @@ module OpenTox
186
194
  def load_feature_values(feature=nil)
187
195
  if feature
188
196
  raise "feature already loaded" unless @dirty_features.include?(feature)
189
- @owl.load_dataset_feature_values(@compounds, @data, feature)
197
+ @owl.load_dataset_feature_values(@compounds, @data, [feature])
190
198
  @dirty_features.delete(feature)
191
199
  else
192
- @data = {}
193
- @owl.load_dataset_feature_values(@compounds, @data)
200
+ @data = {} unless @data
201
+ @owl.load_dataset_feature_values(@compounds, @data, @dirty_features)
194
202
  @dirty_features.clear
195
203
  end
196
204
  end
197
-
198
- def save
199
- # loads all features before loading
205
+
206
+ # overwrite to yaml:
207
+ # in case dataset is loaded from owl:
208
+ # * load all values
209
+ # * set @owl to nil (not necessary in yaml)
210
+ def to_yaml
211
+ # loads all features
200
212
  if ((defined? @dirty_features) && @dirty_features.size > 0)
201
- load_feature_values()
213
+ load_feature_values
202
214
  end
203
-
204
- @features.uniq!
205
- @compounds.uniq!
206
- OpenTox::RestClientWrapper.post(@@config[:services]["opentox-dataset"],{:content_type => "application/x-yaml"},self.to_yaml).strip
207
- end
215
+ @owl = nil
216
+ super.to_yaml
217
+ end
208
218
 
209
- def init_dirty_features(owl)
210
- @dirty_features = @features
211
- @owl = owl
219
+ # saves (changes) as new dataset in dataset service
220
+ # returns uri
221
+ # uses to yaml method (which is overwritten)
222
+ def save
223
+ OpenTox::RestClientWrapper.post(@@config[:services]["opentox-dataset"],{:content_type => "application/x-yaml"},self.to_yaml).strip
212
224
  end
213
225
  end
214
-
215
-
216
226
  end
@@ -1,6 +1,6 @@
1
- require 'logger'
1
+ require "ot-logger"
2
2
  # set default environment
3
- ENV['RACK_ENV'] = 'test' unless ENV['RACK_ENV']
3
+ ENV['RACK_ENV'] = 'production' unless ENV['RACK_ENV']
4
4
 
5
5
  # load/setup configuration
6
6
  basedir = File.join(ENV['HOME'], ".opentox")
@@ -44,69 +44,9 @@ end
44
44
  # load mail settings for error messages
45
45
  load File.join config_dir,"mail.rb" if File.exists?(File.join config_dir,"mail.rb")
46
46
 
47
- # hack: store sinatra in global var to make url_for and halt methods accessible
48
- before{ $sinatra = self unless $sinatra }
49
-
50
- class Sinatra::Base
51
- # overwriting halt to log halts (!= 202)
52
- def halt(*response)
53
- LOGGER.error "halt "+response.first.to_s+" "+(response.size>1 ? response[1].to_s : "") if response and response.first and response.first >= 300
54
- # orig sinatra code:
55
- response = response.first if response.length == 1
56
- throw :halt, response
57
- end
58
- end
59
-
60
- # logging
61
- class MyLogger < Logger
62
-
63
- def pwd
64
- path = Dir.pwd.to_s
65
- index = path.rindex(/\//)
66
- return path if index==nil
67
- path[(index+1)..-1]
68
- end
69
-
70
- def trace()
71
- lines = caller(0)
72
- n = 2
73
- line = lines[n]
74
-
75
- while (line =~ /spork.rb/ or line =~ /as_task/ or line =~ /environment.rb/)
76
- n += 1
77
- line = lines[n]
78
- end
79
-
80
- index = line.rindex(/\/.*\.rb/)
81
- return line if index==nil
82
- line[index..-1]
83
- end
84
-
85
- def format(msg)
86
- pwd.ljust(18)+" :: "+msg.to_s+" :: "+trace+" :: "+($sinatra ? $sinatra.request.env['REMOTE_ADDR'] : nil).to_s
87
- end
88
-
89
- def debug(msg)
90
- super format(msg)
91
- end
92
-
93
- def info(msg)
94
- super format(msg)
95
- end
96
-
97
- def warn(msg)
98
- super format(msg)
99
- end
100
-
101
- def error(msg)
102
- super format(msg)
103
- end
104
-
105
- end
106
-
107
-
108
47
  logfile = "#{LOG_DIR}/#{ENV["RACK_ENV"]}.log"
109
- LOGGER = MyLogger.new(logfile,'daily') # daily rotation
48
+ #LOGGER = MyLogger.new(logfile,'daily') # daily rotation
49
+ LOGGER = MyLogger.new(logfile) # no rotation
110
50
  LOGGER.formatter = Logger::Formatter.new #this is neccessary to restore the formating in case active-record is loaded
111
51
  if @@config[:logger] and @@config[:logger] == "debug"
112
52
  LOGGER.level = Logger::DEBUG
@@ -128,14 +68,6 @@ rescue
128
68
  raise "Please edit #{user_file} and restart your application. Create at least one user with password."
129
69
  end
130
70
 
131
- # RDF namespaces
132
- RDF = Redland::Namespace.new 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
133
- OWL = Redland::Namespace.new 'http://www.w3.org/2002/07/owl#'
134
- DC = Redland::Namespace.new 'http://purl.org/dc/elements/1.1/'
135
- OT = Redland::Namespace.new 'http://www.opentox.org/api/1.1#'
136
- #OT = Redland::Namespace.new 'http://ortona.informatik.uni-freiburg.de/opentox.owl#'
137
- XML = Redland::Namespace.new 'http://www.w3.org/2001/XMLSchema#'
138
-
139
71
  # Regular expressions for parsing classification data
140
72
  TRUE_REGEXP = /^(true|active|1|1.0)$/i
141
73
  FALSE_REGEXP = /^(false|inactive|0|0.0)$/i
@@ -104,6 +104,11 @@ module OpenTox
104
104
  def self.find_all
105
105
  RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n")
106
106
  end
107
+
108
+ def self.predict(compound_uri,model_uri)
109
+ #RestClientWrapper.post(model_uri,{:compound_uri => compound_uri, :accept => 'application/x-yaml'})
110
+ `curl -X POST -d 'compound_uri=#{compound_uri}' -H 'Accept:application/x-yaml' #{model_uri}`
111
+ end
107
112
  end
108
113
  end
109
114
  end
@@ -8,6 +8,6 @@ rescue LoadError
8
8
  puts "Please install Openbabel with 'rake openbabel:install' in the compound component"
9
9
  end
10
10
 
11
- ['owl', 'compound','dataset','algorithm','model','task','validation','utils','authorization','features', 'rest_client_wrapper'].each do |lib|
11
+ ['owl', 'compound','dataset','algorithm','model','task','validation','utils','authorization','features', 'ot-logger', 'overwrite', 'rest_client_wrapper'].each do |lib|
12
12
  require lib
13
13
  end
@@ -0,0 +1,48 @@
1
+ require 'logger'
2
+ # logging
3
+ class MyLogger < Logger
4
+
5
+ def pwd
6
+ path = Dir.pwd.to_s
7
+ index = path.rindex(/\//)
8
+ return path if index==nil
9
+ path[(index+1)..-1]
10
+ end
11
+
12
+ def trace()
13
+ lines = caller(0)
14
+ n = 2
15
+ line = lines[n]
16
+
17
+ while (line =~ /spork.rb/ or line =~ /as_task/ or line =~ /environment.rb/)
18
+ n += 1
19
+ line = lines[n]
20
+ end
21
+
22
+ index = line.rindex(/\/.*\.rb/)
23
+ return line if index==nil
24
+ line[index..-1]
25
+ end
26
+
27
+ def format(msg)
28
+ pwd.ljust(18)+" :: "+msg.to_s+" :: "+trace+" :: "+($sinatra ? $sinatra.request.env['REMOTE_ADDR'] : nil).to_s
29
+ end
30
+
31
+ def debug(msg)
32
+ super format(msg)
33
+ end
34
+
35
+ def info(msg)
36
+ super format(msg)
37
+ end
38
+
39
+ def warn(msg)
40
+ super format(msg)
41
+ end
42
+
43
+ def error(msg)
44
+ super format(msg)
45
+ end
46
+
47
+ end
48
+