forked from nf-core/viralrecon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiqc_to_custom_csv.py
executable file
·379 lines (354 loc) · 13.4 KB
/
multiqc_to_custom_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python
import os
import sys
import errno
import argparse
import yaml
def parse_args(args=None):
Description = "Create custom spreadsheet for pertinent MultiQC metrics generated by the nf-core/viralrecon pipeline."
Epilog = "Example usage: python multiqc_to_custom_tsv.py"
parser = argparse.ArgumentParser(description=Description, epilog=Epilog)
parser.add_argument(
"-pl",
"--platform",
type=str,
dest="PLATFORM",
default="illumina",
help="Sequencing platform for input data. Accepted values = 'illumina' or 'nanopore' (default: 'illumina').",
)
parser.add_argument(
"-md",
"--multiqc_data_dir",
type=str,
dest="MULTIQC_DATA_DIR",
default="multiqc_data",
help="Full path to directory containing YAML files for each module, as generated by MultiQC. (default: 'multiqc_data').",
)
parser.add_argument(
"-op",
"--out_prefix",
type=str,
dest="OUT_PREFIX",
default="summary",
help="Full path to output prefix (default: 'summary').",
)
return parser.parse_args(args)
def make_dir(path):
if not len(path) == 0:
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
# Find key in dictionary created from YAML file recursively
# From https://stackoverflow.com/a/37626981
def find_tag(d, tag):
if tag in d:
yield d[tag]
for k, v in d.items():
if isinstance(v, dict):
for i in find_tag(v, tag):
yield i
def yaml_fields_to_dict(
yaml_file, append_dict={}, field_mapping_list=[], valid_sample_list=[]
):
integer_fields = [
"mapped_passed",
"number_of_SNPs",
"number_of_indels",
"MISSENSE",
"# contigs (>= 0 bp)",
"# contigs (>= 5000 bp)",
"Largest contig",
]
if os.path.exists(yaml_file):
with open(yaml_file) as f:
yaml_dict = yaml.safe_load(f)
for k in yaml_dict.keys():
key = k
if os.path.basename(yaml_file).startswith("multiqc_picard_insertSize"):
if k[-3:] == "_FR":
key = k[:-3]
if os.path.basename(yaml_file).startswith("multiqc_cutadapt"):
names = [x for x in valid_sample_list if key[:-2] == x]
names += [x for x in valid_sample_list if key == x]
if names != []:
key = names[0]
include_sample = True
if len(valid_sample_list) != 0 and key not in valid_sample_list:
include_sample = False
if include_sample:
if key not in append_dict:
append_dict[key] = {}
if field_mapping_list != []:
for i, j in field_mapping_list:
val = list(find_tag(yaml_dict[k], j[0]))
## Fix for Cutadapt reporting reads/pairs as separate values
if j[0] == "r_written" and len(val) == 0:
val = [
list(find_tag(yaml_dict[k], "pairs_written"))[0] * 2
]
if len(val) != 0:
val = val[0]
if len(j) == 2:
val = list(find_tag(val, j[1]))[0]
if j[0] in integer_fields:
val = int(val)
if i not in append_dict[key]:
append_dict[key][i] = val
else:
print(
"WARNING: {} key already exists in dictionary so will be overwritten. YAML file {}.".format(
i, yaml_file
)
)
else:
append_dict[key] = yaml_dict[k]
else:
print("WARNING: File does not exist: {}".format(yaml_file))
if len(valid_sample_list) != 0:
for key in valid_sample_list:
if key not in append_dict:
append_dict[key] = {}
if field_mapping_list != []:
for i, j in field_mapping_list:
if i not in append_dict[key]:
append_dict[key][i] = "NA"
else:
print(
"WARNING: {} key already exists in dictionary so will be overwritten. YAML file {}.".format(
i, yaml_file
)
)
else:
append_dict[key] = "NA"
return append_dict
def metrics_dict_to_file(
file_field_list, multiqc_data_dir, out_file, valid_sample_list=[]
):
metrics_dict = {}
field_list = []
for yaml_file, mapping_list in file_field_list:
yaml_file = os.path.join(multiqc_data_dir, yaml_file)
metrics_dict = yaml_fields_to_dict(
yaml_file=yaml_file,
append_dict=metrics_dict,
field_mapping_list=mapping_list,
valid_sample_list=valid_sample_list,
)
field_list += [x[0] for x in mapping_list]
if metrics_dict != {}:
make_dir(os.path.dirname(out_file))
fout = open(out_file, "w")
header = ["Sample"] + field_list
fout.write("{}\n".format(",".join(header)))
for k in sorted(metrics_dict.keys()):
row_list = [k]
for field in field_list:
if field in metrics_dict[k]:
if metrics_dict[k][field]:
row_list.append(str(metrics_dict[k][field]).replace(',', ';'))
else:
row_list.append("NA")
else:
row_list.append("NA")
fout.write("{}\n".format(",".join(row_list)))
fout.close()
return metrics_dict
def main(args=None):
args = parse_args(args)
## File names for MultiQC YAML along with fields to fetch from each file
illumina_variant_files = [
(
"multiqc_fastp.yaml",
[
("# Input reads", ["before_filtering", "total_reads"]),
("# Trimmed reads (fastp)", ["after_filtering", "total_reads"]),
],
),
(
"multiqc_general_stats.yaml",
[
(
"% Non-host reads (Kraken 2)",
[
"PREPROCESS: Kraken 2_mqc-generalstats-preprocess_kraken_2-Unclassified"
],
)
],
),
("multiqc_bowtie2.yaml", [("% Mapped reads", ["overall_alignment_rate"])]),
(
"multiqc_samtools_flagstat_samtools_bowtie2.yaml",
[("# Mapped reads", ["mapped_passed"])],
),
(
"multiqc_samtools_flagstat_samtools_ivar.yaml",
[("# Trimmed reads (iVar)", ["flagstat_total"])],
),
(
"multiqc_general_stats.yaml",
[
(
"Coverage median",
[
"VARIANTS: mosdepth_mqc-generalstats-variants_mosdepth-median_coverage"
],
),
(
"% Coverage > 1x",
["VARIANTS: mosdepth_mqc-generalstats-variants_mosdepth-1_x_pc"],
),
(
"% Coverage > 10x",
["VARIANTS: mosdepth_mqc-generalstats-variants_mosdepth-10_x_pc"],
),
],
),
(
"multiqc_bcftools_stats.yaml",
[
("# SNPs", ["number_of_SNPs"]),
("# INDELs", ["number_of_indels"]),
],
),
(
"multiqc_snpeff.yaml",
[("# Missense variants", ["MISSENSE"])],
),
(
"multiqc_quast_quast_variants.yaml",
[("# Ns per 100kb consensus", ["# N's per 100 kbp"])],
),
(
"multiqc_pangolin.yaml",
[("Pangolin lineage", ["lineage"])],
),
("multiqc_nextclade_clade-plot.yaml", [("Nextclade clade", ["clade"])]),
]
illumina_assembly_files = [
(
"multiqc_fastp.yaml",
[("# Input reads", ["before_filtering", "total_reads"])],
),
("multiqc_cutadapt.yaml", [("# Trimmed reads (Cutadapt)", ["r_written"])]),
(
"multiqc_general_stats.yaml",
[
(
"% Non-host reads (Kraken 2)",
[
"PREPROCESS: Kraken 2_mqc-generalstats-preprocess_kraken_2-Unclassified"
],
)
],
),
(
"multiqc_quast_quast_spades.yaml",
[
("# Contigs (SPAdes)", ["# contigs (>= 0 bp)"]),
("Largest contig (SPAdes)", ["Largest contig"]),
("% Genome fraction (SPAdes)", ["Genome fraction (%)"]),
("N50 (SPAdes)", ["N50"]),
],
),
(
"multiqc_quast_quast_unicycler.yaml",
[
("# Contigs (Unicycler)", ["# contigs (>= 0 bp)"]),
("Largest contig (Unicycler)", ["Largest contig"]),
("% Genome fraction (Unicycler)", ["Genome fraction (%)"]),
("N50 (Unicycler)", ["N50"]),
],
),
(
"multiqc_quast_quast_minia.yaml",
[
("# Contigs (minia)", ["# contigs (>= 0 bp)"]),
("Largest contig (minia)", ["Largest contig"]),
("% Genome fraction (minia)", ["Genome fraction (%)"]),
("N50 (minia)", ["N50"]),
],
),
]
nanopore_variant_files = [
("multiqc_samtools_flagstat.yaml", [("# Mapped reads", ["mapped_passed"])]),
(
"multiqc_general_stats.yaml",
[
(
"Coverage median",
["mosdepth_mqc-generalstats-mosdepth-median_coverage"],
),
("% Coverage > 1x", ["mosdepth_mqc-generalstats-mosdepth-1_x_pc"]),
("% Coverage > 10x", ["mosdepth_mqc-generalstats-mosdepth-10_x_pc"]),
],
),
(
"multiqc_bcftools_stats.yaml",
[("# SNPs", ["number_of_SNPs"]), ("# INDELs", ["number_of_indels"])],
),
("multiqc_snpeff.yaml", [("# Missense variants", ["MISSENSE"])]),
("multiqc_quast.yaml", [("# Ns per 100kb consensus", ["# N's per 100 kbp"])]),
("multiqc_pangolin.yaml", [("Pangolin lineage", ["lineage"])]),
("multiqc_nextclade_clade-plot.yaml", [("Nextclade clade", ["clade"])]),
]
if args.PLATFORM == "illumina":
## Dictionary of samples being single-end/paired-end
is_pe_dict = {}
yaml_file = os.path.join(args.MULTIQC_DATA_DIR, "multiqc_fastp.yaml")
if os.path.exists(yaml_file):
metrics_dict = yaml_fields_to_dict(
yaml_file=yaml_file,
append_dict={},
field_mapping_list=[("command", ["command"])],
valid_sample_list=[],
)
for sample, val in metrics_dict.items():
if metrics_dict[sample]["command"].find("--out2") != -1:
is_pe_dict[sample] = True
else:
is_pe_dict[sample] = False
## Write variant calling metrics to file
metrics_dict_to_file(
file_field_list=illumina_variant_files,
multiqc_data_dir=args.MULTIQC_DATA_DIR,
out_file=args.OUT_PREFIX + "_variants_metrics_mqc.csv",
valid_sample_list=is_pe_dict.keys(),
)
## Write de novo assembly metrics to file
metrics_dict_to_file(
file_field_list=illumina_assembly_files,
multiqc_data_dir=args.MULTIQC_DATA_DIR,
out_file=args.OUT_PREFIX + "_assembly_metrics_mqc.csv",
valid_sample_list=is_pe_dict.keys(),
)
elif args.PLATFORM == "nanopore":
## List of real samples to output in report
sample_list = []
yaml_file = os.path.join(
args.MULTIQC_DATA_DIR, "multiqc_samtools_flagstat.yaml"
)
if os.path.exists(yaml_file):
metrics_dict = yaml_fields_to_dict(
yaml_file=yaml_file,
append_dict={},
field_mapping_list=[("# Mapped reads", ["mapped_passed"])],
valid_sample_list=[],
)
sample_list = metrics_dict.keys()
metrics_dict_to_file(
file_field_list=nanopore_variant_files,
multiqc_data_dir=args.MULTIQC_DATA_DIR,
out_file=args.OUT_PREFIX + "_variants_metrics_mqc.csv",
valid_sample_list=sample_list,
)
else:
print(
"Unrecognised option passed to --platform: {}. Accepted values = 'illumina' or 'nanopore'".format(
args.PLATFORM
)
)
sys.exit(1)
if __name__ == "__main__":
sys.exit(main())