#!/usr/bin/perl
use Bio::SeqIO;
use Bio::Seq;
use Tie::File;
use Cwd;
my $location_dir=getcwd;
$location_dir=~s/\//\\/;
my @file = glob("$location_dir\\*.filter");
foreach my $file(@file){
open(DATA,"<$file") or die "Can't open $file";
my @list = <DATA>;
foreach my $list (@list){
my @list_elm=split(" ",$list);
if($list_elm[0]=~/Species_names/i){
shift @list_elm;
my @Species_names=@list_elm;
my $Max_pro =@list_elm;
for (my $i = 1; $i <= $Max_pro; ++$i){
my $child_pro = fork(); #创建进程
if (!defined($child_pro)){
die "launching child $child_pro\n";
}
if ($child_pro == 0 ){
do_child($i,$$,$Species_names[$i-1]);
exit 0;
}
}
exit 0;
}
}
close DATA;
}
sub do_child{
my ($i,$id,$Species_names) = @_;
print "process $i ===>>> $id $Species_names\n";
$location_dir=getcwd;
$location_dir=~s/\//\\/;
my $dirpath="$location_dir\\classified";
mkdir ($dirpath);
my @files = glob("$location_dir\\matched\\*.unmatched");
foreach my $files(@files){
my $fa=Bio::SeqIO->new(-file=>$files,-format=>'fasta');
print " successful import $file\n";
while(my $seq_obj=$fa->next_seq){
my $id=$seq_obj->id;
my $seq=$seq_obj->seq;
my $desc=$seq_obj->desc;
if($desc =~ /$Species_names/i){
open WH,">>$dirpath\\$Species_names.fa";
print WH ">$id $desc\n";
print WH "$seq\n";
}
}
}
}