def test_annotate_peptide_fragments(): fragment_tol_mass = 0.02 fragment_tol_mode = 'Da' peptides = [ 'SYELPDGQVITIGNER', 'MFLSFPTTK', 'DLYANTVLSGGTTMYPGIADR', 'YLYEIAR', 'VAPEEHPVLLTEAPLNPK' ] for peptide in peptides: fragment_mz = np.asarray([ fragment.calc_mz for fragment in spectrum._get_theoretical_peptide_fragments(peptide) ]) fragment_mz += np.random.uniform(-0.9 * fragment_tol_mass, 0.9 * fragment_tol_mass, len(fragment_mz)) num_peaks = 150 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), charge, mz, intensity, peptide=peptide) spec.annotate_peptide_fragments(fragment_tol_mass, fragment_tol_mode) assert np.count_nonzero(spec.annotation) == len(fragment_mz)
def test_get_theoretical_fragments_mod_multiple(): peptide = 'HPYLEDR' modifications = {'N-term': 42.01056, 2: 79.96633} fragments = {'b1': 180.076706, 'b2': 277.129486, 'b3': 520.159180, 'b4': 633.243225, 'b5': 762.285828, 'b6': 877.312744, 'y1': 175.118912, 'y2': 290.145844, 'y3': 419.188446, 'y4': 532.272522, 'y5': 775.302185, 'y6': 872.354980} for fragment in spectrum._get_theoretical_peptide_fragments( peptide, modifications): fragment_mz = fragments[f'{fragment.ion_type}{fragment.ion_index}'] assert fragment.calc_mz == pytest.approx(fragment_mz)
def test_get_theoretical_fragments_mod_term(): peptide = 'HPYLEDR' modifications = {'N-term': 42.01056} fragments = {'b1': 180.076706, 'b2': 277.129486, 'b3': 440.192810, 'b4': 553.276917, 'b5': 682.319519, 'b6': 797.346436, 'y1': 175.118912, 'y2': 290.145844, 'y3': 419.188446, 'y4': 532.272522, 'y5': 695.335815, 'y6': 792.388550} for fragment in spectrum._get_theoretical_peptide_fragments( peptide, modifications): fragment_mz = fragments[f'{fragment.ion_type}{fragment.ion_index}'] assert fragment.calc_mz == pytest.approx(fragment_mz)
def test_get_theoretical_fragments_static_mod(): spectrum.static_modification('Y', 79.96633) peptide = 'HPYLEDR' fragments = { 'b1_1': 138.066147, 'b2_1': 235.118912, 'b3_1': 478.148590, 'b4_1': 591.232666, 'b5_1': 720.275269, 'b6_1': 835.302185, 'y1_1': 175.118912, 'y2_1': 290.145844, 'y3_1': 419.188446, 'y4_1': 532.272522, 'y5_1': 775.302185, 'y6_1': 872.354980, 'b1_2': 69.536731, 'b2_2': 118.063111, 'b3_2': 239.577941, 'b4_2': 296.119971, 'b5_2': 360.641266, 'b6_2': 418.154736, 'y1_2': 88.063114, 'y2_2': 145.576584, 'y3_2': 210.097879, 'y4_2': 266.639909, 'y5_2': 388.154739, 'y6_2': 436.681119, 'b1_3': 46.693580, 'b2_3': 79.044500, 'b3_3': 160.054386, 'b4_3': 197.749073, 'b5_3': 240.763270, 'b6_3': 279.105583, 'y1_3': 59.044501, 'y2_3': 97.386815, 'y3_3': 140.401011, 'y4_3': 178.095698, 'y5_3': 259.105585, 'y6_3': 291.456505 } for fragment in spectrum._get_theoretical_peptide_fragments(peptide, None, max_charge=3): fragment_mz = fragments[f'{fragment.ion_type}{fragment.ion_index}' f'_{fragment.charge}'] assert fragment.calc_mz == pytest.approx(fragment_mz) assert spectrum._aa_mass['Y'] == pytest.approx(163.06333 + 79.96633) spectrum.reset_modifications() assert spectrum._aa_mass['Y'] == pytest.approx(163.06333)
def plot_spectrum(identifier, precursor_mz, precursor_charge, mz, intensity, retention_time, peptide): # identifier = spectrum_dict['params']['title'] # precursor_mz = spectrum_dict['params']['pepmass'][0] # precursor_charge = spectrum_dict['params']['charge'][0] # mz = spectrum_dict['m/z array'] # intensity = spectrum_dict['intensity array'] # retention_time = float(spectrum_dict['params']['rtinseconds']) # peptide = 'WNQLQAFWGTGK' # Create the MS/MS spectrum. spectrum = sus.MsmsSpectrum(identifier, precursor_mz=precursor_mz, precursor_charge=precursor_charge, mz=mz, intensity=intensity, retention_time=retention_time, peptide=peptide) # Process the MS/MS spectrum. # fragment_tol_mass = 10 # fragment_tol_mode = 'ppm' fragment_tol_mass = .5 fragment_tol_mode = 'Da' spectrum = (spectrum.set_mz_range( min_mz=100, max_mz=1400).remove_precursor_peak( fragment_tol_mass, fragment_tol_mode).filter_intensity( min_intensity=0.05, max_num_peaks=50).scale_intensity( 'root').annotate_peptide_fragments(fragment_tol_mass, fragment_tol_mode, ion_types='aby')) # Generate theoretical spec ts = sus._get_theoretical_peptide_fragments(peptide) tmz = [frag.calc_mz for frag in ts] ti = [1.0 for _ in tmz] tspec = sus.MsmsSpectrum(identifier, precursor_mz=precursor_mz, precursor_charge=precursor_charge, mz=tmz, intensity=ti, retention_time=retention_time, peptide=peptide) # Plot the MS/MS spectrum. fig, ax = plt.subplots(figsize=(12, 6)) # sup.spectrum(spectrum, ax=ax) sup.mirror(spectrum, tspec, ax=ax) plt.show() plt.close()
def test_get_theoretical_fragments(): peptide = 'HPYLEDR' fragments = { 'b1_1': 138.066147, 'b2_1': 235.118912, 'b3_1': 398.182220, 'b4_1': 511.266266, 'b5_1': 640.308899, 'b6_1': 755.335815, 'y1_1': 175.118912, 'y2_1': 290.145844, 'y3_1': 419.188446, 'y4_1': 532.272522, 'y5_1': 695.335815, 'y6_1': 792.388550, 'b1_2': 69.536731, 'b2_2': 118.063111, 'b3_2': 199.594776, 'b4_2': 256.136806, 'b5_2': 320.658101, 'b6_2': 378.171571, 'y1_2': 88.063114, 'y2_2': 145.576584, 'y3_2': 210.097879, 'y4_2': 266.639909, 'y5_2': 348.171574, 'y6_2': 396.697954, 'b1_3': 46.693580, 'b2_3': 79.044500, 'b3_3': 133.398943, 'b4_3': 171.093630, 'b5_3': 214.107826, 'b6_3': 252.450140, 'y1_3': 59.044501, 'y2_3': 97.386815, 'y3_3': 140.401011, 'y4_3': 178.095698, 'y5_3': 232.450141, 'y6_3': 264.801061 } for fragment in spectrum._get_theoretical_peptide_fragments(peptide, max_charge=3): fragment_mz = fragments[f'{fragment.ion_type}{fragment.ion_index}' f'_{fragment.charge}'] assert fragment.calc_mz == pytest.approx(fragment_mz)
def test_annotate_peaks_most_intense(): fragment_tol_mass = 0.02 fragment_tol_mode = 'Da' peptide = 'YLYEIAR' fragment_mz = np.asarray([mz for _, mz in spectrum._get_theoretical_peptide_fragments( peptide)]) mz = np.asarray([fragment_mz[0] - 0.01, fragment_mz[0] + 0.01]) intensity = np.asarray([10, 20]) charge = 2 spec = spectrum.MsmsSpectrum( 'test_spectrum', mass.calculate_mass(sequence=peptide, charge=charge), charge, mz, intensity, peptide=peptide) spec.annotate_peaks(fragment_tol_mass, fragment_tol_mode, peak_assignment='most_intense') assert spec.annotation[0] is None assert spec.annotation[1] is not None
def test_annotate_peaks_nearest_mz(): fragment_tol_mass = 0.02 fragment_tol_mode = 'Da' peptide = 'YLYEIAR' fragment_mz = np.asarray([fragment.calc_mz for fragment in spectrum._get_theoretical_peptide_fragments( peptide)]) mz = np.asarray([fragment_mz[0] - 0.005, fragment_mz[0] + 0.015]) intensity = np.asarray([10, 20]) charge = 2 spec = spectrum.MsmsSpectrum( 'test_spectrum', mass.calculate_mass(sequence=peptide, charge=charge), charge, mz, intensity, peptide=peptide) spec.annotate_peaks(fragment_tol_mass, fragment_tol_mode, peak_assignment='nearest_mz') assert spec.annotation[0] == spectrum.FragmentAnnotation('b', 1, 1, fragment_mz[0]) assert spec.annotation[1] is None
def _get_theoretical_fragment_mzs(sequence: str) -> np.ndarray: """ Get the theoretical b and y ion m/z values for the given peptide sequence. Parameters ---------- sequence : str The peptide sequence for which b and y ion m/z values will be calculated. Returns ------- np.ndarray An array of sorted m/z values of the b and y ions for the given peptide sequence. """ # Correct for 0-based offsets required by spectrum_utils. mods, mod_pos_offset = {}, 1 for match in re.finditer(regex_mod, sequence): mods[match.start() - mod_pos_offset] = float(match.group(0)) mod_pos_offset += match.end() - match.start() return np.asarray([fragment.calc_mz for fragment in sus._get_theoretical_peptide_fragments( _remove_mod(sequence), mods)])