【问题标题】:Loop through a list of dataframes output plots循环遍历数据框输出图列表
【发布时间】:2020-02-06 14:00:42
【问题描述】:

我尝试制作函数图或我的前 20 个基因。这就是为什么我创建了一个数据框列表,这些数据框存在或不同的列包含值和名称。 数据框中的这些列之一是基因列。我的代码将前 20 个基因变成了函数图。但是现在我在一些存在的数据框中遇到了问题 不到20个基因。这会导致我的代码中止。

因为我希望每页最多有 5 个函数图,所以我不能只定义一个计数器。

感谢您的意见。

我的列表或数据框示例 列表组

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 ...

代码

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

【问题讨论】:

    标签: r list dataframe bioinformatics seurat


    【解决方案1】:

    对于每个基因列表,您可以使用您选择的这样的基因制作一个图(如果您的 PDF 尺寸较大,图看起来会很好):

    • 使用combine=FALSE 并将要绘制的特征数量限制为rownames(pbmc_small)[1 : min(20, nrow(pbmc_small))] 之类的内容以避免错误
    • 然后导出单个绘图列表(允许主题化)并使用cowplot::plot_grid 将绘图转换为pdf
    • 您可以导出为 pdf,而不是在函数 (plot(out)) 内绘图(也许将文件名作为第二个参数传递给函数)。
    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)
    

    【讨论】:

      【解决方案2】:

      未经测试,这样的东西应该可以工作。我们不是为每 5 个基因调用 print 5 次,而是根据基因数量在循环中调用它 n 次。如果我们有 10 个基因 forloop 将打印两次,如果有 20 个则我们调用 print 4 次,依此类推:

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

      【讨论】:

      • 这是一个好主意,下面我调整了代码,使其适用于我的数据类型。
      【解决方案3】:

      感谢zx8754和user12728748的输入。我为我的问题找到了两种解决方案。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-08
        • 1970-01-01
        • 2021-08-31
        • 2021-02-08
        • 1970-01-01
        • 2018-05-30
        • 2015-05-14
        相关资源
        最近更新 更多