python - 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 脚本中名为 snakemake 的 S4 对象。 现在在我的例子中,我在 SLURM 集群上运行 snakemake,我需要在执行 Rscript 之前使用 module load R/3.6.0 加载 R,否则作业将返回:

/usr/bin/bash: Rscript: command not found

我怎样才能告诉 snakemake 这样做呢?如果我将规则作为 shell 而不是脚本运行,不幸的是我的 R 脚本无法访问 snakemake 对象,因此这不是理想的解决方案:

shell:
    "module load R/3.6.0;"
    "Rscript path/to/script.R"

最佳答案

您不能使用 script 标签调用 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环境的使用:

您可以指定用于特定规则的 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 的作业中激活。

关于python - Snakemake - 在调用外部脚本之前加载集群模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57411801/

相关文章:

python - 如何仅使用 OpenCV/numpy/scipy 在 python 中计算色彩空间 Delta E?

regex - R vs sed 正则表达式贪婪

python - Python 如何在我被 LSF 分配了 4 个核心的集群上看到 12 个 cpu?

matlab - 集群环境下运行的Matlab中的随机变量生成器

mysql - 为什么分开写和读更好?

python - 为什么 `tf.matmul` 不能与转置张量一起使用?

python - 如何为在AWS EC2上运行的单个脚本安装第3方Python库?

python - 创建一个新列作为 Pandas DataFrame 的计数

R生度日计算中多波段的栅格代数

r - 使用 knitr 的 spin() 时生成目录 (toc)