forked from metagenlab/diag_pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_rgi_html_report.py
311 lines (264 loc) · 10.6 KB
/
generate_rgi_html_report.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
import pandas
import re
rgi_tsv_output = snakemake.input[0]
rgi_ontology = snakemake.input[1]
gene_depth_file = snakemake.input[2]
contig_gc_depth_file = snakemake.input[3]
samtools_depth = snakemake.input[4]
sample = snakemake.params[0]
report_file = snakemake.output[0]
# parse rgi output file
contig2resistances = {}
with open(rgi_tsv_output, 'r') as f:
for row in f:
data = row.rstrip().split('\t')
contig = '_'.join(data[1].split('_')[0:-1])
if contig not in contig2resistances:
contig2resistances[contig] = [re.sub("'", "", data[8])]
else:
contig2resistances[contig].append(re.sub("'", "", data[8]))
rgi_table = pandas.read_csv(rgi_tsv_output,
delimiter='\t',
header=0)
ontology_table = pandas.read_csv(rgi_ontology,
delimiter='\t',
header=0)
# calculate rgi hit(s) sequencing depth based on position of the CDS in contigs
# todo
def parse_smatools_depth(samtools_depth):
import pandas
with open(samtools_depth, 'r') as f:
table = pandas.read_csv(f, sep='\t', header=None, index_col=0)
return table
samtools_dataframe = parse_smatools_depth(samtools_depth)
hit2depth={}
# get contig depth and GC
contig2gc_content = pandas.read_csv(contig_gc_depth_file,
delimiter='\t',
header=0).set_index("contig").to_dict()["gc_content"]
contig2median_depth = pandas.read_csv(contig_gc_depth_file,
delimiter='\t',
header=0).set_index("contig").to_dict()["median_depth"]
contig2size = pandas.read_csv(contig_gc_depth_file,
delimiter='\t',
header=0).set_index("contig").to_dict()["contig_size"]
# prepare bubble plot GC vs Coverage
dataset_template = '''
{
label: '%s',
data: [
%s
],
backgroundColor:"%s",
hoverBackgroundColor: "%s"
}
'''
data_template = '''{
x: %s,
y: %s,
r: Math.log(%s),
l: "%s",
}'''
bubble_template = '''
var data = {
datasets: [
%s,
%s
]
};
var options = {
legend: {
display: false
},
type: 'bubble',
data: data,
options: {
tooltips: {
enabled: true,
callbacks: {
label: function(tooltipItem, data) {
var label = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index].l;
return label;
}
}
},
title:{
display: true,
text:'Depth vs GC plot'
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'GC(%%)'
},
ticks: {
beginAtZero: true,
steps: 10,
stepValue: 10,
max: 100
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Depth'
},
ticks: {
beginAtZero: true,
}
}]
},
// Container for pan options
pan: {
// Boolean to enable panning
enabled: true,
// Panning directions. Remove the appropriate direction to disable
// Eg. 'y' would only allow panning in the y direction
mode: 'xy'
},
// Container for zoom options
zoom: {
// Boolean to enable zooming
enabled: true,
// Zooming directions. Remove the appropriate direction to disable
// Eg. 'y' would only allow zooming in the y direction
mode: 'x',
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
'''
no_resistance_list = []
resistance_list = []
for contig in contig2gc_content:
depth = contig2median_depth[contig]
gc = contig2gc_content[contig]
if contig in contig2resistances:
resistance_list.append(data_template % (gc,
depth,
contig2size[contig], # contig size
"%s (%sbp): %s" % (contig,
contig2size[contig],
','.join(contig2resistances[contig]))))
else:
no_resistance_list.append(data_template % (gc,
depth,
contig2size[contig], # contig size
"%s (%sbp)" % (contig,
contig2size[contig])))
buuble_chart_code = bubble_template % (dataset_template % ('No resistances',
','.join(no_resistance_list),
"rgba(22, 99, 132, 0.2)",
"rgba(22, 99, 132, 0.2)"),
dataset_template % ('Resistances',
','.join(resistance_list),
'#ff6384',
'#ff6384'
))
template = '''
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://github.com/chartjs/Chart.js/releases/download/v2.7.1/Chart.bundle.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-zoom/0.6.5/chartjs-plugin-zoom.js"></script>
</head>
<body style="max-width:95%%; padding-left:55px;">
<h1>Identification of resistance markers</h1>
<h2>GC-coverage plot</h2>
<div style="width:400px;">
<canvas id="chartJSContainer" width="400" height="400"></canvas>
</div>
<h2>Detailed table<h2>
<div id='vftable' style="max-width:100%%; padding-left:0px;">
%s
</div>
</body>
<script type="text/javascript">
%s
$(document).ready(function() {
$('#res_table').DataTable( {
dom: 'Bfrtip',
"pageLength": 10,
"searching": true,
"bLengthChange": false,
"paging": true,
"info": false
} );
} );
</script>
<style>
th { font-size: 12px; }
td { font-size: 11px; }
</style>
</html>
'''
def resistance_table(rgi_table):
header = ["Contig",
"ORF",
"ARO",
"Model Type",
"Variant",
"Cov (%)",
"Identity (%)",
"Score",
"Score cutoff",
"Family",
"Mechanism",
"Resistance"]
table_rows = []
for n, one_resistance in rgi_table.iterrows():
contig = '_'.join(one_resistance["Contig"].split('_')[0:-1])
orf_id = one_resistance["Contig"].split('_')[-1]
cutoff = one_resistance["Cut_Off"]
name = one_resistance["Best_Hit_ARO"]
identity = one_resistance["Best_Identities"]
aro = one_resistance["ARO"]
cov = one_resistance["Percentage Length of Reference Sequence"]
bitscore = one_resistance["Best_Hit_Bitscore"]
pass_bitscore = one_resistance["Pass_Bitscore"]
mechanism = one_resistance["Resistance Mechanism"]
snps = one_resistance["SNPs_in_Best_Hit_ARO"]
model = one_resistance["Model_type"]
family = one_resistance["AMR Gene Family"]
antibio_res_list = list(ontology_table[ontology_table['Name'] == name]["Antibiotic resistance prediction"])
antibio_res_class_list = list(ontology_table[ontology_table['Name'] == name]["Class"])
resistance_code = ''
for resistance, resistance_class in zip(antibio_res_list, antibio_res_class_list):
#print(resistance, resistance_class)
resistance_code += '%s (%s) </br>' % (resistance, resistance_class)
table_rows.append([contig,
orf_id,
"%s (%s)" % (name, aro),
model,
snps,
cov,
identity,
bitscore,
pass_bitscore,
family,
mechanism,
resistance_code])
df = pandas.DataFrame(table_rows, columns=header)
# cell content is truncated if colwidth not set to -1
pandas.set_option('display.max_colwidth', -1)
df_str = df.to_html(
index=False,
bold_rows=False,
classes=["dataTable"],
table_id="res_table",
escape=False,
border=0)
return df_str.replace("\n", "\n" + 10 * " ")
with open(report_file, 'w') as f:
f.write(template % (resistance_table(rgi_table), buuble_chart_code))