def test_remove_peaks_around_precursor_mz_no_params(): """Using defaults with precursor mz present.""" mz = numpy.array([10, 20, 30, 40], dtype="float") intensities = numpy.array([0, 1, 10, 100], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum_in.set("precursor_mz", 60.) spectrum = remove_peaks_around_precursor_mz(spectrum_in) assert spectrum == spectrum_in, "Expected no changes."
def test_require_precursor_mz_pass(): """Test with correct precursor mz present.""" mz = numpy.array([10, 20, 30, 40], dtype="float") intensities = numpy.array([0, 1, 10, 100], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum_in.set("precursor_mz", 60.) spectrum = require_precursor_mz(spectrum_in) assert spectrum == spectrum_in, "Expected no changes."
def test_require_precursor_mz_fail_because_below_zero(): """Test if spectrum is None when precursor_mz < 0""" mz = numpy.array([10, 20, 30, 40], dtype="float") intensities = numpy.array([0, 1, 10, 100], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum_in.set("precursor_mz", -3.5) spectrum = require_precursor_mz(spectrum_in) assert spectrum is None, "Expected spectrum to be None."
def _assign_ri_value(spectrum: Spectrum, value: Data.RetentionIndexType): """Assign RI value to Spectrum object Args: spectrum (Spectrum): Spectrum to add RI to value (Data.RetentionIndexType): RI to be added to Spectrum """ if value is not None: retention_index = ('%f' % float(value)).rstrip('0').rstrip('.') spectrum.set(key='retentionindex', value=retention_index)
def test_if_spectrum_is_cloned(): """Test if filter is correctly cloning the input spectrum.""" mz = numpy.array([], dtype="float") intensities = numpy.array([], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum_in.set("precursor_mz", 1.) spectrum = remove_peaks_around_precursor_mz(spectrum_in) spectrum.set("testfield", "test") assert not spectrum_in.get( "testfield"), "Expected input spectrum to remain unchanged."
def test_remove_peaks_around_precursor_mz_tolerance_20(): """Set mz_tolerance to 20.""" mz = numpy.array([10, 20, 30, 40], dtype="float") intensities = numpy.array([0, 1, 10, 100], dtype="float") spectrum_in = Spectrum(mz=mz, intensities=intensities) spectrum_in.set("precursor_mz", 60.) spectrum = remove_peaks_around_precursor_mz(spectrum_in, mz_tolerance=20) assert len(spectrum.peaks) == 3, "Expected 3 peaks to remain." assert spectrum.peaks.mz.tolist() == [ 10., 20., 30. ], "Expected different peaks to remain."
def naive_decoys(spectra, initial_fragments, fragments, no_peaks_for_spectra): decoy_spectra = [] no_fragments = len(fragments) for i in range(len(spectra)): spectrum = spectra[i] decoy_intensities = [] decoy_mzs = [] #Adding initial (precursor) peak. initial_fragment = initial_fragments[i, :] decoy_intensities.append(initial_fragment[1]) decoy_mzs.append(initial_fragment[0]) #Add remaining fragment ions randomly desired_no_peaks = no_peaks_for_spectra[i] draws = np.random.randint(no_fragments, size=desired_no_peaks - 1) for draw in draws: fragment = fragments[draw, :] mz = fragment[0] if mz_check(mz, decoy_mzs, spectrum, 5): decoy_intensities.append(fragment[1]) decoy_mzs.append(mz) order = np.argsort(decoy_mzs) decoy_spectrum = Spectrum( intensities=(np.array(decoy_intensities))[order], mz=(np.array((decoy_mzs)))[order]) parentmass = spectrum.get("parent_mass") if spectrum.get( "parent_mass") else get_parent_mass(spectrum) decoy_spectrum.set("parent_mass", parentmass) decoy_spectra.append(decoy_spectrum) return decoy_spectra
def spectrum_based_decoys(spectra, initial_fragments, fragments, sorted_fragments, first_fragments, no_peaks_for_spectra): decoy_spectra = [] fragment_mzs = sorted_fragments[:, 0] for i in range(len(spectra)): spectrum = spectra[i] spectrum_peaks = np.arange(first_fragments[i], first_fragments[i + 1]) desired_no_peaks = no_peaks_for_spectra[i] decoy_intensities = [] decoy_mzs = [] #Adding initial (precursor) peak. initial_fragment = initial_fragments[i, :] decoy_intensities.append(initial_fragment[1]) decoy_mzs.append(initial_fragment[0]) #Adding peaks based on peaks already added to the decoy spectrum. fragment_candidates = [] j = 1 while j != desired_no_peaks: #Find fragments within mz range mz = decoy_mzs[j - 1] tolerance = (mz * 5) / (10**6) lower_mz = mz - tolerance upper_mz = mz + tolerance lower_index = np.searchsorted(fragment_mzs, lower_mz, side="left") upper_index = np.searchsorted(fragment_mzs, upper_mz, side="right") indices = np.arange(lower_index, upper_index) #Adding 5 or less fragments ions to fragment candidates #Fragments are stored by their indices in fragments #First get spectra which have previously added fragment within our p.p.m. tolerance (5) #Then randomly add 5 fragments from these spectra #When there are less than 5 suitable spectra, randomly add fragments from suitable spectra suitable_spectra = np.unique( sorted_fragments[indices, 2]) #Get unique suitable spectra indices if len(suitable_spectra) >= 5: spectra_draws = np.random.choice(suitable_spectra, 5, replace=False) for index in spectra_draws: index = int(index) peak_draw = np.random.choice(no_peaks_for_spectra[index]) fragment_draw = first_fragments[index] + peak_draw fragment_candidates.append(fragment_draw) else: spectra_draws = np.random.choice( suitable_spectra, 5, replace=True) #Note that each spectra must have >=5 peaks for index in spectra_draws: index = int(index) peak_draw = np.random.choice(no_peaks_for_spectra[index]) fragment_draw = first_fragments[index] + peak_draw fragment_candidates.append(fragment_draw) #Add fragment from fragment candidates for decoy spectrum. #Only add fragment if it is not within 5 p.p.m. of previously added fragments #and not greater than the precursor mz of the spectrum being decoyed. #If chosen fragment is not suitable, remove fragment from fragment_candidates_clone #and search again. search_attempts = 0 fragment_found = False no_candidates = len(fragment_candidates) fragment_candidates_clone = fragment_candidates while search_attempts != no_candidates and not fragment_found: choice = np.random.choice(fragment_candidates_clone) fragment_candidate = fragments[choice, :] candidate_mz = fragment_candidate[0] if mz_check(candidate_mz, decoy_mzs, spectrum, 5): decoy_intensities.append(fragment_candidate[1]) decoy_mzs.append(candidate_mz) fragment_found = True j += 1 else: fragment_candidates_clone.remove(choice) search_attempts += 1 #If fragment search is over and a suitable fragment candidate isn't found, then #add random fragment from spectrum being decoyed if not fragment_found: choice = np.random.choice(spectrum_peaks) fragment_candidate = fragments[choice, :] decoy_intensities.append(fragment_candidate[1]) decoy_mzs.append(fragment_candidate[0]) j += 1 print( "Suitable fragment candidate not found. Random fragment from real spectrum added." ) order = np.argsort(decoy_mzs) decoy_spectrum = Spectrum( intensities=(np.array(decoy_intensities))[order], mz=(np.array((decoy_mzs)))[order]) parentmass = spectrum.get("parent_mass") if spectrum.get( "parent_mass") else get_parent_mass(spectrum) decoy_spectrum.set("parent_mass", parentmass) decoy_spectra.append(decoy_spectrum) return decoy_spectra