Repository: scverse/scanpy

Issue:
sc.pl.rank_genes_groups use_raw wrong error raised when use_raw=True
- [√] I have checked that this issue has not already been reported.
- [√] I have confirmed this bug exists on the latest version of scanpy.
- [ ] (optional) I have confirmed this bug exists on the master branch of scanpy.

---
small bug report:
https://github.com/theislab/scanpy/blob/fb9e12f36b5c600913fa6243819d1575906c384e/scanpy/tools/_rank_genes_groups.py
line 538 wrong error raised when `use_raw=True`
```python
if use_raw is None:
        use_raw = adata.raw is not None
elif use_raw is True and adata.raw is not None:
        raise ValueError("Received `use_raw=True`, but `adata.raw` is empty.")
```
Second `not` should be removed ,Corrected codes should be
```python
if use_raw is None:
        use_raw = adata.raw is not None
elif use_raw is True and adata.raw is None:
        raise ValueError("Received `use_raw=True`, but `adata.raw` is empty.")
```
**Note**: Please read [this guide](https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports) detailing how to provide the necessary information for us to reproduce your bug.

#### Versions
this bug appeard in v1.8.0

<details>

[Paste the output of scanpy.logging.print_versions() leaving a blank line after the details tag]

</details>

sc.pl.rank_genes_groups use_raw wrong error raised when use_raw=True
- [√] I have checked that this issue has not already been reported.
- [√] I have confirmed this bug exists on the latest version of scanpy.
- [ ] (optional) I have confirmed this bug exists on the master branch of scanpy.

---
small bug report:
https://github.com/theislab/scanpy/blob/fb9e12f36b5c600913fa6243819d1575906c384e/scanpy/tools/_rank_genes_groups.py
line 538 wrong error raised when `use_raw=True`
```python
if use_raw is None:
        use_raw = adata.raw is not None
elif use_raw is True and adata.raw is not None:
        raise ValueError("Received `use_raw=True`, but `adata.raw` is empty.")
```
Second `not` should be removed ,Corrected codes should be
```python
if use_raw is None:
        use_raw = adata.raw is not None
elif use_raw is True and adata.raw is None:
        raise ValueError("Received `use_raw=True`, but `adata.raw` is empty.")
```
**Note**: Please read [this guide](https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports) detailing how to provide the necessary information for us to reproduce your bug.

#### Versions
this bug appeard in v1.8.0

<details>

[Paste the output of scanpy.logging.print_versions() leaving a blank line after the details tag]

</details>



Discussion:

Resolving patch:
diff --git a/scanpy/tools/_rank_genes_groups.py b/scanpy/tools/_rank_genes_groups.py
--- a/scanpy/tools/_rank_genes_groups.py
+++ b/scanpy/tools/_rank_genes_groups.py
@@ -535,7 +535,7 @@ def rank_genes_groups(
     """
     if use_raw is None:
         use_raw = adata.raw is not None
-    elif use_raw is True and adata.raw is not None:
+    elif use_raw is True and adata.raw is None:
         raise ValueError("Received `use_raw=True`, but `adata.raw` is empty.")
 
     if method is None:


Test patch:
diff --git a/scanpy/tests/test_rank_genes_groups.py b/scanpy/tests/test_rank_genes_groups.py
--- a/scanpy/tests/test_rank_genes_groups.py
+++ b/scanpy/tests/test_rank_genes_groups.py
@@ -11,6 +11,7 @@
 from scipy.stats import mannwhitneyu
 from numpy.random import negative_binomial, binomial, seed
 
+import scanpy as sc
 from anndata import AnnData
 from scanpy.tools import rank_genes_groups
 from scanpy.tools._rank_genes_groups import _RankGenes
@@ -213,6 +214,23 @@ def test_results_layers():
         )
 
 
+def test_rank_genes_groups_use_raw():
+    # https://github.com/theislab/scanpy/issues/1929
+    pbmc = sc.datasets.pbmc68k_reduced()
+    assert pbmc.raw is not None
+
+    sc.tl.rank_genes_groups(pbmc, groupby="bulk_labels", use_raw=True)
+
+    pbmc = sc.datasets.pbmc68k_reduced()
+    del pbmc.raw
+    assert pbmc.raw is None
+
+    with pytest.raises(
+        ValueError, match="Received `use_raw=True`, but `adata.raw` is empty"
+    ):
+        sc.tl.rank_genes_groups(pbmc, groupby="bulk_labels", use_raw=True)
+
+
 def test_singlets():
     pbmc = pbmc68k_reduced()
     pbmc.obs['louvain'] = pbmc.obs['louvain'].cat.add_categories(['11'])