This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Split and store fasta sequences in a specific directory (Perl)

Hello all, I would like to know whether there is a way to split a multi fasta file into several individual files and store all of them in one specific folder using IO::File, IO::File::WithPath; and Bio::SeqIO from Perl. I was trying to do it and I achieved split the multi fasta file into several files according to the sequences number, but I didn't get store all of them in a specific folder. All of them are stored directly at the path where I run the script. Am I doing something wrong with the command lines or maybe I need to use another module?

use IO::File::WithPath;
use Bio::SeqIO;

my $io = IO::File->new('path_new_folder_to_store_fasta_sequences', '>:utf8');
my $infile = "path_fasta_file";

my $seq_in = Bio::SeqIO->new( -file   => $infile, -format => "fasta", );

while (my $inseq = $seq_in->next_seq) {

my $id = $inseq->id();
my $seq = $inseq->seq();

$io  = IO::File->new(">$id.fasta");
print $io ">$id\n$seq\n";

}

Thanks in advance!

fasta io_file bio_seqio perl

1 answer

You define your outputfile with this line:

$io  = IO::File->new(">$id.fasta");

Add your folder name right before the filename should work.

fin swimmer

Thank you very much. A simple and effective solution

Log in to answer this question.