You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Snakemake规则中如何在params段引用输入文件?

Snakemake中将输入文件列表转为逗号分隔字符串的解决方案

问题场景

需要将Snakemake规则的输入文件列表转换为逗号分隔的字符串,传递给CLI程序,但在params段引用输入文件时出现错误。

尝试的代码及报错

尝试的规则代码:

rule a:
    input:
        foo="a.txt",
        bar=expand("{build}.txt", build=config["build"]),
    output:
        baz="result.txt",
    params:
        joined_bar=lambda w: ",".join(input.bar),  # 此处出错
    shell:
        """
        qux --comma-separated-files {params.joined_bar} \
            --foo {input.foo} \
            >{output.baz}
        """

运行报错:

InputFunctionException:
   AttributeError: 'builtin_function_or_method' object has no attribute 'bar'

错误原因

lambda函数中直接使用input.bar是错误的:这里的input是Python内置函数,并非当前规则的输入对象。lambda的参数w代表wildcards对象,必须通过w.input.bar才能访问当前规则的输入文件列表。

解决方案

方法一:通过lambda函数正确引用输入列表

修改params中的lambda函数,通过wildcards对象访问输入文件列表:

rule a:
    input:
        foo="a.txt",
        bar=expand("{build}.txt", build=config["build"]),
    output:
        baz="result.txt",
    params:
        joined_bar=lambda w: ",".join(w.input.bar),
    shell:
        """
        qux --comma-separated-files {params.joined_bar} \
            --foo {input.foo} \
            >{output.baz}
        """

方法二:直接在shell段处理(更简洁)

如果仅需在shell命令中使用该字符串,可直接在shell模板中用Snakemake的字符串处理语法,省略params定义:

rule a:
    input:
        foo="a.txt",
        bar=expand("{build}.txt", build=config["build"]),
    output:
        baz="result.txt",
    shell:
        """
        qux --comma-separated-files {",".join(input.bar)} \
            --foo {input.foo} \
            >{output.baz}
        """

适用场景

  • 方法一适合需要在规则的多个部分复用该逗号分隔字符串的场景;
  • 方法二更简洁,适合仅在shell命令中使用的场景。

内容的提问来源于stack exchange,提问作者Cornelius Roemer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 18:50:44