Extending gmlst¶
Step-by-step recipes for new alignment backends, data providers, and CLI commands, including the questions reviewers will ask.
Adding a New Alignment Backend¶
The alignment layer uses a protocol pattern. Start with gmlst/aligners/base.py.
What the protocol requires¶
Every backend must satisfy the Aligner protocol:
namesupports_fastqcheck_dependencies()index(allele_fastas, index_dir)align(sample, index_path, loci, input_type)
The important design rule is this: backend-specific output should be normalized into AlleleMatch and AlignmentResult, so the calling code stays backend-agnostic.
Step-by-step¶
- Create a new module in
gmlst/aligners/, for examplegmlst/aligners/mybackend.py. - Implement a class such as
MyBackendAlignerthat matches theAlignerprotocol ingmlst/aligners/base.py. - Normalize hits into
AlleleMatchrecords. - Return a full
AlignmentResultwithsample_id,matches,failed_loci, backend name, and runtime. - Add the backend to
_REGISTRYingmlst/aligners/__init__.py. - If the backend should be CLI-selectable, make sure it is exposed through
AVAILABLE_BACKENDS, which Click choices ingmlst/commands/typing.pyandgmlst/commands/utils.pyalready use. - Add tests under
test/. - Update docs if user-visible behavior changes.
Good examples to copy¶
gmlst/aligners/blastn.pygmlst/aligners/minimap2.pygmlst/aligners/kma.pygmlst/aligners/nucmer.py
Questions to answer before opening a PR¶
- does it support FASTA, FASTQ, or both?
- how are dependencies checked?
- what external files are written into
index_dir? - how are low-confidence, partial, and missing loci represented?
- does it behave correctly for multicopy hits and read depth, if applicable?
Adding a New Data Provider¶
Provider integrations follow the same protocol-style idea. Start with gmlst/database/providers/base.py.
What the protocol requires¶
Every provider must satisfy the Provider protocol:
namelabellist_schemes()download_scheme()update_scheme()
The shared metadata container is SchemeInfo.
Step-by-step¶
- Create
gmlst/database/providers/<provider>.py. - Implement a provider class that matches the
Providerprotocol. - Return
SchemeInfoobjects fromlist_schemes(). - Download allele FASTA files and profile files into the target directory in
download_scheme(). - Implement
update_scheme()so existing local content can be refreshed. - Register the provider in
gmlst/database/providers/__init__.py. - Verify scheme listing, download, and update flows through
gmlst scheme list,gmlst scheme download, andgmlst scheme update. - Add tests.
- Update docs.
Important cache and naming rule¶
Do not bypass catalog naming logic in gmlst/database/cache.py. DatabaseCache.save_catalog() normalizes names within a provider and then ensures global uniqueness across providers. If you add a provider, reuse the cache layer instead of assigning ad hoc final names yourself.
Good examples to copy¶
gmlst/database/providers/bigsdb.pygmlst/database/providers/enterobase.pygmlst/database/providers/cgmlst.py
Useful manual checks¶
pixi run gmlst scheme list -p pubmlst
pixi run gmlst scheme list -p pasteur
pixi run gmlst scheme list -p enterobase -t cgmlst
pixi run gmlst scheme download -s saureus_1
pixi run gmlst scheme update -s saureus_1
Adding CLI Commands¶
Most CLI work fits into an existing group.
Existing top-level groups¶
typing, defined ingmlst/commands/typing.pyscheme, defined ingmlst/commands/scheme.pyutils, defined ingmlst/commands/utils.pyvisual, defined ingmlst/visual/cli.py
These are registered in gmlst/cli.py.
Step-by-step¶
- Decide whether your command belongs in
typing,scheme,utils, orvisual. - Add a new Click command with
@cmd_typing.command(...),@scheme_group.command(...),@utils_group.command(...), or@visual_group.command(...). - Keep parsing and validation in the Click layer.
- Move heavy logic into a helper module or domain module.
- Update
docs/usage/cli/index.mdfor user-visible command behavior. - Add tests for both success and failure cases.
Pattern to follow¶
- user-facing help text should be clear and concrete
- option names should be consistent with current commands
- command functions should call domain helpers instead of embedding large workflows inline
Backend checklist¶
[ ] class satisfies the Aligner protocol (name, supports_fastq,
check_dependencies, index, align)
[ ] native output normalised into AlleleMatch (identity in %, coverage 0-1)
[ ] AlignmentResult.failed_loci populated for loci without hits
[ ] registered in gmlst/aligners/__init__.py
[ ] added to CLI choices via AVAILABLE_BACKENDS
[ ] unit test with a tiny allele set
Provider checklist¶
[ ] name / label properties
[ ] list_schemes() returns SchemeInfo objects
[ ] download_scheme() writes <locus>.tfa, the profile table, and .meta.json
[ ] update_scheme() returns True when content changed
[ ] registered in gmlst/database/providers/__init__.py
[ ] never assigns final names itself (DatabaseCache does that)
[ ] manual check: scheme list / download / update
CLI command example¶
@utils_group.command("count-loci")
@click.argument("scheme")
def cmd_count_loci(scheme: str) -> None:
"""Print the number of loci in a cached scheme."""
info = DatabaseCache().get_scheme(scheme)
click.echo(info.n_loci)
Then add a test in test/test_utils_commands.py and document the command in
docs/usage/cli/index.md plus its .zh.md sibling.
Backend protocol, in one glance¶
class Aligner(Protocol):
name: str
supports_fastq: bool
def check_dependencies(self) -> None: ...
def index(self, allele_fastas: list[Path], index_dir: Path) -> Path: ...
def align(self, sample, index_path, loci, input_type) -> AlignmentResult: ...
If your class satisfies this shape, the pipeline can use it without importing your module directly.