pythonconfigsnakemake

Snakemake - Check if configfile has a value


In snakemake, is there any way to check if some configfile was already given?

I have a script where some parameters can be passed through the --config flag, so config is not empty, but I want to check specifically if the user passed a file to the --configfile flag (or at least check if a configfile is given at some point in the code).

The idea is that if no configfile was specified, a default configfile will be used.


Solution

  • There is already a configfile: directive which allows you to provide a default config file to use if none is specified on the command line.

    Is this what you need? If not, can you be a bit more specific? It's possible to inspect the command line arguments to see if --configfile was passed, but this is a bad idea. In multi-threaded mode Snakemake will run any Python code within the body of the Snakefile multiple times, and if the code can ever do a different thing each time (eg. load a different config file) then you can have very whacky and unexpected results.

    If you do use the configfile: directive and want to determine whether the default file or a custom file was used, maybe add a "_default: True" value to the default config, and then detect if that value has been set.


    Edited as it seems you can actually get this info neatly:

    print(workflow.config_settings.configfiles)
    

    And you can also put any configfile: directive within an if clause so:

    if not workflow.config_settings.configfiles:
        configfile: "default_config.yaml"
    

    I think this does answer the original question.