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.2.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.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for snakemake_contracts_wrapper-0.4.2.tar.gz
Algorithm Hash digest
SHA256 076a8cc0361c640df6dce05862099d02ce84cdf2d277555f3fe3f7d39ddcb3e4
MD5 b35fb003e4412e7c932a488a92c3eba6
BLAKE2b-256 9f4de3b1915d1b752e1022860c9c355120db1faf02114e25c88806969c4f7778

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for snakemake_contracts_wrapper-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4a439d9515d857646f76d52f64c2d22c4830921438d0363c42c128ae42758754
MD5 91fcf5ce93534daf486260662e87f897
BLAKE2b-256 9ddabc98158ae597c4a547dcf3cde0267be54a065f6bde688fe616f9d6914e28

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