我正在尝试创建一个采用 TSV 表配置的 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)
关于python - Snakemake - 将附加参数传递给输入函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65718318/