什么是防止 snakemake 在 shell/R 错误时失败的优雅方法?

What would be an elegant way of preventing snakemake from failing upon shell/R error?

我希望能够让我的 snakemake 工作流程继续 运行 即使某些规则失败。

例如,我正在使用各种工具来执行 ChIP-seq 数据的峰值检出。但是,某些程序在无法识别峰时会发出错误。在这种情况下,我更愿意创建一个空的输出文件,并且不会让 snakemake 失败(就像一些 peak-callers 已经做的那样)。

是否有类似 snakemake 的方法来处理这种情况,使用 "shell" 和 "run" 关键字?

谢谢

对于 shell 命令,您始终可以利用条件 "or"、||:

rule some_rule:
    output:
        "outfile"
    shell:
        """
        command_that_errors || true
        """

# or...

rule some_rule:
    output:
        "outfile"
    run:
        shell("command_that_errors || true")

通常退出代码为零 (0) 表示成功,任何非零表示失败。包括 || true 可确保在命令以非零退出代码退出时成功退出(true 总是 returns 0)。

如果您需要允许特定的非零退出代码,您可以使用shell或Python来检查代码。对于 Python,它将类似于以下内容。使用 shlex.split() 模块,因此 shell 命令不需要作为参数数组传递。

import shlex

rule some_rule:
    output:
        "outfile"
    run:
        try:
           proc_output = subprocess.check_output(shlex.split("command_that_errors {output}"), shell=True)                       
        # an exception is raised by check_output() for non-zero exit codes (usually returned to indicate failure)
        except subprocess.CalledProcessError as exc: 
            if exc.returncode == 2: # 2 is an allowed exit code
                # this exit code is OK
                pass
            else:
                # for all others, re-raise the exception
                raise

在 shell 脚本中:

rule some_rule:
    output:
        "outfile"
    run:
        shell("command_that_errors {output} || rc=$?; if [[ $rc == 2 ]]; then exit 0; else exit $?; fi")