【问题标题】:Discard part of filename in Snakemake: "Wildcards in input files cannot be determined from output files"在 Snakemake 中丢弃部分文件名:“输入文件中的通配符无法从输出文件中确定”
【发布时间】:2021-03-10 19:44:39
【问题描述】:

我遇到了 Snakemake 的WildcardError: Wildcards in input files cannot be determined from output files 问题。问题是我不想保留输入文件名的可变部分。例如,假设我有这些文件。

$ mkdir input
$ touch input/a-foo.txt
$ touch input/b-wsdfg.txt
$ touch input/c-3523.txt

我有一个像这样的 Snakemake 文件:

subjects = ['a', 'b', 'c']

result_pattern = "output/{kind}.txt"

rule all:
    input:
        expand(result_pattern, kind=subjects)

rule step1:
    input:
        "input/{kind}-{fluff}.txt"
    output:
        "output/{kind}.txt"
    shell:
        """
        cp {input} {output}
        """

我希望输出文件名只有我感兴趣的部分。我理解输入中的每个通配符都需要输出中对应的通配符的原理。那么我正在尝试做一种反模式吗?例如,我想可能有两个文件input/a-foo.txtinput/a-bar.txt,它们会相互覆盖。我应该在输入蛇形之前重命名我的输入文件吗?

【问题讨论】:

    标签: snakemake


    【解决方案1】:

    我希望输出文件名只包含我感兴趣的部分 [...]。我想可能有两个文件 input/a-foo.txt 和 input/a-bar.txt,它们会相互覆盖。

    在我看来,您需要决定如何解决此类冲突。如果输入文件是:

    input/a-bar.txt
    input/a-foo.txt    <- Note duplicate {a}
    input/b-wsdfg.txt
    input/c-3523.txt
    

    您希望如何命名输出文件以及根据什么标准?答案与snakemake 无关,但根据您的情况,您可以在 Snakefile 中包含 python 代码来自动处理此类冲突。

    基本上,一旦您做出此类决定,您就可以着手解决问题。


    但是假设没有文件名冲突,通配符系统似乎无法处理您想从文件名中删除一些变量绒毛的情况

    可变部分可以使用python的glob模式处理:

    import glob
    ...
    rule step1:
        input:
            glob.glob("input/{kind}-*.txt")
        output:
            "output/{kind}.txt"
        shell:
            """
            cp {input} {output}
            """
    

    您甚至可以更详细地使用专用函数来匹配给定{kind} 通配符的文件:

    def get_kind_files(wc):
        ff = glob.glob("input/%s-*.txt" % wc.kind)
        if len(ff) != 1:
            raise Exception('Exepected exactly 1 file for kind "%s"' % wc.kind)
        # Possibly more checks tha you got the right file
        return ff
    
    rule step1:
        input:
            get_kind_files,
        output:
            "output/{kind}.txt"
        shell:
            """
            cp {input} {output}
            """
    

    【讨论】:

    • 我认为这会比我预处理文件名更好,这样它们就没有我不想要的任何绒毛。但是假设没有文件名冲突,通配符系统似乎无法处理您想要从文件名中删除一些变量绒毛的情况(除非您有某种特殊的聚合步骤)。无论如何,在snakemake之外处理它似乎更容易。
    • @bernie 对于{kind} 通配符中没有重复项的情况,请参阅我的答案编辑。
    • 原来如此简单。我陷入了必须使用蛇形通配符的心态。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多