Seurat构建meta- feature表达矩阵
2022-05-22 本文已影响0人
倪桦
对于小数据集
直接使用aggregate函数对数据框形式的表达矩阵进行运算
counts <- sc[['RNA']]@counts #取出表达矩阵
4 x 4 sparse Matrix of class "dgCMatrix"
cell.1 cell.2 cell.3 cell.4
gene.1 2 4 3 3
gene.2 6 4 3 3
gene.3 1 2 3 3
gene.4 8 9 . 3
data.use <- sc[['RNA']]@meta.features #取出需要重合的gene标签"pseudoid"
pseudoid
gene.1 a
gene.2 a
gene.3 b
gene.4 c
#根据pseudoid聚合基因表达量
counts.new = aggregate(counts,by=list(data.use$pseudoid),FUN=sum) %>% data.table::data.table() %>% tibble::column_to_rownames(var = "Group.1")
sc.agg <- CreateSeuratObject(counts = counts)
对于大型稀疏矩阵,推荐使用矩乘的形式进行处理
bMfeatures <- function(object,method="metaSum"){
data <- object@assays$RNA@meta.features
category.matrix <- Matrix::sparse.model.matrix(object = ~ -1 + data[,1] ) #创建分类矩阵
category.matrix = Matrix::t(category.matrix)
rownames(category.matrix) <- gsub("data\\[, 1\\]","",rownames(category.matrix))
data.use <- GetAssayData(object, slot = "counts")
aggreate.counts = category.matrix %*% data.use
if (method == "gSI") {aggreate.counts = aggreate.counts / Matrix::rowSums(aggreate.counts)}#计算基因表达特异性指数
toRet <- CreateSeuratObject(counts = aggreate.counts)
return(toRet)
}
sc.meta <- bMfeatures(mini.sc,method = 'gSI')