#!/usr/local/bin/perl
	
sub calcF {
    local ($L,$R,$strand)=@_;
    if ($strand eq '+') {$F = ($L -1)%3 +1;}
    else {$F = ($Ltot -$R)%3 +1;}
    return $F;
}


sub perl_script { #return the name of the Perl script in used.
    local($pred,$script);
    $pred=$_[0];
    ($dummy,$script)=($pred =~ /(.+\/)*(.+\.pl)$/);
    return $script;
}

sub Start{

    $predictor=$_[0];
    $prog=$0;
    $Script=&perl_script($prog);
    if ($#ARGV!=1) 
    { &usage($predictor);}
    else {if ($ARGV[0] eq '-l') {$liste=$ARGV[1];
			      %data=($liste,0);
			  }
	  else {
	      $file=$ARGV[0];
	      $Ltot=$ARGV[1];
	      chop $Lot;
	      %data=($file,$Ltot);
}
	  return %data;}
}
    

sub usage{
    $predictor=$_[0];
    $prog=$0;
    $Script=&perl_script($prog);
    die "USAGE:\n$Script -l <list>\t (list is a file containing on each line: seq_name seq_length.)\n
or:
$Script <file> <seq_length>\t (where file = a $predictor output file\n\t\t\t\t\  and length = length of the sequence)\n\n";
}

sub calcAD{
	local($L,$R,$strand)=@_;	
	local($Ac,$Do,$length);
	$Ac = -1+$L;
	$Do = 1+$R;		
	$length = $R-$L+1;
	if ($strand eq '-'){$tmp=$Ac;
			    $Ac=$Do;		
			    $Do=$tmp;}
	return ($length,$Ac,$Do);
    }	# calculation of Donor, Acceptor and from Lend and Rend.


sub calcAD_model{ #new version to take into account the model information Term, Init, Sngl.
	local($L,$R,$strand,$M_type)=@_;	
	local($Ac,$Do,$length);
	$Ac = -1+$L;
	$Do = 1+$R;		
	$length = $R-$L+1;
	if ($strand eq '-'){$tmp=$Ac;
			    $Ac=$Do;		
			    $Do=$tmp;}
	if (($M_type eq 'Term')||($M_type eq 'Sngl')) {$Do="";}
	if (($M_type eq 'Init')||($M_type eq 'Sngl')) {$Ac="";}
	return ($length,$Ac,$Do);
    }	# calculation of Donor, Acceptor and from Lend and Rend.


sub calcBeg{
    local($Ac,$strand)=@_;
    local ($Do,$R,$L);
    if ($strand eq '+'){
	$L=$Ac + 1;
	$R=$Do="";
    }
    else {$R=$Ac-1;
	  $Do=$L="";
      }
    return ($R,$L,$Do);
} #calculation of Lend, Rend and Frame when the program gives Acceptor and Donor splice sites.


sub calcEnd{
    local($Do,$strand)=@_;
    local($Ac,$L,$R);
    if ($strand eq '+'){
	$R=$Do-1;
	$L=$Ac="";
    }
    else {$L=$Do + 1;
	  $R=$Ac="";
      }
    return($R,$L,$Ac);
}
	

sub reverse{ #for prediction programs that must be run once on direct and once on complement strand
    # programs give the site positions numeroted from 3' to 5'. This subroutine calculates the corresponding position on direct strand (5' to 3')
    local($Compl,$Direct);
    $Compl=$_[0];
    $Direct=$Ltot-$Compl+1;
}

sub openST
	{
	    if (($0 =~ /fgene.*2st.pl/)||($0 =~ /spl2st.pl/)||($0 =~ /splicep2st.pl/))
	    {$score ="Score";}
	    else {$score ="Proba";}
	    $standart="dataset_". $_[0]."ST";	
	open(ST,">$standart") || die "Can't open output $standart!";
print ST "Contig\tType\tS\tLend\tRend\tLength\tPhase\tFrame\tAc\tDo\t$score\n\n";
	}

sub EcrireST { 
    local($Seq,$Type,$Str,$L,$R,$length,$Phase,$frame,$Ac,$Do,$Proba)=@_;
    $Lbord=1701; $Rbord=$Ltot-1699;
    if ($frame ne "") {$frame= $Str . $frame;}
    if ($data[1]==0) {
        if (($0 =~ /splicep2st.pl/)||($0 =~ /ng2st.pl/)||($0 =~ /npg2st.pl/)||($0 =~ /spl2st.pl/)||($0 =~ /start2st.pl/))
	{ 
	    if (($L ne "")&&($L>=$Lbord)&&($L<=$Rbord)) 
	    {
		print ST "$Seq\t$Type\t$Str\t$L\t$R\t$length\t$Phase\t$frame\t$Ac\t$Do\t$Proba\n";}
	    elsif (($R ne "")&&($R>=$Lbord)&&($R<=$Rbord)) 
	    {
		print ST "$Seq\t$Type\t$Str\t$L\t$R\t$length\t$Phase\t$frame\t$Ac\t$Do\t$Proba\n";}
	}
	else {
	    if (($L<$Lbord)&&($R>$Lbord)) {$L=$Lbord; 
					   if ($Str eq '+') {$Ac="";} else {$Do="";}}
	    if (($R>$Rbord)&&($L<$Rbord)) {$R=$Rbord; 
					   if ($Str eq '+') {$Do="";} else {$Ac="";}}
	    if (($L>=$Lbord)&&($R<=$Rbord)) {
		print ST "$Seq\t$Type\t$Str\t$L\t$R\t$length\t$Phase\t$frame\t$Ac\t$Do\t$Proba\n";}}}
    else {print ST "$Seq\t$Type\t$Str\t$L\t$R\t$length\t$Phase\t$frame\t$Ac\t$Do\t$Proba\n";}
}
1;

sub ordon { # routine to re-order exons in ascending order, both strand results mixed. 
    @Lend_list=@_;
    @Lend_list=sort{$a <=>$b}@Lend_list;
    local($L_ord);
    foreach $L_ord (@Lend_list){
	for ($k=0;$k<$nbE;$k++)
	{
	    if ($L_ord == $Lend[$k]) {
		$Lend = $Lend[$k];
		$Rend = $Rend[$k];
		$proba = $proba[$k];
		$str = $strand[$k];
	   	$type = $type[$k];
		($lg,$A,$D)=&calcAD($Lend,$Rend,$str);
		if (($type eq 'Term')||($type eq 'Sngl')) {$D="";}
		if (($type eq 'Init')||($type eq 'Sngl')) {$A="";}
		$F = $frame[$k];
		&EcrireST($seq,$type,$str,$Lend,$Rend,$lg,$ph,$F,$A,$D,$proba);
	    }
	}
    }
}







