Repository: scverse/scanpy

Issue:
sc.queries.enrich throws AssertionError with floats
<!-- Please give a clear and concise description of what the bug is: -->
I'm trying to run an enrichment analysis after filtering out certain genes via `sc.tl.filter_rank_genes_groups`, so I use `key='rank_genes_groups_filtered'` as an argument for `sc.queries.enrich`. Since the filtered values are replaced with `nan` I hoped they'd by ignored in the enrichment analysis, but it actually leads to an uninformative `AssertionError`.

My suggestion here is simply to filter `nan` values from the gene list around here and 2 lines later: https://github.com/theislab/scanpy/blob/249fc572471683357b86b8bbf41d3284118bc8f8/scanpy/queries/_queries.py#L296

I can make a little PR if we agree with this simple fix

Note you can reproduce this very simply without an adata object (but of course the likely use case is with an adata object as outlined above):

<!-- Put a minimal reproducible example that reproduces the bug in the code block below: -->
```
sc.queries.enrich([float('nan')])
```
Output:
<!-- Put your Error output in this code block (if applicable, else delete the block): -->
```pytb
AssertionError: query failed with error 500
```

#### Versions:
```
scanpy==1.4.5.post2 anndata==0.6.22.post1 umap==0.3.10 numpy==1.18.1 scipy==1.2.1 pandas==1.0.1 scikit-learn==0.22.1 statsmodels==0.11.0 python-igraph==0.8.0
```


Discussion:
On a related note, would it make sense for an option to call `gprofiler.profile()` with a list of `background` genes for the `sc.queries.enrich(adata)` case. The `background` genes could be all the genes in the adata object? Currently, the `background` is always the full set of protein coding genes for the organism I think, which is quite different if few genes are expressed in the dataset, something like...

```
sc.queries.enrich(adata, group="0", gprofiler_kwargs={"background": list(adata.var.index)})
```
That makes a lot of sense to me. I also would have assumed that you can set the background in the `sc.queries.enrich()` function. But then, maybe I'm not the right person to comment on this as I've never used `sc.tl.filter_rank_genes_groups` and didn't know it existed until just now. I believe @ivirshup worked on this function?
I think both of these things are probably already possible, just not documented. That this fails with `sc.tl. rank_genes_groups_filtered` does seems like a bug. This is partly a problem of not having a convention for documenting dispatched methods (@flying-sheep, do you have recommendations for how to handle this?)

`sc.queries.enrich(adata, ...)` calls `sc.get.rank_genes_groups_df`, then the basic `enrich` method. You can pass arguments to both.

To use custom cutoffs for filtering:

```python
sc.queries.enrich(adata, log2fc_min=2, pval_cutoff=.01)
```

The other example, where you pass `gprofiler_kwargs`, should work as is.
Thanks both, just to clarify, I am using the `min_in_group_fraction` and `max_out_group_fraction` args for `sc.tl.rank_genes_groups_filtered()`, which are not available as custom cutoff args for `sc.queries.enrich()` (otherwise I agree entirely)

Resolving patch:
diff --git a/scanpy/queries/_queries.py b/scanpy/queries/_queries.py
--- a/scanpy/queries/_queries.py
+++ b/scanpy/queries/_queries.py
@@ -235,6 +235,9 @@ def enrich(
     {doc_org}
     gprofiler_kwargs
         Keyword arguments to pass to `GProfiler.profile`, see gprofiler_.
+    **kwargs
+        All other keyword arguments are passed to `sc.get.rank_genes_groups_df`. E.g.
+        pval_cutoff, log2fc_min.
 
     Returns
     -------
@@ -293,7 +296,7 @@ def _enrich_anndata(
         gene_symbols=gene_symbols,
     )
     if gene_symbols is not None:
-        gene_list = list(de[gene_symbols])
+        gene_list = list(de[gene_symbols].dropna())
     else:
-        gene_list = list(de["names"])
+        gene_list = list(de["names"].dropna())
     return enrich(gene_list, org=org, gprofiler_kwargs=gprofiler_kwargs)


Test patch:
diff --git a/scanpy/tests/test_queries.py b/scanpy/tests/test_queries.py
--- a/scanpy/tests/test_queries.py
+++ b/scanpy/tests/test_queries.py
@@ -15,6 +15,10 @@ def test_enrich():
     enrich_list = sc.queries.enrich(list(de_genes))
     assert (enrich_anndata == enrich_list).all().all()
 
+    # theislab/scanpy/#1043
+    sc.tl.filter_rank_genes_groups(pbmc, min_fold_change=1)
+    sc.queries.enrich(pbmc, "1")
+
 
 @pytest.mark.internet
 def test_mito_genes():