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:
- Rewrite the script to work with Snakemake's
script:directive - Maintain two versions - one for CLI testing, one for Snakemake
- 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 filesoutput,output[0],output[1], etc. - Output fileswildcards.{name}- Wildcard valuesparams.{name}- Rule parameterslog,log[0], etc. - Log filesthreads- Number of threadsresources.{name}- Resource specificationsconfig.{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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file snakemake_argparse_bridge-0.2.0.tar.gz.
File metadata
- Download URL: snakemake_argparse_bridge-0.2.0.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f64984815a4ff01ddc35337ebe40c33c53ff0a829eb3988df7ea71356295e24f
|
|
| MD5 |
0cca489f16271939347cd2960ff0d920
|
|
| BLAKE2b-256 |
b79b9a03434eb6d17e0430d8cffd7ecc8dbd59b7ce620f2257c7a0b8601783bb
|
Provenance
The following attestation bundles were made for snakemake_argparse_bridge-0.2.0.tar.gz:
Publisher:
release.yml on cademirch/snakemake-argparse-bridge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snakemake_argparse_bridge-0.2.0.tar.gz -
Subject digest:
f64984815a4ff01ddc35337ebe40c33c53ff0a829eb3988df7ea71356295e24f - Sigstore transparency entry: 1181883084
- Sigstore integration time:
-
Permalink:
cademirch/snakemake-argparse-bridge@d0eed84a1e787565655b9cfc443ebe93611f87fe -
Branch / Tag:
refs/heads/main - Owner: https://github.com/cademirch
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d0eed84a1e787565655b9cfc443ebe93611f87fe -
Trigger Event:
push
-
Statement type:
File details
Details for the file snakemake_argparse_bridge-0.2.0-py3-none-any.whl.
File metadata
- Download URL: snakemake_argparse_bridge-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2119d6f7c12dfc2d91ae898bf0a46094ae695c317360e59f363f831c8fe2274e
|
|
| MD5 |
85a1f9ac688c660ff1ecc0460eb55de8
|
|
| BLAKE2b-256 |
3d46c84dc9d2bb9a47575b17c52a730fb28a7c7de7d3d0817d5d341e0d7f73f0
|
Provenance
The following attestation bundles were made for snakemake_argparse_bridge-0.2.0-py3-none-any.whl:
Publisher:
release.yml on cademirch/snakemake-argparse-bridge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snakemake_argparse_bridge-0.2.0-py3-none-any.whl -
Subject digest:
2119d6f7c12dfc2d91ae898bf0a46094ae695c317360e59f363f831c8fe2274e - Sigstore transparency entry: 1181883096
- Sigstore integration time:
-
Permalink:
cademirch/snakemake-argparse-bridge@d0eed84a1e787565655b9cfc443ebe93611f87fe -
Branch / Tag:
refs/heads/main - Owner: https://github.com/cademirch
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d0eed84a1e787565655b9cfc443ebe93611f87fe -
Trigger Event:
push
-
Statement type: