Class: Biomart::Dataset

Inherits:
Object
  • Object
show all
Includes:
Biomart
Defined in:
lib/biomart/dataset.rb

Overview

Class represetation for a biomart dataset. Can belong to a Biomart::Database and a Biomart::Server.

Constant Summary

Constants included from Biomart

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Biomart

#request

Constructor Details

#initialize(url, args) ⇒ Dataset

Returns a new instance of Dataset.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/biomart/dataset.rb', line 9

def initialize( url, args )
  @url = url or raise ArgumentError, "must pass :url"
  unless @url =~ /martservice/
    @url = @url + "/martservice"
  end
  
  @name         = args["name"] || args[:name]
  @display_name = args["displayName"] || args[:display_name]
  @visible      = ( args["visible"] || args[:visible] ) ? true : false
  
  @filters      = {}
  @attributes   = {}
  @importables  = {}
  @exportables  = {}
end

Instance Attribute Details

#display_nameObject (readonly)

Returns the value of attribute display_name.



7
8
9
# File 'lib/biomart/dataset.rb', line 7

def display_name
  @display_name
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/biomart/dataset.rb', line 7

def name
  @name
end

#visibleObject (readonly)

Returns the value of attribute visible.



7
8
9
# File 'lib/biomart/dataset.rb', line 7

def visible
  @visible
end

Instance Method Details

#attributesObject

Returns a hash (keyed by the biomart ‘internal_name’ for the attribute) of all of the Biomart::Attribute objects belonging to this dataset.



45
46
47
48
49
50
# File 'lib/biomart/dataset.rb', line 45

def attributes
  if @attributes.empty?
    fetch_configuration()
  end
  return @attributes
end

#count(args = {}) ⇒ Object

Function to perform a Biomart count. Returns an integer value for the result of the count query.

optional arguments:

:filters

hash of key-value pairs (filter => search term)



67
68
69
70
71
# File 'lib/biomart/dataset.rb', line 67

def count( args={} )
  args.merge!({ :count => "1" })
  result = request( :method => 'post', :url => @url, :query => generate_xml(args) )
  return result.to_i
end

#filtersObject

Returns a hash (keyed by the biomart ‘internal_name’ for the filter) of all of the Biomart::Filter objects belonging to this dataset.



27
28
29
30
31
32
# File 'lib/biomart/dataset.rb', line 27

def filters
  if @filters.empty?
    fetch_configuration()
  end
  return @filters
end

#generate_xml(args = {}) ⇒ Object

Utility function to build the Biomart query XML



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/biomart/dataset.rb', line 96

def generate_xml( args={} )
  biomart_xml = ""
  xml = Builder::XmlMarkup.new( :target => biomart_xml, :indent => 2 )
  
  xml.instruct!
  xml.declare!( :DOCTYPE, :Query )
  xml.Query( :virtualSchemaName => "default", :formatter => "TSV", :header => "0", :uniqueRows => "1", :count => args[:count], :datasetConfigVersion => "0.6" ) {
    xml.Dataset( :name => @name, :interface => "default" ) {
      
      if args[:filters]
        args[:filters].each do |name,value|
          if value.is_a? Array
            value = value.join(",")
          end
          xml.Filter( :name => name, :value => value )
        end
      else
        self.filters.each do |name,filter|
          if filter.default
            xml.Filter( :name => name, :value => filter.default_value )
          end
        end
      end
      
      unless args[:count]
        if args[:attributes]
          args[:attributes].each do |name|
            xml.Attribute( :name => name )
          end
        else
          self.attributes.each do |name,attribute|
            if attribute.default
              xml.Attribute( :name => name )
            end
          end
        end
      end
      
    }
  }
  
  return biomart_xml
end

#list_attributesObject

Returns an array of the attribute names (biomart ‘internal_name’) for this dataset.



54
55
56
57
58
59
# File 'lib/biomart/dataset.rb', line 54

def list_attributes
  if @attributes.empty?
    fetch_configuration()
  end
  return @attributes.keys
end

#list_filtersObject

Returns an array of the filter names (biomart ‘internal_name’) for this dataset.



36
37
38
39
40
41
# File 'lib/biomart/dataset.rb', line 36

def list_filters
  if @filters.empty?
    fetch_configuration()
  end
  return @filters.keys
end

#search(args = {}) ⇒ Object

Function to perform a Biomart search.

optional arguments:

:filters

hash of key-value pairs (filter => search term)

:attributes

array of attributes to retrieve

:process_results

true/false - convert search results to object

By default will return a hash with the following:

:headers

array of headers

:data

array of arrays containing search results

But with the :process_results option will return an array of hashes, where each hash represents a row of results (keyed by the attribute name).



88
89
90
91
92
93
# File 'lib/biomart/dataset.rb', line 88

def search( args={} )
  response = request( :method => 'post', :url => @url, :query => generate_xml(args) )
  result   = process_tsv( args, response )
  result   = conv_results_to_a_of_h( result ) if args[:process_results]
  return result
end