-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGenerate_PRS_report.py
executable file
·508 lines (419 loc) · 16 KB
/
Generate_PRS_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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/env python
# coding: utf-8
# GOAL: make HTML report to summarize output of GO enrichment after stratification based on PRS, contrasting against a Monte Carlo simulated null
# REQUIRES: topGO_10K_raw_qvals.txt, topGO_10K_sample_dict.txt, and topGO_10K_gene_table.txt to be in working directory
# USAGE: Generate_PRS_report.py [-o report filename] [-p file trio prefix] [-t title inside report ]
import argparse
import json
import re
import numpy as np
import pandas as pd
import plotly.graph_objs as go
import plotly.plotly as py
from plotly.offline import download_plotlyjs
from plotly.offline import init_notebook_mode
from plotly.offline import iplot
from plotly.offline import plot
parser = argparse.ArgumentParser(
description="generate HTML report from simulated and observed GO enrichment"
)
parser.add_argument(
"-o", "--output", dest="report", help="name of report to write to file"
)
parser.add_argument(
"-p", "--prefix", dest="prefix", help="file trio (data, genes, samples) prefix"
)
parser.add_argument(
"-t", "--title", dest="report_title", help="title for head of report"
)
args = parser.parse_args()
## load data and reformat as necessary
# load results from simulation
null_qvals = pd.read_table("./MC_EUR_data/topGO_10K_raw_qvals.txt")
null_sample_dict = pd.read_table("./MC_EUR_data/topGO_10K_sample_dict.txt")
null_gene_dict = pd.read_table("./MC_EUR_data/topGO_10K_gene_table.txt")
# load results from PRS stratification
# convert file prefix to file names
observed_data_fh = args.prefix + "_data.txt"
observed_genes_fh = args.prefix + "_gene_list.txt"
observed_samples_fh = args.prefix + "_sample_list.txt"
# stats from topGO
observed_df = pd.read_table(observed_data_fh)
observed_df.columns = [
"GO_term",
"Name",
"Annotated",
"Significant",
"Expected",
"p_val",
"q_val",
]
# gene list used in topGO
observed_genes = pd.read_table(observed_genes_fh)
observed_genes.columns = ["observed"]
observed_genes = observed_genes.drop_duplicates()
# samples that have those genes
observed_samples = pd.read_table(observed_samples_fh)
observed_samples.columns = ["observed"]
# -log 10 conversion
nlog_null_qvals = null_qvals.apply(lambda x: -np.log(x), axis=0)
observed_df["nlog_q_val"] = -np.log(observed_df.q_val)
observed_df["nlog_p_val"] = -np.log(observed_df.p_val)
# build dict of GO term to name
go_dict = pd.Series(observed_df.Name.values, index=observed_df.GO_term).to_dict()
## new data frame for scatter plot
scatter_df = observed_df.loc[observed_df.q_val <= 0.05]
# get matching simulated values and reorder to match the observed values
null_scatter_df = nlog_null_qvals.loc[
:, nlog_null_qvals.columns.isin(list(scatter_df.GO_term))
]
null_scatter_df = null_scatter_df[scatter_df.GO_term]
# build a dict of name to GO term
name_dict = pd.Series(scatter_df.GO_term.values, index=scatter_df.Name).to_dict()
## plot mean and SD of null vs PRS
trace0 = go.Scatter(
x=list(range(1, scatter_df.shape[0] + 1)),
y=null_scatter_df.mean(),
marker={"color": "blue"},
mode="markers",
name="Simulated Null",
error_y=dict(array=null_scatter_df.std() * 2, visible=True),
text=scatter_df.Name,
)
trace1 = go.Scatter(
x=list(range(1, observed_df.shape[0] + 1)),
y=scatter_df.nlog_q_val,
marker={"color": "red"},
mode="markers",
name="PRS Stratification",
text=scatter_df.GO_term,
)
data = [trace0, trace1]
layout = dict(
title="PRS stratification vs proband ascertainment bias",
yaxis=dict(title="-log Q-value (mean and 2 SDs)"),
xaxis=dict(title="GO Term", ticks="", showticklabels=False),
)
fig = dict(data=data, layout=layout)
stick_plot_div = plot(fig, validate=False, include_plotlyjs=False, output_type="div")
## plot fold enrichment as a function of -log p-val
# color by q-value
trace0 = go.Scatter(
x=observed_df.loc[observed_df.q_val > 0.05].nlog_p_val,
y=observed_df.loc[observed_df.q_val > 0.05].Significant
/ observed_df.loc[observed_df.q_val > 0.05].Expected,
marker={"color": "magenta"},
mode="markers",
name="q > 0.05",
text=observed_df.loc[observed_df.q_val > 0.05].Name,
)
trace1 = go.Scatter(
x=observed_df.loc[observed_df.q_val < 0.05].nlog_p_val,
y=observed_df.loc[observed_df.q_val < 0.05].Significant
/ observed_df.loc[observed_df.q_val < 0.05].Expected,
marker={"color": "green"},
mode="markers",
name="q < 0.05",
text=observed_df.loc[observed_df.q_val < 0.05].Name,
)
data = [trace0, trace1]
layout = dict(
title="Fold enrichment vs -log(p-value)",
yaxis=dict(title="Fold enrichment (observed/expected)"),
xaxis=dict(title="-log(p-value)"),
hovermode="closest",
)
fig = dict(data=data, layout=layout)
enrichment_by_pval_div = plot(
fig, validate=False, include_plotlyjs=False, output_type="div"
)
## plot fold enrichment as a function of number of genes in GO term
# color by q-value
trace0 = go.Scatter(
x=observed_df.loc[observed_df.q_val > 0.05].Annotated,
y=observed_df.loc[observed_df.q_val > 0.05].Significant
/ observed_df.loc[observed_df.q_val > 0.05].Expected,
marker={"color": "orange"},
mode="markers",
name="q > 0.05",
text=observed_df.loc[observed_df.q_val > 0.05].Name,
)
trace1 = go.Scatter(
x=observed_df.loc[observed_df.q_val < 0.05].Annotated,
y=observed_df.loc[observed_df.q_val < 0.05].Significant
/ observed_df.loc[observed_df.q_val < 0.05].Expected,
marker={"color": "purple"},
mode="markers",
name="q < 0.05",
text=observed_df.loc[observed_df.q_val < 0.05].Name,
)
data = [trace0, trace1]
layout = dict(
title="Fold enrichment vs GO term size",
yaxis=dict(title="Fold enrichment (observed/expected)"),
xaxis=dict(title="Number of Genes in GO term"),
hovermode="closest",
)
fig = dict(data=data, layout=layout)
enrichment_by_size_div = plot(
fig, validate=False, include_plotlyjs=False, output_type="div"
)
# master dict to hold all data relvant to each GO term
master_GO_dict = {}
# list of GO names for dropdown menus later
names = []
all_go_terms = scatter_df.GO_term
# all_go_terms = ["GO:0086010"]
for test_go_term in all_go_terms:
# start dictionary with basic info
GO = {}
GO["go_id"] = test_go_term
GO["name"] = go_dict[test_go_term] # lookup from prior dict
# save list of names for dropdown menus later
names.append(go_dict[test_go_term])
## plotting histogram of null q-values
GO["hist_vals"] = list(nlog_null_qvals[test_go_term].values)
GO["hist_line"] = observed_df.loc[
observed_df.GO_term == test_go_term
].nlog_q_val.values[0]
## Explore simulations with test statistics more extreme than observed statistic
# observed value
test_stat = observed_df.loc[observed_df.GO_term == test_go_term].nlog_q_val.values[
0
]
# select simulation data which are more extreme
outlier = nlog_null_qvals.loc[
nlog_null_qvals[test_go_term] > test_stat
].index.values
# lookup in gene list dictionary
outlier_samples = null_sample_dict[outlier]
# lookup in sample dictionary
outlier_genes = null_gene_dict.loc[null_gene_dict.index.isin(outlier)]
outlier_genes = outlier_genes.loc[:, (outlier_genes != 0).any(axis=0)]
# add on the observed data
# samples
outlier_samples["observed"] = observed_samples.observed.values
# genes
outlier_genes = outlier_genes.transpose()
outlier_genes["observed"] = 0
for i in outlier_genes.index.values:
if i in observed_genes.observed.values:
outlier_genes.at[i, "observed"] = 1
else:
continue
outlier_genes["Total_Observations"] = outlier_genes.sum(axis=1)
outlier_genes = outlier_genes[outlier_genes.columns[::-1]]
# print(outlier_genes.head())
# convert to list of lists (where each list is a row) for jquery
gene_header = ["gene"]
gene_header.extend(outlier_genes.columns)
# print(header)
gene_header = [{"title": h} for h in gene_header]
# save to dict
GO["gene_header"] = gene_header
gene_table = []
for i in range(len(outlier_genes.index)):
gene = outlier_genes.index.values[i]
tmp = [gene]
tmp.extend(outlier_genes.iloc[i,].values)
tmp = [str(x) for x in tmp]
gene_table.append(tmp)
# add to dictionary
GO["gene_table"] = gene_table
## outlier samples
# print(outlier_samples.head())
df_arr = outlier_samples.values
groups = outlier_samples.columns.values
colnames = ["sample_id"]
colnames.extend(groups)
empty_df = pd.DataFrame(columns=colnames)
for i, sample in enumerate(set(df_arr.flatten())):
tf_array = sample == df_arr
a = np.array(np.where(tf_array == True))
ii = a[-1]
vals = np.zeros(len(groups))
vals[ii] = 1
vals = list(vals)
col = [sample]
vals.insert(0, sample)
empty_df = empty_df.append(dict(zip(colnames, vals)), ignore_index=True)
sample_table = empty_df
sample_table["Total_Observations"] = sample_table.sum(axis=1)
sample_table = sample_table.set_index(sample_table.sample_id)
sample_table.drop("sample_id", inplace=True, axis=1)
sample_table = sample_table.astype(int)
sample_table = sample_table[sample_table.columns[::-1]]
# print(sample_table.head())
# convert to list of lists (where each list is a row) for jquery
sample_header = ["sample"]
sample_header.extend(outlier_genes.columns)
# print(header)
sample_header = [{"title": h} for h in sample_header]
# save to dict
GO["sample_header"] = sample_header
sample_lists = []
for i in range(len(outlier_samples.index)):
sample = sample_table.index.values[i]
tmp = [sample]
tmp.extend(sample_table.iloc[i,].values)
tmp = [str(x) for x in tmp]
sample_lists.append(tmp)
# add to dictionary
GO["sample_table"] = sample_lists
# add all data from each GO term to dict, keyed by GO term ID
master_GO_dict[test_go_term] = GO
# build template for html report
TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.18/sl-1.3.0/datatables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js" type='text/javascript'></script>
<link href="https://cdn.datatables.net/v/bs4/dt-1.10.18/sl-1.3.0/datatables.min.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<style>
body {margin: 1% 2.5%; background:white }
p { margin: 0 2.5% }
h2 { margin: 1% 0}
.dataTables_filter {
text-align: left !important;
}
</style>
</head>
<body>
<h1 align = 'center'>[TITLE]</h1>
<!- - *** Section 1 *** - ->
<h2>Simulated vs observed enrichment of GO terms</h2>
[STICK_PLOT_DIV]
<p>Red dots outside of blue bars are GO terms significant beyond proband ascertainment bias. Hover over \
lines to get GO term ID, GO term name, observed -log q-value, and simulated mean and standard deviation.</p>
<!- - *** Section 2 *** - ->
<h2>Fold enrichment as a function of p-value</h2>
[PVAL_DIV]
<p>Fold enrichment is calculated as the number of observed genes divided by the number of expected. \
Hover over points to get GO term ID.</p>
<!- - *** Section 3 *** - ->
<h2>Fold enrichment as a function total genes in GO term</h2>
[SIZE_DIV]
<p>Fold enrichment is calculated as the number of observed genes divided by the number of expected. \
Hover over points to get GO term ID.</p>
<!- - *** Section 4 *** - ->
<h2> Analysis of Specific GO terms (populates histogram and tables below) </h2>
<div>
<label>Select GO Term</label>
<select id="go_select">
[TERM_SELECT]
</select>
</div>
<h2>Histogram of GO terms that are more significant than expected by proband ascertainment bias</h2>
<div id = 'hist_div_id'> </div>
<p>Vertical red line indicates value of observed test statistic. Hover over bars to get simulation IDs from \
Monte Carlo used to generate null hypothesis.</p>
<!- - *** Section 5 *** - ->
<h2>Genes in simulated and observed groups</h2>
<p>Cells indicate presence (1) or absence (0) of a gene in a group. Click colums to sort. \
Genes present in all groups may represent a core set of genes driving GO enrichment \
(Sort based on Total Observations). Control + click to sort based on second category. </p>
<table id="gene_table" class="table table-hover pb-3 display nowrap" width="90%"> </table>
<h2>Samples in simulated and observed groups</h2>
<p>Cells indicate presence (1) or absence (0) of a sample in a group. Click colums to sort. \
Control + click to sort based on second category.</p>
<table id="sample_table" class="table table-hover pb-3 display nowrap" width="90%"></table>
</body>
<script>
const data = [DATA]
var gene_table
var sample_table
var go = Object.keys(data)[0]
const build_hist = (go) => {
hist_vals = data[go]['hist_vals']
hist_line = data[go]['hist_line']
hist_title = data[go]['name']
var trace = {
x: hist_vals,
type: 'histogram'
}
var hist_data = [trace]
hist_layout = {'title': hist_title,
'yaxis': {title: 'Frequency'},
'xaxis': {title: '-log(Simulated q-value) vs. -log(observed q-value)'},
'hovermode': 'closest',
'bargap': 0.1,
'shapes':[{'type': 'line',
'x0':hist_line,
'y0':0,
'x1':hist_line,
'y1':1,
'yref': "paper",
'line':{'color':'red','width': 3}
}]
}
let hist_plot = document.getElementById("hist_div_id")
Plotly.react(hist_plot, hist_data, hist_layout)
}
const build_table = (go) => {
if ( $.fn.DataTable.isDataTable('#gene_table') ) {
$('#gene_table').DataTable().destroy()
}
$('#gene_table tbody').empty()
$('#gene_table thead').empty()
if ( $.fn.DataTable.isDataTable('#sample_table') ) {
$('#sample_table').DataTable().destroy()
}
$('#sample_table tbody').empty()
$('#sample_table thead').empty()
gene_table = $("#gene_table").DataTable({
data: data[go]['gene_table'],
columns: data[go]['gene_header'],
scrollY: '600px',
scrollX: true,
scrollCollapse: true,
paging: false,
pagingType: "simple",
info: true,
dom: 'flrtip'
})
sample_table = $("#sample_table").DataTable({
data: data[go]['sample_table'],
columns: data[go]['sample_header'],
scrollY: '600px',
scrollX: true,
scrollCollapse: true,
paging: false,
pagingType: "simple",
info: true,
dom: 'flrtip'
})
}
$('#go_select').on("change", () => {
go = $('#go_select :selected').val()
build_table(go)
build_hist(go)
})
$(document).ready(function() {
build_table(go)
build_hist(go)
})
</script>
</html>
"""
# dump the data into the template in json format
html = TEMPLATE.replace("[DATA]", json.dumps(master_GO_dict))
# plots that only depend on the focal PRS, quartile, and proband/sibling status
html = html.replace("[STICK_PLOT_DIV]", str(stick_plot_div))
html = html.replace("[PVAL_DIV]", str(enrichment_by_pval_div))
html = html.replace("[SIZE_DIV]", str(enrichment_by_size_div))
html = html.replace("[TITLE]", str(args.report_title))
# list of go terms for dropdown menu
select_term = []
for key in name_dict.keys():
select_term.append("<option value = " + name_dict[key] + ">" + key + "</option>")
select_term = " ".join(select_term)
html = html.replace("[TERM_SELECT]", select_term)
# write to file
f = open(args.report, "w")
f.write(html)
f.close()