diff options
author | mr <mr@mrautenberg.de> | 2011-08-04 18:35:25 +0200 |
---|---|---|
committer | mr <mr@mrautenberg.de> | 2011-08-04 18:35:25 +0200 |
commit | 0ff17e212eddea188ca15ea8e050e1cf1dd8ee68 (patch) | |
tree | f0d29cb5100636c5ba5566895f2c86943bce9882 | |
parent | 8662d0b2f43758d9dcbff753bdaea18775cd1877 (diff) | |
parent | 400f8a894bbd0f781735fd94e9707ef8ff8fc577 (diff) |
Merge branch 'release/v2.1.0'
-rw-r--r-- | application.rb | 8 | ||||
-rw-r--r-- | lazar.rb | 98 |
2 files changed, 70 insertions, 36 deletions
diff --git a/application.rb b/application.rb index 96c6267..7d3de38 100644 --- a/application.rb +++ b/application.rb @@ -23,7 +23,7 @@ before do @uri = uri @id @yaml_file = "public/#{@id}.yaml" - halt 404, "Dataset #{@id} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{@id} not found." unless File.exists? @yaml_file end # make sure subjectid is not included in params, subjectid is set as member variable @@ -69,15 +69,15 @@ delete '/:id/?' do if @subjectid and !File.exists? @yaml_file and @uri begin res = OpenTox::Authorization.delete_policies_from_uri(@uri, @subjectid) - LOGGER.debug "Policy deleted for Dataset URI: #{@uri} with result: #{res}" + LOGGER.debug "Policy deleted for Model URI: #{@uri} with result: #{res}" rescue - LOGGER.warn "Policy delete error for Dataset URI: #{@uri}" + LOGGER.warn "Policy delete error for Model URI: #{@uri}" end end response['Content-Type'] = 'text/plain' "Model #{@id} deleted." rescue - halt 404, "Model #{@id} does not exist." + raise OpenTox::NotFoundError.new "Model #{@id} does not exist." end end @@ -3,23 +3,28 @@ require "haml" # Get model representation # @return [application/rdf+xml,application/x-yaml] Model representation get '/:id/?' do - halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file case @accept when /application\/rdf\+xml/ + response['Content-Type'] = 'application/rdf+xml' s = OpenTox::Serializer::Owl.new - s.add_model(@uri,YAML.load_file(@yaml_file).metadata) + metadata = YAML.load_file(@yaml_file).metadata + s.add_model(@uri,metadata) s.to_rdfxml when /yaml/ + response['Content-Type'] = 'application/x-yaml' File.read @yaml_file when /html/ + response['Content-Type'] = 'text/html' OpenTox.text_to_html File.read(@yaml_file) else - halt 400, "Unsupported MIME type '#{@accept}'" + raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" end end get '/:id/metadata.?:ext?' do - halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file + @accept = "application/x-yaml" if params[:ext] and params[:ext].match(/yaml/) metadata = YAML.load_file(@yaml_file).metadata case @accept when /yaml/ @@ -31,8 +36,9 @@ get '/:id/metadata.?:ext?' do end end -get '/:id/dependent' do - halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file +get '/:id/dependent.?:ext?' do + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file + @accept = "application/x-yaml" if params[:ext] and params[:ext].match(/yaml/) feature_uri = YAML.load_file(@yaml_file).metadata[OT.dependentVariables] case @accept when /yaml/ @@ -44,28 +50,50 @@ get '/:id/dependent' do when /html/ OpenTox.text_to_html OpenTox::Feature.find(feature_uri).to_yaml else - halt 400, "Unsupported MIME type '#{@accept}'" + raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" end end -get '/:id/predicted' do - halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file - return feature_uri if @accept == "text/uri-list" - predicted = OpenTox::Feature.new(File.join @uri,"predicted") - dependent = OpenTox::Feature.find(YAML.load_file(@yaml_file).metadata[OT.dependentVariables]) - predicted.metadata[RDF.type] = dependent.metadata[RDF.type] - #predicted.metadata[OT.hasSource] = @uri - #predicted.metadata[DC.creator] = @uri - predicted.metadata[DC.title] = dependent.metadata[DC.title] +get '/:id/predicted/:prop' do + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file + if params[:prop] == "value" or params[:prop] == "confidence" + feature = eval "YAML.load_file(@yaml_file).prediction_#{params[:prop]}_feature" + case @accept + when /yaml/ + content_type "application/x-yaml" + feature.metadata.to_yaml + when /rdf/ + content_type "application/rdf+xml" + feature.to_rdfxml + when /html/ + content_type "text/html" + OpenTox.text_to_html feature.metadata.to_yaml + else + raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" + end + else + raise OpenTox::BadRequestError.new "Unknown URI #{@uri}" + end +end + +get '/:id/predicted.?:ext?' do + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file + @accept = "application/x-yaml" if params[:ext] and params[:ext].match(/yaml/) + features = YAML.load_file(@yaml_file).prediction_features case @accept + when "text/uri-list" + "#{features.collect{|f| f.uri}.join("\n")}\n" when /yaml/ - predicted.to_yaml + features.to_yaml when /rdf/ - predicted.to_rdfxml + serializer = OpenTox::Serializer::Owl.new + features.each{|f| serializer.add_feature(f.uri,f.metadata)} + serializer.to_rdfxml + #feature.to_rdfxml when /html/ - OpenTox.text_to_html predicted.to_yaml + OpenTox.text_to_html features.to_yaml else - halt 400, "Unsupported MIME type '#{@accept}'" + raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" end end @@ -73,12 +101,15 @@ end # @param [Body] lazar Model representation in YAML format # @return [String] Model URI post '/?' do # create model - halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) + raise OpenTox::BadRequestError.new "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) @id = next_id @uri = uri @id @yaml_file = "public/#{@id}.yaml" lazar = YAML.load request.env["rack.input"].read lazar.uri = @uri + value_feature_uri = File.join( @uri, "predicted", "value") + confidence_feature_uri = File.join( @uri, "predicted", "confidence") + lazar.metadata[OT.predictedVariables] = [value_feature_uri, confidence_feature_uri] File.open(@yaml_file,"w+"){|f| f.puts lazar.to_yaml} OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @yaml_file response['Content-Type'] = 'text/uri-list' @@ -92,29 +123,32 @@ end # @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do - halt 404, "Model #{params[:id]} does not exist." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} does not exist." unless File.exists? @yaml_file - halt 404, "No compound_uri or dataset_uri parameter." unless compound_uri = params[:compound_uri] or dataset_uri = params[:dataset_uri] + raise OpenTox::NotFoundError.new "No compound_uri or dataset_uri parameter." unless compound_uri = params[:compound_uri] or dataset_uri = params[:dataset_uri] @lazar = YAML.load_file @yaml_file response['Content-Type'] = 'text/uri-list' if compound_uri cache = PredictionCache.find(:model_uri => @lazar.uri, :compound_uri => compound_uri).first - return cache.dataset_uri if cache and uri_available?(cache.dataset_uri) - begin - prediction_uri = @lazar.predict(compound_uri,true,@subjectid).uri - PredictionCache.create(:model_uri => @lazar.uri, :compound_uri => compound_uri, :dataset_uri => prediction_uri) - prediction_uri - rescue - LOGGER.error "Lazar prediction failed for #{compound_uri} with #{$!} " - halt 500, "Prediction of #{compound_uri} with #{@lazar.uri} failed." + if cache and uri_available?(cache.dataset_uri) + return cache.dataset_uri + else + begin + prediction_uri = @lazar.predict(compound_uri,true,@subjectid).uri + PredictionCache.create(:model_uri => @lazar.uri, :compound_uri => compound_uri, :dataset_uri => prediction_uri) + prediction_uri + rescue + LOGGER.error "Lazar prediction failed for #{compound_uri} with #{$!} " + raise "Prediction of #{compound_uri} with #{@lazar.uri} failed." + end end elsif dataset_uri task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do |task| @lazar.predict_dataset(dataset_uri, @subjectid, task).uri end - halt 503,task.uri+"\n" if task.status == "Cancelled" + raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled" halt 202,task.uri end |