def runIonSeries(self): """Calculate ion series for current compound.""" self.currentIons = None self.currentIon = None # run task try: # get neutral mass mass = self.currentCompound.mass() self.currentIons = [(0, mass[0], mass[1], 0, '[M]')] # get ions i = 0 while True: # get charge i += 1 charge = i*config.massCalculator['ionseriesPolarity']*abs(config.massCalculator['ionseriesAgentCharge']) # check ion if not self.currentCompound.isvalid(charge=charge, agentFormula=config.massCalculator['ionseriesAgent'], agentCharge=config.massCalculator['ionseriesAgentCharge']): break # get ion type if config.massCalculator['ionseriesPolarity'] == 1 and config.massCalculator['ionseriesAgentCharge'] > 0: iontype = '[M+%d%s] %d+' % (i, config.massCalculator['ionseriesAgent'], abs(charge)) elif config.massCalculator['ionseriesPolarity'] == 1: iontype = '[M-%d%s] %d+' % (i, config.massCalculator['ionseriesAgent'], abs(charge)) elif config.massCalculator['ionseriesAgentCharge'] < 0: iontype = '[M+%d%s] %d-' % (i, config.massCalculator['ionseriesAgent'], abs(charge)) else: iontype = '[M-%d%s] %d-' % (i, config.massCalculator['ionseriesAgent'], abs(charge)) # get mz mz = mspy.mz(mass, charge=charge, agentFormula=config.massCalculator['ionseriesAgent'], agentCharge=config.massCalculator['ionseriesAgentCharge']) # add to list self.currentIons.append((abs(charge), mz[0], mz[1], charge, iontype)) # skip next radical ions # if config.massCalculator['ionseriesAgent'] == 'e': # break # check limits if mz[0] < 100 or len(self.currentIons) >= 100: break # task canceled except mspy.ForceQuit: self.currentIons = None self.currentIon = None
def calcDataPoints(self, peaks, polarity=1): """Calculate requested mass and mass defect.""" buff = [] # init Kendrick formula kendrickFormula = mspy.compound( config.massDefectPlot["kendrickFormula"]) # calculate data points for peak in peaks: mass = peak[0] if not config.massDefectPlot["ignoreCharge"] and peak[1]: mass = mspy.mz(peak[0], 1 * polarity, peak[1], agentFormula="H", agentCharge=1) # calc mass defect md = mspy.md( mass=mass, mdType=config.massDefectPlot["yAxis"], kendrickFormula=kendrickFormula, rounding=config.massDefectPlot["nominalMass"], ) # re-calculate selected mass if config.massDefectPlot["xAxis"] == "mz": mass = peak[0] elif config.massDefectPlot["xAxis"] == "nominal": mass = mspy.nominalmass(peak[0], config.massDefectPlot["nominalMass"]) elif config.massDefectPlot["xAxis"] == "kendrick": mass = mspy.nominalmass(peak[0] * kendrickFormula.nominalmass() / kendrickFormula.mass(0)) else: mass = peak[0] # append point buff.append((mass, md)) return buff
def checkMassLimit(self): """Check maximim neutral mass allowed.""" # 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, ) # check mass limit if mass <= config.massToFormula["massLimit"]: return True return False
def runIonSeries(self): """Calculate ion series for current compound.""" self.currentIons = None self.currentIon = None # run task try: # get neutral mass mass = self.currentCompound.mass() self.currentIons = [(0, mass[0], mass[1], 0, '[M]')] # get ions i = 0 while True: # get charge i += 1 charge = i * config.massCalculator['ionseriesPolarity'] * abs( config.massCalculator['ionseriesAgentCharge']) # check ion if not self.currentCompound.isvalid( charge=charge, agentFormula=config.massCalculator['ionseriesAgent'], agentCharge=config. massCalculator['ionseriesAgentCharge']): break # get ion type if config.massCalculator[ 'ionseriesPolarity'] == 1 and config.massCalculator[ 'ionseriesAgentCharge'] > 0: iontype = '[M+%d%s] %d+' % ( i, config.massCalculator['ionseriesAgent'], abs(charge)) elif config.massCalculator['ionseriesPolarity'] == 1: iontype = '[M-%d%s] %d+' % ( i, config.massCalculator['ionseriesAgent'], abs(charge)) elif config.massCalculator['ionseriesAgentCharge'] < 0: iontype = '[M+%d%s] %d-' % ( i, config.massCalculator['ionseriesAgent'], abs(charge)) else: iontype = '[M-%d%s] %d-' % ( i, config.massCalculator['ionseriesAgent'], abs(charge)) # get mz mz = mspy.mz( mass, charge=charge, agentFormula=config.massCalculator['ionseriesAgent'], agentCharge=config.massCalculator['ionseriesAgentCharge']) # add to list self.currentIons.append( (abs(charge), mz[0], mz[1], charge, iontype)) # skip next radical ions # if config.massCalculator['ionseriesAgent'] == 'e': # break # check limits if mz[0] < 100 or len(self.currentIons) >= 100: break # task canceled except mspy.ForceQuit: self.currentIons = None self.currentIon = None
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