首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有什么优雅的方法可以防止snakemake在shell/R错误中失败?

有什么优雅的方法可以防止snakemake在shell/R错误中失败?
EN

Stack Overflow用户
提问于 2017-08-10 20:33:27
回答 1查看 2.3K关注 0票数 5

我希望即使在某些规则失败的情况下,我的snakemake工作流也能继续运行。

例如,我正在使用各种工具来执行ChIP-seq数据的峰值调用。但是,当某些程序不能识别峰值时,它们会发出错误。我更喜欢在这种情况下创建一个空的输出文件,而不是让snakemake失败(就像一些峰值调用者已经做的那样)。

有没有一种使用"shell“和"run”关键字的类似蛇的方式来处理这种情况?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-11 00:47:50

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

代码语言:javascript
复制
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()模块,因此外壳命令不需要作为参数数组传递。

代码语言:javascript
复制
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脚本中:

代码语言:javascript
复制
rule some_rule:
    output:
        "outfile"
    run:
        shell("command_that_errors {output} || rc=$?; if [[ $rc == 2 ]]; then exit 0; else exit $?; fi")
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45613881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档