#!/usr/bin/perl
############################################################
#
# $Id: row-stats,v 1.6 2009/11/05 00:32:07 jvanheld Exp $
#
# Time-stamp: <2002-06-06 13:37:27 jvanheld>
#
############################################################
#use strict;;
if ($0 =~ /([^(\/)]+)$/) {
    push (@INC, "$`lib/");
}
require "RSA.lib";


#### initialise parameters ####
my $start_time = &AlphaDate;

local %infile = ();
local %outfile = ();

local $verbose = 0;
local $in = STDIN;
local $out = STDOUT;
local $number_format = "%g";

&ReadArguments;


#### check argument values ####


### open output file ###
$out = &OpenOutputFile($outfile{output});
&PrintRowHeader();

##### read input #####
($in, $input_dir) = &OpenInputFile($infile{input});
$line_nb = 0;
while (<$in>) {
    $line_nb++;
    next if (/^;/);
    next unless (/\S/);
    chomp;
    @fields = split "\t";
    &PrintRowStats(@fields);
}

close $in if ($infile{input});

#### verbose ####
&Verbose if ($verbose);

###### execute the command #########


###### print output ######


###### verbose ######
if ($verbose) {
  my $done_time = &AlphaDate;
  print $out "; Job started $start_time\n";
  print $out "; Job done    $done_time\n";
}


###### close output file ######
close $out if ($outfile{output});


exit(0);

sub PrintRowHeader {
    print $out join ("\t", 
		     ";ID",
		     "min",
		     "max",
		     "sum",
		     "avg"
		     );
    print $out "\n";

}

sub PrintRowStats {
    my ($id, @values) = @_;
    print $out $id;
    printf $out "\t${number_format}", &checked_min(@values);
    printf $out "\t${number_format}", &checked_max(@values);
    printf $out "\t${number_format}", &checked_sum(@values);
    printf $out "\t${number_format}", &checked_avg(@values);
    print $out "\n";
}


########################## subroutine definition ############################

sub PrintHelp {
#### display full help message #####
  open HELP, "| more";
  print HELP <<End_of_help;
NAME
	row-stats

        2001 by Jacques van Helden (jvanheld\@bigre.ulb.ac.be)
	
USAGE
        row-stats [-i inputfile] [-o outputfile] [-v]

DESCRIPTION

	Calculate basic statistics (min, max, avg, ...) on each row of
	a tab-delimited input file.

CATEGORY
	statistics

OPTIONS
	-h	(must be first argument) display full help message
	-help	(must be first argument) display options
	-v	verbose
	-i inputfile
		if not specified, the standard input is used.
		This allows to place the command within a pipe.
	-o outputfile
		if not specified, the standard output is used.
		This allows to place the command within a pipe.
	-nf	number format
		Any format specification that is compliant with the
		perl printf function is supported (e.g. '%7.4f')

INPUT FORMAT

      The input file is a tab-delimited text file. Each row
      corresponds to a new object, and each column to an attribute of
      this object. The first column must contain the ID of the
      object. The remaning columns are supposed to contain a series
      numbers, on which the statistics will be calculated.

OUTPUT FORMAT

       The output file is a tab-delimited text file. Each row
       corresponds to an object (thos from the input file). The first
       column reports the object ID, and the collowing columns contain
       the requested statistics. The header indicates which statistics
       is contained in which column.


End_of_help
  close HELP;
  exit;
}

sub PrintOptions {
#### display short help message #####
  open HELP, "| more";
  print HELP <<End_short_help;
row-stats options
----------------
-h	(must be first argument) display full help message
-help	(must be first argument) display options
-i	input file
-o	output file
-v	verbose
-nf	number format (e.g. '%7.4f')
End_short_help
  close HELP;
  exit;
}


sub ReadArguments {
#### read arguments ####
    foreach my $a (0..$#ARGV) {
	### verbose ###
	if ($ARGV[$a] eq "-v") {
	    if (&IsNatural($ARGV[$a+1])) {
		$verbose = $ARGV[$a+1];
	    } else {
		$verbose = 1;
	    }
	    
	    ### detailed help
	} elsif ($ARGV[$a] eq "-h") {
	    &PrintHelp;
	    
	    ### list of options
	} elsif ($ARGV[$a] eq "-help") {
	    &PrintOptions;
	    
	    ### input file ###
	} elsif ($ARGV[$a] eq "-i") {
	    $infile{input} = $ARGV[$a+1];
	    
	    ### output file ###
	} elsif ($ARGV[$a] eq "-o") {
	    $outfile{output} = $ARGV[$a+1];
	    
	    ### input file ###
	} elsif ($ARGV[$a] eq "-nf") {
	    $number_format = $ARGV[$a+1];
	    
	    
	}
    }
}

sub Verbose {
  print $out "; row-stats ";
  &PrintArguments($out);
  if (defined(%infile)) {
    print $out "; Input files\n";
    while (($key,$value) = each %infile) {
      print $out ";\t$key\t$value\n";
    }
  }
  if (defined(%outfile)) {
    print $out "; Output files\n";
    while (($key,$value) = each %outfile) {
      print $out ";\t$key\t$value\n";
    }
  }
}
