Skip to content

updated to make consensus work with sam.gz along with threads and ite…#2269

Merged
whitwham merged 2 commits into
samtools:developfrom
vasudeva8:cons_samgz1
Oct 23, 2025
Merged

updated to make consensus work with sam.gz along with threads and ite…#2269
whitwham merged 2 commits into
samtools:developfrom
vasudeva8:cons_samgz1

Conversation

@vasudeva8

Copy link
Copy Markdown
Contributor

Consensus crashes when sam.gz is used as input along with threads and iterators.
This happens due to the data validation attempt in sam_parse1 where fp->bam_header, which is null, is used for header.
To avoid this, each fp in thread data is set with a copy of already read header.
This is made for bam and cram as well, for filters to work fine where an expression needs to access the header (like rname, mrname, library etc.)

Comment thread bam_consensus.c Outdated
thread; filters requiring header also needs this irrespective of file
type; it is easy to copy from tdata[0] rather than reading and parsing;
will be released along with file close */
tdata[i].fp->bam_header = sam_hdr_dup(tdata[0].fp->bam_header);

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.

htsFile ought to be an opaque type, but is not for implementation reasons (mostly that sam_itr_next()/etc are macros). It seems a shame to introduce a direct reference to the bam_header field, which has not been (ab)used from outside htslib previously.

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.

Yes, it would be good to have an interface around this in HTSlib. It could probably also take a reference to the existing header instead of duplicating it, which would be more memory efficient.

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 code could call ignore = sam_hdr_read(tdata[i].fp) (and would probably need to destroy the header returned too) to set up bam_header without new interfaces.

But that sort of API function would be a useful addition too.

@jkbonfield jkbonfield Oct 8, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not loading the header for every file descriptor was deliberate as it's costly in memory. However I obviously didn't think about some filters that need the header.

I note the sam_hdr_incr_ref htslib interface exists but appears to never be used in htslib nor samtools/bcftools, which is a pity.

It originated from io_lib and with a scram_set_header function which is used in the scramble tool to share a header between input and output file descriptors.

Htslib lacks the set-header function, but this is the sort of scenario it would be beneficial for.

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.

Yes, sam_hdr_read will work but it adds the overhead of parsing header which I was trying to avoid.
But accessing bam_header directly is worse than that!
I will add get/set header and use.

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.

Parsing headers 4 or 16 times (or whatever small integer number of threads is specified) doesn't seem like a major overhead. 🤷

This field is not always set, so it should perhaps be a more abstracted API than simple getter/setters.

@vasudeva8

vasudeva8 commented Oct 17, 2025

Copy link
Copy Markdown
Contributor Author

Added get/set methods to htslib and used it. Depends on htslib PR 1959.

@whitwham whitwham merged commit aaf677a into samtools:develop Oct 23, 2025
5 of 7 checks passed
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.

5 participants