diff options
-rw-r--r-- | Rakefile | 4 | ||||
-rw-r--r-- | VERSION | 2 | ||||
-rw-r--r-- | lib/algorithm.rb | 8 | ||||
-rw-r--r-- | lib/compound.rb | 31 | ||||
-rw-r--r-- | lib/environment.rb | 76 | ||||
-rw-r--r-- | lib/model.rb | 5 | ||||
-rw-r--r-- | lib/opentox-ruby-api-wrapper.rb | 2 | ||||
-rw-r--r-- | lib/ot-logger.rb | 48 | ||||
-rw-r--r-- | lib/overwrite.rb | 14 | ||||
-rw-r--r-- | lib/owl.rb | 9 | ||||
-rw-r--r-- | lib/tasks/opentox.rb | 107 | ||||
-rw-r--r-- | lib/templates/config.yaml | 82 | ||||
-rw-r--r-- | lib/utils.rb | 7 | ||||
-rw-r--r-- | opentox-ruby-api-wrapper.gemspec | 9 |
14 files changed, 170 insertions, 234 deletions
@@ -25,7 +25,9 @@ begin "roo", "spreadsheet", "google-spreadsheet-ruby", - "tmail" + "tmail", + "rinruby", + "rjb" ].each { |dep| gem.add_dependency dep } [ "dm-core", 'dm-serializer', @@ -1 +1 @@ -1.5.7 +1.6.0 diff --git a/lib/algorithm.rb b/lib/algorithm.rb index cc06032..f2a3618 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -22,9 +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 => "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 end @@ -37,12 +35,12 @@ module OpenTox class Similarity def self.weighted_tanimoto(fp_a,fp_b,p) common_features = fp_a & fp_b - all_features = fp_a + fp_b + all_features = (fp_a + fp_b).uniq common_p_sum = 0.0 if common_features.size > 0 - common_features.each{|f| common_p_sum += p[f]} + common_features.each{|f| common_p_sum += OpenTox::Utils.gauss(p[f])} all_p_sum = 0.0 - all_features.each{|f| all_p_sum += p[f]} + all_features.each{|f| all_p_sum += OpenTox::Utils.gauss(p[f])} common_p_sum/all_p_sum else 0.0 diff --git a/lib/compound.rb b/lib/compound.rb index 7c7c3d8..49c166f 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -1,3 +1,6 @@ +@@cactus_uri="http://cactus.nci.nih.gov/chemical/structure/" +@@ambit_uri="http://ambit.uni-plovdiv.bg:8080/ambit2/depict/cdk?search=" + module OpenTox class Compound #< OpenTox @@ -6,7 +9,6 @@ module OpenTox # Initialize with <tt>:uri => uri</tt>, <tt>:smiles => smiles</tt> or <tt>:name => name</tt> (name can be also an InChI/InChiKey, CAS number, etc) def initialize(params) - @@cactus_uri="http://cactus.nci.nih.gov/chemical/structure/" if params[:smiles] @inchi = smiles2inchi(params[:smiles]) @uri = File.join(@@config[:services]["opentox-compound"],URI.escape(@inchi)) @@ -45,12 +47,35 @@ module OpenTox obconversion(@inchi,'inchi','sdf') end - def image + def gif RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/image") end + def png + RestClientWrapper.get(File.join @uri, "image") + end + + def names + begin + RestClientWrapper.get("#{@@cactus_uri}#{@inchi}/names") + rescue + "not available" + end + end + + def display_smarts_uri(activating, deactivating, highlight = nil) + LOGGER.debug activating.to_yaml unless activating.nil? + activating_smarts = URI.encode "\"#{activating.join("\"/\"")}\"" + deactivating_smarts = URI.encode "\"#{deactivating.join("\"/\"")}\"" + if highlight.nil? + File.join @@config[:services]["opentox-compound"], "smiles", URI.encode(smiles), "smarts/activating", URI.encode(activating_smarts),"deactivating", URI.encode(deactivating_smarts) + else + File.join @@config[:services]["opentox-compound"], "smiles", URI.encode(smiles), "smarts/activating", URI.encode(activating_smarts),"deactivating", URI.encode(deactivating_smarts), "highlight", URI.encode(highlight) + end + end + def image_uri - "#{@@cactus_uri}#{@inchi}/image" + File.join @uri, "image" end # Matchs a smarts string diff --git a/lib/environment.rb b/lib/environment.rb index 312eec4..1551448 100644 --- a/lib/environment.rb +++ b/lib/environment.rb @@ -1,6 +1,6 @@ -require 'logger' +require "ot-logger" # set default environment -ENV['RACK_ENV'] = 'test' unless ENV['RACK_ENV'] +ENV['RACK_ENV'] = 'production' unless ENV['RACK_ENV'] # load/setup configuration basedir = File.join(ENV['HOME'], ".opentox") @@ -44,69 +44,9 @@ end # load mail settings 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 } - -class Sinatra::Base - # overwriting halt to log halts (!= 202) - def halt(*response) - LOGGER.error "halt "+response.first.to_s+" "+(response.size>1 ? response[1].to_s : "") if response and response.first and response.first >= 300 - # orig sinatra code: - response = response.first if response.length == 1 - throw :halt, response - end -end - -# logging -class MyLogger < Logger - - def pwd - path = Dir.pwd.to_s - index = path.rindex(/\//) - return path if index==nil - path[(index+1)..-1] - end - - def trace() - lines = caller(0) - n = 2 - line = lines[n] - - while (line =~ /spork.rb/ or line =~ /as_task/ or line =~ /environment.rb/) - n += 1 - line = lines[n] - end - - index = line.rindex(/\/.*\.rb/) - return line if index==nil - line[index..-1] - end - - def format(msg) - pwd.ljust(18)+" :: "+msg.to_s+" :: "+trace+" :: "+($sinatra ? $sinatra.request.env['REMOTE_ADDR'] : nil).to_s - end - - def debug(msg) - super format(msg) - end - - def info(msg) - super format(msg) - end - - def warn(msg) - super format(msg) - end - - def error(msg) - super format(msg) - end - -end - - logfile = "#{LOG_DIR}/#{ENV["RACK_ENV"]}.log" -LOGGER = MyLogger.new(logfile,'daily') # daily rotation +#LOGGER = MyLogger.new(logfile,'daily') # daily rotation +LOGGER = MyLogger.new(logfile) # no rotation LOGGER.formatter = Logger::Formatter.new #this is neccessary to restore the formating in case active-record is loaded if @@config[:logger] and @@config[:logger] == "debug" LOGGER.level = Logger::DEBUG @@ -128,14 +68,6 @@ rescue raise "Please edit #{user_file} and restart your application. Create at least one user with password." end -# RDF namespaces -RDF = Redland::Namespace.new 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' -OWL = Redland::Namespace.new 'http://www.w3.org/2002/07/owl#' -DC = Redland::Namespace.new 'http://purl.org/dc/elements/1.1/' -OT = Redland::Namespace.new 'http://www.opentox.org/api/1.1#' -#OT = Redland::Namespace.new 'http://ortona.informatik.uni-freiburg.de/opentox.owl#' -XML = Redland::Namespace.new 'http://www.w3.org/2001/XMLSchema#' - # Regular expressions for parsing classification data TRUE_REGEXP = /^(true|active|1|1.0)$/i FALSE_REGEXP = /^(false|inactive|0|0.0)$/i diff --git a/lib/model.rb b/lib/model.rb index b6cef46..0077cfe 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -104,6 +104,11 @@ module OpenTox def self.find_all RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n") end + + def self.predict(compound_uri,model_uri) + #RestClientWrapper.post(model_uri,{:compound_uri => compound_uri, :accept => 'application/x-yaml'}) + `curl -X POST -d 'compound_uri=#{compound_uri}' -H 'Accept:application/x-yaml' #{model_uri}` + end end end end diff --git a/lib/opentox-ruby-api-wrapper.rb b/lib/opentox-ruby-api-wrapper.rb index 16c23fe..45a3428 100644 --- a/lib/opentox-ruby-api-wrapper.rb +++ b/lib/opentox-ruby-api-wrapper.rb @@ -8,6 +8,6 @@ rescue LoadError puts "Please install Openbabel with 'rake openbabel:install' in the compound component" end -['owl', 'compound','dataset','algorithm','model','task','validation','utils','authorization','features', 'rest_client_wrapper'].each do |lib| +['owl', 'compound','dataset','algorithm','model','task','validation','utils','authorization','features', 'ot-logger', 'overwrite', 'rest_client_wrapper'].each do |lib| require lib end diff --git a/lib/ot-logger.rb b/lib/ot-logger.rb new file mode 100644 index 0000000..6c35ed2 --- /dev/null +++ b/lib/ot-logger.rb @@ -0,0 +1,48 @@ +require 'logger' +# logging +class MyLogger < Logger + + def pwd + path = Dir.pwd.to_s + index = path.rindex(/\//) + return path if index==nil + path[(index+1)..-1] + end + + def trace() + lines = caller(0) + n = 2 + line = lines[n] + + while (line =~ /spork.rb/ or line =~ /as_task/ or line =~ /environment.rb/) + n += 1 + line = lines[n] + end + + index = line.rindex(/\/.*\.rb/) + return line if index==nil + line[index..-1] + end + + def format(msg) + pwd.ljust(18)+" :: "+msg.to_s+" :: "+trace+" :: "+($sinatra ? $sinatra.request.env['REMOTE_ADDR'] : nil).to_s + end + + def debug(msg) + super format(msg) + end + + def info(msg) + super format(msg) + end + + def warn(msg) + super format(msg) + end + + def error(msg) + super format(msg) + end + +end + diff --git a/lib/overwrite.rb b/lib/overwrite.rb new file mode 100644 index 0000000..1d0161b --- /dev/null +++ b/lib/overwrite.rb @@ -0,0 +1,14 @@ +# class overwrites aka monkey patches +# hack: store sinatra in global var to make url_for and halt methods accessible +before{ $sinatra = self unless $sinatra } + +class Sinatra::Base + # overwriting halt to log halts (!= 202) + def halt(*response) + LOGGER.error "halt "+response.first.to_s+" "+(response.size>1 ? response[1].to_s : "") if response and response.first and response.first >= 300 + # orig sinatra code: + response = response.first if response.length == 1 + throw :halt, response + end +end + @@ -1,3 +1,10 @@ +# RDF namespaces +RDF = Redland::Namespace.new 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' +OWL = Redland::Namespace.new 'http://www.w3.org/2002/07/owl#' +DC = Redland::Namespace.new 'http://purl.org/dc/elements/1.1/' +OT = Redland::Namespace.new 'http://www.opentox.org/api/1.1#' +#OT = Redland::Namespace.new 'http://ortona.informatik.uni-freiburg.de/opentox.owl#' +XML = Redland::Namespace.new 'http://www.w3.org/2001/XMLSchema#' # overriding literal to give nice access to datatype # and to access the stored value as correct ruby type @@ -537,4 +544,4 @@ module OpenTox end end end -end
\ No newline at end of file +end diff --git a/lib/tasks/opentox.rb b/lib/tasks/opentox.rb deleted file mode 100644 index 7fce35b..0000000 --- a/lib/tasks/opentox.rb +++ /dev/null @@ -1,107 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', 'opentox-ruby-api-wrapper.rb') - -namespace :opentox do - - namespace :services do - - desc "Run opentox services" - task :start do - @@config[:services].each do |service,uri| - dir = File.join(@@config[:base_dir], service) - server = @@config[:webserver] - case server - when /thin|mongrel|webrick/ - port = uri.sub(/^.*:/,'').sub(/\/$/,'') - Dir.chdir dir - pid_file = File.join(TMP_DIR,"#{service}.pid") - begin - `#{server} --trace --rackup config.ru start -p #{port} -e #{ENV['RACK_ENV']} -P #{pid_file} -d &` - puts "#{service} started on localhost:#{port} in #{ENV['RACK_ENV']} environment with PID file #{pid_file}." - rescue - puts "Cannot start #{service} on port #{port}." - end - when 'passenger' - FileUtils.mkdir_p File.join(dir, 'tmp') - FileUtils.touch File.join(dir, 'tmp/restart.txt') - puts "#{service} restarted." - else - puts "not yet implemented" - end - end - end - - desc "Stop opentox services" - task :stop do - server = @@config[:webserver] - if server =~ /thin|mongrel|webrick/ - @@config[:services].each do |service,uri| - port = uri.sub(/^.*:/,'').sub(/\/$/,'') - pid_file = File.join(TMP_DIR,"#{service}.pid") - begin - puts `#{server} stop -P #{pid_file}` - puts "#{service} stopped on localhost:#{port}" - rescue - puts "Cannot stop #{service} on port #{port}." - end - end - end - end - - desc "Restart opentox services" - task :restart => [:stop, :start] - - end - - desc "Run all OpenTox tests" - task :test do - @@config[:services].each do |service,uri| - dir = File.join(@@config[:base_dir], service) - Dir.chdir dir - puts "Running tests in #{dir}" - `rake test -t 1>&2` - end - end - -end - -desc "Start service in current directory" -task :start do - service = File.basename(Dir.pwd).intern - server = @@config[:webserver] - case server - when /thin|mongrel|webrick/ - port = @@config[:services][service].sub(/^.*:/,'').sub(/\/$/,'') - pid_file = File.join(TMP_DIR,"#{service}.pid") - begin - `#{server} --trace --rackup config.ru start -p #{port} -e #{ENV['RACK_ENV']} -P #{pid_file} -d &` - puts "#{service} started on localhost:#{port} in #{ENV['RACK_ENV']} environment with PID file #{pid_file}." - rescue - puts "Cannot start #{service} on port #{port}." - end - when 'passenger' - FileUtils.mkdir_p File.join(dir, 'tmp') - FileUtils.touch File.join(dir, 'tmp/restart.txt') - puts "#{service} restarted." - else - puts "not yet implemented" - end -end - -desc "Stop service in current directory" -task :stop do - service = File.basename(Dir.pwd).intern - server = @@config[:webserver] - if server =~ /thin|mongrel|webrick/ - port = @@config[:services][service].sub(/^.*:/,'').sub(/\/$/,'') - pid_file = File.join(TMP_DIR,"#{service}.pid") - begin - puts `thin stop -P #{pid_file}` - puts "#{service} stopped on localhost:#{port}" - rescue - puts "Cannot stop #{service} on port #{port}." - end - end -end - -desc "Restart service in current directory" -task :restart => [:stop, :start] diff --git a/lib/templates/config.yaml b/lib/templates/config.yaml index dba0bf3..a833030 100644 --- a/lib/templates/config.yaml +++ b/lib/templates/config.yaml @@ -4,12 +4,12 @@ # # Example MySql: # - :database: - :adapter: mysql - :database: production - :username: root - :password: opentox - :host: localhost +:database: + :adapter: mysql + :database: production + :username: root + :password: opentox + :host: localhost # # Example 1: Using external test services # @@ -22,42 +22,44 @@ # opentox-validation: "http://opentox.informatik.uni-freiburg.de/validation/" # # Example 2: Using local services - :base_dir: /home/ist/webservices - :webserver: passenger - :services: - opentox-compound: "http://localhost/compound/" - opentox-dataset: "http://localhost/dataset/" - opentox-algorithm: "http://localhost/algorithm/" - opentox-model: "http://localhost/model/" - opentox-task: "http://localhost/task/" - opentox-validation: "http://opentox.informatik.uni-freiburg.de/validation/" +:base_dir: /home/ist/webservices +:webserver: passenger +:services: + opentox-compound: "http://localhost/compound/" + opentox-dataset: "http://localhost/dataset/" + opentox-algorithm: "http://localhost/algorithm/" + opentox-model: "http://localhost/model/" + opentox-task: "http://localhost/task/" + opentox-validation: "http://localhost/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" - - "application/rdf+xml" - opentox-algorithm: - - "application/x-yaml" - - "application/rdf+xml" - opentox-model: - - "application/x-yaml" - - "application/rdf+xml" - opentox-task: - - "application/x-yaml" - - "application/rdf+xml" - opentox-validation: - - "application/x-yaml" - - "application/rdf+xml" +:accept_headers: + opentox-compound: + - "chemical/x-daylight-smiles" + - "chemical/x-inchi" + - "chemical/x-mdl-sdfile" + - "image/gif" + - "text/plain" + opentox-dataset: + - "application/x-yaml" + - "application/rdf+xml" + opentox-algorithm: + - "application/x-yaml" + - "application/rdf+xml" + opentox-model: + - "application/x-yaml" + - "application/rdf+xml" + opentox-task: + - "application/x-yaml" + - "application/rdf+xml" + opentox-validation: + - "application/x-yaml" + - "application/rdf+xml" # Timeouts: -# - :default_task_max_duration: 3600 - :external_task_max_duration: 3600 +:default_task_max_duration: 3600 +:external_task_max_duration: 3600 + +# Uncomment for verbose logging +# :logger: debug diff --git a/lib/utils.rb b/lib/utils.rb index 741fbd6..a0e0cbe 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -28,6 +28,13 @@ module OpenTox return false end end + + def self.median(array) + return nil if array.empty? + array.sort! + m_pos = array.size / 2 + return array.size % 2 == 1 ? array[m_pos] : (array[m_pos-1] + array[m_pos])/2 + end end diff --git a/opentox-ruby-api-wrapper.gemspec b/opentox-ruby-api-wrapper.gemspec index 221d53d..6edb865 100644 --- a/opentox-ruby-api-wrapper.gemspec +++ b/opentox-ruby-api-wrapper.gemspec @@ -5,14 +5,14 @@ Gem::Specification.new do |s| s.name = %q{opentox-ruby-api-wrapper} - s.version = "1.5.7" + s.version = "1.6.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Christoph Helma, Martin Guetlein"] - s.date = %q{2010-06-28} + s.date = %q{2010-07-19} s.description = %q{Ruby wrapper for the OpenTox REST API (http://www.opentox.org)} s.email = %q{helma@in-silico.ch} - s.executables = ["yaml2owl.rb", "opentox-install-ubuntu.sh", "opentox-install-debian.sh"] + s.executables = ["opentox-install-ubuntu.sh", "yaml2owl.rb", "opentox-install-debian.sh"] s.extra_rdoc_files = [ "LICENSE", "README.rdoc" @@ -72,6 +72,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency(%q<spreadsheet>, [">= 0"]) s.add_runtime_dependency(%q<google-spreadsheet-ruby>, [">= 0"]) s.add_runtime_dependency(%q<tmail>, [">= 0"]) + s.add_runtime_dependency(%q<rinruby>, [">= 0"]) s.add_runtime_dependency(%q<dm-core>, [">= 1"]) s.add_runtime_dependency(%q<dm-serializer>, [">= 1"]) s.add_runtime_dependency(%q<dm-timestamps>, [">= 1"]) @@ -97,6 +98,7 @@ Gem::Specification.new do |s| s.add_dependency(%q<spreadsheet>, [">= 0"]) s.add_dependency(%q<google-spreadsheet-ruby>, [">= 0"]) s.add_dependency(%q<tmail>, [">= 0"]) + s.add_dependency(%q<rinruby>, [">= 0"]) s.add_dependency(%q<dm-core>, [">= 1"]) s.add_dependency(%q<dm-serializer>, [">= 1"]) s.add_dependency(%q<dm-timestamps>, [">= 1"]) @@ -123,6 +125,7 @@ Gem::Specification.new do |s| s.add_dependency(%q<spreadsheet>, [">= 0"]) s.add_dependency(%q<google-spreadsheet-ruby>, [">= 0"]) s.add_dependency(%q<tmail>, [">= 0"]) + s.add_dependency(%q<rinruby>, [">= 0"]) s.add_dependency(%q<dm-core>, [">= 1"]) s.add_dependency(%q<dm-serializer>, [">= 1"]) s.add_dependency(%q<dm-timestamps>, [">= 1"]) |