diff options
Diffstat (limited to 'lib/utils/shims')
-rw-r--r-- | lib/utils/shims/dataset.rb | 201 | ||||
-rw-r--r-- | lib/utils/shims/feature.rb | 87 | ||||
-rw-r--r-- | lib/utils/shims/model.rb | 40 | ||||
-rw-r--r-- | lib/utils/shims/opentox.rb | 51 | ||||
-rw-r--r-- | lib/utils/shims/task.rb | 62 |
5 files changed, 0 insertions, 441 deletions
diff --git a/lib/utils/shims/dataset.rb b/lib/utils/shims/dataset.rb deleted file mode 100644 index f72ff1b..0000000 --- a/lib/utils/shims/dataset.rb +++ /dev/null @@ -1,201 +0,0 @@ -=begin -* Name: dataset.rb -* Description: Dataset shims -* Author: Andreas Maunz <andreas@maunz.de> -* Date: 10/2012 -=end - -module OpenTox - - # Shims for the Dataset Class - class Dataset - - attr_accessor :feature_positions, :compound_positions - - # Load a dataset from URI - # @param [String] Dataset URI - # @return [OpenTox::Dataset] Dataset object - def self.find(uri, subjectid=nil) - return nil unless uri - ds = OpenTox::Dataset.new uri, subjectid - ds.get - ds - end - - def self.exist?(uri, subjectid=nil) - ds = OpenTox::Dataset.new uri, subjectid - begin - ds.get_metadata - true - rescue - false - end - end - - def split( compound_indices, feats, metadata, subjectid=nil) - - raise "Dataset.split : pls give compounds as indices" if compound_indices.size==0 or !compound_indices[0].is_a?(Fixnum) - raise "Dataset.split : pls give features as feature objects (given: #{feats})" if feats!=nil and feats.size>0 and !feats[0].is_a?(OpenTox::Feature) - $logger.debug "split dataset using "+compound_indices.size.to_s+"/"+@compounds.size.to_s+" compounds" - - dataset = OpenTox::Dataset.new(nil, subjectid) - dataset.metadata = metadata - dataset.features = (feats ? feats : self.features) - compound_indices.each do |c_idx| - dataset << [ self.compounds[c_idx] ] + dataset.features.each_with_index.collect{|f,f_idx| self.data_entries[c_idx][f_idx]} - end - - #compound_indices.each do |c_idx| - # c = @compounds[c_idx] - # dataset.add_compound(c) - # if @data_entries[c] - # features.each do |f| - # if @data_entries[c][f] - # dataset.add_data_entry c,f,@data_entries[c][f][entry_index(c_idx)] - # else - # dataset.add_data_entry c,f,nil - # end - # end - # end - # end - - dataset.put subjectid - dataset - end - - - # maps a compound-index from another dataset to a compound-index from this dataset - # mapping works as follows: - # (compound c is the compound identified by the compound-index of the other dataset) - # * c occurs only once in this dataset? map compound-index of other dataset to index in this dataset - # * c occurs >1 in this dataset? - # ** number of occurences is equal in both datasets? assume order is preserved(!) and map accordingly - # ** number of occurences is not equal in both datasets? cannot map, raise error - # @param [OpenTox::Dataset] dataset that should be mapped to this dataset (fully loaded) - # @param [Fixnum] compound_index, corresponding to dataset - def compound_index( dataset, compound_index ) - unless defined?(@index_map) and @index_map[dataset.uri] - map = {} - dataset.compounds.collect{|c| c.uri}.uniq.each do |compound| - self_indices = compound_indices(compound) - next unless self_indices - dataset_indices = dataset.compound_indices(compound) - if self_indices.size==1 - dataset_indices.size.times do |i| - map[dataset_indices[i]] = self_indices[0] - end - elsif self_indices.size==dataset_indices.size - # we do assume that the order is preseverd! - dataset_indices.size.times do |i| - map[dataset_indices[i]] = self_indices[i] - end - else - raise "cannot map compound #{compound} from dataset #{dataset.uri} to dataset #{uri}, "+ - "compound occurs #{dataset_indices.size} times and #{self_indices.size} times" - end - end - @index_map = {} unless defined?(@index_map) - @index_map[dataset.uri] = map - end - @index_map[dataset.uri][compound_index] - end - - def compound_indices( compound ) - unless defined?(@cmp_indices) and @cmp_indices.has_key?(compound) - @cmp_indices = {} - @compounds.size.times do |i| - c = @compounds[i].uri - if @cmp_indices[c]==nil - @cmp_indices[c] = [i] - else - @cmp_indices[c] = @cmp_indices[c]+[i] - end - end - end - @cmp_indices[compound] - end - - def data_entry_value(compound_index, feature_uri) - build_feature_positions unless @feature_positions - @data_entries[compound_index][@feature_positions[feature_uri]] - end - - ### Index Structures - - # Create value map - # @param [OpenTox::Feature] A feature - # @return [Hash] A hash with keys 1...feature.training_classes.size and values training classes - def value_map(feature) - training_classes = feature.accept_values - raise "no accept values for feature #{feature.uri} in dataset #{uri}" unless training_classes - training_classes.each_index.inject({}) { |h,idx| h[idx+1]=training_classes[idx]; h } - end - - # Create feature positions map - # @return [Hash] A hash with keys feature uris and values feature positions - def build_feature_positions - unless @feature_positions - @feature_positions = @features.each_index.inject({}) { |h,idx| - internal_server_error "Duplicate Feature '#{@features[idx].uri}' in dataset '#{@uri}'" if h[@features[idx].uri] - h[@features[idx].uri] = idx - h - } - end - end - - # Create compounds positions map - # @return [Hash] A hash with keys compound uris and values compound position arrays - def build_compound_positions - unless @compound_positions - @compound_positions = @compounds.each_index.inject({}) { |h,idx| - inchi=OpenTox::Compound.new(@compounds[idx].uri).inchi - h[inchi] = [] unless h[inchi] - h[inchi] << idx if inchi =~ /InChI/ - h - } - end - end - - - ### Associative Search Operations - - # Search a dataset for a feature given its URI - # @param [String] Feature URI - # @return [OpenTox::Feature] Feature object, or nil if not present - def find_feature(uri) - build_feature_positions - res = @features[@feature_positions[uri]] if @feature_positions[uri] - res - end - - # Search a dataset for a compound given its URI - # @param [String] Compound URI - # @return [OpenTox::Compound] Array of compound objects, or nil if not present - def find_compound(uri) - build_compound_positions - inchi = OpenTox::Compound.new(uri).inchi - res = @compounds[@compound_positions[inchi]] if inchi =~ /InChI/ and @compound_positions[inchi] - res - end - - # Search a dataset for a data entry given compound URI and feature URI - # @param [String] Compound URI - # @param [String] Feature URI - # @return [Object] Data entry, or nil if not present - def find_data_entry(compound_uri, feature_uri) - build_compound_positions - build_feature_positions - inchi = OpenTox::Compound.new(compound_uri).inchi - if @compound_positions[inchi] && @feature_positions[feature_uri] - res = [] - @compound_positions[inchi].each { |idx| - res << data_entries[idx][@feature_positions[feature_uri]] - } - end - res - end - - end - - -end diff --git a/lib/utils/shims/feature.rb b/lib/utils/shims/feature.rb deleted file mode 100644 index 9afa5c2..0000000 --- a/lib/utils/shims/feature.rb +++ /dev/null @@ -1,87 +0,0 @@ -=begin -* Name: feature.rb -* Description: Feature shims -* Author: Andreas Maunz <andreas@maunz.de> -* Date: 10/2012 -=end - -module OpenTox - - # Shims for the feature class - class Feature - - # Load a feature from URI - # @param [String] Feature URI - # @return [OpenTox::Feature] Feature object with the full data - def self.find(uri, subjectid=nil) - return nil unless uri - f = OpenTox::Feature.new uri, subjectid - f.get - f - end - - # Load or create a feature given its title and metadata - # Create it if: a) not present, or b) present, but differs in metadata - # Newly created features are stored at the backend - # @param[String] title Feature title - # @param[Hash] metadata Feature metadata - # @return [OpenTox::Feature] Feature object with the full data, or nil - def self.find_by_title(title, metadata) - metadata[RDF.type] = [] unless metadata[RDF.type] - metadata[RDF.type] << RDF::OT.Feature unless metadata[RDF.type].include?(RDF::OT.Feature) - metadata[RDF::DC.title] = title unless (metadata[RDF::DC.title]) - feature = feature_new = OpenTox::Feature.new(File.join($feature[:uri], SecureRandom.uuid), @subjectid) - feature_new.metadata = metadata - sparql = "SELECT DISTINCT ?feature WHERE { ?feature <#{RDF.type}> <#{RDF::OT['feature'.capitalize]}>. ?feature <#{RDF::DC.title}> '#{title.to_s}' }" - feature_uris = OpenTox::Backend::FourStore.query(sparql,"text/uri-list").split("\n") - features_equal = false # relevant also when no features found - feature_uris.each_with_index { |feature_uri,idx| - feature_existing = OpenTox::Feature.find(feature_uri, @subjectid) - if (feature_new.metadata.size+1 == feature_existing.metadata.size) # +1 due to title - features_equal = metadata.keys.collect { |predicate| - unless ( predicate == RDF::DC.title ) - if feature_new[predicate].class == feature_existing[predicate].class - case feature_new[predicate].class.to_s - when "Array" then (feature_new[predicate].sort == feature_existing[predicate].sort) - else (feature_new[predicate] == feature_existing[predicate]) - end - end - else - true - end - }.uniq == [true] - end - (feature=feature_existing and break) if features_equal - } - unless features_equal - feature_new.put - end - feature - end - - # Find out feature type - # Classification takes precedence - # @return [String] Feature type - def feature_type - bad_request_error "rdf type of feature '#{@uri}' not set" unless self[RDF.type] - if self[RDF.type].include?(OT.NominalFeature) - "classification" - elsif self[RDF.type].include?(OT.NumericFeature) - "regression" - else - "unknown" - end - end - - # Get accept values - # @param[String] Feature URI - # @return[Array] Accept values - def accept_values - accept_values = self[OT.acceptValue] - accept_values.sort if accept_values - accept_values - end - - end - -end diff --git a/lib/utils/shims/model.rb b/lib/utils/shims/model.rb deleted file mode 100644 index 26a82c4..0000000 --- a/lib/utils/shims/model.rb +++ /dev/null @@ -1,40 +0,0 @@ - - -module OpenTox - - # Shims for the Task class - class Model - - def feature_type(subjectid=nil) - unless @feature_type - get unless metadata[OT.dependentVariables.to_s] - raise "cannot determine feature type, dependent variable missing" unless metadata[OT.dependentVariables.to_s] - @feature_type = OpenTox::Feature.find( metadata[OT.dependentVariables.to_s][0], subjectid ).feature_type - end - @feature_type - end - - def predicted_variable(subjectid=nil) - load_predicted_variables(subjectid) unless defined? @predicted_var - @predicted_var - end - - def predicted_confidence(subjectid=nil) - load_predicted_variables(subjectid) unless defined? @predicted_conf - @predicted_conf - end - - private - def load_predicted_variables(subjectid=nil) - metadata[OT.predictedVariables.to_s].each do |f| - feat = OpenTox::Feature.find( f, subjectid ) - if feat.title =~ /confidence/ - @predicted_conf = f - else - @predicted_var = f unless @predicted_var - end - end - end - - end -end
\ No newline at end of file diff --git a/lib/utils/shims/opentox.rb b/lib/utils/shims/opentox.rb deleted file mode 100644 index c10d535..0000000 --- a/lib/utils/shims/opentox.rb +++ /dev/null @@ -1,51 +0,0 @@ -=begin -* Name: opentox.rb -* Description: Architecture shims -* Author: Andreas Maunz <andreas@maunz.de> -* Date: 10/2012 -=end - -# This avoids having to prefix everything with "RDF::" (e.g. "RDF::DC"). -# So that we can use our old code mostly as is. -include RDF - -module OpenTox - - # Help function to provide the metadata= functionality. - # Downward compatible to opentox-ruby. - # @param [Hash] Key-Value pairs with the metadata - # @return self - def metadata=(hsh) - hsh.each {|k,v| - self[k]=v - } - end - - - ### Index Structures - - # Create parameter positions map - # @return [Hash] A hash with keys parameter names and values parameter positions - def build_parameter_positions - unless @parameter_positions - @parameters = parameters - @parameter_positions = @parameters.each_index.inject({}) { |h,idx| - h[@parameters[idx][DC.title.to_s]] = idx - h - } - end - end - - - ### Associative Search Operations - - # Search a model for a given parameter - # @param[String] The parameter title - # @return[Object] The parameter value - def find_parameter_value(title) - build_parameter_positions - res = @parameters[@parameter_positions[title]][OT.paramValue.to_s] if @parameter_positions[title] - res - end - -end diff --git a/lib/utils/shims/task.rb b/lib/utils/shims/task.rb deleted file mode 100644 index 7ac8a7d..0000000 --- a/lib/utils/shims/task.rb +++ /dev/null @@ -1,62 +0,0 @@ -=begin -* Name: task.rb -* Description: Task shims -* Author: Andreas Maunz <andreas@maunz.de> -* Date: 10/2012 -=end - - -module OpenTox - - # Shims for the Task class - class Task - - def self.run(description, creator, subjectid=nil) - create($task[:uri],subjectid,{ RDF::DC.description => description, RDF::DC.creator => creator},&Proc.new) - end - - # Check status of a task - # @return [String] Status - def status - self[RDF::OT.hasStatus] - end - - def code - RestClientWrapper.head(@uri).code - end - - end - -end - - -module OpenTox - - class SubTask - - def initialize(task, min, max) - #TODO add subtask code - end - - def self.create(task, min, max) - if task - SubTask.new(task, min, max) - else - nil - end - end - - def waiting_for(task_uri) - #TODO add subtask code - end - - def progress(pct) - #TODO add subtask code - end - - def running?() - #TODO add subtask code - end - end - -end
\ No newline at end of file |