Skip to content

Commit

Permalink
Add layer argument to score_genes() (#2921)
Browse files Browse the repository at this point in the history
Co-authored-by: Isaac Virshup <[email protected]>
Co-authored-by: Philipp A <[email protected]>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent 21aecd9 commit 706d4ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/release-notes/1.11.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
17 changes: 17 additions & 0 deletions scanpy/tests/test_score_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion scanpy/tools/_score_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
-------
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 706d4ef

Please sign in to comment.