#!/usr/bin/perl -w
############################################################
#
# $Id: conditional-probabilities,v 1.6 2010/06/09 23:06:11 jvanheld Exp $
#
# Time-stamp: <2003-07-04 12:48:55 jvanheld>
#
############################################################
#use strict;;
if ($0 =~ /([^(\/)]+)$/) {
    push (@INC, "$`lib/");
}
require "RSA.lib";

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

local %infile = ();
local %outfile = ();
local %subword_freq = ();
local %conditional_proba = ();
local $verbose = 0;
#local $in = STDIN;
local $out = STDOUT;

&ReadArguments();

################################################################
#### check argument values


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

################################################################
##### read input

&CalcConditionalProbabilities($infile{input});
 

################################################################
#### Print output

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

#### header
print $out "; oligo";
foreach my $l (0..$#letters) {
    print $out "\t", $letters[$l];
}
print $out "\n";

#### transition matrix
foreach my $word (sort keys %subword_freq) {
    print $out $word;
    foreach my $l (0..$#letters) {
	print $out "\t", $conditional_proba{$word}[$l];
    }
    print $out "\n";
}

################################################################
###### close output stream
my $exec_time = &RSAT::util::ReportExecutionTime($start_time);
print $main::out $exec_time if ($main::verbose >= 1);
close $out if ($outfile{output});


exit(0);


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


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

        2002 by Jacques van Helden (jvanheld\@bigre.ulb.ac.be)
	
DESCRIPTION

	Calculate conditional residue probabilities on the basis of a
	specified oligonucleotide frequencies. 

INPUT
	A frequency file, such as those generated by oligo-analysis. 
	The input specifies the frequency of each oligonucleotide of
	size k.

OUTPUT
	A transition matrix, one row per oligonucleotide (W of size
	k-1), 4 columns (A, T, G, C).

	The values indicate the conditional probability		
	    P(R|W)
	where
	    R is the residue (A, C, G or T)
	    W is the k-1 letter word preceding the residue

CATEGORY
	util

USAGE
        conditional-probabilities [-i inputfile] [-o outputfile] [-v]

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.

End_of_help
  close HELP;
  exit;
}

################################################################
#### display short help message
sub PrintOptions {
  open HELP, "| more";
  print HELP <<End_short_help;
conditional-probabilities 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;
}


################################################################
#### 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 "; conditional-probabilities ";
    &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";
	}
    }
}
