Skip to main content

Add your description here

Project description

Snakemake Argparse Bridge

A Python package that allows existing argparse-based scripts to work seamlessly in both command-line and Snakemake environments with minimal code changes.

Why does this exist?

You just wrote a Python script for your Snakemake rule that uses argparse so you can test and run it on the command line. But then when you integrate it into your workflow, you realize you need to:

  1. Rewrite the script to work with Snakemake's script: directive
  2. Maintain two versions - one for CLI testing, one for Snakemake
  3. Manually map Snakemake variables (wildcards, input, output) to script arguments

This bridge eliminates that friction. Just add one decorator and your script works seamlessly in both environments.

Installation

pip install snakemake-argparse-bridge

Quick Start

1. Decorate your existing script

# scripts/process_sample.py
import argparse
from snakemake_argparse_bridge import snakemake_compatible

@snakemake_compatible(mapping={
    'sample': 'wildcards.sample',
    'input_file': 'input[0]',
    'output_file': 'output[0]',
    'threads': 'threads'
})
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--sample', required=True)
    parser.add_argument('--input-file', required=True)
    parser.add_argument('--output-file', required=True)
    parser.add_argument('--threads', type=int, default=1)
    
    # This works in both CLI and Snakemake contexts!
    args = parser.parse_args()
    
    # Your processing logic here
    print(f"Processing {args.sample} with {args.threads} threads")

if __name__ == '__main__':
    main()

2. Use in Snakemake

# Snakefile
rule process_sample:
    input: "data/{sample}.txt"
    output: "results/{sample}_processed.txt"
    threads: 4
    script: "scripts/process_sample.py"

3. Still works from command line

python scripts/process_sample.py --sample test --input-file data.txt --output-file results.txt --threads 2

How it works

The @snakemake_compatible decorator temporarily patches argparse.ArgumentParser.parse_args() to:

  • In Snakemake context: Extract values from snakemake.wildcards, snakemake.input, snakemake.params, etc.
  • In CLI context: Use normal argparse behavior

Mapping Options

Explicit Mapping

@snakemake_compatible(mapping={
    'sample': 'wildcards.sample',           # Wildcard
    'input_file': 'input[0]',               # First input file
    'output_file': 'output[0]',             # First output file  
    'method': 'params.method',              # Parameter
    'threads': 'threads',                   # Thread count
    'memory': 'resources.mem_mb',           # Memory resource
    'log_file': 'log[0]',                   # Log file
})

Available Snakemake Attributes

  • input, input[0], input[1], etc. - Input files
  • output, output[0], output[1], etc. - Output files
  • wildcards.{name} - Wildcard values
  • params.{name} - Rule parameters
  • log, log[0], etc. - Log files
  • threads - Number of threads
  • resources.{name} - Resource specifications
  • config.{name} - Configuration values

Testing

# Run tests
pytest tests/

# Test with actual Snakemake (requires snakemake installation)
pytest tests/test_snakemake_integration.py

Requirements

  • Python 3.7+
  • No additional dependencies for basic usage
  • Snakemake (for Snakemake integration, obviously)

License

MIT License

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_argparse_bridge-0.1.3.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

snakemake_argparse_bridge-0.1.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file snakemake_argparse_bridge-0.1.3.tar.gz.

File metadata

File hashes

Hashes for snakemake_argparse_bridge-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1f85a84c5ba255e302db19d70cdb8986b492cc905193d42fc52eaca96e3f9e0f
MD5 88e36d18f106dd86be9b3ca570500648
BLAKE2b-256 8ad0a61ffa8cf59448b2285616d20542c2ddb09019fd4552bc92b954eb0f910d

See more details on using hashes here.

Provenance

The following attestation bundles were made for snakemake_argparse_bridge-0.1.3.tar.gz:

Publisher: release.yml on cademirch/snakemake-argparse-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file snakemake_argparse_bridge-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for snakemake_argparse_bridge-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 546ee7492b50c81cb2d9a5b5a3d674041a10f399b92ee3db8b919f8f9185fcef
MD5 f32053e6830a0f0136e72c8f0f5cc791
BLAKE2b-256 bbb71a33a42dcc683a1ca3e514d815e9d4a275338d04b8a489d0e06f728f79fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for snakemake_argparse_bridge-0.1.3-py3-none-any.whl:

Publisher: release.yml on cademirch/snakemake-argparse-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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