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.

Compute custom statistics and export cell subsets

Compute statistics that CyteArc does not provide from a store’s neighbor graph or count matrix, and export cells to tools that need another format. This page computes two statistics for 3,948 human peripheral blood mononuclear cells (PBMCs): each cell’s graph strength (the total weight of its edges in the saved neighbor graph) and the number of highly variable genes (HVGs) that the cell detects, counted by streaming the count matrix in blocks. The page saves both as cell columns, selects cells by graph strength, and exports the selection to AnnData and to a new CyteArc store.

Graph strength barely differs between the four major cell types: its mean lies between 8.59 and 8.92 in each, and a cutoff at its lowest quarter keeps 74.7% to 76.0% of each. Detected HVGs differ more between the types than their RNA content does: monocytes detect 199.6 of the 500 HVGs on average, twice the 99.0 of T cells, from a median of 10,436 counts per cell, only about a third more than the 7,882 of T cells.

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-18T00:15:56.043394Z | Wall time: 5.851 s

Open the downloaded store and reopen its saved analysis with ds.pipeline.open(label="rna"). CyteArc saves each result of an analysis in the store as an artifact, and the returned pipeline run maps each output name to a reference to the artifact that the run saved under it: run["connectivity_map"] refers to the neighbor graph and run["umap"] to the UMAP. run.cells reads the cell fields that the run recorded, such as its clusters.

Each statistic on this page holds one value per analyzed cell. ds.cells.insert(..., key="I") writes such values to the store’s active cells, the cells flagged True in its I column, in store order, so the values land on the right cells only if the active cells are the analyzed cells. Check that they are:

{'stored cells': 5025, 'analyzed cells': 3948, 'active cells are the analyzed cells': True, 'genes': 33538}

Started: 2026-11-18T00:16:01.897731Z | Wall time: 3.342 s

The run analyzed the 3,948 of the 5,025 stored cells that passed its quality filters, and these are the store’s active cells. The store holds 33,538 genes. The PBMC analysis names the run’s 12 clusters from their marker genes. Save its names as the cell column cellType, joining its CD14 monocytes (cluster 2) and CD16 monocytes (cluster 10) as monocytes, to read each statistic by cell type:

cells
cell type
T cells2385
NK cells312
B cells495
monocytes729
pDC-like cells15
platelets12

Started: 2026-11-18T00:16:05.242767Z | Wall time: 0.084 s

T cells make up 2,385 of the 3,948 analyzed cells; the pDC-like cells and platelets, with 15 and 12 cells, are the two smallest groups.

Sum each cell’s edge weights in the graph

ds.graph.load returns a saved graph as a SciPy sparse matrix with one row and one column for each analyzed cell, in store order. With symmetric=True, two cells share an edge when either chose the other as a neighbor, and upper_only=False stores each edge in both directions; the graph construction tutorial shows how the run built the edges and their weights. The sum of a row is the cell’s graph strength, also called its weighted degree:

{'cells': 3948, 'neighbors chosen per cell': 11, 'edges per cell (min, median, max)': (11, 15, 53), 'nonzero entries': 64858, 'graph strength (min, mean, max)': (5.38, 8.82, 27.03)}

Started: 2026-11-18T00:16:05.329519Z | Wall time: 0.163 s

The run chose 11 neighbors for each cell, and edges also join each cell to the cells that chose it, so the analyzed cells have 11 to 53 edges. The run scales the weights from each cell to its own 11 neighbors so that they add up to the same total for every cell, and graph strength rises above that total only through the cells that chose the cell as a neighbor. Graph strength runs from 5.38 to 27.03, with a mean of 8.82; a high value marks a cell that many other cells chose. Plot the cell types beside graph strength on the run’s UMAP:

Two UMAP panels of the 3,948 analyzed PBMCs. Left, cells colored by broad cell type: T cells fill the large region on the left, NK cells lie below it, B cells form a group at the top, monocytes a group on the right, and the 15 pDC-like cells and 12 platelets form two small groups above the monocytes. Right, the same cells colored by graph strength on a scale from 5.38 to 27.03: most cells are dark, below about 10, and brighter cells, above about 12, are scattered through the T cells, NK cells, B cells and monocytes, while the two small groups stay dark.

Started: 2026-11-18T00:16:05.495504Z | Wall time: 1.314 s

High graph strength marks no single population: the brightest cells lie among the T cells, NK cells, B cells and monocytes alike. The column customGraphStrength keeps no record of the graph it came from, so after building another graph, such as one with more neighbors, compute the column again from that graph.

Count detected HVGs block by block

ds.RNA.rawData is the RNA assay’s count matrix, cells by genes, as a chunked array: indexing it selects cells and genes without reading counts. Select the run’s analyzed cells and its 500 HVGs:

(3948, 500)

Started: 2026-11-18T00:16:06.812747Z | Wall time: 0.169 s

A cell detects an HVG when it has at least one count of that gene. stream_blocks yields the selection one block of cells at a time, in row order, and reads ahead only as many blocks as the memory budget allows; selected_counts.compute() would instead load the whole selection at once. Each block holds at most as many cells as one shard, the stored object that holds a range of rows of the count matrix. This small store keeps all 5,025 cells in one shard, so the 3,948 selected cells arrive in one block. On a store of millions of cells, the loop below runs once for each shard’s worth of selected cells and keeps only a short vector from each block:

{'blocks': 1, 'cells': 3948, 'detected HVGs (min, mean, max)': (24, 123.5, 250), 'equal to count_nonzero(axis=1)': True}

Started: 2026-11-18T00:16:06.984826Z | Wall time: 0.661 s

Each cell detects 24 to 250 of the 500 HVGs, 123.5 on average, the same counts that selected_counts.count_nonzero(axis=1) returns. A chunked array provides count_nonzero, sum, mean and var as reductions that read the same blocks; a loop over stream_blocks handles any calculation they do not cover.

Select cells with a custom column

A Boolean cell column selects cells wherever CyteArc takes a cell key: the export and the subset writer below take it as cell_key, and a pipeline recipe takes it as cells. As an example, select the cells above the lowest quarter of graph strength. The values cover the analyzed cells, and fill_value=False writes False for the other stored cells, so every stored cell holds a Boolean rather than a missing value:

{'graph strength cutoff': 6.65, 'True': 2961, 'False, analyzed cells': 987, 'False, cells outside the run': 1077}

Started: 2026-11-18T00:16:07.648230Z | Wall time: 0.040 s

The cutoff falls at a graph strength of 6.65. The saved column holds True for the 2,961 cells above it, and False for the 987 analyzed cells below it and for the 1,077 cells outside the run.

Compare the statistics across cell types

Plot the detected HVGs beside the selection, with the cells that the selection drops in black. Then read the custom columns back from the store with ds.cells.to_pandas_dataframe and summarize them by cell type:

Two UMAP panels of the 3,948 analyzed PBMCs. Left, cells colored by the number of the 500 HVGs that each detects, from 24 to 250: the monocytes on the right are brightest, mostly between about 150 and 250, the T-cell region on the left is darkest, mostly between about 50 and 120, and the B cells at the top and the NK cells at the bottom lie in between. Right, the 2,961 cells that the selection keeps in gray and the 987 cells in the lowest quarter of graph strength in black; the black cells are scattered through the T cells, NK cells, B cells and monocytes alike.

Started: 2026-11-18T00:16:07.690807Z | Wall time: 0.608 s

cellsmedian total countsmean detected HVGsmean graph strengthselected (%)
cell type
T cells2385788299.08.9274.7
NK cells3126354135.48.5974.7
B cells4957238120.58.6874.9
monocytes72910436199.68.7276.0
pDC-like cells1511778160.78.0773.3
platelets122664127.97.3091.7

Started: 2026-11-18T00:16:08.302497Z | Wall time: 0.074 s

The monocytes, brightest in the left panel, detect twice as many HVGs as T cells from about a third more counts. B cells and NK cells detect more HVGs than T cells from fewer counts: 120.5 and 135.4 HVGs from medians of 7,238 and 6,354 counts, against 99.0 from 7,882. The platelets hold about a third of the T cells’ counts, a median of 2,664, and still detect more HVGs, 127.9. Counts per cell therefore do not set the order: the statistic is the number of the 500 selected genes that a cell detects, so the statistic depends on which genes the selection holds.

The selection keeps a similar share of each major type, so the 2,961 selected cells have nearly the cell-type composition of the 3,948 analyzed cells. The platelets keep 91.7% of their 12 cells; in a group that small, one cell moves the share by 8 points.

Export the selection to AnnData

to_anndata builds an in-memory AnnData object for Scanpy and other Python tools, with the counts in X, the cell table in obs and the feature table in var. Choose the cells and genes before exporting, so the object holds only what the next tool needs. Export the selected cells with six markers of the main lineages:

AnnData object with n_obs × n_vars = 2961 × 6
    obs: 'I', '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', 'cellType', 'customDetectedHVGs', 'customGraphStrength', 'wellConnected'
    var: 'I', 'names', 'dropOuts', 'feature_type', 'genome', 'nCells'
    layers: None (.X)
names
gene_ids
ENSG00000167286CD3D
ENSG00000156738MS4A1
ENSG00000170458CD14
ENSG00000090382LYZ
ENSG00000105374NKG7
ENSG00000115523GNLY

Started: 2026-11-18T00:16:08.379570Z | Wall time: 0.947 s

The export holds the 2,961 selected cells and the 6 genes. obs carries the whole cell table, including cellType and the three custom columns, so Scanpy can group and color the cells by the custom statistics. var is indexed by the genes’ Ensembl IDs, such as ENSG00000167286 for CD3D, so adata.var_names lists IDs rather than the symbols passed as feature_names; the symbols stay in var["names"]. Omit feature_names to export every gene, if the object fits in memory. The import and export tutorial shows how to write H5AD files without building the object in memory, and how to export saved results such as a PCA, a UMAP and clusters beside the counts.

Write the selection to a new store

SubsetZarr writes the selected cells to a new CyteArc store with every gene of the assays it is given, so the next analysis can choose its own genes. Write the store to a temporary directory:

{'cells': 2961, 'genes': 33538, 'custom columns': ['cellType', 'customDetectedHVGs', 'customGraphStrength', 'wellConnected'], 'saved pipeline runs': 0}

Started: 2026-11-18T00:16:09.329150Z | Wall time: 3.272 s

The new store holds the 2,961 selected cells with all 33,538 genes, and its cell table keeps cellType and the custom columns. The store holds no saved pipeline run of the source store, so an analysis of the subset starts from its counts. Python deletes the temporary directory when the session ends; write to a lasting path to keep the store.

Return results through public calls

Write results through public calls, as this page does with ds.cells.insert and SubsetZarr, rather than into the store’s Zarr groups, such as ds.z and ds.zw, whose layout is internal and can change between CyteArc releases. A column written with ds.cells.insert holds only its values, with no record of how they were computed. Coordinates computed by another tool, such as a latent space from scVI, come back as an artifact through ds.reduction.import_coordinates, which records the tool, version and parameters passed as its source; a graph, a UMAP and clusters can then build on the imported coordinates, as the import and export tutorial shows.