-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.py
486 lines (378 loc) · 15.4 KB
/
interface.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
import customtkinter as ctk
import os
import tkinter as tk
from tkinter import filedialog, ttk
import tkinter.font as tkFont
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.colors as mcolors
from alignmentfreegraph import AlignmentFreeGraph
plt.rcParams['figure.figsize'] = [16, 9]
plt.rcParams['figure.dpi'] = 80
# Variables
afg = None
selected_file = None
hash_table = None
fig = None
# Functions
def is_valid_color(color_str):
try:
mcolors.to_rgba(color_str)
return True
except ValueError:
return False
def random_color():
r = np.random.randint(0, 255)
g = np.random.randint(0, 255)
b = np.random.randint(0, 255)
return (r, g, b)
def do_new_nothing():
pass
def close_new_window():
new_window.destroy()
def select_config_file():
global selected_file
selected_file = open_file_dialog_json()
file_label.configure(text=selected_file.split("/")[-1])
def open_file_dialog():
# Use the global keyword to indicate that we want to use the global variable
global selected_file
selected_file = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file",
filetypes=[("json files", "*.json"), ("text files", "*.txt"), ("all files", "*.*")])
return selected_file
def open_file_dialog_json():
filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file",
filetypes=[("json files", "*.json")])
return filename
def open_file_dialog_json_gfa():
filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file",
filetypes=[("json files", "*.json"), ("GFA files", "*.gfa")])
return filename
def chose_file_image():
filename = filedialog.asksaveasfile(initialdir=os.getcwd(), title="Select file", defaultextension=".png", initialfile="graph.png",
filetypes=[("image files", "*.png"), ("image files", "jpeg"), ("all files", "*.*")])
return filename.name
def chose_file_hahstable():
filename = filedialog.asksaveasfile(initialdir=os.getcwd(), title="Select file", defaultextension=".json", initialfile="hash_table.json",
filetypes=[("JSON files", "*.json"), ("Excel file", "*.xlsx"), ("CSV files", "*.csv"), ("all files", "*.*")])
return filename.name
def is_file_in_current_directory(filename):
files_in_directory = os.listdir('.')
return filename in files_in_directory
def login():
location = location_entry.get()
if location == "":
location = None
db_name = db_name_entry.get()
if db_name == "":
db_name = None
username = username_entry.get()
if username == "":
username = None
password = password_entry.get()
if password == "":
password = None
global afg
global selected_file
new_window.destroy()
try:
afg = AlignmentFreeGraph(
location, db_name, username, password, selected_file)
selected_file = None
change_connection()
except Exception as e:
new_connection(error=e)
def plot_graph():
for widget in plot_frame.winfo_children():
widget.destroy()
global fig
fig = Figure(figsize=(10, 6), dpi=50)
ax = fig.add_subplot(111)
global afg
graph = afg.get_networkx_di_graph()
pos = nx.spring_layout(graph)
nx.draw_networkx_nodes(graph, pos, node_size=1000,
node_color="skyblue", node_shape="o", alpha=0.8, ax=ax)
nx.draw_networkx_labels(graph, pos, labels=nx.get_node_attributes(
graph, "name"), font_weight="bold", font_size=14, font_color="black", font_family="arial", ax=ax)
node_labels = {node: f"\n\n\n{node}" for node in graph.nodes}
nx.draw_networkx_labels(
graph, pos, labels=node_labels, font_color='black', ax=ax)
colors = {}
for edge in graph.edges:
rad = -0.2
for color in graph.edges[edge]["label"].split("+"):
if is_valid_color(color):
edge_color = color
else:
if color not in colors:
while True:
new_color = random_color()
if new_color not in colors.values():
colors[color] = "#{:02x}{:02x}{:02x}".format(
*new_color)
break
edge_color = colors[color]
nx.draw_networkx_edges(graph, pos, edgelist=[
edge], connectionstyle=f"arc3,rad={rad}", arrows=True, arrowsize=20, width=2, edge_color=edge_color, ax=ax)
if rad < 0:
rad *= -1
else:
rad += 0.2
rad *= -1
fig.tight_layout()
ax.set_axis_off()
canvas = FigureCanvasTkAgg(fig, master=plot_frame) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side="left", anchor="n", pady=0, padx=0)
def new_connection(message: str = "Create a conection", error: str = None):
global new_window
new_window = ctk.CTkToplevel(master=root)
new_window.title("New Connection")
new_window.geometry("500x500")
if afg is None:
root.config(bg='gray')
new_window.grab_set()
# change the background color back to white when the new window is closed
new_window.bind('<Destroy>', lambda e: root.config(bg=root_bg_color))
new_window.protocol("WM_DELETE_WINDOW", do_new_nothing)
else:
new_window.protocol("WM_DELETE_WINDOW", close_new_window)
message_label = ctk.CTkLabel(
master=new_window, text=message, font=("Roboto", 16))
message_label.pack()
if error is not None:
error_label = ctk.CTkLabel(
master=new_window, text=error, text_color="red")
error_label.pack()
conn_bg_color = new_window.cget("bg")
global file_label
file_button = ctk.CTkButton(
master=new_window, text="Connect by file", command=select_config_file, fg_color="#24a0ed", hover_color="#1183ca")
file_button.pack(pady=10)
file_label = ctk.CTkLabel(
master=new_window, text="", font=("Consolas", 12))
file_label.pack()
global location_entry
location_frame = ctk.CTkFrame(
master=new_window, bg_color=conn_bg_color)
location_label = ctk.CTkLabel(master=location_frame, text="Location")
location_entry = ctk.CTkEntry(
master=location_frame, placeholder_text="Location")
global db_name_entry
db_name_frame = ctk.CTkFrame(master=new_window, bg_color=conn_bg_color)
db_name_label = ctk.CTkLabel(master=db_name_frame, text="DB Name")
db_name_entry = ctk.CTkEntry(
master=db_name_frame, placeholder_text="DB Name")
global username_entry
username_frame = ctk.CTkFrame(master=new_window, bg_color=conn_bg_color)
username_label = ctk.CTkLabel(master=username_frame, text="Username")
username_entry = ctk.CTkEntry(
master=username_frame, placeholder_text="Username")
global password_entry
password_frame = ctk.CTkFrame(master=new_window, bg_color=conn_bg_color)
password_label = ctk.CTkLabel(master=password_frame, text="Password")
password_entry = ctk.CTkEntry(
master=password_frame, show="*", placeholder_text="Password")
location_label.pack(side="left", padx=5)
location_entry.pack(side="left")
location_frame.pack(pady=10)
db_name_label.pack(side="left", padx=5)
db_name_entry.pack(side="left")
db_name_frame.pack(pady=10)
username_label.pack(side="left", padx=5)
username_entry.pack(side="left")
username_frame.pack(pady=10)
password_label.pack(side="left", padx=5)
password_entry.pack(side="left")
password_frame.pack(pady=10)
login_button = ctk.CTkButton(
master=new_window, text="Login", command=login,)
login_button.pack(pady=30)
def show_hashtable():
global afg
# Create a treeview for the hashtable
for widget in hash_table_frame.winfo_children():
widget.destroy()
tree = ttk.Treeview(
hash_table_frame, selectmode='browse', show='headings')
# Assuming hashtable is a dictionary
# Replace with your method to get the hashtable
hashtable = afg.get_hashtable_df()
# Create the columns
columns = tuple(hashtable.columns)
tree["columns"] = columns
tree.column("#0", width=0, stretch=ctk.NO)
for col in columns:
# Calculate the maximum width needed for the column content
max_width = max([tree.heading(col)["text"], *[str(value)
for value in hashtable[col]]], key=len)
# Set column width to the maximum width
tree.column(col, width=tkFont.Font().measure(
max_width) + 20, anchor="w")
# Create the headings
tree.heading("#0", text="", anchor=ctk.W)
for col in columns:
tree.heading(col, text=col, anchor=ctk.W)
# Add the data from the hashtable
for index, row in hashtable.iterrows():
tree.insert("", tk.END, values=tuple(row))
tree.pack(expand=True, fill="both")
def change_k(event):
global afg
global k_value_entry
k = k_value_entry.get()
if feasible_k():
k = int(k)
if k != afg.get_k():
afg.set_k(k)
show_hashtable()
k_value_problem_label.configure(text="")
else:
k_value_entry.delete(0, tk.END)
k_value_entry.insert(0, str(afg.get_k()))
k_value_problem_label.configure(
text=f"{k} not feasible")
def feasible_k():
# controll if k value is a number
global k_value_entry
k = k_value_entry.get()
if is_int(k):
k = int(k)
if k > 0:
return True
return False
def is_int(string: str):
return string.isdigit()
def search_sequence():
global afg
global sequence_entry
global sequence_result_label
sequence = sequence_entry.get()
if sequence != "":
result = afg.sequence_from_graph(sequence)
if result is not None:
sequence_result_label.configure(text="Result: " + str(result))
else:
sequence_result_label.configure(text="Result: ")
def add_from_file():
global afg
file = open_file_dialog_json_gfa()
if file is not None:
if file.endswith(".gfa"):
afg.upload_from_gfa(file)
elif file.endswith(".json"):
afg.upload_from_json(file)
plot_graph()
show_hashtable()
def delete_all_nodes():
global afg
afg.delete_all()
plot_graph()
show_hashtable()
def change_connection():
global afg
global k_value_entry
k_value_entry.insert(0, str(afg.get_k()))
plot_graph()
show_hashtable()
def export_graph():
global fig
fig.savefig(chose_file_image())
def export_hash():
global afg
afg.export_hashtable(chose_file_hahstable())
# Creating interface
# interface style
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("green")
# main window
root = ctk.CTk()
root.geometry("800x800")
root.minsize(500, 500)
root.maxsize(1000, 1000)
root_bg_color = root.cget("bg")
root.title("Alignment-Free Sequence to Graph")
open_window_button = ctk.CTkButton(
master=root, text="New Connection", command=new_connection)
open_window_button.pack(anchor="ne", pady=5, padx=10)
# frame of the interface
frame = ctk.CTkFrame(master=root)
frame.pack(pady=10, padx=15, fill="both", expand=True)
# Project title
title = ctk.CTkLabel(
master=frame, text="Alignment-Free Sequence to Graph", font=("Roboto", 24))
title.pack(pady=5, padx=10)
if afg is None and is_file_in_current_directory("credentials.json"):
try:
afg = AlignmentFreeGraph(configuration="credentials.json")
except Exception as e:
new_connection(error=e)
graph_frame = ctk.CTkFrame(master=frame)
graph_frame.pack(pady=10, padx=15, anchor="n", expand=True)
plot_frame = ctk.CTkFrame(master=graph_frame)
plot_frame.pack(side="left", pady=3, padx=3, expand=True)
if afg is not None:
plot_graph()
k_value_frame = ctk.CTkFrame(master=graph_frame)
k_value_label = ctk.CTkLabel(master=k_value_frame, text="k = ")
k_value_entry = ctk.CTkEntry(master=k_value_frame, width=45)
if afg is not None:
k_value_entry.insert(0, str(afg.get_k()))
k_value_frame.pack(anchor="n", pady=10, padx=5, expand=True)
k_value_label.pack(side="left", anchor="n", pady=10, padx=5)
k_value_entry.pack(side="left", anchor="n", pady=10, padx=3)
k_value_entry.bind("<Return>", change_k)
k_value_problem_label = ctk.CTkLabel(
master=k_value_frame, text="", text_color="red")
k_value_problem_label.pack(side="left", pady=0, padx=1, expand=True)
hash_table_frame = ctk.CTkFrame(master=graph_frame)
hash_table_frame.pack(side="right", pady=10, padx=15, expand=True)
if afg is not None:
show_hashtable()
option_graph_frame = ctk.CTkFrame(master=frame)
option_graph_frame.pack(side="top", anchor="n", pady=10,
padx=5, expand=True, after=graph_frame)
refresh_graph_button = ctk.CTkButton(
master=option_graph_frame, text="Refresh Graph", command=plot_graph, fg_color="#24a0ed", hover_color="#1183ca")
refresh_graph_button.pack(side="left", pady=5, padx=10)
delete_all_button = ctk.CTkButton(
master=option_graph_frame, text="Delete all nodes", command=delete_all_nodes, fg_color="#df2c14", hover_color="#c61a09")
delete_all_button.pack(side="left", pady=5, padx=10)
add_from_file_button = ctk.CTkButton(
master=option_graph_frame, text="Add from file", command=add_from_file, fg_color="#2ecc71", hover_color="#27ae60")
add_from_file_button.pack(side="left", pady=5, padx=10)
export_frame = ctk.CTkFrame(master=frame)
export_frame.pack(side="top", anchor="n", pady=10, padx=15,
expand=True, after=option_graph_frame)
export_graph_button = ctk.CTkButton(master=export_frame, text="Export Graph",
command=export_graph, fg_color="#24a0ed", hover_color="#1183ca")
export_graph_button.pack(side="left", pady=5, padx=10, expand=True)
export_hash_button = ctk.CTkButton(master=export_frame, text="Export HashTable",
command=export_hash, fg_color="#24a0ed", hover_color="#1183ca")
export_hash_button.pack(side="right", pady=5, padx=10, expand=True)
sequence_frame = ctk.CTkFrame(master=frame)
sequence_entry_frame = ctk.CTkFrame(master=sequence_frame)
sequence_label = ctk.CTkLabel(master=sequence_entry_frame, text="Sequence")
sequence_entry = ctk.CTkEntry(master=sequence_entry_frame)
sequence_button = ctk.CTkButton(
master=sequence_frame, text="Search", command=search_sequence)
sequence_result_frame = ctk.CTkFrame(master=sequence_frame)
sequence_result_label = ctk.CTkLabel(
master=sequence_result_frame, text="Result")
sequence_frame.pack(anchor="n", pady=10, padx=15, expand=True)
sequence_entry_frame.pack(anchor="n", pady=10, padx=5, expand=True)
sequence_label.pack(side="left", anchor="n", pady=10, padx=5)
sequence_entry.pack(side="left", anchor="n", pady=10, padx=3)
sequence_entry.bind("<Return>", lambda e: search_sequence())
sequence_button.pack(anchor="n", pady=10, padx=3)
sequence_result_frame.pack(anchor="n", pady=10, padx=5, expand=True)
sequence_result_label.pack(side="left", anchor="n", pady=10, padx=5)
# Create a button that will close the interface when clicked
exit_button = ctk.CTkButton(
master=root, text="Exit", command=root.destroy, fg_color="#df2c14", hover_color="#c61a09")
exit_button.pack(side='bottom', pady=5, padx=10)
root.mainloop()