use strict;
use Bio::Seq;
use Bio::SeqIO;
use Bio::AlignIO;

open(OUT,">99.03hotmut.txt");

open(HOT,"99.02hottype.txt");
open(SEQ,"99.01orth_pro_seq.txt");

my @spes=("w23","tu","ro","bsn5","168");
my @abcde=("a","b","c","d","e");

my %hot; 
while(my $line=<HOT>){
	chomp $line;
	my @data=split("\t",$line);
	$hot{$data[2]}=$data[3];
}

my %gene;
while(my $line=<SEQ>){
	chomp $line;
	my @data=split("\t",$line);
	#if($data[0] eq "CAB12455"){
	#	1;
	my $name1=<SEQ>;my $seq1=<SEQ>;my $name2=<SEQ>;my $seq2=<SEQ>;my $name3=<SEQ>;my $seq3=<SEQ>;my $name4=<SEQ>;my $seq4=<SEQ>;my $name5=<SEQ>;my $seq5=<SEQ>;
	chomp $name1;chomp $seq1;chomp $name2;chomp $seq2;chomp $name3;chomp $seq3;chomp $name4;chomp $seq4;chomp $name5;chomp $seq5;
	my $temfile=$data[0].".txt";
	my $alnfile=$data[0].".aln";
	open(TEM,">".$temfile);
	print TEM $name1."\n".$seq1."\n".$name2."\n".$seq2."\n".$name3."\n".$seq3."\n".$name4."\n".$seq4."\n".$name5."\n".$seq5."\n";
	close TEM;
	system<clustalw $temfile>;
	my $alnfilein=Bio::AlignIO->new('-file'=>$alnfile);
	my $aln=$alnfilein->next_aln();
	$aln=$aln->remove_gaps;  
	my @match=split("",$aln->match_line);
	my @posi;
	my $nummat=0;
	my $i=1;#frist and last aa is not accounted	
	while($i<(scalar(@match)-1)){
		if ($match[$i] ne "*"){
			push(@posi,$i+1);
		}
		else{
			$nummat+=1;
		}
		$i+=1;
	}
	
	my $numhot=0;
	my $nummut=0;
	my $detail="";
	foreach my $tposi(@posi){
		my $aln2=$aln->slice($tposi,$tposi);
		my @tnts;
		my %tnts;
		my %tnts2;
		my $abcde;
		foreach my $tspe (@spes){
			push(@tnts,$aln2->get_seq_by_id($tspe)->seq);
			$tnts{$aln2->get_seq_by_id($tspe)->seq}+=1;
		}
		foreach my $tnt (keys %tnts){
			$tnts2{$tnts{$tnt}}->{$tnt}=1;
		}
		my $j=0;
		foreach my $tnum (sort {$a<=>$b} keys %tnts2){
			my $hashdata=$tnts2{$tnum};
			foreach my $tnt (keys %$hashdata){
				$tnts{$tnt}=@abcde[$j];
				$j+=1;
			}
		}		
		foreach my $tnt (@tnts){
			$abcde.=$tnts{$tnt};
		}
		$detail.=$hot{$abcde}."_";
		$nummut+=$hot{$abcde};
		if($hot{$abcde}>1){
			$numhot+=1;
		}
	}
	print OUT $line."\t".($nummut+$nummat)."\t".$nummut."\t".$numhot."\t".$detail."\n";
	#}
}

