Skip to main content

BkTree

Struct BkTree 

Source
pub struct BkTree { /* private fields */ }
Expand description

BK-tree for efficient Hamming distance queries on UMIs.

A BK-tree (Burkhard-Keller tree) is a metric tree that exploits the triangle inequality to efficiently find all strings within a given edit distance. For Hamming distance queries with threshold k, it achieves O(k * log n) average case complexity instead of O(n) for linear scan.

This implementation uses BitEnc for O(1) Hamming distance computation.

§Algorithm

The tree is built by inserting UMIs one by one:

  1. The first UMI becomes the root
  2. For each subsequent UMI, traverse from root following edges labeled with the Hamming distance to each node’s UMI
  3. Insert as a new child when no edge exists for that distance

To query for all UMIs within distance k of a query:

  1. Compute distance d from query to current node
  2. If d <= k, include this node in results
  3. Recursively search children with edge labels in range [d-k, d+k] (triangle inequality guarantees other children can’t contain matches)

Implementations§

Source§

impl BkTree

Source

pub fn new() -> BkTree

Create a new empty BK-tree.

Source

pub fn from_umis(umis: &[(usize, &str)]) -> Option<BkTree>

Build a BK-tree from a list of UMIs with their indices.

Returns None if any UMI contains non-ACGT bases (cannot be encoded).

Source

pub fn find_within(&self, query: &str, max_distance: u32) -> Vec<(usize, u32)>

Find all UMIs within the given Hamming distance of the query.

Returns a vector of (index, distance) pairs for all matching UMIs. Returns empty if query cannot be encoded (contains non-ACGT bases).

Source

pub fn len(&self) -> usize

Returns the number of UMIs in the tree.

Source

pub fn is_empty(&self) -> bool

Returns true if the tree is empty.

Trait Implementations§

Source§

impl Default for BkTree

Source§

fn default() -> BkTree

Returns the “default value” for a type. 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> 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, 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.