Module: OpenTox

Included in:
Algorithm, Algorithm::Descriptor, Algorithm::Fminer, Algorithm::Generic, Model::Generic
Defined in:
lib/error.rb,
lib/task.rb,
lib/model.rb,
lib/policy.rb,
lib/feature.rb,
lib/opentox.rb,
lib/dataset.rb,
lib/compound.rb,
lib/algorithm.rb,
lib/validation.rb,
lib/authorization.rb,
lib/rest-client-wrapper.rb

Overview

end

Defined Under Namespace

Modules: Algorithm, Authorization, ClassMethods, Model Classes: AlgorithmComparisonReport, Compound, Crossvalidation, CrossvalidationReport, Dataset, Error, Feature, Policies, Policy, RestClientWrapper, SubTask, Task, Validation, ValidationReport

Constant Summary collapse

AA =

if not set in .opentox/conf/.rb

"https://opensso.in-silico.ch"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#metadata(force_update = false) ⇒ Hash

Object metadata (lazy loading)


25
26
27
28
29
30
31
32
# File 'lib/opentox.rb', line 25

def  force_update=false
  if (@metadata.nil? or @metadata.empty? or force_update) and URI.accessible? @uri
    get if @rdf.nil? or @rdf.empty? or force_update 
    # return values as plain strings instead of RDF objects
    @metadata = @rdf.to_hash[RDF::URI.new(@uri)].inject({}) { |h, (predicate, values)| h[predicate] = values.collect{|v| v.to_s}; h }
  end
  @metadata
end

#parameters(force_update = false) ⇒ Hash

Object parameters (lazy loading) OpenTox API


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/opentox.rb', line 52

def parameters force_update=false
  if (@parameters.empty? or force_update) and URI.accessible? @uri
    get if @rdf.empty? or force_update
    params = {}
    query = RDF::Query.new({
      :parameter => {
        RDF.type  => RDF::OT.Parameter,
        :property => :value,
      }
    })
    query.execute(@rdf).each do |solution|
      params[solution.parameter] = {} unless params[solution.parameter] 
      params[solution.parameter][solution.property] = solution.value
    end
    @parameters = params.values
  end
  @parameters
end

#uriObject (readonly)

include RDF CH: leads to namespace clashes with URI class


8
9
10
# File 'lib/opentox.rb', line 8

def uri
  @uri
end

Class Method Details

.included(base) ⇒ Object

define class methods within module


201
202
203
# File 'lib/opentox.rb', line 201

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#[](predicate) ⇒ Array, String

Metadata values


37
38
39
40
# File 'lib/opentox.rb', line 37

def [](predicate)
  return nil if [predicate].nil?
  [predicate].size == 1 ? [predicate].first : [predicate]
end

#[]=(predicate, values) ⇒ Object

Set a metadata entry


45
46
47
# File 'lib/opentox.rb', line 45

def []=(predicate,values)
  @metadata[predicate] = [values].flatten
end

#cancelled?Boolean


101
102
103
# File 'lib/task.rb', line 101

def cancelled?
  code == 503
end

#codeObject


92
93
94
# File 'lib/task.rb', line 92

def code
  RestClientWrapper.get(@uri).code.to_i
end

#completed?Boolean


105
106
107
# File 'lib/task.rb', line 105

def completed?
  code == 200
end

#create_rdfObject


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/opentox.rb', line 131

def create_rdf
  #$logger.debug "#{eval("RDF::OT."+self.class.to_s.split('::').last)}\n"
  @rdf = RDF::Graph.new
  # DG: since model is no self.class anymore 
  @metadata[RDF.type] ||= (eval("RDF::OT."+self.class.to_s.split('::').last) =~ /Lazar|Generic/) ? RDF::URI.new(RDF::OT.Model) : RDF::URI.new(eval("RDF::OT."+self.class.to_s.split('::').last))
  #@metadata[RDF.type] ||= RDF::URI.new(eval("RDF::OT."+self.class.to_s.split('::').last))
  @metadata[RDF::DC.date] ||= DateTime.now
  # DG: uri in object should be in brackets, otherwise query for uri-list ignores the object.
  # see: http://www.w3.org/TR/rdf-testcases/#sec-uri-encoding
  @metadata.each do |predicate,values|
    [values].flatten.each{ |value| @rdf << [RDF::URI.new(@uri), predicate, (URI.valid?(value) ? RDF::URI.new(value) : value)] unless value.nil? }
  end
  @parameters.each do |parameter|
    p_node = RDF::Node.new
    @rdf << [RDF::URI.new(@uri), RDF::OT.parameters, p_node]
    @rdf << [p_node, RDF.type, RDF::OT.Parameter]
    parameter.each { |k,v| @rdf << [p_node, k, v] unless v.nil?}
  end
end

#deleteObject

Delete object at webservice


122
123
124
125
# File 'lib/opentox.rb', line 122

def delete 
  RestClientWrapper.delete(@uri)
  #Authorization.delete_policies_from_uri(@uri) if $aa[:uri]
end

#error?Boolean


109
110
111
# File 'lib/task.rb', line 109

def error?
  code >= 400 and code != 503
end

#error_reportObject


127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/task.rb', line 127

def error_report
  get
  report = {}
  query = RDF::Query.new({
    :report => {
      RDF.type  => RDF::OT.ErrorReport,
      :property => :value,
    }
  })
  query.execute(@rdf).each do |solution|
    report[solution.property] = solution.value.to_s
  end
  report
end

#get(mime_type = "text/plain") ⇒ Object

Get object from webservice


80
81
82
83
84
85
86
87
88
89
# File 'lib/opentox.rb', line 80

def get mime_type="text/plain"
  bad_request_error "Mime type #{mime_type} is not supported. Please use 'text/plain' (default) or 'application/rdf+xml'." unless mime_type == "text/plain" or mime_type == "application/rdf+xml"
  response = RestClientWrapper.get(@uri,{},{:accept => mime_type})
  if URI.task?(response)
    uri = wait_for_task response
    response = RestClientWrapper.get(uri,{},{:accept => mime_type})
  end
  parse_ntriples response if mime_type == "text/plain"
  parse_rdfxml response if mime_type == "application/rdf+xml"
end

#initialize(uri = nil) ⇒ OpenTox

Create a new OpenTox object


16
17
18
19
20
21
# File 'lib/opentox.rb', line 16

def initialize uri=nil
  @rdf = RDF::Graph.new
  @metadata = {}
  @parameters = []
  uri ? @uri = uri.to_s.chomp : @uri = File.join(service_uri, SecureRandom.uuid)
end

#parameter_value(title) ⇒ String

Parameter value


74
75
76
# File 'lib/opentox.rb', line 74

def parameter_value title
  @parameters.collect{|p| p[RDF::OT.paramValue] if p[RDF::DC.title] == title}.compact.first
end

#post(wait = true, mime_type = "text/plain") ⇒ Object

Deprecated.

Post object to webservice (append to object), rarely useful and deprecated


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/opentox.rb', line 93

def post wait=true, mime_type="text/plain"
  bad_request_error "Mime type #{mime_type} is not supported. Please use 'text/plain' (default) or 'application/rdf+xml'." unless mime_type == "text/plain" or mime_type == "application/rdf+xml"
  case mime_type
  when 'text/plain'
    body = self.to_ntriples
  when 'application/rdf+xml'
    body = self.to_rdfxml
  end
  #Authorization.check_policy(@uri) if $aa[:uri]
  uri = RestClientWrapper.post @uri.to_s, body, { :content_type => mime_type}
  wait ? wait_for_task(uri) : uri
end

#put(wait = true, mime_type = "text/plain") ⇒ Object

Save object at webservice (replace or create object)


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/opentox.rb', line 107

def put wait=true, mime_type="text/plain"
  bad_request_error "Mime type #{mime_type} is not supported. Please use 'text/plain' (default) or 'application/rdf+xml'." unless mime_type == "text/plain" or mime_type == "application/rdf+xml"
  @metadata[RDF::OT.created_at] = DateTime.now unless URI.accessible? @uri
  #@metadata[RDF::DC.modified] = DateTime.now
  case mime_type
  when 'text/plain'
    body = self.to_ntriples
  when 'application/rdf+xml'
    body = self.to_rdfxml
  end
  uri = RestClientWrapper.put @uri, body, { :content_type => mime_type}
  wait ? wait_for_task(uri) : uri
end

#running?Boolean

get only header for status requests


97
98
99
# File 'lib/task.rb', line 97

def running?
  code == 202 
end

#service_uriObject


127
128
129
# File 'lib/opentox.rb', line 127

def service_uri
  self.class.service_uri
end

#statusString

Check status of a task


123
124
125
# File 'lib/task.rb', line 123

def status
  self[RDF::OT.hasStatus]
end

#to_htmlString


186
187
188
# File 'lib/opentox.rb', line 186

def to_html
  to_turtle.to_html
end

#to_turtleString


176
177
178
179
180
181
182
183
# File 'lib/opentox.rb', line 176

def to_turtle # redefined to use prefixes (not supported by RDF::Writer)
  prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#"}
  ['OT', 'DC', 'XSD', 'OLO'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") }
  create_rdf
  RDF::Turtle::Writer.for(:turtle).buffer(:prefixes => prefixes)  do |writer|
    writer << @rdf
  end
end