Snakemake如何在上游规则失败时执行下游规则

2024-05-11

抱歉,标题不好 - 我不知道如何最好地用几句话解释我的问题。当其中一条规则失败时,我在处理 Snakemake 中的下游规则时遇到困难。在下面的示例中,黑桃规则在某些样本上失败。这是预料之中的,因为我的一些输入文件会有问题,黑桃将返回错误,并且不会生成目标文件。在我规则 eval_ani 之前这很好。在这里,我基本上想在规则 ani 的所有成功输出上运行此规则。但我不知道如何做到这一点,因为我已经有效地将一些样本丢弃在规则黑桃中。我认为使用 Snakemake 检查点可能有用,但我只是不知道如何从文档中应用它。

我还想知道是否有一种方法可以重新运行规则 ani 而无需重新运行规则黑桃。假设我提前终止了运行,并且规则 ani 没有在所有样本上运行。现在我想重新运行我的管道,但我不希望 Snakemake 尝试重新运行所有失败的黑桃作业,因为我已经知道它们对我没有用,只会浪费资源。我尝试了 -R 和 --allowed-rules 但这些都没有达到我想要的效果。

rule spades:
    input:
        read1=config["fastq_dir"]+"combined/{sample}_1_combined.fastq",
        read2=config["fastq_dir"]+"combined/{sample}_2_combined.fastq"
    output:
        contigs=config["spades_dir"]+"{sample}/contigs.fasta",
        scaffolds=config["spades_dir"]+"{sample}/scaffolds.fasta"
    log:
        config["log_dir"]+"spades/{sample}.log"
    threads: 8
    shell:
        """
        python3 {config[path_to_spades]} -1 {input.read1} -2 {input.read2} -t 16 --tmp-dir {config[temp_dir]}spades_test -o {config[spades_dir]}{wildcards.sample} --careful > {log} 2>&1
        """

rule ani:
    input:
        config["spades_dir"]+"{sample}/scaffolds.fasta"
    output:
        "fastANI_out/{sample}.txt"
    log:
        config["log_dir"]+"ani/{sample}.log"
    shell:
        """
        fastANI -q {input} --rl {config[reference_dir]}ref_list.txt -o fastANI_out/{wildcards.sample}.txt
        """

rule eval_ani:
    input:
        expand("fastANI_out/{sample}.txt", sample=samples)
    output:
        "ani_results.txt"
    log: 
        config["log_dir"]+"eval_ani/{sample}.log"
    shell:
        """
            python3 ./bin/evaluate_ani.py {input} {output} > {log} 2>&1
        """

如果我理解正确的话,您希望允许黑桃失败而不停止整个管道,并且您希望忽略失败的黑桃的输出文件。为此,您可以附加到运行黑桃的命令|| true捕获非零退出状态(因此snakemake不会停止)。然后,您可以分析黑桃的输出并写入“标志”文件,无论该示例是否成功。所以黑桃规则是这样的:

rule spades:
    input:
        read1=config["fastq_dir"]+"combined/{sample}_1_combined.fastq",
        read2=config["fastq_dir"]+"combined/{sample}_2_combined.fastq"
    output:
        contigs=config["spades_dir"]+"{sample}/contigs.fasta",
        scaffolds=config["spades_dir"]+"{sample}/scaffolds.fasta",
        exit= config["spades_dir"]+'{sample}/exit.txt',
    log:
        config["log_dir"]+"spades/{sample}.log"
    threads: 8
    shell:
        """
        python3 {config[path_to_spades]} ... || true
        # ... code that writes to {output.exit} stating whether spades succeded or not 
        """

对于以下步骤,您使用标志文件'{sample}/exit.txt'决定应该使用哪些铲文件以及应该丢弃哪些铲文件。例如:

rule ani:
    input:
        spades= config["spades_dir"]+"{sample}/scaffolds.fasta",
        exit= config["spades_dir"]+'{sample}/exit.txt',
    output:
        "fastANI_out/{sample}.txt"
    log:
        config["log_dir"]+"ani/{sample}.log"
    shell:
        """
        if {input.exit} contains 'PASS':
            fastANI -q {input.spades} --rl {config[reference_dir]}ref_list.txt -o fastANI_out/{wildcards.sample}.txt
        else:
            touch {output}
        """
        
rule eval_ani:
    input:
        ani= expand("fastANI_out/{sample}.txt", sample=samples),
        exit= expand(config["spades_dir"]+'{sample}/exit.txt', sample= samples),
    output:
        "ani_results.txt"
    log: 
        config["log_dir"]+"eval_ani/{sample}.log"
    shell:
        """
        # Parse list of file {input.exit} to decide which files in {input.ani} should be used
        python3 ./bin/evaluate_ani.py {input} {output} > {log} 2>&1
        """

EDIT (未测试)代替|| true在 - 的里面shell指令可能更好地使用run指令并使用 python 的subprocess运行允许失败的系统命令。原因是|| true无论发生什么错误都会返回 0 退出代码;这subprocess相反,解决方案允许更精确地处理异常。例如。

rule spades:
    input:
        ...
    output:
        ...
    run:
        cmd = "spades ..."
        p = subprocess.Popen(cmd, shell= True, stdout= subprocess.PIPE, stderr= subprocess.PIPE)
        stdout, stderr= p.communicate()

        if p.returncode == 0:
            print('OK')
        else:
            # Analyze exit code and stderr and decide what to do next
            print(p.returncode)
            print(stderr.decode())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Snakemake如何在上游规则失败时执行下游规则 的相关文章

随机推荐