Python Snakemake-在调用外部脚本之前加载集群模块
在snakemake中,您可以这样调用外部脚本:Python Snakemake-在调用外部脚本之前加载集群模块,python,r,cluster-computing,snakemake,Python,R,Cluster Computing,Snakemake,在snakemake中,您可以这样调用外部脚本: rule NAME: input: "path/to/inputfile", "path/to/other/inputfile" output: "path/to/outputfile", "path/to/another/outputfile" script: "path/to/script.R" 这样可以方便地访问R脚本中名为snakem
rule NAME:
input:
"path/to/inputfile",
"path/to/other/inputfile"
output:
"path/to/outputfile",
"path/to/another/outputfile"
script:
"path/to/script.R"
这样可以方便地访问R脚本中名为snakemake
的S4对象。
现在,在我的例子中,我正在SLURM集群上运行snakemake,在执行Rscript之前,我需要使用module load R/3.6.0
加载R,否则作业将返回:
/usr/bin/bash: Rscript: command not found
我怎么能让蛇毒这么做?如果将规则作为shell而不是脚本运行,很遗憾,我的R脚本无法访问snakemake
对象,因此这不是理想的解决方案:
shell:
"module load R/3.6.0;"
"Rscript path/to/script.R"
不能使用
脚本
标记调用shell命令。您必须使用shell
标记。始终可以将输入和输出添加为参数:
rule NAME:
input:
in1="path/to/inputfile",
in2="path/to/other/inputfile"
output:
out1="path/to/outputfile",
out2="path/to/another/outputfile"
shell:
"""
module load R/3.6.0
Rscript path/to/script.R {input.in1} {input.in2} {output.out1} {output.out2}
"""
并在R脚本中获取参数:
args=commandArgs(trailingOnly=TRUE)
inFile1=args[1]
inFile2=args[2]
outFile1=args[3]
outFile2=args[4]
康达环境的使用:
您可以指定用于特定规则的conda环境:
rule NAME:
input:
in1="path/to/inputfile",
in2="path/to/other/inputfile"
output:
out1="path/to/outputfile",
out2="path/to/another/outputfile"
conda: "r.yml"
script:
"path/to/script.R"
在您的r.yml文件中:
name: rEnv
channels:
- r
dependencies:
- r-base=3.6
然后,当您运行snakemake时:
snakemake .... --use-conda
Snakemake将在运行之前安装所有环境,每个环境都将在发送给slurm的作业中激活。如果您想在Rscript命令中按名称调用参数,您可以使用类似的方式(基本上是Eric答案的扩展): 然后在
script.R
中,通过解析命令行访问每个参数:
args <- commandArgs(trailingOnly= TRUE)
for(x in args){
if(grepl('^inFile1=', x)){
inFile1 <- sub('^inFile1=', '', x)
}
else if(grepl('^inFile2=', x)){
inFile2 <- sub('^inFile2=', '', x)
}
else if(grepl('^outFile1=', x)){
outFile1 <- sub('^outFile1=', '', x)
}
else if(grepl('^outFile2=', x)){
outFile2 <- sub('^outFile2=', '', x)
}
else {
stop(sprintf('Unrecognized argument %s', x))
}
}
# Do stuff with inFile1, inFile2, etc...
args是的,这是我的临时解决方案。但它不如按名称访问参数那么健壮,因为您必须记住在下一个脚本中哪个参数位于哪个位置。@bgbrink是的。我看到的另一种选择是使用conda并为您的规则指定一个具有r3.6的环境。另一个我不喜欢(不是很容易复制)的方法是在运行snakemake之前加载模块,并将环境变量(包括路径)发送到slurm。如果您只想为此规则加载R,则后一种解决方案有缺点。要继续此操作…如何为规则指定conda环境?或者启动作业的snakemake环境是否会传播给所有的孩子,也就是说,如果我在snakemake环境中安装R,作业会知道吗?@bgbrink我添加了一个conda示例。至于将您的环境变量发送到SLURM,很抱歉,我无能为力。我使用SGE并将snakemake--cluster“qsub-V…”
发送路径和其他变量到SGE作业。
args <- commandArgs(trailingOnly= TRUE)
for(x in args){
if(grepl('^inFile1=', x)){
inFile1 <- sub('^inFile1=', '', x)
}
else if(grepl('^inFile2=', x)){
inFile2 <- sub('^inFile2=', '', x)
}
else if(grepl('^outFile1=', x)){
outFile1 <- sub('^outFile1=', '', x)
}
else if(grepl('^outFile2=', x)){
outFile2 <- sub('^outFile2=', '', x)
}
else {
stop(sprintf('Unrecognized argument %s', x))
}
}
# Do stuff with inFile1, inFile2, etc...