77#include " catch.hpp"
88#include " surjector.hpp"
99#include " aligner.hpp"
10+ #include " alignment.hpp"
11+ #include " annotation.hpp"
1012
1113#include " bdsg/hash_graph.hpp"
1214#include " bdsg/overlays/path_position_overlays.hpp"
@@ -22,10 +24,245 @@ class TestSurjector : public Surjector {
2224
2325 using Surjector::extract_overlapping_paths;
2426 using Surjector::filter_redundant_path_chunks;
27+ using Surjector::prune_and_trim_anchors;
2528
2629};
2730
2831
32+ TEST_CASE (" Mapper-declared tails control anchor pruning" ,
33+ " [surject][tail-pruning]" ) {
34+ bdsg::HashGraph graph;
35+ auto path = graph.create_path_handle (" ref" );
36+ vector<handle_t > nodes{
37+ graph.create_handle (" ACGT" ),
38+ graph.create_handle (" TGCA" ),
39+ graph.create_handle (" GACT" )
40+ };
41+ graph.create_edge (nodes[0 ], nodes[1 ]);
42+ graph.create_edge (nodes[1 ], nodes[2 ]);
43+
44+ vector<pair<step_handle_t , step_handle_t >> ranges;
45+ for (auto node : nodes) {
46+ auto step = graph.append_step (path, node);
47+ ranges.emplace_back (step, step);
48+ }
49+ const auto original_ranges = ranges;
50+
51+ bdsg::PositionOverlay pos_graph (&graph);
52+ TestSurjector surjector (&pos_graph);
53+ surjector.prune_suspicious_anchors = false ;
54+ surjector.prune_tail_region_anchors = true ;
55+ surjector.max_anchors = 1000 ;
56+
57+ const string sequence = " ACGTTGCAGACT" ;
58+ vector<Surjector::path_chunk_t > chunks (3 );
59+ for (size_t i = 0 ; i < chunks.size (); ++i) {
60+ chunks[i].first .first = sequence.begin () + 4 * i;
61+ chunks[i].first .second = sequence.begin () + 4 * (i + 1 );
62+ auto * mapping = chunks[i].second .add_mapping ();
63+ mapping->mutable_position ()->set_node_id (graph.get_id (nodes[i]));
64+ auto * edit = mapping->add_edit ();
65+ edit->set_from_length (4 );
66+ edit->set_to_length (4 );
67+ }
68+
69+ size_t left_tail = 0 ;
70+ size_t right_tail = 0 ;
71+ vector<size_t > expected{0 , 1 , 2 };
72+
73+ SECTION (" Disabled flag preserves anchors inside declared tails" ) {
74+ surjector.prune_tail_region_anchors = false ;
75+ left_tail = 4 ;
76+ right_tail = 4 ;
77+ }
78+ SECTION (" Zero tail lengths preserve all anchors" ) {
79+ }
80+ SECTION (" Left tail removes its fully contained anchor" ) {
81+ left_tail = 4 ;
82+ expected = {1 , 2 };
83+ }
84+ SECTION (" Right tail removes its fully contained anchor" ) {
85+ right_tail = 4 ;
86+ expected = {0 , 1 };
87+ }
88+ SECTION (" Both tails leave only the middle anchor" ) {
89+ left_tail = 4 ;
90+ right_tail = 4 ;
91+ expected = {1 };
92+ }
93+ SECTION (" Anchors crossing tail boundaries are retained" ) {
94+ left_tail = 3 ;
95+ right_tail = 3 ;
96+ }
97+
98+ surjector.prune_and_trim_anchors (sequence, chunks, ranges,
99+ left_tail, right_tail);
100+
101+ REQUIRE (chunks.size () == expected.size ());
102+ REQUIRE (ranges.size () == expected.size ());
103+ for (size_t i = 0 ; i < expected.size (); ++i) {
104+ const auto original = expected[i];
105+ CHECK (chunks[i].first .first == sequence.begin () + 4 * original);
106+ CHECK (chunks[i].first .second == sequence.begin () + 4 * (original + 1 ));
107+ REQUIRE (chunks[i].second .mapping_size () == 1 );
108+ CHECK (chunks[i].second .mapping (0 ).position ().node_id ()
109+ == graph.get_id (nodes[original]));
110+ CHECK (ranges[i] == original_ranges[original]);
111+ }
112+ }
113+
114+
115+ TEST_CASE (" Surjection uses tail annotations in either read orientation" ,
116+ " [surject][tail-pruning]" ) {
117+ bdsg::HashGraph graph;
118+
119+ const string tail_sequence = " ACGTCAGTGCAT" ;
120+ const string bridge_sequence = " GATCTAGC" ;
121+ const string core_sequence = " TGCAGATCGTACCTGATGCACTAGGTCAGTAC" ;
122+
123+ auto misplaced_tail = graph.create_handle (tail_sequence);
124+ auto spacer = graph.create_handle (string (80 , ' C' ));
125+ auto correct_tail = graph.create_handle (tail_sequence);
126+ auto reference_bridge = graph.create_handle (bridge_sequence);
127+ auto core = graph.create_handle (core_sequence);
128+ auto alternate_bridge = graph.create_handle (bridge_sequence);
129+
130+ vector<handle_t > reference_nodes{
131+ misplaced_tail, spacer, correct_tail, reference_bridge, core
132+ };
133+ auto reference = graph.create_path_handle (" ref" );
134+ for (size_t i = 0 ; i < reference_nodes.size (); ++i) {
135+ graph.append_step (reference, reference_nodes[i]);
136+ if (i != 0 ) {
137+ graph.create_edge (reference_nodes[i - 1 ], reference_nodes[i]);
138+ }
139+ }
140+ graph.create_edge (misplaced_tail, alternate_bridge);
141+ graph.create_edge (alternate_bridge, core);
142+
143+ bdsg::PositionOverlay pos_graph (&graph);
144+ Surjector surjector (&pos_graph);
145+ surjector.prune_suspicious_anchors = false ;
146+ unordered_set<path_handle_t > paths{reference};
147+
148+ Alignment forward_read;
149+ forward_read.set_name (" tail-pruning" );
150+ string sequence;
151+ for (auto node : vector<handle_t >{
152+ misplaced_tail, alternate_bridge, core}) {
153+ auto * mapping = forward_read.mutable_path ()->add_mapping ();
154+ mapping->set_rank (forward_read.path ().mapping_size ());
155+ mapping->mutable_position ()->set_node_id (graph.get_id (node));
156+ auto * edit = mapping->add_edit ();
157+ edit->set_from_length (graph.get_length (node));
158+ edit->set_to_length (graph.get_length (node));
159+ sequence += graph.get_sequence (node);
160+ }
161+ forward_read.set_sequence (sequence);
162+ forward_read.set_score (
163+ Aligner ().scorer ->score_contiguous_alignment (forward_read));
164+
165+ auto node_length = [&](nid_t node_id) -> int64_t {
166+ return graph.get_length (graph.get_handle (node_id));
167+ };
168+
169+ bool reverse = false ;
170+ SECTION (" Forward read uses its left-tail annotation" ) {
171+ reverse = false ;
172+ }
173+ SECTION (" Reverse-complemented read uses its right-tail annotation" ) {
174+ reverse = true ;
175+ }
176+
177+ Alignment read = reverse
178+ ? reverse_complement_alignment (forward_read, node_length)
179+ : forward_read;
180+
181+ // Keep the whole read and use ordinary, unspliced surjection.
182+ auto project = [&](const Alignment& input, bool prune) {
183+ surjector.prune_tail_region_anchors = prune;
184+ auto result = surjector.surject (input, paths, true , false );
185+ REQUIRE_FALSE (result.empty ());
186+ for (const auto & alignment : result) {
187+ REQUIRE (alignment.path ().mapping_size () > 0 );
188+ REQUIRE (alignment.refpos_size () == 1 );
189+ }
190+ return result;
191+ };
192+
193+ auto check_same_alignment = [&](const Alignment& actual,
194+ const Alignment& expected) {
195+ CHECK (actual.sequence () == expected.sequence ());
196+ CHECK (actual.score () == expected.score ());
197+ CHECK (actual.path ().SerializeAsString ()
198+ == expected.path ().SerializeAsString ());
199+ CHECK (actual.refpos (0 ).name () == expected.refpos (0 ).name ());
200+ CHECK (actual.refpos (0 ).offset () == expected.refpos (0 ).offset ());
201+ CHECK (actual.refpos (0 ).is_reverse ()
202+ == expected.refpos (0 ).is_reverse ());
203+ };
204+
205+ auto check_same_outputs = [&](const vector<Alignment>& actual,
206+ const vector<Alignment>& expected) {
207+ REQUIRE (actual.size () == expected.size ());
208+ for (size_t i = 0 ; i < actual.size (); ++i) {
209+ check_same_alignment (actual[i], expected[i]);
210+ }
211+ };
212+
213+ const auto baseline = project (read, false );
214+ const auto without_annotations = project (read, true );
215+ check_same_outputs (without_annotations, baseline);
216+
217+ // Annotations describe the stored read sequence. Reversing the read
218+ // moves this tail from left to right. Leave the other annotation absent.
219+ Alignment annotated = read;
220+ set_annotation<double >(
221+ annotated,
222+ reverse ? " right_tail_length" : " left_tail_length" ,
223+ static_cast <double >(tail_sequence.size ()));
224+
225+ const auto disabled = project (annotated, false );
226+ check_same_outputs (disabled, baseline);
227+ const auto pruned_outputs = project (annotated, true );
228+ REQUIRE (pruned_outputs.size () == 1 );
229+ const auto & pruned = pruned_outputs.front ();
230+
231+ CHECK (pruned.refpos (0 ).name () == " ref" );
232+ CHECK (pruned.refpos (0 ).is_reverse () == reverse);
233+ CHECK (pruned.refpos (0 ).offset ()
234+ == static_cast <int64_t >(tail_sequence.size ()
235+ + graph.get_length (spacer)));
236+ // Detect a disconnected annotation reader: enabling pruning must
237+ // actually change the placement in this fixture.
238+ for (const auto & alignment : baseline) {
239+ CHECK (pruned.path ().SerializeAsString ()
240+ != alignment.path ().SerializeAsString ());
241+ }
242+
243+ Alignment normalized = reverse
244+ ? reverse_complement_alignment (pruned, node_length)
245+ : pruned;
246+ CHECK (normalized.sequence () == forward_read.sequence ());
247+
248+ const vector<handle_t > expected_nodes{
249+ correct_tail, reference_bridge, core
250+ };
251+ REQUIRE (normalized.path ().mapping_size () == expected_nodes.size ());
252+ for (size_t i = 0 ; i < expected_nodes.size (); ++i) {
253+ const auto & mapping = normalized.path ().mapping (i);
254+ const auto node = expected_nodes[i];
255+ CHECK (mapping.position ().node_id () == graph.get_id (node));
256+ CHECK_FALSE (mapping.position ().is_reverse ());
257+ CHECK (mapping.position ().offset () == 0 );
258+ REQUIRE (mapping.edit_size () == 1 );
259+ CHECK (mapping.edit (0 ).from_length () == graph.get_length (node));
260+ CHECK (mapping.edit (0 ).to_length () == graph.get_length (node));
261+ CHECK (mapping.edit (0 ).sequence ().empty ());
262+ }
263+ }
264+
265+
29266TEST_CASE ( " Spliced surject algorithm preserves deletions against the path" , " [surject]" ) {
30267
31268 bdsg::HashGraph graph;
0 commit comments