Skip to content

Fix zoom-level blockSum truncation, nBases division, and zero-sum handling in libBigWig/bwStats.c - #175

Open
rpinho wants to merge 1 commit into
deeptools:masterfrom
rpinho:fix/zoom-blocksum
Open

rpinho wants to merge 1 commit into
deeptools:masterfrom
rpinho:fix/zoom-blocksum

Conversation

@rpinho

@rpinho rpinho commented Nov 27, 2026

Copy link
Copy Markdown

Who hits this: anyone calling bw.stats(..., type="sum") (the default exact=False zoom-level path) on a bigWig file with zoom levels, including multi-bin queries, partial zoom-block overlaps, or covered regions whose values cancel to zero (see #146).

Root Cause

In libBigWig/bwStats.c:blockSum() (lines 387–396), getVals() populates v->vals[j]->scalar as a fractional overlap in [0.0, 1.0] (identical to blockMean, blockDev, and blockCoverage), not a base count. blockSum() did three things wrong:

  1. Integer truncation drops partial zoom-block overlaps: uint32_t sizeUse = v->vals[j]->scalar; truncates every fractional scalar in (0, 1) to 0, silently discarding partially overlapping zoom records.
  2. Dividing by nBases returns a sum of per-record means: for a wholly contained zoom record (scalar == 1.0), sizeUse becomes 1, so (v->vals[j]->sum * sizeUse) / v->vals[j]->nBases divides the stored block sum by nBases and adds one per-base mean per zoom record instead of the record's total. Across 63 zoom records covering 10,000 bases of constant +2.0, bw.stats("chr1", 0, 10000, type="sum") returned 126.0 instead of 20000.0 (158.7x too low); on constant -2.5 it returned -157.5 instead of -25000.0.
  3. Zero accumulator conflated with missing data: if(o == 0.0) return strtod("NaN", NULL); converts valid covered regions summing to 0.0 (all-zero bases, or signed values cancelling to zero) into missing (NaN / Python empty value), while unequal per-block counts on cancelling +3.0/-3.0 runs returned -1.5 instead of 0.0.

Fix

  • Align blockSum() with sibling zoom reducers (blockMean, blockDev, blockCoverage) by accumulating o += v->vals[j]->sum * v->vals[j]->scalar and coverage += v->vals[j]->nBases * v->vals[j]->scalar.
  • Return NaN only when !coverage, preserving 0.0 for covered zero-sum regions and keeping uncovered windows missing.
  • Clear v = NULL after destroyVals_t(v) so the error: label cannot double-free v if a later block's getVals() fails.

(Note: libBigWig's README mentions mean * min(bases covered, overlap), whereas blockMean/blockDev/blockCoverage all scale stored sum and nBases proportionally by scalar; this keeps blockSum consistent with sum == mean * cov * width across all zoom reducers. The vendored libBigWig/bwStats.c is identical to upstream dpryan79/libBigWig@6ca74dbf; I can open a matching PR upstream there as well if preferred.)

Verification

  • Built with CPython 3.12 / NumPy 2.2.6 (cc -Wall -Wextra -Wshadow on libBigWig/bwStats.c produced 0 warnings).
  • Verified 6,712 bin queries across 196 combinations of track case, bin count, exact mode, and list/NumPy output against a standalone zoom-record and full-resolution oracle: baseline had 224 zoom-mode departures from the zoom oracle; patched has 0, while exact=True agrees throughout.
  • Added zoom-level regression tests in pyBigWigTest/test.py (TestRemote.doSum and TestZoomSum.testZoomSum) covering full/partial zoom overlaps, multi-bin queries, positive/negative/zero/cancelling covered regions, uncovered missingness, and the sum == mean * cov * (end - start) identity; full pytest suite passes (6/6).

Found and written with AI agents; a separate AI referee reproduced the problem before this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

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