输出字符串中通配符的字符串格式

Wil*_*ell 6 snakemake

我对snakemake相对较新,并且在将分散-收集 DeepVariant 工作流程改编成snakemake规则时遇到了一些麻烦。

在最初的 Snakefile 中,我想将第一步分散到集群中。DeepVariant 使用一种格式来跟踪中间文件格式中的分片编号,因此我需要使用字符串格式来提供、和字段*.00001-of-00256.*中的分片编号和分片总数,并且我将分片编号作为通配符提供规则的。规则字段中的函数正确生成了预期的文件名,但无法找到该步骤将生成的输入文件路径。inputoutputshellparamsscatterexpand()inputgatherscatter

我在下面生成了一个最小的可重现示例,以及运行此示例的输出(稍微编辑以删除一些路径信息)。

N_SHARDS = 8

rule all:
    input: "done.txt"


rule scatter:
    input: "start.txt"
    output: f"test_{{shard:05}}-of-{N_SHARDS:05}.txt"
    params:
        shard = range(N_SHARDS)
    message: "scattering"
    shell:
        f"echo {{wildcards.shard}} {N_SHARDS} > {{output}}"


rule gather:
    input: expand(f"test_{{shard:05}}-of-{N_SHARDS:05}.txt", shard=range(N_SHARDS))
    output: touch("done.txt")
    shell: "echo gathering"
Run Code Online (Sandbox Code Playgroud)
$ touch start.txt
$ snakemake -s example.smk -j 1
Building DAG of jobs...
MissingInputException in line 17 of /redacted/example.smk:
Missing input files for rule gather:
test_00002-of-00008.txt
test_00000-of-00008.txt
test_00006-of-00008.txt
test_00001-of-00008.txt
test_00004-of-00008.txt
test_00005-of-00008.txt
test_00007-of-00008.txt
test_00003-of-00008.txt
Run Code Online (Sandbox Code Playgroud)

我已经为其他不需要通配符字符串格式的分散收集概念构建了非常相似的规则,因此这是我能想到的在这种情况下唯一不同的事情。我将不胜感激任何见解!

更新:一位乐于助人的 Twitter 用户指出,我可以删除:05in scatter->output并且该规则有效。这太棒了,它恰好解决了我原来的问题,但这只是因为 DeepVariant 可以容忍命令行传递的分片参数的零填充。是否有解决方案允许我将格式应用于通配符?

dar*_*ber 4

我就是这样做的:

N_SHARDS = '00008'

shard = ['%05d' % x for x in range(int(N_SHARDS))]

wildcard_constraints:
    shard= '|'.join([re.escape(x) for x in shard])

rule all:
    input: 
        "done.txt",

rule scatter:
    input: 
        "start.txt",
    output:
        "test_{shard}-of-%s.txt" % N_SHARDS,
    shell:
        r"""
        echo {wildcards.shard} %s > {output}"
        """ % N_SHARDS
    
rule gather:
    input:
        expand('test_{shard}-of-%s.txt' % N_SHARDS, shard= shard),
    output: 
        touch("done.txt")
    shell: 
        "echo gathering"
Run Code Online (Sandbox Code Playgroud)

wildcard_constraints位可能是多余的,但如果我确切地知道通配符将采用什么值,我倾向于相当自由地使用它。

有一件事:您似乎事先知道 DeepVariant 将生成多少个分片(N_SHARDS = 8在示例中)。事实真的是这样吗?如果没有,我认为你需要求助于 Snakemake 的检查点功能。