From: martinahansen Date: Wed, 27 May 2009 05:02:25 +0000 (+0000) Subject: migrated merge_records X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=a70fbd0c55f590859ab1675dc948251d4b380d56;p=biopieces.git migrated merge_records git-svn-id: http://biopieces.googlecode.com/svn/trunk@427 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/bp_bin/merge_records b/bp_bin/merge_records index fdf5bd2..cdfe46e 100755 --- a/bp_bin/merge_records +++ b/bp_bin/merge_records @@ -1,6 +1,233 @@ -#!/usr/bin/env perl +#!/usr/bin/env perl -w + +# Copyright (C) 2007-2009 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 + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Merge records in the stream based on identifier values to specific keys. + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + -use warnings; use strict; +use Maasha::Biopieces; +use Maasha::Common; +use Maasha::Filesys; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, $tmp_dir, $file1, $file2, $fh1, $fh2, + $key1, $key2, @keys1, @keys2, @vals1, @vals2, $num1, $num2, $num, $cmp, $i ); + +$options = Maasha::Biopieces::parse_options( + [ + { long => 'keys', short => 'k', type => 'list', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef }, + { long => 'merge', short => 'm', type => 'string', mandatory => 'no', default => 'AandB', allowed => 'AandB,AorB,BorA,AnotB,BnotA', disallowed => undef }, + ] +); + +$in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } ); +$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } ); + +$tmp_dir = Maasha::Biopieces::get_tmpdir(); + +$file1 = "$tmp_dir/merge_records1.tmp"; +$file2 = "$tmp_dir/merge_records2.tmp"; + +$fh1 = Maasha::Filesys::file_write_open( $file1 ); +$fh2 = Maasha::Filesys::file_write_open( $file2 ); + +$key1 = $options->{ "keys" }->[ 0 ]; +$key2 = $options->{ "keys" }->[ 1 ]; + +$num = $key2 =~ s/n$//; +$num1 = 0; +$num2 = 0; + +while ( $record = Maasha::Biopieces::get_record( $in ) ) +{ + if ( exists $record->{ $key1 } ) + { + @keys1 = $key1; + @vals1 = $record->{ $key1 }; + + delete $record->{ $key1 }; + + map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record }; + + print $fh1 join( "\t", @vals1 ), "\n"; + + $num1++; + } + elsif ( exists $record->{ $key2 } ) + { + @keys2 = $key2; + @vals2 = $record->{ $key2 }; + + delete $record->{ $key2 }; + + map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record }; + + print $fh2 join( "\t", @vals2 ), "\n"; + + $num2++; + } +} + +close $fh1; +close $fh2; + +if ( $num ) +{ + Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1; + Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2; +} +else +{ + Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1; + Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2; +} + +$fh1 = Maasha::Filesys::file_read_open( $file1 ); +$fh2 = Maasha::Filesys::file_read_open( $file2 ); + +@vals1 = Maasha::Common::get_fields( $fh1 ); +@vals2 = Maasha::Common::get_fields( $fh2 ); + +while ( $num1 > 0 and $num2 > 0 ) +{ + undef $record; + + if ( $num ) { + $cmp = $vals1[ 0 ] <=> $vals2[ 0 ]; + } else { + $cmp = $vals1[ 0 ] cmp $vals2[ 0 ]; + } + + if ( $cmp < 0 ) + { + if ( $options->{ 'merge' } =~ /^(AorB|AnotB)$/ ) + { + for ( $i = 0; $i < @keys1; $i++ ) { + $record->{ $keys1[ $i ] } = $vals1[ $i ]; + } + + Maasha::Biopieces::put_record( $record, $out ); + } + + @vals1 = Maasha::Common::get_fields( $fh1 ); + $num1--; + } + elsif ( $cmp > 0 ) + { + if ( $options->{ 'merge' } =~ /^(BorA|BnotA)$/ ) + { + for ( $i = 0; $i < @keys2; $i++ ) { + $record->{ $keys2[ $i ] } = $vals2[ $i ]; + } + + Maasha::Biopieces::put_record( $record, $out ); + } + + @vals2 = Maasha::Common::get_fields( $fh2 ); + $num2--; + } + else + { + if ( $options->{ 'merge' } =~ /^(AandB|AorB|BorA)$/ ) + { + for ( $i = 0; $i < @keys1; $i++ ) { + $record->{ $keys1[ $i ] } = $vals1[ $i ]; + } + + for ( $i = 1; $i < @keys2; $i++ ) { + $record->{ $keys2[ $i ] } = $vals2[ $i ]; + } + + Maasha::Biopieces::put_record( $record, $out ); + } + + @vals1 = Maasha::Common::get_fields( $fh1 ); + @vals2 = Maasha::Common::get_fields( $fh2 ); + $num1--; + $num2--; + } +} + +close $fh1; +close $fh2; + +unlink $file1; +unlink $file2; + +if ( $num1 > 0 and $options->{ 'merge' } =~ /^(AorB|AnotB)$/ ) +{ + undef $record; + + for ( $i = 0; $i < @keys1; $i++ ) { + $record->{ $keys1[ $i ] } = $vals1[ $i ]; + } + + Maasha::Biopieces::put_record( $record, $out ); +} + +if ( $num2 > 0 and $options->{ 'merge' } =~ /^(BorA|BnotA)$/ ) +{ + undef $record; + + for ( $i = 0; $i < @keys2; $i++ ) { + $record->{ $keys2[ $i ] } = $vals2[ $i ]; + } + + Maasha::Biopieces::put_record( $record, $out ); +} + +Maasha::Filesys::dir_remove( $tmp_dir ); + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +BEGIN +{ + $run_time_beg = Maasha::Biopieces::run_time(); + + Maasha::Biopieces::log_biopiece(); +} + + +END +{ + Maasha::Biopieces::close_stream( $in ); + Maasha::Biopieces::close_stream( $out ); + + $run_time_end = Maasha::Biopieces::run_time(); + + Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options ); +} + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +__END__ -use Maasha::BioRun; diff --git a/code_perl/Maasha/BioRun.pm b/code_perl/Maasha/BioRun.pm index 147c590..73c7aa6 100644 --- a/code_perl/Maasha/BioRun.pm +++ b/code_perl/Maasha/BioRun.pm @@ -155,7 +155,6 @@ sub run_script elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) } elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) } elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) } - elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) } elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) } elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) } elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) } @@ -521,13 +520,6 @@ sub get_options delimit|d=s ); } - elsif ( $script eq "merge_records" ) - { - @options = qw( - keys|k=s - merge|m=s - ); - } elsif ( $script eq "sort_records" ) { @options = qw( @@ -2774,177 +2766,6 @@ sub script_merge_vals } -sub script_merge_records -{ - # Martin A. Hansen, July 2008. - - # Merges records in the stream based on identical values of two given keys. - - my ( $in, # handle to in stream - $out, # handle to out stream - $options, # options hash - ) = @_; - - # Returns nothing. - - my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2, - $num1, $num2, $num, $cmp, $i ); - - $merge = $options->{ "merge" } || "AandB"; - - $file1 = "$BP_TMP/merge_records1.tmp"; - $file2 = "$BP_TMP/merge_records2.tmp"; - - $fh1 = Maasha::Common::write_open( $file1 ); - $fh2 = Maasha::Common::write_open( $file2 ); - - $key1 = $options->{ "keys" }->[ 0 ]; - $key2 = $options->{ "keys" }->[ 1 ]; - - $num = $key2 =~ s/n$//; - $num1 = 0; - $num2 = 0; - - while ( $record = Maasha::Biopieces::get_record( $in ) ) - { - if ( exists $record->{ $key1 } ) - { - @keys1 = $key1; - @vals1 = $record->{ $key1 }; - - delete $record->{ $key1 }; - - map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record }; - - print $fh1 join( "\t", @vals1 ), "\n"; - - $num1++; - } - elsif ( exists $record->{ $key2 } ) - { - @keys2 = $key2; - @vals2 = $record->{ $key2 }; - - delete $record->{ $key2 }; - - map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record }; - - print $fh2 join( "\t", @vals2 ), "\n"; - - $num2++; - } - } - - close $fh1; - close $fh2; - - if ( $num ) - { - Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1; - Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2; - } - else - { - Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1; - Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2; - } - - $fh1 = Maasha::Common::read_open( $file1 ); - $fh2 = Maasha::Common::read_open( $file2 ); - - @vals1 = Maasha::Common::get_fields( $fh1 ); - @vals2 = Maasha::Common::get_fields( $fh2 ); - - while ( $num1 > 0 and $num2 > 0 ) - { - undef $record; - - if ( $num ) { - $cmp = $vals1[ 0 ] <=> $vals2[ 0 ]; - } else { - $cmp = $vals1[ 0 ] cmp $vals2[ 0 ]; - } - - if ( $cmp < 0 ) - { - if ( $merge =~ /^(AorB|AnotB)$/ ) - { - for ( $i = 0; $i < @keys1; $i++ ) { - $record->{ $keys1[ $i ] } = $vals1[ $i ]; - } - - Maasha::Biopieces::put_record( $record, $out ); - } - - @vals1 = Maasha::Common::get_fields( $fh1 ); - $num1--; - } - elsif ( $cmp > 0 ) - { - if ( $merge =~ /^(BorA|BnotA)$/ ) - { - for ( $i = 0; $i < @keys2; $i++ ) { - $record->{ $keys2[ $i ] } = $vals2[ $i ]; - } - - Maasha::Biopieces::put_record( $record, $out ); - } - - @vals2 = Maasha::Common::get_fields( $fh2 ); - $num2--; - } - else - { - if ( $merge =~ /^(AandB|AorB|BorA)$/ ) - { - for ( $i = 0; $i < @keys1; $i++ ) { - $record->{ $keys1[ $i ] } = $vals1[ $i ]; - } - - for ( $i = 1; $i < @keys2; $i++ ) { - $record->{ $keys2[ $i ] } = $vals2[ $i ]; - } - - Maasha::Biopieces::put_record( $record, $out ); - } - - @vals1 = Maasha::Common::get_fields( $fh1 ); - @vals2 = Maasha::Common::get_fields( $fh2 ); - $num1--; - $num2--; - } - } - - close $fh1; - close $fh2; - - unlink $file1; - unlink $file2; - - if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ ) - { - undef $record; - - for ( $i = 0; $i < @keys1; $i++ ) { - $record->{ $keys1[ $i ] } = $vals1[ $i ]; - } - - Maasha::Biopieces::put_record( $record, $out ); - } - - if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ ) - { - undef $record; - - for ( $i = 0; $i < @keys2; $i++ ) { - $record->{ $keys2[ $i ] } = $vals2[ $i ]; - } - - Maasha::Biopieces::put_record( $record, $out ); - } -} - - sub script_sort_records { # Martin A. Hansen, August 2007. diff --git a/code_perl/Maasha/Common.pm b/code_perl/Maasha/Common.pm index 847ff5d..75f89cd 100644 --- a/code_perl/Maasha/Common.pm +++ b/code_perl/Maasha/Common.pm @@ -551,9 +551,9 @@ sub get_fields $line = <$fh>; - chomp $line; + return if not defined $line; - return if not $line; + chomp $line; $delimiter ||= "\t";