Repository navigation
Fix zoom-level blockSum truncation, nBases division, and zero-sum handling in libBigWig/bwStats.c - #175
Open
rpinho wants to merge 1 commit into
Open
Fix zoom-level blockSum truncation, nBases division, and zero-sum handling in libBigWig/bwStats.c#175rpinho wants to merge 1 commit into
rpinho wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N
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.
Who hits this: anyone calling
bw.stats(..., type="sum")(the defaultexact=Falsezoom-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()populatesv->vals[j]->scalaras a fractional overlap in[0.0, 1.0](identical toblockMean,blockDev, andblockCoverage), not a base count.blockSum()did three things wrong:uint32_t sizeUse = v->vals[j]->scalar;truncates every fractionalscalarin(0, 1)to0, silently discarding partially overlapping zoom records.nBasesreturns a sum of per-record means: for a wholly contained zoom record (scalar == 1.0),sizeUsebecomes1, so(v->vals[j]->sum * sizeUse) / v->vals[j]->nBasesdivides the stored block sum bynBasesand 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")returned126.0instead of20000.0(158.7x too low); on constant-2.5it returned-157.5instead of-25000.0.if(o == 0.0) return strtod("NaN", NULL);converts valid covered regions summing to0.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.0runs returned-1.5instead of0.0.Fix
blockSum()with sibling zoom reducers (blockMean,blockDev,blockCoverage) by accumulatingo += v->vals[j]->sum * v->vals[j]->scalarandcoverage += v->vals[j]->nBases * v->vals[j]->scalar.NaNonly when!coverage, preserving0.0for covered zero-sum regions and keeping uncovered windows missing.v = NULLafterdestroyVals_t(v)so theerror:label cannot double-freevif a later block'sgetVals()fails.(Note:
libBigWig's README mentionsmean * min(bases covered, overlap), whereasblockMean/blockDev/blockCoverageall scale storedsumandnBasesproportionally byscalar; this keepsblockSumconsistent withsum == mean * cov * widthacross all zoom reducers. The vendoredlibBigWig/bwStats.cis identical to upstreamdpryan79/libBigWig@6ca74dbf; I can open a matching PR upstream there as well if preferred.)Verification
cc -Wall -Wextra -WshadowonlibBigWig/bwStats.cproduced 0 warnings).exactmode, 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, whileexact=Trueagrees throughout.pyBigWigTest/test.py(TestRemote.doSumandTestZoomSum.testZoomSum) covering full/partial zoom overlaps, multi-bin queries, positive/negative/zero/cancelling covered regions, uncovered missingness, and thesum == mean * cov * (end - start)identity; fullpytestsuite 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