]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/read_tab
fixed bug in read_tab
[biopieces.git] / bp_bin / read_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 # Read tabular data.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use strict;
30 use Maasha::Filesys;
31 use Maasha::Fasta;
32 use Maasha::Biopieces;
33
34
35 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
36
37
38 my ( $options, $in, $out, $record, $data_in, $num, $skip, $line, @fields, @fields2, $i );
39
40 $options = Maasha::Biopieces::parse_options(
41     [
42         { long => 'data_in', short => 'i', type => 'files!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
43         { long => 'delimit', short => 'd', type => 'string', mandatory => 'no', default => '\s+', allowed => undef, disallowed => undef },
44         { long => 'cols',    short => 'c', type => 'list',   mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
45         { long => 'keys',    short => 'k', type => 'list',   mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
46         { long => 'skip',    short => 's', type => 'uint',   mandatory => 'no', default => '0',   allowed => undef, disallowed => undef },
47         { long => 'num',     short => 'n', type => 'uint',   mandatory => 'no', default => undef, allowed => undef, disallowed => '0' },
48     ]   
49 );
50
51 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
52 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
53
54 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
55     Maasha::Biopieces::put_record( $record, $out );
56 }
57
58 if ( $options->{ 'data_in' } )
59 {
60     $data_in = Maasha::Filesys::files_read_open( $options->{ 'data_in' } );
61
62     $num  = 1;
63     $skip = $options->{ 'skip' };
64
65     while ( $line = <$data_in> ) 
66     {
67         if ( $skip )
68         {
69             $skip--;
70             next;
71         }
72
73         next if $line =~ /^#|^$/;
74
75         chomp $line;
76
77         undef $record;
78         undef @fields2;
79
80         @fields = split /$options->{'delimit'}/, $line;
81
82         if ( $options->{ "cols" } ) {
83             map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
84         } else {
85             @fields2 = @fields;
86         }
87
88         for ( $i = 0; $i < @fields2; $i++ )
89         {
90             if ( $options->{ "keys" }->[ $i ] ) {
91                 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
92             } else {
93                 $record->{ "V" . $i } = $fields2[ $i ];
94             }
95         }
96
97         Maasha::Biopieces::put_record( $record, $out );
98
99         last if $options->{ "num" } and $num == $options->{ "num" };
100
101         $num++;
102     }
103
104     close $data_in;
105 }
106
107 Maasha::Biopieces::close_stream( $in );
108 Maasha::Biopieces::close_stream( $out );
109
110
111 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
112
113
114 BEGIN
115 {
116     Maasha::Biopieces::status_set();
117 }
118
119
120 END
121 {
122     Maasha::Biopieces::status_log();
123 }
124
125
126 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
127
128
129 __END__