#!/usr/bin/perl

$file=shift;

%sequences=();


open in,$file;
while(<in>){
chop $_;
if(length($_)==0){next;}	# salta le righe vuote
@vals=split(/\s+/,$_);
$sequences{$vals[0]} = $sequences{$vals[0]}.$vals[$#vals];	# costruzione stringhe allineamento (1 per ogni specie)
}

foreach $a (sort(keys(%sequences))){
	print "$a\t".length($sequences{$a})."\n";
}



$seq1 = $sequences{"Homo"};
$seq2 = $sequences{"Mus"};
@seq1split=split(//,$seq1);
@seq2split=split(//,$seq2);


####### PARSING #################################

$curhuman="";
$curmus="";

$state=0;

$curalnlen=0;
$alncounter=0;


for($i=0;$i<$#seq1split;$i++){
	if($seq2split[$i] eq "."){
		if($state==1){
			
			$alncounter++;

			print "ALN_$alncounter\t$curalnlen\t$curhuman\t$curmus\n";
			$curhuman="";
			$curmus="";
			$state=0;

			$curalnlen=0;
		}else{	
			next;
		}
	}else{

		$curalnlen++;

		$curhuman=$curhuman.$seq1split[$i];
		$curmus=$curmus.$seq2split[$i];
		$state=1;
	}

}

# PRINT LAST UNPROCESSED ALIGNMENT IF ANY

if($seq2split[$#seq2split] ne "."){
$alncounter++;
print "ALN_$alncounter\t$curalnlen\t$curhuman\t$curmus\n";
}
