Skip to content

Commit 5df1b60

Browse files
Merge pull request #4 from fgvieira/bcftools
Added bcftools utils to parse threads, output format, memory and temp dir
2 parents a4d55b5 + dfa4670 commit 5df1b60

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

snakemake_wrapper_utils/bcftools.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import sys
2+
3+
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+
):
11+
"""Obtain bcftools_opts from output, params, and handle resource definitions in resources."""
12+
bcftools_opts = ""
13+
extra = snakemake.params.get("extra", "")
14+
15+
###############
16+
### Threads ###
17+
###############
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)
27+
)
28+
29+
#####################
30+
### Output format ###
31+
#####################
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+
)
37+
38+
# Output uncompressed BCF; ignored if output is VCF
39+
uncompressed_bcf = snakemake.params.get("uncompressed_bcf", False)
40+
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"
51+
else:
52+
raise ValueError(
53+
"invalid output file extension ('.vcf', '.vcf.gz', '.bcf')."
54+
)
55+
56+
bcftools_opts += f" --output-type {out_format}"
57+
58+
##############
59+
### Memory ###
60+
##############
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)
64+
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"])
76+
77+
################
78+
### Temp dir ###
79+
################
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"])
88+
89+
return bcftools_opts

0 commit comments

Comments
 (0)