Adaptive Anchor-Guided DNA Sequence Alignment
AlignX is a modular DNA sequence alignment framework that bridges the gap between fast heuristic approximations and strict mathematical guarantees. By decomposing sequences using exact matching anchors and adaptively sizing dynamic programming (DP) bounds for the remaining gaps, AlignX significantly accelerates alignment on high-homology sequences without sacrificing the mathematical optimality guaranteed by classical Needleman-Wunsch.
Traditional sequence alignment algorithms like Full Dynamic Programming (DP) guarantee exact optimal alignments but scale quadratically in time and memory
graph TD
A[Reference & Query DNA] --> B[Preprocessing & Validation]
B --> C[Anchor Engine: KMP / Rabin-Karp]
C --> D[Anchor Resolution & Filtering]
D --> E[Gap Extraction]
E --> F[Adaptive Selector]
F -->|Band Estimated| G[Banded DP]
F -->|Divergent| H[Full DP]
G --> I{Boundary Touched?}
I -->|Yes| J{Retries < Max?}
J -->|Yes| K[Widen Band & Retry]
K --> G
J -->|No| H
I -->|No| L[Valid Alignment]
H --> L
L --> M[Alignment Reconstruction]
M --> N[Mutation Detection & Clustering]
N --> O[FinalResult Payload]
- Anchor Engine: Discover linear exact matches via Knuth-Morris-Pratt (KMP) or Rabin-Karp and resolve overlapping constraints monotonically.
- Adaptive Banding: Scale DP diagonal bands dynamically based on local gap insertion/deletion length disparity.
- Boundary-Aware Retries: If the traceback path approaches the computed band edges, the gap is automatically widened and re-run.
- Full DP Fallback: If the band safety is violated repeatedly, the gap degrades safely to exact Full DP.
- Streamlit Research Dashboard: A visual interface to inspect anchors, gaps, structural variant mutations, and algorithmic selector traces.
AlignX was engineered specifically to demonstrate foundational algorithm design paradigms applied to computational genomics:
| Algorithm Paradigm | Time Complexity | Space Complexity | Optimality Guarantee |
|---|---|---|---|
| Needleman-Wunsch (Full DP) | Strict Optimal | ||
| Global Banded DP (fixed |
Heuristic (clips indels) | ||
| AlignX (Decomposed, Homologous) | Strict Optimal (Fallback) | ||
| AlignX (Worst-Case Adversarial) | Strict Optimal |
-
Divide and Conquer: Sequence partitioning into
$K$ independent subproblems isolated by identical invariant anchors. -
Exact String Matching in Linear Time:
-
Knuth-Morris-Pratt (KMP): Failure function (
$\pi$ -table) preprocessing in$\mathcal{O}(L_{\min})$ and linear$\mathcal{O}(N)$ scanning. -
Rabin-Karp: Rolling polynomial hash with modular arithmetic in
$\mathcal{O}(N + M)$ average time.
-
Knuth-Morris-Pratt (KMP): Failure function (
-
Greedy Interval Scheduling: Strict monotonic anchor chain resolution
$R_k < R_{k+1}$ and$Q_k < Q_{k+1}$ in$\mathcal{O}(K \log K)$ time. -
Adaptive Dynamic Programming: Dynamic bandwidth estimation
$W_i = |R_{\text{len}} - Q_{\text{len}}| + \text{margin} + \text{drift}$ with exponential expansion on boundary collision. -
Round-Trip Invariants: Sequence reconstruction proofs asserting zero hallucination:
$\text{strip_gaps}(\text{aligned}) \equiv \text{original}$ .
- Language: Python 3.11+
- Testing:
pytest - UI/Visualization:
streamlit,pandas,matplotlib
Ensure you have Python 3.11 or later installed.
git clone https://github.com/Aryasurya12/AlignX.git
cd AlignX
python -m pip install -e ".[dev]"Launch the Streamlit research dashboard to run real biological FASTA sequences, simulate Big-O scaling, visualize tracebacks, and explore decision metrics:
streamlit run streamlit_app.pyRun the headless terminal demo exercising biological beta-globin CDS alignment, synthetic indels, memory profiling, and complexity work-elimination:
python demo_full.pyRun the full regression test suite (144 passing unit & integration tests) with coverage reporting:
PYTHONPATH="src" pytest --cov=src/anchoralign --cov-report=term-missing tests/Execute the Phase 7 benchmark pipeline (generates deterministic CSV scalability tests):
PYTHONPATH="src" python -m experiments.run_phase7 --smoke
PYTHONPATH="src" python -m experiments.run_phase7 --scaleCurated sample FASTA sequences from NCBI are located in data/samples/:
- Beta-Globin (HBB):
human_hbb.fastavschimp_hbb.fasta(High homology CDS orthologs) - SARS-CoV-2 Spike RBD:
sars_cov2_wuhan_rbd.fastavssars_cov2_omicron_rbd.fasta(Viral lineage mutations) - TP53 Tumor Suppressor Exon 7:
tp53_wildtype.fastavstp53_mutant.fasta(Hotspot R248W + localized deletion)
For sequence lengths up to 10,000 bp with high homology (e.g., identical matches or sparse mutations), AlignX demonstrates measured CPU speedups of 100x to 500x over native Python
Limitations:
-
Quadratic Fallback: In completely divergent sequences lacking exact anchors, AlignX safely falls back to standard
$\mathcal{O}(N \cdot M)$ complexity. - Constant Factor: Pure Python string slicing poses a constant-factor overhead on high-frequency, extremely short microscopic gaps.
-
Memory Footprint: Peak memory is strictly monitored via
tracemallocand bounded to the size of the largest active gap matrix$\mathcal{O}(\max(g_i \cdot W_i))$ .
v1.0.0 Release Ready. All 7 completion plan phases implemented, 144 tests passing, CI workflow configured, full biological FASTA support added, Big-O complexity analyzer visualizer integrated.
MIT License. See LICENSE for details.