def apply_my_filters(s): s = default_filters(s) s = add_parent_mass(s) s = normalize_intensities(s) s = select_by_relative_intensity(s, intensity_from=0.0, intensity_to=1.0) s = select_by_mz(s, mz_from=0, mz_to=1000) s = require_minimum_number_of_peaks(s, n_required=5) return s
def test_add_parent_mass_not_sufficient_data(caplog): """Test when there is not enough information to derive parent_mass.""" metadata = {"precursor_mz": 444.0} spectrum_in = SpectrumBuilder().with_metadata(metadata).build() spectrum = add_parent_mass(spectrum_in) assert spectrum.get("parent_mass") is None, "Expected no parent mass" assert "Not sufficient spectrum metadata to derive parent mass." in caplog.text
def test_use_of_ionmode(ionmode, expected): """Test when there is no charge given, than the ionmode is used to derive parent mass.""" metadata = {"precursor_mz": 444.0, "ionmode": ionmode} spectrum_in = SpectrumBuilder().with_metadata(metadata).build() spectrum = add_parent_mass(spectrum_in) assert spectrum.get("parent_mass") == expected, \ "Expected a different parent_mass"
def test_add_parent_mass(): """Test if parent mass is correctly derived.""" mz = numpy.array([], dtype='float') intensities = numpy.array([], dtype='float') metadata = {"pepmass": (444.0, 10), "charge": -1} spectrum_in = Spectrum(mz=mz, intensities=intensities, metadata=metadata) spectrum = add_parent_mass(spectrum_in) assert numpy.abs(spectrum.get("parent_mass") - 445.0) < .01, "Expected parent mass of about 445.0."
def test_add_parent_mass_using_adduct(adduct, expected): """Test if parent mass is correctly derived from adduct information.""" metadata = {"precursor_mz": 444.0, "adduct": adduct, "charge": +1} spectrum_in = SpectrumBuilder().with_metadata(metadata).build() spectrum = add_parent_mass(spectrum_in) assert numpy.allclose( spectrum.get("parent_mass"), expected, atol=1e-4), f"Expected parent mass of about {expected}." assert isinstance(spectrum.get("parent_mass"), float), "Expected parent mass to be float."
def test_add_parent_mass_precursormz(caplog): """Test if parent mass is correctly derived if "pepmass" is not present.""" metadata = {"precursor_mz": 444.0, "charge": -1} spectrum_in = SpectrumBuilder().with_metadata(metadata).build() spectrum = add_parent_mass(spectrum_in) assert numpy.abs(spectrum.get("parent_mass") - 445.0) < .01, "Expected parent mass of about 445.0." assert isinstance(spectrum.get("parent_mass"), float), "Expected parent mass to be float." assert "Not sufficient spectrum metadata to derive parent mass." not in caplog.text
def apply_my_filters(s): """This is how a user would typically design his own pre- and post- processing pipeline.""" s = default_filters(s) s = add_parent_mass(s) s = normalize_intensities(s) s = reduce_to_number_of_peaks(s, n_required=10, ratio_desired=0.5) s = select_by_mz(s, mz_from=0, mz_to=1000) s = add_losses(s, loss_mz_from=10.0, loss_mz_to=200.0) s = require_minimum_number_of_peaks(s, n_required=5) return s
def test_add_parent_mass_using_adduct(adduct, expected): """Test if parent mass is correctly derived from adduct information.""" mz = numpy.array([], dtype='float') intensities = numpy.array([], dtype='float') metadata = {"precursor_mz": 444.0, "adduct": adduct, "charge": +1} spectrum_in = Spectrum(mz=mz, intensities=intensities, metadata=metadata) spectrum = add_parent_mass(spectrum_in) assert numpy.allclose( spectrum.get("parent_mass"), expected, atol=1e-4), f"Expected parent mass of about {expected}."
def test_add_parent_mass_not_sufficient_data(capsys): """Test when there is not enough information to derive parent_mass.""" mz = numpy.array([], dtype='float') intensities = numpy.array([], dtype='float') metadata = {"precursor_mz": 444.0} spectrum_in = Spectrum(mz=mz, intensities=intensities, metadata=metadata) spectrum = add_parent_mass(spectrum_in) assert spectrum.get("parent_mass") is None, "Expected no parent mass" assert "Not sufficient spectrum metadata to derive parent mass." in capsys.readouterr( ).out
def test_add_parent_mass_no_pepmass(capsys): """Test if correct expection is returned.""" mz = numpy.array([], dtype='float') intensities = numpy.array([], dtype='float') metadata = {"charge": -1} spectrum_in = Spectrum(mz=mz, intensities=intensities, metadata=metadata) spectrum = add_parent_mass(spectrum_in) assert spectrum.get("parent_mass") is None, "Expected no parent mass" assert "Not sufficient spectrum metadata to derive parent mass." in capsys.readouterr( ).out
def test_add_parent_mass_overwrite(overwrite, expected): """Test if parent mass is replaced by newly calculated value.""" metadata = { "precursor_mz": 444.0, "parent_mass": 443.0, "adduct": "[M+H]+", "charge": +1 } spectrum_in = SpectrumBuilder().with_metadata(metadata).build() spectrum = add_parent_mass(spectrum_in, overwrite_existing_entry=overwrite) assert numpy.allclose(spectrum.get("parent_mass"), expected, atol=1e-4), \ "Expected parent mass to be replaced by new value."
def test_add_parent_mass(capsys): """Test if parent mass is correctly derived.""" mz = numpy.array([], dtype='float') intensities = numpy.array([], dtype='float') metadata = {"pepmass": (444.0, 10), "charge": -1} spectrum_in = Spectrum(mz=mz, intensities=intensities, metadata=metadata) spectrum = add_parent_mass(spectrum_in) assert numpy.abs(spectrum.get("parent_mass") - 445.0) < .01, "Expected parent mass of about 445.0." assert isinstance(spectrum.get("parent_mass"), float), "Expected parent mass to be float." assert "Not sufficient spectrum metadata to derive parent mass." not in capsys.readouterr( ).out
def test_add_parent_mass_overwrite(): """Test if parent mass is replaced by newly calculated value.""" mz = numpy.array([], dtype='float') intensities = numpy.array([], dtype='float') metadata = { "precursor_mz": 444.0, "parent_mass": 443.0, "adduct": "[M+H]+", "charge": +1 } spectrum_in = Spectrum(mz=mz, intensities=intensities, metadata=metadata) spectrum = add_parent_mass(spectrum_in, overwrite_existing_entry=True) assert numpy.allclose(spectrum.get("parent_mass"), 442.992724, atol=1e-4), \ "Expected parent mass to be replaced by new value."
def test_empty_spectrum(): spectrum_in = None spectrum = add_parent_mass(spectrum_in) assert spectrum is None, "Expected different handling of None spectrum."
def test_add_parent_mass_exceptions(metadata, expected, caplog): spectrum_in = SpectrumBuilder().with_metadata(metadata).build() spectrum = add_parent_mass(spectrum_in) assert spectrum.get("parent_mass") is None, "Expected no parent mass" assert expected in caplog.text