Search code examples
pythonworkflowpipelinesnakemake

Put optional input files for rule all in Snakemake


In my Snakemake project, I have a config.yaml file which allows users to run certain steps of the pipeline or not, for example:

DEG : 
   exec : True

And so, in the Snakefile, I include the rules associate with the DEG:

if config["DEG"]["exec"]:
   include: "rules/classic_mapping.smk"
   include: "rules/counts.smk"
   include: "rules/run_DESeq2.smk"

The problem is, now I would like to dynamically specify the output files in the "all" rule, so that Snakemake knows which files to generate based on the parameters entered by the user. For example I had in mind to proceed as follows:

rule all:   
   input:
       if config["DEG"]["exec"]:
          "DEG/DEG.txt"
       if config["DTU"]["exec"]:
          "DTU/DTU.txt" 

but it doesn't work: SyntaxError in line 58 of Unexpected keyword if in rule definition (Snakefile, line 58)

I would need an outside point of view to find an alternative because Snakemake should not work on this way

Thank's by advance


Solution

  • You can use snakemake's ability to take functions as input and put the if loop in a function. A sample implementation could be as follows

    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
    

    You can customize the get_input function to include additional conditions if required. This is documented further here.

    Another alternate way of doing this which is far less readable and not recommended but can work incase an addtional function is to be avoided is as follows

    rule all:
        input:
            lambda wildcards: "DEG/DEG.txt" if config["DEG"]["exec"] else [],
            lambda wildcards: "DTU/DTU.txt" if config["DTU"]["exec"] else [],