#!/usr/bin/perl

# Script extract_last_briter.pl reads PSI-BLAST output for multiple iterations and extracts multiple alignment 
# for $niter (or the last iteration before $niter). Then the code for final alignment processing (prep_psiblastali)
# is launched, to get rid of columns with gaps in query and rows with ID>97%. 

my $niter = 5;
my $aln_exist = 0;

my $process_code = "./prep_psiblastali";

while (@ARGV and $ARGV[0] =~ /^-/) {

        $_ = shift;
        last if /^--$/;
        if (/^-i/) {$_ = shift; $infile = $_;}
        if (/^-j/) {$_ = shift; $niter = $_;}
        if (/^-o/) {$_ = shift; $outfile = $_;}
	
}

if($niter==0) { # option -j 0 runs psiblast till convergence; limit niter to 10 
	$niter=10;
}

my $ftmp = "$infile\.parse\.tmp";

# Get the last iteration from BLAST results
for ($iter=$niter; $iter>=1; $iter--) {
	PRINT_ITER($infile, $ftmp, $iter);
	if($aln_exist) {
		# Final processing of alignment
		system("$process_code -i $ftmp -o $outfile");
		last;
	} 
}

unless($aln_exist){
	warn "ERROR in reading PSI-BLAST output: output is not parsable. Please check the format of submitted query\n";
} else {
	print STDERR "PSI-BLAST output is successfully proccessed\n";
} 

# `rm -f $ftmp`;

# Print PSI-BLAST alignment for iteration #  $iteration
sub PRINT_ITER {
	
	my ($fileinput, $fileout, $iteration) = @_;	

	open (FFIN, "$fileinput");	
	open(FFOUT, ">$fileout");

	print FFOUT "\n";	
	my $flag =0;
	my $nprint= 0;

	if($iteration==1) { # first iteration; don't need to look for the round marker
			    # (also includes the case of blastpgp -j 1, where there is no round marker)
		$flag=1;
	}

	while (<FFIN>) {
	        if (/Results from round $iteration/) { $flag =1; }				
		if (/Sequences producing significant alignments\:/ && $flag==1) {$flag=2;}
	        if (/QUERY/ && ($flag == 2)){$flag=3;}
	        if ($flag==3){
	        	if ($_ =~ /Database:/ || $_ =~ /Searching/) {last;}
	        	print FFOUT $_;
			$nprint ++;
	        }
	}
	
	close (FFIN);
	close (FFOUT);

	if($nprint==0) { system("rm -f $fileout"); $aln_exist = 0; }
	else { $aln_exist = 1; }
}
