diff options
Diffstat (limited to 'lazar.rb')
-rw-r--r-- | lazar.rb | 56 |
1 files changed, 39 insertions, 17 deletions
@@ -3,15 +3,13 @@ require "haml" helpers do def uri_available?(urlStr) url = URI.parse(urlStr) - subjectid = params[:subjectid] if params[:subjectid] - subjectid = request.env['HTTP_SUBJECTID'] if !subjectid and request.env['HTTP_SUBJECTID'] - unless subjectid + unless @subjectid Net::HTTP.start(url.host, url.port) do |http| return http.head(url.request_uri).code == "200" end else Net::HTTP.start(url.host, url.port) do |http| - return http.post(url.request_uri, "subjectid=#{subjectid}").code == "202" + return http.post(url.request_uri, "subjectid=#{@subjectid}").code == "202" end end end @@ -20,6 +18,7 @@ end # Get model representation # @return [application/rdf+xml,application/x-yaml] Model representation get '/:id/?' do + accept = request.env['HTTP_ACCEPT'] accept = "application/rdf+xml" if accept == '*/*' or accept == '' or accept.nil? # workaround for browser links @@ -47,20 +46,45 @@ get '/:id/?' do end end +get '/:id/metadata.?:ext?' do + + metadata = YAML.load(ModelStore.get(params[:id]).yaml).metadata + + accept = request.env['HTTP_ACCEPT'] + accept = "application/rdf+xml" if accept == '*/*' or accept == '' or accept.nil? + if params[:ext] + case params[:ext] + when "yaml" + accept = 'application/x-yaml' + when "rdf", "rdfxml" + accept = 'application/rdf+xml' + end + end + response['Content-Type'] = accept + case accept + when /yaml/ + metadata.to_yaml + else #when /rdf/ and anything else + serializer = OpenTox::Serializer::Owl.new + serializer.add_metadata url_for("/#{params[:id]}",:full), metadata + serializer.to_rdfxml + end + +end + # Store a lazar model. This method should not be called directly, use OpenTox::Algorithm::Lazr to create a lazar model # @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/) - model = ModelStore.create - model.subjectid = params[:subjectid] if params[:subjectid] - model.subjectid = request.env["HTTP_SUBJECTID"] if !model.subjectid and request.env["HTTP_SUBJECTID"] - model.uri = url_for("/#{model.id}", :full) - lazar = YAML.load request.env["rack.input"].read + halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) + model = ModelStore.create + model.subjectid = @subjectid + model.uri = url_for("/#{model.id}", :full) + lazar = YAML.load request.env["rack.input"].read lazar.uri = model.uri - model.yaml = lazar.to_yaml - model.save - model.uri + model.yaml = lazar.to_yaml + model.save + model.uri end # Make a lazar prediction. Predicts either a single compound or all compounds from a dataset @@ -69,8 +93,6 @@ end # @param [optional,Header] Accept Content-type of prediction, can be either `application/rdf+xml or application/x-yaml` # @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do - subjectid = params[:subjectid] if params[:subjectid] - subjectid = request.env["HTTP_SUBJECTID"] if !subjectid and request.env["HTTP_SUBJECTID"] @lazar = YAML.load ModelStore.get(params[:id]).yaml @@ -83,7 +105,7 @@ post '/:id/?' do cache = PredictionCache.first(:model_uri => @lazar.uri, :compound_uri => compound_uri) return cache.dataset_uri if cache and uri_available?(cache.dataset_uri) begin - prediction_uri = @lazar.predict(compound_uri,true,subjectid).uri + 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 @@ -92,7 +114,7 @@ post '/:id/?' do 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 + @lazar.predict_dataset(dataset_uri, @subjectid, task).uri end halt 503,task.uri+"\n" if task.status == "Cancelled" halt 202,task.uri |