Method: BioMart.query
- Defined in:
- lib/rbbt/sources/biomart.rb
.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.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rbbt/sources/biomart.rb', line 136 def self.query(database, main, attrs = nil, filters = nil, data = nil, = {}) = Misc.add_defaults , :nocache => false, :filename => nil, :field_names => nil, :by_chr => false filename, field_names, by_chr = Misc. , :filename, :field_names, :by_chr attrs ||= [] = Misc.add_defaults , :keep_empty => false, :merge => true Log.low "BioMart query: '#{main}' [#{(attrs || []) * ', '}] [#{(filters || []) * ', '}] #{.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? chunks << [] if chunks.empty? Log.low "Chunks: #{chunks.length}" if chunks.any? chunks.each_with_index{|chunk,i| Log.low "Chunk #{ i + 1 } / #{chunks.length}: [#{chunk * ", "}]" data = get(database, main, chunk, filters, data, .dup) } else data = get(database, main, [], filters, data, .dup) end [:filename] = "BioMart[#{main}+#{attrs.length}]" if filename.nil? results = TSV.open data, results.key_field = main results.fields = attrs results else Open.write(filename) do |f| f.puts "#: " << Misc.hash2string(TSV::ENTRIES.collect{|key| [key, [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 |