Repository navigation
add matrix to colData of spe object #179
Description
Activity
Hey @cathalgking
Hi @user — thanks for the detailed write-up with the
str()output, that makes this easy to answer.Short version: yes, cell order is conserved, and what you have works.
escape.matrix()never reorders or drops cells. Internally it walks the count matrix in contiguous column blocks ofgroups(default 1000) and binds the per-block results back together in the same order, so:nrow(output) == ncol(input)rownames(output)iscolnames(input), in the original order
The
min.size,min.expr.cells, andmin.filter.byarguments filter gene sets and genes, never cells — you always get one row back per cell. Your output confirms it: 163,797 rows against 163,797 columns, with rownames"1", "2", "4", "5", ...in the same order as yourcolData.Quick caveat: match on names, not position
colData(spe)[, colnames(trial)] <- trialis a positional assignment. It's correct here, but it will silently misalign if anything ever reorders or subsets between the two calls. One extra index makes it order-independent:# reorder rows of `trial` to match the object explicitly colData(spe)[, colnames(trial)] <- trial[colnames(spe), , drop = FALSE]
Reproducible check with the example data
library(escape) library(SingleCellExperiment) GS <- list(Bcells = c("MS4A1", "CD79B", "CD79A", "IGH1", "IGH2"), Tcells = c("CD3E", "CD3D", "CD3G", "CD7", "CD8A")) sce <- Seurat::as.SingleCellExperiment(SeuratObject::pbmc_small) es <- escape.matrix(sce, method = "AUCell", gene.sets = GS, min.size = NULL) dim(es) # 80 x 2 -> cells x gene sets identical(rownames(es), colnames(sce)) # TRUE -> order conserved
Since you're on Xenium data,
colDatais the right call if you're feeding these into spatial plots - and columns there subset correctly withspe[, i], so downstream filtering stays aligned. Worth noting that escape's own plotting functions (heatmapEnrichment(),ridgeEnrichment(), etc.) look for the assay, so you may wantrunEscape()as well and keep both copies if you plan to use those.Hope that helps and let me know if you have any issues.
Nick
That worked, thanks @ncborcherding
One related thing I noticed is thatescape.matrix()seems to be introducing some variability, at least withAUCell. Have you noticed that? Would it be to do with the chunking that is used? I tried setting a seed and still noticed this variability, although very minor when looking at the overall distributions.trial <- escape.matrix(spe, method = "AUCell", gene.sets = GS, min.size = NULL) head(trial) Bcells Tcells 1 0.01092896 0.0000000 2 0.00000000 0.0000000 3 0.00000000 0.0661157 4 0.00000000 0.1033058 5 0.00000000 0.0000000 6 0.00000000 0.0000000re-run
head(trial) Bcells Tcells 1 0.00000000 0.0000000 2 0.00000000 0.0000000 3 0.18032787 0.0000000 4 0.00000000 0.1942149 5 0.00000000 0.0000000 6 0.05464481 0.0000000Hey @cathalgking
This is a quirk of AUCell itself - it is not deterministic, meaning it will give different values each time it is run.
Nick
Reacted by Cathal King
I would like to add the results of escape to the metadata (colData) of my data object, instead of an assay.
Add the dense numeric matrix from the output of
escape.matrixto the colData of mySingleCellExperimentobject. Are the orders of the cells conserved (matched) when I add this to metadata?gene set
Run escape
output
then
colData
Thanks in advance!