# Bio.AlignIO package

## Submodules

- Bio.AlignIO.ClustalIO module
  - `ClustalWriter`
    - `ClustalWriter.write_alignment()`
    - `ClustalWriter.__annotate_func__`
    - `ClustalWriter.__annotations_cache__`
    - `ClustalWriter.__firstlineno__`
    - `ClustalWriter.__static_attributes__`
  - `ClustalIterator`
    - `ClustalIterator.__next__()`
    - `ClustalIterator.__annotate_func__`
    - `ClustalIterator.__annotations_cache__`
    - `ClustalIterator.__firstlineno__`
    - `ClustalIterator.__static_attributes__`
- Bio.AlignIO.EmbossIO module
  - `EmbossIterator`
    - `EmbossIterator.__next__()`
    - `EmbossIterator.__annotate_func__`
    - `EmbossIterator.__annotations_cache__`
    - `EmbossIterator.__firstlineno__`
    - `EmbossIterator.__static_attributes__`
- Bio.AlignIO.FastaIO module
  - `FastaM10Iterator()`
- Bio.AlignIO.Interfaces module
  - `AlignmentIterator`
    - `AlignmentIterator.__init__()`
    - `AlignmentIterator.__next__()`
    - `AlignmentIterator.__iter__()`
    - `AlignmentIterator.__annotate_func__`
    - `AlignmentIterator.__annotations_cache__`
    - `AlignmentIterator.__firstlineno__`
    - `AlignmentIterator.__static_attributes__`
  - `AlignmentWriter`
    - `AlignmentWriter.__init__()`
    - `AlignmentWriter.write_file()`
    - `AlignmentWriter.clean()`
    - `AlignmentWriter.__annotate_func__`
    - `AlignmentWriter.__annotations_cache__`
    - `AlignmentWriter.__firstlineno__`
    - `AlignmentWriter.__static_attributes__`
  - `SequentialAlignmentWriter`
    - `SequentialAlignmentWriter.__init__()`
    - `SequentialAlignmentWriter.write_file()`
    - `SequentialAlignmentWriter.write_header()`
    - `SequentialAlignmentWriter.write_footer()`
    - `SequentialAlignmentWriter.write_alignment()`
    - `SequentialAlignmentWriter.__annotate_func__`
    - `SequentialAlignmentWriter.__annotations_cache__`
    - `SequentialAlignmentWriter.__firstlineno__`
    - `SequentialAlignmentWriter.__static_attributes__`
- Bio.AlignIO.MafIO module
  - `MafWriter`
    - `MafWriter.write_header()`
    - `MafWriter.write_alignment()`
    - `MafWriter.__annotate_func__`
    - `MafWriter.__annotations_cache__`
    - `MafWriter.__firstlineno__`
    - `MafWriter.__static_attributes__`
  - `MafIterator()`
  - `MafIndex`
    - `MafIndex.__init__()`
    - `MafIndex.close()`
    - `MafIndex.search()`
    - `MafIndex.get_spliced()`
    - `MafIndex.__annotate_func__`
    - `MafIndex.__annotations_cache__`
    - `MafIndex.__firstlineno__`
    - `MafIndex.__repr__()`
    - `MafIndex.__static_attributes__`
    - `MafIndex.__len__()`
- Bio.AlignIO.MauveIO module
  - `MauveWriter`
    - `MauveWriter.__init__()`
    - `MauveWriter.write_alignment()`
    - `MauveWriter.__annotate_func__`
    - `MauveWriter.__annotations_cache__`
    - `MauveWriter.__firstlineno__`
    - `MauveWriter.__static_attributes__`
  - `MauveIterator`
    - `MauveIterator.__next__()`
    - `MauveIterator.__annotate_func__()`
    - `MauveIterator.__annotations_cache__`
    - `MauveIterator.__firstlineno__`
    - `MauveIterator.__static_attributes__`
- Bio.AlignIO.MsfIO module
  - `MsfIterator`
    - `MsfIterator.__next__()`
    - `MsfIterator.__annotate_func__`
    - `MsfIterator.__annotations_cache__`
    - `MsfIterator.__firstlineno__`
    - `MsfIterator.__static_attributes__`
- Bio.AlignIO.NexusIO module
  - `NexusIterator()`
  - `NexusWriter`
    - `NexusWriter.write_file()`
    - `NexusWriter.write_alignment()`
    - `NexusWriter.__annotate_func__`
    - `NexusWriter.__annotations_cache__`
    - `NexusWriter.__firstlineno__`
    - `NexusWriter.__static_attributes__`
- Bio.AlignIO.PhylipIO module
  - Note
  - `PhylipWriter`
    - `PhylipWriter.write_alignment()`
    - `PhylipWriter.__annotate_func__`
    - `PhylipWriter.__annotations_cache__`
    - `PhylipWriter.__firstlineno__`
    - `PhylipWriter.__static_attributes__`
  - `PhylipIterator`
    - `PhylipIterator.id_width`
    - `PhylipIterator.__next__()`
    - `PhylipIterator.__annotate_func__`
    - `PhylipIterator.__annotations_cache__`
    - `PhylipIterator.__firstlineno__`
    - `PhylipIterator.__static_attributes__`
  - `RelaxedPhylipWriter`
    - `RelaxedPhylipWriter.write_alignment()`
    - `RelaxedPhylipWriter.__annotate_func__`
    - `RelaxedPhylipWriter.__annotations_cache__`
    - `RelaxedPhylipWriter.__firstlineno__`
    - `RelaxedPhylipWriter.__static_attributes__`
  - `RelaxedPhylipIterator`
    - `RelaxedPhylipIterator.__annotate_func__`
    - `RelaxedPhylipIterator.__annotations_cache__`
    - `RelaxedPhylipIterator.__firstlineno__`
    - `RelaxedPhylipIterator.__static_attributes__`
  - `SequentialPhylipWriter`
    - `SequentialPhylipWriter.write_alignment()`
    - `SequentialPhylipWriter.__annotate_func__`
    - `SequentialPhylipWriter.__annotations_cache__`
    - `SequentialPhylipWriter.__firstlineno__`
    - `SequentialPhylipWriter.__static_attributes__`
  - `SequentialPhylipIterator`
    - `SequentialPhylipIterator.__next__()`
    - `SequentialPhylipIterator.__annotate_func__`
    - `SequentialPhylipIterator.__annotations_cache__`
    - `SequentialPhylipIterator.__firstlineno__`
    - `SequentialPhylipIterator.__static_attributes__`
  - `sanitize_name()`
- Bio.AlignIO.StockholmIO module
  - `StockholmWriter`
    - `StockholmWriter.pfam_gr_mapping`
    - `StockholmWriter.pfam_gc_mapping`
    - `StockholmWriter.pfam_gs_mapping`
    - `StockholmWriter.write_alignment()`
    - `StockholmWriter.__annotate_func__`
    - `StockholmWriter.__annotations_cache__`
    - `StockholmWriter.__firstlineno__`
    - `StockholmWriter.__static_attributes__`
  - `StockholmIterator`
    - `StockholmIterator.pfam_gr_mapping`
    - `StockholmIterator.pfam_gc_mapping`
    - `StockholmIterator.pfam_gs_mapping`
    - `StockholmIterator.__next__()`
    - `StockholmIterator.__annotate_func__`
    - `StockholmIterator.__annotations_cache__`
    - `StockholmIterator.__firstlineno__`
    - `StockholmIterator.__static_attributes__`

## Module contents

Multiple sequence alignment input/output as alignment objects.

The Bio.AlignIO interface is deliberately very similar to Bio.SeqIO, and in fact the two are connected internally. Both modules use the same set of file format names (lower case strings). From the user’s perspective, you can read in a PHYLIP file containing one or more alignments using Bio.AlignIO, or you can read in the sequences within these alignments using Bio.SeqIO.

Bio.AlignIO is also documented at http://biopython.org/wiki/AlignIO and by a whole chapter in our tutorial:

- HTML Tutorial

- PDF Tutorial

### Input

For the typical special case when your file or handle contains one and only one alignment, use the function Bio.AlignIO.read(). This takes an input file handle (or in recent versions of Biopython a filename as a string), format string and optional number of sequences per alignment. It will return a single MultipleSeqAlignment object (or raise an exception if there isn’t just one alignment):

    >>> from Bio import AlignIO
    >>> align = AlignIO.read("Phylip/interlaced.phy", "phylip")
    >>> print(align)
    Alignment with 3 rows and 384 columns
    -----MKVILLFVLAVFTVFVSS---------------RGIPPE...I-- CYS1_DICDI
    MAHARVLLLALAVLATAAVAVASSSSFADSNPIRPVTDRAASTL...VAA ALEU_HORVU
    ------MWATLPLLCAGAWLLGV--------PVCGAAELSVNSL...PLV CATH_HUMAN

For the general case, when the handle could contain any number of alignments, use the function Bio.AlignIO.parse(…) which takes the same arguments, but returns an iterator giving MultipleSeqAlignment objects (typically used in a for loop). If you want random access to the alignments by number, turn this into a list:

    >>> from Bio import AlignIO
    >>> alignments = list(AlignIO.parse("Emboss/needle.txt", "emboss"))
    >>> print(alignments[2])
    Alignment with 2 rows and 120 columns
    -KILIVDDQYGIRILLNEVFNKEGYQTFQAANGLQALDIVTKER...--- ref_rec
    LHIVVVDDDPGTCVYIESVFAELGHTCKSFVRPEAAEEYILTHP...HKE gi|94967506|receiver

Most alignment file formats can be concatenated so as to hold as many different multiple sequence alignments as possible. One common example is the output of the tool seqboot in the PHLYIP suite. Sometimes there can be a file header and footer, as seen in the EMBOSS alignment output.

### Output

Use the function Bio.AlignIO.write(…), which takes a complete set of Alignment objects (either as a list, or an iterator), an output file handle (or filename in recent versions of Biopython) and of course the file format:

    from Bio import AlignIO
    alignments = ...
    count = SeqIO.write(alignments, "example.faa", "fasta")

If using a handle make sure to close it to flush the data to the disk:

    from Bio import AlignIO
    alignments = ...
    with open("example.faa", "w") as handle:
        count = SeqIO.write(alignments, handle, "fasta")

In general, you are expected to call this function once (with all your alignments) and then close the file handle. However, for file formats like PHYLIP where multiple alignments are stored sequentially (with no file header and footer), then multiple calls to the write function should work as expected when using handles.

If you are using a filename, the repeated calls to the write functions will overwrite the existing file each time.

### Conversion

The Bio.AlignIO.convert(…) function allows an easy interface for simple alignment file format conversions. Additionally, it may use file format specific optimisations so this should be the fastest way too.

In general however, you can combine the Bio.AlignIO.parse(…) function with the Bio.AlignIO.write(…) function for sequence file conversion. Using generator expressions provides a memory efficient way to perform filtering or other extra operations as part of the process.

### File Formats

When specifying the file format, use lowercase strings. The same format names are also used in Bio.SeqIO and include the following:

> - clustal - Output from Clustal W or X.

> - emboss - EMBOSS tools’ “pairs” and “simple” alignment formats.

> - fasta - The generic sequence file format where each record starts with an identifier line starting with a “>” character, followed by lines of sequence.

> - fasta-m10 - For the pairwise alignments output by Bill Pearson’s FASTA tools when used with the -m 10 command line option for machine readable output.

> - ig - The IntelliGenetics file format, apparently the same as the MASE alignment format.

> - msf - The GCG MSF alignment format, originally from PileUp tool.

> - nexus - Output from NEXUS, see also the module Bio.Nexus which can also read any phylogenetic trees in these files.

> - phylip - Interlaced PHYLIP, as used by the PHYLIP tools.

> - phylip-sequential - Sequential PHYLIP.

> - phylip-relaxed - PHYLIP like format allowing longer names.

> - stockholm - A richly annotated alignment file format used by PFAM.

> - mauve - Output from progressiveMauve/Mauve

Note that while Bio.AlignIO can read all the above file formats, it cannot write to all of them.

You can also use any file format supported by Bio.SeqIO, such as “fasta” or “ig” (which are listed above), PROVIDED the sequences in your file are all the same length.

Bio.AlignIO.write(alignments, handle, format)

  Write complete set of alignments to a file.

  Arguments:

    - alignments - A list (or iterator) of MultipleSeqAlignment objects, or a single alignment object.

    - handle - File handle object to write to, or filename as string (note older versions of Biopython only took a handle).

    - format - lower case string describing the file format to write.

  You should close the handle after calling this function.

  Returns the number of alignments written (as an integer).

Bio.AlignIO.parse(handle, format, seq_count=None)

  Iterate over an alignment file as MultipleSeqAlignment objects.

  Arguments:

    - handle - handle to the file, or the filename as a string (note older versions of Biopython only took a handle).

    - format - string describing the file format.

    - seq_count - Optional integer, number of sequences expected in each alignment. Recommended for fasta format files.

  If you have the file name in a string ‘filename’, use:

      >>> from Bio import AlignIO
      >>> filename = "Emboss/needle.txt"
      >>> format = "emboss"
      >>> for alignment in AlignIO.parse(filename, format):
      ...     print("Alignment of length %i" % alignment.get_alignment_length())
      Alignment of length 124
      Alignment of length 119
      Alignment of length 120
      Alignment of length 118
      Alignment of length 125

  If you have a string ‘data’ containing the file contents, use:

      from Bio import AlignIO
      from io import StringIO
      my_iterator = AlignIO.parse(StringIO(data), format)

  Use the Bio.AlignIO.read() function when you expect a single record only.

Bio.AlignIO.read(handle, format, seq_count=None)

  Turn an alignment file into a single MultipleSeqAlignment object.

  Arguments:

    - handle - handle to the file, or the filename as a string (note older versions of Biopython only took a handle).

    - format - string describing the file format.

    - seq_count - Optional integer, number of sequences expected in each alignment. Recommended for fasta format files.

  If the handle contains no alignments, or more than one alignment, an exception is raised. For example, using a PFAM/Stockholm file containing one alignment:

      >>> from Bio import AlignIO
      >>> filename = "Clustalw/protein.aln"
      >>> format = "clustal"
      >>> alignment = AlignIO.read(filename, format)
      >>> print("Alignment of length %i" % alignment.get_alignment_length())
      Alignment of length 411

  If however you want the first alignment from a file containing multiple alignments this function would raise an exception.

      >>> from Bio import AlignIO
      >>> filename = "Emboss/needle.txt"
      >>> format = "emboss"
      >>> alignment = AlignIO.read(filename, format)
      Traceback (most recent call last):
          ...
      ValueError: More than one record found in handle

  Instead use:

      >>> from Bio import AlignIO
      >>> filename = "Emboss/needle.txt"
      >>> format = "emboss"
      >>> alignment = next(AlignIO.parse(filename, format))
      >>> print("First alignment has length %i" % alignment.get_alignment_length())
      First alignment has length 124

  You must use the Bio.AlignIO.parse() function if you want to read multiple records from the handle.

Bio.AlignIO.convert(in_file, in_format, out_file, out_format, molecule_type=None)

  Convert between two alignment files, returns number of alignments.

  Arguments:

    - in_file - an input handle or filename

    - in_format - input file format, lower case string

    - output - an output handle or filename

    - out_file - output file format, lower case string

    - molecule_type - optional molecule type to apply, string containing “DNA”, “RNA” or “protein”.

  NOTE - If you provide an output filename, it will be opened which will overwrite any existing file without warning. This may happen if even the conversion is aborted (e.g. an invalid out_format name is given).

  Some output formats require the molecule type be specified where this cannot be determined by the parser. For example, converting to FASTA, Clustal, or PHYLIP format to NEXUS:

      >>> from io import StringIO
      >>> from Bio import AlignIO
      >>> handle = StringIO()
      >>> AlignIO.convert("Phylip/horses.phy", "phylip", handle, "nexus", "DNA")
      1
      >>> print(handle.getvalue())
      #NEXUS
      begin data;
      dimensions ntax=10 nchar=40;
      format datatype=dna missing=? gap=-;
      matrix
      Mesohippus   AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      Hypohippus   AAACCCCCCCAAAAAAAAACAAAAAAAAAAAAAAAAAAAA
      Archaeohip   CAAAAAAAAAAAAAAAACACAAAAAAAAAAAAAAAAAAAA
      Parahippus   CAAACAACAACAAAAAAAACAAAAAAAAAAAAAAAAAAAA
      Merychippu   CCAACCACCACCCCACACCCAAAAAAAAAAAAAAAAAAAA
      'M. secundu' CCAACCACCACCCACACCCCAAAAAAAAAAAAAAAAAAAA
      Nannipus     CCAACCACAACCCCACACCCAAAAAAAAAAAAAAAAAAAA
      Neohippari   CCAACCCCCCCCCCACACCCAAAAAAAAAAAAAAAAAAAA
      Calippus     CCAACCACAACCCACACCCCAAAAAAAAAAAAAAAAAAAA
      Pliohippus   CCCACCCCCCCCCACACCCCAAAAAAAAAAAAAAAAAAAA
      ;
      end;

Biopython v: 1.86

Versions
  Previous
  Latest
  Develop

Biopython Project
  Homepage
  On GitHub