Skip to main content

ReferenceReader

Struct ReferenceReader 

Source
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

Source

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")?;
Source

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);
Source

pub fn fetch_slice( &self, chrom: &str, start: Position, end: Position, ) -> Result<&[u8]>

Borrowed-slice variant of [fetch] that returns a reference into the in-memory sequence — same lookup semantics, no allocation.

Prefer this over [fetch] in hot loops (e.g., per-record reference access in fgumi zipper’s --restore-unconverted-bases path) where allocating a fresh Vec<u8> per call adds up to gigabytes of churn.

§Errors

Same as [fetch]: returns an error if the chromosome is missing or the requested region exceeds the sequence length.

Source

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

Source§

fn clone(&self) -> ReferenceReader

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl RefBaseProvider for ReferenceReader

Available on crate feature simplex only.
Source§

fn base_at_0based(&self, chrom: &str, pos: u64) -> Option<u8>

Returns the base at a 0-based position, or None if out of bounds.
Source§

fn sequence_for(&self, chrom: &str) -> Option<&[u8]>

Returns the full sequence for a chromosome, or None if not found. Read more
Source§

impl ReferenceProvider for ReferenceReader

Source§

fn fetch(&self, chrom: &str, start: Position, end: Position) -> Result<Vec<u8>>

Fetches a subsequence from the reference genome. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V