Search code examples
pythonsnakemake

Accessing snakemake variables in subscript


I have a Snakemake workflow where a function from one python file is called in another python file, similar to the following:

### Snakefile
rule test:
    input:
        "input.file"
    output:
        "output.file"
    script:
        "test_script.py"

### script.py
from test_subscript import run_test

if __name__ == "__main__":
    input_file = snakemake.input[0]
    run_test()

### subscript.py
def run_test():
    out_file = snakemake.output[0]
    with open(out_file, "w") as f:
        f.write("Test")

Now, when I execute the workflow, the snakemake.input variable can be accessed from within script.py, but I cannot access the snakemake.output variable in subscript.py. A workaround would be to pass all required snakemake variables to the run_test() function, but that quickly gets complicated.

Is there a more convenient way to do it?


Solution

  • The convenient way to do that is to make the scripts agnostic of Snakemake. These scripts should be called from any shell. In this case you need to provide the filenames as command line parameters:

    ### Snakefile
    rule test:
        input:
            "input.file"
        output:
            "output.file"
        shell:
            "test_script.py {input} {output}"