Reputation: 980
Suppose I have a code in python that generates a dictionary as the result. I need to write each element of dictionary in a separate folder which later will be used by other set of rules in snakemake. I have written the code as following but it does not work!
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}
"""
The error it return is :
InputFunctionException in line 14 of /$/test.snake:
KeyError: '1'
Wildcards:
simulation_index=1
It seems that problems is with the params section of the rule because deleting it will eliminates the error but I can not figure out what is wrong with the params!
Upvotes: 2
Views: 3358
Reputation: 8184
One can guess from the error message (KeyError: '1'
) that some query in a dictionary went wrong on a key that is '1'
, which happens to be a string.
However, the dictionary used in the indexer
"params" function has integers as keys.
Apparently, using strings instead of ints as keys to this simulation_index_dict
dictionary solves the problem (see comments below the question).
The cause of the problem is likely that the integer nature (inherited from simulation_index_dict.keys()
) of the value assigned to the simulation_index
parameter of the expand
in SimulateAll
is "forgotten" in subsequent steps of the workflow inference.
Indeed, the expand
results in a list of strings, which are then matched against the output of the other rules (which also consist in strings), to infer the values of the wildcards
attributes (which are also strings). Therefore, when the indexer
function is executed, wildcards.simulation_index
is a string, and this causes a KeyError when looking it up in simulation_index_dict
.
Upvotes: 1