-
-my ( $options, $in, $out, $record, $data_out, @vals, $ok, $key, @keys, $A, $B, %no_keys, $sort_keys, $check_sort );
-
-$options = Maasha::Biopieces::parse_options(
- [
- { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
- { long => 'data_out', short => 'o', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
- { long => 'comment', short => 'c', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
- { long => 'delimit', short => 'd', type => 'string', mandatory => 'no', default => "\t", allowed => undef, disallowed => undef },
- { long => 'keys', short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
- { long => 'no_keys', short => 'K', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
- { long => 'compress', short => 'Z', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
- ]
-);
-
-$in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
-$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
-$data_out = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } );
-
-map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
-
-while ( $record = Maasha::Biopieces::get_record( $in ) )
-{
- undef @vals;
- $ok = 1;
-
- if ( $options->{ "keys" } )
- {
- map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
-
- if ( $ok )
- {
- foreach $key ( @{ $options->{ "keys" } } )
- {
- if ( exists $record->{ $key } )
- {
- push @keys, $key if $options->{ "comment" };
- push @vals, $record->{ $key };
- }
- }
- }
- }
+class Numeric
+ def commify
+ self.to_s.gsub(/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/, '\1,')
+ end
+end
+
+require 'terminal-table'
+require 'maasha/biopieces'
+require 'maasha/filesys'
+
+casts = []
+casts << {long: 'no_stream', short: 'x', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil}
+casts << {long: 'pretty', short: 'p', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil}
+casts << {long: 'comment', short: 'c', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil}
+casts << {long: 'commify', short: 'C', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil}
+casts << {long: 'delimit', short: 'd', type: 'string', mandatory: false, default: "\t", allowed: nil, disallowed: nil}
+casts << {long: 'keys', short: 'k', type: 'list', mandatory: false, default: nil, allowed: nil, disallowed: nil}
+casts << {long: 'no_keys', short: 'K', type: 'list', mandatory: false, default: nil, allowed: nil, disallowed: nil}
+casts << {long: 'data_out', short: 'o', type: 'file', mandatory: false, default: nil, allowed: nil, disallowed: nil}
+casts << {long: 'compress', short: 'Z', type: 'string', mandatory: false, default: nil, allowed: "gzip,bzip,bzip2", disallowed: nil}
+
+options = Biopieces.options_parse(ARGV, casts)
+
+compress = options[:compress] ? options[:compress].to_sym : nil
+
+raise "--data_out is mandatory for compressed output" if compress and not options[:data_out]
+
+rows = []
+headings = nil
+comment = true if options[:comment]
+no_keys = options[:no_keys].each_with_object({}) { |i, h| h[i.to_sym] = true } if options[:no_keys]
+
+def columns_order(record)
+ record.keys.each do |key|
+ unless key.match(/^V\d+$/)
+ return record.keys
+ end
+ end
+
+ 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 }
+end
+
+tab_out = options[:data_out] ? Filesys.open(options[:data_out], 'w', compress: compress) : STDOUT
+
+Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
+ input.each do |record|
+ unless headings
+ if options[:keys]
+ headings = options[:keys].map { |k| k.to_sym }
+ else
+ headings = columns_order(record)
+ end
+
+ headings.reject! {|r| no_keys[r] } if options[:no_keys]
+ end
+
+ row = record.values_at(*headings)
+
+ if options[:pretty]
+ rows << row