#!/usr/bin/perl

# OUTFORMAT INFO SEQUENCES:
# Query= NC_000021.9 Homo sapiens chromosome 21, GRCh38.p12 Primary Assembly
# Length=46709983
#                                                                       Score     E
# Sequences producing significant alignments:                          (Bits)  Value
# NC_000084.6  Mus musculus strain C57BL/6J chromosome 18, GRCm38.p...  2675    0.0  
# 
# >NC_000084.6 Mus musculus strain C57BL/6J chromosome 18, GRCm38.p4 C57BL/6J
# Length=90702639

# OUTFORMAT alignments:
########################################################################################
# Score = 2675 bits (1448),  Expect = 0.0							# DESCRIPTION
# Identities = 3003/3763 (80%), Gaps = 70/3763 (2%)
# Strand=Plus/Minus
#
#Query  41190512  TCCTCGTTTCTGCAGCTGGTCACGTGGTTGTAGCCGGTATTGATGACTGCCTTCTTCTAC  41190571	# ACTUAL ALIGNMENT (more than one)
#                 |||||||||||||| ||||| ||||||   ||   |||||| || ||| ||||| |||||
#Sbjct  77270174  TCCTCGTTTCTGCAACTGGTTACGTGGCCTTAATTGGTATTTATAACTACCTTCCTCTAC  77270115
#
########################################################################################

# GLOBAL VARIABLES:
my $counter = 0;	# current alignment identifier
my %hashresults = ();
my %hashalign = ();
my $key="";
# keep track of sequences identifiers
my $current_query = "";
my $current_target = "";
# keep track of coordinates
my $qrystart = 0;
my $qryend = 0;
my $targetstart = 0;
my $targetend = 0;
my $strand = 0;
# STATUS VARIABLE
my $isaln=0;
# ALIGNMENT VARIABLES
my $qryhsp="";
my $trghsp="";


# get the file name
$file=shift;
# open the file 
open in,$file;

# read the file line by line 
while(<in>){
chop $_;		# remove newline
my @vals = split (/\s+/, $_);	# split current line at one or more (continuous) blank characters (space, tab, ...), save the resuling array into the vals @variable

# START PARSING ...
######## SECTION 1 : sequence identifiers ################################# 
if($vals[1] eq 'Query=' ){$current_query=$vals[2]; $isaln=0;}
if(substr($vals[0],0,1) eq '>'){$current_target=substr($vals[0],1); $isaln=0;}

####### SECTION 2 : alignments ############################################

### SECTION 2.1 : DESCRIPTION


if($vals[1] eq 'Score'){
	if($isaln){
		$hashalign{$key}{'Query'}=$qryhsp;
		$qryhsp="";
		$hashalign{$key}{'Sbjct'}=$trghsp;
		$trghsp="";
		
		# PRINT ALIGNMENT
		print "$key Score:$hashresults{$key}{'Score'} Exp:$hashresults{$key}{'Expect'} Id:$hashresults{$key}{'Identities'} Gaps:$hashresults{$key}{'Gaps'} Str:$hashresults{$key}{'Strand'} $qrystart:$qryend $targetstart:$targetend\t$hashalign{$key}{'Query'}\t$hashalign{$key}{'Sbjct'}\n";	
		$qrystart=0;
		$qryend=0;
		$targetstart=0;
		$targetend=0;

		# RESET STATUS
		$isaln=0;
	}

	$counter++;
	$key="aln_".$counter;
	$hashresults{$key}{'Score'}=$vals[3];
	$hashresults{$key}{'Expect'}=$vals[8];



}
if($vals[1] eq 'Identities'){
	$hashresults{$key}{'Identities'}=$vals[3];
	$hashresults{$key}{'Gaps'}=$vals[7];
}
if(substr($vals[1],0,6) eq 'Strand'){
	$hashresults{$key}{'Strand'}=substr($vals[1], 7);
}



if($vals[0] eq 'Query'){
	if($qrystart==0){$qrystart=$vals[1];}
	$qryend=$vals[3];
	$qryhsp = $qryhsp.$vals[2];
	$isaln=1;
}
if($vals[0] eq 'Sbjct'){
	$trghsp = $trghsp.$vals[2];
	if($targetstart==0){$targetstart=$vals[1]};
	$tergetend=$vals[2];
}



}
# close the input file 
close in;
