Repository: matchms/matchms
Pull request #309 (2022-01-21 15:56:40)

added minimum accepted m/z


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d68ca6e7..7a635b25 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Changed
+
+- `require_precursor_mz()` filter will now also discard nonsensical m/z values < 10.0 (value can be adapted by user) [#309](https://github.com/matchms/matchms/pull/309)
+
 ## [0.12.0] - 2022-01-18
 
 ### Added
diff --git a/matchms/filtering/require_precursor_mz.py b/matchms/filtering/require_precursor_mz.py
index 0cf49348..cf91c3f8 100644
--- a/matchms/filtering/require_precursor_mz.py
+++ b/matchms/filtering/require_precursor_mz.py
@@ -6,15 +6,18 @@ from ..typing import SpectrumType
 logger = logging.getLogger("matchms")
 
 
-def require_precursor_mz(spectrum_in: SpectrumType
+def require_precursor_mz(spectrum_in: SpectrumType,
+                         minimum_accepted_mz: float = 10.0
                          ) -> Union[SpectrumType, None]:
 
-    """Returns None if there is no precursor_mz or if <=0
+    """Returns None if there is no precursor_mz or if <= minimum_accepted_mz
 
     Parameters
     ----------
     spectrum_in:
         Input spectrum.
+    minimum_accepted_mz:
+        Set to minimum acceptable value for precursor m/z. Default is set to 10.0.
     """
     if spectrum_in is None:
         return None
@@ -32,7 +35,7 @@ def require_precursor_mz(spectrum_in: SpectrumType
     assert isinstance(precursor_mz, (float, int)), \
         ("Expected 'precursor_mz' to be a scalar number.",
          "Consider applying 'add_precursor_mz' filter first.")
-    if precursor_mz <= 0:
+    if precursor_mz <= minimum_accepted_mz:
         logger.info("Spectrum without precursor_mz was set to None.")
         return None
 


Test patch:
diff --git a/tests/test_require_precursor_mz.py b/tests/test_require_precursor_mz.py
index 52f2a2ea..7ac67f08 100644
--- a/tests/test_require_precursor_mz.py
+++ b/tests/test_require_precursor_mz.py
@@ -24,7 +24,7 @@ def test_require_precursor_mz(metadata, expected):
 
 def test_if_spectrum_is_cloned():
     """Test if filter is correctly cloning the input spectrum."""
-    spectrum_in = SpectrumBuilder().with_metadata({"precursor_mz": 1.}).build()
+    spectrum_in = SpectrumBuilder().with_metadata({"precursor_mz": 100.}).build()
 
     spectrum = require_precursor_mz(spectrum_in)
     spectrum.set("testfield", "test")
@@ -50,13 +50,14 @@ def test_require_precursor_mz_with_input_none():
     assert spectrum is None
 
 
-def test_require_precursor_mz_fail_because_zero():
-    """Test if spectrum is None when precursor_mz == 0"""
+@pytest.mark.parametrize("precursor_mz", [0, 10.0, -200])
+def test_require_precursor_mz_fail_when_mz_too_small(precursor_mz):
+    """Test if spectrum is None when precursor_mz <= minimum_accepted_mz"""
     set_matchms_logger_level("INFO")
     mz = numpy.array([10, 20, 30, 40], dtype="float")
     intensities = numpy.array([0, 1, 10, 100], dtype="float")
     spectrum_in = SpectrumBuilder().with_mz(mz).with_intensities(intensities).build()
-    spectrum_in.set("precursor_mz", 0.0)
+    spectrum_in.set("precursor_mz", precursor_mz)
 
     with LogCapture() as log:
         spectrum = require_precursor_mz(spectrum_in)