From 428b5ef5f590b36e1d7597ab1fa1f66ec0762278 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Mon, 18 Jul 2016 13:57:08 +0200 Subject: [PATCH] validating files and field selection working --- array2xls/gui.py | 16 ++++++++-- array2xls/validators.py | 69 +++++++++++++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 19 deletions(-) diff --git a/array2xls/gui.py b/array2xls/gui.py index a0397fb..7b19db1 100644 --- a/array2xls/gui.py +++ b/array2xls/gui.py @@ -54,7 +54,6 @@ class FieldPanel(tk.Frame): self.rbtn_combine.pack(anchor=tk.W) self.listbox = tk.Listbox(self, selectmode=tk.MULTIPLE, activestyle='none') self.listbox.pack(fill=tk.X, ipady=5, ipadx=5) - self.listbox.insert(0, "HELLO") self.pack(side=tk.TOP, fill=tk.X) def disable_radio_btn(self): @@ -80,6 +79,18 @@ class FieldPanel(tk.Frame): self.enable_listbox() self.enable_radio_btn() + def set_listbox_content(self, choices, selection): + self.enable_listbox() + current = self.listbox.size() + self.listbox.selection_clear(0, current) + self.listbox.delete(0,current) + for i, element in enumerate(choices): + self.listbox.insert(tk.END, element) + if element in selection: + self.listbox.selection_set(i) + self.disable_listbox() + + class ActionPanel(tk.Frame): @@ -122,7 +133,7 @@ class Application(tk.Frame): self.reset() self.file_panel.clear_text() opts = { - 'initialdir': '~', + 'initialdir': '~/_signalyse test', 'filetypes': [('Signalyse Statistics', '.stx'), ('Signalyse Data', '.dat'), ('Sensovation Data', '.csv')], 'multiple': True} file_selection = tk.filedialog.askopenfilename(**opts) @@ -138,6 +149,7 @@ class Application(tk.Frame): self.reset() return self.field_panel.enable_radio_btn() + self.field_panel.set_listbox_content(self.file_validator.data_fields, self.file_validator.defaults) diff --git a/array2xls/validators.py b/array2xls/validators.py index 73da97f..807046a 100644 --- a/array2xls/validators.py +++ b/array2xls/validators.py @@ -5,52 +5,87 @@ from collections import namedtuple class ValidationError(ValueError): pass -Validator = namedtuple('Validator', 'type validate extension') +Validator = namedtuple('Validator', 'type extension validate id_fields data_fields defaults') DataFile = namedtuple('DataFile', 'path separator') def validate_stx(lines): iterator = iter(lines) line = next(iterator) - if not line.startswith('Report_Format\t2'): + if not line.startswith('Report_Format:\t2'): raise ValidationError('1 Unsupported File;' + line) for line in iterator: if line.startswith('Probe_Name\t'): break else: raise ValidationError('1 Unsupported File') - for line in iterator: - probe_name, rest = line.split('\t', 1) - separator = ',' if rest.count(',') > rest.count('.') else '.' - return separator - else: + try: + line = next(iterator) + _, numeric_data = line.split('\t', 1) + except (ValueError, StopIteration): raise ValidationError('No Data Present') + separator = ',' if numeric_data.count(',') > numeric_data.count('.') else '.' + return separator def validate_dat(lines): iterator = iter(lines) - if not next(iterator).startswith('Report_Format\t2'): + if not next(iterator).startswith('Report_Format:\t2'): raise ValidationError('Unsupported File') for line in iterator: if line.startswith('Dot_Number\t'): break else: raise ValidationError('Unsupported File') - for line in iterator: - probe_name, rest = line.split('\t', 3) - separator = ',' if rest.count(',') > rest.count('.') else '.' - return separator - else: + try: + line = next(iterator) + _, _, _, numeric_data = line.split('\t', 3) + except (ValueError, StopIteration): raise ValidationError('No Data Present') + separator = ',' if numeric_data.count(',') > numeric_data.count('.') else '.' + return separator + def validate_csv(lines): - pass + iterator = iter(lines) + if not next(iterator).startswith(' ID '): + raise ValidationError('Unsupported File') + try: + line = next(iterator) + _, numeric_data = line.split('\t', 1) + except (ValueError, StopIteration): + raise ValidationError('No Data Present') + separator = ',' if numeric_data.count(',') > numeric_data.count('.') else '.' + return separator + + +stx_validator = Validator( + 'Signalyse Statistic Files', '.stx', validate_stx, + ['Probe_Name'], + ['Count', 'Net_Signal', 'Net_Signal_SD', 'Net_Integral', 'Net_Integral_SD', 'Proc_Control'], + ['Net_Signal', 'Net_Signal_SD', 'Net_Integral', 'Net_Integral_SD']) + +dat_validator = Validator( + 'Signalyse Data Files', '.dat', validate_dat, + ['Dot_Number', 'Probe_Name', 'Gene_Name', 'Col', 'Row'], + ['X[Pix]', 'Y[Pix]', 'DX[Pix]', 'DY[Pix]', 'Spot_Diameter', 'ROI_Width', 'ROI_Heigth', 'Pixels', 'Bkg', + 'Bkg_SD', 'Net_Signal', 'Net_Signal_SD', 'Net_Integral', 'Net_Integral_SD', 'Acc_Number', 'Proc_Control'], + ['Bkg', 'Bkg_SD', 'Net_Signal', 'Net_Signal_SD', 'Net_Integral', 'Net_Integral_SD'] ) + +csv_validator = Validator( + 'Sensovation Data Files', '.csv', validate_csv, + [' ID '], + ['Pos.X', 'Pos.Y', 'Bkg.Mean', 'Spot.Mean', 'Bkg.Median', 'Spot.Median', 'Bkg.StdDev', 'Spot.StdDev', + 'Bkg.Sum', 'Spot.Sum', 'Bkg.Area', 'Spot.Area', 'Spot.Sat. (%)', 'Found', 'Pos.Nom.X', 'Pos.Nom.Y', 'Dia.', + 'Rect.', 'Contour'], + ['Bkg.Mean', 'Spot.Mean', 'Bkg.Median', 'Spot.Median', 'Bkg.StdDev', 'Spot.StdDev', 'Bkg.Sum', 'Spot.Sum'] ) validation_map = { - '.stx': Validator('Signalyse Statistic Files', validate_stx, '.stx'), - '.dat': Validator('Signalyse Data Files', validate_dat, '.dat'), - '.csv': Validator('Sensovation Data Files', validate_csv, '.csv') + stx_validator.extension: stx_validator, + dat_validator.extension: dat_validator, + csv_validator.extension: csv_validator } + def guess_validator(unvalidated): # get the validation method by examining the first file _, extension = os.path.splitext(unvalidated[0])