Skip to content

Commit e1699a6

Browse files
committed
Workaround for single-three freebayes issue
1 parent 37706cb commit e1699a6

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

workflow/scripts/freebayes.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,27 @@
1818
# =================================================================================================
1919

2020
from snakemake.shell import shell
21+
import os
2122

2223
shell.executable("bash")
23-
24-
log = snakemake.log_fmt_shell(stdout=True, stderr=True)
24+
log = snakemake.log_fmt_shell(stdout=True, stderr=True, append=True)
2525

2626
extra_params = snakemake.params.get("extra", "")
2727
norm = snakemake.params.get("normalize", False)
2828
assert norm in [True, False]
2929

30+
def local_log(message):
31+
"""
32+
Append a message to the Snakemake log file.
33+
Ensures there's a trailing newline.
34+
"""
35+
log_path = snakemake.log[0]
36+
os.makedirs(os.path.dirname(log_path), exist_ok=True)
37+
if not message.endswith("\n"):
38+
message += "\n"
39+
with open(log_path, "a") as lf:
40+
lf.write(message)
41+
3042
# Additions to the original wrapper made by LC:
3143
#
3244
# We separate by contig, to increase parallel runs on clusters.
@@ -91,6 +103,7 @@
91103
chrom_length = int(fields[1])
92104
if chrom_name == contig:
93105
regions = '<(echo "' + chrom_name + ":0-" + str(chrom_length) + '")'
106+
local_log(f"regions = {regions} (after chromosome)")
94107

95108
# If we are here, we must have found the contig in the fai file,
96109
# otherwise that name would not have appeared in the "{contig}" wildcard of our snakemake rule -
@@ -111,16 +124,22 @@
111124
"{regions}) -b {snakemake.input.regions} | "
112125
r"sed 's/\t\([0-9]*\)\t\([0-9]*\)$/:\1-\2/')"
113126
).format(regions=regions, snakemake=snakemake)
127+
local_log(f"regions = {regions} (after intersection)")
114128
else:
115129
# If there are no regions yet, we have the case that a small contig group was provided.
116130
# In this case, we just parse that file and turn its bed format into the freebayes
117131
# regions format.
118132
regions = ("<(cat {snakemake.input.regions} | sed 's/\\t/:/' | sed 's/\\t/-/')").format(
119133
snakemake=snakemake
120134
)
135+
local_log(f"regions = {regions} (after small contig)")
121136

122-
if snakemake.threads == 1:
137+
# The single threaded cases has some issue with the region bash substitution...
138+
# Just deactivating this for now, and running the parallel case instead.
139+
# if snakemake.threads == 1:
140+
if False:
123141
freebayes = "freebayes --region " + regions
142+
# freebayes = "freebayes --region <(" + regions + ")"
124143
else:
125144
# Ideally, we'd be using bamtools coverage and coverage_to_regions.py here,
126145
# as suggsted in the freebayes-parallel script, but this runs a long time and had some errors
@@ -140,6 +159,7 @@
140159
"{chunks}) | "
141160
r"sed 's/\t\([0-9]*\)\t\([0-9]*\)$/:\1-\2/')"
142161
).format(regions=regions, chunks=chunks)
162+
local_log(f"regions = {regions} (with threads)")
143163
freebayes = ("freebayes-parallel {regions} {snakemake.threads}").format(
144164
snakemake=snakemake, regions=regions
145165
)
@@ -162,6 +182,6 @@
162182
pipe = "| bcftools sort -Ou - " + pipe
163183

164184
shell(
165-
"({freebayes} {extra_params} -f {snakemake.input.ref}"
185+
"set -x; ({freebayes} {extra_params} -f {snakemake.input.ref}"
166186
" --bam-list {snakemake.output.bamlist} {pipe} > {snakemake.output[0]}) {log}"
167187
)

0 commit comments

Comments
 (0)