Skip to content

Commit ba95f9c

Browse files
committed
surject: prune anchors in mapper-declared tail regions
1 parent d08602f commit ba95f9c

3 files changed

Lines changed: 79 additions & 11 deletions

File tree

‎src/subcommand/surject_main.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ void help_surject(char** argv) {
7171
<< " -P, --prune-low-cplx prune short/low complexity anchors in realignment" << endl
7272
<< " (on by default for long reads)" << endl
7373
<< " --no-prune-low-cplx disable anchor pruning" << endl
74+
<< " -j, --prune-tail-region prune anchors lying fully inside mapper-declared" << endl
75+
<< " tail regions" << endl
7476
<< " -I, --max-slide N look for offset duplicates of anchors up to N bp" << endl
7577
<< " away when pruning "
7678
<< "(default: " << Surjector::DEFAULT_MAX_SLIDE << ")" << endl
@@ -228,6 +230,7 @@ int main_surject(int argc, char** argv) {
228230
bool validate = true;
229231
bool show_progress = false;
230232
bool left_align = false;
233+
bool prune_tail_region = false;
231234

232235
int c;
233236
optind = 2; // force optind past command positional argument
@@ -261,6 +264,7 @@ int main_surject(int argc, char** argv) {
261264
{"spliced", no_argument, 0, 'S'},
262265
{"prune-low-cplx", no_argument, 0, 'P'},
263266
{"no-prune-low-cplx", no_argument, 0, OPT_NO_PRUNE_LOW_CPLX},
267+
{"prune-tail-region", no_argument, 0, 'j'},
264268
{"max-slide", required_argument, 0, 'I'},
265269
{"max-anchors", required_argument, 0, 'a'},
266270
{"qual-adj", no_argument, 0, 'A'},
@@ -277,7 +281,7 @@ int main_surject(int argc, char** argv) {
277281
};
278282

279283
int option_index = 0;
280-
c = getopt_long (argc, argv, "h?x:p:F:n:lT:g:GmcbsiUf:uBN:R:C:t:D:SPI:a:AE:LHMVw:r",
284+
c = getopt_long (argc, argv, "h?x:p:F:n:lT:g:GmcbsiUf:uBN:R:C:t:D:SPjI:a:AE:LHMVw:r",
281285
long_options, &option_index);
282286

283287
// Detect the end of the options.
@@ -376,6 +380,10 @@ int main_surject(int argc, char** argv) {
376380
prune_anchors = false;
377381
break;
378382

383+
case 'j':
384+
prune_tail_region = true; //remove surject anchors from tails
385+
break;
386+
379387
case 'I':
380388
max_slide = parse<int64_t>(optarg);
381389
break;
@@ -544,6 +552,7 @@ int main_surject(int argc, char** argv) {
544552
default_full_length_bonus);
545553
}
546554
surjector.prune_suspicious_anchors = *prune_anchors;
555+
surjector.prune_tail_region_anchors = prune_tail_region;
547556
surjector.max_slide = max_slide;
548557
surjector.max_anchors = max_anchors;
549558
if (spliced) {

‎src/surjector.cpp‎

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,30 @@ using namespace std;
340340
}
341341
#endif
342342

343-
// we want to remove anchors that can be error-prone: short anchors in the tails and anchors in
344-
// low complexity sequences
343+
// Read mapper-declared tail lengths. These coordinates are
344+
// relative to the stored read sequence and independent of path orientation.
345+
// Missing annotations remain zero, making tail-region pruning a no-op.
346+
size_t left_tail_length = 0, right_tail_length = 0;
347+
if (source_aln) {
348+
if (has_annotation(*source_aln, "left_tail_length")) {
349+
left_tail_length = static_cast<size_t>(get_annotation<double>(*source_aln, "left_tail_length"));
350+
}
351+
if (has_annotation(*source_aln, "right_tail_length")) {
352+
right_tail_length = static_cast<size_t>(get_annotation<double>(*source_aln, "right_tail_length"));
353+
}
354+
}
355+
else {
356+
if (source_mp_aln->has_annotation("left_tail_length")) {
357+
left_tail_length = static_cast<size_t>(*((const double*) source_mp_aln->get_annotation("left_tail_length").second));
358+
}
359+
if (source_mp_aln->has_annotation("right_tail_length")) {
360+
right_tail_length = static_cast<size_t>(*((const double*) source_mp_aln->get_annotation("right_tail_length").second));
361+
}
362+
}
345363
for (auto it = path_overlapping_anchors.begin(); it != path_overlapping_anchors.end(); ++it) {
346364
prune_and_trim_anchors(source_aln ? source_aln->sequence() : source_mp_aln->sequence(),
347-
it->second.first, it->second.second);
365+
it->second.first, it->second.second,
366+
left_tail_length, right_tail_length);
348367
}
349368

350369
// the surjected alignment for each path we overlapped
@@ -4647,10 +4666,11 @@ using namespace std;
46474666
}
46484667

46494668
void Surjector::prune_and_trim_anchors(const string& sequence, vector<path_chunk_t>& path_chunks,
4650-
vector<pair<step_handle_t, step_handle_t>>& step_ranges) const {
4651-
4652-
if (!prune_suspicious_anchors && max_anchors > path_chunks.size()) {
4653-
// the setting don't require us to prune anything here
4669+
vector<pair<step_handle_t, step_handle_t>>& step_ranges,
4670+
size_t left_tail_length, size_t right_tail_length) const {
4671+
4672+
if (!prune_suspicious_anchors && !prune_tail_region_anchors && max_anchors > path_chunks.size()) {
4673+
// the settings don't require us to prune anything here
46544674
return;
46554675
}
46564676

@@ -4674,7 +4694,37 @@ using namespace std;
46744694
}
46754695

46764696
vector<bool> keep(path_chunks.size(), true);
4677-
4697+
4698+
// Clamp tail lengths to the read length
4699+
size_t read_length = sequence.size();
4700+
left_tail_length = std::min(left_tail_length, read_length);
4701+
right_tail_length = std::min(right_tail_length, read_length);
4702+
size_t right_tail_begin = read_length - right_tail_length;
4703+
4704+
if (prune_tail_region_anchors) {
4705+
// Drop anchors whose entire read interval lies inside a mapper-declared tail region.
4706+
for (int i = 0; i < path_chunks.size(); ++i) {
4707+
auto& chunk = path_chunks[i];
4708+
size_t anchor_read_start = chunk.first.first - sequence.begin();
4709+
size_t anchor_read_end = chunk.first.second - sequence.begin();
4710+
4711+
bool fully_in_left_tail = (left_tail_length > 0 && anchor_read_end <= left_tail_length);
4712+
bool fully_in_right_tail = (right_tail_length > 0 && anchor_read_start >= right_tail_begin);
4713+
4714+
if (fully_in_left_tail || fully_in_right_tail) {
4715+
#ifdef debug_anchored_surject
4716+
cerr << "anchor " << i << " (read[" << anchor_read_start << ":" << anchor_read_end
4717+
<< "]) pruned for lying fully inside "
4718+
<< (fully_in_left_tail ? "left" : "right")
4719+
<< " tail region"
4720+
<< " (left_tail_length=" << left_tail_length
4721+
<< ", right_tail_length=" << right_tail_length << ")" << endl;
4722+
#endif
4723+
keep[i] = false;
4724+
}
4725+
}
4726+
}
4727+
46784728
if (prune_suspicious_anchors) {
46794729
#ifdef debug_anchored_surject
46804730
cerr << "pruning suspicious anchors";
@@ -4686,7 +4736,12 @@ using namespace std;
46864736
for (int i = 0; i < path_chunks.size(); ++i) {
46874737
auto& chunk = path_chunks[i];
46884738
// Mark anchors that are themselves suspicious as not to be kept.
4689-
4739+
4740+
if (!keep[i]) {
4741+
// Already pruned by the tail-region check above; skip remaining checks.
4742+
continue;
4743+
}
4744+
46904745
// Short tails
46914746
if ((chunk.first.first == path_chunks.front().first.first || chunk.first.second == path_chunks.back().first.second) // Is at either tail
46924747
&& (anchor_lengths[i] <= max_tail_anchor_prune || chunk.first.second - chunk.first.first <= max_tail_anchor_prune)) { // And is too short

‎src/surjector.hpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ using namespace std;
254254
mutable atomic_flag warned_about_subgraph_size = ATOMIC_FLAG_INIT;
255255

256256
bool prune_suspicious_anchors = false;
257+
/// Remove anchors contained entirely within mapper-declared read tails.
258+
/// Tail coordinates are relative to the stored read sequence.
259+
bool prune_tail_region_anchors = false;
257260
int64_t max_tail_anchor_prune = 4;
258261
static constexpr int64_t DEFAULT_MAX_SLIDE = 6;
259262
/// Declare an anchor suspicious if it appears again at any offset up
@@ -358,7 +361,8 @@ using namespace std;
358361
vector<tuple<size_t, size_t, int32_t>>& connections) const;
359362

360363
void prune_and_trim_anchors(const string& sequence, vector<path_chunk_t>& path_chunks,
361-
vector<pair<step_handle_t, step_handle_t>>& step_ranges) const;
364+
vector<pair<step_handle_t, step_handle_t>>& step_ranges,
365+
size_t left_tail_length = 0, size_t right_tail_length = 0) const;
362366

363367
/// Compute the widest end-inclusive interval of path positions that
364368
/// the realigned sequence could align to, or an interval where start >

0 commit comments

Comments
 (0)