From ddb83b4302e8628b333402d24e3e05fc90b3faef Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Wed, 10 Jul 2013 14:36:39 +0200 Subject: subjectids partially removed --- lib/algorithm.rb | 8 ++++---- lib/authorization.rb | 11 +++++------ lib/compound.rb | 17 ++++++++--------- lib/dataset.rb | 14 +++++++------- lib/model.rb | 16 +++------------- lib/opentox-client.rb | 10 +++++++++- lib/opentox.rb | 43 ++++++++++++++++++++----------------------- lib/overwrite.rb | 8 ++++---- lib/rest-client-wrapper.rb | 2 +- lib/task.rb | 20 ++++---------------- 10 files changed, 65 insertions(+), 84 deletions(-) (limited to 'lib') diff --git a/lib/algorithm.rb b/lib/algorithm.rb index 5cffa88..a725141 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -9,7 +9,7 @@ module OpenTox # @param [optional,Boolean] wait set to false if method should return a task uri instead of the algorithm result # @return [String] URI of new resource (dataset, model, ...) def run params=nil, wait=true - uri = RestClientWrapper.post @uri, params, { :content_type => "text/uri-list", :subjectid => @subjectid} + uri = RestClientWrapper.post @uri, params, { :content_type => "text/uri-list", :subjectid => SUBJECTID} wait_for_task uri if wait end @@ -29,11 +29,11 @@ module OpenTox when "Array" klasses = compounds.collect{|c| c.class}.uniq bad_request_error "First argument contains objects with a different class than OpenTox::Compound or OpenTox::Dataset #{klasses.inspect}" unless klasses.size == 1 and klasses.first == Compound - JSON.parse(Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s), SUBJECTID).run(:compound_uri => compounds.collect{|c| c.uri}, :descriptors => descriptors)) + JSON.parse(Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s)).run(:compound_uri => compounds.collect{|c| c.uri}, :descriptors => descriptors)) when "OpenTox::Compound" - JSON.parse(Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s), SUBJECTID).run(:compound_uri => compounds.uri, :descriptors => descriptors)) + JSON.parse(Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s)).run(:compound_uri => compounds.uri, :descriptors => descriptors)) when "OpenTox::Dataset" - task_uri = Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s), SUBJECTID).run(:dataset_uri => compounds.uri, :descriptors => descriptors) + task_uri = Descriptor.new(File.join(self.service_uri, "descriptor", descriptor.to_s)).run(:dataset_uri => compounds.uri, :descriptors => descriptors) Dataset.new(wait_for_task task_uri) else bad_request_error "First argument contains objects with a different class than OpenTox::Compound or OpenTox::Dataset" diff --git a/lib/authorization.rb b/lib/authorization.rb index 4b63cef..4a193ae 100644 --- a/lib/authorization.rb +++ b/lib/authorization.rb @@ -1,12 +1,11 @@ module OpenTox + if defined?($aa) and $aa[:uri] AA = $aa[:uri] - SUBJECTID = OpenTox::Authorization.authenticate($aa[:user],$aa[:password]) - unauthorized_error "Failed to authenticate user \"#{$aa[:user]}\"." unless OpenTox::Authorization.is_token_valid(SUBJECTID) else AA = "https://opensso.in-silico.ch" #if not set in .opentox/conf/[SERVICE].rb - SUBJECTID = nil end + #Module for Authorization and Authentication #@example Authentication # require "opentox-client" @@ -70,11 +69,11 @@ module OpenTox def self.authenticate(user, pw) return nil if !AA begin - out = RestClientWrapper.post("#{AA}/auth/authenticate",{:username=>user, :password => pw}).sub("token.id=","").sub("\n","") + out = RestClientWrapper.post("#{AA}/auth/authenticate",{:username=>user, :password => pw},{:subjectid => ""}).sub("token.id=","").sub("\n","") return out rescue - resource_not_found_error "#{out.inspect}" - return nil + bad_request_error "Authentication failed #{out.inspect}" + #return nil # does not return after throwing an error end end diff --git a/lib/compound.rb b/lib/compound.rb index 82f0d73..9d99ae9 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -1,4 +1,3 @@ -#require "openbabel" CACTUS_URI="http://cactus.nci.nih.gov/chemical/structure/" module OpenTox @@ -15,22 +14,22 @@ module OpenTox # compound = OpenTox::Compound.from_smiles("c1ccccc1") # @param [String] smiles Smiles string # @return [OpenTox::Compound] Compound - def self.from_smiles smiles, subjectid=nil - Compound.new RestClientWrapper.post(service_uri, smiles, {:content_type => 'chemical/x-daylight-smiles', :subjectid => subjectid}) + def self.from_smiles smiles + Compound.new RestClientWrapper.post(service_uri, smiles, {:content_type => 'chemical/x-daylight-smiles'}) end # Create a compound from inchi string # @param inchi [String] smiles InChI string # @return [OpenTox::Compound] Compound - def self.from_inchi inchi, subjectid=nil - Compound.new RestClientWrapper.post(service_uri, inchi, {:content_type => 'chemical/x-inchi', :subjectid => subjectid}) + def self.from_inchi inchi + Compound.new RestClientWrapper.post(service_uri, inchi, {:content_type => 'chemical/x-inchi'}) end # Create a compound from sdf string # @param sdf [String] smiles SDF string # @return [OpenTox::Compound] Compound - def self.from_sdf sdf, subjectid=nil - Compound.new RestClientWrapper.post(service_uri, sdf, {:content_type => 'chemical/x-mdl-sdfile', :subjectid => subjectid}) + def self.from_sdf sdf + Compound.new RestClientWrapper.post(service_uri, sdf, {:content_type => 'chemical/x-mdl-sdfile'}) end # Create a compound from name. Relies on an external service for name lookups. @@ -38,9 +37,9 @@ module OpenTox # compound = OpenTox::Compound.from_name("Benzene") # @param name [String] can be also an InChI/InChiKey, CAS number, etc # @return [OpenTox::Compound] Compound - def self.from_name name, subjectid=nil + def self.from_name name @inchi = RestClientWrapper.get File.join(CACTUS_URI,URI.escape(name),"stdinchi") - Compound.new RestClientWrapper.post(service_uri, @inchi, {:content_type => 'chemical/x-inchi', :subjectid => subjectid}) + Compound.new RestClientWrapper.post(service_uri, @inchi, {:content_type => 'chemical/x-inchi'}) end # Get InChI diff --git a/lib/dataset.rb b/lib/dataset.rb index 354b443..c2ff833 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -7,7 +7,7 @@ module OpenTox attr_writer :features, :compounds, :data_entries - def initialize uri=nil, subjectid=nil + def initialize uri=nil, subjectid=SUBJECTID super uri, subjectid @features = [] @compounds = [] @@ -35,7 +35,7 @@ module OpenTox if @features.empty? or force_update uri = File.join(@uri,"features") uris = RestClientWrapper.get(uri,{},{:accept => "text/uri-list", :subjectid => @subjectid}).split("\n") # ordered datasets return ordered features - @features = uris.collect{|uri| Feature.new(uri,@subjectid)} + @features = uris.collect{|uri| Feature.new(uri)} end @features end @@ -45,7 +45,7 @@ module OpenTox if @compounds.empty? or force_update uri = File.join(@uri,"compounds") uris = RestClientWrapper.get(uri,{},{:accept => "text/uri-list", :subjectid => @subjectid}).split("\n") # ordered datasets return ordered compounds - @compounds = uris.collect{|uri| Compound.new(uri,@subjectid)} + @compounds = uris.collect{|uri| Compound.new(uri)} end @compounds end @@ -120,7 +120,7 @@ module OpenTox prediction_feature = nil confidence_feature = nil metadata[RDF::OT.predictedVariables].each do |uri| - feature = OpenTox::Feature.new uri, @subjectid + feature = OpenTox::Feature.new uri case feature.title when /prediction$/ prediction_feature = feature @@ -214,7 +214,7 @@ module OpenTox query = RDF::Query.new({ :uri => { RDF.type => RDF::OT.Compound } }) @compounds = query.execute(@rdf).collect { |solution| OpenTox::Compound.new solution.uri } query = RDF::Query.new({ :uri => { RDF.type => RDF::OT.Feature } }) - @features = query.execute(@rdf).collect { |solution| OpenTox::Feature.new solution.uri, @subjectid } + @features = query.execute(@rdf).collect { |solution| OpenTox::Feature.new solution.uri } @compounds.each_with_index do |c,i| @features.each_with_index do |f,j| end @@ -310,11 +310,11 @@ module OpenTox # @param feats [Array] features objects # @param metadata [Hash] # @return [OpenTox::Dataset] - def split( compound_indices, feats, metadata, subjectid=nil) + def split( compound_indices, feats, metadata) bad_request_error "Dataset.split : Please give compounds as indices" if compound_indices.size==0 or !compound_indices[0].is_a?(Fixnum) bad_request_error "Dataset.split : Please give features as feature objects (given: #{feats})" if feats!=nil and feats.size>0 and !feats[0].is_a?(OpenTox::Feature) - dataset = OpenTox::Dataset.new(nil, subjectid) + dataset = OpenTox::Dataset.new dataset.metadata = metadata dataset.features = (feats ? feats : self.features) compound_indices.each do |c_idx| diff --git a/lib/model.rb b/lib/model.rb index c2914eb..356f4e4 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -1,21 +1,11 @@ module OpenTox module Model -=begin - # Run a model with parameters - # @param params [Hash] Parameters for OpenTox model - # @param wait [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly - # @return [text/uri-list] Task or resource URI - def run params=nil, wait=true - uri = RestClientWrapper.post @uri, params, { :content_type => "text/uri-list", :subjectid => @subjectid} - wait_for_task uri if wait - end -=end - def feature_type # CH: subjectid is a object variable, no need to pass it as a parameter + def feature_type unless @feature_type bad_request_error "Cannot determine feature type, dependent variable missing in model #{@uri}" unless metadata[RDF::OT.dependentVariables] - @feature_type = OpenTox::Feature.new( metadata[RDF::OT.dependentVariables][0], @subjectid ).feature_type + @feature_type = OpenTox::Feature.new( metadata[RDF::OT.dependentVariables][0]).feature_type end @feature_type end @@ -33,7 +23,7 @@ module OpenTox private def load_predicted_variables metadata[RDF::OT.predictedVariables].each do |f| - feat = OpenTox::Feature.new( f, @subjectid ) + feat = OpenTox::Feature.new( f) if feat.title =~ /confidence/ @predicted_confidence = f else diff --git a/lib/opentox-client.rb b/lib/opentox-client.rb index f25f05a..77c6eae 100644 --- a/lib/opentox-client.rb +++ b/lib/opentox-client.rb @@ -26,7 +26,6 @@ RDF::TB = RDF::Vocabulary.new "http://onto.toxbank.net/api/" RDF::ISA = RDF::Vocabulary.new "http://onto.toxbank.net/isa/" RDF::OWL = RDF::Vocabulary.new "http://www.w3.org/2002/07/owl#" -#CLASSES = ["Generic", "Compound", "Feature", "Dataset", "Algorithm", "Model", "Validation", "Task", "Investigation"] CLASSES = ["Compound", "Feature", "Dataset", "Validation", "Task", "Investigation"] RDF_FORMATS = [:rdfxml,:ntriples,:turtle] @@ -51,3 +50,12 @@ FALSE_REGEXP = /^(false|inactive|0|0.0|low tox|deactivating|non-carcinogen|non-m "validation.rb" ].each{ |f| require_relative f } +module OpenTox + if defined?($aa) and $aa[:uri] + SUBJECTID = OpenTox::Authorization.authenticate($aa[:user],$aa[:password]) + unauthorized_error "Failed to authenticate user \"#{$aa[:user]}\"." unless OpenTox::Authorization.is_token_valid(SUBJECTID) + else + SUBJECTID = nil + end +end + diff --git a/lib/opentox.rb b/lib/opentox.rb index ec18c0f..e6fcf63 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -12,11 +12,9 @@ module OpenTox # Create a new OpenTox object # @param uri [optional,String] URI - # @param subjectid [optional,String] # @return [OpenTox] OpenTox object - def initialize uri=nil, subjectid=nil + def initialize uri=nil, subjectid=SUBJECTID @rdf = RDF::Graph.new - @subjectid = subjectid @metadata = {} @parameters = [] uri ? @uri = uri.to_s.chomp : @uri = File.join(service_uri, SecureRandom.uuid) @@ -25,7 +23,7 @@ module OpenTox # Object metadata (lazy loading) # @return [Hash] Object metadata def metadata force_update=false - if (@metadata.nil? or @metadata.empty? or force_update) and URI.accessible? @uri, @subjectid + if (@metadata.nil? or @metadata.empty? or force_update) and URI.accessible? @uri get if @rdf.nil? or @rdf.empty? or force_update # return values as plain strings instead of RDF objects @metadata = @rdf.to_hash[RDF::URI.new(@uri)].inject({}) { |h, (predicate, values)| h[predicate] = values.collect{|v| v.to_s}; h } @@ -52,7 +50,7 @@ module OpenTox # {http://opentox.org/dev/apis/api-1.2/interfaces OpenTox API} # @return [Hash] Object parameters def parameters force_update=false - if (@parameters.empty? or force_update) and URI.accessible? @uri, @subjectid + if (@parameters.empty? or force_update) and URI.accessible? @uri get if @rdf.empty? or force_update params = {} query = RDF::Query.new({ @@ -81,10 +79,10 @@ module OpenTox # @param [String,optional] mime_type def get mime_type="text/plain" bad_request_error "Mime type #{mime_type} is not supported. Please use 'text/plain' (default) or 'application/rdf+xml'." unless mime_type == "text/plain" or mime_type == "application/rdf+xml" - response = RestClientWrapper.get(@uri,{},{:accept => mime_type, :subjectid => @subjectid}) + response = RestClientWrapper.get(@uri,{},{:accept => mime_type,:subjectid => @subjectid}) if URI.task?(response) uri = wait_for_task response - response = RestClientWrapper.get(uri,{},{:accept => mime_type, :subjectid => @subjectid}) + response = RestClientWrapper.get(uri,{},{:accept => mime_type,:subjectid => @subjectid}) end parse_ntriples response if mime_type == "text/plain" parse_rdfxml response if mime_type == "application/rdf+xml" @@ -100,15 +98,15 @@ module OpenTox when 'application/rdf+xml' body = self.to_rdfxml end - Authorization.check_policy(@uri, @subjectid) if $aa[:uri] - uri = RestClientWrapper.post @uri.to_s, body, { :content_type => mime_type, :subjectid => @subjectid} + Authorization.check_policy(@uri, SUBJECTID) if $aa[:uri] + uri = RestClientWrapper.post @uri.to_s, body, { :content_type => mime_type,:subjectid => @subjectid} wait ? wait_for_task(uri) : uri end # Save object at webservice (replace or create object) def put wait=true, mime_type="text/plain" bad_request_error "Mime type #{mime_type} is not supported. Please use 'text/plain' (default) or 'application/rdf+xml'." unless mime_type == "text/plain" or mime_type == "application/rdf+xml" - @metadata[RDF::OT.created_at] = DateTime.now unless URI.accessible? @uri, @subjectid + @metadata[RDF::OT.created_at] = DateTime.now unless URI.accessible? @uri #@metadata[RDF::DC.modified] = DateTime.now case mime_type when 'text/plain' @@ -116,14 +114,14 @@ module OpenTox when 'application/rdf+xml' body = self.to_rdfxml end - uri = RestClientWrapper.put @uri.to_s, body, { :content_type => mime_type, :subjectid => @subjectid} + uri = RestClientWrapper.put @uri.to_s, body, { :content_type => mime_type,:subjectid => @subjectid} wait ? wait_for_task(uri) : uri end # Delete object at webservice - def delete subjectid=nil - RestClientWrapper.delete(@uri.to_s,nil,{:subjectid => @subjectid}) - Authorization.delete_policies_from_uri(@uri, @subjectid) if $aa[:uri] + def delete + RestClientWrapper.delete(@uri.to_s,{},{:subjectid => @subjectid}) + Authorization.delete_policies_from_uri(@uri, SUBJECTID) if $aa[:uri] end def service_uri @@ -210,26 +208,25 @@ module OpenTox c = Class.new do include OpenTox - def self.all subjectid=nil - uris = RestClientWrapper.get(service_uri, {}, :accept => 'text/uri-list', :subjectid => subjectid).split("\n").compact - uris.collect{|uri| self.new(uri, subjectid)} + def self.all + uris = RestClientWrapper.get(service_uri, {},{:accept => 'text/uri-list',:subjectid=>@subjectid}).split("\n").compact + uris.collect{|uri| self.new(uri)} end #@example fetching a model # OpenTox::Model.find() -> model-object - def self.find uri, subjectid=nil - URI.accessible?(uri, subjectid) ? self.new(uri, subjectid) : nil + def self.find uri, subjectid=SUBJECTID + URI.accessible?(uri) ? self.new(uri) : nil end - def self.create metadata, subjectid=nil + def self.create metadata, subjectid=SUBJECTID object = self.new nil, subjectid object.metadata = metadata object.put object end - def self.find_or_create metadata, subjectid=nil - puts metadata.inspect + def self.find_or_create metadata, subjectid=SUBJECTID sparql = "SELECT DISTINCT ?s WHERE { " metadata.each do |predicate,objects| unless [RDF::DC.date,RDF::DC.modified,RDF::DC.description].include? predicate # remove dates and description (strange characters in description may lead to SPARQL errors) @@ -245,7 +242,7 @@ module OpenTox end end sparql << "}" - uris = RestClientWrapper.get(service_uri,{:query => sparql},{:accept => "text/uri-list", :subjectid => subjectid}).split("\n") + uris = RestClientWrapper.get(service_uri,{:query => sparql},{:accept => "text/uri-list",:subjectid=>subjectid}).split("\n") if uris.empty? self.create metadata, subjectid else diff --git a/lib/overwrite.rb b/lib/overwrite.rb index fc050ce..c6bf02a 100644 --- a/lib/overwrite.rb +++ b/lib/overwrite.rb @@ -84,20 +84,20 @@ module URI uri =~ /task/ and URI.valid? uri end - def self.dataset? uri, subjectid=nil + def self.dataset? uri, subjectid=OpenTox::SUBJECTID uri =~ /dataset/ and URI.accessible? uri, subjectid end - def self.model? uri, subjectid=nil + def self.model? uri, subjectid=OpenTox::SUBJECTID uri =~ /model/ and URI.accessible? uri, subjectid end - def self.ssl? uri, subjectid=nil + def self.ssl? uri, subjectid=OpenTox::SUBJECTID URI.parse(uri).instance_of? URI::HTTPS end # @return [Boolean] checks if resource exists by making a HEAD-request - def self.accessible?(uri, subjectid=nil) + def self.accessible?(uri, subjectid=OpenTox::SUBJECTID) parsed_uri = URI.parse(uri + (subjectid ? "?subjectid=#{CGI.escape subjectid}" : "")) http_code = URI.task?(uri) ? 600 : 400 http = Net::HTTP.new(parsed_uri.host, parsed_uri.port) diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index f3e6300..2de9377 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -17,7 +17,7 @@ module OpenTox # check input bad_request_error "Headers are not a hash: #{headers.inspect}", uri unless headers==nil or headers.is_a?(Hash) - @subjectid = headers[:subjectid] ? headers[:subjectid] : nil + headers[:subjectid] ||= OpenTox::SUBJECTID bad_request_error "Invalid URI: '#{uri}'", uri unless URI.valid? uri #resource_not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri) # make sure that no header parameters are set in the payload diff --git a/lib/task.rb b/lib/task.rb index 3cc05c0..d642f48 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -10,7 +10,7 @@ module OpenTox super true # always update metadata end - def self.run(description, creator=nil, subjectid=nil) + def self.run(description, creator=nil, subjectid=SUBJECTID) task = Task.new nil, subjectid task[RDF::OT.created_at] = DateTime.now @@ -21,21 +21,9 @@ module OpenTox pid = fork do begin task.completed yield - #result_uri = yield - #task.completed result_uri rescue -=begin - #unless $!.is_a?(RuntimeError) # PENDING: only runtime Errors are logged when raised - msg = "\nTask ERROR\n"+ - "task description: #{task[RDF::DC.description]}\n"+ - "task uri: #{$!.class.to_s}\n"+ - "error msg: #{$!.message}\n"+ - "error backtrace:\n#{$!.backtrace[0..cut_index].join("\n")}\n" - $logger.error msg - #end -=end if $!.respond_to? :to_ntriples - RestClientWrapper.put(File.join(task.uri,'Error'),:errorReport => $!.to_ntriples,:content_type => 'text/plain') + RestClientWrapper.put(File.join(task.uri,'Error'),{:errorReport => $!.to_ntriples},{:content_type => 'text/plain', :subjectid => task.subjectid}) else cut_index = $!.backtrace.find_index{|line| line.match /gems\/sinatra/} cut_index = -1 unless cut_index @@ -53,7 +41,7 @@ module OpenTox nt = RDF::Writer.for(:ntriples).buffer do |writer| @rdf.each{|statement| writer << statement} end - RestClientWrapper.put(File.join(task.uri,'Error'),:errorReport => nt,:content_type => 'text/plain') + RestClientWrapper.put(File.join(task.uri,'Error'),{:errorReport => nt},{:content_type => 'text/plain', :subjectid => task.subjectid}) end task.kill end @@ -119,7 +107,7 @@ module OpenTox end def code - RestClientWrapper.head(@uri).code.to_i + RestClientWrapper.head(@uri,{},:subjectid => @subjectid).code.to_i end # get only header for status requests -- cgit v1.2.3