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 this class 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
88 89 90 91 92 93 94 |
# File 'lib/bio-table/table.rb', line 88 def [] row if row TableRow.new(@rownames[row],@rows[row]) else nil end end |
#each ⇒ Object
126 127 128 129 130 |
# File 'lib/bio-table/table.rb', line 126 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.
97 98 99 100 101 102 103 |
# File 'lib/bio-table/table.rb', line 97 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
78 79 80 81 82 83 84 85 86 |
# File 'lib/bio-table/table.rb', line 78 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
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bio-table/table.rb', line 50 def read_file filename, = {} lines = [] if not [:in_format] and filename =~ /\.csv$/ @logger.debug "Autodetected CSV file" [:in_format] = :csv end # 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 (the case with merge/concat tables).
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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 newheader = @header[1..-1] # parse the rest prev_line = newheader (lines[1..-1]).each_with_index do | line, line_num | rowname, data_fields = table_apply.parse_row(line_num, line, newheader, 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 return @rownames,@rows end |
#row_by_columns(zip, idx = nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/bio-table/table.rb', line 109 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
105 106 107 |
# File 'lib/bio-table/table.rb', line 105 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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bio-table/table.rb', line 63 def write = {} format = [:format] format = :tab if not format evaluate = nil if format == :eval evaluate = [:evaluate] end formatter = FormatFactory::create(format,evaluate) formatter.write(@header) if [:write_header] each do | tablerow,num | # p tablerow formatter.write(tablerow.all_fields) if tablerow.all_valid? end end |