[Bioperl-l] Newbie question: fasta output to string

gert thijs gert.thijs@esat.kuleuven.ac.be
Mon, 28 May 2001 19:09:40 +0200


Marc,

You can use 'IO::Scalar' to tie a file handle to a string.

e.g.
my $new_seq ='';
tie *OUT, 'IO::Scalar', \$new_seq;
$out = Bio::SeqIO->new(-fh => \*OUT, '-format' => 'Genbank');
$out->write_seq($seq); # $seq is a Bio::Seq or Bio::RichSeq object
$out->close;

Gert Thijs


Marc Logghe wrote:
> 
> Thanks Ewan, for your fast response, but I am afraid that was not exactly
> what I meant.
> For the moment I am reformatting sequences in the exact way you suggest, but
> the thing is you always have to deal with these file handles. I had
> something else in mind, like the 'layout'-method of the PreSeq.pm module.
> The thing is I need the formatted sequence (fasta, genbank,... ) in a string
> so that I can ad some highlighting by inserting some HTML tags before the
> output is sent to the browser. What I have done up to now, is something like
> this:
> # reformat sequence
> open (OUT, ">$outfile");
> $out = Bio::SeqIO->new(-fh => \*OUT, '-format' => 'Genbank');
> $out->write_seq($seq); # $seq is a Bio::Seq or Bio::RichSeq object
> close OUT;
> # read in Genbank sequence again
> local $/ = undef;
> open (FILE, "<$outfile") or die("Cannot open $outfile: $!\n");
> my $genbank = (<FILE>);
> close (FILE);
> # add some tags
> $genbank = colour($genbank, $pre, $post, $type, $label);
> 
> You see what I mean, you first have to write a file and than open it again
> to be able to catch your string
> instead of something like:
> my $genbank = $seq->layout('fasta');
> As far as I know, the PreSeq.pm module was the 'beta' version of the Seq.pm
> module, where the layout method apparently has been removed.
> Marc
>