代码一:基于count数据的加和
library(Seurat)
library(Matrix)
library(tidyverse)
#Create pseudo-bulk counts file
#obj是seurat对象
#projid是想要分组的对象 比如可以是celltype,这样得到的就是以celltype为结果的pseudobulk
pseudobulk <- function(obj){
matrix <- GetAssayData(obj, slot="counts")
df <- data.frame(rownames(matrix))
samples <- as.character(unique(obj@meta.data$projid))
for (i in samples){
matrix <- GetAssayData(subset(obj, subset = projid == i), slot="counts")
bulk <- data.frame(Matrix::rowSums(matrix))
df[as.character(i)] <- bulk[1]
}
df <- data.frame(df, row.names = 1)
names(df) <- sub("^X","",names(df))
return(df)
write.csv(df, "bulk.csv")