|
1 | 1 | import sys
|
2 | 2 |
|
3 | 3 |
|
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 | +): |
5 | 11 | """Obtain bcftools_opts from output, params, and handle resource definitions in resources."""
|
6 | 12 | bcftools_opts = ""
|
7 | 13 | extra = snakemake.params.get("extra", "")
|
8 | 14 |
|
9 | 15 | ###############
|
10 | 16 | ### Threads ###
|
11 | 17 | ###############
|
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) |
15 | 27 | )
|
16 |
| - bcftools_opts += ( |
17 |
| - "" if snakemake.threads <= 1 else "--threads {}".format(snakemake.threads - 1) |
18 |
| - ) |
19 | 28 |
|
20 | 29 | #####################
|
21 | 30 | ### Output format ###
|
22 | 31 | #####################
|
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 | + ) |
27 | 37 |
|
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) |
30 | 40 |
|
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" |
39 | 51 | 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 | + ) |
43 | 55 |
|
44 |
| - bcftools_opts += f" --output-type {out_format}" |
| 56 | + bcftools_opts += f" --output-type {out_format}" |
45 | 57 |
|
46 | 58 | ##############
|
47 | 59 | ### Memory ###
|
48 | 60 | ##############
|
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) |
51 | 64 |
|
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"]) |
63 | 76 |
|
64 | 77 | ################
|
65 | 78 | ### Temp dir ###
|
66 | 79 | ################
|
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"]) |
74 | 88 |
|
75 | 89 | return bcftools_opts
|
0 commit comments