#!/usr/local/bin/perl

###############################################################################################
# Perl script
# Author C.Mathe 24/02/99
# this script extract data from a fexa output and write them in a standart format
###############################################################################################

($dir,$name_script)= ($0 =~/(.+\/)*(.+\.pl)$/);
$need=$dir."util.pl";
require "$need";

#require "/home/camat/BIOCOMP/Perl/util.pl";

sub ModType{ #write a type of exons from fexa listing
if (/Num/) {$FEX_type="Intr";}
	if (/First/) {$FEX_type="Init";}
	if (/Last/) {$FEX_type="Term";}
return ($FEX_type);
}

sub readFEXD{ #read fexa output on direct strand input.
open(FGD,"<$_[0]") ||die "Unable to open $_[0]";
$nbE=0;
undef @Lend_list;
while(<FGD>)
{	
    $type=$Lend=$Rend=$lg=$ph=$F=$A=$D=$proba="";
    $str= "+";
    if (($Lend[$nbE],$Rend[$nbE],$proba[$nbE],$Fex_type[$nbE],$orfL[$nbE],$orfR[$nbE])=($_ =~ /^\s+(\d+)\s\-\s+(\d+)\sw=\s+(\d{1,2}\.\d{2})\sORF=\s\s\d{1}\s(.*)\s[(\sexon)?(ORFs\s\s\d{1})?]\s+(\d+)\s\-\s+(\d+)$/))
    {
	$type[$nbE]=&ModType;
	push(@Lend_list,$Lend[$nbE]);
	$strand[$nbE]=$str;
	$frame[$nbE]=&calcF($orfL[$nbE],$orfR[$nbE],$strand[$nbE]);
	$nbE++;
    }
}
close FGD;
}

sub readFEXR{ #read fexa output on reverse strand input
    open(FGR,"<$_[0]")||die "Unable to open @_[0]";
    while(<FGR>)
    {

	$type=$Lend=$Rend=$lg=$ph=$F=$A=$D=$proba="";
	$str="-";
	if (($Revbeg,$Revend,$proba[$nbE],$Fex_type[$nbE],$orfL[$nbE],$orfR[$nbE])=($_ =~ /^\s+(\d+)\s\-\s+(\d+)\sw=\s+(\d{1,2}\.\d{2})\sORF=\s\s\d{1}\s(.*)\s[(\sexon)?(ORFs\s\s\d{1})?]\s+(\d+)\s\-\s+(\d+)$/))
	{
	    $type[$nbE]=&ModType;
	    $Lend[$nbE]=&reverse($Revend); # recalculate coordinate in 5'->3'  
	    $Rend[$nbE]=&reverse($Revbeg);
	    push(@Lend_list,$Lend[$nbE]);
	    $frame[$nbE]=&calcF($orfL[$nbE],$orfR[$nbE],'+');
	    $strand[$nbE]=$str;
	    $nbE++;
	}
    }

    close FGR;
}

sub readLS{
    open(LS,"< $liste") || die "Unable to open $liste.\n";
    $ST=&openST("fex");
    while(<LS>){
	$count++;
	($seq,$Ltot)=split;              # read each fex file name and each corresponding sequence length
	if (($seq eq "")||($Ltot eq "")) #check if the fex_list is correctly formatted
	{
	    &usage("Fexa");
	    die "!!! Incorrect input list at line $count!!!\n";}
	$fileD=$seq . "fex1.txt";
	$fileR=$seq . "fex2.txt";
	&readFEXD($fileD); 
	&readFEXR($fileR);
	&ordon(@Lend_list);
	print ST "\n";}
close LS;	
}

sub createST{
     @data=&Start("Fexa");
if ($data[1]==0) { 
    $liste=$data[0];
    &readLS;
    print "output file: $standart\n";
}
else 
{
    $Ltot=$data[1];
    $fileD=$data[0];
    @tmp=split('fex1',$fileD);
    $fileR=$tmp[0] . "fex2.txt";
    print "Take $fileR as output name on reverse strand.\n";
    $seq=$tmp[0];
    $file2 =$tmp[0] . "fex12.txtST";
    open(ST,">$file2");
    print ST "Contig\tType\tStrand\tLend\tRend\tlength\tPhase\tFrame\tAcceptor\tDonor\tScore\n";
    &readFEXD($fileD);
    &readFEXR($fileR);
    &ordon(@Lend_list); #mix results from strand + and -, and ordon them in ascending order.
    print "output file: $file2\n";
}
close ST;
}

&createST;
   
