From 6c35e3f8369ba96cb251eac487424bd949fdcf6c Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 2 Jul 2013 19:23:01 +0200 Subject: Algorithms and Models are modules instead of classes. --- lib/algorithm.rb | 57 ++++++++++++++++++++++++++++++++++----------------- lib/authorization.rb | 10 +++++++-- lib/compound.rb | 4 ++++ lib/dataset.rb | 3 +++ lib/model.rb | 23 +++++++++++++++++++-- lib/opentox-client.rb | 12 +++++++++-- lib/opentox.rb | 22 ++++++++++++-------- 7 files changed, 98 insertions(+), 33 deletions(-) (limited to 'lib') diff --git a/lib/algorithm.rb b/lib/algorithm.rb index 1ffc883..7d9a5a2 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -1,7 +1,8 @@ module OpenTox # Wrapper for OpenTox Algorithms - class Algorithm + module Algorithm + include OpenTox # Execute algorithm with parameters, please consult the OpenTox API and the webservice documentation for acceptable parameters # @param [optional,Hash] params Algorithm parameters @@ -11,32 +12,50 @@ module OpenTox uri = RestClientWrapper.post @uri, params, { :content_type => "text/uri-list", :subjectid => @subjectid} wait_for_task uri if wait end - end - module Descriptor + class Generic + include OpenTox + include Algorithm + end - class Smarts + class Descriptor + include OpenTox + include Algorithm + + [:smarts_match,:smarts_count,:openbabel,:cdk,:joelib,:physchem,:lookup].each do |descriptor| + Descriptor.define_singleton_method(descriptor) do |compounds,descriptors| + descriptors = [descriptors] unless descriptors.is_a? Array + case compounds.class.to_s + when "Array" + klasses = compounds.collect{|c| c.class}.uniq + bad_request_error "First argument contains objects with a different class than OpenTox::Compound or OpenTox::Dataset #{klasses.inspect}" unless klasses.size == 1 and klasses.first == Compound + JSON.parse(Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s), SUBJECTID).run(:compound_uri => compounds.collect{|c| c.uri}, :descriptors => descriptors)) + when "OpenTox::Compound" + JSON.parse(Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s), SUBJECTID).run(:compound_uri => compounds.uri, :descriptors => descriptors)) + when "OpenTox::Dataset" + task_uri = Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s), SUBJECTID).run(:dataset_uri => compounds.uri, :descriptors => descriptors) + puts task_uri + #task_uri + Dataset.new(Task.new(task_uri).wait_for_task) + else + bad_request_error "First argument contains objects with a different class than OpenTox::Compound or OpenTox::Dataset" + end - def self.fingerprint compounds, smarts, count=false - matcher = Algorithm.new File.join($algorithm[:uri],"descriptor","smarts","fingerprint") - smarts = [smarts] unless smarts.is_a? Array - if compounds.is_a? OpenTox::Compound - json = matcher.run :compound_uri => compounds.uri, :smarts => smarts, :count => count - elsif compounds.is_a? OpenTox::Dataset - # TODO: add task and return dataset instead of result - json = matcher.run :dataset_uri => compounds.uri, :smarts => smarts, :count => count - else - bad_request_error "Cannot match smarts on #{compounds.class} objects." end - - JSON.parse json end - def self.count compounds, smarts - fingerprint compounds,smarts,true - end end + class Fminer + include OpenTox + include Algorithm + def self.bbrc params + Fminer.new(File.join(service_uri, "fminer", "bbrc")).run params + end + def self.last params + Fminer.new(File.join(service_uri, "fminer", "last")).run params + end + end end end diff --git a/lib/authorization.rb b/lib/authorization.rb index 7f0e840..4b63cef 100644 --- a/lib/authorization.rb +++ b/lib/authorization.rb @@ -1,6 +1,12 @@ module OpenTox - AA = $aa[:uri] if defined? $aa - AA ||= "https://opensso.in-silico.ch" #if not set in .opentox/conf/[SERVICE].rb + if defined?($aa) and $aa[:uri] + AA = $aa[:uri] + SUBJECTID = OpenTox::Authorization.authenticate($aa[:user],$aa[:password]) + unauthorized_error "Failed to authenticate user \"#{$aa[:user]}\"." unless OpenTox::Authorization.is_token_valid(SUBJECTID) + else + AA = "https://opensso.in-silico.ch" #if not set in .opentox/conf/[SERVICE].rb + SUBJECTID = nil + end #Module for Authorization and Authentication #@example Authentication # require "opentox-client" diff --git a/lib/compound.rb b/lib/compound.rb index daeabb9..9bd2066 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -6,6 +6,10 @@ module OpenTox # Ruby wrapper for OpenTox Compound Webservices (http://opentox.org/dev/apis/api-1.2/structure). class Compound + def ==(c) + @uri == c.uri + end + # Create a compound from smiles string # @example # compound = OpenTox::Compound.from_smiles("c1ccccc1") diff --git a/lib/dataset.rb b/lib/dataset.rb index 5df20b4..354b443 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -64,6 +64,8 @@ module OpenTox RestClientWrapper.get(service_uri,{:query => sparql},{:accept => "text/uri-list", :subjectid => @subjectid}).split("\n").each do |row| r,c,v = row.split("\t") @data_entries[r.to_i] ||= [] + #v = v.to_f if v.numeric? + #v = nil if v.is_a? String and v.empty? @data_entries[r.to_i][c.to_i] = v end # TODO: fallbacks for external and unordered datasets @@ -169,6 +171,7 @@ module OpenTox # TODO: remove? might be dangerous if feature ordering is incorrect # MG: I would not remove this because add_data_entry is very slow (4 times searching in arrays) + # CH: do you have measurements? compound and feature arrays are not that big, I suspect that feature search/creation is the time critical step # @param row [Array] # @example # d = Dataset.new diff --git a/lib/model.rb b/lib/model.rb index e49eff3..1dbac6d 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -1,7 +1,7 @@ module OpenTox - class Model - + module Model +=begin # Run a model with parameters # @param params [Hash] Parameters for OpenTox model # @param wait [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly @@ -10,6 +10,7 @@ module OpenTox uri = RestClientWrapper.post @uri, params, { :content_type => "text/uri-list", :subjectid => @subjectid} wait_for_task uri if wait end +=end def feature_type # CH: subjectid is a object variable, no need to pass it as a parameter unless @feature_type @@ -41,5 +42,23 @@ module OpenTox end end + class Generic + include OpenTox + include OpenTox::Algorithm + end + + class Lazar + include OpenTox + include OpenTox::Algorithm + def self.create params + Lazar.new(File.join($algorithm[:uri], "lazar")).run params + end + + def predict params + run params + end + + end + end end diff --git a/lib/opentox-client.rb b/lib/opentox-client.rb index 02724c2..f25f05a 100644 --- a/lib/opentox-client.rb +++ b/lib/opentox-client.rb @@ -10,6 +10,13 @@ require 'json' require 'logger' require "securerandom" +default_config = File.join(ENV["HOME"],".opentox","config","default.rb") +client_config = File.join(ENV["HOME"],".opentox","config","opentox-client.rb") + +puts "Could not find configuration files #{default_config} or #{client_config}" unless File.exist? default_config or File.exist? client_config +require default_config if File.exist? default_config +require client_config if File.exist? client_config + # define constants and global variables RDF::OT = RDF::Vocabulary.new 'http://www.opentox.org/api/1.2#' RDF::OT1 = RDF::Vocabulary.new 'http://www.opentox.org/api/1.1#' @@ -19,7 +26,8 @@ RDF::TB = RDF::Vocabulary.new "http://onto.toxbank.net/api/" RDF::ISA = RDF::Vocabulary.new "http://onto.toxbank.net/isa/" RDF::OWL = RDF::Vocabulary.new "http://www.w3.org/2002/07/owl#" -CLASSES = ["Generic", "Compound", "Feature", "Dataset", "Algorithm", "Model", "Validation", "Task", "Investigation"] +#CLASSES = ["Generic", "Compound", "Feature", "Dataset", "Algorithm", "Model", "Validation", "Task", "Investigation"] +CLASSES = ["Compound", "Feature", "Dataset", "Validation", "Task", "Investigation"] RDF_FORMATS = [:rdfxml,:ntriples,:turtle] # Regular expressions for parsing classification data @@ -38,8 +46,8 @@ FALSE_REGEXP = /^(false|inactive|0|0.0|low tox|deactivating|non-carcinogen|non-m "compound.rb", "feature.rb", "dataset.rb", - "model.rb", "algorithm.rb", + "model.rb", "validation.rb" ].each{ |f| require_relative f } diff --git a/lib/opentox.rb b/lib/opentox.rb index 52f705c..044da4b 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -190,6 +190,20 @@ module OpenTox end end + # define class methods within module + def self.included(base) + base.extend(ClassMethods) + end + + module ClassMethods + def service_uri + service = self.to_s.split('::')[1].downcase + eval("$#{service}[:uri]") + rescue + bad_request_error "$#{service}[:uri] variable not set. Please set $#{service}[:uri] or use an explicit uri as first constructor argument " + end + end + # create default OpenTox classes with class methods # (defined in opentox-client.rb) CLASSES.each do |klass| @@ -237,14 +251,6 @@ module OpenTox self.new uris.first, subjectid end end - - def self.service_uri - service = self.to_s.split('::').last.downcase - eval("$#{service}[:uri]") - rescue - bad_request_error "$#{service}[:uri] variable not set. Please set $#{service}[:uri] or use an explicit uri as first constructor argument " - end - end OpenTox.const_set klass,c end -- cgit v1.2.3