From 723ac1e2d5d23862d84d5ec72f3167ca5ab0ec9d Mon Sep 17 00:00:00 2001 From: Joshua Klein Date: Sat, 28 Sep 2024 09:38:50 -0400 Subject: [PATCH] Fix silencing of errors during ProForma mass calculations --- pyteomics/proforma.py | 78 ++++++++++++++++++++++++++---------------- tests/test_proforma.py | 3 +- 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/pyteomics/proforma.py b/pyteomics/proforma.py index 2d71c33..188a622 100644 --- a/pyteomics/proforma.py +++ b/pyteomics/proforma.py @@ -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__ = () @@ -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''' @@ -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 @@ -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): @@ -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 @@ -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) diff --git a/tests/test_proforma.py b/tests/test_proforma.py index 2425f83..3331344 100644 --- a/tests/test_proforma.py +++ b/tests/test_proforma.py @@ -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"