comparison 0.7.0/bin/create_mqc_data_table.py @ 17:0e7a0053e4a6

planemo upload
author kkonganti
date Mon, 15 Jul 2024 10:42:02 -0400
parents
children
comparison
equal deleted inserted replaced
16:b90e5a7a3d4f 17:0e7a0053e4a6
1 #!/usr/bin/env python
2
3 import os
4 import sys
5 from textwrap import dedent
6
7 import yaml
8
9
10 def main():
11 """
12 Takes a tab-delimited text file with a mandatory header
13 column and generates an HTML table.
14 """
15
16 args = sys.argv
17 if len(args) < 2 or len(args) >= 4:
18 print(
19 f"\nAt least one argument specifying the *.tblsum file is required.\n"
20 + "No more than 2 command-line arguments should be passed.\n"
21 )
22 exit(1)
23
24 table_sum_on = str(args[1]).lower()
25 table_sum_on_file = table_sum_on + ".tblsum.txt"
26 cell_colors = f"{table_sum_on}.cellcolors.yml"
27
28 if len(args) == 3:
29 description = str(args[2])
30 else:
31 description = "The results table shown here is a collection from all samples."
32
33 if os.path.exists(cell_colors) and os.path.getsize(cell_colors) > 0:
34 with open(cell_colors, "r") as cc_yml:
35 cell_colors = yaml.safe_load(cc_yml)
36 else:
37 cell_colors = dict()
38
39 if not (os.path.exists(table_sum_on_file) and os.path.getsize(table_sum_on_file) > 0):
40 exit(0)
41
42 with open(table_sum_on_file, "r") as tbl:
43 header = tbl.readline()
44 header_cols = header.strip().split("\t")
45
46 html = [
47 dedent(
48 f"""<script type="text/javascript">
49 $(document).ready(function () {{
50 $('#cpipes-process-custom-res-{table_sum_on}').DataTable({{
51 scrollX: true,
52 fixedColumns: true, dom: 'Bfrtip',
53 buttons: [
54 'copy',
55 {{
56 extend: 'print',
57 title: 'CPIPES: MultiQC Report: {table_sum_on}'
58 }},
59 {{
60 extend: 'excel',
61 filename: '{table_sum_on}_results',
62 }},
63 {{
64 extend: 'csv',
65 filename: '{table_sum_on}_results',
66 }}
67 ]
68 }});
69 }});
70 </script>
71 <div class="table-responsive">
72 <style>
73 #cpipes-process-custom-res tr:nth-child(even) {{
74 background-color: #f2f2f2;
75 }}
76 </style>
77 <table class="table" style="width:100%" id="cpipes-process-custom-res-{table_sum_on}">
78 <thead>
79 <tr>"""
80 )
81 ]
82
83 for header_col in header_cols:
84 html.append(
85 dedent(
86 f"""
87 <th> {header_col} </th>"""
88 )
89 )
90
91 html.append(
92 dedent(
93 """
94 </tr>
95 </thead>
96 <tbody>"""
97 )
98 )
99
100 for row in tbl:
101 html.append("<tr>\n")
102 data_cols = row.strip().split("\t")
103 if len(header_cols) != len(data_cols):
104 print(
105 f"\nWARN: Number of header columns ({len(header_cols)}) and data "
106 + f"columns ({len(data_cols)}) are not equal!\nWill append empty columns!\n"
107 )
108 if len(header_cols) > len(data_cols):
109 data_cols += (len(header_cols) - len(data_cols)) * " "
110 print(len(data_cols))
111 else:
112 header_cols += (len(data_cols) - len(header_cols)) * " "
113
114 html.append(
115 dedent(
116 f"""
117 <td><samp>{data_cols[0]}</samp></td>
118 """
119 )
120 )
121
122 for data_col in data_cols[1:]:
123 data_col_w_color = f"""<td>{data_col}</td>
124 """
125 if (
126 table_sum_on in cell_colors.keys()
127 and data_col in cell_colors[table_sum_on].keys()
128 ):
129 data_col_w_color = f"""<td style="background-color: {cell_colors[table_sum_on][data_col]}">{data_col}</td>
130 """
131 html.append(dedent(data_col_w_color))
132 html.append("</tr>\n")
133 html.append("</tbody>\n")
134 html.append("</table>\n")
135 html.append("</div>\n")
136
137 mqc_yaml = {
138 "id": f"{table_sum_on.upper()}_collated_table",
139 "section_name": f"{table_sum_on.upper()}",
140 "section_href": f"https://github.com/CFSAN-Biostatistics/bettercallsal",
141 "plot_type": "html",
142 "description": f"{description}",
143 "data": ("").join(html),
144 }
145
146 with open(f"{table_sum_on.lower()}_mqc.yml", "w") as html_mqc:
147 yaml.dump(mqc_yaml, html_mqc, default_flow_style=False)
148
149
150 if __name__ == "__main__":
151 main()