1 # BioPerl module for Bio::SeqIO
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 # Cared for by Ewan Birney <birney@ebi.ac.uk>
6 # and Lincoln Stein <lstein@cshl.org>
8 # Copyright Ewan Birney
10 # You may distribute this module under the same terms as perl itself
13 # October 18, 1999 Largely rewritten by Lincoln Stein
15 # POD documentation - main docs before the code
19 Bio::SeqIO - Handler for SeqIO Formats
25 $in = Bio::SeqIO->new(-file => "inputfilename" ,
27 $out = Bio::SeqIO->new(-file => ">outputfilename" ,
30 while ( my $seq = $in->next_seq() ) {
31 $out->write_seq($seq);
34 # Now, to actually get at the sequence object, use the standard Bio::Seq
35 # methods (look at Bio::Seq if you don't know what they are)
39 $in = Bio::SeqIO->new(-file => "inputfilename" ,
40 -format => 'genbank');
42 while ( my $seq = $in->next_seq() ) {
43 print "Sequence ",$seq->id, " first 10 bases ",
44 $seq->subseq(1,10), "\n";
48 # The SeqIO system does have a filehandle binding. Most people find this
49 # a little confusing, but it does mean you can write the world's
50 # smallest reformatter
54 $in = Bio::SeqIO->newFh(-file => "inputfilename" ,
56 $out = Bio::SeqIO->newFh(-format => 'EMBL');
58 # World's shortest Fasta<->EMBL format converter:
59 print $out $_ while <$in>;
64 Bio::SeqIO is a handler module for the formats in the SeqIO set (eg,
65 Bio::SeqIO::fasta). It is the officially sanctioned way of getting at
66 the format objects, which most people should use.
68 The Bio::SeqIO system can be thought of like biological file handles.
69 They are attached to filehandles with smart formatting rules (eg,
70 genbank format, or EMBL format, or binary trace file format) and
71 can either read or write sequence objects (Bio::Seq objects, or
72 more correctly, Bio::SeqI implementing objects, of which Bio::Seq is
73 one such object). If you want to know what to do with a Bio::Seq
74 object, read L<Bio::Seq>.
76 The idea is that you request a stream object for a particular format.
77 All the stream objects have a notion of an internal file that is read
78 from or written to. A particular SeqIO object instance is configured
79 for either input or output. A specific example of a stream object is
80 the Bio::SeqIO::fasta object.
82 Each stream object has functions
88 $stream->write_seq($seq);
90 As an added bonus, you can recover a filehandle that is tied to the
91 SeqIO object, allowing you to use the standard E<lt>E<gt> and print
92 operations to read and write sequence objects:
96 $stream = Bio::SeqIO->newFh(-format => 'Fasta',
98 # read from standard input or the input filenames
100 while ( $seq = <$stream> ) {
101 # do something with $seq
106 print $stream $seq; # when stream is in output mode
108 This makes the simplest ever reformatter
113 my $format2 = shift || die
114 "Usage: reformat format1 format2 < input > output";
118 my $in = Bio::SeqIO->newFh(-format => $format1, -fh => \*ARGV );
119 my $out = Bio::SeqIO->newFh(-format => $format2 );
120 # Note: you might want to quote -format to keep older
121 # perl's from complaining.
123 print $out $_ while <$in>;
128 =head2 Bio::SeqIO-E<gt>new()
130 $seqIO = Bio::SeqIO->new(-file => 'filename', -format=>$format);
131 $seqIO = Bio::SeqIO->new(-fh => \*FILEHANDLE, -format=>$format);
132 $seqIO = Bio::SeqIO->new(-format => $format);
134 The new() class method constructs a new Bio::SeqIO object. The
135 returned object can be used to retrieve or print Seq objects. new()
136 accepts the following parameters:
142 A file path to be opened for reading or writing. The usual Perl
145 'file' # open file for reading
146 '>file' # open file for writing
147 '>>file' # open file for appending
148 '+<file' # open file read/write
149 'command |' # open a pipe from the command
150 '| command' # open a pipe to the command
154 You may provide new() with a previously-opened filehandle. For
155 example, to read from STDIN:
157 $seqIO = Bio::SeqIO->new(-fh => \*STDIN);
159 Note that you must pass filehandles as references to globs.
161 If neither a filehandle nor a filename is specified, then the module
162 will read from the @ARGV array or STDIN, using the familiar E<lt>E<gt>
165 A string filehandle is handy if you want to modify the output in the
166 memory, before printing it out. The following program reads in EMBL
167 formatted entries from a file and prints them out in fasta format with
172 my $in = Bio::SeqIO->new(-file => "emblfile",
174 while ( my $seq = $in->next_seq() ) {
175 # the output handle is reset for every file
176 my $stringio = IO::String->new($string);
177 my $out = Bio::SeqIO->new(-fh => $stringio,
179 # output goes into $string
180 $out->write_seq($seq);
182 $string =~ s|(>)(\w+)|$1<font color="Red">$2</font>|g;
189 Specify the format of the file. Supported formats include fasta,
190 genbank, embl, swiss (SwissProt), Entrez Gene and tracefile formats
191 such as abi (ABI) and scf. There are many more, for a complete listing
192 see the SeqIO HOWTO (L<http://bioperl.open-bio.org/wiki/HOWTO:SeqIO>).
194 If no format is specified and a filename is given then the module will
195 attempt to deduce the format from the filename suffix. If there is no
196 suffix that Bioperl understands then it will attempt to guess the
197 format based on file content. If this is unsuccessful then SeqIO will
200 The format name is case-insensitive: 'FASTA', 'Fasta' and 'fasta' are
203 Currently, the tracefile formats (except for SCF) require installation
204 of the external Staden "io_lib" package, as well as the
205 Bio::SeqIO::staden::read package available from the bioperl-ext
210 Sets the alphabet ('dna', 'rna', or 'protein'). When the alphabet is
211 set then Bioperl will not attempt to guess what the alphabet is. This
212 may be important because Bioperl does not always guess correctly.
216 By default, all files (or filehandles) opened for writing sequences
217 will be flushed after each write_seq() (making the file immediately
218 usable). If you do not need this facility and would like to marginally
219 improve the efficiency of writing multiple sequences to the same file
220 (or filehandle), pass the -flush option '0' or any other value that
221 evaluates as defined but false:
223 my $gb = Bio::SeqIO->new(-file => "<gball.gbk",
225 my $fa = Bio::SeqIO->new(-file => ">gball.fa",
227 -flush => 0); # go as fast as we can!
228 while($seq = $gb->next_seq) { $fa->write_seq($seq) }
233 =head2 Bio::SeqIO-E<gt>newFh()
235 $fh = Bio::SeqIO->newFh(-fh => \*FILEHANDLE, -format=>$format);
236 $fh = Bio::SeqIO->newFh(-format => $format);
239 This constructor behaves like new(), but returns a tied filehandle
240 rather than a Bio::SeqIO object. You can read sequences from this
241 object using the familiar E<lt>E<gt> operator, and write to it using
242 print(). The usual array and $_ semantics work. For example, you can
243 read all sequence objects into an array like this:
247 Other operations, such as read(), sysread(), write(), close(), and
248 printf() are not supported.
250 =head1 OBJECT METHODS
252 See below for more detailed summaries. The main methods are:
254 =head2 $sequence = $seqIO-E<gt>next_seq()
256 Fetch the next sequence from the stream, or nothing if no more.
258 =head2 $seqIO-E<gt>write_seq($sequence [,$another_sequence,...])
260 Write the specified sequence(s) to the stream.
262 =head2 TIEHANDLE(), READLINE(), PRINT()
264 These provide the tie interface. See L<perltie> for more details.
270 User feedback is an integral part of the evolution of this and other
271 Bioperl modules. Send your comments and suggestions preferably to one
272 of the Bioperl mailing lists.
274 Your participation is much appreciated.
276 bioperl-l@bioperl.org - General discussion
277 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
281 Please direct usage questions or support issues to the mailing list:
283 bioperl-l@bioperl.org
285 rather than to the module maintainer directly. Many experienced and
286 responsive experts will be able look at the problem and quickly
287 address it. Please include a thorough description of the problem
288 with code and data examples if at all possible.
290 =head2 Reporting Bugs
292 Report bugs to the Bioperl bug tracking system to help us keep track
293 the bugs and their resolution. Bug reports can be submitted via the
296 https://redmine.open-bio.org/projects/bioperl/
298 =head1 AUTHOR - Ewan Birney, Lincoln Stein
300 Email birney@ebi.ac.uk
305 The rest of the documentation details each of the object
306 methods. Internal methods are usually preceded with a _
310 #' Let the code begin...
316 use Bio
::Factory
::FTLocationFactory
;
317 use Bio
::Seq
::SeqBuilder
;
318 use Bio
::Tools
::GuessSeqFormat
;
321 use base
qw(Bio::Root::Root Bio::Root::IO Bio::Factory::SequenceStreamI);
323 my %valid_alphabet_cache;
328 Usage : $stream = Bio::SeqIO->new(-file => $filename,
330 Function: Returns a new sequence stream
331 Returns : A Bio::SeqIO stream initialised with the appropriate format
332 Args : Named parameters:
334 -fh => filehandle to attach to
337 Additional arguments may be used to set factories and
338 builders involved in the sequence object creation. None of
339 these must be provided, they all have reasonable defaults.
340 -seqfactory the Bio::Factory::SequenceFactoryI object
341 -locfactory the Bio::Factory::LocationFactoryI object
342 -objbuilder the Bio::Factory::ObjectBuilderI object
344 See L<Bio::SeqIO::Handler>
351 my ($caller,@args) = @_;
352 my $class = ref($caller) || $caller;
354 # or do we want to call SUPER on an object if $caller is an
356 if( $class =~ /Bio::SeqIO::(\S+)/ ) {
357 my ($self) = $class->SUPER::new
(@args);
358 $self->_initialize(@args);
363 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
365 unless( defined $param{-file
} ||
366 defined $param{-fh
} ||
367 defined $param{-string
} ) {
368 $class->throw("file argument provided, but with an undefined value")
369 if exists $param{'-file'};
370 $class->throw("fh argument provided, but with an undefined value")
371 if exists $param{'-fh'};
372 $class->throw("string argument provided, but with an undefined value")
373 if exists($param{'-string'});
374 # $class->throw("No file, fh, or string argument provided"); # neither defined
377 my $format = $param{'-format'} ||
378 $class->_guess_format( $param{-file
} || $ARGV[0] );
382 $format = Bio
::Tools
::GuessSeqFormat
->new(-file
=> $param{-file
}||$ARGV[0] )->guess;
383 } elsif ($param{-fh
}) {
384 $format = Bio
::Tools
::GuessSeqFormat
->new(-fh
=> $param{-fh
}||$ARGV[0] )->guess;
387 # changed 1-3-11; no need to print out an empty string (only way this
388 # exception is triggered) - cjfields
389 $class->throw("Could not guess format from file/fh") unless $format;
390 $format = "\L$format"; # normalize capitalization to lower case
392 if ($format =~ /-/) {
393 ($format, my $variant) = split('-', $format, 2);
394 push @args, (-variant
=> $variant);
396 return unless( $class->_load_format_module($format) );
397 return "Bio::SeqIO::$format"->new(@args);
404 Usage : $fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
405 Function: does a new() followed by an fh()
406 Example : $fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
407 $sequence = <$fh>; # read a sequence object
408 print $fh $sequence; # write a sequence object
409 Returns : filehandle tied to the Bio::SeqIO::Fh class
412 See L<Bio::SeqIO::Fh>
418 return unless my $self = $class->new(@_);
427 Example : $fh = $obj->fh; # make a tied filehandle
428 $sequence = <$fh>; # read a sequence object
429 print $fh $sequence; # write a sequence object
430 Returns : filehandle tied to Bio::SeqIO class
438 my $class = ref($self) || $self;
439 my $s = Symbol
::gensym
;
440 tie
$$s,$class,$self;
444 # _initialize is chained for all SeqIO classes
447 my($self, @args) = @_;
449 # flush is initialized by the Root::IO init
451 my ($seqfact,$locfact,$objbuilder, $alphabet) =
452 $self->_rearrange([qw(SEQFACTORY
458 $locfact = Bio
::Factory
::FTLocationFactory
->new(-verbose
=> $self->verbose)
460 $objbuilder = Bio
::Seq
::SeqBuilder
->new(-verbose
=> $self->verbose)
462 $self->sequence_builder($objbuilder);
463 $self->location_factory($locfact);
465 # note that this should come last because it propagates the sequence
466 # factory to the sequence builder
467 $seqfact && $self->sequence_factory($seqfact);
470 $alphabet && $self->alphabet($alphabet);
473 # initialize the IO part
474 $self->_initialize_io(@args);
480 Usage : $seq = stream->next_seq
481 Function: Reads the next sequence object from the stream and returns it.
483 Certain driver modules may encounter entries in the stream
484 that are either misformatted or that use syntax not yet
485 understood by the driver. If such an incident is
486 recoverable, e.g., by dismissing a feature of a feature
487 table or some other non-mandatory part of an entry, the
488 driver will issue a warning. In the case of a
489 non-recoverable situation an exception will be thrown. Do
490 not assume that you can resume parsing the same stream
491 after catching the exception. Note that you can always turn
492 recoverable errors into exceptions by calling
495 Returns : a Bio::Seq sequence object, or nothing if no more sequences
500 See L<Bio::Root::RootI>, L<Bio::Factory::SeqStreamI>, L<Bio::Seq>
505 my ($self, $seq) = @_;
506 $self->throw("Sorry, you cannot read from a generic Bio::SeqIO object.");
512 Usage : $stream->write_seq($seq)
513 Function: writes the $seq object into the stream
514 Returns : 1 for success and 0 for error
515 Args : Bio::Seq object
520 my ($self, $seq) = @_;
521 $self->throw("Sorry, you cannot write to a generic Bio::SeqIO object.");
528 Usage : $self->alphabet($newval)
529 Function: Set/get the molecule type for the Seq objects to be created.
530 Example : $seqio->alphabet('protein')
531 Returns : value of alphabet: 'dna', 'rna', or 'protein'
532 Args : newvalue (optional)
533 Throws : Exception if the argument is not one of 'dna', 'rna', or 'protein'
538 my ($self, $value) = @_;
540 if ( defined $value) {
542 unless ($valid_alphabet_cache{$value}) {
543 # instead of hard-coding the allowed values once more, we check by
544 # creating a dummy sequence object
546 require Bio
::PrimarySeq
;
547 my $seq = Bio
::PrimarySeq
->new('-verbose' => $self->verbose,
548 '-alphabet' => $value);
551 $self->throw("Invalid alphabet: $value\n. See Bio::PrimarySeq for allowed values.");
553 $valid_alphabet_cache{$value} = 1;
555 $self->{'alphabet'} = $value;
557 return $self->{'alphabet'};
560 =head2 _load_format_module
562 Title : _load_format_module
563 Usage : *INTERNAL SeqIO stuff*
564 Function: Loads up (like use) a module at run time on demand
571 sub _load_format_module
{
572 my ($self, $format) = @_;
573 my $module = "Bio::SeqIO::" . $format;
577 $ok = $self->_load_module($module);
581 $self: $format cannot be found
583 For more information about the SeqIO system please see the SeqIO docs.
584 This includes ways of checking for formats at compile time, not run time
591 =head2 _concatenate_lines
593 Title : _concatenate_lines
594 Usage : $s = _concatenate_lines($line, $continuation_line)
595 Function: Private. Concatenates two strings assuming that the second stems
596 from a continuation line of the first. Adds a space between both
597 unless the first ends with a dash.
599 Takes care of either arg being empty.
606 sub _concatenate_lines
{
607 my ($self, $s1, $s2) = @_;
609 $s1 .= " " if($s1 && ($s1 !~ /-$/) && $s2);
610 return ($s1 ?
$s1 : "") . ($s2 ?
$s2 : "");
616 Usage : $obj->_filehandle($newval)
617 Function: This method is deprecated. Call _fh() instead.
619 Returns : value of _filehandle
620 Args : newvalue (optional)
625 my ($self,@args) = @_;
626 return $self->_fh(@args);
631 Title : _guess_format
632 Usage : $obj->_guess_format($filename)
633 Function: guess format based on file suffix
635 Returns : guessed format of filename (lower case)
637 Notes : formats that _filehandle() will guess include fasta,
638 genbank, scf, pir, embl, raw, gcg, ace, bsml, swissprot,
645 return unless $_ = shift;
646 return 'abi' if /\.ab[i1]$/i;
647 return 'ace' if /\.ace$/i;
648 return 'alf' if /\.alf$/i;
649 return 'bsml' if /\.(bsm|bsml)$/i;
650 return 'ctf' if /\.ctf$/i;
651 return 'embl' if /\.(embl|ebl|emb|dat)$/i;
652 return 'entrezgene' if /\.asn$/i;
653 return 'exp' if /\.exp$/i;
654 return 'fasta' if /\.(fasta|fast|fas|seq|fa|fsa|nt|aa|fna|faa)$/i;
655 return 'fastq' if /\.fastq$/i;
656 return 'gcg' if /\.gcg$/i;
657 return 'genbank' if /\.(gb|gbank|genbank|gbk|gbs)$/i;
658 return 'phd' if /\.(phd|phred)$/i;
659 return 'pir' if /\.pir$/i;
660 return 'pln' if /\.pln$/i;
661 return 'qual' if /\.qual$/i;
662 return 'raw' if /\.txt$/i;
663 return 'scf' if /\.scf$/i;
664 return 'swiss' if /\.(swiss|sp)$/i;
666 # from Strider 1.4 Release Notes: The file name extensions used by
667 # Strider 1.4 are ".xdna", ".xdgn", ".xrna" and ".xprt" for DNA,
668 # DNA Degenerate, RNA and Protein Sequence Files, respectively
669 return 'strider' if /\.(xdna|xdgn|xrna|xprt)$/i;
671 return 'ztr' if /\.ztr$/i;
680 my ($class,$val) = @_;
681 return bless {'seqio' => $val}, $class;
686 return $self->{'seqio'}->next_seq() unless wantarray;
688 push @list, $obj while $obj = $self->{'seqio'}->next_seq();
694 $self->{'seqio'}->write_seq(@_);
697 =head2 sequence_factory
699 Title : sequence_factory
700 Usage : $seqio->sequence_factory($seqfactory)
701 Function: Get/Set the Bio::Factory::SequenceFactoryI
702 Returns : Bio::Factory::SequenceFactoryI
703 Args : [optional] Bio::Factory::SequenceFactoryI
707 sub sequence_factory
{
708 my ($self,$obj) = @_;
710 if( ! ref($obj) || ! $obj->isa('Bio::Factory::SequenceFactoryI') ) {
711 $self->throw("Must provide a valid Bio::Factory::SequenceFactoryI object to ".ref($self)."::sequence_factory()");
713 $self->{'_seqio_seqfactory'} = $obj;
714 my $builder = $self->sequence_builder();
715 if($builder && $builder->can('sequence_factory') &&
716 (! $builder->sequence_factory())) {
717 $builder->sequence_factory($obj);
720 $self->{'_seqio_seqfactory'};
723 =head2 object_factory
725 Title : object_factory
726 Usage : $obj->object_factory($newval)
727 Function: This is an alias to sequence_factory with a more generic name.
729 Returns : value of object_factory (a scalar)
730 Args : on set, new value (a scalar or undef, optional)
735 return shift->sequence_factory(@_);
738 =head2 sequence_builder
740 Title : sequence_builder
741 Usage : $seqio->sequence_builder($seqfactory)
742 Function: Get/Set the Bio::Factory::ObjectBuilderI used to build sequence
745 If you do not set the sequence object builder yourself, it
746 will in fact be an instance of L<Bio::Seq::SeqBuilder>, and
747 you may use all methods documented there to configure it.
749 Returns : a Bio::Factory::ObjectBuilderI compliant object
750 Args : [optional] a Bio::Factory::ObjectBuilderI compliant object
754 sub sequence_builder
{
755 my ($self,$obj) = @_;
757 if( ! ref($obj) || ! $obj->isa('Bio::Factory::ObjectBuilderI') ) {
758 $self->throw("Must provide a valid Bio::Factory::ObjectBuilderI object to ".ref($self)."::sequence_builder()");
760 $self->{'_object_builder'} = $obj;
762 $self->{'_object_builder'};
765 =head2 location_factory
767 Title : location_factory
768 Usage : $seqio->location_factory($locfactory)
769 Function: Get/Set the Bio::Factory::LocationFactoryI object to be used for
770 location string parsing
771 Returns : a Bio::Factory::LocationFactoryI implementing object
772 Args : [optional] on set, a Bio::Factory::LocationFactoryI implementing
777 sub location_factory
{
778 my ($self,$obj) = @_;
780 if( ! ref($obj) || ! $obj->isa('Bio::Factory::LocationFactoryI') ) {
781 $self->throw("Must provide a valid Bio::Factory::LocationFactoryI" .
782 " object to ".ref($self)."->location_factory()");
784 $self->{'_seqio_locfactory'} = $obj;
786 $self->{'_seqio_locfactory'};