summaryrefslogtreecommitdiff
path: root/lib/utils/sparql/dataset.rb
blob: e781f08bce3a604d573611b7260b99c64d648e4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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