def test_init_annotation_order(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) intensity = np.random.lognormal(0, 1, num_peaks) annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz, intensity, annotation) for this_mz, this_annotation in zip(spec.mz, spec.annotation): assert this_mz == pytest.approx(this_annotation.calc_mz)
def test_filter_intensity_keep_all(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) intensity = np.random.lognormal(0, 1, num_peaks) annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz, intensity, annotation) spec.filter_intensity() assert len(spec.mz) == num_peaks assert len(spec.intensity) == num_peaks assert len(spec.annotation) == num_peaks
def test_annotation_none(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) intensity = np.random.lognormal(0, 1, num_peaks) spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz, intensity) assert spec.annotation is None annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec.annotation = annotation assert type(spec.annotation) == np.ndarray spec.annotation = None assert spec.annotation is None
def test_set_mz_range_none(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) intensity = np.random.lognormal(0, 1, num_peaks) annotation = [ spectrum.PeptideFragmentAnnotation('y', i, 1, this_mz) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz, intensity, annotation) spec.set_mz_range(None, None) assert len(spec.mz) == num_peaks assert len(spec.intensity) == num_peaks assert len(spec.annotation) == num_peaks
def test_annotation_array(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) intensity = np.random.lognormal(0, 1, num_peaks) annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz, intensity, annotation) assert type(spec.annotation) == np.ndarray spec.annotation = annotation assert type(spec.annotation) == np.ndarray with pytest.raises(ValueError): spec.annotation = 14
def test_filter_intensity_max_num_peaks(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) intensity = np.random.lognormal(0, 1, num_peaks) max_intensity = intensity.max() annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz, intensity, annotation) max_num_peaks = 50 spec.filter_intensity(max_num_peaks=max_num_peaks) assert len(spec.mz) == max_num_peaks assert len(spec.intensity) == max_num_peaks assert len(spec.annotation) == max_num_peaks assert spec.intensity.max() == pytest.approx(max_intensity)
def test_set_mz_range_truncate_right(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) intensity = np.random.lognormal(0, 1, num_peaks) annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz, intensity, annotation) min_mz, max_mz = 0, 1200 assert spec.mz.max() > max_mz spec.set_mz_range(min_mz, max_mz) assert len(spec.mz) < num_peaks assert len(spec.intensity) < num_peaks assert len(spec.annotation) < num_peaks assert spec.mz.max() <= max_mz
def test_round_merge_len(): num_peaks = 10 mz = np.arange(1, num_peaks + 1) + np.random.uniform(-0.2, 0.2, num_peaks) mz[4] = mz[3] + 0.0002 mz[5] = mz[3] + 0.0005 mz[7] = mz[8] - 0.00037 intensity = np.random.exponential(1, num_peaks) annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz, intensity, annotation) spec.round(1) assert len(spec.mz) == len(mz) - 3 assert len(spec.mz) == len(spec.intensity) assert len(spec.mz) == len(spec.annotation)
def test_round_no_merge(): num_peaks = 150 mz = np.arange(1, num_peaks + 1) + np.random.uniform(-0.49, 0.5, num_peaks) intensity = np.random.exponential(1, num_peaks) annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz.copy(), intensity.copy(), annotation.copy()) decimals = 0 spec.round(decimals) assert len(spec.mz) == num_peaks assert len(spec.intensity) == num_peaks assert len(spec.annotation) == num_peaks np.testing.assert_allclose(spec.mz, np.around(mz, decimals)) np.testing.assert_allclose(spec.intensity, intensity)
def test_remove_precursor_peak_none(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) fragment_tol_mass = np.random.uniform(0, 0.5) fragment_tol_mode = 'Da' precursor_mz = mz[np.random.randint(0, num_peaks)] + fragment_tol_mass * 2 intensity = np.random.lognormal(0, 1, num_peaks) annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', precursor_mz, 2, mz, intensity, annotation) spec.remove_precursor_peak(fragment_tol_mass, fragment_tol_mode) assert len(spec.mz) == num_peaks assert len(spec.intensity) == num_peaks assert len(spec.annotation) == num_peaks assert np.abs(precursor_mz - spec.mz).all() > fragment_tol_mass
def test_filter_intensity_remove_low_intensity(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) intensity = np.random.lognormal(0, 1, num_peaks) max_intensity = intensity.max() annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz, intensity, annotation) min_intensity = 0.05 assert spec.intensity.min() < min_intensity * spec.intensity.max() spec.filter_intensity(min_intensity=min_intensity) assert len(spec.mz) < num_peaks assert len(spec.intensity) < num_peaks assert len(spec.annotation) < num_peaks assert spec.intensity.max() == pytest.approx(max_intensity) assert spec.intensity.min() >= min_intensity * max_intensity
def test_remove_precursor_peak_isotope(): num_peaks = 150 mz = np.random.uniform(100, 1400, num_peaks) fragment_tol_mass = np.random.uniform(0, 0.5) fragment_tol_mode = 'Da' precursor_mz = mz[np.random.randint(0, num_peaks)] + fragment_tol_mass / 2 precursor_charge = 3 mz[-1] = precursor_mz + 1 / precursor_charge mz[-2] = precursor_mz + 2 / precursor_charge intensity = np.random.lognormal(0, 1, num_peaks) annotation = [ spectrum.PeptideFragmentAnnotation(1, this_mz, 'y', i) for i, this_mz in enumerate(mz) ] spec = spectrum.MsmsSpectrum('test_spectrum', precursor_mz, precursor_charge, mz, intensity, annotation) spec.remove_precursor_peak(fragment_tol_mass, fragment_tol_mode, 2) assert np.abs(precursor_mz - spec.mz).all() > fragment_tol_mass assert len(spec.mz) <= num_peaks - 3 assert len(spec.intensity) <= num_peaks - 3 assert len(spec.annotation) <= num_peaks - 3
def test_annotate_peptide_fragments_most_intense(): 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.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_peptide_fragments(fragment_tol_mass, fragment_tol_mode, peak_assignment='most_intense') assert spec.annotation[0] is None assert spec.annotation[1] == spectrum.PeptideFragmentAnnotation( 1, fragment_mz[0], 'b', 1)
def test_round_merge_annotation(): num_peaks = 10 mz = np.arange(1, num_peaks + 1) + np.random.uniform(-0.2, 0.2, num_peaks) mz[4] = mz[3] + 0.0002 mz[5] = mz[3] + 0.0005 mz[7] = mz[8] - 0.00037 intensity = np.arange(1, 11) annotation = [ None, None, None, spectrum.PeptideFragmentAnnotation(1, mz[3], 'b', 4), None, spectrum.PeptideFragmentAnnotation(1, mz[5], 'b', 6), spectrum.PeptideFragmentAnnotation(1, mz[6], 'b', 7), None, None, spectrum.PeptideFragmentAnnotation(1, mz[9], 'b', 10) ] spec = spectrum.MsmsSpectrum('test_spectrum', 500, 2, mz.copy(), intensity, annotation.copy()) spec.round(1, 'max') np.testing.assert_array_equal(spec.annotation, [ None, None, None, spectrum.PeptideFragmentAnnotation(1, mz[5], 'b', 6), spectrum.PeptideFragmentAnnotation(1, mz[6], 'b', 7), None, spectrum.PeptideFragmentAnnotation(1, mz[9], 'b', 10) ])
def _get_sus_annotation(mz, annotation): """Get spectrum_utils.PeptideFragmentAnnotation objects.""" return [ sus.PeptideFragmentAnnotation(1, mz, annotation[0], annotation[1:]) for mz, annotation in zip(mz, annotation) ]