summaryrefslogtreecommitdiff
path: root/lib/utils/rdf/dataset.rb
blob: 5cfb8273ab434ecbb0a30d087956ebe52d69417d (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
=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