From ea3829ca583ea706cd96a9544e22949bd751158b Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Sat, 9 Jan 2010 18:22:47 +0100 Subject: tasks added --- Rakefile | 10 ++--- lib/algorithm.rb | 2 - lib/dataset.rb | 40 ------------------- lib/model.rb | 108 ++++++++------------------------------------------- lib/task.rb | 37 +++++++++--------- lib/tasks/opentox.rb | 20 ---------- 6 files changed, 39 insertions(+), 178 deletions(-) diff --git a/Rakefile b/Rakefile index c5cb026..5974a9c 100644 --- a/Rakefile +++ b/Rakefile @@ -10,13 +10,9 @@ begin gem.email = "helma@in-silico.ch" gem.homepage = "http://github.com/helma/opentox-ruby-api-wrapper" gem.authors = ["Christoph Helma"] - gem.add_dependency "rest-client" - gem.add_dependency "sinatra" - gem.add_dependency "rack" - gem.add_dependency "rack-contrib" - gem.add_dependency "thin" - gem.add_dependency "emk-sinatra-url-for" - gem.add_dependency "cehoffman-sinatra-respond_to" + ["sinatra", "rest-client", "rack", "rack-contrib", "thin", "emk-sinatra-url-for", "cehoffman-sinatra-respond_to", "dm-more", "dm-core"].each do |dep| + gem.add_dependency dep + end gem.add_development_dependency "cucumber" gem.files = FileList["[A-Z]*", "{bin,generators,lib,test}/**/*", 'lib/jeweler/templates/.gitignore'] gem.files.include %w(lib/tasks/owl.rb, lib/environment.rb, lib/algorithm.rb, lib/compound.rb, lib/dataset.rb, lib/model.rb, lib/utils.rb, lib/templates/*) diff --git a/lib/algorithm.rb b/lib/algorithm.rb index 9918110..c5d162a 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -45,7 +45,6 @@ module OpenTox end class Similarity - def self.weighted_tanimoto(fp_a,fp_b,p) common_features = fp_a & fp_b all_features = fp_a + fp_b @@ -59,7 +58,6 @@ module OpenTox 0.0 end end - end end diff --git a/lib/dataset.rb b/lib/dataset.rb index ad69038..8d1c0c1 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -158,46 +158,6 @@ module OpenTox features end -=begin - def tuples - tuples = [] - @model.subjects(RDF['type'], OT["Tuple"]).each do |t| - tuple = {} - compounds = [] - @model.subjects(OT['values'], t).each do |data_entry| - compound_node = @model.object(data_entry,OT['compound']) - compounds << @model.object(compound_node, DC["identifier"]).to_s - end - @model.find(t, OT['tuple'],nil) do |s,p,pair| - feature_node = @model.object(pair, OT['feature']) - feature_name = @model.object(feature_node, DC['title']).to_s - value_node = @model.object(pair, OT['value']) - value = @model.object(value_node, OT['value']).to_s - value = value.to_f if value.match(/^[\d\.]+$/) - tuple[feature_name.to_sym] = value - end - tuple[:compounds] = compounds - tuples << tuple - end - tuples - end - - def tuple(compound_uri) - compound_node = @model.subject(DC["identifier"],compound_uri) - #puts compound_uri - @model.subjects(OT['compound'], compound_node).each do |data_entry| - values_node = @model.object(data_entry, OT['values']) - @model.find(values_node, OT['tuple'], nil) do |s,p,tuple| - @model.find(tuple, OT['feature'], nil) do |s,p,feature| - name = @model.object(feature,DC['title']).to_s - #puts name - end - end - #puts values_node - end - end -=end - def compounds compounds = [] @model.subjects(RDF['type'], OT["Compound"]).each do |compound_node| diff --git a/lib/model.rb b/lib/model.rb index cd35f09..24f6e52 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -4,40 +4,28 @@ module OpenTox class Lazar include Owl - attr_accessor :dataset, :predictions - # Create a new prediction model from a dataset - def initialize(yaml) - super() - id = File.basename(yaml,'.yaml') - # TODO Untyped Individual: http://localhost:4003/lazar/{id} ???? - @lazar = YAML.load_file yaml - self.uri = File.join(@@config[:services]["opentox-model"],'lazar',id) - self.title = "lazar model for #{@lazar[:endpoint]}" + def initialize + super self.source = "http://github.com/helma/opentox-model" - self.parameters = { - "Dataset URI" => { :scope => "mandatory", :value => "dataset_uri=#{@lazar[:activity_dataset]}" }, - "Feature URI for dependent variable" => { :scope => "mandatory", :value => "feature_uri=#{@lazar[:endpoint]}" }, - "Feature generation URI" => { :scope => "mandatory", :value => "feature_generation_uri=" } #TODO write to yaml - } self.algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar") - self.trainingDataset = @lazar[:activity_dataset] - self.dependentVariables = @lazar[:endpoint] - self.independentVariables = "http://localhost:4002/fminer#BBRC_representative" # TODO read this from dataset - self.predictedVariables = @lazar[:endpoint] #+ " lazar prediction" - @dataset = OpenTox::Dataset.new - @predictions = {} + self.independentVariables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative") # TODO read this from dataset end - def self.find(uri) -=begin - begin - YAML.load(RestClient.get uri) - Lazar.new uri - rescue - halt 404, "Model #{uri} not found." - end -=end + def self.from_yaml(yaml) + yaml = YAML.load yaml + lazar = Lazar.new + lazar.title = "lazar model for #{yaml[:endpoint]}" + lazar.parameters = { + "Dataset URI" => { :scope => "mandatory", :value => "dataset_uri=#{yaml[:activity_dataset]}" }, + "Feature URI for dependent variable" => { :scope => "mandatory", :value => "feature_uri=#{yaml[:endpoint]}" }, + "Feature generation URI" => { :scope => "mandatory", :value => "feature_generation_uri=#{File.join(@@config[:services]["opentox-algorithm"],"fminer")}"} #TODO write to yaml + } + lazar.algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar") + lazar.trainingDataset = yaml[:activity_dataset] + lazar.dependentVariables = yaml[:endpoint] + lazar.predictedVariables = yaml[:endpoint] #+ " lazar prediction" + lazar end def self.find_all @@ -49,68 +37,6 @@ module OpenTox RestClient.post(@uri, :compound_uri => compound.uri) end - def database_activity?(compound_uri) - # find database activities - db_activities = @lazar[:activities][compound_uri] - if db_activities - c = @dataset.find_or_create_compound(compound_uri) - f = @dataset.find_or_create_feature(@lazar[:endpoint]) - v = db_activities.join(',') - @dataset.add c,f,v - @predictions[compound_uri] = { @lazar[:endpoint] => {:measured_activities => db_activities}} - true - else - false - end - end - - def classify(compound_uri) - - compound = OpenTox::Compound.new(:uri => compound_uri) - compound_matches = compound.match @lazar[:features] - - conf = 0.0 - neighbors = [] - classification = nil - - @lazar[:fingerprints].each do |uri,matches| - - sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(compound_matches,matches,@lazar[:p_values]) - if sim > 0.3 - neighbors << uri - @lazar[:activities][uri].each do |act| - case act.to_s - when 'true' - conf += OpenTox::Utils.gauss(sim) - when 'false' - conf -= OpenTox::Utils.gauss(sim) - end - end - end - end - - conf = conf/neighbors.size - if conf > 0.0 - classification = true - elsif conf < 0.0 - classification = false - end - - compound = @dataset.find_or_create_compound(compound_uri) - feature = @dataset.find_or_create_feature(@lazar[:endpoint]) - - if (classification != nil) - tuple = @dataset.create_tuple(feature,{ 'lazar#classification' => classification, 'lazar#confidence' => conf}) - @dataset.add_tuple compound,tuple - @predictions[compound_uri] = { @lazar[:endpoint] => { :lazar_prediction => { - :classification => classification, - :confidence => conf, - :neighbors => neighbors, - :features => compound_matches - } } } - end - end - def self.base_uri @@config[:services]["opentox-model"] end diff --git a/lib/task.rb b/lib/task.rb index 079d387..8dbd578 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -1,20 +1,21 @@ module OpenTox - class Task #< OpenTox + class Task + + attr_accessor :uri def initialize(uri) - super(uri) + #super() + @uri = uri end - #def self.create(uri) def self.create - puts @@config[:services]["opentox-task"] - uri = RestClient.post @@config[:services]["opentox-task"], ''#, :dataset_uri => uri + uri = RestClient.post @@config[:services]["opentox-task"], nil Task.new(uri) end - def self.find(params) - Task.new(params[:uri]) + def self.find(uri) + Task.new(uri) end def self.base_uri @@ -22,32 +23,32 @@ module OpenTox end def start - RestClient.put @uri, :status => 'started' + RestClient.put File.join(@uri,'started'), nil end - def stop - RestClient.put @uri, :status => 'stopped' + def cancel + RestClient.put File.join(@uri,'cancelled'), nil end - def completed - RestClient.put @uri, :status => 'completed' + def completed(uri) + RestClient.put File.join(@uri,'completed'), :resource => uri end def status RestClient.get File.join(@uri, 'status') end - - def completed? - self.status == 'completed' + + def resource + RestClient.get File.join(@uri, 'resource') end - def resource - RestClient.get @uri + def completed? + self.status.to_s == 'completed' end def wait_for_completion until self.completed? - sleep 1 + sleep 0.1 end end diff --git a/lib/tasks/opentox.rb b/lib/tasks/opentox.rb index 38202dd..e330c87 100644 --- a/lib/tasks/opentox.rb +++ b/lib/tasks/opentox.rb @@ -1,24 +1,4 @@ require File.join(File.dirname(__FILE__), '..', 'opentox-ruby-api-wrapper.rb') -require File.join(File.dirname(__FILE__), 'redis.rb') - -namespace :redis do - - desc "Flush Redis testing database" - task :flush do - require 'redis' - r = Redis.new :db => 2 - r.flush_db - end - - desc "Flush all Redis databases" - task :flushall do - require 'redis' - r = Redis.new - r.flush_all - end - - -end namespace :opentox do -- cgit v1.2.3