首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用KeyError的InputFunctionException

使用KeyError的InputFunctionException
EN

Stack Overflow用户
提问于 2018-02-10 00:08:18
回答 1查看 1.8K关注 0票数 1

假设我有一段python代码,它生成一个字典作为结果。我需要在一个单独的文件夹中编写字典的每个元素,稍后将由snakemake中的其他规则集使用。我写的代码如下,但它不能工作!

代码语言:javascript
复制
simulation_index_dict={1:'test1',2:'test2'}


def indexer(wildcards):
    return(simulation_index_dict[wildcards.simulation_index])

rule SimulateAll:
       input:
        expand("{simulation_index}/ProteinCodingGene/alfsim.drw",simulation_index=simulation_index_dict.keys())



rule simulate_phylogeny:
    output:
        ProteinCodingGeneParams=expand("{{simulation_index}}/ProteinCodingGene/alfsim.drw"),
        IntergenicRegionParams=expand("{{simulation_index}}/IntergenicRegions/dawg_IR.dawg"),
        RNAGeneParams=expand("{{simulation_index}}/IntergenicRegions/dawg_RG.dawg"),
        RepeatRegionParams=expand("{{simulation_index}}/IntergenicRegions/dawg_RR.dawg"),
    params:
        value= indexer,
    shell:
        """
        echo {params.value} > {output.ProteinCodingGeneParams}
        echo {params.value} > {output.IntergenicRegionParams}
        echo {params.value} > {output.RNAGeneParams}
        echo {params.value} > {output.RepeatRegionParams}
        """

它返回的错误是:

代码语言:javascript
复制
InputFunctionException in line 14 of /$/test.snake:
KeyError: '1'
Wildcards:
simulation_index=1

看起来问题出在规则的params部分,因为删除它会消除错误,但我不知道params有什么问题!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-13 17:41:35

解决方案:使用字符串作为字典键

可以从错误消息(KeyError: '1')中猜测,字典中的某个查询在'1'键上出错,而这个键恰好是一个字符串。

但是,indexer "params“函数中使用的字典将整数作为键。

显然,使用字符串而不是int作为这个simulation_index_dict字典的键可以解决这个问题(参见问题下面的注释)。

原因:工作流推理过程中类型信息丢失

问题的原因很可能是分配给SimulateAllexpandsimulation_index参数的值的整数性质(从simulation_index_dict.keys()继承)在工作流推理的后续步骤中被“遗忘”。

实际上,expand会生成一个字符串列表,然后将这些字符串与其他规则(也包含在字符串中)的输出进行匹配,以推断wildcards属性(也是字符串)的值。因此,当执行indexer函数时,wildcards.simulation_index是一个字符串,这会导致在simulation_index_dict中查找它时出现KeyError。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48709798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档