Bio::SeqIO(3pm)
π 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 usenew()with a opened filehandle, provided as a glob reference. for example, to read fromSTDIN:
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 eachwrite_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-flushoption'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 aBio::Factory::SequenceFactoryIobject. see thesequence_factory()method.-locfactoryβ provide aBio::Factory::LocationFactoryIobject. see thelocation_factory()method.-objbuilderβ provide aBio::Factory::ObjectBuilderIobject. see theobject_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 asread(),sysread(),write(),close(), andprintf()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::SeqIOstream 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=>0or1(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 anfh(), 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::Fhclass - 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::SeqIOclass - 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::Seqsequence object, or nothing if there are no more sequences available - Args β none
see alsoBio::Root::RootI,Bio::Factory::SeqStreamI,Bio::Seqβ
β¨ write_seq β¨
- Title β
write_seq - Usage β
$stream->write_seq($seq) - Function β writes the
$seqobject into the stream - Returns β
1for success and0for error - Args β a
Bio::Seqobject
β¨ 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
Seqobjects 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 includefasta,genbank,scf,pir,embl,raw,gcg,ace,bsml,swissprot,fastqandphd/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_factorywith 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::ObjectBuilderIused to build sequence objects. this only applies to rich sequence formats, e.g.genbankbut notfasta.
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::ObjectBuilderIcompliant object - Args β [optional] a
Bio::Factory::ObjectBuilderIcompliant object
β¨ location_factory β¨
- Title β
location_factory - Usage β
$seqio->location_factory($locfactory) - Function β get/set the
Bio::Factory::LocationFactoryIobject that's used for location string parsing - Returns β a
Bio::Factory::LocationFactoryIimplementing object - Args β [optional] on set, a
Bio::Factory::LocationFactoryIimplementing 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
- debian bookworm: libbio-perl-perl 1.7.8-1 Β· Perl
- debian trixie: libbio-perl-perl 1.7.8-1 Β· Perl
- ubuntu jammy: libbio-perl-perl 1.7.8-1 Β· Perl
- ubuntu noble: libbio-perl-perl 1.7.8-1 Β· Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms