From 9a228a9fcfd85f69f81dd0d70f3ed5829cb7aead Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Tue, 3 Apr 2012 16:26:50 +0200 Subject: Added openbabel --- feature_selection.rb | 93 ---------------------------------------------------- fs.rb | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ pc.rb | 54 +++++++++++++++++++++++++++--- 3 files changed, 142 insertions(+), 98 deletions(-) delete mode 100644 feature_selection.rb create mode 100644 fs.rb diff --git a/feature_selection.rb b/feature_selection.rb deleted file mode 100644 index 469fa87..0000000 --- a/feature_selection.rb +++ /dev/null @@ -1,93 +0,0 @@ -# Get list of feature selection algorithms -# -# @return [text/uri-list] URIs of feature selection algorithms -get '/feature_selection/?' do - list = [ url_for('/feature_selection/rfe', :full) ].join("\n") + "\n" - case request.env['HTTP_ACCEPT'] - when /text\/html/ - content_type "text/html" - OpenTox.text_to_html list - else - content_type 'text/uri-list' - list - end -end - -# Get RDF/XML representation of feature_selection rfe algorithm -# @return [application/rdf+xml] OWL-DL representation of feature_selection rfe algorithm -get "/feature_selection/rfe/?" do - algorithm = OpenTox::Algorithm::Generic.new(url_for('/feature_selection/rfe',:full)) - algorithm.metadata = { - DC.title => 'recursive feature elimination', - DC.creator => "andreas@maunz.de, helma@in-silico.ch", - DC.contributor => "vorgrimmlerdavid@gmx.de", - BO.instanceOf => "http://opentox.org/ontology/ist-algorithms.owl#feature_selection_rfe", - RDF.type => [OT.Algorithm,OTA.PatternMiningSupervised], - OT.parameters => [ - { DC.description => "Dataset URI", OT.paramScope => "mandatory", DC.title => "dataset_uri" }, - { DC.description => "Prediction Feature URI", OT.paramScope => "mandatory", DC.title => "prediction_feature_uri" }, - { DC.description => "Feature Dataset URI", OT.paramScope => "mandatory", DC.title => "feature_dataset_uri" }, - { DC.description => "Delete Instances with missing values", OT.paramScope => "optional", DC.title => "del_missing" } - ] - } - case request.env['HTTP_ACCEPT'] - when /text\/html/ - content_type "text/html" - OpenTox.text_to_html algorithm.to_yaml - when /application\/x-yaml/ - content_type "application/x-yaml" - algorithm.to_yaml - else - response['Content-Type'] = 'application/rdf+xml' - algorithm.to_rdfxml - end -end - -# Run rfe algorithm on dataset -# -# @param [String] dataset_uri URI of the training dataset -# @param [String] feature_dataset_uri URI of the feature dataset -# @return [text/uri-list] Task URI -post '/feature_selection/rfe/?' do - - raise OpenTox::NotFoundError.new "Please submit a dataset_uri." unless params[:dataset_uri] - raise OpenTox::NotFoundError.new "Please submit a prediction_feature_uri." unless params[:prediction_feature_uri] - raise OpenTox::NotFoundError.new "Please submit a feature_dataset_uri." unless params[:feature_dataset_uri] - - ds_csv=OpenTox::RestClientWrapper.get( params[:dataset_uri], {:accept => "text/csv"} ) - tf_ds=Tempfile.open(['rfe_', '.csv']) - tf_ds.puts(ds_csv) - tf_ds.flush() - - prediction_feature = params[:prediction_feature_uri].split('/').last # get col name - - fds_csv=OpenTox::RestClientWrapper.get( params[:feature_dataset_uri], {:accept => "text/csv"}) - tf_fds=Tempfile.open(['rfe_', '.csv']) - tf_fds.puts(fds_csv) - tf_fds.flush() - - del_missing = params[:del_missing] == "true" ? true : false - - task = OpenTox::Task.create("Recursive Feature Elimination", url_for('/feature_selection',:full)) do |task| - r_result_file = OpenTox::Algorithm::FeatureSelection.rfe( { :ds_csv_file => tf_ds.path, :prediction_feature => prediction_feature, :fds_csv_file => tf_fds.path, :del_missing => del_missing } ) - - parser = OpenTox::Parser::Spreadsheets.new - ds = OpenTox::Dataset.new - ds.save - parser.dataset = ds - ds = parser.load_csv(File.open(r_result_file).read,false,true) - ds.save - r_result_uri = ds.uri - #r_result_uri = OpenTox::Dataset.create_from_csv_file(r_result_file).uri - begin - tf_ds.close!; tf_fds.close! - File.unlink(r_result_file) - rescue - end - r_result_uri - end - response['Content-Type'] = 'text/uri-list' - raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled" - halt 202,task.uri.to_s+"\n" -end - diff --git a/fs.rb b/fs.rb new file mode 100644 index 0000000..469fa87 --- /dev/null +++ b/fs.rb @@ -0,0 +1,93 @@ +# Get list of feature selection algorithms +# +# @return [text/uri-list] URIs of feature selection algorithms +get '/feature_selection/?' do + list = [ url_for('/feature_selection/rfe', :full) ].join("\n") + "\n" + case request.env['HTTP_ACCEPT'] + when /text\/html/ + content_type "text/html" + OpenTox.text_to_html list + else + content_type 'text/uri-list' + list + end +end + +# Get RDF/XML representation of feature_selection rfe algorithm +# @return [application/rdf+xml] OWL-DL representation of feature_selection rfe algorithm +get "/feature_selection/rfe/?" do + algorithm = OpenTox::Algorithm::Generic.new(url_for('/feature_selection/rfe',:full)) + algorithm.metadata = { + DC.title => 'recursive feature elimination', + DC.creator => "andreas@maunz.de, helma@in-silico.ch", + DC.contributor => "vorgrimmlerdavid@gmx.de", + BO.instanceOf => "http://opentox.org/ontology/ist-algorithms.owl#feature_selection_rfe", + RDF.type => [OT.Algorithm,OTA.PatternMiningSupervised], + OT.parameters => [ + { DC.description => "Dataset URI", OT.paramScope => "mandatory", DC.title => "dataset_uri" }, + { DC.description => "Prediction Feature URI", OT.paramScope => "mandatory", DC.title => "prediction_feature_uri" }, + { DC.description => "Feature Dataset URI", OT.paramScope => "mandatory", DC.title => "feature_dataset_uri" }, + { DC.description => "Delete Instances with missing values", OT.paramScope => "optional", DC.title => "del_missing" } + ] + } + case request.env['HTTP_ACCEPT'] + when /text\/html/ + content_type "text/html" + OpenTox.text_to_html algorithm.to_yaml + when /application\/x-yaml/ + content_type "application/x-yaml" + algorithm.to_yaml + else + response['Content-Type'] = 'application/rdf+xml' + algorithm.to_rdfxml + end +end + +# Run rfe algorithm on dataset +# +# @param [String] dataset_uri URI of the training dataset +# @param [String] feature_dataset_uri URI of the feature dataset +# @return [text/uri-list] Task URI +post '/feature_selection/rfe/?' do + + raise OpenTox::NotFoundError.new "Please submit a dataset_uri." unless params[:dataset_uri] + raise OpenTox::NotFoundError.new "Please submit a prediction_feature_uri." unless params[:prediction_feature_uri] + raise OpenTox::NotFoundError.new "Please submit a feature_dataset_uri." unless params[:feature_dataset_uri] + + ds_csv=OpenTox::RestClientWrapper.get( params[:dataset_uri], {:accept => "text/csv"} ) + tf_ds=Tempfile.open(['rfe_', '.csv']) + tf_ds.puts(ds_csv) + tf_ds.flush() + + prediction_feature = params[:prediction_feature_uri].split('/').last # get col name + + fds_csv=OpenTox::RestClientWrapper.get( params[:feature_dataset_uri], {:accept => "text/csv"}) + tf_fds=Tempfile.open(['rfe_', '.csv']) + tf_fds.puts(fds_csv) + tf_fds.flush() + + del_missing = params[:del_missing] == "true" ? true : false + + task = OpenTox::Task.create("Recursive Feature Elimination", url_for('/feature_selection',:full)) do |task| + r_result_file = OpenTox::Algorithm::FeatureSelection.rfe( { :ds_csv_file => tf_ds.path, :prediction_feature => prediction_feature, :fds_csv_file => tf_fds.path, :del_missing => del_missing } ) + + parser = OpenTox::Parser::Spreadsheets.new + ds = OpenTox::Dataset.new + ds.save + parser.dataset = ds + ds = parser.load_csv(File.open(r_result_file).read,false,true) + ds.save + r_result_uri = ds.uri + #r_result_uri = OpenTox::Dataset.create_from_csv_file(r_result_file).uri + begin + tf_ds.close!; tf_fds.close! + File.unlink(r_result_file) + rescue + end + r_result_uri + end + response['Content-Type'] = 'text/uri-list' + raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled" + halt 202,task.uri.to_s+"\n" +end + diff --git a/pc.rb b/pc.rb index f297014..5c38052 100644 --- a/pc.rb +++ b/pc.rb @@ -3,12 +3,12 @@ # Author: Andreas Maunz -# Get a list of OpenBabel algorithms -# @return [text/uri-list] URIs of OpenBabel algorithms +# Get a list of pc algorithms +# @return [text/uri-list] URIs of algorithms get '/pc' do algorithms = YAML::load_file File.join(ENV['HOME'], ".opentox", "config", "pc_descriptors.yaml") response['Content-Type'] = 'text/uri-list' - list = (algorithms.keys << "AllDescriptors").join("\n") + "\n" + list = (algorithms.keys.sort << "AllDescriptors").collect { |name| url_for("/pc/#{name}",:full) }.join("\n") + "\n" case request.env['HTTP_ACCEPT'] when /text\/html/ content_type "text/html" @@ -28,7 +28,7 @@ get '/pc/:descriptor' do if params[:descriptor] != "AllDescriptors" descriptors = descriptors[params[:descriptor]] else - alg_params << { DC.description => "Descriptor Category, one or more of '#{descriptors.collect { |id, info| info[:category] }.uniq.sort.join(",")}'", OT.paramScope => "optional", DC.title => "category" } + alg_params << { DC.description => "Physico-chemical type, one or more of '#{descriptors.collect { |id, info| info[:pc_type] }.uniq.sort.join(",")}'", OT.paramScope => "optional", DC.title => "pc_type" } alg_params << { DC.description => "Software Library, one or more of '#{descriptors.collect { |id, info| info[:lib] }.uniq.sort.join(",")}'", OT.paramScope => "optional", DC.title => "lib" } descriptors = {:id => "AllDescriptors", :name => "All PC descriptors" } end @@ -44,7 +44,7 @@ get '/pc/:descriptor' do RDF.type => [OTA.DescriptorCalculation], } algorithm.metadata[OT.parameters] = alg_params - algorithm.metadata[DC.description] << (", category: " + descriptors[:category]) unless descriptors[:id] == "AllDescriptors" + algorithm.metadata[DC.description] << (", pc_type: " + descriptors[:pc_type]) unless descriptors[:id] == "AllDescriptors" algorithm.metadata[DC.description] << (", lib: " + descriptors[:lib]) unless descriptors[:id] == "AllDescriptors" # Deliver @@ -65,3 +65,47 @@ get '/pc/:descriptor' do end end +# Run pc descriptor calculation algorithm on dataset +# +# @param [String] dataset_uri URI of the training dataset +# @return [text/uri-list] Task URI +post '/pc' do + response['Content-Type'] = 'text/uri-list' + raise OpenTox::NotFoundError.new "Parameter 'dataset_uri' missing." unless params[:dataset_uri] + + descriptors = YAML::load_file File.join(ENV['HOME'], ".opentox", "config", "pc_descriptors.yaml") + params[:pc_type] = descriptors.collect { |id,info| info[:pc_type]}.uniq.sort.join(',') unless params[:pc_type] + + task = OpenTox::Task.create("PC descriptor calculation for dataset ", @uri) do |task| + Rjb.load(nil,["-Xmx64m"]) # start vm + byteArray = Rjb::import('java.io.ByteArrayOutputStream'); printStream = Rjb::import('java.io.PrintStream'); + out = byteArray.new() ; Rjb::import('java.lang.System').out = printStream.new(out) # joelib is too verbose + s = Rjb::import('JoelibFc') # import main class + OpenTox::Algorithm.pc_descriptors( { :dataset_uri => params[:dataset_uri], :pc_type => params[:pc_type], :rjb => s, :task => task, :lib => params[:lib] } ) + end + raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled" + halt 202,task.uri.to_s+"\n" +end + +# Run pc descriptor calculation algorithm on dataset +# +# @param [String] dataset_uri URI of the training dataset +# @return [text/uri-list] Task URI +post '/pc/:descriptor' do + response['Content-Type'] = 'text/uri-list' + raise OpenTox::NotFoundError.new "Parameter 'dataset_uri' missing." unless params[:dataset_uri] + + descriptors = YAML::load_file File.join(ENV['HOME'], ".opentox", "config", "pc_descriptors.yaml") + params[:pc_type] = descriptors.collect { |id,info| info[:pc_type]}.uniq.sort.join(',') + + task = OpenTox::Task.create("PC descriptor calculation for dataset ", @uri) do |task| + Rjb.load(nil,["-Xmx64m"]) # start vm + byteArray = Rjb::import('java.io.ByteArrayOutputStream'); printStream = Rjb::import('java.io.PrintStream'); + out = byteArray.new() ; Rjb::import('java.lang.System').out = printStream.new(out) # joelib is too verbose + s = Rjb::import('JoelibFc') # import main class + OpenTox::Algorithm.pc_descriptors( { :dataset_uri => params[:dataset_uri], :pc_type => params[:pc_type], :descriptor => params[:descriptor], :rjb => s, :task => task } ) + end + raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled" + halt 202,task.uri.to_s+"\n" +end + -- cgit v1.2.3