Introduce new hashing mechanisms and support for directional unitig checks. Fix fragmentation issues and improve performance metrics through optimized graph construction and new benchmarking utilities.
96 lines
3.1 KiB
Rust
96 lines
3.1 KiB
Rust
use obikseq::{CanonicalKmer, Sequence};
|
|
use std::sync::atomic::{AtomicU8, Ordering};
|
|
|
|
use super::graph::GraphDeBruijn;
|
|
use super::node::Node;
|
|
|
|
/// A position in a walk: the node's kmer, its flags as read when the step was
|
|
/// taken, and the node's own table entry. Keeping `atomic` means the caller
|
|
/// that wants to mark the node visited does not look the kmer up a second
|
|
/// time (a random access in a table far larger than the cache).
|
|
pub struct WalkState<'g> {
|
|
pub(super) kmer: CanonicalKmer,
|
|
pub(super) node: Node,
|
|
pub(super) direct: bool,
|
|
pub(super) atomic: &'g AtomicU8,
|
|
}
|
|
|
|
impl<'g> WalkState<'g> {
|
|
pub fn new(kmer: CanonicalKmer, node: Node, direct: bool, atomic: &'g AtomicU8) -> Self {
|
|
debug_assert!(!node.is_visited(), "Cannot walk over a visited node");
|
|
Self { kmer, node, direct, atomic }
|
|
}
|
|
|
|
pub fn leavable(&self, graph: &'g GraphDeBruijn) -> bool {
|
|
self.walk(graph).is_some()
|
|
}
|
|
|
|
pub fn reachable(&self, graph: &'g GraphDeBruijn) -> bool {
|
|
WalkState {
|
|
kmer: self.kmer,
|
|
node: self.node,
|
|
direct: !self.direct,
|
|
atomic: self.atomic,
|
|
}
|
|
.leavable(graph)
|
|
}
|
|
|
|
pub fn walk(&self, graph: &'g GraphDeBruijn) -> Option<(WalkState<'g>, u8)> {
|
|
if self.direct {
|
|
if !self.node.can_extend_right() {
|
|
return None;
|
|
}
|
|
let nuc = self.node.right_nuc();
|
|
let next = self.kmer.into_kmer().push_right(nuc);
|
|
let cnext = next.canonical();
|
|
let dnext = next.raw() == cnext.raw();
|
|
let next_atomic = graph.nodes.get(&cnext).unwrap();
|
|
let next_node = Node(next_atomic.load(Ordering::Relaxed));
|
|
if next_node.is_visited() {
|
|
return None;
|
|
}
|
|
let reachable = if dnext {
|
|
next_node.can_extend_left()
|
|
} else {
|
|
next_node.can_extend_right()
|
|
};
|
|
reachable.then_some((
|
|
WalkState {
|
|
kmer: cnext,
|
|
node: next_node,
|
|
direct: dnext,
|
|
atomic: next_atomic,
|
|
},
|
|
nuc,
|
|
))
|
|
} else {
|
|
if !self.node.can_extend_left() {
|
|
return None;
|
|
}
|
|
let nuc = self.node.left_nuc();
|
|
let next = self.kmer.into_kmer().push_left(nuc);
|
|
let cnext = next.canonical();
|
|
let dnext = next.raw() != cnext.raw();
|
|
let next_atomic = graph.nodes.get(&cnext).unwrap();
|
|
let next_node = Node(next_atomic.load(Ordering::Relaxed));
|
|
if next_node.is_visited() {
|
|
return None;
|
|
}
|
|
let reachable = if dnext {
|
|
next_node.can_extend_left()
|
|
} else {
|
|
next_node.can_extend_right()
|
|
};
|
|
reachable.then_some((
|
|
WalkState {
|
|
kmer: cnext,
|
|
node: next_node,
|
|
direct: dnext,
|
|
atomic: next_atomic,
|
|
},
|
|
3 - nuc,
|
|
))
|
|
}
|
|
}
|
|
}
|