#!/usr/bin/perl -w

#analyze BLASTALL output ran with -m8 or -m9 option to extract only 1st line for each query GI
#Sun 12 Dec 2004

# this is a modification of the extract_lines.pl.  This program adds a column 
# to the output table that gives the bitscore per aligned residue

# The program works on a file created by blastall in ht em8 or m9 option
# the filename is handed to the program on the command line.  


unless (@ARGV==1) {
    die "usage: extract_lines.pl filename";
}

$filename=$ARGV[0];

open(IN, "< $filename") or die "cannot open $filename: $!";

$outfile=$filename.".top";

open(OUT, "> $outfile") or die "cannot open $outfile: $!";

$old="";
# print header
print OUT "#Query id \tSubject id \t% identity \talignment length \tnumber of mismatches \tnumber of gap openings \tposition of query start \tposition of query end \tposition of subject start \tposition of subject end \te-value of a hit \tbit score of a hit \tbit score per aligned  position\n";
#
#added bitscore/aligned position
#
while(defined($line=<IN>)){

    if($line=~"#"){
	#skip the line, since it is a comment line
	#copy line to OUT
	#print OUT "$line";
	#if you want to keep comment lines un comment the last line
    }
    else{
    	chomp ($line);
	@parts=split(/\t/, $line);
	if ($parts[0] eq $old)
	{
	#skip line;
	#print "$old\n";
	}
	else {
#next line calculates bitscoredivided by alignment length;
	$ratio=$parts[11]/$parts[3];
	print OUT "$line\t$ratio\n";
	$old=$parts[0];
	}
    }

}

close(IN);
close(OUT);
exit;


