diff options
author | mr <mr@mrautenberg.de> | 2011-09-23 12:14:28 +0200 |
---|---|---|
committer | mr <mr@mrautenberg.de> | 2011-09-23 12:14:28 +0200 |
commit | c9b74b70171a75c07e4be04d2f00d30dcfae9af7 (patch) | |
tree | 5e825796571d1937588970c30b23eeffd0a34316 /lazar.rb | |
parent | 0ff17e212eddea188ca15ea8e050e1cf1dd8ee68 (diff) | |
parent | a8b6b8debe373b3c936d6cbf863c38ff2862441b (diff) |
Merge branch 'release/v3.0.0'
Diffstat (limited to 'lazar.rb')
-rw-r--r-- | lazar.rb | 95 |
1 files changed, 58 insertions, 37 deletions
@@ -1,63 +1,85 @@ require "haml" +helpers do + + def yaml_file(id) + file = "#{@@datadir}/#{id}.yaml" + unless File.exists? file # lazy yaml generation + lazar = OpenTox::Model::Lazar.from_json(File.read(@json_file)) + File.open(file,"w+") { |f| f.puts lazar.to_yaml } + end + file + end + + def lazar + OpenTox::Model::Lazar.from_json(File.read(@json_file)) + end + + #def metadata + #lazar.metadata + #end + +end + # Get model representation -# @return [application/rdf+xml,application/x-yaml] Model representation +# @return [application/rdf+xml,application/x-yaml,aapplication/json] Model representation get '/:id/?' do - raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @json_file case @accept when /application\/rdf\+xml/ response['Content-Type'] = 'application/rdf+xml' s = OpenTox::Serializer::Owl.new - metadata = YAML.load_file(@yaml_file).metadata - s.add_model(@uri,metadata) + s.add_model(@uri,lazar.metadata) s.to_rdfxml when /yaml/ - response['Content-Type'] = 'application/x-yaml' - File.read @yaml_file + send_file yaml_file(params[:id]), :type => 'application/x-yaml' + when /json/ + send_file @json_file, :type => 'application/json' when /html/ response['Content-Type'] = 'text/html' - OpenTox.text_to_html File.read(@yaml_file) + #OpenTox.text_to_html File.read(yaml_file(params[:id])) + OpenTox.text_to_html JSON.pretty_generate(JSON.parse(File.read(@json_file))) else raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" end end get '/:id/metadata.?:ext?' do - raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @json_file @accept = "application/x-yaml" if params[:ext] and params[:ext].match(/yaml/) - metadata = YAML.load_file(@yaml_file).metadata case @accept when /yaml/ - metadata.to_yaml + lazar.metadata.to_yaml else #when /rdf/ and anything else serializer = OpenTox::Serializer::Owl.new - serializer.add_metadata @uri, metadata + serializer.add_metadata @uri, lazar.metadata serializer.to_rdfxml end end get '/:id/dependent.?:ext?' do - raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @json_file @accept = "application/x-yaml" if params[:ext] and params[:ext].match(/yaml/) - feature_uri = YAML.load_file(@yaml_file).metadata[OT.dependentVariables] + feature_uri = lazar.metadata[OT.dependentVariables] case @accept when /yaml/ - OpenTox::Feature.find(feature_uri).to_yaml + OpenTox::Feature.find(feature_uri, @subjectid).to_yaml when "text/uri-list" feature_uri when /rdf/ - OpenTox::Feature.find(feature_uri).to_rdfxml + OpenTox::Feature.find(feature_uri, @subjectid).to_rdfxml when /html/ - OpenTox.text_to_html OpenTox::Feature.find(feature_uri).to_yaml + OpenTox.text_to_html OpenTox::Feature.find(feature_uri, @subjectid).to_yaml else raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" end end get '/:id/predicted/:prop' do - raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @json_file if params[:prop] == "value" or params[:prop] == "confidence" - feature = eval "YAML.load_file(@yaml_file).prediction_#{params[:prop]}_feature" + #feature = eval "YAML.load_file(@json_file).prediction_#{params[:prop]}_feature" + feature = eval "lazar.prediction_#{params[:prop]}_feature" case @accept when /yaml/ content_type "application/x-yaml" @@ -77,9 +99,9 @@ get '/:id/predicted/:prop' do end get '/:id/predicted.?:ext?' do - raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @json_file @accept = "application/x-yaml" if params[:ext] and params[:ext].match(/yaml/) - features = YAML.load_file(@yaml_file).prediction_features + features = lazar.prediction_features case @accept when "text/uri-list" "#{features.collect{|f| f.uri}.join("\n")}\n" @@ -89,7 +111,6 @@ get '/:id/predicted.?:ext?' do 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 features.to_yaml else @@ -97,21 +118,21 @@ get '/:id/predicted.?:ext?' do end end -# Store a lazar model. This method should not be called directly, use OpenTox::Algorithm::Lazr to create a lazar model +# Store a lazar model. This method should not be called directly, use OpenTox::Algorithm::Lazar to create a lazar model # @param [Body] lazar Model representation in YAML format # @return [String] Model URI post '/?' do # create model - raise OpenTox::BadRequestError.new "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(/json/) @id = next_id @uri = uri @id - @yaml_file = "public/#{@id}.yaml" - lazar = YAML.load request.env["rack.input"].read - lazar.uri = @uri + @json_file = "#{@@datadir}/#{@id}.json" + hash = Yajl::Parser.parse request.env["rack.input"].read + hash["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 + hash["metadata"][OT.predictedVariables] = [value_feature_uri, confidence_feature_uri] + File.open(@json_file,"w+"){|f| f.puts Yajl::Encoder.encode(hash)} + OpenTox::Authorization.check_policy(@uri, @subjectid) if File.exists? @json_file response['Content-Type'] = 'text/uri-list' @uri end @@ -123,30 +144,30 @@ end # @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do - raise OpenTox::NotFoundError.new "Model #{params[:id]} does not exist." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} does not exist." unless File.exists? @json_file 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 + #@lazar = YAML.load_file @json_file response['Content-Type'] = 'text/uri-list' if compound_uri - cache = PredictionCache.find(:model_uri => @lazar.uri, :compound_uri => compound_uri).first + cache = PredictionCache.find(:model_uri => lazar.uri, :compound_uri => compound_uri).first 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 = 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." + 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 + task = OpenTox::Task.create("Predict dataset",url_for("/#{lazar.id}", :full)) do |task| + lazar.predict_dataset(dataset_uri, @subjectid, task).uri end raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled" halt 202,task.uri |