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
| Python | |
|---|---|
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.