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

将字典参数传递给Snakemake时报MissingInputException缺失输入文件错误

错误原因

Snakemake规则的input块中声明的所有值都会被识别为输入文件路径,Snakemake构建DAG时会自动校验这些文件是否存在,不存在就会抛出MissingInputException。
你的代码中,info_dict_from_sample函数返回的字典同时包含了输入文件路径filepath和HLA分型字符串hla,两个值都被unpack到了input块,Snakemake将HLA分型字符串HLA-A02:01误判为输入文件路径,找不到对应文件就触发了报错。

解决方法

将非文件类型的参数从input块移到params块中,params块的内容会被当作普通参数处理,不会校验文件是否存在,修改后的参考代码如下:

import pandas as pd

samples = pd.read_csv("acc_filepath_df.csv").set_index("sample", drop=False)
sids = samples['sample']

# 单独获取输入文件路径的函数
def get_input_file(wildcards):
    return samples.loc[wildcards.sample, "filepath"]

# 单独获取HLA参数的函数
def get_hla_param(wildcards):
    return samples.loc[wildcards.sample, "HLA"]

expected_filenames = "mhctyped/{sample}.txt.gz"

rule all:
    input:
        expand(expected_filenames,
            zip,
            sample=sids)

rule test_run:
    input:
        get_input_file
    output:
        "mhctyped/{sample}.txt.gz"
    params:
        hla = get_hla_param
    shell:
        "cat {input} {params.hla} > {output}"

如果需要保留返回字典的写法,也可以调整为:

import pandas as pd

samples = pd.read_csv("acc_filepath_df.csv").set_index("sample", drop=False)
sids = samples['sample']

def info_dict_from_sample(wildcards):
    return {
        "filepath": samples.loc[wildcards.sample, "filepath"], 
        "hla": samples.loc[wildcards.sample, "HLA"]
    }

expected_filenames = "mhctyped/{sample}.txt.gz"

rule all:
    input:
        expand(expected_filenames,
            zip,
            sample=sids)

rule test_run:
    input:
        filepath = lambda wildcards: info_dict_from_sample(wildcards)["filepath"]
    output:
        "mhctyped/{sample}.txt.gz"
    params:
        hla = lambda wildcards: info_dict_from_sample(wildcards)["hla"]
    shell:
        "cat {input.filepath} {params.hla} > {output}"

内容的提问来源于stack exchange,提问作者Ravin Poudel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 12:57:02