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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Maasha::Biopieces;
35 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 my ( $options, $in, $out, $record, $data_out, @vals, $ok, $key, @keys, $A, $B, %no_keys, $sort_keys, $check_sort );
40 $options = Maasha::Biopieces::parse_options(
42 { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
43 { long => 'data_out', short => 'o', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
44 { long => 'comment', short => 'c', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
45 { long => 'delimit', short => 'd', type => 'string', mandatory => 'no', default => "\t", allowed => undef, disallowed => undef },
46 { long => 'keys', short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
47 { long => 'no_keys', short => 'K', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
48 { long => 'compress', short => 'Z', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
52 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
53 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
54 $data_out = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } );
56 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
58 while ( $record = Maasha::Biopieces::get_record( $in ) )
63 if ( $options->{ "keys" } )
65 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
69 foreach $key ( @{ $options->{ "keys" } } )
71 if ( exists $record->{ $key } )
73 push @keys, $key if $options->{ "comment" };
74 push @vals, $record->{ $key };
81 if ( not $check_sort )
85 map { $sort_keys = 0 if $_ !~ /^V(\d+)$/ } keys %{ $record };
92 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
94 next if exists $no_keys{ $key };
96 push @keys, $key if $options->{ "comment" };
97 push @vals, $record->{ $key };
102 foreach $key ( keys %{ $record } )
104 next if exists $no_keys{ $key };
106 push @keys, $key if $options->{ "comment" };
107 push @vals, $record->{ $key };
113 if ( @keys and $options->{ "comment" } )
115 print $data_out "#", join( $options->{ "delimit" }, @keys ), "\n";
117 delete $options->{ "comment" };
120 print $data_out join( $options->{ "delimit" }, @vals ), "\n" if @vals;
122 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
127 Maasha::Biopieces::close_stream( $in );
128 Maasha::Biopieces::close_stream( $out );
131 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
136 Maasha::Biopieces::status_set();
142 Maasha::Biopieces::status_log();
146 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<