Skip to content

Commit

Permalink
Address #156
Browse files Browse the repository at this point in the history
  • Loading branch information
levitsky committed Sep 26, 2024
1 parent 67b17ef commit 663fa96
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
30 changes: 18 additions & 12 deletions pyteomics/mass/mass.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,19 @@ def mass(self, **kwargs):
mass = 0.0
average = kwargs.get('average', False)

for isotope_string, amount in self.items():
element_name, isotope_num = _parse_isotope_string(isotope_string)
# Calculate average mass if required and the isotope number is
# not specified.
if (not isotope_num) and average:
for isotope, data in mass_data[element_name].items():
if isotope:
mass += (amount * data[0] * data[1])
else:
mass += (amount * mass_data[element_name][isotope_num][0])
try:
for isotope_string, amount in self.items():
element_name, isotope_num = _parse_isotope_string(isotope_string)
# Calculate average mass if required and the isotope number is
# not specified.
if (not isotope_num) and average:
for isotope, data in mass_data[element_name].items():
if isotope:
mass += (amount * data[0] * data[1])
else:
mass += (amount * mass_data[element_name][isotope_num][0])
except KeyError as e:
raise PyteomicsError('No mass information for element: {}'.format(e.args[0]))

return self._mass_to_mz(mass, self, **kwargs)

Expand Down Expand Up @@ -1016,10 +1019,13 @@ def fast_mass2(sequence, ion_type=None, charge=None, **kwargs):
elif parser.is_term_mod(aa):
assert num == 1
group = aa.strip('-')
if group in aa_mass:
if group.islower() and group in aa_mass:
_raise_term_label_exception('mass')
else:
mass += calculate_mass(formula=group, mass_data=mass_data)
try:
mass += calculate_mass(formula=group, mass_data=mass_data)
except PyteomicsError:
raise PyteomicsError('Could not parse terminal group as a formula: {}'.format(group))
else:
mod, X = parser._split_label(aa)
mass += (aa_mass[mod] + aa_mass[X]) * num
Expand Down
1 change: 1 addition & 0 deletions pyteomics/mgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ def _warn_empty(self):
self.__class__.__name__, getattr(self._source, 'name', self._source_init), not self._index_by_scans))
warnings.warn(text)


class MGF(MGFBase, aux.FileReader):
"""
A class representing an MGF file. Supports the `with` syntax and direct iteration for sequential
Expand Down
3 changes: 3 additions & 0 deletions tests/test_mass.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def test_fast_mass2_no_term(self):
mass.fast_mass2(pep, aa_mass=self.test_aa_mass),
sum(pep.count(aa) * m for aa, m in self.test_aa_mass.items()) + self.mass_H * 2.0 + self.mass_O)

def test_fast_mass2_sanity(self):
self.assertAlmostEqual(mass.fast_mass2('PEPTIDE'), mass.fast_mass('PEPTIDE'), 799.36)

def test_fast_mass2_term(self):
for pep in self.random_peptides:
nterm = 'AB2C3-'
Expand Down

0 comments on commit 663fa96

Please sign in to comment.