X-Git-Url: https://git.donarmstrong.com/?p=biopieces.git;a=blobdiff_plain;f=bp_bin%2Fanalyze_vals;h=e7e8249a8faf73c53bce080d333e56a73a2b7aa1;hp=c25d2e8b9dd393bca05f4b4152ed693895250637;hb=9e506c77bf020025b9788beac9ece546e0dd93ee;hpb=f7e5c1b54c562cc1b25fe419cacd4da9ac01f129 diff --git a/bp_bin/analyze_vals b/bp_bin/analyze_vals deleted file mode 120000 index c25d2e8..0000000 --- a/bp_bin/analyze_vals +++ /dev/null @@ -1 +0,0 @@ -../code_perl/Maasha/bin/analyze_vals \ No newline at end of file diff --git a/bp_bin/analyze_vals b/bp_bin/analyze_vals new file mode 100755 index 0000000..e7e8249 --- /dev/null +++ b/bp_bin/analyze_vals @@ -0,0 +1,115 @@ +#!/usr/bin/env ruby + +# Copyright (C) 2007-2013 Martin A. Hansen. + +# 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__