繁体   English   中英

如何访问snakemake python 代码中的重试尝试?

[英]How to access retry attempts in snakemake python code?

当您使用 --restart-times >= 1 执行蛇形脚本时,它将尝试重新执行失败的运行。 重新执行后,可以通过“资源”中的 lambda function 访问执行尝试次数。 但是,我想在我的规则之外访问 python 代码块中的尝试次数。 我试图将尝试变量从资源块传递给我的 python function,但无济于事。 我的蛇形版本是 5.32.1,使用 6.0.3 进行的快速测试看起来非常相似。

def getTargetFiles(files, attempted):
    do stuff
    return modified-target-files

rule do_things_rule:
    input: 
        ...
    output:
        getTargetFiles("file/path.txt", resources.attempt)
    resources:
        attempt=lambda wildcards, attempt: attempt,

不幸的是,这会产生错误。 “xxxx.py 第 172 行中的 NameError:未定义名称‘资源’”

我最接近的是访问“workflow.attempt”,但这似乎总是设置为 1。也许这是尝试的默认值?

rule do_things_rule:
    input: 
        ...
    output:
        getTargetFiles("file/path.txt", workflow.attempt)

我正在查看snakemake的内部结构,希望能找到解决方案。 不幸的是,我的 python 知识无法胜任这项任务。 可以访问一些变量来代替 workflow.attempt,它们没有 integer 值。 不确定是否有一种方法可以通过稍微不同的方式获得当前的尝试次数:

print snakemake.jobs.Job.attempt
<property object at 0x7f4eecba66d0>

print snakemake.jobs.Job._attempt
<member '_attempt' of 'Job' objects>

这是一个最小的工作示例,我可以用它来重现您的错误。

def getTargetFiles(files, attempted):
  return f"{files[:-4]}-{attempted}.txt"

rule do_things_rule:
  resources:
    nr = lambda wildcards, attempt: attempt
  output:
    getTargetFiles("test.txt", resources.nr)
  shell:
    'echo "Failing on purpose to produce file'
    '{output} at attempt {resources.nr}'
    '"; exit 1 '

确实, output不知道resources 我认为这是因为需要在规则运行之前访问它(见下文)。 相反,如果您将getTargetFiles("test.txt", resources.nr)替换为getTargetFiles("test.txt", 1) ,则规则运行正确的次数并且 shell 命令可以访问resources.nr

据我了解,这个问题是有根本原因的。

snakemake 工作流程“根据定义如何从输入文件创建 output 文件的规则进行定义。规则之间的依赖关系是自动确定的”。 (引自教程)这意味着snakemake需要知道该规则将创建哪个output文件。 然后,它将确定是否需要运行该规则。 因此,尝试次数至少通常不应该是 output 文件名的一部分。

也许您想组合失败尝试的不同文件? 但是,如果规则失败,则不会有 output 文件。 即使你强迫它。 该文件将被snakemake 删除。 (见下面的例子)

def getTargetFiles(files, attempted):
  return f"{files[:-4]}-{attempted}.txt"

rule combine:
  input:
    'test-1.txt'
  output:
    'test-combined.txt'
  shell:
    'cat test-[0-9]*.txt > test-combined.txt'

rule do_things_rule:
  resources:
    nr = lambda wildcards, attempt: attempt
  output:
    getTargetFiles("test.txt", 1)
  shell:
    'touch {output}; '
    'echo "Failing on purpose to produce file'
    '{output} at attempt {resources.nr}'
    '"; exit 1 '

在 shell 命令中使用resources.nr代替文件名中的尝试次数怎么样?

希望这能为您的问题提供解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM