如何在 Snakemake 中进行局部展开?

How to do a partial expand in Snakemake?

我试图首先为 LETTERS x NUMS 组合生成 4 个文件,然后对 NUMS 进行汇总以获得 LETTERS 中每个元素一个文件:

LETTERS = ["A", "B"]
NUMS = ["1", "2"]


rule all:
    input:
        expand("combined_{letter}.txt", letter=LETTERS)

rule generate_text:
    output:
        "text_{letter}_{num}.txt"
    shell:
        """
        echo "test" > {output}
        """

rule combine text:
    input:
        expand("text_{letter}_{num}.txt", num=NUMS)
    output:
        "combined_{letter}.txt"
    shell:
        """
        cat {input} > {output}
        """

执行此 snakefile 会导致以下错误:

WildcardError in line 19 of /tmp/Snakefile:
No values given for wildcard 'letter'.
  File "/tmp/Snakefile", line 19, in <module>

看来部分expand是不行的。是 expand 的限制吗?如果是这样,我应该如何规避它?

更新(25/11/2020):根据 ,由于 expandallow_missing 参数,现在可以在没有多括号的情况下进行部分展开。


这似乎不是 expand 的限制,而是我对 python 中字符串格式化工作方式的熟悉程度的限制。我需要为非扩展通配符使用双括号:

LETTERS = ["A", "B"]
NUMS = ["1", "2"]


rule all:
    input:
        expand("combined_{letter}.txt", letter=LETTERS)

rule generate_text:
    output:
        "text_{letter}_{num}.txt"
    shell:
        """
        echo "test" > {output}
        """

rule combine text:
    input:
        expand("text_{{letter}}_{num}.txt", num=NUMS)
    output:
        "combined_{letter}.txt"
    shell:
        """
        cat {input} > {output}
        """

现在执行此 snakefile 会生成预期的以下文件:

text_A_2.txt
text_A_1.txt
text_B_2.txt
text_B_1.txt
combined_A.txt
combined_B.txt

的确,在expand中想要忽略大括号时需要转义。它依赖于 str.format,因此来自 format 的任何规则也适用于 expand

可以使用 allow_missing=True 进行部分扩展。

例如:

expand("text_{letter}_{num}.txt", num=[1, 2], allow_missing=True)

> ["text_{letter}_1.txt", "text_{letter}_2.txt"]