]> git.donarmstrong.com Git - biopieces.git/commitdiff
adding table.rb
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 24 Oct 2013 08:19:44 +0000 (08:19 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 24 Oct 2013 08:19:44 +0000 (08:19 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@2244 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/lib/maasha/table.rb [new file with mode: 0644]

diff --git a/code_ruby/lib/maasha/table.rb b/code_ruby/lib/maasha/table.rb
new file mode 100644 (file)
index 0000000..3013f5d
--- /dev/null
@@ -0,0 +1,104 @@
+# Copyright (C) 2013 Martin A. Hansen.
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# http://www.gnu.org/copyleft/gpl.html
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# This software is part of the Biopieces framework (www.biopieces.org).
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+require 'maasha/filesys'
+
+# Error class for all exceptions to do with Table.
+class TableError < StandardError; end
+
+class Table
+  def initialize(options = {})
+    @options = options
+    @header  = []
+    @table   = []
+
+    table_parse if @options[:file_in]
+  end
+
+  def rows
+    @table.size
+  end
+
+  def columns
+    @table.first.size
+  end
+
+  def each_row
+    @table.each do |row|
+      yield row
+    end
+
+    self
+  end
+
+  def each_column
+    @table.first.each_with_index do |key, i|
+      column = []
+      column << @header[i] unless @header.empty?
+
+      self.each_row { |r| column << r[i] }
+
+      yield column
+    end
+
+    self
+  end
+
+  def to_s
+    lines = []
+    lines << "#" + @header.join("\t") unless @header.empty?
+
+    self.each_row { |r| lines << r.join("\t") }
+    lines.join("\n")
+  end
+
+  private
+
+  def table_parse
+    File.open(@options[:file_in]) do |ios|
+      ios.each do |line|
+        if line[0] == '#'
+          if @header.empty?
+            @header = line[1 .. line.size].chomp.split(" ")
+
+            uniq = {}
+            
+            @header.each do |key|
+              raise TableError, "Duplicate header: #{key}" if uniq[key]
+              uniq[key] = true
+            end
+          end
+        else
+          fields = line.chomp.split(" ")
+
+          unless @header.empty? 
+            raise TableError, "Bad number of fields: #{fields.size} - expecting: #{@header.size}" unless @header.size == fields.size
+          end
+
+          @table << fields
+        end
+      end
+    end
+  end
+end