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




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

&ReadArguments();


#### check argument values ####


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


&Verbose() if ($verbose >= 1);

##### read input #####
($in) = &OpenInputFile($infile{input});
while (<$in>) {
    s/\s+/\t/g;
    if ((/^\s*PO\s+(.*)/) ||  
	(/^\s*PO\s+(.*)/)) {
	@alphabet = split "\t", $1;
    } elsif (/^\s*(\d+)\s+(.*)/) {
	$pos = $1;
	$max_pos = &max($pos,$max_pos);
	$weigths = $2;
	$weigths =~ s/\s+/\t/g;
	@weigths = split /\s+/, $weigths;
	for $a (0..$#alphabet) {
	    $matrix[$a][$pos] = $weigths[$a];
	}
    }
}
close $in unless ($infile{input} eq "");


###### print matrix ######
foreach $a (0..$#alphabet) {
  print $out $alphabet[$a];
  print $out "  |";
  foreach $pos (1..$max_pos) {
    if ($matrix[$a][$pos] eq "") {
      $matrix[$a][$pos] =0;
    }
    printf $out "%4s", $matrix[$a][$pos];
  }
  print $out "\n";
}


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

#### display full help message #####
sub PrintHelp {

  open HELP, "| more";
  print HELP <<End_of_help;
NAME
	matrix-from-transfac

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

DESCRIPTION
	Converts a matrix from Transfac database into a matrix that 
	can be read by Jerry Hertz\'s patser program.
	
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
	the input must be a Transfac record. See for example the GAL4 
	matrix from Transfac below. 

OUTPUT FORMAT
	Matrix in Jerry Hertzs format (see consensus, patser).

	Example :

	AC  M00049
	XX
	ID  F$GAL4_01
	XX
	DT  13.04.95 (created); ewi.
	XX
	NA  GAL4
	XX
	DE  GAL4
	XX
	BF  T00302; GAL4; Species: yeast, Saccharomyces cerevisiae.
	XX
	P0      A      C      G      T
	01      1      5      3      2      N
	02      5      2      1      3      N
	03      3      2      1      5      N
	04      1     10      0      0      C
	05      0      0     10      1      G
	06      0      1     10      0      G
	07      4      3      3      1      N
	08      1      3      4      3      N
	09      2      4      4      1      N
	10      7      0      2      2      A
	11      1      8      2      0      C
	12      4      1      0      6      W
	13      1      3      5      2      N
	14      0      2      1      8      T
	15      1      6      2      2      C
	16      1      5      4      1      S
	17      2      1      1      7      T
	18      0     10      1      0      C
	19      0     11      0      0      C
	20      0      0     11      0      G
	21      8      0      0      3      A
	22      7      0      4      0      R
	23      2      6      3      0      S
	XX
	BA  11 genomic binding sites from 6 genes
	XX
	CC  compiled sequences
	XX
	RN  [1]
	RA  Bram R. J., Lue N. F., Kornberg R. D.
	RT  A GAL family of upstream activating sequences in yeast: 
	RT  roles in both induction and repression of transcription
	RL  EMBO J. 5:603-608 (1986).
	XX
	//


	
EXAMPLES
       matrix-from-transfac -v -i mydata -o myresult

End_of_help
  close HELP;
  exit;
}

################################################################
#### read arguments 
sub ReadArguments {
    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];
	    
	}
    }
}

################################################################
#### verbose message
sub Verbose {
    print $out "; matrix-from-transfac ";
    &PrintArguments($out);
    if (%main::infile) {
	print $out "; Input files\n";
	while (($key,$value) = each %infile) {
	    print $out ";\t$key\t$value\n";
	}
    }
    if (%main::outfile) {
	print $out "; Output files\n";
	while (($key,$value) = each %outfile) {
	    print $out ";\t$key\t$value\n";
	}
    }
}

################################################################
#### display short help message
sub PrintOptions {

  #### display short help message #####
  open HELP, "| more";
  print HELP <<End_short_help;
matrix-from-transfac 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;
}
