From 5d5688fbe43806af8523e5481715b5969feeab60 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 6 Oct 2009 10:39:43 +0200 Subject: Passes tests in opentox-test --- Rakefile | 2 + lib/algorithm.rb | 16 ++++--- lib/compound.rb | 8 ++-- lib/dataset.rb | 8 +++- lib/environment.rb | 19 +++++++- lib/feature.rb | 16 ++++--- lib/model.rb | 23 +++++++--- lib/opentox-ruby-api-wrapper.rb | 4 +- lib/opentox.rb | 1 - lib/tasks/opentox.rb | 16 +++++-- lib/templates/config.yaml | 14 +++--- lib/utils.rb | 2 - opentox-ruby-api-wrapper.gemspec | 17 +++---- test/hamster_carcinogenicity.csv | 85 ----------------------------------- test/opentox-ruby-api-wrapper_test.rb | 47 ------------------- test/start-local-webservices.rb | 13 ------ test/test_helper.rb | 10 ----- 17 files changed, 91 insertions(+), 210 deletions(-) delete mode 100644 test/hamster_carcinogenicity.csv delete mode 100644 test/opentox-ruby-api-wrapper_test.rb delete mode 100755 test/start-local-webservices.rb delete mode 100644 test/test_helper.rb diff --git a/Rakefile b/Rakefile index 6f71f5d..99d2812 100644 --- a/Rakefile +++ b/Rakefile @@ -11,6 +11,8 @@ begin gem.homepage = "http://github.com/helma/opentox-ruby-api-wrapper" gem.authors = ["Christoph Helma"] gem.add_dependency "technoweenie-rest-client" + gem.add_dependency "sinatra" + gem.files = FileList["[A-Z]*", "{bin,generators,lib,test}/**/*", 'lib/jeweler/templates/.gitignore'] gem.files.include %w(lib/tasks/opentox.rb, lib/environment.rb, lib/algorithm.rb, lib/compound.rb, lib/dataset.rb, lib/feature.rb, lib/model.rb, lib/utils.rb, lib/templates/*) # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings diff --git a/lib/algorithm.rb b/lib/algorithm.rb index 7007e3a..43f7ede 100644 --- a/lib/algorithm.rb +++ b/lib/algorithm.rb @@ -3,20 +3,24 @@ module OpenTox class Fminer < OpenTox # Create a new dataset with BBRC features - def self.create(training_dataset_uri) - RestClient.post @@config[:services]["opentox-fminer"], :dataset_uri => training_dataset_uri + def self.create(params) + puts params[:dataset_uri] + uri = RestClient.post File.join(@@config[:services]["opentox-algorithm"],'fminer'), :dataset_uri => params[:dataset_uri] + print "fminer finsihed " + puts uri + uri end end class Similarity < OpenTox def self.tanimoto(dataset1,compound1,dataset2,compound2) - RestClient.get File.join(@@config[:services]["opentox-dataset"], 'algorithm/tanimoto/dataset',dataset1.name,compound1.inchi,'dataset',dataset2.name,compound2.inchi) + RestClient.get File.join(@@config[:services]["opentox-algorithm"], 'tanimoto/dataset',dataset1.name,compound1.inchi,'dataset',dataset2.name,compound2.inchi) end def self.weighted_tanimoto(dataset1,compound1,dataset2,compound2) # URI.escape does not work here - uri = File.join(@@config[:services]["opentox-dataset"], 'algorithm/weighted_tanimoto/dataset',CGI.escape(dataset1.name),'compound',CGI.escape(compound1.inchi),'dataset',CGI.escape(dataset2.name),'compound',CGI.escape(compound2.inchi)) + uri = File.join(@@config[:services]["opentox-algorithm"], 'weighted_tanimoto/dataset',CGI.escape(dataset1.name),'compound',CGI.escape(compound1.inchi),'dataset',CGI.escape(dataset2.name),'compound',CGI.escape(compound2.inchi)) RestClient.get uri end @@ -24,8 +28,8 @@ module OpenTox class Lazar < OpenTox # Create a new prediction model from a dataset - def initialize(params) - @uri = RestClient.post @@config[:services]["opentox-lazar"] + 'models' , :dataset_uri => params[:dataset_uri] + def self.create(params) + RestClient.post File.join(@@config[:services]["opentox-algorithm"],"lazar_classification"), params end end diff --git a/lib/compound.rb b/lib/compound.rb index 67c8004..c4ba8d9 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -10,13 +10,13 @@ module OpenTox @@cactus_uri="http://cactus.nci.nih.gov/chemical/structure/" if params[:smiles] @inchi = smiles2inchi(params[:smiles]) - @uri = File.join(@@config[:services]["opentox-dataset"],"compound",@inchi) + @uri = File.join(@@config[:services]["opentox-compound"],URI.escape(@inchi)) elsif params[:inchi] - @inchi = inchi - @uri = File.join(@@config[:services]["opentox-dataset"],"compound",@inchi) + @inchi = params[:inchi] + @uri = File.join(@@config[:services]["opentox-compound"],URI.escape(@inchi)) elsif params[:name] @inchi = RestClient.get "#{@@cactus_uri}#{params[:name]}/stdinchi" - @uri = File.join(@@config[:services]["opentox-dataset"],"compound",@inchi) + @uri = File.join(@@config[:services]["opentox-compound"],URI.escape(@inchi)) elsif params[:uri] @inchi = params[:uri].sub(/^.*InChI/, 'InChI') @uri = params[:uri] diff --git a/lib/dataset.rb b/lib/dataset.rb index b635985..754e7f4 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -14,7 +14,7 @@ module OpenTox end def self.create(params) - uri = RestClient.post File.join(@@config[:services]["opentox-dataset"],"datasets"), :name => params[:name] + uri = RestClient.post @@config[:services]["opentox-dataset"], :name => params[:name] Dataset.new(uri.to_s) end @@ -36,6 +36,10 @@ module OpenTox self.create(params) unless self.find(params) end + def self.base_uri + @@config[:services]["opentox-dataset"] + end + def import(params) if params[:csv] # RestClient seems not to work for file uploads @@ -45,7 +49,7 @@ module OpenTox end def add(features) - RestClient.post @uri, :features => features.to_yaml + RestClient.put @uri, :features => features end # Get all compounds from a dataset diff --git a/lib/environment.rb b/lib/environment.rb index c65e968..3a9319d 100644 --- a/lib/environment.rb +++ b/lib/environment.rb @@ -1,7 +1,7 @@ -# load configuration - +# 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") @@ -16,3 +16,18 @@ else puts "Please edit #{config_file} and restart your application." exit end + +# configure redis database +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 diff --git a/lib/feature.rb b/lib/feature.rb index 8b0839f..a3ba333 100644 --- a/lib/feature.rb +++ b/lib/feature.rb @@ -9,31 +9,33 @@ module OpenTox if params[:uri] @uri = params[:uri] items = URI.split(@uri)[5].split(/\//) - @name = items[2] + @name = items[1] @values = {} - i = 4 + i = 2 while i < items.size @values[items[i]] = items[i+1] i += 2 end else @name = params[:name] - #@name = URI.encode(URI.decode(params[:name])) - @values = params[:values] - @uri = File.join(@@config[:services]["opentox-dataset"],"feature",path) + @values = {} + params.each do |k,v| + @values[k] = v unless k.to_s == 'name' + end + @uri = File.join(@@config[:services]["opentox-feature"],path) end end def values_path path = '' @values.each do |k,v| - path += '/' + URI.encode(k.to_s) + '/' + URI.encode(v.to_s) + path = File.join path, URI.encode(k.to_s), URI.encode(v.to_s) end path end def path - File.join(@name,values_path) + File.join(URI.encode(@name),values_path) end def value(property) diff --git a/lib/model.rb b/lib/model.rb index 0fa3be6..ec4d069 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -1,22 +1,27 @@ module OpenTox - # key: /models - # set: dataset uris module Model - class Lazar < OpenTox + class LazarClassification < OpenTox # Create a new prediction model from a dataset - def initialize(params) - super(params[:uri]) + def initialize(uri) + super(uri) + end + + def self.create(params) + uri = RestClient.post File.join(@@config[:services]["opentox-model"], 'lazar_classification'), params + puts "URI: " + uri + LazarClassification.new(uri.to_s) end def self.find(name) - RestClient.get File.join(@@config[:services]["opentox-lazar"], 'model', URI.encode(params[:name])) + uri = RestClient.get File.join(@@config[:services]["opentox-model"], 'lazar_classification', URI.encode(params[:name])) + LazarClassification.new(uri) end def self.find_all - RestClient.get File.join(@@config[:services]["opentox-lazar"], 'models')#.split("\n") + RestClient.get File.join(@@config[:services]["opentox-model"], 'lazar_classification')#.split("\n") end # Predict a compound @@ -24,6 +29,10 @@ module OpenTox LazarPrediction.new(:uri => RestClient.post(@uri, :compound_uri => compound.uri)) end + def self.base_uri + @@config[:services]["opentox-model"] + end + end end diff --git a/lib/opentox-ruby-api-wrapper.rb b/lib/opentox-ruby-api-wrapper.rb index fd68e72..3e66528 100644 --- a/lib/opentox-ruby-api-wrapper.rb +++ b/lib/opentox-ruby-api-wrapper.rb @@ -1,7 +1,7 @@ -['rubygems', 'sinatra', 'sinatra/url_for', 'builder', 'rest_client', 'yaml', 'cgi', 'openbabel'].each do |lib| +['rubygems', 'sinatra', 'sinatra/url_for', 'redis','builder', 'rest_client', 'yaml', 'cgi', 'openbabel', 'spork', 'environment'].each do |lib| require lib end -['environment', 'opentox', 'compound','feature','dataset','algorithm','model','utils'].each do |lib| +['opentox', 'compound','feature','dataset','algorithm','model','task','utils'].each do |lib| require lib end diff --git a/lib/opentox.rb b/lib/opentox.rb index 1a12123..9b1226b 100644 --- a/lib/opentox.rb +++ b/lib/opentox.rb @@ -25,4 +25,3 @@ module OpenTox end end - diff --git a/lib/tasks/opentox.rb b/lib/tasks/opentox.rb index 0dace9a..4cac9a5 100644 --- a/lib/tasks/opentox.rb +++ b/lib/tasks/opentox.rb @@ -1,4 +1,16 @@ -load File.join(File.dirname(__FILE__), '..', 'environment.rb') +require File.join(File.dirname(__FILE__), '..', 'opentox-ruby-api-wrapper.rb') +require File.join(File.dirname(__FILE__), 'redis.rb') + +namespace :redis do + + desc "Flush Redis testing database" + task :flush do + require 'redis' + r = Redis.new :db => 2 + r.flushdb + end + +end namespace :opentox do @@ -19,8 +31,6 @@ namespace :opentox do @@config[:services].each do |service,uri| dir = File.join(@@config[:base_dir], service) server = @@config[:webserver] - #puts "Starting Redis database" - #puts `redis-server /etc/redis.conf &` case server when /thin|mongrel|webrick/ port = uri.sub(/^.*:/,'').sub(/\/$/,'') diff --git a/lib/templates/config.yaml b/lib/templates/config.yaml index 14326fe..7b46d43 100644 --- a/lib/templates/config.yaml +++ b/lib/templates/config.yaml @@ -1,10 +1,10 @@ :base_dir: /home/ch/webservices :webserver: thin :services: -# make sure to provide a full uri (including training slash) - opentox-feature: "http://localhost:5000/" - opentox-compound: "http://localhost:5001/" - opentox-dataset: "http://localhost:5002/" - opentox-fminer: "http://localhost:5003/" - opentox-similarity: "http://localhost:5004/" - opentox-lazar: "http://localhost:5005/" +# 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/" diff --git a/lib/utils.rb b/lib/utils.rb index 2716f45..cccb5ea 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -1,11 +1,9 @@ module OpenTox module Utils - # gauss kernel def self.gauss(sim, sigma = 0.3) x = 1.0 - sim Math.exp(-(x*x)/(2*sigma*sigma)) end - end end diff --git a/opentox-ruby-api-wrapper.gemspec b/opentox-ruby-api-wrapper.gemspec index fd8094f..aa23416 100644 --- a/opentox-ruby-api-wrapper.gemspec +++ b/opentox-ruby-api-wrapper.gemspec @@ -5,11 +5,11 @@ Gem::Specification.new do |s| s.name = %q{opentox-ruby-api-wrapper} - s.version = "1.0.0" + s.version = "1.1.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Christoph Helma"] - s.date = %q{2009-09-11} + s.date = %q{2009-10-06} s.description = %q{Ruby wrapper for the OpenTox REST API (http://www.opentox.org)} s.email = %q{helma@in-silico.ch} s.extra_rdoc_files = [ @@ -31,27 +31,20 @@ Gem::Specification.new do |s| "lib/opentox-ruby-api-wrapper.rb", "lib/opentox.rb", "lib/spork.rb", + "lib/task.rb", "lib/tasks/opentox.rb", + "lib/tasks/redis.rb", "lib/templates/config.ru", "lib/templates/config.ru", "lib/templates/config.yaml", "lib/templates/config.yaml", - "lib/utils.rb", - "test/hamster_carcinogenicity.csv", - "test/opentox-ruby-api-wrapper_test.rb", - "test/start-local-webservices.rb", - "test/test_helper.rb" + "lib/utils.rb" ] s.homepage = %q{http://github.com/helma/opentox-ruby-api-wrapper} s.rdoc_options = ["--charset=UTF-8"] s.require_paths = ["lib"] s.rubygems_version = %q{1.3.5} s.summary = %q{Ruby wrapper for the OpenTox REST API} - s.test_files = [ - "test/test_helper.rb", - "test/opentox-ruby-api-wrapper_test.rb", - "test/start-local-webservices.rb" - ] if s.respond_to? :specification_version then current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION diff --git a/test/hamster_carcinogenicity.csv b/test/hamster_carcinogenicity.csv deleted file mode 100644 index 009808f..0000000 --- a/test/hamster_carcinogenicity.csv +++ /dev/null @@ -1,85 +0,0 @@ -CC=O,true -C12C3=C(C=CC=C3)CC1=CC(=CC=2)NC(C)=O,true -O=C(N)\C(C2=CC=CO2)=C/C1=CC=C([N+]([O-])=O)O1,true -C1(N=CNN=1)N,false -Br(=O)(=O)[O-].[K+],true -[Cl-].[Cd+2].[Cl-],false -O=S(=O)([O-])[O-].[Cd+2],false -ClC1=CC(=NC(=N1)SCC(=O)O)NC2=CC=CC(=C2C)C,false -ClCOC,true -C=C(Cl)C=C,false -Clc1ccc(cc1)c2ccc(COC(C)(C)C(O)=O)cc2,false -O=C1OC2=C(C=CC=C2)C=C1,false -ClC(=C(C1=CC=C(C=C1)Cl)C2=CC=C(C=C2)Cl)Cl,true -ClC(C(C1=CC=C(C=C1)Cl)C2=CC=C(C=C2)Cl)(Cl)Cl,false -C=CCN(CC=C)N=O,true -Cl\C2=C(/Cl)C3(Cl)C1C4CC(C1C2(Cl)C3(Cl)Cl)C5OC45,false -O=C(N(C)C)Cl,true -CN(C)N,true -N(NC)C.[H]Cl.[H]Cl,true -CCO,false -O=C(N(CC)N=O)NCCO,true -O=C(N(CC)N=O)NCC(=O)C,true -C=O,false -[O-][N+](=O)C1=CC=C(O1)C2=CSC(=N2)NNC=O,true -O=CC1=CC=CO1,false -OCC1CO1,true -O=C2C1=C(OC)C=C(OC)C(Cl)=C1O[C@]32C(OC)=CC(C[C@@](C)3[H])=O,false -ClC1=C(C(=C(C(=C1Cl)Cl)Cl)Cl)Cl,true -NN,true -OS(=O)(=O)O.NN,true -CC(=O)N(O)C1=CC2=C(C=C1)C3=CC=CC=C3C2,true -OCCNN,false -O=C(C1=CC=NC=C1)NN,false -OC(=O)C1=CC=NC=C1,false -O=C(NC1=CC=CC(=C1)Cl)OC(C)C,false -O=C(NC1=CC=CC=C1)OC(C)C,false -[O-]C(C)=O.[O-]C(C)=O.[Pb+2].[OH-].[OH-].[Pb+2].[OH-].[OH-].[Pb+2],false -CN(C)CCN(CC2=CC=CS2)C1=NC=CC=C1.Cl,false -NC1=C2C(=NC(=N1)N)N=CC(=N2)CN(C3=CC=C(C=C3)C(=O)N[C@@H](CCC(=O)O)C(=O)O)C,false -CN(N)C=O,true -O=C(C(=C)C)OC,false -CNN,true -O=C(C1=CC=CN=C1)CCCN(N=O)C,false -CC1=CC(=O)NC(=S)N1,true -CC(C(O)=O)(OC1=CC=C(C=C1)C2CCCC3=C2C=CC=C3)C,false -O=N[O-].[Na+],false -[O-][N+](C1=CC=C(C2=CSC(NC(C)=O)=N2)O1)=O,true -[O-][N+](=O)C1=CC=C(O1)C2=CSC(=N2)NC=O,true -O=[N+](C1=CC=C2C3=C1C=CC=C3CC2)[O-],false -N(CC(CO)O)(CC(O)C)N=O,true -N(CC(CO)O)(CC(C)=O)N=O,true -N(CC(CO)O)(CCO)N=O,false -O=C(C)CN(N=O)CCO,true -C1C(N(C(CN1N=O)C)C)C,true -N(CC(C)=O)(CC=C)N=O,true -N(CC(CO)O)(C)N=O,true -O=NN1CCOCC1,true -N1C=CC=C(C=1)C2N(N=O)CCC2,true -C1=CC=C(C=[N+]1[O-])C2CCCN2N=O,false -O=NN1CCCCC1,true -O=NN1CCCC1,true -O=C(N(CC(C)=O)N=O)NCCCl,true -N(C(=O)N)(N=O)CC(C)=O,true -C1(CCN=C=S)=CC=CC=C1,false -O=C1C(C2=CC=CC=C2)(C(=O)NC(=O)N1)CC,false -C1=C2C(=CC=C1NC3=CC=CC=C3)C=CC=C2,false -O=C1N2C(C3=C(C=CC=C3)CC2)CN(C1)C(=O)C4CCCCC4,false -C1(=CC(=C(O)C=C1)O)C(O)=O,false -O=C1C2=C(C=C(C=C2O)O)O/C(=C\1O)C3=CC(=C(C=C3)O)O.O.O,false -C1=C(C=CC(=C1)C(C2=CC=C(N)C(=C2)C)=C3C=CC(=N)C=C3)N.[H]Cl,false -C(C1=CC=C(C=C1)N)(C2=CC=C(C=C2)N)=C3C=CC(C=C3)=N.[H]Cl,false -OC2=CC1=C(C(O)=C2)C(C(O[C@@H]4O[C@@H]([C@H]([C@H](O)[C@H]4O)O)CO[C@H]3[C@H](O)[C@H](O)[C@H]([C@H](C)O3)O)=C(C5=CC(O)=C(C=C5)O)O1)=O,false -ClC(=CCl)Cl,false -NC(=O)OCC,true -C=CCl,true -N#[N+]C1=CC=CC=C1.F[B-](F)(F)F,false -C1(CN(CC(N1N=O)C)N=O)C,true -N(CCN(C)C)(C)N=O,true -C1(CN(N=O)CC(O1)C)C,true -O1C(N(CC1C)N=O)=O,true -CCOC(=O)N(C)N=O,true -C1N(COC1)N=O,true -O=C(N(CCC1=CC=CC=C1)N=O)N,true -O=NN1CCC1,true -F[B-](F)(F)F.[Na+],false diff --git a/test/opentox-ruby-api-wrapper_test.rb b/test/opentox-ruby-api-wrapper_test.rb deleted file mode 100644 index 0a394e2..0000000 --- a/test/opentox-ruby-api-wrapper_test.rb +++ /dev/null @@ -1,47 +0,0 @@ -require File.join(File.dirname(__FILE__), 'test_helper.rb') - -class OpentoxRubyApiWrapperTest < Test::Unit::TestCase - - def setup - if ENV['LOCAL'] - port = 5000 - [ "opentox-compound", "opentox-feature" , "opentox-dataset" , "opentox-fminer" , "opentox-lazar" ].each do |component| - ENV[component.upcase.gsub(/-/,'_')] = "http://localhost:#{port}/" - port += 1 - end - end - ENV['OPENTOX'] = "test" - end - - def test_create_dataset_and_model_and_make_a_prediction - dataset = OpenTox::Dataset.new :name => "Hamster Carcinogenicity", :filename => "test/hamster_carcinogenicity.csv" - puts dataset.uri - wait_for_completion dataset - assert_match(/#{ENV['OPENTOX_DATASET']}\d+$/,dataset.uri) - assert_equal("Hamster Carcinogenicity",dataset.name) - assert_equal(true,dataset.finished?) - lazar = OpenTox::Lazar.new :dataset_uri => dataset.uri - puts lazar.uri - wait_for_completion lazar - assert_equal(true,lazar.finished?) - assert_match(/#{ENV['OPENTOX_LAZAR']}model\/\d+$/,lazar.uri) - query_structure = OpenTox::Compound.new :smiles => 'c1ccccc1NN' - puts query_structure.uri - prediction = lazar.predict query_structure - puts prediction.uri - wait_for_completion prediction - puts prediction.classification - puts prediction.confidence - puts prediction.neighbors - puts prediction.features - assert_equal(true, prediction.classification) - assert_match(/0\.\d+/, prediction.confidence.to_s) - end - -end - -def wait_for_completion(object) - while (!object.finished?) - sleep 1 - end -end diff --git a/test/start-local-webservices.rb b/test/start-local-webservices.rb deleted file mode 100755 index 66dea29..0000000 --- a/test/start-local-webservices.rb +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env ruby -require 'fileutils' - -port = 5000 -[ "opentox-compound", "opentox-feature" , "opentox-dataset" , "opentox-fminer" , "opentox-lazar" ].each do |component| - ENV[component.upcase.gsub(/-/,'_')] = "http://localhost:#{port}/" - Dir.chdir ENV['HOME'] + '/webservices/' + component - Dir["test.sqlite3"].each { |f| FileUtils.rm_rf(f) } - file = 'application.rb' - pid = fork {`urxvt -title #{component} -e thin --debug --rackup config.ru start -p #{port} -e test`} - Process.detach(pid) - port += 1 -end diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index 4911cf4..0000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -require 'rubygems' -require 'test/unit' - -$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) -$LOAD_PATH.unshift(File.dirname(__FILE__)) - -require 'opentox-ruby-api-wrapper' - -class Test::Unit::TestCase -end -- cgit v1.2.3