3 # Copyright (C) 2007-2013 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Write tabular output from the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
33 self.to_s.gsub(/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/, '\1,')
37 require 'terminal-table'
38 require 'maasha/biopieces'
39 require 'maasha/filesys'
42 casts << {long: 'no_stream', short: 'x', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil}
43 casts << {long: 'pretty', short: 'p', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil}
44 casts << {long: 'comment', short: 'c', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil}
45 casts << {long: 'commify', short: 'C', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil}
46 casts << {long: 'delimit', short: 'd', type: 'string', mandatory: false, default: "\t", allowed: nil, disallowed: nil}
47 casts << {long: 'keys', short: 'k', type: 'list', mandatory: false, default: nil, allowed: nil, disallowed: nil}
48 casts << {long: 'no_keys', short: 'K', type: 'list', mandatory: false, default: nil, allowed: nil, disallowed: nil}
49 casts << {long: 'data_out', short: 'o', type: 'file', mandatory: false, default: nil, allowed: nil, disallowed: nil}
50 casts << {long: 'compress', short: 'Z', type: 'string', mandatory: false, default: nil, allowed: "gzip,bzip,bzip2", disallowed: nil}
52 options = Biopieces.options_parse(ARGV, casts)
54 compress = options[:compress] ? options[:compress].to_sym : nil
56 raise "--data_out is mandatory for compressed output" if compress and not options[:data_out]
60 comment = true if options[:comment]
61 no_keys = options[:no_keys].each_with_object({}) { |i, h| h[i.to_sym] = true } if options[:no_keys]
63 def columns_order(record)
64 record.keys.each do |key|
65 unless key.match(/^V\d+$/)
70 record.keys.sort { |a, b| a.to_s[1 .. a.to_s.size].to_i <=> b.to_s[1 .. a.to_s.size].to_i }
73 tab_out = options[:data_out] ? Filesys.open(options[:data_out], 'w', compress: compress) : STDOUT
75 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
76 input.each do |record|
79 headings = options[:keys].map { |k| k.to_sym }
81 headings = columns_order(record)
84 headings.reject! {|r| no_keys[r] } if options[:no_keys]
87 row = record.values_at(*headings)
93 tab_out.puts "#" + headings.join(options[:delimit]) unless headings.empty?
97 tab_out.puts row.join(options[:delimit]) unless row.empty?
100 output.puts record unless options[:no_stream]
105 table = Terminal::Table.new
106 table.headings = headings if options[:comment]
108 first_row = rows.first.dup
112 row.each_with_index do |cell, i|
114 row[i] = cell.to_i.commify
117 row[i] = cell.to_f.commify
127 first_row.each_with_index do |cell, i|
129 table.align_column(i, :right)
140 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<