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