
LEMUR analysis for neighborhoods on predicted gene differential expression with conformal methods
Source:R/conformeR.R
conformeR.RdRuns a LEMUR analysis, identifies differential-expression neighborhoods,
and applies conformal procedures to a set of genes of interest.
The data are split into training, calibration, and test sets based on the
covariates specified in the LEMUR design formula. The results are added
to the input SingleCellExperiment, which is returned with its
original data and annotations preserved.
Usage
conformeR(
sce,
design_lemur = ~1,
contrast_column,
contrast_levels = NULL,
n_embedding = 60,
use_assay = "logcounts",
test_fraction_lemur = 0.2,
cp_train_frac = 0.5,
cp_cal_frac = 0.25,
cp_alpha = 0.05,
genes_of_interest,
what = c("conf_selection", "conf_clustering"),
BPPARAM = BiocParallel::SerialParam(),
verbose = TRUE
)Arguments
- sce
A
SingleCellExperimentobject containing the single-cell expression data. It must have unique, non-missing row and column names.- design_lemur
A formula specifying the design used by LEMUR. All variables must be columns of
colData(sce)andcontrast_columnmust be one of them.- contrast_column
A column name of
colData(sce)specifying the comparison used for differential-expression testing in LEMUR.- contrast_levels
NULLor a character vector of length two,c(level_of_interest, reference_level). Required whencontrast_columnhas more than two levels.- n_embedding
Number of dimensions in the LEMUR embedding.
- use_assay
Character string specifying the assay to use.
- test_fraction_lemur
Fraction of cells used as the LEMUR test set.
- cp_train_frac
Fraction of cells used for conformal training (the remaining cells form the conformal test set).
- cp_cal_frac
Fraction of the training cells set aside for conformal calibration.
- cp_alpha
Error level of the conformal procedures, in (0, 1): target false discovery rate for conformal selection and miscoverage level for conformal clustering (target coverage \(1 - \alpha\)). See the section Interpretation of
cp_alpha.- genes_of_interest
Character vector containing the names of genes to include in the conformal analysis. They need to correspond to the row names of
sce.- what
Character vector specifying the conformal procedure(s) to run:
"conf_selection","conf_clustering", or both.- BPPARAM
A
BiocParallelParamobject specifying the parallel back-end used to process the gene chunks. Defaults toSerialParam(). Use, e.g.,MulticoreParam()on Linux/macOS orSnowParam()on Windows.- verbose
Logical indicating whether to display progress information.
Value
The input SingleCellExperiment with its original assays,
rowData, colData and reduced dimensions retained, enriched
with:
colData(sce)$conformeR_splitFactor with levels
"train","cal"and"test"giving the set each cell was assigned to. Conformal results are only defined for test cells.reducedDim(sce, "LEMUR")The aligned LEMUR embedding (cells x
n_embedding).reducedDim(sce, "LEMUR_UMAP"),reducedDim(sce, "LEMUR_aligned_UMAP")UMAPs of the LEMUR embedding before and after Harmony alignment.
altExp(sce, "conformeR")A
SingleCellExperimentwith one row per gene of interest and the same columns assce. Its assays are:DENumeric, the LEMUR predicted differential expression for every cell.
lemur_neighborhoodLogical,
TRUEif the test cell belongs to the LEMUR differential-expression neighborhood of the gene.conf_selectionLogical,
TRUEif the test cell is selected by conformal selection, i.e. confidently belongs to the neighborhood at FDR levelcp_alpha(only if"conf_selection"is inwhat).conf_clustering_inside,conf_clustering_outsideLogical,
TRUEif the label "inside the neighborhood" (respectively "outside") belongs to the conformal prediction set of the test cell (only if"conf_clustering"is inwhat).conf_set_sizeInteger in {0, 1, 2}, the size of the conformal clustering prediction set. Size 1 means an unambiguous assignment; sizes 0 and 2 mean the assignment is ambiguous at level
cp_alpha.
All logical and set-size assays are
NAfor training and calibration cells. TherowDatacontains the LEMUR neighborhood statistics of the test set (prefixed withlemur_), the number of cells in the LEMUR neighborhood (n_lemur_neighborhood), the number of conformally selected cells (n_conf_selection), the score quantile of conformal clustering (conf_quantile) andcp_alpha.metadata(sce)$conformeRA list with the analysis parameters, the contrast and the number of cells in each split.
Because all results are stored in the object, they stay synchronized when the object is subset.
Details
The conformal procedures are applied to a subset of genes of
interest. Genes are processed in chunks of five, which are distributed
over the workers of BPPARAM.
Interpretation of cp_alpha
cp_alpha is an error level, not a coverage level: the target
coverage is \(1 - \alpha\).
- Conformal selection
cp_alphais the target false discovery rate (FDR). Conformal p-values are computed for every test cell and the Benjamini-Hochberg procedure is applied at levelcp_alpha. The expected proportion of selected cells that do not truly belong to the LEMUR neighborhood is at mostcp_alpha. Withcp_alpha = 0.05, at most 5 percent of the selected cells are expected to be false inclusions.- Conformal clustering
cp_alphais the miscoverage level of the prediction sets. For a new cell, the prediction set (inside and/or outside the neighborhood) contains the true neighborhood label with probability at least \(1 - \alpha\) (marginal coverage). Withcp_alpha = 0.05, the prediction sets have 95 percent coverage.
Smaller values of cp_alpha give more conservative results: fewer
selected cells and larger (more often ambiguous) prediction sets.
Examples
data("sce_reduced")
sce_conformer <- conformeR(sce_reduced,
design_lemur = ~ patient_id + condition,
contrast_column = "condition",
n_embedding = 15,
genes_of_interest = c(
"ENSG00000163453",
"ENSG00000163734"
)
)
#> Loading required namespace: SingleCellExperiment
#> Select cells that are considered close with 'harmony'
#> Storing 20% of the data (600 cells) as test data.
#> Regress out global effects using linear method.
#> Find base point for differential embedding
#> Fit differential embedding model
#> Initial error: 4.6e+05
#> ---Fit Grassmann linear model
#> Final error: 1.4e+05
#> Find optimal neighborhood using zscore.
#> Validate neighborhoods using test data
#> Form pseudobulk (summing counts)
#> Calculate size factors for each gene
#> Fit edgeR model on pseudobulk data
#> Fit diff-in-diff effect
#> Find optimal neighborhood using zscore.
#> Validate neighborhoods using test data
#> Form pseudobulk (summing counts)
#> Calculate size factors for each gene
#> Fit edgeR model on pseudobulk data
#> Fit diff-in-diff effect
#> Find optimal neighborhood using zscore.
#> Validate neighborhoods using test data
#> Form pseudobulk (summing counts)
#> Calculate size factors for each gene
#> Fit edgeR model on pseudobulk data
#> Fit diff-in-diff effect
#> Running conformal procedures on 2 gene(s).
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
sce_conformer
#> class: SingleCellExperiment
#> dim: 100 3000
#> metadata(1): conformeR
#> 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(4): cell_type condition patient_id conformeR_split
#> reducedDimNames(3): LEMUR LEMUR_UMAP LEMUR_aligned_UMAP
#> mainExpName: NULL
#> altExpNames(1): conformeR
## conformal results
SingleCellExperiment::altExp(sce_conformer, "conformeR")
#> class: SingleCellExperiment
#> dim: 2 3000
#> metadata(0):
#> assays(6): DE lemur_neighborhood ... conf_clustering_outside
#> conf_set_size
#> rownames(2): ENSG00000163453 ENSG00000163734
#> 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):
SummarizedExperiment::rowData(
SingleCellExperiment::altExp(sce_conformer, "conformeR")
)
#> DataFrame with 2 rows and 15 columns
#> lemur_n_cells lemur_sel_statistic lemur_pval lemur_adj_pval
#> <integer> <numeric> <numeric> <numeric>
#> ENSG00000163453 911 -33.0114 0.623054 0.865149
#> ENSG00000163734 1041 41.6579 0.152668 0.738744
#> lemur_f_statistic lemur_df1 lemur_df2 lemur_lfc lemur_did_pval
#> <numeric> <integer> <numeric> <numeric> <numeric>
#> ENSG00000163453 0.243105 1 100 -0.789439 0.731742
#> ENSG00000163734 2.076898 1 100 2.186511 0.799525
#> lemur_did_adj_pval lemur_did_lfc n_lemur_neighborhood
#> <numeric> <numeric> <numeric>
#> ENSG00000163453 0.994837 -0.669947 911
#> ENSG00000163734 0.994837 0.578481 1041
#> n_conf_selection conf_quantile cp_alpha
#> <numeric> <numeric> <numeric>
#> ENSG00000163453 0 0.430461 0.05
#> ENSG00000163734 0 0.330858 0.05
## number of conformally selected test cells per gene
rowSums(SummarizedExperiment::assay(
SingleCellExperiment::altExp(sce_conformer, "conformeR"),
"conf_selection"
), na.rm = TRUE)
#> ENSG00000163453 ENSG00000163734
#> 0 0