forked from cryoEM-CNIO/CNIO_Relion_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relion_analyse.py
executable file
·1270 lines (1018 loc) · 59.8 KB
/
relion_analyse.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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Relion analyse dashboard
Nayim Gonzalez-Rodriguez, Emma Arean-Ulloa & Rafael Fernandez-Leiro 2022
"""
"""
Activate conda environment before running
Usage: run relion_analyse.py in your relion project directory
"""
### Libraries setup
import os
import pandas as pd
import starfile
import dash
from dash import html
from dash import dcc
from dash import ctx
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import plotly.graph_objects as go
import plotly.express as px
import socket
import argparse
import glob
import dash_cytoscape as cyto
import regex as re
from PIL import Image
# Load extra layouts for pipeline display
cyto.load_extra_layouts()
# Parsing port number and host
parser = argparse.ArgumentParser()
parser.add_argument("--port", "-p", help = "choose port to run the webapp")
parser.add_argument("--host", "-host", help = "choose host to run the webapp")
parser.add_argument("--debug", "-d", help = "launch app in debug mode")
args, unknown = parser.parse_known_args()
# Set localhost and 8051 as host and port by default
if not args.port: port_number = 8051
else: port_number = args.port
if not args.host: hostname = socket.gethostname()
else: hostname = args.host
if not args.debug: debug_mode = False
else: debug_mode = args.host
### FUNCTION DEFINITIONS ###
# Reading pipeline data
def pipeline_reader1(pipeline_starfile, nodetype):
pipeline_df = starfile.read(pipeline_starfile)['pipeline_nodes']
nodes = list(pipeline_df[pipeline_df['rlnPipeLineNodeTypeLabel'].str.contains(str(nodetype), na=False)]['rlnPipeLineNodeName'])
return nodes
def pipeline_reader2(pipeline_starfile, nodetype):
pipeline_df = starfile.read(pipeline_starfile)['pipeline_processes']
nodes = list(pipeline_df[pipeline_df['rlnPipeLineProcessName'].str.contains(str(nodetype), na=False)]['rlnPipeLineProcessName'])
return nodes
# Plot scatterplot with side violin plots
def plot_scatter(dataframe, x_data, y_data, coloring):
plot = px.scatter(
data_frame = dataframe,
x = x_data,
y = y_data,
marginal_x = "violin",
marginal_y = "violin",
color = coloring,
render_mode = 'webgl',
template = 'plotly_white',
opacity = 0.5
)
return plot
# Plot scatterplot with side violin plots using WebGL for large datasets
def plot_scattergl(data_frame, x_data, y_data, coloring, colorscale, hist_color):
plot = go.Figure()
main_scatter = plot.add_scattergl(
x = data_frame[x_data],
y = data_frame[y_data],
mode = 'markers',
marker_colorscale=colorscale,
marker = dict(color=coloring, opacity=0.5, showscale=True),
fillcolor = 'white'
)
side_histogram_x = plot.add_trace(go.Violin(
x = data_frame[x_data],
name = x_data,
yaxis = 'y2',
marker = dict(opacity=0.5, color=hist_color),
))
side_histogram_y = plot.add_trace(go.Violin(
y = data_frame[y_data],
name = y_data,
xaxis = 'x2',
marker = dict(opacity=0.5, color=hist_color),
))
plot.layout = dict(xaxis=dict(domain=[0, 0.85], zeroline=True, title=x_data,gridcolor='#CBCBCB'),
yaxis=dict(domain=[0, 0.85], zeroline=True, title=y_data, gridcolor='#CBCBCB'),
showlegend=False,
margin=dict(t=50),
hovermode='closest',
bargap=0,
xaxis2=dict(domain=[0.85, 1], showgrid=True, zeroline=False),
yaxis2=dict(domain=[0.85, 1], showgrid=True, zeroline=False),
plot_bgcolor = 'rgba(0,0,0,0)',
)
def do_zoom(layout, xaxis_range, yaxis_range):
inds = ((xaxis_range[0] <= data_frame[x_data]) & (data_frame[x_data] <= xaxis_range[1]) &
(yaxis_range[0] <= data_frame[y_data]) & (data_frame[y_data] <= yaxis_range[1]))
with plot.batch_update():
side_histogram_x.x = data_frame[x_data][inds]
side_histogram_y.y = data_frame[y_data][inds]
plot.layout.on_change(do_zoom, 'xaxis.range', 'yaxis.range')
return plot
def empty_graph(text=None, fontsize=20):
fig = go.Figure()
if text:
fig.add_annotation(text=text, showarrow=False, font = dict(size = fontsize))
fig.update_layout(template="simple_white", showlegend=False, height=220)
fig.update_xaxes(visible=False)
fig.update_yaxes(visible=False)
return fig
def image_to_figure(src, width, height, scale_factor):
# Create figure
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=[0, width * scale_factor],
y=[0, height * scale_factor],
mode="markers",
marker_opacity=0
)
)
# Configure axes
fig.update_xaxes(
visible=False,
range=[0, width * scale_factor]
)
fig.update_yaxes(
visible=False,
range=[0, height * scale_factor],
scaleanchor="x"
)
# Add image
fig.add_layout_image(
dict(
x=0,
sizex=width * scale_factor,
y=height * scale_factor,
sizey=height * scale_factor,
xref="x",
yref="y",
opacity=1.0,
layer="below",
sizing="stretch",
source=src)
)
# Configure other layout
fig.update_layout(
width=width * scale_factor,
height=height * scale_factor,
margin={"l": 0, "r": 0, "t": 0, "b": 0},
)
return fig
# Plotting line plots
def plot_line(data_frame, x_data, y_data):
plot = px.line(
x = x_data,
y = data_frame[y_data],
render_mode = 'webgl',
template = 'plotly_white',
)
return plot
def plot_line_multi(data_frame, x_data, y_data, coloring):
plot = px.line(
x = x_data,
y = data_frame[y_data],
color = data_frame[coloring],
render_mode = 'webgl',
template = 'plotly_white',
)
return plot
# Area plots for class distribution over iterations
def plot_area(data_frame):
plot = px.area(data_frame, template = 'plotly_white')
return plot
# Heatmap plots for angular distribution
def plot_angdist(data_frame, x_data, y_data, bins, coloring):
plot = px.density_heatmap(
data_frame = data_frame,
x = x_data,
y = y_data,
facet_col = coloring,
facet_col_wrap = 4,
color_continuous_scale='deep', #colorscales giving decent contrast: RdBu, deep, rainbow
nbinsx = bins,
nbinsy = bins
)
return plot
# Empty graph to show when no data is loaded
def empty_graph(text=None, fontsize=20):
fig = go.Figure()
if text:
fig.add_annotation(text=text, showarrow=False, font = dict(size = fontsize))
fig.update_layout(template="simple_white", showlegend=False, height=220)
fig.update_xaxes(visible=False)
fig.update_yaxes(visible=False)
return fig
### STYLE ###
# Header
header_style = {'width':'100%', 'vertical-align':'center' , 'display':'inline-flex', 'justify-content': 'space-between'}
title1_style = {"margin-left": "15px", "margin-top": "15px", "margin-bottom": "0em", "color": "Black",
"font-family" : "Helvetica", "font-size":"2.5em"}
title2_style = {"margin-left": "15px", "margin-top": "0em", "color": "Black", "font-family" : "Helvetica"}
header_button_style = {'margin-top':'40px','margin-right':'40px'}
# Tabs
pre_style = {'overflowY': 'scroll', 'height': '500px'}
tabs_style = {'height': '3em', 'width': '100%', 'display': 'inline', 'vertical-align': 'bottom', 'borderBottom':'3px #000000'}
tab_style = {'padding':'0.5em', "font-family" : "Helvetica", 'background-color':'white'}
tab_selected_style = {'padding':'0.5em', 'borderTop': '3px solid #000000', "font-family" : "Helvetica", 'font-weight':'bold'}
tab_left_div_style = {'width':'20%', 'vertical-align':'top' , 'display':'inline-block'}
tab_right_div_style = {'width':'80%', 'vertical-align':'top' , 'display':'inline-block'}
tab_bottom_right_style = {'width':'100%', 'vertical-align':'top' , 'display':'inline-block'}
H5_title_style = {'font-family':'Helvetica', 'font-weight':'regular'}
# Dropdowns
dd_style = {"font-size":"0.9em",'width':"100%", "margin-left": "0%", "color": "black",
"font-family" : "Helvetica", 'vertical-align': 'top', "margin-bottom":"2px"}
box_style = {"font-size":"0.9em",'padding':'0.3em 1.2em','width':"87%", "margin-left": "0%", "color": "black",
"font-family" : "Helvetica", 'vertical-align': 'center', "margin-bottom":"2px"}
# Buttons
bt_style = {"align-items": "center", "background-color": "#F2F3F4", "border": "2px solid #000",
"box-sizing": "border-box", "color": "#000", "cursor": "pointer", "display": "inline-flex",
"font-family": "Helvetica", "margin-bottom":"3px", 'padding':'0.3em 1.2em', 'margin':'0 0.3em 0.3em 0',
"font-size": "0.9em", 'font-weight':'500', 'border-radius':'2em', 'text-align':'center',
'transition':'all 0.2s'}
# Pipeline nodes
nodes_style = [
{'selector': 'node', 'style': {'content': 'data(label)', 'font-family': 'monospace', 'text-wrap': 'wrap',
'text-max-width': '80px', 'text-overflow-wrap': 'anywhere',
'text-halign': 'center', 'text-valign': 'center', 'color': 'white',
'text-outline-width': 2, 'width': '90px', 'height': '90px'}},
{'selector': '.Import', 'style': {'background-color': '#FFB3D9', 'shape': 'rectangle'}}, # Pastel Pink
{'selector': '.MotionCorr', 'style': {'background-color': '#FFD9B3'}}, # Pastel Orange
{'selector': '.CtfFind', 'style': {'background-color': '#C9C9C9'}}, # Silver
{'selector': '.AutoPick', 'style': {'background-color': '#A3D8E9'}}, # Pastel Blue
{'selector': '.Extract', 'style': {'background-color': '#FFB3D9', 'shape': 'square'}}, # Pastel Pink
{'selector': '.Select', 'style': {'background-color': '#B9E5C0', 'width': '120px', 'height': '40px'}}, # Pastel Green
{'selector': '.Class2D', 'style': {'background-color': '#FFEAB3'}}, # Pastel Yellow
{'selector': '.Class3D', 'style': {'background-color': '#FFB3B0'}}, # Pastel Salmon
{'selector': '.Refine3D', 'style': {'background-color': '#D9C3D9'}}, # Thistle
{'selector': '.MaskCreate', 'style': {'background-color': '#FFB3B0'}}, # Pastel Salmon
{'selector': '.Polish', 'style': {'background-color': '#C2E0E2'}}, # Pastel Cyan
{'selector': '.LocalResolution', 'style': {'background-color': '#F0CB95'}}, # Pastel Apricot ## Double check this
{'selector': '.CtfRefinement', 'style': {'background-color': '#B1E4B4'}}, # Pastel Green ## Double check this
{'selector': '.Subtract', 'style': {'background-color': '#A8C3D9'}}, # Pastel Blue # Double check this
{'selector': '.PostProcess', 'style': {'background-color': '#E9E9E9', 'line-color': '#E9E9E9', 'shape': 'star'}}, # Pastel Gray
{'selector': '.External', 'style': {'background-color': '#C5E5F0'}}, # Pale Cyan
]
hover_style = {
'position': 'fixed',
'z-index': '1',
'bottom': '10px',
'right': '10px',
'padding': '20px',
'overflow-y': 'scroll',
'background-color': '#f7f7f7',
'box-shadow': '0px 2px 5px rgba(0, 0, 0, 0.2)',
'border-radius': '12px',
'height': '40%',
'width': '20%',
'font-family': 'Arial, sans-serif',
'font-size': '1em',
}
job_specific_data = {
"relion.import.movies":['Cs', 'Q0', 'angpix', 'beamtilt_x', 'beamtilt_y', 'kV'],
"relion.motioncorr.own":['bfactor', 'bin_factor', 'do_float16', 'dose_per_frame', 'fn_gain_ref', 'gain_flip', 'gain_rot', 'group_frames', 'patch_x', 'patch_y'],
"relion.motioncorr.motioncor2":['bfactor', 'bin_factor', 'do_float16', 'dose_per_frame', 'fn_gain_ref', 'gain_flip', 'gain_rot', 'group_frames', 'patch_x', 'patch_y'],
"relion.ctffind.ctffind4":['box', 'ctf_win', 'dast', 'dfmax', 'dfmin', 'dfstep', 'use_noDW'],
"relion.ctffind.gctf":['box', 'ctf_win', 'dast', 'dfmax', 'dfmin', 'dfstep', 'use_noDW'],
"relion.manualpick":['color_label', 'diameter'],
"relion.autopick.log":['log_adjust_thr', 'log_diam_max', 'log_diam_min', 'log_invert', 'log_maxres', 'log_upper_thr'],
"relion.autopick.topaz.train":['topaz_train_parts','topaz_train_picks'],
"relion.autopick.topaz.pick":['topaz_model', 'topaz_nr_particles', 'topaz_other_args', 'topaz_particle_diameter'],
"relion.autopick.ref2d":['ref3d_sampling', 'ref3d_symmetry', 'shrink', 'threshold_autopick', ],
"relion.extract":['do_fom_threshold', 'do_invert', 'do_recenter', 'do_reextract', 'do_rescale','extract_size', 'minimum_pick_fom', 'rescale'],
"relion.extract.reextract":['do_fom_threshold', 'do_invert', 'do_recenter', 'do_reextract', 'do_rescale','extract_size', 'minimum_pick_fom', 'rescale'],
"relion.extract":['do_float16', 'do_recenter', 'do_reextract', 'extract_size', 'minimum_pick_fom', 'rescale'],
"relion.class2d":[ 'do_center', 'do_em', 'do_grad', 'do_helix', 'highres_limit', 'min_dedicated', 'nr_classes', 'nr_iter_em', 'nr_iter_grad', 'particle_diameter', 'tau_fudge'],
"relion.initialmodel":['nr_classes', 'nr_iter', 'particle_diameter','sym_name'],
"relion.class3d":['do_ctf_correction', 'do_zero_mask', 'do_fast_subsets', 'do_helix', 'do_local_ang_searches', 'dont_skip_align', 'highres_limit', 'ini_high', 'nr_classes', 'nr_iter', 'particle_diameter', 'range_psi', 'range_rot', 'range_tilt', 'sigma_angles', 'sym_name', 'tau_fudge'],
"relion.refine3d":['auto_faster', 'auto_local_sampling', 'do_ctf_correction', 'do_zero_mask', 'ini_high', 'other_args', 'particle_diameter', 'range_psi', 'range_rot', 'range_tilt', 'sampling', 'sym_name'],
"relion.external":['fn_exe', 'in_3dref', 'in_coords', 'in_mask', 'in_mic', 'in_mov', 'in_part','param1_label', 'param1_value', 'param2_label', 'param2_value', 'param3_label', 'param3_value', 'param4_label', 'param4_value', 'param5_label', 'param5_value', 'param6_label', 'param6_value', 'param7_label', 'param7_value', 'param8_label', 'param8_value', 'param9_label', 'param9_value'],
"relion.localres.own":['do_queue', 'fn_mask', 'fn_mtf', 'maxres', 'minres', 'stepres'],
"relion.ctfrefine":['do_4thorder', 'do_aniso_mag', 'do_astig', 'do_bfactor', 'do_ctf', 'do_defocus', 'do_phase', 'do_tilt', 'do_trefoil', 'minres'],
"relion.ctfrefine.anisomag":['do_4thorder', 'do_aniso_mag', 'do_astig', 'do_bfactor', 'do_ctf', 'do_defocus', 'do_phase', 'do_tilt', 'do_trefoil', 'minres'],
"relion.polish":['do_polish', 'do_float16', 'do_own_params', 'do_param_optim', 'eval_frac', 'extract_size', 'first_frame', 'last_frame', 'maxres', 'min_dedicated', 'minres', 'opt_params', 'optim_min_part', 'rescale', 'sigma_acc', 'sigma_div', 'sigma_vel'],
"relion.polish.train":['do_polish', 'do_float16', 'do_own_params', 'do_param_optim', 'eval_frac', 'extract_size', 'first_frame', 'last_frame', 'maxres', 'min_dedicated', 'minres', 'opt_params', 'optim_min_part', 'rescale', 'sigma_acc', 'sigma_div', 'sigma_vel'],
"relion.postprocess":['adhoc_bfac', 'angpix', 'autob_lowres', 'do_adhoc_bfac', 'do_auto_bfac','do_skip_fsc_weighting', 'low_pass', 'min_dedicated'],
"relion.maskcreate":['angpix', 'extend_inimask', 'inimask_threshold', 'lowpass_filter', 'width_mask_edge']
}
### Project directory
relion_wd = os.getcwd()
print('starting up relion_live dashboard in ' + str(relion_wd) + ' ...')
### Initialising dash APP ###
assets_path = relion_wd # this reads the whole folder (!!) so takes long if it is a big project
app = dash.Dash(
__name__,
assets_folder=assets_path,
meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}],
)
app.title = "RELION Analyse Dashboard"
server = app.server
### APP Layout ###
def serve_layout():
return html.Div([
# Title
html.Div(style=header_style,children=[
html.Div(children=[
html.H1("RELION", style=title1_style),
html.H2("Analyse Dashboard", style=title2_style),
]),
html.Div(style=header_button_style, children=[
html.A(html.Button('Reload pipeline', style=bt_style), id='RefreshPage', href='/'),
]),
]),
# Tabs
html.Div([
dcc.Tabs([
# Tab Pipeline
dcc.Tab(label=' Relion Pipeline', children=[
html.Div(style={'width':'10%'}, children=[
# Reload Graph button
html.A(html.Button('Reload graph', style=bt_style), id='reloadgraphbutton', href='/'),
]),
html.Div(children=[
html.Div(style={'width':'100%'},children=[
# Graphs
cyto.Cytoscape(id='pipeline_graph', layout={'name': 'dagre'},
style={'width': '100%', 'height': '800px'},
boxSelectionEnabled=True,
elements=[],
stylesheet=nodes_style),
html.Div(id='hovered_node_info', style=hover_style),
]),
]),
], style=tab_style, selected_style=tab_selected_style),
# Tab Analyse Micrographs
dcc.Tab(label=' Analyse Micrographs', children=[
html.Div(style=tab_left_div_style, children=[
# Dropdowns for starfile selection
html.H5('Micrograph starfile to analyse', style=H5_title_style),
dcc.Dropdown(id='mic_star', placeholder='choose starfile...', options=pipeline_reader1('default_pipeline.star', 'MicrographsData'), style=dd_style),
# Dropdowns for variable selection
html.H5('Choose axes (x, y and colouring)', style=H5_title_style),
html.Div(children=[
dcc.Dropdown(id='mic_dropdown_x', value='Index', options=[], style=dd_style),
dcc.Dropdown(id='mic_dropdown_y', value='rlnCtfMaxResolution', options=[], style=dd_style),
dcc.Dropdown(id='mic_dropdown_class', value='rlnCtfFigureOfMerit', options=[], style=dd_style),
]),
# Scatter plot actions
html.H5("SCATTER PLOT", style=H5_title_style),
# Selected micrographs print and export button
html.H5("Export selection based on SCATTER plot (basename):", style={'font-family':'Helvetica', 'font-weight':'regular'}),
html.Div(style={'width':'90%', 'vertical-align':'center', 'display':'flex', 'justify-content': 'space-between'}, children=[
html.Div(style={'width':'40%'}, children=[
dcc.Input(id='basename_export_mic', value='export', style=box_style),
]),
html.Div(style={'width':'20%'}, children=[
html.Button('Export', id='export_micrographs_button', style=bt_style),
]),
html.Div(style={'width':'20%'}, children=[
html.Button('Display', id='display_sel_mic', style=bt_style),
]),
]),
# Selected micrographs list
html.Div(style={'width':'95%','margin-right':'1%','margin-left':'1%'},children=[
html.Pre(id='selected_micrographs' , style=pre_style),
]),
]),
html.Div(style=tab_right_div_style,children=[
html.Div(style={'width':'100%'},children=[
# Graphs
dcc.Graph(id='mic_scatter2D_graph', figure={}, style={'display': 'inline-block', 'width': '80%', 'height': '50vh'}),
# Mic-CTF pngs
html.Div([
dcc.Graph(id='mic_png', figure={}, responsive=True, style={"width" : "28vw", "height" : "20vw"}),
dcc.Graph(id='ctf_png', figure={}, responsive=True, style={"width" : "20vw", "height" : "20vw"}),
], style={"display": "grid", "grid-template-columns": "50% 50%", "justify-items": "start"})
]),
]),
], style=tab_style, selected_style=tab_selected_style),
# Tab Analyse Particles
dcc.Tab(label=' Analyse Particles', children=[
html.Div(style=tab_left_div_style,children=[
# Dropdowns for starfile selection
html.H5('Particle starfile to analyse', style=H5_title_style),
dcc.Dropdown(id='ptcl_star', placeholder='choose starfile...', options=pipeline_reader1('default_pipeline.star', 'ParticlesData'), style=dd_style),
# Dropdowns for variable selection
html.H5('Choose axes (x, y and colouring)', style={'font-family':'Helvetica', 'font-weight':'regular'}),
html.Div(children=[
dcc.Dropdown(id='ptcl_dropdown_x', value='rlnDefocusU', options=[], style=dd_style),
dcc.Dropdown(id='ptcl_dropdown_y', value='rlnDefocusV', options=[], style=dd_style),
dcc.Dropdown(id='ptcl_dropdown_class', value='rlnOpticsGroup', options=[], style=dd_style),
]),
# Selected particles print and export button
html.H5("Export selection (basename):", style=H5_title_style),
html.Div(style={'width':'90%', 'vertical-align':'center', 'display':'flex', 'justify-content': 'space-between'}, children=[
html.Div(style={'width':'40%'}, children=[
dcc.Input(id='basename_export_ptcl', value='exported', style=box_style),
]),
html.Div(style={'width':'50%'}, children=[
html.Button('Export', id='export_particles_button', style=bt_style),
]),
]),
# Selected particles list
html.Div(style={'width':'95%','margin-right':'1%','margin-left':'1%'},children=[
html.Pre(id='selected_particles' , style=pre_style),
]),
]),
html.Div(style=tab_right_div_style,children=[
html.Div(style={'width':'100%'},children=[
# Graphs
dcc.Graph(id='ptcl_scatter2D_graph', figure={}, style={'display': 'inline-block', 'width': '80%', 'margin-lef':'15px', 'height': '70vh'}),
]),
]),
], style=tab_style, selected_style=tab_selected_style),
# Tab Analyse 2D Classification
dcc.Tab(label=' Analyse 2D Classification', children=[
html.Div(style=tab_left_div_style,children=[
# Dropdowns
html.H5('Classification job to analyse', style=H5_title_style),
dcc.Dropdown(id='C2Djob2follow', placeholder='choose job to follow...', options=pipeline_reader2('default_pipeline.star', 'Class2D'), style=dd_style),
dcc.Input(id='C2Dfollow_msg', type='text', debounce=True, style=box_style),
html.H5('Select variable to plot', style=H5_title_style),
dcc.Dropdown(id='C2Dfollow_dropdown_y', value='rlnChangesOptimalClasses', options=[], style=dd_style),
# Buttons
html.H5("Display last iteration (external):", style=H5_title_style),
html.Div(style={'width':'100%', 'vertical-align':'center', 'display':'flex', 'justify-content': 'space-between'}, children=[
html.Div(style={'width':'50%'}, children=[
html.Button('Display classes (RELION)', id='C2Ddisplay_last_ite', style=bt_style),
]),
]),
]),
html.Div(style=tab_right_div_style,children=[
html.Div(style={'width':'50%', 'vertical-align':'top' , 'display':'inline-block'},children=[
# Plot follow progression
dcc.Graph(id='C2Dfollow_graph', figure={}),
]),
html.Div(style={'width':'50%', 'vertical-align':'top' , 'display':'inline-block'},children=[
# Plot follow classes
dcc.Graph(id='C2Dclassnumber_graph', figure={}),
]),
])], style=tab_style, selected_style=tab_selected_style),
# Tab Analyse 3D Classification
dcc.Tab(label=' Analyse 3D Classification', children=[
html.Div(style=tab_left_div_style,children=[
# Dropdowns
html.H5('Classification job to analyse', style=H5_title_style),
dcc.Dropdown(id='job2follow', placeholder='choose job to follow...', options=pipeline_reader2('default_pipeline.star', 'Class3D'), style=dd_style),
html.H5('Select variable to plot', style=H5_title_style),
dcc.Input(id='follow_msg', type='text', debounce=True, style=box_style),
dcc.Dropdown(id='follow_dropdown_y', value='rlnChangesOptimalClasses', options=[], style=dd_style),
dcc.Dropdown(id='follow_model_dropdown_y', value='rlnSpectralOrientabilityContribution', options=[], style=dd_style),
# Buttons
html.H5("Display last iteration (external):", style=H5_title_style),
html.Div(style={'width':'100%', 'vertical-align':'center', 'display':'flex', 'justify-content': 'space-between'}, children=[
html.Div(style={'width':'50%'}, children=[
html.Button('Display classes (RELION)', id='display_last_ite', style=bt_style),
]),
html.Div(style={'width':'50%'}, children=[
html.Button('Display maps (Chimera)', id='display_chimera_last_ite', style=bt_style),
]),
]),
]),
html.Div(style=tab_right_div_style,children=[
html.Div(style={'width':'33%', 'vertical-align':'top' , 'display':'inline-block'},children=[
# Plot follow progression
dcc.Graph(id='follow_graph', figure={}),
]),
html.Div(style={'width':'33%', 'vertical-align':'top' , 'display':'inline-block'},children=[
# Plot follow classes
dcc.Graph(id='classnumber_graph', figure={}),
]),
html.Div(style={'width':'33%', 'vertical-align':'top' , 'display':'inline-block'},children=[
# Plot FSC
dcc.Graph(id='follow_model', figure={})
]),
]),
html.Div(style=tab_bottom_right_style,children=[
html.Div(style={'width':'100%', 'vertical-align':'top' , 'display':'inline-block'},children=[
# Plot follow progression
dcc.Graph(id='angdist_per_class', figure={}),
]),
])], style=tab_style, selected_style=tab_selected_style),
# Tab Analyse 3D Refinement
dcc.Tab(label=' Analyse 3D Refinement', children=[
html.Div(style=tab_left_div_style,children=[
# Dropdowns
html.H5('Refine3D job to analyse', style=H5_title_style),
dcc.Dropdown(id='ref_job2follow', placeholder='choose job to follow...', options=pipeline_reader2('default_pipeline.star', 'Refine3D'), style=dd_style),
html.H5('Select variable to plot', style=H5_title_style),
dcc.Input(id='ref_follow_msg', type='text', debounce=True, style=box_style),
dcc.Dropdown(id='ref_follow_dropdown_y', value='rlnCurrentResolution', options=[], style=dd_style),
dcc.Dropdown(id='ref_follow_model_dropdown_y', value='rlnGoldStandardFsc', options=[], style=dd_style),
# Buttons
html.H5("Display last iteration (external):", style=H5_title_style),
html.Div(style={'width':'100%', 'vertical-align':'center', 'display':'flex', 'justify-content': 'space-between'}, children=[
html.Div(style={'width':'50%'}, children=[
html.Button('Display slices (RELION)', id='ref_display_last_ite', style=bt_style),
]),
html.Div(style={'width':'50%'}, children=[
html.Button('Display maps (Chimera)', id='ref_display_chimera_last_ite', style=bt_style),
]),
]),
]),
html.Div(style=tab_right_div_style,children=[
html.Div(style={'width':'33%', 'vertical-align':'top' , 'display':'inline-block'},children=[
# Plot follow progression
dcc.Graph(id='ref_follow_graph', figure={}),
]),
html.Div(style={'width':'33%', 'vertical-align':'top' , 'display':'inline-block'},children=[
# Plot FSC
dcc.Graph(id='ref_follow_model', figure={})
]),
html.Div(style={'width':'33%', 'vertical-align':'top' , 'display':'inline-block'},children=[
# Plot follow progression
dcc.Graph(id='ref_follow_angdist', figure={}),
]),
])], style=tab_style, selected_style=tab_selected_style),
], style=tabs_style),
]),
])
app.layout = serve_layout
### Callbacks
## Callback reload pipeline
@app.callback(
[Output(component_id='RefreshPage', component_property='title')],
[Input(component_id='RefreshPage', component_property='n_clicks')],
prevent_initial_call=True)
def reload_pipeline(pipeline_reload_button_press):
pipeline_reload_button_press_changed = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'pipeline_reload_button' in pipeline_reload_button_press_changed:
print('reloading pipeline')
bttitle = ''
return ([bttitle])
## Callback Pipeline Graph
@app.callback(
[Output(component_id='pipeline_graph', component_property='elements'),
Output(component_id='hovered_node_info', component_property='children'),
Output(component_id='hovered_node_info', component_property='style')],
[Input(component_id='reloadgraphbutton', component_property='n_clicks'),
Input(component_id='pipeline_graph', component_property='tapNodeData'),
State(component_id='pipeline_graph', component_property='elements')],
prevent_initial_call=False)
def plot_pipeline(reloadgraphbutton, tapdata, current_cytoelements):
# If callback has been triggered by reaload button or is the first launch,
# then read pipeline data and create Cytoscape graph
if 'pipeline_graph' != ctx.triggered_id:
print('reloading graph')
# Read default_pipeline starfile
pipe_nodes = starfile.read('default_pipeline.star')['pipeline_processes']['rlnPipeLineProcessName']
pipe_input_edges = starfile.read('default_pipeline.star')['pipeline_input_edges']
cytoelements = []
for i in pipe_nodes:
cytoelements.append({'data': {'id':i[-7:-1], 'label':i}, 'classes':i[:-8]})
for i, row in pipe_input_edges.iterrows():
try: cytoelements.append({'data': {'source':re.search(r'job\d\d\d', row['rlnPipeLineEdgeFromNode'])[0], 'target':row['rlnPipeLineEdgeProcess'][-7:-1]}})
except:
print('there\'s a problem with node: '+row['rlnPipeLineEdgeFromNode'])
continue
job_data_output_ = "No node selected, nothing to display"
hoverstyle = None
# Otherwise, show clicked job's information
else:
cytoelements = current_cytoelements
try:
job_file = str(tapdata['label'])+str('job.star')
print(job_file)
job_data = starfile.read(job_file)
job_type = job_data['job']['rlnJobTypeLabel'][0]
with open(job_file, 'r') as file: job_data_output = file.read()
#with starfile.read(job_file) as ffile: print(ffile)
job_data_output_ = []
job_data_output_.append(job_file)
job_data_output_.append(html.Br())
job_data_output_.append(html.Br())
for i in job_specific_data[job_type]:
val = job_data['joboptions_values']['rlnJobOptionValue'][job_data['joboptions_values']['rlnJobOptionVariable'] == i].to_string(index=False)
line = str(i) + ':\t' + val
job_data_output_.append(line)
job_data_output_.append(html.Br())
hoverstyle = hover_style
except:
job_data_output_ = "No node selected, nothing to display"
hoverstyle = None
return ([cytoelements, job_data_output_, hoverstyle])
## Callback Micrographs
@app.callback(
[Output(component_id='mic_dropdown_x', component_property='options'),
Output(component_id='mic_dropdown_y', component_property='options'),
Output(component_id='mic_dropdown_class', component_property='options'),
Output(component_id='mic_scatter2D_graph', component_property='figure'),
Output(component_id='selected_micrographs', component_property='children')
],
[Input(component_id='mic_star', component_property='value'),
Input(component_id='mic_dropdown_x', component_property='value'),
Input(component_id='mic_dropdown_y', component_property='value'),
Input(component_id='mic_dropdown_class', component_property='value'),
State(component_id='mic_scatter2D_graph', component_property='selectedData'),
State(component_id='basename_export_mic', component_property='value'),
Input(component_id='export_micrographs_button', component_property='n_clicks'),
Input(component_id='display_sel_mic', component_property='n_clicks'),
]
)
def load_df_and_graphs(mic_star, mic_dd_x, mic_dd_y, mic_dd_class,
selectedMicData, basename_export_mic,
exportMic_button_press, display_sel_mic_button_press):
### Micrographs
# Importing CTF starfile data
try:
ctf_df_optics = starfile.read(mic_star)['optics']
ctf_df = starfile.read(mic_star)['micrographs']
except:
print('No valid starfile selected')
raise PreventUpdate
# Importing MotionCorr data from all CTF-corrected micrographs, even if they
# come from different motioncor jobs.
motion_df = ''
for i in ctf_df['rlnMicrographName'].str[:18].unique():
motion_star_path = str(i)+'/corrected_micrographs.star'
try:
if type(motion_df) == type('') :
motion_df = starfile.read(motion_star_path)['micrographs']
else:
motion_df = pd.concat([motion_df , starfile.read(motion_star_path)['micrographs']], ignore_index = True)
except:
print("motion_df empty, can't find the files")
# Merging CTF and MotionCorr data in a single dataframe for easy plotting
if type(motion_df) == pd.DataFrame:
motion_df = motion_df.drop('rlnOpticsGroup', axis=1)
job_df = pd.merge(ctf_df, motion_df).drop(columns=['rlnMicrographName', 'rlnCtfPowerSpectrum', 'rlnMicrographMetadata']) # what if they don't match?
job_df.insert(0, 'Index', job_df.index)
else:
job_df = ctf_df
print("motion_df empty, can't find the files")
mic_dropdown_x = list(job_df)
mic_dropdown_y = list(job_df)
mic_dropdown_class = list(job_df)
# Duplicating df info (really needed?)
mic_dff = job_df.copy()
# Color definitions
color1 = 'cornflowerblue'
if mic_dd_class == None: mic_dd_class = 'cornflowerblue'
else: mic_dd_class = mic_dff[mic_dd_class].astype(float)
# Plot using WebGL given probably large dataframes
mic_scatter2D = plot_scattergl(mic_dff, mic_dd_x, mic_dd_y, mic_dd_class, 'Viridis', 'cornflowerblue')
mic_scatter2D.update_layout()
# Parsing info from manual on-plot selection
selected_micrographs_indices = []
NOTselected_micrographs_indices = []
if isinstance(selectedMicData, dict):
print("selecting based on scatter plot selection")
for i in selectedMicData['points']:
selected_micrographs_indices.append(int(i['pointIndex']))
NOTselected_micrographs = mic_dff.loc[mic_dff.index.difference(selected_micrographs_indices)]
NOTselected_micrographs_indices = list(NOTselected_micrographs.index.values)
# Output definitions
selectionMic_output = 'You\'ve selected '+str(len(selected_micrographs_indices))+' micrographs on SCATTER plot with indices: '+str(selected_micrographs_indices)
outfile_mic_YES = str(basename_export_mic + '_selected_micrographs.star')
outfile_mic_NO = str(basename_export_mic + '_not_selected_micrographs.star')
exportMic_button_press_changed = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'export_micrographs_button' in exportMic_button_press_changed:
dict_mic_output_YES = {'optics' : ctf_df_optics , 'micrographs' : ctf_df.iloc[selected_micrographs_indices]}
dict_mic_output_NO = {'optics' : ctf_df_optics , 'micrographs' : ctf_df.iloc[NOTselected_micrographs_indices]}
starfile.write(dict_mic_output_YES, outfile_mic_YES, overwrite=True)
starfile.write(dict_mic_output_NO, outfile_mic_NO, overwrite=True)
print('Exported selected micrographs as '+ outfile_mic_YES + ' and not selected micrographs as ' + outfile_mic_NO)
# Display mics
display_sel_mic_button_press_changed = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'display_sel_mic' in display_sel_mic_button_press_changed:
os.system(str('`which relion_display` --i '+outfile_mic_YES+' --gui '))
### RETURN
return (mic_dropdown_x, mic_dropdown_y, mic_dropdown_class, mic_scatter2D, selectionMic_output)
@app.callback(
[Output(component_id='mic_png', component_property='figure'),
Output(component_id='ctf_png', component_property='figure')],
[Input(component_id='mic_star', component_property='value'),
Input(component_id='mic_scatter2D_graph', component_property='clickData')]
)
def load_mic_ctf(ctfstar_path, clickdata):
# If there is no job file selected
if ctfstar_path is None:
mic_png = empty_graph("No micrograph data loaded")
ctf_png = empty_graph("No CTF data loaded")
# If no image is selecetd
elif clickdata is None:
mic_png = empty_graph("No micrograph selected")
ctf_png = empty_graph("No CTF selected")
else:
# Get image index
point_number = clickdata['points'][0]['pointNumber']
ctf_df = starfile.read(ctfstar_path)['micrographs']
# ctf_df.index += 1
# Get image path
mic_file_df = ctf_df['rlnMicrographName']
ctf_file_df = ctf_df['rlnCtfImage']
mic_file = mic_file_df[point_number].replace('mrc','png')
ctf_file = ctf_file_df[point_number].replace('ctf:mrc','png')
## Load Images
try:
mic_png_src = app.get_asset_url(mic_file)
mic_image = Image.open(mic_file)
mic_png = image_to_figure(mic_png_src, mic_image.size[0], mic_image.size[1], 1) #enables to zoom in the image
except:
mic_png = empty_graph("Error, ensure you have run png_out.py")
try:
ctf_png_src = app.get_asset_url(ctf_file)
ctf_image = Image.open(ctf_file)
ctf_png = image_to_figure(ctf_png_src, ctf_image.size[0], ctf_image.size[1], 1)
except:
ctf_png = empty_graph("Error, ensure you have run png_out.py")
return (mic_png, ctf_png)
## Callback Particles
@app.callback(
[Output(component_id='ptcl_dropdown_x', component_property='options'),
Output(component_id='ptcl_dropdown_y', component_property='options'),
Output(component_id='ptcl_dropdown_class', component_property='options'),
Output(component_id='ptcl_scatter2D_graph', component_property='figure'),
Output(component_id='selected_particles', component_property='children')],
[Input(component_id='ptcl_star', component_property='value'),
Input(component_id='ptcl_dropdown_x', component_property='value'),
Input(component_id='ptcl_dropdown_y', component_property='value'),
Input(component_id='ptcl_dropdown_class', component_property='value'),
State(component_id='ptcl_scatter2D_graph', component_property='selectedData'),
State(component_id='basename_export_ptcl', component_property='value'),
Input(component_id='export_particles_button', component_property='n_clicks'),
]
)
def load_df_and_graphs(ptcl_star, ptcl_dd_x, ptcl_dd_y, ptcl_dd_class,selectedPtclData,basename_export_ptcl,exportPtcl_button_press):
### Particles
# Importing particles starfile data
try:
ptcl_df = starfile.read(ptcl_star)['particles']
ptcl_df_optics = starfile.read(ptcl_star)['optics']
except:
print('No starfile selected')
raise PreventUpdate
# Exclude name labels for dropdowns
dropdown_labels = [label for label in list(ptcl_df) if 'Name' not in label]
ptcl_dropdown_x = dropdown_labels
ptcl_dropdown_y = dropdown_labels
ptcl_dropdown_class = dropdown_labels
# Duplicating df info
ptcl_dff = ptcl_df.copy()
# Color definitions
color1 = 'cornflowerblue'
if ptcl_dd_class == None: ptcl_dd_class = 'cornflowerblue'
else: ptcl_dd_class = ptcl_dff[ptcl_dd_class].astype(float)
# Particles plot using WebGL given probably large dataframes
ptcl_scatter2D = plot_scattergl(ptcl_dff, ptcl_dd_x, ptcl_dd_y, ptcl_dd_class, 'Viridis', 'cornflowerblue')
# Parsing info from manual on-plot selection
selected_particle_indices = []
NOTselected_particle_indices = []
if isinstance(selectedPtclData, dict):
for i in selectedPtclData['points']:
selected_particle_indices.append(int(i['pointIndex']))
NOTselected_particles = ptcl_dff.loc[ptcl_dff.index.difference(selected_particle_indices)]
NOTselected_particle_indices = list(NOTselected_particles.index.values)
# Output definitions
selectionPtcl_output = 'You\'ve selected '+str(len(selected_particle_indices))+' particles with indices: '+str(selected_particle_indices)
outfile_ptcl_YES = str(basename_export_ptcl + '_selected_particles.star')
outfile_ptcl_NO = str(basename_export_ptcl + '_not_selected_particles.star')
exportPtcl_button_press_changed = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'export_particles_button' in exportPtcl_button_press_changed:
dict_ptcl_output_YES = {'optics' : ptcl_df_optics , 'particles' : ptcl_df.iloc[selected_particle_indices]}
dict_ptcl_output_NO = {'optics' : ptcl_df_optics , 'particles' : ptcl_df.iloc[NOTselected_particle_indices]}
starfile.write(dict_ptcl_output_YES, outfile_ptcl_YES, overwrite=True)
starfile.write(dict_ptcl_output_NO, outfile_ptcl_NO, overwrite=True)
print('Exported selected particles as '+ outfile_ptcl_YES + ' and not selected particles as ' + outfile_ptcl_NO)
### RETURN
return ([ptcl_dropdown_x, ptcl_dropdown_y, ptcl_dropdown_class, ptcl_scatter2D, selectionPtcl_output])
## Callback 2D Classification
@app.callback(
[Output(component_id='C2Dfollow_msg', component_property='value'),
Output(component_id='C2Dfollow_graph', component_property='figure'),
Output(component_id='C2Dclassnumber_graph', component_property='figure'),
Output(component_id='C2Dfollow_dropdown_y', component_property='options')],
[Input(component_id='C2Djob2follow', component_property='value'),
Input(component_id='C2Dfollow_dropdown_y', component_property='value'),
Input(component_id='C2Ddisplay_last_ite', component_property='n_clicks'),
]
)
def load_df_and_graphs(C2Djob2follow, C2Dfollow_dd_y, C2Ddisplay_last_ite_button_press):
### Follow 2D
job = C2Djob2follow
follow_dd_y = C2Dfollow_dd_y
if 'Class2D' in str(job):
C2Dfollow_msg = 'Following Class2D job: '+str(job)
# Get a list of files matching regular expression "run_it*_optimiser.star"
all_opt = glob.glob(os.path.join(job+'run_it*_optimiser.star'))
all_opt.sort()
stars_opt = []
# Read each file and get it's information
for filename in all_opt:
optimiser_df = starfile.read(filename)
stars_opt.append(optimiser_df)
# Build dataframe
follow_opt_df = pd.concat(stars_opt, axis=0, ignore_index=True)
C2Dfollow_dd_y_list = list(follow_opt_df)
# Get a list of files matching regular expression "run_it*_model.star"
all_model = glob.glob(os.path.join(job+'run_it*_model.star'))