X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bp_bin%2Fanalyze_vals;h=40dd0e0239737e08b16eb75d815f6445a8ed7a72;hb=bfab657c8174eb352504848abb4395bb3c065f7f;hp=fdf5bd287f78613d2aea83bb9b15f855cad57e77;hpb=c32b13419ca8e524a5c98869048d0be670ee76d0;p=biopieces.git diff --git a/bp_bin/analyze_vals b/bp_bin/analyze_vals index fdf5bd2..40dd0e0 100755 --- a/bp_bin/analyze_vals +++ b/bp_bin/analyze_vals @@ -1,6 +1,129 @@ -#!/usr/bin/env perl +#!/usr/bin/env ruby -use warnings; -use strict; +# Copyright (C) 2007-2013 Martin A. Hansen. -use Maasha::BioRun; +# 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} +casts << {long: 'no_stream', short: 'x', type: 'flag', mandatory: false, default: nil, allowed: nil, disallowed: nil} +casts << {long: 'data_out', short: 'o', type: 'file', 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] + + if value + 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 + + output.puts record unless options[:no_stream] + end + + if options[:data_out] + data_out = File.open(options[:data_out], 'w') + 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) + } + + if options[:data_out] + data_out.puts stat_record + else + output.puts stat_record + end + end +end + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +__END__