如何解决蛇形 - 不删除失败规则的输出
我有一个蛇形工作流,其中包含一个运行另一个“内部”蛇形工作流的规则。
有时内部工作流的某个规则失败,这意味着内部工作流失败。因此,内部工作流的 output
下列出的所有文件都会被外部工作流删除,即使创建它们的内部工作流的规则已成功完成。
有没有办法防止snakemake删除失败规则的输出?或者您可以建议另一种解决方法?
一些注意事项:
- 必须列出内部工作流的输出,b/c 它们用作外部工作流中其他规则的输入。
- 我尝试将内部工作流的输出设置为
protected
,但这没有帮助。 - 我还尝试将
exit 0
添加到对内部工作流的调用的末尾,以使 snakemake 认为它已成功完成,
像这样:
rule run_inner:
input:
inputs...
output:
outputs...
shell:
"""
snakemake -s inner.snakefile
exit 0
"""
但输出仍被删除。
将不胜感激任何帮助。谢谢!
解决方法
一种选择可能是让 run_inner
生成一个标记规则完成的虚拟输出文件。 run_inner
之后的规则将接收虚拟文件的输入。例如:
rule run_inner:
...
output:
# or just 'run_inner.done' if wildcards are not involved
touch('{sample}.run_inner.done'),shell:
'snakemake -s inner.snakefile'
run next:
input:
'{sample}.run_inner.done',params:
real_input= '{sample}.data.txt',# This is what run_inner actually produces
shell:
'do stuff {params.real_input}'
如果 snakemake -s inner.snakefile
失败,虚拟输出将被删除,但 snakemake -s inner.snakefile
将从它离开的地方重新开始。
另一种选择是将 inner.snakefile
中的规则集成到您的外部管道中,例如使用include 语句。我觉得这个选项更可取,但当然,实施起来会更复杂。
一种解决方法是使用 run
而不是 shell
:
rule run_inner:
input:
inputs...
output:
outputs...
run:
shell("""snakemake -s inner.snakefile""")
# Add your code here to store the files before removing
即使 shell
函数调用中的脚本失败,文件仍然存在,直到 run
部分中的脚本完成。您可以将文件复制到那里的安全位置。
更新:您需要处理异常以在脚本返回错误时继续执行。下面的脚本说明了这个想法:print
块中的 except:
函数打印 True
,onerror
中的另一个打印 False
rule run_inner:
output:
"output.txt"
run:
try:
shell("""touch output.txt; exit 1""")
except:
print(os.path.exists("output.txt"))
onerror:
print(os.path.exists("output.txt"))
,
当返回非零返回值时,程序“失败”。因此,我们只需要“修复”这个问题来欺骗内壳,认为所有程序都已成功完成。最简单的方法是使用 some error command || true
。下面是一个最小的例子:
rule test:
output:
"test.output",shell:
"""
touch test.output
# below cat will trigger error
cat file_not_exist || true
"""
您会发现,尽管 cat
抛出了错误,test.output
仍然存在。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。