summaryrefslogtreecommitdiff
path: root/lib/model.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/model.rb')
-rw-r--r--lib/model.rb50
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/model.rb b/lib/model.rb
new file mode 100644
index 0000000..6188cce
--- /dev/null
+++ b/lib/model.rb
@@ -0,0 +1,50 @@
+
+# Get a list of all prediction models
+# @param [Header] Accept one of text/uri-list,
+# @return [text/uri-list] list of all prediction models
+get "/model/?" do
+ models = OpenTox::Model::Prediction.all
+ case @accept
+ when "text/uri-list"
+ uri_list = models.collect{|model| uri("/model/#{model.model_id}")}
+ return uri_list.join("\n") + "\n"
+ when "application/json"
+ models = JSON.parse models.to_json
+ models.each_index do |idx|
+ models[idx][:URI] = uri("/model/#{models[idx]["model_id"]["$oid"]}")
+ models[idx][:crossvalidation_uri] = uri("/crossvalidation/#{models[idx]["crossvalidation_id"]["$oid"]}") if models[idx]["crossvalidation_id"]
+ end
+ return models.to_json
+ else
+ bad_request_error "Mime type #{@accept} is not supported."
+ end
+end
+
+get "/model/:id/?" do
+ model = OpenTox::Model::Lazar.find params[:id]
+ resource_not_found_error "Model with id: #{params[:id]} not found." unless model
+ model[:URI] = uri("/model/#{model.id}")
+ model[:neighbor_algorithm_parameters][:feature_dataset_uri] = uri("/dataset/#{model[:neighbor_algorithm_parameters][:feature_dataset_id]}") if model[:neighbor_algorithm_parameters][:feature_dataset_id]
+ model[:training_dataset_uri] = uri("/dataset/#{model.training_dataset_id}") if model.training_dataset_id
+ model[:prediction_feature_uri] = uri("/dataset/#{model.prediction_feature_id}") if model.prediction_feature_id
+ return model.to_json
+end
+
+
+post "/model/:id/?" do
+ identifier = params[:identifier].split(",")
+ begin
+ # get compound from SMILES
+ compounds = identifier.collect{ |i| Compound.from_smiles i.strip }
+ rescue
+ @error_report = "Attention, '#{params[:identifier]}' is not a valid SMILES string."
+ return @error_report
+ end
+ model = OpenTox::Model::Lazar.find params[:id]
+ batch = {}
+ compounds.each do |compound|
+ prediction = model.predict(compound)
+ batch[compound] = {:id => compound.id, :inchi => compound.inchi, :smiles => compound.smiles, :model => model, :prediction => prediction}
+ end
+ return batch.to_json
+end