Reservoir sampling for high-depth pileup#588
Open
stein-andrew wants to merge 1 commit intonanoporetech:masterfrom
Open
Reservoir sampling for high-depth pileup#588stein-andrew wants to merge 1 commit intonanoporetech:masterfrom
stein-andrew wants to merge 1 commit intonanoporetech:masterfrom
Conversation
Problem: When --high-depth is used, positions that reach max_depth stop accepting new reads. Because reads are iterated in BAM coordinate sort order (leftmost mapping position first), this biases each genomic position toward reads whose alignments begin further upstream, systematically excluding reads that map further downstream even though they cover the same position. This issue was first noticed when working with a high-depth synthetic oligonucleotide dataset. Solution: Replace first-come-first-served depth limiting with reservoir sampling. Once a position reaches max_depth, each subsequent read is accepted with probability max_depth/n_seen, giving every read covering a position an equal chance of being included regardless of BAM iteration order. Changes (all in modkit-core/src/pileup/pileup_processor.rs): - Added ReservoirState struct with reservoir sampling logic - DnaPileupWorker: reservoir check replaces reached_max_depth in pileup loop when CHECK_DEPTH=true - GenericPileupWorker/Tally2: reservoir sampling in add_call() when total_cov >= max_coverage - Behavior unchanged when depth is below max_depth
Contributor
|
Hello @stein-andrew, Thank you for this PR! Let me give it a few minutes to review and test and I'll be back with some feedback. |
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.
Problem:
When --high-depth is used, positions that reach max_depth stop accepting new reads. Because reads are iterated in BAM coordinate sort order (leftmost mapping position first), this biases each genomic position toward reads whose alignments begin further upstream, systematically excluding reads that map further downstream even though they cover the same position. This issue was first noticed when working with a high-depth synthetic oligonucleotide dataset.
Solution:
Replace first-come-first-served depth limiting with reservoir sampling. Once a position reaches max_depth, each subsequent read is accepted with probability max_depth/n_seen, giving every read covering a position an equal chance of being included regardless of BAM iteration order.
Changes (all in modkit-core/src/pileup/pileup_processor.rs):