#!/usr/local/bin/perl

###############################################################################################
# Perl script
# Author C.Mathe 21/10/98
# this script extract data from a Splice_predictor 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 readSPP{ #read Splice_predictor output.
open(SPP,"<$_[0]") ||die "Unable to open $_[0]";

while(<SPP>)
{
    $lg=$F=$type=$ph=$Rend=$Lend=$D=$A=$proba="";
    if (/^[AD]\s*[<-]/)	{
	    ($splice,$fig,$pos,$seqpart,$P,$rho,$gamma,$proba,$rest)=split;
	    if ($splice eq 'A') {
		$A=$pos;
		($Rend,$Lend,$D)=&calcBeg($A,$str);
		&EcrireST($seq,$type,$str,$Lend,$Rend,$lg,$ph,$F,$A,$D,$proba);		
	    }
	    else {$D=$pos;
		  ($Rend,$Lend,$A)=&calcEnd($D,$str);
		  &EcrireST($seq,$type,$str,$Lend,$Rend,$lg,$ph,$F,$A,$D,$proba);	      
	      }
	}
    }
close(SPP);
}


sub readLS{
    open(LS,"< $liste") || die "Unable to open $liste.\n";
    $ST=&openST("splicepredictor");
    while(<LS>){
	$count++;
	($seq,$Ltot)=split;              # read each file name and each corresponding sequence length
	if (($seq eq "")||($Ltot eq "")) #check if the list is correctly formatted
	{
	    &usage("Splice_predictor");
	    die "!!! Incorrect input list at line $count!!!\n";}
	$fileD=$seq . "splicep1.txt";
	$fileR=$seq . "splicep2.txt";
	$str="+";
	&readSPP($fileD); #read direct strand file
	$str="-";
	&readSPP($fileR); #read reverse strand file
	print ST "\n";}
close LS;	
}

sub createST{
     @data=&Start("Splice_predictor");
     if ($data[1]==0) { 
	 $liste=$data[0];
	 &readLS;
	 print "output file: $standart\n";
     }
     else 
     {
	 $Ltot=$data[1];
	 $fileD=$data[0];
	 @tmp=split('splicep1',$fileD);
	 $fileR=$tmp[0] . "splicep2.txt";
	 print "Take $fileR as output name on reverse strand.\n";
	 $seq=$tmp[0];
	 $file2 =$tmp[0] . "splicep12.txtST";
	 open(ST,">$file2");
	 print ST "Contig\tType\tStrand\tLend\tRend\tlength\tPhase\tFrame\tAcceptor\tDonor\tScore\n";
	 $str="+";
	 &readSPP($fileD);
	 $str="-";
	 &readSPP($fileR);
	 print "output file: $file2\n";
     }
     close ST;
}

&createST;


