kaptive.core.genome¶
Module for loading, parsing, and managing genomic sequence assemblies and FASTA I/O.
This module provides high-performance FASTA file reading using rammappy Rust bindings
and encapsulates genome assemblies in the GenomeAssembly
dataclass, supported by transparent decompression (.gz, .bz2, .xz).
Classes:
-
FastaReaderβHigh-performance FASTA file iterator.
-
GenomeAssemblyβContainer for a genome assembly with support for transparent decompression.
FastaReader
¶
flowchart TD
kaptive.core.genome.FastaReader[FastaReader]
click kaptive.core.genome.FastaReader href "" "kaptive.core.genome.FastaReader"
High-performance FASTA file iterator.
Parses raw binary FASTA streams into SeqRecord instances
using optimized C/Rust bindings via rammappy. Assumes the input handle is opened in
binary mode ('rb').
Parameters:
Parameters:
Methods:
-
__del__βClean up resources by closing the underlying binary handle if still open.
-
__enter__βEnter the runtime context for the FASTA reader.
-
__exit__βExit the runtime context and close the underlying binary stream.
-
__iter__βReturn the iterator object itself.
-
__next__βFetch the next sequence record from the parsed FASTA stream.
Source code in src/kaptive/core/genome.py
__del__
¶
__enter__
¶
__enter__() -> Self
Enter the runtime context for the FASTA reader.
Returns:
-
FastaReader(Self) βThe current reader context manager instance.
__exit__
¶
Exit the runtime context and close the underlying binary stream.
Parameters:
-
(exc_type¶Any) βException type if an exception was raised inside context.
-
(exc_val¶Any) βException instance if an exception was raised.
-
(exc_tb¶Any) βTraceback object if an exception was raised.
Source code in src/kaptive/core/genome.py
__next__
¶
__next__() -> SeqRecord
Fetch the next sequence record from the parsed FASTA stream.
Returns:
Raises:
-
StopIterationβWhen all FASTA records have been consumed.
Source code in src/kaptive/core/genome.py
GenomeAssembly
dataclass
¶
Container for a genome assembly with support for transparent decompression.
Stores contig sequence data as a contiguous memory block using Sequences
and supports thread-safe, lazy-cached index creation for high-performance sequence alignment.
Attributes:
-
id(str) βUnique genome assembly identifier.
-
contigs(Sequences) βStructure-of-Arrays container
Sequencesholding all contig sequences. -
id_map(dict[str, int]) βMapping from contig ID to sequence index offset within
contigs. -
rammappy_index(Any) βLazy-cached
rammappy.Indexobject for sequence lookups.
Methods:
-
__getitem__βRetrieve raw sequence bytes for a contig by its identifier.
-
__iter__βIterate over contigs in the assembly.
-
__len__βCalculate the total number of base pairs across all contigs in the assembly.
-
__post_init__βInitialize derived fields such as
id_mapafter dataclass creation. -
__str__βReturn the string representation of the assembly.
-
ensureβEnsure the input object is coerced into a
GenomeAssembly. -
from_fileβLoad a genome assembly from a FASTA file path.
-
from_recordsβConstruct a
GenomeAssemblyfrom sequence records. -
from_streamβLoad a genome assembly from an open binary stream.
-
get_rammappy_indexβLazily build and return a thread-safe cached
rammappy.Indexfor the assembly.
__getitem__
¶
Retrieve raw sequence bytes for a contig by its identifier.
Parameters:
Returns:
-
bytes(bytes) βContig sequence bytes.
Raises:
-
KeyErrorβIf
itemis not a recognized contig identifier inid_map.
Source code in src/kaptive/core/genome.py
__iter__
¶
Iterate over contigs in the assembly.
Returns:
__len__
¶
__len__() -> int
Calculate the total number of base pairs across all contigs in the assembly.
Returns:
-
int(int) βCumulative sequence length in base pairs.
__post_init__
¶
Initialize derived fields such as id_map after dataclass creation.
__str__
¶
__str__() -> str
Return the string representation of the assembly.
Returns:
-
str(str) βAssembly identifier string (
id).
ensure
classmethod
¶
Ensure the input object is coerced into a GenomeAssembly.
Parameters:
-
(genome¶Self | str | Path | IO[bytes]) βExisting assembly object, file path, or binary FASTA stream.
Returns:
-
GenomeAssembly(Self) βValidated or loaded
GenomeAssemblyinstance.
Source code in src/kaptive/core/genome.py
from_file
classmethod
¶
Load a genome assembly from a FASTA file path.
Supports plain .fasta / .fa / .fna files as well as compressed
.gz, .bz2, and .xz files.
Parameters:
Returns:
-
GenomeAssembly(Self) βLoaded
GenomeAssemblyinstance.
Raises:
-
NotImplementedErrorβIf the file format or compression extension is unsupported.
Source code in src/kaptive/core/genome.py
from_records
classmethod
¶
Construct a GenomeAssembly from sequence records.
Parameters:
-
(id_¶str) βGenome assembly identifier.
-
(records¶Iterable[SeqRecord]) βIterable of
SeqRecordobjects.
Returns:
-
GenomeAssembly(Self) βConstructed
GenomeAssemblyinstance.
Source code in src/kaptive/core/genome.py
from_stream
classmethod
¶
Load a genome assembly from an open binary stream.
Parameters:
-
(handle¶IO[bytes]) βOpen binary FASTA stream handle.
-
(id_¶str | None, default:None) βCustom assembly identifier. If None, derived from handle name or defaults to
'unknown'.
Returns:
-
GenomeAssembly(Self) βLoaded
GenomeAssemblyinstance.
Source code in src/kaptive/core/genome.py
get_rammappy_index
¶
get_rammappy_index() -> Any
Lazily build and return a thread-safe cached rammappy.Index for the assembly.
Returns:
-
Any(Any) βThe compiled
rammappy.Indexinstance.