| name | pairwise-alignment |
| description | Pairwise sequence alignment using dynamic programming algorithms. Use when computing optimal alignments between two sequences using Needleman-Wunsch (global) or Smith-Waterman (local) algorithms. |
Pairwise Alignment
Dynamic programming algorithms for aligning two sequences.
Global Alignment (Needleman-Wunsch)
from Bio import pairwise2
from Bio.pairwise2 import format_alignment
seq1 = "HEAGAWGHEE"
seq2 = "PAWHEAE"
# Simple global alignment
alignments = pairwise2.align.globalxx(seq1, seq2)
for align in alignments[:3]:
print(format_alignment(*align))
Local Alignment (Smith-Waterman)
# Local alignment finds best local match
alignments = pairwise2.align.localxx(seq1, seq2)
best = alignments[0]
print(f"Score: {best[2]}")
print(f"Start: {best[3]}, End: {best[4]}")
Custom Scoring
# Custom match/mismatch scores
alignments = pairwise2.align.globalms(seq1, seq2,
match=2, # Match score
mismatch=-1, # Mismatch penalty
open=-0.5, # Gap open penalty
extend=-0.1) # Gap extend penalty
Using Substitution Matrices
from Bio.Align import substitution_matrices
blosum62 = substitution_matrices.load("BLOSUM62")
# Alignment with BLOSUM62
alignments = pairwise2.align.globalds(seq1, seq2, blosum62, -10, -0.5)
Alignment Statistics
def calculate_identity(align1, align2):
"""Calculate percent identity between aligned sequences."""
matches = sum(1 for a, b in zip(align1, align2) if a == b and a != '-')
length = len(align1.replace('-', ''))
return (matches / length) * 100