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