summaryrefslogtreecommitdiff
path: root/application.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2012-07-26 16:21:02 +0200
committerChristoph Helma <helma@in-silico.ch>2012-07-26 16:21:02 +0200
commit5be059caa873bd2697acc92c0651ca04aa2a5baa (patch)
tree9aee3c4997e0a40cdfd10eb86077a74a5b8a789e /application.rb
parentf85836b4eabe7efe1c29af6bf432cfc3b7b8dd26 (diff)
initial model service
Diffstat (limited to 'application.rb')
-rw-r--r--application.rb122
1 files changed, 10 insertions, 112 deletions
diff --git a/application.rb b/application.rb
index 8d79812..5f38ef8 100644
--- a/application.rb
+++ b/application.rb
@@ -1,116 +1,14 @@
-require 'rubygems'
-gem "opentox-ruby", "~> 4"
-require 'opentox-ruby'
-
-set :lock, true
-
-@@datadir = "data"
-
-class PredictionCache < Ohm::Model
- attribute :compound_uri
- attribute :model_uri
- attribute :dataset_uri
-
- index :compound_uri
- index :model_uri
-end
-
-before do
- @accept = request.env['HTTP_ACCEPT']
- @accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil?
- response['Content-Type'] = @accept
- @id = request.path_info.match(/^\/\d+/)
- unless @id.nil?
- @id = @id.to_s.sub(/\//,'').to_i
-
- @uri = uri @id
- @json_file = "#{@@datadir}/#{@id}.json"
- raise OpenTox::NotFoundError.new "Model #{@id} not found." unless File.exists? @json_file
-
- extension = File.extname(request.path_info)
- unless extension.empty?
- case extension
- when ".html"
- @accept = 'text/html'
- when ".yaml"
- @accept = 'application/x-yaml'
- when ".json"
- @accept = 'application/json'
- when ".rdfxml"
- @accept = 'application/rdf+xml'
- else
- raise OpenTox::NotFoundError.new "File format #{extension} not supported."
+module OpenTox
+ class Application < Service
+ post "/model/:id/?" do
+ [RDF::OT.featureCalculationAlgorithm, RDF::OT.predictionAlgorithm, RDF::OT.similarityAlgorithm, RDF::OT.trainingDataset , RDF::OT.dependentVariables , RDF::OT.featureDataset].each do |param|
+ query = "SELECT ?uri FROM <#{@uri}> WHERE {<#{@uri}> <#{param.to_s}> ?uri}"
+ param_uri = FourStore.query query, "text/uri-list"
+ param_name = param.to_s.split("#").last.underscore
+ params[param_name] = param_uri
end
+ #puts params.inspect
+ RestClientWrapper.post File.join($algorithm[:uri],"lazar","predict"), params
end
end
-
- # make sure subjectid is not included in params, subjectid is set as member variable
- params.delete(:subjectid)
-end
-
-require 'lazar.rb'
-
-helpers do
-
- def next_id
- id = Dir["./#{@@datadir}/*json"].collect{|f| File.basename(f.sub(/.json/,'')).to_i}.sort.last
- id = 0 if id.nil?
- id + 1
- end
-
- def uri(id)
- url_for "/#{id}", :full
- end
-
- def activity(a)
- case a.to_s
- when "true"
- act = "active"
- when "false"
- act = "inactive"
- else
- act = "not available"
- end
- act
- end
-end
-
-get '/?' do # get index of models
- uri_list = Dir["./#{@@datadir}/*json"].collect{|f| File.basename(f.sub(/.json/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n"
- case @accept
- when /html/
- response['Content-Type'] = 'text/html'
- OpenTox.text_to_html uri_list
- else
- response['Content-Type'] = 'text/uri-list'
- uri_list
- end
-end
-
-delete '/:id/?' do
- LOGGER.debug "Deleting model with id "+@id.to_s
- begin
- FileUtils.rm @json_file
- if @subjectid and !File.exists? @json_file and @uri
- begin
- res = OpenTox::Authorization.delete_policies_from_uri(@uri, @subjectid)
- LOGGER.debug "Policy deleted for Model URI: #{@uri} with result: #{res}"
- rescue
- LOGGER.warn "Policy delete error for Model URI: #{@uri}"
- end
- end
- response['Content-Type'] = 'text/plain'
- "Model #{@id} deleted."
- rescue
- raise OpenTox::NotFoundError.new "Model #{@id} does not exist."
- end
-end
-
-
-delete '/?' do
- # TODO delete datasets
- FileUtils.rm Dir["#{@@datadir}/*.json"]
- PredictionCache.all.each {|cache| cache.delete }
- response['Content-Type'] = 'text/plain'
- "All models and cached predictions deleted."
end