Read, write, and convert multiple sequence alignment files using Biopython Bio.AlignIO. Supports Clustal, PHYLIP, Stockholm, FASTA, Nexus, and other alignment formats for phylogenetics and conservation analysis. Use when reading, writing, or converting alignment file formats.
SKILL.md
Version Compatibility
Reference examples tested with: BioPython 1.83+
Before using code patterns, verify installed versions match. If versions differ:
Python: pip show <package> then help(module.function) to check signatures
If code throws ImportError, AttributeError, or TypeError, introspect the installed
package and adapt the example to match the actual API rather than retrying.
Alignment File I/O
Read, write, and convert multiple sequence alignment files in various formats.
Required Import
Goal: Load modules for reading, writing, and manipulating multiple sequence alignments.
Approach: Import AlignIO for file I/O and supporting classes for programmatic alignment construction.
from Bio import AlignIO
from Bio.Align import MultipleSeqAlignment
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
Supported Formats
Format
Extension
Read
Write
Description
clustal
.aln
Yes
Yes
Clustal W/X output
fasta
.fasta, .fa
Yes
Yes
Aligned FASTA
phylip
.phy
Yes
Yes
Interleaved PHYLIP
phylip-sequential
.phy
Yes
Yes
Sequential PHYLIP
phylip-relaxed
.phy
Yes
Yes
PHYLIP with long names
stockholm
.sto, .stk
Yes
Yes
Pfam/Rfam annotated
nexus
.nex
Yes
Yes
NEXUS format
emboss
.txt
Yes
No
EMBOSS tools output
fasta-m10
.txt
Yes
No
FASTA -m 10 output
maf
.maf
Yes
Yes
Multiple Alignment Format
mauve
.xmfa
Yes
No
progressiveMauve output
msf
.msf
Yes
No
GCG MSF format
Reading Alignments
"Read an alignment file" → Parse an alignment file into an alignment object with sequences and metadata accessible.
Goal: Load alignment data from files in various formats (Clustal, PHYLIP, Stockholm, FASTA).
Approach: Use AlignIO.read() for single-alignment files or AlignIO.parse() for files containing multiple alignments.
Single Alignment File
from Bio import AlignIO
alignment = AlignIO.read('alignment.aln', 'clustal')
print(f'Alignment length: {alignment.get_alignment_length()}')
print(f'Number of sequences: {len(alignment)}')
Multiple Alignments in One File
for alignment in AlignIO.parse('multi_alignment.sto', 'stockholm'):
print(f'Alignment with {len(alignment)} sequences, length {alignment.get_alignment_length()}')
alignment = AlignIO.read('pfam.sto', 'stockholm')
# Access annotations
for record in alignment:
print(record.id, record.annotations)
Clustal Format
# Clustal preserves conservation symbols in file but not when parsed
alignment = AlignIO.read('clustal.aln', 'clustal')
Batch Processing Multiple Files
Goal: Convert a directory of alignment files from one format to another in bulk.
Approach: Glob for input files and iterate, reading each alignment and writing to the target format.
from pathlib import Path
input_dir = Path('alignments/')
output_dir = Path('converted/')
for input_file in input_dir.glob('*.aln'):
alignment = AlignIO.read(input_file, 'clustal')
output_file = output_dir / f'{input_file.stem}.fasta'
AlignIO.write(alignment, output_file, 'fasta')
Alternative: Bio.Align Module I/O
Goal: Use the modern Bio.Align module for alignment I/O with access to newer features like counts and substitutions.
Approach: Use Align.read(), Align.parse(), and Align.write() which return Alignment objects instead of MultipleSeqAlignment.
The newer Bio.Align module provides its own I/O functions that return Alignment objects (instead of MultipleSeqAlignment). These support additional formats and provide access to modern alignment features.
from Bio import Align
# Read single alignment (returns Alignment object)
alignment = Align.read('alignment.aln', 'clustal')
# Parse multiple alignments
for alignment in Align.parse('multi.sto', 'stockholm'):
print(f'Alignment with {len(alignment)} sequences')
# Write alignment
Align.write(alignment, 'output.fasta', 'fasta')