3 # Copyright (C) 2007-2009 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Determine type, count, min, max, sum and mean for values in stream.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 use Maasha::Biopieces;
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39 my ( $options, $in, $out, $record, $analysis, $key, $len, %skip_hash,
40 %key_hash, $skip, $keys, $types, $counts, $mins, $maxs, $sums, $means );
42 $options = Maasha::Biopieces::parse_options(
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 { long => 'no_keys', short => 'K', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
50 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
51 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
53 map { $skip_hash{ $_ } = 1 } @{ $options->{ "no_keys" } };
54 map { $key_hash{ $_ } = 1; $skip = 1 } @{ $options->{ "keys" } };
56 while ( $record = Maasha::Biopieces::get_record( $in ) )
58 foreach $key ( keys %{ $record } )
60 next if $skip and not exists $key_hash{ $key };
61 next if $skip_hash{ $key };
63 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
65 if ( not exists $analysis->{ $key } )
67 $analysis->{ $key }->{ "MIN" } = $record->{ $key };
68 $analysis->{ $key }->{ "MAX" } = $record->{ $key };
71 $analysis->{ $key }->{ "MAX" } = Maasha::Calc::max( $analysis->{ $key }->{ "MAX" }, $record->{ $key } );
72 $analysis->{ $key }->{ "MIN" } = Maasha::Calc::min( $analysis->{ $key }->{ "MIN" }, $record->{ $key } );
74 $analysis->{ $key }->{ "TYPE" } = "num";
75 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
79 $len = length $record->{ $key };
81 if ( not exists $analysis->{ $key } )
83 $analysis->{ $key }->{ "MIN" } = $len;
84 $analysis->{ $key }->{ "MAX" } = $len;
87 $analysis->{ $key }->{ "MAX" } = Maasha::Calc::max( $analysis->{ $key }->{ "MAX" }, $len );
88 $analysis->{ $key }->{ "MIN" } = Maasha::Calc::min( $analysis->{ $key }->{ "MIN" }, $len );
90 $analysis->{ $key }->{ "TYPE" } = "alph";
91 $analysis->{ $key }->{ "SUM" } += $len;
94 $analysis->{ $key }->{ "COUNT" }++;
97 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
100 foreach $key ( keys %{ $analysis } )
102 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
103 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
114 foreach $key ( sort keys %{ $analysis } )
116 $keys .= sprintf "% 15s", $key;
117 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
118 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
119 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
120 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
121 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
122 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
133 Maasha::Biopieces::close_stream( $in );
134 Maasha::Biopieces::close_stream( $out );
137 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
142 Maasha::Biopieces::status_set();
148 Maasha::Biopieces::status_log();
152 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<