Skip to content

Commit

Permalink
Fix silencing of errors during ProForma mass calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
mobiusklein committed Sep 28, 2024
1 parent 2d0a874 commit 723ac1e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
78 changes: 48 additions & 30 deletions pyteomics/proforma.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ def find_tag_type(self, tag_type):
def parse(cls, buffer):
return process_tag_tokens(buffer)

def has_mass(self):
"""
Check if this tag carries a mass value.
Returns
-------
bool
"""
return False


class GroupLabelBase(TagBase):
__slots__ = ()
Expand Down Expand Up @@ -742,6 +752,16 @@ def mass(self):
'''
return self.definition['mass']

def has_mass(self):
"""
Check if this tag carries a mass value.
Returns
-------
bool
"""
return True

@property
def composition(self):
'''The chemical composition shift this modification applies'''
Expand Down Expand Up @@ -850,6 +870,16 @@ def key(self):
def mass(self):
return self.value

def has_mass(self):
"""
Check if this tag carries a mass value.
Returns
-------
bool
"""
return True

def __eq__(self, other):
if isinstance(other, ModificationToken):
return other == self
Expand Down Expand Up @@ -2321,38 +2351,32 @@ def mass(self):
c_term = i == c_term_v
for rule in fixed_modifications:
if rule.is_valid(aa, n_term, c_term):
mass += rule.modification_tag.mass
if rule.modification_tag.has_mass():
mass += rule.modification_tag.mass
tags = position[1]
if tags:
for tag in tags:
try:
if tag.has_mass():
mass += tag.mass
except (AttributeError, KeyError):
continue
for mod in self.properties['labile_modifications']:
mass += mod.mass
for mod in self.properties['unlocalized_modifications']:
mass += mod.mass
if self.properties.get('n_term'):
for mod in self.properties['n_term']:
try:
if mod.has_mass():
mass += mod.mass
except (AttributeError, KeyError):
continue
mass += calculate_mass(formula="H")
if self.properties.get('c_term'):
for mod in self.properties['c_term']:
try:
if mod.has_mass():
mass += mod.mass
except (AttributeError, KeyError):
continue

mass += calculate_mass(formula="OH")
for iv in self.properties['intervals']:
try:
mass += iv.tag.mass
except (AttributeError, KeyError):
continue
for tag in iv.tags:
if tag.has_mass():
mass += tag.mass
return mass

def fragments(self, ion_shift, charge=1, reverse=None, include_labile=True, include_unlocalized=True):
Expand Down Expand Up @@ -2419,21 +2443,18 @@ def fragments(self, ion_shift, charge=1, reverse=None, include_labile=True, incl
if not reverse:
if self.properties.get('n_term'):
for mod in self.properties['n_term']:
try:
if mod.has_mass():
mass += mod.mass
except (AttributeError, KeyError):
continue
else:
if self.properties.get('c_term'):
for mod in self.properties['c_term']:
try:
if mod.has_mass():
mass += mod.mass
except (AttributeError, KeyError):
continue

if include_unlocalized:
for mod in self.properties['unlocalized_modifications']:
mass += mod.mass
if mod.has_mass():
mass += mod.mass

mass += _WATER_MASS

Expand All @@ -2459,23 +2480,20 @@ def fragments(self, ion_shift, charge=1, reverse=None, include_labile=True, incl
c_term = i == c_term_v
for rule in fixed_modifications:
if rule.is_valid(aa, n_term, c_term):
mass += rule.modification_tag.mass
if rule.modification_tag.has_mass():
mass += rule.modification_tag.mass

tags = position[1]
if tags:
for tag in tags:
try:
if tag.has_mass():
mass += tag.mass
except (AttributeError, KeyError):
continue

while intervals and intervals[0].contains(i):
iv = intervals.popleft()

try:
mass += iv.tag.mass
except (AttributeError, KeyError):
continue
for tag in iv.tags:
if tag.has_mass():
mass += tag.mass

masses.append(mass)

Expand Down
3 changes: 1 addition & 2 deletions tests/test_proforma.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def test_complicated_short(self):
assert properties['fixed_modifications'][0] == ModificationRule(
GenericModification('Carbamidomethyl', None, None), ['C'])
assert to_proforma(tokens, **properties) == complicated_short
self.assertAlmostEqual(
ProForma(tokens, properties).mass, 1210.5088, 3)
self.assertAlmostEqual(ProForma(tokens, properties).mass, 1228.6588, 3)

def test_range(self):
seq = "PRQT(EQC[Carbamidomethyl]FQRMS)[+19.0523]ISK"
Expand Down

0 comments on commit 723ac1e

Please sign in to comment.