#!/usr/bin/perl
if ($0 =~ /([^(\/)]+)$/) {
    push (@INC, "$`lib/");
}
require "RSA.lib";



if ($ARGV[0] eq "-h") {
#### display full help message #####
  open HELP, "| more";
  print HELP <<End_of_help;
NAME
	Fisher-exact-test

        1998 by Jacques van Helden (jvanheld\@bigre.ulb.ac.be)
	
USAGE
        Fisher-exact-test [-i inputfile] [-o outputfile] [-v]

CATEGORY
	statistics

DESCRIPTION
	Calculates the value of probability for Fisher's exact test,
	for a given table of numerical values.
	
OPTIONS
        -h      (must be first argument) display full help message
        -help   (must be first argument) display options
	-v	verbose
		returns a data report, i.e. a table with the input values
		and the marginal sums.
		
	-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.		
	
INPUT FORMAT
	The input must be a table containing exclusively numerical 
	values. Within each row, the values are separated by a single 
	tabulation. All rows must contain the same number of values.
	All values must be natural numbers (i.e. 0,1,2,...). 
	Blank lines are ignored.

OUTPUT FORMAT
	The output is a single number comprized between 0 and 1, 
	indicating the probability for the hypothesis of homogeneity 
	to be true.

REFERENCE
	Fisher's exact test is described in about any textbook of 
	statistics. I read the definition in:
		B.W. Lindgren (1976). Statistical Theory.
		Macmillan Publishers co. 3rd edition. 	
	
EXAMPLES
       Fisher-exact-test -v -i mydata -o myresult
	
End_of_help
  close HELP;
  exit;
}

if ($ARGV[0] eq "-help") {
#### display short help message #####
  open HELP, "| more";
  print HELP <<End_short_help;
Fisher-exact-test 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
End_short_help
  close HELP;
  exit;
}

$start_time = `date '+%d/%m/%y %H:%M:%S %Z'`;

#### initialise parameters ####

#### read arguments ####
foreach $a (0..$#ARGV) {

    if ($ARGV[$a] eq "-v") {
	$verbose = 1;
	$report_data = 1;

    } elsif ($ARGV[$a] eq "-i") {
	$inputfile = $ARGV[$a+1];

    } elsif ($ARGV[$a] eq "-o") {
	$outputfile = $ARGV[$a+1];

    }
}


#### check argument values ####



### open input file ###
($in, $input_dir) = &OpenInputFile($inputfile);

### open output file ###
$out = &OpenOutputFile($outputfile);

#### verbose ####
if ($verbose) {
  print $out ";Fisher-exact-test result\n";
  if ($inputfile ne "") {
    print $out ";Input file	$inputfile\n";
  }
  if ($outputfile ne "") {
    print $out ";Output file	$outputfile\n";
  }
}

### read a table of positive integer values ###
$row = 0;
while (<$in>) {
    if (/\S/) {
	$row++;
	@fields = split /\s+/;
	if ($row == 1) {
	    $col_nb = $#fields + 1;
	} elsif ($#fields + 1 != $col_nb) {
	    print "Error: each line must contain the same number of values\n";
	    print "Type Fisher-exact-test -h for info.\n";
	    exit;
	}
	foreach $f (@fields) {
	    if (&IsNatural($f)) {
		push(@values, $f);
	    } else {
		print "Error: $f is not a positive integer value\n";
		print "Type Fisher-exact-test -h for info.\n";
		exit;
	    }
	}
    }
}
$row_nb = $row;

$proba = &FisherExactTest($row_nb, $col_nb, @values);

#### print output ####
print $out $proba, "\n";


###### verbose ######
if ($verbose) {
  $done_time = `date '+%d/%m/%y %H:%M:%S %Z'`;
  print $out ";Job started $start_time";
  print $out ";Job done    $done_time";
}


###### close input file ######
close $in unless ($inputfile eq "");

###### close output file ######
close $out unless ($outputfile eq "");


exit(0);


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