class InterMine::Results::ResultsRow
Synopsis¶ ↑
query.each_row do |row| puts row[0], row[1], row[2] puts row["symbol"], row["proteins.name"] end
Description¶ ↑
A dual-faced object, these representations of rows of results are intended to provide a single object that allows Array-like and Hash-like access to an individual row of data. The primary means of access for this is the use of item access with ResultsRow#[]
, which accepts Array
like index arguments, as well as Hash like key arguments.
As well as value retrieval via indices and keys, iteration is also supported with ResultsRow#each
and the aliases each_pair
and each_value. If one parameter is requested the iteration will be by value, if two are requested it will be by pair.
There is no attempt to fully emulate the complete Hash and Array
interfaces - that would make little sense as it does not make any sense to insert values into a row, or to re-sort it in place. If you want to do such things, call ResultsRow#to_a
or ResultsRow#to_h
to get real Hash or Array
values you are free to manipulate.
- author
-
Alex Kalderimis dev@intermine.org
- homepage
- Licence
-
Copyright (C) 2002-2011 FlyMine
This code may be freely distributed and modified under the terms of the GNU Lesser General Public Licence. This should be distributed with the code. See the LICENSE file for more information or www.gnu.org/copyleft/lesser.html.
Public Class Methods
Construct a new result row.
You will not need to do this - ResultsRow
objects are created for you when the results are parsed.
# File lib/intermine/results.rb 52 def initialize(results, columns) 53 @results = results.is_a?(Array) ? results : JSON.parse(results) 54 unless @results.is_a?(Array) 55 raise ArgumentError, "Bad results format: #{results}" 56 end 57 unless @results.size == columns.size 58 raise ArgumentError, "Column size (#{columns.size}) != results size (#{@results.size})" 59 end 60 61 @columns = columns 62 end
Public Instance Methods
Retrieve values from the row¶ ↑
row[0] # first element row[-1] # last element row[2, 3] # Slice of three cells, starting at cell no. 3 (index 2) row[1..3] # Slice of three cells, starting at cell no. 2 (index 1) row["length"] # Get a cell's value by column name row["Gene.length"] # Use of the root class is optional row[:length] # For bare attributes, a symbol may be used.
This method emulated both the array and hash style of access, based on argument type. Passing in integer indexes or ranges retrieves values in a manner that treats the row as a list of values. Passing in string or symbols retrieves values in a manner that treates the row as a Hash. It is possible to access the values in the row by using either the short or long forms of the column name.
Unlike the corresponding method in either Hash or Array
, this method throws errors when trying to access single elements (not when requesting array slices) and the result cannot be found.
# File lib/intermine/results.rb 85 def [](arg, length=nil) 86 unless length.nil? 87 raise ArgumentError, "when providing a length, the first argument must be an Integer" unless arg.is_a? Integer 88 return @results[arg, length].map {|x| x["value"]} 89 end 90 91 case arg 92 when Range 93 return @results[arg].map {|x| x["value"]} 94 when Integer 95 idx = arg 96 else 97 idx = index_for(arg) 98 end 99 100 raise ArgumentError, "Bad argument: #{arg}" if idx.nil? 101 102 cell = @results[idx] 103 104 raise IndexError, "Argument out of range" if cell.nil? 105 106 return cell["value"] 107 end
Iterate over the values in this row in the order specified by the query's view.
row.each do |value| puts value end row.each do |column, value| puts "#{column} is #{value}" end
If one parameter is specified, then the iteration simply includes values, if more than one is specified, then it will be by column/value pairs.
# File lib/intermine/results.rb 134 def each(&block) 135 if block 136 if block.arity == 1 137 @results.each {|x| block.call(x["value"])} 138 else block.arity == 2 139 (0 ... @results.size).to_a.each {|i| block.call(@columns[i], @results[i]["value"])} 140 end 141 end 142 return self 143 end
Returns the first value in this row
# File lib/intermine/results.rb 110 def first 111 return @results[0]["value"] 112 end
Returns the last value in this row
# File lib/intermine/results.rb 115 def last 116 return @results.last["value"] 117 end
Return the number of cells in this row.
# File lib/intermine/results.rb 149 def size 150 return @results.size 151 end
Return an Array
version of the row.
# File lib/intermine/results.rb 156 def to_a 157 return @results.map {|x| x["value"]} 158 end
Return the information in this row as a line suitable for prining in a CSV or TSV file. The optional separator argument will be used to delimit columns
# File lib/intermine/results.rb 187 def to_csv(sep="\t") 188 return @results.map {|x| x["value"].inspect}.join(sep) 189 end
Return a Hash version of the row. All keys in this hash will be full length column headers.
# File lib/intermine/results.rb 162 def to_h 163 hash = {} 164 @results.each_index do |x| 165 key = @columns[x].to_s 166 hash[key] = self[x] 167 end 168 return hash 169 end
Return a readable representation of the information in this row
# File lib/intermine/results.rb 172 def to_s 173 bufs = [] 174 @results.each_index do |idx| 175 buffer = "" 176 buffer << @columns[idx].to_headless_s 177 buffer << "=" 178 buffer << self[idx].to_s 179 bufs << buffer 180 end 181 return @columns.first.rootClass.name + ": " + bufs.join(",\t") 182 end
Private Instance Methods
Return the index for a string or symbol key.
# File lib/intermine/results.rb 194 def index_for(key) 195 if @indexes.nil? 196 @indexes = {} 197 @results.each_index do |idx| 198 idx_key = @columns[idx] 199 @indexes[idx_key.to_s] = idx 200 201 ## Include root-less paths as aliases 202 # But allow for string columns 203 if idx_key.respond_to?(:to_headless_s) 204 @indexes[idx_key.to_headless_s] = idx 205 end 206 end 207 end 208 return @indexes[key.to_s] 209 end