将规则的可选输入文件全部放入 Snakemake 中

Vin*_*bot 2 python workflow pipeline snakemake

在我的 Snakemake 项目中,我有一个 config.yaml 文件,它允许用户运行或不运行管道的某些步骤,例如:

DEG : 
   exec : True
Run Code Online (Sandbox Code Playgroud)

因此,在 Snakefile 中,我包含了与 DEG 相关的规则:

if config["DEG"]["exec"]:
   include: "rules/classic_mapping.smk"
   include: "rules/counts.smk"
   include: "rules/run_DESeq2.smk"
Run Code Online (Sandbox Code Playgroud)

问题是,现在我想在“all”规则中动态指定输出文件,以便Snakemake知道根据用户输入的参数生成哪些文件。例如,我想按如下方式进行:

rule all:   
   input:
       if config["DEG"]["exec"]:
          "DEG/DEG.txt"
       if config["DTU"]["exec"]:
          "DTU/DTU.txt" 
Run Code Online (Sandbox Code Playgroud)

但它不起作用:如果在规则定义中,则 Unexpected 关键字的第 58 行出现 SyntaxError (Snakefile,第 58 行)

我需要外部观点来找到替代方案,因为 Snakemake 不应该以这种方式工作

提前致谢

pd3*_*321 5

您可以使用 Snakemake 的功能将函数作为输入并将 if 循环放入函数中。示例实现如下

def get_input(wildcards):
    input_list = []
    if config["DEG"]["exec"]:
          input_list.append("DEG/DEG.txt")
    if config["DTU"]["exec"]:
          input_list.append("DTU/DTU.txt")
    return input_list

rule all:
    input:
        get_input
Run Code Online (Sandbox Code Playgroud)

如果需要,您可以自定义该get_input函数以包含其他条件。此处对此进行了进一步记录。

另一种替代方法的可读性差得多,不推荐,但可以在要避免使用附加功能的情况下使用,如下所示

rule all:
    input:
        lambda wildcards: "DEG/DEG.txt" if config["DEG"]["exec"] else [],
        lambda wildcards: "DTU/DTU.txt" if config["DTU"]["exec"] else [],
Run Code Online (Sandbox Code Playgroud)