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

open(OUT,">99.01hotmut.txt");
print OUT "168name\tname\tleadorlag\tgenelen\talainlen\tavisite\tmutnum\thotnum\n";
open(LEN168,"Bacillus_subtilis_subsp_subtilis_str_168.GCA_000009045.1.17.pep.all.fa");
my %len;
my $seqhu=new Bio::SeqIO(-file=>"Bacillus_subtilis_subsp_subtilis_str_168.GCA_000009045.1.17.pep.all.fa",-format=>"fasta");
while(my $this_seq=$seqhu->next_seq){
	$len{$this_seq->id}=$this_seq->length;
}

open(IN,"99.00nature-s2.txt");
my %hash;
my $line0=<IN>;
while(my $line=<IN>){
	chomp $line;
	my @data=split("\t",$line);#spoIIIAF	Leading	120 (H -> N;R -> H);180(S -> L; T -> S)
	my @data2=split('\(',$data[2]);
	$hash{$data[0]}=scalar(@data2)-1;
}

open(IN1,"99.00avalible_sites.txt");
my %avble;
while(my $line=<IN1>){
	chomp $line;
	my @data=split("\t",$line);#CAX52685 1
	$avble{$data[0]}=$data[1];
}

my $i=0;
open(IN2,"99.00hotmut.txt");#CAX52685	ybdZ	leading	67	4
while(my $line=<IN2>){
	chomp $line;
	my @data=split("\t",$line);	
	if($len{$data[0]}>=200){
		print OUT $data[0]."\t".$data[1]."\t".$data[2]."\t".$len{$data[0]}."\t".$data[3]."\t";
		if(exists $hash{$data[1]} and exists $avble{$data[0]}){
			print OUT $avble{$data[0]}."\t".($hash{$data[1]}+$data[4])."\t".$hash{$data[1]}."\n";
			$i+=1;
		}
		if(exists $hash{$data[1]} and !exists $avble{$data[0]}){
			print OUT $data[4]."\t".($hash{$data[1]}+$data[4])."\t".$hash{$data[1]}."\n";
		}
		if(!exists $hash{$data[1]} and exists $avble{$data[0]}){
			print OUT $avble{$data[0]}."\t".$data[4]."\t"."0"."\n";
			$i+=1;
		}
		if(!exists $hash{$data[1]} and !exists $avble{$data[0]}){
			print OUT $data[4]."\t".$data[4]."\t"."0"."\n";
		}
	}
}
print $i."\n";

