From 763cdff161108afdcd19e31ed0dbfe6dc2a5e078 Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 20 Jul 2010 15:26:07 +0200 Subject: pre merge with helma/master --- application.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/application.rb b/application.rb index 304f8c0..6229178 100644 --- a/application.rb +++ b/application.rb @@ -8,7 +8,8 @@ class Model property :id, Serial property :uri, String, :length => 255 property :owl, Text, :length => 2**32-1 - property :yaml, Text, :length => 2**32-1 + property :yaml, Text, :length => 2**32-1 + property :token_id, String, :length => 255 property :created_at, DateTime end @@ -23,8 +24,17 @@ end delete '/:id/?' do begin - Model.get(params[:id]).destroy! + model = Model.get(params[:id]) + uri = model.uri + model.destroy! "Model #{params[:id]} deleted." + if params["token_id"] and !Model.get(params[:id]) and uri + begin + aa = OpenTox::Authorization.delete_policy_from_uri(uri, params["token_id"]) + rescue + LOGGER.warn "Policy delete error for Model URI: #{uri}" + end + end rescue halt 404, "Model #{params[:id]} does not exist." end -- cgit v1.2.3 From 91acc6de3edf5c36b2f29c3c5659d4748302457c Mon Sep 17 00:00:00 2001 From: mr Date: Fri, 23 Jul 2010 17:03:08 +0200 Subject: testing AA --- application.rb | 7 +++++++ lazar.rb | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/application.rb b/application.rb index ad13b0d..ec44402 100644 --- a/application.rb +++ b/application.rb @@ -11,6 +11,13 @@ class Model property :yaml, Text, :length => 2**32-1 property :token_id, String, :length => 255 property :created_at, DateTime + + after :save, :check_policy + + def check_policy + OpenTox::Authorization.check_policy(uri, token_id) + end + end DataMapper.auto_upgrade! diff --git a/lazar.rb b/lazar.rb index 7e0bb98..19a4b2a 100755 --- a/lazar.rb +++ b/lazar.rb @@ -292,6 +292,9 @@ end post '/?' do # create model halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) model = Lazar.new + model.token_id = params["token_id"] if params["token_id"] + LOGGER.debug "mr create model post /model/? with token_id: #{model.token_id}" + #model.token_id = request.env["HTTP_TOKEN_ID"] if request.env["HTTP_TOKEN_ID"] model.save model.uri = url_for("/#{model.id}", :full) model.yaml = request.env["rack.input"].read @@ -307,6 +310,7 @@ post '/:id/?' do # create prediction @prediction = OpenTox::Dataset.new @prediction.creator = lazar.uri + @prediction.token_id = params[:token_id] dependent_variable = YAML.load(lazar.yaml).dependentVariables @prediction.title = URI.decode(dependent_variable.split(/#/).last) case dependent_variable -- cgit v1.2.3 From 3de9e162beeb5f88d68505b560bc4dea7737e5f8 Mon Sep 17 00:00:00 2001 From: mr Date: Fri, 30 Jul 2010 16:23:09 +0200 Subject: Authorization minor changes => testing --- application.rb | 8 +++++--- lazar.rb | 9 ++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/application.rb b/application.rb index ec44402..d1ff70e 100644 --- a/application.rb +++ b/application.rb @@ -12,8 +12,9 @@ class Model property :token_id, String, :length => 255 property :created_at, DateTime - after :save, :check_policy + after :save, :check_policy + private def check_policy OpenTox::Authorization.check_policy(uri, token_id) end @@ -50,9 +51,10 @@ delete '/:id/?' do uri = model.uri model.destroy! "Model #{params[:id]} deleted." - if params["token_id"] and !Model.get(params[:id]) and uri + if params[:token_id] and !Model.get(params[:id]) and uri begin - aa = OpenTox::Authorization.delete_policy_from_uri(uri, params["token_id"]) + aa = OpenTox::Authorization.delete_policy_from_uri(uri, params[:token_id]) + LOGGER.debug "Policy deleted for Model URI: #{uri} with token_id: #{params[:token_id]} with result: #{aa}" rescue LOGGER.warn "Policy delete error for Model URI: #{uri}" end diff --git a/lazar.rb b/lazar.rb index 19a4b2a..e43995d 100755 --- a/lazar.rb +++ b/lazar.rb @@ -291,11 +291,10 @@ end post '/?' do # create model halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) - model = Lazar.new - model.token_id = params["token_id"] if params["token_id"] - LOGGER.debug "mr create model post /model/? with token_id: #{model.token_id}" - #model.token_id = request.env["HTTP_TOKEN_ID"] if request.env["HTTP_TOKEN_ID"] + model = Lazar.new model.save + model.token_id = params[:token_id] if params[:token_id] + model.token_id = request.env["HTTP_TOKEN_ID"] if !model.token_id and request.env["HTTP_TOKEN_ID"] model.uri = url_for("/#{model.id}", :full) model.yaml = request.env["rack.input"].read model.save @@ -311,6 +310,7 @@ post '/:id/?' do # create prediction @prediction = OpenTox::Dataset.new @prediction.creator = lazar.uri @prediction.token_id = params[:token_id] + @prediction.token_id = request.env["HTTP_TOKEN_ID"] if !@prediction.token_id and request.env["HTTP_TOKEN_ID"] dependent_variable = YAML.load(lazar.yaml).dependentVariables @prediction.title = URI.decode(dependent_variable.split(/#/).last) case dependent_variable @@ -357,7 +357,6 @@ post '/:id/?' do # create prediction end haml :prediction end - elsif dataset_uri response['Content-Type'] = 'text/uri-list' task_uri = OpenTox::Task.as_task do -- cgit v1.2.3 From aa9fdad000d45e6ad0fb126704c51fb00c032ea4 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 25 Aug 2010 14:49:33 +0200 Subject: opentox-api-wrapper bumped to 1.6.6 --- application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application.rb b/application.rb index 3e80e8b..d0d12af 100644 --- a/application.rb +++ b/application.rb @@ -1,5 +1,5 @@ require 'rubygems' -gem "opentox-ruby-api-wrapper", "= 1.6.5" +gem "opentox-ruby-api-wrapper", "= 1.6.6" require 'opentox-ruby-api-wrapper' class Model -- cgit v1.2.3 From dc7c3474ce544f2f3fa3c23a483f6c24339999de Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Mon, 13 Sep 2010 17:09:37 +0200 Subject: intermediary commit for new owl serializer --- lazar.rb | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lazar.rb b/lazar.rb index 7d2198a..560a513 100644 --- a/lazar.rb +++ b/lazar.rb @@ -215,17 +215,16 @@ class Lazar < Model data = YAML.load(yaml) activity_dataset = YAML.load(RestClient.get(data.trainingDataset, :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.create 'Model', uri - owl.set("creator","http://github.com/helma/opentox-model") - owl.set("title", URI.decode(data.dependentVariables.split(/#/).last) ) - #owl.set("title","#{URI.decode(activity_dataset.title)} lazar classification") - owl.set("date",created_at.to_s) - owl.set("algorithm",data.algorithm) - owl.set("dependentVariables",activity_dataset.features.join(', ')) - owl.set("independentVariables",feature_dataset.features.join(', ')) - owl.set("predictedVariables", data.dependentVariables ) - #owl.set("predictedVariables",activity_dataset.features.join(', ') + "_lazar_classification") - owl.set("trainingDataset",data.trainingDataset) + owl = OpenTox::OwlSerializer.create 'Model', uri + owl.annotate("creator","http://github.com/helma/opentox-model") + owl.annotate("title", URI.decode(data.dependentVariables.split(/#/).last) ) + owl.annotate("date",created_at.to_s) + owl.annotate("algorithm",data.algorithm) + owl.annotate("dependentVariables",activity_dataset.features.join(', ')) + owl.annotate("independentVariables",feature_dataset.features.join(', ')) + owl.annotate("predictedVariables", data.dependentVariables ) + owl.annotate("trainingDataset",data.trainingDataset) +=begin owl.parameters = { "Dataset URI" => { :scope => "mandatory", :value => data.trainingDataset }, @@ -234,6 +233,7 @@ class Lazar < Model "Feature generation URI" => { :scope => "mandatory", :value => feature_dataset.creator } } +=end owl.rdf end @@ -257,11 +257,12 @@ get '/:id/?' do case accept when "application/rdf+xml" response['Content-Type'] = 'application/rdf+xml' - unless model.owl # lazy owl creation - model.owl = model.to_owl - model.save - end - model.owl +# unless model.owl # lazy owl creation +# model.owl = model.to_owl +# model.save +# end +# model.owl + model.to_owl when /yaml/ response['Content-Type'] = 'application/x-yaml' model.yaml -- cgit v1.2.3 From 5334912982dd06f61103025504a6f78438847ee0 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 14 Sep 2010 20:52:04 +0200 Subject: simplified version of OwlSerializer, RDF/XML for algorithm/lazar, RDF/XML for annotations,compounds, features in dataset --- lazar.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lazar.rb b/lazar.rb index 560a513..87387df 100644 --- a/lazar.rb +++ b/lazar.rb @@ -216,14 +216,16 @@ class Lazar < Model activity_dataset = YAML.load(RestClient.get(data.trainingDataset, :accept => 'application/x-yaml').to_s) feature_dataset = YAML.load(RestClient.get(data.feature_dataset_uri, :accept => 'application/x-yaml').to_s) owl = OpenTox::OwlSerializer.create 'Model', uri - owl.annotate("creator","http://github.com/helma/opentox-model") + #owl.creator "helma@in-silico.ch" + #owl.contributor "helma@in-silico.ch" + owl.annotate("creator",data.algorithm,XSD.AnyUri) owl.annotate("title", URI.decode(data.dependentVariables.split(/#/).last) ) - owl.annotate("date",created_at.to_s) - owl.annotate("algorithm",data.algorithm) - owl.annotate("dependentVariables",activity_dataset.features.join(', ')) - owl.annotate("independentVariables",feature_dataset.features.join(', ')) - owl.annotate("predictedVariables", data.dependentVariables ) - owl.annotate("trainingDataset",data.trainingDataset) + owl.annotate("date",created_at.to_s,XSD.DateTime) + #owl.annotate("algorithm",data.algorithm) + #owl.annotate("dependentVariables",activity_dataset.features.join(', ')) + #owl.annotate("independentVariables",feature_dataset.features.join(', ')) + #owl.annotate("predictedVariables", data.dependentVariables ) + #owl.annotate("trainingDataset",data.trainingDataset) =begin owl.parameters = { "Dataset URI" => -- cgit v1.2.3 From 6d9653546115e73b732eaadfcb3d1dbe9ab7d99a Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 19 Nov 2010 14:42:29 +0100 Subject: lazar predictions and toxcreate are working --- application.rb | 18 +-- lazar.rb | 361 +++++++++--------------------------------------------- property_lazar.rb | 303 --------------------------------------------- 3 files changed, 68 insertions(+), 614 deletions(-) delete mode 100644 property_lazar.rb diff --git a/application.rb b/application.rb index d0d12af..94c6039 100644 --- a/application.rb +++ b/application.rb @@ -2,28 +2,28 @@ require 'rubygems' gem "opentox-ruby-api-wrapper", "= 1.6.6" require 'opentox-ruby-api-wrapper' -class Model +class ModelStore include DataMapper::Resource + attr_accessor :prediction_dataset property :id, Serial property :uri, String, :length => 255 - property :owl, Text, :length => 2**32-1 property :yaml, Text, :length => 2**32-1 property :created_at, DateTime end -class Prediction +class PredictionCache # cache predictions include DataMapper::Resource property :id, Serial property :compound_uri, String, :length => 255 property :model_uri, String, :length => 255 - property :yaml, Text, :length => 2**32-1 + property :dataset_uri, String, :length => 255 end DataMapper.auto_upgrade! require 'lazar.rb' -require 'property_lazar.rb' +#require 'property_lazar.rb' helpers do @@ -42,12 +42,12 @@ end get '/?' do # get index of models response['Content-Type'] = 'text/uri-list' - Model.all(params).collect{|m| m.uri}.join("\n") + "\n" + ModelStore.all(params).collect{|m| m.uri}.join("\n") + "\n" end delete '/:id/?' do begin - Model.get(params[:id]).destroy! + ModelStore.get(params[:id]).destroy! "Model #{params[:id]} deleted." rescue halt 404, "Model #{params[:id]} does not exist." @@ -57,8 +57,8 @@ end delete '/?' do # TODO delete datasets - Model.auto_migrate! - Prediction.auto_migrate! + ModelStore.auto_migrate! + #Prediction.auto_migrate! response['Content-Type'] = 'text/plain' "All models and cached predictions deleted." end diff --git a/lazar.rb b/lazar.rb index 87387df..3b8a0cf 100644 --- a/lazar.rb +++ b/lazar.rb @@ -4,244 +4,10 @@ R = nil require "rinruby" require "haml" +#require "lazar-helper" -class Lazar < Model - - attr_accessor :prediction_dataset - - # AM begin - # regression function, created 06/10 - # ch: please properly integrate this into the workflow. You will need some criterium for distinguishing regression/classification (hardcoded regression for testing) - def regression(compound_uri,prediction,verbose=false) - - lazar = YAML.load self.yaml - compound = OpenTox::Compound.new(:uri => compound_uri) - - # obtain X values for query compound - compound_matches = compound.match lazar.features - - conf = 0.0 - features = { :activating => [], :deactivating => [] } - neighbors = {} - regression = nil - - regr_occurrences = [] # occurrence vector with {0,1} entries - sims = [] # similarity values between query and neighbors - acts = [] # activities of neighbors for supervised learning - neighbor_matches = [] # as in classification: URIs of matches - gram_matrix = [] # square matrix of similarities between neighbors; implements weighted tanimoto kernel - i = 0 - - # aquire data related to query structure - lazar.fingerprints.each do |uri,matches| - sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(compound_matches,matches,lazar.p_values) - lazar.activities[uri].each do |act| - if sim > 0.3 - neighbors[uri] = {:similarity => sim} - neighbors[uri][:features] = { :activating => [], :deactivating => [] } unless neighbors[uri][:features] - matches.each do |m| - if lazar.effects[m] == 'activating' - neighbors[uri][:features][:activating] << {:smarts => m, :p_value => lazar.p_values[m]} - elsif lazar.effects[m] == 'deactivating' - neighbors[uri][:features][:deactivating] << {:smarts => m, :p_value => lazar.p_values[m]} - end - end - lazar.activities[uri].each do |act| - neighbors[uri][:activities] = [] unless neighbors[uri][:activities] - neighbors[uri][:activities] << act - end - conf += OpenTox::Utils.gauss(sim) - sims << OpenTox::Utils.gauss(sim) - #TODO check for 0 s - acts << Math.log10(act.to_f) - neighbor_matches[i] = matches - i+=1 - end - end - end - conf = conf/neighbors.size - LOGGER.debug "Regression: found " + neighbor_matches.size.to_s + " neighbors." - - - unless neighbor_matches.length == 0 - # gram matrix - (0..(neighbor_matches.length-1)).each do |i| - gram_matrix[i] = [] - # lower triangle - (0..(i-1)).each do |j| - sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(neighbor_matches[i], neighbor_matches[j], lazar.p_values) - gram_matrix[i] << OpenTox::Utils.gauss(sim) - end - # diagonal element - gram_matrix[i][i] = 1.0 - # upper triangle - ((i+1)..(neighbor_matches.length-1)).each do |j| - sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(neighbor_matches[i], neighbor_matches[j], lazar.p_values) - gram_matrix[i] << OpenTox::Utils.gauss(sim) - end - end - - @r = RinRuby.new(false,false) # global R instance leads to Socket errors after a large number of requests - @r.eval "library('kernlab')" # this requires R package "kernlab" to be installed - LOGGER.debug "Setting R data ..." - # set data - @r.gram_matrix = gram_matrix.flatten - @r.n = neighbor_matches.length - @r.y = acts - @r.sims = sims - - LOGGER.debug "Preparing R data ..." - # prepare data - @r.eval "y<-as.vector(y)" - @r.eval "gram_matrix<-as.kernelMatrix(matrix(gram_matrix,n,n))" - @r.eval "sims<-as.vector(sims)" - - # model + support vectors - LOGGER.debug "Creating SVM model ..." - @r.eval "model<-ksvm(gram_matrix, y, kernel=matrix, type=\"nu-svr\", nu=0.8)" - @r.eval "sv<-as.vector(SVindex(model))" - @r.eval "sims<-sims[sv]" - @r.eval "sims<-as.kernelMatrix(matrix(sims,1))" - LOGGER.debug "Predicting ..." - @r.eval "p<-predict(model,sims)[1,1]" - regression = 10**(@r.p.to_f) - LOGGER.debug "Prediction is: '" + regression.to_s + "'." - @r.quit # free R - - end - - if (regression != nil) - feature_uri = lazar.dependentVariables - prediction.compounds << compound_uri - prediction.features << feature_uri - prediction.data[compound_uri] = [] unless prediction.data[compound_uri] - compound_matches.each { |m| features[lazar.effects[m].to_sym] << {:smarts => m, :p_value => lazar.p_values[m] } } - tuple = { - File.join(@@config[:services]["opentox-model"],"lazar#regression") => regression, - File.join(@@config[:services]["opentox-model"],"lazar#confidence") => conf - } - if verbose - tuple[File.join(@@config[:services]["opentox-model"],"lazar#neighbors")] = neighbors - tuple[File.join(@@config[:services]["opentox-model"],"lazar#features")] = features - end - prediction.data[compound_uri] << {feature_uri => tuple} - end - - end - # AM end - - - def classification(compound_uri,prediction,verbose=false) - - lazar = YAML.load self.yaml - compound = OpenTox::Compound.new(:uri => compound_uri) - compound_matches = compound.match lazar.features - - conf = 0.0 - features = { :activating => [], :deactivating => [] } - 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] = {:similarity => sim} - neighbors[uri][:features] = { :activating => [], :deactivating => [] } unless neighbors[uri][:features] - matches.each do |m| - if lazar.effects[m] == 'activating' - neighbors[uri][:features][:activating] << {:smarts => m, :p_value => lazar.p_values[m]} - elsif lazar.effects[m] == 'deactivating' - neighbors[uri][:features][:deactivating] << {:smarts => m, :p_value => lazar.p_values[m]} - end - end - lazar.activities[uri].each do |act| - neighbors[uri][:activities] = [] unless neighbors[uri][:activities] - neighbors[uri][:activities] << 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 - if (classification != nil) - feature_uri = lazar.dependentVariables - prediction.compounds << compound_uri - prediction.features << feature_uri - prediction.data[compound_uri] = [] unless prediction.data[compound_uri] - compound_matches.each { |m| features[lazar.effects[m].to_sym] << {:smarts => m, :p_value => lazar.p_values[m] } } - tuple = { - File.join(@@config[:services]["opentox-model"],"lazar#classification") => classification, - File.join(@@config[:services]["opentox-model"],"lazar#confidence") => conf - } - if verbose - tuple[File.join(@@config[:services]["opentox-model"],"lazar#neighbors")] = neighbors - tuple[File.join(@@config[:services]["opentox-model"],"lazar#features")] = features - end - prediction.data[compound_uri] << {feature_uri => tuple} - end - end - - def database_activity?(compound_uri,prediction) - # find database activities - lazar = YAML.load self.yaml - db_activities = lazar.activities[compound_uri] - if db_activities - prediction.creator = lazar.trainingDataset - feature_uri = lazar.dependentVariables - 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 - end - end - - def to_owl - data = YAML.load(yaml) - activity_dataset = YAML.load(RestClient.get(data.trainingDataset, :accept => 'application/x-yaml').to_s) - feature_dataset = YAML.load(RestClient.get(data.feature_dataset_uri, :accept => 'application/x-yaml').to_s) - owl = OpenTox::OwlSerializer.create 'Model', uri - #owl.creator "helma@in-silico.ch" - #owl.contributor "helma@in-silico.ch" - owl.annotate("creator",data.algorithm,XSD.AnyUri) - owl.annotate("title", URI.decode(data.dependentVariables.split(/#/).last) ) - owl.annotate("date",created_at.to_s,XSD.DateTime) - #owl.annotate("algorithm",data.algorithm) - #owl.annotate("dependentVariables",activity_dataset.features.join(', ')) - #owl.annotate("independentVariables",feature_dataset.features.join(', ')) - #owl.annotate("predictedVariables", data.dependentVariables ) - #owl.annotate("trainingDataset",data.trainingDataset) -=begin - owl.parameters = { - "Dataset URI" => - { :scope => "mandatory", :value => data.trainingDataset }, - "Feature URI for dependent variable" => - { :scope => "mandatory", :value => activity_dataset.features.join(', ')}, - "Feature generation URI" => - { :scope => "mandatory", :value => feature_dataset.creator } - } -=end - - owl.rdf - end - -end - +# Get model representation +# @return [application/rdf+xml,application/x-yaml] Model representation get '/:id/?' do accept = request.env['HTTP_ACCEPT'] accept = "application/rdf+xml" if accept == '*/*' or accept == '' or accept.nil? @@ -254,17 +20,14 @@ get '/:id/?' do params[:id].sub!(/.rdf$/,'') accept = 'application/rdf+xml' end - model = Lazar.get(params[:id]) - halt 404, "Model #{params[:id]} not found." unless model + halt 404, "Model #{params[:id]} not found." unless model = ModelStore.get(params[:id]) + lazar = YAML.load model.yaml case accept - when "application/rdf+xml" - response['Content-Type'] = 'application/rdf+xml' -# unless model.owl # lazy owl creation -# model.owl = model.to_owl -# model.save -# end -# model.owl - model.to_owl + when /application\/rdf\+xml/ + s = OpenTox::Serializer::Owl.new + s.add_model(url_for('/lazar',:full),lazar.metadata) + response['Content-Type'] = 'application/rdf+xml' + s.to_rdfxml when /yaml/ response['Content-Type'] = 'application/x-yaml' model.yaml @@ -273,88 +36,82 @@ get '/:id/?' do end end +=begin get '/:id/algorithm/?' do response['Content-Type'] = 'text/plain' - YAML.load(Lazar.get(params[:id]).yaml).algorithm + YAML.load(ModelStore.get(params[:id]).yaml).algorithm end get '/:id/trainingDataset/?' do response['Content-Type'] = 'text/plain' - YAML.load(Lazar.get(params[:id]).yaml).trainingDataset + YAML.load(ModelStore.get(params[:id]).yaml).trainingDataset end get '/:id/feature_dataset/?' do response['Content-Type'] = 'text/plain' - YAML.load(Lazar.get(params[:id]).yaml).feature_dataset_uri + YAML.load(ModelStore.get(params[:id]).yaml).feature_dataset_uri end +=end +# Store a lazar model. This method should not be called directly, use OpenTox::Algorithm::Lazr to create a lazar model +# @param [Body] lazar Model representation in YAML format +# @return [String] Model URI post '/?' do # create model halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) - model = Lazar.new - model.save + model = ModelStore.create model.uri = url_for("/#{model.id}", :full) - model.yaml = request.env["rack.input"].read + lazar = YAML.load request.env["rack.input"].read + lazar.uri = model.uri + model.yaml = lazar.to_yaml model.save model.uri end -post '/:id/?' do # create prediction - - lazar = Lazar.get(params[:id]) - halt 404, "Model #{params[:id]} does not exist." unless lazar +# Make a lazar prediction. Predicts either a single compound or all compounds from a dataset +# @param [optional,String] dataset_uri URI of the dataset to be predicted +# @param [optional,String] compound_uri URI of the compound to be predicted +# @param [optional,Header] Accept Content-type of prediction, can be either `application/rdf+xml or application/x-yaml` +# @return [text/uri-list,application/rdf+xml,application/x-yaml] URI of prediction task (dataset prediction) or prediction in requested representation +post '/:id/?' do + + start = Time.now + @lazar = YAML.load ModelStore.get(params[:id]).yaml + + 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.creator = lazar.uri - dependent_variable = YAML.load(lazar.yaml).dependentVariables - @prediction.title = URI.decode(dependent_variable.split(/#/).last) - case dependent_variable - when /classification/ - prediction_type = "classification" - when /regression/ - prediction_type = "regression" - end - + s = Time.now if compound_uri - # look for cached prediction first - if cached_prediction = Prediction.first(:model_uri => lazar.uri, :compound_uri => compound_uri) - @prediction = YAML.load(cached_prediction.yaml) - else - begin - # AM: switch here between regression and classification - eval "lazar.#{prediction_type}(compound_uri,@prediction,true) unless lazar.database_activity?(compound_uri,@prediction)" - Prediction.create(:model_uri => lazar.uri, :compound_uri => compound_uri, :yaml => @prediction.to_yaml) - rescue - LOGGER.error "#{prediction_type} failed for #{compound_uri} with #{$!} " - halt 500, "Prediction of #{compound_uri} failed." - end - end - case request.env['HTTP_ACCEPT'] - when /yaml/ - @prediction.to_yaml - when 'application/rdf+xml' - @prediction.to_owl - else - halt 400, "MIME type \"#{request.env['HTTP_ACCEPT']}\" not supported." - end + #begin + @prediction = @lazar.predict(compound_uri,true) + #rescue + #LOGGER.error "Lazar prediction failed for #{compound_uri} with #{$!} " + #halt 500, "Prediction of #{compound_uri} with #{@lazar.uri} failed." + #end + #accept = request.env['HTTP_ACCEPT'] + #accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil? + LOGGER.debug "Total: #{Time.now - start} seconds" + #case accept + #when /yaml/ + @prediction.uri + #else # RestClientWrapper does not send accept header + #when /application\/rdf\+xml/ + #@prediction.to_rdfxml + #else + #halt 400, "MIME type \"#{request.env['HTTP_ACCEPT']}\" not supported." + #end elsif dataset_uri response['Content-Type'] = 'text/uri-list' task_uri = OpenTox::Task.as_task("Predict dataset",url_for("/#{lazar.id}", :full)) do - input_dataset = OpenTox::Dataset.find(dataset_uri) - input_dataset.compounds.each do |compound_uri| - # AM: switch here between regression and classification - begin - eval "lazar.#{prediction_type}(compound_uri,@prediction) unless lazar.database_activity?(compound_uri,@prediction)" - rescue - LOGGER.error "#{prediction_type} failed for #{compound_uri} with #{$!} " - end - end - begin - uri = @prediction.save.chomp - rescue - halt 500, "Could not save prediction dataset" + OpenTox::Dataset.find(dataset_uri).compounds.each do |compound_uri| + #begin + predict(compound_uri,true) + #rescue + #LOGGER.error "#{prediction_type} failed for #{compound_uri} with #{$!} " + #end end + @prediction.save end halt 202,task_uri end diff --git a/property_lazar.rb b/property_lazar.rb deleted file mode 100644 index 6e68718..0000000 --- a/property_lazar.rb +++ /dev/null @@ -1,303 +0,0 @@ -# R integration -# workaround to initialize R non-interactively (former rinruby versions did this by default) -# avoids compiling R with X -R = nil -require "rinruby" -require "haml" - -class PropertyLazar < Model - - attr_accessor :prediction_dataset - -=begin - # AM begin - # regression function, created 06/10 - def regression(compound_uri,prediction,verbose=false) - - lazar = YAML.load self.yaml - compound = OpenTox::Compound.new(:uri => compound_uri) - - # obtain X values for query compound - compound_matches = compound.match lazar.features - - conf = 0.0 - features = { :activating => [], :deactivating => [] } - neighbors = {} - regression = nil - - regr_occurrences = [] # occurrence vector with {0,1} entries - sims = [] # similarity values between query and neighbors - acts = [] # activities of neighbors for supervised learning - neighbor_matches = [] # as in classification: URIs of matches - gram_matrix = [] # square matrix of similarities between neighbors; implements weighted tanimoto kernel - i = 0 - - # aquire data related to query structure - lazar.fingerprints.each do |uri,matches| - sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(compound_matches,matches,lazar.p_values) - lazar.activities[uri].each do |act| - if sim > 0.3 - neighbors[uri] = {:similarity => sim} - neighbors[uri][:features] = { :activating => [], :deactivating => [] } unless neighbors[uri][:features] - matches.each do |m| - if lazar.effects[m] == 'activating' - neighbors[uri][:features][:activating] << {:smarts => m, :p_value => lazar.p_values[m]} - elsif lazar.effects[m] == 'deactivating' - neighbors[uri][:features][:deactivating] << {:smarts => m, :p_value => lazar.p_values[m]} - end - end - lazar.activities[uri].each do |act| - neighbors[uri][:activities] = [] unless neighbors[uri][:activities] - neighbors[uri][:activities] << act - end - conf += OpenTox::Utils.gauss(sim) - sims << OpenTox::Utils.gauss(sim) - #TODO check for 0 s - acts << Math.log10(act.to_f) - neighbor_matches[i] = matches - i+=1 - end - end - end - conf = conf/neighbors.size - LOGGER.debug "Regression: found " + neighbor_matches.size.to_s + " neighbors." - - - unless neighbor_matches.length == 0 - # gram matrix - (0..(neighbor_matches.length-1)).each do |i| - gram_matrix[i] = [] - # lower triangle - (0..(i-1)).each do |j| - sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(neighbor_matches[i], neighbor_matches[j], lazar.p_values) - gram_matrix[i] << OpenTox::Utils.gauss(sim) - end - # diagonal element - gram_matrix[i][i] = 1.0 - # upper triangle - ((i+1)..(neighbor_matches.length-1)).each do |j| - sim = OpenTox::Algorithm::Similarity.weighted_tanimoto(neighbor_matches[i], neighbor_matches[j], lazar.p_values) - gram_matrix[i] << OpenTox::Utils.gauss(sim) - end - end - - @r = RinRuby.new(false,false) # global R instance leads to Socket errors after a large number of requests - @r.eval "library('kernlab')" # this requires R package "kernlab" to be installed - LOGGER.debug "Setting R data ..." - # set data - @r.gram_matrix = gram_matrix.flatten - @r.n = neighbor_matches.length - @r.y = acts - @r.sims = sims - - LOGGER.debug "Preparing R data ..." - # prepare data - @r.eval "y<-as.vector(y)" - @r.eval "gram_matrix<-as.kernelMatrix(matrix(gram_matrix,n,n))" - @r.eval "sims<-as.vector(sims)" - - # model + support vectors - LOGGER.debug "Creating SVM model ..." - @r.eval "model<-ksvm(gram_matrix, y, kernel=matrix, type=\"nu-svr\", nu=0.8)" - @r.eval "sv<-as.vector(SVindex(model))" - @r.eval "sims<-sims[sv]" - @r.eval "sims<-as.kernelMatrix(matrix(sims,1))" - LOGGER.debug "Predicting ..." - @r.eval "p<-predict(model,sims)[1,1]" - regression = 10**(@r.p.to_f) - LOGGER.debug "Prediction is: '" + regression.to_s + "'." - @r.quit # free R - - end - - if (regression != nil) - feature_uri = lazar.dependentVariables - prediction.compounds << compound_uri - prediction.features << feature_uri - prediction.data[compound_uri] = [] unless prediction.data[compound_uri] - compound_matches.each { |m| features[lazar.effects[m].to_sym] << {:smarts => m, :p_value => lazar.p_values[m] } } - tuple = { - File.join(@@config[:services]["opentox-model"],"lazar#regression") => regression, - File.join(@@config[:services]["opentox-model"],"lazar#confidence") => conf - } - if verbose - tuple[File.join(@@config[:services]["opentox-model"],"lazar#neighbors")] = neighbors - tuple[File.join(@@config[:services]["opentox-model"],"lazar#features")] = features - end - prediction.data[compound_uri] << {feature_uri => tuple} - end - - end - # AM end -=end - - - def classification(compound_uri,prediction,verbose=false) - - lazar = YAML.load self.yaml - compound = OpenTox::Compound.new(:uri => compound_uri) - compound_properties = lazar.properties[compound.uri] - - conf = 0.0 - neighbors = {} - features = [] - classification = nil - - lazar.properties.each do |uri,properties| - - sim = OpenTox::Algorithm::Similarity.euclidean(compound_properties,properties) - if sim and sim > 0.001 - neighbors[uri] = {:similarity => sim} - neighbors[uri][:features] = [] unless neighbors[uri][:features] - properties.each do |p,v| - neighbors[uri][:features] << {p => v} - end - lazar.activities[uri].each do |act| - neighbors[uri][:activities] = [] unless neighbors[uri][:activities] - neighbors[uri][:activities] << 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 - if (classification != nil) - feature_uri = lazar.dependentVariables - prediction.compounds << compound_uri - prediction.features << feature_uri - prediction.data[compound_uri] = [] unless prediction.data[compound_uri] - compound_properties.each { |p,v| features << {p => v} } - tuple = { - File.join(@@config[:services]["opentox-model"],"lazar#classification") => classification, - File.join(@@config[:services]["opentox-model"],"lazar#confidence") => conf - } - if verbose - tuple[File.join(@@config[:services]["opentox-model"],"lazar#neighbors")] = neighbors - tuple[File.join(@@config[:services]["opentox-model"],"lazar#features")] = features - end - prediction.data[compound_uri] << {feature_uri => tuple} - end - end - - def database_activity?(compound_uri,prediction) - # find database activities - lazar = YAML.load self.yaml - db_activities = lazar.activities[compound_uri] - if db_activities - prediction.creator = lazar.trainingDataset - feature_uri = lazar.dependentVariables - 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 - end - end - - def to_owl - data = YAML.load(yaml) - activity_dataset = YAML.load(RestClient.get(data.trainingDataset, :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.create 'Model', uri - owl.set("creator","http://github.com/helma/opentox-model") - owl.set("title", URI.decode(data.dependentVariables.split(/#/).last) ) - #owl.set("title","#{URI.decode(activity_dataset.title)} lazar classification") - owl.set("date",created_at.to_s) - owl.set("algorithm",data.algorithm) - owl.set("dependentVariables",activity_dataset.features.join(', ')) - owl.set("independentVariables",feature_dataset.features.join(', ')) - owl.set("predictedVariables", data.dependentVariables ) - #owl.set("predictedVariables",activity_dataset.features.join(', ') + "_lazar_classification") - owl.set("trainingDataset",data.trainingDataset) - owl.parameters = { - "Dataset URI" => - { :scope => "mandatory", :value => data.trainingDataset }, - "Feature URI for dependent variable" => - { :scope => "mandatory", :value => activity_dataset.features.join(', ')}, - "Feature generation URI" => - { :scope => "mandatory", :value => feature_dataset.creator } - } - - owl.rdf - end - -end - -post '/pl/:id/?' do # create prediction - - lazar = PropertyLazar.get(params[:id]) - LOGGER.debug lazar.to_yaml - 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.creator = lazar.uri - dependent_variable = YAML.load(lazar.yaml).dependentVariables - @prediction.title = URI.decode(dependent_variable.split(/#/).last) - case dependent_variable - when /classification/ - prediction_type = "classification" - when /regression/ - prediction_type = "regression" - end - - if compound_uri - # look for cached prediction first - #if cached_prediction = Prediction.first(:model_uri => lazar.uri, :compound_uri => compound_uri) - #@prediction = YAML.load(cached_prediction.yaml) - #else - begin - # AM: switch here between regression and classification - lazar.classification(compound_uri,@prediction,true) #unless lazar.database_activity?(compound_uri,@prediction)" - #eval "lazar.#{prediction_type}(compound_uri,@prediction,true) unless lazar.database_activity?(compound_uri,@prediction)" - #Prediction.create(:model_uri => lazar.uri, :compound_uri => compound_uri, :yaml => @prediction.to_yaml) - rescue - LOGGER.error "#{prediction_type} failed for #{compound_uri} with #{$!} " - halt 500, "Prediction of #{compound_uri} failed." - end - #end - case request.env['HTTP_ACCEPT'] - when /yaml/ - @prediction.to_yaml - when 'application/rdf+xml' - @prediction.to_owl - else - halt 400, "MIME type \"#{request.env['HTTP_ACCEPT']}\" not supported." - end - - elsif dataset_uri - response['Content-Type'] = 'text/uri-list' - task_uri = OpenTox::Task.as_task("Predict dataset",url_for("/#{lazar.id}", :full)) do - input_dataset = OpenTox::Dataset.find(dataset_uri) - input_dataset.compounds.each do |compound_uri| - # AM: switch here between regression and classification - begin - eval "lazar.#{prediction_type}(compound_uri,@prediction) unless lazar.database_activity?(compound_uri,@prediction)" - rescue - LOGGER.error "#{prediction_type} failed for #{compound_uri} with #{$!} " - end - end - begin - uri = @prediction.save.chomp - rescue - halt 500, "Could not save prediction dataset" - end - end - halt 202,task_uri - end - -end -- cgit v1.2.3 From 2d0c76b7838f5f31552a09c54a81e621d1618d2d Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 19 Nov 2010 16:51:14 +0100 Subject: working predictions for toxcreate --- lazar.rb | 65 +++++++++++++--------------------------------------------------- 1 file changed, 13 insertions(+), 52 deletions(-) diff --git a/lazar.rb b/lazar.rb index 3b8a0cf..8ad9fed 100644 --- a/lazar.rb +++ b/lazar.rb @@ -1,8 +1,3 @@ -# R integration -# workaround to initialize R non-interactively (former rinruby versions did this by default) -# avoids compiling R with X -R = nil -require "rinruby" require "haml" #require "lazar-helper" @@ -36,23 +31,6 @@ get '/:id/?' do end end -=begin -get '/:id/algorithm/?' do - response['Content-Type'] = 'text/plain' - YAML.load(ModelStore.get(params[:id]).yaml).algorithm -end - -get '/:id/trainingDataset/?' do - response['Content-Type'] = 'text/plain' - YAML.load(ModelStore.get(params[:id]).yaml).trainingDataset -end - -get '/:id/feature_dataset/?' do - response['Content-Type'] = 'text/plain' - YAML.load(ModelStore.get(params[:id]).yaml).feature_dataset_uri -end -=end - # Store a lazar model. This method should not be called directly, use OpenTox::Algorithm::Lazr to create a lazar model # @param [Body] lazar Model representation in YAML format # @return [String] Model URI @@ -71,47 +49,30 @@ end # @param [optional,String] dataset_uri URI of the dataset to be predicted # @param [optional,String] compound_uri URI of the compound to be predicted # @param [optional,Header] Accept Content-type of prediction, can be either `application/rdf+xml or application/x-yaml` -# @return [text/uri-list,application/rdf+xml,application/x-yaml] URI of prediction task (dataset prediction) or prediction in requested representation +# @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do - start = Time.now @lazar = YAML.load ModelStore.get(params[:id]).yaml 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] - s = Time.now + response['Content-Type'] = 'text/uri-list' if compound_uri - #begin - @prediction = @lazar.predict(compound_uri,true) - #rescue - #LOGGER.error "Lazar prediction failed for #{compound_uri} with #{$!} " - #halt 500, "Prediction of #{compound_uri} with #{@lazar.uri} failed." - #end - #accept = request.env['HTTP_ACCEPT'] - #accept = 'application/rdf+xml' if accept == '*/*' or accept == '' or accept.nil? - LOGGER.debug "Total: #{Time.now - start} seconds" - #case accept - #when /yaml/ - @prediction.uri - #else # RestClientWrapper does not send accept header - #when /application\/rdf\+xml/ - #@prediction.to_rdfxml - #else - #halt 400, "MIME type \"#{request.env['HTTP_ACCEPT']}\" not supported." - #end + begin + cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) + return cache.dataset_uri if cache + prediction_uri = @lazar.predict(compound_uri,true).uri + PredictionCache.create(:model_uri => @lazar.uri, :compound_uri => compound_uri, :dataset_uri => prediction_uri) + prediction_uri + rescue + LOGGER.error "Lazar prediction failed for #{compound_uri} with #{$!} " + halt 500, "Prediction of #{compound_uri} with #{@lazar.uri} failed." + end elsif dataset_uri - response['Content-Type'] = 'text/uri-list' task_uri = OpenTox::Task.as_task("Predict dataset",url_for("/#{lazar.id}", :full)) do - OpenTox::Dataset.find(dataset_uri).compounds.each do |compound_uri| - #begin - predict(compound_uri,true) - #rescue - #LOGGER.error "#{prediction_type} failed for #{compound_uri} with #{$!} " - #end - end - @prediction.save + @lazar.predict(dataset_uri).uri end halt 202,task_uri end -- cgit v1.2.3 From 40d607c935e27ab26a2f2dd1d641717c9d6a5e31 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 23 Nov 2010 16:45:21 +0100 Subject: dataset prediction fixed --- lazar.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lazar.rb b/lazar.rb index 8ad9fed..0a9dddf 100644 --- a/lazar.rb +++ b/lazar.rb @@ -59,9 +59,9 @@ post '/:id/?' do response['Content-Type'] = 'text/uri-list' if compound_uri + #cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) + #return cache.dataset_uri if cache begin - cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) - return cache.dataset_uri if cache prediction_uri = @lazar.predict(compound_uri,true).uri PredictionCache.create(:model_uri => @lazar.uri, :compound_uri => compound_uri, :dataset_uri => prediction_uri) prediction_uri @@ -71,10 +71,10 @@ post '/:id/?' do end elsif dataset_uri - task_uri = OpenTox::Task.as_task("Predict dataset",url_for("/#{lazar.id}", :full)) do - @lazar.predict(dataset_uri).uri + task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do + @lazar.predict_dataset(dataset_uri).uri end - halt 202,task_uri + halt 202,task.uri end end -- cgit v1.2.3 From 6e5aa1678e7d0380de77e462347ae2260fd3051b Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 24 Nov 2010 11:48:32 +0100 Subject: status 503 for rejected tasks --- lazar.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lazar.rb b/lazar.rb index 0a9dddf..e543802 100644 --- a/lazar.rb +++ b/lazar.rb @@ -1,5 +1,13 @@ require "haml" -#require "lazar-helper" + +helpers do + def uri_available?(urlStr) + url = URI.parse(urlStr) + Net::HTTP.start(urlStr.host, urlStr.port) do |http| + return http.head(urlStr.request_uri).code == "200" + end + end +end # Get model representation # @return [application/rdf+xml,application/x-yaml] Model representation @@ -59,8 +67,8 @@ post '/:id/?' do response['Content-Type'] = 'text/uri-list' if compound_uri - #cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) - #return cache.dataset_uri if cache + cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) + return cache.dataset_uri if cache and uri_available?(cache.dataset_uri) begin prediction_uri = @lazar.predict(compound_uri,true).uri PredictionCache.create(:model_uri => @lazar.uri, :compound_uri => compound_uri, :dataset_uri => prediction_uri) @@ -74,6 +82,7 @@ post '/:id/?' do task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do @lazar.predict_dataset(dataset_uri).uri end + halt 503,task.uri+"\n" if task.status == "Cancelled" halt 202,task.uri end -- cgit v1.2.3 From 61ba09a60cd00c8e8f54ba6766cabe56e658051e Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 24 Nov 2010 13:10:52 +0100 Subject: opentox-ruby-api-wrapper renamed to opentox-ruby --- application.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application.rb b/application.rb index 94c6039..eb7db12 100644 --- a/application.rb +++ b/application.rb @@ -1,6 +1,6 @@ require 'rubygems' -gem "opentox-ruby-api-wrapper", "= 1.6.6" -require 'opentox-ruby-api-wrapper' +gem "opentox-ruby", "~> 0" +require 'opentox-ruby' class ModelStore include DataMapper::Resource -- cgit v1.2.3 From 12220a1cc4b37fda3a6776c4d0fd787d90a16882 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 24 Nov 2010 14:43:20 +0100 Subject: opentox-ruby gem in config.ru --- config.ru | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config.ru b/config.ru index 6f5db40..1616a96 100644 --- a/config.ru +++ b/config.ru @@ -1,6 +1,5 @@ require 'rubygems' -require 'opentox-ruby-api-wrapper' +require 'opentox-ruby' require 'config/config_ru' set :app_file, __FILE__ # to get the view path right run Sinatra::Application - -- cgit v1.2.3 From 1981b852f31223227fc1d384111d5cff59db07ea Mon Sep 17 00:00:00 2001 From: mr Date: Fri, 10 Dec 2010 18:32:12 +0100 Subject: fix delete policies after delete/:id and --- application.rb | 6 ++++-- lazar.rb | 12 ++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/application.rb b/application.rb index b522baf..f11a211 100644 --- a/application.rb +++ b/application.rb @@ -59,14 +59,16 @@ delete '/:id/?' do uri = ModelStore.get(params[:id]).uri ModelStore.get(params[:id]).destroy! "Model #{params[:id]} deleted." - if params[:token_id] and !Model.get(params[:id]) and uri + if params[:token_id] and !ModelStore.get(params[:id]) and uri begin - aa = OpenTox::Authorization.delete_policy_from_uri(uri, params[:token_id]) + aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:token_id]) LOGGER.debug "Policy deleted for Model URI: #{uri} with token_id: #{params[:token_id]} with result: #{aa}" rescue LOGGER.warn "Policy delete error for Model URI: #{uri}" end end + response['Content-Type'] = 'text/plain' + "Model #{params[:id]} deleted." rescue halt 404, "Model #{params[:id]} does not exist." end diff --git a/lazar.rb b/lazar.rb index 13c3caa..c565a74 100644 --- a/lazar.rb +++ b/lazar.rb @@ -3,8 +3,16 @@ require "haml" helpers do def uri_available?(urlStr) url = URI.parse(urlStr) - Net::HTTP.start(urlStr.host, urlStr.port) do |http| - return http.head(urlStr.request_uri).code == "200" + token_id = params[:token_id] if params[:token_id] + token_id = request.env['HTTP_TOKEN_ID'] if !token_id and request.env['HTTP_TOKEN_ID'] + unless token_id + Net::HTTP.start(url.host, url.port) do |http| + return http.head(url.request_uri).code == "200" + end + else + Net::HTTP.start(url.host, url.port) do |http| + return http.post(url.request_uri, "token_id=#{token_id}").code == "202" + end end end end -- cgit v1.2.3 From baecdfe03a6bff2ee28d687f77b1c6606e102003 Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 14 Dec 2010 12:47:16 +0100 Subject: remove token_id from saving in table --- application.rb | 6 ++++-- lazar.rb | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/application.rb b/application.rb index f11a211..7935577 100644 --- a/application.rb +++ b/application.rb @@ -4,13 +4,15 @@ require 'opentox-ruby' class ModelStore include DataMapper::Resource - attr_accessor :prediction_dataset + attr_accessor :prediction_dataset, :token_id property :id, Serial property :uri, String, :length => 255 property :yaml, Text, :length => 2**32-1 - property :token_id, String, :length => 255 + #property :token_id, String, :length => 255 property :created_at, DateTime + @token_id = nil + after :save, :check_policy private diff --git a/lazar.rb b/lazar.rb index c565a74..e930f15 100644 --- a/lazar.rb +++ b/lazar.rb @@ -69,6 +69,8 @@ end # @param [optional,Header] Accept Content-type of prediction, can be either `application/rdf+xml or application/x-yaml` # @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do + token_id = params[:token_id] if params[:token_id] + token_id = request.env["HTTP_TOKEN_ID"] if !token_id and request.env["HTTP_TOKEN_ID"] @lazar = YAML.load ModelStore.get(params[:id]).yaml @@ -81,7 +83,7 @@ post '/:id/?' do cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) return cache.dataset_uri if cache and uri_available?(cache.dataset_uri) begin - prediction_uri = @lazar.predict(compound_uri,true).uri + prediction_uri = @lazar.predict(compound_uri,true,token_id).uri PredictionCache.create(:model_uri => @lazar.uri, :compound_uri => compound_uri, :dataset_uri => prediction_uri) prediction_uri rescue -- cgit v1.2.3 From 96356b0d439342b9132847b9f6c38cb7a10206c8 Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 14 Dec 2010 16:37:56 +0100 Subject: rename token_id to subjectid --- application.rb | 13 ++++++------- lazar.rb | 18 +++++++++--------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/application.rb b/application.rb index 7935577..99e4619 100644 --- a/application.rb +++ b/application.rb @@ -4,20 +4,19 @@ require 'opentox-ruby' class ModelStore include DataMapper::Resource - attr_accessor :prediction_dataset, :token_id + attr_accessor :prediction_dataset, :subjectid property :id, Serial property :uri, String, :length => 255 property :yaml, Text, :length => 2**32-1 - #property :token_id, String, :length => 255 property :created_at, DateTime - @token_id = nil + @subjectid = nil after :save, :check_policy private def check_policy - OpenTox::Authorization.check_policy(uri, token_id) + OpenTox::Authorization.check_policy(uri, subjectid) end end @@ -61,10 +60,10 @@ delete '/:id/?' do uri = ModelStore.get(params[:id]).uri ModelStore.get(params[:id]).destroy! "Model #{params[:id]} deleted." - if params[:token_id] and !ModelStore.get(params[:id]) and uri + if params[:subjectid] and !ModelStore.get(params[:id]) and uri begin - aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:token_id]) - LOGGER.debug "Policy deleted for Model URI: #{uri} with token_id: #{params[:token_id]} with result: #{aa}" + aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:subjectid]) + LOGGER.debug "Policy deleted for Model URI: #{uri} with subjectid: #{params[:subjectid]} with result: #{aa}" rescue LOGGER.warn "Policy delete error for Model URI: #{uri}" end diff --git a/lazar.rb b/lazar.rb index e930f15..ba34361 100644 --- a/lazar.rb +++ b/lazar.rb @@ -3,15 +3,15 @@ require "haml" helpers do def uri_available?(urlStr) url = URI.parse(urlStr) - token_id = params[:token_id] if params[:token_id] - token_id = request.env['HTTP_TOKEN_ID'] if !token_id and request.env['HTTP_TOKEN_ID'] - unless token_id + subjectid = params[:subjectid] if params[:subjectid] + subjectid = request.env['HTTP_SUBJECTID'] if !subjectid and request.env['HTTP_SUBJECTID'] + unless subjectid Net::HTTP.start(url.host, url.port) do |http| return http.head(url.request_uri).code == "200" end else Net::HTTP.start(url.host, url.port) do |http| - return http.post(url.request_uri, "token_id=#{token_id}").code == "202" + return http.post(url.request_uri, "subjectid=#{subjectid}").code == "202" end end end @@ -53,8 +53,8 @@ end post '/?' do # create model halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) model = ModelStore.create - model.token_id = params[:token_id] if params[:token_id] - model.token_id = request.env["HTTP_TOKEN_ID"] if !model.token_id and request.env["HTTP_TOKEN_ID"] + model.subjectid = params[:subjectid] if params[:subjectid] + model.subjectid = request.env["HTTP_SUBJECTID"] if !model.subjectid and request.env["HTTP_SUBJECTID"] model.uri = url_for("/#{model.id}", :full) lazar = YAML.load request.env["rack.input"].read lazar.uri = model.uri @@ -69,8 +69,8 @@ end # @param [optional,Header] Accept Content-type of prediction, can be either `application/rdf+xml or application/x-yaml` # @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do - token_id = params[:token_id] if params[:token_id] - token_id = request.env["HTTP_TOKEN_ID"] if !token_id and request.env["HTTP_TOKEN_ID"] + subjectid = params[:subjectid] if params[:subjectid] + subjectid = request.env["HTTP_SUBJECTID"] if !subjectid and request.env["HTTP_SUBJECTID"] @lazar = YAML.load ModelStore.get(params[:id]).yaml @@ -83,7 +83,7 @@ post '/:id/?' do cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) return cache.dataset_uri if cache and uri_available?(cache.dataset_uri) begin - prediction_uri = @lazar.predict(compound_uri,true,token_id).uri + prediction_uri = @lazar.predict(compound_uri,true,subjectid).uri PredictionCache.create(:model_uri => @lazar.uri, :compound_uri => compound_uri, :dataset_uri => prediction_uri) prediction_uri rescue -- cgit v1.2.3 From 0499d38412d15a3524b27afb5cb61ab01e42f617 Mon Sep 17 00:00:00 2001 From: mr Date: Wed, 15 Dec 2010 11:18:37 +0100 Subject: get subjectid also out of request headers for delete --- application.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/application.rb b/application.rb index 99e4619..a2fbd37 100644 --- a/application.rb +++ b/application.rb @@ -56,23 +56,25 @@ get '/?' do # get index of models end delete '/:id/?' do - begin - uri = ModelStore.get(params[:id]).uri - ModelStore.get(params[:id]).destroy! - "Model #{params[:id]} deleted." - if params[:subjectid] and !ModelStore.get(params[:id]) and uri + begin + uri = ModelStore.get(params[:id]).uri + ModelStore.get(params[:id]).destroy! + "Model #{params[:id]} deleted." + subjectid = params[:subjectid] if params[:subjectid] + subjectid = request.env['HTTP_SUBJECTID'] if !subjectid and request.env['HTTP_SUBJECTID'] + if subjectid and !ModelStore.get(params[:id]) and uri begin - aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:subjectid]) - LOGGER.debug "Policy deleted for Model URI: #{uri} with subjectid: #{params[:subjectid]} with result: #{aa}" + res = OpenTox::Authorization.delete_policies_from_uri(uri, subjectid) + LOGGER.debug "Policy deleted for Model URI: #{uri} with subjectid: #{subjectid} with result: #{res}" rescue LOGGER.warn "Policy delete error for Model URI: #{uri}" end end response['Content-Type'] = 'text/plain' "Model #{params[:id]} deleted." - rescue - halt 404, "Model #{params[:id]} does not exist." - end + rescue + halt 404, "Model #{params[:id]} does not exist." + end end -- cgit v1.2.3 From e2cb62d02a4239de3fcc132c0bd077a2aadc0f09 Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 16 Dec 2010 10:55:35 +0100 Subject: A&A for lazar.predict_dataset --- lazar.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lazar.rb b/lazar.rb index ba34361..8226b06 100644 --- a/lazar.rb +++ b/lazar.rb @@ -92,7 +92,7 @@ post '/:id/?' do end elsif dataset_uri task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do - @lazar.predict_dataset(dataset_uri).uri + @lazar.predict_dataset(dataset_uri, subjectid).uri end halt 503,task.uri+"\n" if task.status == "Cancelled" halt 202,task.uri -- cgit v1.2.3 From 6edd4387f6e1eaf0178321ca97d9814deb8f3e1f Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 11 Jan 2011 17:01:53 +0100 Subject: get metadata for models / indenting --- application.rb | 23 +++++++++++++---------- lazar.rb | 30 +++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/application.rb b/application.rb index 99e4619..4fc749d 100644 --- a/application.rb +++ b/application.rb @@ -52,27 +52,30 @@ end get '/?' do # get index of models response['Content-Type'] = 'text/uri-list' + params.delete_if{|k,v| k=="subjectid"} ModelStore.all(params).collect{|m| m.uri}.join("\n") + "\n" end delete '/:id/?' do - begin - uri = ModelStore.get(params[:id]).uri - ModelStore.get(params[:id]).destroy! - "Model #{params[:id]} deleted." - if params[:subjectid] and !ModelStore.get(params[:id]) and uri + begin + uri = ModelStore.get(params[:id]).uri + ModelStore.get(params[:id]).destroy! + "Model #{params[:id]} deleted." + subjectid = params[:subjectid] if params[:subjectid] + subjectid = request.env['HTTP_SUBJECTID'] if !subjectid and request.env['HTTP_SUBJECTID'] + if subjectid and !ModelStore.get(params[:id]) and uri begin - aa = OpenTox::Authorization.delete_policies_from_uri(uri, params[:subjectid]) - LOGGER.debug "Policy deleted for Model URI: #{uri} with subjectid: #{params[:subjectid]} with result: #{aa}" + res = OpenTox::Authorization.delete_policies_from_uri(uri, subjectid) + LOGGER.debug "Policy deleted for Model URI: #{uri} with subjectid: #{subjectid} with result: #{res}" rescue LOGGER.warn "Policy delete error for Model URI: #{uri}" end end response['Content-Type'] = 'text/plain' "Model #{params[:id]} deleted." - rescue - halt 404, "Model #{params[:id]} does not exist." - end + rescue + halt 404, "Model #{params[:id]} does not exist." + end end diff --git a/lazar.rb b/lazar.rb index ba34361..c37c629 100644 --- a/lazar.rb +++ b/lazar.rb @@ -20,6 +20,7 @@ end # Get model representation # @return [application/rdf+xml,application/x-yaml] Model representation get '/:id/?' do + accept = request.env['HTTP_ACCEPT'] accept = "application/rdf+xml" if accept == '*/*' or accept == '' or accept.nil? # workaround for browser links @@ -47,6 +48,33 @@ get '/:id/?' do end end +get '/:id/metadata.?:ext?' do + + metadata = YAML.load(ModelStore.get(params[:id]).yaml).metadata + + accept = request.env['HTTP_ACCEPT'] + accept = "application/rdf+xml" if accept == '*/*' or accept == '' or accept.nil? + if params[:ext] + case params[:ext] + when "yaml" + accept = 'application/x-yaml' + when "rdf", "rdfxml" + accept = 'application/rdf+xml' + end + end + response['Content-Type'] = accept + case accept + when /yaml/ + metadata.to_yaml + else #when /rdf/ and anything else + serializer = OpenTox::Serializer::Owl.new + serializer.add_metadata url_for("/#{params[:id]}",:full), metadata + serializer.to_rdfxml + end + +end + + # Store a lazar model. This method should not be called directly, use OpenTox::Algorithm::Lazr to create a lazar model # @param [Body] lazar Model representation in YAML format # @return [String] Model URI @@ -92,7 +120,7 @@ post '/:id/?' do end elsif dataset_uri task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do - @lazar.predict_dataset(dataset_uri).uri + @lazar.predict_dataset(dataset_uri, subjectid).uri end halt 503,task.uri+"\n" if task.status == "Cancelled" halt 202,task.uri -- cgit v1.2.3 From 6a94d2a6922b63d698e0085d1e6221990802c867 Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 11 Jan 2011 17:05:17 +0100 Subject: get metadata for models / indenting --- lazar.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lazar.rb b/lazar.rb index c37c629..460050a 100644 --- a/lazar.rb +++ b/lazar.rb @@ -52,8 +52,8 @@ get '/:id/metadata.?:ext?' do metadata = YAML.load(ModelStore.get(params[:id]).yaml).metadata - accept = request.env['HTTP_ACCEPT'] - accept = "application/rdf+xml" if accept == '*/*' or accept == '' or accept.nil? + accept = request.env['HTTP_ACCEPT'] + accept = "application/rdf+xml" if accept == '*/*' or accept == '' or accept.nil? if params[:ext] case params[:ext] when "yaml" -- cgit v1.2.3 From 433b2cd242b94e2b744f5fb40b6196658b146653 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 13 Jan 2011 12:14:59 +0100 Subject: fixing model metadata rdf representation --- lazar.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lazar.rb b/lazar.rb index 8226b06..9bfeedf 100644 --- a/lazar.rb +++ b/lazar.rb @@ -36,7 +36,7 @@ get '/:id/?' do case accept when /application\/rdf\+xml/ s = OpenTox::Serializer::Owl.new - s.add_model(url_for('/lazar',:full),lazar.metadata) + s.add_model(model.uri,lazar.metadata) response['Content-Type'] = 'application/rdf+xml' s.to_rdfxml when /yaml/ -- cgit v1.2.3 From c8ece50aeac195ecb9bf3c5f3cff2d408b19b6e6 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Fri, 14 Jan 2011 14:58:43 +0100 Subject: set percentage completed --- lazar.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lazar.rb b/lazar.rb index 9bfeedf..de94d8b 100644 --- a/lazar.rb +++ b/lazar.rb @@ -91,8 +91,8 @@ post '/:id/?' do halt 500, "Prediction of #{compound_uri} with #{@lazar.uri} failed." end elsif dataset_uri - task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do - @lazar.predict_dataset(dataset_uri, subjectid).uri + task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do |task| + @lazar.predict_dataset(dataset_uri, subjectid, task).uri end halt 503,task.uri+"\n" if task.status == "Cancelled" halt 202,task.uri -- cgit v1.2.3 From 0fdf7ed7fb5b80c78aaa16f8c75ce11dc3840d6e Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 18 Jan 2011 11:29:42 +0100 Subject: get subjectid from api-wrapper helper --- application.rb | 9 ++++----- lazar.rb | 30 ++++++++++++------------------ 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/application.rb b/application.rb index 4fc749d..ae4d064 100644 --- a/application.rb +++ b/application.rb @@ -61,12 +61,11 @@ delete '/:id/?' do uri = ModelStore.get(params[:id]).uri ModelStore.get(params[:id]).destroy! "Model #{params[:id]} deleted." - subjectid = params[:subjectid] if params[:subjectid] - subjectid = request.env['HTTP_SUBJECTID'] if !subjectid and request.env['HTTP_SUBJECTID'] - if subjectid and !ModelStore.get(params[:id]) and uri + + if @subjectid and !ModelStore.get(params[:id]) and uri begin - res = OpenTox::Authorization.delete_policies_from_uri(uri, subjectid) - LOGGER.debug "Policy deleted for Model URI: #{uri} with subjectid: #{subjectid} with result: #{res}" + res = OpenTox::Authorization.delete_policies_from_uri(uri, @subjectid) + LOGGER.debug "Policy deleted for Model URI: #{uri} with subjectid: #{@subjectid} with result: #{res}" rescue LOGGER.warn "Policy delete error for Model URI: #{uri}" end diff --git a/lazar.rb b/lazar.rb index 460050a..16327d8 100644 --- a/lazar.rb +++ b/lazar.rb @@ -3,15 +3,13 @@ require "haml" helpers do def uri_available?(urlStr) url = URI.parse(urlStr) - subjectid = params[:subjectid] if params[:subjectid] - subjectid = request.env['HTTP_SUBJECTID'] if !subjectid and request.env['HTTP_SUBJECTID'] - unless subjectid + unless @subjectid Net::HTTP.start(url.host, url.port) do |http| return http.head(url.request_uri).code == "200" end else Net::HTTP.start(url.host, url.port) do |http| - return http.post(url.request_uri, "subjectid=#{subjectid}").code == "202" + return http.post(url.request_uri, "subjectid=#{@subjectid}").code == "202" end end end @@ -74,21 +72,19 @@ get '/:id/metadata.?:ext?' do end - # Store a lazar model. This method should not be called directly, use OpenTox::Algorithm::Lazr to create a lazar model # @param [Body] lazar Model representation in YAML format # @return [String] Model URI post '/?' do # create model - halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) - model = ModelStore.create - model.subjectid = params[:subjectid] if params[:subjectid] - model.subjectid = request.env["HTTP_SUBJECTID"] if !model.subjectid and request.env["HTTP_SUBJECTID"] - model.uri = url_for("/#{model.id}", :full) - lazar = YAML.load request.env["rack.input"].read + halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) + model = ModelStore.create + model.subjectid = @subjectid + model.uri = url_for("/#{model.id}", :full) + lazar = YAML.load request.env["rack.input"].read lazar.uri = model.uri - model.yaml = lazar.to_yaml - model.save - model.uri + model.yaml = lazar.to_yaml + model.save + model.uri end # Make a lazar prediction. Predicts either a single compound or all compounds from a dataset @@ -97,8 +93,6 @@ end # @param [optional,Header] Accept Content-type of prediction, can be either `application/rdf+xml or application/x-yaml` # @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do - subjectid = params[:subjectid] if params[:subjectid] - subjectid = request.env["HTTP_SUBJECTID"] if !subjectid and request.env["HTTP_SUBJECTID"] @lazar = YAML.load ModelStore.get(params[:id]).yaml @@ -111,7 +105,7 @@ post '/:id/?' do cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) return cache.dataset_uri if cache and uri_available?(cache.dataset_uri) begin - prediction_uri = @lazar.predict(compound_uri,true,subjectid).uri + prediction_uri = @lazar.predict(compound_uri,true,@subjectid).uri PredictionCache.create(:model_uri => @lazar.uri, :compound_uri => compound_uri, :dataset_uri => prediction_uri) prediction_uri rescue @@ -120,7 +114,7 @@ post '/:id/?' do end elsif dataset_uri task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do - @lazar.predict_dataset(dataset_uri, subjectid).uri + @lazar.predict_dataset(dataset_uri, @subjectid).uri end halt 503,task.uri+"\n" if task.status == "Cancelled" halt 202,task.uri -- cgit v1.2.3 From 803d65518c945ca52d5871c553c21cd0628b3d74 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Thu, 20 Jan 2011 11:25:20 +0100 Subject: disable sinatra built in error handling to allow new ot error handling --- config.ru | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.ru b/config.ru index 1616a96..ec39d1b 100644 --- a/config.ru +++ b/config.ru @@ -3,3 +3,5 @@ require 'opentox-ruby' require 'config/config_ru' set :app_file, __FILE__ # to get the view path right run Sinatra::Application +set :raise_errors, false +set :show_exceptions, false \ No newline at end of file -- cgit v1.2.3 From e9918ecda2a658855d483ca953b1348d1803d22f Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 11 Feb 2011 17:02:01 +0100 Subject: Martins and Michas changes merged --- lazar.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lazar.rb b/lazar.rb index fe65d63..30c0be4 100644 --- a/lazar.rb +++ b/lazar.rb @@ -94,6 +94,7 @@ end # @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do + puts params.to_yaml @lazar = YAML.load ModelStore.get(params[:id]).yaml halt 404, "Model #{params[:id]} does not exist." unless @lazar -- cgit v1.2.3 From b4513bfbf3a2d3e0c34de0765d4ea604e2f1500c Mon Sep 17 00:00:00 2001 From: root Date: Thu, 24 Feb 2011 10:45:33 +0000 Subject: file storage implemented for models --- .gitignore | 2 +- application.rb | 126 +++++++++++++++++++++++++++++++++++++++++---------------- lazar.rb | 118 +++++++++++++++++++++++------------------------------ 3 files changed, 143 insertions(+), 103 deletions(-) diff --git a/.gitignore b/.gitignore index b5729fb..4638e4f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ tmp/* *.sqlite3 log/* db/* -models/* +public/* diff --git a/application.rb b/application.rb index ae4d064..eb6d53a 100644 --- a/application.rb +++ b/application.rb @@ -2,14 +2,16 @@ require 'rubygems' gem "opentox-ruby", "~> 0" require 'opentox-ruby' +set :lock, true +=begin class ModelStore - include DataMapper::Resource - attr_accessor :prediction_dataset, :subjectid - property :id, Serial - property :uri, String, :length => 255 - property :yaml, Text, :length => 2**32-1 - property :created_at, DateTime - + include DataMapper::Resource + attr_accessor :prediction_dataset, :subjectid + property :id, Serial + property :uri, String, :length => 255 + property :yaml, Text, :length => 2**32-1 + property :created_at, DateTime + @subjectid = nil after :save, :check_policy @@ -18,45 +20,100 @@ class ModelStore def check_policy OpenTox::Authorization.check_policy(uri, subjectid) end - + end +=end class PredictionCache # cache predictions - include DataMapper::Resource - property :id, Serial - property :compound_uri, String, :length => 255 - property :model_uri, String, :length => 255 - property :dataset_uri, String, :length => 255 + include DataMapper::Resource + property :id, Serial + property :compound_uri, String, :length => 255 + property :model_uri, String, :length => 255 + property :dataset_uri, String, :length => 255 end DataMapper.auto_upgrade! -require 'lazar.rb' -#require 'property_lazar.rb' +before do + @accept = request.env['HTTP_ACCEPT'] + @accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil? + @id = request.path_info.match(/^\/\d+/) + unless @id.nil? + @id = @id.to_s.sub(/\//,'').to_i + + @uri = uri @id + @yaml_file = "public/#{@id}.yaml" + halt 404, "Dataset #{@id} not found." unless File.exists? @yaml_file + end + + # make sure subjectid is not included in params, subjectid is set as member variable + params.delete(:subjectid) +end +require 'lazar.rb' helpers do - def activity(a) - case a.to_s - when "true" - act = "active" - when "false" - act = "inactive" - else - act = "not available" - end - act - end + + def next_id + id = Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last + id = 0 if id.nil? + id + 1 + end + + def uri(id) + url_for "/#{id}", :full + end + + def uri_available?(urlStr) + url = URI.parse(urlStr) + unless @subjectid + Net::HTTP.start(url.host, url.port) do |http| + return http.head(url.request_uri).code == "200" + end + else + Net::HTTP.start(url.host, url.port) do |http| + return http.post(url.request_uri, "subjectid=#{@subjectid}").code == "202" + end + end + end + + def activity(a) + case a.to_s + when "true" + act = "active" + when "false" + act = "inactive" + else + act = "not available" + end + act + end end get '/?' do # get index of models - response['Content-Type'] = 'text/uri-list' - params.delete_if{|k,v| k=="subjectid"} - ModelStore.all(params).collect{|m| m.uri}.join("\n") + "\n" + response['Content-Type'] = 'text/uri-list' + Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n" end delete '/:id/?' do + LOGGER.debug "Deleting model with id "+@id.to_s + begin + FileUtils.rm @yaml_file + if @subjectid and !File.exists? @yaml_file and @uri + begin + res = OpenTox::Authorization.delete_policies_from_uri(@uri, @subjectid) + LOGGER.debug "Policy deleted for Dataset URI: #{@uri} with result: #{res}" + rescue + LOGGER.warn "Policy delete error for Dataset URI: #{@uri}" + end + end + response['Content-Type'] = 'text/plain' + "Model #{@id} deleted." + rescue + halt 404, "Model #{@id} does not exist." + end +=begin begin uri = ModelStore.get(params[:id]).uri ModelStore.get(params[:id]).destroy! @@ -75,13 +132,14 @@ delete '/:id/?' do rescue halt 404, "Model #{params[:id]} does not exist." end +=end end delete '/?' do - # TODO delete datasets - ModelStore.auto_migrate! - #Prediction.auto_migrate! - response['Content-Type'] = 'text/plain' - "All models and cached predictions deleted." + # TODO delete datasets + FileUtils.rm Dir["public/*.yaml"] + PredictionCache.auto_migrate! + response['Content-Type'] = 'text/plain' + "All models and cached predictions deleted." end diff --git a/lazar.rb b/lazar.rb index 30c0be4..2f3b126 100644 --- a/lazar.rb +++ b/lazar.rb @@ -1,72 +1,52 @@ require "haml" -helpers do - def uri_available?(urlStr) - url = URI.parse(urlStr) - unless @subjectid - Net::HTTP.start(url.host, url.port) do |http| - return http.head(url.request_uri).code == "200" - end - else - Net::HTTP.start(url.host, url.port) do |http| - return http.post(url.request_uri, "subjectid=#{@subjectid}").code == "202" - end - end - end -end - # Get model representation # @return [application/rdf+xml,application/x-yaml] Model representation get '/:id/?' do - accept = request.env['HTTP_ACCEPT'] - 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 - halt 404, "Model #{params[:id]} not found." unless model = ModelStore.get(params[:id]) - lazar = YAML.load model.yaml - case accept - when /application\/rdf\+xml/ +=begin + accept = request.env['HTTP_ACCEPT'] + 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 +=end + halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file + response['Content-Type'] = @accept + case @accept + when /application\/rdf\+xml/ s = OpenTox::Serializer::Owl.new - s.add_model(model.uri,lazar.metadata) + s.add_model(@uri,YAML.load_file(@yaml_file).metadata) response['Content-Type'] = 'application/rdf+xml' s.to_rdfxml - when /yaml/ - response['Content-Type'] = 'application/x-yaml' - model.yaml - else - halt 400, "Unsupported MIME type '#{accept}'" - end + when /yaml/ + response['Content-Type'] = 'application/x-yaml' + File.read @yaml_file + when /html/ + response['Content-Type'] = 'text/html' + OpenTox.text_to_html File.read(@yaml_file) + else + halt 400, "Unsupported MIME type '#{@accept}'" + end end get '/:id/metadata.?:ext?' do - metadata = YAML.load(ModelStore.get(params[:id]).yaml).metadata + metadata = YAML.load_file(@yaml_file).metadata - accept = request.env['HTTP_ACCEPT'] - accept = "application/rdf+xml" if accept == '*/*' or accept == '' or accept.nil? - if params[:ext] - case params[:ext] - when "yaml" - accept = 'application/x-yaml' - when "rdf", "rdfxml" - accept = 'application/rdf+xml' - end - end - response['Content-Type'] = accept - case accept + response['Content-Type'] = @accept + case @accept when /yaml/ metadata.to_yaml else #when /rdf/ and anything else serializer = OpenTox::Serializer::Owl.new - serializer.add_metadata url_for("/#{params[:id]}",:full), metadata + serializer.add_metadata @uri, metadata serializer.to_rdfxml end @@ -77,14 +57,17 @@ end # @return [String] Model URI post '/?' do # create model halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) - model = ModelStore.create - model.subjectid = @subjectid - model.uri = url_for("/#{model.id}", :full) - lazar = YAML.load request.env["rack.input"].read - lazar.uri = model.uri - model.yaml = lazar.to_yaml - model.save - model.uri + #model = ModelStore.create + #model.subjectid = @subjectid + #model.uri = url_for("/#{model.id}", :full) + @id = next_id + @uri = uri @id + @yaml_file = "public/#{@id}.yaml" + lazar = YAML.load request.env["rack.input"].read + lazar.uri = @uri + File.open(@yaml_file,"w+"){|f| f.puts lazar.to_yaml} + response['Content-Type'] = 'text/uri-list' + @uri end # Make a lazar prediction. Predicts either a single compound or all compounds from a dataset @@ -94,15 +77,14 @@ end # @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do - puts params.to_yaml - @lazar = YAML.load ModelStore.get(params[:id]).yaml + halt 404, "Model #{params[:id]} does not exist." unless File.exists? @yaml_file - 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] + halt 404, "No compound_uri or dataset_uri parameter." unless compound_uri = params[:compound_uri] or dataset_uri = params[:dataset_uri] + @lazar = YAML.load_file @yaml_file response['Content-Type'] = 'text/uri-list' - if compound_uri + if compound_uri cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) return cache.dataset_uri if cache and uri_available?(cache.dataset_uri) begin @@ -113,12 +95,12 @@ post '/:id/?' do LOGGER.error "Lazar prediction failed for #{compound_uri} with #{$!} " halt 500, "Prediction of #{compound_uri} with #{@lazar.uri} failed." end - elsif dataset_uri - task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do |task| + elsif dataset_uri + task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do |task| @lazar.predict_dataset(dataset_uri, @subjectid, task).uri - end + end halt 503,task.uri+"\n" if task.status == "Cancelled" halt 202,task.uri - end + end end -- cgit v1.2.3 From c13727354eda6a71e0cb2b583b4e04efc1ee8a1e Mon Sep 17 00:00:00 2001 From: root Date: Fri, 25 Feb 2011 17:53:25 +0000 Subject: ohm/redis backend --- application.rb | 20 ++++++++++++-------- lazar.rb | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/application.rb b/application.rb index eb6d53a..79063f5 100644 --- a/application.rb +++ b/application.rb @@ -24,16 +24,19 @@ class ModelStore end =end -class PredictionCache +class PredictionCache < Ohm::Model # cache predictions - include DataMapper::Resource - property :id, Serial - property :compound_uri, String, :length => 255 - property :model_uri, String, :length => 255 - property :dataset_uri, String, :length => 255 + #include DataMapper::Resource + #attribute :id, Serial + attribute :compound_uri + attribute :model_uri + attribute :dataset_uri + + index :compound_uri + index :model_uri end -DataMapper.auto_upgrade! +#DataMapper.auto_upgrade! before do @accept = request.env['HTTP_ACCEPT'] @@ -139,7 +142,8 @@ end delete '/?' do # TODO delete datasets FileUtils.rm Dir["public/*.yaml"] - PredictionCache.auto_migrate! + PredictionCache.all.each {|cache| cache.delete } + #PredictionCache.auto_migrate! response['Content-Type'] = 'text/plain' "All models and cached predictions deleted." end diff --git a/lazar.rb b/lazar.rb index 2f3b126..74680dd 100644 --- a/lazar.rb +++ b/lazar.rb @@ -85,7 +85,7 @@ post '/:id/?' do response['Content-Type'] = 'text/uri-list' if compound_uri - cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) + cache = PredictionCache.find(:model_uri => @lazar.uri, :compound_uri => compound_uri).first return cache.dataset_uri if cache and uri_available?(cache.dataset_uri) begin prediction_uri = @lazar.predict(compound_uri,true,@subjectid).uri -- cgit v1.2.3 From 018c39fc95e6525ce10cc73167aab57ce0ae8de9 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Sun, 27 Feb 2011 09:32:57 +0100 Subject: code cleanup --- application.rb | 46 ---------------------------------------------- lazar.rb | 20 -------------------- 2 files changed, 66 deletions(-) diff --git a/application.rb b/application.rb index 79063f5..ff3abe7 100644 --- a/application.rb +++ b/application.rb @@ -3,31 +3,8 @@ gem "opentox-ruby", "~> 0" require 'opentox-ruby' set :lock, true -=begin -class ModelStore - include DataMapper::Resource - attr_accessor :prediction_dataset, :subjectid - property :id, Serial - property :uri, String, :length => 255 - property :yaml, Text, :length => 2**32-1 - property :created_at, DateTime - - @subjectid = nil - - after :save, :check_policy - - private - def check_policy - OpenTox::Authorization.check_policy(uri, subjectid) - end - -end -=end class PredictionCache < Ohm::Model - # cache predictions - #include DataMapper::Resource - #attribute :id, Serial attribute :compound_uri attribute :model_uri attribute :dataset_uri @@ -36,8 +13,6 @@ class PredictionCache < Ohm::Model index :model_uri end -#DataMapper.auto_upgrade! - before do @accept = request.env['HTTP_ACCEPT'] @accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil? @@ -116,26 +91,6 @@ delete '/:id/?' do rescue halt 404, "Model #{@id} does not exist." end -=begin - begin - uri = ModelStore.get(params[:id]).uri - ModelStore.get(params[:id]).destroy! - "Model #{params[:id]} deleted." - - if @subjectid and !ModelStore.get(params[:id]) and uri - begin - res = OpenTox::Authorization.delete_policies_from_uri(uri, @subjectid) - LOGGER.debug "Policy deleted for Model URI: #{uri} with subjectid: #{@subjectid} with result: #{res}" - rescue - LOGGER.warn "Policy delete error for Model URI: #{uri}" - end - end - response['Content-Type'] = 'text/plain' - "Model #{params[:id]} deleted." - rescue - halt 404, "Model #{params[:id]} does not exist." - end -=end end @@ -143,7 +98,6 @@ delete '/?' do # TODO delete datasets FileUtils.rm Dir["public/*.yaml"] PredictionCache.all.each {|cache| cache.delete } - #PredictionCache.auto_migrate! response['Content-Type'] = 'text/plain' "All models and cached predictions deleted." end diff --git a/lazar.rb b/lazar.rb index 74680dd..ba85784 100644 --- a/lazar.rb +++ b/lazar.rb @@ -3,20 +3,6 @@ require "haml" # Get model representation # @return [application/rdf+xml,application/x-yaml] Model representation get '/:id/?' do - -=begin - accept = request.env['HTTP_ACCEPT'] - 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 -=end halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file response['Content-Type'] = @accept case @accept @@ -37,9 +23,7 @@ get '/:id/?' do end get '/:id/metadata.?:ext?' do - metadata = YAML.load_file(@yaml_file).metadata - response['Content-Type'] = @accept case @accept when /yaml/ @@ -49,7 +33,6 @@ get '/:id/metadata.?:ext?' do serializer.add_metadata @uri, metadata serializer.to_rdfxml end - end # Store a lazar model. This method should not be called directly, use OpenTox::Algorithm::Lazr to create a lazar model @@ -57,9 +40,6 @@ end # @return [String] Model URI post '/?' do # create model halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) - #model = ModelStore.create - #model.subjectid = @subjectid - #model.uri = url_for("/#{model.id}", :full) @id = next_id @uri = uri @id @yaml_file = "public/#{@id}.yaml" -- cgit v1.2.3 From f65ab8815edc18e918e659cb74cb18269fedf5a3 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 9 Mar 2011 10:26:05 +0100 Subject: uri_available moved to opentox-ruby helper --- application.rb | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/application.rb b/application.rb index ff3abe7..9d8c0d5 100644 --- a/application.rb +++ b/application.rb @@ -43,19 +43,6 @@ helpers do url_for "/#{id}", :full end - def uri_available?(urlStr) - url = URI.parse(urlStr) - unless @subjectid - Net::HTTP.start(url.host, url.port) do |http| - return http.head(url.request_uri).code == "200" - end - else - Net::HTTP.start(url.host, url.port) do |http| - return http.post(url.request_uri, "subjectid=#{@subjectid}").code == "202" - end - end - end - def activity(a) case a.to_s when "true" -- cgit v1.2.3 From fa86b693e73db0959f92c5291050634dec237d38 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 9 Mar 2011 12:00:09 +0100 Subject: version bumped to 1.0.0 --- application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application.rb b/application.rb index 9d8c0d5..866a267 100644 --- a/application.rb +++ b/application.rb @@ -1,5 +1,5 @@ require 'rubygems' -gem "opentox-ruby", "~> 0" +gem "opentox-ruby", "~> 1" require 'opentox-ruby' set :lock, true -- cgit v1.2.3 From 854e3ff1d872300151a23f787adf72afc54071f5 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 10 Mar 2011 11:58:12 +0100 Subject: README updated --- README | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/README b/README index 81c96e4..e4d478c 100644 --- a/README +++ b/README @@ -5,7 +5,8 @@ OpenTox model * Implements the OpenTox model API * Ruby implementation of lazar prediction models -REST operations: +REST operations +--------------- Get a list of all lazar models GET / - List of model URIs 200 Get the representation of a lazar model GET /{id} - Model representation 200,404 @@ -13,12 +14,14 @@ Predict a compound POST /{id} compound_uri Prediction Predict a dataset POST /{id} dataset_uri Prediction dataset URI 200,404,500 Delete a model DELETE /{id} - - 200,404 -Supported MIME formats (http://chemical-mime.sourceforge.net/): +Supported MIME formats (http://chemical-mime.sourceforge.net/) +-------------------------------------------------------------- * application/rdf+xml (default): read/write OWL-DL * application/x-yaml -Examples: +Examples +-------- List all lazar models curl http://webservices.in-silico.ch/model @@ -38,6 +41,10 @@ Predict a dataset Delete a model curl -X DELETE http://webservices.in-silico.ch/model/{id} -More documentation: Source code (application.rb, lazar.rb) +API documentation +----------------- + +http://rdoc.info/github/opentox/model + +Copyright (c) 2009-2011 Christoph Helma, Martin Guetlein, Micha Rautenberg, Andreas Maunz, David Vorgrimmler, Denis Gebele. See LICENSE for details. -Copyright (c) 2009 Christoph Helma. See LICENSE for details. -- cgit v1.2.3