summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndreas Maunz <andreas@maunz.de>2012-10-29 16:44:03 +0100
committerAndreas Maunz <andreas@maunz.de>2012-10-29 16:44:03 +0100
commit19582925f0496e4cb07e71ead8aea1261abf0bc8 (patch)
tree4df2da4026889878f6b913e96c0b9b764434a198 /lib
parentb55f670feb82dff3c782b4f86bf90ac1212a0361 (diff)
sparql property request function
Diffstat (limited to 'lib')
-rw-r--r--lib/utils/rdf/dataset.rb12
-rw-r--r--lib/utils/sparql/dataset.rb39
2 files changed, 39 insertions, 12 deletions
diff --git a/lib/utils/rdf/dataset.rb b/lib/utils/rdf/dataset.rb
index 5cfb827..b2deeb8 100644
--- a/lib/utils/rdf/dataset.rb
+++ b/lib/utils/rdf/dataset.rb
@@ -9,8 +9,8 @@ module OpenTox
class Dataset
# Load features via RDF (slow)
- # @param [String] Dataset URI
- # @return [Array] Features in order
+ # @param [String] uri Dataset URI
+ # @return [Array] features Features in order
def self.find_features_rdf(rdf)
query = RDF::Query.new do
pattern [:uri, RDF.type, RDF::OT.Feature]
@@ -20,8 +20,8 @@ module OpenTox
end
# Load compounds via RDF (slow)
- # @param [String] Dataset URI
- # @return [Array] Compounds in order
+ # @param [String] uri Dataset URI
+ # @return [Array] compounds Compounds in order
def self.find_compounds_rdf(rdf)
query = RDF::Query.new do
pattern [:uri, RDF.type, RDF::OT.Compound]
@@ -31,8 +31,8 @@ module OpenTox
end
# Load data entries via RDF (slow)
- # @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_rdf(rdf)
query = RDF::Query.new do
pattern [:data_entry, RDF::OLO.index, :cidx] # compound index: now a free variable
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 ;