From 98da72f307ca97658cd342b1d0f06727ebad555c Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Mon, 1 Mar 2010 10:21:44 +0100 Subject: wrapper version bumped to 1.3.0 --- application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application.rb b/application.rb index 74929dc..3f60159 100644 --- a/application.rb +++ b/application.rb @@ -1,5 +1,5 @@ require 'rubygems' -gem 'opentox-ruby-api-wrapper', '= 1.2.7' +gem 'opentox-ruby-api-wrapper', '= 1.3.0' require 'opentox-ruby-api-wrapper' class Model -- cgit v1.2.3 From 4501570b7daa83a17cab7ba43446f2b6209fe955 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 2 Mar 2010 11:18:42 +0100 Subject: version bumped to 1.3.1 --- application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application.rb b/application.rb index 3f60159..77b4443 100644 --- a/application.rb +++ b/application.rb @@ -1,5 +1,5 @@ require 'rubygems' -gem 'opentox-ruby-api-wrapper', '= 1.3.0' +gem 'opentox-ruby-api-wrapper', '= 1.3.1' require 'opentox-ruby-api-wrapper' class Model -- cgit v1.2.3 From bc15ed2e36fb2ce7434e0c352d942f211178dea4 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 10 Mar 2010 12:00:49 +0100 Subject: newline added to end of uri-lists --- application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application.rb b/application.rb index 77b4443..641c28d 100644 --- a/application.rb +++ b/application.rb @@ -17,7 +17,7 @@ require 'lazar.rb' get '/?' do # get index of models response['Content-Type'] = 'text/uri-list' - Model.all.collect{|m| m.uri}.join("\n") + Model.all.collect{|m| m.uri}.join("\n") + "\n" end get '/:id/?' do -- cgit v1.2.3 From ad23a652417a2ff22e03af7c7be21c1055c4e4ef Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 10 Mar 2010 19:17:47 +0100 Subject: owl-dl (temporarily) removed, switched to YAML representation --- application.rb | 1 + lazar.rb | 71 +++++++++++++++++++++------------------------------------- 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/application.rb b/application.rb index 641c28d..9c50383 100644 --- a/application.rb +++ b/application.rb @@ -1,6 +1,7 @@ require 'rubygems' gem 'opentox-ruby-api-wrapper', '= 1.3.1' require 'opentox-ruby-api-wrapper' +LOGGER.progname = File.expand_path(__FILE__) class Model include DataMapper::Resource diff --git a/lazar.rb b/lazar.rb index 9014d3b..8966527 100644 --- a/lazar.rb +++ b/lazar.rb @@ -4,24 +4,25 @@ class Lazar < Model def classify(compound_uri) - unless @dataset - @dataset = OpenTox::Dataset.new + #unless @dataset + #@dataset = OpenTox::Dataset.new @predictions = {} - end - lazar = YAML.load yaml + #end + lazar = YAML.load self.yaml compound = OpenTox::Compound.new(:uri => compound_uri) - compound_matches = compound.match lazar[:features] + compound_matches = compound.match lazar.features conf = 0.0 - neighbors = [] + similarities = {} classification = nil - lazar[:fingerprints].each do |uri,matches| + 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| + sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(compound_matches,matches,lazar.p_values) + LOGGER.debug sim + if sim > 0#.3 + similarities[uri] = sim + lazar.activities[uri].each do |act| case act.to_s when 'true' conf += OpenTox::Utils.gauss(sim) @@ -32,23 +33,24 @@ class Lazar < Model end end - conf = conf/neighbors.size + conf = conf/similarities.size if conf > 0.0 classification = true elsif conf < 0.0 classification = false end + LOGGER.debug conf - compound = @dataset.find_or_create_compound(compound_uri) - feature = @dataset.find_or_create_feature(lazar[:endpoint]) + #compound = @dataset.compounds[compound_uri] + #feature = @dataset.features[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 => { + #tuple = @dataset.create_tuple(feature,{ 'lazar#classification' => classification, 'lazar#confidence' => conf}) + #@dataset.add_tuple compound,tuple + @predictions[compound_uri] = { lazar.dependent_variable => { :lazar_prediction => { :classification => classification, :confidence => conf, - :neighbors => neighbors, + :similarities => similarities, :features => compound_matches } } } end @@ -58,15 +60,9 @@ class Lazar < Model def database_activity?(compound_uri) # find database activities lazar = YAML.load self.yaml - db_activities = lazar[:activities][compound_uri] + db_activities = lazar.activities[compound_uri] if db_activities - @dataset = OpenTox::Dataset.new - @predictions = {} - 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}} + @predictions = {:compound_uri => { lazar.dependent_variable => {:measured_activities => db_activities}}} true else false @@ -76,27 +72,12 @@ class Lazar < Model end post '/?' do # create model - #model = Lazar.new(:task_uri => params[:task_uri]) - #model.uri = url_for("/#{model.id}", :full) + halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) model = Lazar.new model.save model.uri = url_for("/#{model.id}", :full) -# model.uri -#end -# -#put '/:id/?' do # create model from yaml representation -# model = Lazar.first(params[:id]) - case request.content_type - when /yaml/ - input = request.env["rack.input"].read - model.yaml = input - lazar = OpenTox::Model::Lazar.from_yaml(input) - lazar.uri = model.uri - model.owl = lazar.rdf - model.save - else - halt 400, "MIME type \"#{request.content_type}\" not supported." - end + model.yaml = request.env["rack.input"].read + model.save model.uri end @@ -107,7 +88,7 @@ post '/:id/?' do # create prediction halt 404, "No compound_uri or dataset_uri parameter." unless compound_uri = params[:compound_uri] or dataset_uri = params[:dataset_uri] if compound_uri - lazar.classify(compound_uri) unless lazar.database_activity?(compound_uri) # FEHLER + lazar.classify(compound_uri) unless lazar.database_activity?(compound_uri) elsif dataset_uri input_dataset = OpenTox::Dataset.find(dataset_uri) input_dataset.compounds.each do |compound_uri| -- cgit v1.2.3 From a5144cd21c5c6537d2e95e608b970e750a421986 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 16 Mar 2010 11:05:40 +0100 Subject: toxcreate adapted for new representation --- application.rb | 2 +- lazar.rb | 87 +++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 53 insertions(+), 36 deletions(-) diff --git a/application.rb b/application.rb index 9c50383..b43b11f 100644 --- a/application.rb +++ b/application.rb @@ -7,7 +7,7 @@ class Model include DataMapper::Resource property :id, Serial property :uri, String, :length => 255 - property :owl, Text, :length => 2**32-1 + #property :owl, Text, :length => 2**32-1 property :yaml, Text, :length => 2**32-1 property :created_at, DateTime end diff --git a/lazar.rb b/lazar.rb index 8966527..ae32734 100644 --- a/lazar.rb +++ b/lazar.rb @@ -1,13 +1,11 @@ class Lazar < Model - attr_accessor :dataset, :predictions - - def classify(compound_uri) - - #unless @dataset - #@dataset = OpenTox::Dataset.new - @predictions = {} - #end + attr_accessor :prediction_dataset + + def classify(compound_uri,prediction) + + prediction.title += " lazar classification" + lazar = YAML.load self.yaml compound = OpenTox::Compound.new(:uri => compound_uri) compound_matches = compound.match lazar.features @@ -19,8 +17,7 @@ class Lazar < Model lazar.fingerprints.each do |uri,matches| sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(compound_matches,matches,lazar.p_values) - LOGGER.debug sim - if sim > 0#.3 + if sim > 0.3 similarities[uri] = sim lazar.activities[uri].each do |act| case act.to_s @@ -39,30 +36,36 @@ class Lazar < Model elsif conf < 0.0 classification = false end - LOGGER.debug conf - - #compound = @dataset.compounds[compound_uri] - #feature = @dataset.features[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.dependent_variable => { :lazar_prediction => { + feature_uri = lazar.dependent_variable + "_lazar_prediction" + prediction.compounds << compound_uri + prediction.features << feature_uri + prediction.data[compound_uri] = [] unless prediction.data[compound_uri] + tuple = { :classification => classification, :confidence => conf, :similarities => similarities, :features => compound_matches - } } } + } + prediction.data[compound_uri] << {feature_uri => tuple} end end - def database_activity?(compound_uri) + def database_activity?(compound_uri,prediction) # find database activities lazar = YAML.load self.yaml db_activities = lazar.activities[compound_uri] if db_activities - @predictions = {:compound_uri => { lazar.dependent_variable => {:measured_activities => db_activities}}} + prediction.source = lazar.activity_dataset_uri + feature_uri = lazar.dependent_variable + prediction.compounds << compound_uri + prediction.features << feature_uri + prediction.data[compound_uri] = [] unless prediction.data[compound_uri] + db_activities.each do |act| + prediction.data[compound_uri] << {feature_uri => act} + end true else false @@ -87,24 +90,38 @@ post '/:id/?' do # create prediction halt 404, "Model #{params[:id]} does not exist." unless lazar halt 404, "No compound_uri or dataset_uri parameter." unless compound_uri = params[:compound_uri] or dataset_uri = params[:dataset_uri] + prediction = OpenTox::Dataset.new + prediction.source = lazar.uri + prediction.title = URI.decode YAML.load(lazar.yaml).dependent_variable.split(/#/).last + if compound_uri - lazar.classify(compound_uri) unless lazar.database_activity?(compound_uri) - elsif dataset_uri - input_dataset = OpenTox::Dataset.find(dataset_uri) - input_dataset.compounds.each do |compound_uri| - lazar.classify(compound_uri) unless lazar.database_activity?(compound_uri) + lazar.classify(compound_uri,prediction) unless lazar.database_activity?(compound_uri,prediction) + LOGGER.debug prediction.to_yaml + case request.env['HTTP_ACCEPT'] + when /yaml/ + prediction.to_yaml + when 'application/rdf+xml' + prediction.to_owl + else + halt 404, "Content type #{request.env['HTTP_ACCEPT']} not available." end - end - - case request.env['HTTP_ACCEPT'] - when /yaml/ - lazar.predictions.to_yaml - else - if params[:compound_uri] - lazar.dataset.rdf - elsif params[:dataset_uri] - lazar.dataset.save + elsif dataset_uri + task = OpenTox::Task.create + pid = Spork.spork(:logger => LOGGER) do + task.started + task = OpenTox::Task.create + input_dataset = OpenTox::Dataset.find(dataset_uri) + input_dataset.compounds.each do |compound_uri| + lazar.classify(compound_uri) unless lazar.database_activity?(compound_uri) + end + uri = lazar.prediction_dataset.save + task.completed(uri) end + task.pid = pid + LOGGER.debug "Task PID: " + pid.to_s + #status 303 + response['Content-Type'] = 'text/uri-list' + task.uri + "\n" end end -- cgit v1.2.3 From 506952dff164c3535ece28859778260f69d748fa Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 16 Mar 2010 17:35:34 +0100 Subject: OWL-DL export added --- application.rb | 17 ----------------- lazar.rb | 29 ++++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/application.rb b/application.rb index b43b11f..14c4aa6 100644 --- a/application.rb +++ b/application.rb @@ -21,23 +21,6 @@ get '/?' do # get index of models Model.all.collect{|m| m.uri}.join("\n") + "\n" end -get '/:id/?' do - model = Model.get(params[:id]) - halt 404, "Model #{uri} not found." unless model - accept = request.env['HTTP_ACCEPT'] - accept = "application/rdf+xml" if accept == '*/*' or accept =~ /html/ or accept == '' or accept.nil? - case accept - when "application/rdf+xml" - response['Content-Type'] = 'application/rdf+xml' - model.owl - when /yaml/ - response['Content-Type'] = 'application/x-yaml' - model.yaml - else - halt 400, "Unsupported MIME type '#{accept}'" - end -end - delete '/:id/?' do begin Model.get(params[:id]).destroy! diff --git a/lazar.rb b/lazar.rb index ae32734..51a6c20 100644 --- a/lazar.rb +++ b/lazar.rb @@ -38,7 +38,7 @@ class Lazar < Model end if (classification != nil) - feature_uri = lazar.dependent_variable + "_lazar_prediction" + feature_uri = lazar.dependent_variable + "_lazar_classification" prediction.compounds << compound_uri prediction.features << feature_uri prediction.data[compound_uri] = [] unless prediction.data[compound_uri] @@ -72,6 +72,33 @@ class Lazar < Model end end + def to_owl + data = YAML.load(yaml) + owl = OpenTox::Owl.new 'Model', uri + owl.source = "http://github.com/helma/opentox-model" + #owl.algorithm = data.algorithm + owl.dependentVariable = data.activity_dataset_uri + owl.independentVariables = data.feature_dataset_uri + owl.rdf + end + +end + +get '/:id/?' do + model = Lazar.get(params[:id]) + halt 404, "Model #{uri} not found." unless model + accept = request.env['HTTP_ACCEPT'] + accept = "application/rdf+xml" if accept == '*/*' or accept =~ /html/ or accept == '' or accept.nil? + case accept + when "application/rdf+xml" + response['Content-Type'] = 'application/rdf+xml' + model.to_owl + when /yaml/ + response['Content-Type'] = 'application/x-yaml' + model.yaml + else + halt 400, "Unsupported MIME type '#{accept}'" + end end post '/?' do # create model -- cgit v1.2.3 From 00845f15fa41b921fb7d16100a74553aaf61d62d Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 16 Mar 2010 19:42:55 +0100 Subject: initial test for dataset prediction added --- lazar.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lazar.rb b/lazar.rb index 51a6c20..bef367a 100644 --- a/lazar.rb +++ b/lazar.rb @@ -132,20 +132,20 @@ post '/:id/?' do # create prediction else halt 404, "Content type #{request.env['HTTP_ACCEPT']} not available." end + elsif dataset_uri task = OpenTox::Task.create pid = Spork.spork(:logger => LOGGER) do task.started - task = OpenTox::Task.create input_dataset = OpenTox::Dataset.find(dataset_uri) input_dataset.compounds.each do |compound_uri| - lazar.classify(compound_uri) unless lazar.database_activity?(compound_uri) + lazar.classify(compound_uri,prediction) unless lazar.database_activity?(compound_uri,prediction) end - uri = lazar.prediction_dataset.save + uri = prediction.save.chomp task.completed(uri) end task.pid = pid - LOGGER.debug "Task PID: " + pid.to_s + LOGGER.debug "Prediction task PID: " + pid.to_s #status 303 response['Content-Type'] = 'text/uri-list' task.uri + "\n" -- cgit v1.2.3 From ec441343dd0482a94978ea3e10d5056d749bceba Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 18 Mar 2010 20:54:25 +0100 Subject: more detailed model information in toxcreate --- application.rb | 2 +- lazar.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/application.rb b/application.rb index 14c4aa6..75f2271 100644 --- a/application.rb +++ b/application.rb @@ -7,7 +7,7 @@ class Model include DataMapper::Resource property :id, Serial property :uri, String, :length => 255 - #property :owl, Text, :length => 2**32-1 + property :owl, Text, :length => 2**32-1 property :yaml, Text, :length => 2**32-1 property :created_at, DateTime end diff --git a/lazar.rb b/lazar.rb index bef367a..8c8c018 100644 --- a/lazar.rb +++ b/lazar.rb @@ -74,25 +74,52 @@ class Lazar < Model def to_owl data = YAML.load(yaml) + activity_dataset = YAML.load(RestClient.get(data.activity_dataset_uri, :accept => 'application/x-yaml').to_s) + feature_dataset = YAML.load(RestClient.get(data.feature_dataset_uri, :accept => 'application/x-yaml').to_s) owl = OpenTox::Owl.new 'Model', uri owl.source = "http://github.com/helma/opentox-model" - #owl.algorithm = data.algorithm - owl.dependentVariable = data.activity_dataset_uri - owl.independentVariables = data.feature_dataset_uri + owl.title = "#{URI.decode(activity_dataset.title)} lazar classification" + owl.date = created_at.to_s + owl.algorithm = data.algorithm + owl.dependentVariables = activity_dataset.features.join(', ') + owl.independentVariables = feature_dataset.features.join(', ') + owl.predictedVariables = activity_dataset.features.join(', ') + "_lazar_classification" + owl.parameters = { + "Dataset URI" => + { :scope => "mandatory", :value => data.activity_dataset_uri }, + "Feature URI for dependent variable" => + { :scope => "mandatory", :value => activity_dataset.features.join(', ')}, + "Feature generation URI" => + { :scope => "mandatory", :value => feature_dataset.source } + } + owl.trainingDataset = data.activity_dataset_uri owl.rdf end end get '/:id/?' do - model = Lazar.get(params[:id]) - halt 404, "Model #{uri} not found." unless model accept = request.env['HTTP_ACCEPT'] - accept = "application/rdf+xml" if accept == '*/*' or accept =~ /html/ or accept == '' or accept.nil? + accept = "application/rdf+xml" if accept == '*/*' or accept == '' or accept.nil? + # workaround for browser links + case params[:id] + when /.yaml$/ + params[:id].sub!(/.yaml$/,'') + accept = 'application/x-yaml' + when /.rdf$/ + params[:id].sub!(/.rdf$/,'') + accept = 'application/rdf+xml' + end + model = Lazar.get(params[:id]) + halt 404, "Model #{params[:id]} not found." unless model case accept when "application/rdf+xml" response['Content-Type'] = 'application/rdf+xml' - model.to_owl + unless model.owl # lazy owl creation + model.owl = model.to_owl + model.save + end + model.owl when /yaml/ response['Content-Type'] = 'application/x-yaml' model.yaml @@ -101,6 +128,21 @@ get '/:id/?' do end end +get '/:id/algorithm/?' do + response['Content-Type'] = 'text/plain' + YAML.load(Lazar.get(params[:id]).yaml).algorithm +end + +get '/:id/training_dataset/?' do + response['Content-Type'] = 'text/plain' + YAML.load(Lazar.get(params[:id]).yaml).activity_dataset_uri +end + +get '/:id/feature_dataset/?' do + response['Content-Type'] = 'text/plain' + YAML.load(Lazar.get(params[:id]).yaml).feature_dataset_uri +end + post '/?' do # create model halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) model = Lazar.new -- cgit v1.2.3 From a3bfe1c366951828ccce6823a68b7f379979a8bf Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 18 Mar 2010 21:03:38 +0100 Subject: opentox-api-wrapper version bumped to 1.4.0 --- application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application.rb b/application.rb index 75f2271..2552c52 100644 --- a/application.rb +++ b/application.rb @@ -1,5 +1,5 @@ require 'rubygems' -gem 'opentox-ruby-api-wrapper', '= 1.3.1' +gem 'opentox-ruby-api-wrapper', '= 1.4.0' require 'opentox-ruby-api-wrapper' LOGGER.progname = File.expand_path(__FILE__) -- cgit v1.2.3 From 3c847e5555ac0af41e62f11e6c92d915d8618794 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 19 Mar 2010 16:24:42 +0100 Subject: owl parsing implemented --- lazar.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lazar.rb b/lazar.rb index 8c8c018..b07201d 100644 --- a/lazar.rb +++ b/lazar.rb @@ -38,7 +38,7 @@ class Lazar < Model end if (classification != nil) - feature_uri = lazar.dependent_variable + "_lazar_classification" + feature_uri = lazar.dependent_variables + "_lazar_classification" prediction.compounds << compound_uri prediction.features << feature_uri prediction.data[compound_uri] = [] unless prediction.data[compound_uri] @@ -59,7 +59,7 @@ class Lazar < Model db_activities = lazar.activities[compound_uri] if db_activities prediction.source = lazar.activity_dataset_uri - feature_uri = lazar.dependent_variable + feature_uri = lazar.dependent_variables prediction.compounds << compound_uri prediction.features << feature_uri prediction.data[compound_uri] = [] unless prediction.data[compound_uri] @@ -76,7 +76,7 @@ class Lazar < Model data = YAML.load(yaml) activity_dataset = YAML.load(RestClient.get(data.activity_dataset_uri, :accept => 'application/x-yaml').to_s) feature_dataset = YAML.load(RestClient.get(data.feature_dataset_uri, :accept => 'application/x-yaml').to_s) - owl = OpenTox::Owl.new 'Model', uri + owl = OpenTox::Owl.create 'Model', uri owl.source = "http://github.com/helma/opentox-model" owl.title = "#{URI.decode(activity_dataset.title)} lazar classification" owl.date = created_at.to_s @@ -161,7 +161,7 @@ post '/:id/?' do # create prediction prediction = OpenTox::Dataset.new prediction.source = lazar.uri - prediction.title = URI.decode YAML.load(lazar.yaml).dependent_variable.split(/#/).last + prediction.title = URI.decode YAML.load(lazar.yaml).dependent_variables.split(/#/).last if compound_uri lazar.classify(compound_uri,prediction) unless lazar.database_activity?(compound_uri,prediction) -- cgit v1.2.3