1 # Copyright (C) 2013 Martin A. Hansen.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # http://www.gnu.org/copyleft/gpl.html
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
21 # This software is part of the Biopieces framework (www.biopieces.org).
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 require 'maasha/filesys'
27 # Error class for all exceptions to do with Table.
28 class TableError < StandardError; end
31 def initialize(options = {})
36 table_parse if @options[:file_in]
56 @table.first.each_with_index do |key, i|
58 column << @header[i] unless @header.empty?
60 self.each_row { |r| column << r[i] }
70 lines << "#" + @header.join("\t") unless @header.empty?
72 self.each_row { |r| lines << r.join("\t") }
79 File.open(@options[:file_in]) do |ios|
83 @header = line[1 .. line.size].chomp.split(" ")
88 raise TableError, "Duplicate header: #{key}" if uniq[key]
93 fields = line.chomp.split(" ")
96 raise TableError, "Bad number of fields: #{fields.size} - expecting: #{@header.size}" unless @header.size == fields.size