6

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

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

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

谢谢

4

1 回答 1

8

对于shell命令,您始终可以利用条件“或”,||

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总是返回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")
于 2017-08-10T16:47:50.070 回答