Ejemplo n.º 1
0
def mz(mass,
       charge,
       currentCharge=0,
       agentFormula='H',
       agentCharge=1,
       massType='Mo'):
    """Calculate m/z value for given mass and charge.
        mass: (tuple of (Mo,Av) or float)
        charge: (int) desired charge of ion
        currentCharge: (int) if mass is charged already
        agentFormula: (str) charging agent formula
        agentCharge: (int) charging agent charge
        massType: ('Mo' or 'Av') used mass type if mass value is float
    """

    # set mass type
    if massType == 'Mo':
        massType = 0
    else:
        massType = 1

    # get agent mass
    agentFormula = objects.formula(agentFormula)
    agentMass = agentFormula.getMass()
    agentMass = (agentMass[0] - agentCharge * 0.000549,
                 agentMass[1] - agentCharge * 0.000549)

    # recalculate zero charge
    agentCount = currentCharge / agentCharge
    if currentCharge != 0:
        if type(mass) in (tuple, list):
            massMo = mass[0] * abs(currentCharge) - agentMass[0] * agentCount
            massAv = mass[1] * abs(currentCharge) - agentMass[1] * agentCount
            mass = (massMo, massAv)
        else:
            mass = mass * abs(currentCharge) - agentMass[massType] * agentCount
    if charge == 0:
        return mass

    # calculate final charge
    agentCount = charge / agentCharge
    if type(mass) in (tuple, list):
        massMo = (mass[0] + agentMass[0] * agentCount) / abs(charge)
        massAv = (mass[1] + agentMass[1] * agentCount) / abs(charge)
        return (massMo, massAv)
    else:
        return (mass + agentMass[massType] * agentCount) / abs(charge)
Ejemplo n.º 2
0
def mz(mass, charge, currentCharge=0, agentFormula='H', agentCharge=1, massType='Mo'):
    """Calculate m/z value for given mass and charge.
        mass: (tuple of (Mo,Av) or float)
        charge: (int) desired charge of ion
        currentCharge: (int) if mass is charged already
        agentFormula: (str) charging agent formula
        agentCharge: (int) charging agent charge
        massType: ('Mo' or 'Av') used mass type if mass value is float
    """
    
    # set mass type
    if massType == 'Mo':
        massType = 0
    else:
        massType = 1
    
    # get agent mass
    agentFormula = objects.formula(agentFormula)
    agentMass = agentFormula.getMass()
    agentMass = (agentMass[0]-agentCharge*0.000549, agentMass[1]-agentCharge*0.000549)
    
    # recalculate zero charge
    agentCount = currentCharge/agentCharge
    if currentCharge != 0:
        if type(mass) in (tuple, list):
            massMo = mass[0]*abs(currentCharge) - agentMass[0]*agentCount
            massAv = mass[1]*abs(currentCharge) - agentMass[1]*agentCount
            mass = (massMo, massAv)
        else:
            mass = mass*abs(currentCharge) - agentMass[massType]*agentCount
    if charge == 0:
        return mass
    
    # calculate final charge
    agentCount = charge/agentCharge
    if type(mass) in (tuple, list):
        massMo = (mass[0] + agentMass[0]*agentCount)/abs(charge)
        massAv = (mass[1] + agentMass[1]*agentCount)/abs(charge)
        return (massMo, massAv)
    else:
        return (mass + agentMass[massType]*agentCount)/abs(charge)
Ejemplo n.º 3
0
    def initMasses(self):
        """Initialize masses."""

        self.mass = objects.formula(self.formula).getMass()
Ejemplo n.º 4
0
    def initMasses(self):
        """Initialize masses."""

        self.nTermMass = objects.formula(self.nTermFormula).getMass()
        self.cTermMass = objects.formula(self.cTermFormula).getMass()
        self.lossMass = objects.formula(self.lossFormula).getMass()
Ejemplo n.º 5
0
    def initMasses(self):
        """Initialize masses."""

        gain = objects.formula(self.gainFormula).getMass()
        loss = objects.formula(self.lossFormula).getMass()
        self.mass = (gain[0]-loss[0], gain[1]-loss[1])
Ejemplo n.º 6
0
def isoPattern(compound, fwhm=0.1, relIntTreshold=0.01, charge=1, agentFormula='H', agentCharge=1, rounding=7):
    """Calculate isotopic pattern for given compound (formula or sequence object).
    Set appropriate relative intensity treshold and resolution in fwhm.
    To charge isotopes set charge value. Charge agent can be changed from H
    to any formula."""
    
    finalPattern = []
    
    # set internal treshold
    internalTreshold = relIntTreshold/100.
    
    # add charging agent to formula and get composition
    composition = compound.getComposition()
    if charge:
        formula = compound.getFormula()
        formula += '%s%d' % (agentFormula, (charge/agentCharge))
        composition = objects.formula(formula).getComposition()
    
    # check composition
    for atom in composition:
        if composition[atom] < 0:
            return False
    
    # calculate pattern
    for atom in composition:
        atomCount = composition[atom]
        
        # get isotopic profile for current atom or specified isotope only
        atomPattern = []
        match = objects.elementPattern.match(atom)
        symbol, massNumber, tmp = match.groups()
        if massNumber:
            isotope = blocks.elements[atom].isotopes[massNumber]
            atomPattern.append([isotope[0], 1.])
        else:
            for massNumber, isotope in blocks.elements[atom].isotopes.items():
                if isotope[1] > 0.:
                    atomPattern.append(list(isotope))
        
        # add atoms
        for i in range(atomCount):
            currentPattern = {}
            
            # if pattern in empty (first atom) add current atom pattern
            if not finalPattern:
                finalPattern = atomPattern
                finalPattern.sort()
                continue
            
            # add atom to each peak of final pattern
            for peak in finalPattern:
                
                # skip peak under relevant abundance treshold
                if peak[1] < internalTreshold:
                    continue
                
                # add each isotope of current atom to peak
                for isotope in atomPattern:
                    mass = peak[0] + isotope[0]
                    abundance = peak[1] * isotope[1]
                    
                    # add abundance to stored peak or add new peak
                    mass = round(mass, rounding)
                    if mass in currentPattern:
                        currentPattern[mass] += abundance
                    else:
                        currentPattern[mass] = abundance
            
            # replace final pattern by current
            finalPattern = []
            for mass, abundance in currentPattern.items():
                finalPattern.append([mass, abundance])
            finalPattern.sort()
            
            # noramlize pattern
            finalPattern = _normalize(finalPattern)
    
    # check pattern
    if not finalPattern:
        return None
    
    # correct charge
    if charge:
        for i in range(len(finalPattern)):
            finalPattern[i][0] = (finalPattern[i][0]-0.000549*charge)/abs(charge)
    
    # collect isotopes according to resolution (FWHM)
    tol = fwhm/2
    if charge:
        tol /= abs(charge)
    collectedPeaks = [finalPattern[0]]
    lastPeak = finalPattern[0]
    for currentPeak in finalPattern[1:]:
        if (lastPeak[0] + tol) > currentPeak[0]:
            abundance = lastPeak[1] + currentPeak[1]
            mass = (lastPeak[0]*lastPeak[1] + currentPeak[0]*currentPeak[1]) / abundance
            collectedPeaks[-1] = [mass, abundance]
            lastPeak = [mass, abundance]
        else:
            collectedPeaks.append(currentPeak)
            lastPeak = currentPeak
    finalPattern = _normalize(collectedPeaks)
    
    # discard peaks below treshold
    filteredPeaks = []
    for peak in finalPattern:
        if peak[1] >= relIntTreshold:
            filteredPeaks.append(peak)
    finalPattern = filteredPeaks
    
    return finalPattern
Ejemplo n.º 7
0
    def initMasses(self):
        """Initialize masses."""

        self.mass = objects.formula(self.formula).getMass()
Ejemplo n.º 8
0
    def initMasses(self):
        """Initialize masses."""

        self.nTermMass = objects.formula(self.nTermFormula).getMass()
        self.cTermMass = objects.formula(self.cTermFormula).getMass()
        self.lossMass = objects.formula(self.lossFormula).getMass()
Ejemplo n.º 9
0
    def initMasses(self):
        """Initialize masses."""

        gain = objects.formula(self.gainFormula).getMass()
        loss = objects.formula(self.lossFormula).getMass()
        self.mass = (gain[0] - loss[0], gain[1] - loss[1])
Ejemplo n.º 10
0
def isoPattern(compound,
               fwhm=0.1,
               relIntTreshold=0.01,
               charge=1,
               agentFormula='H',
               agentCharge=1,
               rounding=7):
    """Calculate isotopic pattern for given compound (formula or sequence object).
    Set appropriate relative intensity treshold and resolution in fwhm.
    To charge isotopes set charge value. Charge agent can be changed from H
    to any formula."""

    finalPattern = []

    # set internal treshold
    internalTreshold = relIntTreshold / 100.

    # add charging agent to formula and get composition
    composition = compound.getComposition()
    if charge:
        formula = compound.getFormula()
        formula += '%s%d' % (agentFormula, (charge / agentCharge))
        composition = objects.formula(formula).getComposition()

    # check composition
    for atom in composition:
        if composition[atom] < 0:
            return False

    # calculate pattern
    for atom in composition:
        atomCount = composition[atom]

        # get isotopic profile for current atom or specified isotope only
        atomPattern = []
        match = objects.elementPattern.match(atom)
        symbol, massNumber, tmp = match.groups()
        if massNumber:
            isotope = blocks.elements[atom].isotopes[massNumber]
            atomPattern.append([isotope[0], 1.])
        else:
            for massNumber, isotope in blocks.elements[atom].isotopes.items():
                if isotope[1] > 0.:
                    atomPattern.append(list(isotope))

        # add atoms
        for i in range(atomCount):
            currentPattern = {}

            # if pattern in empty (first atom) add current atom pattern
            if not finalPattern:
                finalPattern = atomPattern
                finalPattern.sort()
                continue

            # add atom to each peak of final pattern
            for peak in finalPattern:

                # skip peak under relevant abundance treshold
                if peak[1] < internalTreshold:
                    continue

                # add each isotope of current atom to peak
                for isotope in atomPattern:
                    mass = peak[0] + isotope[0]
                    abundance = peak[1] * isotope[1]

                    # add abundance to stored peak or add new peak
                    mass = round(mass, rounding)
                    if mass in currentPattern:
                        currentPattern[mass] += abundance
                    else:
                        currentPattern[mass] = abundance

            # replace final pattern by current
            finalPattern = []
            for mass, abundance in currentPattern.items():
                finalPattern.append([mass, abundance])
            finalPattern.sort()

            # noramlize pattern
            finalPattern = _normalize(finalPattern)

    # check pattern
    if not finalPattern:
        return None

    # correct charge
    if charge:
        for i in range(len(finalPattern)):
            finalPattern[i][0] = (finalPattern[i][0] -
                                  0.000549 * charge) / abs(charge)

    # collect isotopes according to resolution (FWHM)
    tol = fwhm / 2
    if charge:
        tol /= abs(charge)
    collectedPeaks = [finalPattern[0]]
    lastPeak = finalPattern[0]
    for currentPeak in finalPattern[1:]:
        if (lastPeak[0] + tol) > currentPeak[0]:
            abundance = lastPeak[1] + currentPeak[1]
            mass = (lastPeak[0] * lastPeak[1] +
                    currentPeak[0] * currentPeak[1]) / abundance
            collectedPeaks[-1] = [mass, abundance]
            lastPeak = [mass, abundance]
        else:
            collectedPeaks.append(currentPeak)
            lastPeak = currentPeak
    finalPattern = _normalize(collectedPeaks)

    # discard peaks below treshold
    filteredPeaks = []
    for peak in finalPattern:
        if peak[1] >= relIntTreshold:
            filteredPeaks.append(peak)
    finalPattern = filteredPeaks

    return finalPattern