summaryrefslogtreecommitdiff
path: root/application.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.de>2009-09-10 15:05:24 +0200
committerChristoph Helma <helma@in-silico.de>2009-09-10 15:05:24 +0200
commit58a4e153cbe073e3c22a0b122326f5f5521a8bf6 (patch)
tree715991649505e2d851b27273fbda69503a4af84a /application.rb
parentd51476f720d4cbd02e9a6f1ddf005916e0f16f64 (diff)
working version with new webservices
Diffstat (limited to 'application.rb')
-rw-r--r--application.rb217
1 files changed, 73 insertions, 144 deletions
diff --git a/application.rb b/application.rb
index d1aa61a..b5d8d4e 100644
--- a/application.rb
+++ b/application.rb
@@ -1,174 +1,103 @@
-load 'environment.rb'
-
-get '/models/?' do # get index of models
- Model.all.collect{ |m| m.uri }.join("\n")
+['rubygems', 'sinatra', 'redis', 'builder', 'opentox-ruby-api-wrapper'].each do |lib|
+ require lib
end
-get '/model/:id' do
- halt 404, "Model #{params[:id]} not found." unless model = Model.get(params[:id])
- halt 202, model.to_yaml unless model.finished
- model.to_yaml
-# builder do |xml|
-# xml.instruct!
-# end
- #xml model
+load File.join(File.dirname(__FILE__), 'model.rb')
+
+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
-post '/models/?' do # create a model
+set :default_content, :yaml
- training_dataset = OpenTox::Dataset.new :uri => params[:dataset_uri]
- model = Model.create(:name => training_dataset.name, :training_dataset_uri => training_dataset.uri)
- model.update_attributes(:uri => url_for("/model/", :full) + model.id.to_s)
+helpers do
- Spork.spork do
- feature_generation = OpenTox::Fminer.new(training_dataset)
- feature_dataset = feature_generation.dataset
- model.feature_dataset_uri = feature_dataset.uri.chomp
- model.finished = true
- model.save
+ def find
+ uri = uri(params[:splat].first)
+ halt 404, "Dataset \"#{uri}\" not found." unless @model = Model.find(uri)
end
-
- model.uri.to_s
-end
-delete '/model/:id' do
- halt 404, "Model #{params[:id]} not found." unless model = Model.get(params[:id])
- model.predictions.each do |p|
- p.neighbors.each { |n| n.destroy }
- p.features.each { |n| f.destroy }
- p.destroy
+ def uri(name)
+ uri = url_for("/model/", :full) + URI.encode(name)
end
- model.destroy
- "Model #{params[:id]} succesfully deleted."
- # TODO: what happens with datasets, avoid stale datasets, but other components might need them
end
-post '/model/:id' do # create prediction
-
- halt 404, "Model #{params[:id]} not found." unless model = Model.get(params[:id])
- query_compound = OpenTox::Compound.new :uri => params[:compound_uri]
- activity_dataset = OpenTox::Dataset.new :uri => model.training_dataset_uri
-
-# database_activities = activity_dataset.features(query_compound)
-
-# if database_activities.size > 0 # return database values
-# database_activities.collect{ |f| f.uri }.join('\n')
-
-# else # make prediction
- prediction = Prediction.find_or_create(:model_uri => model.uri, :compound_uri => params[:compound_uri])
-
- unless prediction.finished # present cached prediction if finished
-
- prediction.update_attributes(:uri => url_for("/prediction/", :full) + prediction.id.to_s)
- Spork.spork do
- feature_dataset = OpenTox::Dataset.new :uri => model.feature_dataset_uri
- compound_descriptors = feature_dataset.all_compounds_and_features_uris
- training_features = feature_dataset.all_features
- compound_activities = activity_dataset.all_compounds_and_features_uris
- query_features = query_compound.match(training_features)
- query_features.each do |f|
- Feature.find_or_create(:feature_uri => f.uri, :prediction_uri => prediction.uri)
- end
- query_feature_uris = query_features.collect{|f| f.uri}
-
- conf = 0.0
- nr_neighbors = 0
-
- compound_descriptors.each do |compound_uri,feature_uris|
- sim = similarity(feature_uris,query_feature_uris)
- if sim > 0.0
- nr_neighbors += 1
- # datamapper default precision is 10, floats with higher precision are not saved
- n = Neighbor.create(:uri => compound_uri, :similarity => (1000*sim).round/1000.0, :prediction_uri => prediction.uri)
- compound_activities[compound_uri].each do |a|
- case OpenTox::Feature.new(:uri => a).value('classification').to_s
- when 'true'
- conf += gauss(sim)
- when 'false'
- conf -= gauss(sim)
- end
- end
- end
- end
- conf = conf/nr_neighbors
- if conf > 0.0
- classification = true
- elsif conf < 0.0
- classification = false
- end
- prediction.update_attributes(:confidence => (1000*conf).round/1000.0, :classification => classification, :finished => true)
-
- end
-
- end
-
- prediction.uri
-# end
+get '/algorithms' do
+ url_for("/algorithm/classification", :full)
end
-# PREDICTIONS
-get '/predictions?' do # get index of predictions
- Prediction.all.collect{ |p| p.uri }.join("\n")
+post '/algorithm/classification/?' do # create a model
+ #halt 403,
+ activity_dataset_uri = OpenTox::Dataset.find(:uri => params[:dataset_uri]).uri
+ feature_dataset_uri = OpenTox::Algorithm::Fminer.create(activity_dataset_uri)
+ Model.create(:activity_dataset_uri => activity_dataset_uri, :feature_dataset_uri => feature_dataset_uri).uri
end
-get '/prediction/:id' do # display prediction
- halt 404, "Prediction #{params[:id]} not found." unless prediction = Prediction.get(params[:id])
- halt 202, prediction.to_yaml unless prediction.finished
- prediction.to_yaml
- #xml prediction
+get '/models/?' do # get index of models
+ Model.find_all.join("\n")
end
-get '/prediction/:id/neighbors' do
- halt 404, "Prediction #{params[:id]} not found." unless prediction = Prediction.get(params[:id])
- halt 202, "Prediction #{params[:id]} not yet finished, please try again later." unless prediction.finished
- #xml Neighbor.all(:prediction_uri => prediction.uri)
- Neighbor.all(:prediction_uri => prediction.uri).to_yaml
+get '/model/*/?' do
+ #halt 404, "Model #{name} not found." unless @model = Model.find(request.url)
+ find
+ @model.to_yaml
end
-get '/prediction/:id/features' do
- halt 404, "Prediction #{params[:id]} not found." unless prediction = Prediction.get(params[:id])
- halt 202, "Prediction #{params[:id]} not yet finished, please try again later." unless prediction.finished
- #xml Feature.all(:prediction_uri => prediction.uri)
- Feature.all(:prediction_uri => prediction.uri).to_yaml
+delete '/model/*' do
+ name = params[:splat].first
+ halt 404, "Model #{name} not found." unless @model = Model.find(request.url)
+ @model.destroy
+ "Model #{params[:id]} succesfully deleted."
end
-delete '/prediction/:id' do
- halt 404, "Prediction #{params[:id]} not found." unless prediction = Prediction.get(params[:id])
- p.neighbors.each { |n| n.destroy }
- p.features.each { |f| f.destroy }
- p.destroy
- "Prediction #{params[:id]} succesfully deleted."
+post '/model/*' do # create prediction
+ name = params[:splat].first
+ halt 404, "Model #{name} not found." unless @model = Model.find(request.url)
+ compound = OpenTox::Compound.new :uri => params[:compound_uri]
+ @model.predict(compound)
end
-# Utility functions
-def similarity(neighbor_features, query_features)
-
- common_features = neighbor_features & query_features
- all_features = neighbor_features | query_features
-
- #common_features.size.to_f/all_features.size.to_f
- sum_p_common = 0.0
- sum_p_all = 0.0
+# PREDICTIONS
+get '/model/*/predictions?' do # get dataset URI
+ name = params[:splat].first
+ halt 404, "Model #{name} not found." unless @model = Model.find(request.url)
+ # Dataset.find
+end
- all_features.each do |f|
- sum_p_all += gauss(OpenTox::Feature.new(:uri => f).value('p_value').to_f)
- end
- common_features.each do |f|
- sum_p_common += gauss(OpenTox::Feature.new(:uri => f).value('p_value').to_f)
- end
- sum_p_common/sum_p_all
+get '/model/*/prediction/*' do # display prediction for a compound
+ name = params[:splat].first
+ compound_uri = params[:splat][1]
+ halt 404, "Model #{name} not found." unless @model = Model.find(request.url)
+ # prediction not found
+ #prediction.to_yaml
+ #xml prediction
+end
+get '/model/*/prediction/*/neighbors' do
+ name = params[:splat].first
+ compound_uri = params[:splat][1]
+ halt 404, "Model #{name} not found." unless @model = Model.find(request.url)
+ # prediction not found
+ # prediction.neighbors
end
-# gauss kernel
-def gauss(sim, sigma = 0.3)
- x = 1.0 - sim
- Math.exp(-(x*x)/(2*sigma*sigma))
+get '/model/*/prediction/*/features' do
+ name = params[:splat].first
+ compound_uri = params[:splat][1]
+ halt 404, "Model #{name} not found." unless @model = Model.find(request.url)
+ # prediction not found
+ # prediction not found
+ # prediction.features
end
-def xml(object)
- builder do |xml|
- xml.instruct!
- object.to_xml
- end
+delete '/model/*/prediction/*' do # display prediction for a compound
+ name = params[:splat].first
+ halt 404, "Model #{name} not found." unless @model = Model.find(request.url)
+ # Prediction.destroy
end