]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/analyze_vals
added use strict to all biopieces
[biopieces.git] / bp_bin / analyze_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 # Determine type, count, min, max, sum and mean for values in stream.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Biopieces;
32 use Maasha::Calc;
33 use Data::Dumper;
34
35
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37
38
39 my ( $options, $in, $out, $record, $analysis, $key, $len,
40      %key_hash, $skip, $keys, $types, $counts, $mins, $maxs, $sums, $means );
41
42 $options = Maasha::Biopieces::parse_options(
43     [
44         { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
45         { long => 'keys',      short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
46     ]   
47 );
48
49 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
50 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
51
52 map { $key_hash{ $_ } = 1; $skip = 1 } @{ $options->{ "keys" } };
53
54 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
55 {
56     foreach $key ( keys %{ $record } )
57     {
58         next if $skip and not exists $key_hash{ $key };
59
60         if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
61         {
62             if ( not exists $analysis->{ $key } )
63             {
64                 $analysis->{ $key }->{ "MIN" } = $record->{ $key };
65                 $analysis->{ $key }->{ "MAX" } = $record->{ $key };
66             }
67
68             $analysis->{ $key }->{ "MAX" } = Maasha::Calc::max( $analysis->{ $key }->{ "MAX" }, $record->{ $key } );
69             $analysis->{ $key }->{ "MIN" } = Maasha::Calc::min( $analysis->{ $key }->{ "MIN" }, $record->{ $key } );
70
71             $analysis->{ $key }->{ "TYPE" } = "num";
72             $analysis->{ $key }->{ "SUM" } += $record->{ $key };
73         }
74         else
75         {
76             $len = length $record->{ $key };
77
78             if ( not exists $analysis->{ $key } )
79             {
80                 $analysis->{ $key }->{ "MIN" } = $len;
81                 $analysis->{ $key }->{ "MAX" } = $len;
82             }
83
84             $analysis->{ $key }->{ "MAX" } = Maasha::Calc::max( $analysis->{ $key }->{ "MAX" }, $len );
85             $analysis->{ $key }->{ "MIN" } = Maasha::Calc::min( $analysis->{ $key }->{ "MIN" }, $len );
86
87             $analysis->{ $key }->{ "TYPE" } = "alph";
88             $analysis->{ $key }->{ "SUM" } += $len;
89         }
90
91         $analysis->{ $key }->{ "COUNT" }++;
92     }
93
94     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
95 }
96
97 foreach $key ( keys %{ $analysis } )
98 {
99     $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
100     $analysis->{ $key }->{ "SUM" }  = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
101 }
102
103 $keys   = "KEY  ";
104 $types  = "TYPE ";
105 $counts = "COUNT";
106 $mins   = "MIN  ";
107 $maxs   = "MAX  ";
108 $sums   = "SUM  ";
109 $means  = "MEAN ";
110
111 foreach $key ( keys %{ $analysis } )
112 {
113     $keys   .= sprintf "% 15s", $key;
114     $types  .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
115     $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
116     $mins   .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
117     $maxs   .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
118     $sums   .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
119     $means  .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
120 }
121
122 print "$keys\n";
123 print "$types\n";
124 print "$counts\n";
125 print "$mins\n";
126 print "$maxs\n";
127 print "$sums\n";
128 print "$means\n";
129
130 Maasha::Biopieces::close_stream( $in );
131 Maasha::Biopieces::close_stream( $out );
132
133
134 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
135
136
137 BEGIN
138 {
139     Maasha::Biopieces::status_set();
140 }
141
142
143 END
144 {
145     Maasha::Biopieces::status_log();
146 }
147
148
149 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
150
151
152 __END__