Repository navigation
Speed up coverage_mask with str.find and a difference array - #229
Merged
levitsky merged 1 commit intoNov 4, 2026
Merged
Conversation
Each peptide was located with a lookahead regex, which cannot use a literal prefix search, and each occurrence added 1 over its span. Find the same overlapping occurrences with str.find and add them as +1/-1 at their ends, then take the running sum. The mask is the same.
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.
coverage_masklocated each peptide withre.finditer(f'(?={peptide})', protein). A lookahead pattern can't use the regex engine's literal prefix search, so every peptide costs a full regex scan of the protein, and each occurrence then adds 1 over its slice of the mask.This finds the same overlapping occurrences with
protein.find(peptide, i + 1), records +1 where an occurrence starts and -1 where it ends, and returns the running sum. Empty peptides are skipped (they matched everywhere before but added nothing). The result is the same array with the same dtype.On a random 35,000-residue protein with tryptic peptides:
coverage()callscoverage_mask(), so it gets the same speedup.Checks:
findloop yields exactly the lookahead matches and that the running sum equals the old mask at every residue: https://github.com/TheNetherWatcher/lean-perf-proofs/tree/main/pyteomics-coverage-maskOne difference, outside the documented input: a peptide containing regex metacharacters was matched as a pattern before and is now matched literally. Peptides are documented as plain one-letter sequences, and
fast=Falsestrips them to that.