如何在 Seurat 对象上使用 lapply 函数

问题描述 投票:0回答:1

我正在尝试计算 Seurat 对象中每个簇中的细胞数量,并且我必须在 4 个不同的 Seurat 对象上执行此操作(每个对象都是来自 4 个不同运行的集成对象),我不太确定如何构建我的代码。

这是我到目前为止所拥有的:

library(dplyr)
library(ggplot2)
library(Seurat)



Integrated.Vmat <- readRDS("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_RDS_files_20230102/All_SpeciesVmat.RDS")
Integrated.VGlut <- readRDS("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_RDS_files_20230102/All_SpeciesVGlut.RDS")
Integrated.Gad1 <- readRDS("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_RDS_files_20230102/All_SpeciesGad1.RDS")
Integrated.VAChT <- readRDS("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_RDS_files_20230102/All_SpeciesVAChT.RDS")



l <- list(Integrated.Vmat, Integrated.VGlut, Integrated.Gad1, Integrated.VAChT)

lapply(l, function(x) {
  cell.per.cluster <- table(Idents(x))
  write.csv(cell.per.cluster, file= paste0("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_Subset_Plots_20230102/cells_per_cluster", x, ".csv"))
  
  #How many cells are in each replicate ?
  cell.per.replicate <- table(x$Species)
  write.csv(cell.per.replicate, file=paste0("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_Subset_Plots_20230102/cells_per_replicate", x, ".csv"))
  
  #what proportion of cells are in each cluster?
  proportion.of.cells.per.cluster <- prop.table(table(Idents(x)))
  write.csv(proportion.of.cells.per.cluster, file=paste0("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_Subset_Plots_20230102/Dere/proportion_of_cells_per_cluster", x, ".csv"))

  #How does cluster membership vary by replicate ? 
  proportion.of.cells.per.cluster.per.replicate <- table(Idents(x), x$Species)
  write.csv(proportion.of.cells.per.cluster.per.replicate, file=paste0("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_Subset_Plots_20230102/proportion_of_cells_per_replicate_per_cluster", x, ".csv"))
})

但是我收到这个错误

Error in as.character.default(new("Seurat", assays = list(RNA = new("Assay", :
no method for coercing this S4 class to a vector

有人可以给我建议吗?

非常感谢!

r lapply seurat
1个回答
0
投票

当您调用

lapply(l, function(x))
时,列表中的项目一次会传递给函数并分配给
x
paste()
命令尝试将 Seurat 对象
x
连接到文件名字符串,因此会出现错误。如果您在创建 Seurat 对象时设置了 [email protected],则可以改为使用它。

file = paste0("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_Subset_Plots_20230102/cells_per_cluster",
[email protected], ".csv")
© www.soinside.com 2019 - 2024. All rights reserved.