From: martinahansen Date: Thu, 28 May 2009 10:01:12 +0000 (+0000) Subject: migrated sort_records X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=ce9afe503d79816a65b6d063df94a09f0e73ef5a;p=biopieces.git migrated sort_records git-svn-id: http://biopieces.googlecode.com/svn/trunk@438 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/bp_bin/sort_records b/bp_bin/sort_records index fdf5bd2..32d59df 100755 --- a/bp_bin/sort_records +++ b/bp_bin/sort_records @@ -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; diff --git a/code_perl/Maasha/BioRun.pm b/code_perl/Maasha/BioRun.pm index 8ad9239..aa82088 100644 --- a/code_perl/Maasha/BioRun.pm +++ b/code_perl/Maasha/BioRun.pm @@ -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.