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

时间:2019-11-25 20:28:56

标签: 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
        """

1 个答案:

答案 0 :(得分:0)

如果我的理解正确,那么您希望在不停止整个管道的情况下允许铲失败,并且您希望忽略发生故障的铲的输出文件。为此,您可以在运行黑桃|| 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
        """
相关问题