From 9c8b20a910d316c19d24e79dcf52868b6b8383b7 Mon Sep 17 00:00:00 2001 From: Andreas Maunz Date: Wed, 24 Oct 2012 16:35:09 +0200 Subject: Merged shims --- lib/opentox-client.rb | 3 ++ lib/utils/diag.rb | 11 +++++ lib/utils/html.rb | 49 +++++++++++++++++++++ lib/utils/shims/dataset.rb | 104 +++++++++++++++++++++++++++++++++++++++++++++ lib/utils/shims/feature.rb | 87 +++++++++++++++++++++++++++++++++++++ lib/utils/shims/opentox.rb | 51 ++++++++++++++++++++++ lib/utils/shims/task.rb | 22 ++++++++++ 7 files changed, 327 insertions(+) create mode 100644 lib/utils/diag.rb create mode 100644 lib/utils/html.rb create mode 100644 lib/utils/shims/dataset.rb create mode 100644 lib/utils/shims/feature.rb create mode 100644 lib/utils/shims/opentox.rb create mode 100644 lib/utils/shims/task.rb (limited to 'lib') diff --git a/lib/opentox-client.rb b/lib/opentox-client.rb index d08d07a..67e0ce7 100644 --- a/lib/opentox-client.rb +++ b/lib/opentox-client.rb @@ -39,3 +39,6 @@ FALSE_REGEXP = /^(false|inactive|0|0.0|low tox|deactivating|non-carcinogen|non-m "algorithm.rb" ].each{ |f| require File.join(File.dirname(__FILE__),f) } +Dir["#{File.dirname(__FILE__)}/utils/shims/*.rb"].each { |f| require f } # Shims for legacy code +Dir["#{File.dirname(__FILE__)}/utils/*.rb"].each { |f| require f } # Utils for Libs + diff --git a/lib/utils/diag.rb b/lib/utils/diag.rb new file mode 100644 index 0000000..fd37945 --- /dev/null +++ b/lib/utils/diag.rb @@ -0,0 +1,11 @@ +=begin +* Name: diag.rb +* Description: Diagnostic tools +* Author: Andreas Maunz +* Date: 10/2012 +=end + +# Print a diagnostic message +def uri_list + puts "My load path is:\n#{$LOAD_PATH.join("\n")} \nI have loaded #{$LOADED_FEATURES.size} objects.\n\n" +end diff --git a/lib/utils/html.rb b/lib/utils/html.rb new file mode 100644 index 0000000..91dfc64 --- /dev/null +++ b/lib/utils/html.rb @@ -0,0 +1,49 @@ +#OT_LOGO = File.join(CONFIG[:services]["opentox-validation"],"resources/ot-logo.png") + +=begin +* Name: html.rb +* Description: Tools to provide html output +* Author: Andreas Maunz +* Date: 10/2012 +=end + +# AM: needed since this gem has a nested directory structure + +class String + # encloses URI in text with with link tag + # @return [String] new text with marked links + def link_urls + regex = Regexp.new '(https?:\/\/[\S]+)([>"])' + self.gsub( regex, '\1\2' ) + end +end + +module OpenTox + + # produces a html page for making web services browser friendly + # format of text (=string params) is preserved (e.g. line breaks) + # urls are marked as links + # + # @param [String] text this is the actual content, + # @param [optional,String] related_links info on related resources + # @param [optional,String] description general info + # @param [optional,Array] post_command, infos for the post operation, object defined below + # @return [String] html page + def self.text_to_html( text, subjectid=nil, related_links=nil, description=nil, post_command=nil ) + + # TODO add title as parameter + title = nil #$sinatra.url_for($sinatra.request.env['PATH_INFO'], :full) if $sinatra + html = "" + html += ""+title+"" if title + #html += "<\/img>" + + html += "

Description

"+description.link_urls+"

" if description + html += "

Related links

"+related_links.link_urls+"

" if related_links + html += "

Content

" if description || related_links + html += "

" + html += text.link_urls + html += "

" + html + end + +end diff --git a/lib/utils/shims/dataset.rb b/lib/utils/shims/dataset.rb new file mode 100644 index 0000000..75948e0 --- /dev/null +++ b/lib/utils/shims/dataset.rb @@ -0,0 +1,104 @@ +=begin +* Name: dataset.rb +* Description: Dataset shims +* Author: Andreas Maunz +* 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 + + + + ### 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 + 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 new file mode 100644 index 0000000..297748b --- /dev/null +++ b/lib/utils/shims/feature.rb @@ -0,0 +1,87 @@ +=begin +* Name: feature.rb +* Description: Feature shims +* Author: Andreas Maunz +* 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 [RDF.type].to_a.flatten.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/opentox.rb b/lib/utils/shims/opentox.rb new file mode 100644 index 0000000..c10d535 --- /dev/null +++ b/lib/utils/shims/opentox.rb @@ -0,0 +1,51 @@ +=begin +* Name: opentox.rb +* Description: Architecture shims +* Author: Andreas Maunz +* 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 new file mode 100644 index 0000000..cb73e72 --- /dev/null +++ b/lib/utils/shims/task.rb @@ -0,0 +1,22 @@ +=begin +* Name: task.rb +* Description: Task shims +* Author: Andreas Maunz +* Date: 10/2012 +=end + + +module OpenTox + + # Shims for the Task class + class Task + + # Check status of a task + # @return [String] Status + def status + self[RDF::OT.hasStatus] + end + + end + +end -- cgit v1.2.3