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

snakemake-style-guide: Recommend *raw* triple-quoted strings for shell blocks #221

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/reference/snakemake-style-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ For example, do this:
params:
name = lambda _: config["name"]
shell:
"""
r"""
echo {params.name:q}
"""

Expand Down Expand Up @@ -152,7 +152,7 @@ the ``:q`` modifier for interpolation:
params:
file = "filename with spaces.txt"
shell:
"""
r"""
wc -l {params.file:q}
"""

Expand All @@ -169,31 +169,43 @@ string. Snakemake will interpolate it correctly:
params:
files = ["a.txt", "b.txt", "c.txt"]
shell:
"""
r"""
wc -l {params.files:q}
"""

Use triple-quoted command definitions
=====================================
.. _use-triple-quoted-command-definitions:

Use raw, triple-quoted shell blocks
===================================

Using triple-quoted (``"""`` or ``'''``) command definitions make it
Using raw, triple-quoted (``r"""`` or ``r'''``) ``shell`` blocks makes it
much easier to build readable commands with one-option per line. It also
avoids any nested quoting issues if you need to use literal single or
double quotes in your command.
double quotes in your command. The command will remain readable in
Snakemake's logging messages because it'll look like the source form
(e.g. with backslashes and newlines retained instead of collapsed).

Example:

.. code-block:: python

shell:
"""
r"""
augur parse \
--sequences {input:q} \
--fields {params.fields:q} \
--output-sequences {output.sequences:q} \
--output-metadata {output.metadata:q}
"""

.. hint::
If you're converting interpreted strings to raw strings (e.g.
``"""`` to ``r"""``), make sure to check that they're not relying on
`escape sequences`_ like ``\n``, ``\t``, or ``\\`` to be interpreted by
Python before the shell (Bash) sees them.

.. _escape sequences: https://docs.python.org/3/reference/lexical_analysis.html#escape-sequences

Log standard out and error output to log files and the terminal
===============================================================

Expand All @@ -212,7 +224,7 @@ Example:
log:
"logs/filter.txt"
shell:
"""
r"""
augur filter \
--metadata {input.metadata} \
--output-metadata {output.metadata} 2>&1 | tee {log}
Expand Down
Loading