3 # Copyright (C) 2007-2009 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
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Write tabular output from the stream.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
34 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37 my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, $data_out, @vals, $ok, $key, @keys, $A, $B, %no_keys, $sort_keys, $check_sort );
39 $options = Maasha::Biopieces::parse_options(
41 { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
42 { long => 'data_out', short => 'o', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
43 { long => 'comment', short => 'c', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
44 { long => 'delimit', short => 'd', type => 'string', mandatory => 'no', default => "\t", allowed => undef, disallowed => undef },
45 { long => 'keys', short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
46 { long => 'no_keys', short => 'K', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
47 { long => 'compress', short => 'Z', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
51 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
52 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
53 $data_out = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } );
55 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
57 while ( $record = Maasha::Biopieces::get_record( $in ) )
62 if ( $options->{ "keys" } )
64 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
68 foreach $key ( @{ $options->{ "keys" } } )
70 if ( exists $record->{ $key } )
72 push @keys, $key if $options->{ "comment" };
73 push @vals, $record->{ $key };
80 if ( not $check_sort )
84 map { $sort_keys = 0 if $_ !~ /^V(\d+)$/ } keys %{ $record };
91 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
93 next if exists $no_keys{ $key };
95 push @keys, $key if $options->{ "comment" };
96 push @vals, $record->{ $key };
101 foreach $key ( keys %{ $record } )
103 next if exists $no_keys{ $key };
105 push @keys, $key if $options->{ "comment" };
106 push @vals, $record->{ $key };
112 if ( @keys and $options->{ "comment" } )
114 print $data_out "#", join( $options->{ "delimit" }, @keys ), "\n";
116 delete $options->{ "comment" };
119 print $data_out join( $options->{ "delimit" }, @vals ), "\n" if @vals;
121 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
127 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
132 $run_time_beg = Maasha::Biopieces::run_time();
134 Maasha::Biopieces::log_biopiece();
139 Maasha::Biopieces::close_stream( $in );
140 Maasha::Biopieces::close_stream( $out );
142 $run_time_end = Maasha::Biopieces::run_time();
144 Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
148 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<