繁体   English   中英

修改R脚本以在Snakemake中使用命令行参数

[英]Modifying R script to use command line arguments for use in Snakemake

我写了这个小R脚本来绘制DNA序列覆盖率数据图,并将其作为目录中所有文件的输入。

coverage.files<-list.files("~/coverage_plotting", full.names = TRUE, pattern = ".txt")
coverage.names<-list.files("~/coverage_plotting", full.names = F, pattern=".txt")
pdf.files <- gsub("txt","pdf", coverage.file)
plot.colors <- c("red","blue","green","yellow","purple")
for(i in 1:length(coverage.name)) {
  coverage <- read.delim(coverage.file[i])
  pdf(pdf.files[i], width = 5, height= 4)
  colnames(coverage) <- c("contig", "position", "coverage")
  contigs <- unique(coverage[,1])
  plot(-100,-100, xlim=c(0,800), ylim=c(0,500000), xlab="Coverage", ylab="Number of basepairs")
  for(j in contigs) {
    contig.cov <- subset(coverage,contig==j)
    cov.hist <- hist(contig.cov$coverage, breaks=seq(0,5000, by = 2), plot=F)
    points(cov.hist$mids, cov.hist$counts, type="p", col=plot.colors[j], pch=19, cex=0.5)
  }
  dev.off()
}

现在,我想将该脚本包含在Snakemake文件中,因此希望将其更改为将单个文件作为命令行输入。 我找到了commandArgs()并尝试使用它,也摆脱了第一个循环,因为现在一次只输入一个文件。 我最终得到了这样的东西

coverage.file <- commandArgs()
pdf.file <- gsub("txt","pdf", coverage.file)
plot.colors <- c("red","blue","green","yellow","purple")
coverage <- read.delim(coverage.file)
pdf(pdf.file, width = 5, height= 4)
colnames(coverage) <- c("contig", "position", "coverage")
contigs <- unique(coverage[,1])
plot(-100,-100, xlim=c(0,800), ylim=c(0,500000), xlab="Coverage", ylab="Number of basepairs")
  for(j in contigs) {
    contig.cov <- subset(coverage,contig==j)
    cov.hist <- hist(contig.cov$coverage, breaks=seq(0,5000, by = 2), plot=F)
    points(cov.hist$mids, cov.hist$counts, type="p", col=plot.colors[j], pch=19, cex=0.5)
  }
  dev.off()

运行它时,出现以下错误,

Error in file(file, "rt") : cannot open the connection
Calls: read.delim -> read.table -> file
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'coverage.file': No such file or directory
Execution halted

有人对我应该如何修改它以从命令行获取单个输入有任何建议吗?

谢谢

R doc声明了commandArgs()

包含可执行文件名称和用户提供的命令行参数的字符向量。 第一个元素是调用R的可执行文件的名称。 此元素的确切形式取决于平台:它可以是标准名称,或者只是应用程序的最后一个组件(或基本名称),对于嵌入式R,它可以是程序员提供的任何东西。如果railingOnly = TRUE,则为--args之后提供的那些参数(如果有)的字符向量。

参见https://www.rdocumentation.org/packages/base/versions/3.0.3/topics/commandArgs

因此,您的object coverage.file是矢量,您应该通过在矢量中指定位置来访问参数。 例如:

args <- commandArgs(trailingOnly=TRUE)
# access i'th argument depending how you write you shell command in the snakemake. ex:
coverage.file <- args[1]
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM