'Loop for subsetting SingleCellExperiment object in R

I have a SingleCellExperiment object (sce) which I would like to subset by cluster name (19 clusters) to return a list of 19 SingleCellExperiments which I can then use as input to plot expression of 9 markers.

To do this individually, I use the following code:

# add cluster ids to cells (in colData)
sce[["Cluster"]] <- cluster_ids(sce, "annotated")

# subset sce so have marker of interest (example using "153Eu_pSTAT1)
sce <- sce["153Eu_pSTAT1", ]

# split sce by cluster
cells_by_cluster <- split(seq(ncol(sce)), cluster_ids(sce, k = "annotated"))

# example using the cluster "CD56 hi NK"
sce_nk <- sce[, unlist(cells_by_cluster$`CD56 hi NK`)]

# violin plot (using scater plotExpression function)
plotExpression(sce, features = rownames(sce),exprs_values = "exprs", x = "patient_id", colour_by = "patient_id", point_size = 0.1, show_median = TRUE, show_violin = TRUE, add_legend = FALSE)

I want to generate a plot for each of the 19 cluster x 9 marker combinations so want to put this in a loop. I have very limited experience at this, and am not having much success so would appreciate any pointers on where I am going wrong.

I have tried the following

# add cluster ids to cells (in colData)
sce[["Cluster"]] <- cluster_ids(sce, "annotated")

# list of cell clusters in sce from annotation file
anno_30 <- as.data.frame(read.csv("anno_table_30.csv", header = TRUE))

anno_30$new_cluster <- factor(anno_30$new_cluster, levels = c("naive CD4+ T", "CM CD4+ T", "naive CD8+ T", "effector CD8+ T", "CM CD8+ T", "EM CD8+ T", "CD8+ T", "MAIT", "CD56 hi NK", "CD56 lo NK", "B cells", "plasmablasts", "classical monocytes", "alt/int monocytes", "cDC1", "cDC2", "pDC", "basophils", "unknown"))   
     
cluster_list <- as.character(levels(anno_30$new_cluster))

# use this to make a list of clusters to subset sce by (this works)
clusters <- list()
for (i in cluster_list) {
  name <- paste("cells_by_cluster$`", i, "`", sep = "")
  clusters[[i]] <- name
}

# make a list of 19 sce objects subsetted by cluster name (this returns a list of 19 sce objects, one for each cluster, but they contain 0 cells)
sce_list <- list()
for (i in clusters) {
  sce_cluster[[i]] <- sce_v_plots[, unlist(clusters[[i]])]
  sce_list[[i]] <- sce_cluster
}

Clearly there is something wrong with the way I am constructing the loop to subset the sce but I am unable to work out what it is - any help would be great! Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source