【问题标题】:How to gsub through a list of names in a loop如何通过循环中的名称列表 gsub
【发布时间】:2020-11-11 23:13:37
【问题描述】:

我有一批样本要提交以在我的大学集群上进行处理。我有超过 1000 个样本需要运行。不必手动创建脚本,我想知道我可以制作一个 for 循环来替换示例 ID。每个脚本本质上都是一样的,我只需要更改示例 ID 和文件位置即可。

df <- structure(list(V1 = c("#!/bin/bash", "#BSUB -W 1440", "#BSUB -n 16", 
                            "#BSUB -x", "#BSUB -R \"rusage[mem=4000] span[hosts=1]\"", "#BSUB -o /gpfs_common/share01/files/abc123.out.%J.txt", 
                            "#BSUB -e /gpfs_common/share01/files/abc123.err.%J.txt", "", 
                            "", "", "", "mcli cp def456/abc123 /panfs/roc/groups/0/location/data.base", 
                            "gzip /panfs/roc/groups/0/location/data.base/abc123", "mcli mv /panfs/roc/groups/0/location/data.base/abc123.gz def456/", 
                            "", "", "#BSUB -J abc123", "\t\t\t", "", "", "", "", "")), row.names = c(NA, 
                                                                                                     -23L), class = c("data.table", "data.frame"))

names <- list(V1 = c("D00268.merged.dedup.realn.haplotypecaller.g.vcf", 
                         "D00316.merged.dedup.realn.haplotypecaller.g.vcf", "D00426.merged.dedup.realn.haplotypecaller.g.vcf", 
                         "D00432.merged.dedup.realn.haplotypecaller.g.vcf", "D00474.merged.dedup.realn.haplotypecaller.g.vcf", 
                         "D00510.merged.dedup.realn.haplotypecaller.g.vcf", "D00574.merged.dedup.realn.haplotypecaller.g.vcf", 
                         "D00607.merged.dedup.realn.haplotypecaller.g.vcf", "D00619.merged.dedup.realn.haplotypecaller.g.vcf", 
                         "D00662.merged.dedup.realn.haplotypecaller.g.vcf"))
    
locations <- list(V1 = c("s3/lab/wgs/yrkt/D00268/gvcf/", "s3/lab/wgs/dach/D00316/gvcf/", 
                         "s3/lab/wgs/mnpd/D00426/gvcf/", "s3/lab/wgs/yrkt/D00432/gvcf/", 
                         "s3/lab/wgs/ckcs/D00474/gvcf/", "s3/lab/wgs/lbrt/D00510/gvcf/", 
                         "s3/lab/wgs/shlt/D00574/gvcf/", "s3/lab/wgs/shlt/D00607/gvcf/", 
                         "s3/lab/wgs/mnsc/D00619/gvcf/", "s3/lab/wgs/gtdn/D00662/gvcf/"
))

所以 df 只是我想要运行 for 循环的主脚本。我在主脚本中将示例名称更改为“abc123”,将示例位置更改为“def456”,这样我就可以使用 gsub 之类的东西来识别这两种模式并将它们替换为示例 ID 和示例位置。我希望在完成后创建一个看起来像这样的文本文件。

#!/bin/bash
#BSUB -W 1440
#BSUB -n 16
#BSUB -x
#BSUB -R "rusage[mem=4000] span[hosts=1]"
#BSUB -o /gpfs_common/share01/files/D00268.merged.dedup.realn.haplotypecaller.g.vcf.out.%J.txt
#BSUB -e /gpfs_common/share01/files/D00268.merged.dedup.realn.haplotypecaller.g.vcf.err.%J.txt




mcli cp s3/lab/wgs/yrkt/D00268/gvcf/D00268.merged.dedup.realn.haplotypecaller.g.vcf /panfs/roc/groups/0/location/data.base
gzip /panfs/roc/groups/0/location/data.base/D00268.merged.dedup.realn.haplotypecaller.g.vcf
mcli mv /panfs/roc/groups/0/location/data.base/D00268.merged.dedup.realn.haplotypecaller.g.vcf.gz s3/lab/wgs/yrkt/D00268/gvcf/


#BSUB -J D00268.merged.dedup.realn.haplotypecaller.g.vcf
        

我认为 for 循环将是最简单的方法,但我愿意接受建议。希望这一切都说得通。如果您有任何问题,请告诉我

我过去使用过这个 for 循环,但我从未使用过 for 循环来 gsub 通过列表

for(i in 1:nrow(df)){
  df[i,'V1'] <- gsub("abc123", "D00268.merged.dedup.realn.haplotypecaller.g.vcf", df[i,'V1'])
  df[i,'V1'] <- gsub("def456", "s3/lab/wgs/yrkt/D00268/gvcf/", df[i,'V1'])
  
}

【问题讨论】:

  • 您能告诉我们您用来替换 ID 的逻辑吗?
  • 我想我编辑了问题来回答你的问题
  • 您想要一个包含 10 个数据框的列表吗? (names$v1 / locations$V1 的每个元素一个)
  • 没错。然后我可以将数据帧写入可以为每个样本提交的文本文件
  • 在您的示例中,for-loop "def345" 应该是 "def456" ,根据 df 中的占位符

标签: r for-loop


【解决方案1】:

要坚持 for 循环的想法并修改您建议的方法,您可以执行以下操作:

for(i in 1:length(locations[[1]])){

df2 <- df
df2[,'V1'] <- gsub("abc123", names[['V1']][i], df2[,'V1'])
df2[,'V1'] <- gsub("def456", locations[['V1']][i], df2[,'V1'])
fileConn<-file(paste0("script_", i, ".sh" ))
writeLines(df2$V1, fileConn)
close(fileConn)

}

【讨论】:

    【解决方案2】:

    purrr::map2 可以对两个向量的元素应用一个函数。

    res <- purrr::map2(
      names$V1,
      locations$V1, 
      function(name, location) {
        result <- gsub("abc123", name, df$V1)
        result <- gsub("def456", location, result)
        result
      }
    )
    
    length(res)
    #> [1] 10
    

    这是第一个结果

    cat(paste0(res[[1]], collapse = "\n"))
    #> #!/bin/bash
    #> #BSUB -W 1440
    #> #BSUB -n 16
    #> #BSUB -x
    #> #BSUB -R "rusage[mem=4000] span[hosts=1]"
    #> #BSUB -o /gpfs_common/share01/files/D00268.merged.dedup.realn.haplotypecaller.g.vcf.out.%J.txt
    #> #BSUB -e /gpfs_common/share01/files/D00268.merged.dedup.realn.haplotypecaller.g.vcf.err.%J.txt
    #> 
    #> 
    #> 
    #> 
    #> mcli cp s3/lab/wgs/yrkt/D00268/gvcf//D00268.merged.dedup.realn.haplotypecaller.g.vcf /panfs/roc/groups/0/location/data.base
    #> gzip /panfs/roc/groups/0/location/data.base/D00268.merged.dedup.realn.haplotypecaller.g.vcf
    #> mcli mv /panfs/roc/groups/0/location/data.base/D00268.merged.dedup.realn.haplotypecaller.g.vcf.gz s3/lab/wgs/yrkt/D00268/gvcf//
    #>   
    #>   
    #>   #BSUB -J D00268.merged.dedup.realn.haplotypecaller.g.vcf
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 2021-10-24
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多