Snakemake中访问嵌套字典的值的正确方法是什么?

huangapple go评论50阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月4日 02:17:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定