-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv_dashboard.py
460 lines (386 loc) · 12.1 KB
/
mv_dashboard.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
#!/usr/bin/env python
# coding: utf-8
# # Dashboard
# ## Import modules
# In[ ]:
import dash
import dash_bio as dashbio
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
#from dash_bio_utils import pdb_parser as parser
from dash.dependencies import Input, Output
import json
import tempfile
import re
import os
from shutil import copy2
import parmed as pmd
import copy
import pandas as pd
try:
import mutagenesis_visualization as mut
jupyterlab = False
pdb = 'data/5p21.pdb'
except ModuleNotFoundError: # This step is only for when I run the notebooks locally
import sys
sys.path.append('../../../')
import mutagenesis_visualization as mut
__name__ = '__main__'
jupyterlab = True # for local use in jupyter lab
pdb = '../../data/5p21.pdb'
# # Auxiliary functions
# In[ ]:
def _parse_pdb(pdb):
'''return the pdb in jason format'''
# Parse pdb file
modata = parser(pdb)
#modata = parser.create_data(pdb)
# Put in jason format
fmodel = _files_data_style(modata)
with open(fmodel) as fm:
model_data = json.load(fm)
return model_data
def _parse_styles_data(
self,
model_data,
df,
gof,
lof,
mode,
position_correction,
chain,
):
'''
From a dataframe with enrichment scores, this function will return a jason file
with the color of each atom.
Returns
-------
styles_data : jason file
'''
# Create empty dict
styles_data = {}
# Calculate df with colors
df_color = _add_color(
self.dataframe.copy(), gof, lof, mode, position_correction=0
)
# Iterate over parsed pdb
for item in model_data['atoms']:
#if item['chain'] != 'A': # only color atoms from selected chain
#break
try:
style_atom = {
'color': _assign_color_jason(df_color, item['residue_index']),
'visualization_type': 'cartoon'
}
styles_data[str(item['serial'])] = style_atom
except IndexError: # in case we runt out of index
pass
return styles_data
def _assign_color_jason(df, residue):
'''
Give a color to the atom based on the enrichment score of that residue.
As an input takes the dataframe that _add_color returns.
'''
return df.loc[df['Position_Corrected'] == residue, 'Color'].iloc[0]
def _add_color(df, gof, lof, mode, position_correction):
'''You input the dataframe. Removes stop codons.
Returns the positions that are going to be colored blue,red and white'''
# Correct position
df['Position_Corrected'] = df['Position'] + position_correction
# Add dummy color column
red = '#FD3216'
blue = '#6A76FC'
green = '#16FF32'
# Select grouping
if mode == 'mean':
df_grouped = df.groupby(['Position'], as_index=False).mean()
else:
df_grouped = df.loc[df['Aminoacid'] == mode]
# Color of mutations
df_grouped['Color'] = green
df_grouped.loc[df_grouped['Score'] < lof, 'Color'] = blue
df_grouped.loc[df_grouped['Score'] > gof, 'Color'] = red
return df_grouped
def _files_data_style(content):
'''
Function to create the modelData and style files for molecule visualization
'''
fdat = tempfile.NamedTemporaryFile(suffix=".js", delete=False, mode='w+')
fdat.write(content)
dataFile = fdat.name
fdat.close()
return dataFile
# ## PDB parser
# In[ ]:
def parser(pdb_path):
"""
Parse the protein data bank (PDB) file to generate
input modelData
@param pdb_path
Name of the biomolecular structure file in PDB format
"""
# Create local copy of temp file
copy2(pdb_path, './tmp.pdb')
# Use parmed to read the bond information from temp file
top = pmd.load_file('tmp.pdb')
# Remove the created temp file
#os.remove('tmp.pdb') #was giving error when uploading to huroku
# Read PDB file to create atom/bond information
with open(pdb_path, 'r') as infile:
# store only non-empty lines
lines = [l.strip() for l in infile if l.strip()]
# Initialize all variables
var_nchains = []
serial = []
atm_name = []
res_name = []
chain = []
res_id = []
positions = []
occupancy = []
temp_factor = []
atom_type = []
ct = 0
datb = {'atoms': [], 'bonds': []}
# Variables that store the character positions of different
# parameters from the molecule PDB file
serialpos = [6, 11]
atm_namepos = [12, 16]
r_namepos = [17, 20]
chainpos = [21, 22]
r_idpos = [22, 26]
xpos = [30, 38]
ypos = [38, 46]
zpos = [46, 54]
occupos = [54, 60]
bfacpos = [60, 66]
atm_typepos = [77, 79]
for l in lines:
line = l.split()
if "ATOM" in line[0] or "HETATM" in line[0]:
serial.append(int(l[serialpos[0]:serialpos[1]]))
atm_name.append(l[atm_namepos[0]:atm_namepos[1]].strip())
val_r_name = l[r_namepos[0]:r_namepos[1]].strip()
res_name.append(val_r_name)
chain_val = l[chainpos[0]:chainpos[1]].strip()
chain.append(chain_val)
if chain_val not in var_nchains:
var_nchains.append(chain_val)
val_r_id = int(l[r_idpos[0]:r_idpos[1]])
res_id.append(val_r_id)
x = float(l[xpos[0]:xpos[1]])
y = float(l[ypos[0]:ypos[1]])
z = float(l[zpos[0]:zpos[1]])
positions.append([x, y, z])
occupancy.append(l[occupos[0]:occupos[1]].strip())
temp_factor.append(l[bfacpos[0]:bfacpos[1]].strip())
atom_type.append(l[atm_typepos[0]:atm_typepos[1]].strip())
ct += 1
# Create list of atoms
tmp_res = res_id[0]
resct = 1
for i in range(len(chain)): # pylint: disable=consider-using-enumerate
if tmp_res != res_id[i]:
tmp_res = res_id[i]
resct += 1
datb['atoms'].append({
"name": atm_name[i],
"chain": chain[i],
"positions": positions[i],
"residue_index": resct,
"element": atom_type[i],
"residue_name": res_name[i] + str(res_id[i]),
"serial": i,
})
# Create list of bonds using the parmed module
for i in range(len(top.bonds)):
bondpair = top.bonds[i].__dict__
atom1 = re.findall(r"\[(\d+)\]", str(bondpair['atom1']))
atom2 = re.findall(r"\[(\d+)\]", str(bondpair['atom2']))
datb['bonds'].append({
'atom2_index': int(atom1[0]), 'atom1_index': int(atom2[0])
})
return json.dumps(datb)
# # Main dashboard function
# In[ ]:
hras_RBD = mut.hras_RBD()
self = hras_RBD
chain = 'A'
position_correction = 0
# update kwargs
temp_kwargs = copy.deepcopy(mut.code_kwargs.kwargs())
# Load data from pdb file
model_data = _parse_pdb(pdb)
# Open app
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server
# App layout
app.layout = html.Div([
dbc.Row(
dbc.Col(
html.H3("Site-saturation mutagenesis of H-Ras"), # title
width={'size': 12},
),
),
dbc.Row(
dbc.Col(
dcc.Graph( # heatmap
id="heatmap",
figure={},
config={'displayModeBar': False},
),
width=12,
)
),
dbc.Row([
dbc.Col([
html.Div("Select cutoffs"),
dcc.RangeSlider(
id='range_slider',
min=-1,
max=1,
step=0.01,
value=[-0.3,0.1],
marks={
-1: {'label': '-1', 'style': {'color': '#6A76FC'}},
0: {'label': '0'},
1: {'label': '+1', 'style': {'color': '#FD3216'}},
},
)],
width={'size': 3, 'offset': 1},
),
dbc.Col([
html.Div("Filter the data by:"),
dcc.Dropdown(
id='dropdown',
placeholder='choose amino acid',
clearable=False,
value='mean',
options=[
{'label': 'Mean', 'value': 'mean'},
{'label': 'Alanine', 'value': 'A'},
{'label': 'Arginine', 'value': 'R'},
{'label': 'Asparagine', 'value': 'N'},
{'label': 'Aspartic acid ', 'value': 'D'},
{'label': 'Cysteine', 'value': 'C'},
{'label': 'Glutamine', 'value': 'Q'},
{'label': 'Glutamic acid ', 'value': 'E'},
{'label': 'Glycine', 'value': 'G'},
{'label': 'Histidine', 'value': 'H'},
{'label': 'Isoleucine', 'value': 'I'},
{'label': 'Leucine', 'value': 'L'},
{'label': 'Lysine', 'value': 'K'},
{'label': 'Methionine', 'value': 'M'},
{'label': 'Phenylalanine', 'value': 'F'},
{'label': 'Proline', 'value': 'P'},
{'label': 'Serine', 'value': 'S'},
{'label': 'Threonine', 'value': 'T'},
{'label': 'Tryptophan', 'value': 'W'},
{'label': 'Tyrosine', 'value': 'Y'},
{'label': 'Valine', 'value': 'V'},
#{'label': 'Stop codon', 'value': '*'},
]
),
],width={'size': 3, 'offset': 3},),
]),
dbc.Row([
dbc.Col(
id='moleculeviewer',
children={},
width={"size": 5, 'order':1, "offset": -1},
),
dbc.Col(
[dbc.Row(
dcc.Graph(
id="mean",
figure={},
config={'displayModeBar': False},
),
),
dbc.Row([dbc.Col(
dcc.Graph(
id="scatter_3d",
figure={},
config={'displayModeBar': False},
),width={"size": 7, 'order':1},
),dbc.Col(
dcc.Graph(
id="histogram",
figure={},
config={'displayModeBar': False},
),width={"size": 5, 'order':2},
)],no_gutters=True),],width={"size": 7, 'order':2},)
],
no_gutters=True),
])
@app.callback([Output('moleculeviewer', 'children')], [
Input('dropdown', 'value'),
Input('range_slider', 'value'),
])
def update_molecule3d(mode, range_slider):
'''
Call the dashbio.molecule3dviewer and updated the coloring based on user input.
'''
# Calculate styles based on enrichment scores for the 3d viewer
styles_data = _parse_styles_data(
self,
model_data,
self.dataframe.copy(),
range_slider[1],
range_slider[0],
mode,
position_correction,
chain,
)
return [html.Div(dashbio.Molecule3dViewer( # 3d molecule
modelData=model_data,
styles=styles_data,
selectionType='Chain',
))]
@app.callback([
Output('heatmap', 'figure'),
Output('mean', 'figure'),
Output('scatter_3d', 'figure'),
Output('histogram', 'figure'),
], [
Input('dropdown', 'value'),
])
def update_graphs(mode='mean'):
'''
Aux function to update the plotly figures based on the user input.
'''
heatmap = self.heatmap_plotly(
return_plot_object=True,
show=False,
title='Heatmap',
figsize=(8, 2.5),
)
mean = self.mean_plotly(
mode=mode,
return_plot_object=True,
show=False,
figsize=(6, 2.5),
)
scatter_3d = self.scatter_3D_plotly(
mode=mode,
pdb_path=pdb,
return_plot_object=True,
show=False,
figsize=(3, 3),
title='C-α carbons',
)
histogram = self.histogram_plotly(
mode=mode,
return_plot_object=True,
show=False,
figsize=(3, 3),
)
return heatmap, mean, scatter_3d, histogram
# Run server
if jupyterlab:
app.run_server(port=8083)
else:
if __name__ == '__main__':
app.run_server(debug=True)
# In[ ]: