#!/usr/bin/env perl use strict; use warnings; my $RCS_ID = '$Id: sorttbl,v 1.3 2006/09/09 22:31:11 hiram Exp $' ; $0 =~ s-.*/-- ; # Modified by markd@cse.ucsc.edu to use gsort -g # Modified by karplus@soe.ucsc.edu to look for gsort then sort use English; use File::Basename; my $HelpInfo = <){ my $ADDCOL = 0; if( $lln < 2 ){ print unless $CHK ; if( /^\s*#/ ){ next ; } # comment chop ; @H = split( /\t/, $_ ); if( ++$lln == 1 ){ # header line @F = @H ; &get_col_x ; } # chk column names else{ # definition line $| = 0 ; for my $arg ( @ARGV ){ if( $arg =~ /^-r.*/ ){ $cf .= 'r' ; next ; } for( my $f=0, my $g=1 ; $f <= $#F ; $f++, $g++ ){ if( $arg eq $F[$f] ){ if( $H[$f] =~ /(\S+)/ && $1 =~ /N/i ){ # numeric $cf .= 'g' ; } if( $H[$f] =~ /(\S+)/ && $1 =~ /M/i ){ # month $cf .= 'M' ; } $sarg .= "+$f -$g$cf " ; $cf = '' ; last ; } } } if( $CHK && $sarg =~ /\+/ ){ $ADDCOL++ ; my $f = @H ; my $g = $f +1 ; $cf = 'g' ; $sarg .= "+$f -$g$cf " ; } print STDERR "sort arg: -t\"\t\" $sarg\n" if $XBUG ; # try gsort first for non-linux systems without GNU sort my $arch = `uname -m`; chomp $arch; my $gsort = "/projects/compbio/bin/$arch/gsort"; my $sortCmd; if (-e $gsort) { $sortCmd = $gsort; } else { $sortCmd = "sort"; } open( SS, "| $sortCmd -t\"\t\" $sarg" ) || die "\nCan't open pipe to gsort or sort\n" ; } next ; } if( $ADDCOL ){ # add dummy column for check option chop ; print SS $_, "\t", $., "\n" ; } else{ # normal case print SS $_ ; } } close( SS ) || die("sort failed"); sub get_col_x { # get, chk column indexes, inc -v, die if bad column # uses @H, $INV, put indexes in @n. # modified for sorttbl. my $f; my $ok; my @nn; my $INV = 0; my @n; for my $arg (@ARGV){ next if $arg =~ /^-/ ; for( $ok=$f=0 ; $f < @H ; $f++ ){ if( $arg eq $H[$f] ){ # match existing column $ok++ ; push( @n, $f ); last ; } } die "\n$0: Bad column name: $arg\n" if ! $ok ; } if( $INV ){ # inverse option loop: for( $f=0 ; $f < @H ; $f++ ){ for my $i (@n){ next loop if $i eq $f ; } push( @nn, $f ); } @n = @nn ; } }