]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/write_tab
rewrote write_tab
[biopieces.git] / bp_bin / write_tab
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2013 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 # This program is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 # Write tabular output from the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 class Numeric
32   def commify
33     self.to_s.gsub(/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/, '\1,')
34   end
35 end
36
37 require 'terminal-table'
38 require 'maasha/biopieces'
39 require 'maasha/filesys'
40
41 casts = []
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}
51
52 options = Biopieces.options_parse(ARGV, casts)
53
54 compress = options[:compress] ? options[:compress].to_sym : nil
55
56 raise "--data_out is mandatory for compressed output" if compress and not options[:data_out]
57
58 rows     = []
59 headings = nil
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]
62
63 def columns_order(record)
64   record.keys.each do |key|
65     unless key.match(/^V\d+$/)
66       return record.keys
67     end
68   end
69
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 }
71 end
72
73 tab_out = options[:data_out] ? Filesys.open(options[:data_out], 'w', compress: compress) : STDOUT
74
75 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
76   input.each do |record|
77     unless headings
78       if options[:keys]
79         headings = options[:keys].map { |k| k.to_sym }
80       else
81         headings = columns_order(record)
82       end
83
84       headings.reject! {|r| no_keys[r] } if options[:no_keys]
85     end
86
87     row = record.values_at(*headings)
88
89     if options[:pretty]
90       rows << row
91     else
92       if comment
93         tab_out.puts "#" + headings.join(options[:delimit]) unless headings.empty?
94         comment = false
95       end
96
97       tab_out.puts row.join(options[:delimit]) unless row.empty?
98     end
99
100     output.puts record unless options[:no_stream]
101   end
102 end
103
104 if options[:pretty]
105   table = Terminal::Table.new
106   table.headings = headings if options[:comment]
107
108   first_row = rows.first.dup
109
110   if options[:commify]
111     rows.each do |row|
112       row.each_with_index do |cell, i|
113         begin Integer(cell)
114           row[i] = cell.to_i.commify
115         rescue
116           begin Float(cell)
117             row[i] = cell.to_f.commify
118           rescue
119           end
120         end
121       end
122     end
123   end
124
125   table.rows = rows
126   
127   first_row.each_with_index do |cell, i|
128     begin Float(cell)
129       table.align_column(i, :right)
130     rescue
131     end
132   end
133
134   tab_out.puts table
135 end
136
137 tab_out.close
138
139
140 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
141
142
143 __END__