Snakemake - 将附加参数传递给输入函数

Snakemake - passing additional parameters to an input function

我正在尝试创建一个采用 TSV table 配置的 Snakemake 工作流,它看起来像这样:

sample    path
s1    /path/to/s1_dir
s2    /path/to/s2_dir

对于每个示例,我提供了一个目录,我从中使用了工作流程中的各种路径。
我希望能够通过单个输入函数获得各种输入。我试过这个:

import pandas as pd

samples = pd.read_table('samples.tsv').set_index("sample", drop=False)

rule all:
    '...'

def get(wildcards, what):
    sample_dir = samples.loc[wildcards.sample, 'path']
    if what == 1:
        return sample_dir + '/sub/' + 'someInput'
    elif what == 2:
        return sample_dir + '/sub2/' + 'otherInput'

rule rule1:
    input:
        get(what=1)
    ...

rule rule2:
    input:
        get(what=2)
    ...

但是,这会导致出现错误消息,并且根据文档,输入函数只能采用单个参数(通配符)。我想一种解决方法是使用多个输入函数:

def get1(wildcards):
    sample_dir = samples.loc[wildcards.sample, 'path']
    return sample_dir + '/sub/' + 'someInput'

def get2(wildcards):
    sample_dir = samples.loc[wildcards.sample, 'path']
    return sample_dir + '/sub2/' + 'otherInput'

但是如果我有 10 个不同的输入怎么办?知道怎么做吗?
谢谢!

这就是我会做的。将您的自定义函数 get 与 lambda 函数组合:

def get(wildcards, what):
    # Do stuff with wildcards and what
    ...

rule one:
    input:
        lambda wc: get(wc, what= 1)

rule two:
    input:
        lambda wc: get(wc, what= 2)