#!/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
	overlap-coeff

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

DESCRIPTION
	Calculates overlap coefficient for a given sequence. 

CATEGORY
	statistics
	sequences
	
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.
	-2str	take reverse complement into account to calculate overlap coefficient		
	
INPUT FORMAT
	Any text file. The first word of each line is considered as the sequence
	for which overlap coefficient is calculated.
	
OUTPUT FORMAT
	The input file with one column added, providing the overlap coefficient.
	
EXAMPLES
       overlap-coeff -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;
overlap-coeff 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
-2str	take reverse complement into account to calculate overlap coefficient		
End_short_help
  close HELP;
  exit;
}

#### initialise parameters ####
$start_time = &RSAT::util::StartScript();


#### read arguments ####
foreach $a (0..$#ARGV) {
    ### verbose ###
    if ($ARGV[$a] eq "-v") {
	$verbose = 1;
    ### input file ###
    } elsif ($ARGV[$a] eq "-i") {
	$inputfile = $ARGV[$a+1];
    ### output file ###
    } elsif ($ARGV[$a] eq "-o") {
	$outputfile = $ARGV[$a+1];

    } elsif ($ARGV[$a] eq "-2str") {
	$sum_strands = $1;

    }
}


#### check argument values ####



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

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

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

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

while ($line = <$in>) {
    chomp($line);
    if (($line =~ /^;/) || !($line =~ /\S/)) {
	print $out $_;
	next;
    }

    if ($line =~ /^\s*(\S+)/) {
	$word = $1;
	$coeff = &OverlapCoeff($word);
	print $out "$line";
	print $out "\t$coeff\n";
    }

}

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


################################################################
## Report execution time and close output stream
my $exec_time = &RSAT::util::ReportExecutionTime($start_time); ## This has to be exectuted by all scripts
print $main::out $exec_time if ($main::verbose >= 1); ## only report exec time if verbosity is specified
close $main::out if ($outputfile);


exit(0);


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

