This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Convert A Plain Squence In A Perl String Variable To Fasta Format

Hi,

I have a couple of plain sequences (without any formatting and annotations) saved in sqlite3 database. After reading them into Perl strings, I have to convert them into fasta format using Perl. I see methods in Bio::SeqIO to convert one file format to another. But my sequences are in perl string variables and not in files.

Thanks

bioperl perl fasta

2 answers

I don't see why a library is necessarily helpful here? Fasta format is super simple, so all you'd need to do is come up with some IDs, then print something like: print ">$NewId\n$sequence\n";

I'd be interested to know why people use libraries in this context, as I might be missing something.

I cannot see any reason why a library would be useful. This solution is simple, fast and scalable.

Quite right; printing out Fasta is simple without libraries (you might want to include a line wrap since sequence lines are in principle not supposed to exceed 80 characters). I just explained the Bioperl solution because the OP was using Bio::SeqIO and seemed confused by it.

A simple line wrapping can be obtained using unpack:

print '>', $seqId, "\n"; foreach $seqLine (unpack('(a[60])*', $seqStr)) { print $seqLine, "\n"; }

The Bio::SeqIO HOWTO might be helpful.

To create sequences using Bioperl requires Bio::Seq in addition to Bio::SeqIO.

Assuming that your sequence string is $string and you want to write to file myFile.fa:

#!/usr/bin/perl -w

use strict;
use Bio::SeqIO;
use Bio::Seq;

my $string   = "acaaaatcttgagagatt";
my $seq      = Bio::Seq->new(-display_id => "mySeq1", -seq => $string);
my $outseq   = Bio::SeqIO->new(-format => "fasta", -file => ">myFile.fa");

$outseq->write_seq($seq);

Obviously without annotation, you will have to devise a sensible method to generate sequence IDs for the Fasta header.

Log in to answer this question.