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

Snakemake中从JSON读取的路径无法通过expand展开如何解决

问题原因

这是expand函数的语法使用错误,与JSON配置文件的读取逻辑无关。
你将config["data_input"]写在了expand的引号包裹范围中,Python会将这部分识别为普通字符串字面量,不会解析为config字典对应的路径取值,最终生成的输入路径为字面量config["data_input"]/data_1234.tab,和你实际存在的文件路径不匹配,因此触发缺失输入文件的报错。

修复方案

有两种常用的正确写法可选:

写法1:将配置路径作为expand的替换变量传入(更推荐,无转义成本)

rule them_all:
    input:
        expand("{data_input}/data_{wc}.tab", data_input=config["data_input"], wc = wildcard)
    output:
        expand("{data_output}/output_{wc}.rda", data_output=config["data_output"], wc = wildcard)
    shell:
        "Rscript ./my_script.R"

写法2:用f-string提前拼接路径前缀,注意占位符需要双层大括号转义

rule them_all:
    input:
        expand(f"{config['data_input']}/data_{{wc}}.tab", wc = wildcard)
    output:
        expand(f"{config['data_output']}/output_{{wc}}.rda", wc = wildcard)
    shell:
        "Rscript ./my_script.R"

修改后重新执行snakemake -np即可正常解析到正确的文件路径,不会再触发输入缺失报错。

内容的提问来源于stack exchange,提问作者Giulio Centorame

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 22:36:04