summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormguetlein <martin.guetlein@gmail.com>2010-08-11 08:48:02 +0200
committermguetlein <martin.guetlein@gmail.com>2010-08-11 08:48:02 +0200
commit26369d68b9edcf053789e2d024e0286fd517a29d (patch)
tree1da11b9beef0df56a291f836e6a6cf46bfacaa01 /lib
parent2806bfff8a01f75351f31c5bd676b25b2fb36017 (diff)
parent44d05cec5a612f50f8a61ccd762c8176a0b8fd68 (diff)
Merge remote branch 'helma/master' into test
Diffstat (limited to 'lib')
-rw-r--r--lib/algorithm.rb8
-rw-r--r--lib/compound.rb31
-rw-r--r--lib/dataset.rb3
-rw-r--r--lib/environment.rb80
-rw-r--r--lib/model.rb5
-rw-r--r--lib/opentox-ruby-api-wrapper.rb2
-rw-r--r--lib/ot-logger.rb48
-rw-r--r--lib/overwrite.rb14
-rw-r--r--lib/owl.rb9
-rw-r--r--lib/tasks/opentox.rb107
-rw-r--r--lib/templates/config.yaml62
-rw-r--r--lib/utils.rb7
12 files changed, 141 insertions, 235 deletions
diff --git a/lib/algorithm.rb b/lib/algorithm.rb
index 30a41e0..9d87c7a 100644
--- a/lib/algorithm.rb
+++ b/lib/algorithm.rb
@@ -41,9 +41,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
@@ -56,12 +54,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/dataset.rb b/lib/dataset.rb
index 84b1ec0..654a275 100644
--- a/lib/dataset.rb
+++ b/lib/dataset.rb
@@ -31,8 +31,7 @@ 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/
- if (uri.match(@@config[:services]["opentox-dataset"]) || uri =~ /in-silico.ch/) && !@@config[:accept_headers]["opentox-dataset"].grep(/yaml/).empty?
+ if (@@config[:yaml_hosts].include?(URI.parse(uri).host))
accept_header = 'application/x-yaml'
else
accept_header = "application/rdf+xml"
diff --git a/lib/environment.rb b/lib/environment.rb
index 312eec4..80899c5 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,18 +68,10 @@ 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
# Task durations
-DEFAULT_TASK_MAX_DURATION = @@config[:default_task_max_duration]
-EXTERNAL_TASK_MAX_DURATION = @@config[:external_task_max_duration]
+DEFAULT_TASK_MAX_DURATION = 3600
+EXTERNAL_TASK_MAX_DURATION = 3600
diff --git a/lib/model.rb b/lib/model.rb
index 25266e3..e8f6048 100644
--- a/lib/model.rb
+++ b/lib/model.rb
@@ -103,6 +103,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
+
diff --git a/lib/owl.rb b/lib/owl.rb
index 245dcc7..a4c2e68 100644
--- a/lib/owl.rb
+++ b/lib/owl.rb
@@ -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..00c00cb 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,20 @@
# 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:
+# Yaml capable hosts (faster than OWL-DL)
#
- :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"
+:yaml_hosts:
+ - "localhost"
-# Timeouts:
-#
- :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