Skip to content

surject changes: --diploid-map for long-read placements - #5054

Open
Sagorikanag wants to merge 7 commits into
surject-stack-03-target-path-slidingfrom
surject-stack-04-diploid-long-read
Open

Sagorikanag wants to merge 7 commits into
surject-stack-03-target-path-slidingfrom
surject-stack-04-diploid-long-read

Conversation

@Sagorikanag

@Sagorikanag Sagorikanag commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Changelog Entry

  • vg surject can jointly surject multiple graph placements of the same read onto a diploid reference assembly with --diploid-map.

Description

This adds diploid long-read surjection for name-grouped GAM/GAF input.

For each read, vg surject --diploid-map NAME:

  • groups consecutive graph placements with the same read name
  • surjects each placement to all matching paths in the selected assembly
  • preserves each candidate's supplementary pieces
  • chooses a preferred haplotype-path candidate within each graph placement
  • chooses one overall primary candidate across graph placements
  • reports haplotype and source-mapping confidence through typed annotations, serialized as hp, hq, and aq

The grouped-placement operation lives in Surjector rather than the CLI so the selection and quality behavior can be unit tested directly.

This layer also rejects unsupported paired/interleaved and GAMP diploid input instead of silently falling back to ordinary surjection.

Depends on:

Stacked on #5052.
Depends on vgteam/libvgio#96.

Testing

  • 722 surjection assertions pass
  • 112 vg surject CLI tests pass
  • tests cover grouped placements, primary selection, haplotype selection and quality, MAPQ handling, duplicate graph placements, unmapped output, and supplementary/SA relationships

Remaining work

  • RNG-compatible tie-breaking

Stack created with GitHub Stacks CLI • Give Feedback 💬

@Sagorikanag
Sagorikanag added this pull request to stack #5053 October 2, 2026 17:26
@Sagorikanag
Sagorikanag force-pushed the surject-stack-04-diploid-long-read branch from ff5aa7a to 6c1c44d Compare October 7, 2026 05:18
@Sagorikanag
Sagorikanag force-pushed the surject-stack-04-diploid-long-read branch 2 times, most recently from 049416c to b0d4981 Compare October 7, 2026 19:49
@Sagorikanag
Sagorikanag force-pushed the surject-stack-04-diploid-long-read branch from 067c569 to e1e3dd0 Compare October 21, 2026 12:39
@Sagorikanag
Sagorikanag force-pushed the surject-stack-04-diploid-long-read branch 2 times, most recently from 7f44980 to 9f40d55 Compare October 21, 2026 13:53
@adamnovak adamnovak added the mapping Theme/team for mapping to pangenomes label Nov 2, 2026
@Sagorikanag
Sagorikanag force-pushed the surject-stack-04-diploid-long-read branch from 91a7154 to c862557 Compare November 4, 2026 18:38
@adamnovak
adamnovak force-pushed the surject-stack-04-diploid-long-read branch from c862557 to c755cb9 Compare November 9, 2026 17:22
@adamnovak
adamnovak force-pushed the surject-stack-04-diploid-long-read branch from c755cb9 to c862557 Compare November 9, 2026 17:39

@adamnovak adamnovak left a comment

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.

This looks like it's written pretty well.

Comment thread src/surjector.cpp Outdated
if (annotate_with_graph_alignment) {
annotate_graph_cigar(surjected, source, positions);
}
const size_t first_candidate = candidates.size();

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 JavaScript-flavored consts on random locals are back.

Comment thread src/surjector.cpp
Comment on lines +149 to +151
if (source.mapping_quality() < 0 || source.mapping_quality() > 255) {
throw invalid_argument("diploid input MAPQ must be between 0 and 255");
}

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.

This might be better handled by clamping the input MAPQ, at least on the high end. If someobdy comes up with a tool that decides to send MAPQs of 1024 or something, I don't know that that's a good reason to crash the program. It's allowed by the format.

Comment on lines +647 to +649
} catch (const std::exception& ex) {
report_exception(ex);
}

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.

Since we have a lot of exceptions that surject_diploid can throw that amount to "Bad user! Don't do that! It's not implemented!", or "Your data file seems incoherent", we might consider giving those their own type that we could catch here. It's best if those sorts of errors are reported as if we expected that they would actually happen in normal operation (i.e. without a stack trace and instructions to report a bug).

Comment thread src/alignment.cpp
Comment on lines +1174 to +1183
for (const auto& tag : diploid_tags) {
if (get<1>(tag) == 'Z') {
bam_aux_append(bam, get<0>(tag).c_str(), 'Z', get<2>(tag).size() + 1,
reinterpret_cast<const uint8_t*>(get<2>(tag).c_str()));
} else {
int32_t quality = stoi(get<2>(tag));
bam_aux_append(bam, get<0>(tag).c_str(), 'i', sizeof(quality),
reinterpret_cast<const uint8_t*>(&quality));
}
}

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.

I feel like, instead of getting the diploid-surjection tags, scanning them all for each tag annotation to skip annotation tags with the same names as diploid-surjection tags, doing the big complex loop to write annotation tags according to their types, and then looking at the diploid tags and doing specialized logic to write them according to the types we think they must have (either Z or i), we should instead get all the annotation tags, apply the diploid tags on top, and then serialize that collection of tags.

Comment thread src/surjector.cpp
Comment on lines +200 to +218
auto clear_sa = [](Alignment& aln) {
if (!has_annotation(aln, "tags")) {
return;
}
string tags = get_annotation<string>(aln, "tags");
string retained;
for (size_t start = 0; start < tags.size();) {
size_t end = tags.find('\t', start);
string tag = tags.substr(start, end == string::npos ? end : end - start);
if (tag.compare(0, 3, "SA:") != 0) {
if (!retained.empty()) retained += '\t';
retained += tag;
}
if (end == string::npos) break;
start = end + 1;
}
if (retained.empty()) clear_annotation(aln, "tags");
else set_annotation(aln, "tags", retained);
};

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.

We can build accessors like this to let us use the SA tag inside the tags annotation to store stuff, but if we're going to treat it as somewhere we store important stuff about the alignment that we might need to change later, we should probably consider using a different internal format.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mapping Theme/team for mapping to pangenomes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants