diff --git a/docs/release-notes/1.9.2.md b/docs/release-notes/1.9.2.md index dfe3d89f56..1aff3bcc9f 100644 --- a/docs/release-notes/1.9.2.md +++ b/docs/release-notes/1.9.2.md @@ -6,6 +6,8 @@ ```{rubric} Bug fixes ``` +- {func}`~scanpy.pp.highly_variable_genes` `layer` argument now works in tandem with `batches` {pr}`2302` {smaller}`D Schaumont` + ```{rubric} Performance ``` diff --git a/scanpy/preprocessing/_highly_variable_genes.py b/scanpy/preprocessing/_highly_variable_genes.py index 7db4a098c3..3dda8d3fab 100644 --- a/scanpy/preprocessing/_highly_variable_genes.py +++ b/scanpy/preprocessing/_highly_variable_genes.py @@ -458,6 +458,7 @@ def highly_variable_genes( hvg = _highly_variable_genes_single_batch( adata_subset, + layer=layer, min_disp=min_disp, max_disp=max_disp, min_mean=min_mean, diff --git a/scanpy/tests/test_highly_variable_genes.py b/scanpy/tests/test_highly_variable_genes.py index bda388a447..2f732292c6 100644 --- a/scanpy/tests/test_highly_variable_genes.py +++ b/scanpy/tests/test_highly_variable_genes.py @@ -36,11 +36,26 @@ def test_highly_variable_genes_basic(): assert 'highly_variable_intersection' in adata.var.columns adata = sc.datasets.blobs() - adata.obs['batch'] = np.random.binomial(4, 0.5, size=(adata.n_obs)) + batch = np.random.binomial(4, 0.5, size=(adata.n_obs)) + adata.obs['batch'] = batch adata.obs['batch'] = adata.obs['batch'].astype('category') sc.pp.highly_variable_genes(adata, batch_key='batch', n_top_genes=3) assert 'highly_variable_nbatches' in adata.var.columns assert adata.var['highly_variable'].sum() == 3 + highly_var_first_layer = adata.var['highly_variable'] + + adata = sc.datasets.blobs() + new_layer = adata.X.copy() + np.random.shuffle(new_layer) + adata.layers['test_layer'] = new_layer + adata.obs['batch'] = batch + adata.obs['batch'] = adata.obs['batch'].astype('category') + sc.pp.highly_variable_genes( + adata, batch_key='batch', n_top_genes=3, layer='test_layer' + ) + assert 'highly_variable_nbatches' in adata.var.columns + assert adata.var['highly_variable'].sum() == 3 + assert (highly_var_first_layer != adata.var['highly_variable']).any() sc.pp.highly_variable_genes(adata) no_batch_hvg = adata.var.highly_variable.copy()