]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/analyze_vals
726c91c53c62f5c22560b77260f45d094340b018
[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, %skip_hash, $data_out,
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 => 'data_out',  short => 'o', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
46         { long => 'keys',      short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
47         { long => 'no_keys',   short => 'K', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
48     ]   
49 );
50
51 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
52 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
53
54 $data_out = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } );
55 $data_out ||= \*STDOUT;
56
57 map { $skip_hash{ $_ } = 1 } @{ $options->{ "no_keys" } };
58 map { $key_hash{ $_ } = 1; $skip = 1 } @{ $options->{ "keys" } };
59
60 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
61 {
62     foreach $key ( keys %{ $record } )
63     {
64         next if $skip and not exists $key_hash{ $key };
65         next if $skip_hash{ $key };
66
67         if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
68         {
69             if ( not exists $analysis->{ $key } )
70             {
71                 $analysis->{ $key }->{ "MIN" } = $record->{ $key };
72                 $analysis->{ $key }->{ "MAX" } = $record->{ $key };
73             }
74
75             $analysis->{ $key }->{ "MAX" } = Maasha::Calc::max( $analysis->{ $key }->{ "MAX" }, $record->{ $key } );
76             $analysis->{ $key }->{ "MIN" } = Maasha::Calc::min( $analysis->{ $key }->{ "MIN" }, $record->{ $key } );
77
78             $analysis->{ $key }->{ "TYPE" } = "num";
79             $analysis->{ $key }->{ "SUM" } += $record->{ $key };
80         }
81         else
82         {
83             $len = length $record->{ $key };
84
85             if ( not exists $analysis->{ $key } )
86             {
87                 $analysis->{ $key }->{ "MIN" } = $len;
88                 $analysis->{ $key }->{ "MAX" } = $len;
89             }
90
91             $analysis->{ $key }->{ "MAX" } = Maasha::Calc::max( $analysis->{ $key }->{ "MAX" }, $len );
92             $analysis->{ $key }->{ "MIN" } = Maasha::Calc::min( $analysis->{ $key }->{ "MIN" }, $len );
93
94             $analysis->{ $key }->{ "TYPE" } = "alph";
95             $analysis->{ $key }->{ "SUM" } += $len;
96         }
97
98         $analysis->{ $key }->{ "COUNT" }++;
99     }
100
101     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
102 }
103
104 foreach $key ( keys %{ $analysis } )
105 {
106     $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
107     $analysis->{ $key }->{ "SUM" }  = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
108 }
109
110 $keys   = "KEY  ";
111 $types  = "TYPE ";
112 $counts = "COUNT";
113 $mins   = "MIN  ";
114 $maxs   = "MAX  ";
115 $sums   = "SUM  ";
116 $means  = "MEAN ";
117
118 foreach $key ( sort keys %{ $analysis } )
119 {
120     $keys   .= sprintf "% 15s", $key;
121     $types  .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
122     $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
123     $mins   .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
124     $maxs   .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
125     $sums   .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
126     $means  .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
127 }
128
129 print $data_out "$keys\n";
130 print $data_out "$types\n";
131 print $data_out "$counts\n";
132 print $data_out "$mins\n";
133 print $data_out "$maxs\n";
134 print $data_out "$sums\n";
135 print $data_out "$means\n";
136
137 close $data_out;
138
139 Maasha::Biopieces::close_stream( $in );
140 Maasha::Biopieces::close_stream( $out );
141
142
143 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
144
145
146 BEGIN
147 {
148     Maasha::Biopieces::status_set();
149 }
150
151
152 END
153 {
154     Maasha::Biopieces::status_log();
155 }
156
157
158 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
159
160
161 __END__