Skip to content

Make sam_hdr_remove_lines rebuild the target arrays. - #2105

Open
jkbonfield wants to merge 1 commit into
samtools:developfrom
jkbonfield:sam_hdr_remove_lines
Open

jkbonfield wants to merge 1 commit into
samtools:developfrom
jkbonfield:sam_hdr_remove_lines

Conversation

@jkbonfield

Copy link
Copy Markdown
Contributor

Without this, attempting to remove an SQ line works for SAM and CRAM, but BAM keeps it in the binary portion of the header and the SQ line is put back again on decode. It'd likely have also broken sam_hdr_tid2name.

Without this, attempting to remove an SQ line works for SAM and CRAM,
but BAM keeps it in the binary portion of the header and the SQ line
is put back again on decode.  It'd likely have also broken
sam_hdr_tid2name.

Signed-off-by: James Bonfield <jkb@sanger.ac.uk>
@jkbonfield
jkbonfield marked this pull request as draft October 15, 2026 08:11
@jkbonfield

Copy link
Copy Markdown
Contributor Author

Switched to draft. The PR may be trivial and it may well pass tests, but it's breaking samtools.

It turns out that removing SQ lines does need this if it's to fix the headers written out in BAM, however in BAM world it then can no longer decode some records (SAM works) via the multi-region iterator. I suspect it's still doing the name lookup for RNEXT and failing (if we remove a ref referred to in another record). In SAM this produces a warning, and in BAM it's a hard failure. We need to be consistent in error handling.

@jkbonfield
jkbonfield marked this pull request as ready for review October 15, 2026 10:12
@jkbonfield

jkbonfield commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

I was overly cautious earlier on seeing my samtools failures.

With hindsight it's obvious. If we modify a header then the in-memory binary bam1_t format is invalid as we read it from the input (with full headers) and we need to correct tid and mtid before writing it back out with the shrunken header. This fix is therefore fine and the problem lies in my own samtools changes.

We do still have inconsistencies in how we handle invalid references, but that's baked into the format. In SAM it's text so we can validate. In BAM it's binary so while we could check ranges in more places, we can't spot most invalidities. It's also an issue for another PR should we wish to fix it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant