Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Highly variable genes

Select the highly variable genes (HVGs) of 3,948 human peripheral blood mononuclear cells (PBMCs) from a healthy donor. PCA, the neighbor graph, and the clusters and layouts built on them use only these genes. A gene’s variance rises with its mean expression, so a ranking by raw variance favors abundant transcripts, such as housekeeping and ribosomal protein genes, whose variance follows sequencing depth more than cell type. features.hvgs instead compares each gene’s variance with the variance of genes of similar mean and keeps the genes that rise furthest above that background.

The page changes one choice at a time: the gene families excluded by name, the variability score, and the number of genes. The highest-scoring genes each peak in one population, led by PPBP in the 12 platelets. The default name exclusions keep 8 MHC class II genes and 3 histone genes out of the top 500, although these genes mark the antigen-presenting cells and the platelets. Each pair of the three variability scores shares 362 to 414 of its 500 genes, and only seurat_v3 leaves out the T-cell marker CD3D. Doubling the selection to 1,000 genes redraws the T-cell clusters: the larger selection separates CD8 effector cells and a 52-cell regulatory T-cell cluster, and splits the naive T cells by the immediate early gene EGR1, a split that may come from sample handling. Every cluster outside the T cells keeps at least 92.9% of its cells together.

Open the saved PBMC analysis

The example store holds the counts of the 10x Genomics dataset “5k Peripheral blood mononuclear cells (PBMCs) from a healthy donor (v3 chemistry)”, released under CC BY 4.0, and the saved RNA analysis whose clusters the PBMC analysis names.

Started: 2026-11-23T23:00:44.256258Z | Wall time: 6.620 s

ds.pipeline.open(label="rna") returns the saved analysis as a pipeline run, and indexing the run by an output name, such as run["analysis_cell_selection"] below, returns that saved result as an ArtifactRef, a reference to a result that never changes. analysis_cell_selection is the run’s frozen cell selection: a saved record of the cells that passed quality control, which later edits of the cell table cannot alter, so every gene selection on this page scores the same 3,948 cells. The run selected 500 genes to keep the example small; the RNA recipe, ds.pipeline.rna, selects 1,000 by default. The comparisons below read clusters through the broad cell types that the PBMC analysis assigns from marker genes, so the next cell also saves those names as the cell column pbmc_cell_type.

3,948 of 5,025 cells analyzed; 33,538 genes
broad cell type T cells 2385 CD14 monocytes 704 B cells 495 NK cells 312 CD16 monocytes 25 pDC-like cells 15 platelets 12 Name: cells, dtype: int64

Started: 2026-11-23T23:00:50.877851Z | Wall time: 0.889 s

The 3,948 analyzed cells are mostly T cells (2,385), CD14 monocytes (704), B cells (495), and NK cells (312), with three small populations: 25 CD16 monocytes, 15 pDC-like cells, and 12 platelets.

Fit the mean-variance model

features.hvgs takes a cell selection and returns the selected genes as a feature-selection artifact. It scales each cell’s counts to a total of 1,000, computes each gene’s variance over the selected cells, and divides that variance by the lower quartile of the variances of genes with a similar mean. This corrected variance is 1 for a gene whose variance equals that background and grows as the gene varies more than its peers. The call keeps the top_n genes with the highest corrected variance, after the exclusions covered below, and draws its diagnostic: one point per gene, placed by its mean expression in the cells that detect it and by its corrected variance, both on log2 scales. Outlined points are the selected genes, and color shows how many cells detect each gene, from blue to green for the other genes and from pale yellow to black for the selected ones:

Scatter plot with one point per gene: log2 mean nonzero expression on the x axis, from about -4 to 5.5, and log2 corrected variance on the y axis, from about -0.7 to 8. The 500 selected genes are outlined circles colored from pale yellow, for genes that few cells detect, to black, for genes that most cells detect. They span log2 means from about -2.8 to 3.5 and log2 corrected variances from about 1.2 up to PPBP near 8, above most unselected genes. Unselected genes are small dots colored from blue, for few detecting cells, to green, for many. Most widely detected genes form a green band near 0. A few blue dots and a few green dots, among them one near 3.5, lie among and above the selected genes, and MALAT1 lies alone at the far right near 1.6.
Same genes as the saved run: True

Started: 2026-11-23T23:00:51.768201Z | Wall time: 1.044 s

The selected genes lie above the unselected cloud across most of the range of mean expression, while most genes that nearly every cell detects, the green band at the lower right, sit near a corrected variance of 1 (0 on the log2 axis). A few unselected points score higher than the lowest selected genes: blue ones, and green ones such as FTL and MALAT1, the point at the far right. The blacklist or the detection bounds exclude these genes, as the next section shows. The call selects the same 500 genes as the saved run, which selected its genes on the same cells with the same settings.

The selection records its input, the feature summary: the per-gene statistics it was computed from, such as each gene’s variance and the number of cells that detect it. Read the statistics, then rank all genes by raw variance and the selected genes by corrected variance:

Genes detected in any analyzed cell: 21,148
Spearman correlation of their variance and mean: 0.995
variancecorrected variancecells detectingselected
gene
MALAT1492.43.13945False
LYZ65.632.71857True
S100A960.944.61288True
S100A832.948.3952True
MT-CO125.81.13945False
FTL22.911.63929False
EEF1A120.71.23944False
B2M19.51.43948False
RPS2718.11.63945False
RPS1217.51.63941False
corrected variancecells detectinghighest mean in
gene
PPBP243.236platelets
NRGN132.3534platelets
PF490.921platelets
TUBB176.321platelets
CAVIN268.224platelets
GNG1165.557platelets
IGLC351.0223B cells
S100A848.3952CD14 monocytes
IGLC248.2269B cells
IGKC45.4671B cells

Started: 2026-11-23T23:00:52.813085Z | Wall time: 0.980 s

Variance follows the mean closely: over the 21,148 genes that any analyzed cell detects, the Spearman correlation between the two is 0.995. The 10 genes with the highest raw variance are therefore mostly abundant transcripts that nearly every cell detects: MALAT1, EEF1A1, B2M, FTL, the mitochondrial MT-CO1, and the ribosomal RPS27 and RPS12. Five of these seven score near the background of 1, with corrected variances of 1.1 to 1.6, while MALAT1 scores 3.1 and FTL 11.6. None of the seven is selected, because each is detected in at least 3,929 of the 3,948 cells, more than the upper detection bound described below allows; MT-CO1, RPS27, and RPS12 also match the name blacklist. The other three, the monocyte genes LYZ, S100A9, and S100A8, rank high on both scales and are selected.

Ranked by corrected variance, the top 10 selected genes each peak in one population: six in the platelets, three immunoglobulin light chains in the B cells, and S100A8 in the CD14 monocytes. The platelet gene PPBP leads with a variance 243 times the background of genes with a similar mean. A population of 12 cells can lead the ranking because its genes are highly expressed in those cells and nearly absent elsewhere: only 36 of the 3,948 cells detect PPBP.

Check what the default exclusions remove

Before ranking, features.hvgs removes genes by name and by how many cells detect them. The default blacklist, DEFAULT_HVG_BLACKLIST, is a case-insensitive regular expression matched from the start of each gene name. It combines the seven gene families of cytearc.features.gene_families: mitochondrial genes (MT-), cytosolic and mitochondrial ribosomal protein genes (RPS, RPL, MRPS, MRPL), cyclins (CCN followed by a letter), the MHC genes of human (HLA-) and mouse (H2-), the replication-dependent histones under their old and current symbols, and ten sex-linked genes such as XIST. Count the genes of each family in this dataset, and read the detection bounds that the selection applied to these cells:

Genes matched by the default blacklist: 329
Detection bounds: {'min_cells': 20, 'max_cells': 3929}
family mitochondrial 13 ribosomal 184 cellCycleCcn 31 hla 20 h2 0 histone 71 sexLinked 10 Name: genes matched, dtype: int64

Started: 2026-11-23T23:00:53.794319Z | Wall time: 0.115 s

The blacklist matches 329 genes here, most of them ribosomal protein genes (184) and histones (71). These families vary with sequencing depth, cell quality, the cell cycle, or sex, and the measured expression of the polymorphic HLA genes can differ between donors. In many datasets they shape PCA without separating the cell types of interest. The patterns match names, not functions: ^CCN[A-Z] removes the cyclin CCND1 but not the matricellular protein CCN1, and the histone family leaves out replacement histones such as H3-3A. blacklist= replaces the whole default pattern, and blacklist="" excludes no names.

The detection bounds scale with the number of selected cells. min_cells keeps genes detected in at least the cube root of the number of selected cells, rounded up and never fewer than 20, so that from 8,000 cells on, genes seen in only a handful of cells of a large selection do not crowd out genes that mark cell types; a selection of fewer than 381 cells uses 5% of its cells instead, and at least 5. max_cells drops genes detected in more than 99.5% of the selected cells. For these 3,948 cells, a gene must be detected in at least 20 cells and in fewer than 3,929. Pass min_cells= or max_cells= to move the detection bounds; max_cells=np.inf removes the upper one.

Select 500 genes again with blacklist="" to see which genes the exclusions keep out. The table ranks each entering gene within the new selection and gives the percentage of each broad cell type’s cells that detect it:

Genes that leave, by rank in the default selection: HMOX1 (490), GADD45G (491), NUP214 (492), MARCH1 (493), CYTOR (494), WARS (495), LYN (496), LMO2 (497), PCLAF (498), BST1 (499), CD83 (500)
rank without blacklistT cells (%)CD14 monocytes (%)B cells (%)NK cells (%)CD16 monocytes (%)pDC-like cells (%)platelets (%)
gene
HIST1H2AC19121014122027100
HLA-DRA421999100271001000
HLA-DRB1552098100461001000
HLA-DQA1602999336270
HLA-DPA1683694100711001000
HLA-DPB1783892100741001008
HIST1H2BJ11440144775
HLA-DQB112035398264730
HIST1H3H20121130792
HLA-DMB33958692656930
HLA-DMA34811819210681008

Started: 2026-11-23T23:00:53.910394Z | Wall time: 1.677 s

Clearing the blacklist moves 8 MHC class II genes and 3 histone genes into the top 500 and pushes out the 11 lowest-ranked genes of the default selection, ranks 490 to 500. Two of the entering genes rank near the top of the new selection: HIST1H2AC 19th and HLA-DRA 42nd. The MHC class II genes mark the antigen-presenting cells: HLA-DRA is detected in 100% of B cells, 99% of CD14 monocytes, and all 25 CD16 monocytes and 15 pDC-like cells, against 19% of T cells and 27% of NK cells. The histone genes are detected in 75 to 100% of the 12 platelets and in at most 27% of the cells of any other population.

Color the saved UMAP by HLA-DRA, the highest-ranked MHC class II gene, beside the broad cell types:

Two UMAP panels of the 3,948 analyzed PBMCs on the saved layout. Left, cells colored by HLA-DRA expression on a log(1 + counts per 1,000) scale from 0 to about 3: the B-cell group at the top, the small pDC-like cluster, and the monocytes on the right are teal to yellow, with the CD14 monocytes darker toward their lower end, while the large T-cell region on the left, the NK cells at the bottom, and the small platelet cluster are dark purple, with scattered lighter T and NK cells. Right, the same cells colored by broad cell type: B cells, CD14 monocytes, CD16 monocytes, NK cells, pDC-like cells, platelets, and T cells.

Started: 2026-11-23T23:00:55.589033Z | Wall time: 0.282 s

HLA-DRA expression is high in the B-cell, monocyte, and pDC-like clusters and low or absent in most T and NK cells and in the platelets. The default selection separates these populations with other genes, but an analysis of antigen presentation, or of activated T cells, which express MHC class II, can keep the HLA genes and exclude the other families:

# Exclude every default gene family except the HLA genes.
blacklist = "|".join(
    pattern for family, pattern in GENE_FAMILY_PATTERNS.items() if family != "hla"
)
ds.features.hvgs(cell_selection, top_n=500, blacklist=blacklist)

Choose a variability score

features.hvgs scores variability with one of three flavors:

  • "cytearc", the default, divides the variance of library-size normalized values by the background of genes with a similar mean, as above.

  • "seurat_v3" fits the trend of raw-count variance against the mean by local regression, standardizes each gene’s counts with that trend, clips them, and scores the variance of the result, as Seurat’s vst method and Scanpy’s flavor="seurat_v3" do Stuart et al., 2019.

  • "pearson_residuals" scores the variance of analytic Pearson residuals of the counts under a negative binomial model Lause et al., 2021, as Scanpy’s experimental flavor of that name does.

The two count-based flavors apply no detection bounds unless you pass them, but they keep the blacklist; pass blacklist="" to reproduce Scanpy’s selection. Select 500 genes with each flavor and count the genes that each pair shares:

cytearcseurat_v3pearson_residuals
cytearc500362414
seurat_v3362500374
pearson_residuals414374500

Started: 2026-11-23T23:00:55.871889Z | Wall time: 1.843 s

lineage markers left outgenes detected in fewer than 20 cellsmedian cells detecting a gene
flavor
cytearcnone0614
seurat_v3CD3D56494
pearson_residualsnone53611

Started: 2026-11-23T23:00:57.715622Z | Wall time: 0.006 s

Each pair of flavors shares 362 to 414 of its 500 genes. Without a detection bound, the count-based flavors also select genes that few cells detect: 56 seurat_v3 genes and 53 Pearson residual genes are detected in fewer than 20 cells, which the default flavor’s lower bound excludes. Only seurat_v3 shifts its whole selection toward rarely detected genes: its median gene is detected in 494 cells, against 614 for the default flavor and 611 for Pearson residuals. seurat_v3 also leaves out the T-cell marker CD3D, while the other two flavors keep all seven lineage markers.

Pearson residuals also serve as a normalization. Setting ds.RNA.normMethod to a PearsonResidualNormalizer makes every normalized read of the assay return residuals, including the reads of marker search. theta sets the negative binomial overdispersion, 100 by default, and clip the largest absolute residual, by default the square root of the number of normalized cells; the example clips at 10. cytearc.assay.norm_pearson_residuals is the normalizer with both defaults. Run PCA on the residuals of the residual flavor’s genes without rescaling each gene, since residuals are already standardized, then restore library-size normalization for the rest of the page:

(3948, 15)

Started: 2026-11-23T23:00:57.722547Z | Wall time: 0.843 s

Select genes across samples

When samples differ technically, a pooled selection can favor genes that separate the samples rather than cell states. With batch, features.hvgs scores every sample on its own, counts the samples in which each gene ranks among the top_n, and selects the genes variable in the most samples, ties broken by their median rank. That is the order of Seurat’s SelectIntegrationFeatures and of Scanpy’s flavor="seurat_v3_paper"; Scanpy’s flavor="seurat_v3" sorts by median rank first. batch takes a cell metadata column or a label artifact such as HTO identities; every selected cell needs a label, and every sample at least two selected cells. This dataset holds one sample, so the call is shown without running it, and the Harmony batch correction tutorial compares pooled and batch-aware selections before Harmony:

# Score each sample on its own and keep the genes variable in the most samples.
by_sample = ds.features.hvgs(cell_selection, flavor="seurat_v3", batch="sample", top_n=2000)

# Count, for each gene, the samples that rank it among their top 2,000.
batch_count = ds.artifacts.load(by_sample)["batch_count"][:]

Compare 500 and 1,000 genes

The number of selected genes sets the basis of PCA, and through it the neighbor graph and the clusters. Select 1,000 genes on the same cells:

Genes of the 500-gene selection among the 1,000: 500

Started: 2026-11-23T23:00:58.566678Z | Wall time: 0.677 s

The 1,000 genes include all 500 of the smaller selection, so the larger set adds the next 500 genes of the ranking. Build a branch from each selection: its own normalization, PCA, neighbor graph, UMAP, and Leiden clusters. Both branches use the saved run’s settings of 15 PCs, 11 neighbors, and Leiden resolution 0.5, so that only the number of genes differs:

The 500-gene branch reproduces the saved run's clusters: True
selected genes 500 12 1000 14 Name: Leiden clusters, dtype: int64

Started: 2026-11-23T23:00:59.244626Z | Wall time: 8.631 s

The 500-gene branch reproduces the saved run’s clusters, and the 1,000-gene branch finds 14 clusters against 12. Color each branch’s UMAP by its own clusters, and both by the broad cell types:

Four UMAP panels, 500 genes in the left column and 1,000 genes in the right. The top row colors each branch's cells by its own Leiden clusters, numbered on the plot: 12 clusters with 500 genes and 14 with 1,000. The bottom row colors the same layouts by broad cell type with one legend. In both layouts the T cells form the large region on the left with the NK cells below them, the CD14 monocytes and the small group of CD16 monocytes lie on the right, the B cells lie at the top, and the pDC-like cells and platelets form two small separate clusters. The 500-gene layout divides the T cells into clusters 1, 3, 4, and 8; the 1,000-gene layout divides them into clusters 1, 3, 4, 6, 9, and 10, with the small cluster 10 between clusters 1 and 3.

Started: 2026-11-23T23:01:07.876752Z | Wall time: 0.557 s

In both layouts the broad cell types stay apart: the B cells, the monocytes, and the small pDC-like and platelet clusters lie away from the T and NK cells. The partitions differ inside the T cells, which form clusters 1, 3, 4, and 8 with 500 genes and six clusters with 1,000. Each partition numbers its clusters by size, so the same number can name different cells in the two. Cross-tabulate the partitions cell by cell, with the broad cell type of each 500-gene cluster, and measure their agreement with the adjusted Rand index (ARI), which is 1 for identical partitions and near 0 for unrelated ones:

Adjusted Rand index: 0.704
1,000 genes1234567891011121314cellslargest share (%)
500 genes
1 (T cells)921072820000520000121775.7
2 (CD14 monocytes)070100000000030070499.6
3 (T cells)20236092790000200052852.8
4 (T cells)600309550000050000047465.2
5 (NK cells)000027640000300028397.5
6 (B cells)000000247900000025696.5
7 (B cells)0000001722200000023992.9
8 (T cells)1100580000970000016658.4
9 (NK cells)00000000002900029100.0
10 (CD16 monocytes)00000000000250025100.0
11 (pDC-like cells)00000000000015015100.0
12 (platelets)00000000000001212100.0

Started: 2026-11-23T23:01:08.435670Z | Wall time: 0.016 s

The partitions agree with an ARI of 0.704. Outside the T cells, every 500-gene cluster keeps at least 92.9% of its cells in one 1,000-gene cluster. The four T-cell clusters keep 52.8 to 75.7% of their cells together and become six. Read the new T-cell clusters with markers of T-cell states: the table gives whether each selection includes a marker and the percentage of each cluster’s cells that detect it.

marksamong the 500 genesamong the 1,000 genes1 (994 cells)3 (552 cells)4 (395 cells)6 (283 cells)9 (102 cells)10 (52 cells)
marker
CCR7naive and central memoryFalseTrue84288578733
LEF1naiveFalseTrue773578108250
CD8BCD8 T cellsTrueTrue581464960
GZMKeffector memoryTrueTrue24735712
GZMHcytotoxic effectorTrueTrue1717012
IL7Rnaive and memory, low in regulatoryTrueTrue889891549342
FOXP3regulatoryTrueTrue1111046
CTLA4regulatoryFalseTrue2410027
IKZF2regulatoryFalseTrue24717446
EGR1immediate early responseTrueTrue767227497344

Started: 2026-11-23T23:01:08.453103Z | Wall time: 1.158 s

The markers name the six T-cell clusters of the 1,000-gene partition:

  • CD8 effector cells separate from the other memory T cells. Of the 528 cells of 500-gene cluster 3, 279 form most of 1,000-gene cluster 6, where 64% of cells detect CD8B and 70% GZMH. Another 236 join 309 cells of 500-gene cluster 4 in 1,000-gene cluster 3, where 8% detect CD8B and 47% GZMK.

  • Regulatory T cells form a cluster of their own. Cluster 10 of the 1,000-gene partition holds 52 cells, 50 of them from 500-gene cluster 4: 46% of its cells detect FOXP3, 46% IKZF2, and 27% CTLA4, and fewer detect IL7R (42%) than in any other T-cell cluster.

  • The naive T cells split by detection of the immediate early gene EGR1. Of the 1,217 cells of 500-gene cluster 1, 921 form most of 1,000-gene cluster 1 and 282 join 1,000-gene cluster 4. Both clusters detect CCR7 in most cells (84% and 85%), but EGR1 in 76% and 27% of cells.

  • Naive CD8 T cells make up 1,000-gene cluster 9: 96% of its 102 cells detect CD8B, 87% CCR7, and 82% LEF1. The cluster takes 97 of the 166 cells of 500-gene cluster 8, and 58 others join 1,000-gene cluster 4.

Only the larger selection holds the naive T-cell markers CCR7 and LEF1 and the regulatory T-cell genes CTLA4 and IKZF2. With 1,000 genes, the clusters resolve regulatory T cells and CD8 effector cells. The naive split needs checking before it is read as a cell state, because immediate early genes such as EGR1 respond within minutes to stimulation, including the handling of cells before capture.

Supply your own gene set

A study can call for a specific gene program or a gene set chosen elsewhere. features.snapshot saves such a set as a feature selection, from a Boolean mask over the complete gene axis or from integer indexes into the gene table, and rejects duplicate or out-of-range indexes, misaligned masks, and empty sets. A graph built from a small marker panel separates the identities that the panel was chosen for, so it cannot test those identities. Save a six-gene panel:

mask length: 33538 selected: 6
ArtifactRef(assay='RNA', kind='feature_selection', artifact_id='5de8a22e6ccf...')

Started: 2026-11-23T23:01:09.612686Z | Wall time: 0.026 s

The returned ArtifactRef takes the place of an HVG selection in every later step, such as ds.features.normalize(cell_selection, custom_features). ds.RNA.feats.sift and multi_sift build Boolean masks from feature metadata columns, and get_index_by returns feature indexes for feature_indexes=. features.detected(cell_selection, min_cells=...) selects genes by detection alone, and features.universe() selects every gene.

Set the gene selection of the RNA recipe

The RNA recipe selects 1,000 genes with the default flavor and blacklist, and its hvg keyword takes the settings of features.hvgs:

# Select 2,000 genes and keep the HLA genes, with the blacklist built above.
ds.pipeline.rna(hvg={"top_n": 2000, "blacklist": blacklist})

Compare feature sets on the same frozen cells with features.hvgs, as this page does, before fixing the recipe’s settings. For scATAC-seq, features.prevalent_peaks plays the same role: it keeps the most prevalent peaks, and the ATAC recipe takes prevalent_peaks={"top_n": ...}, as the scATAC-seq tutorial shows.

References
  1. Stuart, T., Butler, A., Hoffman, P., Hafemeister, C., Papalexi, E., Mauck, W. M., Hao, Y., Stoeckius, M., Smibert, P., & Satija, R. (2019). Comprehensive Integration of Single-Cell Data. Cell, 177(7), 1888-1902.e21. 10.1016/j.cell.2019.05.031
  2. Lause, J., Berens, P., & Kobak, D. (2021). Analytic Pearson residuals for normalization of single-cell RNA-seq UMI data. Genome Biology, 22(1). 10.1186/s13059-021-02451-7