Skip to content

Commit

Permalink
Fix calculate_mass with composition kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
levitsky committed Feb 13, 2024
1 parent 74944e6 commit 4f4a502
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 0 additions & 1 deletion CHANGELOG
Expand Up @@ -21,7 +21,6 @@
(`#138 <https://github.com/levitsky/pyteomics/pull/138>`_).



4.6.3
-----

Expand Down
8 changes: 5 additions & 3 deletions pyteomics/mass/mass.py
Expand Up @@ -464,14 +464,13 @@ def mass(self, **kwargs):
-------
mass : float
"""
composition = self
mass_data = kwargs.get('mass_data', nist_mass)

# Calculate mass
mass = 0.0
average = kwargs.get('average', False)

for isotope_string, amount in composition.items():
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.
Expand Down Expand Up @@ -649,7 +648,10 @@ def calculate_mass(*args, **kwargs):
if k in kwargs:
mass_kw[k] = kwargs.pop(k)
# Make a copy of `composition` keyword argument.
composition = (Composition(kwargs['composition']) if 'composition' in kwargs else Composition(*args, **kwargs))
if 'composition' in kwargs:
composition = Composition(kwargs.pop('composition'))
else:
composition = Composition(*args, **kwargs)
kwargs.update(mass_kw)
return composition.mass(**kwargs)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_mass.py
Expand Up @@ -148,6 +148,11 @@ def test_calculate_mass(self):
mass.calculate_mass(parsed_sequence=['H-', 'X', 'Y', 'Z', '-OH'], aa_comp=self.aa_comp, mass_data=self.mass_data),
sum(self.mass_data[atom][0][0] for atom in 'ABCDE'))

# Calculate mass by composition
self.assertEqual(
mass.calculate_mass(composition={'A': 1, 'B': 1, 'C': 1, 'D': 1, 'E': 1}, mass_data=self.mass_data),
sum(self.mass_data[atom][0][0] for atom in 'ABCDE'))

# Calculate average mass by a formula.
self.assertEqual(
mass.calculate_mass(formula='ABCDE', average=True, mass_data=self.mass_data),
Expand Down Expand Up @@ -175,6 +180,10 @@ def test_calculate_mass(self):
mass.calculate_mass(formula='ABCDE', ion_type='M', charge=charge, mass_data=self.mass_data),
(mass.calculate_mass(formula='ABCDE', mass_data=self.mass_data) + self.mass_data['H+'][0][0] * charge) / charge)

self.assertAlmostEqual(
mass.calculate_mass(composition={'A': 1, 'B': 1, 'C': 1, 'D': 1, 'E': 1}, charge=charge, charge_carrier='BC+', mass_data=self.mass_data),
mass.calculate_mass(composition={'A': 1, 'B': 1 + charge, 'C': 1 + charge, 'D': 1, 'E': 1}, mass_data=self.mass_data) / charge)

self.assertRaises(auxiliary.PyteomicsError, mass.calculate_mass, **{'formula': 'ABCDEH+%d' % charge,
'ion_type': 'M', 'charge': charge, 'mass_data': self.mass_data})

Expand Down

0 comments on commit 4f4a502

Please sign in to comment.