1use std::collections::{HashMap, HashSet, VecDeque};
2
3use gen_core::{HashId, NodeIntervalBlock, Workspace, is_terminal};
4use gen_graph::{GenGraph, GraphError, GraphNode, GraphNodePosition, MergeGraph};
5use intervaltree::IntervalTree;
6use petgraph::Direction;
7
8use crate::{
9 block_group::BlockGroup, block_group_edge::AugmentedEdge, db::GraphConnection, edge::Edge,
10 node::Node,
11};
12
13pub struct ResolvedGraph {
14 pub graph: GenGraph,
15 pub interval_tree: IntervalTree<i64, NodeIntervalBlock>,
16 pub block_group_id: HashId,
17}
18
19pub fn expand(
22 conn: &GraphConnection,
23 workspace: &Workspace,
24 graph: &mut GenGraph,
25 block_group_id: &HashId,
26 node_id: HashId,
27) -> bool {
28 let edges_1hop = Edge::edges_for_block_group_nodes(conn, block_group_id, &[node_id], None)
29 .unwrap_or_default();
30
31 let mut neighbor_ids: Vec<HashId> = edges_1hop
32 .iter()
33 .flat_map(|ae| [ae.edge.source_node_id, ae.edge.target_node_id])
34 .filter(|id| *id != node_id)
35 .collect();
36 neighbor_ids.sort();
37 neighbor_ids.dedup();
38
39 let mut all_edges: HashMap<HashId, AugmentedEdge> =
40 edges_1hop.into_iter().map(|ae| (ae.edge.id, ae)).collect();
41 if !neighbor_ids.is_empty() {
42 let neighbor_edges =
43 Edge::edges_for_block_group_nodes(conn, block_group_id, &neighbor_ids, None)
44 .unwrap_or_default();
45 for ae in neighbor_edges {
46 all_edges.entry(ae.edge.id).or_insert(ae);
47 }
48 }
49
50 let existing_edge_ids: HashSet<HashId> = graph
51 .all_edges()
52 .flat_map(|(_, _, edges)| edges.iter().map(|e| e.edge_id))
53 .collect();
54
55 let new_edges: Vec<AugmentedEdge> = all_edges
56 .into_values()
57 .filter(|ae| !existing_edge_ids.contains(&ae.edge.id))
58 .collect();
59
60 if new_edges.is_empty() {
61 return false;
62 }
63
64 let fragment =
65 match BlockGroup::get_graph_from_edges(conn, workspace, block_group_id, &new_edges) {
66 Ok(g) => g,
67 Err(_) => return false,
68 };
69 graph.merge_graph(&fragment);
70 true
71}
72
73pub fn find_offset(
79 graph: &mut GenGraph,
80 anchor: &GraphNodePosition,
82 distance: i64,
85 mut expand: impl FnMut(&mut GenGraph, HashId) -> bool,
86) -> Result<Vec<GraphNodePosition>, GraphError> {
87 if distance == 0 {
88 return Ok(vec![*anchor]);
89 }
90
91 match find_offset_with_optional_expansion(graph, anchor, distance, false, &mut expand) {
92 Ok(results) => Ok(results),
93 Err(GraphError::OutOfBounds(_)) => {
94 find_offset_with_optional_expansion(graph, anchor, distance, true, &mut expand)
95 }
96 Err(err) => Err(err),
97 }
98}
99
100fn find_offset_with_optional_expansion(
102 graph: &mut GenGraph,
103 anchor: &GraphNodePosition,
104 distance: i64,
105 expand_through_existing_paths: bool,
107 expand: &mut impl FnMut(&mut GenGraph, HashId) -> bool,
108) -> Result<Vec<GraphNodePosition>, GraphError> {
109 let forward = distance > 0;
110 let mut queue = VecDeque::new();
111 let mut visited = HashSet::new();
112 let mut results = Vec::new();
113 let mut result_seen = HashSet::new();
114
115 queue.push_back((*anchor, distance));
116 visited.insert((*anchor, distance));
117
118 while let Some((pos, remaining)) = queue.pop_front() {
119 let node = pos.graph_node;
120 let node_len = node.length();
121
122 if remaining == 0 {
123 if result_seen.insert(pos) {
124 results.push(pos);
125 }
126 continue;
127 }
128
129 if forward {
130 let in_node = node_len - pos.offset;
131 if remaining <= in_node {
132 let result = GraphNodePosition {
133 graph_node: node,
134 offset: pos.offset + remaining,
135 };
136 if result_seen.insert(result) {
137 results.push(result);
138 }
139 continue;
140 }
141
142 let mut neighbors: Vec<GraphNode> = graph
144 .neighbors_directed(node, Direction::Outgoing)
145 .collect();
146
147 if (neighbors.is_empty() || expand_through_existing_paths)
148 && expand(graph, node.node_id)
149 {
150 neighbors = graph
151 .neighbors_directed(node, Direction::Outgoing)
152 .collect();
153 }
154
155 for neighbor in neighbors {
156 let next_pos = GraphNodePosition {
157 graph_node: neighbor,
158 offset: 0,
159 };
160 let next_remaining = remaining - in_node;
161 if visited.insert((next_pos, next_remaining)) {
162 queue.push_back((next_pos, next_remaining));
163 }
164 }
165 } else {
166 let in_node = pos.offset;
167 let abs_remaining = -remaining;
168 if abs_remaining <= in_node {
169 let result = GraphNodePosition {
170 graph_node: node,
171 offset: pos.offset + remaining,
172 };
173 if result_seen.insert(result) {
174 results.push(result);
175 }
176 continue;
177 }
178
179 let mut neighbors: Vec<GraphNode> = graph
181 .neighbors_directed(node, Direction::Incoming)
182 .collect();
183
184 if (neighbors.is_empty() || expand_through_existing_paths)
185 && expand(graph, node.node_id)
186 {
187 neighbors = graph
188 .neighbors_directed(node, Direction::Incoming)
189 .collect();
190 }
191
192 for neighbor in neighbors {
193 let next_pos = GraphNodePosition {
194 graph_node: neighbor,
195 offset: neighbor.length(),
196 };
197 let next_remaining = remaining + in_node;
198 if visited.insert((next_pos, next_remaining)) {
199 queue.push_back((next_pos, next_remaining));
200 }
201 }
202 }
203 }
204
205 if results.is_empty() {
206 return Err(GraphError::OutOfBounds(distance));
207 }
208
209 Ok(results)
210}
211
212impl ResolvedGraph {
213 pub fn resolve_anchor(
216 &self,
217 coord: i64,
218 conn: &GraphConnection,
219 workspace: &Workspace,
220 ) -> Result<GraphNodePosition, GraphError> {
221 let mut block = None;
222 for item in self.interval_tree.query_point(coord) {
223 let b = &item.value;
224 if !is_terminal(b.node_id) {
225 block = Some(*b);
226 break;
227 }
228 }
229
230 if let Some(b) = block {
231 let offset = coord - b.start;
232 return Ok(GraphNodePosition {
233 graph_node: GraphNode {
234 node_id: b.node_id,
235 sequence_start: b.sequence_start,
236 sequence_end: b.sequence_end,
237 },
238 offset,
239 });
240 }
241
242 let last_block = self
243 .interval_tree
244 .iter()
245 .map(|item| &item.value)
246 .filter(|b| !is_terminal(b.node_id))
247 .max_by_key(|b| b.end)
248 .copied();
249
250 let before_tree = coord < 0;
251 let after_tree = last_block.map(|b| coord >= b.end).unwrap_or(false);
252
253 let boundary_block = if before_tree {
254 self.interval_tree
255 .iter()
256 .map(|item| &item.value)
257 .filter(|b| !is_terminal(b.node_id))
258 .min_by_key(|b| b.start)
259 .copied()
260 } else if after_tree {
261 last_block
262 } else {
263 None
264 };
265
266 let boundary = boundary_block.ok_or(GraphError::NoPath)?;
267 let boundary_node = GraphNode {
268 node_id: boundary.node_id,
269 sequence_start: boundary.sequence_start,
270 sequence_end: boundary.sequence_end,
271 };
272
273 let boundary_anchor = GraphNodePosition {
274 graph_node: boundary_node,
275 offset: if before_tree {
276 0
277 } else {
278 boundary.sequence_end - boundary.sequence_start
279 },
280 };
281
282 let boundary_distance = if before_tree {
283 coord - boundary.start
284 } else {
285 coord - boundary.end
286 };
287
288 let same_node_coordinate = if before_tree {
289 boundary.sequence_start + boundary_distance
290 } else {
291 boundary.sequence_end + boundary_distance
292 };
293 if same_node_coordinate >= 0
294 && let Ok(node_lengths) = Node::query_nodes_length(conn, &[boundary.node_id])
295 && same_node_coordinate <= *node_lengths.get(&boundary.node_id).unwrap_or(&0)
296 {
297 return Ok(GraphNodePosition {
298 graph_node: boundary_node,
299 offset: boundary_anchor.offset + boundary_distance,
300 });
301 }
302
303 let mut expanded_graph = self.graph.clone();
304 expand(
305 conn,
306 workspace,
307 &mut expanded_graph,
308 &self.block_group_id,
309 boundary_node.node_id,
310 );
311
312 let anchors = find_offset(
313 &mut expanded_graph,
314 &boundary_anchor,
315 boundary_distance,
316 |g, nid| expand(conn, workspace, g, &self.block_group_id, nid),
317 )?;
318
319 anchors.into_iter().next().ok_or(GraphError::NoPath)
320 }
321}
322
323#[cfg(test)]
324mod tests {
325 use std::collections::HashSet;
326
327 use gen_core::{PATH_END_NODE_ID, PATH_START_NODE_ID, Strand};
328 use gen_graph::{GraphEdge, graph_from_interval_tree};
329
330 use super::*;
331 use crate::{
332 block_group::{BlockGroup, NewBlockGroup},
333 block_group_edge::{BlockGroupEdge, BlockGroupEdgeData},
334 collection::Collection,
335 node::Node,
336 sample::{NewSample, Sample},
337 sequence::Sequence,
338 test_helpers::{get_connection, test_workspace},
339 };
340
341 fn setup_subset_graph() -> (crate::db::GraphConnection, HashId) {
342 let conn = get_connection(None).unwrap();
343 Collection::get_or_create(&conn, "test").unwrap();
344 Sample::get_or_create(
345 &conn,
346 NewSample {
347 name: "test",
348 ..Default::default()
349 },
350 )
351 .unwrap();
352 let block_group = BlockGroup::create(
353 &conn,
354 NewBlockGroup {
355 collection_name: "test",
356 sample_name: "test",
357 name: "chr1",
358 ..Default::default()
359 },
360 )
361 .unwrap();
362
363 let seq_x = Sequence::new()
364 .sequence_type("DNA")
365 .sequence("XXXXX")
366 .save(&conn)
367 .unwrap();
368 let seq_y = Sequence::new()
369 .sequence_type("DNA")
370 .sequence("YYYYY")
371 .save(&conn)
372 .unwrap();
373 let seq_z = Sequence::new()
374 .sequence_type("DNA")
375 .sequence("ZZZZZ")
376 .save(&conn)
377 .unwrap();
378
379 let node_x = Node::create(&conn, &seq_x.hash, &HashId::convert_str("node-x")).unwrap();
380 let node_y = Node::create(&conn, &seq_y.hash, &HashId::convert_str("node-y")).unwrap();
381 let node_z = Node::create(&conn, &seq_z.hash, &HashId::convert_str("node-z")).unwrap();
382
383 let e_start = Edge::create(
384 &conn,
385 PATH_START_NODE_ID,
386 -1,
387 Strand::Forward,
388 node_x,
389 0,
390 Strand::Forward,
391 )
392 .unwrap();
393 let e_xy = Edge::create(
394 &conn,
395 node_x,
396 5,
397 Strand::Forward,
398 node_y,
399 0,
400 Strand::Forward,
401 )
402 .unwrap();
403 let e_yz = Edge::create(
404 &conn,
405 node_y,
406 5,
407 Strand::Forward,
408 node_z,
409 0,
410 Strand::Forward,
411 )
412 .unwrap();
413 let e_end = Edge::create(
414 &conn,
415 node_z,
416 5,
417 Strand::Forward,
418 PATH_END_NODE_ID,
419 0,
420 Strand::Forward,
421 )
422 .unwrap();
423
424 BlockGroupEdge::bulk_create(
425 &conn,
426 &[
427 BlockGroupEdgeData {
428 block_group_id: block_group.id,
429 edge_id: e_start.id,
430 chromosome_index: 0,
431 phased: 0,
432 },
433 BlockGroupEdgeData {
434 block_group_id: block_group.id,
435 edge_id: e_xy.id,
436 chromosome_index: 0,
437 phased: 0,
438 },
439 BlockGroupEdgeData {
440 block_group_id: block_group.id,
441 edge_id: e_yz.id,
442 chromosome_index: 0,
443 phased: 0,
444 },
445 BlockGroupEdgeData {
446 block_group_id: block_group.id,
447 edge_id: e_end.id,
448 chromosome_index: 0,
449 phased: 0,
450 },
451 ],
452 );
453
454 (conn, block_group.id)
455 }
456
457 fn subset_interval_tree() -> IntervalTree<i64, NodeIntervalBlock> {
458 vec![(
459 0..5,
460 NodeIntervalBlock {
461 node_id: HashId::convert_str("node-y"),
462 start: 0,
463 end: 5,
464 sequence_start: 0,
465 sequence_end: 5,
466 strand: Strand::Forward,
467 },
468 )]
469 .into_iter()
470 .collect()
471 }
472
473 fn test_edge(edge_id: &str) -> Vec<GraphEdge> {
474 vec![GraphEdge {
475 edge_id: HashId::convert_str(edge_id),
476 source_strand: Strand::Forward,
477 target_strand: Strand::Forward,
478 chromosome_index: 0,
479 phased: 0,
480 created_on: 0,
481 }]
482 }
483
484 fn variable_length_branched_graph() -> GenGraph {
485 let node_aaa = GraphNode {
486 node_id: HashId::convert_str("node-aaa"),
487 sequence_start: 0,
488 sequence_end: 3,
489 };
490 let node_cc = GraphNode {
491 node_id: HashId::convert_str("node-cc"),
492 sequence_start: 0,
493 sequence_end: 2,
494 };
495 let node_gggg = GraphNode {
496 node_id: HashId::convert_str("node-gggg"),
497 sequence_start: 0,
498 sequence_end: 4,
499 };
500 let node_ttt = GraphNode {
501 node_id: HashId::convert_str("node-ttt"),
502 sequence_start: 0,
503 sequence_end: 3,
504 };
505
506 let mut graph = GenGraph::new();
507 graph.add_edge(node_aaa, node_cc, test_edge("edge-aaa-cc"));
508 graph.add_edge(node_aaa, node_gggg, test_edge("edge-aaa-gggg"));
509 graph.add_edge(node_cc, node_ttt, test_edge("edge-cc-ttt"));
510 graph.add_edge(node_gggg, node_ttt, test_edge("edge-gggg-ttt"));
511 graph
512 }
513
514 fn position_set(positions: &[GraphNodePosition]) -> HashSet<(HashId, i64)> {
515 positions
516 .iter()
517 .map(|pos| (pos.graph_node.node_id, pos.offset))
518 .collect()
519 }
520
521 #[test]
522 fn test_find_offset_within_subset_node() {
523 let (conn, bg_id) = setup_subset_graph();
524 let tree = subset_interval_tree();
525 let mut graph = graph_from_interval_tree(&tree);
526
527 let y_node = GraphNode {
528 node_id: HashId::convert_str("node-y"),
529 sequence_start: 0,
530 sequence_end: 5,
531 };
532 let anchor = GraphNodePosition {
533 graph_node: y_node,
534 offset: 0,
535 };
536
537 let result = find_offset(&mut graph, &anchor, 3, |g, nid| {
538 expand(&conn, test_workspace(), g, &bg_id, nid)
539 });
540 assert!(
541 result.is_ok(),
542 "find_offset(3) should succeed: {:?}",
543 result.err()
544 );
545 let positions = result.unwrap();
546 assert_eq!(positions.len(), 1);
547 assert_eq!(positions[0].graph_node, y_node);
548 assert_eq!(positions[0].offset, 3);
549 }
550
551 #[test]
552 fn test_find_offset_forward_expands_beyond_subset() {
553 let (conn, bg_id) = setup_subset_graph();
554 let tree = subset_interval_tree();
555 let mut graph = graph_from_interval_tree(&tree);
556
557 let y_node = GraphNode {
558 node_id: HashId::convert_str("node-y"),
559 sequence_start: 0,
560 sequence_end: 5,
561 };
562 let anchor = GraphNodePosition {
563 graph_node: y_node,
564 offset: 0,
565 };
566
567 let result = find_offset(&mut graph, &anchor, 7, |g, nid| {
568 expand(&conn, test_workspace(), g, &bg_id, nid)
569 });
570 assert!(
571 result.is_ok(),
572 "find_offset(7) should expand to Z: {:?}",
573 result.err()
574 );
575 let positions = result.unwrap();
576 assert_eq!(positions.len(), 1);
577 assert_eq!(
578 positions[0].graph_node.node_id,
579 HashId::convert_str("node-z")
580 );
581 assert_eq!(positions[0].offset, 2);
582 }
583
584 #[test]
585 fn test_find_offset_forward_expands_after_existing_paths_fail() {
586 let (conn, bg_id) = setup_subset_graph();
587 let mut graph = GenGraph::new();
588
589 let y_node = GraphNode {
590 node_id: HashId::convert_str("node-y"),
591 sequence_start: 0,
592 sequence_end: 5,
593 };
594 let dead_end = GraphNode {
595 node_id: HashId::convert_str("dead-end"),
596 sequence_start: 0,
597 sequence_end: 1,
598 };
599 graph.add_edge(
600 y_node,
601 dead_end,
602 vec![GraphEdge {
603 edge_id: HashId::convert_str("edge-y-dead"),
604 source_strand: Strand::Forward,
605 target_strand: Strand::Forward,
606 chromosome_index: 0,
607 phased: 0,
608 created_on: 0,
609 }],
610 );
611 let anchor = GraphNodePosition {
612 graph_node: y_node,
613 offset: 0,
614 };
615
616 let result = find_offset(&mut graph, &anchor, 7, |g, nid| {
617 expand(&conn, test_workspace(), g, &bg_id, nid)
618 });
619 assert!(
620 result.is_ok(),
621 "find_offset(7) should expand from Y after the existing path fails: {:?}",
622 result.err()
623 );
624 let positions = result.unwrap();
625 assert_eq!(positions.len(), 1);
626 assert_eq!(
627 positions[0].graph_node.node_id,
628 HashId::convert_str("node-z")
629 );
630 assert_eq!(positions[0].offset, 2);
631 }
632
633 #[test]
634 fn test_find_offset_backward_expands_beyond_subset() {
635 let (conn, bg_id) = setup_subset_graph();
636 let tree = subset_interval_tree();
637 let mut graph = graph_from_interval_tree(&tree);
638
639 let y_node = GraphNode {
640 node_id: HashId::convert_str("node-y"),
641 sequence_start: 0,
642 sequence_end: 5,
643 };
644 let anchor = GraphNodePosition {
645 graph_node: y_node,
646 offset: 0,
647 };
648
649 let result = find_offset(&mut graph, &anchor, -3, |g, nid| {
650 expand(&conn, test_workspace(), g, &bg_id, nid)
651 });
652 assert!(
653 result.is_ok(),
654 "find_offset(-3) should expand to X: {:?}",
655 result.err()
656 );
657 let positions = result.unwrap();
658 assert_eq!(positions.len(), 1);
659 assert_eq!(
660 positions[0].graph_node.node_id,
661 HashId::convert_str("node-x")
662 );
663 assert_eq!(positions[0].offset, 2);
664 }
665
666 #[test]
667 fn test_find_offset_out_of_bounds_with_expansion() {
668 let (conn, bg_id) = setup_subset_graph();
669 let tree = subset_interval_tree();
670 let mut graph = graph_from_interval_tree(&tree);
671
672 let y_node = GraphNode {
673 node_id: HashId::convert_str("node-y"),
674 sequence_start: 0,
675 sequence_end: 5,
676 };
677 let anchor = GraphNodePosition {
678 graph_node: y_node,
679 offset: 0,
680 };
681
682 let result = find_offset(&mut graph, &anchor, 100, |g, nid| {
683 expand(&conn, test_workspace(), g, &bg_id, nid)
684 });
685 assert!(result.is_err());
686 }
687
688 #[test]
689 fn test_find_offset_with_fragment_node() {
690 let (conn, bg_id) = setup_subset_graph();
691
692 let tree: IntervalTree<i64, NodeIntervalBlock> = vec![(
693 0..3,
694 NodeIntervalBlock {
695 node_id: HashId::convert_str("node-y"),
696 start: 0,
697 end: 3,
698 sequence_start: 0,
699 sequence_end: 3,
700 strand: Strand::Forward,
701 },
702 )]
703 .into_iter()
704 .collect();
705 let mut graph = graph_from_interval_tree(&tree);
706
707 let y_frag = GraphNode {
708 node_id: HashId::convert_str("node-y"),
709 sequence_start: 0,
710 sequence_end: 3,
711 };
712 let anchor = GraphNodePosition {
713 graph_node: y_frag,
714 offset: 0,
715 };
716
717 let result = find_offset(&mut graph, &anchor, 2, |g, nid| {
718 expand(&conn, test_workspace(), g, &bg_id, nid)
719 });
720 assert!(result.is_ok());
721 let positions = result.unwrap();
722 assert_eq!(positions[0].graph_node, y_frag);
723 assert_eq!(positions[0].offset, 2);
724 }
725
726 #[test]
727 fn resolve_anchor_before_fragment_stays_on_same_backing_node() {
728 let (conn, bg_id) = setup_subset_graph();
732
733 let tree: IntervalTree<i64, NodeIntervalBlock> = vec![(
734 0..3,
735 NodeIntervalBlock {
736 node_id: HashId::convert_str("node-y"),
737 start: 0,
738 end: 3,
739 sequence_start: 2,
740 sequence_end: 5,
741 strand: Strand::Forward,
742 },
743 )]
744 .into_iter()
745 .collect();
746 let graph = graph_from_interval_tree(&tree);
747 let resolved = ResolvedGraph {
748 graph,
749 interval_tree: tree,
750 block_group_id: bg_id,
751 };
752
753 let position = resolved
754 .resolve_anchor(-2, &conn, test_workspace())
755 .unwrap();
756
757 assert_eq!(
758 position.graph_node,
759 GraphNode {
760 node_id: HashId::convert_str("node-y"),
761 sequence_start: 2,
762 sequence_end: 5,
763 }
764 );
765 assert_eq!(position.offset, -2);
766 assert_eq!(position.coordinate(), 0);
767 }
768
769 #[test]
770 fn test_expand_adds_neighbors() {
771 let (conn, bg_id) = setup_subset_graph();
772 let tree = subset_interval_tree();
773 let mut graph = graph_from_interval_tree(&tree);
774
775 assert_eq!(graph.node_count(), 1);
776
777 let expanded = expand(
778 &conn,
779 test_workspace(),
780 &mut graph,
781 &bg_id,
782 HashId::convert_str("node-y"),
783 );
784 assert!(expanded, "expand should add new nodes");
785 assert!(
786 graph.node_count() > 1,
787 "graph should have more nodes after expansion"
788 );
789
790 let node_ids: Vec<HashId> = graph.nodes().map(|n| n.node_id).collect();
791 assert!(
792 node_ids.contains(&HashId::convert_str("node-x")),
793 "X should be added after expanding Y"
794 );
795 assert!(
796 node_ids.contains(&HashId::convert_str("node-z")),
797 "Z should be added after expanding Y"
798 );
799 }
800
801 #[test]
802 fn test_find_offset_in_variable_length_branch_finds_middle_nodes() {
803 let mut graph = variable_length_branched_graph();
804 let aaa_anchor = GraphNodePosition {
805 graph_node: GraphNode {
806 node_id: HashId::convert_str("node-aaa"),
807 sequence_start: 0,
808 sequence_end: 3,
809 },
810 offset: 2,
811 };
812
813 let from_aaa = find_offset(&mut graph, &aaa_anchor, 2, |_, _| false).unwrap();
814 assert_eq!(
815 position_set(&from_aaa),
816 HashSet::from([
817 (HashId::convert_str("node-cc"), 1),
818 (HashId::convert_str("node-gggg"), 1)
819 ])
820 );
821
822 let ttt_anchor = GraphNodePosition {
823 graph_node: GraphNode {
824 node_id: HashId::convert_str("node-ttt"),
825 sequence_start: 0,
826 sequence_end: 3,
827 },
828 offset: 0,
829 };
830 let from_ttt = find_offset(&mut graph, &ttt_anchor, -2, |_, _| false).unwrap();
831 assert_eq!(
832 position_set(&from_ttt),
833 HashSet::from([
834 (HashId::convert_str("node-cc"), 0),
835 (HashId::convert_str("node-gggg"), 2)
836 ])
837 );
838 }
839
840 #[test]
841 fn test_find_offset_in_variable_length_branch_returns_single_position_within_node() {
842 let mut graph = variable_length_branched_graph();
843 let anchor = GraphNodePosition {
844 graph_node: GraphNode {
845 node_id: HashId::convert_str("node-aaa"),
846 sequence_start: 0,
847 sequence_end: 3,
848 },
849 offset: 1,
850 };
851
852 let positions = find_offset(&mut graph, &anchor, 1, |_, _| false).unwrap();
853 assert_eq!(
854 position_set(&positions),
855 HashSet::from([(HashId::convert_str("node-aaa"), 2)])
856 );
857 }
858
859 #[test]
860 fn test_find_offset_in_variable_length_branch_finds_different_ttt_offsets() {
861 let mut graph = variable_length_branched_graph();
862 let anchor = GraphNodePosition {
863 graph_node: GraphNode {
864 node_id: HashId::convert_str("node-aaa"),
865 sequence_start: 0,
866 sequence_end: 3,
867 },
868 offset: 2,
869 };
870
871 let positions = find_offset(&mut graph, &anchor, 6, |_, _| false).unwrap();
872 assert_eq!(
873 position_set(&positions),
874 HashSet::from([
875 (HashId::convert_str("node-ttt"), 1),
876 (HashId::convert_str("node-ttt"), 3)
877 ])
878 );
879 }
880}