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.

Smooth sparse expression with graph diffusion

Smooth the sparse RNA signal of one gene over the neighbor graph of a saved analysis, and check whether the smoothed signal stays in the populations that express the gene. The data are the 3,948 human peripheral blood mononuclear cells (PBMCs) that pass quality control in the 10x Genomics 5k PBMC dataset from a healthy donor (tenx_5K_pbmc_rnaseq), and the saved RNA analysis whose clusters the PBMC analysis names. The gene is CD4, which CD4 T cells and monocytes express and CD8 T cells lack. A zero count can mean that a cell does not express a gene or that its transcripts were not captured, and only 36% of the 3,948 cells have a CD4 count, fewer than half of the cells in each T-cell cluster.

Graph diffusion, the smoothing step of MAGIC Dijk et al., 2018, replaces each cell’s value with a weighted average over its neighbors in the graph. Two diffusion steps give 97% of the cells a CD4 value above zero and leave the mean of every cluster almost unchanged. The smoothed map shows CD4 highest in the CD14 monocytes, at a lower, even level in the T cells without CD8 transcripts, low in the T cells with CD8 transcripts, and near zero in the B cells and NK cells. Where a cluster mixes CD8 T cells with other T cells, smoothing roughly triples the low CD4 of the CD8 T cells with signal from the cells beside them, an exchange that the cluster mean hides. One and three steps change how smooth the map is and barely move the cluster means.

Open the saved analysis and its graph

DataStore has 3948 (5025) cells with 1 assays: RNA Cell metadata: 'I', 'ids', 'names', 'RNA_G2M_score', 'RNA_S_score', 'RNA_UMAP1', 'RNA_UMAP2', 'RNA_cell_cycle_phase', 'RNA_clusters', 'RNA_doublet_score', 'RNA_leiden_cluster', 'RNA_nCounts', 'RNA_nFeatures', 'RNA_paris_cluster', 'RNA_percentMito', 'RNA_percentRibo' RNA assay has 33538 features and following metadata: 'I', 'ids', 'names', 'dropOuts', 'feature_type', 'genome', 'nCells'

Started: 2026-11-18T00:16:01.627555Z | Wall time: 9.928 s

ds holds 5,025 cells, and plots and analyses use the 3,948 that the saved analysis kept. ds.pipeline.open(label="rna") reopens that analysis as a pipeline run, and run["connectivity_map"] is an ArtifactRef to the weighted neighbor graph that the run built, which links each cell to the cells with the most similar expression profiles. The run computed its UMAP and its Leiden clusters from the same graph.

Locate the cell populations

Number the run’s clusters on its UMAP, so that the CD4 maps below can be read by population:

UMAP of the 3,948 analyzed PBMCs colored by the 12 Leiden clusters, with each cluster's number printed on its cells. Clusters 1, 3, 4, and 8 form a large region on the left, with cluster 8 at its left edge, cluster 5 at its lower end, and the small cluster 9 beside cluster 3. Clusters 6 and 7 form a group at the top, cluster 2 a large group on the right with cluster 10 at its upper tip, and the small clusters 11 and 12 lie between the top group and cluster 2.

Started: 2026-11-18T00:16:11.561756Z | Wall time: 1.793 s

The PBMC analysis names the clusters from marker genes: T cells in clusters 1, 3, 4, and 8 on the left, NK cells in clusters 5 and 9 at the lower end of that region, B cells in clusters 6 and 7 at the top, and CD14 monocytes in cluster 2 on the right. The CD16 monocytes of cluster 10 form the upper tip of cluster 2, and pDC-like cells and platelets form the small clusters 11 and 12 above cluster 2. The cell type annotation tutorial finds CD8 transcripts in most cells of cluster 8 and in the lower part of cluster 3, and almost none in clusters 1 and 4.

Smooth CD4 with two diffusion steps

ds.imputation.diffusion turns the graph into a diffusion operator of t steps, two by default. One step replaces each cell’s value with the average of its neighbors’ values, weighted by the graph’s edge weights, and t steps repeat that average, so that signal reaches cells up to t edges away. The operator is saved in the store, and a repeated call with the same graph and t reuses it. ds.imputation.compute_imputed applies the operator to one gene’s normalized expression, its counts per 1,000 counts in each cell, and returns one value per analyzed cell. key="I" in ds.cells.insert places those values on the rows of the 3,948 analyzed cells for plotting, and ds.get_cell_vals reads the observed values in the same units:

fraction above 0meanmaximum
CD4 per 1,000 counts
observed0.3590.070.816
t=20.9660.070.303

Started: 2026-11-18T00:16:13.359323Z | Wall time: 1.929 s

Observed CD4 is above zero in 35.9% of the cells, the cells with at least one CD4 count. After two steps, 96.6% of the cells have a value above zero and the mean stays at 0.07. A smoothed value above zero means that some cell within two edges has a CD4 count, not necessarily the cell itself.

A plot with run=run colors cells only by the run’s own cell fields, such as clusters, so the plots below take the UMAP coordinates from the run with layout=run["umap"] and color the cells by the gene and by the new smoothed columns. Draw the observed and smoothed values on that UMAP, each panel on its own color scale, because smoothing lowers the maximum from 0.816 to 0.303:

Two UMAP panels of the 3,948 PBMCs, each colored on its own scale from dark purple (0) to yellow (highest). Left, observed CD4 per 1,000 counts, from 0 to about 0.8: cells with CD4 counts are scattered among cells without in the T-cell region on the left and in the monocytes on the right, while the B cells at the top and the NK cells at the bottom are dark. Right, CD4 smoothed with two diffusion steps, from 0 to 0.3: the monocytes grade from blue at their lower end through green to yellow at their upper end, T-cell clusters 1 and 4 and the upper part of cluster 3 are an even blue, and the lower part of cluster 3, cluster 8 at the left edge of the T-cell region, the B cells, and the NK cells stay dark.

Started: 2026-11-18T00:16:15.292407Z | Wall time: 0.687 s

Observed CD4 is speckled: in the T-cell region on the left and in the monocytes on the right, cells with CD4 counts sit among cells without. The smoothed map replaces the speckles with regional levels. On its own scale, from 0 to 0.3, smoothed CD4 is brightest in the CD14 monocytes of cluster 2, most of all at their upper end next to cluster 10, where the observed panel also has more of its brightest cells. T-cell clusters 1 and 4 are an even blue. Cluster 3 splits in two: its upper part, next to cluster 4, is as blue as clusters 1 and 4, and its lower part, which holds the CD8 transcripts, is as dark as cluster 8 at the left edge, the B cells at the top, and the NK cells at the bottom.

Compare one, two, and three steps

Smooth CD4 with one and with three steps, and summarize the three results beside the observed values:

fraction above 0meanmaximum
CD4 per 1,000 counts
observed0.3590.070.816
t=10.7660.070.370
t=20.9660.070.303
t=30.9980.070.277

Started: 2026-11-18T00:16:15.983514Z | Wall time: 1.695 s

Each step gives more cells a value above zero: 76.6% after one step and 99.8% after three, which includes nearly all B cells and NK cells although they have almost no CD4 counts. Only the level of a smoothed value then separates the populations. The maximum falls from 0.370 after one step to 0.277 after three. Draw the three results on one shared color scale, so that their colors compare directly:

Three UMAP panels of CD4 smoothed with one, two, and three diffusion steps, on one shared color scale from 0 (dark purple) to 0.37 (yellow). After one step, single monocytes on the right reach yellow and the T-cell region on the left is mottled blue and teal. After two and three steps the speckles are gone: the brightest monocytes dim to green, cluster 2 grades from green at its upper end to blue at its lower end, and T-cell clusters 1 and 4 are an even dark blue. The B cells at the top, the NK cells at the bottom, cluster 8 at the left edge of the T-cell region, and the lower part of cluster 3 stay dark in all three panels.

Started: 2026-11-18T00:16:17.681668Z | Wall time: 0.709 s

After one step the map is still speckled: single monocytes in cluster 2 reach the top of the scale, and the T-cell region is mottled. Two steps remove most of the speckle, and a third changes little more: it raises the cells above zero from 96.6% to 99.8% and dims the brightest monocytes. After two and after three steps, cluster 2 keeps its gradient from green at its upper end next to cluster 10 to blue at its lower end, and T-cell clusters 1 and 4 are an even dark blue. The default of two steps already shows the regional pattern.

Check whether smoothing moves CD4 between populations

Wherever the graph links two populations, diffusion mixes their values. Average CD4 within each cluster, observed and after one, two, and three steps, beside the fraction of the cluster’s cells with at least one CD4 count:

cell typecellsfraction detectedmean observedmean t=1mean t=2mean t=3
cluster
1T cells12170.390.0670.0670.0670.067
2CD14 monocytes7040.760.1810.1800.1790.179
3T cells5280.230.0380.0370.0370.038
4T cells4740.490.0830.0850.0850.084
5NK cells2830.020.0020.0020.0020.002
6B cells2560.030.0030.0020.0020.002
7B cells2390.010.0010.0010.0010.001
8T cells1660.050.0140.0140.0150.016
9NK cells290.030.0080.0080.0070.007
10CD16 monocytes250.800.1190.1130.1200.124
11pDC-like cells150.400.0430.0420.0430.043
12platelets120.000.0000.0010.0010.002

Started: 2026-11-18T00:16:18.393874Z | Wall time: 0.034 s

CD4 counts are most frequent in the monocytes, in 76% of cluster 2 and 80% of the 25 cells of cluster 10, followed by T-cell clusters 1 and 4 (39% and 49%) and the 15 pDC-like cells (40%). They are rarer in clusters 3 and 8, the clusters with CD8 transcripts (23% and 5%), and reach at most 3% in the B cells, NK cells, and platelets. The CD14 monocytes average 0.181 CD4 counts per 1,000 counts, more than twice clusters 1 and 4 (0.067 and 0.083), so in these PBMCs CD4 RNA is higher in monocytes than in any T-cell cluster, as the cell type annotation tutorial also finds.

Smoothing leaves the mean of each large cluster nearly where it was: cluster 1 stays at 0.067 in every column, the CD14 monocytes go from 0.181 to 0.179, and the B-cell and NK-cell clusters stay at 0.008 or less. Leiden drew the clusters from the same graph, as groups of cells with many edges among them and few to other groups, so a few steps keep almost all of a large cluster’s CD4 inside it. The smaller clusters show signal crossing between populations. The 12 platelets, none of which has a CD4 count, reach 0.002 after three steps, cluster 8 rises from 0.014 to 0.016, and the 25 cells of cluster 10 rise from 0.113 after one step to 0.124 after three, toward the 0.179 of the CD14 monocytes beside them.

Cluster 3 mixes CD8 T cells with other T cells, so its steady mean can hide an exchange between them. Compare the cluster’s cells with and without a CD8A count:

cellsmean observedmean t=1mean t=2mean t=3
CD8A count
no2920.0650.0590.0580.057
yes2360.0040.0100.0120.013

Started: 2026-11-18T00:16:18.430290Z | Wall time: 0.253 s

The 236 cells with a CD8A count have little CD4: their mean is 0.004, against 0.065 for the other 292 cells. Smoothing raises their mean to 0.012 after two steps and lowers the mean of the other cells to 0.058, while the cluster mean stays at 0.037 to 0.038: diffusion moves CD4 into the CD8 T cells from the cells beside them. The CD8 T cells still end at a fifth of the level of the other cells, so the smoothed map keeps the split. A cluster’s mean shows exchange with other clusters but not inside the cluster, so before reading a smoothed map, compare the populations that the analysis is about, as the CD8A split does here. The graph decides which cells exchange signal, and the neighbor graph tutorial builds it stage by stage and compares neighbor counts.

Keep smoothed values out of statistical tests

Each smoothed value is a weighted average of the normalized CD4 of the cells within t edges, so the smoothed values of neighboring cells are not independent measurements, and a test that counts each cell as one observation overstates its certainty. Use smoothed values to display expression, and test differences in expression on the observed counts, with donors as replicates, as in the donor-level comparisons tutorial.

References
  1. van Dijk, D., Sharma, R., Nainys, J., Yim, K., Kathail, P., Carr, A. J., Burdziak, C., Moon, K. R., Chaffer, C. L., Pattabiraman, D., Bierie, B., Mazutis, L., Wolf, G., Krishnaswamy, S., & Pe’er, D. (2018). Recovering Gene Interactions from Single-Cell Data Using Data Diffusion. Cell, 174(3), 716-729.e27. 10.1016/j.cell.2018.05.061