I am trying to define and execute a snakemake workflow in pure python using the snakemake python package. There isn’t a specific use case. I am just trying to understand what is possible and what is not in snakemake. I imagine something like
from snakemake.rules import Rule
from snakemake.workflow import Workflow
# implement logic for defining rules
# add rules to workflow
# run workflow
I saw the first answer to [this question] (Is there a way to create a snakemake rule dynamically?). However it does not work any more in snakemake 8.18.2.
For example they create a Rule object by giving input and output files to snakemake Rule class’s init function. Now the init function does not take such arguments.
I am playing with the toy example below where I try to create the workflow defined in this snakefile.
# write into
rule all:
input:
'my_file.txt'
rule writer:
output:
out_pipe=pipe('my_pipe.txt')
shell:
"""
for i in {{1..10000}};
do echo "All work and no play makes Jack a dull boy.";
done > {output.out_pipe}
"""
# read from pipe
rule reader:
input:
in_pipe='my_pipe.txt'
output:
out_file='my_file.txt'
shell:
"""
cat {input.in_pipe} > {output.out_file}
"""
But I have problems immediately at defining the ‘all’ rule
import snakemake.io
import snakemake.rules
all_output_files = snakemake.io.OutputFiles('my_file.txt')
all = snakemake.rules.Rule('my_rule','workflow')
all.set_output(all_output_files)
Gives the error
Traceback (most recent call last):
File "/mnt/local_data/snakemake_demo/make_workflow.py", line 11, in <module>
all.set_output(all_output_files)
File "/mnt/local_data/micromamba/envs/snakemake/lib/python3.12/site-packages/snakemake/rules.py", line 325, in set_output
self._set_inoutput_item(item, output=True)
File "/mnt/local_data/micromamba/envs/snakemake/lib/python3.12/site-packages/snakemake/rules.py", line 519, in _set_inoutput_item
self._set_inoutput_item(i, output=output)
File "/mnt/local_data/micromamba/envs/snakemake/lib/python3.12/site-packages/snakemake/rules.py", line 431, in _set_inoutput_item
item = self.apply_path_modifier(item, path_modifier, property=property)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/mnt/local_data/micromamba/envs/snakemake/lib/python3.12/site-packages/snakemake/rules.py", line 357, in apply_path_modifier
assert path_modifier is not None```
I am wondering if anyone knows how to accomplish this now in snakemake 8.18?
Any help is greatly appreciated.
Harry