Repository: matchms/matchms

Issue:
Pepmass string format breaks code
In #551 @kozo2 mentioned a second bug with MSP files. 
His message:

In version 0.24.0, I still encountered an IndexError as shown below.

```
WARNING:pickydict:Key 'exact_mass' will be interpreted as 'parent_mass'. But this entry already exists. Please use 'parent_mass' if you want to replace the entry.
2023-11-27 09:50:37,482:WARNING:matchms:add_precursor_mz:No precursor_mz found in metadata.
WARNING:matchms:No precursor_mz found in metadata.
WARNING:pickydict:Key 'exact_mass' will be interpreted as 'parent_mass'. But this entry already exists. Please use 'parent_mass' if you want to replace the entry.
2023-11-27 09:50:37,489:WARNING:matchms:add_precursor_mz:No precursor_mz found in metadata.
WARNING:matchms:No precursor_mz found in metadata.
WARNING:pickydict:Key 'exact_mass' will be interpreted as 'parent_mass'. But this entry already exists. Please use 'parent_mass' if you want to replace the entry.
2023-11-27 09:50:37,499:WARNING:matchms:add_precursor_mz:No precursor_mz found in metadata.
WARNING:matchms:No precursor_mz found in metadata.
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
[<ipython-input-4-5079fe4afd5e>](https://localhost:8080/#) in <cell line: 1>()
----> 1 spectrums = list(tmp)

4 frames
[/usr/local/lib/python3.10/dist-packages/matchms/filtering/metadata_processing/interpret_pepmass.py](https://localhost:8080/#) in _get_mz_intensity_charge(pepmass)
     71             if len(matches) > 1:
     72                 raise ValueError("Found more than one tuple in pepmass field.")
---> 73             pepmass = matches[0].split(",")
     74         length = len(pepmass)
     75         values = [None, None, None]

IndexError: list index out of range
```

This can be reproduced with the following Google Colab notebook.

https://colab.research.google.com/drive/1wlBfQmOD6lw23NWeTvPQrq9fLT1DvjxQ?usp=sharing

Let me know if there is anything you have in mind.

Discussion:

Patch:
diff --git a/matchms/filtering/metadata_processing/interpret_pepmass.py b/matchms/filtering/metadata_processing/interpret_pepmass.py
index 7eec4284..cffe96a1 100644
--- a/matchms/filtering/metadata_processing/interpret_pepmass.py
+++ b/matchms/filtering/metadata_processing/interpret_pepmass.py
@@ -69,8 +69,14 @@ def _get_mz_intensity_charge(pepmass):
         if isinstance(pepmass, str):
             matches = re.findall(r'\(([^)]+)\)', pepmass)
             if len(matches) > 1:
-                raise ValueError("Found more than one tuple in pepmass field.")            
-            pepmass = matches[0].split(",")
+                raise ValueError("Found more than one tuple in pepmass field.")
+            if len(matches) == 1:
+                pepmass = matches[0].split(",")
+            if len(matches) == 0:
+                try:
+                    pepmass = float(pepmass)
+                except ValueError:
+                    return None, None, None 
         length = len(pepmass)
         values = [None, None, None]
         for i in range(length):


Test patch:
diff --git a/tests/filtering/test_interpret_pepmass.py b/tests/filtering/test_interpret_pepmass.py
index 8dece25c..0afeb8d6 100644
--- a/tests/filtering/test_interpret_pepmass.py
+++ b/tests/filtering/test_interpret_pepmass.py
@@ -87,8 +87,10 @@ def test_empty_spectrum():
 
 
 @pytest.mark.parametrize("input_pepmass, expected_results", [
- ["(981.54, None)", (981.54, None, None)],
- ["(981.54, 44, -2)", (981.54, 44, -2)],
+    ["(981.54, None)", (981.54, None, None)],
+    ["(981.54, 44, -2)", (981.54, 44, -2)],
+    ["100.2", (100.2, None, None)],
+    ["something_random", (None, None, None)]
 ])
 def test_interpret_pepmass_error_v0_22_0(input_pepmass, expected_results):
     spectrum = SpectrumBuilder().with_metadata({"PEPMASS": input_pepmass}, metadata_harmonization=True).build()
@@ -129,9 +131,9 @@ def test_load_pepmass_error_issue_452():
         "ExactMass": "504.169034944",
         "Comments": comment
     }
-    
+
     mz = np.array([387.3, 387.4, 505, 505.1, 505.2], dtype=np.float64)
     ints = np.array([50, 0, 0, 50, 100], dtype=np.float64)
 
     spectrum = SpectrumBuilder().with_mz(mz).with_intensities(ints).with_metadata(metadata, metadata_harmonization=True).build()
-    assert spectrum is not None
\ No newline at end of file
+    assert spectrum is not None