Module: BioMart

Defined in:
lib/rbbt/sources/biomart.rb

Overview

This module interacts with BioMart. It performs queries to BioMart and synthesises a hash with the results. Note that this module connects to the online BioMart WS using the Open in ‘rbbt/util/open’ module which offers caching by default. To obtain up to date results you may need to clear the cache from previous queries.

Defined Under Namespace

Classes: QueryError

Constant Summary collapse

BIOMART_URL =
'http://biomart.org/biomart/martservice?query='
MISSING_IN_ARCHIVE =
Rbbt.etc.biomart.missing_in_archive.exists? ? Rbbt.etc.biomart.missing_in_archive.yaml : {}

Class Method Summary collapse

Class Method Details

.query(database, main, attrs = nil, filters = nil, data = nil, open_options = {}) ⇒ Object

This method performs a query in biomart for a datasets and a given set of attributes, there must be a main attribute that will be used as the key in the result hash, optionally there may be a list of additional attributes and filters. The data parameter at the end is used internally to incrementally building the result, due to a limitation of the BioMart WS that only allows 3 external arguments, users normally should leave it unspecified or nil. The result is a hash, where the keys are the different values for the main attribute, and the value is a hash with every other attribute as key, and as value and array with all possible values (Note that for a given value of the main attribute, there may be more than one value for another attribute). If filters is left a nil it adds a filter to the BioMart query to remove results with the main attribute empty, this may cause an error if the BioMart WS does not allow filtering with that attribute.



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
139
140
141
142
143
144
145
146
147
# File 'lib/rbbt/sources/biomart.rb', line 100

def self.query(database, main, attrs = nil, filters = nil, data = nil, open_options = {})
  open_options = Misc.add_defaults open_options, :nocache => false, :filename => nil, :field_names => nil
  filename, field_names = Misc.process_options open_options, :filename, :field_names
  attrs   ||= []
    
  open_options = Misc.add_defaults open_options, :keep_empty => false, :merge => true

  Log.low "BioMart query: '#{main}' [#{(attrs || []) * ', '}] [#{(filters || []) * ', '}] #{open_options.inspect}"

  max_items = 2
  chunks = []
  chunk = []
  attrs.each{|a|
    chunk << a
    if chunk.length == max_items
      chunks << chunk
      chunk = []
    end
  }

  chunks << chunk if chunk.any?

  Log.low "Chunks: #{chunks.length}"
  chunks.each_with_index{|chunk,i|
    Log.low "Chunk #{ i + 1 } / #{chunks.length}: [#{chunk * ", "}]"
    data = get(database, main, chunk, filters, data, open_options)
  }

  open_options[:filename] ||= "BioMart[#{main}+#{attrs.length}]"
  if filename.nil?
    results = TSV.open data, open_options
    results.key_field = main
    results.fields = attrs
    results
  else
    Open.write(filename) do |f|
      f.puts "#: " << Misc.hash2string(TSV::ENTRIES.collect{|key| [key, open_options[key]]})
      if field_names.nil?
        f.puts "#" << [main, attrs].flatten * "\t"
      else
        f.puts "#" << field_names * "\t"
      end
      f.write Open.read(data)
    end
    FileUtils.rm data
    filename
  end
end

.tsv(database, main, attrs = nil, filters = nil, data = nil, open_options = {}) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rbbt/sources/biomart.rb', line 149

def self.tsv(database, main, attrs = nil, filters = nil, data = nil, open_options = {})
  if @archive_url 
    attrs = attrs.reject{|attr| (MISSING_IN_ARCHIVE[@archive] || []).include? attr[1]}
  end

  codes = attrs.collect{|attr| attr[1]}
  if open_options[:filename].nil?
    tsv = query(database, main.last, codes, filters, data, open_options)
    tsv.key_field = main.first
    tsv.fields    = attrs.collect{|attr| attr.first} 
    tsv
  else
    query(database, main.last, codes, filters, data, open_options.merge(:field_names => [main.first, attrs.collect{|attr| attr.first}].flatten))
  end
end