【问题标题】:Snakemake: wildcards for parameter keysSnakemake:参数键的通配符
【发布时间】:2021-03-19 11:58:03
【问题描述】:

我正在尝试创建一个蛇形规则,其输入和输出是通配符指定的配置参数,但有问题。

我想做这样的事情:

config.yaml

cam1:
  raw: "src/in1.avi"
  bg: "out/bg1.png"
cam2:
  raw: "src/in2.avi"
  bg: "out/bg2.png"
cam3:
  raw: "src/in3.avi"
  bg: "out/bg3.png"

蛇文件:

configfile: "config.yml"

...
rule all:
  input:
    [config[f'cam{id}']['background'] for id in [1, 2, 3]]

rule make_bg:
  input:
    raw=config["{cam}"]["raw"]
  output:
    bg=config["{cam}"]["bg"]
  shell:
    """
    ./process.py {input.raw} {output.bg}
    """

但这似乎不起作用 - 我希望将 {cam} 视为通配符,而不是得到 {cam} 的 KeyError。有人可以帮忙吗?

是否可以将{cam} 指定为通配符(或其他内容),然后可以将其用作配置键?

【问题讨论】:

    标签: snakemake


    【解决方案1】:

    我认为这种方法存在一些问题:

    概念上

    config 中指定确切的inputoutput 文件名没有多大意义,因为这与使用snakemake 的原因截然相反:从输入推断出需要运行管道以创建所需的输出。在这种情况下,您总是必须首先编辑每个输入/输出对的配置,而整个自动化点就丢失了。

    现在,实际问题是从config 访问配置变量以获取inputoutput。通常,你会例如在配置中提供一些路径并使用类似的东西:

    config.yaml:

    raw_input = 'src'
    bg_output = 'out'
    

    在管道中,您可以像这样使用它:

    input: os.path.join(config['raw_input'], in{id}.avi)
    output: os.path.join(config['bg_output'], bg{id}.avi)
    

    就像我说的,在配置文件中特别指定输出是没有意义的。

    如果您要在 config.yaml 中指定输入:

    cam1:
      raw: "src/in1.avi"
    cam2:
      raw: "src/in2.avi"
    cam3:
      raw: "src/in3.avi"
    
    

    然后您可以使用以下函数获取输入:

    configfile: "config.yaml"
    
    # create sample data
    os.makedirs('src', exist_ok= True)
    for i in [1,2,3]:
        Path(f'src/in{i}.avi').touch()
    
    ids = [1,2,3]
    
    def get_raw(wildcards):
        id = 'cam' + wildcards.id
        raw = config[f'{id}']['raw']
        return raw
    
    rule all:
      input: expand('out/bg{id}.png', id = ids)
    
    rule make_bg:
        input:
            raw = get_raw
        output:
            bg='out/bg{id}.png'
        shell:
            " touch {input.raw} ;"
            " cp {input.raw} {output.bg};"
    

    【讨论】:

    • 感谢您的反馈。我还在学习snakemake,所以这对我很有用。
    猜你喜欢
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2022-11-01
    • 2023-03-20
    相关资源
    最近更新 更多