Skip to content

Commit 2d71020

Browse files
author
Adam Talbot
committed
mosdepth missing values imputed
Changes: - Mosdepth output has missing values in *.{region,global}.dist.txt - This change to module fills any missing values with the next value - e.g., if there is 100% at 100X and 80% at 80X, the value at 90X will be recorded as 80X - This may underestimate coverage slightly but it's not clear from MosDepth docs how it should be handled. - See brentp/mosdepth#190
1 parent 45c5408 commit 2d71020

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

multiqc/modules/mosdepth/mosdepth.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,30 @@ def parse_cov_dist(self):
290290
return genstats, cumcov_dist_data, cov_dist_data, xmax, perchrom_avg_data
291291

292292
def genstats_cov_thresholds(self, genstats, genstats_headers, cumcov_dist_data, threshs, hidden_threshs):
293+
def recursive_get_value(d: OrderedDict, t: int) -> int:
294+
"""
295+
If the depth threshold (t) is not in the OrderedDict, iterate up the depth values.
296+
This means the % at the threshold will be estimated slightly lower but prevents zero values.
297+
"""
298+
depths = list(d.keys())
299+
if t in d:
300+
return d[t]
301+
else:
302+
greater_than_t = [x for x in depths if x > t]
303+
if len(greater_than_t) == 0:
304+
# No depths available greater than t
305+
log.debug(f"No values found for threshold {t}, assuming 0%")
306+
return 0
307+
else:
308+
greater_than_t.sort()
309+
return recursive_get_value(d, greater_than_t[0])
310+
293311
for s_name, d in cumcov_dist_data.items():
294-
dist_subset = {t: data for t, data in d.items() if t in threshs}
295312
for t in threshs:
296-
if int(t) in dist_subset:
297-
genstats[s_name][f"{t}_x_pc"] = dist_subset[t]
298-
else:
313+
try:
314+
genstats[s_name][f"{t}_x_pc"] = recursive_get_value(d, int(t))
315+
except KeyError:
316+
# If value doesn't exist, use zero
299317
genstats[s_name][f"{t}_x_pc"] = 0
300318

301319
for t in threshs:

0 commit comments

Comments
 (0)