Bio::SeqIO(3pm)

πŸ“¦
version 1.7.8 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble

πŸŽ€ Bio::SeqIO πŸŽ€

okay bestie, welcome to the manual page for Bio::SeqIO(3pm) β€” she's a User Contributed Perl Documentation page and she's lowkey iconic. grab a snack, this is part one of two, and we're going in 🌈


✨ NAME ✨

Bio::SeqIO β€” your handler for all the SeqIO formats, fr fr πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so first thing first, here's the vibe:

use Bio::SeqIO;

$in  = Bio::SeqIO->new(-file => "inputfilename" ,
                       -format => 'Fasta');
$out = Bio::SeqIO->new(-file => ">outputfilename" ,
                       -format => 'EMBL');

while ( my $seq = $in->next_seq() ) {
    $out->write_seq($seq);
}

and then, to actually get at the sequence object, you use the standard Bio::Seq methods (go read Bio::Seq if you don't know what those are, no shame):

use Bio::SeqIO;

$in  = Bio::SeqIO->new(-file => "inputfilename" ,
                       -format => 'genbank');

while ( my $seq = $in->next_seq() ) {
   print "Sequence ",$seq->id, " first 10 bases ",
         $seq->subseq(1,10), "\n";
}

and now the part that makes people go 😭 β€” the SeqIO system does have a filehandle binding. most people find this a little confusing, but it does mean you can write the world's smallest reformatter:

use Bio::SeqIO;

$in  = Bio::SeqIO->newFh(-file => "inputfilename" ,
                         -format => 'Fasta');
$out = Bio::SeqIO->newFh(-format => 'EMBL');

# World's shortest Fasta<->EMBL format converter:
print $out $_ while <$in>;

πŸ’œ DESCRIPTION πŸ’œ

Bio::SeqIO is the handler module for the formats in the SeqIO set (eg, Bio::SeqIO::fasta). it's the officially sanctioned way of getting at the format objects, and most people should use it. periodt.

think of the Bio::SeqIO system like biological file handles 🧬. they're attached to filehandles with smart formatting rules (eg, genbank format, or EMBL format, or binary trace file format) and they can either read or write sequence objects (Bio::Seq objects β€” or more correctly, Bio::SeqI implementing objects, of which Bio::Seq is one such object). if you wanna know what to do with a Bio::Seq object, go read Bio::Seq.

the idea is that you request a stream object for a particular format. all the stream objects have a notion of an internal file that is read from or written to. a particular SeqIO object instance is configured for either input or output. a specific example of a stream object is the Bio::SeqIO::fasta object.

every stream object has these functions:

$stream->next_seq();

and

$stream->write_seq($seq);

as an added bonus 🎁, you can recover a filehandle that is tied to the SeqIO object, allowing you to use the standard <> and print operations to read and write sequence objects:

use Bio::SeqIO;

$stream = Bio::SeqIO->newFh(-format => 'Fasta',
                            -fh     => \*ARGV);
# read from standard input or the input filenames

while ( $seq = <$stream> ) {
    # do something with $seq
}

and

print $stream $seq; # when stream is in output mode

which makes the simplest ever reformatter:

#!/usr/bin/perl
use strict;
my $format1 = shift;
my $format2 = shift || die
   "Usage: reformat format1 format2 < input > output";

use Bio::SeqIO;

my $in  = Bio::SeqIO->newFh(-format => $format1, -fh => \*ARGV );
my $out = Bio::SeqIO->newFh(-format => $format2 );
# Note: you might want to quote -format to keep older
# perl's from complaining.

print $out $_ while <$in>;

🌸 CONSTRUCTORS 🌸

πŸ’… Bio::SeqIO->new()

$seqIO = Bio::SeqIO->new(-file   => 'seqs.fasta', -format => $format);
$seqIO = Bio::SeqIO->new(-fh     => \*FILEHANDLE, -format => $format);
$seqIO = Bio::SeqIO->new(-string => $string     , -format => $format);
$seqIO = Bio::SeqIO->new(-format => $format);

the new() class method constructs a new Bio::SeqIO object ✨. the returned object can be used to retrieve or print Seq objects. new() accepts the following parameters:

  • -file β€” a file path to be opened for reading or writing πŸ“‚. the usual Perl conventions apply:
'file'       # open file for reading
'>file'      # open file for writing
'>>file'     # open file for appending
'+<file'     # open file read/write
to read from or write to a piped command, open a filehandle and use the -fh option instead, bestie 🫢
  • -fh β€” you may use new() with a opened filehandle, provided as a glob reference. for example, to read from STDIN:
my $seqIO = Bio::SeqIO->new(-fh => \*STDIN);

a string filehandle is handy if you want to modify the output in the memory, before printing it out. this program reads in EMBL formatted entries from a file and prints them out in fasta format with some HTML tags:

use Bio::SeqIO;
use IO::String;
my $in = Bio::SeqIO->new(-file => "emblfile",
                         -format => 'EMBL');
while ( my $seq = $in->next_seq() ) {
    # the output handle is reset for every file
    my $stringio = IO::String->new($string);
    my $out = Bio::SeqIO->new(-fh => $stringio,
                              -format => 'fasta');
    # output goes into $string
    $out->write_seq($seq);
    # modify $string
    $string =~ s|(>)(\w+)|$1<font color="Red">$2</font>|g;
    # print into STDOUT
    print $string;
}

filehandles can also be used to read from or write to a piped command:

use Bio::SeqIO;
#convert .fastq.gz to .fasta
open my $zcat, 'zcat seq.fastq.gz |' or die $!;
my $in=Bio::SeqIO->new(-fh=>$zcat,
                       -format=>'fastq');
my $out=Bio::SeqIO->new(-file=>'>seq.fasta',
                        -format=>'fasta');
while (my $seq=$in->next_seq) {
    $out->write_seq($seq)
}
  • -string β€” a string to read the sequences from πŸ“œ. for example:
my $string = ">seq1\nACGCTAGCTAGC\n";
my $seqIO = Bio::SeqIO->new(-string => $string);
  • -format β€” specify the format of the file πŸ—‚οΈ. supported formats include fasta, genbank, embl, swiss (SwissProt), Entrez Gene and tracefile formats such as abi (ABI) and scf. there are many more, for a complete listing see the SeqIO HOWTO (http://bioperl.org/howtos/SeqIO_HOWTO.html).

if no format is specified and a filename is given then the module will attempt to deduce the format from the filename suffix πŸ‘€. if there is no suffix that Bioperl understands then it will attempt to guess the format based on file content. if this is unsuccessful then SeqIO will throw a fatal error πŸ’€.

the format name is case-insensitive: 'FASTA', 'Fasta' and 'fasta' are all valid. slay.

  • -alphabet β€” sets the alphabet ('dna', 'rna', or 'protein'). when the alphabet is set then Bioperl will not attempt to guess what the alphabet is. this may be important because Bioperl does not always guess correctly 😬.
  • -flush β€” by default, all files (or filehandles) opened for writing sequences will be flushed after each write_seq() (making the file immediately usable). if you do not need this facility and would like to marginally improve the efficiency of writing multiple sequences to the same file (or filehandle), pass the -flush option '0' or any other value that evaluates as defined but false:
my $gb = Bio::SeqIO->new(-file   => "<gball.gbk",
                         -format => "gb");
my $fa = Bio::SeqIO->new(-file   => ">gball.fa",
                         -format => "fasta",
                         -flush  => 0); # go as fast as we can!
while($seq = $gb->next_seq) { $fa->write_seq($seq) }
  • -seqfactory β€” provide a Bio::Factory::SequenceFactoryI object. see the sequence_factory() method.
  • -locfactory β€” provide a Bio::Factory::LocationFactoryI object. see the location_factory() method.
  • -objbuilder β€” provide a Bio::Factory::ObjectBuilderI object. see the object_builder() method.

πŸ¦‹ Bio::SeqIO->newFh()

$fh = Bio::SeqIO->newFh(-fh => \*FILEHANDLE, -format=>$format);
$fh = Bio::SeqIO->newFh(-format => $format);
# etc.

this constructor behaves like new(), but returns a tied filehandle rather than a Bio::SeqIO object πŸͺ„. you can read sequences from this object using the familiar <> operator, and write to it using print(). the usual array and $_ semantics work. for example, you can read all sequence objects into an array like this:

@sequences = <$fh>;
other operations, such as read(), sysread(), write(), close(), and printf() are not supported. don't come for her about it πŸ™ˆ

⭐ OBJECT METHODS ⭐

see below for more detailed summaries. the main methods are:

🌈 $sequence = $seqIO->next_seq()

fetch the next sequence from the stream, or nothing if there's no more 🎣.

πŸ’– $seqIO->write_seq($sequence [,$another_sequence,...])

write the specified sequence(s) to the stream ✍️.

πŸ‘‘ TIEHANDLE(), READLINE(), PRINT()

these provide the tie interface. see perltie for more details, iykyk.


🫢 FEEDBACK 🫢

πŸ’Œ Mailing Lists

user feedback is an integral part of the evolution of this and other Bioperl modules 🧬. send your comments and suggestions preferably to one of the Bioperl mailing lists.

your participation is much appreciated πŸ’œ.

bioperl-l@bioperl.org                  - General discussion
http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

🌟 Support

please direct usage questions or support issues to the mailing list:

bioperl-l@bioperl.org

rather than to the module maintainer directly πŸ™. many experienced and responsive experts will be able look at the problem and quickly address it. please include a thorough description of the problem with code and data examples if at all possible.

πŸ› Reporting Bugs

report bugs to the Bioperl bug tracking system to help us keep track the bugs and their resolution. bug reports can be submitted via the web:

https://github.com/bioperl/bioperl-live/issues

🌸 AUTHOR - Ewan Birney, Lincoln Stein 🌸

email:

birney@ebi.ac.uk
lstein@cshl.org

🌸 APPENDIX 🌸

okay bestie, this is the deep lore part β€” every single object method gets its own little spotlight. the internal ones (the ones starting with _) are, like, not for you, they're backstage. but we're listing them anyway because completeness is the vibe. πŸ’…


✨ new ✨

  • Title β€” new
  • Usage β€” $stream = Bio::SeqIO->new(-file => 'sequences.fasta', -format => 'fasta');
  • Function β€” hands you back a brand new sequence stream, fresh out of the box
  • Returns β€” a Bio::SeqIO stream all set up with whatever format you asked for
  • Args β€” named parameters telling it where to read sequences from or write them to:
    • -file => a filename, OR
    • -fh => a filehandle to attach to, OR
    • -string => a string

plus these extra args, and every one of them has a reasonable default so you don't have to sweat it:

  • -format => the format of the sequences, usually auto-detected
  • -alphabet => 'dna', 'rna', or 'protein'
  • -flush => 0 or 1 (default: flush filehandles after each write)
  • -seqfactory => sequence factory
  • -locfactory => location factory
  • -objbuilder => object builder
also go peek at Bio::SeqIO::Handler for more πŸ‘€

✨ newFh ✨

  • Title β€” newFh
  • Usage β€” $fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
  • Function β€” it's literally a new() followed by an fh(), two-for-one deal
  • Example β€”
$fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
$sequence = <$fh>;   # read a sequence object
print $fh $sequence; # write a sequence object
  • Returns β€” a filehandle tied to the Bio::SeqIO::Fh class
  • Args β€” (none, it's just vibes)
see also Bio::SeqIO::Fh πŸ’–

✨ fh ✨

  • Title β€” fh
  • Usage β€” $obj->fh
  • Function β€” get or set the IO filehandle
  • Example β€”
$fh = $obj->fh;      # make a tied filehandle
$sequence = <$fh>;   # read a sequence object
print $fh $sequence; # write a sequence object
  • Returns β€” a filehandle tied to the Bio::SeqIO class
  • Args β€” none

✨ next_seq ✨

  • Title β€” next_seq
  • Usage β€” $seq = stream->next_seq
  • Function β€” reads the next sequence object off the stream and hands it over

okay so here's the tea 🍡 β€” some driver modules will run into entries in the stream that are either misformatted or that use syntax the driver doesn't understand yet. if that kind of incident is recoverable (like, say, by dismissing a feature of a feature table or some other non-mandatory part of an entry), the driver will throw a warning. but if it's a non-recoverable situation, an exception gets thrown. do not assume you can resume parsing the same stream after catching that exception, bestie, that's not how it works. and heads up: you can always turn recoverable errors into exceptions by calling $stream->verbose(2).

  • Returns β€” a Bio::Seq sequence object, or nothing if there are no more sequences available
  • Args β€” none
see also Bio::Root::RootI, Bio::Factory::SeqStreamI, Bio::Seq ⭐

✨ write_seq ✨

  • Title β€” write_seq
  • Usage β€” $stream->write_seq($seq)
  • Function β€” writes the $seq object into the stream
  • Returns β€” 1 for success and 0 for error
  • Args β€” a Bio::Seq object

✨ format ✨

  • Title β€” format
  • Usage β€” $format = $stream->format()
  • Function β€” get the sequence format
  • Returns β€” the sequence format, e.g. fasta, fastq
  • Args β€” none

✨ alphabet ✨

  • Title β€” alphabet
  • Usage β€” $self->alphabet($newval)
  • Function β€” set/get the molecule type for the Seq objects that are gonna be created
  • Example β€” $seqio->alphabet('protein')
  • Returns β€” the value of alphabet: 'dna', 'rna', or 'protein'
  • Args β€” newvalue (optional)
  • Throws β€” an exception if the argument is not one of 'dna', 'rna', or 'protein'

✨ _load_format_module ✨

  • Title β€” _load_format_module
  • Usage β€” INTERNAL SeqIO stuff
  • Function β€” loads up (like use) a module at run time on demand
  • Example β€” (none)
  • Returns β€” (none)
  • Args β€” (none)

✨ _concatenate_lines ✨

  • Title β€” _concatenate_lines
  • Usage β€” $s = _concatenate_lines($line, $continuation_line)
  • Function β€” private. concatenates two strings, assuming the second one stems from a continuation line of the first. it adds a space between both unless the first one ends with a dash.

also it takes care of either arg being empty, so no crying over undef 🫢

  • Example β€” (none)
  • Returns β€” a string.
  • Args β€” (none)

✨ _filehandle ✨

  • Title β€” _filehandle
  • Usage β€” $obj->_filehandle($newval)
  • Function β€” this method is deprecated. call _fh() instead, okay? it's giving legacy energy πŸ’€
  • Example β€” (none)
  • Returns β€” value of _filehandle
  • Args β€” newvalue (optional)

✨ _guess_format ✨

  • Title β€” _guess_format
  • Usage β€” $obj->_guess_format($filename)
  • Function β€” guesses the format based on the file suffix
  • Example β€” (none)
  • Returns β€” the guessed format of the filename (lower case)
  • Args β€” (none)
  • Notes β€” the formats that _filehandle() will guess include fasta, genbank, scf, pir, embl, raw, gcg, ace, bsml, swissprot, fastq and phd/phred

✨ sequence_factory ✨

  • Title β€” sequence_factory
  • Usage β€” $seqio->sequence_factory($seqfactory)
  • Function β€” get/set the Bio::Factory::SequenceFactoryI
  • Returns β€” Bio::Factory::SequenceFactoryI
  • Args β€” [optional] Bio::Factory::SequenceFactoryI

✨ object_factory ✨

  • Title β€” object_factory
  • Usage β€” $obj->object_factory($newval)
  • Function β€” this is an alias to sequence_factory with a more generic name (same energy, different label πŸ’…)
  • Example β€” (none)
  • Returns β€” value of object_factory (a scalar)
  • Args β€” on set, the new value (a scalar or undef, optional)

✨ sequence_builder ✨

  • Title β€” sequence_builder
  • Usage β€” $seqio->sequence_builder($seqfactory)
  • Function β€” get/set the Bio::Factory::ObjectBuilderI used to build sequence objects. this only applies to rich sequence formats, e.g. genbank but not fasta.

and if you don't set the sequence object builder yourself, it will in fact be an instance of Bio::Seq::SeqBuilder, and you can use all the methods documented there to configure it. slay ✨

  • Returns β€” a Bio::Factory::ObjectBuilderI compliant object
  • Args β€” [optional] a Bio::Factory::ObjectBuilderI compliant object

✨ location_factory ✨

  • Title β€” location_factory
  • Usage β€” $seqio->location_factory($locfactory)
  • Function β€” get/set the Bio::Factory::LocationFactoryI object that's used for location string parsing
  • Returns β€” a Bio::Factory::LocationFactoryI implementing object
  • Args β€” [optional] on set, a Bio::Factory::LocationFactoryI implementing object.

and that's the whole page bestie!! πŸŽ€ this one came from perl v5.32.1, dated 2021-08-15 β€” so yeah, it's a lil vintage but it's still iconic. go parse some sequences, you absolute main character. πŸ’–πŸ§¬βœ¨

πŸ“¦ where this page comes from