Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print statements outside of rules are printed each time a rule using "Run" containing a print() statement is executed #1297

Open
jgriffin-encoded opened this issue Dec 9, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@jgriffin-encoded
Copy link

Snakemake version
5.30.2 & 6.12.1. I haven't tried others.

Describe the bug

  • Print statements outside of any rule seemingly(?) print each time a rule using the "run" directive and containing a print statement is executed.
  • Possibly related: In this example below, each time a rule with a run directive is executed the "Job counts" text is also repeated.

Logs

snakemake -s test.smk -j all
I should print once
Building DAG of jobs...
Using shell: /bin/bash
Provided cores: 12
Rules claiming more threads will be scaled down.
Job counts:
        count   jobs
        1       all
        2       run_version
        2       shell_version
        5
...
I should print once
I should print once
Job counts:
        count   jobs
        1       run_version
        1
run directive
Job counts:
        count   jobs
        1       run_version
        1
run directive
...

Minimal example

from pathlib import Path
print("I should print once")

rule all:
    input:
        "A.txt",
        "B.txt",
        "C.csv",
        "D.csv"

rule shell_version:
    output:
        "{sample}.csv"
    shell:
        """
        echo 'Shell directive'
        touch {output}
        """

rule run_version:
    output:
        "{sample}.txt"
    run:
        Path(output[0]).touch()
        print('run directive')

Additional context

@jgriffin-encoded jgriffin-encoded added the bug Something isn't working label Dec 9, 2021
@boulund
Copy link
Contributor

boulund commented Dec 20, 2021

I think this is expected considering how Snakemake re-executes the snakefile several times during workflow execution. I have worked around the issue using the onstart handler in my workflows:
https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#onstart-onsuccess-and-onerror-handlers

Try something like this in your Snakefile:

onstart:
    print("I should print once")

@carruizper
Copy link

Do you know how to access any variable created in the onstart handler? I need to execute a function at start and then for it to be available for subsequent rules

@alephreish
Copy link

To add to the discussion: this behavior caused a difficult-to-figure-out issue related to variables dependent on timestamps in one of my workflows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants