Search code examples
snakemake

How to pass a list or dictionary using Snakemake's command line config option


I want to pass a list of filenames to produce through my Snakemake workflow by using the --config CLI option. What's the syntax I need for that?


Solution

  • Just specify the list or dict in YAML (~Python) syntax on the command line:

    snakemake -c1 --config 'foo={"a":"a.txt", "b":"b.txt"}' 'bar=["file1.txt","file2.csv"]'
    

    You can then access this list in a sample Snakemake rule as follows:

    rule all:
        input:
            baz=config["foo"]["a"],
            qux=config["foo"]["b"],
            quux=config["bar"],
    

    As usual, the name before the = will be how you access the data after the = through the config variable. Multiple config items can be passed through space separation as shown above.

    Here is the full parsing order of the --config CLI option (as of November '21):

    --config key=value [key=value […]] sets top-level keys to the given value, which is parsed, in order, as either: int(value), float(value), literal True or False (a bool), a YAML-encoded value, or finally str(value). Only the last occurrence of the option is used if it is given more than once.
    https://github.com/tsibley/blab-standup/blob/master/2021-11-04.md#specifying-config

    The full post is a good reference of how Snakemake config handling works (the documentation is a bit lacking at times).