pub struct ReferenceReader { /* private fields */ }Expand description
A thread-safe reference genome reader with all sequences preloaded into memory.
This reader loads the entire FASTA file into memory at construction time,
providing O(1) lookup performance for sequence fetches. This approach matches
fgbio’s nmUqMdTagRegeneratingWriter which reads all contigs into a Map upfront.
For a typical human reference (e.g., hs38DH at ~3GB), this uses approximately 3GB of memory (raw byte storage like htsjdk) and provides the fastest load times.
Implementations§
Source§impl ReferenceReader
impl ReferenceReader
Sourcepub fn new<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self>
Creates a new reference reader, loading all sequences into memory.
This reads the entire FASTA file into memory at construction time. For a typical human reference (~3GB), this takes a few seconds but provides O(1) lookup performance for all subsequent fetches.
§Arguments
path- Path to the reference FASTA file (may be gzipped)
§Errors
Returns an error if:
- The file does not exist
- The file cannot be read or parsed as FASTA
§Examples
use fgumi_lib::reference::ReferenceReader;
let reader = ReferenceReader::new("reference.fasta")?;Sourcepub fn fetch(
&self,
chrom: &str,
start: Position,
end: Position,
) -> Result<Vec<u8>>
pub fn fetch( &self, chrom: &str, start: Position, end: Position, ) -> Result<Vec<u8>>
Retrieves a subsequence from the reference genome.
Since all sequences are preloaded into memory, this is an O(1) lookup followed by a slice copy.
§Arguments
chrom- Chromosome/sequence name (e.g., “chr1”, “1”)start- Start position (1-based, inclusive)end- End position (1-based, inclusive)
§Returns
The requested subsequence as a vector of bytes (preserving original case)
§Errors
Returns an error if:
- The chromosome is not found in the reference
- The requested region exceeds the chromosome length
§Examples
use fgumi_lib::reference::ReferenceReader;
use noodles::core::Position;
let reader = ReferenceReader::new("reference.fasta")?;
// Fetch first 100 bases of chr1
let seq = reader.fetch("chr1", Position::try_from(1)?, Position::try_from(100)?)?;
assert_eq!(seq.len(), 100);Sourcepub fn base_at(&self, chrom: &str, pos: Position) -> Result<u8>
pub fn base_at(&self, chrom: &str, pos: Position) -> Result<u8>
Gets a single base from the reference at the specified position.
This is a convenience method that delegates to fetch() with a single-base region.
§Arguments
chrom- Chromosome/sequence name (e.g., “chr1”, “1”)pos- Position (1-based)
§Returns
The base at the specified position (preserving original case from FASTA)
§Errors
Returns an error if:
- The chromosome is not found in the reference
- The position exceeds the chromosome length
§Examples
use fgumi_lib::reference::ReferenceReader;
use noodles::core::Position;
let reader = ReferenceReader::new("reference.fasta")?;
// Get the base at position 1000 of chr1
let base = reader.base_at("chr1", Position::try_from(1000)?)?;
assert!(matches!(base, b'A' | b'C' | b'G' | b'T' | b'N'));Trait Implementations§
Source§impl Clone for ReferenceReader
impl Clone for ReferenceReader
Source§fn clone(&self) -> ReferenceReader
fn clone(&self) -> ReferenceReader
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for ReferenceReader
impl RefUnwindSafe for ReferenceReader
impl Send for ReferenceReader
impl Sync for ReferenceReader
impl Unpin for ReferenceReader
impl UnsafeUnpin for ReferenceReader
impl UnwindSafe for ReferenceReader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more