From 1e67b08822c79b95c428faa0539384fd499d70d9 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Thu, 4 Aug 2011 18:21:56 +0200 Subject: add missing subject ids --- lazar.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lazar.rb b/lazar.rb index fa54ed6..08efcf8 100644 --- a/lazar.rb +++ b/lazar.rb @@ -42,13 +42,13 @@ get '/:id/dependent.?:ext?' do feature_uri = YAML.load_file(@yaml_file).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 -- cgit v1.2.3 From a368d3ad8015a905c0aee873c5c10c2fad4afbdb Mon Sep 17 00:00:00 2001 From: mr Date: Wed, 17 Aug 2011 14:06:03 +0200 Subject: move data directory to folder data for A&A reasons --- application.rb | 10 ++++++---- data/.gitignore | 3 +++ lazar.rb | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 data/.gitignore diff --git a/application.rb b/application.rb index 7d3de38..96287c3 100644 --- a/application.rb +++ b/application.rb @@ -4,6 +4,8 @@ require 'opentox-ruby' set :lock, true +@@datadir = "data" + class PredictionCache < Ohm::Model attribute :compound_uri attribute :model_uri @@ -22,7 +24,7 @@ before do @id = @id.to_s.sub(/\//,'').to_i @uri = uri @id - @yaml_file = "public/#{@id}.yaml" + @yaml_file = "#{@@datadir}/#{@id}.yaml" raise OpenTox::NotFoundError.new "Model #{@id} not found." unless File.exists? @yaml_file end @@ -35,7 +37,7 @@ require 'lazar.rb' helpers do def next_id - id = Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last + id = Dir["./#{@@datadir}/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last id = 0 if id.nil? id + 1 end @@ -59,7 +61,7 @@ end get '/?' do # get index of models response['Content-Type'] = 'text/uri-list' - Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n" + Dir["./#{@@datadir}/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n" end delete '/:id/?' do @@ -84,7 +86,7 @@ end delete '/?' do # TODO delete datasets - FileUtils.rm Dir["public/*.yaml"] + FileUtils.rm Dir["#{@@datadir}/*.yaml"] PredictionCache.all.each {|cache| cache.delete } response['Content-Type'] = 'text/plain' "All models and cached predictions deleted." diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..59e3edd --- /dev/null +++ b/data/.gitignore @@ -0,0 +1,3 @@ +*.rdfxml +*.xls +*.yaml diff --git a/lazar.rb b/lazar.rb index 08efcf8..89f3423 100644 --- a/lazar.rb +++ b/lazar.rb @@ -104,7 +104,7 @@ post '/?' do # create model 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" + @yaml_file = "#{@@datadir}/#{@id}.yaml" lazar = YAML.load request.env["rack.input"].read lazar.uri = @uri value_feature_uri = File.join( @uri, "predicted", "value") -- cgit v1.2.3 From 63d391467814b60f165075f32a0adbe33cced67b Mon Sep 17 00:00:00 2001 From: mr Date: Tue, 23 Aug 2011 12:04:51 +0200 Subject: fix output for fileextensions on curl requests --- application.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/application.rb b/application.rb index 96287c3..0b6b851 100644 --- a/application.rb +++ b/application.rb @@ -26,6 +26,20 @@ before do @uri = uri @id @yaml_file = "#{@@datadir}/#{@id}.yaml" raise OpenTox::NotFoundError.new "Model #{@id} not found." unless File.exists? @yaml_file + + extension = File.extname(request.path_info) + unless extension.empty? + case extension + when ".html" + @accept = 'text/html' + when ".yaml" + @accept = 'application/x-yaml' + when ".rdfxml" + @accept = 'application/rdf+xml' + else + raise OpenTox::NotFoundError.new "File format #{extension} not supported." + end + end end # make sure subjectid is not included in params, subjectid is set as member variable -- cgit v1.2.3 From 2c8a4681e998fa8661dec657ac6bbe675afb9acb Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 25 Aug 2011 10:41:44 +0000 Subject: json store for models --- application.rb | 16 ++++++----- lazar.rb | 88 +++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 63 insertions(+), 41 deletions(-) diff --git a/application.rb b/application.rb index 0b6b851..1345c4c 100644 --- a/application.rb +++ b/application.rb @@ -24,8 +24,8 @@ before do @id = @id.to_s.sub(/\//,'').to_i @uri = uri @id - @yaml_file = "#{@@datadir}/#{@id}.yaml" - raise OpenTox::NotFoundError.new "Model #{@id} not found." unless File.exists? @yaml_file + @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? @@ -34,6 +34,8 @@ before do @accept = 'text/html' when ".yaml" @accept = 'application/x-yaml' + when ".json" + @accept = 'application/json' when ".rdfxml" @accept = 'application/rdf+xml' else @@ -51,7 +53,7 @@ require 'lazar.rb' helpers do def next_id - id = Dir["./#{@@datadir}/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last + id = Dir["./#{@@datadir}/*json"].collect{|f| File.basename(f.sub(/.json/,'')).to_i}.sort.last id = 0 if id.nil? id + 1 end @@ -75,14 +77,14 @@ end get '/?' do # get index of models response['Content-Type'] = 'text/uri-list' - Dir["./#{@@datadir}/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n" + Dir["./#{@@datadir}/*json"].collect{|f| File.basename(f.sub(/.json/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n" end delete '/:id/?' do LOGGER.debug "Deleting model with id "+@id.to_s begin - FileUtils.rm @yaml_file - if @subjectid and !File.exists? @yaml_file and @uri + 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}" @@ -100,7 +102,7 @@ end delete '/?' do # TODO delete datasets - FileUtils.rm Dir["#{@@datadir}/*.yaml"] + FileUtils.rm Dir["#{@@datadir}/*.json"] PredictionCache.all.each {|cache| cache.delete } response['Content-Type'] = 'text/plain' "All models and cached predictions deleted." diff --git a/lazar.rb b/lazar.rb index 89f3423..481a59d 100644 --- a/lazar.rb +++ b/lazar.rb @@ -1,45 +1,65 @@ 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])) 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, @subjectid).to_yaml @@ -55,9 +75,10 @@ get '/:id/dependent.?:ext?' do 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 +98,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 +110,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 +117,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 = "#{@@datadir}/#{@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 +143,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 -- cgit v1.2.3 From 1ccdf7e9f0541d4281d68ef90d6326da9c9592e4 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Thu, 25 Aug 2011 17:33:06 +0000 Subject: data directory excluded from git --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4638e4f..45c8bd1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ tmp/* log/* db/* public/* +data/* -- cgit v1.2.3 From 05d133d09aba7c1853ae597c7c95cf6dc8e9912f Mon Sep 17 00:00:00 2001 From: mr Date: Wed, 14 Sep 2011 16:05:01 +0200 Subject: unify GET '/' view for browser in HTML --- application.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/application.rb b/application.rb index 1345c4c..0507ad8 100644 --- a/application.rb +++ b/application.rb @@ -76,8 +76,15 @@ helpers do end get '/?' do # get index of models - response['Content-Type'] = 'text/uri-list' - Dir["./#{@@datadir}/*json"].collect{|f| File.basename(f.sub(/.json/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n" + 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 -- cgit v1.2.3 From a8b6b8debe373b3c936d6cbf863c38ff2862441b Mon Sep 17 00:00:00 2001 From: mr Date: Wed, 14 Sep 2011 16:10:13 +0200 Subject: have json representation for HTML output --- lazar.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lazar.rb b/lazar.rb index 481a59d..3af6c1c 100644 --- a/lazar.rb +++ b/lazar.rb @@ -37,7 +37,8 @@ get '/:id/?' do send_file @json_file, :type => 'application/json' when /html/ response['Content-Type'] = 'text/html' - OpenTox.text_to_html File.read(yaml_file(params[:id])) + #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 -- cgit v1.2.3