Skip to content

Commit ec52715

Browse files
committed
COPAL_gui: added output data checker, check for validity of output settings
1 parent 2800f90 commit ec52715

File tree

1 file changed

+70
-20
lines changed

1 file changed

+70
-20
lines changed

COPAL_gui.py

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def get_normalisation(normtype, normcol, normfile):
7474
return (norm_check, normcol, normfile)
7575

7676
def clear_last_input():
77+
"""
78+
removes last added set of input specs from global input dict
79+
"""
7780
added_samples = len(input['samplecolumns'][-1])
7881
print('clearing wrong input..')
7982
print('number of added samples: {}'.format(added_samples))
@@ -201,7 +204,7 @@ def check_input():
201204

202205
return True
203206

204-
def check_input_data(input):
207+
def check_input_data():
205208
"""
206209
check if input data is valid and matches input specs
207210
"""
@@ -243,15 +246,16 @@ def check_input_data(input):
243246
clear_last_input()
244247
return False
245248

246-
# check if samplecolumns exist and check sample length
249+
# check if samplecolumns exist
250+
dataset_columns = list(data.columns)
247251
for col_pair in samplecolumns:
248-
if not col_pair[0] in data.columns:
252+
if not col_pair[0] in dataset_columns:
249253
warn_msg = "start column header not in dataset: {}. check input".format(col_pair[0])
250254
input_status_var.set(warn_msg)
251255
messagebox.showwarning("input error",warn_msg)
252256
clear_last_input()
253257
return False
254-
if not col_pair[1] in data.columns:
258+
if not col_pair[1] in dataset_columns:
255259
warn_msg = "end column header not in dataset: {}. check input".format(col_pair[1])
256260
input_status_var.set(warn_msg)
257261
messagebox.showwarning("input error",warn_msg)
@@ -263,15 +267,13 @@ def check_input_data(input):
263267
sample_data = data.loc[:,col_pair[0]:col_pair[1]]
264268
except Exception as error:
265269
warn_msg = "sample could not be extracted from data. error: {}".format(error)
266-
"please check input"
267270
input_status_var.set("problem extracting sample from data. check input")
268271
messagebox.showwarning("input error",warn_msg)
269272
clear_last_input()
270273
return False
271274

272275
if sample_data.shape[1] < 2:
273276
warn_msg = "found sample with 0 or 1 columns: {}:{}. ".format(col_pair[0],col_pair[1])
274-
"Please check input"
275277
input_status_var.set(warn_msg)
276278
messagebox.showwarning("input error", warn_msg)
277279
clear_last_input()
@@ -280,12 +282,13 @@ def check_input_data(input):
280282
# check for nan values in sample
281283
if sample_data.isnull().values.any():
282284
warn_msg = "sample contains missing (nan) values: {}:{}. ".format(col_pair[0],col_pair[1])
283-
"please check input"
284285
input_status_var.set(warn_msg)
285286
messagebox.showwarning("input error", warn_msg)
286287
clear_last_input()
287288
return False
288289

290+
# store columns of each dataset to check outputspecs
291+
input['dataset_columns'].append(dataset_columns)
289292
return True
290293

291294
def check_output():
@@ -346,6 +349,42 @@ def check_output():
346349

347350
return True
348351

352+
def check_output_data():
353+
"""
354+
checks if output data is valid
355+
"""
356+
input_columnsets = input['dataset_columns']
357+
normcol = input['normcol']
358+
normfile = input['normfile']
359+
gseacol = input['GSEA_rank_column']
360+
361+
# check normcol
362+
if normcol:
363+
for dataset in input_columnsets:
364+
if not normcol in dataset:
365+
warn_msg = "normalisation column header not present in one of the input datasets!"
366+
status_var.set(warn_msg)
367+
messagebox.showwarning("input error",warn_msg)
368+
return False
369+
# check gsea_col
370+
if gseacol:
371+
for dataset in input_columnsets:
372+
if not gseacol in dataset:
373+
warn_msg = "gsea column header not present in one of the input datasets!"
374+
status_var.set(warn_msg)
375+
messagebox.showwarning("input error",warn_msg)
376+
return False
377+
378+
# check normfile
379+
if normfile:
380+
if not os.path.isfile(normfile):
381+
warn_msg = "normfile does not exist! check input."
382+
status_var.set(warn_msg)
383+
messagebox.showwarning("input error",warn_msg)
384+
return False
385+
386+
return True
387+
349388
def print_input():
350389
"""prints out global input variables to stdout"""
351390
print("file name: ", input['filename'])
@@ -388,6 +427,8 @@ def run_analysis():
388427

389428
def back_to_input_handler(event = None):
390429
"""clears input and goes back to first GUI frame"""
430+
# clear dataset columns when re-entering input
431+
input['dataset_columns'] = []
391432
clear_input_vars()
392433
first_input_frame()
393434

@@ -427,7 +468,13 @@ def save_proceed_handler(event = None):
427468
"""
428469
if check_input():
429470
save_input_settings()
430-
if check_input_data(input):
471+
if check_input_data():
472+
if len(input['samplenames']) < 2:
473+
warn_msg = ('only 1 sample specified! need at least two samples for alignment. '
474+
'add another sample from this file or add another file')
475+
status_var.set(warn_msg)
476+
messagebox.showwarning("input error",warn_msg)
477+
return
431478
print_input()
432479
output_frame = tk.Frame(root)
433480
output_frame.grid(row = 0, column = 0, sticky = "news")
@@ -443,7 +490,7 @@ def append_proceed_handler(event = None):
443490
"""
444491
if check_input():
445492
append_extra_input()
446-
if check_input_data(input):
493+
if check_input_data():
447494
print_input()
448495
output_frame = tk.Frame(root)
449496
output_frame.grid(row = 0, column = 0, sticky = "news")
@@ -459,7 +506,7 @@ def save_extra_input_handler(event = None):
459506
"""
460507
if check_input():
461508
save_input_settings()
462-
if check_input_data(input):
509+
if check_input_data():
463510
clear_input_vars()
464511
extra_frame = tk.Frame(root)
465512
extra_frame.grid(row = 0, column = 0, sticky = "news")
@@ -476,7 +523,7 @@ def append_extra_input_handler(event = None):
476523
"""
477524
if check_input():
478525
append_extra_input()
479-
if check_input_data(input):
526+
if check_input_data():
480527
clear_input_vars()
481528
extra_frame = tk.Frame(root)
482529
extra_frame.grid(row = 0, column = 0, sticky = "news")
@@ -499,14 +546,15 @@ def save_output_and_run_handler(event = None):
499546
root.update_idletasks()
500547
return
501548

502-
status_var.set("Running analysis. This might take a while...")
503-
root.update_idletasks() # ensures the status label gets updated in a timely fashion
504549
save_output_settings()
505-
print_input()
506-
print_output()
507-
analysis_thread = threading.Thread(target = run_analysis)
508-
analysis_thread.daemon = True
509-
analysis_thread.start()
550+
if check_output_data():
551+
status_var.set("Running analysis. This might take a while...")
552+
root.update_idletasks() # ensures the status label gets updated in a timely fashion
553+
print_input()
554+
print_output()
555+
analysis_thread = threading.Thread(target = run_analysis)
556+
analysis_thread.daemon = True
557+
analysis_thread.start()
510558

511559
# ---------- GUI WIDGET CREATING FUNCTIONS ---------- #
512560
def input_frame(master):
@@ -527,7 +575,7 @@ def input_frame(master):
527575
get_file_button.grid(row = 1, column = 4, padx = 4, sticky = tk.W)
528576

529577
# choose filetype dropdown menu
530-
file_type_dropdown = ttk.OptionMenu(master, file_type,"excel","excel", "csv del ';' dec ','",
578+
file_type_dropdown = ttk.OptionMenu(master, file_type,"csv del ',' dec '.'","excel", "csv del ';' dec ','",
531579
"csv del ',' dec '.'", "tsv del '\\t' dec ','",
532580
"tsv del '\\t' dec '.'")
533581
file_type_dropdown.grid(row = 1, column = 5, sticky = tk.EW)
@@ -701,7 +749,6 @@ def first_input_frame():
701749
#root.resizable(0,0) # prevents window from being resized
702750

703751
# try to set application icon (does not work on all platforms)
704-
705752
try:
706753
root.iconbitmap(os.path.join(os.getcwd(),'copal/static/Copal.ico'))
707754
except Exception as e:
@@ -730,6 +777,9 @@ def first_input_frame():
730777

731778
# initialize global dictionary containing input for analysis
732779
input = {}
780+
# create empty list. to enter column-list of each dataset into.
781+
# used to check if output frame specs match input data
782+
input['dataset_columns'] = []
733783

734784
# set variable default values
735785
file_type.set("excel")

0 commit comments

Comments
 (0)