-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
executable file
·1235 lines (1097 loc) · 67.7 KB
/
gui.py
File metadata and controls
executable file
·1235 lines (1097 loc) · 67.7 KB
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 python
from tkinter import *
import customtkinter
from tkinter.font import BOLD
from tkinter import simpledialog
from tkinter import filedialog as fd
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
import os
import re
import webbrowser
import json
from PIL import Image
import easygui
import ScriptBuilder
import easygui as e
import csv
import numpy as np
import xlsxwriter
customtkinter.set_appearance_mode("Dark") # Modes: "Dark" (standard), "Light"
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
"""class App is an extension of a toplevel CTk widget with an associated folder in the file system"""
class App(customtkinter.CTk):
WIDTH = 1080
HEIGHT = 530
def __init__(self):
super().__init__()
self.title("Protocol designer")
self.geometry(f"{App.WIDTH}x{App.HEIGHT}")
self.protocol("WM_DELETE_WINDOW", self.on_closing) # call .on_closing() when app gets closed
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
# ask for directory in which all temporary files (folder input) and result files will be written
self.UserPath = fd.askdirectory()
# create the necessary inputs folder in case it does not exist:
parent_dir = os.path.join(self.UserPath, "inputs")
if not os.path.isdir(parent_dir):
os.mkdir(parent_dir)
# keep the path to the inputs folder
self.inputsPath = parent_dir
self.resizable(False, False)
def on_closing(self, event=0):
self.destroy()
"""
An object of class ControlFrame handles all general protocol information (metadata, pipets) and has an interactive
visualization of the slots connected to InputFrames.
"""
class ControlFrame(customtkinter.CTkFrame):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
# grid ControlFrame object
self.grid(row=0, column=1, sticky="nsew")
# ============ create two frames ============
# configure grid layout (2x1)
self.grid_columnconfigure(1, weight=1)
self.grid_rowconfigure(0, weight=1)
self.grid(row=0, column=0, sticky="nsew")
self.frame_left = customtkinter.CTkFrame(master=self,
width=180,
corner_radius=0)
self.frame_left.grid(row=0, column=0, sticky="nsew")
self.frame_right = customtkinter.CTkFrame(master=self)
self.frame_right.grid(row=0, column=1, sticky="nsew", padx=20, pady=20)
# ============ frame_left ============
# configure grid layout (2x20)
self.frame_left.grid_rowconfigure(0, minsize=10) # empty row with minsize as spacing
self.frame_left.grid_rowconfigure(10, weight=1) # empty row as spacing
self.frame_left.grid_rowconfigure(15, minsize=20) # empty row with minsize as spacing
self.frame_left.grid_rowconfigure(20, minsize=10) # empty row with minsize as spacing
# Title: Protocol metadata
self.label_1 = customtkinter.CTkLabel(master=self.frame_left,
text="Protocol metadata",
font=("Roboto Medium", -20)) # font name and size in px
self.label_1.grid(row=1, column=0, columnspan=2, pady=10, padx=10)
# Name protocol fill-in
self.label_2 = customtkinter.CTkLabel(master=self.frame_left,
text="Name protocol:")
self.label_2.grid(row=2, column=0, pady=5, padx=0)
self.name = customtkinter.CTkEntry(master=self.frame_left)
self.name.grid(row=2, column=1, pady=5, padx=5, sticky="ew")
# Author fill-in
self.label_3 = customtkinter.CTkLabel(master=self.frame_left,
text="Author:")
self.label_3.grid(row=3, column=0, pady=5, padx=0)
self.author = customtkinter.CTkEntry(master=self.frame_left)
self.author.grid(row=3, column=1, pady=5, padx=5, sticky="ew")
# Description fill-in
self.label_6 = customtkinter.CTkLabel(master=self.frame_left, text="Description:")
self.label_6.grid(row=4, column=0, pady=1, padx=0, sticky="ew")
self.description = customtkinter.CTkTextbox(master=self.frame_left, width=400, height=130, border_width=2)
self.description.grid(row=5, column=0, columnspan=2, pady=0, padx=10, sticky="ns")
# Title: Pipettes
self.label_1 = customtkinter.CTkLabel(master=self.frame_left,
text="Pipettes",
font=("Roboto Medium", -16)) # font name and size in px
self.label_1.grid(row=11, column=0, columnspan=2, pady=10, padx=10)
# initialize the hasTwoPipets attribute. True indicates that two input rows are present in the "Pipettes" section
self.hasTwoPipets = False
# create frame for pipets
self.selected_side_pipet = customtkinter.StringVar()
self.frame_pipet = customtkinter.CTkFrame(master=self.frame_left)
self.frame_pipet.grid(row=12, column=0, columnspan=2, padx=10, sticky="nsew")
# First pipet fill-in
self.AddPipetLabel = customtkinter.CTkLabel(master=self.frame_pipet, text="Add pipet(s): ", width=30)
self.AddPipetLabel.grid(row=0, column=0, padx=10, sticky="nsew")
self.AddPipet = customtkinter.CTkEntry(master=self.frame_pipet)
self.AddPipet.grid(row=0, column=1, sticky="nsew")
self.optionmenu_pip = customtkinter.CTkOptionMenu(master=self.frame_pipet,
values=["left", "right"],
variable=self.selected_side_pipet,
width=80)
self.optionmenu_pip.grid(row=0, column=2, padx=10, sticky="nsew")
# Button: add second pipet
self.AddPipetButton = customtkinter.CTkButton(master=self.frame_pipet, text="Add... ",
command=self.addPipet, width=54)
self.AddPipetButton.grid(row=0, column=4)
self.AddPipetButton.grid_propagate(False)
##autofill all information about the pipets that is already available in the inputs folder
# path to pipet information
pipetsFileName = os.path.join(self.parent.inputsPath, "pipets.txt")
if os.path.exists(pipetsFileName):
with open(pipetsFileName, "r") as f:
lines = f.readlines()
# label and position of the first pipet (at least one defined)
self.AddPipet.insert(END, lines[0].replace("\n", ""))
self.optionmenu_pip.set(lines[1].replace("\n", ""))
# read and fill in the information of a second pipet if available
if len(lines) == 4:
self.addPipet(lines[2].replace("\n", ""), lines[3].replace("\n", ""))
# Apply button: write pipet information to file in inputsfolder
self.ApplyButton = customtkinter.CTkButton(master=self.frame_pipet, text="Apply", command=self.pipetApply,
width=100)
self.ApplyButton.grid(row=5, column=0, padx=5, pady=20, sticky="ew")
# Dark mode switch
switch_var = customtkinter.StringVar(value="on")
self.switch_darkmode = customtkinter.CTkSwitch(master=self.frame_left, text="Dark mode",
command=self.change_appearance_mode, variable=switch_var,
onvalue="on", offvalue="off")
self.switch_darkmode.grid(row=20, column=0, columnspan=2, pady=10, padx=20, sticky="")
self.switch_darkmode.select()
# ============ frame_right ============
# configure grid layout (7x3)
self.frame_right.rowconfigure((0, 1, 2, 3), weight=1)
self.frame_right.rowconfigure(7, weight=10)
self.frame_right.columnconfigure((0, 1, 2), weight=1)
self.frame_right.columnconfigure(2, weight=0)
self.frame_panels = customtkinter.CTkFrame(master=self.frame_right, fg_color="transparent", bg_color="transparent")
self.frame_panels.grid(row=1, column=0, columnspan=3, rowspan=4, pady=20, padx=20, sticky="nsew")
# ============ frame_panels ============
imagepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images")
self.waste = customtkinter.CTkImage(light_image=Image.open(os.path.join(imagepath, "wastebin_light.png")),
size=(40, 40),dark_image=Image.open(os.path.join(imagepath, "wastebin_dark.png")))
# configure grid layout (3x4)
self.frame_panels.rowconfigure((0, 1, 2, 3), weight=1)
self.frame_panels.columnconfigure((0, 1, 2), weight=1)
self.button1 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="1",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 1))
self.button2 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="2",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 2))
self.button3 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="3",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 3))
self.button4 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="4",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 4))
self.button5 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="5",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 5))
self.button6 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="6",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 6))
self.button7 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="7",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 7))
self.button8 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="8",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 8))
self.button9 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="9",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 9))
self.button10 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="10",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 10))
self.button11 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text="11",
text_color=("black", "grey80"),
font=("Verdana", -40),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 11))
self.button12 = customtkinter.CTkButton(master=self.frame_panels,
height=148,
width=175,
text='',
image=self.waste,
font=("Verdana", -30),
fg_color="transparent",
bg_color="transparent",
border_width=2,
border_color=("black", "grey80"),
corner_radius=10,
command=lambda *args: createInputFrame(self, 12),
state="disabled")
self.button1.grid(row=3, column=0, pady=2, padx=2, sticky="nesw")
self.button2.grid(row=3, column=1, pady=2, padx=2, sticky="nesw")
self.button3.grid(row=3, column=2, pady=2, padx=2, sticky="nesw")
self.button4.grid(row=2, column=0, pady=2, padx=2, sticky="nesw")
self.button5.grid(row=2, column=1, pady=2, padx=2, sticky="nesw")
self.button6.grid(row=2, column=2, pady=2, padx=2, sticky="nesw")
self.button7.grid(row=1, column=0, pady=2, padx=2, sticky="nesw")
self.button8.grid(row=1, column=1, pady=2, padx=2, sticky="nesw")
self.button9.grid(row=1, column=2, pady=2, padx=2, sticky="nesw")
self.button10.grid(row=0, column=0, pady=2, padx=2, sticky="nesw")
self.button11.grid(row=0, column=1, pady=2, padx=2, sticky="nesw")
self.button12.grid(row=0, column=2, pady=2, padx=2, sticky="nesw")
# create an InputFrame for the slot selected
def createInputFrame(self, index):
if isinstance(self.frame_right, InputFrame):
self.frame_right.destroy() # else if multiple buttons pressed, all frames will stack
self.frame_right = InputFrame(parent, index)
self.frame_right.tkraise()
# action buttons
self.button13 = customtkinter.CTkButton(master=self.frame_right,
text="Load from parameter file",
border_width=2, # <- custom border_width
fg_color="transparent", # <- no fg_color
command=self.load_from_parameterfile)
self.button13.grid(row=8, column=1, columnspan=1, pady=10, padx=5, sticky="e")
self.button14 = customtkinter.CTkButton(master=self.frame_right,
text="Generate Protocol",
border_width=2, # <- custom border_width
fg_color="transparent", # <- no fg_color
command=self.generate_protocol)
self.button14.grid(row=8, column=2, columnspan=1, pady=10, padx=5, sticky="")
"""
The addPipet method inserts a second row in the "Pipettes" section where information of a second pipet can be specified.
It removes the AddPipetButton in the first row and creates a RemovePipetButton in the second row to enforce that
always at least one and at most two pipets are specified.
"""
def addPipet(self, label="", place="left"):
# create second row
self.AddPipet2 = customtkinter.CTkEntry(master=self.frame_pipet)
self.AddPipet2.insert(END, label)
self.AddPipet2.grid(row=2, column=1, sticky="nsew")
self.optionmenu_pip2 = customtkinter.CTkOptionMenu(master=self.frame_pipet,
values=["left", "right"], width=80)
self.optionmenu_pip2.set(place)
self.optionmenu_pip2.grid(row=2, column=2, padx=10, sticky="nsew")
# create the RemovePipetButton
self.RemovePipetButton = customtkinter.CTkButton(master=self.frame_pipet,
text="remove", width=54, command=self.removePipet)
self.RemovePipetButton.grid(row=2, column=4)
self.RemovePipetButton.grid_propagate(False)
# remove the AddPipetButton
self.AddPipetButton.grid_remove()
self.hasTwoPipets = True
# The removePipet method removes the input row for the second pipet in the "Pipettes" section
def removePipet(self):
Lst = self.frame_pipet.grid_slaves(row=2)
for l in Lst:
l.destroy()
self.AddPipetButton.grid()
self.hasTwoPipets = False
"""
The pipetApply method writes all currently specified information in the "Pipettes" section to a 'pipets.txt' file
in the inputs folder. If such a file is already present, its content will be overwritten.
"""
def pipetApply(self):
# ensure the existence of the inputs folder
isExist = os.path.exists(self.parent.inputsPath)
if not isExist:
os.makedirs(self.parent.inputsPath)
# ensure label of first pipet not empty
if self.AddPipet.get() != "":
instruments = self.AddPipet.get() + "\n" + self.optionmenu_pip.get() + "\n"
with open(os.path.join(self.parent.inputsPath, "pipets.txt"), "w") as f:
f.write(instruments)
else:
e.msgbox("First pipet not specified", "Error")
return None
# same if second input row present in "Pipettes" section
if self.hasTwoPipets == True:
if self.AddPipet2.get() != "":
with open(os.path.join(self.parent.inputsPath, "pipets.txt"), "a") as f:
f.write(self.AddPipet2.get() + "\n" + self.optionmenu_pip2.get() + "\n")
else:
e.msgbox("Second pipet not specified", "Error")
return None
# swith Dark/Light mode
def change_appearance_mode(self):
switch = self.switch_darkmode.get()
if switch == "on":
self.configure(customtkinter.set_appearance_mode("Dark"))
else:
self.configure(customtkinter.set_appearance_mode("Light"))
"""
The generate_protocol method reads all information of slots and pipets in the inputsFolder, creates a parameter file
(params.txt) in the predefined format, a protocol file (.txt) through ScriptBuilder and in case of a 1D or 2D screen,
an excel file (.xlsx file) with for every well plate and compound, the concentrations in each well
"""
def generate_protocol(self):
# ensure existence of the inputs folder
isExist = os.path.exists(self.parent.inputsPath)
if not isExist:
os.makedirs(self.parent.inputsPath)
return None
# initialize lists for information of unknown compounds (i.e. not in compLibrary.txt)
names_conc_to_add = []
conc_to_add = []
labels_to_add = []
# ask user for protocol (and parameter) filename
protocolFilename = simpledialog.askstring("Protocol filename", "filename for new protocol\t\t\t")
# if not cancelled
if protocolFilename:
protocolFilename += ".py"
# read in compound library:
with open(os.path.join(os.path.dirname(__file__),'compLibrary.txt'), 'r') as l:
reader = csv.reader(l, delimiter="\t")
lines = [entry for line in reader for entry in line]
labels = str(lines[0::3])[1:-1]
types = str(lines[1::3])[1:-1]
# read in all user-specified information stored in the inputs folder
directory = os.path.join(self.parent.inputsPath)
tips = ""
tuberacks = ""
instruments = ""
plates = ""
screens = ""
# ensure at least one slot has information stored
atLeastOneInput = 0
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# check if it is a file
if os.path.isfile(f):
with open(f, "r") as f:
# In all slot files, first line is Type of container
Type = f.readline().replace("\n", "")
if Type == "Tube rack":
atLeastOneInput += 1
# read in dictionary
dictTur = json.loads(f.read())
for key in dictTur:
# ensure all necessary input specified, else: error message to user
if dictTur.get(key) != "":
tuberacks += dictTur.get(key) + "\n"
else:
e.msgbox("Empty string value for name plate: " + filename, "Error")
return None
elif Type == "Tip rack":
atLeastOneInput += 1
# read in dictionary
dictTir = json.loads(f.read())
for key in dictTir.keys():
# ensure all necessary input specified, else: error message to user
if dictTir.get(key) != "":
tips += dictTir.get(key) + "\n"
else:
e.msgbox("Empty string value for name plate: " + filename, "Error")
return None
elif Type == "Well plate":
atLeastOneInput += 1
# read in dictionary
dict = json.loads(f.readline())
plates += dict["label"] + "\n" + str(dict["index"]) + "\n"
# ensure label specified, else: error message to user
if dict["label"] == "":
e.msgbox("Empty string value for name plate: " + filename, "Error")
return None
screens += str(dict["dimension"]) + "\n"
# if every compound has a label and concentration, proceed. Else: error message to user
if "" not in dict["names_conc"].split(","):
screens += dict["names_conc"] + "\n"
else:
e.msgbox(
"Empty string value for name or concentration of one of the compounds: " + filename,
"Error")
return None
# if every compound has a position, proceed. Else: error message to user
if "" not in dict["positions"].split(","):
screens += dict["positions"] + "\n"
else:
e.msgbox("Empty string value for position of one of the compounds: " + filename,
"Error")
return None
# if every compound has a completely specified range, proceed. Else: error message to user
if all(not (string.endswith("-") or string.startswith("-")) for string in
dict["ranges"].split(",")):
screens += dict["ranges"] + "\n"
else:
e.msgbox("Empty string value for ranges of one of the compounds: " + filename, "Error")
return None
screens += str(dict["index"]) + "\n"
# if well plate has a WorkingVolume, proceed. Else: error message to user
if "" != dict["WorkingVolume"]:
screens += str(dict["WorkingVolume"]) + "\n"
else:
e.msgbox("Empty string value for the working volume in: " + filename,
"Error")
return None
# check if all compounds are already present in the library.
names_conc = dict["names_conc"].split(",")[:-1]
types_compounds = dict["types_compounds"].split(",")
# get stock concentration of each compound. names_conc elements are of
# the form: "label (concentration unit)"
concs = []
for i in names_conc:
# get concentration, if regex matching fails: error message to the user
try:
concs.append(re.search(r"([\.0-9]+)", i.split(" (")[-1]).group(1))
except AttributeError:
e.msgbox(
"Unrecognized or empty value for one of the concentrations in: " + filename,
"Error")
return None
# tuples (label,type) for each compound in compLibrary
zipLabelType = list(map(lambda x, y: (x.replace("\'", ""), y.replace("\'", "")),
labels.split(", "), types.split(", ")))
# tuples (label,type) for each compound in input
zipInput = list(map(lambda x, y: (x, y), names_conc, types_compounds))
# if any (label,type) in input not in compLibrary:
if any(x not in zipLabelType for x in zipInput):
# iterate over input
for idx in range(len(zipInput)):
# if compound not in compLibrary yet
if (zipInput[idx] not in zipLabelType):
# if same label with different type already in compLibrary: error message to user.
# Else: add information to lists
if zipInput[idx][0] in list(
map(lambda x: x.replace("\'", ""), labels.split(", "))):
e.msgbox(filename + "\n" + str(zipInput[idx][
0]) + ": A compound with the same label, but different type is already present in the library."
, "Error")
return None
names_conc_to_add.append(names_conc[idx])
labels_to_add.append(types_compounds[idx])
conc_to_add.append(concs[idx])
# if screen 1D or 2D: create concentrations matrix as Excel file
# TODO: also for 3D
if str(dict["dimension"]) == 1 or str(dict["dimension"] == 2):
try:
# get the number of wells from the well plate's label (assuming that it follows
# the Opentrons naming convention!)
numberOfWells = int(re.search(r'([0-9]+)', dict["label"]).group(1))
ranges = dict["ranges"].split(",")
# create workbook with one worksheet
workbook = xlsxwriter.Workbook(
os.path.join(self.parent.UserPath, protocolFilename.replace(".py", ".xlsx")))
worksheet = workbook.add_worksheet()
headers = ["Name"] + [*range(1, numberOfWells + 1)]
# write column names
for i in range(len(headers)):
worksheet.write(0, i, headers[i])
for i in range(len(names_conc)):
# for compound i, get range boundaries
raw_range = [float(j) for j in ranges[i].split('-')]
# append label compound and concentration gradient
RowToAdd = [names_conc[i]] + list(
map(str, np.linspace(raw_range[0], raw_range[1], numberOfWells).tolist()))
# write label and concentrations
for k in range(len(RowToAdd)):
worksheet.write(i + 1, k, RowToAdd[k])
workbook.close()
# catch error due to deviation from Opentrons' well plate naming convention
except AttributeError:
continue
# if no slots have information specified, stop the program from creating an incomplete parameter file
if atLeastOneInput == 0:
e.msgbox("No input files with slot information found. Specify input for at least one slot", "Error")
return None
# if list with information of compounds not yet in compLibrary is non-empty.
# First, ask permission of user to add to compLibrary
if len(names_conc_to_add) > 0:
answer = messagebox.askquestion(
message="Are you sure you want to continue and add following compounds to the library?\n" +
"\n".join(' '.join(map(str, tup)) for tup in
list(zip(names_conc_to_add, labels_to_add, conc_to_add))))
# If user allows, write new compounds to compLibrary
if answer == "yes":
with open(os.path.join(os.path.dirname(__file__),'compLibrary.txt'), "a") as f:
for idx in range(len(names_conc_to_add)):
f.write("\n" + names_conc_to_add[idx] + "\t")
f.write(labels_to_add[idx].capitalize() + "\t")
f.write(conc_to_add[idx])
# read stored information for pipets. If none specified: error message to user
FilenamePipets = os.path.join(self.parent.inputsPath, "pipets.txt")
if os.path.isfile(FilenamePipets):
with open(FilenamePipets) as f:
instruments += "".join(f.readlines())
else:
e.msgbox("no prior information of pipet(s) saved. Specify name and position.", "Error")
return None
#if no plates defined for screen: error messsage to user
if plates == "":
easygui.msgbox("Add at least one well plate to the screen")
return None
#if no tipracks defined for screen: error message to user
if tips == "":
easygui.msgbox("Add at least one tip rack to the screen")
return None
# write all information to parameter file
paramFilename = protocolFilename.replace(".py", ".param.txt")
paramFilePath = os.path.join(self.parent.UserPath, paramFilename)
with open(paramFilePath, "w+") as f:
f.write(tips + "\n")
f.write(tuberacks + "\n")
f.write(instruments + "\n")
f.write(plates + "\n")
f.write(screens)
# generate the protocol
ScriptBuilder.BuildWithMetadata(paramFilePath, os.path.join(self.parent.UserPath, protocolFilename),
self.name.get(), self.description.get("0.0","end").replace("\n", ""), self.author.get())
"""
The load_from_parameterfile method allows the user to select an existing parameter file. The method then tries to write the
contents of this file to temporary files located in the inputs folder.
"""
def load_from_parameterfile(self):
# ensure the inputs folder exists
try:
isExist = os.path.exists(self.parent.inputsPath)
if not isExist:
os.makedirs(self.parent.inputsPath)
# ask user to select a parameter file
parampath = askopenfilename()
# read contents
with open(parampath, 'r') as f:
lines = f.read()
lines = lines.split("\n\n")
# parse parameter file
tips_raw = lines[0].split("\n")
tuberack_raw = lines[1].split("\n")
instr_raw = lines[2].split("\n")
plates_raw = lines[3].split("\n")
screens_raw = lines[4].split('\n')
if screens_raw[-1] == '':
screens_raw = screens_raw[:-1]
else:
screens_raw = screens_raw
## write files for Tip rack assigned slots
tipsInput = tips_raw[0::3]
tipsLocation = tips_raw[1::3]
tipsPipette = tips_raw[2::3]
for i in range(len(tipsLocation)):
filename = "Input_plate" + str(tipsLocation[i]) + ".txt"
filePath = os.path.join(self.parent.inputsPath, filename)
with open(filePath, "w") as f:
f.write("Tip rack" + "\n")
dict = {"label": tipsInput[i], "index": str(tipsLocation[i]),
"AssignedPipetOption": tipsPipette[i]}
f.write(json.dumps(dict))
## write files for Tube rack assigned slots
tuberacksInput = tuberack_raw[0::2]
tuberacksLocation = tuberack_raw[1::2]
for i in range(len(tuberacksLocation)):
filename = "Input_plate" + str(tuberacksLocation[i]) + ".txt"
filePath = os.path.join(self.parent.inputsPath, filename)
with open(filePath, "w") as f:
f.write("Tube rack" + "\n")
dict = {"label": tuberacksInput[i], "index": str(tuberacksLocation[i])}
f.write(json.dumps(dict))
## write pipets.txt, a file with all pipets information
instrumentInput = instr_raw[0::2]
instrumentLocation = instr_raw[1::2]
filename = "pipets.txt"
filePath = os.path.join(self.parent.inputsPath, filename)
with open(filePath, "w") as f:
for i in range(len(instrumentInput)):
f.write(instrumentInput[i] + "\n")
f.write(instrumentLocation[i] + "\n")
# after the new pipets information is loaded, update the information displayed in the "Pipettes" section
self.AddPipet.delete(0, END)
self.AddPipet.insert(END, instrumentInput[0])
self.optionmenu_pip.set(instrumentLocation[0])
if len(instrumentInput) == 2:
self.addPipet(instrumentInput[1], instrumentLocation[1])
## write files for Well plate assigned slots
platesInput = plates_raw[0::2]
platesLocation = plates_raw[1::2]
all_screen_types = screens_raw[0::6]
all_screen_compounds = screens_raw[1::6]
all_screen_stocks = screens_raw[2::6]
all_screen_ranges = screens_raw[3::6]
all_screen_plates = screens_raw[4::6]
all_screen_workVol = screens_raw[5::6]
# read in compLibrary
with open(os.path.join(os.path.dirname(__file__),'compLibrary.txt'), 'r') as l:
reader = csv.reader(l, delimiter="\t")
lines = [entry for line in reader for entry in line]
labels = lines[0::3]
types = lines[1::3]
# check if all compounds are already in compLibrary
# initialize a list with all labels of the compounds
label_compounds = []
# For each screen
for i in range(len(all_screen_plates)):
# get compound labels
list_screen_compounds = all_screen_compounds[i].split(",")
# initialize a list with all types of the compounds in screen i
types_i = []
# For each label, get type from compLibrary. If label not yet in compLibrary: error message to user
for j in range(len(list_screen_compounds)):
comp = list_screen_compounds[j]
compIdx = labels.index(comp)
if compIdx != None:
types_i.append(types[compIdx])
else:
e.msgbox("Unknown compound " + comp + " in parameter file", "Error")
return None
label_compounds.append(types_i)
# write files for Well plate assigned slots
for i in range(len(platesLocation)):
filename = "Input_plate" + str(platesLocation[i]) + ".txt"
filePath = os.path.join(self.parent.inputsPath, filename)
with open(filePath, "w") as f:
f.write("Well plate" + "\n")
dict = {"label": platesInput[i], "index": platesLocation[i], "dimension": str(all_screen_types[i]),
"names_conc": all_screen_compounds[i], "positions": all_screen_stocks[i],
"ranges": all_screen_ranges[i],
"WorkingVolume": all_screen_workVol[i], "Tuberack": all_screen_stocks[i][0],
"types_compounds": ",".join(label_compounds[i][:-1])}
f.write(json.dumps(dict))
# If anything goes wrong during the reading of the parameter file or writing of the temporary files,
# display the error message to the user.
except Exception as err:
e.msgbox(
"Something went wrong during loading of the parameter file. Make sure the file follows the correct format exactly, including newline characters.\n" + str(
err), "Error")
return None
"""
A frame of class InputFrame appears on top of the right side of the app when the user clicks on a slot.
All information for that slot can be added/removed/modified in this frame.
"""
class InputFrame(customtkinter.CTkFrame):
def __init__(self, parent, index):
super().__init__(parent)
self.index = index
self.parent = parent
# grid ControlFrame object
self.grid(row=0, column=0, sticky="nes")
# ============ create frame up ============
self.frame_up = customtkinter.CTkFrame(master=self, width=650, height=472)
self.frame_up.columnconfigure((0, 1), weight=1)
self.frame_up.grid(row=0, column=0, sticky="nsew")
self.frame_up.grid_propagate(False)
# Title: slot + index
customtkinter.CTkLabel(master=self.frame_up, text="slot " + str(self.index), font=('Segoe UI', 15, BOLD)) \
.grid(row=0, column=0, columnspan=3, sticky="nsew", pady=(1, 10))
# name container fill-in
customtkinter.CTkLabel(master=self.frame_up, text="name container: ").grid(row=1, column=0, sticky="W")
self.PlateLabel = customtkinter.CTkEntry(master=self.frame_up)
self.PlateLabel.grid(row=1, column=1, sticky="nsew")
# type container selection
self.valuesPlateOptionMenu = ["Choose...", "Tube rack", "Tip rack", "Well plate"]
self.plateOptionVar = customtkinter.StringVar()
customtkinter.CTkLabel(master=self.frame_up, text="add to slot:").grid(row=2, column=0, sticky="W")
self.PlateOptionMenu = customtkinter.CTkOptionMenu(master=self.frame_up, variable=self.plateOptionVar,
values=self.valuesPlateOptionMenu,
command=self.CompoundsFrameEvent)
self.PlateOptionMenu.grid(row=2, column=1, sticky="nsew")
# autofill label and type of container for selected slot
filename = "Input_plate" + str(self.index) + ".txt"
completeFilename = os.path.join(self.parent.inputsPath, filename)
# if file already exists
if os.path.exists(completeFilename):
with open(completeFilename, "r") as f:
Platetype = f.readline()
# autofill label
if any(t in Platetype for t in ["Tip rack", "Tube rack", "Well plate"]):
self.PlateLabel.insert(END, json.loads(f.readline())['label'])
# autofill type
if "Tube rack" in Platetype:
self.PlateOptionMenu.set(self.valuesPlateOptionMenu[1])
elif "Tip rack" in Platetype:
self.PlateOptionMenu.set(self.valuesPlateOptionMenu[2])
elif "Well plate" in Platetype:
self.PlateOptionMenu.set(self.valuesPlateOptionMenu[3])
else:
self.PlateOptionMenu.set(self.valuesPlateOptionMenu[0])
# ============ create frame compounds (middle frame) ============
self.frame_compounds = customtkinter.CTkFrame(master=self.frame_up, width=680)
self.frame_compounds.grid(row=3, column=0, columnspan=2, sticky="ns")
self.frame_compounds.grid_propagate(False)
self.frame_compounds.rowconfigure(2, minsize=5) # empty row with minsize as spacing)
## initialize attributes
# first available rowindex in frame_compounds for frame_comp objects generated by method addcompound
self.irow = 8
# dictionary to store irow : {frame_comp, unit} pairs
self.dict_compounds = {}
# evoke the creation of frame_compounds based on the value of PlateOptionMenu
self.CompoundsFrameEvent(self.PlateOptionMenu.get())
# ============ create frame down ============
self.frame_down = customtkinter.CTkFrame(master=self)
self.frame_down.columnconfigure([0, 1, 2], weight=1)
self.frame_down.grid(row=2, column=0, sticky="nsew")
#reset button
self.buttonReset = customtkinter.CTkButton(master=self.frame_down, text="reset", command=self.reset,
fg_color="grey")
self.buttonReset.grid(column=0, row=3, sticky="nsew")
#cancel button
self.buttonCancel = customtkinter.CTkButton(master=self.frame_down, text="cancel", command=self.cancel,
fg_color="grey")
self.buttonCancel.grid(column=1, row=3, sticky="nsew")
# apply button
self.buttonApply = customtkinter.CTkButton(master=self.frame_down, text="apply", command=self.button_event)
self.buttonApply.grid(column=2, row=3, sticky="nsew")
self.grid(column=1, row=0, padx=5, pady=5, sticky="nsew")
"""
The method CompoundsFrameEvent creates a frame based on the type of container specified for the slot.
Here, all information about the contents associated to the container can be specified
"""
def CompoundsFrameEvent(self, choice):
#filename for current slot in inputs folder
filename = "Input_plate" + str(self.index) + ".txt"
completeFilename = os.path.join(self.parent.inputsPath, filename)
if choice == "Tip rack":
self.frame_compounds.destroy()
self.frame_compounds = customtkinter.CTkFrame(master=self.frame_up, width=680, fg_color=["#F9F9FA", "#343638"])
self.frame_compounds.grid(row=3, column=0, columnspan=2, sticky="nw")
self.frame_compounds.grid_rowconfigure(0, minsize=3)
#select pipet associated to tips in Tip rack
self.AssignedPipetLabel = customtkinter.CTkLabel(master=self.frame_compounds,
text="Assigned to pipet:", anchor="w")
self.AssignedPipetLabel.grid(row=2, column=0, sticky="nw")
self.AssignedPipetOption = customtkinter.CTkOptionMenu(master=self.frame_compounds,
values=["left", "right"])
self.AssignedPipetOption.grid(row=2, column=1, sticky="nsew")
# if information is already stored in the inputs folder for this combination
# of slot and container type == Tip rack
if os.path.exists(completeFilename):
with open(completeFilename, "r") as f:
lines = f.readlines()
if "Tip rack" in lines[0]:
self.AssignedPipetOption.set(json.loads(lines[1])['AssignedPipetOption'])
elif choice == "Tube rack":
self.frame_compounds.destroy()
elif choice == "Well plate":
self.frame_compounds.destroy()
self.frame_compounds = customtkinter.CTkFrame(master=self.frame_up, width=680)
self.frame_compounds.grid(row=3, column=0, columnspan=2, sticky="ns")
self.frame_compounds.propagate(False)
self.frame_compounds.grid_rowconfigure(6, minsize=5) # empty row with minsize as spacing
#Title Compounds
self.CompoundsLabel = customtkinter.CTkLabel(master=self.frame_compounds, text="Compounds",
font=('Segoe UI', 10, BOLD))
self.CompoundsLabel.grid(row=2, column=0, sticky="nw")
self.CompoundsLabel.grid_propagate(False)
# fill-in WorkingVolume
self.WorkingVolumeLabel = customtkinter.CTkLabel(master=self.frame_compounds,
text="working volume (µl): ")
self.WorkingVolumeLabel.grid(row=3, column=0)
self.WorkingVolume = customtkinter.CTkEntry(master=self.frame_compounds)
self.WorkingVolume.grid(row=3, column=1)
# fill-in position of associated Tube rack
self.TubeRackLabel = customtkinter.CTkLabel(master=self.frame_compounds,
text="tube rack position: ")
self.TubeRack = customtkinter.CTkEntry(master=self.frame_compounds)
self.TubeRackLabel.grid(row=4, column=0)
self.TubeRack.grid(row=4, column=1)
# fill-in position in Tube rack of milli-Q (i.e. the diluent)
self.MQpositionLabel = customtkinter.CTkLabel(master=self.frame_compounds, text="Milli-Q position: ")
self.MQpositionLabel.grid(row=5, column=0)
self.MQposition = customtkinter.CTkEntry(master=self.frame_compounds)
self.MQposition.grid(row=5, column=1)
#redirect to page in OT-2 API V2 with info on Tube rack position labels
self.HelpPositions = customtkinter.CTkButton(master=self.frame_compounds, text="?",
width=0.2, height=0.1, fg_color="grey33",
command=self.openHelpPositions)
self.HelpPositions.grid(row=5, column=2, sticky="w", padx=5)
# if information is already stored in the inputs folder for this combination of
# slot and container type == Well plate: fill in WorkingVolume, Tube rack & milli-Q position
if os.path.exists(completeFilename):
with open(completeFilename, "r") as f:
lines = f.readlines()
if "Well plate" in lines[0]:
dict = json.loads(lines[1])
self.WorkingVolume.insert(END, dict["WorkingVolume"])
self.TubeRack.insert(END, dict["Tuberack"])
compounds = dict["names_conc"].split(",")
positions = dict["positions"].split(",")
# extract the position of compound 'MQ'. If this info isn't stored, ignore
for j in range(len(compounds)):
if 'MQ' == compounds[j]:
# in case no position previously given by user, suppress AttributeError
try:
self.MQposition.insert(END, re.search(r"/(\w+)", positions[j]).group(1))
except AttributeError:
pass
#button Add salt
self.AddSalt = customtkinter.CTkButton(master=self.frame_compounds, text="Add salt",
command=lambda *args: self.addcompound("salt"),
width=215)
self.AddSalt.grid(row=7, column=0, padx=0, sticky="nw")
self.AddSalt.grid_propagate(False)