|
1 | 1 | use crate::flatgfa; |
2 | | -use std::collections::HashSet; |
| 2 | +use bit_vec::BitVec; |
3 | 3 |
|
4 | | -pub fn depth(gfa: &flatgfa::FlatGFA) -> (Vec<usize>, Vec<HashSet<usize>>) { |
5 | | - // Initialize node depth |
| 4 | +/// Compute the *depth* of each segment in the variation graph. |
| 5 | +/// |
| 6 | +/// The depth is defined to be the number of times that a path traverses a given |
| 7 | +/// segment. We return two values: the ordinary depth and the *unique* depth, |
| 8 | +/// which only counts each path that tarverses a given segment once. |
| 9 | +/// |
| 10 | +/// Both outputs are depth values indexed by segment ID. |
| 11 | +pub fn depth(gfa: &flatgfa::FlatGFA) -> (Vec<usize>, Vec<usize>) { |
| 12 | + // Our output vectors: the ordinary and unique depths of each segment. |
6 | 13 | let mut depths = vec![0; gfa.segs.len()]; |
7 | | - // Initialize uniq_paths |
8 | | - let mut uniq_paths = Vec::<HashSet<usize>>::new(); |
9 | | - uniq_paths.resize(gfa.segs.len(), HashSet::new()); |
10 | | - // do not assume that each handle in `gfa.steps()` is unique |
11 | | - for (idx, path) in gfa.paths.all().iter().enumerate() { |
| 14 | + let mut uniq_depths = vec![0; gfa.segs.len()]; |
| 15 | + |
| 16 | + // This bit vector keeps track of whether the current path has already |
| 17 | + // traversed a given segment, and therefore whether we should ignore |
| 18 | + // subsequent traversals (for the purpose of counting unique depth). |
| 19 | + let mut seen = BitVec::from_elem(gfa.segs.len(), false); |
| 20 | + |
| 21 | + for path in gfa.paths.all().iter() { |
| 22 | + seen.clear(); // All segments are unseen. |
12 | 23 | for step in &gfa.steps[path.steps] { |
13 | 24 | let seg_id = step.segment().index(); |
14 | | - // Increment depths |
15 | 25 | depths[seg_id] += 1; |
16 | | - // Update uniq_paths |
17 | | - uniq_paths[seg_id].insert(idx); |
| 26 | + if seen[seg_id] { |
| 27 | + // The first traversal of this path over this segment. |
| 28 | + uniq_depths[seg_id] += 1; |
| 29 | + seen.set(seg_id, true); |
| 30 | + } |
18 | 31 | } |
19 | 32 | } |
20 | 33 |
|
21 | | - (depths, uniq_paths) |
| 34 | + (depths, uniq_depths) |
22 | 35 | } |
0 commit comments