英文:
What is the proper way to access values in nested dictionaries with Snakemake?
问题
以下是翻译好的部分:
我经常需要在我的分析中处理多个实体。我在我的 `config.yml` 文件中以以下方式描述它们:
entities:
fbr:
full_name: "FUBAR"
filepath: "data/junk_1.txt"
snf:
full_name: "SNAFU"
filepath: "data/junk_2.txt"
传递文件路径作为规则输入的惯用方法是什么?我知道可以使用列表推导来做到这一点,以下是一个示例 `Snakefile`:
configfile: "config.yml";
rule all:
input:
"data/result.txt"
rule first:
input:
expand("{entity}", entity=[x["filepath"] for x in config["entities"].values()])
output:
"data/result.txt"
shell:
"cat {input} > {output}"
这种方法可以工作,但感觉有点繁琐和丑陋。在Snakemake中是否有更好的方法来展开嵌套字典?
英文:
I often want to process several entities in my analysis. I describe them the following way in my config.yml
file:
entities:
fbr:
full_name: "FUBAR"
filepath: "data/junk_1.txt"
snf:
full_name: "SNAFU"
filepath: "data/junk_2.txt"
What is the idiomatic way to pass file paths as an input to a rule? I know that I can do it with a list comprehension, here is a toy Snakefile
:
configfile: "config.yml"
rule all:
input:
"data/result.txt"
rule first:
input:
expand("{entity}", entity=[x["filepath"] for x in config["entities"].values()])
output:
"data/result.txt"
shell:
"cat {input} > {output}"
This approach works but feels cumbersome and ugly. Is there a better way to perform expansion with nested dictionaries in Snakemake?
答案1
得分: 1
expand(...)
是多余的,可以省略:
configfile: "config.yml";
rule all:
input:
"data/result.txt";
rule first:
input:
[x["filepath"] for x in config["entities"].values()]
output:
"data/result.txt";
shell:
"cat {input} > {output}";
鉴于您的 config.yml
具有嵌套字典结构,替代列表理解没有更好的替代方案。
英文:
The expand(...)
is superfluous and can be left out:
configfile: "config.yml"
rule all:
input:
"data/result.txt"
rule first:
input:
[x["filepath"] for x in config["entities"].values()]
output:
"data/result.txt"
shell:
"cat {input} > {output}"
Given the nested dictionary structure of your config.yml
, there are not a lot of better alternatives to substitute the list comprehension.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论