#! /usr/bin/perl -w

# script pour parser les resultats lassap contre la swall
# pour ajouter dans le champ product du fichier de resultat 
# unique AMIGene les resultats de lassap

my $amigene;
my $lsp;
my $step = 3 ;
my $sub ;
my $query ;
my $nbl = 0 ;
my $aa;
my $identity;




if ( $#ARGV > 2 || $#ARGV < 1)  {
	print "\nUsage:\t $0 <lsp results file> <unique AMIGene file>\n\n";
	exit (0);
}
else 
{
	$lsp = $ARGV[0] ;
	$amigene = $ARGV[1] ;
}

#print  STDERR "\nArguments Fetched with : \n Lassap file : $lsp\n Unique AMIGene : $amigene\n";


open(F,$lsp) or die "Error can't open file : $lsp\n";

while (<F>)
{
	$line = $_ ;
		#print ($nbl % 6 ) ;
		#print " ".$nbl.":".$line;
		
		if ( ($nbl % 6)  == 1  ) 
		{
			$sub = "Similar to ";
			$sub .= parseElement(\$line,\"DE");
			$sub .= " from ";
			$sub .= parseElement(\$line,\"OS");
			$sub .= "(";
			$sub .= parseElement(\$line,\"AC");
			$sub .= "), (";
			$sub .= parseElement(\$line,\"");
			$sub .= ")";
		}
		elsif ( ($nbl % 6)  == 3)
		{
			$query = lc(parseElement(\$line,\""));
		}
		elsif ( ($nbl % 6) == 5)  
		{
			if ($line =~ /L= (\d+) Id= (\d+\.\d+)/) 
			{
				$aa = $1;
				$identity = $2 ;
			}
		
			if ($line =~ /LD= (\d+)[\t|\s| ]LQ= (\d+)/)
			{
				#print "LENGTH ".$1 . " " . $2 . "\n";	
				$l1 = $1 ;
				$l2 = $2 ;
				if ($l1 > $l2) { $min = $l2; }
				else { $min = $l1 ;}
			#	print "MIN: ".$min."\n";
			}
			if ($line =~ /E= (\d+.\d+|\d+.\d+e\-\d+) /)
			{
				$eval = $1 ;
			#	print "EVAL = " .$eval."\n";
			}
			open (FA,$amigene) or die "Error can't open file : $amigene\n";
			while (<FA>)
			{
				$tmp = lc($_);
				if ($tmp =~ /$query/)
				{
					$entrie = $_ ;
					last ;
				}
			}
			close (FA);
			#print "THREESHOLD: ".( $aa  / $min )."\n";
			$Eval = sprintf("%.20f",$eval);
			# Eval seuil 10-3, et rapport alignement/longueur de la plus petite >= 0.4
			if ( (( $aa  / $min ) >= 0.4) && ($Eval < 0.001) ) 
			{
				$sub .= " \%identity = ".$identity." on ".$aa." aa ";
				&printEntrie($sub,$entrie);
			}
			else 
			{
				$sub =~ s/Similar.*$/NO SIGNIFICANT DATABANK MATCH/;
				$aa = "";
				$identity = "";
				&printEntrie($sub,$entrie);
			}
		}
		$nbl++;
}

close F ;


sub printEntrie 
{
	my ($s,$e) = @_ ;
	$e =~ s/NULL\$UNIQUE_AMIGene/$s\$UNIQUE_AMIGene/ ;
	print $e ;
}
sub parseElement
{
	local (*s,*e) = @_ ;
	my $i = 0 ;
	my $f = "" ;
	my @l ;
	my @h ;
	@l = split //, $s ;
	if ($e eq "") 
	{	
		while ($i < $#l && $l[$i] ne '#' && $l[$i] ne ' ' && $l[$i] ne ';' )
		{
			$f .= $l[$i];
			$i++;
		}	
		$f =~ s/ //g;
	}
	else  
	{
		@h = split //, $e ;
		while ($i < $#l )
		{
			if ($l[$i-1] eq '#' && $l[$i] eq $h[0] && $l[$i+1] eq $h[1])
			{
				$i += 2 ;
				while ($i < $#l && $l[$i] ne '#' && $l[$i] ne '(' && $l[$i] ne ';' )
				{
					$f .= $l[$i];
					$i++;
				} 
				$f =~ s/^ //;
				return $f ;
			}
			else
			{
				$i++;
			}
		}
	}
	return $f; 
}


sub printStruct 
{
	local (*s) = @_ ;
	print $s{"NA"}."|".$s{"AC"}." ".$s{"DE"}." ".$s{"OS"}." ".$s{"KW"}." "."\n";
}

sub initStruct 
{
	local (*s) = @_ ;
	$s{"DE"} = "" ;
	$s{"AC"} = "" ;
	$s{"OS"} = "" ;
	$s{"KW"} = "" ;
	$s{"NA"} = "" ;
	return \%s ;
}
