如何根据第一个规则选择第二个通配符?

问题描述

我有两个通配符,第一个通配符,第二个通配符基本上是特定物种的每个文件的计数器。例如,我有Cow_A,Cow_B和Pig_A,Pig_B,Pig_C(文件数量因物种而异)

现在我想要的是在输入中使用类似这样的选择一个物种的所有文件

input: expand("{{species}}_{rep}",rep=count(species))
output: "{species}_combined"

如何告诉输入函数计数使用当前种类的通配符?

解决方法

您必须使用一个函数,即输入函数:

def inputfunction(wildcards):
    expand("{species}_{rep}",species=wildcards.species,rep=count(species))

input: inputfunction
output: "{species}_combined"

或者您可以使用lambda函数:

input: lambda wildcards: expand("{species}_{rep}",rep=count(species))
output: "{species}_combined"
,

您可以在输入部分中使用一个函数(或lambda):

input:
    lambda wildcards: expand("{species}_{rep}",species=[wildcards.species],rep=get_reps(wildcards.species))
output:
    "{species}_combined"

您需要定义一个函数,该函数返回物种的代表。可以从配置中读取,也可以使用glob_wildcards函数:

def get_reps(species):
    return glob_wildcards(f"{species}_{{rep}}").rep