Skip to content

feat: add temp dir and code refactor #4319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions bio/mosdepth/test/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ rule mosdepth:
bai="aligned/{dataset}.bam.bai",
output:
"mosdepth/{dataset}.mosdepth.global.dist.txt",
"mosdepth/{dataset}.per-base.bed.gz", # produced unless --no-per-base specified
summary="mosdepth/{dataset}.mosdepth.summary.txt", # this named output is required for prefix parsing
"mosdepth/{dataset}.per-base.bed.gz",
summary="mosdepth/{dataset}.mosdepth.summary.txt",
log:
"logs/mosdepth/{dataset}.log",
params:
extra="--fast-mode", # optional
# additional decompression threads through `--threads`
threads: 4 # This value - 1 will be sent to `--threads`
extra="--fast-mode",
threads: 4
wrapper:
"master/bio/mosdepth"

Expand All @@ -25,13 +24,12 @@ rule mosdepth_bed:
"mosdepth_bed/{dataset}.mosdepth.global.dist.txt",
"mosdepth_bed/{dataset}.mosdepth.region.dist.txt",
"mosdepth_bed/{dataset}.regions.bed.gz",
summary="mosdepth_bed/{dataset}.mosdepth.summary.txt", # this named output is required for prefix parsing
summary="mosdepth_bed/{dataset}.mosdepth.summary.txt",
log:
"logs/mosdepth_bed/{dataset}.log",
params:
extra="--no-per-base --use-median", # optional
# additional decompression threads through `--threads`
threads: 4 # This value - 1 will be sent to `--threads`
extra="--use-median",
threads: 4
wrapper:
"master/bio/mosdepth"

Expand All @@ -44,15 +42,14 @@ rule mosdepth_by_threshold:
"mosdepth_by_threshold/{dataset}.mosdepth.global.dist.txt",
"mosdepth_by_threshold/{dataset}.mosdepth.region.dist.txt",
"mosdepth_by_threshold/{dataset}.regions.bed.gz",
"mosdepth_by_threshold/{dataset}.thresholds.bed.gz", # needs to go with params.thresholds spec
summary="mosdepth_by_threshold/{dataset}.mosdepth.summary.txt", # this named output is required for prefix parsing
"mosdepth_by_threshold/{dataset}.thresholds.bed.gz",
summary="mosdepth_by_threshold/{dataset}.mosdepth.summary.txt",
log:
"logs/mosdepth_by/{dataset}.log",
params:
by="500", # optional, window size, specifies --by for mosdepth.region.dist.txt and regions.bed.gz
thresholds="1,5,10,30", # optional, specifies --thresholds for thresholds.bed.gz
# additional decompression threads through `--threads`
threads: 4 # This value - 1 will be sent to `--threads`
by="500", # needed for mosdepth.region.dist.txt and regions.bed.gz
thresholds="1,5,10,30", # needed for thresholds.bed.gz
threads: 4
wrapper:
"master/bio/mosdepth"

Expand All @@ -63,16 +60,14 @@ rule mosdepth_quantize_precision:
bai="aligned/{dataset}.bam.bai",
output:
"mosdepth_quantize_precision/{dataset}.mosdepth.global.dist.txt",
"mosdepth_quantize_precision/{dataset}.quantized.bed.gz", # optional, needs to go with params.quantize spec
summary="mosdepth_quantize_precision/{dataset}.mosdepth.summary.txt", # this named output is required for prefix parsing
"mosdepth_quantize_precision/{dataset}.quantized.bed.gz",
summary="mosdepth_quantize_precision/{dataset}.mosdepth.summary.txt",
log:
"logs/mosdepth_quantize_precision/{dataset}.log",
params:
extra="--no-per-base", # optional
quantize="0:1:5:150", # optional, specifies --quantize for quantized.bed.gz
quantize="0:1:5:150", # needed for quantized.bed.gz
precision="5", # optional, set decimals of precision
# additional decompression threads through `--threads`
threads: 4 # This value - 1 will be sent to `--threads`
threads: 4
wrapper:
"master/bio/mosdepth"

Expand All @@ -87,12 +82,11 @@ rule mosdepth_cram:
"mosdepth_cram/{dataset}.mosdepth.global.dist.txt",
"mosdepth_cram/{dataset}.mosdepth.region.dist.txt",
"mosdepth_cram/{dataset}.regions.bed.gz",
summary="mosdepth_cram/{dataset}.mosdepth.summary.txt", # this named output is required for prefix parsing
summary="mosdepth_cram/{dataset}.mosdepth.summary.txt",
log:
"logs/mosdepth_cram/{dataset}.log",
params:
extra="--no-per-base --use-median", # optional
# additional decompression threads through `--threads`
threads: 4 # This value - 1 will be sent to `--threads`
extra="--use-median",
threads: 4
wrapper:
"master/bio/mosdepth"
2 changes: 1 addition & 1 deletion bio/mosdepth/test/test.bed
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ecoliK12_pbi_March2013 40645 40845
Sheila 2 6
138 changes: 63 additions & 75 deletions bio/mosdepth/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,50 @@
__email__ = "wrowell@pacb.com"
__license__ = "MIT"

import sys

import tempfile
from snakemake.shell import shell


extra = snakemake.params.get("extra", "")
log = snakemake.log_fmt_shell(stdout=True, stderr=True)

bed = snakemake.input.get("bed", "")
by = snakemake.params.get("by", "")
if by:
if bed:
sys.exit(
"Either provide a bed input file OR a window size via params.by, not both."
)
else:
by = f"--by {by}"
if bed:
by = f"--by {bed}"

quantize_out = False
thresholds_out = False
regions_bed_out = False
region_dist_out = False
log = snakemake.log_fmt_shell(stdout=True, stderr=True, append=True)


# Check output files
per_base_out = quantize_out = thresholds_out = global_dist_out = region_dist_out = (
regions_bed_out
) = False
for file in snakemake.output:
if ".per-base." in file and "--no-per-base" in extra:
sys.exit(
"You asked not to generate per-base output (--no-per-base), but your rule specifies a '.per-base.' output file. Remove one of the two."
)
if ".quantized.bed.gz" in file:
quantize_out = True
if ".thresholds.bed.gz" in file:
thresholds_out = True
if ".mosdepth.region.dist.txt" in file:
region_dist_out = True
if ".regions.bed.gz" in file:
regions_bed_out = True


if by and not regions_bed_out:
sys.exit(
"You ask for by-region output. Please also specify *.regions.bed.gz as a rule output."
)
if file.endswith(".per-base.bed.gz"):
per_base_out = file
if file.endswith(".quantized.bed.gz"):
quantize_out = file
if file.endswith(".thresholds.bed.gz"):
thresholds_out = file
if file.endswith(".mosdepth.global.dist.txt"):
global_dist_out = file
if file.endswith(".mosdepth.region.dist.txt"):
region_dist_out = file
if file.endswith(".regions.bed.gz"):
regions_bed_out = file

if by and not region_dist_out:
sys.exit(
"You ask for by-region output. Please also specify *.mosdepth.region.dist.txt as a rule output."
)

if (region_dist_out or regions_bed_out) and not by:
sys.exit(
"You specify *.regions.bed.gz and/or *.mosdepth.region.dist.txt as a rule output. You also need to ask for by-region output via 'input.bed' or 'params.by'."
)
if not per_base_out:
extra += " --no-per-base"

quantize = snakemake.params.get("quantize", "")
if quantize:
if not quantize_out:
sys.exit(
"You ask for quantized output via params.quantize. Please also specify *.quantized.bed.gz as a rule output."
)
quantize = f"--quantize {quantize}"

if not quantize and quantize_out:
sys.exit(
"The rule has output *.quantized.bed.gz specified. Please also specify params.quantize to actually generate it."
)
if quantize_out:
extra += f" --quantize {snakemake.params.quantize}"


thresholds = snakemake.params.get("thresholds", "")
if thresholds:
if not thresholds_out:
sys.exit(
"You ask for --thresholds output via params.thresholds. Please also specify *.thresholds.bed.gz as a rule output."
)
thresholds = f"--thresholds {thresholds}"
if thresholds_out:
extra += f" --thresholds {snakemake.params.thresholds}"

if not thresholds and thresholds_out:
sys.exit(
"The rule has output *.thresholds.bed.gz specified. Please also specify params.thresholds to actually generate it."
)

if region_dist_out or regions_bed_out:
by = snakemake.input.get("bed", snakemake.params.get("by", False))
if by:
extra += f" --by {by}"


precision = snakemake.params.get("precision", "")
Expand All @@ -93,16 +59,38 @@
fasta = f"--fasta {fasta}"


# mosdepth takes additional threads through its option --threads
# One thread for mosdepth
# Other threads are *additional* decompression threads passed to the '--threads' argument
# mosdepth uses additional threads for decompression, passed to the '--threads' argument
threads = "" if snakemake.threads <= 1 else "--threads {}".format(snakemake.threads - 1)


# named output summary = "*.mosdepth.summary.txt" is required
prefix = snakemake.output.summary.replace(".mosdepth.summary.txt", "")
with tempfile.TemporaryDirectory() as tmpdir:
shell(
"{precision} mosdepth {threads} {fasta} {extra} {tmpdir}/temp {snakemake.input.bam} {log}"
)

if snakemake.output.get("summary"):
shell(
"mv --verbose {tmpdir}/temp.mosdepth.summary.txt {snakemake.output.summary} {log}"
)

if per_base_out:
shell("mv --verbose {tmpdir}/temp.per-base.bed.gz {per_base_out} {log}")

if quantize_out:
shell("mv --verbose {tmpdir}/temp.quantized.bed.gz {quantize_out} {log}")

if thresholds_out:
shell("mv --verbose {tmpdir}/temp.thresholds.bed.gz {thresholds_out} {log}")

if global_dist_out:
shell(
"mv --verbose {tmpdir}/temp.mosdepth.global.dist.txt {global_dist_out} {log}"
)

if region_dist_out:
shell(
"mv --verbose {tmpdir}/temp.mosdepth.region.dist.txt {region_dist_out} {log}"
)

shell(
"({precision} mosdepth {threads} {fasta} {by} {quantize} {thresholds} {extra} {prefix} {snakemake.input.bam}) {log}"
)
if regions_bed_out:
shell("mv --verbose {tmpdir}/temp.regions.bed.gz {regions_bed_out} {log}")
52 changes: 0 additions & 52 deletions test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3752,61 +3752,9 @@ def test_mosdepth(run):
"--cores",
"4",
"mosdepth/m54075_180905_225130.ccs.ecoliK12_pbi_March2013.mosdepth.summary.txt",
"--use-conda",
"-F",
],
)


def test_mosdepth_bed(run):
run(
"bio/mosdepth",
[
"snakemake",
"--cores",
"4",
"mosdepth_bed/m54075_180905_225130.ccs.ecoliK12_pbi_March2013.mosdepth.summary.txt",
"--use-conda",
"-F",
],
)


def test_mosdepth_by_threshold(run):
run(
"bio/mosdepth",
[
"snakemake",
"--cores",
"4",
"mosdepth_by_threshold/m54075_180905_225130.ccs.ecoliK12_pbi_March2013.mosdepth.summary.txt",
"--use-conda",
"-F",
],
)


def test_mosdepth_quantize_precision(run):
run(
"bio/mosdepth",
[
"snakemake",
"--cores",
"4",
"mosdepth_quantize_precision/m54075_180905_225130.ccs.ecoliK12_pbi_March2013.mosdepth.summary.txt",
"--use-conda",
"-F",
],
)


def test_mosdepth_cram(run):
run(
"bio/mosdepth",
[
"snakemake",
"--cores",
"4",
"mosdepth_cram/a.mosdepth.summary.txt",
"--use-conda",
"-F",
Expand Down
Loading