def rawMassLynx_MZDT_extract(path=None, mz_start=0, mz_end=50000, mz_nPoints=5000, dt_start=1, dt_end=200, silent_extract=True, driftscope_path='C:\DriftScope\lib', **kwargs): # check if data should be extracted to data folder OR temporary folder if kwargs.get("use_temp_folder", True) and os.path.exists(temp_data_folder): out_path = temp_data_folder else: out_path = path # Create input file range_file = os.path.join(out_path, '__.2dDTMZ.inp') try: fileID = open(range_file, 'w') except IOError as err: dlgBox(exceptionTitle="Error", exceptionMsg=str(err), type="Error") return fileID.write("{} {} {}\n0.0 9999.0 1\n{} {} 200".format(mz_start, mz_end, mz_nPoints, dt_start, dt_end)) fileID.close() # Create command for execution cmd = ''.join([driftscope_path, '\imextract.exe -d "', path, '" -f 1 -o "', out_path, '\output.2dDTMZ" -t mobilicube -p "', range_file, '"']) extractIMS = Popen(cmd, shell=kwargs.get("verbose", True), creationflags=CREATE_NEW_CONSOLE) extractIMS.wait() # return data if kwargs.get("return_data", False): if kwargs.get("normalize", False): dt, dtnorm = rawMassLynx_MZDT_load(out_path, normalize=True) return dt, dtnorm else: dt = rawMassLynx_MZDT_load(out_path) return dt else: return None
def remove_noise_1D(inputData=None, threshold=0): # Check whether threshold values meet the criteria. # First check if value is not above the maximum or below 0 if (threshold > np.max(inputData)) or (threshold < 0): dlgBox( exceptionTitle='Warning', exceptionMsg= "Threshold value was too high - the maximum value is %s. Value was reset to 0. Consider reducing your threshold value." % np.max(inputData), type="Warning") threshold = 0 elif threshold == 0.0: pass # Check if the value is a fraction (i.e. if working on a normalized dataset) elif (threshold < (np.max(inputData) / 10000)): # This is somewhat guesswork! It won't be 100 % fool proof if (threshold > 1) or (threshold <= 0): threshold = 0 dlgBox( exceptionTitle='Warning', exceptionMsg= "Threshold value was too low - the maximum value is %s. Value was reset to 0. Consider increasing your threshold value." % np.max(inputData), type="Warning") threshold = 0 # Or leave it as is if the values are correct else: threshold = threshold inputData[inputData <= threshold] = 0 return inputData
def onUniDecView(self, evt): self.config.unidec_plot_panel_view = self.unidec_view_value.GetStringSelection() dlgBox(exceptionTitle="Warning", exceptionMsg="Changing the panel view will not take place until you restart ORIGAMI.", type="Warning")
def smooth_gaussian_2D(inputData=None, sigma=2): # smoothDataGaussian # Check if input data is there if inputData is None or len(inputData) == 0: return None if sigma < 0: dlgBox(exceptionTitle='Warning', exceptionMsg="Value of sigma is too low. Value was reset to 1", type="Warning") sigma = 1 else: sigma = sigma dataOut = gaussian_filter(inputData, sigma=sigma, order=0) dataOut[dataOut < 0] = 0 return dataOut
def smooth_gaussian_1D(data=None, sigma=1): # smooth1D """ This function uses Gaussian filter to smooth 1D data """ if data is None or len(data) == 0: return None if sigma < 0: dlgBox(exceptionTitle='Warning', exceptionMsg="Value of sigma is too low. Value was reset to 1", type="Warning") sigma = 1 else: sigma = sigma dataOut = gaussian_filter(data, sigma=sigma, order=0) return dataOut
def rawMassLynx_MS_extract(path, bin_size=10000, rt_start=0, rt_end=99999.0, dt_start=1, dt_end=200, mz_start=0, mz_end=50000, driftscope_path='C:\DriftScope\lib', **kwargs): """ @param bin_size (int): number of points in the m/z dimension @param rt_start (float): start retention time (minutes) @param rt_end (float): end retention time (minutes) @param dt_start (float): start drift time (bins) @param dt_end (float): end drift time (bins) @param mz_start (float): start m/z (Da) @param mz_end (float): end m/z (Da) @param driftscope_path (str): path to DriftScope directory """ # check if data should be extracted to data folder OR temporary folder if kwargs.get("use_temp_folder", True) and os.path.exists(temp_data_folder): out_path = temp_data_folder else: out_path = path # Write range file range_file = os.path.join(out_path, '__.1dMZ.inp') try: fileID = open(range_file, 'w') except IOError as err: dlgBox(exceptionTitle="Error", exceptionMsg=str(err), type="Error") return fileID.write("%s %s %s\n%s %s 1\n%s %s 1" % (mz_start, mz_end, bin_size, rt_start, rt_end, dt_start, dt_end)) fileID.close() # Create command for execution cmd = ''.join([driftscope_path, '\imextract.exe -d "', path, '" -f 1 -o "', out_path, '\output.1dMZ" -t mobilicube -p "', range_file, '"']) # Extract command extractIMS = Popen(cmd, shell=kwargs.get("verbose", True), creationflags=CREATE_NEW_CONSOLE) extractIMS.wait() # return data if kwargs.get("return_data", False): msX, msY = rawMassLynx_MS_load(out_path) return msX, msY else: return None
def rawMassLynx_RT_extract(path=None, rt_start=0, rt_end=99999.0, dt_start=1, dt_end=200, mz_start=0, mz_end=50000, driftscope_path='C:\DriftScope\lib', **kwargs): """ Extract the retention time for specified (or not) mass range """ # check if data should be extracted to data folder OR temporary folder if kwargs.get("use_temp_folder", True) and os.path.exists(temp_data_folder): out_path = temp_data_folder else: out_path = path # Create input file range_file = os.path.join(out_path, '__.1dRT.inp') try: fileID = open(range_file, 'w') except IOError as err: dlgBox(exceptionTitle="Error", exceptionMsg=str(err), type="Error") return fileID.write("{} {} 1\n0.0 9999.0 5000\n{} {} 1".format(mz_start, mz_end, dt_start, dt_end)) fileID.close() # Create command for execution cmd = ''.join([driftscope_path, '\imextract.exe -d "', path, '" -f 1 -o "', out_path, '\output.1dRT" -t mobilicube -p "', range_file, '"']) extractIMS = Popen(cmd, shell=kwargs.get("verbose", True), creationflags=CREATE_NEW_CONSOLE) extractIMS.wait() # return data if kwargs.get("return_data", False): if kwargs.get("normalize", False): rtY, rtYnorm = rawMassLynx_RT_load(out_path, normalize=True) rtX = np.arange(1, len(rtY) + 1) return rtX, rtY, rtYnorm else: rtY = rawMassLynx_RT_load(out_path) rtX = np.arange(1, len(rtY) + 1) return rtX, rtY else: return None
def smooth_savgol_2D(inputData=None, polyOrder=2, windowSize=5): # smoothDataSavGol # Check if input data is there if inputData is None or len(inputData) == 0: return None # Check whether polynomial order is of correct size if (polyOrder <= 0): dlgBox( exceptionTitle='Warning', exceptionMsg="Polynomial order is too small. Value was reset to 2", type="Warning") polyOrder = 2 else: polyOrder = polyOrder # Check whether window size is of correct size if windowSize is None: windowSize = polyOrder + 1 elif (windowSize % 2) and (windowSize > polyOrder): windowSize = windowSize elif windowSize <= polyOrder: dlgBox( exceptionTitle='Warning', exceptionMsg= "Window size was smaller than the polynomial order. Value was reset to %s" % (polyOrder + 1), type="Warning") windowSize = polyOrder + 1 else: print('Window size is even. Adding 1 to make it odd.') windowSize = windowSize + 1 dataOut = savgol_filter(inputData, polyorder=polyOrder, window_length=windowSize, axis=0) dataOut[dataOut < 0] = 0 # Remove any values that are below 0 return dataOut
def rawMassLynx_MS_bin(filename=None, startScan=0, endScan=-1, function=1, mzStart=None, mzEnd=None, binsize=None, binData=False, **kwargs): """ Extract MS data, bin it --- @param binData: boolean, determines if data should be binned or not """ tstart = time.clock() # Create pointer to the file try: filePointer = mlLib.newCMassLynxRawReader(filename) except WindowsError as err: dlgBox(exceptionTitle="Error", exceptionMsg=str(err), type="Error") return # Setup scan reader dataPointer = mlLib.newCMassLynxRawScanReader(filePointer) # Extract number of scans available from the file nScans = mlLib.getScansInFunction(dataPointer, function) # Initilise pointers to data xpoint = c_float() ypoint = c_float() # Initilise empty lists msX = [] msY = [] msDict = {} if endScan == -1 or endScan > nScans: endScan = nScans if 'linearization_mode' in kwargs: if kwargs['linearization_mode'] == 'Raw': binData = False if binsize == 0.: binData = False if binData: if 'auto_range' in kwargs and kwargs['auto_range'] : mzStart = kwargs['mz_min'] mzEnd = kwargs['mz_max'] if mzStart == None or mzEnd == None or binsize == None: print('Missing parameters') return elif kwargs['linearization_mode'] == "Binning": msList = np.arange(mzStart, mzEnd + binsize, binsize) msCentre = msList[:-1] + (binsize / 2) else: msCentre = get_linearization_range(mzStart, mzEnd, binsize, kwargs['linearization_mode']) msRange = np.arange(startScan, endScan) + 1 # First extract data for scan in msRange: nPoints = mlLib.getScanSize(dataPointer, function, scan) # Read XY coordinates mlLib.getXYCoordinates(filePointer, function, scan, byref(xpoint), byref(ypoint)) # Prepare pointers mzP = (c_float * nPoints)() mzI = (c_float * nPoints)() # Read spectrum mlLib.readSpectrum(dataPointer, function, scan, byref(mzP), byref(mzI)) # Extract data from pointer and assign it to list msX = np.ndarray((nPoints,), 'f', mzP, order='C') msY = np.ndarray((nPoints,), 'f', mzI, order='C') if binData: if kwargs['linearization_mode'] == "Binning": msYbin = bin_1D(x=msX, y=msY, bins=msList) else: msCentre, msYbin = linearize(data=np.transpose([msX, msY]), binsize=binsize, mode=kwargs['linearization_mode'], input_list=msCentre) msDict[scan] = [msCentre, msYbin] else: msDict[scan] = [msX, msY] tend = time.clock() print("It took {:.4f} seconds to process {} scans".format((tend - tstart), len(msRange))) # Return data return msDict
def saveFigure2(self, path, **kwargs): """ Saves figures in specified location. Transparency and DPI taken from config file """ # check if plot exists if not hasattr(self, 'plotMS'): print('Cannot save a plot that does not exist') return # Get resize parameter resizeName = kwargs.get('resize', None) resizeSize = None if resizeName is not None: resizeSize = self.config._plotSettings[resizeName]['resize_size'] if not hasattr(self.plotMS, "get_position"): resizeSize = None if resizeSize is not None and not self.lock_plot_from_updating_size: dpi = wx.ScreenDC().GetPPI() # Calculate new size figsizeNarrowPix = (int(resizeSize[0] * dpi[0]), int(resizeSize[1] * dpi[1])) # Set new canvas size and reset the view self.canvas.SetSize(figsizeNarrowPix) self.canvas.draw() # Get old and new plot sizes oldAxesSize = self.plotMS.get_position() newAxesSize = self.config._plotSettings[resizeName]['save_size'] try: self.plotMS.set_position(newAxesSize) except RuntimeError: self.plotMS.set_position(oldAxesSize) self.repaint() # Save figure try: kwargs['bbox_inches'] = "tight" self.figure.savefig(path, **kwargs) except IOError: # reset axes size if resizeSize is not None and not self.lock_plot_from_updating_size: self.plotMS.set_position(oldAxesSize) self.sizeHandler() # warn user dlgBox(exceptionTitle='Warning', exceptionMsg="Cannot save file: %s as it appears to be currently open or the folder doesn't exist" % path, type="Error") # get file extension fname, delimiter_txt = splitext(path) try: bname = basename(fname) except: bname = "" fileType = "Image file (%s)|*%s" % (delimiter_txt, delimiter_txt) dlg = wx.FileDialog(None, "Save as...", "", "", fileType, wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) dlg.SetFilename(bname) if dlg.ShowModal() == wx.ID_OK: fname, __ = splitext(dlg.GetPath()) path = join(fname + delimiter_txt) # reset axes, again if resizeSize is not None and not self.lock_plot_from_updating_size: self.canvas.SetSize(figsizeNarrowPix) self.canvas.draw() try: self.plotMS.set_position(newAxesSize) except RuntimeError: self.plotMS.set_position(oldAxesSize) self.repaint() try: kwargs['bbox_inches'] = "tight" self.figure.savefig(path, **kwargs) except: try: del kwargs['bbox_inches'] self.figure.savefig(path, **kwargs) except: pass # Reset previous view if resizeSize is not None and not self.lock_plot_from_updating_size: self.plotMS.set_position(oldAxesSize) self.sizeHandler()