summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.de>2009-10-06 10:39:43 +0200
committerChristoph Helma <helma@in-silico.de>2009-10-06 10:39:43 +0200
commit5d5688fbe43806af8523e5481715b5969feeab60 (patch)
treec5b2d26cbc2a2ebffa4606dfded62afd6d4c5821 /lib
parent9f16c1ede56a95368044a333f716d09c0240e5ab (diff)
Passes tests in opentox-test
Diffstat (limited to 'lib')
-rw-r--r--lib/algorithm.rb16
-rw-r--r--lib/compound.rb8
-rw-r--r--lib/dataset.rb8
-rw-r--r--lib/environment.rb19
-rw-r--r--lib/feature.rb16
-rw-r--r--lib/model.rb23
-rw-r--r--lib/opentox-ruby-api-wrapper.rb4
-rw-r--r--lib/opentox.rb1
-rw-r--r--lib/tasks/opentox.rb16
-rw-r--r--lib/templates/config.yaml14
-rw-r--r--lib/utils.rb2
11 files changed, 84 insertions, 43 deletions
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