Snakemake:在一个输出目录中输出文件

问题描述

我正在运行的程序只需要指定目录即可保存输出文件。如果我用 snakemake 运行它,它会给我错误

IOError: [Errno 2] No such file or directory: 'BOB/call_images/chrX_194_1967_BOB_78.rpkm.png'

我的尝试:

rule all:
    input:
        "BOB/call_images/"

rule plot:
    input:
        hdf="BOB/hdf5/analysis.hdf5",txt="BOB/calls.txt"
    output:
        directory("BOB/call_images/")
    shell:
        """
        python someprogram.py plotcalls --input {input.hdf} --calls {input.txt} --outputdir {output[0]}
        """

此版本均无效:

output:
     outdir="BOB/call_images/"

解决方法

通常,snakemake 会为指定的输出文件创建输出目录。蛇形directory()声明告诉蛇形该目录是输出,因此它留给规则来创建输出目录。

如果您可以预测输出文件的名称(即使您没有在命令行中指定它们),您应该在 output: 字段中告诉 snakemake 输出文件名。 EG:

rule plot:
    input:
        hdf="BOB/hdf5/analysis.hdf5",txt="BOB/calls.txt"
    output:
        "BOB/call_images/chrX_194_1967_BOB_78.rpkm.png"
    params:
        outdir="BOB/call_images"
    shell:
        """
        python someprogram.py plotcalls --input {input.hdf} --calls {input.txt} --outputdir {params.outdir}
        """

(使用 params 定义输出目录而不是在 shell 命令中对其进行硬编码的优点是您可以在那里使用通配符)

如果您无法预测输出文件名,那么您必须手动运行 mkdir -p {output} 作为 shell 命令的第一步。

rule plot:
    input:
        hdf="BOB/hdf5/analysis.hdf5",txt="BOB/calls.txt"
    output: directory("BOB/call_images")
    shell:
        """
        mkdir -p {output}
        python someprogram.py plotcalls --input {input.hdf} --calls {input.txt} --outputdir {output}
        """