コード例 #1
0
    def batch_process(self):
        if self.process_parameters.data_folder:
            self.process_window = BatchProcessProgressWindow(self)
            self.process_window.create_window()

            # Calibration
            if self.process_parameters.calibration_file:
                self.reference = read_peak_list(
                    self.process_parameters.calibration_file)
                self.files = self.get_calibration_files()

                # Read data
                self.read_data()

                # Perform calibration
                progress = self.process_window.calibration_progress_bar
                for index, self.chrom in enumerate(self.data):
                    progress.counter.set((float(index) / len(self.data)) * 100)
                    progress.update_progress_bar()
                    self.chrom.determine_calibration_timepairs()
                    # Still need to include a check against number of calibrants
                    self.chrom.determine_calibration_function()
                    self.chrom.calibrate_chromatogram()
                progress.fill_bar()

            # Quantitation
            if self.process_parameters.quantitation_file:
                self.reference = read_peak_list(
                    self.process_parameters.quantitation_file)
                self.files = self.get_quantitation_files()

                # Read data
                if not self.data:
                    self.data = self.read_data()

                # Perform quantitation
                progress = self.process_window.quantitation_progress_bar
                for index, self.chrom in enumerate(self.data):
                    progress.counter.set((float(index) / len(self.data)) * 100)
                    progress.update_progress_bar()
                    self.chrom.quantify_chromatogram()
                progress.fill_bar()

                # Generate summary file
                output = Output(self)
                output.init_output_file()
                output.build_output_file()

            # Report generation
            if self.output_parameters.pdf_report.get() == True:
                progress = self.process_window.report_progress_bar
                for index, self.chrom in enumerate(self.data):
                    progress.counter.set((float(index) / len(self.data)) * 100)
                    progress.update_progress_bar()
                    self.chrom.generate_pdf_report()
                progress.fill_bar()
コード例 #2
0
ファイル: HappyTools.py プロジェクト: johnmetta/HappyTools
class HappyToolsGui(object):
    @classmethod
    def run(cls):
        root = tk.Tk()
        HappyToolsGui(root)
        root.mainloop()

    def __init__(self, master):
        # Inherit Tk() root object
        self.master = master

        # Define task_label for progress bar functionality
        task_label = tk.StringVar()
        task_label.set('Idle')

        # LOGGING
        logging.basicConfig(filename='HappyTools.log',
                            format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                            datefmt='%Y-%m-%d %H:%M', filemode='a',
                            level=logging.WARNING)

        # ACCESS CHECK
        self.directories = directories
        if not check_disk_access(self):
            messagebox.showinfo(
                'Access Error', 'HappyTools does ' +
                'not have sufficient disk access rights. Please close ' +
                'HappyTools and check if the current user has read/' +
                'write access to all folders in the Happytools folder.')

        # CANVAS
        fig = figure.Figure(figsize=(12,6))
        axes = fig.add_subplot(111)
        axes.axis('off')
        canvas = FigureCanvasTkAgg(fig, master=master)
        CustomToolbar(canvas, master)
        canvas.get_tk_widget().pack(fill=tk.BOTH, expand=tk.YES)
        canvas.draw()

        # FRAME
        tk.Frame(master)
        master.title('HappyTools '+str(version.version) +
                     ' (Build '+str(version.build)+')')
        iconbitmap = Path.cwd() / 'HappyTools' / 'gui' / 'assets' / 'Icon.ico'
        backgroundimage = Path.cwd() / 'HappyTools' / 'gui' / 'assets' / 'UI.png'
        try:
            master.iconbitmap(default=iconbitmap)
        except tk.TclError as e:
            logging.getLogger(__name__).warn(e)
        if backgroundimage.is_file():
            img = image.imread(str(backgroundimage))
            axes.imshow(img)
            axes.set_aspect('auto')
        task  = tk.Label(master, textvariable=task_label, width=20)
        task.pack()
        progress = ProgressBar(self.master)
        progress.bar.pack(fill=tk.X)

        # QUIT
        master.protocol('WM_DELETE_WINDOW', self.close)

        # MENU
        menu = tk.Menu(master)
        master.config(menu=menu)

        filemenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='File', menu=filemenu)
        filemenu.add_command(label='Open Chromatogram',
                             command=self.open_chromatogram_window)
        filemenu.add_command(label='Smooth Chromatogram',
                             command=self.smooth_chromatogram)
        filemenu.add_command(label='Baseline Correction',
                             command=self.baseline_correction)
        filemenu.add_command(label='Normalize chromatogram',
                             command=self.normalize_chromatogram)
        filemenu.add_command(label='Save Chromatogram',
                             command=self.save_chromatogram)

        processmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='Process', menu=processmenu)
        processmenu.add_command(label='Calibrate Chromatogram',
                                command=self.calibrate_chromatogram)
        processmenu.add_command(label='Quantify Chromatogram',
                                command=self.quantify_chromatogram)
        processmenu.add_command(label='Select Outputs',
                                command=self.open_output_window)

        advancedmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='Advanced', menu=advancedmenu)
        advancedmenu.add_command(label='Peak Detection',
                                 command=self.peak_detection)
        advancedmenu.add_command(label='Save Calibrants',
                                 command=self.save_calibrants)
        advancedmenu.add_command(label='Save Annotation',
                                 command=self.save_annotation)

        batchmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='Batch', menu=batchmenu)
        batchmenu.add_command(label='Batch Process',
                              command=self.open_batch_window)

        settingsmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='Settings', menu=settingsmenu)
        settingsmenu.add_command(label='Settings',
                                 command=self.settings_window)

        aboutmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='About', menu=aboutmenu)
        aboutmenu.add_command(label='About HappyTools',
                              command=self.open_about_window)

        if (Path.cwd() / 'HappyTools' / 'plugins').glob("*.py"):
            pluginsmenu = tk.Menu(menu,tearoff=0)
            menu.add_cascade(label="Plugins", menu=pluginsmenu)
            for file in(Path.cwd() / 'HappyTools' / 'plugins').glob('*.py'):
                if '__' in str(file):
                    continue
                module_name = PurePath(file).stem
                module = "HappyTools.plugins."+str(module_name)
                module = importlib.import_module(module)
                try:
                    module_name = module.name
                except Exception as e:
                    logging.getLogger(__name__).error(e)
                pluginsmenu.add_command(label=module_name,
                                        command=self.make_function(module))

        # INHERITANCE
        self.logger = logging.getLogger(__name__)
        self.settings = Settings(self)
        self.output_parameters = OutputParameters(self)
        self.process_parameters = ProcessParameters(self)
        self.axes = axes
        self.canvas = canvas
        self.progress = progress
        self.task_label = task_label

    def make_function(self, module):
        try:
            def x():
                return module.start(self)
        except AttributeError as e:
            self.logger.error('Problem with the plugin: '+str(e))
        return x

    @classmethod
    def open_about_window(cls):
        AboutWindow()

    def open_chromatogram_window(self):
        files = filedialog.askopenfilenames(title='Open Chromatogram File(s)')
        data = []

        if files:
            self.task_label.set('Opening Chromatograms')
            self.progress.reset_bar()
            for index, filename in enumerate(files):
                self.progress.counter.set((float(index) /
                        len(files))*100)
                self.progress.update_progress_bar()
                self.filename = Path(filename)
                chromatogram = Chromatogram(self)
                chromatogram.open_chromatogram()
                data.append(chromatogram)
            self.data = data
            self.task_label.set('Idle')
            self.progress.fill_bar()

        self.axes.clear()
        for chrom in self.data:
            chrom.plot_chromatogram()
        finalize_plot(self)

    def open_output_window(self):
        OutputWindow(self)

    def open_settings_window(self):
        self.settings.settings_popup(self.settings)

    def calibrate_chromatogram(self):
        try:
            self.process_parameters.calibration = True
            self.process_parameters.calibration_file = filedialog.askopenfilename(
                title='Select Calibration File')
            if not self.process_parameters.calibration_file:
                self.process_parameters.quantitation = False
                return
            self.reference = read_peak_list(
                    self.process_parameters.calibration_file)

            self.progress.reset_bar()
            self.task_label.set('Calibrating Chromatograms')
            for index, self.chrom in enumerate(self.data):
                self.progress.counter.set((float(index) /
                        len(self.data))*100)
                self.progress.update_progress_bar()

                self.chrom.determine_calibration_timepairs()
                # Still need to include a check against number of calibrants
                self.chrom.determine_calibration_function()
                self.chrom.calibrate_chromatogram()
            self.task_label.set('Idle')
            self.progress.fill_bar()

            self.process_parameters.quantitation = False

        except Exception as e:
            self.logger.error(e)
        self.progress.fill_bar()

        self.axes.clear()
        self.progress.reset_bar()
        self.task_label.set('Plotting Chromatograms')
        for index, chrom in enumerate(self.data):
            self.progress.counter.set((float(index) /
                    len(self.data))*100)
            self.progress.update_progress_bar()
            chrom.plot_chromatogram()
        finalize_plot(self)
        self.task_label.set('Idle')
        self.progress.fill_bar()

    def close(self):
        self.master.destroy()
        self.master.quit()

    def generate_pdf_reports(self):
        self.progress.reset_bar()
        self.task_label.set('Generating PDF reports')
        for index, chrom in enumerate(self.data):
            self.progress.counter.set((float(index) /
                    len(self.data))*100)
            self.progress.update_progress_bar()
            chrom.generate_pdf_report()
        self.task_label.set('Idle')
        self.progress.fill_bar()

    def open_batch_window(self):
        batchWindow(self)

    def normalize_chromatogram(self):
        try:
            self.task_label.set('Normalizing Chromatograms')
            self.progress.reset_bar()
            self.axes.clear()
            for index, chrom in enumerate(self.data):
                self.progress.counter.set((float(index) /
                        len(self.data))*100)
                self.progress.update_progress_bar()
                chrom.normalize_chromatogram()
                chrom.plot_chromatogram()
            finalize_plot(self)
            self.task_label.set('Idle')
            self.progress.fill_bar()
        except Exception as e:
            self.logger.error(e)

    def quantify_chromatogram(self):
        try:
            self.process_parameters.quantitation = True
            self.process_parameters.quanititation_file = filedialog.askopenfilename(
                title='Select Quantitation File')
            if not self.process_parameters.quanititation_file:
                self.process_parameters.quantitation = False
                return
            self.reference = read_peak_list(
                    self.process_parameters.quanititation_file)

            self.progress.reset_bar()
            self.task_label.set('Quantifying Chromatograms')
            for index, chrom in enumerate(self.data):
                self.progress.counter.set((float(index) /
                        len(self.data))*100)
                self.progress.update_progress_bar()
                chrom.quantify_chromatogram()
            self.task_label.set('Idle')
            self.progress.fill_bar()

            if self.output_parameters.pdf_report.get() == True:
                self.generate_pdf_reports()

            self.output = Output(self)
            self.output.init_output_file()
            self.output.build_output_file()

            self.process_parameters.quantitation = False

        except Exception as e:
            self.logger.error(e)

    def peak_detection(self):
        try:
            self.axes.clear()
            for self.chrom in self.data:
                self.detected_peaks = PeakDetection(self)
                self.detected_peaks.detect_peaks()
                self.detected_peaks.plot_peaks()
                self.chrom.plot_chromatogram()
            finalize_plot(self)
        except Exception as e:
            self.logger.error(e)

    def save_annotation(self):
        try:
            for self.chrom in self.data:
                self.detected_peaks.write_peaks()
        except Exception as e:
            self.logger.error(e)

    def save_calibrants(self):
        save_calibrants(self)

    def smooth_chromatogram(self):
        try:
            self.task_label.set('Smoothing Chromatograms')
            self.progress.reset_bar()
            self.axes.clear()
            for index, chrom in enumerate(self.data):
                self.progress.counter.set((float(index) /
                        len(self.data))*100)
                self.progress.update_progress_bar()
                chrom.smooth_chromatogram()
                chrom.plot_chromatogram()
            finalize_plot(self)
            self.task_label.set('Idle')
            self.progress.fill_bar()
        except Exception as e:
            self.logger.error(e)

    def save_chromatogram(self):
        try:
            for chrom in self.data:
                chrom.save_chromatogram(self)
        except Exception as e:
            self.logger.error(e)

    def settings_window(self):
        try:
            SettingsWindow(self)
        except Exception as e:
            self.logger.error(e)

    def baseline_correction(self):
        try:
            self.task_label.set('Baseline Correcting')
            self.progress.reset_bar()
            self.axes.clear()
            for index, chrom in enumerate(self.data):
                self.progress.counter.set((float(index) /
                        len(self.data))*100)
                self.progress.update_progress_bar()
                chrom.baseline_correction()
                chrom.plot_chromatogram()
            finalize_plot(self)
            self.task_label.set('Idle')
            self.progress.fill_bar()
        except Exception as e:
            self.logger.error(e)
コード例 #3
0
class HappyToolsGui(object):
    @classmethod
    def run(cls):
        root = tk.Tk()
        HappyToolsGui(root)
        root.mainloop()

    def __init__(self, master):
        self.output_window_open = tk.IntVar(value=0)
        self.batch_folder = tk.StringVar(value=Path.cwd())
        self.abs_int = tk.IntVar(value=1)
        self.rel_int = tk.IntVar(value=1)
        self.gauss_int = tk.IntVar(value=1)
        self.bck_sub = tk.IntVar(value=1)
        self.bck_noise = tk.IntVar(value=1)
        self.peak_qual = tk.IntVar(value=1)
        self.create_figure = 'True'

        self.master = master
        self.counter = tk.IntVar(value=0)
        logging.basicConfig(
            filename='HappyTools.log',
            format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
            datefmt='%Y-%m-%d %H:%M',
            filemode='a',
            level=logging.WARNING)
        self.logger = logging.getLogger(__name__)
        self.functions = Functions(self)  # Change functions from class to non

        # ACCESS CHECK
        self.directories = directories
        if not self.functions.check_disk_access(self):
            messagebox.showinfo(
                'Access Error', 'HappyTools does ' +
                'not have sufficient disk access rights. Please close ' +
                'HappyTools and check if the current user has read/' +
                'write access to all folders in the Happytools folder.')

        # SETTINGS
        self.settings = Settings(self)
        if (Path.cwd() / self.settings.settings).is_file():
            self.settings.read_settings(self.settings)

        # CANVAS
        self.fig = figure.Figure(figsize=(12, 6))
        self.fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95)
        self.axes = self.fig.add_subplot(111)
        self.axes.axis('off')
        self.canvas = FigureCanvasTkAgg(self.fig, master=master)
        self.toolbar = CustomToolbar(self.canvas, master)
        self.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=tk.YES)
        self.canvas.draw()

        # FRAME
        tk.Frame(master)
        master.title('HappyTools ' + str(version.version) + ' (Build ' +
                     str(version.build) + ')')
        iconbitmap = Path.cwd() / 'HappyTools' / 'gui' / 'assets' / 'Icon.ico'
        backgroundimage = Path.cwd(
        ) / 'HappyTools' / 'gui' / 'assets' / 'UI.png'
        if iconbitmap.is_file():
            master.iconbitmap(default=iconbitmap)
        if backgroundimage.is_file():
            img = image.imread(str(backgroundimage))
            self.axes.imshow(img)
            self.axes.set_aspect('auto')
        self.progress = progressbar.SimpleProgressBar(self)
        self.progress.bar.pack(fill=tk.X)

        # QUIT
        master.protocol('WM_DELETE_WINDOW', self.close)

        # MENU
        menu = tk.Menu(master)
        master.config(menu=menu)

        filemenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='File', menu=filemenu)
        filemenu.add_command(label='Open Chromatogram',
                             command=self.open_chromatogram_window)
        filemenu.add_command(label='Smooth Chromatogram',
                             command=self.smooth_chromatogram)
        filemenu.add_command(label='Baseline Correction',
                             command=self.baseline_correction)
        filemenu.add_command(label='Normalize chromatogram',
                             command=self.normalize_chromatogram)
        filemenu.add_command(label='Save Chromatogram',
                             command=self.save_chromatogram)

        processmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='Process', menu=processmenu)
        processmenu.add_command(label='Calibrate Chromatogram',
                                command=self.calibrate_chromatogram)
        processmenu.add_command(label='Quantify Chromatogram',
                                command=self.quantify_chromatogram)
        processmenu.add_command(label='Select Outputs',
                                command=self.open_output_window)

        advancedmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='Advanced', menu=advancedmenu)
        advancedmenu.add_command(label='Peak Detection',
                                 command=self.peak_detection)
        advancedmenu.add_command(label='Save Calibrants',
                                 command=self.save_calibrants)
        advancedmenu.add_command(label='Save Annotation',
                                 command=self.save_annotation)

        batchmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='Batch', menu=batchmenu)
        batchmenu.add_command(label='Batch Process',
                              command=self.open_batch_window)

        settingsmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='Settings', menu=settingsmenu)
        settingsmenu.add_command(label='Settings',
                                 command=self.open_settings_window)

        aboutmenu = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label='About', menu=aboutmenu)
        aboutmenu.add_command(label='About HappyTools',
                              command=self.open_about_window)

        if (Path.cwd() / 'HappyTools' / 'plugins').glob("*.py"):
            pluginsmenu = tk.Menu(menu, tearoff=0)
            menu.add_cascade(label="Plugins", menu=pluginsmenu)
            for file in (Path.cwd() / 'HappyTools' / 'plugins').glob('*.py'):
                if '__' in str(file):
                    continue
                module_name = PurePath(file).stem
                module = "HappyTools.plugins." + str(module_name)
                module = importlib.import_module(module)
                try:
                    module_name = module.name
                except Exception as e:
                    self.logger.error(e)
                pluginsmenu.add_command(label=module_name,
                                        command=self.make_function(module))

    def make_function(self, module):
        try:

            def x():
                return module.start(self)
        except AttributeError as e:
            self.logger.error('Problem with the plugin: ' + str(e))
        return x

    @classmethod
    def open_about_window(cls):
        AboutWindow()

    def open_chromatogram_window(self):
        files = filedialog.askopenfilenames(title='Open Chromatogram File(s)')
        data = []
        if files:
            for file in files:
                foo = Chromatogram(Path(file))
                data.append(foo)
            self.data = data

        self.axes.clear()
        for chrom in self.data:
            chrom.plot_chrom(self)
        finalize_plot(self)

    def open_output_window(self):
        OutputWindow(self)

    def open_settings_window(self):
        self.settings.settings_popup(self.settings)

    def calibrate_chromatogram(self):
        try:
            self.cal_file = filedialog.askopenfilename(
                title='Select Calibration File')
            if not self.cal_file:
                self.cal_file = None
            self.reference = self.functions.read_peak_list(self.cal_file)

            self.progress.reset_bar(self)
            for index, self.chrom in enumerate(self.data):

                progress = (float(index) / len(self.data)) * 100
                self.counter.set(progress)
                self.progress.update_progress_bar(self)

                self.time_pairs = self.functions.find_peak(self)
                self.function = self.functions.determine_calibration_function(
                    self)
                self.functions.apply_calibration_function(self)

        except Exception as e:
            self.logger.error(e)
        self.progress.fill_bar(self)

        self.axes.clear()
        for chrom in self.data:
            chrom.plot_chrom(self)
        finalize_plot(self)

    def close(self):
        self.master.destroy()
        self.master.quit()

    def open_batch_window(self):
        batchWindow(self)

    def normalize_chromatogram(self):
        try:
            self.axes.clear()
            for chrom in self.data:
                chrom.trace.norm_chrom(self)
                chrom.plot_chrom(self)
            finalize_plot(self)
        except Exception as e:
            self.logger.error(e)

    def quantify_chromatogram(self):
        try:
            self.results = []
            self.functions.batch_folder = self.batch_folder
            self.quant_file = filedialog.askopenfilename(
                title='Select Quantitation File')
            if not self.quant_file:
                self.quant_file = None
            self.reference = self.functions.read_peak_list(self.quant_file)

            self.progress.reset_bar(self)
            for index, self.chrom in enumerate(self.data):

                progress = (float(index) / len(self.data)) * 100
                self.counter.set(progress)
                self.progress.update_progress_bar(self)

                self.results.append({
                    'file':
                    Path(self.chrom.filename).name,
                    'results':
                    self.functions.quantify_chrom(self)
                })

            self.output = Output(self)
            self.output.init_output_file(self)
            self.output.build_output_file(self)
            self.progress.fill_bar(self)
        except Exception as e:
            self.logger.error(e)

    def peak_detection(self):
        try:
            self.axes.clear()
            for self.chrom in self.data:
                self.detected_peaks = PeakDetection(self)
                self.detected_peaks.detect_peaks(self)
                self.detected_peaks.plot_peaks(self)
                self.chrom.plot_chrom(self)
            finalize_plot(self)
        except Exception as e:
            self.logger.error(e)

    def save_annotation(self):
        try:
            for self.chrom in self.data:
                self.detected_peaks.write_peaks(self)
        except Exception as e:
            self.logger.error(e)

    def save_calibrants(self):
        self.functions.save_calibrants(self)

    def smooth_chromatogram(self):
        try:
            self.axes.clear()
            for chrom in self.data:
                chrom.trace.smooth_chrom(self)
                chrom.plot_chrom(self)
            finalize_plot(self)
        except Exception as e:
            self.logger.error(e)

    def save_chromatogram(self):
        try:
            for chrom in self.data:
                chrom.save_chrom(self)
        except Exception as e:
            self.logger.error(e)

    def baseline_correction(self):
        try:
            self.axes.clear()
            for chrom in self.data:
                chrom.trace.baseline_correction(self)
                chrom.plot_chrom(self)
            finalize_plot(self)
        except Exception as e:
            self.logger.error(e)