]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/write_tab
rewrite begin
[biopieces.git] / bp_bin / write_tab
1 #!/usr/bin/env perl -w
2
3 # Copyright (C) 2007-2009 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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Write tabular output from the stream.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use strict;
30 use Maasha::Fasta;
31 use Maasha::Biopieces;
32
33
34 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
35
36
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 );
38
39 $options = Maasha::Biopieces::parse_options(
40     [
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 },
48     ]   
49 );
50
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" } );
54
55 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
56
57 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
58 {
59     undef @vals;
60     $ok = 1;
61     
62     if ( $options->{ "keys" } )
63     {
64         map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
65
66         if ( $ok )
67         {
68             foreach $key ( @{ $options->{ "keys" }  } )
69             {
70                 if ( exists $record->{ $key } )
71                 {
72                     push @keys, $key if $options->{ "comment" };
73                     push @vals, $record->{ $key };
74                 }
75             }
76          }
77     }
78     else
79     {
80         if ( not $check_sort )
81         {
82             $sort_keys = 1;
83
84             map { $sort_keys = 0 if $_ !~ /^V(\d+)$/ } keys %{ $record };
85
86             $check_sort = 1;
87         }
88
89         if ( $sort_keys )
90         {
91             foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
92             {
93                 next if exists $no_keys{ $key };
94
95                 push @keys, $key if $options->{ "comment" };
96                 push @vals, $record->{ $key };
97             }
98         }
99         else
100         {
101             foreach $key ( keys %{ $record } )
102             {
103                 next if exists $no_keys{ $key };
104
105                 push @keys, $key if $options->{ "comment" };
106                 push @vals, $record->{ $key };
107             }
108         
109         }
110     }
111
112     if ( @keys and $options->{ "comment" } )
113     {
114         print $data_out "#", join( $options->{ "delimit" }, @keys ), "\n";
115
116         delete $options->{ "comment" };
117     }
118
119     print $data_out join( $options->{ "delimit" }, @vals ), "\n" if @vals;
120
121     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
122 }
123
124 close $data_out;
125
126
127 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
128
129
130 BEGIN
131 {
132     $run_time_beg = Maasha::Biopieces::run_time();
133
134     Maasha::Biopieces::log_biopiece();
135 }
136
137 END
138 {
139     Maasha::Biopieces::close_stream( $in );
140     Maasha::Biopieces::close_stream( $out );
141
142     $run_time_end = Maasha::Biopieces::run_time();
143
144     Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
145 }
146
147
148 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
149
150
151 __END__