Skip to main content

Snakemake rule wrapper that expands named inputs, outputs, and params into script CLI arguments

Project description

snakemake-contracts-wrapper

Snakemake rule wrapper that expands named input, output, log, and params objects into script CLI arguments.

Motivation

Snakemake rules commonly repeat the same wiring: --flag {input.foo} --other-flag {output.bar} for every rule that calls a script. This package removes that repetition. It reads the named objects already present on a rule and turns them into ordinary command-line arguments, so the script itself stays unaware of Snakemake and only ever sees CLI flags.

This package is the wrapper layer for snakemake-contracts, but it does not depend on it and can be used on its own.

Usage

As a Python helper:

from snakemake_contracts_wrapper import build_argv, render_shell

argv = build_argv(
    script="scripts/train.py",
    input={"data": "data.csv"},
    output={"model": "model.pkl"},
    params={"epochs": 10},
)
# ["scripts/train.py", "--data", "data.csv", "--output-model", "model.pkl", "--epochs", "10"]

render_shell(
    script="scripts/train.py",
    input={"data": "data.csv"},
    output={"model": "model.pkl"},
)
# "scripts/train.py --data data.csv --output-model model.pkl"

As a Snakemake wrapper, point a rule's wrapper: directive at snakemake_contracts_wrapper.WRAPPER_DIR:

from snakemake_contracts_wrapper import WRAPPER_DIR

rule train:
    input:
        script="scripts/train.py",
        data="data.csv",
    output:
        model="model.pkl",
    params:
        epochs=10,
    wrapper:
        f"file:{WRAPPER_DIR}"

The wrapper requires a named input called script, which is excluded from the generated argument list by default.

Log argument

The Snakemake wrapper does not pass --log unless the rule declares a log: output itself, or the workflow config explicitly opts in with scripts_accept_log_arg: true and a cache path is configured. When both are set and no log: is declared, a default log path is synthesized under {cache}/logs/{script_stem}.log. This opt-in is required because a cache config alone does not prove that every wrapped script accepts a --log CLI argument.

Surface

  • build_argv(command, script=None, input=None, output=None, log=None, params=None, exclude_input=("script",), output_prefix="output-")
  • render_shell(command, script=None, input=None, output=None, log=None, params=None, exclude_input=("script",), output_prefix="output-")
  • namespace_flag(namespace, name, output_prefix="output-")
  • snake_to_kebab(name)
  • WRAPPER_DIR

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

snakemake_contracts_wrapper-0.4.1.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snakemake_contracts_wrapper-0.4.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file snakemake_contracts_wrapper-0.4.1.tar.gz.

File metadata

File hashes

Hashes for snakemake_contracts_wrapper-0.4.1.tar.gz
Algorithm Hash digest
SHA256 22de601b7167d8b2ce9ee81aa4f37786d554f38a9e26c5870287c319736c50cd
MD5 5eeef226a76c33fb02645401e5e84244
BLAKE2b-256 f7f48109bafc6c2247a5aafbeab4c628dabdb84b861cfc608b79e12eedb69dfe

See more details on using hashes here.

File details

Details for the file snakemake_contracts_wrapper-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for snakemake_contracts_wrapper-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a6467f17b027ce93111dbc43ff72455806af40f93d54d8abdbbbafdb3aa4b8a
MD5 bcf35e82f135a0fbef8d6e9324bd8818
BLAKE2b-256 2f1d7ea120b131952459131a2b8a52d084766493e27fcbb44f2be318c1bdb371

See more details on using hashes here.

Supported by

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