From 7cbb2402a4d01abcbfd3850650f9687b46804723 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 24 Aug 2010 17:52:33 +0200 Subject: initial lazar version for quantitative descriptors --- lib/algorithm.rb | 12 ++++ lib/model.rb | 162 ++++++++++++++++++++++++++++++++---------------------- lib/validation.rb | 11 +--- 3 files changed, 109 insertions(+), 76 deletions(-) (limited to 'lib') diff --git a/lib/algorithm.rb b/lib/algorithm.rb index 2fde0eb..d7b57af 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -64,6 +64,18 @@ module OpenTox 0.0 end end + def self.euclidean(prop_a,prop_b) + common_properties = prop_a.keys & prop_b.keys + if common_properties.size > 1 + dist_sum = 0 + common_properties.each do |p| + dist_sum += (prop_a[p] - prop_b[p])**2 + end + 1/(1+Math.sqrt(dist_sum)) + else + nil + end + end end end diff --git a/lib/model.rb b/lib/model.rb index e8f6048..1cc0825 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -1,13 +1,13 @@ module OpenTox - module Model + module Model - class Generic + class Generic MODEL_ATTRIBS = [:uri, :title, :creator, :date, :format, :predictedVariables, :independentVariables, :dependentVariables, :trainingDataset, :algorithm] MODEL_ATTRIBS.each{ |a| attr_accessor(a) } - def self.find(uri) - owl = OpenTox::Owl.from_uri(uri, "Model") + def self.find(uri) + owl = OpenTox::Owl.from_uri(uri, "Model") return self.new(owl) end @@ -36,78 +36,108 @@ module OpenTox RestClientWrapper.raise_uri_error "invalid model: '"+ex.message+"'\n"+self.to_yaml+"\n",@uri.to_s end end - end - end + end + end - class PredictionModel < Generic + class PredictionModel < Generic + + def self.build( algorithm_uri, algorithm_params ) + + LOGGER.debug "Build model, algorithm_uri:"+algorithm_uri.to_s+", algorithm_parms: "+algorithm_params.inspect.to_s + uri = OpenTox::RestClientWrapper.post(algorithm_uri,algorithm_params).to_s + LOGGER.debug "Build model done: "+uri.to_s + RestClientWrapper.raise_uri_error("Invalid build model result: '"+uri.to_s+"'", algorithm_uri, algorithm_params ) unless Utils.model_uri?(uri) + return PredictionModel.find(uri) + end - def self.build( algorithm_uri, algorithm_params ) - - LOGGER.debug "Build model, algorithm_uri:"+algorithm_uri.to_s+", algorithm_parms: "+algorithm_params.inspect.to_s - uri = OpenTox::RestClientWrapper.post(algorithm_uri,algorithm_params).to_s - LOGGER.debug "Build model done: "+uri.to_s - RestClientWrapper.raise_uri_error("Invalid build model result: '"+uri.to_s+"'", algorithm_uri, algorithm_params ) unless Utils.model_uri?(uri) - return PredictionModel.find(uri) - end - - def predict_dataset( dataset_uri ) - - LOGGER.debug "Predict dataset: "+dataset_uri.to_s+" with model "+@uri.to_s - uri = RestClientWrapper.post(@uri, {:accept => "text/uri-list", :dataset_uri=>dataset_uri}) - RestClientWrapper.raise_uri_error("Prediciton result no dataset uri: "+uri.to_s, @uri, {:dataset_uri=>dataset_uri} ) unless Utils.dataset_uri?(uri) - uri - end - - def classification? - #HACK replace with request to ontology server - if @title =~ /(?i)classification/ - return true - elsif @title =~ /(?i)regression/ - return false - elsif @uri =~/ntua/ and @title =~ /mlr/ - return false - elsif @uri =~/tu-muenchen/ and @title =~ /regression|M5P|GaussP/ - return false - elsif @uri =~/ambit2/ and @title =~ /pKa/ || @title =~ /Regression|Caco/ - return false - elsif @uri =~/majority/ - return (@uri =~ /class/) != nil - else - raise "unknown model, uri:'"+@uri.to_s+"' title:'"+@title.to_s+"'" - end - end - end - + def predict_dataset( dataset_uri ) + + LOGGER.debug "Predict dataset: "+dataset_uri.to_s+" with model "+@uri.to_s + uri = RestClientWrapper.post(@uri, {:accept => "text/uri-list", :dataset_uri=>dataset_uri}) + RestClientWrapper.raise_uri_error("Prediciton result no dataset uri: "+uri.to_s, @uri, {:dataset_uri=>dataset_uri} ) unless Utils.dataset_uri?(uri) + uri + end + + def classification? + #HACK replace with request to ontology server + if @title =~ /(?i)classification/ + return true + elsif @title =~ /(?i)regression/ + return false + elsif @uri =~/ntua/ and @title =~ /mlr/ + return false + elsif @uri =~/tu-muenchen/ and @title =~ /regression|M5P|GaussP/ + return false + elsif @uri =~/ambit2/ and @title =~ /pKa/ || @title =~ /Regression|Caco/ + return false + elsif @uri =~/majority/ + return (@uri =~ /class/) != nil + else + raise "unknown model, uri:'"+@uri.to_s+"' title:'"+@title.to_s+"'" + end + end + end - class Lazar < Generic + class Lazar < Generic attr_accessor :feature_dataset_uri, :effects, :activities, :p_values, :fingerprints, :features - def initialize - @source = "http://github.com/helma/opentox-model" - @algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar") - #@independent_variables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative") - @features = [] - @effects = {} - @activities = {} - @p_values = {} - @fingerprints = {} - end + def initialize + @source = "http://github.com/helma/opentox-model" + @algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar") + #@independent_variables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative") + @features = [] + @effects = {} + @activities = {} + @p_values = {} + @fingerprints = {} + end + + def save + @features.uniq! + resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) + resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s + end + + def self.find_all + RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n") + end + + def self.predict(compound_uri,model_uri) + #RestClientWrapper.post(model_uri,{:compound_uri => compound_uri, :accept => 'application/x-yaml'}) + `curl -X POST -d 'compound_uri=#{compound_uri}' -H 'Accept:application/x-yaml' #{model_uri}` + end + end + + class PropertyLazar < Generic + + attr_accessor :feature_dataset_uri, :properties, :features, :activities#, :effects, :p_values + + def initialize + @source = "http://github.com/helma/opentox-model" + @algorithm = File.join(@@config[:services]["opentox-algorithm"],"property_lazar") + #@independent_variables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative") + @features = [] + #@effects = {} + @activities = {} + #@p_values = {} + @properties = {} + end - def save - @features.uniq! - resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s - end + def save + @features.uniq! + resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) + resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s + end - def self.find_all - RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n") - end + def self.find_all + RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n") + end def self.predict(compound_uri,model_uri) #RestClientWrapper.post(model_uri,{:compound_uri => compound_uri, :accept => 'application/x-yaml'}) - `curl -X POST -d 'compound_uri=#{compound_uri}' -H 'Accept:application/x-yaml' #{model_uri}` + `curl -X POST -d 'compound_uri=#{compound_uri}' -H 'Accept:application/x-yaml' #{model_uri}` end - end - end + end + end end diff --git a/lib/validation.rb b/lib/validation.rb index bd38488..89a2a0c 100644 --- a/lib/validation.rb +++ b/lib/validation.rb @@ -4,16 +4,7 @@ module OpenTox attr_accessor :uri def initialize(params) - #resource = RestClient::Resource.new(params[:uri], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - #@uri = resource.post(params).body - #LOGGER.debug "VALIDATION URI: " + @uri.to_s - call = "curl -X POST " - params.each do |k,v| - call += " -d "+k.to_s+"=\""+URI.encode(v.to_s)+"\"" unless k == :uri - end - call += " "+params[:uri] - LOGGER.debug call - @uri = `#{call}` + @uri = OpenTox::RestClientWrapper.post(File.join(@@config[:services]["opentox-validation"],"/crossvalidation"),params,nil,false) end def self.crossvalidation(params) -- cgit v1.2.3