运行 snakemake 中带通配符的外部脚本

Running external scripts with wildcards in snakemake

我正在尝试 运行 带有外部脚本的 snakemake 规则,该脚本包含 snakemake reathedocs 中所述的通配符。然而,当 运行ning snakemake.

时,我 运行ning 进入 KeyError

例如,如果我们有以下规则:

SAMPLE = ['test']

rule all:
    input:
        expand("output/{sample}.txt", sample=SAMPLE)

rule NAME:
    input: "workflow/scripts/{sample}.R"
    output: "output/{sample}.txt",
    script: "workflow/scripts/{wildcards.sample}.R"

使用包含以下代码的脚本 workflow/scripts/test.R

out.path = snakemake@output[[1]]

out = "Hello World"

writeLines(out, out.path)

尝试执行 snakemake 时出现以下错误。

Building DAG of jobs...
Using shell: /usr/bin/bash
Provided cores: 1 (use --cores to define parallelism)
Rules claiming more threads will be scaled down.
Job counts:
    count   jobs
    1   NAME
    1   all
    2

[Fri May 21 12:04:55 2021]
rule NAME:
    input: workflow/scripts/test.R
    output: output/test.txt
    jobid: 1
    wildcards: sample=test

[Fri May 21 12:04:55 2021]
Error in rule NAME:
    jobid: 1
    output: output/test.txt

RuleException:
KeyError in line 14 of /sc/arion/projects/LOAD/Projects/sandbox/Snakefile:
'wildcards'
  File "/sc/arion/work/andres12/conda/envs/py38/lib/python3.8/site-packages/snakemake/executors/__init__.py", line 2231, in run_wrapper
  File "/sc/arion/projects/LOAD/Projects/sandbox/Snakefile", line 14, in __rule_NAME
  File "/sc/arion/work/andres12/conda/envs/py38/lib/python3.8/site-packages/snakemake/executors/__init__.py", line 560, in _callback
  File "/sc/arion/work/andres12/conda/envs/py38/lib/python3.8/concurrent/futures/thread.py", line 57, in run
  File "/sc/arion/work/andres12/conda/envs/py38/lib/python3.8/site-packages/snakemake/executors/__init__.py", line 546, in cached_or_run
  File "/sc/arion/work/andres12/conda/envs/py38/lib/python3.8/site-packages/snakemake/executors/__init__.py", line 2262, in run_wrapper
Shutting down, this might take some time.
Exiting because a job execution failed. Look above for error message
Complete log: /sc/arion/projects/LOAD/Projects/sandbox/.snakemake/log/2021-05-21T120454.713963.snakemake.log

有谁知道为什么这不能正常工作?

我同意 Dmitry Kuzminov 的观点,即根据通配符编写脚本很奇怪。也许有更好的解决方案。

无论如何,下面这个在 snakemake 6.0.0 上对我有用。请注意,在您的 R 脚本中 snakemake@output[1] 应该是 snakemake@output[[1]],但这并没有给出您报告的问题。

SAMPLE = ['test']

rule all:
    input:
        expand("output/{sample}.txt", sample=SAMPLE)

rule make_script:
    output:
        "workflow/scripts/{sample}.R",
    shell:
        r"""
echo 'out.path = snakemake@output[[1]]' > {output}
echo 'out = "Hello World"' >> {output}
echo 'writeLines(out, out.path)' >> {output}
        """

rule NAME:
    input: 
        "workflow/scripts/{sample}.R"
    output: 
        "output/{sample}.txt",
    script: 
        "workflow/scripts/{wildcards.sample}.R"