Sub-clustering and per-group pipelines¶
Python port of the R vignette Sub-clustering and per-group pipelines. Original author: Gregor Lueg.
Intro¶
Your first pass of clustering gives you lineages: T cells, B cells, monocytes. What it does not give you is the structure inside those lineages. The highly variable genes and the principal components were chosen to separate a monocyte from a B cell, and on that axis every T cell looks the same. If you crank up the Leiden resolution on the full object you mostly get the lineages chopped into arbitrary pieces, not naive versus memory CD4.
The fix is to throw the global feature selection away and redo it on the cells
you actually care about. That is what SingleCellsSubset is for. It gives you a
view onto a subset of a SingleCells object that shares the on-disk counts
with the parent, but carries its own empty cache, so HVG, PCA, neighbours and
clusters all get recomputed from scratch on the subset alone.
This vignette assumes you have read the design choices and the PBMC walkthrough. The standard chain is not re-explained here.
import tempfile
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import polars as pl
import bixverse as bv
import bixverse.embed
from bixverse.params import NeighboursParams
from bixverse.pipeline import (
Pipeline,
apply_pipeline,
apply_pipeline_per_group,
step_clusters,
step_hvg,
step_neighbours,
step_pca,
)
A coarse first pass¶
Same PBMC3k data set as the main walkthrough, run through QC, HVG, PCA, neighbours and Leiden without commentary.
pbmc3k_path = bv.io.download_pbmc3k()
tempdir_pbmc = tempfile.TemporaryDirectory()
sc = bv.io.load_mtx(
Path(tempdir_pbmc.name) / "pbmc3k",
bv.io.get_cell_ranger_params(pbmc3k_path),
)
sc.rename_var_col("column1", "gene_symbol")
var = sc.var()
ensembl_to_symbol = dict(zip(var["gene_id"], var["gene_symbol"], strict=True))
symbol_to_ensembl = dict(zip(var["gene_symbol"], var["gene_id"], strict=True))
Text pass (real): 1 buckets, ~2700 cells/bucket Text pass done: 29.52ms (cell-major order) Genes passing QC: 11_139 / 32_738 Cells passing QC: 2_700 / 2_700 Writing pass: 1 buckets, no sort Wrote bucket 1/1 (100%) Writing pass done: 12.02ms Converting the cell-based data into the gene-based format.
Counted 2_269_399 non-zeros over 11_139 genes in 1.42ms; converting in 1 phase(s). Phase 1/1: genes 0-11139, scatter 2.05ms, compress + write 6.63ms Conversion into gene-friendly format done: 10.86ms
Mitochondrial proportions, MAD outliers, cells to keep.
gs_of_interest = {
"MT": var.filter(pl.col("gene_symbol").str.contains("^MT-"))["gene_id"].to_list(),
}
bv.qc.gene_set_proportions(sc, gs_of_interest, streaming=False)
qc_df = sc.obs(cols=["cell_id", "lib_size", "nnz", "MT"])
qc = bv.qc.run_cell_qc(
{
"log10_lib_size": np.log10(qc_df["lib_size"].to_numpy()),
"log10_nnz": np.log10(qc_df["nnz"].to_numpy()),
"MT": qc_df["MT"].to_numpy(),
},
sc.cells_to_keep,
directions={
"log10_lib_size": "twosided",
"log10_nnz": "twosided",
"MT": "above",
},
threshold=3.0,
)
sc.set_cells_to_keep(qc.cell_idx[~qc.combined])
Load in data: 2.66ms Finished the gene set proportion calculations: 948.17µs
Now the standard chain. The data is tiny, so exhaustive kNN beats building an index.
bv.pp.find_hvg(sc, hvg_no=2000)
bv.pp.calculate_pca(sc, no_pcs=30, sparse_svd=True)
bv.graph.find_neighbours(
sc, neighbours_params=NeighboursParams(knn_method="exhaustive")
)
bv.graph.find_clusters(sc, res=1.0, name="leiden_global")
bv.embed.umap(sc)
HVG (VST): 11_139 genes, 1 batches, 1 disk block(s) HVG (VST): Calculated gene statistics in 3.81ms HVG (VST): Fitted Loess in 523.21µs HVG (VST): Standardised variance in 874.75µs (1_174 gene(s) re-read for the clip) HVG (VST): Total run time -> 5.24ms Using sparse SVD solving on scaled data on 2000 HVG.
Sparse PCA: Loaded in data in 6.09ms Sparse PCA: finished the data preparations in 1.07ms
Sparse PCA: finished calculations in 169.20ms Sparse PCA: total run time -> 176.41ms Generating kNN data with exhaustive method.
Building Exhaustive index ... ... built Exhaustive index in 121.00µs Generated Exhaustive index: 126.17µs Querying Exhaustive index (self) ... ... queried Exhaustive index (self) in 3.84ms Queried Exhaustive index: 3.87ms Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 8.59ms
For the annotation we deliberately stay coarse. No CD4 versus CD8 split, no naive versus memory. Lineage only, because the fine structure is exactly what we want the subset to recover.
coarse_markers = {
"CD3D": "T cells",
"CD3E": "T cells",
"CD3G": "T cells",
"TRAC": "T cells",
"MS4A1": "B cells",
"CD79A": "B cells",
"CD19": "B cells",
"CD14": "Monocytes",
"LYZ": "Monocytes",
"S100A8": "Monocytes",
"FCGR3A": "Monocytes",
"GNLY": "NK",
"NKG7": "NK",
"KLRD1": "NK",
"FCER1A": "DC",
"CD1C": "DC",
"LILRA4": "DC",
"PPBP": "Platelet",
"PF4": "Platelet",
}
coarse_df = pl.DataFrame(
{"gene_symbol": list(coarse_markers), "cell_type": list(coarse_markers.values())}
).join(var.select("gene_symbol", "gene_id"), on="gene_symbol")
sctype_scores = bv.annotate.calc_sc_type_scores(
sc, bv.annotate.prepare_cell_markers(sc, coarse_df)
)
obs = sc.obs(cols=["cell_idx", "leiden_global"], filtered=True)
cell_type_anno = bv.annotate.score_clusters(
sctype_scores, obs["leiden_global"].to_numpy()
)
sc.add_obs(
obs.join(
cell_type_anno.select(pl.col("cluster_id").alias("leiden_global"), "cell_type"),
on="leiden_global",
how="left",
).select("cell_idx", "cell_type")
)
sc.obs(filtered=True)["cell_type"].value_counts(sort=True)
ScType: 6 cell types, 18 marker genes, 2490 cells ScType: streamed scoring in 351.50µs ScType: total run time -> 615.54µs
| cell_type | count |
|---|---|
| str | u32 |
| "T cells" | 1130 |
| "Monocytes" | 599 |
| "NK" | 424 |
| "B cells" | 313 |
| "DC" | 24 |
bv.pl.embedding_plot(sc, "umap", "cell_type", label_by="cell_type", discrete=True)
plt.show()
Taking a subset¶
The constructor takes the parent, a column in obs, and the level you want.
t_cells = bv.SingleCellsSubset(sc, "cell_type", "T cells")
t_cells
SingleCellsSubset( dir='/var/folders/l0/d50zd8812jg94mhtl8t2swfc0000gn/T/tmpt2wqpfax/pbmc3k', group=cell_type = T cells, cells=1130, genes=11139, hvg=False, pca=False, embeddings=none, knn=False, graph=False, magic=False, residuals=False, stale=none )
The repr tells you the important bit: HVG, PCA, kNN and the graph are all
False. Nothing was inherited. The subset reads the parent's stores, so no
counts were copied and no files were written, but the ScCache is brand new
and the parent's HVG selection was dropped on purpose.
Everything else behaves like the parent object. The obs table is filtered to the group and held in memory, the var table is untouched, and the usual getters work.
t_cells.shape
(1130, 11139)
t_cells.obs(cols=["cell_id", "cell_idx", "lib_size", "cell_type"]).head()
| cell_id | cell_idx | lib_size | cell_type |
|---|---|---|---|
| str | u32 | f64 | str |
| "AAACATACAACCAC-1" | 0 | 2418.0 | "T cells" |
| "AAACATTGATCAGC-1" | 2 | 3144.0 | "T cells" |
| "AAACGCACTGGTAC-1" | 5 | 2154.0 | "T cells" |
| "AAACTTGATCCAGA-1" | 11 | 2382.0 | "T cells" |
| "AAAGAGACGAGATA-1" | 12 | 2403.0 | "T cells" |
A note on indices¶
This is the one place where you can shoot yourself in the foot, so it is worth
being explicit. In Python there is a single cell index space: the 0-based
position in the parent's stores. cell_idx in the subset's obs,
subset_to_original, cells_to_keep and every integer selection you hand to
a getter live in it. Integer selections must belong to the subset, and cell
ids are matched against the subset's cells only. The one exception is
obs(indices=...), which takes row positions within the subset's own obs
table.
cell_indices() maps ids to that space.
first_three = t_cells.cell_ids()[:3]
# 0-based position in the parent's stores, which is what Rust sees
t_cells.cell_indices(list(first_three))
array([0, 2, 5], dtype=uint32)
assert np.array_equal(
t_cells.cell_indices(list(first_three)), t_cells.subset_to_original[:3]
)
The practical upshot: you never translate manually. Hand cell ids or store
indices to the getters and let the class deal with it. The cell_idx column
is there so you can join results back onto the parent later, which is exactly
what we do further down.
Re-running the chain¶
Same functions as on the parent. They take the subset just the same.
bv.pp.find_hvg(t_cells, hvg_no=2000)
bv.pp.calculate_pca(t_cells, no_pcs=20, sparse_svd=True)
bv.graph.find_neighbours(
t_cells, neighbours_params=NeighboursParams(knn_method="exhaustive")
)
bv.graph.find_clusters(t_cells, res=0.5, name="t_subcluster")
bv.embed.umap(t_cells)
t_cells
HVG (VST): 11_139 genes, 1 batches, 1 disk block(s) HVG (VST): Calculated gene statistics in 3.82ms HVG (VST): Fitted Loess in 550.79µs HVG (VST): Standardised variance in 1.04ms (827 gene(s) re-read for the clip) HVG (VST): Total run time -> 5.43ms Using sparse SVD solving on scaled data on 2000 HVG.
Sparse PCA: Loaded in data in 1.28ms Sparse PCA: finished the data preparations in 212.71µs Sparse PCA: finished calculations in 100.66ms Sparse PCA: total run time -> 102.18ms Generating kNN data with exhaustive method.
Building Exhaustive index ... ... built Exhaustive index in 25.54µs Generated Exhaustive index: 32.12µs Querying Exhaustive index (self) ... ... queried Exhaustive index (self) in 1.91ms Queried Exhaustive index: 1.93ms Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 5.03ms
SingleCellsSubset( dir='/var/folders/l0/d50zd8812jg94mhtl8t2swfc0000gn/T/tmpt2wqpfax/pbmc3k', group=cell_type = T cells, cells=1130, genes=11139, hvg=True, pca=True, embeddings=['umap'], knn=True, graph=True, magic=False, residuals=False, stale=none )
Was any of that worth it? Check how much the feature selection actually moved.
global_hvg = sc.map.hvg
subset_hvg = t_cells.map.hvg
len(np.intersect1d(global_hvg, subset_hvg))
986
984 of the 2000 genes are shared, so around half of the feature set is different. The subset PCA is built on genes that vary within T cells, which is the whole point.
bv.pl.embedding_plot(
t_cells, "umap", "t_subcluster", label_by="t_subcluster", discrete=True
)
plt.show()
Markers for the sub-clusters¶
Differential expression on the subset is confined to it. Each sub-cluster is tested against the other T sub-clusters, not against every monocyte in the data set.
sub_markers = bv.de.find_all_markers(t_cells, "t_subcluster").with_columns(
pl.col("gene_id").replace_strict(ensembl_to_symbol).alias("gene_symbol")
)
(
sub_markers.filter(pl.col("fdr") <= 0.05)
.sort(["grp", "lfc"], descending=[False, True])
.group_by("grp", maintain_order=True)
.head(5)
.select("grp", "gene_symbol", "lfc", "fdr")
)
Progress: 17% (2_000 / 11_139 genes, 2.08ms) Progress: 26% (3_000 / 11_139 genes, 3.09ms) Progress: 35% (4_000 / 11_139 genes, 4.07ms) Progress: 44% (5_000 / 11_139 genes, 5.02ms) Progress: 53% (6_000 / 11_139 genes, 5.86ms) Progress: 62% (7_000 / 11_139 genes, 6.85ms) Progress: 71% (8_000 / 11_139 genes, 7.77ms) Progress: 80% (9_000 / 11_139 genes, 8.76ms) Progress: 98% (11_000 / 11_139 genes, 10.65ms) Progress: 100% (11_139 / 11_139 genes, 11.21ms) One-vs-rest across 2 groups: 12.20ms
| grp | gene_symbol | lfc | fdr |
|---|---|---|---|
| i32 | str | f64 | f64 |
| 0 | "S100A4" | 1.587584 | 1.0652e-99 |
| 0 | "ANXA1" | 0.931931 | 3.3234e-38 |
| 0 | "S100A11" | 0.817802 | 1.3670e-37 |
| 0 | "IL32" | 0.816576 | 6.7289e-35 |
| 0 | "KLF6" | 0.809296 | 4.5255e-29 |
| 1 | "CCR7" | 0.591368 | 5.0854e-24 |
| 1 | "CD8B" | 0.427798 | 5.3164e-14 |
| 1 | "C6orf48" | 0.416754 | 2.3410e-12 |
| 1 | "CD7" | 0.34809 | 2.2321e-7 |
| 1 | "MALAT1" | 0.331015 | 1.0650e-42 |
S100A4, ANXA1 and IL32 on one side, CCR7 and CD8B on the other. That is the memory versus naive split, and the global clustering had no way of seeing it.
The expression plots work on the subset too, so you can go straight to a dot plot without touching the parent.
t_features = [
symbol_to_ensembl[s]
for s in (
"CCR7",
"LEF1",
"SELL",
"IL7R",
"S100A4",
"IL32",
"CD8A",
"CD8B",
"GZMK",
"NKG7",
)
if s in symbol_to_ensembl
]
bv.pl.dot_plot(
t_cells,
t_features,
"t_subcluster",
feature_labels={f: ensembl_to_symbol[f] for f in t_features},
scale_exp=True,
cluster_groups=True,
)
plt.show()
Getting the labels back to the parent¶
So far everything lives in the subset, in memory. To make the labels permanent,
or to plot them on the parent's global UMAP, push them into the parent's DuckDB.
merge_subset_obs() does the join on cell_idx for you.
bv.merge_subset_obs(sc, t_cells, cols=["t_subcluster"], prefix_values=True)
sc.obs(filtered=True)["t_subcluster"].value_counts().sort("t_subcluster")
Merged 1 column(s) for 1130 cells into obs (1570 cells left as NULL).
| t_subcluster | count |
|---|---|
| str | u32 |
| null | 1360 |
| "T cells_0" | 582 |
| "T cells_1" | 548 |
prefix_values=True stamps the subset's group onto every value, so you get
T cells_0 rather than a bare 0. Drop it if you want the raw labels. Leave
cols out and everything the subset gained that the parent does not have yet
gets merged, which after a pipeline run is exactly the new columns.
The join is a left join, so every cell that was not a T cell gets null. That
is the behaviour you want, and the downstream methods respect it.
Results that already carry their own cell_idx skip the helper entirely.
fast_cluster() returns memberships keyed in parent space, so add_obs()
takes them directly.
fast_sub = bv.graph.fast_cluster(t_cells, resolutions=(1.0, 0.5))
sc.add_obs(fast_sub.memberships)
sc.obs(filtered=True).select(
"cell_id", "cell_type", "t_subcluster", *fast_sub.memberships.columns[1:]
).head()
k-means with 134 centroids: 12.86ms
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 419.83µs
Centroid graph: 751.83µs
Community detection: 874.88µs
| cell_id | cell_type | t_subcluster | res_1 | res_0.5 |
|---|---|---|---|---|
| str | str | str | i32 | i32 |
| "AAACATACAACCAC-1" | "T cells" | "T cells_0" | 0 | 0 |
| "AAACATTGAGCTAC-1" | "B cells" | null | null | null |
| "AAACATTGATCAGC-1" | "T cells" | "T cells_0" | 0 | 0 |
| "AAACCGTGCTTCCG-1" | "Monocytes" | null | null | null |
| "AAACCGTGTATGCG-1" | "NK" | null | null | null |
Pipelines¶
Once you are doing this for more than one cell type, writing out HVG, PCA,
neighbours and clusters by hand five times gets old. A Pipeline lets you
declare the chain once. Steps chain with >>.
subcluster_pipeline = (
step_hvg(hvg_no=2000)
>> step_pca(no_pcs=20, sparse_svd=True)
>> step_neighbours(neighbours_params=NeighboursParams(knn_method="exhaustive"))
>> step_clusters(res=0.5, name="subcluster")
)
subcluster_pipeline
Pipeline(n_steps=4, steps=['hvg', 'pca', 'neighbours', 'clusters'])
If you prefer, Pipeline takes a plain list of steps too.
assert Pipeline(list(subcluster_pipeline.steps)) == subcluster_pipeline
Pipelines are inert. Nothing runs until you apply one, and the same pipeline
works on a SingleCells or a SingleCellsSubset, because each step's function
takes either.
b_cells = bv.SingleCellsSubset(sc, "cell_type", "B cells")
b_cells = apply_pipeline(subcluster_pipeline, b_cells)
b_cells
HVG (VST): 11_139 genes, 1 batches, 1 disk block(s) HVG (VST): Calculated gene statistics in 3.52ms HVG (VST): Fitted Loess in 489.33µs HVG (VST): Standardised variance in 657.38µs (441 gene(s) re-read for the clip) HVG (VST): Total run time -> 4.71ms Using sparse SVD solving on scaled data on 2000 HVG.
Sparse PCA: Loaded in data in 909.83µs Sparse PCA: finished the data preparations in 112.50µs Sparse PCA: finished calculations in 51.33ms Sparse PCA: total run time -> 52.38ms Generating kNN data with exhaustive method.
Building Exhaustive index ... ... built Exhaustive index in 3.12µs Generated Exhaustive index: 6.21µs Querying Exhaustive index (self) ... ... queried Exhaustive index (self) in 567.12µs Queried Exhaustive index: 578.46µs Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 1.27ms
SingleCellsSubset( dir='/var/folders/l0/d50zd8812jg94mhtl8t2swfc0000gn/T/tmpt2wqpfax/pbmc3k', group=cell_type = B cells, cells=313, genes=11139, hvg=True, pca=True, embeddings=none, knn=True, graph=True, magic=False, residuals=False, stale=none )
One pipeline, every group¶
apply_pipeline_per_group() builds the subset for you and returns a dict keyed
by group.
Sizing matters here. PCA with 20 components needs more than 20 cells, so a rare population that the annotation happened to call will fall over and take the whole run with it. Restrict the groups explicitly rather than hoping for the best. On this data set the coarse annotation returned five lineages. The cell below keeps groups with at least 100 cells, a safe margin over what the PCA needs, so the 24 DCs stay out.
cell_counts = sc.obs(filtered=True)["cell_type"].value_counts(sort=True)
groups_to_run = cell_counts.filter(pl.col("count") >= 100)["cell_type"].to_list()
groups_to_run
['T cells', 'Monocytes', 'NK', 'B cells']
per_group = apply_pipeline_per_group(
subcluster_pipeline, sc, "cell_type", groups=groups_to_run
)
subsets = {
name: x for name, x in per_group.items() if isinstance(x, bv.SingleCellsSubset)
}
assert len(subsets) == len(per_group)
pl.DataFrame(
[
{
"cell_type": name,
"n_cells": x.shape[0],
"n_subclusters": x.obs()["subcluster"].n_unique(),
"hvg_shared_with_global": len(np.intersect1d(x.map.hvg, global_hvg)),
}
for name, x in subsets.items()
]
).sort("n_cells", descending=True)
HVG (VST): 11_139 genes, 1 batches, 1 disk block(s) HVG (VST): Calculated gene statistics in 4.02ms HVG (VST): Fitted Loess in 437.83µs HVG (VST): Standardised variance in 1.18ms (827 gene(s) re-read for the clip) HVG (VST): Total run time -> 5.66ms Using sparse SVD solving on scaled data on 2000 HVG.
Sparse PCA: Loaded in data in 1.40ms Sparse PCA: finished the data preparations in 301.54µs
Sparse PCA: finished calculations in 94.54ms Sparse PCA: total run time -> 96.29ms Generating kNN data with exhaustive method.
Building Exhaustive index ... ... built Exhaustive index in 25.46µs Generated Exhaustive index: 29.12µs Querying Exhaustive index (self) ... ... queried Exhaustive index (self) in 1.34ms Queried Exhaustive index: 1.37ms Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 5.00ms
HVG (VST): 11_139 genes, 1 batches, 1 disk block(s)
HVG (VST): Calculated gene statistics in 3.42ms
HVG (VST): Fitted Loess in 422.71µs
HVG (VST): Standardised variance in 676.50µs (677 gene(s) re-read for the clip)
HVG (VST): Total run time -> 4.56ms
Using sparse SVD solving on scaled data on 2000 HVG.
Sparse PCA: Loaded in data in 3.21ms Sparse PCA: finished the data preparations in 333.58µs
Sparse PCA: finished calculations in 78.35ms Sparse PCA: total run time -> 81.93ms Generating kNN data with exhaustive method.
Building Exhaustive index ... ... built Exhaustive index in 7.12µs Generated Exhaustive index: 12.17µs Querying Exhaustive index (self) ... ... queried Exhaustive index (self) in 739.71µs Queried Exhaustive index: 756.08µs Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 2.40ms
HVG (VST): 11_139 genes, 1 batches, 1 disk block(s)
HVG (VST): Calculated gene statistics in 3.46ms HVG (VST): Fitted Loess in 396.83µs HVG (VST): Standardised variance in 636.08µs (551 gene(s) re-read for the clip) HVG (VST): Total run time -> 4.52ms Using sparse SVD solving on scaled data on 2000 HVG.
Sparse PCA: Loaded in data in 1.13ms Sparse PCA: finished the data preparations in 224.54µs
Sparse PCA: finished calculations in 58.46ms Sparse PCA: total run time -> 59.84ms Generating kNN data with exhaustive method.
Building Exhaustive index ... ... built Exhaustive index in 5.42µs Generated Exhaustive index: 11.08µs Querying Exhaustive index (self) ...
... queried Exhaustive index (self) in 709.79µs Generating the snn graph.
Queried Exhaustive index: 723.04µs
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 1.56ms
HVG (VST): 11_139 genes, 1 batches, 1 disk block(s)
HVG (VST): Calculated gene statistics in 3.39ms
HVG (VST): Fitted Loess in 369.71µs
HVG (VST): Standardised variance in 454.00µs (441 gene(s) re-read for the clip)
HVG (VST): Total run time -> 4.24ms
Using sparse SVD solving on scaled data on 2000 HVG.
Sparse PCA: Loaded in data in 882.17µs Sparse PCA: finished the data preparations in 198.21µs
Sparse PCA: finished calculations in 48.05ms Sparse PCA: total run time -> 49.15ms Generating kNN data with exhaustive method.
Building Exhaustive index ... ... built Exhaustive index in 3.08µs Generated Exhaustive index: 7.00µs Querying Exhaustive index (self) ... ... queried Exhaustive index (self) in 576.00µs Queried Exhaustive index: 589.33µs Generating the snn graph.
Added 0 edges to reconnect components that pruning cut off.
Built the Snn { similarity: Jaccard, prune: 0.0625, rescue: true } graph: 1.23ms
| cell_type | n_cells | n_subclusters | hvg_shared_with_global |
|---|---|---|---|
| str | i64 | i64 | i64 |
| "T cells" | 1130 | 2 | 986 |
| "Monocytes" | 599 | 2 | 856 |
| "NK" | 424 | 2 | 660 |
| "B cells" | 313 | 1 | 569 |
Every lineage picked a different feature set, and the shared fraction with the global HVGs is nowhere near complete. Running one global PCA and hoping it resolves everything was never going to work.
Writing all of that back is one call. merge_subset_obs() takes the values of
the dict straight from apply_pipeline_per_group(), checks that no cell shows
up twice, and does a single join.
bv.merge_subset_obs(
sc,
list(subsets.values()),
cols=["subcluster"],
new_names=["fine_label"],
prefix_values=True,
)
sc.obs(filtered=True)["fine_label"].value_counts().sort("fine_label")
Merged 1 column(s) for 2466 cells into obs (234 cells left as NULL).
| fine_label | count |
|---|---|
| str | u32 |
| null | 24 |
| "B cells_0" | 313 |
| "Monocytes_0" | 362 |
| "Monocytes_1" | 237 |
| "NK_0" | 276 |
| "NK_1" | 148 |
| "T cells_0" | 582 |
| "T cells_1" | 548 |
The prefix is doing real work here. Every group's Leiden run starts counting at
zero, so without it the T cell 1 and the B cell 1 collapse into one label.
Leave it out and you get a warning naming the values that clash. The DCs never
went through the pipeline, so they are the null row.
bv.pl.embedding_plot(sc, "umap", "fine_label", label_by="fine_label", discrete=True)
plt.show()
Source-pure meta cells¶
All three meta cell generators run on a subset, so you can aggregate per
patient and never mix two donors into one meta cell. The catch used to be the
last step: you ended up with one MetaCells object per patient and no way to
run SCENIC or NMF across the lot. bv.metacells.merge_meta_cells() closes
that.
step_metacells() puts the generation into the chain itself. It is the one
step that changes the class of the object, so a pipeline that ends on it hands
you MetaCells rather than SingleCellsSubset. Everything before it still runs
per patient, which is the point when your samples are split over two plates and
need correcting within a donor.
from bixverse.params import BtMetacellsParams
from bixverse.pipeline import step_harmony, step_metacells
mc_pipeline = (
step_hvg(hvg_no=2000)
>> step_pca(no_pcs=20)
>> step_harmony(batch_column="plate")
>> step_neighbours(embd_to_use="harmony")
>> step_metacells(
"bootstrapped", params=BtMetacellsParams(target_no_metacells=100)
)
)
per_patient = apply_pipeline_per_group(mc_pipeline, sc, "patient_id")
meta_cells = bv.metacells.merge_meta_cells(per_patient)
A few things worth knowing:
bv.metacells.meta_cells_per_group()does the whole thing in one call if you do not need the per-patient objects. Same arguments, plusmethodandmc_params.- The merged obs table gains a
source_idcolumn and the meta cell ids are prefixed with it.original_cell_idxstays in the index space of its own source, so it is only meaningful alongsidesource_id. - Gene spaces are reconciled by
"intersect"(the default) or"union", the latter zero-filling genes an input lacks. Both are no-ops when every input came from the same parent object. - Counts are carried over untouched. Normalisation is per meta cell, so row-binding leaves it valid.
- PCA, kNN, sNN and HVG are dropped on merge. They were fitted per patient and mean nothing side by side. Recompute them on the merged object, which is the whole reason for merging.
validate_pipeline()checks a chain against a starting class before anything runs.apply_pipeline()andapply_pipeline_per_group()call it for you, so puttingstep_harmony()afterstep_metacells()errors immediately instead of after twenty minutes of PCA.
Caveats and what is next¶
A few sharp edges to be aware of before you build on this.
Meta cell memberships come back as positions in the parent's stores, which
is what makes them joinable back onto the original object.
bv.de.get_pseudobulked() runs on subsets too if aggregation is all you are
after.
Subsets are RNA only, so multi-modal work stays on SingleCellsMultiModal
(see the multi-modal vignette).
Subsets nest conceptually but not in code: you cannot build a
SingleCellsSubset from another SingleCellsSubset. If you want a third level,
write the labels back to the parent and subset from there.
Clean up¶
tempdir_pbmc.cleanup()