diff --git a/docs/release-notes/1.11.0.md b/docs/release-notes/1.11.0.md index fb6c33a443..8189632632 100644 --- a/docs/release-notes/1.11.0.md +++ b/docs/release-notes/1.11.0.md @@ -3,6 +3,8 @@ ```{rubric} Features ``` +* Add layer argument to {func}`scanpy.tl.score_genes` and {func}`scanpy.tl.score_genes_cell_cycle` {pr}`2921` {smaller}`L Zappia` + ```{rubric} Docs ``` diff --git a/scanpy/tests/test_score_genes.py b/scanpy/tests/test_score_genes.py index d85eda6410..4db68ed53d 100644 --- a/scanpy/tests/test_score_genes.py +++ b/scanpy/tests/test_score_genes.py @@ -239,6 +239,23 @@ def test_use_raw_None(): sc.tl.score_genes(adata, adata_raw.var_names[:3], use_raw=None) +def test_layer(): + adata = _create_adata(100, 1000, p_zero=0, p_nan=0) + + sc.pp.normalize_per_cell(adata, counts_per_cell_after=1e4) + sc.pp.log1p(adata) + + # score X + gene_set = adata.var_names[:10] + sc.tl.score_genes(adata, gene_set, score_name="X_score") + # score layer (`del` makes sure it actually uses the layer) + adata.layers["test"] = adata.X.copy() + del adata.X + sc.tl.score_genes(adata, gene_set, score_name="test_score", layer="test") + + np.testing.assert_array_equal(adata.obs["X_score"], adata.obs["test_score"]) + + @pytest.mark.parametrize("gene_pool", [[], ["foo", "bar"]]) def test_invalid_gene_pool(gene_pool): adata = _create_adata(100, 1000, p_zero=0, p_nan=0) diff --git a/scanpy/tools/_score_genes.py b/scanpy/tools/_score_genes.py index c873dfcd91..a743abff37 100644 --- a/scanpy/tools/_score_genes.py +++ b/scanpy/tools/_score_genes.py @@ -66,6 +66,7 @@ def score_genes( random_state: AnyRandom = 0, copy: bool = False, use_raw: bool | None = None, + layer: str | None = None, ) -> AnnData | None: """\ Score a set of genes :cite:p:`Satija2015`. @@ -101,6 +102,8 @@ def score_genes( .. versionchanged:: 1.4.5 Default value changed from `False` to `None`. + layer + Key from `adata.layers` whose value will be used to perform tests on. Returns ------- @@ -145,7 +148,7 @@ def score_genes( # interval of expression. def get_subset(genes: pd.Index[str]): - x = _get_obs_rep(adata, use_raw=use_raw) + x = _get_obs_rep(adata, use_raw=use_raw, layer=layer) if len(genes) == len(var_names): return x idx = var_names.get_indexer(genes)