#!/usr/bin/perl
use strict;
use warnings;
use Text::Trim qw(trim);
#Usage perl extractSeqbyID.pl ids.txt seq.fasta Result.fasta
$ARGV[2] or die "use extractSeqbyID.pl LIST FASTA OUT\n";
my $list = shift @ARGV;
my $fasta = shift @ARGV;
my $out = shift @ARGV;
my %select;
open LINE, "$list" or die;
while (<LINE>) {
chomp;
next if /^\s*$/;
s/>//g;
my @ids=split (/\t/, $_);
$select{$ids[0]} = 1;
}
my $size = keys %select;
print "Total Ids $size\n";
close LINE;
$/ = "\n>";
open OUT, ">$out" or die;
open FILE, "$fasta" or die;
while (<FILE>) {
trim($_);
s/>//g;
my ($id) = split (/\n/, $_);
#my @i=split (/\s/, $id); # To avoid >flattened_line_10751 circular cases
print OUT ">$_" if (defined $select{$id});
}
close FILE;
close OUT;