summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
authorAndreas Maunz <andreas@maunz.de>2012-10-26 16:11:43 +0200
committerAndreas Maunz <andreas@maunz.de>2012-10-26 16:11:43 +0200
commitb55f670feb82dff3c782b4f86bf90ac1212a0361 (patch)
tree3c276dc147f6ec6cc29836c614ea74caefc071ca /lib/utils
parentf519c7cf4ef23289dd3511fc00312dbed2b56d09 (diff)
Separate libs for sparql and rdf
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/rdf/dataset.rb48
-rw-r--r--lib/utils/shims/dataset.rb38
-rw-r--r--lib/utils/sparql/dataset.rb49
3 files changed, 97 insertions, 38 deletions
diff --git a/lib/utils/rdf/dataset.rb b/lib/utils/rdf/dataset.rb
new file mode 100644
index 0000000..5cfb827
--- /dev/null
+++ b/lib/utils/rdf/dataset.rb
@@ -0,0 +1,48 @@
+=begin
+* Name: dataset.rb
+* Description: Dataset RDF tools
+* Author: Andreas Maunz <andreas@maunz.de>
+* Date: 10/2012
+=end
+
+module OpenTox
+ class Dataset
+
+ # Load features via RDF (slow)
+ # @param [String] Dataset URI
+ # @return [Array] Features in order
+ def self.find_features_rdf(rdf)
+ query = RDF::Query.new do
+ pattern [:uri, RDF.type, RDF::OT.Feature]
+ pattern [:uri, RDF::OLO.index, :idx]
+ end
+ query.execute(rdf).sort_by{|s| s.idx}.collect{|s| OpenTox::Feature.new(s.uri.to_s)}
+ end
+
+ # Load compounds via RDF (slow)
+ # @param [String] Dataset URI
+ # @return [Array] Compounds in order
+ def self.find_compounds_rdf(rdf)
+ query = RDF::Query.new do
+ pattern [:uri, RDF.type, RDF::OT.Compound]
+ pattern [:uri, RDF::OLO.index, :idx]
+ end
+ query.execute(rdf).sort_by{|s| s.idx}.collect{|s| OpenTox::Compound.new(s.uri.to_s)}
+ end
+
+ # Load data entries via RDF (slow)
+ # @param [String] Dataset uri
+ # @return [Array] 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
+ pattern [:data_entry, RDF::OT.values, :vals]
+ pattern [:vals, RDF::OT.feature, :f]
+ pattern [:f, RDF::OLO.index, :fidx]
+ pattern [:vals, RDF::OT.value, :val]
+ end
+ query.execute(rdf).order_by(:fidx, :cidx).collect { |s| s.val.to_s }
+ end
+
+ end
+end
diff --git a/lib/utils/shims/dataset.rb b/lib/utils/shims/dataset.rb
index b5faf18..912510c 100644
--- a/lib/utils/shims/dataset.rb
+++ b/lib/utils/shims/dataset.rb
@@ -23,44 +23,6 @@ module OpenTox
end
- # Load features via SPARQL (fast)
- # @param [String] Dataset URI
- # @return [Array] Features in order
- def self.find_features(uri)
- sparql = "SELECT DISTINCT ?s FROM <#{uri}> WHERE {
- ?s <#{RDF.type}> <#{RDF::OT.Feature}> ;
- <#{RDF::OLO.index}> ?fidx
- } ORDER BY ?fidx"
- OpenTox::Backend::FourStore.query(sparql, "text/uri-list").split("\n").collect { |uri| OpenTox::Feature.new uri.strip }
- end
-
- # Load compounds via SPARQL (fast)
- # @param [String] Dataset URI
- # @return [Array] Compounds in order
- def self.find_compounds(uri)
- sparql = "SELECT DISTINCT ?compound FROM <#{uri}> WHERE {
- ?s <#{RDF.type}> <#{RDF::OT.DataEntry}> ;
- <#{RDF::OLO.index}> ?cidx;
- <#{RDF::OT.compound}> ?compound
- } ORDER BY ?cidx"
- OpenTox::Backend::FourStore.query(sparql, "text/uri-list").split("\n").collect { |uri| OpenTox::Compound.new uri.strip }
- end
-
- # Load data entries via SPARQL (fast)
- # @param [String] Dataset uri
- # @return [Array] Data entries, ordered primarily over rows and secondarily over cols
- def self.find_data_entries(uri)
- sparql = "SELECT ?value FROM <#{uri}> WHERE {
- ?data_entry <#{RDF::OLO.index}> ?cidx ;
- <#{RDF::OT.values}> ?v .
- ?v <#{RDF::OT.feature}> ?f;
- <#{RDF::OT.value}> ?value .
- ?f <#{RDF::OLO.index}> ?fidx.
- } ORDER BY ?cidx ?fidx"
- OpenTox::Backend::FourStore.query(sparql,"text/uri-list").split("\n").collect { |val| val.strip }
- end
-
-
### Index Structures
# Create value map
diff --git a/lib/utils/sparql/dataset.rb b/lib/utils/sparql/dataset.rb
new file mode 100644
index 0000000..e781f08
--- /dev/null
+++ b/lib/utils/sparql/dataset.rb
@@ -0,0 +1,49 @@
+=begin
+* Name: dataset.rb
+* Description: Dataset SPARQL tools
+* Author: Andreas Maunz <andreas@maunz.de>
+* Date: 10/2012
+=end
+
+module OpenTox
+ class Dataset
+
+ # Load features via SPARQL (fast)
+ # @param [String] Dataset URI
+ # @return [Array] Features in order
+ def self.find_features_sparql(uri)
+ sparql = "SELECT DISTINCT ?s FROM <#{uri}> WHERE {
+ ?s <#{RDF.type}> <#{RDF::OT.Feature}> ;
+ <#{RDF::OLO.index}> ?fidx
+ } ORDER BY ?fidx"
+ OpenTox::Backend::FourStore.query(sparql, "text/uri-list").split("\n").collect { |uri| OpenTox::Feature.new uri.strip }
+ end
+
+ # Load compounds via SPARQL (fast)
+ # @param [String] Dataset URI
+ # @return [Array] Compounds in order
+ def self.find_compounds_sparql(uri)
+ sparql = "SELECT DISTINCT ?compound FROM <#{uri}> WHERE {
+ ?s <#{RDF.type}> <#{RDF::OT.DataEntry}> ;
+ <#{RDF::OLO.index}> ?cidx;
+ <#{RDF::OT.compound}> ?compound
+ } ORDER BY ?cidx"
+ OpenTox::Backend::FourStore.query(sparql, "text/uri-list").split("\n").collect { |uri| OpenTox::Compound.new uri.strip }
+ end
+
+ # Load data entries via SPARQL (fast)
+ # @param [String] Dataset uri
+ # @return [Array] 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 ;
+ <#{RDF::OT.values}> ?v .
+ ?v <#{RDF::OT.feature}> ?f;
+ <#{RDF::OT.value}> ?value .
+ ?f <#{RDF::OLO.index}> ?fidx.
+ } ORDER BY ?cidx ?fidx"
+ OpenTox::Backend::FourStore.query(sparql,"text/uri-list").split("\n").collect { |val| val.strip }
+ end
+
+ end
+end