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