1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
# -------------------------------------------------------------------------
# Copyright (C) 2005-2012 Martin Strohalm <www.mmass.org>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# Complete text of GNU GPL can be found in the file LICENSE.TXT in the
# main directory of the program.
# -------------------------------------------------------------------------
# load libs
import re
# load stopper
from mod_stopper import CHECK_FORCE_QUIT
# load blocks
import blocks
# load objects
import obj_compound
# BASIC CONSTANTS
# ---------------
ELECTRON_MASS = 0.00054857990924
FORMULA_PATTERN = re.compile(r'''
^(
([\(])* # start parenthesis
(
([A-Z][a-z]{0,2}) # atom symbol
(\{[\d]+\})? # isotope
(([\-][\d]+)|[\d]*) # atom count
)+
([\)][\d]*)* # end parenthesis
)*$
''', re.X)
ELEMENT_PATTERN = re.compile(r'''
([A-Z][a-z]{0,2}) # atom symbol
(?:\{([\d]+)\})? # isotope
([\-]?[\d]*) # atom count
''', re.X)
# BASIC FUNCTIONS
# ---------------
def delta(measuredMass, countedMass, units='ppm'):
"""Calculate error between measured Mass and counted Mass in specified units.
measuredMass (float) - measured mass
countedMass (float) - counted mass
units (Da, ppm or %) - error units
"""
if units == 'ppm':
return (measuredMass - countedMass) / countedMass*1000000
elif units == 'Da':
return (measuredMass - countedMass)
elif units == '%':
return (measuredMass - countedMass) / countedMass*100
else:
raise ValueError, 'Unknown units for delta! -->' + units
# ----
def mz(mass, charge, currentCharge=0, agentFormula='H', agentCharge=1, massType=0):
"""Calculate m/z value for given mass and charge.
mass (tuple of (Mo, Av) or float) - current mass
charge (int) - final charge of ion
currentCharge (int) - current mass charge
agentFormula (str or mspy.compound) - charging agent formula
agentCharge (int) - charging agent unit charge
massType (0 or 1) - used mass type if mass value is float, 0 = monoisotopic, 1 = average
"""
# check agent formula
if agentFormula != 'e' and not isinstance(agentFormula, obj_compound.compound):
agentFormula = obj_compound.compound(agentFormula)
# get agent mass
if agentFormula == 'e':
agentMass = [ELECTRON_MASS, ELECTRON_MASS]
else:
agentMass = agentFormula.mass()
agentMass = (agentMass[0]-agentCharge*ELECTRON_MASS, agentMass[1]-agentCharge*ELECTRON_MASS)
# 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)
# ----
# FORMULA FUNCTIONS
# -----------------
def rdbe(compound):
"""Get RDBE (Range or Double Bonds Equivalents) of a given compound.
compound (str or mspy.compound) - compound
"""
# check compound
if not isinstance(compound, obj_compound.compound):
compound = obj_compound.compound(compound)
# get composition
comp = compound.composition()
# get atoms from composition
atoms = []
for item in comp:
match = ELEMENT_PATTERN.match(item)
if match and not match.group(1) in atoms:
atoms.append(match.group(1))
# get rdbe
rdbeValue = 0.
for a in atoms:
valence = blocks.elements[a].valence
if valence:
rdbeValue += (valence - 2) * compound.count(a, groupIsotopes=True)
rdbeValue /= 2.
rdbeValue += 1.
return rdbeValue
# ----
def frules(compound, rules=['HC','NOPSC','NOPS','RDBE','RDBEInt'], HC=(0.1, 3.0), NOPSC=(4,3,2,3), RDBE=(-1,40)):
"""Check formula rules for a given compound.
compound (str or mspy.compound) - compound
rules (list of str) - rules to be checked
HC (tuple) - H/C limits
NOPSC (tuple) - NOPS/C max values
RDBE (tuple) - RDBE limits
"""
# check compound
if not isinstance(compound, obj_compound.compound):
compound = obj_compound.compound(compound)
# get element counts
countC = float(compound.count('C', groupIsotopes=True))
countH = float(compound.count('H', groupIsotopes=True))
countN = float(compound.count('N', groupIsotopes=True))
countO = float(compound.count('O', groupIsotopes=True))
countP = float(compound.count('P', groupIsotopes=True))
countS = float(compound.count('S', groupIsotopes=True))
# get carbon ratios
if countC:
ratioHC = countH / countC
ratioNC = countN / countC
ratioOC = countO / countC
ratioPC = countP / countC
ratioSC = countS / countC
# get RDBE
rdbeValue = rdbe(compound)
# check HC rule
if 'HC' in rules and countC:
if (ratioHC < HC[0] or ratioHC > HC[1]):
return False
# check NOPS rule
if 'NOPSC' in rules and countC:
if (ratioNC > NOPSC[0] or ratioOC > NOPSC[1] or ratioPC > NOPSC[2] or ratioSC > NOPSC[3]):
return False
# check NOPS all > 1 rule
if 'NOPS' in rules and (countN > 1 and countO > 1 and countP > 1 and countS > 1):
if (countN >= 10 or countO >= 20 or countP >= 4 or countS >= 3):
return False
# check NOP all > 3 rule
if 'NOPS' in rules and (countN > 3 and countO > 3 and countP > 3):
if (countN >= 11 or countO >= 22 or countP >= 6):
return False
# check NOS all > 1 rule
if 'NOPS' in rules and (countN > 1 and countO > 1 and countS > 1):
if (countN >= 19 or countO >= 14 or countS >= 8):
return False
# check NPS all > 1 rule
if 'NOPS' in rules and (countN > 1 and countP > 1 and countS > 1):
if (countN >= 3 or countP >= 3 or countS >= 3):
return False
# check OPS all > 1 rule
if 'NOPS' in rules and (countO > 1 and countP > 1 and countS > 1):
if (countO >= 14 or countP >= 3 or countS >= 3):
return False
# check RDBE range
if 'RDBE' in rules:
if rdbeValue < RDBE[0] or rdbeValue > RDBE[1]:
return False
# check integer RDBE
if 'RDBEInt' in rules:
if rdbeValue % 1:
return False
# all ok
return True
# ----
|