Skip to content

Commit 0adfa6f

Browse files
committed
Snakemake 8+ support
1 parent b30ed99 commit 0adfa6f

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

microhapulator/cli/pipe.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
from os import cpu_count, symlink
1919
from pathlib import Path
2020
from shutil import copy
21-
from snakemake import snakemake
21+
from snakemake.api import SnakemakeApi as smk_api
22+
from snakemake.exceptions import WorkflowError
23+
from snakemake.settings import types as smk_types
2224
import sys
2325

2426

@@ -29,10 +31,10 @@ def main(args):
2931
config = dict(
3032
samples=samples,
3133
readfiles=workingfiles,
32-
mhrefr=Path(args.markerrefr).resolve(),
33-
mhdefn=Path(args.markerdefn).resolve(),
34-
hg38path=Path(args.hg38).resolve(),
35-
hg38index=Path(args.hg38idx).resolve(),
34+
mhrefr=str(Path(args.markerrefr).resolve()),
35+
mhdefn=str(Path(args.markerdefn).resolve()),
36+
hg38path=str(Path(args.hg38).resolve()),
37+
hg38index=str(Path(args.hg38idx).resolve()),
3638
thresh_static=args.static,
3739
thresh_dynamic=args.dynamic,
3840
thresh_file=args.config,
@@ -43,19 +45,28 @@ def main(args):
4345
thresh_gap_alert=args.gap_alert,
4446
hspace=args.hspace,
4547
)
46-
snakefile = files("microhapulator") / "workflows" / "analysis.smk"
47-
success = snakemake(
48-
snakefile,
49-
cores=args.threads,
50-
printshellcmds=True,
51-
dryrun=args.dryrun,
52-
config=config,
53-
workdir=args.workdir,
54-
)
55-
if not success:
48+
try:
49+
run_snakemake(config, args.workdir, cores=args.threads, dryrun=args.dryrun)
50+
return 0
51+
except WorkflowError:
5652
return 1
5753

5854

55+
def run_snakemake(config, workdir, cores=1, dryrun=False):
56+
outcfg = smk_types.OutputSettings(printshellcmds=True)
57+
with smk_api(outcfg) as smk:
58+
workflow = smk.workflow(
59+
config_settings=smk_types.ConfigSettings(config=config),
60+
storage_settings=smk_types.StorageSettings(),
61+
resource_settings=smk_types.ResourceSettings(cores=cores),
62+
snakefile=files("microhapulator") / "workflows" / "analysis.smk",
63+
workdir=Path(workdir),
64+
)
65+
dag = workflow.dag(smk_types.DAGSettings(targets=["report"]))
66+
mode = "dryrun" if dryrun else "local"
67+
dag.execute_workflow(executor=mode)
68+
69+
5970
def validate_panel_config(markerseqs, markerdefn):
6071
print("[MicroHapulator] validating panel configuration", file=sys.stderr)
6172
index = MicrohapIndex.from_files(markerdefn, markerseqs)

microhapulator/workflows/analysis.smk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ rule apply_filters:
211211
params:
212212
static=f"--static {config['thresh_static']}",
213213
dynamic=f"--dynamic {config['thresh_dynamic']}",
214-
threshfile="" if config["thresh_file"] == "" else f"--config {config['thresh_file']}",
214+
threshfile=(
215+
"" if config["thresh_file"] in ("", None) else f"--config {config['thresh_file']}"
216+
),
215217
shell:
216218
"mhpl8r filter {input} --out {output} {params.static} {params.dynamic} {params.threshfile}"
217219

@@ -270,7 +272,7 @@ rule plot_haplotype_calls:
270272
result = TypingResult(fromfile=input.result)
271273
mhapi.plot_haplotype_calls(
272274
result,
273-
"analysis/{}/03typing/callplots".format(wildcards.sample),
275+
f"analysis/{wildcards.sample}/03typing/callplots",
274276
sample=wildcards.sample,
275277
)
276278

microhapulator/workflows/preproc-paired.smk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ rule fastqc:
4040
outfiles = sorted(Path(params.outdir).glob("*.html"))
4141
for end, outfile in enumerate(outfiles, 1):
4242
outfile = Path(outfile)
43-
# Snakemake f-strings break with Python 3.12: https://github.com/snakemake/snakemake/issues/2648
44-
linkfile = "{}/R{}-fastqc.html".format(params.outdir, end)
43+
linkfile = f"{params.outdir}/R{end}-fastqc.html"
4544
symlink(outfile.name, linkfile)
4645

4746

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"pulp==2.3.1",
5656
"scipy>=1.7",
5757
"seaborn>=0.13.2",
58-
"snakemake>=7.15.2,<8.0",
58+
"snakemake>=8",
5959
"termgraph>=0.5",
6060
"tqdm>=4.0",
6161
],

0 commit comments

Comments
 (0)