Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/subcommand/surject_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void help_surject(char** argv) {
<< " -P, --prune-low-cplx prune short/low complexity anchors in realignment" << endl
<< " (on by default for long reads)" << endl
<< " --no-prune-low-cplx disable anchor pruning" << endl
<< " -j, --prune-tail-region prune anchors lying fully inside mapper-declared" << endl
<< " tail regions" << endl
<< " -I, --max-slide N look for offset duplicates of anchors up to N bp" << endl
<< " away when pruning "
<< "(default: " << Surjector::DEFAULT_MAX_SLIDE << ")" << endl
Expand Down Expand Up @@ -228,6 +230,7 @@ int main_surject(int argc, char** argv) {
bool validate = true;
bool show_progress = false;
bool left_align = false;
bool prune_tail_region = false;

int c;
optind = 2; // force optind past command positional argument
Expand Down Expand Up @@ -261,6 +264,7 @@ int main_surject(int argc, char** argv) {
{"spliced", no_argument, 0, 'S'},
{"prune-low-cplx", no_argument, 0, 'P'},
{"no-prune-low-cplx", no_argument, 0, OPT_NO_PRUNE_LOW_CPLX},
{"prune-tail-region", no_argument, 0, 'j'},
{"max-slide", required_argument, 0, 'I'},
{"max-anchors", required_argument, 0, 'a'},
{"qual-adj", no_argument, 0, 'A'},
Expand All @@ -277,7 +281,7 @@ int main_surject(int argc, char** argv) {
};

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

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

case 'j':
prune_tail_region = true;
break;

case 'I':
max_slide = parse<int64_t>(optarg);
break;
Expand Down Expand Up @@ -544,6 +552,7 @@ int main_surject(int argc, char** argv) {
default_full_length_bonus);
}
surjector.prune_suspicious_anchors = *prune_anchors;
surjector.prune_tail_region_anchors = prune_tail_region;
surjector.max_slide = max_slide;
surjector.max_anchors = max_anchors;
if (spliced) {
Expand Down
73 changes: 64 additions & 9 deletions src/surjector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,30 @@ using namespace std;
}
#endif

// we want to remove anchors that can be error-prone: short anchors in the tails and anchors in
// low complexity sequences
// Read mapper-declared tail lengths. These coordinates are
// relative to the stored read sequence and independent of path orientation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be so weird for the annotations stored in the read to be relative to some path in the graph and not the read that they are actually on, to the point where I think it is going to confuse the reader to tell them that it isn't the case.

// Missing annotations remain zero, making tail-region pruning a no-op.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The annotation can't remain 0 if it's missing and so doesn't exist to begin with.

I also don't know what the reader would think tail-region pruning would do for a zero-length tail other than nothing. This part isn't actually about the pruning (just the parsing) so we don't need to explain the edge case behavior of the pruning here.

Suggested change
// Missing annotations remain zero, making tail-region pruning a no-op.
// Missing annotations are interpreted as zero.

size_t left_tail_length = 0, right_tail_length = 0;
if (source_aln) {
if (has_annotation(*source_aln, "left_tail_length")) {
left_tail_length = static_cast<size_t>(get_annotation<double>(*source_aln, "left_tail_length"));
}
if (has_annotation(*source_aln, "right_tail_length")) {
right_tail_length = static_cast<size_t>(get_annotation<double>(*source_aln, "right_tail_length"));
}
}
else {
if (source_mp_aln->has_annotation("left_tail_length")) {
left_tail_length = static_cast<size_t>(*((const double*) source_mp_aln->get_annotation("left_tail_length").second));
}
if (source_mp_aln->has_annotation("right_tail_length")) {
right_tail_length = static_cast<size_t>(*((const double*) source_mp_aln->get_annotation("right_tail_length").second));
}
}
for (auto it = path_overlapping_anchors.begin(); it != path_overlapping_anchors.end(); ++it) {
prune_and_trim_anchors(source_aln ? source_aln->sequence() : source_mp_aln->sequence(),
it->second.first, it->second.second);
it->second.first, it->second.second,
left_tail_length, right_tail_length);
}

// the surjected alignment for each path we overlapped
Expand Down Expand Up @@ -4647,10 +4666,11 @@ using namespace std;
}

void Surjector::prune_and_trim_anchors(const string& sequence, vector<path_chunk_t>& path_chunks,
vector<pair<step_handle_t, step_handle_t>>& step_ranges) const {

if (!prune_suspicious_anchors && max_anchors > path_chunks.size()) {
// the setting don't require us to prune anything here
vector<pair<step_handle_t, step_handle_t>>& step_ranges,
size_t left_tail_length, size_t right_tail_length) const {

if (!prune_suspicious_anchors && !prune_tail_region_anchors && max_anchors > path_chunks.size()) {
// the settings don't require us to prune anything here
return;
}

Expand All @@ -4674,7 +4694,37 @@ using namespace std;
}

vector<bool> keep(path_chunks.size(), true);


// Clamp tail lengths to the read length
size_t read_length = sequence.size();
left_tail_length = std::min(left_tail_length, read_length);
right_tail_length = std::min(right_tail_length, read_length);
Comment on lines +4697 to +4700

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we encountering tails longer than reads in real life? Do we have a good excuse for them? If not we might want to fail or warn, because if some of our tail annotations are garbage the rest might also be garbage.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We won't encounter a tail longer than the read right now, but surject can create this situation internally: when it splits a read with supplementary alignments into hard-clipped pieces, it currently copies the full read’s tail annotations unchanged. Eg. - A valid 500 bp tail on a 5 kb read can therefore exceed the length of a 300 bp piece.

I’ve kept the existing clamping for now and added a TODO to translate the annotations into each piece’s coordinates. Clamping prevents out-of-range lengths but does not correct the coordinates. Once this is taken care of, I agree that oversized tails should be reported rather than silently accepted.

But since that might need to address the whole supplementary surjection structure, I'm not introducing that in this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, then I think it makes sense to keep this here.

size_t right_tail_begin = read_length - right_tail_length;

if (prune_tail_region_anchors) {
// Drop anchors whose entire read interval lies inside a mapper-declared tail region.
for (int i = 0; i < path_chunks.size(); ++i) {
auto& chunk = path_chunks[i];
size_t anchor_read_start = chunk.first.first - sequence.begin();
size_t anchor_read_end = chunk.first.second - sequence.begin();

bool fully_in_left_tail = (left_tail_length > 0 && anchor_read_end <= left_tail_length);
bool fully_in_right_tail = (right_tail_length > 0 && anchor_read_start >= right_tail_begin);

if (fully_in_left_tail || fully_in_right_tail) {
#ifdef debug_anchored_surject
cerr << "anchor " << i << " (read[" << anchor_read_start << ":" << anchor_read_end
<< "]) pruned for lying fully inside "
<< (fully_in_left_tail ? "left" : "right")
<< " tail region"
<< " (left_tail_length=" << left_tail_length
<< ", right_tail_length=" << right_tail_length << ")" << endl;
#endif
keep[i] = false;
}
}
}

if (prune_suspicious_anchors) {
#ifdef debug_anchored_surject
cerr << "pruning suspicious anchors";
Expand All @@ -4686,7 +4736,12 @@ using namespace std;
for (int i = 0; i < path_chunks.size(); ++i) {
auto& chunk = path_chunks[i];
// Mark anchors that are themselves suspicious as not to be kept.


if (!keep[i]) {
// Already pruned by the tail-region check above; skip remaining checks.
continue;
}

// Short tails
if ((chunk.first.first == path_chunks.front().first.first || chunk.first.second == path_chunks.back().first.second) // Is at either tail
&& (anchor_lengths[i] <= max_tail_anchor_prune || chunk.first.second - chunk.first.first <= max_tail_anchor_prune)) { // And is too short
Expand Down
6 changes: 5 additions & 1 deletion src/surjector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ using namespace std;
mutable atomic_flag warned_about_subgraph_size = ATOMIC_FLAG_INIT;

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

void prune_and_trim_anchors(const string& sequence, vector<path_chunk_t>& path_chunks,
vector<pair<step_handle_t, step_handle_t>>& step_ranges) const;
vector<pair<step_handle_t, step_handle_t>>& step_ranges,
size_t left_tail_length = 0, size_t right_tail_length = 0) const;

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