#! /usr/bin/perl -w

##### Program description #######
#
# Title:     bp_ex4.pl
# Author(s): Mattias Ohlsson
# Description:
#   A small Perl program that parses the result of a blast run. The 
#   Bio::Search and Bio::SearchIO is used.
#
##################################

use strict; 
use Bio::SearchIO; 

my $in = new Bio::SearchIO(-fh => \*STDIN, -format => 'blast');

# We only have one report!
my $result = $in->next_result();

while ( my $hit = $result->next_hit() ) { 
    while ( my $hsp = $hit->next_hsp ) { 
	if ( $hsp->percent_identity >= 80 && $hsp->percent_identity <= 85 ) {
	    
	    print "======= ID: ", $hit->name, "=======\n";
	    printf "=> Identities: %5.2f%%\n", $hsp->percent_identity;

	    my $q = $hsp->query_string; 
	    my @qarr = ($q =~ /(.{1,60})/g);
	    my $sQ = $hsp->start('query');
	    
	    my $s = $hsp->hit_string;
	    my @sarr = ($s =~ /(.{1,60})/g);
	    my $sS = $hsp->start('subject');

	    my $hom = $hsp->homology_string;
	    my @homarr = ($hom =~ /(.{1,60})/g);
	    
	    for ( my $i = 0; $i < scalar @qarr; $i++ ) {
		printf "Query: %4d $qarr[$i] %4d\n", $sQ, $sQ+length($qarr[$i])-1;
		printf "       %4s $homarr[$i]\n", '';
		printf "Subj:  %4d $sarr[$i] %4d\n\n", $sS, $sS+length($sarr[$i])-1;
	    }
	} 
    } 
} 
