Class: BioTable::Table
- Inherits:
-
Object
- Object
- BioTable::Table
- Includes:
- Enumerable
- Defined in:
- lib/bio-table/table.rb
Overview
In memory table representation - note that the default parser/emitter does not use this class as it expects all data to be in memory.
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rownames ⇒ Object
readonly
Returns the value of attribute rownames.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #[](row) ⇒ Object
- #each ⇒ Object
-
#find_fields(rowname) ⇒ Object
Find a record by rowname and return the fields.
-
#initialize(header = nil) ⇒ Table
constructor
A new instance of Table.
- #push(rownames, fields = nil) ⇒ Object
- #read_file(filename, options = {}) ⇒ Object
-
#read_lines(lines, options = {}) ⇒ Object
Read lines (list/array of string) and add them to the table, setting row names and row fields.
- #row_by_columns(zip, idx = nil) ⇒ Object
- #row_by_name(name) ⇒ Object
- #set_name(fn) ⇒ Object
- #write(options = {}) ⇒ Object
Constructor Details
#initialize(header = nil) ⇒ Table
Returns a new instance of Table.
12 13 14 15 16 17 |
# File 'lib/bio-table/table.rb', line 12 def initialize header=nil @header = header if header @logger = Bio::Log::LoggerPlus['bio-table'] @rows = [] @rownames = [] end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
9 10 11 |
# File 'lib/bio-table/table.rb', line 9 def filename @filename end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
10 11 12 |
# File 'lib/bio-table/table.rb', line 10 def header @header end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/bio-table/table.rb', line 9 def name @name end |
#rownames ⇒ Object (readonly)
Returns the value of attribute rownames.
10 11 12 |
# File 'lib/bio-table/table.rb', line 10 def rownames @rownames end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
10 11 12 |
# File 'lib/bio-table/table.rb', line 10 def rows @rows end |
Instance Method Details
#[](row) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/bio-table/table.rb', line 83 def [] row if row TableRow.new(@rownames[row],@rows[row]) else nil end end |
#each ⇒ Object
121 122 123 124 125 |
# File 'lib/bio-table/table.rb', line 121 def each @rows.each_with_index do | row,i | yield TableRow.new(@rownames[i], row) end end |
#find_fields(rowname) ⇒ Object
Find a record by rowname and return the fields. Empty fields are nils.
92 93 94 95 96 97 98 |
# File 'lib/bio-table/table.rb', line 92 def find_fields rowname row = row_by_name(rowname) fields = (row ? row.fields : []) # fill fields with nil to match header length # say header=5 fields=2 fill=2 (skip rowname) fields.fill(nil,fields.size,header.size-1-fields.size) end |
#push(rownames, fields = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/bio-table/table.rb', line 73 def push rownames,fields = nil if fields == nil and rownames.kind_of?(TableRow) @rownames << rownames.rowname @rows << rownames.fields else @rownames << rownames @rows << fields end end |
#read_file(filename, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bio-table/table.rb', line 48 def read_file filename, = {} lines = [] if not [:in_format] and filename =~ /\.csv$/ @logger.debug "Autodetected CSV file" [:in_format] = :csv end @logger.debug() # Read the file lines into an Array, not lazy FIXME File.open(filename).each_line do | line | lines.push line end read_lines(lines, ) end |
#read_lines(lines, options = {}) ⇒ Object
Read lines (list/array of string) and add them to the table, setting row names and row fields. The first row is assumed to be the header and ignored if the header has been set.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bio-table/table.rb', line 28 def read_lines lines, = {} table_apply = TableApply.new() header = table_apply.parse_header(lines[0], ) Validator::valid_header?(header, @header) # compare against older header when merging column_index,header = table_apply.column_index(header) # we may rewrite the header @header = header if not @header # parse the rest prev_line = @header[1..-1] (lines[1..-1]).each_with_index do | line, line_num | rowname, data_fields = table_apply.parse_row(line_num, line, column_index, prev_line, ) if data_fields @rownames << rowname if not [:with_rownames] # otherwise doubles rownames @rows << data_fields if data_fields end prev_line = data_fields end end |
#row_by_columns(zip, idx = nil) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/bio-table/table.rb', line 104 def row_by_columns zip,idx=nil index = zip.first[0] value = zip.first[1] if idx row = idx[zip.transpose[1]] return row if row.match_all_fields?(zip) else each do | row | fields = row.all_fields if fields[index] == value return row if row.match_all_fields?(zip) end end end nil end |
#row_by_name(name) ⇒ Object
100 101 102 |
# File 'lib/bio-table/table.rb', line 100 def row_by_name name self[rownames.index(name)] end |
#set_name(fn) ⇒ Object
19 20 21 22 |
# File 'lib/bio-table/table.rb', line 19 def set_name fn @filename = fn @name = File.basename(fn,File.extname(fn)) end |
#write(options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/bio-table/table.rb', line 62 def write = {} format = [:format] format = :tab if not format formatter = FormatFactory::create(format) formatter.write(@header) if [:write_header] each do | tablerow | # p tablerow formatter.write(tablerow.all_fields) if tablerow.all_valid? end end |