-
Notifications
You must be signed in to change notification settings - Fork 2
/
usage.py
863 lines (771 loc) · 24.2 KB
/
usage.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
from base64 import b64decode
import glob
import gzip
import zlib
import re
import flask
import dash_ngl
import dash
from dash.dependencies import Input, Output, State
from dash.dash import no_update
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
f_app = flask.Flask(__name__)
app = dash.Dash(
__name__,
server=f_app,
external_stylesheets=[dbc.themes.BOOTSTRAP]
)
app.css.config.serve_locally = True
# Preset colors for the shown molecules
color_list = [
'#e41a1c',
'#377eb8',
'#4daf4a',
'#984ea3',
'#ff7f00',
'#ffff33',
'#a65628',
'#f781bf',
'#999999',
]
representations = [
'axes',
'axes+box',
'backbone',
'ball+stick',
'cartoon',
'helixorient',
'hyperball',
'licorice',
'line',
'ribbon',
'rope',
'spacefill',
'surface',
'trace',
'tube',
'unitcell',
]
# PDB examples
pdbs_list = [
'MPRO',
'PLPR',
'NSP2',
'NSP4',
'NSP6',
'1PNK',
'1KMQ',
'5L73',
'4GWM',
'3L9P',
'6CHG',
'3K8P',
'2MRU',
'1BNA',
'6NZK',
'6OHW',
]
# Placeholder which is loaded if no molecule is selected
data_dict = {
'uploaded': False,
'selectedValue': 'placeholder',
'resetView': False,
'chain': 'ALL',
'aaRange': 'ALL',
'chosen': {'atoms': '', 'residues': ''},
'color': '#e41a1c',
'filename': 'placeholder',
'ext': '',
'config': {'type': '', 'input': ''},
}
# user input triggered error messages:
input_wrong = 'wrong input make sure it matches that format: \
pdbID1.chain:start-end_pdbID2.chain:start-end'
mols2colors_missmatch = 'more molecules selected as chain colors defined \
add a extra color in the view tab and try again'
# Canvas container to display the structures
component_id = 'nglViewer'
viewer = html.Div(
id='ngl-biomolecule-viewer',
children=[dash_ngl.DashNgl(id=component_id, data=[data_dict])],
)
about_html = [
html.H4(className='what-is', children='What is Ngl Molecule Viewer?'),
html.P(
'Ngl Molecule Viewer is a visualizer that allows you'
' to view biomolecules in multiple representations.'
),
html.P(
'You can select a preloaded structure, or upload your own,'
' in the "Data" tab. Supported formats: .pdb(.gz) / .cif(.gz) '
),
html.P(
'Additionally you can show multiple structures and (or) specify a chain/'
' residues range / highlight Cα of chosen residues or single atoms.'
),
html.P(
'In the "View" tab, you can change the style of the viewer.'
' Like the background color, the chain colors, the render quality etc.'
' On top you can change the molecular representation.'
),
]
regexp = [
'^([A-Za-z0-9]{4})',
'(.[a-zA-Z])?',
'([:][0-9-]+)?',
'[@]?([a0-9,]+)?'
]
data_tab = [
html.Div(className='app-controls-name', children='Select structure'),
dcc.Dropdown(
id='pdb-dropdown',
clearable=False,
options=[{'label': k, 'value': k} for k in pdbs_list],
placeholder='Select a molecule',
),
html.Br(),
html.Div(
children=[
html.P(
'Show multiple structures and (or) \
specify a chain/ residues range/ \
highlight chosen residues/ atoms',
style={'fontSize': '10pt'},
)
]
),
dbc.Input(
id='pdb-string',
className='mb-3',
placeholder='pdbID1.chain:start-end_pdbID2.chain:start-end',
value='6CHG.A:1-450@a50,a100,a150_3K8P.D',
type='text',
pattern="^[A-Za-z0-9]{4}([A-Za-z0-9-@,_.:]+)?",
debounce=True
),
html.Br(),
html.Button('submit', id='btn-pdbString'),
html.Button('reset view', id='btn-resetView'),
html.Br(),
html.Div(
title='Upload biomolecule to view here',
className='app-controls-block',
id='ngl-upload-container',
children=[
dcc.Upload(
id='ngl-upload-data',
className='control-upload',
children=html.Div(
['Drag and drop or click to upload (multiple) pdb/cif file(s).']
),
# Allow multiple files to be uploaded
multiple=True,
),
# html.Div(id='uploaded-files', children=html.Div([''])),
html.Div(id='warning_div', children=html.Div([''])),
],
),
html.Div(id='ngl-data-info'),
]
view_tab = [
html.Div(
title='select molecule style',
className='app-controls-block',
id='ngl-mols-style',
children=[
html.P('Style',
style={'fontWeight': 'bold', 'marginBottom': '10px'}),
dcc.Dropdown(
id='molecules-representation-style',
options=[{'label': e, 'value': e.lower()} for e in representations],
placeholder='select molecule style',
value=['cartoon', 'axes+box'],
multi=True,
),
],
),
html.Div(
title='show molecules side by side',
className='app-controls-block',
id='ngl-mols-sideByside',
children=[
html.P('if multiple molecules show them side by side?',
style={'fontWeight': 'bold', 'marginBottom': '10px'}),
dcc.RadioItems(
id='sideByside-selector',
options=[
{'label': 'Yes (mols cannot be moved/rotated independently)', 'value':'True'},
{'label': 'No (move/rotate mols independently by ctrl)', 'value':'False'},
],
value='False'
),
],
),
html.Div(
title='set molecules x-axis spacing',
className='app-controls-block',
id='ngl-mols-spacing',
children=[
html.P(
'x-axis spacing (if side by side yes)', style={'fontWeight': 'bold', 'marginBottom': '10px'},
),
dcc.Input(
id='molecules-xaxis-spacing',
placeholder='set x-axis spacing',
value=100,
),
],
),
html.P('To apply the settings go back to data and submit again'),
# modifying the chain colors only works when individual chains are displayed
html.Div(
title='set chain color',
className='app-controls-block',
id='ngl-mols-color',
children=[
html.P(
'Chain colors',
style={'fontWeight': 'bold', 'marginBottom': '10px'},
),
dcc.Input(id='molecules-chain-color', value=','.join(color_list)),
],
),
html.Div(
title='set chosen atoms color',
className='app-controls-block',
id='ngl-atom-color',
children=[
html.P(
'Chosen atoms Color',
style={'fontWeight': 'bold', 'marginBottom': '10px'},
),
dcc.Input(id='chosen-atoms-color', value='#808080'),
],
),
html.Div(
title='set chosen atoms radius',
className='app-controls-block',
id='ngl-atom-radius',
children=[
html.P(
'Chosen atoms radius',
style={'fontWeight': 'bold', 'marginBottom': '10px'},
),
dcc.Input(id='chosen-atoms-radius', value='1.1'),
],
),
html.Div(
title='select background color',
className='app-controls-block',
id='ngl-style-color',
children=[
html.P(
'Background color',
style={'fontWeight': 'bold', 'marginBottom': '10px'},
),
dcc.Dropdown(
id='stage-bg-color',
options=[{'label': e, 'value': e.lower()} for e in ['black', 'white']],
value='white',
),
],
),
html.Div(
title='Camera settings',
className='app-controls-block',
id='ngl-selection-display',
children=[
html.P(
'Camera settings', style={'fontWeight': 'bold', 'marginBottom': '10px'},
),
dcc.Dropdown(
id='stage-camera-type',
options=[
{'label': e.capitalize(), 'value': e}
for e in ['perspective', 'orthographic']
],
value='perspective',
),
],
),
html.Div(
title='select render quality',
className='app-controls-block',
id='ngl-style',
children=[
html.P(
'Render quality', style={'fontWeight': 'bold', 'marginBottom': '10px'},
),
dcc.Dropdown(
id='stage-render-quality',
options=[
{'label': e, 'value': e.lower()}
for e in ['auto', 'low', 'medium', 'high']
],
value='auto',
),
],
),
]
download_tab = [
html.Button('Download Image', id='btn-downloadImage'),
html.Div(
title='antialias',
className='app-controls-block',
id='ngl-image-antialias',
children=[
html.P('antialias', style={'fontWeight': 'bold', 'marginBottom': '10px'},),
dcc.Dropdown(
id='image-antialias',
options=[{'label': c, 'value': c} for c in ['Yes', 'No']],
value='Yes',
),
],
),
html.Div(
title='trim',
className='app-controls-block',
id='ngl-image-trim',
children=[
html.P('trim', style={'fontWeight': 'bold', 'marginBottom': '10px'}),
dcc.Dropdown(
id='image-trim',
options=[{'label': c, 'value': c} for c in ['Yes', 'No']],
value='Yes',
),
],
),
html.Div(
title='transparent',
className='app-controls-block',
id='ngl-image-transparent',
children=[
html.P(
'transparent', style={'fontWeight': 'bold', 'marginBottom': '10px'},
),
dcc.Dropdown(
id='image-transparent',
options=[{'label': c, 'value': c} for c in ['Yes', 'No']],
value='Yes',
),
],
),
]
htmls = [('About', 'what-is', about_html),
('Data', 'upload-select', data_tab),
('View', 'view-options', view_tab),
('Download', 'download-options', download_tab)]
tabs = html.Div(
id='ngl-control-tabs',
className='control-tabs',
children=[
dcc.Tabs(
id='ngl-tabs',
value='what-is',
children=[
dcc.Tab(
label=l,
value=v,
children=html.Div(className='control-tab', children=h),
) for l, v, h in htmls
],
),
],
)
# LAYOUT
app.layout = html.Div(
id='main-page',
children=[
# looses the data when the browser/tab closes.
dcc.Store(id='uploaded-files', storage_type='session'),
html.Div(
id='app-page-header',
children=[html.H1('Ngl Molecule Viewer')],
style={'background': '#e7625f', 'color': 'white'},
),
html.Div(
id='app-page-content',
children=[
html.Div(
id='ngl-body',
className='app-body',
children=[
tabs,
viewer
# using dcc.Loading leads to remounting with every selection change
# dcc.Loading(viewer),
],
),
],
),
],
)
def createDict(
selection,
chain,
aa_range,
highlight_dic,
color,
filename,
ext,
contents,
resetView=False,
uploaded=False,
):
print('create dic')
print(highlight_dic)
return {
'filename': filename,
'ext': ext,
'selectedValue': selection,
'chain': chain,
'aaRange': aa_range,
'chosen': highlight_dic,
'color': color,
'config': {'type': 'text/plain', 'input': contents},
'resetView': resetView,
'uploaded': uploaded,
}
def getHighlights(string, sep, atom_indicator):
print('getHighlights')
residues_list = []
atoms_list = []
str_, _str = string.split(sep)
for e in _str.split(','):
if atom_indicator in e:
atoms_list.append(e.replace(atom_indicator, ''))
else:
residues_list.append(e)
print(atoms_list)
print(residues_list)
return (str_, {'atoms': ','.join(atoms_list), 'residues': ','.join(residues_list)})
# Helper function to load structures from local storage
def getLocalData(selection, pdb_id, color, uploadedFiles, resetView=False):
print('getLocalData')
print (color)
chain = 'ALL'
aa_range = 'ALL'
highlight_dic = {'atoms': '', 'residues': ''}
highlights_sep = '@'
atom_indicator = 'a'
### replace all this if statements with a regex
# and then get value for each group
# pdb = regex group (1)
# chain = regex group (2)
# aa_range = regex_group(3)
# Check if only one chain should be shown
if '.' in pdb_id:
pdb_id, chain = pdb_id.split('.')
# Check if only a specified amino acids range should be shown:
if ':' in chain:
chain, aa_range = chain.split(':')
# Check if atoms should be highlighted
if highlights_sep in aa_range:
aa_range, highlight_dic = getHighlights(
aa_range, highlights_sep, atom_indicator
)
else:
if highlights_sep in chain:
chain, highlight_dic = getHighlights(
chain, highlights_sep, atom_indicator
)
# print ('highlight_dic')
# print (highlight_dic)
if pdb_id not in pdbs_list:
if pdb_id in uploadedFiles:
print('Already uploaded')
# print(files[:-1].split(','))
fname = [i for i in uploadedFiles[:-1].split(',') if pdb_id in i][0]
print(fname)
content = ''
return createDict(
selection,
chain,
aa_range,
highlight_dic,
color,
fname,
fname.split('.')[1],
content,
resetView,
uploaded=False,
)
return data_dict
# get path to protein structure
fname = [f for f in glob.glob('data/' + pdb_id + '.*')][0]
if 'gz' in fname:
ext = fname.split('.')[-2]
with gzip.open(fname, 'r') as f:
content = f.read().decode('UTF-8')
else:
ext = fname.split('.')[-1]
with open(fname, 'r') as f:
content = f.read()
filename = fname.split('/')[-1]
return createDict(
selection,
chain,
aa_range,
highlight_dic,
color,
filename,
ext,
content,
resetView,
uploaded=False,
)
# Helper function to load structures from uploaded content
def getUploadedData(uploaded_content):
data = []
uploads = []
ext = 'pdb'
chain = 'ALL'
aa_range = 'ALL'
highlight_dic = {'atoms': '', 'residues': ''}
for i, content in enumerate(uploaded_content):
content_type, content = str(content).split(',')
if 'gzip' in content_type:
content = zlib.decompress(b64decode(content), zlib.MAX_WBITS | 16)
else:
content = b64decode(content)
content = content.decode('UTF-8')
pdb_id = content.split('\n')[0].split()[-1]
if 'data_' in pdb_id:
pdb_id = pdb_id.split('_')[1]
ext = 'cif'
print(pdb_id)
filename = pdb_id + '.' + ext
uploads.append(filename)
data.append(
createDict(
pdb_id,
chain,
aa_range,
highlight_dic,
color_list[i],
filename,
ext,
content,
resetView=False,
uploaded=True,
)
)
return data, uploads
# CB viewport
@app.callback(
[
Output(component_id, 'data'),
Output(component_id, 'molStyles'),
Output('pdb-dropdown', 'options'),
Output('uploaded-files', 'data'),
Output('pdb-dropdown', 'placeholder'),
Output('warning_div', 'children'),
Output('pdb-string', 'invalid')
],
[
Input('pdb-dropdown', 'value'),
Input('ngl-upload-data', 'contents'),
Input('btn-pdbString', 'n_clicks'),
Input('btn-resetView', 'n_clicks'),
Input('molecules-representation-style', 'value'),
],
[
State('pdb-string', 'value'),
State('pdb-dropdown', 'options'),
State('uploaded-files', 'data'),
State('molecules-chain-color', 'value'),
State('chosen-atoms-color', 'value'),
State('chosen-atoms-radius', 'value'),
State('molecules-xaxis-spacing', 'value'),
State('sideByside-selector', 'value')
],
)
def display_output(
selection,
uploaded_content,
pdbString_clicks,
resetView_clicks,
molStyles_list,
pdbString,
dropdown_options,
files,
colors,
chosenAtomsColor,
chosenAtomsRadius,
molSpacing_xAxis,
sideByside_text
):
print('selection,pdbString_clicks,pdbString,type uploaded_content', 'files')
print(
selection,
molStyles_list,
pdbString_clicks,
pdbString,
type(uploaded_content),
type(files),
)
input_id = None
options = dropdown_options
colors_list = colors.split(',')
print (colors_list)
# files = files['props']['children'] if isinstance(files, dict) else ''.join(files)
# print('files', files)
# Give a default data dict if no files are uploaded
files = files or {'uploaded': []}
print('files', files)
ctx = dash.callback_context
if ctx.triggered:
input_id = ctx.triggered[0]['prop_id'].split('.')[0]
print('triggered', input_id)
sideByside_bool = False
if sideByside_text == 'True':
sideByside_bool = True
molStyles_dict = {
'representations': molStyles_list,
'chosenAtomsColor': chosenAtomsColor,
'chosenAtomsRadius': float(chosenAtomsRadius),
'molSpacingXaxis': float(molSpacing_xAxis),
'sideByside': sideByside_bool,
}
print (molStyles_dict)
if input_id is None:
return [data_dict], molStyles_dict, options, files, no_update, no_update, no_update
if input_id == 'pdb-dropdown':
print('dropdown changed')
pdb_id = selection
uploaded_files = files['uploaded']
if pdb_id in ','.join(uploaded_files):
print('Already uploaded')
# print(files[:-1].split(','))
fname = [e for e in uploaded_files if pdb_id in e][0]
# fname = [i for i in files[:-1].split(',') if pdb_id in i][0]
print(fname)
content = ''
chain = 'ALL'
aa_range = 'ALL'
highlight_dic = {'atoms': '', 'residues': ''}
return (
[
createDict(
pdb_id,
chain,
aa_range,
highlight_dic,
colors_list[0],
fname,
fname.split('.')[1],
content,
resetView=False,
uploaded=False,
)
],
molStyles_dict,
options,
files,
no_update,
no_update,
no_update
)
print('HERE')
data = [getLocalData(selection, pdb_id, colors_list[0], files, resetView=False)]
return data, molStyles_dict, options, files, no_update, no_update, False
# TODO submit and reset view in one button
# reset view/submit is triggered two times when the mol style is changed
if input_id in ['btn-pdbString', 'btn-resetView']:
warning = ''
if pdbString is None:
return no_update, no_update, no_update, no_update, no_update, no_update, no_update
resetView = False
if input_id == 'btn-resetView':
resetView = True
patt = re.compile("".join(regexp))
data = []
if len(pdbString) > 3:
pdb_id = pdbString
if '_' in pdbString:
for i, pdb_id in enumerate(pdbString.split('_')):
if patt.fullmatch(pdb_id) is None:
warning = input_wrong
return no_update, no_update, no_update, no_update, no_update, warning, True
if i <= len(colors_list) - 1:
data.append(
getLocalData(
pdbString,
pdb_id,
colors_list[i],
files,
resetView=resetView,
)
)
else:
data.append(data_dict)
warning = mols2colors_missmatch
return data, no_update, options, files, no_update, warning, False
else:
if patt.fullmatch(pdb_id) is None:
warning = input_wrong
return no_update, no_update, no_update, no_update, no_update, warning, True
data.append(
getLocalData(
pdbString, pdb_id, colors_list[0], files, resetView=resetView
)
)
else:
data.append(data_dict)
return data, molStyles_dict, options, files, 'Select a molecule', warning, False
if input_id == 'ngl-upload-data':
data, uploads = getUploadedData(uploaded_content)
for pdb_id, ext in [e.split('.') for e in uploads]:
if pdb_id not in [e['label'] for e in options]:
options.append({'label': pdb_id, 'value': pdb_id})
fname = pdb_id + '.' + ext
print('uploaded', pdb_id)
# files += pdb_id + '.' + ext + ','
if fname not in files['uploaded']:
files['uploaded'].append(fname)
return data, molStyles_dict, options, files, pdb_id, no_update, False
if input_id == 'molecules-representation-style':
return no_update, molStyles_dict, no_update, no_update, no_update, no_update, no_update
# CB change molecule representation
@app.callback(
Output(component_id, 'stageParameters'),
[
Input('stage-bg-color', 'value'),
Input('stage-camera-type', 'value'),
Input('stage-render-quality', 'value'),
],
)
def update_stage(bgcolor, camera_type, quality):
return {
'backgroundColor': bgcolor,
'cameraType': camera_type,
'quality': quality,
}
# CB download Image
bool_dict = {'Yes': True, 'No': False}
@app.callback(
[Output(component_id, 'downloadImage'), Output(component_id, 'imageParameters')],
[Input('btn-downloadImage', 'n_clicks')],
[
State('image-antialias', 'value'),
State('image-trim', 'value'),
State('image-transparent', 'value'),
],
)
def download_image(n_clicks, antialias, trim, transparent):
if n_clicks is None:
return False, no_update
ctx = dash.callback_context
return (
bool(ctx.triggered),
{
'antialias': bool_dict[antialias],
'trim': bool_dict[trim],
'transparent': bool_dict[transparent],
'defaultFilename': 'dash_ngl_output',
},
)
if __name__ == '__main__':
app.run_server(host='0.0.0.0', port=8051, debug=True, use_reloader=False)