kaptive.db.manager¶
Database management module for downloading, compiling, and managing Kaptive databases.
This module defines the DatabaseManager class, which acts as the core controller
for managing Kaptive surface antigen database assets both locally and remotely.
Databases are curated as source GenBank (.gbk) and metadata (.toml) files in remote GitHub repositories.
DatabaseManager handles remote retrieval over HTTP, version evaluation via
DatabaseMetadata, on-the-fly parsing and compilation into optimized
Database instances, local caching in ~/.kaptive, fast disk serialization (pickling),
and lifecycle commands (install, update, uninstall, reset, add, load, save).
Classes:
-
DatabaseManager–Class managing local database storage and remote GitHub database retrieval.
DatabaseManager
¶
Class for managing Kaptive databases both on the user's disk and in curator GitHub repositories.
This class provides a comprehensive mechanism for downloading, compiling, and managing Kaptive databases.
Databases are maintained as source files (GenBank and TOML) in Git repositories. The
DatabaseManager fetches these files, compiles them into optimized, flat
Database objects (using a Structure-of-Arrays layout for vectorized operations),
and stores them locally as serialized pickle files (.pkl) alongside .json metadata sidecars in the user's
local directory (defaults to ~/.kaptive or $KAPTIVE_DB_DIR).
The manager handles:
- Installation: Fetching a known database from its remote repository
(
install), or a custom database from any GitHub repository (add), compiling it, and caching the result locally. - Updates: Checking the local compiled database against the remote repository's version (specified in
the TOML metadata) and downloading/recompiling if a newer version exists
(
update). - Storage & Retrieval: Saving (
save) and loading (load) these compiled.pklfiles efficiently. - Lifecycle Management: Uninstalling specific databases
(
uninstall) or completely resetting the local cache (reset).
Attributes:
-
_KNOWN(dict[str, tuple[str, str, str]]) –Internal lookup mapping of officially supported database keywords to tuples of
(repository_owner, repository_name, database_base_name). -
_DB_DIR(Path) –Local cache directory path where
.pkldatabase files and.jsonmetadata sidecars are stored.
Methods:
-
add–Add or update a database directly from a specified remote Git repository.
-
get–Load a Database from a file path or resolve and load it by keyword.
-
install–Install known, officially supported databases by keyword.
-
installed–Return a list of keywords for all currently installed databases.
-
known–Return a list of keywords for all currently known, officially supported databases.
-
load–Load a locally installed, compiled database using its keyword.
-
reset–Remove all installed databases by deleting their compiled files from the local directory.
-
save–Serialize and save a compiled Database object and its metadata to local storage.
-
uninstall–Uninstall a specific database by removing its compiled local
.pkland.jsonfiles. -
update–Update installed databases by checking against their remote GitHub repositories.
add
classmethod
¶
add(owner: str, repo_name: str, db_name: str, branch: str = 'main', local_meta: DatabaseMetadata | None = None) -> Database | None
Add or update a database directly from a specified remote Git repository.
This is the primary method for adding custom or official databases from GitHub. The procedure:
- Constructs raw GitHub URL endpoints for the repository's
.tomlmetadata and.gbkGenBank files. - Downloads and parses the remote TOML metadata to extract version information.
- Compares the remote version against local metadata (if available). If up-to-date, skips remaining steps and
returns
None. - Downloads the raw GenBank file content over HTTP.
- Writes source files into a temporary directory and compiles them using
from_genbank. - Serializes and caches the compiled
Databaseobject into the local storage directory.
Parameters:
-
(owner¶str) –Owner or organization of the GitHub repository (e.g.,
'klebgenomics'). -
(repo_name¶str) –Name of the GitHub repository (e.g.,
'KpSC_surface_antigen_loci'). -
(db_name¶str) –Base name of the database files in the repository (e.g.,
'Klebsiella_pneumoniae_Species_Complex_K'). -
(branch¶str, default:'main') –Git branch name to fetch from. Defaults to
'main'. -
(local_meta¶DatabaseMetadata | None, default:None) –Pre-loaded metadata of local database installation. Defaults to
None.
Returns:
-
Database | None–Database | None: The newly compiled
Databaseobject if installed or updated, orNoneif the local version was already up-to-date.
Raises:
-
DatabaseError–If repository files are not found, network issues occur, or file compilation fails.
See Also
Source code in src/kaptive/db/manager.py
get
classmethod
¶
get(file_or_keyword: str | Path) -> Database
Load a Database from a file path or resolve and load it by keyword.
If file_or_keyword points to an existing file, it is loaded directly.
Otherwise, it is treated as a keyword. If the keyword is not installed locally,
it will be automatically downloaded and installed.
Parameters:
Returns:
Source code in src/kaptive/db/manager.py
install
classmethod
¶
Install known, officially supported databases by keyword.
Looks up the repository details (owner, repo, database name) associated with the provided keyword(s)
in the internal registry (_KNOWN) and delegates file retrieval
and compilation to add. If 'all' or a list of keywords is
supplied, fetching is performed concurrently via a thread pool executor.
Parameters:
-
(kwd¶str | list[str]) –The keyword(s) of the known database(s) to install (e.g.,
'kpsc_k',['kpsc_k', 'ab_k'], or'all').
Returns:
Raises:
-
DatabaseError–If any keyword is not recognized in the list of known databases, or if network/parsing errors occur.
See Also
Source code in src/kaptive/db/manager.py
installed
classmethod
¶
Return a list of keywords for all currently installed databases.
Scans the local storage directory for .pkl files and extracts their keywords from the file stems.
Returns:
-
list[str]–list[str]: A list of database keywords corresponding to installed
.pkldatabase files. Returns an empty list if no databases are installed or if the storage directory does not exist.
See Also
Source code in src/kaptive/db/manager.py
known
classmethod
¶
Return a list of keywords for all currently known, officially supported databases.
These databases can be installed directly by providing their keyword to
install.
Returns:
-
list[str]–list[str]: A list of known database keywords (e.g.,
['kpsc_k', 'kpsc_o', 'kosc_k', ...]).
See Also
Source code in src/kaptive/db/manager.py
load
classmethod
¶
Load a locally installed, compiled database using its keyword.
Reads and unpickles the serialized .pkl database file from the local cache directory.
Parameters:
Returns:
Raises:
-
DatabaseError–If the specified database is not installed locally.
See Also
Source code in src/kaptive/db/manager.py
reset
classmethod
¶
Remove all installed databases by deleting their compiled files from the local directory.
This clears the user's ~/.kaptive cache directory of any .pkl database files and .json metadata
sidecar files, effectively uninstalling all downloaded and compiled databases.
Returns:
-
None–None
See Also
Source code in src/kaptive/db/manager.py
save
classmethod
¶
Serialize and save a compiled Database object and its metadata to local storage.
Saves the database as a .pkl file named {keyword}.pkl in the local cache directory (_DB_DIR).
Also writes a companion {keyword}.json file containing the serialized metadata for fast version checking.
Parameters:
Returns:
-
int(int) –The total number of bytes written to the
.pkldatabase file.
See Also
Source code in src/kaptive/db/manager.py
uninstall
classmethod
¶
Uninstall a specific database by removing its compiled local .pkl and .json files.
Parameters:
Returns:
-
None–None
Raises:
-
DatabaseError–If the specified database is not currently installed locally.
See Also
Source code in src/kaptive/db/manager.py
update
classmethod
¶
Update installed databases by checking against their remote GitHub repositories.
Extracts local metadata to determine the source repository and version, then checks the remote GitHub
repository for a newer version. If a newer version is available, the source files (.gbk and .toml)
are fetched, compiled into a new Database object, and saved to disk. When
updating multiple databases or "all", remote fetches are executed concurrently using a thread pool executor.
Parameters:
-
(kwd¶str | list[str], default:'all') –The keyword(s) of the database to update (e.g.,
'kpsc_k',['kpsc_k', 'ab_k'], or'all'). Defaults to"all", which updates all currently installed databases.
Yields:
-
Database(Database) –The newly compiled
Databaseobject for each database that required an update. Databases that are already up-to-date yield nothing.
Raises:
-
DatabaseError–If a requested database is not installed locally, or if network/parsing failures occur during update.
See Also
installed,
add,
DatabaseManager,
Database,
DatabaseMetadata,
DatabaseError