-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
813 lines (718 loc) · 29.9 KB
/
app.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
import pathlib
import numpy as np
from shiny import reactive, req
from shiny.express import input, ui, render
from shinywidgets import render_plotly
import helicon
from . import compute
params = reactive.value(None)
data_all = reactive.value(None)
abundance = reactive.value([])
image_size = reactive.value(0)
displayed_class_ids = reactive.value([])
displayed_class_images = reactive.value([])
displayed_class_title = reactive.value("Select class(es):")
displayed_class_labels = reactive.value([])
initial_selected_image_indices = reactive.value([0])
selected_images = reactive.value([])
selected_image_labels = reactive.value([])
selected_helices = reactive.value(([], [], 0))
retained_helices_by_length = reactive.value([])
pair_distances = reactive.value([])
df_selected_helices = reactive.value(([], [], 0))
pair_distances_df_selected = reactive.value([])
ui.head_content(ui.tags.title("HelicalPitch"))
helicon.shiny.google_analytics(id="G-998MGRETTF")
helicon.shiny.setup_ajdustable_sidebar()
helicon.shiny.set_input_text_numeric_update_on_change()
ui.tags.style(
"""
* { font-size: 10pt; padding:0; border: 0; margin: 0; }
aside {--_padding-icon: 10px;}
"""
)
urls = {
"empiar-10940_job010": (
"https://ftp.ebi.ac.uk/empiar/world_availability/10940/data/EMPIAR/Class2D/job010/run_it020_data.star",
"https://ftp.ebi.ac.uk/empiar/world_availability/10940/data/EMPIAR/Class2D/job010/run_it020_classes.mrcs",
)
}
url_key = "empiar-10940_job010"
with ui.sidebar(
width="33vw", style="display: flex; flex-direction: column; height: 100%;"
):
with ui.div(id="input_files", style="flex-shrink: 0;"):
ui.input_radio_buttons(
"input_mode_params",
"How to obtain the Class2D parameter file:",
choices=["upload", "url"],
selected="url",
inline=True,
)
with ui.panel_conditional("input.input_mode_params === 'upload'"):
ui.input_file(
"upload_params",
"Upload the class2d parameters in a RELION star or cryoSPARC cs file",
accept=[".star", ".cs"],
placeholder="star or cs file",
)
with ui.panel_conditional("input.input_mode_params === 'url'"):
ui.input_text(
"url_params",
"Download URL for a RELION star or cryoSPARC cs file",
value=urls[url_key][0],
)
ui.input_radio_buttons(
"input_mode_classes",
"How to obtain the class average images:",
choices=["upload", "url"],
selected="url",
inline=True,
)
with ui.panel_conditional("input.input_mode_classes === 'upload'"):
ui.input_file(
"upload_classes",
"Upload the class averages in MRC format (.mrcs, .mrc)",
accept=[".mrcs", ".mrc"],
placeholder="mrcs or mrc file",
)
with ui.panel_conditional("input.input_mode_classes === 'url'"):
ui.input_text(
"url_classes",
"Download URL for a RELION or cryoSPARC Class2D output mrc(s) file",
value=urls[url_key][1],
)
ui.input_task_button("run", label="Run", style="width: 100%;")
with ui.div(id="class-selection", style="flex-grow: 1; overflow-y: auto;"):
helicon.shiny.image_select(
id="select_classes",
label=displayed_class_title,
images=displayed_class_images,
image_labels=displayed_class_labels,
image_size=reactive.value(128),
initial_selected_indices=initial_selected_image_indices,
)
@reactive.effect
@reactive.event(input.select_classes)
def update_selected_images():
selected_images.set(
[displayed_class_images()[i] for i in input.select_classes()]
)
selected_image_labels.set(
[displayed_class_labels()[i] for i in input.select_classes()]
)
title = "HelicalPitch: determine helical pitch/twist using 2D Classification info"
ui.h1(title, style="font-weight: bold;")
with ui.layout_columns(col_widths=(5, 7, 12)):
with ui.card():
with ui.div(style="max-height: 40vh; overflow-y: auto;"):
@render.ui
@reactive.event(selected_images)
def display_selected_images():
return helicon.shiny.image_gallery(
id="display_selected_image",
label=reactive.value("Selected classe(s):"),
images=selected_images,
image_labels=selected_image_labels,
)
with ui.layout_columns(col_widths=[12, 12], style="align-items: flex-end;"):
@render_plotly
@reactive.event(selected_helices, input.bins)
def lengths_histogram_display():
req(input.bins() is not None and input.bins() > 0)
fig = getattr(lengths_histogram_display, "fig", None)
helices, lengths, count = selected_helices()
data = lengths
class_indices = [
str(displayed_class_ids()[i] + 1) for i in input.select_classes()
]
title = f"Filament Lengths: Class {' '.join(class_indices)}<br><i>{len(helices):,} filaments | {count:,} segments</i>"
xlabel = "Filament Legnth (Å)"
ylabel = "# of Filaments"
log_y = True
nbins = input.bins()
fig = compute.plot_histogram(
data=data,
title=title,
xlabel=xlabel,
ylabel=ylabel,
bins=nbins,
log_y=log_y,
fig=fig,
)
lengths_histogram_display.fig = fig
return fig
with ui.layout_columns(
col_widths=[6, 6, 12], style="align-items: flex-end;"
):
ui.input_numeric(
"min_len", "Minimal length (Å)", min=0.0, value=0, step=1.0
)
ui.input_numeric(
"rise",
"Helical rise (Å)",
min=0.01,
max=1000.0,
value=4.75,
step=0.01,
)
with ui.accordion(id="additional_parameters", open=False):
with ui.accordion_panel(title="Additional parameters:"):
with ui.layout_columns(
col_widths=6, style="align-items: flex-start;"
):
ui.input_checkbox(
"ignore_blank", "Ignore blank classes", value=True
)
ui.input_checkbox(
"sort_abundance",
"Sort the classes by abundance",
value=True,
)
ui.input_checkbox(
"auto_min_len",
"Auto-set minimal filament length",
value=True,
)
ui.input_checkbox(
"show_sharable_url",
"Show sharable URL",
value=False,
)
with ui.layout_columns(
col_widths=6, style="align-items: flex-end;"
):
ui.input_numeric(
"max_len",
"Maximal length (Å)",
min=-1,
value=-1,
step=1.0,
)
ui.input_numeric(
"max_pair_dist",
"Maximal pair distance (Å) to plot",
min=-1,
value=-1,
step=1.0,
)
ui.input_numeric(
"bins",
"Number of histogram bins",
min=1,
value=100,
step=1,
)
@reactive.effect
@reactive.event(input.min_len)
def _():
ui.update_numeric("max_len", min=input.min_len())
if 0 < input.max_len() < input.min_len():
ui.update_numeric("max_len", value=-1)
@reactive.effect
@reactive.event(input.max_len)
def _():
if input.max_len() > 0:
ui.update_numeric("min_len", max=input.max_len())
if input.min_len() >= input.max_len():
ui.update_numeric("min_len", value=0)
@render.data_frame
@reactive.event(params, input.select_classes)
def display_helices_dataframe():
df = params()
# Group df by helixID and create a summary dataframe
summary_df = (
df.groupby("helixID")
.agg(
{
"length": "first",
"rlnClassNumber": lambda x: list(x.value_counts().index),
"rlnMicrographName": "first",
}
)
.reset_index()
)
summary_df = summary_df.rename(columns={"rlnClassNumber": "classes"})
if len(input.select_classes()):
selected_classes = [
int(displayed_class_ids()[i]) + 1 for i in input.select_classes()
]
summary_df = summary_df[
summary_df["classes"].apply(
lambda x: any(cls in selected_classes for cls in x)
)
]
summary_df["classes"] = summary_df["classes"].apply(
lambda x: ",".join(map(str, x))
)
summary_df = summary_df.sort_values("length", ascending=False)
# Use the summary dataframe for display
df = summary_df
return render.DataGrid(
summary_df,
selection_mode="rows",
filters=True,
height="30vh",
width="100%",
)
@reactive.effect
def get_df_selected_helices():
df_selected = display_helices_dataframe.data_view(selected=True)
df_selected_helixids = df_selected['helixID'].tolist()
mask = params()["helixID"].astype(int).isin(df_selected_helixids)
particles = params().loc[mask, :]
class_indices = [displayed_class_ids()[i] for i in input.select_classes()]
helices = compute.select_classes(params=particles, class_indices=class_indices)
#if len(helices):
# class_indices2 = (
# np.unique(
# np.concatenate([h["rlnClassNumber"] for hi, h in helices])
# ).astype(int)
# - 1
# )
# assert set(class_indices) == set(class_indices2)
if len(helices):
filement_lengths = compute.get_filament_length(helices=helices)
segments_count = np.sum([abundance()[i] for i in class_indices])
else:
filement_lengths = []
segments_count = 0
df_selected_helices.set((helices, filement_lengths, segments_count))
with ui.card(max_height="90vh"):
@render_plotly
@reactive.event(pair_distances, input.bins, input.max_pair_dist, input.rise)
def pair_distances_histogram_display():
req(input.bins() is not None and input.bins() > 0)
req(input.max_pair_dist() is not None)
req(input.rise() is not None and input.rise() > 0)
fig = getattr(pair_distances_histogram_display, "fig", None)
data = pair_distances()
segment_count = np.sum([len(h) for hi, h in retained_helices_by_length()])
if len(retained_helices_by_length()):
class_indices = np.unique(
np.concatenate(
[h["rlnClassNumber"] for hi, h in retained_helices_by_length()]
)
).astype(int)
else:
class_indices = []
class_indices = [
str(displayed_class_ids()[i] + 1)
for i in input.select_classes()
if (displayed_class_ids()[i] + 1) in class_indices
]
rise = input.rise()
log_y = True
title = f"Pair Distances: Class {' '.join(class_indices)}<br><i>{len(retained_helices_by_length())} filaments | {segment_count:,} segments | {len(pair_distances()):,} segment pairs"
xlabel = "Pair Distance (Å)"
ylabel = "# of Pairs"
nbins = input.bins()
max_pair_dist = input.max_pair_dist()
fig = compute.plot_histogram(
data=data,
title=title,
xlabel=xlabel,
ylabel=ylabel,
max_pair_dist=max_pair_dist,
bins=nbins,
log_y=log_y,
show_pitch_twist=dict(rise=rise, csyms=(1, 2, 3, 4)),
multi_crosshair=True,
fig=fig,
)
pair_distances_histogram_display.fig = fig
return fig
ui.markdown(
"**How to interpret the histogram:** An informative histogram should have clear peaks with equal spacing. If so, hover your mouse pointer over the first major peak off the origin to align the vertical lines well with the peaks. Once you have decided on the line position, read the hover text, which shows the twist values assuming the pair distance is the helical pitch (adjusted for the cyclic symmetries around the helical axis). You need to decide which cyclic symmetry and the corresponding twist should be used. \n If the histogram does not show clear peaks, it indicates that the Class2D quality is bad. You might consider changing the 'Minimal length (Å)' from 0 to a larger value (for example, 1000 Å) to improve the peaks in the histogram. If that does not help, you might consider redoing the Class2D task with longer extracted segments (>0.5x helical pitch) from longer filaments (> 1x pitch)."
)
@render.ui
def _():
if len(pair_distances()) > 0:
download_ui = render.download(
label="Download selected helices", filename="helices.star"
)
@download_ui
def download_retained_helices():
req(retained_helices_by_length())
indices = np.concatenate(
[h.index for hi, h in retained_helices_by_length()]
)
params_to_save = params().iloc[indices]
import starfile
d = dict(
optics=params_to_save.attrs["optics"], particles=params_to_save
)
yield starfile.to_string(d)
return download_ui
else:
return None
@render_plotly
@reactive.event(pair_distances_df_selected, input.bins, input.max_pair_dist, input.rise)
def pair_distances_histogram_df_selected_display():
req(input.bins() is not None and input.bins() > 0)
req(input.max_pair_dist() is not None)
req(input.rise() is not None and input.rise() > 0)
fig = getattr(pair_distances_histogram_df_selected_display, "fig", None)
data = pair_distances_df_selected()
try:
pitch = compute.find_pitch(data)
print('Found pitch = '+ str(pitch))
except:
print('No pitch found')
(helices, filement_lengths, _) = df_selected_helices()
if len(helices):
class_indices = np.unique(
np.concatenate(
[h["rlnClassNumber"] for hi, h in helices]
)
).astype(int)
else:
class_indices = []
class_indices = [
str(displayed_class_ids()[i] + 1)
for i in input.select_classes()
if (displayed_class_ids()[i] + 1) in class_indices
]
segment_count = np.sum([len(h) for hi, h in helices])
rise = input.rise()
log_y = True
title = f"Pair Distances: Class {' '.join(class_indices)}<br><i>{len(helices)} filaments | {segment_count:,} segments | {len(pair_distances_df_selected()):,} segment pairs"
xlabel = "Pair Distance (Å)"
ylabel = "# of Pairs"
nbins = input.bins()
max_pair_dist = input.max_pair_dist()
fig = compute.plot_histogram(
data=data,
title=title,
xlabel=xlabel,
ylabel=ylabel,
max_pair_dist=max_pair_dist,
bins=nbins,
log_y=log_y,
show_pitch_twist=dict(rise=rise, csyms=(1, 2, 3, 4)),
multi_crosshair=True,
fig=fig,
)
pair_distances_histogram_df_selected_display.fig = fig
return fig
#@render.ui
#def get_download_estimated_first_peaks_ui():
# download_ui = render.download(
# label="Download estimated first peaks", filename="peaks.txt"
# )
# @download_ui
# def download_estimated_first_peaks():
# df_selected = display_helices_dataframe.data_view(selected=True)
# df_selected_helixids = df_selected['helixID'].tolist()
# out_str=""
# for hid in df_selected_helixids:
# mask = params()["helixID"].astype(int).isin([hid])
# particles = params().loc[mask, :]
# class_indices = [displayed_class_ids()[i] for i in input.select_classes()]
# helices = compute.select_classes(params=particles, class_indices=class_indices)
# dists, _ = compute.compute_pair_distances(helices=helices)
# try:
# first_peak,peaks = compute.find_first_peak(dists)
# except:
# first_peak=0
# peaks=[]
# out_str+= str(hid)+","+str(first_peak)+","+str(peaks)+"\n"
# yield out_str
#
# return download_ui
ui.HTML(
"<i><p>Developed by the <a href='https://jiang.bio.purdue.edu/HelicalPitch' target='_blank'>Jiang Lab</a>. Report issues to <a href='https://github.com/jianglab/HelicalPitch/issues' target='_blank'>HelicalPitch@GitHub</a>.</p></i>"
)
@reactive.effect
@reactive.event(input.run)
def get_class2d_from_upload():
req(input.input_mode_classes() == "upload")
fileinfo = input.upload_classes()
class_file = fileinfo[0]["datapath"]
try:
data, apix = compute.get_class2d_from_file(class_file)
nx = data.shape[-1]
except Exception as e:
print(e)
data, apix = None, 0
nx = 0
m = ui.modal(
f"failed to read the uploaded 2D class average images from {fileinfo[0]['name']}",
title="File upload error",
easy_close=True,
footer=None,
)
ui.modal_show(m)
data_all.set((data, apix))
image_size.set(nx)
@reactive.effect
@reactive.event(input.run)
def get_class2d_from_url():
req(input.input_mode_classes() == "url")
req(len(input.url_classes()) > 0)
url = input.url_classes()
try:
data, apix = compute.get_class2d_from_url(url)
nx = data.shape[-1]
except Exception as e:
print(e)
data, apix = None, 0
nx = 0
m = ui.modal(
f"failed to download 2D class average images from {input.url_classes()}",
title="File download error",
easy_close=True,
footer=None,
)
ui.modal_show(m)
data_all.set((data, apix))
image_size.set(nx)
@reactive.effect
@reactive.event(params, data_all, input.ignore_blank, input.sort_abundance)
def get_displayed_class_images():
req(params() is not None)
req(data_all() is not None)
data, apix = data_all()
n = len(data)
images = [data[i] for i in range(n)]
image_size.set(max(images[0].shape))
try:
df = params()
abundance.set(compute.get_class_abundance(df, n))
except Exception as e:
print(e)
m = ui.modal(
f"Failed to get class abundance from the provided Class2D parameter and image files. Make sure that the two files are for the same Class2D job",
title="Information error",
easy_close=True,
footer=None,
)
ui.modal_show(m)
return None
display_seq_all = np.arange(n, dtype=int)
if input.sort_abundance():
display_seq_all = np.argsort(abundance())[::-1]
if input.ignore_blank():
included = []
for i in range(n):
image = images[display_seq_all[i]]
if np.max(image) > np.min(image):
included.append(display_seq_all[i])
images = [images[i] for i in included]
else:
included = display_seq_all
image_labels = [f"{i+1}: {abundance()[i]:,d}" for i in included]
displayed_class_ids.set(included)
displayed_class_images.set(images)
displayed_class_title.set(
f"{len(included)}/{n} classes | {images[0].shape[1]}x{images[0].shape[0]} pixels | {apix} Å/pixel"
)
displayed_class_labels.set(image_labels)
@reactive.effect
@reactive.event(input.run)
def get_params_from_upload():
req(input.input_mode_params() == "upload")
fileinfo = input.upload_params()
param_file = fileinfo[0]["datapath"]
msg = None
try:
tmp_params = compute.get_class2d_helix_params_from_file(param_file)
except Exception as e:
print(e)
msg = str(e).replace(param_file, fileinfo[0]["name"])
tmp_params = None
params.set(tmp_params)
if params() is None:
if msg is None:
msg = f"failed to parse the upload class2D parameters from {fileinfo[0]['name']}"
msg = ui.markdown(
msg.replace("<", "<").replace(">", ">").replace("\n", "<br><br>")
)
m = ui.modal(
msg,
title="File upload error",
easy_close=True,
footer=None,
)
ui.modal_show(m)
@reactive.effect
@reactive.event(input.run)
def get_params_from_url():
req(input.input_mode_params() == "url")
url = input.url_params()
msg = None
try:
tmp_params = compute.get_class2d_helix_params_from_url(url)
except Exception as e:
print(e)
msg = str(e)
tmp_params = None
params.set(tmp_params)
if params() is None:
if msg is None:
msg = f"failed to download class2D parameters from {input.url_params()}"
msg = ui.markdown(
msg.replace("<", "<").replace(">", ">").replace("\n", "<br><br>")
)
m = ui.modal(
msg,
title="File download error",
easy_close=True,
footer=None,
)
ui.modal_show(m)
selected_helices_min_len = reactive.value(([[], [], 0], 0))
@reactive.effect
@reactive.event(input.select_classes, params)
def get_selected_helices():
req(params() is not None)
req(image_size())
req(len(abundance()))
class_indices = [displayed_class_ids()[i] for i in input.select_classes()]
helices = compute.select_classes(params=params(), class_indices=class_indices)
if len(helices):
class_indices2 = (
np.unique(
np.concatenate([h["rlnClassNumber"] for hi, h in helices])
).astype(int)
- 1
)
assert set(class_indices) == set(class_indices2)
if len(helices):
filement_lengths = compute.get_filament_length(helices=helices)
segments_count = np.sum([abundance()[i] for i in class_indices])
else:
filement_lengths = []
segments_count = 0
selected_helices.set((helices, filement_lengths, segments_count))
if not input.auto_min_len():
selected_helices_min_len.set((selected_helices(), input.min_len()))
@reactive.effect
@reactive.event(selected_helices)
def auto_set_filament_min_len():
req(input.auto_min_len() is True)
helices, filament_lengths, segments_count = selected_helices()
_, min_len_tmp = compute.compute_pair_distances(
helices=helices, lengths=filament_lengths, target_total_count=1000
)
min_len_tmp = int(min_len_tmp)
ui.update_numeric("min_len", value=min_len_tmp)
selected_helices_min_len.set((selected_helices(), min_len_tmp))
@reactive.effect
@reactive.event(input.min_len_changed)
def update_selected_helices_min_len():
selected_helices_min_len.set((selected_helices(), input.min_len()))
@reactive.effect
@reactive.event(selected_helices_min_len, input.max_len)
def select_helices_by_length():
previous = getattr(select_helices_by_length, "previous", ([], 0))
selected_image_indices_previous, min_len_previous = previous
(helices, filement_lengths, _), min_len = selected_helices_min_len()
req(
set(selected_image_indices_previous) != set(input.select_classes())
or min_len_previous != min_len
)
if len(helices) == 0:
retained_helices_by_length.set([])
elif min_len == 0 and input.max_len() <= 0:
retained_helices_by_length.set(helices)
else:
helices_retained, n_ptcls = compute.select_helices_by_length(
helices=helices,
lengths=filement_lengths,
min_len=min_len,
max_len=input.max_len(),
)
retained_helices_by_length.set(helices_retained)
select_helices_by_length.previous = (input.select_classes(), min_len)
@reactive.effect
@reactive.event(retained_helices_by_length)
def get_pair_lengths():
if len(retained_helices_by_length()):
dists, _ = compute.compute_pair_distances(helices=retained_helices_by_length())
pair_distances.set(dists)
else:
pair_distances.set([])
@reactive.effect
@reactive.event(df_selected_helices)
def get_pair_lengths_df_selected():
(helices, filement_lengths, _) = df_selected_helices()
if len(helices):
dists, _ = compute.compute_pair_distances(helices=helices)
pair_distances_df_selected.set(dists)
else:
pair_distances_df_selected.set([])
float_vars = dict(
max_len=-1,
max_pair_dist=-1,
min_len=0,
rise=4.75,
)
int_vars = dict(
auto_min_len=1, bins=100, ignore_blank=1, show_sharable_url=0, sort_abundance=1
)
str_vars = dict(
input_mode_classes="url",
input_mode_params="url",
url_params=urls[url_key][0],
url_classes=urls[url_key][1],
)
all_input_vars = list(float_vars.keys()) + list(int_vars.keys()) + list(str_vars.keys())
reactive_vars_in = dict(select=(initial_selected_image_indices, int))
reactive_vars_out = dict(selected_image_indices=(input.select_classes, [0], "select"))
connection_made = reactive.Value(False)
@reactive.effect
@reactive.event(lambda: not connection_made())
def apply_initial_params_from_browser_url():
d = helicon.shiny.get_client_url_query_params(input=input, keep_list=True)
for k, v in d.items():
if k in float_vars:
v = list(map(float, v))
if v[0] != float_vars[k]:
if k in input:
ui.update_numeric(k, value=v[0])
elif k in int_vars:
v = list(map(int, v))
if v[0] != int_vars[k]:
if k in input:
ui.update_numeric(k, value=v[0])
elif k in str_vars:
if k in input:
ui.update_text(k, value=v[0])
elif k in reactive_vars_in:
var, val_type = reactive_vars_in[k]
v = list(map(val_type, v))
var.set(v)
if input.input_mode_params() == "url" and input.input_mode_classes() == "url":
script = ui.tags.script(f"""document.getElementById('run').click();""")
ui.insert_ui(ui=script, selector="body", where="afterEnd")
@render.ui
@reactive.event(
*([input[k] for k in all_input_vars] + [v[0] for v in reactive_vars_out.values()])
)
def update_browser_url():
if input.show_sharable_url():
d = {}
d.update(
{
k: float(input[k]())
for k in float_vars
if float_vars[k] != float(input[k]())
}
)
d.update(
{k: int(input[k]()) for k in int_vars if int_vars[k] != int(input[k]())}
)
d.update({k: input[k]() for k in str_vars if str_vars[k] != input[k]()})
d.update(
{
var_url: var()
for k, (var, val, var_url) in reactive_vars_out.items()
if val != var()
}
)
d = {k: d[k] for k in sorted(d.keys())}
else:
d = {}
script = helicon.shiny.set_client_url_query_params(query_params=d)
return script