Differential expression and markers¶
Find the genes that distinguish groups of cells: a pairwise test between two groups (which genes are up in one cluster against another, or in one lassoed region against another), or the marker genes of each of three or more groups against the rest. Marker runs are stored in the dataset, so you can change how many genes to show, filter them, and plot them later without running the test again.
Where to find it
Analyze → Cells → DEG, or lasso cells and use Set as Group 1 / Set as Group 2 and Compare in the Cells panel.
How it works¶
Both tests are scanpy's rank_genes_groups (Wolf et al. 2018) on .X, with use_raw=False, so the test sees exactly the values the app shows.
- Two groups: group 1 against group 2 as the reference. The result lists the top genes up in each group, with log2 fold change and adjusted p-value.
- Three or more groups: each group against the other cells of the groups you ticked (one-versus-rest), with the Wilcoxon rank-sum test. The full ranking is stored in the dataset, and the lists you see are a selection from it.
Run it on log-normalized data
scanpy computes fold changes assuming .X is log-transformed, and the tests compare whatever values .X holds. Run Normalize Total and Log1p first, or check the scale badge (Expression scale detection). On raw counts, the reported log2 fold changes are wrong.
Two scopes apply to both tests:
- The gene mask in the Genes panel limits which genes are tested (The Genes panel).
- The Genes dropdown limits the test to a boolean
.varcolumn. It defaults tohighly_variablewhen that column exists, so open it and pick All genes when you want every gene tested.
Run it¶
- Choose a categorical column in Select .obs column. Each group is listed with its number of cells.
- Tick the groups to compare. The line below says what will run.
- With two groups ticked: set Top N genes (default 25) and click Run Diff Exp. The Differential Expression window opens with the result. This path tests every gene (subject to the gene mask); to choose a gene subset or test method, use Re-run in that window.
- With three or more ticked: click Run Marker Genes (N). The Find Marker Genes window opens on the same column. Check the ticked groups there before clicking Run: it pre-ticks the groups ticked in the Cells panel for that column, otherwise every group.
- Lasso the first group of cells and click Set as Group 1 in the Cells panel.
- Lasso the second group and click Set as Group 2. The Comparison box shows each group and its cell count. The two groups must not share cells.
- Click Compare to run with the defaults (top 25 genes, Wilcoxon, Benjamini-Hochberg, every gene), or Options to open the Differential Expression window and set the parameters first.
You can also reach the two-group test from Select cells… on a gene or gene set: label the cells above and below a threshold, then Open Diff Exp ▸ (Select, mask and delete).
Two groups: the Differential Expression window¶
The window shows Group 1 and Group 2. Before a run, it offers Genes, Top N and a collapsible Parameters section; Run Comparison runs the test.
| Parameter | Default | What it does |
|---|---|---|
| Genes | highly_variable if present, else All genes |
Restrict the test to a boolean .var column |
| Top N | 100 | Genes listed for each direction (1 to 500) |
| Test method | Wilcoxon rank-sum | Also t-test, t-test (overestim. var) and Logistic regression |
| P-value correction | Benjamini-Hochberg | Or Bonferroni |
| Min fold change | none | filter_rank_genes_groups: minimum fold change |
| Max adjusted p-value | none | Drop genes above this adjusted p-value |
| Min in-group fraction | none | Minimum fraction (0 to 1) of group 1 cells expressing the gene |
| Max out-group fraction | none | Maximum fraction (0 to 1) of group 2 cells expressing the gene |
The result has two columns, Upregulated in group 1 and Upregulated in group 2, each gene with its log2FC and padj. Then:
- Add to Gene Sets files two sets, one per direction, in the Differential Expression category of the Genes panel (Gene sets).
- Enrichment… opens a preranked GSEA of group 1 against group 2 (offered when both groups come from a column).
- Re-run returns to the parameters.
The pairwise test writes nothing to the dataset. It is logged in the analysis record.
Three or more groups: Find Marker Genes¶
- Pick the Column and tick the groups (Select Groups (min 2); Select All toggles every group).
- Set Genes (default
highly_variableif present) and Top N genes per group (default 5). - Optionally open Advanced Filters.
- Click Run. Each group's markers are listed with log2FC, % in (fraction of the group's cells expressing the gene), % out (fraction of the other cells) and p-adj.
"The rest" is the other cells of the groups you ticked, so tick every group to find markers against all other cells. With a cell mask active, the test uses the active cells only; each group shows how many of its cells are active, a group the mask empties is left out, and a group with one active cell is not tested. Run needs two ticked groups with at least two active cells each.
The stored ranking¶
A run stores, for every group, the top 500 genes of the full ranking with their Wilcoxon score, log2 fold change, fraction expressing in and out of the group, and raw and adjusted p-values. The result line says where: Stored as markers_leiden (500 ranked genes per group). Top N and the filters select from it without re-running.
Changing Top N genes per group, the Advanced Filters or Show only after a run re-selects from the stored table without testing again. To run a fresh test, click Re-run test. A second run on the same column is stored under a new key (markers_leiden_2) rather than overwriting the first.
| Where | What |
|---|---|
uns['skopia_marker_genes']['markers_<column>'] |
The stored ranking, as a JSON string, with a unique id that saved figures use to detect a re-made run |
Advanced filters¶
| Filter | Default | What it does |
|---|---|---|
| Min % in group | none | Minimum fraction (0 to 1, for example 0.25) of the group's cells expressing the gene |
| Max % in others | none | Maximum fraction (0 to 1, for example 0.75) of the other cells expressing the gene |
| Min fold change | none | Minimum fold change on a linear scale (2.0 keeps log2FC of 1 or more) |
| Show only | None | Keep only marker genes in a Genes panel set or a boolean .var column |
Despite the % in their labels, the first two take fractions between 0 and 1.
Show only keeps, say, only the markers that are transcription factors, without re-running the test: the stored ranking is filtered to that list before the thresholds and Top N apply. Only genes among a group's stored 500 can appear, so a group whose annotated genes were not strong markers comes up short. Each group header gives how many genes passed, and a warning appears when a group holds exactly the stored number of rows, since a filter may then have lost candidates the store never kept. The restriction is a snapshot: editing or deleting the set later does not change a saved plot, and the picker shows <name> (saved) until you pick something else. The gene mask is not applied to the restriction.
What to do with the result¶
- Add to Gene Sets files one set per group (
<group> markers, Top N genes each) in a folder named after the column, in the Marker Genes category. With a cell mask, the folder name carries the scope. - Plot… opens the Expression tab with these markers as the heatmap rows, carrying Top N, the filters and Show only as set here. Switch to a dot plot there, and save either as a figure (Expression heatmaps and dot plots).
- Enrichment… opens a preranked GSEA of each ticked group against the rest, against cached gene-set libraries (Enrichment). The DEG tab offers the same hand-off as Enrichment for these groups… before any marker run.
Tips and pitfalls¶
Top N for enrichment
Five markers per group is the default because it is the list you read, add as sets and plot. Overrepresentation tests on each group's markers use 100 genes per group under their own setting (enrichment.marker_top_n), since five genes are too few.
Changing the defaults
The window defaults come from the diff_exp and marker_genes sections of the config file, including marker_genes.store_top_n (500), the number of ranked genes kept per group (Configure defaults).
Small groups
scanpy refuses a group with one cell. In a marker run, Skopia skips such a group instead of failing the run and says it was not tested. Tests on groups of a handful of cells still give unstable rankings, whatever the adjusted p-values say.
Related¶
- Enrichment (ORA and GSEA)
- Expression heatmaps and dot plots: plot a stored marker run
- Gene sets: where Add to Gene Sets puts the genes
- Select, mask and delete: lasso, groups and the cell mask
- Labels and annotations: name the clusters once you know their markers
- PCA, neighbors, UMAP, Leiden: the clusters to compare