Skip to content
Advertisement

snakemake – accessing config variables from cluster submission wrapper

I am using a cluster submission wrapper script with snakemake --cluster "python qsub_script.py". I need to pass a global variable, taken from the config['someVar']. This should be applied to all rules. I could add it to the params of each rule, and then access it using job_properties['params']['someVar'], but this is probably not the best solution. Is there a way to access config from the submission wrapper? Simply using config['someVar'] gives me a NameError.

If that’s not possible, can you suggest an alternative? I suspect using profiles could be helpful, but couldn’t figure out how this interacts with the submission wrapper.

Advertisement

Answer

The problem statement is a bit broad, so the solution below might not be the most optimal, but it should achieve what you are looking for. Specifically, the code below allows modifying every rule in the workflow.

Here’s a reproducible demo:

rule all:
    input:
        "a.txt",
        "b.txt",


rule a:
    output:
        "a.txt",
    shell:
        """
        echo {params.val} > {output}
        """


rule b:
    output:
        "b.txt",
    shell:
        """
        echo {params.val} > {output}
        """

# this allows iteration across every rule using python syntax
# so complex business logic can be implemented
for n, r in enumerate(workflow.rules):
    r.set_params(val=n)

For your specific use-case, the last chunk of code could look like this:

for r in workflow.rules:
    r.set_params(someVar=config["someVar"])

Where the config dictionary is defined within the workflow using configfile:

configfile: "config.yaml"
Advertisement