diff options
-rw-r--r-- | lib/algorithm.rb | 29 | ||||
-rw-r--r-- | lib/compound.rb | 49 | ||||
-rw-r--r-- | lib/opentox.rb | 2 | ||||
-rw-r--r-- | lib/overwrite.rb | 24 |
4 files changed, 79 insertions, 25 deletions
diff --git a/lib/algorithm.rb b/lib/algorithm.rb index 8576681..1ffc883 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -5,11 +5,38 @@ 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,Boolean] wait set to true if method should wait for task result + # @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} wait_for_task uri if wait end end + + module Descriptor + + class Smarts + + def self.fingerprint compounds, smarts, count=false + matcher = Algorithm.new File.join($algorithm[:uri],"descriptor","smarts","fingerprint") + smarts = [smarts] unless smarts.is_a? Array + if compounds.is_a? OpenTox::Compound + json = matcher.run :compound_uri => compounds.uri, :smarts => smarts, :count => count + elsif compounds.is_a? OpenTox::Dataset + # TODO: add task and return dataset instead of result + json = matcher.run :dataset_uri => compounds.uri, :smarts => smarts, :count => count + else + bad_request_error "Cannot match smarts on #{compounds.class} objects." + end + + JSON.parse json + end + + def self.count compounds, smarts + fingerprint compounds,smarts,true + end + end + + + end end diff --git a/lib/compound.rb b/lib/compound.rb index 51ef96d..daeabb9 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -1,4 +1,4 @@ -require "openbabel" +#require "openbabel" CACTUS_URI="http://cactus.nci.nih.gov/chemical/structure/" module OpenTox @@ -116,6 +116,27 @@ module OpenTox # compound.match?("cN") # returns false # @param [String] smarts Smarts string def match?(smarts) + matcher = Algorithm.new File.join($algorithm[:uri],"descriptor","smarts") + matcher.run :compound_uri => @uri, :smarts => smarts, :count => false + end + + # 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) + matcher = Algorithm.new File.join($algorithm[:uri],"descriptor","smarts") + matcher.run :compound_uri => @uri, :smarts => smarts_array, :count => false + end + + # 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') @@ -175,19 +196,18 @@ module OpenTox end # Convert identifier from OpenBabel input_format to OpenBabel output_format - def self.obconversion(identifier,input_format,output_format) - obconversion = OpenBabel::OBConversion.new - obmol = OpenBabel::OBMol.new - obconversion.set_in_and_out_formats input_format, output_format - obconversion.read_string obmol, identifier - case output_format - when /smi|can|inchi/ - obconversion.write_string(obmol).gsub(/\s/,'').chomp - else - obconversion.write_string(obmol) - end - end -=end + def self.obconversion(identifier,input_format,output_format) + obconversion = OpenBabel::OBConversion.new + obmol = OpenBabel::OBMol.new + obconversion.set_in_and_out_formats input_format, output_format + obconversion.read_string obmol, identifier + case output_format + when /smi|can|inchi/ + obconversion.write_string(obmol).gsub(/\s/,'').chomp + else + obconversion.write_string(obmol) + end + end @@ -225,6 +245,7 @@ module OpenTox def match(smarts_array) match_hits(smarts_array,false) end +=end end end diff --git a/lib/opentox.rb b/lib/opentox.rb index 8099f41..52f705c 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -108,6 +108,8 @@ module OpenTox # 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::DC.modified] = DateTime.now case mime_type when 'text/plain' body = self.to_ntriples diff --git a/lib/overwrite.rb b/lib/overwrite.rb index fbe8f7d..b2382e0 100644 --- a/lib/overwrite.rb +++ b/lib/overwrite.rb @@ -28,6 +28,14 @@ class String downcase end + # convert strings to boolean values + # @return [TrueClass,FalseClass] true or false + def to_boolean + return true if self == true || self =~ (/(true|t|yes|y|1)$/i) + return false if self == false || self.nil? || self =~ (/(false|f|no|n|0)$/i) + bad_request_error "invalid value for Boolean: \"#{self}\"" + end + # encloses URI in text with with link tag # @return [String] new text with marked links def link_urls @@ -77,11 +85,11 @@ module URI end def self.dataset? uri, subjectid=nil - uri =~ /dataset/ and URI.accessible? uri, subjectid=nil + uri =~ /dataset/ and URI.accessible? uri, subjectid end def self.model? uri, subjectid=nil - uri =~ /model/ and URI.accessible? uri, subjectid=nil + uri =~ /model/ and URI.accessible? uri, subjectid end def self.ssl? uri, subjectid=nil @@ -92,17 +100,13 @@ module URI 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) == 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 = Net::HTTP.new(parsed_uri.host, parsed_uri.port) + if (URI.ssl? uri) == true http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE - request = Net::HTTP::Head.new(parsed_uri.request_uri) - http.request(request).code.to_i < http_code end + request = Net::HTTP::Head.new(parsed_uri.request_uri) + http.request(request).code.to_i < http_code rescue false end |