Skip to content

Fix tabix/bgzip for presorted, gzipped, streamed, and BAM inputs - #434

Merged
daler merged 8 commits into
daler:v0.12.2rcfrom
rpinho:fix-bgzip
Nov 27, 2026
Merged

daler merged 8 commits into
daler:v0.12.2rcfrom
rpinho:fix-bgzip

Conversation

@rpinho

@rpinho rpinho commented Nov 18, 2026 •

Copy link
Copy Markdown

This PR fixes BedTool.tabix() and BedTool.bgzip(), which were incorrectly handling some input types and argument combinations.

Specifically:

  • tabix(in_place=False, is_sorted=True) would index an empty tempfile, so all queries would return nothing. This now correctly indexes the input.

  • is_sorted=True for either method on a gzip-compressed file would be bgzip-compressed on top of that, doing a double-compression. Now, the gzip is decompressed first and then re-compressed with bgzip.

  • in_place=True for either method on a .gz file would write a .gz.gz file. Now, doing so raises a ValueError and suggests in_place=False.

  • in_place=True for either method on a streaming BedTool would fail. Now, falls back to writing a tempfile.

  • Using either method on a BAM input would return the bam unindexed with no error, or if force=True it would be re-compressed. Now, raises a ValueError and suggests pysam.index().

In addition, BedTool._tmp() now supports suffixes (like .gz) which makes it a little more streamlined to register temp gz files.

Original PR description from @rpinho:

Calling tabix with in_place=False and is_sorted=True compresses an empty file, and presorted gzip input can be compressed twice. Run the call on the bundled a.bed file and query a region that overlaps a row. Use the source file for presorted input and decompress gzip input before BGZIP compression. The regression test compares the bytes and queries the index for both output locations.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

rpinho added a commit to rpinho/pybedtools that referenced this pull request Nov 18, 2026
@rpinho

rpinho commented Nov 24, 2026

Copy link
Copy Markdown
Author

Let me know if you have any thoughts on this one.

@daler
daler changed the base branch from master to v0.12.2rc November 27, 2026 18:06
@daler daler changed the title preserve input rows when indexing presorted files Fix tabix/bgzip for presorted, gzipped, streamed, and BAM inputs Nov 27, 2026
@daler

daler commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thanks. Not sure how much these methods got used, but this PR got me looking at them again. I added more fixes and rewrote the PR description.

@daler
daler merged commit 2f54f3b into daler:v0.12.2rc Nov 27, 2026
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.

2 participants