From 1d0c46a9711050e116147437fbb334bd800324f3 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 12 May 2010 12:31:39 +0200 Subject: Modification for new RestClient: body instead of to_s --- Rakefile | 2 +- lib/algorithm.rb | 2 +- lib/config/config_ru.rb | 13 +++-- lib/dataset.rb | 124 +++------------------------------------------- lib/environment.rb | 8 ++- lib/model.rb | 69 +------------------------- lib/task.rb | 7 +-- lib/templates/config.yaml | 58 ++++++++++++++++++++-- 8 files changed, 78 insertions(+), 205 deletions(-) diff --git a/Rakefile b/Rakefile index 4c9e00a..c00a13f 100644 --- a/Rakefile +++ b/Rakefile @@ -10,7 +10,7 @@ begin gem.email = "helma@in-silico.ch" gem.homepage = "http://github.com/helma/opentox-ruby-api-wrapper" gem.authors = ["Christoph Helma"] - ["sinatra", "rest-client", "rack", "rack-contrib", "rack-flash", "emk-sinatra-url-for", "cehoffman-sinatra-respond_to", "dm-more", "dm-core", "sinatra-static-assets"].each do |dep| + ["sinatra", "rest-client", "rack", "rack-contrib", "rack-flash", "sinatra-url-for", "sinatra-respond_to", "dm-more", "dm-core", "sinatra-static-assets","tmail"].each do |dep| gem.add_dependency dep end ['cucumber','jeweler'].each do |dep| diff --git a/lib/algorithm.rb b/lib/algorithm.rb index b573ea3..cc06032 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -22,7 +22,7 @@ module OpenTox LOGGER.debug params LOGGER.debug File.basename(__FILE__) + ": creating model" LOGGER.debug File.join(@@config[:services]["opentox-algorithm"], "lazar") - #resource = RestClient::Resource.new(File.join(@@config[:services]["opentox-algorithm"], "lazar"), :user => @@users[:users].keys[0], :password => @@users[:users].values[0], :content_type => "text/x-yaml") + #resource = RestClient::Resource.new(File.join(@@config[:services]["opentox-algorithm"], "lazar"), :user => @@users[:users].keys[0], :password => @@users[:users].values[0], :content_type => "application/x-yaml") resource = RestClient::Resource.new(File.join(@@config[:services]["opentox-algorithm"], "lazar"), :user => @@users[:users].keys[0], :password => @@users[:users].values[0], :content_type => "application/x-yaml") #@uri = resource.post(:dataset_uri => params[:dataset_uri], :feature_uri => params[:feature_uri], :feature_generation_uri => File.join(@@config[:services]["opentox-algorithm"], "fminer")).chomp @uri = resource.post(:dataset_uri => params[:dataset_uri], :prediction_feature => params[:prediction_feature], :feature_generation_uri => File.join(@@config[:services]["opentox-algorithm"], "fminer")).body.chomp diff --git a/lib/config/config_ru.rb b/lib/config/config_ru.rb index 8470f0e..09fba3b 100644 --- a/lib/config/config_ru.rb +++ b/lib/config/config_ru.rb @@ -18,8 +18,11 @@ set :raise_errors, true end use Rack::ShowExceptions -#use Rack::MailExceptions do |mail| -# mail.to '...@example.com' -# mail.subject '[ERROR] %s' -# mail.smtp MAIL -#end +#if MAIL +# use Rack::MailExceptions do |mail| +# mail.to 'helma@in-silico.ch' +# mail.subject '[ERROR] %s' +# mail.from "toxcreate@in-silico.ch" +# mail.smtp MAIL +# end +#end diff --git a/lib/dataset.rb b/lib/dataset.rb index e21ccce..17f0997 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -15,16 +15,17 @@ module OpenTox def self.find(uri, accept_header=nil) unless accept_header - if uri.match(@@config[:services]["opentox-dataset"]) || uri=~ /188.40.32.88/ || uri =~ /informatik/ - accept_header = 'text/x-yaml' + #if uri.match(@@config[:services]["opentox-dataset"]) || uri=~ /188.40.32.88/ || uri =~ /informatik/ + if !@@config[:accept_headers]["opentox-dataset"].grep(/yaml/).empty? + accept_header = 'application/x-yaml' else accept_header = "application/rdf+xml" end end case accept_header - when "text/x-yaml" - d = YAML.load RestClientWrapper.get(uri.to_s.strip, :accept => 'text/x-yaml').to_s + when "application/x-yaml" + d = YAML.load RestClientWrapper.get(uri.to_s.strip, :accept => 'application/x-yaml').to_s d.uri = uri unless d.uri when "application/rdf+xml" owl = OpenTox::Owl.from_uri(uri.to_s.strip, "Dataset") @@ -172,122 +173,9 @@ module OpenTox @features.uniq! @compounds.uniq! - OpenTox::RestClientWrapper.post(@@config[:services]["opentox-dataset"],{:content_type => "text/x-yaml"},self.to_yaml).strip + OpenTox::RestClientWrapper.post(@@config[:services]["opentox-dataset"],{:content_type => "application/x-yaml"},self.to_yaml).strip end -=begin - # create/add to entry from uris or Redland::Resources - def add(compound,feature,value) - compound = self.find_or_create_compound compound unless compound.class == Redland::Resource - feature = self.find_or_create_feature feature unless feature.class == Redland::Resource - data_entry = @model.subject OT['compound'], compound - if data_entry.nil? - data_entry = @model.create_resource - dataset = @model.subject(RDF['type'],OT[self.owl_class]) - @model.add dataset, OT['dataEntry'], data_entry - @model.add data_entry, RDF['type'], OT["DataEntry"] - @model.add data_entry, OT['compound'], compound - end - values = @model.create_resource - @model.add data_entry, OT['values'], values - @model.add values, RDF['type'], OT['FeatureValue'] - @model.add values, OT['feature'], feature - @model.add values, OT['value'], value.to_s - end - - def add_tuple(compound,tuple) - compound = self.find_or_create_compound compound unless compound.class == Redland::Resource - data_entry = @model.subject OT['compound'], compound - if data_entry.nil? - data_entry = @model.create_resource - dataset = @model.subject(RDF['type'],OT[self.owl_class]) - @model.add dataset, OT['dataEntry'], data_entry - @model.add data_entry, RDF['type'], OT["DataEntry"] - @model.add data_entry, OT['compound'], compound - end - @model.add data_entry, OT['values'], tuple - end - - def create_tuple(feature,t) - feature = self.find_or_create_feature feature unless feature.class == Redland::Resource - tuple = @model.create_resource - @model.add tuple, RDF['type'], OT["Tuple"] - @model.add tuple, OT['feature'], feature - t.each do |name,value| - f = self.find_or_create_feature name unless name.class == Redland::Resource - complex_value = @model.create_resource - feature = self.find_or_create_feature(name) - @model.add tuple, OT['complexValue'], complex_value - @model.add complex_value, RDF['type'], OT["FeatureValue"] - @model.add complex_value, OT['feature'], f - @model.add complex_value, OT['value'], value.to_s - end - - tuple - end - - # find or create a new compound and return the resource - def find_or_create_compound(uri) - compound = @model.subject(DC["identifier"], uri) - if compound.nil? - compound = @model.create_resource(uri) - @model.add compound, RDF['type'], OT["Compound"] - @model.add compound, DC["identifier"], uri - end - compound - end - - # find or create a new feature and return the resource - def find_or_create_feature(uri) - feature = @model.subject(DC["identifier"], uri) - if feature.nil? - feature = @model.create_resource(uri) - @model.add feature, RDF['type'], OT["Feature"] - @model.add feature, DC["identifier"], uri - @model.add feature, DC["title"], File.basename(uri).split(/#/)[1] - @model.add feature, DC['source'], uri - end - feature - end - - def self.create(data, content_type = 'application/rdf+xml') - resource = RestClient::Resource.new(@@config[:services]["opentox-dataset"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - uri = resource.post data, :content_type => content_type - dataset = Dataset.new - dataset.read uri.chomp.to_s - dataset - end - - def features - features = [] - @model.subjects(RDF['type'], OT["Feature"]).each do |feature_node| - features << @model.object(feature_node, DC["identifier"])# - end - features - end - - def compounds - compounds = [] - @model.subjects(RDF['type'], OT["Compound"]).each do |compound_node| - compounds << @model.object(compound_node, DC["identifier"]).to_s - end - compounds - end - - # Delete a dataset - def delete - resource = RestClient::Resource.new(@uri, :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.delete - end - - def to_owl - end - - def from_owl - end - -=end - def init_dirty_features(owl) @dirty_features = @features @owl = owl diff --git a/lib/environment.rb b/lib/environment.rb index 44efd6e..c112669 100644 --- a/lib/environment.rb +++ b/lib/environment.rb @@ -40,8 +40,8 @@ if @@config[:database] end end -# mail -#load File.join config_dir,"mail.rb" +# mail for error messages +load File.join config_dir,"mail.rb" if File.exists?(File.join config_dir,"mail.rb") # hack: store sinatra in global var to make url_for and halt methods accessible before {$sinatra = self unless $sinatra} @@ -136,3 +136,7 @@ XML = Redland::Namespace.new 'http://www.w3.org/2001/XMLSchema#' # Regular expressions for parsing classification data TRUE_REGEXP = /^(true|active|$1^)/ FALSE_REGEXP = /^(false|inactive|$0^)/ + +# Task durations +DEFAULT_TASK_MAX_DURATION = @@config[:default_task_max_duration] +EXTERNAL_TASK_MAX_DURATION = @@config[:external_task_max_duration] diff --git a/lib/model.rb b/lib/model.rb index 74bb598..3fb713f 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -102,79 +102,12 @@ module OpenTox def save @features.uniq! resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.post(self.to_yaml, :content_type => "text/x-yaml").chomp.to_s + resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s end def self.find_all RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n") end -=begin - - # Predict a compound - def predict(compound) - # nicht absichern?? - resource = RestClient::Resource.new(@uri, :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.post(:compound_uri => compound.uri) - end - - def self.base_uri - File.join @@config[:services]["opentox-model"],'lazar' - end - - def self.create(data) - resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.post(data, :content_type => "text/x-yaml").chomp.to_s - end - - def delete - resource = RestClient::Resource.new(self.uri, :user => @@users[:users].keys[0], :password => @@users[:users].values[0]) - resource.delete - #RestClient.delete @uri if @uri - #RestClient.delete model.task_uri if model.task_uri - end - -# def self.create(task) -# @uri = RestClient.post(@@config[:services]["opentox-model"], :task_uri => task.uri) -# end - -# def yaml=(data) -# RestClient.put(@@uri, data, :content_type => "text/x-yaml").to_s -# end - - def endpoint - YAML.load(RestClient.get(uri))[:endpoint] - end - - def algorithm=(algorithm) - me = @model.subject(RDF['type'],OT[self.owl_class]) - @model.add me, OT['algorithm'], Redland::Uri.new(algorithm) # untyped individual comes from this line, why?? - @model.add Redland::Uri.new(algorithm), RDF['type'], OT['Algorithm'] - end - - def trainingDataset=(trainingDataset) - me = @model.subject(RDF['type'],OT[self.owl_class]) - @model.add me, OT['trainingDataset'], Redland::Uri.new(trainingDataset) # untyped individual comes from this line, why?? - @model.add Redland::Uri.new(trainingDataset), RDF['type'], OT['Dataset'] - end - - def dependentVariables=(dependentVariables) - me = @model.subject(RDF['type'],OT[self.owl_class]) - @model.add me, OT['dependentVariables'], Redland::Uri.new(dependentVariables) # untyped individual comes from this line, why?? - @model.add Redland::Uri.new(dependentVariables), RDF['type'], OT['Feature'] - end - - def independentVariables=(independentVariables) - me = @model.subject(RDF['type'],OT[self.owl_class]) - @model.add me, OT['independentVariables'], Redland::Uri.new(independentVariables) # untyped individual comes from this line, why?? - @model.add Redland::Uri.new(independentVariables), RDF['type'], OT['Feature'] - end - - def predictedVariables=(predictedVariables) - me = @model.subject(RDF['type'],OT[self.owl_class]) - @model.add me, OT['predictedVariables'], Redland::Uri.new(predictedVariables) # untyped individual comes from this line, why?? - @model.add Redland::Uri.new(predictedVariables), RDF['type'], OT['Feature'] - end -=end end end end diff --git a/lib/task.rb b/lib/task.rb index 8f87f45..66d39f4 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -1,8 +1,5 @@ LOGGER.progname = File.expand_path(__FILE__) -DEFAULT_TASK_MAX_DURATION = 3600 -EXTERNAL_TASK_MAX_DURATION = 1200 - $self_task=nil module OpenTox @@ -40,14 +37,14 @@ module OpenTox end def reload - result = RestClientWrapper.get(uri, {:accept => 'application/rdf+xml'}, false)#'text/x-yaml'}) + result = RestClientWrapper.get(uri, {:accept => 'application/rdf+xml'}, false)#'application/x-yaml'}) @http_code = result.code reload_from_data(result, result.content_type, uri) end def reload_from_data( data, content_type, base_uri ) case content_type - when /text\/x-yaml/ + when /yaml/ task = YAML.load data TASK_ATTRIBS.each do |a| raise "task yaml data invalid, key missing: "+a.to_s unless task.has_key?(a) diff --git a/lib/templates/config.yaml b/lib/templates/config.yaml index ad7c4ab..e8084bd 100644 --- a/lib/templates/config.yaml +++ b/lib/templates/config.yaml @@ -1,13 +1,25 @@ # Example configuration for OpenTox, please adjust to your settings # +# Database setup: +# +# Example MySql: +# +# :database: +# :adapter: mysql +# :database: production +# :username: production +# :password: your_password +# :host: localhost +# # Example 1: Using external test services # # :services: -# opentox-compound: "http://webservices.in-silico.ch/test/compound/" -# opentox-dataset: "http://webservices.in-silico.ch/test/dataset/" -# opentox-algorithm: "http://webservices.in-silico.ch/test/algorithm/" -# opentox-model: "http://webservices.in-silico.ch/test/model/" -# opentox-task: "http://webservices.in-silico.ch/test/task/" +# opentox-compound: "http://webservices.in-silico.ch/compound/" +# opentox-dataset: "http://webservices.in-silico.ch/dataset/" +# opentox-algorithm: "http://webservices.in-silico.ch/algorithm/" +# opentox-model: "http://webservices.in-silico.ch/model/" +# opentox-task: "http://webservices.in-silico.ch/task/" +# opentox-validation: "http://opentox.informatik.uni-freiburg.de/validation/" # # Example 2: Using local services # :base_dir: /home/ch/webservices @@ -18,3 +30,39 @@ # opentox-algorithm: "http://localhost:4002/" # opentox-model: "http://localhost:4003/" # opentox-task: "http://localhost:4004/" +# opentox-validation: "http://opentox.informatik.uni-freiburg.de/validation/" +# +# Accept headers: +# +# :accept_headers: +# opentox-compound: +# - "chemical/x-daylight-smiles" +# - "chemical/x-inchi" +# - "chemical/x-mdl-sdfile" +# - "image/gif" +# - "text/plain" +# opentox-dataset: +# - "application/x-yaml" +# - "text/x-yaml" +# - "application/rdf+xml" +# opentox-algorithm: +# - "application/x-yaml" +# - "text/x-yaml" +# - "application/rdf+xml" +# opentox-model: +# - "application/x-yaml" +# - "text/x-yaml" +# - "application/rdf+xml" +# opentox-task: +# - "application/x-yaml" +# - "text/x-yaml" +# - "application/rdf+xml" +# opentox-validation: +# - "application/x-yaml" +# - "text/x-yaml" +# - "application/rdf+xml" + +# Timeouts: +# +# :default_task_max_duration: 3600 +# :external_task_max_duration: 3600 -- cgit v1.2.3