Snakemake 有没有办法像 GNU Make 中的 `eval` 那样评估动态 Snakefile 结构?

sap*_*pjw 7 python bash eval gnu-make snakemake

我希望在我的 Snakemake 工作流程中拥有各种动态“快捷方式”(规则名称),而不需要标记文件。我想到的方法与evalGNU Make 中的方法类似,但 Snakemake 似乎无法评估 Snakefile 语法中的变量扩展代码。有办法做到这一点吗?

这是一个简化的示例 Snakefile。我想要一个与每个输出“阶段”相对应的规则名称,现在我必须手动定义它们。想象一下,如果我有更多“阶段”和“步骤”,并且希望有一个规则,如果我添加这些阶段,可以生成所有“b”、“d”或“z”文件。动态定义规则名称比定义每次添加新阶段时更新的每个组合要简洁得多。

stages = ['a', 'b']
steps = [1, 2]

rule all:
    input:
        expand('{stage}{step}_file', stage=stages, step=steps)

rule:
    output:
        touch('{stage}{step}_file')

# Can these two be combined so that I don't have to add more
# rules for each new "stage" above while retaining the shorthand
# rule name corresponding to the stage?
rule a:
    input: expand('a{step}_file', step=steps)

rule b:
    input: expand('b{step}_file', step=steps)
Run Code Online (Sandbox Code Playgroud)

Sul*_*yev 8

由于Snakefile是一个 Python 文件,以下内容可能有助于实现您的目标:

for stage in stages:
    rule:
        name: f'{stage}'
        input: expand(f'{stage}{{step}}_file', step=steps)
Run Code Online (Sandbox Code Playgroud)

  • `name` 指令于 2020 年末在 [版本 5.31.0](https://snakemake.readthedocs.io/en/stable/project_info/history.html?#id165) 中添加。我没有看到任何其他关于它的文档——很棒的提示。 (4认同)