From: martinahansen Date: Fri, 15 Oct 2010 13:46:32 +0000 (+0000) Subject: polished sum_vals and added tests X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=bff4b6c20f06e95aca9968b7fa3fb43e6ec4dc6a;p=biopieces.git polished sum_vals and added tests git-svn-id: http://biopieces.googlecode.com/svn/trunk@1140 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/bp_bin/sum_vals b/bp_bin/sum_vals index b672de3..e20f178 100755 --- a/bp_bin/sum_vals +++ b/bp_bin/sum_vals @@ -29,21 +29,26 @@ use warnings; use strict; use Maasha::Biopieces; +use Maasha::Calc; # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< -my ( $options, $in, $out, $record, $key, %sum_hash, $fh ); +my ( $options, $in, $out, $record, $new_record, $key, %sum_hash, $fh ); $options = Maasha::Biopieces::parse_options( [ - { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, - { long => 'data_out', short => 'o', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, - { long => 'keys', short => 'k', type => 'list', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef }, + { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'data_out', short => 'o', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'keys', short => 'k', type => 'list', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'list', short => 'l', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, ] ); +Maasha::Common::error( qq(both --keys and --list specified) ) if $options->{ "keys" } and $options->{ "list" }; +Maasha::Common::error( qq(no --keys or --list specified) ) if not $options->{ "keys" } and not $options->{ "list" }; + $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } ); $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } ); @@ -56,19 +61,30 @@ while ( $record = Maasha::Biopieces::get_record( $in ) ) } } + if ( $options->{ 'list' } and $record->{ $options->{ 'list' } } ) { + $record->{ $options->{ 'list' } . "_SUM" } = sprintf( "%.2f", Maasha::Calc::sum( [ split ";", $record->{ $options->{ 'list' } } ] ) ); + } + Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" }; } +Maasha::Biopieces::close_stream( $in ); +Maasha::Biopieces::close_stream( $out ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); foreach $key ( @{ $options->{ "keys" } } ) { - Maasha::Biopieces::put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh ); + $new_record->{ $key . "_SUM" } = $sum_hash{ $key } || 0; } -close $fh; +if ( $options->{ "keys" } and $new_record ) +{ + $new_record->{ 'REC_TYPE' } = "SUM"; -Maasha::Biopieces::close_stream( $in ); -Maasha::Biopieces::close_stream( $out ); + Maasha::Biopieces::put_record( $new_record, $fh ); +} + +close $fh; # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< diff --git a/bp_test/in/sum_vals.in b/bp_test/in/sum_vals.in new file mode 100644 index 0000000..401f694 --- /dev/null +++ b/bp_test/in/sum_vals.in @@ -0,0 +1,15 @@ +V3: 0;1;2;3;4;5 +V0: Human +V2: 78 +V1: 123 +--- +V3: 6;7;8 +V0: Dog +V2: 81 +V1: 45 +--- +V3: 9 +V0: Mouse +V2: 5 +V1: 6 +--- diff --git a/bp_test/out/sum_vals.out.1 b/bp_test/out/sum_vals.out.1 new file mode 100644 index 0000000..0cef976 --- /dev/null +++ b/bp_test/out/sum_vals.out.1 @@ -0,0 +1,4 @@ +V1_SUM: 174 +REC_TYPE: SUM +V2_SUM: 164 +--- diff --git a/bp_test/out/sum_vals.out.2 b/bp_test/out/sum_vals.out.2 new file mode 100644 index 0000000..8b23650 --- /dev/null +++ b/bp_test/out/sum_vals.out.2 @@ -0,0 +1,18 @@ +V3_SUM: 15.00 +V3: 0;1;2;3;4;5 +V0: Human +V2: 78 +V1: 123 +--- +V3_SUM: 21.00 +V3: 6;7;8 +V0: Dog +V2: 81 +V1: 45 +--- +V3_SUM: 9.00 +V3: 9 +V0: Mouse +V2: 5 +V1: 6 +--- diff --git a/bp_test/test/test_sum_vals b/bp_test/test/test_sum_vals new file mode 100755 index 0000000..5fabaa4 --- /dev/null +++ b/bp_test/test/test_sum_vals @@ -0,0 +1,11 @@ +#!/bin/bash + +source "$BP_DIR/bp_test/lib/test.sh" + +run "$bp -I $in -k V1,V2 -o $tmp -x" +assert_no_diff $tmp $out.1 +clean + +run "$bp -I $in -l V3 -O $tmp" +assert_no_diff $tmp $out.2 +clean