Search code examples
rlapplyseurat

How do I use the function lapply on Seurat objects


I am trying to calculate the number of cells in each cluster in a Seurat object and I have to do this on 4 different Seurat objects (each single one is an integrated object from 4 different runs) I am not so sure how to structure my code.

Here there is what I have so far:

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"))
})

But I get this error

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

Can someone advise me on this?

Thank you very much!


Solution

  • When you call lapply(l, function(x)), the items in the list are passed to the function one at a time and assigned to x. The paste() command attempts to concatenate x, a Seurat object, to the file name string, hence the error. You can use [email protected] instead, if you set it when creating the Seurat objects.

    file = paste0("/Users/avolaa/Documents/Single_Cell_Paper/Species_Integration_Seurat/All_species_20230102/Seurat_Subset_Plots_20230102/cells_per_cluster",
    [email protected], ".csv")