diff options
author | Christoph Helma <helma@in-silico.ch> | 2013-05-03 10:00:10 +0200 |
---|---|---|
committer | Christoph Helma <helma@in-silico.ch> | 2013-05-03 10:00:10 +0200 |
commit | f901ca7ca635e522f124e97d274bee8ddc7698b8 (patch) | |
tree | 1aaca19d3b15b5521f02241a66a3ec389c4d8d08 | |
parent | dfb23736c7fce77e8dddcd7ae7d7133b0c4bdf99 (diff) |
algorithm service restructured
-rw-r--r-- | application.rb | 15 | ||||
-rw-r--r-- | descriptor.rb | 5 | ||||
-rw-r--r-- | feature-selection.rb (renamed from webapp/fs.rb) | 12 | ||||
-rw-r--r-- | fminer.rb (renamed from webapp/fminer.rb) | 24 | ||||
-rw-r--r-- | lib/algorithm.rb | 30 | ||||
-rw-r--r-- | lib/dataset.rb | 32 | ||||
-rw-r--r-- | lib/feature_values.rb (renamed from lib/algorithm/feature_values.rb) | 8 | ||||
-rw-r--r-- | lib/fminer.rb (renamed from lib/algorithm/fminer.rb) | 21 | ||||
-rw-r--r-- | lib/lazar.rb | 180 | ||||
-rw-r--r-- | lib/neighbors.rb (renamed from lib/algorithm/neighbors.rb) | 1 | ||||
-rw-r--r-- | lib/similarity.rb (renamed from lib/algorithm/similarity.rb) | 0 | ||||
-rw-r--r-- | lib/transform.rb (renamed from lib/algorithm/transform.rb) | 0 | ||||
-rw-r--r-- | test.rb (renamed from webapp/test.rb) | 0 | ||||
-rw-r--r-- | webapp/lazar.rb | 73 | ||||
-rw-r--r-- | webapp/sinatra.rb | 47 |
15 files changed, 154 insertions, 294 deletions
diff --git a/application.rb b/application.rb index f15aa48..d40e70d 100644 --- a/application.rb +++ b/application.rb @@ -12,10 +12,7 @@ require_relative 'last-utils/lu.rb' $logger.debug "Algorithm booting: #{$algorithm.collect{ |k,v| "#{k}: '#{v}'"} }" Dir['./lib/algorithm/*.rb'].each { |f| require f; also_reload f } # Libs Dir['./lib/*.rb'].each { |f| require f; also_reload f } # Libs -Dir['./webapp/*.rb'].each { |f| require f; also_reload f } # Webapps -require_relative "descriptor.rb" -also_reload "descriptor.rb" -#Dir['./webapp/pc-descriptors.rb'].each { |f| require f; also_reload f } # Webapps +Dir['./*.rb'].each { |f| require_relative f; also_reload f } # Webapps # Entry point module OpenTox @@ -23,13 +20,13 @@ module OpenTox get '/?' do list = [ to('/lazar', :full), to('/fminer/bbrc', :full), - to('/fminer/bbrc/sample', :full), + #to('/fminer/bbrc/sample', :full), to('/fminer/last', :full), - to('/fminer/bbrc/match', :full), - to('/fminer/last/match', :full), - to('/fs/rfe', :full), + #to('/fminer/bbrc/match', :full), + #to('/fminer/last/match', :full), + to('/feature-selection/recursive-feature-elimination', :full), to('/descriptor') ].join("\n") + "\n" - format_output (list) + render list end end end diff --git a/descriptor.rb b/descriptor.rb index 96599d6..03e32cf 100644 --- a/descriptor.rb +++ b/descriptor.rb @@ -214,7 +214,7 @@ module OpenTox get '/descriptor/:lib/:descriptor/?' do @algorithm[RDF::DC.title] = @descriptor[:title] @algorithm[RDF::DC.description] = @descriptor[:description] if @descriptor[:description] - format_output(@algorithm) + render(@algorithm) end post '/descriptor/?' do @@ -222,7 +222,6 @@ module OpenTox if params[:descriptor_uris] descriptors = {} params[:descriptor_uris].each do |descriptor_uri| - #lib, title = descriptor.split('/') lib = descriptor_uri.split('/')[-2] descriptors[lib.to_sym] ||= [] descriptors[lib.to_sym] += DESCRIPTORS[lib.to_sym].select{|d| d[:uri] == descriptor_uri} @@ -235,7 +234,7 @@ module OpenTox elsif params[:dataset_uri] compounds = Dataset.new(params[:dataset_uri]).compounds end - [:openbabel, :cdk, :joelib].each{ |lib| send lib, compounds, descriptors[lib] } + [:openbabel, :cdk, :joelib].each{ |lib| send lib, compounds, descriptors[lib] if descriptors[lib] } @feature_dataset.put @feature_dataset.uri end diff --git a/webapp/fs.rb b/feature-selection.rb index e570860..dd2f683 100644 --- a/webapp/fs.rb +++ b/feature-selection.rb @@ -10,15 +10,15 @@ module OpenTox # Get list of feature selection algorithms # @return [text/uri-list] URIs - get '/fs/?' do - list = [ to('/fs/rfe', :full) ].join("\n") + "\n" - format_output(list) + get '/feature-selection/?' do + list = [ to('/feature-selection/recursive-feature-elimination', :full) ].join("\n") + "\n" + render(list) end # Get representation of Recursive Feature Elimination algorithm # @return [String] Representation - get "/fs/rfe/?" do - algorithm = OpenTox::Algorithm.new(to('/fs/rfe',:full)) + get "/feature-selection/recursive-feature-elimination/?" do + algorithm = OpenTox::Algorithm.new(to('/feature-selection/recursive-feature-elimination',:full)) algorithm.metadata = { RDF::DC.title => 'Recursive Feature Elimination', RDF::DC.creator => "andreas@maunz.de", @@ -30,7 +30,7 @@ module OpenTox { RDF::DC.description => "Feature Dataset URI", RDF::OT.paramScope => "mandatory", RDF::DC.title => "feature_dataset_uri" }, { RDF::DC.description => "Delete Instances with missing values", RDF::OT.paramScope => "optional", RDF::DC.title => "del_missing" } ] - format_output(algorithm) + render(algorithm) end end diff --git a/webapp/fminer.rb b/fminer.rb index 4f9abfe..30426da 100644 --- a/webapp/fminer.rb +++ b/fminer.rb @@ -21,13 +21,21 @@ module OpenTox # Get list of fminer algorithms # @return [text/uri-list] URIs get '/fminer/?' do +=begin list = [ uri('/fminer/bbrc'), - uri('/fminer/bbrc/sample'), + #uri('/fminer/bbrc/sample'), uri('/fminer/last'), - uri('/fminer/bbrc/match'), - uri('/fminer/last/match') + #uri('/fminer/bbrc/match'), + #uri('/fminer/last/match') ].join("\n") + "\n" - format_output(list) + render(list) +=end + render [ uri('/fminer/bbrc'), + #uri('/fminer/bbrc/sample'), + uri('/fminer/last'), + #uri('/fminer/bbrc/match'), + #uri('/fminer/last/match') + ] end # Get representation of BBRC algorithm @@ -49,7 +57,7 @@ module OpenTox { RDF::DC.description => "Whether subgraphs should be weighted with their occurrence counts in the instances (frequency)", RDF::OT.paramScope => "optional", RDF::DC.title => "nr_hits" }, { RDF::DC.description => "Set to 'true' to obtain target variables as a feature", RDF::OT.paramScope => "optional", RDF::DC.title => "get_target" } ] - format_output(algorithm) + render(algorithm) end # Get representation of BBRC-sample algorithm @@ -71,7 +79,7 @@ module OpenTox { RDF::DC.description => "BBRC classes, pass 'false' to switch off mining for BBRC representatives.", RDF::OT.paramScope => "optional", RDF::DC.title => "backbone" }, { RDF::DC.description => "Chisq estimation method, pass 'mean' to use simple mean estimate for chisq test.", RDF::OT.paramScope => "optional", RDF::DC.title => "method" } ] - format_output(algorithm) + render(algorithm) end # Get representation of fminer LAST-PM algorithm @@ -91,7 +99,7 @@ module OpenTox { RDF::DC.description => "Whether subgraphs should be weighted with their occurrence counts in the instances (frequency)", RDF::OT.paramScope => "optional", RDF::DC.title => "nr_hits" }, { RDF::DC.description => "Set to 'true' to obtain target variables as a feature", RDF::OT.paramScope => "optional", RDF::DC.title => "get_target" } ] - format_output(algorithm) + render(algorithm) end @@ -109,7 +117,7 @@ module OpenTox { RDF::DC.description => "Feature Dataset URI", RDF::OT.paramScope => "mandatory", RDF::DC.title => "feature_dataset_uri" }, { RDF::DC.description => "Feature URI for dependent variable", RDF::OT.paramScope => "optional", RDF::DC.title => "prediction_feature" } ] - format_output(algorithm) + render(algorithm) end diff --git a/lib/algorithm.rb b/lib/algorithm.rb deleted file mode 100644 index fb47385..0000000 --- a/lib/algorithm.rb +++ /dev/null @@ -1,30 +0,0 @@ -=begin -* Name: algorithm.rb -* Description: General algorithms -* Author: Andreas Maunz <andreas@maunz.de> -* Date: 10/2012 -=end - -module OpenTox - class Algorithm - - # Minimum Frequency - # @param [Integer] per-mil value - # return [Integer] min-frequency - def self.min_frequency(training_dataset,prediction_feature,per_mil) - nr_labeled_cmpds=0 - f_idx=training_dataset.features.collect{|f| f.uri}.index prediction_feature.uri - training_dataset.compounds.each_with_index { |cmpd, c_idx| - if ( training_dataset.data_entries[c_idx] ) - unless training_dataset.data_entries[c_idx][f_idx].nil? - nr_labeled_cmpds += 1 - end - end - } - minfreq = per_mil * nr_labeled_cmpds.to_f / 1000.0 # AM sugg. 8-10 per mil for BBRC, 50 per mil for LAST - minfreq = 2 unless minfreq > 2 - Integer (minfreq) - end - - end -end diff --git a/lib/dataset.rb b/lib/dataset.rb deleted file mode 100644 index a42356b..0000000 --- a/lib/dataset.rb +++ /dev/null @@ -1,32 +0,0 @@ -=begin -* Name: dataset.rb -* Description: Dataset algorithms -* Author: Andreas Maunz <andreas@maunz.de> -* Date: 10/2012 -=end - -module OpenTox - class Dataset - - # Find database activities and calculate a consens - # @param [Hash] uri Compound URI, Feature URI - # @return [Object] activity Database activity, or nil - def database_activity(params) - f=Feature.new params[:prediction_feature_uri], @subjectid - db_act = values(Compound.new(params[:compound_uri]), f) - if !db_act.empty? - if f.feature_type == "classification" - db_act = db_act.to_scale.mode.dup - else - db_act = db_act.to_scale.median - end - $logger.debug "Database activity for '#{params[:compound_uri]}': '#{db_act}'" - db_act - else - nil - end - end - - end -end - diff --git a/lib/algorithm/feature_values.rb b/lib/feature_values.rb index 9a575a3..b441c23 100644 --- a/lib/algorithm/feature_values.rb +++ b/lib/feature_values.rb @@ -29,13 +29,15 @@ module OpenTox # @param [Hash] keys: compound, feature_dataset, pc_type, lib, values: OpenTox::Compound, String, String # @return [Hash] Hash with feature name as key and value as value def self.lookup(params, subjectid) + puts "lookup started" ds = params[:feature_dataset] - ds.build_feature_positions + #ds.build_feature_positions cmpd_inchi = params[:compound].inchi cmpd_idxs = ds.compounds.each_with_index.collect{ |cmpd,idx| idx if cmpd.inchi == cmpd_inchi }.compact if cmpd_idxs.size > 0 # We have entries + puts "entries" cmpd_numeric_f = ds.features.collect { |f| f if f[RDF.type].include? RDF::OT.NumericFeature }.compact @@ -58,12 +60,14 @@ module OpenTox cmpd_fingerprints[f.title] = values.to_scale.mode # AM: mode for the others } else # We need lookup + puts "no entries" params[:subjectid] = subjectid [:compound, :feature_dataset].each { |p| params.delete(p) }; [:pc_type, :lib].each { |p| params.delete(p) if params[p] == "" } single_cmpd_ds = OpenTox::Dataset.new(nil,subjectid) + # TODO: ntriples !!! single_cmpd_ds.parse_rdfxml(RestClientWrapper.post(File.join($compound[:uri],cmpd_inchi,"pc"), params, {:accept => "application/rdf+xml"})) single_cmpd_ds.get(true) - single_cmpd_ds.build_feature_positions + #single_cmpd_ds.build_feature_positions cmpd_fingerprints = single_cmpd_ds.features.inject({}) { |h,f| h[f.title] = single_cmpd_ds.data_entries[0][single_cmpd_ds.feature_positions[f.uri]] h diff --git a/lib/algorithm/fminer.rb b/lib/fminer.rb index 5b0527a..7f88c8b 100644 --- a/lib/algorithm/fminer.rb +++ b/lib/fminer.rb @@ -61,7 +61,7 @@ module OpenTox bad_request_error "Minimum frequency must be integer [n], or a percentage [n]pc, or a per-mil [n]pm , with n greater 0" if bad_request end if @minfreq.nil? - @minfreq=OpenTox::Algorithm.min_frequency(@training_dataset,@prediction_feature,per_mil) + @minfreq=min_frequency(@training_dataset,@prediction_feature,per_mil) $logger.debug "min_frequency #{@minfreq} (input was #{per_mil} per-mil)" end end @@ -176,6 +176,25 @@ module OpenTox metadata[RDF::OT.hasSource]=feature_dataset_uri if feature_dataset_uri [ metadata, parameters ] end + + # Minimum Frequency + # @param [Integer] per-mil value + # return [Integer] min-frequency + def min_frequency(training_dataset,prediction_feature,per_mil) + nr_labeled_cmpds=0 + f_idx=training_dataset.features.collect{|f| f.uri}.index prediction_feature.uri + training_dataset.compounds.each_with_index { |cmpd, c_idx| + if ( training_dataset.data_entries[c_idx] ) + unless training_dataset.data_entries[c_idx][f_idx].nil? + nr_labeled_cmpds += 1 + end + end + } + minfreq = per_mil * nr_labeled_cmpds.to_f / 1000.0 # AM sugg. 8-10 per mil for BBRC, 50 per mil for LAST + minfreq = 2 unless minfreq > 2 + Integer (minfreq) + end + end end diff --git a/lib/lazar.rb b/lib/lazar.rb index 3b8621c..6c8145d 100644 --- a/lib/lazar.rb +++ b/lib/lazar.rb @@ -12,6 +12,7 @@ module OpenTox attr_accessor :prediction_dataset def initialize(params) + puts "prediction started" @prediction_dataset = OpenTox::Dataset.new(nil, @subjectid) # set instance variables and prediction dataset parameters from parameters params.each {|k,v| @@ -27,7 +28,6 @@ module OpenTox @prediction_feature = OpenTox::Feature.new(@prediction_feature_uri,@subjectid) @predicted_variable = OpenTox::Feature.new @predicted_variable_uri, @subjectid @predicted_confidence = OpenTox::Feature.new @predicted_confidence_uri, @subjectid - #@prediction_dataset.metadata = { @prediction_dataset.metadata = { RDF::DC.title => "Lazar prediction for #{@prediction_feature.title}", RDF::DC.creator => @model_uri, @@ -36,6 +36,8 @@ module OpenTox RDF::OT.predictedVariables => [@predicted_variable_uri,@predicted_confidence_uri] } + puts "metadata set" + @training_dataset = OpenTox::Dataset.new(@training_dataset_uri,@subjectid) @feature_dataset = OpenTox::Dataset.new(@feature_dataset_uri, @subjectid) @@ -52,29 +54,35 @@ module OpenTox else compounds = [ OpenTox::Compound.new(@compound_uri,@subjectid) ] end + + puts "compounds started" compounds.each do |compound| - database_activity = @training_dataset.database_activity(params) - if database_activity - @prediction_dataset.add_data_entry compound, @prediction_feature, database_activity + + #database_activity = @training_dataset.database_activity(params) + database_activities = @training_dataset.values(compound,@prediction_feature) + puts "checking dataset activity" + if database_activities and !database_activities.empty? + database_activities.each do |database_activity| + @prediction_dataset.add_data_entry compound, @prediction_feature, database_activity + end next else - #pc_type = @feature_dataset.parameters["pc_type"] - #@model.pc_type = pc_type unless pc_type.nil? - #lib = @feature_dataset.parameters["lib"] - #@model.lib = lib unless lib.nil? - # AM: transform to cosine space @min_sim = (@min_sim.to_f*2.0-1.0).to_s if @similarity_algorithm =~ /cosine/ compound_params = { :compound => compound, :feature_dataset => @feature_dataset, - # TODO: fix in algorithm/lib/algorithm/feature_values.rb - #:pc_type => @model.pc_type, - #:lib => @model.lib } - compound_fingerprints = OpenTox::Algorithm::FeatureValues.send( @feature_calculation_algorithm, compound_params, @subjectid ) + puts "fingerprints" + puts @feature_calculation_algorithm + puts compound_params + #compound_fingerprints = OpenTox::Algorithm::FeatureValues.send( @feature_calculation_algorithm, compound_params, @subjectid ) + # TODO: fix for pc descriptors + compound_fingerprints = OpenTox::Algorithm::Descriptor.send( @feature_calculation_algorithm, compound, @feature_dataset.features.collect{ |f| f[RDF::DC.title] } ) + puts "Fingerprints" + puts compound_fingerprints.inspect @training_dataset.compounds.each_with_index { |cmpd, idx| act = @training_dataset.data_entries[idx][prediction_feature_pos] @acts << (@prediction_feature.feature_type=="classification" ? @prediction_feature.value_map.invert[act] : nil) @@ -82,15 +90,20 @@ module OpenTox @cmpds << cmpd.uri } +=begin @q_prop = @feature_dataset.features.collect { |f| val = compound_fingerprints[f.title] bad_request_error "Can not parse value '#{val}' to numeric" if val and !val.numeric? val ? val.to_f : 0.0 } # query structure +=end + @q_prop = compound_fingerprints.first.collect{|v| v.to_f} + puts @q_prop.inspect mtf = OpenTox::Algorithm::Transform::ModelTransformer.new(self) mtf.transform + puts "Prediction" prediction = OpenTox::Algorithm::Neighbors.send(@prediction_algorithm, { :props => mtf.props, :acts => mtf.acts, @@ -129,81 +142,82 @@ module OpenTox class Model - # Check parameters for plausibility - # Prepare lazar object (includes graph mining) - # @param[Array] lazar parameters as strings - # @param[Hash] REST parameters, as input by user - def create_model(params) - - training_dataset = OpenTox::Dataset.new(params[:dataset_uri], @subjectid) - @parameters << {RDF::DC.title => "training_dataset_uri", RDF::OT.paramValue => training_dataset.uri} - - # TODO: This is inconsistent, it would be better to have prediction_feature_uri in the API - if params[:prediction_feature] - resource_not_found_error "No feature '#{params[:prediction_feature]}' in dataset '#{params[:dataset_uri]}'" unless training_dataset.find_feature_uri( params[:prediction_feature] ) - else # try to read prediction_feature from dataset - resource_not_found_error "Please provide a prediction_feature parameter" unless training_dataset.features.size == 1 - params[:prediction_feature] = training_dataset.features.first.uri - end - self[RDF::OT.trainingDataset] = training_dataset.uri - prediction_feature = OpenTox::Feature.new(params[:prediction_feature], @subjectid) - predicted_variable = OpenTox::Feature.find_or_create({RDF::DC.title => "#{prediction_feature.title} prediction", RDF.type => [RDF::OT.Feature, prediction_feature[RDF.type]]}, @subjectid) - self[RDF::DC.title] = prediction_feature.title - @parameters << {RDF::DC.title => "prediction_feature_uri", RDF::OT.paramValue => prediction_feature.uri} - self[RDF::OT.dependentVariables] = prediction_feature.uri - - bad_request_error "Unknown prediction_algorithm #{params[:prediction_algorithm]}" if params[:prediction_algorithm] and !OpenTox::Algorithm::Neighbors.respond_to?(params[:prediction_algorithm]) - @parameters << {RDF::DC.title => "prediction_algorithm", RDF::OT.paramValue => params[:prediction_algorithm]} if params[:prediction_algorithm] - - confidence_feature = OpenTox::Feature.find_or_create({RDF::DC.title => "predicted_confidence", RDF.type => [RDF::OT.Feature, RDF::OT.NumericFeature]}, @subjectid) - self[RDF::OT.predictedVariables] = [ predicted_variable.uri, confidence_feature.uri ] - case prediction_feature.feature_type - when "classification" - @parameters << {RDF::DC.title => "prediction_algorithm", RDF::OT.paramValue => "weighted_majority_vote"} unless parameter_value "prediction_algorithm" - self[RDF.type] = [RDF::OT.Model, RDF::OTA.ClassificationLazySingleTarget] - when "regression" - @parameters << {RDF::DC.title => "prediction_algorithm", RDF::OT.paramValue => "local_svm_regression"} unless parameter_value "prediction_algorithm" - self[RDF.type] = [RDF::OT.Model, RDF::OTA.RegressionLazySingleTarget] - end - parameter_value("prediction_algorithm") =~ /majority_vote/ ? @parameters << {RDF::DC.title => "propositionalized", RDF::OT.paramValue => false} : @parameters << {RDF::DC.title => "propositionalized", RDF::OT.paramValue => true} - - @parameters << {RDF::DC.title => "min_sim", RDF::OT.paramValue => params[:min_sim].to_f} if params[:min_sim] and params[:min_sim].numeric? - @parameters << {RDF::DC.title => "feature_generation_uri", RDF::OT.paramValue => params[:feature_generation_uri]} - #@parameters["nr_hits"] = params[:nr_hits] - case params["feature_generation_uri"] - when /fminer/ - if (params[:nr_hits] == "true") - @parameters << {RDF::DC.title => "feature_calculation_algorithm", RDF::OT.paramValue => "match_hits"} - else - @parameters << {RDF::DC.title => "feature_calculation_algorithm", RDF::OT.paramValue => "match"} + class Lazar < Model + + # Check parameters for plausibility + # Prepare lazar object (includes graph mining) + # @param[Array] lazar parameters as strings + # @param[Hash] REST parameters, as input by user + def create(params) + + training_dataset = OpenTox::Dataset.new(params[:dataset_uri], @subjectid) + @parameters << {RDF::DC.title => "training_dataset_uri", RDF::OT.paramValue => training_dataset.uri} + + # TODO: This is inconsistent, it would be better to have prediction_feature_uri in the API + if params[:prediction_feature] + resource_not_found_error "No feature '#{params[:prediction_feature]}' in dataset '#{params[:dataset_uri]}'" unless training_dataset.find_feature_uri( params[:prediction_feature] ) + else # try to read prediction_feature from dataset + resource_not_found_error "Please provide a prediction_feature parameter" unless training_dataset.features.size == 1 + params[:prediction_feature] = training_dataset.features.first.uri + end + self[RDF::OT.trainingDataset] = training_dataset.uri + prediction_feature = OpenTox::Feature.new(params[:prediction_feature], @subjectid) + predicted_variable = OpenTox::Feature.find_or_create({RDF::DC.title => "#{prediction_feature.title} prediction", RDF.type => [RDF::OT.Feature, prediction_feature[RDF.type]]}, @subjectid) + self[RDF::DC.title] = prediction_feature.title + @parameters << {RDF::DC.title => "prediction_feature_uri", RDF::OT.paramValue => prediction_feature.uri} + self[RDF::OT.dependentVariables] = prediction_feature.uri + + bad_request_error "Unknown prediction_algorithm #{params[:prediction_algorithm]}" if params[:prediction_algorithm] and !OpenTox::Algorithm::Neighbors.respond_to?(params[:prediction_algorithm]) + @parameters << {RDF::DC.title => "prediction_algorithm", RDF::OT.paramValue => params[:prediction_algorithm]} if params[:prediction_algorithm] + + confidence_feature = OpenTox::Feature.find_or_create({RDF::DC.title => "predicted_confidence", RDF.type => [RDF::OT.Feature, RDF::OT.NumericFeature]}, @subjectid) + self[RDF::OT.predictedVariables] = [ predicted_variable.uri, confidence_feature.uri ] + case prediction_feature.feature_type + when "classification" + @parameters << {RDF::DC.title => "prediction_algorithm", RDF::OT.paramValue => "weighted_majority_vote"} unless parameter_value "prediction_algorithm" + self[RDF.type] = [RDF::OT.Model, RDF::OTA.ClassificationLazySingleTarget] + when "regression" + @parameters << {RDF::DC.title => "prediction_algorithm", RDF::OT.paramValue => "local_svm_regression"} unless parameter_value "prediction_algorithm" + self[RDF.type] = [RDF::OT.Model, RDF::OTA.RegressionLazySingleTarget] + end + parameter_value("prediction_algorithm") =~ /majority_vote/ ? @parameters << {RDF::DC.title => "propositionalized", RDF::OT.paramValue => false} : @parameters << {RDF::DC.title => "propositionalized", RDF::OT.paramValue => true} + + @parameters << {RDF::DC.title => "min_sim", RDF::OT.paramValue => params[:min_sim].to_f} if params[:min_sim] and params[:min_sim].numeric? + @parameters << {RDF::DC.title => "feature_generation_uri", RDF::OT.paramValue => params[:feature_generation_uri]} + #@parameters["nr_hits"] = params[:nr_hits] + case params["feature_generation_uri"] + when /fminer/ + if (params[:nr_hits] == "true") + @parameters << {RDF::DC.title => "feature_calculation_algorithm", RDF::OT.paramValue => "match_hits"} + else + @parameters << {RDF::DC.title => "feature_calculation_algorithm", RDF::OT.paramValue => "match"} + end + @parameters << {RDF::DC.title => "similarity_algorithm", RDF::OT.paramValue => "tanimoto"} + @parameters << {RDF::DC.title => "min_sim", RDF::OT.paramValue => 0.3} unless parameter_value("min_sim") + when /descriptor/ + @parameters << {RDF::DC.title => "feature_calculation_algorithm", RDF::OT.paramValue => "lookup"} + @parameters << {RDF::DC.title => "similarity_algorithm", RDF::OT.paramValue => "cosine"} + @parameters << {RDF::DC.title => "min_sim", RDF::OT.paramValue => 0.7} unless parameter_value("min_sim") end - @parameters << {RDF::DC.title => "similarity_algorithm", RDF::OT.paramValue => "tanimoto"} - @parameters << {RDF::DC.title => "min_sim", RDF::OT.paramValue => 0.3} unless parameter_value("min_sim") - when /descriptors/ - @parameters << {RDF::DC.title => "feature_calculation_algorithm", RDF::OT.paramValue => "lookup"} - @parameters << {RDF::DC.title => "similarity_algorithm", RDF::OT.paramValue => "cosine"} - @parameters << {RDF::DC.title => "min_sim", RDF::OT.paramValue => 0.7} unless parameter_value("min_sim") - end - #TODO: check if these parameters are necessary with new version - #set_parameter("pc_type", params[:pc_type] if params[:pc_type] - #set_parameter("lib", params[:lib] if params[:lib] + bad_request_error "Parameter min_train_performance is not numeric." if params[:min_train_performance] and !params[:min_train_performance].numeric? + @parameters << {RDF::DC.title => "min_train_performance", RDF::OT.paramValue => params[:min_train_performance].to_f} if params[:min_train_performance] and params[:min_train_performance].numeric? + @parameters << {RDF::DC.title => "min_train_performance", RDF::OT.paramValue => 0.1} unless parameter_value("min_train_performance") - bad_request_error "Parameter min_train_performance is not numeric." if params[:min_train_performance] and !params[:min_train_performance].numeric? - @parameters << {RDF::DC.title => "min_train_performance", RDF::OT.paramValue => params[:min_train_performance].to_f} if params[:min_train_performance] and params[:min_train_performance].numeric? - @parameters << {RDF::DC.title => "min_train_performance", RDF::OT.paramValue => 0.1} unless parameter_value("min_train_performance") + if params[:feature_dataset_uri] + bad_request_error "Feature dataset #{params[:feature_dataset_uri]} does not exist." unless URI.accessible? params[:feature_dataset_uri] + @parameters << {RDF::DC.title => "feature_dataset_uri", RDF::OT.paramValue => params[:feature_dataset_uri]} + self[RDF::OT.featureDataset] = params["feature_dataset_uri"] + else + # run feature generation algorithm + feature_dataset_uri = OpenTox::Algorithm.new(params[:feature_generation_uri]).run(params) + @parameters << {RDF::DC.title => "feature_dataset_uri", RDF::OT.paramValue => feature_dataset_uri} + self[RDF::OT.featureDataset] = feature_dataset_uri + end + put + @uri - if params[:feature_dataset_uri] - bad_request_error "Feature dataset #{params[:feature_dataset_uri]} does not exist." unless URI.accessible? params[:feature_dataset_uri] - @parameters << {RDF::DC.title => "feature_dataset_uri", RDF::OT.paramValue => params[:feature_dataset_uri]} - self[RDF::OT.featureDataset] = params["feature_dataset_uri"] - else - # run feature generation algorithm - feature_dataset_uri = OpenTox::Algorithm.new(params[:feature_generation_uri]).run(params) - @parameters << {RDF::DC.title => "feature_dataset_uri", RDF::OT.paramValue => feature_dataset_uri} - self[RDF::OT.featureDataset] = feature_dataset_uri end - end end diff --git a/lib/algorithm/neighbors.rb b/lib/neighbors.rb index 1a82dc8..b255f18 100644 --- a/lib/algorithm/neighbors.rb +++ b/lib/neighbors.rb @@ -68,6 +68,7 @@ module OpenTox # @param [Hash] params Keys `:props, :acts, :sims, :min_train_performance` are required # @return [Numeric] A prediction value. def self.local_svm_regression(params) + puts "SVM" confidence = 0.0 prediction = nil diff --git a/lib/algorithm/similarity.rb b/lib/similarity.rb index 22b4c28..22b4c28 100644 --- a/lib/algorithm/similarity.rb +++ b/lib/similarity.rb diff --git a/lib/algorithm/transform.rb b/lib/transform.rb index b2f7e7e..b2f7e7e 100644 --- a/lib/algorithm/transform.rb +++ b/lib/transform.rb diff --git a/webapp/lazar.rb b/webapp/lazar.rb deleted file mode 100644 index 921b08e..0000000 --- a/webapp/lazar.rb +++ /dev/null @@ -1,73 +0,0 @@ -module OpenTox - class Application < Service - - # Get representation of lazar algorithm - # @return [String] Representation - get '/lazar/?' do - algorithm = OpenTox::Algorithm.new(to('/lazar',:full)) - algorithm.metadata = { - RDF::DC.title => 'lazar', - RDF::DC.creator => 'helma@in-silico.ch, andreas@maunz.de', - RDF.Type => [RDF::OT.Algorithm] - } - algorithm.parameters = [ - { RDF::DC.description => "Dataset URI", RDF::OT.paramScope => "mandatory", RDF::DC.title => "dataset_uri" }, - { RDF::DC.description => "Feature URI for dependent variable", RDF::OT.paramScope => "optional", RDF::DC.title => "prediction_feature" }, - { RDF::DC.description => "Feature generation service URI", RDF::OT.paramScope => "optional", RDF::DC.title => "feature_generation_uri" }, - { RDF::DC.description => "Feature dataset URI", RDF::OT.paramScope => "optional", RDF::DC.title => "feature_dataset_uri" }, - { RDF::DC.description => "Further parameters for the feature generation service", RDF::OT.paramScope => "optional" } - ] - format_output(algorithm) - end - - - # Create a lazar prediction model - # @param [String] dataset_uri Training dataset URI - # @param [optional,String] prediction_feature URI of the feature to be predicted - # @param [optional,String] feature_generation_uri URI of the feature generation algorithm - # @param [optional,String] - further parameters for the feature generation service - # @return [text/uri-list] Task URI - post '/lazar/?' do - resource_not_found_error "Please provide a dataset_uri parameter." unless params[:dataset_uri] - resource_not_found_error "Dataset '#{params[:dataset_uri]}' not found." unless URI.accessible? params[:dataset_uri] - bad_request_error "Please provide a feature_generation_uri parameter." unless params[:feature_generation_uri] - task = OpenTox::Task.run("Create lazar model", uri('/lazar'), @subjectid) do |task| - lazar = OpenTox::Model.new(nil, @subjectid) - lazar.create_model(params) - lazar.put - lazar.uri - end - response['Content-Type'] = 'text/uri-list' - halt 202,task.uri - end - - - # Make a lazar prediction -- not to be called directly - # @param [String] compound_uri URI of compound to be predicted - # @param [String] training_dataset_uri URI of training dataset - # @param [String] prediction_feature_uri URI of prediction feature - # @param [String] feature_dataset_uri URI of feature dataset - # @param [String] feature_calculation_algorithm Name of feature calculation algorithm - # @param [String] min_sim Numeric value for minimum similarity - # @param [String] prediction_algorithm Name of prediction algorithm - # @param [String] propositionalized Whether propositionalization should be used - # @param [optional,String] pc_type Physico-chemical descriptor type - # @param [optional,String] pc_lib Physico-chemical descriptor library - # @param [optional,String] Further parameters for the feature generation service - # @return [text/uri-list] Task URI - post '/lazar/predict/?' do - # pass parameters instead of model_uri, because model service is blocked by incoming call - - task = OpenTox::Task.run("Apply lazar model",uri('/lazar/predict'), @subjectid) do |task| - - lazar = OpenTox::LazarPrediction.new params - lazar.prediction_dataset.uri - - end - response['Content-Type'] = 'text/uri-list' - halt 202,task.uri - end - - - end -end diff --git a/webapp/sinatra.rb b/webapp/sinatra.rb deleted file mode 100644 index b61b0a3..0000000 --- a/webapp/sinatra.rb +++ /dev/null @@ -1,47 +0,0 @@ -=begin -* Name: sinatra.rb -* Description: Helper code for sinatra -* Author: Andreas Maunz <andreas@maunz.de> -* Date: 10/2012 -=end - -module OpenTox - class Application < Service - - # Conveniently accessible from anywhere within the Application class, - # it negotiates the appropriate output format based on object class - # and requested MIME type. - # @param [Object] an object - # @return [String] object serialization - def format_output (obj) - - if obj.class == String - - case @accept - when /text\/html/ - content_type "text/html" - obj.to_html - else - content_type 'text/uri-list' - obj - end - - else - - case @accept - when "application/rdf+xml" - content_type "application/rdf+xml" - obj.to_rdfxml - when /text\/html/ - content_type "text/html" - obj.to_html - else - content_type "text/turtle" - obj.to_turtle - end - - end - end - - end -end |