$npx -y skills add GPTomics/bioSkills --skill pairwise-alignmentPerform pairwise sequence alignment using Biopython Bio.Align.PairwiseAligner. Use when comparing two sequences, finding optimal alignments, scoring similarity, and identifying local or global matches between DNA, RNA, or protein sequences.
| 1 | ## Version Compatibility |
| 2 | |
| 3 | Reference examples tested with: BioPython 1.83+ |
| 4 | |
| 5 | Before using code patterns, verify installed versions match. If versions differ: |
| 6 | - Python: `pip show <package>` then `help(module.function)` to check signatures |
| 7 | |
| 8 | If code throws ImportError, AttributeError, or TypeError, introspect the installed |
| 9 | package and adapt the example to match the actual API rather than retrying. |
| 10 | |
| 11 | # Pairwise Sequence Alignment |
| 12 | |
| 13 | **"Align two sequences"** -> Compute an optimal alignment between a pair of sequences using dynamic programming. |
| 14 | - Python: `PairwiseAligner()` (BioPython Bio.Align) |
| 15 | - CLI: `needle` (global) or `water` (local) from EMBOSS |
| 16 | - R: `pairwiseAlignment()` (Biostrings) |
| 17 | |
| 18 | Align two sequences using dynamic programming algorithms (Needleman-Wunsch for global, Smith-Waterman for local). |
| 19 | |
| 20 | ## Required Import |
| 21 | |
| 22 | **Goal:** Load modules needed for pairwise alignment operations. |
| 23 | |
| 24 | **Approach:** Import the PairwiseAligner class along with sequence and I/O utilities from Biopython. |
| 25 | |
| 26 | ```python |
| 27 | from Bio.Align import PairwiseAligner |
| 28 | from Bio.Seq import Seq |
| 29 | from Bio import SeqIO |
| 30 | ``` |
| 31 | |
| 32 | ## Pairwise Library Selection |
| 33 | |
| 34 | `Bio.Align.PairwiseAligner` is the right default for interactive use, scripting, and pair sizes up to a few thousand residues, but it is not the fastest or most scalable option. For high-throughput screens, very long sequences, or production pipelines, switch to a SIMD-accelerated or specialised library. |
| 35 | |
| 36 | | Library | Speed vs Bio.Align | Alphabet | Scoring | Vectorization | When to use | |
| 37 | |---------|-------------------|----------|---------|---------------|-------------| |
| 38 | | `Bio.Align.PairwiseAligner` (BioPython) | 1x baseline | DNA / RNA / protein | Matrix + affine | C-backed Gotoh | Default, <10 kb pairs, interactive use | |
| 39 | | `parasail` (Daily 2016 BMC Bioinf) | 10-100x | DNA / protein | Matrix + affine | SSE / AVX SIMD | High-throughput SW or NW; benchmark loops | |
| 40 | | `edlib` (Sosic & Sikic 2017 Bioinf) | 100-1000x | DNA only | Edit distance only | Bit-parallel Myers | Read mapping, k-mer search, primer placement | |
| 41 | | `pywfa` / WFA2 (Marco-Sola 2021 Bioinformatics 37:456; BiWFA: Marco-Sola 2023 Bioinformatics 39:btad074) | Best for low-divergence | DNA | Matrix + affine | Wavefront, O(s) memory | Long, near-identical sequences (>10 kb, <5% diverged) | |
| 42 | | `mappy` / minimap2 (Li 2018 Bioinf) | Production reads-to-genome | DNA | Chain + base-level | k-mer chain | Long-read mapping, splice-aware DNA | |
| 43 | | `Bio.pairwise2` | DEPRECATED | -- | -- | -- | Migrate to `PairwiseAligner` (deprecated in BioPython 1.80; not yet removed; migrate proactively) | |
| 44 | | EMBOSS `needle` / `water` | ~Bio.Align | DNA / protein | Matrix + affine | None | Reproducibility, audit trails (fixed, documented default parameters) | |
| 45 | |
| 46 | Speed numbers in the table are rough; benchmark on representative inputs before committing. Critical caveats: |
| 47 | - **WFA / BiWFA**: 10-100x faster than Gotoh below 5% divergence; above ~10% it converges to Gotoh complexity. Right tool for PacBio HiFi self-similarity or assembly-vs-reference; not for distant homologs. |
| 48 | - **edlib**: 100-1000x faster than Gotoh at low divergence (<5% errors); above ~50% divergence the effective speedup drops to ~64x. For high-divergence DNA (<70% nucleotide identity), prefer parasail's SIMD score-only mode. |
| 49 | - **parasail**: SIMD only realises its advantage on long sequences in amortised batch loops. |
| 50 | |
| 51 | When uncertain which algorithm Biopython's aligner selected internally, inspect `aligner.algorithm` after configuration -- it returns the resolved variant ("Needleman-Wunsch", "Smith-Waterman", "Gotoh global alignment algorithm", "Gotoh local alignment algorithm", "Waterman-Smith-Beyer global alignment algorithm", "Waterman-Smith-Beyer local alignment algorithm") for deterministic auditing. |
| 52 | |
| 53 | ## Core Concepts |
| 54 | |
| 55 | | Mode | Algorithm | Use Case | |
| 56 | |------|-----------|----------| |
| 57 | | `global` | Needleman-Wunsch | Full-length alignment, similar-length sequences | |
| 58 | | `local` | Smith-Waterman | Find best matching regions, different-length sequences | |
| 59 | | `global` + free end gaps | Semi-global | Overlap detection, fragment-to-reference alignment | |
| 60 | |
| 61 | ### Choosing the Right Mode |
| 62 | |
| 63 | - **Global**: Both sequences are expected to be homologous over their full length (e.g., two orthologs of similar size). Forces end-to-end alignment. |
| 64 | - **Local**: Conserved domains or motifs within otherwise dissimilar sequences. BLAST uses local alignment internally. Preferred when protein termini are highly divergent (termini accumulate mutations faster than core regions). |
| 65 | - **Semi-global**: One sequence is a fragment or subsequence of the other (e.g., primer to template, read to reference, detecting overlap betw |