summaryrefslogtreecommitdiff
path: root/lib/utils/sparql/dataset.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils/sparql/dataset.rb')
-rw-r--r--lib/utils/sparql/dataset.rb39
1 files changed, 33 insertions, 6 deletions
diff --git a/lib/utils/sparql/dataset.rb b/lib/utils/sparql/dataset.rb
index e781f08..ecc0321 100644
--- a/lib/utils/sparql/dataset.rb
+++ b/lib/utils/sparql/dataset.rb
@@ -9,8 +9,8 @@ module OpenTox
class Dataset
# Load features via SPARQL (fast)
- # @param [String] Dataset URI
- # @return [Array] Features in order
+ # @param [String] uri Dataset URI
+ # @return [Array] features OpenTox::Features in order
def self.find_features_sparql(uri)
sparql = "SELECT DISTINCT ?s FROM <#{uri}> WHERE {
?s <#{RDF.type}> <#{RDF::OT.Feature}> ;
@@ -19,9 +19,36 @@ module OpenTox
OpenTox::Backend::FourStore.query(sparql, "text/uri-list").split("\n").collect { |uri| OpenTox::Feature.new uri.strip }
end
+ # Load properties via SPARQL (fast)
+ # @param [Array] uris URIs (assumed ordered)
+ # @param [Hash] properties Properties (keys: user-defined identifier, values: rdf identifier as strings)
+ # @return [Array] types Properties in order of URIs
+ def self.find_props_sparql(uris, props)
+ selects = props.keys
+ conditions = selects.collect{ |k|
+ "<#{props[k]}> ?#{k.to_s}"
+ }
+ h={}
+ uris.each{ |uri|
+ sparql = "SELECT ?id #{selects.collect{|k| "?#{k.to_s}"}.join(" ")} FROM <#{uri}> WHERE { ?id #{conditions.join(";")} }"
+ res = OpenTox::Backend::FourStore.query(sparql, "text/uri-list")
+ res.split("\n").inject(h){ |h,row|
+ values = row.split("\t")
+ id=values.shift
+ h[id] = {}
+ values.each_with_index { |val,idx|
+ h[id][selects[idx]] = [] unless h[id][selects[idx]]
+ h[id][selects[idx]] << val.to_s
+ }
+ h
+ }
+ }
+ h
+ end
+
# Load compounds via SPARQL (fast)
- # @param [String] Dataset URI
- # @return [Array] Compounds in order
+ # @param [String] uri Dataset URI
+ # @return [Array] compounds Compounds in order
def self.find_compounds_sparql(uri)
sparql = "SELECT DISTINCT ?compound FROM <#{uri}> WHERE {
?s <#{RDF.type}> <#{RDF::OT.DataEntry}> ;
@@ -32,8 +59,8 @@ module OpenTox
end
# Load data entries via SPARQL (fast)
- # @param [String] Dataset uri
- # @return [Array] Data entries, ordered primarily over rows and secondarily over cols
+ # @param [String] uri Dataset uri
+ # @return [Array] entries Data entries, ordered primarily over rows and secondarily over cols
def self.find_data_entries_sparql(uri)
sparql = "SELECT ?value FROM <#{uri}> WHERE {
?data_entry <#{RDF::OLO.index}> ?cidx ;