snakemake #15

Merged
yarikoptic merged 7 commits from wjm/datalad-blog:snakemake into main 2026-04-19 12:37:28 +00:00
Contributor
  • explain the choice for snakemake
  • get to the bottom of the timestamps
  • update the conclusion
  • restructure the sections
  • add mermaid support & replace the diagram in the worktree piece with raw code
- explain the choice for snakemake - get to the bottom of the timestamps - update the conclusion - restructure the sections - add mermaid support & replace the diagram in the worktree piece with raw code
Load mermaid.js (v11) from jsdelivr CDN in the footer partial.
A script finds fenced ```mermaid code blocks — whether plain
<pre><code class="language-mermaid"> or wrapped in Hugo/Chroma's
<div class="highlight"> — replaces them with <div class="mermaid">,
and calls mermaid.initialize() to render them as SVG diagrams.

No Hugo config changes required; any post can now use standard
```mermaid fenced blocks and they will render as interactive diagrams.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

contain mermaid rendering
add new post on automated workflow using snakemake
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 16s
4987369061
@ -35,0 +47,4 @@
P1 --> A2
A2 --> DE2[Dead End]
DE2 -.-> D
```
Member

oh, cool, forgot that there was a diagram there!

oh, cool, forgot that there was a diagram there!
Author
Contributor

my beautiful scientific maze :D

my beautiful scientific maze :D
wjm marked this conversation as resolved
@ -0,0 +48,4 @@
of C compilers. Like Make, it builds a directed acyclic graph (DAG) of rules, figures out what's
missing or outdated, and only runs what it needs to. Unlike Make, it's pure Python —
`pip install snakemake`, write a `Snakefile`, done. No extra infrastructure, same ecosystem as
DataLad.
Member

FWIW -- I really like this concise intro.

FWIW -- I really like this concise intro.
wjm marked this conversation as resolved
@ -0,0 +54,4 @@
shell command, files out.** A Snakemake rule declares `input:`, `output:`, and `shell:`. A
`datalad run` call takes `-i/--input`, `-o/--output`, and a shell command. Both even use placeholder
expansion — DataLad's `{inputs}` and `{outputs}` resolve at run-time to whatever you specified with
`-i` and `-o`, while Snakemake's **wildcards** like `{subject}` and `{experiment}` fan out one rule
Member

eh, that's what I would be missing in forgejo - absent support for suggestions: https://codeberg.org/forgejo/forgejo/issues/3933 . here thought to say that it is

arbitrary wildcards

to accent that it is not hardcoded in Snakemake subject and experiment

eh, that's what I would be missing in forgejo - absent support for suggestions: https://codeberg.org/forgejo/forgejo/issues/3933 . here thought to say that it is arbitrary [**wildcards**](https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#snakefiles-wildcards) to accent that it is not hardcoded in Snakemake `subject` and `experiment`
Author
Contributor

good point, will resolve in the next commit

good point, will resolve in the next commit
wjm marked this conversation as resolved
@ -0,0 +61,4 @@
The result: a Snakemake rule wraps a `datalad run` call without any glue code, adapters, or plugins
— Snakemake handles the orchestration, DataLad handles the provenance. Shared mental model,
distributed tasks.
Member

you are selling it so well, we really need to look into making some kind of 'datalad emphemeral executor' for those since snakemake indeed defines all the needed ingredients for the datalad run call pretty much, and here we are kinda just duplicating them

you are selling it so well, we really need to look into making some kind of 'datalad emphemeral executor' for those since snakemake indeed defines all the needed ingredients for the datalad run call pretty much, and here we are kinda just duplicating them
Author
Contributor

hm I might have a different opinion on this. I think it should be rather the other way around:

If your pipeline is composed of a series of well-defined datalad runcmd, then it should be possible to generate a Snakefile based on that because all information snakemake needs is already in the datalad runcmds. In the section below I discuss how the inputs/outputs serve completely different purposes. Essentially, one should walk bottom-up -- from the custom script / shell command up to the datalad run cmd and then up to the snakemake rule. At each level inputs/outputs would serve different purposes: computation -> provenance tracking -> staleness detection. So it is better to strip off duplicated declaration top-down: first at the level of staleness detection, then ... Otherwise you would end up not being able to execute a single datalad runcmd without it being embedded in a Snakerule.

hm I might have a different opinion on this. I think it should be rather the other way around: If your pipeline is composed of a series of well-defined datalad runcmd, then it should be possible to generate a Snakefile based on that because all information snakemake needs is already in the datalad runcmds. In the section below I discuss how the inputs/outputs serve completely different purposes. Essentially, one should walk bottom-up -- from the custom script / shell command up to the datalad run cmd and then up to the snakemake rule. At each level inputs/outputs would serve different purposes: computation -> provenance tracking -> staleness detection. So it is better to strip off duplicated declaration top-down: first at the level of staleness detection, then ... Otherwise you would end up not being able to execute a single datalad runcmd without it being embedded in a Snakerule.
@ -0,0 +69,4 @@
sequence steps (or to remember which steps need a rerun for which subjects and why ... seriously, how do
people do it?)
- **Incremental execution** — only stale or missing outputs get recomputed
- **Provenance** — every step is a `datalad run` commit with full input -> output tracking
Member

side topic: and i bet if you try with https://github.com/datalad/datalad/pull/7821 smth like

datalad run snakemake ...

you might really like what you get ;) (may be even spice it up with duct ahead of snakemake)

side topic: and i bet if you try with https://github.com/datalad/datalad/pull/7821 smth like ``` datalad run snakemake ... ``` you might really like what you get ;) (may be even spice it up with `duct` ahead of `snakemake`)
Author
Contributor

so you are serving me a DataLad-Sandwich? :D sounds yummy

so you are serving me a DataLad-Sandwich? :D sounds yummy
@ -0,0 +93,4 @@
linkStyle 0,2,3 stroke:#b2d9e0,stroke-width:3px
linkStyle 1 stroke:#f9b97f,stroke-width:3px
```
Member

unfortunately there is also no preview yet on the hub, but that is something we could work to improve using smth like https://codeberg.org/eniix/actions-netlify

unfortunately there is also no preview yet on the hub, but that is something we could work to improve using smth like https://codeberg.org/eniix/actions-netlify
wjm force-pushed snakemake from 4987369061
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 16s
to ec2e156e4c
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 16s
2026-04-08 10:40:33 +00:00
Compare
Author
Contributor

Following the discussion with @yarikoptic on Element, pushed two commits regarding the mermaid rendering

  • 3950f9c341 adds a bash script to vendor mermaid locally from the official source; plus a render hook for loading only when needed
  • 0cfac7b6da replaces the previously downloaded mermaid.mini.js with a datalad runcmd that calls vendor_mermaid.sh
Following the discussion with @yarikoptic on Element, pushed two commits regarding the mermaid rendering - 3950f9c34117bc430b7a58b4c89ff97da5998b62 adds a bash script to vendor mermaid locally from the official source; plus a render hook for loading only when needed - 0cfac7b6dab03b2e37e3bb02c7a8a90979193866 replaces the previously downloaded mermaid.mini.js with a datalad runcmd that calls vendor_mermaid.sh
yarikoptic left a comment

more comments ! sorry -- I guess it is too dense that it takes me a bit to get through... but also it brings me to those outstanding issues/PRs/experiments to do to ensure that everything presented adequately...

when you get tired of this -- just say so, we can publish at any time now I think ;-)

more comments ! sorry -- I guess it is too dense that it takes me a bit to get through... but also it brings me to those outstanding issues/PRs/experiments to do to ensure that everything presented adequately... when you get tired of this -- just say so, we can publish at any time now I think ;-)
@ -0,0 +179,4 @@
To prevent Snakemake from expanding them,
they're escaped with double braces: `{{inputs}}` and `{{outputs}}`.
Because the two serve different purposes, they don't have to agree.
Member

Frankly here you make me pause since even though if purpose might be different, one is superset of another, so in a "limit" (no outputs yet produced, only inputs there) they should be identical...

Frankly here you make me pause since even though if *purpose* might be different, one is superset of another, so in a "limit" (no outputs yet produced, only inputs there) they should be identical...
Author
Contributor

You are right, since both snakemake's input and datalad's input do refer to the same things. And in most cases, it is convenient and makes the logic even simpler if they just agree. For example, a generic snakemake rule for a datalad runcmd can be as follow (have not actually tried out):

	rule do_something:
    input:
        input1,
        input2,
    output:
        output1,
        output2,
    shell:
        """
        datalad run \
            --explicit \
            -m "Do something." \
            -i {input} \
            -o {output} \
            "python some_command {{inputs}} {{outputs}}"
        """

But I believe the dissociation of the purposes can provide some extra flexibility. For instance Suite2p expects directories as inputs, and not files. So my custom script also expects directories as inputs, this propagates up to the datalad runcmd. But for snakemake, if input is a directory, then only adding or removing files will be detected, but not inplace file modifications. So in this case, I might want to specify the actual files as input: in the rule definition, bue keep the directories for datalad's -i.

Maybe I'm overcomplicating things, happy to hear suggestions as what the blog post should get across :)

You are right, since both snakemake's input and datalad's input do refer to the same things. And in most cases, it is convenient and makes the logic even simpler if they just agree. For example, a generic snakemake rule for a datalad runcmd can be as follow (have not actually tried out): ``` rule do_something: input: input1, input2, output: output1, output2, shell: """ datalad run \ --explicit \ -m "Do something." \ -i {input} \ -o {output} \ "python some_command {{inputs}} {{outputs}}" """ ``` But I believe the dissociation of the purposes can provide some extra flexibility. For instance Suite2p expects directories as inputs, and not files. So my custom script also expects directories as inputs, this propagates up to the datalad runcmd. But for snakemake, if input is a directory, then only adding or removing files will be detected, but not inplace file modifications. So in this case, I might want to specify the actual files as `input:` in the rule definition, bue keep the directories for datalad's `-i`. Maybe I'm overcomplicating things, happy to hear suggestions as what the blog post should get across :)
@ -0,0 +218,4 @@
1. Removing annexed files is instant (just delete the symlink), whereas `datalad unlock` on large
binary files can take a very long time because it replaces the symlink with a full copy. So
Snakemake's "delete first, write fresh" approach is actually *faster* than the unlock-modify-save
cycle.
Member

there is

❯ datalad run --help | grep assume
                   [--assume-ready {inputs|outputs|both}] [--explicit]
  --assume-ready {inputs|outputs|both}

to avoid unlocks etc... so you could overcome unlocking by using --assume-ready outputs

there is ``` ❯ datalad run --help | grep assume [--assume-ready {inputs|outputs|both}] [--explicit] --assume-ready {inputs|outputs|both} ``` to avoid unlocks etc... so you could overcome unlocking by using `--assume-ready outputs`
Author
Contributor

but the output file still needs to be unlocked, right? otherwise the command will fail in the final step of writing with a 'permission denied' error.

but the output file still needs to be unlocked, right? otherwise the command will fail in the final step of writing with a 'permission denied' error.
Member

right, but what I meant that since snakemake removes them anyways , --assume-ready outputs would allow datalad run to not crash due to failed status check, but that seems not even seems needed in explicit mode?:

❯ datalad create test; cd test
create(ok): /tmp/test (dataset)
❯ datalad run 'touch 123'
[INFO   ] == Command start (output follows) ===== 
[INFO   ] == Command exit (modification check follows) ===== 
run(ok): /tmp/test (dataset) [touch 123]
add(ok): 123 (file)                                                             
save(ok): . (dataset)                                                           
❯ rm 123
❯ datalad run --output 123 'touch 123'
run(impossible): /tmp/test (dataset) [clean dataset required to detect changes from command; use `datalad status` to inspect unsaved changes]
❯ datalad run --explicit --output 123 'touch 123'
[INFO   ] == Command start (output follows) ===== 
[INFO   ] == Command exit (modification check follows) ===== 
run(ok): /tmp/test (dataset) [touch 123]
add(ok): 123 (file)    
right, but what I meant that since snakemake removes them anyways , `--assume-ready outputs` would allow `datalad run` to not crash due to failed status check, but that seems not even seems needed in explicit mode?: ``` ❯ datalad create test; cd test create(ok): /tmp/test (dataset) ❯ datalad run 'touch 123' [INFO ] == Command start (output follows) ===== [INFO ] == Command exit (modification check follows) ===== run(ok): /tmp/test (dataset) [touch 123] add(ok): 123 (file) save(ok): . (dataset) ❯ rm 123 ❯ datalad run --output 123 'touch 123' run(impossible): /tmp/test (dataset) [clean dataset required to detect changes from command; use `datalad status` to inspect unsaved changes] ❯ datalad run --explicit --output 123 'touch 123' [INFO ] == Command start (output follows) ===== [INFO ] == Command exit (modification check follows) ===== run(ok): /tmp/test (dataset) [touch 123] add(ok): 123 (file) ```
@ -0,0 +221,4 @@
cycle.
2. Some software intentionally reuses existing outputs to avoid heavy recomputation. This can be
convenient when you are sure that the outputs won't change, but for full reproducibility a fresh
computation of outputs should be the go-to.
Member

yeap, and there are cases where it is pretty much that output is also an input, so needs to be declared as such, but then better not to be removed! (e.g. it is some persistent db or state file and process just adjusts it) What will snakemake do if output is also declared as an input?

Actually that might be a good example (for me?) to separate out staleness from provenance: even though such input/output file is truly its own input, we do not care about its staleness for recompute! it is just a side-effect of a kind and not worth tracking as 'input' for snakemake but needs to be declared as input for datalad since it needs first to get it (although might need to get for output as well to unlock)

yeap, and there are cases where it is pretty much that output is also an input, so needs to be declared as such, but then better not to be removed! (e.g. it is some persistent db or state file and process just adjusts it) What will snakemake do if output is also declared as an input? Actually that might be a good example (for me?) to separate out staleness from provenance: even though such input/output file is truly its own input, we do not care about its staleness for recompute! it is just a side-effect of a kind and not worth tracking as 'input' for snakemake but needs to be declared as input for datalad since it needs first to get it (although might need to get for output as well to unlock)
Author
Contributor

Good question, yarik! I have such a case in the climate project, should start setting up a Snakefile there as well!

So, here is what I think: When in step B you have in-place manipulation of a file, i.e. file_B -> step B -> file_B, you would want to trigger a rerun of this step, when the output of the previous step A has changed. So you would have rule like this:

	rule A:
    input:
        input_A,
    output:
        output_A,
    shell:
        """
        datalad run \
            --explicit \
            -m "Run A." \
            -i {input} \
            -o {output} \
            "python command_A {{inputs}} {{outputs}}"
        """

	rule B:
    input:
        output_A,
    output:
        file_B,
    shell:
        """
        datalad run \
            --explicit \
            -m "Run B." \
            -i {output} \
            -o {output} \
            "python command_B {{inputs/outputs}}"
        """

Well this actually only makes sense when output_A == output_B because otherwise there is no dependency between these two rules. But also, in a well-defined workflow file_B has to come from somewhere, right?

Good question, yarik! I have such a case in the climate project, should start setting up a Snakefile there as well! So, here is what I think: When in step B you have in-place manipulation of a file, i.e. file_B -> step B -> file_B, you would want to trigger a rerun of this step, when the output of the previous step A has changed. So you would have rule like this: ``` rule A: input: input_A, output: output_A, shell: """ datalad run \ --explicit \ -m "Run A." \ -i {input} \ -o {output} \ "python command_A {{inputs}} {{outputs}}" """ rule B: input: output_A, output: file_B, shell: """ datalad run \ --explicit \ -m "Run B." \ -i {output} \ -o {output} \ "python command_B {{inputs/outputs}}" """ ``` Well this actually only makes sense when output_A == output_B because otherwise there is no dependency between these two rules. But also, in a well-defined workflow file_B has to come from somewhere, right?
@ -0,0 +227,4 @@
outputs (skip staleness detection) since Snakemake's `output:` is completely decoupled from what
DataLad or your script sees.
### Run, Snakemake! (Or schlängeln?)
Member

;-)

;-)
Author
Contributor

Isn't that just a beautiful word? How do you describe the locomotion of a snake (dt. Schlange)? The german says: snake (dt. schlängeln)?

Isn't that just a beautiful word? How do you describe the locomotion of a snake (dt. Schlange)? The german says: snake (dt. schlängeln)?
@ -0,0 +264,4 @@
Snakemake is designed for parallelism —
give it `--cores 8` and it will run independent rules simultaneously.
But `datalad run` commits after every rule,
and git only supports one writer at a time.
Member

all of that could be likely worked around by using locking (e.g. via flock or alike) prior save in datalad run! we really should look into it; and then using --explicit so it would save only what it worked on (e.g. specific subject). This way they could work in parallel and then serially, in FIFO order, go through saves. But indeed worktrees are better solution...!

all of that could be likely worked around by using locking (e.g. via flock or alike) prior save in `datalad run`! we really should look into it; and then using `--explicit` so it would save only what it worked on (e.g. specific subject). This way they could work in parallel and then serially, in FIFO order, go through saves. But indeed worktrees are better solution...!
Author
Contributor

I'm completely lost on this comment, but happy to LOOK INTO IT! TOGETHER ;-)

I'm completely lost on this comment, but happy to LOOK INTO IT! TOGETHER ;-)
@ -0,0 +285,4 @@
For full parallelism, separate **ephemeral clones** are needed —
throw-away clones that share the annex object store,
but each have their own index and their own `git-annex` branch.
Member

And that is what git-annex achieves using worktrees -- they can be used as ephemeral, but apparently annex does not declare them as "private":

❯ rm -rf test-clone-; git -C test worktree add -B newbranch $PWD/test-clone-; git -C test-clone- config annex.private; ls -l test-clone-/.git/annex
Preparing worktree (resetting branch 'newbranch'; was at 4703f09)
HEAD is now at 4703f09 [DATALAD] Recorded changes
lrwxrwxrwx 1 yoh yoh 11 Apr  8 10:03 test-clone-/.git/annex -> ../../annex/

unless what we do for --reckless ephemeral:

❯ rm -rf test-clone-; datalad clone --reckless=ephemeral test test-clone-; git -C test-clone- config annex.private
install(ok): /tmp/test-clone- (dataset)                                                                         
true

So, in your case, after all the merges -- do you have "git annex memory" (git annex info) about those temproary clones?

Relevant

And that is what git-annex achieves using worktrees -- they can be used as ephemeral, but apparently annex does not declare them as "private": ``` ❯ rm -rf test-clone-; git -C test worktree add -B newbranch $PWD/test-clone-; git -C test-clone- config annex.private; ls -l test-clone-/.git/annex Preparing worktree (resetting branch 'newbranch'; was at 4703f09) HEAD is now at 4703f09 [DATALAD] Recorded changes lrwxrwxrwx 1 yoh yoh 11 Apr 8 10:03 test-clone-/.git/annex -> ../../annex/ ``` unless what we do for `--reckless ephemeral`: ``` ❯ rm -rf test-clone-; datalad clone --reckless=ephemeral test test-clone-; git -C test-clone- config annex.private install(ok): /tmp/test-clone- (dataset) true ``` So, in your case, after all the merges -- do you have "git annex memory" (`git annex info`) about those temproary clones? Relevant - https://github.com/datalad/datalad/pull/7247
Author
Contributor

Good to know!

You mean whether git annex treats worktrees as ephemeral clones (so the other way around)? When I ran git annex info I don't see any worktree listed.

Good to know! You mean whether git annex treats worktrees as ephemeral clones (so the other way around)? When I ran `git annex info` I don't see any worktree listed.
@ -0,0 +293,4 @@
Even so, Snakemake still provides its core value — staleness detection and
automatic chaining — which is why I'm here in the first place.
But staleness detection has some sharp edges
when combined with git and worktrees.
Member

I wonder if worth simplifying here and making explicit that you will not go through this possibility of parallelization via worktrees in this blog post because of this.

Because you do talk about worktrees below in the context of regular serial snakemake'ing in them (if I got it right) but this potential use of worktrees for parallelization kinda occupied my mental picture here and thus needs explicit "purge" I guess ;-)

I wonder if worth simplifying here and making explicit that you will not go through this possibility of parallelization via worktrees in this blog post because of this. Because you do talk about worktrees below in the context of regular serial snakemake'ing in them (if I got it right) but this potential use of worktrees for parallelization kinda occupied my mental picture here and thus needs explicit "purge" I guess ;-)
Author
Contributor

hm i think it's worth being explicit : multiple worktrees is not a solution for parallelism! would this help?

hm i think it's worth being explicit : multiple worktrees is not a solution for parallelism! would this help?
Author
Contributor

actually it's already stated so in the text just slightly above, maybe your mind is too occupied from all these long comments I left on worktrees across platforms.. ?

actually it's already stated so in the text just slightly above, maybe your mind is too occupied from all these long comments I left on worktrees across platforms.. ?
Author
Contributor

Can you check 44c0aadcb7 if it helps?

Can you check 44c0aadcb7 if it helps?
wjm force-pushed snakemake from ec2e156e4c
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 16s
to 7b5a0815e6
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 15s
2026-04-08 17:55:10 +00:00
Compare
wjm force-pushed snakemake from 7b5a0815e6
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 15s
to 3bfade791c
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 15s
2026-04-08 17:58:23 +00:00
Compare
wjm force-pushed snakemake from 3bfade791c
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 15s
to 44c0aadcb7
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 15s
2026-04-08 18:04:07 +00:00
Compare
update the date
All checks were successful
Codespell / Check for spelling errors (pull_request) Successful in 14s
37ddc14ae5
Author
Contributor

I updated the date, ready for you to merge @yarikoptic :)

I updated the date, ready for you to merge @yarikoptic :)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
www/datalad-blog!15
No description provided.