-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.py
More file actions
323 lines (254 loc) · 13.5 KB
/
App.py
File metadata and controls
323 lines (254 loc) · 13.5 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
import customtkinter
import webbrowser
from PIL import Image
from functools import partial
from SequenceClass import Sequence
from SubmissionTasks import sequence_formatting, nucleotide_analysis, codon_analysis
from GraphGenerator import graph_gen
from ResultsPage import Results
DEL_IMAGE = customtkinter.CTkImage(light_image=Image.open("Images/trash_(light_mode).png"),
dark_image=Image.open("Images/trash_(dark_mode).png"),
size=(30, 30))
UPLOAD_IMAGE = customtkinter.CTkImage(light_image=Image.open("Images/File_Upload_(dark_mode).png"),
dark_image=Image.open("Images/File_Upload_(dark_mode).png"),
size=(20, 20))
customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("dark-blue")
class MainSeq:
"""Class that constructs the main sequence frame."""
def __init__(self, master):
# Frame for main sequence
self.main_seq_frame = customtkinter.CTkFrame(master=master, corner_radius=0, fg_color="transparent")
self.main_seq_frame.pack(side="top", fill="x", expand="true")
# Label for main sequence
self.main_seq_label = customtkinter.CTkLabel(master=self.main_seq_frame, text="Main Sequence", padx=20)
self.main_seq_label.pack(anchor="w")
# Text box
self.main_seq_text = customtkinter.CTkTextbox(self.main_seq_frame)
self.main_seq_text.pack(padx=(20, 0), pady=(0, 10), side="left", anchor="w", fill="both", expand="true")
# Delete button
self.main_del_button = customtkinter.CTkButton(master=self.main_seq_frame,
image=DEL_IMAGE,
text="",
command=self.main_seq_delete,
height=20,
width=20)
self.main_del_button.pack(side="left", padx=10)
def main_seq_delete(self):
"""Clears the main sequence textbox."""
self.main_seq_text.delete('0.0', "end")
class SeqFrame:
"""Class that constructs the sequence frames after main sequence."""
def __init__(self, master, count):
# Frame for sequence
self.new_text_frame = customtkinter.CTkFrame(master=master, corner_radius=0, fg_color="transparent")
self.new_text_frame.pack(side="top", fill="x", expand="true")
# Label for sequence
self.seq_name = f"Sequence #{count}"
self.new_label = customtkinter.CTkLabel(master=self.new_text_frame, text=self.seq_name, padx=20)
self.new_label.pack(anchor="w")
# Text box
self.new_seq_box = customtkinter.CTkTextbox(master=self.new_text_frame)
self.new_seq_box.pack(padx=(20, 0), pady=(0, 10), side="left", anchor="w", fill="both", expand="true")
# Delete button
self.new_del_button = customtkinter.CTkButton(master=self.new_text_frame,
image=DEL_IMAGE,
text="",
command=self.remove_frame,
height=20,
width=20)
self.new_del_button.pack(side="left", padx=10)
def remove_frame(self):
"""If text present in the textbox, delete text. Else, delete entire frame. Updates labels for all frames."""
if len(self.new_seq_box.get("0.0", "end")) > 1:
self.new_seq_box.delete('0.0', "end")
else:
self.new_text_frame.destroy()
app.frame_list.remove(self)
app.toolbar_frame.seq_list_labels[-1].destroy()
app.toolbar_frame.seq_list_labels.pop()
app.count = len(app.frame_list) + 1
app.update_labels()
class ToolBar:
"""Constructs the toolbar on the right side of the window."""
def __init__(self, master):
self.seq_list_labels = []
# The frame itself
self.toolbar_frame = customtkinter.CTkFrame(master=master, corner_radius=0, fg_color="transparent")
self.toolbar_frame.pack(side="right", fill="both")
# Instruction hyperlink button
self.instruction_link_button = customtkinter.CTkButton(master=self.toolbar_frame,
text="Documentation",
command=self.instruction_callback)
self.instruction_link_button.pack(padx=20, pady=(20, 10))
# New sequence button
self.newSeqBox = customtkinter.CTkButton(master=self.toolbar_frame,
text="New Sequence",
command=new_frame)
self.newSeqBox.pack(padx=20, pady=10)
# Sequence list
self.seq_list = customtkinter.CTkScrollableFrame(master=self.toolbar_frame, label_text="Sequence List")
self.seq_list.pack(padx=20, pady=10, fill="both", expand="true")
main_seq_label = customtkinter.CTkLabel(master=self.seq_list, text="Main Sequence")
main_seq_label.focus_set()
main_seq_label.bind(sequence="<Button-1>", command=partial(pop_out, -1))
main_seq_label.pack()
# Theme changing dropdown
self.appearance_mode_menu = customtkinter.CTkOptionMenu(self.toolbar_frame,
values=["System", "Dark", "Light"],
fg_color=("gray90", "gray20"),
button_color=("gray90", "gray20"),
button_hover_color=("gray75", "gray28"),
text_color=("gray10", "gray90"),
anchor="center",
command=change_appearance_mode_event)
self.appearance_mode_menu.pack(padx=20, pady=(10, 20), side="bottom")
# File upload button
# self.fileButton = customtkinter.CTkButton(self.toolbar_frame,
# text="File Upload",
# image=UPLOAD_IMAGE,
# command=self.upload)
# self.fileButton.pack(pady=10)
# Submit button
self.submitButton = customtkinter.CTkButton(self.toolbar_frame,
fg_color="#FA8072",
hover_color="#CD5C5C",
text_color=("gray10", "gray90"),
text="Submit",
command=submission)
self.submitButton.pack(padx=20, pady=10, side="bottom")
def seq_list_update(self, count):
new_label = customtkinter.CTkLabel(master=self.seq_list,
text=f"Sequence #{count}")
new_label.focus_set()
new_label.bind(sequence="<Button-1>", command=partial(pop_out, count))
new_label.pack()
self.seq_list_labels.append(new_label)
def upload(self):
pass
@staticmethod
def instruction_callback():
"""Opens README of GitHub repo."""
webbrowser.open_new("https://github.com/AhmetMuratAcar/CrossSeq/blob/master/README.md")
class App:
"""Class that puts everything together. The main window."""
def __init__(self, master):
self.root = master
self.root.title("CrossSeq")
self.frame_list = []
self.count = len(self.frame_list) + 1
self.pop_out_window = None
self.results_window = None
# Bringing the toolbar frame into the main window
self.toolbar_frame = ToolBar(self.root)
# The scrollable frame in which all sequences are stored
self.sequences_frame = customtkinter.CTkScrollableFrame(master=self.root,
width=700,
height=400,
corner_radius=0,
fg_color="transparent")
self.sequences_frame.pack(side="left", fill="both", expand="true")
# Bringing in the main sequence frame
self.main_frame = MainSeq(self.sequences_frame)
self.results = []
def update_labels(self):
"""Updates all sequence labels after a sequence frame is deleted."""
for i, frame in enumerate(self.frame_list):
frame.new_label.configure(text=f"Sequence #{i + 1}")
def open_results(self):
"""Creates the Toplevel window to display results."""
self.results_window = Results()
def pop_out(position, *args): # I have no idea why *args is needed but when I take it out everything breaks.
"""On press of a sequence list label, creates a pop out window of the corresponding sequence frame."""
# Checks if there is already a pop out window.
if app.pop_out_window is not None:
app.pop_out_window.focus()
return
top = customtkinter.CTkToplevel()
top.geometry("650x250")
app.pop_out_window = top
# Setting title of pop out window.
if position == -1:
seq_name = "Main Sequence"
else:
seq_name = f"Sequence #{position}"
top.title(seq_name)
# Creating and placing the copies.
label_copy = customtkinter.CTkLabel(master=top, text=seq_name, padx=20)
label_copy.pack(anchor="w")
textbox_copy = customtkinter.CTkTextbox(master=top)
textbox_copy.pack(padx=20, pady=(0, 20), side="left", anchor="w", fill="both", expand="true")
if position == -1:
textbox_copy.insert("0.0", app.main_frame.main_seq_text.get("0.0", "end"))
else:
textbox_copy.insert("0.0", app.frame_list[position-1].new_seq_box.get("0.0", "end"))
# Updating the text box in the main window from changes in the pop out window.
def update_main_window(event):
if position == -1:
app.main_frame.main_seq_text.delete("0.0", "end")
app.main_frame.main_seq_text.insert("0.0", textbox_copy.get("0.0", "end"))
else:
app.frame_list[position - 1].new_seq_box.delete("0.0", "end")
app.frame_list[position - 1].new_seq_box.insert("0.0", textbox_copy.get("0.0", "end"))
textbox_copy.bind("<KeyRelease>", update_main_window)
# Resetting the presence of a pop out window.
def reset_top(event):
app.pop_out_window = None
top.bind("<Destroy>", func=reset_top)
def change_appearance_mode_event(new_scaling: str):
"""Changes the theme of the app (Light, Dark, System)."""
customtkinter.set_appearance_mode(new_scaling)
def new_frame():
"""Creates the new sequence frames and updates the sequence list."""
app.frame_list.append(SeqFrame(master=app.sequences_frame, count=app.count))
app.toolbar_frame.seq_list_update(count=app.count)
app.count += 1
def submission():
"""Creates the main/compared Sequence class objects. Then creates the pop out window in which the results are
shown."""
app.seq_list = []
del_indexes = []
# Verifying correct submission format
for frame in app.frame_list:
curr_text = frame.new_seq_box.get("0.0", "end")
if curr_text.startswith(">"):
app.seq_list.append(curr_text)
else:
del_indexes.append(app.frame_list.index(frame))
# Deleting incorrectly formatted submissions
def submission_del(indexes):
for index in indexes:
app.frame_list[index].new_text_frame.destroy()
app.frame_list[index] = None
app.toolbar_frame.seq_list_labels[-1].destroy()
app.toolbar_frame.seq_list_labels.pop()
app.count = len(app.frame_list) + 1
app.frame_list = [i for i in app.frame_list if i is not None] # It works I guess.
app.count = len(app.frame_list) + 1
app.update_labels()
submission_del(del_indexes)
# Create Sequence class objects.
app.seq_tuples = []
main_seq_object = Sequence()
app.seq_tuples.append((app.main_frame.main_seq_text, main_seq_object))
for frame in app.frame_list:
seq_object = Sequence()
analysis_tuple = (frame.new_seq_box, seq_object)
app.seq_tuples.append(analysis_tuple)
sequence_formatting(app.seq_tuples) # Correctly formats all Sequence objects using text box submission.
app.object_list = [] # Storing all Sequence objects
for sequence in app.seq_tuples:
app.object_list.append(sequence[1])
# Here is where the analysis between the two sequences would happen.
codon_analysis(app.object_list)
nucleotide_analysis(app.object_list)
# Generating graphs from analysis results. [1:] because first object is the main_seq object.
app.results = graph_gen(app.object_list[1:])
# Creating and displaying results page
app.open_results()
app.results_window.result_creation(main_seq=app.object_list[0], objects=app.object_list[1:], graphs=app.results)
root = customtkinter.CTk()
root.geometry("1050x600")
if __name__ == "__main__":
app = App(root)
root.mainloop()