def __init__(self, dir, printlabel, Settings="empty"): from functions import fn_check_dir from GUI_mainframe import update_GUI update_GUI("Reading in experiment.", printlabel) self.dir = dir if Settings == "empty": from functions import fn_settings Settings = fn_settings() self.Settings = Settings self.printlabel = printlabel if fn_check_dir(dir[:dir.find('pdata')], "pulseprogram"): if "seldigpzs2d" in open( dir[:dir.find('pdata')] + r"\pulseprogram", "r").readlines()[0]: self.pp = "seldigpzs2d" self.init_seldigpzs2d(self.dir, self.printlabel) elif "dipsigpphzs" in open( dir[:dir.find('pdata')] + r"\pulseprogram", "r").readlines()[0]: self.pp = "dipsigpphzsbs" self.init_dipsigpphzsbs(self.dir, self.printlabel) else: update_GUI("error; no correct pulseprogram found", printlabel)
def fn_save(self): #save the files from GUI_mainframe import update_GUI import pickle update_GUI("Completed database - 100%", self.printlabel) pickle.dump(self.content, open(self.dir + r"\Database.p", "wb")) # temporary add an extra txt file if str(self.printlabel) == "testing": file = open(self.dir + r"\Database.txt", "a") for x in self.content: file.write(str(x) + "\n") self.status = True return self.status
def fn_load(self): # load in the functions from pickle import load from functions import fn_check_dir from GUI_mainframe import update_GUI if (fn_check_dir(self.dir, r"\Database.p")): self.content = load(open(self.dir + r"\Database.p", "rb")) update_GUI("Loaded the Database from given location.", self.printlabel) self.fn_override_settings(self.Settings) else: self.content = [] update_GUI("No database file was found in the given location.", self.printlabel) return self
def __init__(self, values, peak_ind, max_curve, sample_name, mtlist, chunk_fq, printlabel, Settings): #set all parameters given for backup self.data = values #self.data now is a list of lists self.peak_ind = peak_ind self.max_curve = max_curve self.sample_name = sample_name self.mtlist = mtlist self.chunk_fq = chunk_fq self.printlabel = printlabel self.Settings = Settings #import GUI printer from GUI_mainframe import update_GUI import numpy as np update_GUI( format("Fitting curves on chunk of %s..." % self.sample_name), self.printlabel) self.content = [] #self.content will become a list of curves # split up the values into different curves for x in range(len(self.data)): temp_curve = Curve( self.data[x], self.peak_ind[x], self.max_curve[self.peak_ind[x][0]:self.peak_ind[x][1]], self.mtlist, self.Settings) if temp_curve.ok: self.content.append(temp_curve) # sort the curves (shouldnt be required) self.content.sort(key=lambda x: x.index[0]) # duplet and triplet filtering if self.Settings["gp_duplet_filtering"]: # triplet filtering self.content = self.fn_triplet_cluster() # quadruplet filtering #self.content = self.fn_quadruplet() # duplet filtering self.content = self.fn_duplet() # baseline filter #self.content = self.fn_baseline() self.fn_normalise() self.fn_curve_filter() if self.Settings["plot_values"]: self.fn_plot()
def fn_compile(self): from GUI_mainframe import update_GUI from functions import fn_check_folders # check if database exist and prompt user from functions import fn_check_dir, fn_progress_gen if fn_check_dir(self.dir, "Database.p"): import tkinter.messagebox as mg if self.printlabel == "testing": import tkinter as tk crap = tk.Tk() crap.withdraw() override = mg.askyesno( "Delete", "Override current database?\nIf you are missing items the database will not be complete." ) if not override: update_GUI("Not performing database compilation.", self.printlabel) return # check for all existing folders in the given location dir_list = fn_check_folders(self.dir) self.dir_list = dir_list #initialise all experiments and calculate progressbar progress = round(0.000000, 1) update_GUI("Performing database compilation.", self.printlabel) # wipe current self.content = [] for y in range(len(self.dir_list)): if round((1 + y) / (len(dir_list) + 1), 1) != progress: progress = round(y / len(dir_list), 1) update_GUI(fn_progress_gen(progress), self.printlabel) #load in the experiment exp = Experiment(dir_list[y] + "\\pdata\\1", "ignore") for chunk in exp.chunks: self.content.append(chunk) self.status = self.fn_save()
def katelijne(printlabel, Settings): import nmrglue as ng #NMR software import numpy as np katelijne_lijst = [] sugar_list = [[ "a-Glucopyr.", r"D:\DATA\Katelijne2011\glucose_700", [21, 22, 23, 24, 25, 26, 27, 28, 29, 30] ], [ "b-Glucopyr.", r"D:\DATA\Katelijne2011\glucose_700", [41, 42, 43, 44, 45, 46, 47, 48, 49, 50] ], [ "a-Rhamnopyr.", r"D:\DATA\Katelijne2011\rhamnose_700", [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] ], [ "b-Rhamnopyr.", r"D:\DATA\Katelijne2011\rhamnose_700", [23, 24, 25, 26, 27, 28, 29, 30, 31, 32] ], [ "a-Galactopyr.", r"D:\DATA\Katelijne2011\galactose_700", [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] ], [ "b-Galactopyr.", r"D:\DATA\Katelijne2011\galactose_700", [23, 24, 25, 26, 27, 28, 29, 30, 31, 32] ], [ "a-Mannose.", r"D:\DATA\Katelijne2011\mannose_700", [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] ], [ "b-Mannose.", r"D:\DATA\Katelijne2011\mannose_700", [23, 24, 25, 26, 27, 28, 29, 30, 31, 32] ]] for sugar_id in sugar_list: sugar = sugar_id[0] dir = sugar_id[1] list = sugar_id[2] from GUI_mainframe import update_GUI update_GUI( "Performing Katelijne analysis on sugar %s\n\nUsing %s" % (sugar, str(list)), printlabel) vclist = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] data1 = [] data2 = [] for x in list: dir_exp = dir + '\\' + str(x) + r"\pdata\1" dic, data = ng.bruker.read_pdata(dir_exp) mixingtime = (open(dir_exp + r"\title").readlines()[0])[ (open(dir_exp + r"\title").readlines()[0]).find('ms') - 2:(open(dir_exp + r"\title").readlines()[0]).find('ms')] mixingtime = int(mixingtime) if mixingtime == 0: mixingtime = 100 data1.append([mixingtime, data]) data1.sort(key=lambda x: x[0], reverse=False) for x in data1: data2.append(x[1]) # collect the parameters and convert to ppm dic2 = open(dir_exp[:dir_exp.find('pdata')] + r'\acqus', 'r').read() B0_hz = float(dic2[dic2.find(r'$SFO1=') + 7:dic2.find('##$SFO2')]) SO1_hz = float(dic2[dic2.find(r'$O1=') + 5:dic2.find('##$O2')]) SW_hz = float(dic['procs']['SW_p']) SW_ppm = SW_hz / B0_hz SO1_ppm = SO1_hz / B0_hz duplet_ppm = 14 / B0_hz from Class import Chunk temp = Chunk(data2, sugar, 100, SW_ppm, duplet_ppm, vclist, printlabel, Settings) temp.fn_plot() katelijne_lijst.append(temp) import pickle pickle.dump(katelijne_lijst, open(Settings["Database_Directory"] + r"\kat.p", "wb")) return katelijne_lijst
def init_seldigpzs2d(self, dir, printlabel): import nmrglue as ng import numpy as np from functions import fn_check_dir from GUI_mainframe import update_GUI self.dic, self.data = ng.bruker.read_pdata(dir) # read in the alternate data if possible - compenation for the NMRGlue chunk error # can be deleted once we fixed nmrglue!!! if self.dic["procs"]["XDIM"] != self.dic["procs"]["FTSIZE"]: self.data_file = "" if fn_check_dir(dir, "DATABASE.txt"): self.data_file = open(dir + r"\DATABASE.txt", 'r').readlines() elif fn_check_dir(dir, "SAMPLES.txt"): self.data_file = open(dir + r"\SAMPLES.txt", 'r').readlines() if self.data_file != "": self.data = [] for line in self.data_file: if ("row" in line): self.data.append([]) else: if "#" not in line: self.data[len(self.data) - 1].append( float(line.replace(r"\n", ""))) else: print("No extra data file was found...") print("This was not correctly processed using topspin!") quit() # reform the data for oversaving effect from copy import deepcopy test_data = deepcopy(self.data) self.data = [] for x in range(len(test_data)): if (test_data[x][0] != 0.0): self.data.append(test_data[x]) # collect the parameters from functions import fn_vclist, fn_fqlist, fn_parameters, fn_read_title self.dic2 = open(dir[:dir.find('pdata')] + r'\acqus', 'r').read() self.dic3 = open(dir[:dir.find('pdata')] + r'\acqu2s', 'r').read() self.B0_hz, self.SW_ppm, self.SW_hz, self.duplet_ppm, self.chunk_num, self.offset = fn_parameters( self.dic, self.dic2, self.dic3, self.pp) self.SW_ppm = 0.5 * self.SW_ppm self.SO1_ppm = 0.5 * self.SW_ppm - self.offset # collect the lists self.vclist = fn_vclist(dir) self.fqlist, self.fqlist_ppm = fn_fqlist(dir, self.B0_hz) self.mtlist = np.array(self.vclist) * 115.112 * 25 * 10**(-6) # colect the title self.sample_name, self.extra_info, self.order = fn_read_title(dir) # print out the title update_GUI("Working on %s sample." % self.sample_name, self.printlabel) # all data was read in now - end of initialisation of the sample # starting the chunkification if present import numpy as np from functions import fn_unique from GUI_mainframe import update_GUI # find the unique fqs self.fqlist_u = fn_unique(self.fqlist) # chunk up the data per fq and remove the minus signals (reduce data amount for calculations) self.chunks = [] update_GUI("Calculating peaks and integrals for each chunk.", self.printlabel) for x in range(self.chunk_num): chunk = self.data[x * len(self.vclist):((x + 1) * len(self.vclist))] max = np.max(chunk) row = chunk[0] keep = int(len(row) / 2) # remove negative half new_chunk, new_data = [], [] for row in chunk: new_chunk.append(row[:keep] / max) # create the title for the chunk if len(self.order) != 0: title = self.order[x] else: title = "%i-%s" % (x, self.sample_name) #initialise chunk and perform the functions that are needed chunk_temp = Chunk(new_chunk, title, self.fqlist[x], self.SW_ppm, self.duplet_ppm, self.mtlist, self.SO1_ppm, self.pp, self.printlabel, self.Settings) self.chunks.append(chunk_temp) #return self # list of all chunk objects if self.Settings["plot_exp"]: self.fn_plot()
def init_dipsigpphzsbs(self, dir, printlabel): import nmrglue as ng import numpy as np from functions import fn_check_dir, fn_p3d_reform, fn_p3d_diagonal, fn_noise_filter, index_from_ppm, ppm_from_index from GUI_mainframe import update_GUI self.dic, self.data = ng.bruker.read_pdata(dir) #oversave effect and reorder the data self.data = fn_p3d_reform(self.data) # collect the parameters from functions import fn_vclist, fn_parameters, fn_read_title self.dic2 = open(dir[:dir.find('pdata')] + r'\acqus', 'r').read() self.dic3 = open(dir[:dir.find('pdata')] + r'\acqu2s', 'r').read() self.B0_hz, self.SW_ppm, self.SW2_ppm, self.SW_hz, self.duplet_ppm, self.chunk_num, self.offset = fn_parameters( self.dic, self.dic2, self.dic3, self.pp) # extra parameter self.SO1_ppm = float(self.dic2[self.dic2.find(r'$O1=') + 5:] [:self.dic2[self.dic2.find(r'$O1=') + 5:]. find("\n")]) / self.B0_hz - self.offset # collect the lists self.vclist = fn_vclist(dir) self.mtlist = np.array(self.vclist) * 115.112 * 25 * 10**(-6) # colect the title self.sample_name, self.extra_info, self.order = fn_read_title(dir) # find the diagonal self.diag = fn_p3d_diagonal(self.data[0], self.SW_ppm, self.SW2_ppm, self.SO1_ppm) from detect_peaks import detect_peaks self.ind_diag = detect_peaks(self.diag, show=self.Settings["plot_diagonal"], mph=self.Settings["gp_threshold"]) self.ind_diag, limit = fn_noise_filter(self.ind_diag, self.diag, 2) # find duplets on the diagonal; must be averaged index & recombine duplet_ind = int(self.duplet_ppm / self.SW2_ppm * len(self.data[0])) duplet_ind_new, remove = [], [] self.ind_diag.append(9) for z in range(len(self.ind_diag) - 1): x = self.ind_diag[z] y = self.ind_diag[z + 1] if (abs(self.diag[x] - self.diag[y]) / np.max([self.diag[x], self.diag[y]]) < 0.3) and ( (y - x) < duplet_ind): duplet_ind_new.append(int((x + y) / 2)) remove.append(z + 1) else: duplet_ind_new.append(x) # remove the H2O peak len2 = len(self.data[0]) h2p_ind = index_from_ppm(4.79, self.SO1_ppm, self.SW2_ppm, len2) for z in range(len(duplet_ind_new)): x = duplet_ind_new[z] if (abs(h2p_ind - x) < 10 / 1024 * len2): remove.append(z) unique = [] [unique.append(item) for item in remove if item not in unique] remove = sorted(unique, reverse=True) for x in remove: duplet_ind_new.pop(x) self.ind_diag = duplet_ind_new self.chunk_num = len(self.ind_diag) # get the new data 2d planes and initialise the chunks self.chunks = [] update_GUI("Calculating peaks and integrals for each chunk.", self.printlabel) for x in range(self.chunk_num): index = self.ind_diag[x] chunk = [] for y in self.data: chunk.append(y[index]) max = np.max(chunk) new_chunk, new_data = [], [] for row in chunk: new_chunk.append(row / max) # create the title for the chunk temp_ppm = ( len2 - self.ind_diag[x] ) / len2 * self.SW2_ppm + self.SO1_ppm - 0.5 * self.SW2_ppm title = "%.2fppm - %s" % ((temp_ppm), self.sample_name) # initialise chunk and perform the functions that are needed chunk_temp = Chunk(new_chunk, title, temp_ppm, self.SW_ppm, self.duplet_ppm, self.mtlist, self.SO1_ppm, self.pp, self.printlabel, self.Settings) self.chunks.append(chunk_temp)