Skip to content

Commit dfa4670

Browse files
committed
Added option to disable automatic parsing of some parameters, in case the tool does no support it.
1 parent e2909e1 commit dfa4670

File tree

1 file changed

+59
-45
lines changed

1 file changed

+59
-45
lines changed

snakemake_wrapper_utils/bcftools.py

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,89 @@
11
import sys
22

33

4-
def get_bcftools_opts(snakemake):
4+
def get_bcftools_opts(
5+
snakemake,
6+
parse_threads=True,
7+
parse_output_format=True,
8+
parse_memory=True,
9+
parse_temp_dir=True,
10+
):
511
"""Obtain bcftools_opts from output, params, and handle resource definitions in resources."""
612
bcftools_opts = ""
713
extra = snakemake.params.get("extra", "")
814

915
###############
1016
### Threads ###
1117
###############
12-
if "--threads" in extra:
13-
sys.exit(
14-
"You have specified number of threads (`--threads`) in params.extra; please use only `threads`."
18+
if parse_threads:
19+
if "--threads" in extra:
20+
sys.exit(
21+
"You have specified number of threads (`--threads`) in params.extra; please use only `threads`."
22+
)
23+
bcftools_opts += (
24+
""
25+
if snakemake.threads <= 1
26+
else "--threads {}".format(snakemake.threads - 1)
1527
)
16-
bcftools_opts += (
17-
"" if snakemake.threads <= 1 else "--threads {}".format(snakemake.threads - 1)
18-
)
1928

2029
#####################
2130
### Output format ###
2231
#####################
23-
if "-O" in extra or "--output-type" in extra:
24-
sys.exit(
25-
"You have specified output format (`-O/--output-type`) in params.extra; this is automatically infered from output file extension."
26-
)
32+
if parse_output_format:
33+
if "-O" in extra or "--output-type" in extra:
34+
sys.exit(
35+
"You have specified output format (`-O/--output-type`) in params.extra; this is automatically infered from output file extension."
36+
)
2737

28-
# Output uncompressed BCF; ignored if output is VCF
29-
uncompressed_bcf = snakemake.params.get("uncompressed_bcf", False)
38+
# Output uncompressed BCF; ignored if output is VCF
39+
uncompressed_bcf = snakemake.params.get("uncompressed_bcf", False)
3040

31-
output = snakemake.output[0]
32-
if output.endswith(".vcf"):
33-
out_format = "v"
34-
elif output.endswith(".vcf.gz"):
35-
out_format = "z"
36-
elif output.endswith(".bcf"):
37-
if uncompressed_bcf:
38-
out_format = "u"
41+
output = snakemake.output[0]
42+
if output.endswith(".vcf"):
43+
out_format = "v"
44+
elif output.endswith(".vcf.gz"):
45+
out_format = "z"
46+
elif output.endswith(".bcf"):
47+
if uncompressed_bcf:
48+
out_format = "u"
49+
else:
50+
out_format = "b"
3951
else:
40-
out_format = "b"
41-
else:
42-
raise ValueError("invalid output file extension ('.vcf', '.vcf.gz', '.bcf').")
52+
raise ValueError(
53+
"invalid output file extension ('.vcf', '.vcf.gz', '.bcf')."
54+
)
4355

44-
bcftools_opts += f" --output-type {out_format}"
56+
bcftools_opts += f" --output-type {out_format}"
4557

4658
##############
4759
### Memory ###
4860
##############
49-
# Should resources.mem_mb be passed to bcftools (not all tools support this option)
50-
bcftools_use_mem = snakemake.params.get("bcftools_use_mem", False)
61+
if parse_memory:
62+
# Should resources.mem_mb be passed to bcftools (not all tools support this option)
63+
bcftools_use_mem = snakemake.params.get("bcftools_use_mem", False)
5164

52-
if "-m" in extra or "--max-mem" in extra:
53-
sys.exit(
54-
"You have provided `-m/--max-mem` in params.extra; please only use resources.mem_mb and set params.bcftools_use_mem."
55-
)
56-
# Getting memory in megabytes, as advised in documentation.
57-
if "mem_mb" in snakemake.resources.keys() and bcftools_use_mem:
58-
bcftools_opts += " --max-mem {}M".format(snakemake.resources["mem_mb"])
59-
# Getting memory in gigabytes, for user convenience. Please prefer the use
60-
# of mem_mb over mem_gb as advised in documentation.
61-
elif "mem_gb" in snakemake.resources.keys() and bcftools_use_mem:
62-
bcftools_opts += " --max-mem {}G".format(snakemake.resources["mem_gb"])
65+
if "-m" in extra or "--max-mem" in extra:
66+
sys.exit(
67+
"You have provided `-m/--max-mem` in params.extra; please only use resources.mem_mb and set params.bcftools_use_mem."
68+
)
69+
# Getting memory in megabytes, as advised in documentation.
70+
if "mem_mb" in snakemake.resources.keys() and bcftools_use_mem:
71+
bcftools_opts += " --max-mem {}M".format(snakemake.resources["mem_mb"])
72+
# Getting memory in gigabytes, for user convenience. Please prefer the use
73+
# of mem_mb over mem_gb as advised in documentation.
74+
elif "mem_gb" in snakemake.resources.keys() and bcftools_use_mem:
75+
bcftools_opts += " --max-mem {}G".format(snakemake.resources["mem_gb"])
6376

6477
################
6578
### Temp dir ###
6679
################
67-
if "-T" in extra or "--temp-dir" in extra:
68-
sys.exit(
69-
"You have provided `-T/--temp-dir` in params.extra; please provide a temp dir as output.bcftools_temp."
70-
)
71-
# Getting temp directory from output files list
72-
if "bcftools_temp" in snakemake.output.keys():
73-
bcftools_opts += " --temp-dir {}".format(snakemake.output["bcftools_temp"])
80+
if parse_temp_dir:
81+
if "-T" in extra or "--temp-dir" in extra:
82+
sys.exit(
83+
"You have provided `-T/--temp-dir` in params.extra; please provide a temp dir as output.bcftools_temp."
84+
)
85+
# Getting temp directory from output files list
86+
if "bcftools_temp" in snakemake.output.keys():
87+
bcftools_opts += " --temp-dir {}".format(snakemake.output["bcftools_temp"])
7488

7589
return bcftools_opts

0 commit comments

Comments
 (0)