#!/usr/bin/env 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
	matrix-from-contrast

        1998 by Jacques van Helden (Jacques.van-Helden\@univ-amu.fr)
	
USAGE
        matrix-from-contrast [-i inputfile] [-o outputfile] [-v]

DESCRIPTION
	Converts the output file from contrast into a matrix that can 
	be read by PHYLIP.
	
CATEGORY
	util
	conversion
	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.
		
	
INPUT FORMAT
	Each line has the following form:

	r   between cpa1*              & pho5*              is  0.015

OUTPUT FORMAT
	A distance matrix in a format that can be read by PHYLIP.
	
REFERENCE
	S.Pietrokovski, J.Hirshon and E.N. Trifonov (1990).
	Linguistic measure of functional taxonomy and functional 
	relatedness of nucleotide sequences. 
	J. Biomolecular Structure & Dynamics, 7 (6): 1251-1268.

EXAMPLES
       matrix-from-contrast -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;
matrix-from-contrast 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 = &RSAT::util::StartScript(); 
$date = $start_time;

#### initialise parameters ####

#### 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];

    }
}


#### check argument values ####



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

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

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

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

while (<$in>) {
    if (/between\s+(\S+)\*.*\&\s+(\S+)\*.*is\s+(\S+)/) {
	$id1 = $1;
	$id2 = $2;
	$id_hash{$id1} = 1;
	$id_hash{$id2} = 1;
	$score = $3;
	if (&IsReal($score)) {
	    $score{$id1}{$id2} = $score;
	    $score{$id2}{$id1} = $score;
	} else {
	    $error{$id1}{$id2} = $score;
	}
    }
}

###### print output ######
@id_list = sort keys %id_hash;
print $out $#id_list + 1, "\n";
foreach $id1 (@id_list) {
    $score{$id1}{$id1} = 1;
    $print_id = substr($id1, 0, 10);
    for $sp (length($print_id)..10) {
	$print_id .= " ";
    }
    print $out "$print_id";
    foreach $id2 (@id_list) {
	print $out " $score{$id1}{$id2}";
    }
    print $out "\n";
}


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

###### close output file ######
my $exec_time = &RSAT::util::ReportExecutionTime($start_time);
print $main::out $exec_time if ($main::verbose >= 1);
close $out unless ($outputfile eq "");


exit(0);


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

