首页 > 解决方案 > 在snakemake中运行带有通配符的外部脚本

问题描述

我正在尝试使用包含通配符的外部脚本运行snakemake 规则,如snakemake rethedocs中所述。但是我在运行snakemake时遇到了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

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

标签: snakemake

解决方案


我同意 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"

推荐阅读