Search code examples
pythonrsnakemake

Executing snakemake workflow without a Snakefile


I am using R and reticulate library to run some python code in a mainly R project. My goal is to somehow wrap a snakemake workflow in an R script so that the Snakefile uses the same environment as the rest of R code. If it was a regular script instead of a Snakefile, it would be possible to do so with reticulate, however I have not yet succeeded in converting a Snakefile to a python script. I am aware of the option to snakemake '--print-compilation' which compiles the Snakefile to a python script, but I have not been able to run it successfully even with a minimal example.

Has anyone had any luck with running snakemake purely from python environment?


Solution

  • In the unlikely case I understood your problem, a solution could be to pass between one snakemake rule and another an R session saved as an image. In this way different rules would share the same environment. For example:

    rule all:
        input:
            'stuff.txt',
            'plot.pdf',
    
    rule one:
        input:
            rdata= config['rdata'], # This may come from outside snakemake
        output:
            rdata= 'step2.Rdata',
        run:
            R(r"""
            load('{input.rdata}')
            x <- 1:10
            # More stuff in R...
            save.image('{output.rdata}')
            """)
    
    rule two:
        input:
            rdata= 'step2.Rdata',
        output:
            stuff= 'stuff.txt',
            plot= 'plot.pdf',
        shell:
            R(r"""
            load('{input.rdata}')
            # ...
            write.table('{output.stuff}')
            """)
    

    Execute as snakemake ... --config rdata=my_input.Rdata. For the R() function see scripting-with-r. Note that, as far as I can tell, snakemake is designed to be run as a command line program rather than as a library inside another script.

    (If you post a brief example of your problem you may get better answers)