Add multi-region iterator support for tabix#1997
Open
HeyangQin wants to merge 1 commit intosamtools:developfrom
Open
Add multi-region iterator support for tabix#1997HeyangQin wants to merge 1 commit intosamtools:developfrom
HeyangQin wants to merge 1 commit intosamtools:developfrom
Conversation
Implement tbx_itr_regions() and tbx_itr_multi_next() to provide multi-region query support for tabix-indexed files (VCF, BED, GFF, GAF, etc.), matching the existing functionality for BAM/CRAM via sam_itr_regions(). The multi-region iterator handles overlapping regions by merging intervals and deduplicating records, so each record is returned at most once regardless of how many query regions it falls in. Implementation: - Add readrec_data field to hts_itr_t, allowing hts_itr_multi_next() to pass format-specific context (tbx_t*) to the readrec callback instead of htsFile*. When NULL (default), the existing behaviour of passing htsFile* is preserved. - Add tbx_itr_regions() which creates a multi-region iterator using the existing hts_itr_multi_bam query function (tabix and BAM share the same binning index format). - Add tbx_itr_multi_next() macro and tbx_pseek/tbx_ptell helpers. Closes samtools#1913.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1913 (and addresses the multi-region query aspect of #1949).
This adds
tbx_itr_regions()andtbx_itr_multi_next(), giving tabix-indexed files (VCF, BED, GFF, GAF, etc.) the same multi-region query capability that BAM/CRAM already have viasam_itr_regions().The key feature is automatic deduplication: when query regions overlap, each record is returned exactly once. This is critical for use cases like "query all known cancer genes" where gene regions may be adjacent or overlapping.
Design
The core insight is that tabix and BAM share the same binning index format (TBI/CSI ↔ BAI/CSI), so
hts_itr_multi_bamworks directly as the query function for tabix — no new query function needed.The one interface gap was that
hts_itr_multi_next()hardcodeshtsFile*as the data argument toreadrec, buttbx_readrecexpectstbx_t*. This is resolved by adding avoid *readrec_datafield tohts_itr_t:tbx_itr_regions), it is passed toreadrecinstead offdcalloc), existing BAM/CRAM behaviour is preservedhts_itr_multi_next— fully backwards-compatibleUsage
Changes
htslib/hts.hvoid *readrec_datatohts_itr_thts.creadrec_datawhen set inhts_itr_multi_next(3 call sites)htslib/tbx.htbx_itr_regions()declaration,tbx_itr_multi_next()macrotbx.ctbx_itr_regions(),tbx_pseek(),tbx_ptell()htslib.maptbx_itr_regionsTest plan