]> git.donarmstrong.com Git - biopieces.git/commitdiff
polished sum_vals and added tests
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Fri, 15 Oct 2010 13:46:32 +0000 (13:46 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Fri, 15 Oct 2010 13:46:32 +0000 (13:46 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1140 74ccb610-7750-0410-82ae-013aeee3265d

bp_bin/sum_vals
bp_test/in/sum_vals.in [new file with mode: 0644]
bp_test/out/sum_vals.out.1 [new file with mode: 0644]
bp_test/out/sum_vals.out.2 [new file with mode: 0644]
bp_test/test/test_sum_vals [new file with mode: 0755]

index b672de388c88b63dee2901f477fb66acffd49e1f..e20f178d504f4c65cf772a4283ea7a16b83978c8 100755 (executable)
 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 (file)
index 0000000..401f694
--- /dev/null
@@ -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 (file)
index 0000000..0cef976
--- /dev/null
@@ -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 (file)
index 0000000..8b23650
--- /dev/null
@@ -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 (executable)
index 0000000..5fabaa4
--- /dev/null
@@ -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