Alignment Backends¶
The Aligner protocol, AlleleMatch normalization, and how BLAST+, KMA, minimap2, and MUMmer4 plug into one calling pipeline.
Alignment backend architecture¶
Protocol pattern¶
gmlst/aligners/base.py defines the Aligner Protocol. In Python, a Protocol is a structural interface. A backend does not need to inherit from one base class. It only needs to provide the required attributes and methods.
In practice, each aligner implementation must behave like this contract:
namesupports_fastqcheck_dependencies()index(allele_fastas, index_dir)align(sample, index_path, loci, input_type)
This lets gmlst/core/pipeline.py choose a backend once and then use it generically.
AlleleMatch normalization¶
gmlst/aligners/base.py also defines AlleleMatch and AlignmentResult.
Every backend translates its native output into AlleleMatch, including:
- locus name
- allele id
- identity
- coverage
- score
- depth for read-based inputs when available
- extracted sequence for novel allele paths when available
- coordinate and copy-count metadata when available
This normalization is the key reason downstream calling can stay backend-agnostic.
Backend implementations¶
gmlst/aligners/blastn.pytargets BLAST+ and FASTA-heavy workflows.gmlst/aligners/kma.pysupports FASTA and FASTQ, and is the preferred cgMLST FASTQ route.gmlst/aligners/minimap2.pysupports FASTA and FASTQ, plus representative and hash-prefilter driven cgMLST optimizations.gmlst/aligners/nucmer.pytargets MUMmer4 for FASTA alignment.
Minimal backend skeleton¶
import shutil
import time
from pathlib import Path
from gmlst.aligners.base import AlleleMatch, AlignmentResult
class MyToolAligner:
name = "mytool"
supports_fastq = False
def check_dependencies(self) -> None:
if shutil.which("mytool") is None:
raise RuntimeError("mytool is not installed")
def index(self, allele_fastas: list[Path], index_dir: Path) -> Path:
index_dir.mkdir(parents=True, exist_ok=True)
... # write native index files
return index_dir
def align(self, sample, index_path, loci, input_type) -> AlignmentResult:
started = time.monotonic()
hits = ... # run the tool and parse its output
matches = [
AlleleMatch(
locus=hit.locus,
allele_id=hit.allele,
identity=hit.identity, # percent, 0-100
coverage=hit.coverage, # fraction, 0-1
score=hit.score,
depth=None,
sequence=hit.sequence,
)
for hit in hits
]
return AlignmentResult(
sample_id=sample.sample_id,
matches=matches,
failed_loci=[],
backend=self.name,
runtime_seconds=time.monotonic() - started,
)
Choosing a backend¶
| Backend | Input | Strength | Typical cost |
|---|---|---|---|
blastn |
FASTA | precise, forgiving thresholds | slower on large schemes |
minimap2 |
FASTA/FASTQ | fast, good for cgMLST | needs tuning for distant alleles |
nucmer |
FASTA | sensitive whole-genome comparison | slowest |
kma |
FASTA/FASTQ | read consensus, strong for FASTQ | needs k-mer index space |
The pipeline picks minimap2 for FASTA cgMLST and kma for FASTQ; MLST
defaults to blastn. See typing pipeline for where those
defaults are applied.
Normalization rules reviewers check¶
- One
AlleleMatchper (locus, allele) hit, including multicopy hits. identityis a percentage in[0, 100];coverageis a fraction in[0, 1].sequenceis populated only when the backend can extract the allele.failed_locilists loci with no hit, so callers do not have to infer missingness from an empty match list.copy_countand contig coordinates are filled when the backend can report them; downstream code treats them as optional.
Engine switch¶
gmlst/gseek_engine.py wraps every registered backend in GseekAligner.
get_aligner() reads GMLST_ENGINE once per call:
| Mode | Behaviour |
|---|---|
gseek (default) |
delegate planning/parsing to gseek |
legacy |
return the built-in aligner implementation |
shadow |
run both, compare allele calls, log differences, return legacy |
Because the wrapper sits behind get_aligner(), command and pipeline code never
branches on the engine. The wrapper reuses the same allele FASTA materialization
for indexing, so switching engines never invalidates an existing index.
from gmlst.aligners import get_aligner
aligner = get_aligner("blastn") # GseekAligner by default
legacy = get_aligner("blastn") # with GMLST_ENGINE=legacy
If gseek is missing and GMLST_ENGINE is unset, gmlst logs a warning and falls
back to legacy; an explicit GMLST_ENGINE=gseek or shadow raises instead.