#!/Perl/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);
$pairs=1;
if($list_elm[0]=~/primer_pairs_count/){
$primer_pairs_count=$list_elm[1];
}
while($pairs<$primer_pairs_count+1){
if($list_elm[0]=~/F_$pairs/){
$ORFF[$pairs]=$list_elm[1];
print "F_$pairs : $ORFF[$pairs]\n";
}
if($list_elm[0]=~/P_$pairs/){
$ORFP[$pairs]=$list_elm[1];
print "P_$pairs : $ORFP[$pairs]\n";
}
if($list_elm[0]=~/R_$pairs/){
$rev_seq= reverse ($list_elm[1]);
$rev_seq=~tr/[ATCGatcg]/[TAGCtagc]/;
$ORFR[$pairs]=$rev_seq;
print "R_$pairs : $ORFR[$pairs]\n";
}
$pairs=$pairs+1;
}
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];
}
if($list_elm[0]=~/Species_names/){
shift @list_elm;
@Species_names=@list_elm;
}
if($list_elm[0]=~/Merger_acquisition_bases/){
shift @list_elm;
@Merger_acquisition_bases=@list_elm;
}
}
close DATA;
}
$location_dir=getcwd;
$location_dir=~s/\//\\/;
@file = glob("$location_dir\\database\\*.fa");
$dirpath="$location_dir\\matched";
mkdir ($dirpath);
foreach $file(@file){
$fa=Bio::SeqIO->new(-file=>$file,-format=>'fasta');
print " successful import $file\n";
$c=0;
$e=1;
while($seq_obj=$fa->next_seq){
$id=$seq_obj->id;
$seq=$seq_obj->seq;
$seq_description=$seq_obj->desc;
$OF=1;
$OR=1;
$OP=1;
$NF2=1;
$NR2=1;
$NP2=1;
if($seq =~ /$ORFF[1]/)
{$OF=0;}
if($seq =~ /$ORFR[1]/)
{$OR=0;}
if($seq =~ /$ORFP[1]/)
{$OP=0;}
if($seq =~ /$ORFF[2]/)
{$NF2=0;}
if($seq =~ /$ORFR[2]/)
{$NR2=0;}
if($seq =~ /$ORFP[2]/)
{$NP2=0;}
$O=$OF+$OR+$OP;
$N=$NF2+$NR2+$NP2;
print "$file $id $O $N\n";
$files=$file;
$files=~s/\://g;
$num=$files=~s/\\/_/g;
$files=~s/\.fa//;
@files=split("_",$files);
if($O+$N==0){
open WH, ">>$dirpath\\$files[$num].matched";
print WH ">$id $seq_description\n";
print WH "$seq\n";
close WH;
}elsif($O>0)
{
if($N>0){
open WH, ">>$dirpath\\$files[$num].unmatched";
print WH ">$id $seq_description\n";
print WH "$seq\n";
close WH;
}else{
open WH, ">>$dirpath\\$files[$num]\_Primer1.matched";
print WH ">$id $seq_description\n";
print WH "$seq\n";
close WH;
}
}else{
open WH, ">>$dirpath\\$files[$num]\_Primer2.matched";
print WH ">$id $seq_description\n";
print WH "$seq\n";
close WH;
}
}
}