Skip to content

surject realigns a large portion of the read instead of splitting into supplementaries #4959

Open

Description

@faithokamoto

1. What were you trying to do?

Surject a GAM file containing a single read. The alignment, dotplotted against CHM13, looks like

Image

Note that all "legs" of the X can connect to each other, i.e. /, \, < are also possible alignment "shapes"

2. What did you want to happen?

Using --supplementary should allow the surjected alignment to be split up into two parts, aligned in opposite directions along approximately the same reference sequence. This would recapitulate what I see in the Giraffe alignments.

3. What actually happened?

The end of the read gets realigned to take a straight-line path instead of being split up to represent the U-turn path.

4. What data and command can the vg dev team use to make the problem happen?

Convert the JSON into a GAM: vg view -aJG sim_r10_HG002_1m.S46_75882.realigned.new.json > sim_r10_HG002_1m.S46_75882.realigned.new.gam

If you want to run the alignment too, this currently requires my "reversals" branch PR #4953. It aligns along the recombination#2#chrX#4 path which visits node 217495936 (in the middle of the X) in both directions.

ADAM_GRAPH=/private/groups/patenlab/anovak/projects/hprc/lr-giraffe/graphs/hprc-v2.1-mc-chm13-eval-sampled16o_fragmentlinked-for-real-r10y2025-HG002-full
REV_INDEX_PREFIX=/private/groups/patenlab/fokamoto/giraffe-loops/graph/hprc-v2.1-mc-chm13-eval-sampled16o_fragmentlinked-for-real-r10y2025-HG002-full.reversals

vg giraffe --gbz-name $ADAM_GRAPH.gbz -b r10 --threads 1 \
    -G sim_r10_HG002_1m.S46_75882.realigned.new.gam \
    --dist-name $ADAM_GRAPH.dist --minimizer-name $REV_INDEX_PREFIX.longread.withzip.min \
    --zipcode-name $REV_INDEX_PREFIX.longread.zipcodes > sim_r10_HG002_1m.S46_75882.even_more_realigned.new.gam 

But for just the surjection

# Without supplementaries (no choice but to realign)
vg surject --read-length long -x $ADAM_GRAPH.gbz \
    sim_r10_HG002_1m.S46_75882.realigned.new.gam > sim_r10_HG002_1m.S46_75882.realigned.new.nosup.gam

# With supplementaries (should split into two pieces)
vg surject --read-length long -x $ADAM_GRAPH.gbz \
    --supplementary \
    sim_r10_HG002_1m.S46_75882.realigned.new.gam > sim_r10_HG002_1m.S46_75882.realigned.new.withsup.gam

The start of the read gets realigned, while the end of the read stays in the exact same locations. The bug is that this happens even if supplementaries are requested.

vg filter --tsv-out nodes sim_r10_HG002_1m.S46_75882.realigned.new.gam | tr "," "\n" > original_alignment.nodes.txt
vg filter --tsv-out nodes sim_r10_HG002_1m.S46_75882.realigned.new.nosup.gam | tr "," "\n" > nosup_surject.nodes.txt
vg filter --tsv-out nodes sim_r10_HG002_1m.S46_75882.realigned.new.withsup.gam | tr "," "\n" > withsup_surject.nodes.txt

# These two are the same!
diff nosup_surject.nodes.txt withsup_surject.nodes.txt
# Start changes, end stays constant
diff --side-by-side original_alignment.nodes.txt nosup_surject.nodes.txt > orig_vs_nosup.txt

5. What does running vg version say?

vg version v1.75.0-59-g18f9eb09e "Spike"
Compiled with g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 on Linux
Linked against libstd++ 20230528
Using HTSlib headers 101990, library 1.19.1-29-g3cfe8769
Built by fokamoto@mustard

Activity

  1. adamnovak commented on Jul 7, 2026

    @adamnovak
    Member

    @eizengaj-roche You wrote the surject supplementary logic, right? Do you have any inklings as to why it might not be kicking in here?

  2. jeizenga commented on Jul 7, 2026

    @jeizenga
    Contributor

    I'm having a hard time picturing what this sequence looks like, but maybe.

    In general, I think the method that's implemented for getting supplementary alignments from big deletions is more compelling than for getting supplementary alignments from inverting motifs. The main reason is that, when dividing up the surject problem to align on either end of a deletion breakpoint, you can stay within one path-strand's surject routine. For inverting (or cross-chromosome) motifs, you also run awkwardly into the final surjection choice routine. The way surject was previously written was to expect that you produce one best surjection for each path strand, and then you choose the best path strand and move forward with it. The way I tried to work around this was to try to identify a "supplementary cover" consisting of path strands. Essentially, you try to find a set of path-strands whose collections of supplementary alignments collectively partition the read non-redundantly (this is roughly the same principal that is used to identify supplementary alignments in short-read giraffe). It looks to me like both strands align the full read, so one possibility is that the two strands' alignments are seen as redundant and therefore not a legitimate collection of supplementary alignments.

    I don't necessarily think that this is the best formulation of the problem, so feel free to improve on it.

  3. adamnovak commented on Jul 13, 2026

    @adamnovak
    Member

    Whatever's going on here is not as simple as vg surject not being able to produce reverse-strand supplementary alignments.

    I set up a tiny GFA with an inversion of nodes 6 and 7, and mapped a read with the lead-up to the inversion and then the inversion. When surjecting the resulting graph alignment with --supplementary, I managed to get the expected output of a primary alignment on the forward strand and a supplementary alignment on the reverse strand.

    cat >flip.gfa <<'EOF'
    H	VN:Z:1.1	RS:Z:ref
    S	1	AAACCCCGTTACCATGTCCAATTTACGAGCTC
    S	2	AGCATGTTCGAGACTGTACCCCACACTAGTGT
    S	3	TAGGGACGGGCCTGGGGTCGAGGCTACCTACA
    S	4	GTCCTGAGCAGCTAGAGAGTTCCGTATATGAT
    S	5	AATACAGC
    S	6	TTGGGCCTTGGAGCTTCCTGCTGAGGTACCGT
    S	7	TGGGCGAATTCCCTTCCGCACACTGTTAGATG
    S	8	ACCCTTCCTCTAAAGAGCATCGTACGTGATTA
    S	9	TAATTGGAGAGATCAAACTCGCTTCGCGTGTC
    S	10	TCACCACGTGCATACATCTATTCTCGCCTTGA
    S	11	TCATTACGCCCGCTGGTAGTCGGATATAGATT
    S	12	ACAACCCCAGAGTGGGTCTATCACTGCCG
    L	1	+	2	+	0M
    L	2	+	3	+	0M
    L	3	+	4	+	0M
    L	4	+	5	+	0M
    L	5	+	6	+	0M
    L	5	+	8	-	0M
    L	6	+	7	+	0M
    L	6	-	9	+	0M
    L	7	+	8	+	0M
    L	8	+	9	+	0M
    L	9	+	10	+	0M
    L	10	+	11	+	0M
    L	11	+	12	+	0M
    W	ref	0	chr1	0	357	>1>2>3>4>5>6>7>8>9>10>11>12
    EOF
    
    cat >flip.fq <<'EOF'
    @read1
    AAACCCCGTTACCATGTCCAATTTACGAGCTCAGCATGTTCGAGACTGTACCCCACACTAGTGTTAGGGACGGGCCTGGGGTCGAGGCTACCTACAGTCCTGAGCAGCTAGAGAGTTCCGTATATGATAATACAGCTAATCACGTACGATGCTCTTTAGAGGAAGGGTCATCTAACAGTGTGCGGAAGGGAATTCGCCCAACGGTACCTCAGCAGGAAGCTCCAAGGCCCAA
    +
    ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
    EOF
    
    vg autoindex --gfa flip.gfa -p flip-index -w lr-giraffe
    vg giraffe -Z flip-index.giraffe.gbz -b hifi -f flip.fq -o gaf > flip.gaf
    vg surject -x flip-index.giraffe.gbz --read-length long --supplementary --gaf-input flip.gaf --sam-out >flip.sam
    
    @HD	VN:1.5	SO:unknown
    @SQ	SN:ref#0#chr1	LN:357	M5:0270f6d106d832fe2d2bf28d23b3b691
    @PG	ID:0	PN:vg
    read1	0	ref#0#chr1	1	3	137M95S	*	0	0	AAACCCCGTTACCATGTCCAATTTACGAGCTCAGCATGTTCGAGACTGTACCCCACACTAGTGTTAGGGACGGGCCTGGGGTCGAGGCTACCTACAGTCCTGAGCAGCTAGAGAGTTCCGTATATGATAATACAGCTAATCACGTACGATGCTCTTTAGAGGAAGGGTCATCTAACAGTGTGCGGAAGGGAATTCGCCCAACGGTACCTCAGCAGGAAGCTCCAAGGCCCAA	((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((	AS:i:142	SA:Z:ref#0#chr1,137,-,96M136S,3,0;
    read1	2064	ref#0#chr1	137	3	96M136S	*	0	0	TTGGGCCTTGGAGCTTCCTGCTGAGGTACCGTTGGGCGAATTCCCTTCCGCACACTGTTAGATGACCCTTCCTCTAAAGAGCATCGTACGTGATTAGCTGTATTATCATATACGGAACTCTCTAGCTGCTCAGGACTGTAGGTAGCCTCGACCCCAGGCCCGTCCCTAACACTAGTGTGGGGTACAGTCTCGAACATGCTGAGCTCGTAAATTGGACATGGTAACGGGGTTT	((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((	AS:i:101	SA:Z:ref#0#chr1,1,+,137M95S,3,0;
    
  4. adamnovak commented on Jul 13, 2026

    @adamnovak
    Member

    I could replicate this with (note that I'm breaking up the GFA nodes in autoindex):

    cat >flip2.gaf <<'EOF'
    @HD	VN:Z:1.0
    @RN	a652a71d0ed60aaaca7c9fa3abd899c0247d21c4e81bc9a0e772de8256dac3d4
    read1	648	0	648	+	>8>9>10>11>12>13>14<14<13<12<11<10<9<8<7<6<5<4<3<2<1	648	0	648	648	648	5	AS:i:648	bq:Z:((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((	cs:Z::648	dv:f:0
    EOF
    
    cat >flip2.gfa <<'EOF'
    H	VN:Z:1.1	RS:Z:ref
    S	1	CTTCTGCGGAGACGCGGTGAGTCTCCGCCAGGAACCCCTGTGAGGTCTTGACCAGTTATCACTACAGCTAGCCCGACATTGTCCCGGCTATCCAAGCAGTGCATAGCCGTTTACTAAGGACTTTCGAGGATTTCCTTGCGTCTTGTGTTAATCGTCCGGGCCGACCCTAGTCCATGTAATACACGTGTGAATACAACACATAAGAAGCGTCGACAG
    S	2	GTTCTTACTATTCGTAATATCCGAAATTGTCCCTTGGCAGTAGCACGTCAAGCAAGATACACCACCGTGAAGTCTACCAATATAGATAACTCACTCGCTCGAGTGTAAGCAGGGTTAGACGGGGAGATAAACGCCGGGTGCTGCTTTCAGCGCAGATGGATGGGGCCATGCAACTGCGGATCGCTAACGTGGTGGCGGCCTGACCGTCCGACTACA
    S	3	TGTCGTCGGACGGTCAGCCCGCCACCACGTTAGCGATTCGCCCTTGTATGGCCCCATCCATCTGCGCTGAAGGCAGCTACCGGCGTGTATCTCCCCGTCTAACCCCGCGTACACTCGGGCGAGTGAGTTATCTATATTGGTAGACCTCACGGTGGTGTATCTTGCTTGACGTGCTACTGCCAAGGGCCAATTTCGGATATTACGCATAGTAAGAAC
    S	4	CTGGCGACGCTTCTTATGTGTTGTATCCACACGTGTATTACATGGACTAGGGTGGGCCCGGAGGATTAACACAAGAAGCAAGCATATCCTCGAAAGTCCTTTGTATGCGGCTATTCACTGCTTGGTTAGCCCGGACAATGTCGGGCTTGTCGTAGTGATAACTGGTCAAGACCTCACAGGGGTTCCTGGCGGAGACTCACCGCGTCTCCGCAGAAG
    L	1	+	2	+	0M
    L	2	+	3	+	0M
    L	2	+	2	-	0M
    L	3	+	4	+	0M
    W	ref	0	chr1	0	128	>1>2>3>4
    W	revsamp	1	chr1	0	128	>1>2<2<1
    EOF
    
    vg autoindex --gfa flip2.gfa -p flip2-index -w lr-giraffe
    vg surject -x flip2-index.giraffe.gbz --read-length long --supplementary --gaf-input flip2.gaf --sam-out >flip2.sam
    

    You can also get that GAF using the reversals branch with:

    cat >flip2.fq <<'EOF'
    @read1
    GTTCTTACTATTCGTAATATCCGAAATTGTCCCTTGGCAGTAGCACGTCAAGCAAGATACACCACCGTGAAGTCTACCAATATAGATAACTCACTCGCTCGAGTGTAAGCAGGGTTAGACGGGGAGATAAACGCCGGGTGCTGCTTTCAGCGCAGATGGATGGGGCCATGCAACTGCGGATCGCTAACGTGGTGGCGGCCTGACCGTCCGACTACATGTAGTCGGACGGTCAGGCCGCCACCACGTTAGCGATCCGCAGTTGCATGGCCCCATCCATCTGCGCTGAAAGCAGCACCCGGCGTTTATCTCCCCGTCTAACCCTGCTTACACTCGAGCGAGTGAGTTATCTATATTGGTAGACTTCACGGTGGTGTATCTTGCTTGACGTGCTACTGCCAAGGGACAATTTCGGATATTACGAATAGTAAGAACCTGTCGACGCTTCTTATGTGTTGTATTCACACGTGTATTACATGGACTAGGGTCGGCCCGGACGATTAACACAAGACGCAAGGAAATCCTCGAAAGTCCTTAGTAAACGGCTATGCACTGCTTGGATAGCCGGGACAATGTCGGGCTAGCTGTAGTGATAACTGGTCAAGACCTCACAGGGGTTCCTGGCGGAGACTCACCGCGTCTCCGCAGAAG
    +
    ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
    EOF
    
    vg autoindex --gfa flip2.gfa -p flip2-index -w lr-giraffe
    vg giraffe -Z flip2-index.giraffe.gbz -b hifi -f flip2.fq -o gaf --show-work > flip2.gaf
    

    This is about the same as the X except there are enough base differences that only the left half of the X shows up appreciably in chaining.

    Next I have to dig into surject and try and see why it can't handle this. I could also try trimming down the graph further.

  5. adamnovak commented on Jul 14, 2026

    @adamnovak
    Member

    OK, I've looked at the surject debug logs, and @jeizenga is right about what's happening here: we break the alignment into two problems of aligning to two different path-strand combinations, each of those produces a full-length alignment rather than softclipping, and we don't assemble a collection of supplementary alignments across those two path-strands and we just pick one alignment to win.

    I think doing things that way makes sense for short-read alignments, but with long reads we might have e.g. a read that completely traverses an inverted region relative to the reference, or a full-length RNAseq read from an interchromosomal fusion between two paralogs that have better-than-a-softclip alignment scores. We want to be able to, at least as a mode if not by default, trust the graph alignment's idea of what pieces of the target reference path-strand set we should be visiting at any given point in the read.

    I think the Right Way to do this would involve some high-level redesign of the surjector to not work within a single path-strand at a time, but instead to be trying to push the read onto the stick-forest graph of the target paths, with some transition cost for jumping from one place in there to another one suggested by the input graph-space alignment, which we can compare to the cost of aligning the rest of the tail right there. (If the graph alignment went through some off-target-reference material before reaching the target reference again, we'd need to do a new kind of DP problem, or at least fake it, to find out exactly where to put the cutover point and which read bases should belong to one supplementary alignment versus the other.) This all looks quite a lot like what @faithokamoto is working on with trying to build a DP problem in Giraffe that can produce supplementary graph alignments out of basically a multipath_alignment_t, so we might be able to use the same code against that stick-forest graph. Alternately, we could adapt the existing spliced surjection logic here, with its concepts of colinearity graphs and friends (which I still haven't read through but look promising), but expand it to allow "splicing" together bits of multiple path-strands together when the graph alignment suggests it. This would probably be more total code, and still be somewhat duplicative of what Faith's trying to do, but would probably allow for changing current behavior less.

    Since I want to avoid bumping into Faith's work, I'm also thinking of doing this the Wrong Way: considering only single-path alignments, generate the alignments for the different path-strands as we do now, and then try to cut and paste them together into supplementary alignment sets if the result can get a higher overall score, with some cost for each cut. I think this would be fairly simple to implement to the point where it solves this case:

    1. When scanning the incoming graph alignment, remember each point where we switch between path-strand chunks and the corresponding read position (or read position interval).
    2. Do all the alignments like we do right now.
    3. Look at each of those cut-overs. Find the best alignment to the left path-strand on the left of the cut-over area and the best alignment to the right path-strand on the right of the cut-over area. Find the best point along the read to cut between them, paying some supplementary alignment transition cost. If the resulting supplementary set has a better score than both of the things being combined, keep it as the new winner for the path-strands it visits at the read bases it visits them with. Repeat with the other cut-overs until you've done all the merges you can and built supplementary sets.
    4. Output the best single alignment or supplementary set.

    This approach would solve the problem we have now of only being able to combine alignments into supplementary sets when each has softclipped in the region the other covers, while being guided by the graph alignment and some transition cost and not just letting you combine anything with anything for free. But, it's layering on more passes and more code, and means we'd be computing and then throwing away a lot of base-level alignments. And once we had a unified approach to pulling supplementary sets out of a multipath_alignment_t or something like it, we'd probably want to throw this away and use that instead.

  6. adamnovak commented on Jul 23, 2026

    @adamnovak
    Member

    I tried to try to do this in a surject-supplementary-reversal branch. But so far all I've managed to do is heavily refactor the guts of target path finding (introducing a SurjectionRecord to hold things that have been in big tuples), and also (basically separately) refactor supplementary finding so that it outputs its results in terms of alignments and not in terms of strands.

    I gave up on the idea of remembering cut-overs since I'm not sure we really need/want that. Shouldn't we be able to cut over anywhere? Also unless we jump right from one target path strand to another, it's hard to tell a cutover ought to exist.

    I think that second part of what I did so far is necessary but not sufficient; I still need something that can figure out when alignments that substantially overlap (from dubious tail alignment) need to be cut apart with the pieces added to a supplementary cover. I don't want to go through all pairs of Alignments and separately all pairs of MultipathAlignments, but I'm not seeing a great way out of it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions