]> git.donarmstrong.com Git - biopieces.git/commitdiff
migrated sort_records
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 28 May 2009 10:01:12 +0000 (10:01 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 28 May 2009 10:01:12 +0000 (10:01 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@438 74ccb610-7750-0410-82ae-013aeee3265d

bp_bin/sort_records
code_perl/Maasha/BioRun.pm

index fdf5bd287f78613d2aea83bb9b15f855cad57e77..32d59dfc38c6e337b25d815f0375ba9487f70c85 100755 (executable)
@@ -1,6 +1,106 @@
-#!/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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# Sort records in the stream.
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
 
-use warnings;
 use strict;
+use Maasha::Biopieces;
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+my ( $run_time_beg, $run_time_end, $options, $in, $out, @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
+
+$options = Maasha::Biopieces::parse_options(
+    [
+        { long => 'keys',    short => 'k', type => 'list', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
+        { long => 'reverse', short => 'r', type => 'flag', mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
+    ]   
+);
+
+$in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
+$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
+
+foreach $key ( @{ $options->{ "keys" } } )
+{
+    if ( $key =~ s/n$// ) {
+        push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
+    } else {
+        push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
+    }
+}
+
+$sort_str = join " or ", @sort_cmd;
+$sort_sub = eval "sub { $sort_str }";   # NB security issue!
+
+while ( $record = Maasha::Biopieces::get_record( $in ) ) {
+    push @records, $record;
+}
+
+@records = sort $sort_sub @records;
+
+if ( $options->{ "reverse" } )
+{
+    for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
+        Maasha::Biopieces::put_record( $records[ $i ], $out );
+    }
+}
+else
+{
+    for ( $i = 0; $i < scalar @records; $i++ ) {
+        Maasha::Biopieces::put_record( $records[ $i ], $out );
+    }
+}
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+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 8ad923964d17b37898232e0ae6869b2091f98803..aa82088fabd69a5d42a7e97a4732fa730b06fc87 100644 (file)
@@ -149,7 +149,6 @@ sub run_script
     elsif ( $script eq "remove_ucsc_tracks" )       { script_remove_ucsc_tracks(        $in, $out, $options ) }
     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 "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 ) }
     elsif ( $script eq "plot_chrdist" )             { script_plot_chrdist(              $in, $out, $options ) }
@@ -465,13 +464,6 @@ sub get_options
             invert|i
         );
     }
-    elsif ( $script eq "sort_records" )
-    {
-        @options = qw(
-            reverse|r
-            keys|k=s
-        );
-    }
     elsif ( $script eq "plot_histogram" )
     {
         @options = qw(
@@ -654,7 +646,6 @@ sub get_options
     Maasha::Common::error( qq(both --in_file and --genome specified) )  if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
     Maasha::Common::error( qq(no --genome specified) )                  if $script =~ /get_genome_align|get_genome_phastcons|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
     Maasha::Common::error( qq(no --key specified) )                     if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
-    Maasha::Common::error( qq(no --keys speficied) )                    if $script =~ /sort_records/ and not $options{ "keys" };
 
     if ( $script eq "upload_to_ucsc" )
     {
@@ -2404,54 +2395,6 @@ sub script_uniq_vals
 }
 
 
-sub script_sort_records
-{
-    # Martin A. Hansen, August 2007.
-
-    # Sort to sort records according to keys.
-
-    my ( $in,        # handle to in stream
-         $out,       # handle to out stream
-         $options,   # options hash
-       ) = @_;
-
-    # Returns nothing.
-
-    my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
-
-    foreach $key ( @{ $options->{ "keys" } } )
-    {
-        if ( $key =~ s/n$// ) {
-            push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
-        } else {
-            push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
-        }
-    }
-
-    $sort_str = join " or ", @sort_cmd;
-    $sort_sub = eval "sub { $sort_str }";   # NB security issue!
-
-    while ( $record = Maasha::Biopieces::get_record( $in ) ) {
-        push @records, $record;
-    }
-
-    @records = sort $sort_sub @records;
-
-    if ( $options->{ "reverse" } )
-    {
-        for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
-            Maasha::Biopieces::put_record( $records[ $i ], $out );
-        }
-    }
-    else
-    {
-        for ( $i = 0; $i < scalar @records; $i++ ) {
-            Maasha::Biopieces::put_record( $records[ $i ], $out );
-        }
-    }
-}
-
-
 sub script_plot_histogram
 {
     # Martin A. Hansen, September 2007.