def compareSelected(self, pkIndex): """Compare selected mass only.""" self.currentMatches = [] # get current peak p1 = self.currentPeaklist[pkIndex] # compare mass for p2 in self.currentPeaklist: # check charge if not config.comparePeaklists['ignoreCharge'] and (p1[2] != p2[2]) and (p1[2] != None and p2[2] != None): continue # check error error = mspy.delta(p1[0], p2[0], config.comparePeaklists['units']) if abs(error) <= config.comparePeaklists['tolerance']: ratio1 = p1[3]/p2[3] ratio2 = 1/ratio1 self.currentMatches.append([p2[1], p2[0], error, ratio1, ratio2, p1[1]==p2[1]]) elif error < 0: break # sort matches by document self.currentMatches.sort()
def calcCalibration(self): """Get calibration based on currently assigned masses.""" # clear last calibration self.currentCalibration = None # disable buttons self.apply_butt.SetLabel("Apply") self.apply_butt.Enable(False) # get calibration points points = [] for item in self.currentReferences: item[3] = None item[5] = None if item[6] and item[2] != None: points.append([item[2], item[1]]) # get calibration model = config.calibration["fitting"] if (model == "linear" and len(points) >= 1) or (model == "quadratic" and len(points) >= 3): self.currentCalibration = mspy.calibration(points, model) model = self.currentCalibration[0] params = self.currentCalibration[1] # recalculate assigned peaks for x, item in enumerate(self.currentReferences): if item[2] != None: calibrated = model(params, item[2]) self.currentReferences[x][3] = calibrated self.currentReferences[x][5] = mspy.delta(calibrated, item[1], config.calibration["units"]) # enable buttons self.apply_butt.Enable(True)
def statisticalCalibration(self): """Do statistical calibration.""" # get peaklist peaklist = self.currentDocument.spectrum.peaklist if not peaklist: wx.Bell() return # get reference masses self.currentReferences = [] for peak in peaklist: # check statCutOff if peak.mz < config.calibration["statCutOff"]: continue # get theoretical mass theoretical = math.modf(peak.mz)[1] + math.modf(round(peak.mz) * 1.00048)[0] if (peak.mz - theoretical) > 0.5: theoretical += 1 elif (peak.mz - theoretical) < -0.5: theoretical -= 1 title = "Peak %.2f" % (peak.mz) delta = mspy.delta(peak.mz, theoretical, config.calibration["units"]) self.currentReferences.append([title, theoretical, peak.mz, None, delta, None, True]) # get calibration self.calcCalibration()
def statisticalCalibration(self): """Do statistical calibration.""" # get peaklist peaklist = self.currentDocument.spectrum.peaklist if not peaklist: wx.Bell() return # get reference masses self.currentReferences = [] for peak in peaklist: # check statCutOff if peak.mz < config.calibration['statCutOff']: continue # get theoretical mass theoretical = math.modf(peak.mz)[1] + math.modf( round(peak.mz) * 1.00048)[0] if (peak.mz - theoretical) > 0.5: theoretical += 1 elif (peak.mz - theoretical) < -0.5: theoretical -= 1 title = 'Peak %.2f' % (peak.mz) delta = mspy.delta(peak.mz, theoretical, config.calibration['units']) self.currentReferences.append( [title, theoretical, peak.mz, None, delta, None, True]) # get calibration self.calcCalibration()
def compareSelected(self, pkIndex): """Compare selected mass only.""" self.currentMatches = [] # get current peak p1 = self.currentPeaklist[pkIndex] # compare mass for p2 in self.currentPeaklist: # check charge if (not config.comparePeaklists["ignoreCharge"] and (p1[2] != p2[2]) and (p1[2] is not None and p2[2] is not None)): continue # check error error = mspy.delta(p1[0], p2[0], config.comparePeaklists["units"]) if abs(error) <= config.comparePeaklists["tolerance"]: ratio1 = p1[3] / p2[3] ratio2 = 1 / ratio1 self.currentMatches.append( [p2[1], p2[0], error, ratio1, ratio2, p1[1] == p2[1]]) elif error < 0: break # sort matches by document self.currentMatches.sort()
def runCompare(self): """Compare all peaklists.""" self.currentMatches = [] # run task try: # erase previous matches count = len(self.currentDocuments) for i, item in enumerate(self.currentPeaklist): item[-1] = count * [False] item[-1][item[1]] = True # compare peaklists count = len(self.currentPeaklist) for i in range(count): for j in range(i, count): p1 = self.currentPeaklist[i] p2 = self.currentPeaklist[j] matched = False # check charge if not config.comparePeaklists['ignoreCharge'] and ( p1[2] != p2[2]) and (p1[2] != None and p2[2] != None): continue # check error error = mspy.delta(p1[0], p2[0], config.comparePeaklists['units']) if abs(error) <= config.comparePeaklists['tolerance']: matched = True elif error < 0: break # check ratio if matched and config.comparePeaklists[ 'ratioCheck'] and p1[3] and p2[3]: ratio = p1[3] / p2[3] if config.comparePeaklists['ratioThreshold'] > 1 and ratio < 1 \ or config.comparePeaklists['ratioThreshold'] < 1 and ratio > 1: ratio = 1. / ratio if (config.comparePeaklists['ratioDirection'] == 1 and ratio < config.comparePeaklists['ratioThreshold']) \ or (config.comparePeaklists['ratioDirection'] == -1 and ratio > config.comparePeaklists['ratioThreshold']): matched = False # save matched if matched: p1[-1][p2[1]] = True p2[-1][p1[1]] = True # task canceled except mspy.ForceQuit: return
def onUnitsChanged(self, evt): """Set current units and update data.""" # get units config.calibration["units"] = "Da" if self.unitsPpm_radio.GetValue(): config.calibration["units"] = "ppm" # update tolerance units label self.toleranceUnits_label.SetLabel(config.calibration["units"]) # recalculate errors if self.currentReferences: for x, item in enumerate(self.currentReferences): if item[2] != None and item[3] != None: self.currentReferences[x][4] = mspy.delta(item[2], item[1], config.calibration["units"]) self.currentReferences[x][5] = mspy.delta(item[3], item[1], config.calibration["units"]) # update GUI self.updateReferencesList() self.updateErrorPlot()
def runCompare(self): """Compare all peaklists.""" self.currentMatches = [] # run task try: # erase previous matches count = len(self.currentDocuments) for i, item in enumerate(self.currentPeaklist): item[-1] = count*[False] item[-1][item[1]] = True # compare peaklists count = len(self.currentPeaklist) for i in range(count): for j in range(i, count): p1 = self.currentPeaklist[i] p2 = self.currentPeaklist[j] matched = False # check charge if not config.comparePeaklists['ignoreCharge'] and (p1[2] != p2[2]) and (p1[2] != None and p2[2] != None): continue # check error error = mspy.delta(p1[0], p2[0], config.comparePeaklists['units']) if abs(error) <= config.comparePeaklists['tolerance']: matched = True elif error < 0: break # check ratio if matched and config.comparePeaklists['ratioCheck'] and p1[3] and p2[3]: ratio = p1[3]/p2[3] if config.comparePeaklists['ratioThreshold'] > 1 and ratio < 1 \ or config.comparePeaklists['ratioThreshold'] < 1 and ratio > 1: ratio = 1./ratio if (config.comparePeaklists['ratioDirection'] == 1 and ratio < config.comparePeaklists['ratioThreshold']) \ or (config.comparePeaklists['ratioDirection'] == -1 and ratio > config.comparePeaklists['ratioThreshold']): matched = False # save matched if matched: p1[-1][p2[1]] = True p2[-1][p1[1]] = True # task canceled except mspy.ForceQuit: return
def onUnitsChanged(self, evt): """Set current units and update data.""" # get units config.calibration['units'] = 'Da' if self.unitsPpm_radio.GetValue(): config.calibration['units'] = 'ppm' # update tolerance units label self.toleranceUnits_label.SetLabel(config.calibration['units']) # recalculate errors if self.currentReferences: for x, item in enumerate(self.currentReferences): if item[2] != None and item[3] != None: self.currentReferences[x][4] = mspy.delta( item[2], item[1], config.calibration['units']) self.currentReferences[x][5] = mspy.delta( item[3], item[1], config.calibration['units']) # update GUI self.updateReferencesList() self.updateErrorPlot()
def onUnitsChanged(self, evt): """Set current units and update data.""" # get units config.massToFormula['units'] = 'ppm' if self.unitsDa_radio.GetValue(): config.massToFormula['units'] = 'Da' # recalculate errors if self.currentFormulae: for x, item in enumerate(self.currentFormulae): self.currentFormulae[x][3] = mspy.delta(self.currentMass, item[2], config.massToFormula['units']) # update GUI self.updateFormulaeList()
def setData(self, document, references=None): """Set current document.""" # set new document self.currentDocument = document # disable buttons self.apply_butt.SetLabel('Apply') self.apply_butt.Enable(False) # clear references if self.statCalibration_check.GetValue(): self.currentReferences = None elif self.currentReferences: for x in range(len(self.currentReferences)): self.currentReferences[x][2:] = [None, None, None, None, True] # internal calibration on given references if references: # set GUI self.onToolSelected(tool='references') self.statCalibration_check.SetValue(False) self.references_choice.Enable(True) self.tolerance_value.Enable(True) self.references_choice.Select(0) # set references self.currentReferences = [] for ref in references: delta = mspy.delta(ref[2], ref[1], config.calibration['units']) self.currentReferences.append( [ref[0], ref[1], ref[2], None, delta, None, True]) # get calibration self.calcCalibration() # enable clipboard elif self.currentCalibration and document != None: self.apply_butt.SetLabel('Apply Recent') self.apply_butt.Enable(True) # update GUI self.updateReferencesList(scroll=True) self.updateErrorPlot()
def setData(self, document, references=None): """Set current document.""" # set new document self.currentDocument = document # disable buttons self.apply_butt.SetLabel("Apply") self.apply_butt.Enable(False) # clear references if self.statCalibration_check.GetValue(): self.currentReferences = None elif self.currentReferences: for x in range(len(self.currentReferences)): self.currentReferences[x][2:] = [None, None, None, None, True] # internal calibration on given references if references: # set GUI self.onToolSelected(tool="references") self.statCalibration_check.SetValue(False) self.references_choice.Enable(True) self.tolerance_value.Enable(True) self.references_choice.Select(0) # set references self.currentReferences = [] for ref in references: delta = mspy.delta(ref[2], ref[1], config.calibration["units"]) self.currentReferences.append([ref[0], ref[1], ref[2], None, delta, None, True]) # get calibration self.calcCalibration() # enable clipboard elif self.currentCalibration and document != None: self.apply_butt.SetLabel("Apply Recent") self.apply_butt.Enable(True) # update GUI self.updateReferencesList(scroll=True) self.updateErrorPlot()
def makeCalibrationCurve(self): """Make calibration curve to show in plot.""" # get range minX = self.currentDocument.spectrum.peaklist[0].mz maxX = self.currentDocument.spectrum.peaklist[-1].mz # init params step = (maxX - minX) / 100. fn = self.currentCalibration[0] params = self.currentCalibration[1] # make points in Da points = [] for x in range(100): mz = minX + step * x error = mspy.delta(mz, fn(params, mz), config.calibration['units']) points.append((mz, error)) return points
def makeCalibrationCurve(self): """Make calibration curve to show in plot.""" # get range minX = self.currentDocument.spectrum.peaklist[0].mz maxX = self.currentDocument.spectrum.peaklist[-1].mz # init params step = (maxX - minX) / 100.0 fn = self.currentCalibration[0] params = self.currentCalibration[1] # make points in Da points = [] for x in range(100): mz = minX + step * x error = mspy.delta(mz, fn(params, mz), config.calibration["units"]) points.append((mz, error)) return points
def internalCalibration(self): """Assign reference masses to peaks.""" # check references if not self.currentReferences: wx.Bell() return else: for x in range(len(self.currentReferences)): self.currentReferences[x][2:7] = (None, None, None, None, True) # get peaklist peaklist = self.currentDocument.spectrum.peaklist if not peaklist: wx.Bell() return # get tolerance try: config.calibration['tolerance'] = float( self.tolerance_value.GetValue()) except: wx.Bell() return # find peaks within tolerance for x, item in enumerate(self.currentReferences): for peak in peaklist: delta = mspy.delta(peak.mz, item[1], config.calibration['units']) if abs(delta) <= config.calibration['tolerance']: if self.currentReferences[x][2] == None or abs( delta) < abs(self.currentReferences[x][4]): self.currentReferences[x][2] = peak.mz self.currentReferences[x][4] = delta if delta > config.calibration['tolerance']: break # get calibration self.calcCalibration()
def internalCalibration(self): """Assign reference masses to peaks.""" # check references if not self.currentReferences: wx.Bell() return else: for x in range(len(self.currentReferences)): self.currentReferences[x][2:7] = (None, None, None, None, True) # get peaklist peaklist = self.currentDocument.spectrum.peaklist if not peaklist: wx.Bell() return # get tolerance try: config.calibration["tolerance"] = float(self.tolerance_value.GetValue()) except: wx.Bell() return # find peaks within tolerance for x, item in enumerate(self.currentReferences): for peak in peaklist: delta = mspy.delta(peak.mz, item[1], config.calibration["units"]) if abs(delta) <= config.calibration["tolerance"]: if self.currentReferences[x][2] == None or abs(delta) < abs(self.currentReferences[x][4]): self.currentReferences[x][2] = peak.mz self.currentReferences[x][4] = delta if delta > config.calibration["tolerance"]: break # get calibration self.calcCalibration()
def calcCalibration(self): """Get calibration based on currently assigned masses.""" # clear last calibration self.currentCalibration = None # disable buttons self.apply_butt.SetLabel('Apply') self.apply_butt.Enable(False) # get calibration points points = [] for item in self.currentReferences: item[3] = None item[5] = None if item[6] and item[2] != None: points.append([item[2], item[1]]) # get calibration model = config.calibration['fitting'] if (model == 'linear' and len(points) >= 1) or (model == 'quadratic' and len(points) >= 3): self.currentCalibration = mspy.calibration(points, model) model = self.currentCalibration[0] params = self.currentCalibration[1] # recalculate assigned peaks for x, item in enumerate(self.currentReferences): if item[2] != None: calibrated = model(params, item[2]) self.currentReferences[x][3] = calibrated self.currentReferences[x][5] = mspy.delta( calibrated, item[1], config.calibration['units']) # enable buttons self.apply_butt.Enable(True)
def runMatch(self): """Match data to peaklist.""" # run task try: # set columns if self.currentModule == 'massfilter': massCol = 1 chargeCol = None errorCol = 2 matchObject = doc.annotation elif self.currentModule == 'digest': massCol = 2 chargeCol = 3 errorCol = 5 matchObject = doc.match elif self.currentModule == 'fragment': massCol = 2 chargeCol = 3 errorCol = 5 matchObject = doc.match elif self.currentModule == 'compounds': massCol = 1 chargeCol = 2 errorCol = 5 matchObject = doc.annotation # clear previous match for item in self.currentData: item[errorCol] = None item[-1] = [] # match data self.currentErrors = [] self.currentCalibrationPoints = [] digits = '%0.' + `config.main['mzDigits']` + 'f' for pIndex, peak in enumerate(self.currentPeaklist): for x, item in enumerate(self.currentData): mspy.CHECK_FORCE_QUIT() # check charge if (chargeCol==None or peak.charge==None or config.match['ignoreCharge'] or peak.charge==item[chargeCol]): # check mass tolerance error = mspy.delta(peak.mz, item[massCol], config.match['units']) if abs(error) <= config.match['tolerance']: # create new match object match = matchObject(label='', mz=peak.mz, ai=peak.ai, base=peak.base, theoretical=item[massCol]) match.peakIndex = pIndex self.currentData[x][-1].append(match) # errors and calibration points label = 'Peak ' + digits % peak.mz self.currentErrors.append([peak.mz, error]) self.currentCalibrationPoints.append([label, item[massCol], peak.mz]) # show best error only for item in self.currentData: for match in item[-1]: error = match.delta(config.match['units']) if item[errorCol] == None or abs(item[errorCol]) > abs(error): item[errorCol] = error # get match summary self.makeMatchSummary() # task canceled except mspy.ForceQuit: self.currentErrors = [] self.currentCalibrationPoints = [] self.currentSummary = [] for item in self.currentData: item[errorCol] = None item[-1] = [] return
def runMatch(self): """Match data to peaklist.""" # run task try: # set columns if self.currentModule == 'massfilter': massCol = 1 chargeCol = None errorCol = 2 matchObject = doc.annotation elif self.currentModule == 'digest': massCol = 2 chargeCol = 3 errorCol = 5 matchObject = doc.match elif self.currentModule == 'fragment': massCol = 2 chargeCol = 3 errorCol = 5 matchObject = doc.match elif self.currentModule == 'compounds': massCol = 1 chargeCol = 2 errorCol = 5 matchObject = doc.annotation # clear previous match for item in self.currentData: item[errorCol] = None item[-1] = [] # match data self.currentErrors = [] self.currentCalibrationPoints = [] digits = '%0.' + ` config.main['mzDigits'] ` + 'f' for pIndex, peak in enumerate(self.currentPeaklist): for x, item in enumerate(self.currentData): mspy.CHECK_FORCE_QUIT() # check charge if (chargeCol == None or peak.charge == None or config.match['ignoreCharge'] or peak.charge == item[chargeCol]): # check mass tolerance error = mspy.delta(peak.mz, item[massCol], config.match['units']) if abs(error) <= config.match['tolerance']: # create new match object match = matchObject(label='', mz=peak.mz, ai=peak.ai, base=peak.base, theoretical=item[massCol]) match.peakIndex = pIndex self.currentData[x][-1].append(match) # errors and calibration points label = 'Peak ' + digits % peak.mz self.currentErrors.append([peak.mz, error]) self.currentCalibrationPoints.append( [label, item[massCol], peak.mz]) # show best error only for item in self.currentData: for match in item[-1]: error = match.delta(config.match['units']) if item[errorCol] == None or abs( item[errorCol]) > abs(error): item[errorCol] = error # get match summary self.makeMatchSummary() # task canceled except mspy.ForceQuit: self.currentErrors = [] self.currentCalibrationPoints = [] self.currentSummary = [] for item in self.currentData: item[errorCol] = None item[-1] = [] return
def runGenerator(self): """Generate formula for given mass.""" # run task try: self.currentFormulae = [] # get agent charge agentCharge = 1 if config.massToFormula["ionization"] == "e": agentCharge = -1 # approximate CHNO composition from neutral mass mass = mspy.mz( mass=self.currentMass, charge=0, currentCharge=config.massToFormula["charge"], agentFormula=config.massToFormula["ionization"], agentCharge=agentCharge, ) composition = {} if config.massToFormula["autoCHNO"]: if mass < 500: composition = { "C": [0, 40], "H": [0, 80], "N": [0, 20], "O": [0, 20], } elif mass < 1000: composition = { "C": [0, 80], "H": [0, 130], "N": [0, 30], "O": [0, 30], } elif mass < 2000: composition = { "C": [0, 160], "H": [0, 250], "N": [0, 40], "O": [0, 70], } else: composition = { "C": [0, 180], "H": [0, 300], "N": [0, 60], "O": [0, 90], } # add user-specified compositions minComposition = mspy.compound( config.massToFormula["formulaMin"]).composition() for el in minComposition: if el in composition: composition[el][0] = minComposition[el] else: composition[el] = [minComposition[el], minComposition[el]] maxComposition = mspy.compound( config.massToFormula["formulaMax"]).composition() for el in maxComposition: if el in composition: composition[el][1] = maxComposition[el] else: composition[el] = [0, maxComposition[el]] # calculate formulae formulae = mspy.formulator( mz=self.currentMass, charge=config.massToFormula["charge"], tolerance=config.massToFormula["tolerance"], units=config.massToFormula["units"], composition=composition, agentFormula=config.massToFormula["ionization"], agentCharge=agentCharge, limit=config.massToFormula["countLimit"], ) # make compounds buff = [] for formula in formulae: # make compound cmpd = mspy.compound(formula) mass = cmpd.mass(0) mz = cmpd.mz( config.massToFormula["charge"], config.massToFormula["ionization"], 1, )[0] error = mspy.delta(self.currentMass, mz, config.massToFormula["units"]) errorDa = mspy.delta(self.currentMass, mz, "Da") # compare isotopic pattern similarity = None if config.massToFormula["checkPattern"] and cmpd.isvalid( charge=config.massToFormula["charge"], agentFormula=config.massToFormula["ionization"], ): similarity = self.compareIsotopicPattern( cmpd, config.massToFormula["charge"], config.massToFormula["ionization"], errorDa, ) # count ratios countC = float(cmpd.count("C", groupIsotopes=True)) countH = float(cmpd.count("H", groupIsotopes=True)) hc = None if countC: hc = countH / countC # count rdbe rdbe = cmpd.rdbe() # add item buff.append([ cmpd.formula(), mass, mz, error, hc, rdbe, similarity, cmpd ]) self.currentFormulae = buff # task canceled except mspy.ForceQuit: return