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

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

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 %hash3;
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;
	$hash3{$data[0]}=$data[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];
}

open(IN2,"99.00hotmut.txt");#CAX52685	ybdZ	leading	67	4	0	1_1_1_1_
my %hash2;
while(my $line=<IN2>){
	chomp $line;
	my @data=split("\t",$line);
	my @data2=split("_",$data[6]);
	if(exists $hash{$data[1]}){
		print OUT $data[0]."\t".$data[1]."\t".$data[2]."\t".$data[3]."\t".($hash{$data[1]}+scalar(@data2))."\t".$hash{$data[1]}."\t".scalar(@data2)."\t";
		$hash2{$data[1]}=1;
		if(exists $avble{$data[0]}){
			print OUT $avble{$data[0]}."\t".$len{$data[0]}."\n";
		}
		else{
			print OUT "-1\t".$len{$data[0]}."\n";
		}
	}
	else{
		print OUT $data[0]."\t".$data[1]."\t".$data[2]."\t".$data[3]."\t".scalar(@data2)."\t0\t".scalar(@data2)."\t";
		if(exists $avble{$data[0]}){
			print OUT $avble{$data[0]}."\t".$len{$data[0]}."\n";
		}
		else{
			print OUT "-1\t".$len{$data[0]}."\n";
		}
	}
}

foreach my $gene (keys %hash){
	if(!exists $hash2{$gene}){
		print $gene."\t".$hash3{$gene}."\n";
	}
}	
