
Getting Started with conformeR
Justine Leclerc and Christof Seiler
Source:vignettes/conformeR-introduction.Rmd
conformeR-introduction.RmdIntroduction
Overview
LEMUR fits a low-dimensional embedding and assigns cells to neighborhoods based on similar responses to a given contrast and gene. Because these neighborhood assignments are themselves derived from a fitted model, they are subject to sampling variability: refitting the model on a different sample of cells can shift the neighborhood boundaries.
conformeR quantifies this uncertainty by wrapping the neighborhood construction step in a conformal prediction framework. It is designed to integrate naturally into existing Bioconductor workflows for single-cell data analysis, operating downstream of SingleCellExperiment-based preprocessing and LEMUR analyses to provide uncertainty-aware inference for neighborhood assignments.
It provides two complementary views of that uncertainty:
- Conformal selection asks, for a given gene, which cells can we confidently say belong to the affected neighborhood? It returns a subset of neighborhood cells with a guaranteed bound on the proportion of false inclusions.
- Conformal clustering asks, for each cell, how uncertain are we about its neighborhood assignment? It returns a prediction set of plausible neighborhoods per cell, which can contain zero, one, or several labels.
Why this matters. A single LEMUR fit gives you one
particular neighborhood assignment, with no way to tell how much of it
would survive resampling. conformeR turns that single
assignment into a statement with a formal, finite-sample coverage
guarantee.
Installation
conformeR is distributed through Bioconductor. Install it with
BiocManager:
if (!requireNamespace("BiocManager", quietly = TRUE)) {
install.packages("BiocManager")
}
BiocManager::install("conformeR")
library(conformeR)
library(SingleCellExperiment)
library(BiocParallel)
library(dplyr)
library(ggplot2)
library(tibble)
library(knitr)
theme_set(
theme_minimal(base_size = 13) +
theme(
panel.grid.minor = element_blank(),
plot.title = element_text(face = "bold"),
legend.position = "bottom"
)
)
conformer_palette <- c(
"inside" = "#4472A8",
"outside" = "#D9822B",
"none" = "#B0B0B0"
)Use-case of conformeR
Example dataset
This vignette runs conformeR on a reduced version of the
single-cell RNA-seq dataset from Zhao, Dovas, Spinazzi, et al. (2021),
“Deconvolution of cell type-specific drug responses in human tumor
tissue with single-cell RNA-seq,” published in Genome
Medicine (GEO accession GSE148842). As with any input
to conformeR, the data have already been preprocessed and
quality-checked upstream.
data("sce_reduced")
sce_reduced
#> class: SingleCellExperiment
#> dim: 100 3000
#> metadata(0):
#> assays(2): counts logcounts
#> rownames(100): ENSG00000118785 ENSG00000210082 ... ENSG00000113070
#> ENSG00000181218
#> rowData names(2): gid gene
#> colnames(3000): cell 6769 cell 6786 ... cell 27922 cell 11673
#> colData names(3): cell_type condition patient_id
#> reducedDimNames(0):
#> mainExpName: NULL
#> altExpNames(0):
rownames(sce_reduced) <- rowData(sce_reduced)$gene
head(colData(sce_reduced))
#> DataFrame with 6 rows and 3 columns
#> cell_type condition patient_id
#> <character> <character> <character>
#> cell 6769 Oligodendrocytes ctrl PW030
#> cell 6786 Myeloid cells ctrl PW030
#> cell 27448 Myeloid cells panobinostat PW030
#> cell 76998 Myeloid cells ctrl PW034
#> cell 92754 Myeloid cells panobinostat PW034
#> cell 22521 Myeloid cells ctrl PW030We restrict the analysis to three genes with distinct expected
behavior, to illustrate a range of neighborhood sizes and uncertainty
patterns: HIST3H2A, PTPRZ1, and
HBEGF.
pan_affect_genes <- rowData(sce_reduced) |>
as_tibble() |>
dplyr::select(gid, gene) |>
filter(gene %in% c("HIST3H2A", "PTPRZ1", "HBEGF"))
kable(pan_affect_genes, caption = "Genes of interest for this vignette")| gid | gene |
|---|---|
| ENSG00000106278 | PTPRZ1 |
| ENSG00000113070 | HBEGF |
| ENSG00000181218 | HIST3H2A |
Running conformeR
conformeR() fits LEMUR and layers both
conformal procedures on top of it in one call. The key arguments
are:
-
design_lemur— the LEMUR design formula; here we adjust forpatient_idand estimate the effect ofcondition. -
contrast_column— which column of the design defines the contrast of interest. -
genes_of_interest— restrict the (more expensive) conformal layers to a gene subset; LEMUR itself is still fit genome-wide. -
what— which conformal procedure(s) to run:"conf_selection","conf_clustering", or both.
By default the data are split 50% training / 50% test, with the
calibration set taking 25% of the training set
(cp_train_frac and cp_cal_frac set these
proportions). The split is stratified on the variables of
design_lemur.
Gene chunks are processed in parallel with the BiocParallel
back-end given in BPPARAM (serial by default). Use, for
instance, MulticoreParam(workers = 4) on Linux and macOS or
SnowParam(workers = 4) on Windows.
Interpretation of cp_alpha
cp_alpha is an error level; the corresponding
confidence (or coverage) level is 1 - cp_alpha. With the
default cp_alpha = 0.05:
-
Conformal selection:
cp_alphais the target false discovery rate. Among the cells selected as belonging to a gene’s neighborhood, the expected proportion of cells that are actually outside the neighborhood is at most 5%. -
Conformal clustering:
cp_alphais the miscoverage level of the prediction sets. For a new cell, the prediction set contains the cell’s true neighborhood label with probability at least 95%.
Lowering cp_alpha makes both procedures more
conservative: fewer cells are selected and more prediction sets are
ambiguous.
set.seed(2002)
sce_res <- conformeR(
sce_reduced,
design_lemur = ~ patient_id + condition,
contrast_column = "condition",
genes_of_interest = pan_affect_genes$gene,
what = c("conf_selection", "conf_clustering"),
cp_alpha = 0.05,
BPPARAM = SerialParam(),
verbose = TRUE
)Structure of the output
conformeR() returns the input
SingleCellExperiment, with all its original assays and
annotations, enriched with the results.
sce_res
#> class: SingleCellExperiment
#> dim: 100 3000
#> metadata(1): conformeR
#> assays(2): counts logcounts
#> rownames(100): SPP1 MT-RNR2 ... HBEGF HIST3H2A
#> rowData names(2): gid gene
#> colnames(3000): cell 6769 cell 6786 ... cell 27922 cell 11673
#> colData names(4): cell_type condition patient_id conformeR_split
#> reducedDimNames(3): LEMUR LEMUR_UMAP LEMUR_aligned_UMAP
#> mainExpName: NULL
#> altExpNames(1): conformeR-
colData(sce_res)$conformeR_splitrecords whether each cell was used for conformal training ("train"), calibration ("cal") or testing ("test"). Conformal results are reported for the test cells only. -
reducedDim(sce_res, "LEMUR")holds the aligned LEMUR embedding, and"LEMUR_UMAP"/"LEMUR_aligned_UMAP"its UMAPs before and after Harmony alignment. -
altExp(sce_res, "conformeR")has one row per gene of interest and one column per cell. Its assays contain the LEMUR predicted differential expression (DE), the LEMUR neighborhood membership (lemur_neighborhood), the conformal selection (conf_selection), and the conformal clustering prediction sets (conf_clustering_inside,conf_clustering_outside,conf_set_size). Non-test cells areNA. -
metadata(sce_res)$conformeRstores the analysis parameters.
conf <- altExp(sce_res, "conformeR")
conf
#> class: SingleCellExperiment
#> dim: 3 3000
#> metadata(0):
#> assays(6): DE lemur_neighborhood ... conf_clustering_outside
#> conf_set_size
#> rownames(3): PTPRZ1 HBEGF HIST3H2A
#> rowData names(15): lemur_n_cells lemur_sel_statistic ... conf_quantile
#> cp_alpha
#> colnames(3000): cell 6769 cell 6786 ... cell 27922 cell 11673
#> colData names(0):
#> reducedDimNames(0):
#> mainExpName: NULL
#> altExpNames(0):
table(sce_res$conformeR_split)
#>
#> train cal test
#> 1123 376 1501The rowData of the alternative experiment gathers
per-gene results: the LEMUR neighborhood statistics (prefixed with
lemur_), the number of test cells in the LEMUR
neighborhood, the number of conformally selected cells, and the
conformal clustering score quantile.
rowData(conf)
#> DataFrame with 3 rows and 15 columns
#> lemur_n_cells lemur_sel_statistic lemur_pval lemur_adj_pval
#> <integer> <numeric> <numeric> <numeric>
#> PTPRZ1 840 21.440 0.1115789 0.495948
#> HBEGF 778 10.726 0.1718821 0.566741
#> HIST3H2A 934 -26.690 0.0783046 0.495948
#> lemur_f_statistic lemur_df1 lemur_df2 lemur_lfc lemur_did_pval
#> <numeric> <integer> <numeric> <numeric> <numeric>
#> PTPRZ1 2.57702 1 100 1.79608 0.931927
#> HBEGF 1.89350 1 100 -1.77716 0.564614
#> HIST3H2A 3.16427 1 100 -3.18413 0.369775
#> lemur_did_adj_pval lemur_did_lfc n_lemur_neighborhood n_conf_selection
#> <numeric> <numeric> <numeric> <numeric>
#> PTPRZ1 0.999992 -0.145731 840 2
#> HBEGF 0.984415 1.017739 778 0
#> HIST3H2A 0.970905 -2.184354 934 474
#> conf_quantile cp_alpha
#> <numeric> <numeric>
#> PTPRZ1 0.422027 0.05
#> HBEGF 0.402994 0.05
#> HIST3H2A 0.432338 0.05Conformal selection
For each gene, conformal selection returns the set of cells flagged
as belonging to the neighborhood affected by condition,
with the false discovery rate controlled at cp_alpha. The
table below reports both the raw neighborhood size and its share of all
cells in the test set.
test_cells <- sce_res$conformeR_split == "test"
selected <- assay(conf, "conf_selection")[, test_cells]
selection_summary <- tibble(
gene = rownames(conf),
`Neighborhood size` = rowSums(selected),
`% of cells` = round(rowMeans(selected) * 100)
)
kable(selection_summary,
caption =
"Conformal selection: neighborhood size per gene (test cells)"
)| gene | Neighborhood size | % of cells |
|---|---|---|
| PTPRZ1 | 2 | 0 |
| HBEGF | 0 | 0 |
| HIST3H2A | 474 | 32 |
Conformal clustering
Conformal clustering instead reports, per cell, a prediction
set: the neighborhoods that cell could plausibly belong to, with
coverage 1 - cp_alpha. A prediction set of size zero or two
means the procedure could not confidently assign that cell to any
neighborhood. It is ambiguous.
inside <- assay(conf, "conf_clustering_inside")[, test_cells]
outside <- assay(conf, "conf_clustering_outside")[, test_cells]
set_size <- assay(conf, "conf_set_size")[, test_cells]
clustering_summary <- tibble(
gene = rownames(conf),
`Prediction set = inside` = rowSums(set_size == 1 & inside),
`Prediction set = outside` = rowSums(set_size == 1 & outside),
`Prediction set size 2 or empty` = rowSums(set_size != 1)
)
kable(clustering_summary,
caption =
"Conformal clustering: prediction set composition per gene"
)| gene | Prediction set = inside | Prediction set = outside | Prediction set size 2 or empty |
|---|---|---|---|
| PTPRZ1 | 450 | 999 | 52 |
| HBEGF | 197 | 1260 | 44 |
| HIST3H2A | 975 | 488 | 38 |
Visualizing results on the LEMUR embedding
Conformal selection
plotter_conformal_selection(sce_res, pan_affect_genes$gene)
Conformal selection overlaid on LEMUR
Cells shaded as part of the selected neighborhood are those the procedure can confidently attribute to the condition effect for that gene; unshaded cells fall outside the selection at the chosen false discovery rate.
Conformal clustering
plotter_conformal_clustering(sce_res, pan_affect_genes$gene)
Conformal prediction sets overlaid on LEMUR
Here color reflects the prediction set itself rather than a single hard assignment, making it easy to spot the boundary regions where neighborhood membership is genuinely uncertain versus the core regions where every resampling agrees.
Session info
sessionInfo()
#> R version 4.6.2 (2026-10-29)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 26.04.1 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so; LAPACK version 3.12.0
#>
#> locale:
#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats4 stats graphics grDevices utils datasets methods
#> [8] base
#>
#> other attached packages:
#> [1] knitr_1.52 tibble_3.3.1
#> [3] ggplot2_4.0.4 dplyr_1.2.1
#> [5] BiocParallel_1.46.0 SingleCellExperiment_1.34.0
#> [7] SummarizedExperiment_1.42.0 Biobase_2.72.0
#> [9] GenomicRanges_1.64.0 Seqinfo_1.2.0
#> [11] IRanges_2.46.0 S4Vectors_0.50.3
#> [13] BiocGenerics_0.58.1 generics_0.1.4
#> [15] MatrixGenerics_1.24.0 matrixStats_1.5.0
#> [17] conformeR_0.99.4 BiocStyle_2.40.0
#>
#> loaded via a namespace (and not attached):
#> [1] tidyselect_1.2.1 farver_2.1.2 S7_1.0.0
#> [4] fastmap_1.2.0 digest_0.6.39 lifecycle_1.0.5
#> [7] statmod_1.5.2 lemur_1.10.2 magrittr_2.0.5
#> [10] compiler_4.6.2 rlang_1.3.0 sass_0.5.0
#> [13] tools_4.6.2 yaml_2.3.12 FNN_1.1.4.1
#> [16] labeling_0.4.3 S4Arrays_1.12.1 DelayedArray_0.38.2
#> [19] RColorBrewer_1.1-3 abind_1.4-8 withr_3.0.3
#> [22] purrr_1.2.2 desc_1.4.3 grid_4.6.2
#> [25] beachmat_2.28.0 future_1.82.0 edgeR_4.10.7
#> [28] MASS_7.3-66 globals_0.20.0 scales_1.4.0
#> [31] isoband_0.3.0 cli_3.6.6 rmarkdown_2.32
#> [34] ragg_1.5.2 otel_0.3.0 RSpectra_0.16-2
#> [37] glmGamPoi_1.24.0 cachem_1.1.0 splines_4.6.2
#> [40] parallel_4.6.2 BiocManager_1.30.27 XVector_0.52.0
#> [43] vctrs_0.7.3 Matrix_1.7-6 jsonlite_2.0.0
#> [46] bookdown_0.48 listenv_1.1.0 systemfonts_1.3.4
#> [49] locfit_1.5-9.12 limma_3.68.5 ggnewscale_0.5.2
#> [52] jquerylib_0.1.4 tidyr_1.3.2 glue_1.8.1
#> [55] parallelly_1.50.0 pkgdown_2.2.1 codetools_0.2-20
#> [58] cowplot_1.2.0 uwot_0.2.5 rsample_1.3.2
#> [61] gtable_0.3.6 pillar_1.11.1 furrr_0.4.0
#> [64] htmltools_0.5.9 R6_2.6.1 textshaping_1.1.0
#> [67] sparseMatrixStats_1.24.0 evaluate_1.0.5 lattice_0.23-1
#> [70] bslib_0.12.0 Rcpp_1.1.2 SparseArray_1.12.3
#> [73] xfun_0.62 fs_2.1.0 pkgconfig_2.0.3