-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace HashMaps with a bit-vector for unique depth computation (#201)
- Loading branch information
Showing
4 changed files
with
34 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
use crate::flatgfa; | ||
use std::collections::HashSet; | ||
use bit_vec::BitVec; | ||
|
||
pub fn depth(gfa: &flatgfa::FlatGFA) -> (Vec<usize>, Vec<HashSet<usize>>) { | ||
// Initialize node depth | ||
/// Compute the *depth* of each segment in the variation graph. | ||
/// | ||
/// The depth is defined to be the number of times that a path traverses a given | ||
/// segment. We return two values: the ordinary depth and the *unique* depth, | ||
/// which only counts each path that tarverses a given segment once. | ||
/// | ||
/// Both outputs are depth values indexed by segment ID. | ||
pub fn depth(gfa: &flatgfa::FlatGFA) -> (Vec<usize>, Vec<usize>) { | ||
// Our output vectors: the ordinary and unique depths of each segment. | ||
let mut depths = vec![0; gfa.segs.len()]; | ||
// Initialize uniq_paths | ||
let mut uniq_paths = Vec::<HashSet<usize>>::new(); | ||
uniq_paths.resize(gfa.segs.len(), HashSet::new()); | ||
// do not assume that each handle in `gfa.steps()` is unique | ||
for (idx, path) in gfa.paths.all().iter().enumerate() { | ||
let mut uniq_depths = vec![0; gfa.segs.len()]; | ||
|
||
// This bit vector keeps track of whether the current path has already | ||
// traversed a given segment, and therefore whether we should ignore | ||
// subsequent traversals (for the purpose of counting unique depth). | ||
let mut seen = BitVec::from_elem(gfa.segs.len(), false); | ||
|
||
for path in gfa.paths.all().iter() { | ||
seen.clear(); // All segments are unseen. | ||
for step in &gfa.steps[path.steps] { | ||
let seg_id = step.segment().index(); | ||
// Increment depths | ||
depths[seg_id] += 1; | ||
// Update uniq_paths | ||
uniq_paths[seg_id].insert(idx); | ||
if seen[seg_id] { | ||
// The first traversal of this path over this segment. | ||
uniq_depths[seg_id] += 1; | ||
seen.set(seg_id, true); | ||
} | ||
} | ||
} | ||
|
||
(depths, uniq_paths) | ||
(depths, uniq_depths) | ||
} |