Skip to main content

The uncompromising Snakemake code formatter

Project description

Snakefmt

GitHub Workflow Status codecov PyPI PyPI - Python Version License: MIT Code style: black

This repository provides formatting for Snakemake files. It follows the design and specifications of Black.

⚠️WARNING⚠️: snakefmt modifies files in-place by default, thus we strongly recommend ensuring your files are under version control before doing any formatting. You can also pipe the file in from stdin, which will print it to the screen, or use the --diff or --check options. See Usage for more details.

Table of Contents

Install

PyPi

pip install snakefmt

Conda

Conda (channel only) bioconda version

conda install -c bioconda snakefmt

Containers

Docker

Docker Image Version (latest semver)

docker pull snakemake/snakefmt
docker run -it snakemake/snakefmt snakefmt --help

You can find all the available tags on the Docker Hub repository.

Singularity

URI="docker://snakemake/snakefmt"
singularity exec "$URI" snakefmt --help

The above will use the latest version. If you want to specify a version then use a tag like so.

VERSION="x.x.x"
URI="docker://snakemake/snakefmt:${VERSION}"

Local

These instructions include installing poetry.

# install poetry
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3

git clone https://github.com/snakemake/snakefmt && cd snakefmt
# install snakefmt in a new environment
poetry install
# activate the environment so snakefmt is available on your PATH
poetry shell

GitHub Actions

GitHub Actions in combination with super-linter allows you to automatically run snakefmt on all Snakefiles in your repository e.g. whenever you push a new commit.

To do so, create the file .github/workflows/linter.yml in your repository:

---
name: Lint Code Base

on:
  push:
  pull_request:
    branches: [master]

jobs:
  build:
    name: Lint Code Base
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v2

      - name: Lint Code Base
        uses: github/super-linter@v3
        env:
          VALIDATE_ALL_CODEBASE: false
          DEFAULT_BRANCH: master
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

          VALIDATE_SNAKEMAKE_SNAKEFMT: true

Additional configuration parameters can be specified by creating .github/linters/.snakefmt.toml:

[tool.black]
skip-string-normalization = 1

For more information check the super-linter readme.

Example File

Input

from snakemake.utils import min_version
min_version("5.14.0")
configfile: "config.yaml" # snakemake keywords are treated like classes i.e. 2 newlines
SAMPLES = ['s1', 's2'] # strings are normalised
CONDITIONS = ["a", "b", "longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglong"] # long lines are wrapped
include: "rules/foo.smk" # 2 newlines

rule all:
    input: "data/results.txt" # newlines after keywords enforced and trailing comma

rule gets_separated_by_two_newlines:
    input:
        files = expand("long/string/to/data/files/gets_broken_by_black/{sample}.{condition}",sample=SAMPLES, condition=CONDITIONS)
if True:
    rule can_be_inside_python_code:
        input: "parameters", "get_indented"
        threads: 4 # Numeric params stay unindented
        params: key_val = "PEP8_formatted"
        run:

                print("weirdly_spaced_string_gets_respaced")

Output

from snakemake.utils import min_version

min_version("5.14.0")


configfile: "config.yaml" # snakemake keywords are treated like classes i.e. 2 newlines


SAMPLES = ["s1", "s2"] # strings are normalised
CONDITIONS = [
    "a",
    "b",
    "longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglong",
]  # long lines are wrapped


include: "rules/foo.smk" # 2 newlines


rule all:
    input:
        "data/results.txt", # newlines after keywords enforced and trailing comma


rule gets_separated_by_two_newlines:
    input:
        files=expand(
            "long/string/to/data/files/gets_broken_by_black/{sample}.{condition}",
            sample=SAMPLES,
            condition=CONDITIONS,
        ),


if True:

    rule can_be_inside_python_code:
        input:
            "parameters",
            "get_indented",
        threads: 4 # Numeric params stay unindented
        params:
            key_val="PEP8_formatted",
        run:
            print("weirdly_spaced_string_gets_respaced")

Usage

Basic Usage

Format a single Snakefile.

snakefmt Snakefile

Format all Snakefiles within a directory.

snakefmt workflows/

Format a file but write the output to stdout.

snakefmt - < Snakefile

Full Usage

$ snakefmt --help
Usage: snakefmt [OPTIONS] [SRC]...

  The uncompromising Snakemake code formatter.

  SRC specifies directories and files to format. Directories will be
  searched for file names that conform to the include/exclude patterns
  provided.

  Files are modified in-place by default; use diff, check, or  `snakefmt - <
  Snakefile` to avoid this.

Options:
  -l, --line-length INT  Lines longer than INT will be wrapped.  [default: 88]
  --check                Don't write the files back, just return the status.
                         Return code 0 means nothing would change. Return code
                         1 means some files would be reformatted. Return code
                         123 means there was an error.

  -d, --diff             Don't write the files back, just output a diff for
                         each file to stdout.

  --compact-diff         Same as --diff but only shows lines that would change
                         plus a few lines of context.

  --include PATTERN      A regular expression that matches files and
                         directories that should be included on recursive
                         searches.  An empty value means all files are
                         included regardless of the name.  Use forward slashes
                         for directories on all platforms (Windows, too).
                         Exclusions are calculated first, inclusions later.
                         [default: (\.smk$|^Snakefile)]

  --exclude PATTERN      A regular expression that matches files and
                         directories that should be excluded on recursive
                         searches.  An empty value means no paths are
                         excluded. Use forward slashes for directories on all
                         platforms (Windows, too). Exclusions are calculated
                         first, inclusions later.  [default: (\.snakemake|\.eg
                         gs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|\.svn|_
                         build|buck-out|build|dist)]

  -c, --config PATH      Read configuration from PATH. By default, will try to
                         read from `./pyproject.toml`

  -h, --help             Show this message and exit.
  -V, --version          Show the version and exit.
  -v, --verbose          Turns on debug-level logging.

Configuration

snakefmt is able to read project-specific default values for its command line options from a pyproject.toml file. In addition, it will also load any black configurations you have in the same file.

By default, snakefmt will search in your current directory for a file named pyproject.toml. If your configuration file is located somewhere else or called something different, then specify the location with --config.

Any options you pass on the command line will take precedence over default values in the configuration file.

Example

pyproject.toml

[tool.snakefmt]
line-length = 90
include = '\.smk$|^Snakefile|\.py$'

# snakefmt passes these options on to black
[tool.black]
target-version = ["py37"]

In this example we increase the --line-length value and also include python (*.py) files for formatting - this effectively runs black on them. snakefmt will also pass on the [tool.black] settings, internally, to black.

Editor Integration

For instructions on how to integrate snakefmt into your editor of choice, refer to docs/editor_integration.md

Plug Us

If you can't get enough of badges, then feel free to show others you're using snakefmt in your project.

Code style: snakefmt

Markdown

[![Code style: snakefmt](https://img.shields.io/badge/code%20style-snakefmt-000000.svg)](https://github.com/snakemake/snakefmt)

ReStructuredText

.. image:: https://img.shields.io/badge/code%20style-snakefmt-000000.svg
    :target: https://github.com/snakemake/snakefmt

Changes

See CHANGELOG.md.

Contributing

See CONTRIBUTING.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snakefmt-0.2.1.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

snakefmt-0.2.1-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file snakefmt-0.2.1.tar.gz.

File metadata

  • Download URL: snakefmt-0.2.1.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.6

File hashes

Hashes for snakefmt-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6569cf849719b5383261250cf0d82effe0bc57b58f412a196a5daeadfa201ed6
MD5 feb1c4d29f80bfe85f25a8081524f468
BLAKE2b-256 37ce6a4eed8a15a234bed26212f48744d507a50eeb21ff10e36261ef4e37bdb5

See more details on using hashes here.

File details

Details for the file snakefmt-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: snakefmt-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 21.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.6

File hashes

Hashes for snakefmt-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 adb3c7a512e4ceff557057e25f87f4792436c17d24db7441722c3e153e89626c
MD5 54c65f3752c7a95feeb363094af9c257
BLAKE2b-256 1c84d35ae9af0d27d33edb4485502d275ea6566a6d6cb3670439ec10ffb9b08e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page