#!/usr/bin/perl
use Bio::SeqIO;
use Bio::Seq;
use Tie::File;
use Cwd;
$location_dir=getcwd;
$location_dir=~s/\//\\/;
@file = glob("$location_dir\\*.filter");
foreach $file(@file){
open(DATA,"<$file") or die "Can't open $file";
@list = <DATA>;
foreach $list (@list){
@list_elm=split(" ",$list);
if($list_elm[0]=~/Seq_N/){
$Seq_N=$list_elm[1];
}
if($list_elm[0]=~/Seq_min_length/){
$Seq_min_length=$list_elm[1];
}
if($list_elm[0]=~/Seq_max_length/){
$Seq_max_length=$list_elm[1];
}
}
close DATA;
}
$location_dir=getcwd;
$location_dir=~s/\//\\/;
@file = glob("$location_dir\\database\\*.fa");
$dirpath="$location_dir\\filtered";
mkdir ($dirpath);
foreach $file(@file){
$fa=Bio::SeqIO->new(-file=>$file,-format=>'fasta');
print " successful import $file\n";
while($seq_obj=$fa->next_seq){
$id=$seq_obj->id;
$seq=$seq_obj->seq;
$length=$seq_obj->length;
$seq_length=@seq;
$desc=$seq_obj->desc;
$seqs=$seq;
$N_count=$seq=~s/N//i;
$N_ratio=$N_count/$length;
if($N_ratio<=$Seq_N){
if($Seq_min_length<=$length){
if($length<=$Seq_max_length){
open WH, ">>$dirpath\\database.filtered";
print WH ">$id $seq_desc\n";
print WH "$seq\n";
close WH;
}
}
}
}
}