Bio::SeqIO(3)

NAME
Bio::SeqIO - Handler for SeqIO Formats

SYNOPSIS
    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);
    }

  # Now, to actually get at the sequence object, use the standard Bio::Seq
  # methods (look at Bio::Seq if you don't know what they are)

    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";
    }

  # 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 a handler module for the formats in the SeqIO set (eg, Bio::SeqIO::fasta). It is the officially sanctioned way of getting at the format objects, which most people should use.

The Bio::SeqIO system can be thought of like biological file handles. They are attached to filehandles with smart formatting rules (eg, genbank format, or EMBL format, or binary trace file format) and 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 want to know what to do with a Bio::Seq object, 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.

Each stream object has 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

This 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.

-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. The following 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.

-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.

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 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.

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
The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _

  new
 Title   : new
 Usage   : $stream = Bio::SeqIO->new(-file => 'sequences.fasta',
                                     -format => 'fasta');
 Function: Returns a new sequence stream
 Returns : A Bio::SeqIO stream initialised with the appropriate format
 Args    : Named parameters indicating where to read the sequences from or to
           write them to:
             -file   => filename, OR
             -fh     => filehandle to attach to, OR
             -string => string

           Additional arguments, all with reasonable defaults:
             -format     => 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

See Bio::SeqIO::Handler

  newFh
 Title   : newFh
 Usage   : $fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
 Function: Does a new() followed by an fh()
 Example : $fh = Bio::SeqIO->newFh(-file=>$filename,-format=>'Format')
           $sequence = <$fh>;   # read a sequence object
           print $fh $sequence; # write a sequence object
 Returns : filehandle tied to the Bio::SeqIO::Fh class
 Args    :

See 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 : filehandle tied to Bio::SeqIO class
 Args    : none

  next_seq
 Title   : next_seq
 Usage   : $seq = stream->next_seq
 Function: Reads the next sequence object from the stream and returns it.

           Certain driver modules may encounter entries in the stream
           that are either misformatted or that use syntax not yet
           understood by the driver. If such an incident is
           recoverable, e.g., by dismissing a feature of a feature
           table or some other non-mandatory part of an entry, the
           driver will issue a warning. In the case of a
           non-recoverable situation an exception will be thrown.  Do
           not assume that you can resume parsing the same stream
           after catching the exception. Note that you can always turn
           recoverable errors into exceptions by calling
           $stream->verbose(2).

 Returns : a Bio::Seq sequence object, or nothing if no more sequences
           are available

 Args    : none

See 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    : Bio::Seq object

  format
 Title   : format
 Usage   : $format = $stream->format()
 Function: Get the sequence format
 Returns : 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 to be created.
 Example : $seqio->alphabet('protein')
 Returns : value of alphabet: 'dna', 'rna', or 'protein'
 Args    : newvalue (optional)
 Throws  : 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 :
 Returns :
 Args    :

  _concatenate_lines
 Title   : _concatenate_lines
 Usage   : $s = _concatenate_lines($line, $continuation_line)
 Function: Private. Concatenates two strings assuming that the second stems
           from a continuation line of the first. Adds a space between both
           unless the first ends with a dash.

           Takes care of either arg being empty.
 Example :
 Returns : A string.
 Args    :

  _filehandle
 Title   : _filehandle
 Usage   : $obj->_filehandle($newval)
 Function: This method is deprecated. Call _fh() instead.
 Example :
 Returns : value of _filehandle
 Args    : newvalue (optional)

  _guess_format
 Title   : _guess_format
 Usage   : $obj->_guess_format($filename)
 Function: guess format based on file suffix
 Example :
 Returns : guessed format of filename (lower case)
 Args    :
 Notes   : 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.
 Example :
 Returns : value of object_factory (a scalar)
 Args    : on set, 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 applies to rich sequence formats only, e.g. genbank
           but not fasta.

           If you do not set the sequence object builder yourself, it
           will in fact be an instance of L<Bio::Seq::SeqBuilder>, and
           you may use all methods documented there to configure it.

 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 to be used for
           location string parsing
 Returns : a Bio::Factory::LocationFactoryI implementing object
 Args    : [optional] on set, a Bio::Factory::LocationFactoryI implementing
           object.