summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2015-07-19 21:26:03 +0200
committerChristoph Helma <helma@in-silico.ch>2015-07-19 21:26:03 +0200
commit51e7a29c8aac568ff8ef04de5b15d2f6db8f66da (patch)
tree0f714ff3b20bb2cf156cfa9d4f802581f38688bb /lib
parent52fef792922c59d102825193fff05be9c3fa88a8 (diff)
lazar predictions working in principle
Diffstat (limited to 'lib')
-rw-r--r--lib/algorithm.rb77
-rw-r--r--lib/compound.rb4
-rw-r--r--lib/dataset.rb49
-rw-r--r--lib/feature.rb6
-rw-r--r--lib/opentox-client.rb10
-rw-r--r--lib/opentox.rb3
-rw-r--r--lib/task.rb15
7 files changed, 52 insertions, 112 deletions
diff --git a/lib/algorithm.rb b/lib/algorithm.rb
deleted file mode 100644
index 8f05d6a..0000000
--- a/lib/algorithm.rb
+++ /dev/null
@@ -1,77 +0,0 @@
-module OpenTox
-
- # Wrapper for OpenTox Algorithms
- 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
- # @param [optional,Boolean] wait set to false if method should return a task uri instead of the algorithm result
- # @return [String] URI of new resource (dataset, model, ...)
- def run params=nil, wait=true
- uri = RestClientWrapper.post @uri, params, { :content_type => "text/uri-list"}
- wait_for_task uri if wait
- end
-
- class Generic
- include OpenTox
- include Algorithm
- include Mongoid::Document
- include Mongoid::Timestamps
- field :parameters, type: Array
- end
-
- class Descriptor
- include OpenTox
- include Algorithm
- include Mongoid::Document
- include Mongoid::Timestamps
- field :parameters, type: Array
-
- [:smarts_match,:smarts_count,:physchem,:lookup].each do |descriptor|
- Descriptor.define_singleton_method(descriptor) do |compounds,descriptors=nil|
- 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)).run(:compound_uri => compounds.collect{|c| c.uri}, :descriptors => descriptors))
- when "OpenTox::Compound"
- p descriptor.to_s
- send descriptor.to_sym, compounds
- #JSON.parse(Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s)).run(:compound_uri => compounds.uri, :descriptors => descriptors))
- when "OpenTox::Dataset"
- task_uri = Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s)).run(:dataset_uri => compounds.uri, :descriptors => descriptors)
- Dataset.new(wait_for_task task_uri)
- else
- bad_request_error "First argument contains objects with a different class than OpenTox::Compound or OpenTox::Dataset"
- end
-
- end
- end
-
- # returns a hash, keys: physchem descriptors, values: their description
- def self.physchem_descriptors
- Hash[ RestClientWrapper.get(File.join(service_uri, "descriptor", "physchem", "list")).to_s.split("\n").collect{|l| l.split("\t")} ]
- end
-
- # returns array of "descriptor-values", as CDK descriptors calculate serveral values, e.g., ALOGP produces ALOGP.ALogP, ALOGP.ALogp2, ALOGP.AMR
- def self.physchem_descriptor_values
- RestClientWrapper.get(File.join(service_uri, "descriptor", "physchem", "list_values")).to_s.split("\n")
- 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/compound.rb b/lib/compound.rb
index b588c75..6cc4707 100644
--- a/lib/compound.rb
+++ b/lib/compound.rb
@@ -12,6 +12,10 @@ module OpenTox
@inchi = inchi
end
+ def == compound
+ self.inchi == compound.inchi
+ 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 bcbacb2..2a777bb 100644
--- a/lib/dataset.rb
+++ b/lib/dataset.rb
@@ -4,21 +4,17 @@ module OpenTox
# Ruby wrapper for OpenTox Dataset Webservices (http://opentox.org/dev/apis/api-1.2/dataset).
class Dataset
- include Mongoid::Document
+ include Mongoid::Document
- field :feature_ids, type: Array
- field :inchis, type: Array
- field :data_entries, type: Array
- field :warnings, type: Array
+ field :feature_ids, type: Array, default: []
+ field :inchis, type: Array, default: []
+ field :data_entries, type: Array, default: []
+ field :warnings, type: Array, default: []
field :source, type: String
- def initialize
- super
- self.feature_ids = []
- self.inchis = []
- self.data_entries = []
- self.warnings = []
- end
+ # TODO subclass Datasets
+ field :dependent_variables, type: BSON::ObjectId
+ field :predicted_variables, type: Array
# Readers
@@ -53,8 +49,8 @@ module OpenTox
# @param feature [OpenTox::Feature] OpenTox Feature object
# @return [Array] Data entry values
def values(compound, feature)
- rows = (0 ... inchis.length).select { |r| inchis[r].uri == compound.uri }
- col = feature_ids.collect{|f| f.uri}.index feature.uri
+ rows = (0 ... inchis.length).select { |r| inchis[r] == compound.inchi }
+ col = feature_ids.index feature.id
rows.collect{|row| data_entries[row][col]}
end
@@ -63,9 +59,9 @@ module OpenTox
# Search a dataset for a feature given its URI
# @param uri [String] Feature URI
# @return [OpenTox::Feature] Feature object, or nil if not present
- def find_feature_uri(uri)
- features.select{|f| f.uri == uri}.first
- end
+ #def find_feature_uri(uri)
+ #features.select{|f| f.uri == uri}.first
+ #end
# Search a dataset for a compound given its URI
# @param uri [String] Compound URI
@@ -120,17 +116,18 @@ module OpenTox
# @param value [Object] (will be converted to String)
# @return [Array] data_entries
def add_data_entry compound, feature, value
- @data["compounds"] << compound unless @data["compounds"].collect{|c| c.uri}.include?(compound.uri)
- row = @data["compounds"].collect{|c| c.uri}.index(compound.uri)
- @data["features"] << feature unless @data["features"].collect{|f| f.uri}.include?(feature.uri)
- col = @data["features"].collect{|f| f.uri}.index(feature.uri)
- if @data["data_entries"][row] and @data["data_entries"][row][col] # duplicated values
- @data["compounds"] << compound
- row = @data["compounds"].collect{|c| c.uri}.rindex(compound.uri)
+ # TODO: optimize
+ add_compound compound unless self.compounds.include?(compound)
+ row = self.compounds.index(compound)
+ add_feature feature unless self.features.include?(feature)
+ col = self.features.index(feature)
+ if self.data_entries[row] and self.data_entries[row][col] # duplicated values
+ add_compound compound
+ row = self.compounds.rindex(compound)
end
if value
- @data["data_entries"][row] ||= []
- @data["data_entries"][row][col] = value
+ self.data_entries[row] ||= []
+ self.data_entries[row][col] = value
end
end
diff --git a/lib/feature.rb b/lib/feature.rb
index 43cf7e9..b2ddce4 100644
--- a/lib/feature.rb
+++ b/lib/feature.rb
@@ -1,10 +1,16 @@
module OpenTox
+ # TODO subclass features
class Feature
field :string, type: Boolean, default: false
field :nominal, type: Boolean, default: false
field :numeric, type: Boolean, default: false
+ field :substructure, type: Boolean, default: false
+ field :prediction, type: Boolean
+ field :smarts, type: String
+ field :pValue, type: Float
+ field :effect, type: String
field :accept_values, type: Array
# Find out feature type
diff --git a/lib/opentox-client.rb b/lib/opentox-client.rb
index 6358705..40f87cf 100644
--- a/lib/opentox-client.rb
+++ b/lib/opentox-client.rb
@@ -4,11 +4,11 @@ require "bundler/setup"
#require 'rdf/raptor'
#require 'rdf/turtle'
require "rest-client"
-require 'uri'
+#require 'uri'
require 'yaml'
require 'json'
require 'logger'
-require "securerandom"
+#require "securerandom"
require 'mongoid'
default_config = File.join(ENV["HOME"],".opentox","config","default.rb")
@@ -50,8 +50,8 @@ FALSE_REGEXP = /^(false|inactive|0|0.0|low tox|deactivating|non-carcinogen|non-m
"compound.rb",
"feature.rb",
"dataset.rb",
- "algorithm.rb",
- "model.rb",
+ #"algorithm.rb",
+ #"model.rb",
"validation.rb"
].each{ |f| require_relative f }
@@ -61,7 +61,7 @@ FALSE_REGEXP = /^(false|inactive|0|0.0|low tox|deactivating|non-carcinogen|non-m
#end
# defaults to stderr, may be changed to file output (e.g in opentox-service)
-$logger = OTLogger.new(STDERR)
+$logger = OTLogger.new(STDOUT) # STDERR did not work on my development machine (CH)
$logger.level = Logger::DEBUG
#Mongo::Logger.logger = $logger
Mongo::Logger.level = Logger::WARN
diff --git a/lib/opentox.rb b/lib/opentox.rb
index 68a841b..554e686 100644
--- a/lib/opentox.rb
+++ b/lib/opentox.rb
@@ -15,7 +15,8 @@ module OpenTox
field :title, type: String
field :description, type: String
- field :parameters, type: Array
+ field :parameters, type: Array, default: []
+ field :creator, type: String
# TODO check if needed
def self.subjectid
diff --git a/lib/task.rb b/lib/task.rb
index 55d024d..cd2dd92 100644
--- a/lib/task.rb
+++ b/lib/task.rb
@@ -1,3 +1,5 @@
+# TODO: task seems to run twice, see fminser tests
+# TODO: do we need tasks for internal use
DEFAULT_TASK_MAX_DURATION = 36000
module OpenTox
# TODO: fix error reports
@@ -5,13 +7,16 @@ module OpenTox
# Class for handling asynchronous tasks
class Task
+ include Mongoid::Document
+ include Mongoid::Timestamps
field :creator, type: String
field :percentageCompleted, type: Float
field :error_code, type: Integer # workaround name, cannot overwrite accessors in current mongoid version
field :finished, type: Time # workaround name, cannot overwrite accessors in current mongoid version
# TODO
- field :result_object, type: String
+ field :result_type, type: String
+ field :result_id, type: BSON::ObjectId
field :report, type: String
field :pid, type: Integer
field :observer_pid, type: Integer
@@ -66,7 +71,7 @@ module OpenTox
end
def completed(result)
- update_attributes(:error_code => 200, :finished => Time.now, :percentageCompleted => 100, :result_object => result)
+ update_attributes(:error_code => 200, :finished => Time.now, :percentageCompleted => 100, :result_type => result.type, :result_id => result.id)
end
# waits for a task, unless time exceeds or state is no longer running
@@ -92,7 +97,11 @@ module OpenTox
end
def result
- OpenTox::Task.find(id).result_object
+ c = OpenTox::Task.find(id).result_type.downcase.to_sym
+ rid = OpenTox::Task.find(id).result_id
+ p c, rid
+ p $mongo[collection].all
+ $mongo[collection].find(rid).first
end
def finished_at