处理 snakemake 中的 SIGPIPE 错误

Handling SIGPIPE error in snakemake

以下 snakemake 脚本:

rule all:
    input:
        'test.done'

rule pipe:
   output:
       'test.done'
   shell:
        """
        seq 1 10000 | head > test.done
        """

失败并出现以下错误:

snakemake -s test.snake

Provided cores: 1
Rules claiming more threads will be scaled down.
Job counts:
    count   jobs
    1   all
    1   pipe
    2

rule pipe:
    output: test.done
    jobid: 1

Error in job pipe while creating output file test.done.
RuleException:
CalledProcessError in line 9 of /Users/db291g/Tritume/test.snake:
Command '
        seq 1 10000 | head > test.done
        ' returned non-zero exit status 141.
  File "/Users/db291g/Tritume/test.snake", line 9, in __rule_pipe
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py", line 55, in run
Removing output files of failed job pipe since they might be corrupted:
test.done
Will exit after finishing currently running jobs.
Exiting because a job execution failed. Look above for error message

解释returned non-zero exit status 141似乎是说snakemake已经捕获到head发送的SIGPIPE失败。我想严格来说 snakemake 在捕获失败方面做的是正确的,但我想知道是否有可能忽略某些类型的错误,例如这种错误。我有一个使用 head 命令的 snakemake 脚本,我正试图找到解决此错误的方法。

一个笨拙的解决方案是将 || true 附加到脚本。这将使命令始终干净地退出,这是不可接受的。要检查脚本是否真的成功,您可以查询数组变量 ${PIPESTATUS[@]} 以确保它包含预期的退出代码:

这个脚本没问题:

seq 1 10000 | head | grep 1 > test.done || true
echo ${PIPESTATUS[@]}
141 0 0

这不行:

seq 1 10000 | head | FOOBAR > test.done || true
echo ${PIPESTATUS[@]}
0

是的,Snakemake 默认设置了 pipefail,因为在大多数情况下这是人们隐含的期望。您始终可以通过在 shell 命令前加上 set +o pipefail; 来为特定命令停用它。