#!/usr/bin/perl -w 

open TXT1 , "<./target_8_taxon_species_remove_unwanted.txt" or die "can't open file1:$!";

open TXT2 , "<./ext_target_fasta.txt" or die "can't open file2:$!";

$out_file = "./target_fasta_8_taxon_species_remove_unwanted_test0512.txt";
system "rm $out_file";
open OUT , ">>./$out_file";


while(<TXT1>)
{

	chomp;
	
	if($_=~ /^(\S+)\t(\S+\t\S+\t(\S+).*\S+)/)
	{         

		$gi_1 = $1;
		$_ = $2;
#		$phylum = $3;
		s/\t/\;/g;
		s/\;(n\;)+/;/g;

		s/\;n$//g;
		s/\;$//g;
#		$taxo_name[$i] = $_."_"."$phylum";
		$gi_check{"$gi_1"} = $_;

	}



}


$k = 0;
$m = 0;
while(<TXT2>)
{
	chomp;
	
	if($_=~/^\>gi\|(\d+)\|.*/)
	{
		$gi = $1;
		$k = 1;	
		$m = 0;

	
			if(exists $gi_check{"$gi"})
			{
				$m = 1;
				$value = $gi_check{"$gi"};
				print OUT "\>$gi\t$value\n";

			}

			

	}else{$k = 0;}
	
	if($k == 0 && $m == 1)
	{
		print OUT "$_\n";
		

	}



}





