scanpy.tl.filter_rank_genes_groups#

scanpy.tl.filter_rank_genes_groups(adata, *, key=None, groupby=None, use=None, use_raw=None, min_in_group_fraction=0.25, min_fold_change=1, max_out_group_fraction=0.5, compare_abs=False, out=False, key_added=None)[source]#

Filter out genes based on two criteria.

  1. log fold change and

  2. fraction of genes expressing the gene within and outside the groupby categories.

See rank_genes_groups().

Results are stored in adata.uns['rank_genes_groups_filtered'] (see out).

To preserve the original structure of adata.uns[‘rank_genes_groups’], filtered genes are set to NaN.

The log2 fold changes and fractions of expressing cells are taken from the rank_genes_groups() results if they are stored there ('logfoldchanges', and 'pts' and 'pts_rest' if pts=True was passed), groupby and use_raw are the same as for that call, and it compared each group to reference='rest'. Otherwise, they are computed from the expression values, comparing each group to all other cells.

Parameters:
adata AnnData

Annotated data matrix containing the results of rank_genes_groups().

key str | None (default: None)

The key in adata.uns where the results of rank_genes_groups() are stored (its key_added). Defaults to 'rank_genes_groups'.

groupby AdRef | str | None (default: None)

The key of the observations grouping to consider. Defaults to the one used for rank_genes_groups().

use LayerAcc | str | None (default: None)

Which matrix to compute fractions and fold changes from. Defaults to A.X (or adata.raw, see use_raw). Given by an accessor, e.g. A.X or A.layers['counts']. strs are anndata.acc.AdAcc.resolve()d, e.g. 'layers.counts'. Unlike use_raw, it is not taken from the results.

use_raw bool | None (default: None)

Deprecated since version 1.13.0: raw is deprecated. Keep the data in a layer instead.

Use raw attribute of adata when fractions or fold changes are computed. Defaults to the value used for rank_genes_groups(), or to False if use is specified.

min_in_group_fraction float (default: 0.25)

Only keep genes expressed (i.e. with a non-zero value) in more than this fraction of the cells in the group.

min_fold_change float (default: 1)

Only keep genes whose log2 fold change between the group and all other cells is greater than this. See compare_abs.

max_out_group_fraction float (default: 0.5)

Only keep genes expressed in less than this fraction of the cells outside the group.

compare_abs bool (default: False)

If True, compare absolute values of log fold change with min_fold_change.

out bool (default: False)

Where to write the filtered results. If True, the filtered results is returned instead of written. If False, it is written to .uns['rank_genes_groups_filtered'].

key_added str | None (default: None)

Name of the uns entry to write to instead (until there are .uns accessors).

Return type:

dict[str, Any] | None

Returns:

Same output as scanpy.tl.rank_genes_groups() but with filtered genes names set to nan. Returned as a dict if out=True, else None.

Examples

>>> import scanpy as sc
>>> adata = sc.datasets.pbmc68k_reduced()
>>> sc.tl.rank_genes_groups(adata, "bulk_labels", method="wilcoxon")
>>> sc.tl.filter_rank_genes_groups(adata, min_fold_change=3)
>>> # visualize results
>>> sc.pl.rank_genes_groups(adata, key="rank_genes_groups_filtered")
>>> # visualize results using dotplot
>>> sc.pl.rank_genes_groups_dotplot(adata, key="rank_genes_groups_filtered")