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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
# -------------------------------------------------------------------------
# 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 stopper
from mod_stopper import CHECK_FORCE_QUIT
# load objects
import blocks
# load modules
import mod_basics
import mod_pattern
# COMPOUND OBJECT DEFINITION
# --------------------------
class compound:
"""Compound object definition."""
def __init__(self, expression, **attr):
# check formula
self._checkFormula(expression)
self.expression = expression
# buffers
self._composition = None
self._formula = None
self._mass = None
# get additional attributes
self.attributes = {}
for name, value in attr.items():
self.attributes[name] = value
# ----
def __iadd__(self, other):
"""Append formula."""
# check and append value
if isinstance(other, compound):
self.expression += other.expression
else:
self._checkFormula(other)
self.expression += other
# clear buffers
self.reset()
return self
# ----
def reset(self):
"""Clear formula buffers."""
self._composition = None
self._formula = None
self._mass = None
# ----
# GETTERS
def count(self, item, groupIsotopes=False):
"""Count atom in formula."""
# get composition
comp = self.composition()
# get atoms to count
atoms = [item]
if groupIsotopes and item in blocks.elements:
for massNo in blocks.elements[item].isotopes:
atom = '%s{%d}' % (item,massNo)
atoms.append(atom)
# count atom
atomsCount = 0
for atom in atoms:
if atom in comp:
atomsCount += comp[atom]
return atomsCount
# ----
def formula(self):
"""Get formula."""
# check formula buffer
if self._formula != None:
return self._formula
self._formula = ''
# get composition
comp = self.composition()
elements = sorted(comp.keys())
# add C and H first
for el in ('C', 'C{12}', 'C{13}', 'C{14}', 'H', 'H{1}', 'H{2}', 'H{3}'):
if el in elements:
if comp[el] == 1:
self._formula += el
else:
self._formula += '%s%d' % (el, comp[el])
del elements[elements.index(el)]
# add remaining atoms
for el in elements:
if comp[el] == 1:
self._formula += el
else:
self._formula += '%s%d' % (el, comp[el])
return self._formula
# ----
def composition(self):
"""Get elemental composition."""
# check composition buffer
if self._composition != None:
return self._composition
# unfold brackets
unfoldedFormula = self._unfoldBrackets(self.expression)
# group elements
self._composition = {}
for symbol, isotop, count in mod_basics.ELEMENT_PATTERN.findall(unfoldedFormula):
# make atom
if isotop:
atom = '%s{%s}' % (symbol, isotop)
else:
atom = symbol
# convert counting
if count:
count = int(count)
else:
count = 1
# add atom
if atom in self._composition:
self._composition[atom] += count
else:
self._composition[atom] = count
# remove zeros
for atom in self._composition.keys():
if self._composition[atom] == 0:
del self._composition[atom]
return self._composition
# ----
def mass(self, massType=None):
"""Get mass."""
# get mass
if self._mass == None:
massMo = 0
massAv = 0
# get composition
comp = self.composition()
# get mass for each atom
for atom in comp:
count = comp[atom]
# check specified isotope and mass
match = mod_basics.ELEMENT_PATTERN.match(atom)
symbol, massNumber, tmp = match.groups()
if massNumber:
isotope = blocks.elements[symbol].isotopes[int(massNumber)]
atomMass = (isotope[0], isotope[0])
else:
atomMass = blocks.elements[symbol].mass
# multiply atom mass
massMo += atomMass[0]*count
massAv += atomMass[1]*count
# store mass in buffer
self._mass = (massMo, massAv)
# return mass
if massType == 0:
return self._mass[0]
elif massType == 1:
return self._mass[1]
else:
return self._mass
# ----
def mz(self, charge, agentFormula='H', agentCharge=1):
"""Get ion m/z."""
return mod_basics.mz(self.mass(),
charge = charge,
agentFormula = agentFormula,
agentCharge = agentCharge
)
# ----
def pattern(self, fwhm=0.1, threshold=0.01, charge=0, agentFormula='H', agentCharge=1, real=True):
"""Get isotopic pattern."""
return mod_pattern.pattern(
compound = self,
fwhm = fwhm,
threshold = threshold,
charge = charge,
agentFormula = agentFormula,
agentCharge = agentCharge,
real = real
)
# ----
def rdbe(self):
"""Get RDBE (Range or Double Bonds Equivalents)."""
return mod_basics.rdbe(self)
# ----
def isvalid(self, charge=0, agentFormula='H', agentCharge=1):
"""Check ion composition."""
# check agent formula
if agentFormula != 'e' and not isinstance(agentFormula, compound):
agentFormula = compound(agentFormula)
# make ion compound
if charge and agentFormula != 'e':
ionFormula = self.expression
for atom, count in agentFormula.composition().items():
ionFormula += '%s%d' % (atom, count*(charge/agentCharge))
ion = compound(ionFormula)
else:
ion = compound(self.expression)
# get composition
for atom, count in ion.composition().items():
if count < 0:
return False
return True
# ----
def frules(self, rules=['HC','NOPSC','NOPS','RDBE'], HC=(0.1, 3.0), NOPSC=(4,3,2,3), RDBE=(-1,40)):
"""Check formula rules."""
return mod_basics.frules(
compound = self,
rules = rules,
HC = HC,
NOPSC = NOPSC,
RDBE = RDBE
)
# ----
# MODIFIERS
def negate(self):
"""Make all atom counts negative."""
# get composition
comp = self.composition()
# negate composition
formula = ''
for el in sorted(comp.keys()):
formula += '%s%d' % (el, -1*comp[el])
self.expression = formula
# clear buffers
self.reset()
# ----
# HELPERS
def _checkFormula(self, formula):
"""Check given formula."""
# check formula
if not mod_basics.FORMULA_PATTERN.match(formula):
raise ValueError, 'Wrong formula! --> ' + formula
# check elements and isotopes
for atom in mod_basics.ELEMENT_PATTERN.findall(formula):
if not atom[0] in blocks.elements:
raise ValueError, 'Unknown element in formula! --> ' + atom[0] + ' in ' + formula
elif atom[1] and not int(atom[1]) in blocks.elements[atom[0]].isotopes:
raise ValueError, 'Unknown isotope in formula! --> ' + atom[0] + atom[1] + ' in ' + formula
# check brackets
if formula.count(')') != formula.count('('):
raise ValueError, 'Wrong number of brackets in formula! --> ' + formula
# ----
def _unfoldBrackets(self, string):
"""Unfold formula and count each atom."""
unfoldedFormula = ''
brackets = [0,0]
enclosedFormula = ''
i = 0
while i < len(string):
# handle brackets
if string[i] == '(':
brackets[0] += 1
elif string[i] == ')':
brackets[1] += 1
# part outside brackets
if brackets == [0,0]:
unfoldedFormula += string[i]
# part within brackets
else:
enclosedFormula += string[i]
# unfold part within brackets
if brackets[0] == brackets[1]:
enclosedFormula = self._unfoldBrackets(enclosedFormula[1:-1])
# multiply part within brackets
count = ''
while len(string)>(i+1) and string[i+1].isdigit():
count += string[i+1]
i += 1
if count:
enclosedFormula = enclosedFormula * int(count)
# add and clear
unfoldedFormula += enclosedFormula
enclosedFormula = ''
brackets = [0,0]
i += 1
return unfoldedFormula
# ----
|