Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4373 +/- ##
==========================================
+ Coverage 81.92% 81.96% +0.03%
==========================================
Files 134 134
Lines 13027 13059 +32
==========================================
+ Hits 10673 10704 +31
- Misses 2354 2355 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
This branch has not been deployed
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.
Closes #3845
What does this PR do?
score_genescurrently samplesctrl_sizereference genes once for each expression bin represented ingene_list.This can give different genes in
gene_listvery different influence over the reference set. For example, if six genes from the gene set fall into one expression bin and only one gene falls into another, the current implementation samples the same number of controls from both bins. The bin containing six genes is therefore under-represented relative to the number of genes it is meant to match.This PR adds a new
ctrl_per_geneoption. When enabled,score_genessamples a matched control set for every gene ingene_listand combines those control sets so that each gene contributes equally to the reference mean, following the per-gene matching described in Tirosh et al. (2016).The existing behavior is preserved under the
ScanpyV1preset:ScanpyV1:ctrl_per_gene=FalseScanpyV2Preview:ctrl_per_gene=TrueThis follows the same preset-based approach Scanpy uses for other behavior changes planned for 2.0, so existing V1 results are not silently changed.
Implementation
The main changes are:
ctrl_per_geneto thescore_genesAPI andScoreGenesPreset.ScanpyV1andScanpyV2Preview.When
ctrl_per_gene=False, the existing scoring path is retained.Testing
I added tests covering:
sc.tl.score_genesdoesn't match reference #3845, where the current per-bin approach produces a systematic bias;ctrl_as_ref;The focused test suite passes:
The existing preset-selection test also passes:
I also compared the unmodified and patched implementations directly under the
ScanpyV1preset. For the tested dense/sparse,ctrl_as_ref=True/False, and NaN-containing cases, the resulting score arrays were byte-for-byte identical.Reproducing #3845
I also reproduced the issue independently with a scaled synthetic dataset preserving the structure of the example in #3845: many genes from the target set in one expression level and only a few in other levels, with no intended biological signal.
The current per-bin implementation produced:
With
ctrl_per_gene=True:The latter is much closer to the expected null behavior, with the score distribution centered around zero rather than being systematically shifted negative.
Notes
The new implementation follows the per-gene matching described in Tirosh et al. (2016). It does not attempt to reproduce every detail of Seurat's
AddModuleScoreimplementation, in particular its handling of duplicate control genes.sc.tl.score_genesdoesn't match reference #3845