From 7ecb4d01df920a5bec9a1d4d1b2477eabaa94a26 Mon Sep 17 00:00:00 2001 From: Wout Date: Wed, 1 Jul 2020 18:41:34 -0700 Subject: [PATCH] Make sure charge isn't 0 during peak annotation Fixes #11. --- spectrum_utils/spectrum.py | 2 +- spectrum_utils/tests/spectrum_test.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/spectrum_utils/spectrum.py b/spectrum_utils/spectrum.py index 9dc4aed..d50c946 100755 --- a/spectrum_utils/spectrum.py +++ b/spectrum_utils/spectrum.py @@ -1037,7 +1037,7 @@ def annotate_peptide_fragments(self, fragment_tol_mass: float, if self.peptide is None: raise ValueError('No peptide sequence available for the spectrum') if max_ion_charge is None: - max_ion_charge = self.precursor_charge - 1 + max_ion_charge = max(1, self.precursor_charge - 1) theoretical_fragments = _get_theoretical_peptide_fragments( self.peptide, self.modifications, ion_types, max_ion_charge) diff --git a/spectrum_utils/tests/spectrum_test.py b/spectrum_utils/tests/spectrum_test.py index 95a0c1c..2e8035e 100644 --- a/spectrum_utils/tests/spectrum_test.py +++ b/spectrum_utils/tests/spectrum_test.py @@ -646,10 +646,10 @@ def test_annotate_peptide_fragments(): fragment_tol_mode = 'Da' peptides = ['SYELPDGQVITIGNER', 'MFLSFPTTK', 'DLYANTVLSGGTTMYPGIADR', 'YLYEIAR', 'VAPEEHPVLLTEAPLNPK'] - for peptide in peptides: + for charge, peptide in enumerate(peptides, 1): fragment_mz = np.asarray([fragment.calc_mz for fragment in spectrum._get_theoretical_peptide_fragments( - peptide)]) + peptide, max_charge=charge - 1)]) fragment_mz += np.random.uniform( -0.9 * fragment_tol_mass, 0.9 * fragment_tol_mass, len(fragment_mz)) @@ -657,7 +657,6 @@ def test_annotate_peptide_fragments(): mz = np.random.uniform(100, 1400, num_peaks) mz[: len(fragment_mz)] = fragment_mz intensity = np.random.lognormal(0, 1, num_peaks) - charge = 2 spec = spectrum.MsmsSpectrum( 'test_spectrum', mass.calculate_mass(sequence=peptide, charge=charge),