]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/analyze_vals
951f321f808e7b4e907d11d791e29c37cb1d64c2
[biopieces.git] / bp_bin / analyze_vals
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2013 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
23 # This program is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 # Determine basic stats for records in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32
33 casts = []
34 casts << {long: 'keys',      short: 'k', type: 'list', mandatory: false, default: nil, allowed: nil, disallowed: nil}
35 casts << {long: 'no_keys',   short: 'K', type: 'list', mandatory: false, default: nil, allowed: nil, disallowed: nil}
36 casts << {long: 'no_stream', short: 'x', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil}
37 casts << {long: 'data_out',  short: 'o', type: 'file', mandatory: false, default: nil, allowed: nil, disallowed: nil}
38
39 options = Biopieces.options_parse(ARGV, casts)
40
41 stats = Hash.new { |h, k| h[k] = {} }
42
43 def to_number(value)
44   begin num = Integer(value)
45     return num
46   rescue
47     begin num = Float(value)
48       return num
49     rescue
50     end
51   end
52 end
53
54 def min(value1, value2)
55   return value2 unless value1
56   [value1, value2].min
57 end
58
59 def max(value1, value2)
60   return value2 unless value1
61   [value1, value2].max
62 end
63
64 keys    = options[:keys].map { |k| k.to_sym }                                  if options[:keys]
65 no_keys = options[:no_keys].each_with_object({}) { |i, h| h[i.to_sym] = true } if options[:no_keys]
66 get_keys = nil
67
68 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
69   input.each do |record|
70     unless get_keys
71       get_keys = keys || record.keys
72       get_keys.reject! { |k| no_keys[k] } if no_keys
73     end
74
75     get_keys.each do |key|
76       key = key.to_sym
77       value = record[key]
78
79       stats[key][:sum]   ||= 0
80       stats[key][:count] ||= 0
81
82       if num = to_number(value)
83         stats[key][:min]   = min(stats[key][:min], num)
84         stats[key][:max]   = max(stats[key][:max], num)
85         stats[key][:type]  = :numeric
86         stats[key][:sum]  += num
87       else
88         stats[key][:min]   = min(stats[key][:min], value.length)
89         stats[key][:max]   = max(stats[key][:max], value.length)
90         stats[key][:type]  = :alphabetic
91         stats[key][:sum]  += value.length
92       end
93
94       stats[key][:count] += 1
95     end
96
97     output.puts record unless options[:no_stream]
98   end
99
100   if options[:data_out]
101     data_out = File.open(options[:data_out], 'w')
102   end
103
104   stats.each do |key, value|
105     stat_record = {
106       :KEY   => key,
107       :TYPE  => value[:type].to_s.capitalize,
108       :COUNT => value[:count],
109       :MIN   => value[:min].is_a?(Float) ? "%0.2f" % value[:min] : value[:min],
110       :MAX   => value[:max].is_a?(Float) ? "%0.2f" % value[:max] : value[:max],
111       :SUM   => value[:sum].is_a?(Float) ? "%0.2f" % value[:sum] : value[:sum],
112       :MEAN  => "%0.2f" % (value[:sum] / value[:count].to_f)
113     }
114
115     if options[:data_out]
116       data_out.puts stat_record
117     else
118       output.puts stat_record
119     end
120   end
121 end
122
123
124 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
125
126
127 __END__