Skip to content

Commit 232f7dd

Browse files
committed
surject: test mapper-declared tail pruning
1 parent ba95f9c commit 232f7dd

3 files changed

Lines changed: 300 additions & 2 deletions

File tree

‎src/subcommand/surject_main.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ int main_surject(int argc, char** argv) {
381381
break;
382382

383383
case 'j':
384-
prune_tail_region = true; //remove surject anchors from tails
384+
prune_tail_region = true;
385385
break;
386386

387387
case 'I':

‎src/unittest/surject.cpp‎

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
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+
29266
TEST_CASE( "Spliced surject algorithm preserves deletions against the path", "[surject]" ) {
30267

31268
bdsg::HashGraph graph;

‎test/t/15_vg_surject.t‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ BASH_TAP_ROOT=../deps/bash-tap
55

66
PATH=../bin:$PATH # for vg
77

8-
plan tests 81
8+
plan tests 95
99

1010
vg construct -r small/x.fa >j.vg
1111
vg index -x j.xg j.vg
@@ -288,3 +288,64 @@ vg map -d g -f reads/ts.fq | vg surject -x g.xg -b --off-ref-position - > g.bam
288288
is $(samtools view g.bam | grep "NR:Z:x:8+" | wc -l | sed 's/^[[:space:]]*//') "1" "off reference reads can be annotated with the nearest reference position"
289289

290290
rm g.xg g.gcsa g.gcsa.lcp g.bam
291+
292+
# Reuse the unit-test graph: the input takes 1 -> 6 -> 5, while the
293+
# exact reference placement after tail pruning is 3 -> 4 -> 5.
294+
# Without -u, this fixture already produces the exact placement without
295+
# tail pruning. Enable supplementary output so the baseline retains the
296+
# misplaced tail as a separate alignment, making pruning's effect observable.
297+
tail_dir=$(mktemp -d) || exit 1
298+
tail_seq=ACGTCAGTGCAT
299+
bridge_seq=GATCTAGC
300+
core_seq=TGCAGATCGTACCTGATGCACTAGGTCAGTAC
301+
spacer_seq=$(printf '%080d' 0 | tr 0 C)
302+
printf 'H\tVN:Z:1.0\nS\t1\t%s\nS\t2\t%s\nS\t3\t%s\nS\t4\t%s\nS\t5\t%s\nS\t6\t%s\n' \
303+
"$tail_seq" "$spacer_seq" "$tail_seq" "$bridge_seq" "$core_seq" "$bridge_seq" > "$tail_dir/graph.gfa"
304+
printf 'L\t1\t+\t2\t+\t0M\nL\t2\t+\t3\t+\t0M\nL\t3\t+\t4\t+\t0M\nL\t4\t+\t5\t+\t0M\nL\t1\t+\t6\t+\t0M\nL\t6\t+\t5\t+\t0M\nP\tref\t1+,2+,3+,4+,5+\t*\n' >> "$tail_dir/graph.gfa"
305+
306+
jq -n --arg sequence "${tail_seq}${bridge_seq}${core_seq}" '
307+
{name: "tail-pruning", sequence: $sequence,
308+
annotation: {left_tail_length: 12},
309+
path: {mapping: [
310+
{rank: 1, position: {node_id: "1"},
311+
edit: [{from_length: 12, to_length: 12}]},
312+
{rank: 2, position: {node_id: "6"},
313+
edit: [{from_length: 8, to_length: 8}]},
314+
{rank: 3, position: {node_id: "5"},
315+
edit: [{from_length: 32, to_length: 32}]}
316+
]}}' > "$tail_dir/read.json"
317+
vg view -JGa "$tail_dir/read.json" > "$tail_dir/read.gam"
318+
is "$?" 0 "Tail-pruning annotated GAM fixture is created"
319+
jq 'del(.annotation)' "$tail_dir/read.json" > "$tail_dir/plain.json"
320+
vg view -JGa "$tail_dir/plain.json" > "$tail_dir/plain.gam"
321+
is "$?" 0 "Tail-pruning unannotated GAM fixture is created"
322+
323+
vg surject -x "$tail_dir/graph.gfa" -p ref -t 1 -u -s "$tail_dir/read.gam" > "$tail_dir/baseline.sam"
324+
is "$?" 0 "Annotated input can be surjected without tail pruning"
325+
vg surject -x "$tail_dir/graph.gfa" -p ref -t 1 -u -s --prune-tail-region "$tail_dir/read.gam" > "$tail_dir/long.sam"
326+
is "$?" 0 "The long tail-pruning flag is accepted"
327+
is "$(grep -v '^@' "$tail_dir/long.sam" | cut -f2-4,6)" "$(printf '0\tref\t93\t52M')" \
328+
"Tail pruning produces one exact alignment at the expected reference position"
329+
cmp -s <(grep -v '^@' "$tail_dir/baseline.sam" | cut -f2-4,6) <(grep -v '^@' "$tail_dir/long.sam" | cut -f2-4,6)
330+
is "$?" 1 "Tail pruning changes the misplaced-tail alignment"
331+
332+
vg surject -x "$tail_dir/graph.gfa" -p ref -t 1 -u -s -j "$tail_dir/read.gam" > "$tail_dir/short.sam"
333+
is "$?" 0 "The short tail-pruning flag is accepted"
334+
is "$(grep -v '^@' "$tail_dir/short.sam")" "$(grep -v '^@' "$tail_dir/long.sam")" \
335+
"Short and long tail-pruning flags produce identical alignment records"
336+
337+
vg surject -x "$tail_dir/graph.gfa" -p ref -t 1 -u -s "$tail_dir/plain.gam" > "$tail_dir/plain-baseline.sam"
338+
is "$?" 0 "Unannotated input can be surjected without tail pruning"
339+
vg surject -x "$tail_dir/graph.gfa" -p ref -t 1 -u -s --prune-tail-region "$tail_dir/plain.gam" > "$tail_dir/plain-pruned.sam"
340+
is "$?" 0 "Unannotated input can be surjected with tail pruning"
341+
is "$(grep -v '^@' "$tail_dir/plain-pruned.sam")" "$(grep -v '^@' "$tail_dir/plain-baseline.sam")" \
342+
"Tail pruning leaves unannotated input unchanged"
343+
344+
vg convert "$tail_dir/graph.gfa" -G "$tail_dir/read.gam" -t 1 > "$tail_dir/read.gaf"
345+
is "$?" 0 "Annotated tail-pruning input converts to GAF"
346+
vg surject -x "$tail_dir/graph.gfa" -p ref -t 1 -u -s -G --prune-tail-region "$tail_dir/read.gaf" > "$tail_dir/gaf.sam"
347+
is "$?" 0 "Tail pruning accepts annotated GAF input"
348+
is "$(grep -v '^@' "$tail_dir/gaf.sam" | cut -f2-4,6)" "$(printf '0\tref\t93\t52M')" \
349+
"GAF tail annotations produce the same expected exact placement as GAM"
350+
351+
rm -rf -- "$tail_dir"

0 commit comments

Comments
 (0)