comparison 0.5.0/bin/create_mqc_data_table.py @ 1:365849f031fd

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