diff options
-rw-r--r-- | lib/compound.rb | 9 | ||||
-rw-r--r-- | lib/dataset.rb | 71 | ||||
-rw-r--r-- | lib/environment.rb | 31 | ||||
-rw-r--r-- | lib/model.rb | 1 | ||||
-rw-r--r-- | lib/owl.rb | 3 | ||||
-rw-r--r-- | lib/task.rb | 19 | ||||
-rw-r--r-- | lib/tasks/opentox.rb | 8 | ||||
-rw-r--r-- | lib/tasks/redis.rb | 125 | ||||
-rw-r--r-- | lib/templates/config.ru | 13 | ||||
-rw-r--r-- | lib/templates/config.yaml | 30 |
10 files changed, 90 insertions, 220 deletions
diff --git a/lib/compound.rb b/lib/compound.rb index 0a90663..562baaa 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -20,19 +20,22 @@ module OpenTox @inchi = RestClient.get("#{@@cactus_uri}#{params[:name]}/stdinchi").chomp @uri = File.join(@@config[:services]["opentox-compound"],URI.escape(@inchi)) elsif params[:uri] + @uri = params[:uri] if params[:uri].match(/InChI/) # shortcut for IST services @inchi = params[:uri].sub(/^.*InChI/, 'InChI') else @inchi = RestClient.get @uri, :accept => 'chemical/x-inchi' + # AMBIT does not provide InChIs + #smiles = RestClient.get(@uri, :accept => 'chemical/x-daylight-smiles').split(/\s+/).first # fix ambit output + #@inchi = obconversion(smiles,'smi','inchi') end - @uri = params[:uri] end end # Get the (canonical) smiles def smiles - RestClient.get(@uri, :accept => 'chemical/x-daylight-smiles').split(/\s+/).first # fix ambit output - #obconversion(@inchi,'inchi','can') + #RestClient.get(@uri, :accept => 'chemical/x-daylight-smiles').split(/\s+/).first # fix ambit output + obconversion(@inchi,'inchi','can') end def sdf diff --git a/lib/dataset.rb b/lib/dataset.rb index 8d1c0c1..0a49e87 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -85,14 +85,11 @@ module OpenTox end def self.find(uri) - begin - dataset = Dataset.new - data = RestClient.get uri, :accept => 'application/rdf+xml' # check if the resource is available - dataset.rdf = data - dataset - rescue - nil - end + dataset = Dataset.new + data = `curl "#{uri}"` + #data = RestClient.get uri, :accept => 'application/rdf+xml' # unclear why this does not work for complex uris, Dataset.find works from irb + dataset.rdf = data + dataset end def features @@ -107,57 +104,44 @@ module OpenTox data = {} @model.subjects(RDF['type'], OT['DataEntry']).each do |data_entry| compound_node = @model.object(data_entry, OT['compound']) - @model.find(compound_node, OT['identifier'],nil) {|s,p,o| puts o.to_s} compound_uri = @model.object(compound_node, DC['identifier']).to_s - data[compound_uri] = [] unless data[compound_uri] @model.find(data_entry, OT['values'], nil) do |s,p,values| - entry = {} feature_node = @model.object values, OT['feature'] - feature_uri = @model.object(feature_node, DC['identifier']).to_s - # TODO simple features + feature_uri = @model.object(feature_node, DC['identifier']).to_s.sub(/\^\^.*$/,'') # remove XML datatype type = @model.object(values, RDF['type']) if type == OT['FeatureValue'] - #entry[feature_uri] = [] unless entry[feature_uri] - entry[feature_uri] = @model.object(values, OT['value']).to_s + value = @model.object(values, OT['value']).to_s + case value.to_s + when TRUE_REGEXP # defined in environment.rb + value = true + when FALSE_REGEXP # defined in environment.rb + value = false + else + LOGGER.warn compound_uri + " has value '" + value.to_s + "' for feature " + feature_uri + value = nil + end + data[compound_uri] = {} unless data[compound_uri] + data[compound_uri][feature_uri] = [] unless data[compound_uri][feature_uri] + data[compound_uri][feature_uri] << value unless value.nil? elsif type == OT['Tuple'] - entry[feature_uri] = {} unless entry[feature_uri] + entry = {} + data[compound_uri] = {} unless data[compound_uri] + data[compound_uri][feature_uri] = [] unless data[compound_uri][feature_uri] @model.find(values, OT['complexValue'],nil) do |s,p,complex_value| name_node = @model.object complex_value, OT['feature'] name = @model.object(name_node, DC['title']).to_s value = @model.object(complex_value, OT['value']).to_s - entry[feature_uri][name] = value + v = value.sub(/\^\^.*$/,'') # remove XML datatype + v = v.to_f if v.match(/^[\.|\d]+$/) # guess numeric datatype + entry[name] = v end + data[compound_uri][feature_uri] << entry end - data[compound_uri] << entry end end data end - def feature_values(feature_uri) - features = {} - feature = @model.subject(DC["identifier"],feature_uri) - @model.subjects(RDF['type'], OT["Compound"]).each do |compound_node| - compound = @model.object(compound_node, DC["identifier"]).to_s.sub(/^\[(.*)\]$/,'\1') - features[compound] = [] unless features[compound] - data_entry = @model.subject(OT['compound'], compound_node) - @model.find( data_entry, OT['values'], nil ) do |s,p,values| - if feature == @model.object(values, OT['feature']) - value = @model.object(values, OT['value']) - case value.to_s - when "true" - features[compound] << true - when "false" - features[compound] << false - else - features[compound] << value.to_s - end - end - end - end - features - end - def compounds compounds = [] @model.subjects(RDF['type'], OT["Compound"]).each do |compound_node| @@ -183,7 +167,8 @@ module OpenTox :source => self.source, :identifier => self.identifier, :compounds => self.compounds.collect{|c| c.to_s.to_s.sub(/^\[(.*)\]$/,'\1')}, - :features => self.features.collect{|f| f.to_s } + :features => self.features.collect{|f| f.to_s }, + :data => self.data }.to_yaml end diff --git a/lib/environment.rb b/lib/environment.rb index 6100928..a9d0797 100644 --- a/lib/environment.rb +++ b/lib/environment.rb @@ -1,21 +1,27 @@ +require 'logger' # set default environment ENV['RACK_ENV'] = 'test' unless ENV['RACK_ENV'] # load configuration basedir = File.join(ENV['HOME'], ".opentox") config_dir = File.join(basedir, "config") -@@tmp_dir = File.join(basedir, "tmp") config_file = File.join(config_dir, "#{ENV['RACK_ENV']}.yaml") +TMP_DIR = File.join(basedir, "tmp") +LOG_DIR = File.join(basedir, "log") + if File.exist?(config_file) @@config = YAML.load_file(config_file) else - FileUtils.mkdir_p config_dir - FileUtils.mkdir_p @@tmp_dir + FileUtils.mkdir_p TMP_DIR + FileUtils.mkdir_p LOG_DIR FileUtils.cp(File.join(File.dirname(__FILE__), 'templates/config.yaml'), config_file) puts "Please edit #{config_file} and restart your application." exit end +logfile = "#{LOG_DIR}/#{ENV["RACK_ENV"]}.log" +LOGGER = Logger.new(logfile,'daily') # daily rotation +LOGGER.level = Logger::DEBUG # RDF namespaces RDF = Redland::Namespace.new 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' @@ -23,19 +29,6 @@ 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#' -# configure redis database -=begin -begin - case ENV['RACK_ENV'] - when 'production' - @@redis = Redis.new :db => 0 - when 'development' - @@redis = Redis.new :db => 1 - when 'test' - @@redis = Redis.new :db => 2 - @@redis.flush_db - end -rescue - puts "Redis database not running, please start it with 'rake redis:start'." -end -=end +# Regular expressions for parsing classification data +TRUE_REGEXP = /^(true|active|1)/ +FALSE_REGEXP = /^(false|inactive|0)/ diff --git a/lib/model.rb b/lib/model.rb index c4041f2..bd9d546 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -33,7 +33,6 @@ module OpenTox end def self.find(uri) - puts uri yaml = RestClient.get(uri, :accept => "application/x-yaml") OpenTox::Model::Lazar.from_yaml(yaml) end @@ -42,7 +42,8 @@ module OpenTox # I have no idea, why 2 subjects are returned # iterating over all subjects leads to memory allocation problems # SPARQL queries also do not work - me = @model.subjects(RDF['type'],OT[self.owl_class])[1] + #me = @model.subjects(RDF['type'],OT[self.owl_class])[1] + me = @model.subject(RDF['type'],OT[self.owl_class]) @model.object(me, DC['title']).to_s end diff --git a/lib/task.rb b/lib/task.rb index e50a982..24bd944 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -22,6 +22,11 @@ module OpenTox @@config[:services]["opentox-task"] end + def self.all + task_uris = RestClient.get(@@config[:services]["opentox-task"]).split(/\n/) + task_uris.collect{|uri| Task.new(uri)} + end + def started #LOGGER.info File.join(@uri,'started') RestClient.put File.join(@uri,'started'), {} @@ -35,6 +40,14 @@ module OpenTox RestClient.put File.join(@uri,'completed'), :resource => uri end + def created_at + RestClient.get File.join(@uri, 'created_at') + end + + def finished_at + RestClient.get File.join(@uri, 'finished_at') + end + def status RestClient.get File.join(@uri, 'status') end @@ -42,6 +55,10 @@ module OpenTox def resource RestClient.get File.join(@uri, 'resource') end + + def pid=(pid) + RestClient.put File.join(@uri, 'pid'), :pid => pid + end def completed? self.status.to_s == 'completed' @@ -49,7 +66,7 @@ module OpenTox def wait_for_completion until self.completed? - sleep 0.1 + sleep 1 end end diff --git a/lib/tasks/opentox.rb b/lib/tasks/opentox.rb index e330c87..7fce35b 100644 --- a/lib/tasks/opentox.rb +++ b/lib/tasks/opentox.rb @@ -13,7 +13,7 @@ namespace :opentox do when /thin|mongrel|webrick/ port = uri.sub(/^.*:/,'').sub(/\/$/,'') Dir.chdir dir - pid_file = File.join(@@tmp_dir,"#{service}.pid") + 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}." @@ -36,7 +36,7 @@ namespace :opentox do if server =~ /thin|mongrel|webrick/ @@config[:services].each do |service,uri| port = uri.sub(/^.*:/,'').sub(/\/$/,'') - pid_file = File.join(@@tmp_dir,"#{service}.pid") + pid_file = File.join(TMP_DIR,"#{service}.pid") begin puts `#{server} stop -P #{pid_file}` puts "#{service} stopped on localhost:#{port}" @@ -71,7 +71,7 @@ task :start do case server when /thin|mongrel|webrick/ port = @@config[:services][service].sub(/^.*:/,'').sub(/\/$/,'') - pid_file = File.join(@@tmp_dir,"#{service}.pid") + 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}." @@ -93,7 +93,7 @@ task :stop do server = @@config[:webserver] if server =~ /thin|mongrel|webrick/ port = @@config[:services][service].sub(/^.*:/,'').sub(/\/$/,'') - pid_file = File.join(@@tmp_dir,"#{service}.pid") + pid_file = File.join(TMP_DIR,"#{service}.pid") begin puts `thin stop -P #{pid_file}` puts "#{service} stopped on localhost:#{port}" diff --git a/lib/tasks/redis.rb b/lib/tasks/redis.rb deleted file mode 100644 index ed317d3..0000000 --- a/lib/tasks/redis.rb +++ /dev/null @@ -1,125 +0,0 @@ -# Inspired by rabbitmq.rake the Redbox project at http://github.com/rick/redbox/tree/master -require 'fileutils' -require 'open-uri' - -class RedisRunner - - def self.redisdir - "/tmp/redis/" - end - - def self.redisconfdir - '/etc/redis.conf' - end - - def self.dtach_socket - '/tmp/redis.dtach' - end - - # Just check for existance of dtach socket - def self.running? - File.exists? dtach_socket - end - - def self.start - puts 'Detach with Ctrl+\ Re-attach with rake redis:attach' - sleep 3 - exec "dtach -A #{dtach_socket} redis-server #{redisconfdir}" - end - - def self.attach - exec "dtach -a #{dtach_socket}" - end - - def self.stop - sh 'echo "SHUTDOWN" | nc localhost 6379' - end - -end - -namespace :redis do - - desc 'About redis' - task :about do - puts "\nSee http://code.google.com/p/redis/ for information about redis.\n\n" - end - - desc 'Start redis' - task :start do - RedisRunner.start - end - - desc 'Stop redis' - task :stop do - RedisRunner.stop - end - - desc 'Restart redis' - task :restart do - RedisRunner.stop - RedisRunner.start - end - - desc 'Attach to redis dtach socket' - task :attach do - RedisRunner.attach - end - - desc 'Install the lastest verison of Redis from Github (requires git, duh)' - task :install => [:about, :download, :make] do - %w(redis-benchmark redis-cli redis-server).each do |bin| - sh "sudo cp /tmp/redis/#{bin} /usr/bin/" - end - - puts "Installed redis-benchmark, redis-cli and redis-server to /usr/bin/" - - unless File.exists?('/etc/redis.conf') - sh 'sudo cp /tmp/redis/redis.conf /etc/' - puts "Installed redis.conf to /etc/ \n You should look at this file!" - end - end - - task :make do - sh "cd #{RedisRunner.redisdir} && make clean" - sh "cd #{RedisRunner.redisdir} && make" - end - - desc "Download package" - task :download do - sh 'rm -rf /tmp/redis/' if File.exists?("#{RedisRunner.redisdir}/.svn") - sh 'git clone git://github.com/antirez/redis.git /tmp/redis' unless File.exists?(RedisRunner.redisdir) - sh "cd #{RedisRunner.redisdir} && git pull" if File.exists?("#{RedisRunner.redisdir}/.git") - end - -end - -namespace :dtach do - - desc 'About dtach' - task :about do - puts "\nSee http://dtach.sourceforge.net/ for information about dtach.\n\n" - end - - desc 'Install dtach 0.8 from source' - task :install => [:about] do - - Dir.chdir('/tmp/') - unless File.exists?('/tmp/dtach-0.8.tar.gz') - require 'net/http' - - url = 'http://downloads.sourceforge.net/project/dtach/dtach/0.8/dtach-0.8.tar.gz' - open('/tmp/dtach-0.8.tar.gz', 'wb') do |file| file.write(open(url).read) end - end - - unless File.directory?('/tmp/dtach-0.8') - system('tar xzf dtach-0.8.tar.gz') - end - - Dir.chdir('/tmp/dtach-0.8/') - sh 'cd /tmp/dtach-0.8/ && ./configure && make' - sh 'sudo cp /tmp/dtach-0.8/dtach /usr/bin/' - - puts 'Dtach successfully installed to /usr/bin.' - end -end - diff --git a/lib/templates/config.ru b/lib/templates/config.ru deleted file mode 100644 index 81cea9d..0000000 --- a/lib/templates/config.ru +++ /dev/null @@ -1,13 +0,0 @@ -require 'rubygems' -require 'opentox-ruby-api-wrapper' -require 'application.rb' -require 'rack' -require 'rack/contrib' - -FileUtils.mkdir_p @@tmp_dir -log = File.new("#{@@tmp_dir}/#{ENV["RACK_ENV"]}.log", "a+") -$stdout.reopen(log) -$stderr.reopen(log) - -use Rack::ShowExceptions -run Sinatra::Application diff --git a/lib/templates/config.yaml b/lib/templates/config.yaml index 7b46d43..ad7c4ab 100644 --- a/lib/templates/config.yaml +++ b/lib/templates/config.yaml @@ -1,10 +1,20 @@ -:base_dir: /home/ch/webservices -:webserver: thin -:services: -# make sure to enter a full uri (including training slash) - opentox-compound: "http://localhost:4000/" - opentox-feature: "http://localhost:4001/" - opentox-dataset: "http://localhost:4002/" - opentox-algorithm: "http://localhost:4003/" - opentox-model: "http://localhost:4004/" - #opentox-task: "http://localhost:4005/" +# Example configuration for OpenTox, please adjust to your settings +# +# Example 1: Using external test services +# +# :services: +# opentox-compound: "http://webservices.in-silico.ch/test/compound/" +# opentox-dataset: "http://webservices.in-silico.ch/test/dataset/" +# opentox-algorithm: "http://webservices.in-silico.ch/test/algorithm/" +# opentox-model: "http://webservices.in-silico.ch/test/model/" +# opentox-task: "http://webservices.in-silico.ch/test/task/" +# +# Example 2: Using local services +# :base_dir: /home/ch/webservices +# :webserver: thin +# :services: +# opentox-compound: "http://localhost:4000/" +# opentox-dataset: "http://localhost:4001/" +# opentox-algorithm: "http://localhost:4002/" +# opentox-model: "http://localhost:4003/" +# opentox-task: "http://localhost:4004/" |