]> git.donarmstrong.com Git - biopieces.git/commitdiff
migrated merge_records
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 27 May 2009 05:02:25 +0000 (05:02 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 27 May 2009 05:02:25 +0000 (05:02 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@427 74ccb610-7750-0410-82ae-013aeee3265d

bp_bin/merge_records
code_perl/Maasha/BioRun.pm
code_perl/Maasha/Common.pm

index fdf5bd287f78613d2aea83bb9b15f855cad57e77..cdfe46e88ce6c02f87695fb6f5d11e2c2b06785d 100755 (executable)
@@ -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;
index 147c5901e139ef9a9572ec815a58abda934c0624..73c7aa699653418e3c48f1b514f7439d74baccc7 100644 (file)
@@ -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.
index 847ff5d3337a8ddd053a3924e7ed89e52e257a5c..75f89cd0b209e078da2880c7ec8e3d4486879ab6 100644 (file)
@@ -551,9 +551,9 @@ sub get_fields
 
     $line = <$fh>;
 
-    chomp $line;
+    return if not defined $line;
 
-    return if not $line;
+    chomp $line;
 
     $delimiter ||= "\t";