#!/usr/local/bin/perl

###############################################################################
# Perl script
# Author C.Mathe 09/10/98,modified 20/10/98, modified 5/05/99 to read "H"
# this script extract data from a NetGene2 output file and write them in a standard file format
###############################################################################

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

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

sub readNG2{
open(NG2,"<@_") || die "Unable to open @_.\n";
$splice=0;

while(<NG2>)
{
    $lg=$F=$type=$ph=$Rend=$Lend=$D=$A=$proba="";
    @word=split;
    if ($word[0] eq 'Branch') {close(NG2);}
    if ($splice==0){
	if ($word[0] eq 'Donor'){$splice=1;}
	if ($word[0] eq 'Acceptor'){$splice=2;}
    }
    
    if ($splice==1){
	if ($word[2] eq '+') {
	    
	    ($D,$ph,$str,$proba,$motif)=@word;
	    if ($#word==5) {$proba=$proba." \tH";}
	    ($Rend,$Lend,$A)=&calcEnd($D,$str);
	    &EcrireST($seq,$type,$str,$Lend,$Rend,$lg,$ph,$F,$A,$D,$proba);	    
#&EcrireST;
	}
	
	if ($word[3] eq '-') {
	    ($D,$R,$ph,$str,$proba,$motif)=@word;
	    if ($#word==6) {$proba=$proba." \tH";}
	    ($Rend,$Lend,$A)=&calcEnd($D,$str);	
	    &EcrireST($seq,$type,$str,$Lend,$Rend,$lg,$ph,$F,$A,$D,$proba);    
#&EcrireST;
	}

	if (($word[0] eq "")||($word[0] eq 'No')||($word[0] eq 'Branch')) {$splice=0;}
    }
    
    if ($splice==2){
	if ($word[2] eq '+') {
	    ($A,$ph,$str,$proba,$motif)=@word;
	    if ($#word==5) {$proba=$proba." \tH";}
	    ($Rend,$Lend,$D)=&calcBeg($A,$str);
	    &EcrireST($seq,$type,$str,$Lend,$Rend,$lg,$ph,$F,$A,$D,$proba);
	}
	
	if ($word[3] eq '-') {
	    ($A,$R,$ph,$str,$proba,$motif)=@word;
	    if ($#word==6) {$proba=$proba." \tH";}
	    ($Rend,$Lend,$D)=&calcBeg($A,$str);
	    &EcrireST($seq,$type,$str,$Lend,$Rend,$lg,$ph,$F,$A,$D,$proba);
	    if (($word[0] eq "")||($word[0] eq 'No')||($word[0] eq 'Branch')) {$splice=0;}
	}

    }
}
print ST "\n";
close NG2;
}

sub readLS{
open(LS,"< $liste") || die "Unable to open $liste.\n";
$ST=&openST("netgene2");

while(<LS>)
#read the file containing all the NetGene2 output files
{
    ($seq,$Ltot)=split;   # read each sequence name and each corresponding length
    if (($seq eq "")||($Ltot eq "")) #check if the list is correctly formatted
    {
	die "!!! Incorrect input list !!!\n"&usage("NetGene2");}
    $file=$seq . "netgene2.txt";
    &readNG2($file);
    print ST "\n";
}
close LS;
}

sub createST{
@data=&Start("NetGene2");
if ($data[1]==0) { 
    $liste=$data[0];
    &readLS($liste);
    print "output file: $standart\n";
}
else 
{
    $Ltot=$data[1];
    $file=$data[0];
    @tmp=split('netgene2',$file);
    $seq=$tmp[0];
    $file2 =$file . "ST";
    open(ST,">$file2");
    print ST "Contig\tType\tStrand\tLend\tRend\tlength\tPhase\tFrame\tAcceptor\tDonor\tProba\n";
    &readNG2($file);
    print "output file $file2\n";
}
close ST;
}

&createST;





