]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/count_vals
fixed seq qual length check
[biopieces.git] / bp_bin / count_vals
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 # Count the number of times values of given keys exists in stream.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Filesys;
32 use Maasha::Biopieces;
33
34
35 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
36
37
38 my ( $options, $in, $out, $num, $record, %count_hash, @records, $tmp_dir, $tmp_file, $fh_out, $fh_in, $cache );
39
40 $options = Maasha::Biopieces::parse_options(
41     [
42         { long => 'keys', short => 'k', type => 'list', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
43     ]   
44 );
45
46 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
47 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
48
49 $tmp_dir  = Maasha::Biopieces::get_tmpdir();
50
51 $tmp_file = "$tmp_dir/count_cache.tmp";
52
53 $fh_out   = Maasha::Filesys::file_write_open( $tmp_file );
54
55 $cache    = 0;
56 $num      = 0;
57
58 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
59 {
60     map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
61
62     push @records, $record;
63
64     if ( scalar @records > 5_000_000 )   # too many records to hold in memory - use disk cache
65     {
66         map { Maasha::Biopieces::put_record( $_, $fh_out ) } @records;
67
68         undef @records;
69
70         $cache = 1;
71     }
72
73     print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
74
75     $num++;
76 }
77
78 close $fh_out;
79
80 if ( $cache )
81 {
82     $num   = 0;
83
84     $fh_in = Maasha::Common::read_open( $tmp_file );
85
86     while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
87     {
88         map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
89
90         Maasha::Biopieces::put_record( $record, $out );
91
92         print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
93
94         $num++;
95     }
96
97     close $fh_in;
98 }
99
100 foreach $record ( @records )
101 {
102     map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
103
104     Maasha::Biopieces::put_record( $record, $out );
105 }
106
107 unlink $tmp_file;
108
109 Maasha::Biopieces::close_stream( $in );
110 Maasha::Biopieces::close_stream( $out );
111
112
113 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
114
115
116 BEGIN
117 {
118     Maasha::Biopieces::status_set();
119 }
120
121
122 END
123 {
124     Maasha::Biopieces::status_log();
125 }
126
127
128 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
129
130
131 __END__