X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bp_bin%2Fanalyze_vals;h=e7e8249a8faf73c53bce080d333e56a73a2b7aa1;hb=9e506c77bf020025b9788beac9ece546e0dd93ee;hp=4cd1d4413a253b7338666f848168a0f9929fba03;hpb=e1e61f6b2e3f51a98c0be95573d6955dc921a697;p=biopieces.git diff --git a/bp_bin/analyze_vals b/bp_bin/analyze_vals index 4cd1d44..e7e8249 100755 --- a/bp_bin/analyze_vals +++ b/bp_bin/analyze_vals @@ -1,6 +1,115 @@ -#!/usr/bin/env perl +#!/usr/bin/env ruby -use warnings; -use strict; +# Copyright (C) 2007-2013 Martin A. Hansen. -use Maasha::Biopieces; +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# http://www.gnu.org/copyleft/gpl.html + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# This program is part of the Biopieces framework (www.biopieces.org). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Determine basic stats for records in the stream. + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +require 'maasha/biopieces' + +casts = [] +casts << {long: 'keys', short: 'k', type: 'list', mandatory: false, default: nil, allowed: nil, disallowed: nil} +casts << {long: 'no_keys', short: 'K', type: 'list', mandatory: false, default: nil, allowed: nil, disallowed: nil} + +options = Biopieces.options_parse(ARGV, casts) + +stats = Hash.new { |h, k| h[k] = {} } + +def to_number(value) + begin num = Integer(value) + return num + rescue + begin num = Float(value) + return num + rescue + end + end +end + +def min(value1, value2) + return value2 unless value1 + [value1, value2].min +end + +def max(value1, value2) + return value2 unless value1 + [value1, value2].max +end + +keys = options[:keys].map { |k| k.to_sym } if options[:keys] +no_keys = options[:no_keys].each_with_object({}) { |i, h| h[i.to_sym] = true } if options[:no_keys] +get_keys = nil + +Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| + input.each do |record| + unless get_keys + get_keys = keys || record.keys + get_keys.reject! { |k| no_keys[k] } if no_keys + end + + get_keys.each do |key| + key = key.to_sym + value = record[key] + + stats[key][:sum] ||= 0 + stats[key][:count] ||= 0 + + if num = to_number(value) + stats[key][:min] = min(stats[key][:min], num) + stats[key][:max] = max(stats[key][:max], num) + stats[key][:type] = :numeric + stats[key][:sum] += num + else + stats[key][:min] = min(stats[key][:min], value.length) + stats[key][:max] = max(stats[key][:max], value.length) + stats[key][:type] = :alphabetic + stats[key][:sum] += value.length + end + + stats[key][:count] += 1 + end + end + + stats.each do |key, value| + stat_record = { + :KEY => key, + :TYPE => value[:type].to_s.capitalize, + :COUNT => value[:count], + :MIN => value[:min].is_a?(Float) ? "%0.2f" % value[:min] : value[:min], + :MAX => value[:max].is_a?(Float) ? "%0.2f" % value[:max] : value[:max], + :SUM => value[:sum].is_a?(Float) ? "%0.2f" % value[:sum] : value[:sum], + :MEAN => "%0.2f" % (value[:sum] / value[:count].to_f) + } + + output.puts stat_record + end +end + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +__END__