comparison rps2tree_html.py @ 0:e889010415a1 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit 3a3b40c15ae5e82334f016e88b1f3c5bbbb3b2cd
author iuc
date Mon, 04 Mar 2024 19:55:52 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e889010415a1
1 #!/usr/bin/env python3
2 import argparse
3 import csv
4 import logging
5 import sys
6
7
8 logging.basicConfig(level=logging.INFO)
9 logger = logging.getLogger(__name__)
10
11
12 def main():
13 options = _set_options()
14 data, headers = _read_map_file(options.map)
15 html = _print_html(data, headers, options.out)
16 index_file = options.out + '/index.html'
17 fh = open(index_file, mode='w')
18 fh.write(html)
19 fh.close()
20
21
22 def _get_google_script_headers(data, headers, out_dir):
23 html = '<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>' + "\n"
24 html += '<script type="text/javascript">' + "\n"
25 html += 'google.charts.load(\'current\', {\'packages\':[\'table\']});' + "\n"
26 chart_names, java_scripts = _get_google_js(data, headers, out_dir)
27 for i in range(0, len(chart_names)):
28 html += 'google.charts.setOnLoadCallback(' + chart_names[i].replace('-', '_') + ');' + "\n"
29 html += 'function ' + chart_names[i].replace('-', '_') + '() {' + "\n"
30 html += java_scripts[i] + "\n"
31 html += '}' + "\n"
32 html += '</script>' + "\n"
33 return html
34
35
36 def _get_google_js(data, headers, out_dir):
37 java_scripts = []
38 chart_names = []
39 for cdd in data:
40 chart_names.append(cdd['cdd_id'] + '_' + cdd['description'])
41 js = 'var data = new google.visualization.DataTable();' + "\n"
42 mat, head = _parse_csv(out_dir + '/' + cdd['cluster_nb_reads_files'])
43 for el in head:
44 if el == '#OTU_name':
45 js += 'data.addColumn(\'string\', \'' + el + '\');' + "\n"
46 elif el == 'taxonomy':
47 js += 'data.addColumn(\'string\', \'' + el + '\');' + "\n"
48 elif el == 'contigs_list' or el == 'seq_list':
49 js += 'data.addColumn(\'string\', \'' + el + '\');' + "\n"
50 else:
51 js += 'data.addColumn(\'number\', \'' + el + '\');' + "\n"
52 js += 'data.addRows([' + "\n"
53 for j in range(0, len(mat)):
54 js += '[\'' + mat[j][head[0]] + '\''
55 for i in range(1, len(head) - 2):
56 js += ',' + mat[j][head[i]]
57 js += ',\'' + mat[j][head[len(head) - 2]] + '\''
58 js += ',\'' + mat[j][head[len(head) - 1]] + '\''
59 js += ']'
60 if j != (len(mat) - 1):
61 js += ','
62 js += "\n"
63 js += ']);' + "\n"
64 js += 'var table = new google.visualization.Table(document.getElementById(\'' + (cdd['cdd_id'] + '_' + cdd['description']).replace('-', '_') + '_div' + '\'));' + "\n"
65 js += 'table.draw(data, {showRowNumber: false, width: \'70%\', height: \'70%\'});' + "\n"
66 java_scripts.append(js)
67 return chart_names, java_scripts
68
69
70 def _parse_csv(file):
71 fh = open(file)
72 reader = csv.reader(fh, delimiter="\t")
73 data = list(reader)
74 headers = data[0]
75 matrix = []
76 for i in range(1, len(data)):
77 dict = {}
78 for j in range(0, len(data[i])):
79 if data[i][j] == '':
80 dict[headers[j]] = None
81 elif data[i][j] == 'null':
82 dict[headers[j]] = None
83 else:
84 dict[headers[j]] = data[i][j]
85 matrix.append(dict)
86 return matrix, headers
87
88
89 def _print_html(data, headers, out_dir):
90 html = '<html>' + "\n"
91 html += '<head>' + "\n"
92 html += '<title>' + 'rps2tree' + '</title>'
93 html += _get_google_script_headers(data, headers, out_dir)
94 html += '</head>' + "\n"
95 html += '<div style="text-align:center">' + "\n"
96 html += '<h1 align=center>rps2tree</h1>' + "\n"
97 html += '<body>' + "\n"
98 html += _print_data(data, headers)
99 html += '</body>' + "\n"
100 html += '</div>' + "\n"
101 html += '</html>' + "\n"
102 return html
103
104
105 def _print_data(data, headers):
106 html = ''
107 for cdd in data:
108 html += '<h2>' + cdd['cdd_id'] + ' ' + cdd['description'] + '</h2>' + "\n"
109 html += '<p>' + cdd['full_description'] + '</br>' + '</p>' + "\n"
110 html += '<div id="' + (cdd['cdd_id'] + '_' + cdd['description']).replace('-', '_') + '_div' + '"></div>' + "\n"
111 html += '</br>' + "\n"
112 html += '</br>' + "\n"
113 html += '<img src=' + cdd['tree_files'] + ' href="' + cdd['tree_files'] + '">' + "\n"
114 html += '</br>' + "\n"
115 html += '<a href="' + cdd['align_files'] + '">' + cdd['align_files'] + '</a>' + "\n"
116 html += '</br>' + "\n"
117 html += '<a href="' + cdd['cluster_files'] + '">' + cdd['cluster_files'] + '</a>' + "\n"
118 html += '</br>' + "\n"
119 html += '<a href="' + cdd['cluster_nb_reads_files'] + '">' + cdd['cluster_nb_reads_files'] + '</a>' + "\n"
120 html += '</br>' + "\n"
121 html += '<a href="' + cdd['pairwise_files'] + '">' + cdd['pairwise_files'] + '</a>' + "\n"
122 html += '</br>' + "\n"
123 html += '</br>' + "\n"
124 html += '<hr>' + "\n"
125 return html
126
127
128 def _read_map_file(file):
129 reader = csv.reader(file, delimiter="\t")
130 data = list(reader)
131 headers = data[0]
132 headers[0] = headers[0][1:]
133 map_obj = []
134 for i in range(1, len(data)):
135 dict = {}
136 if len(data[i]) != len(headers):
137 sys.exit('line and headers not the same length.')
138 for j in range(0, len(headers)):
139 dict[headers[j]] = data[i][j]
140 map_obj.append(dict)
141 return map_obj, headers
142
143
144 def _set_options():
145 parser = argparse.ArgumentParser()
146 parser.add_argument('-m', '--map', help='The map file produced by rps2tree.pl script.', action='store', type=argparse.FileType('r'), required=True)
147 parser.add_argument('-o', '--out', help='The title for the HTML page.', action='store', type=str, default='./')
148 args = parser.parse_args()
149 return args
150
151
152 if __name__ == "__main__":
153 main()