diff options
author | Christoph Helma <helma@in-silico.ch> | 2013-05-29 15:12:52 +0200 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2013-05-29 15:12:52 +0200 |
commit | f2ef6216ef785656d799c880f22013f6c7e900e7 (patch) | |
tree | fa7d72d434c9cb70483222aa4bc7f580e0bd8fa9 /lib | |
parent | 0f770837cbfe17fe17fc86aa0d360c55e281c7f7 (diff) | |
parent | ef122484598a8979de915344cdeff475cff752c5 (diff) |
Merge branch 'development' of github.com:opentox/opentox-client into development
Conflicts:
lib/compound.rb
Diffstat (limited to 'lib')
-rw-r--r-- | lib/algorithm.rb | 2 | ||||
-rw-r--r-- | lib/authorization.rb | 39 | ||||
-rw-r--r-- | lib/compound.rb | 101 | ||||
-rw-r--r-- | lib/dataset.rb | 10 | ||||
-rw-r--r-- | lib/error.rb | 2 | ||||
-rw-r--r-- | lib/opentox-client.rb | 2 | ||||
-rw-r--r-- | lib/opentox.rb | 4 | ||||
-rw-r--r-- | lib/otlogger.rb | 2 | ||||
-rw-r--r-- | lib/overwrite.rb | 16 | ||||
-rw-r--r-- | lib/rest-client-wrapper.rb | 1 | ||||
-rw-r--r-- | lib/task.rb | 2 |
11 files changed, 96 insertions, 85 deletions
diff --git a/lib/algorithm.rb b/lib/algorithm.rb index 455e9ad..530296a 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -5,7 +5,7 @@ module OpenTox # Execute algorithm with parameters, please consult the OpenTox API and the webservice documentation for acceptable parameters # @param [optional,Hash] params Algorithm parameters - # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly + # @param [optional,Boolean] set to true if method should wait for task 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} diff --git a/lib/authorization.rb b/lib/authorization.rb index 5a846c9..469c16c 100644 --- a/lib/authorization.rb +++ b/lib/authorization.rb @@ -1,10 +1,10 @@ module OpenTox AA = $aa[:uri] if defined? $aa - AA ||= "https://opensso.in-silico.ch" #if not set in .opentox/conf/[application]/[test].rb + AA ||= "https://opensso.in-silico.ch" #if not set in .opentox/conf/[SERVICE].rb #Module for Authorization and Authentication #@example Authentication # require "opentox-client" - # OpenTox::Authorization::AA = "https://opensso.in-silico.ch" #if not set in .opentox/conf/[environment].yaml + # OpenTox::Authorization::AA = "https://opensso.in-silico.ch" #if not set in .opentox/conf/[SERVICE].rb # subjectid = OpenTox::Authorization.authenticate("username", "password") #@see http://www.opentox.org/dev/apis/api-1.2/AA OpenTox A&A API 1.2 specification @@ -337,31 +337,24 @@ module OpenTox end private - def self.free_uri?(uri, request_method) - if $aa[:free_uris] - $aa[:free_uris].each do |request_methods,uris| - if request_methods and uris and request_methods.include?(request_method.to_s) - uris.each do |u| - return true if u.match uri - end - end - end - end - return false - end - - def self.authorize_exception?(uri, request_method) - if $aa[:authorize_exceptions] - $aa[:authorize_exceptions].each do |request_methods,uris| - if request_methods and uris and request_methods.include?(request_method.to_sym) - uris.each do |u| - return true if u.match uri + # extend class methods + class << self + # methods: free_uri and authorize_exception + # @return [Boolean] checks if uri-method pair is included in $aa[:free_uri] or $aa[:authorize_exception] + [:free_uri, :authorize_exception].each do |method| + define_method "#{method}?".to_sym do |uri, request_method| + if $aa["#{method}s".to_sym] + $aa["#{method}s".to_sym].each do |request_methods, uris| + if request_methods and uris and request_methods.include?(request_method.to_sym) + uris.each do |u| + return true if u.match uri + end + end end end + return false end end - return false end - end end diff --git a/lib/compound.rb b/lib/compound.rb index 2bef69e..be782eb 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -1,9 +1,10 @@ +require "openbabel" CACTUS_URI="http://cactus.nci.nih.gov/chemical/structure/" module OpenTox # Ruby wrapper for OpenTox Compound Webservices (http://opentox.org/dev/apis/api-1.2/structure). - class Compound + class Compound # Create a compound from smiles string # @example @@ -39,57 +40,57 @@ module OpenTox Compound.new RestClientWrapper.post(service_uri, @inchi, {:content_type => 'chemical/x-inchi', :subjectid => subjectid}) end - # Get InChI + # Get InChI # @return [String] InChI string - def inchi + def inchi @inchi ||= RestClientWrapper.get(@uri,{},{:accept => 'chemical/x-inchi'}).chomp - end + end - # Get InChIKey + # Get InChIKey # @return [String] InChI string - def inchikey + def inchikey @inchikey ||= RestClientWrapper.get(@uri,{},{:accept => 'chemical/x-inchikey'}).chomp - end + end - # Get (canonical) smiles + # Get (canonical) smiles # @return [String] Smiles string - def smiles + def smiles @smiles ||= RestClientWrapper.get(@uri,{},{:accept => 'chemical/x-daylight-smiles'}).chomp - end + end # Get sdf # @return [String] SDF string - def sdf + def sdf RestClientWrapper.get(@uri,{},{:accept => 'chemical/x-mdl-sdfile'}).chomp - end + end # Get gif image # @return [image/gif] Image data - def gif + def gif RestClientWrapper.get File.join(CACTUS_URI,inchi,"image") - end + end # Get png image # @example # image = compound.png # @return [image/png] Image data - def png + def png RestClientWrapper.get(File.join @uri, "image") - end + end # Get URI of compound image # @return [String] Compound image URI - def image_uri + def image_uri File.join @uri, "image" - end + end # Get all known compound names. Relies on an external service for name lookups. # @example # names = compound.names # @return [String] Compound names - def names + def names RestClientWrapper.get("#{CACTUS_URI}#{inchi}/names").split("\n") - end + end def cid pug_uri = "http://pubchem.ncbi.nlm.nih.gov/rest/pug/" @@ -106,40 +107,40 @@ module OpenTox end =begin - # Match a smarts string + # Match a smarts string # @example # compound = OpenTox::Compound.from_name("Benzene") # compound.match?("cN") # returns false # @param [String] smarts Smarts string - def match?(smarts) - obconversion = OpenBabel::OBConversion.new - obmol = OpenBabel::OBMol.new - obconversion.set_in_format('inchi') - obconversion.read_string(obmol,@inchi) - smarts_pattern = OpenBabel::OBSmartsPattern.new - smarts_pattern.init(smarts) - smarts_pattern.match(obmol) - end + def match?(smarts) + obconversion = OpenBabel::OBConversion.new + obmol = OpenBabel::OBMol.new + obconversion.set_in_format('inchi') + obconversion.read_string(obmol,@inchi) + smarts_pattern = OpenBabel::OBSmartsPattern.new + smarts_pattern.init(smarts) + smarts_pattern.match(obmol) + end - # Match an array of smarts strings, returns array with matching smarts + # Match an array of smarts strings, returns array with matching smarts # @example # compound = OpenTox::Compound.from_name("Benzene") # compound.match(['cc','cN']) # returns ['cc'] # @param [Array] smarts_array Array with Smarts strings # @return [Array] Array with matching Smarts strings - def match(smarts_array) + def match(smarts_array) # avoid recreation of OpenBabel objects - obconversion = OpenBabel::OBConversion.new - obmol = OpenBabel::OBMol.new - obconversion.set_in_format('inchi') - obconversion.read_string(obmol,@inchi) - smarts_pattern = OpenBabel::OBSmartsPattern.new - smarts_array.collect do |smarts| + obconversion = OpenBabel::OBConversion.new + obmol = OpenBabel::OBMol.new + obconversion.set_in_format('inchi') + obconversion.read_string(obmol,@inchi) + smarts_pattern = OpenBabel::OBSmartsPattern.new + smarts_array.collect do |smarts| smarts_pattern.init(smarts) smarts if smarts_pattern.match(obmol) end.compact #smarts_array.collect { |s| s if match?(s)}.compact - end + end # Get URI of compound image with highlighted fragments # @@ -156,19 +157,19 @@ module OpenTox private # Convert sdf to inchi - def self.sdf2inchi(sdf) - Compound.obconversion(sdf,'sdf','inchi') - end + def self.sdf2inchi(sdf) + Compound.obconversion(sdf,'sdf','inchi') + end # Convert smiles to inchi - def self.smiles2inchi(smiles) - Compound.obconversion(smiles,'smi','inchi') - end + def self.smiles2inchi(smiles) + Compound.obconversion(smiles,'smi','inchi') + end # Convert smiles to canonical smiles - def self.smiles2cansmi(smiles) - Compound.obconversion(smiles,'smi','can') - end + def self.smiles2cansmi(smiles) + Compound.obconversion(smiles,'smi','can') + end # Convert identifier from OpenBabel input_format to OpenBabel output_format def self.obconversion(identifier,input_format,output_format) @@ -205,7 +206,7 @@ module OpenTox smarts_array.collect do |smarts| smarts_pattern.init(smarts) if smarts_pattern.match(obmol) - if use_hits + if use_hits hits = smarts_pattern.get_map_list smarts_hits[smarts] = hits.to_a.size else @@ -222,5 +223,5 @@ module OpenTox end =end - end + end end diff --git a/lib/dataset.rb b/lib/dataset.rb index f261641..8d5f5d1 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -64,7 +64,15 @@ module OpenTox # TODO: fallbacks for external and unordered datasets features.each_with_index do |feature,i| if feature[RDF.type].include? RDF::OT.NumericFeature - @data_entries.each { |row| row[i] = row[i].to_f if row[i] } + if feature[RDF.type].include? RDF::OT.NominalFeature + if feature[RDF.type].include? RDF::OT.StringFeature + @data_entries.each { |row| row[i] = row[i].to_s if row[i] } + else + @data_entries.each { |row| row[i] = row[i] if row[i] } + end + else + @data_entries.each { |row| row[i] = row[i].to_f if row[i] } + end end end end diff --git a/lib/error.rb b/lib/error.rb index eb72144..85ce35d 100644 --- a/lib/error.rb +++ b/lib/error.rb @@ -41,7 +41,7 @@ module OpenToxError def to_turtle # redefine to use prefixes (not supported by RDF::Writer) prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#"} ['OT', 'DC', 'XSD', 'OLO'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") } - RDF::N3::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer| + RDF::Turtle::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer| @rdf.each{|statement| writer << statement} if @rdf end end diff --git a/lib/opentox-client.rb b/lib/opentox-client.rb index 16b8552..02724c2 100644 --- a/lib/opentox-client.rb +++ b/lib/opentox-client.rb @@ -2,7 +2,7 @@ require 'rubygems' require "bundler/setup" require 'rdf' require 'rdf/raptor' -require 'rdf/n3' +require 'rdf/turtle' require "rest-client" require 'uri' require 'yaml' diff --git a/lib/opentox.rb b/lib/opentox.rb index 04edd0c..0b78d7e 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -131,7 +131,7 @@ module OpenTox end def create_rdf - @rdf = RDF::Graph.new + @rdf = RDF::Graph.new if @rdf.empty? or URI.task?(@uri) @metadata[RDF.type] ||= eval("RDF::OT."+self.class.to_s.split('::').last) @metadata[RDF::DC.date] ||= DateTime.now @metadata.each do |predicate,values| @@ -169,7 +169,7 @@ module OpenTox prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#"} ['OT', 'DC', 'XSD', 'OLO'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") } create_rdf - RDF::N3::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer| + RDF::Turtle::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer| @rdf.each{|statement| writer << statement} end end diff --git a/lib/otlogger.rb b/lib/otlogger.rb index 57b8170..0f0caa4 100644 --- a/lib/otlogger.rb +++ b/lib/otlogger.rb @@ -1,3 +1,5 @@ + +# extend logger to add current source file, line-number and source location where the log command is called class OTLogger < Logger def pwd diff --git a/lib/overwrite.rb b/lib/overwrite.rb index d1f3d9d..52b8a9f 100644 --- a/lib/overwrite.rb +++ b/lib/overwrite.rb @@ -12,13 +12,14 @@ class Object end module Enumerable + # @return [Array] only the duplicates of an enumerable def duplicates inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys end end class String - + # @return [String] converts camel-case to underscore-case (OpenTox::SuperModel -> open_tox/super_model) def underscore self.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). @@ -88,16 +89,19 @@ module URI 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) parsed_uri = URI.parse(uri + (subjectid ? "?subjectid=#{CGI.escape subjectid}" : "")) http_code = URI.task?(uri) ? 600 : 400 - unless URI.ssl? uri - Net::HTTP.get_response(parsed_uri).code.to_i < http_code + unless (URI.ssl? uri) == true + http = Net::HTTP.new(parsed_uri.host, parsed_uri.port) + request = Net::HTTP::Head.new(parsed_uri.request_uri) + http.request(request).code.to_i < http_code else http = Net::HTTP.new(parsed_uri.host, parsed_uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Get.new(parsed_uri.request_uri) + request = Net::HTTP::Head.new(parsed_uri.request_uri) http.request(request).code.to_i < http_code end rescue @@ -114,6 +118,7 @@ module URI end class File + # @return [String] mime_type including charset using linux cmd command def mime_type `file -ib #{self.path}`.chomp end @@ -136,6 +141,7 @@ module Kernel internal_server_error $!.message end + # @return [String] uri of task result, if task fails, an error according to task is raised def wait_for_task uri if URI.task?(uri) t = OpenTox::Task.new uri @@ -163,7 +169,7 @@ end class Array - # Sum of an array for Arrays + # Sum up the size of single arrays in an array of arrays # @param [Array] Array of arrays # @return [Integer] Sum of size of array elements def sum_size diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb index 38219c1..f3e6300 100644 --- a/lib/rest-client-wrapper.rb +++ b/lib/rest-client-wrapper.rb @@ -57,6 +57,7 @@ module OpenTox end end + #@return [Array] of hashes with error code, method and class def self.known_errors errors = [] RestClient::STATUSES.each do |code,k| diff --git a/lib/task.rb b/lib/task.rb index 944f20c..3cc05c0 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -46,7 +46,7 @@ module OpenTox @rdf << [subject, RDF::OT.errorCode, $!.class.to_s] @rdf << [subject, RDF::OT.errorCause, $!.backtrace[0..cut_index].join("\n")] prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#", :ot => RDF::OT.to_s} - turtle = RDF::N3::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer| + turtle = RDF::Turtle::Writer.for(:turtle).buffer(:prefixes => prefixes) do |writer| @rdf.each{|statement| writer << statement} end $logger.error turtle |