Skillversitas

Multiple Sequence Alignment

Multiple sequence alignment (MSA) aligns three or more sequences simultaneously, exposing conserved positions across a family. Biopython reads and writes MSA formats (Clustal, FASTA, PHYLIP) through Bio.AlignIO, and the Bio.Align module can produce alignments from pairwise strategies when a dedicated MSA tool (Clustal Omega, MAFFT, MUSCLE) is not available.

Reading and Writing MSA Files

from Bio import AlignIO

aln = AlignIO.read("family.clustal", "clustal")
print(aln)
print(aln.get_alignment_length())
print(aln.sequences[:3])
print(len(aln), "sequences")

# convert to FASTA:
AlignIO.write(aln, "family.mfa", "fasta")

Conservation Analysis

from Bio.Align import Info

info = Info(aln)
# D (diversity) per column: high = variable, low = conserved
conserved = [i for i, d in enumerate(info["D"]) if d == 0]
print("fully conserved columns:", conserved[:20])

# a simple consensus: most frequent residue per column
from collections import Counter
consensus = "".join(Counter(col).most_common(1)[0][0] for col in aln.columns)
print(consensus[:60])

Feeding MSA into Phylogenetics

An MSA is the direct input for tree building. In Biopython you can read a Clustal alignment and hand the columns to a distance method, or export it for external tools (FastTree, IQ-TREE):

aln = AlignIO.read("family.clustal", "clustal")
# columns of the alignment are strings, one per sequence
for col in aln.columns[:3]:
    print(col)

Gap Handling

  • Alignments use "-" for gaps; some formats use spaces - AlignIO normalizes to gaps on read.
  • For statistics, decide explicitly: include gaps, or mask columns above a gap threshold.
  • Clipped ends: aln.slice(0, n) subsets columns; useful to drop poorly aligned flanks.

Producing an MSA with Biopython

For a quick alignment of a few sequences, a pairwise-based progressive approach works; for real datasets, run Clustal Omega or MAFFT from the command line and read the result with AlignIO:

clustalo -i family.mfa -o family.clustal
mafft --auto family.mfa > family.mafft.mfa

Summary

Work with MSAs via Bio.AlignIO: read Clustal/FASTA/PHYLIP, inspect columns and conservation (Info, per-column counters), compute a consensus, and convert formats. Generate the alignment itself with Clustal Omega or MAFFT for quality, and use the alignment columns as input to phylogenetic methods.