1
votes

I try to make function plots or my top 20 genes. That's why I created a list of data frames, these data frames exist or different columns contain values ​​and names. One of these columns in the data frame is the gene column. My code turns the first 20 genes into function plots. But now I have the problem in some of the data frames that exist fewer than 20 genes. This causes my code to abort.

Because I want a maximum of 5 function plots per page, I cannot just define a counter.

Thank you for the input.

Example of my list or data frames listGroups

group1_2: 'data.frame': 68 obs. of 7 variables:
    ..$ p_val: num [1:68] 1.15 1.43 ...
    ..$ score: num [1:68] 15.5 27.14 ...
    ..$ gene: Factor w/ 68 levels "BRA1", "NED",...: 41 52 ...
group2_3: 'data.frame': 3 obs. of 7 variables:
    ..$ p_val: num [1:3] 1.15 1.43 ...
    ..$ score: num [1:3] 15.5 27.14 ...
    ..$ gene: Factor w/ 3 levels "BCL12", "DEF1",...: 41 52 ...

Code

groupNames <- c("cluster1_2","cluster2_3","cluster3_4","cluster4_5","cluster5_6")
for (i in 1:length(listGroups)) {
  Grouplist <- listGroups[[i]]
  genesList <- Grouplist['gene']
  lengths(geneList)
  print(groupNames[i])
  # Make Featureplots for top20 DE genes per cluster_group
  pdf(file=paste0(sampleFolder,"/Featureplots_cluster_",groupNames[i],"_",sampleName,".pdf"))
  print(FeaturePlot(object = seuratObj, features = c(as.character(genesList[1:5,]))))
  print(FeaturePlot(object = seuratObj, features = c(as.character(genesList[6:10,]))))
  print(FeaturePlot(object = seuratObj, features = c(as.character(genesList[11:15,]))))
  print(FeaturePlot(object = seuratObj, features = c(as.character(genesList[16:20,]))))
  dev.off()
}
3

3 Answers

2
votes

For each genelist you could make one plot with your choice of genes like this (plot would look fine with larger size, as in your PDF):

  • use combine=FALSE and limit the number of features to plot to something like rownames(pbmc_small)[1 : min(20, nrow(pbmc_small))] to avoid errors
  • then export the list of single plots (allows for themeing) and plot into pdf using cowplot::plot_grid
  • instead of plotting within the function (plot(out)), you could export to pdf (maybe pass filename as second argument to function).
library(Seurat)
genelist <- list(
    l1 = sample(rownames(pbmc_small), 23),
    l2 = sample(rownames(pbmc_small), 14),
    l3 = sample(rownames(pbmc_small), 4))

plotFeatures <- function(x){
    p <- FeaturePlot(object = pbmc_small, 
        features = x[1 : min(20, length(x))], 
        combine = FALSE, label.size = 2)
    out <- cowplot::plot_grid(plotlist = p, ncol = 5, nrow = 4)
    plot(out)
}
lapply(genelist, plotFeatures)
1
votes

Not tested, something like this should work. Instead of calling print 5 times for each 5 genes, we call it in a loop n times based on number of genes. If we have 10 genes forloop will print twice, if 20 then we call print 4 times, etc:

groupNames <- c("cluster1_2","cluster2_3","cluster3_4","cluster4_5","cluster5_6")

for (i in 1:length(listGroups)) {
  Grouplist <- listGroups[[i]]
  genesList <- Grouplist['gene']
  #lengths(geneList)
  print(groupNames[i])
  # Make Featureplots for top20 DE genes per cluster_group

  # make chunks of 5 each. 
  myChunks <- split(genesList, ceiling(seq_along(genesList)/5))

  pdf(file=paste0(sampleFolder,"/Featureplots_cluster_",groupNames[i],"_",sampleName,".pdf"))

  # loop through genes plotting 5 genes each time.
  for(x %in% seq(myChunks) ){
    print(FeaturePlot(object = seuratObj, features = myChunks[[ x ]]))
  }

  dev.off()
}
0
votes

Thanks to the input of zx8754 and user12728748. I found two solutions for my problem.

        for (i in 1:length(listGroups)) {
      Grouplist <- listGroups[[1]]
      genesList <- Grouplist['gene']
      print(groupNames[1])

    ## Solution 1
    # Here all genes are printed. I didn't find a way yet to limited to 20

      # make chunks of 5 each. 
      myChunks <- split(genesList,ceiling(seq(lengths(genesList))/5))
      # Make Featureplots for top20 DE genes per cluster_group
      pdf(file=paste0(sampleFolderAggr,"results/Featureplots_",groupNames[i],"_",sampleNameAggr,".pdf"))
      # loop through genes plotting 5 genes each time.
      for(x in 1:min(5, length(myChunks) ){
        # Create a list of 5 genes
        my5Genes <- as.list(myChunks[[x]])

        print(FeaturePlot(object = seuratObj, features = c(as.character(my5Genes$gene))))
      }
      dev.off()

    ## Solution 2

    pdf(file=paste0(sampleFolderAggr,"results/Featureplots_",groupNames[i],"_",sampleNameAggr,".pdf"))

    plotFeatures <- function(x){
        p <- FeaturePlot(object = seuratObj, features = c(as.character(x[1: min(20, lengths(x)),])), combine = FALSE, label.size = 2)
        out <- cowplot::plot_grid(plotlist = p, ncol = 5, nrow = 4)
        # Make Featureplots for top20 DE genes per cluster_group
        plot(out)
        }
      lapply(genelist, plotFeatures)
      dev.off()
    }