#!/usr/bin/perl
# parse blast output

use Bio::SearchIO;

$usage = "Usage: $0 blastresultfile outputfile.\n";
$file = shift or die $usage;
$outfile = shift or die $usage;

open(OUTF, ">$outfile");

print OUTF "Query\tHit Chr\tHit Start\tHit End\tLength\tHit Strand\tPossibility";
print OUTF "\tHSP Identity\n";

my $in = new Bio::SearchIO(-format => 'blast', -file=> $file);
my ($chr,$start,$end);
while( my $result = $in->next_result ) {
	my $qid = $result->query_name;
    while( my $hit = $result->next_hit ) {
		$hitname = $hit->name;
		$len = $hit->length;
		$strand = getStrand($hit->strand);
		if ( $hitname=~/NOT:([A-P])_(\d+)\-(\d+)/ ) {
    		$chr    = ord($1) - 64;
        	$start  = $2;
        	$end    = $3;

	   		if ( defined $uniq{"$qid:$hitname"} ) { next; }
       		$uniq{"$qid:$hitname"}=1;

	   		print "$qid:$hitname\n";
       		my $hsp = $hit->next_hsp; # get the first hsp only 
	   		#$expect=$hsp->expect;
	   		$pvalue=$hit->significance;
	   		$total=$hsp->length('total');
	   		$identnum=$hsp->num_identical;
       		$identfrac=$hsp->frac_identical;
       		$identpct = int($identfrac*100);
	   		print OUTF "$qid\t$chr\t$start\t$end\t$len\t$strand\t$pvalue";
	   		print OUTF "\t$identnum/$total($identpct%)\n";
		}
	} 
}
close OUTF;


sub getStrand {
  my $strand=shift;
  
  if ( $strand == 1 ) {
     $strand = "W";
  } else {
     $strand = "C";
  }
  return $strand;
} 

