Class: InterMine::Results::ResultsRow

Inherits:
Object
  • Object
show all
Defined in:
lib/intermine/results.rb

Overview

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.

:include:contact_header.rdoc

Instance Method Summary collapse

Constructor Details

#initialize(results, columns) ⇒ ResultsRow

Construct a new result row.

You will not need to do this - ResultsRow objects are created for you when the results are parsed.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/intermine/results.rb', line 52

def initialize(results, columns)
    @results = results.is_a?(Array) ? results : JSON.parse(results)
    unless @results.is_a?(Array)
        raise ArgumentError, "Bad results format: #{results}"
    end
    unless @results.size == columns.size
        raise ArgumentError, "Column size (#{columns.size}) != results size (#{@results.size})"
    end

    @columns = columns
end

Instance Method Details

#[](arg, length = nil) ⇒ Object

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.

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/intermine/results.rb', line 85

def [](arg, length=nil)
    unless length.nil?
        raise ArgumentError, "when providing a length, the first argument must be an Integer" unless arg.is_a? Integer
        return @results[arg, length].map {|x| x["value"]}
    end

    case arg
    when Range
        return @results[arg].map {|x| x["value"]}
    when Integer
        idx = arg
    else
        idx = index_for(arg)
    end

    raise ArgumentError, "Bad argument: #{arg}" if idx.nil?

    cell = @results[idx]

    raise IndexError, "Argument out of range" if cell.nil?

    return cell["value"]
end

#each(&block) ⇒ Object Also known as: each_value, each_pair

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.



134
135
136
137
138
139
140
141
142
143
# File 'lib/intermine/results.rb', line 134

def each(&block) 
    if block
        if block.arity == 1
            @results.each {|x| block.call(x["value"])}
        else block.arity == 2
            (0 ... @results.size).to_a.each {|i| block.call(@columns[i], @results[i]["value"])}
        end
    end
    return self
end

#firstObject

Returns the first value in this row



110
111
112
# File 'lib/intermine/results.rb', line 110

def first
    return @results[0]["value"]
end

#lastObject

Returns the last value in this row



115
116
117
# File 'lib/intermine/results.rb', line 115

def last
    return @results.last["value"]
end

#sizeObject Also known as: length

Return the number of cells in this row.



149
150
151
# File 'lib/intermine/results.rb', line 149

def size
    return @results.size
end

#to_aObject

Return an Array version of the row.



156
157
158
# File 'lib/intermine/results.rb', line 156

def to_a
    return @results.map {|x| x["value"]}
end

#to_csv(sep = "\t") ⇒ Object

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



187
188
189
# File 'lib/intermine/results.rb', line 187

def to_csv(sep="\t")
    return @results.map {|x| x["value"].inspect}.join(sep)
end

#to_hObject

Return a Hash version of the row. All keys in this hash will be full length column headers.



162
163
164
165
166
167
168
169
# File 'lib/intermine/results.rb', line 162

def to_h
    hash = {}
    @results.each_index do |x|
        key = @columns[x].to_s
        hash[key] = self[x]
    end
    return hash
end

#to_sObject

Return a readable representation of the information in this row



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/intermine/results.rb', line 172

def to_s 
    bufs = []
    @results.each_index do |idx|
        buffer = ""
        buffer << @columns[idx].to_headless_s
        buffer << "="
        buffer << self[idx].to_s
        bufs << buffer
    end
    return @columns.first.rootClass.name + ": " + bufs.join(",\t")
end