Mercurial > repos > kkonganti > hfp_nowayout
comparison 0.5.0/bin/create_mqc_data_table.py @ 0:97cd2f532efe
planemo upload
author | kkonganti |
---|---|
date | Mon, 31 Mar 2025 14:50:40 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:97cd2f532efe |
---|---|
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 ( | |
40 os.path.exists(table_sum_on_file) and os.path.getsize(table_sum_on_file) > 0 | |
41 ): | |
42 exit(0) | |
43 | |
44 with open(table_sum_on_file, "r") as tbl: | |
45 header = tbl.readline() | |
46 header_cols = header.strip().split("\t") | |
47 | |
48 html = [ | |
49 dedent( | |
50 f"""<script type="text/javascript"> | |
51 $(document).ready(function () {{ | |
52 $('#cpipes-process-custom-res-{table_sum_on}').DataTable({{ | |
53 scrollX: true, | |
54 fixedColumns: true, dom: 'Bfrtip', | |
55 buttons: [ | |
56 'copy', | |
57 {{ | |
58 extend: 'print', | |
59 title: 'CPIPES: MultiQC Report: {table_sum_on}' | |
60 }}, | |
61 {{ | |
62 extend: 'excel', | |
63 filename: '{table_sum_on}_results', | |
64 }}, | |
65 {{ | |
66 extend: 'csv', | |
67 filename: '{table_sum_on}_results', | |
68 }} | |
69 ] | |
70 }}); | |
71 }}); | |
72 </script> | |
73 <div class="table-responsive"> | |
74 <style> | |
75 #cpipes-process-custom-res tr:nth-child(even) {{ | |
76 background-color: #f2f2f2; | |
77 }} | |
78 </style> | |
79 <table class="table" style="width:100%" id="cpipes-process-custom-res-{table_sum_on}"> | |
80 <thead> | |
81 <tr>""" | |
82 ) | |
83 ] | |
84 | |
85 for header_col in header_cols: | |
86 html.append( | |
87 dedent( | |
88 f""" | |
89 <th> {header_col} </th>""" | |
90 ) | |
91 ) | |
92 | |
93 html.append( | |
94 dedent( | |
95 """ | |
96 </tr> | |
97 </thead> | |
98 <tbody>""" | |
99 ) | |
100 ) | |
101 | |
102 for row in tbl: | |
103 html.append("<tr>\n") | |
104 data_cols = row.strip().split("\t") | |
105 if len(header_cols) != len(data_cols): | |
106 print( | |
107 f"\nWARN: Number of header columns ({len(header_cols)}) and data " | |
108 + f"columns ({len(data_cols)}) are not equal!\nWill append empty columns!\n" | |
109 ) | |
110 if len(header_cols) > len(data_cols): | |
111 data_cols += (len(header_cols) - len(data_cols)) * " " | |
112 print(len(data_cols)) | |
113 else: | |
114 header_cols += (len(data_cols) - len(header_cols)) * " " | |
115 | |
116 html.append( | |
117 dedent( | |
118 f""" | |
119 <td><samp>{data_cols[0]}</samp></td> | |
120 """ | |
121 ) | |
122 ) | |
123 | |
124 for data_col in data_cols[1:]: | |
125 data_col_w_color = f"""<td>{data_col}</td> | |
126 """ | |
127 if ( | |
128 table_sum_on in cell_colors.keys() | |
129 and data_col in cell_colors[table_sum_on].keys() | |
130 ): | |
131 data_col_w_color = f"""<td style="background-color: {cell_colors[table_sum_on][data_col]}">{data_col}</td> | |
132 """ | |
133 html.append(dedent(data_col_w_color)) | |
134 html.append("</tr>\n") | |
135 html.append("</tbody>\n") | |
136 html.append("</table>\n") | |
137 html.append("</div>\n") | |
138 | |
139 mqc_yaml = { | |
140 "id": f"{table_sum_on.upper()}_collated_table", | |
141 "section_name": f"{table_sum_on.upper()}", | |
142 "section_href": f"https://github.com/CFSAN-Biostatistics/nowayout", | |
143 "plot_type": "html", | |
144 "description": f"{description}", | |
145 "data": ("").join(html), | |
146 } | |
147 | |
148 with open(f"{table_sum_on.lower()}_mqc.yml", "w") as html_mqc: | |
149 yaml.dump(mqc_yaml, html_mqc, default_flow_style=False) | |
150 | |
151 | |
152 if __name__ == "__main__": | |
153 main() |