3 # Copyright (C) 2007-2013 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
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Determine basic stats for records in the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 require 'maasha/biopieces'
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}
39 options = Biopieces.options_parse(ARGV, casts)
41 stats = Hash.new { |h, k| h[k] = {} }
44 begin num = Integer(value)
47 begin num = Float(value)
54 def min(value1, value2)
55 return value2 unless value1
59 def max(value1, value2)
60 return value2 unless value1
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]
68 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
69 input.each do |record|
71 get_keys = keys || record.keys
72 get_keys.reject! { |k| no_keys[k] } if no_keys
75 get_keys.each do |key|
80 stats[key][:sum] ||= 0
81 stats[key][:count] ||= 0
83 if num = to_number(value)
84 stats[key][:min] = min(stats[key][:min], num)
85 stats[key][:max] = max(stats[key][:max], num)
86 stats[key][:type] = :numeric
87 stats[key][:sum] += num
89 stats[key][:min] = min(stats[key][:min], value.length)
90 stats[key][:max] = max(stats[key][:max], value.length)
91 stats[key][:type] = :alphabetic
92 stats[key][:sum] += value.length
95 stats[key][:count] += 1
99 output.puts record unless options[:no_stream]
102 if options[:data_out]
103 data_out = File.open(options[:data_out], 'w')
106 stats.each do |key, value|
109 :TYPE => value[:type].to_s.capitalize,
110 :COUNT => value[:count],
111 :MIN => value[:min].is_a?(Float) ? "%0.2f" % value[:min] : value[:min],
112 :MAX => value[:max].is_a?(Float) ? "%0.2f" % value[:max] : value[:max],
113 :SUM => value[:sum].is_a?(Float) ? "%0.2f" % value[:sum] : value[:sum],
114 :MEAN => "%0.2f" % (value[:sum] / value[:count].to_f)
117 if options[:data_out]
118 data_out.puts stat_record
120 output.puts stat_record
126 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<