Skip to content

Refactor active Snakefiles to Snakemake best practices#43

Merged
caleblareau merged 1 commit into
masterfrom
refactor-snakemake
Jul 6, 2026
Merged

Refactor active Snakefiles to Snakemake best practices#43
caleblareau merged 1 commit into
masterfrom
refactor-snakemake

Conversation

@caleblareau

Copy link
Copy Markdown
Owner

Follow-up to #42. Refactors the three Snakefiles driving the active commands (Snakefile.bap2.chr, Snakefile.bap_frags, Snakefile.bap_bulk_frags) from the run: + os.system(<string>) anti-pattern to idiomatic Snakemake.

Changes

  • shell: directives replace string-concatenated os.system() calls, using {input}/{output}/{params}/{threads} and global variables.
  • log: directives replace manual 2> / &> redirects.
  • params: hold computed values (name-sorted bam path, temp/annotated paths, cut columns, QC flag toggles). awk programs are kept as readable named constants and substituted verbatim via {params.*} (avoids brace-escaping noise).
  • determine_HQ_beads keeps run: (it has genuine Python: Counter/knee-calling logic) but swaps os.system() for Snakemake's shell() helper.
  • Replaced pysam.sort/pysam.index with the configured samtools binary so those rules become clean shell: blocks, and fixed a latent NameError (undefined pp when --all-pairs is set) via a proper_pair_filter param.

Behavior preservation

Command semantics are preserved exactly. Every rule's rendered shell command was verified with snakemake -n -p against a scaffolded config + DAG (all three build valid DAGs: 17 / 5 / 4 jobs). Subtle differences were kept intact — e.g. bap_frags gzips its bedpe while bap_bulk_frags does not, the nc_threshold argument position, samtools collate vs sort -n, and the tabix <file> -p bed argument order. One real bug was caught and fixed during validation: a bare {name} collided with Snakemake's reserved shell-namespace key and rendered the rule name; it now passes the sample name via a sample_name param.

Out of scope

The deprecated v1 Snakefile.bap.chr is left unchanged, consistent with leaving the v1 CLI untouched in #42.

🤖 Generated with Claude Code

Convert the run:/os.system() anti-pattern in the three active-command
Snakefiles (bap2.chr, bap_frags, bap_bulk_frags) to idiomatic Snakemake:

- Replace string-concatenated os.system() calls with proper shell: directives
  using {input}/{output}/{params}/{threads} and global variables
- Move manual 2>/&> redirects into log: directives
- Move computed values (name-sorted bam path, temp/anno paths, cut columns,
  QC flag toggles) into params:; keep awk programs as readable named constants
  substituted verbatim via {params.*}
- For determine_HQ_beads (genuine Python: Counter/knee logic) keep run: but
  swap os.system() for Snakemake's shell() helper
- Replace pysam.sort/pysam.index with the configured samtools binary so the
  rule is a clean shell:, and fix a latent NameError (undefined `pp` when
  --all-pairs is set) via a proper_pair_filter param

Command semantics are preserved exactly (verified by rendering every rule's
shell command with `snakemake -n -p` against a scaffolded config/DAG). The
deprecated v1 Snakefile.bap.chr is intentionally left unchanged, consistent
with leaving the v1 CLI untouched.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@caleblareau caleblareau merged commit 763fb13 into master Jul 6, 2026
4 checks passed
@caleblareau caleblareau deleted the refactor-snakemake branch July 6, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant