Repository: matchms/matchms

Issue:
Interpret pepmass doesn't handle strings
**Describe the bug**
Reading a spectrum with pepmass information fails upon metadata harmonization.

**To Reproduce**
Read the following spectrum:

```
PEPMASS: (981.54, None)
CHARGE: 1
MSLEVEL: 2
SOURCE_INSTRUMENT: LC-ESI-qTof
FILENAME: 130618_Ger_Jenia_WT-3-Des-MCLR_MH981.4-qb.1.1..mgf
SEQ: *..*
IONMODE: positive
ORGANISM: GNPS-LIBRARY
NAME: 3-Des-Microcystein_LR M+H
PI: Gerwick
DATACOLLECTOR: Jenia
SMILES: CC(C)CC1NC(=O)C(C)NC(=O)C(=C)N(C)C(=O)CCC(NC(=O)C(C)C(NC(=O)C(CCCNC(N)=N)NC(=O)C(C)C(NC1=O)C(O)=O)\C=C\C(\C)=C\C(C)C(O)Cc1ccccc1)C(O)=O
INCHI: InChI=1S/C48H72N10O12/c1-25(2)22-36-45(66)57-39(47(69)70)29(6)41(62)54-34(16-13-21-51-48(49)50)44(65)53-33(18-17-26(3)23-27(4)37(59)24-32-14-11-10-12-15-32)28(5)40(61)55-35(46(67)68)19-20-38(60)58(9)31(8)43(64)52-30(7)42(63)56-36/h10-12,14-15,17-18,23,25,27-30,33-37,39,59H,8,13,16,19-22,24H2,1-7,9H3,(H,52,64)(H,53,65)(H,54,62)(H,55,61)(H,56,63)(H,57,66)(H,67,68)(H,69,70)(H4,49,50,51)/b18-17+,26-23+
INCHIAUX: N/A
PUBMED: N/A
SUBMITUSER: mwang87
LIBRARYQUALITY: 1
SPECTRUMID: CCMSLIB00000001547
SCANS: 1
COMPOUND_NAME: 3-Des-Microcystein LR
ADDUCT: [M+H]+
PRECURSOR_MZ: 981.54
PARENT_MASS: [980.532724]
INCHIKEY: IYDKWWDUBYWQGF-NNAZGLEUSA-N
NUM PEAKS: 218
289.286377	8068.0
295.545288	22507.0
298.489624	3925.0
317.324951	18742.0
319.655945	8604.0
324.482422	8041.0
325.316284	9738.0
339.789429	16145.0
343.947021	18094.0
347.020508	13981.0
347.913391	6765.0
361.147705	11763.0
361.84436	24296.0
364.232727	2346.0
364.858154	10782.0
365.845886	10242.0
368.22168	12761.0
368.965698	19147.0
375.069519	15644.0
375.751953	25393.0
382.750549	12765.0
384.197083	17912.0
390.574219	7993.0
394.049194	16135.0
397.106262	13986.0
404.420715	12326.0
411.092712	2348.0
413.784546	8715.0
427.667358	68137.0
436.192749	14879.0
443.266113	44427.0
446.267273	23472.0
447.747498	29292.0
455.25	70939.0
456.107544	105392.0
456.822144	3.0
457.543213	12862.0
464.285461	8617.0
469.872314	87594.0
471.062195	31482.0
475.257324	15449.0
476.143616	23143.0
476.975159	28430.0
478.891113	27890.0
479.975952	33235.0
483.242432	13564.0
487.210388	32885.0
488.160156	20786.0
491.191956	55073.0
494.279602	7435.0
495.653992	32208.0
498.412964	11684.0
503.028198	30643.0
503.699951	2.0
504.344543	36421.0
505.154541	9667.0
510.176514	38891.0
512.168701	10175.0
513.265381	16524.0
514.957397	11384.0
515.922852	78764.0
520.973389	28857.0
521.82373	5810.0
523.168945	58926.0
529.036865	20722.0
530.991211	31845.0
532.376709	3005.0
534.575195	12906.0
538.003174	220949.0
539.217773	272296.0
540.672852	43876.0
548.061401	13655.0
554.117432	76225.0
556.030396	214421.0
557.288818	52970.0
557.996094	6202.0
559.942261	18112.0
561.328735	14656.0
564.123047	25971.0
564.94873	34630.0
566.439941	35564.0
571.33374	61305.0
572.047363	17235.0
575.219238	42127.0
575.888916	6.0
577.102905	29550.0
579.645874	7151.0
580.942627	17609.0
582.110107	102075.0
583.458984	10113.0
585.237061	36774.0
598.172485	26085.0
599.352783	764523.0
600.382812	114267.0
601.06665	4.0
602.267578	27144.0
609.302002	10247.0
613.415771	8621.0
622.208984	23787.0
623.023193	63940.0
623.991455	19154.0
625.216187	23050.0
638.299561	12481.0
640.265625	17392.0
641.235107	65873.0
646.095947	8409.0
649.2771	5446.0
651.526611	17521.0
657.128906	12911.0
658.094971	14824.0
659.420898	41969.0
663.387695	18284.0
668.33252	65700.0
669.357178	5671.0
680.219727	44374.0
681.987793	24446.0
685.957764	19166.0
691.648682	29177.0
693.22583	33545.0
694.307861	22539.0
696.332397	121211.0
697.127808	9503.0
709.464478	20171.0
710.79541	22346.0
711.744873	32675.0
714.071777	50487.0
715.578979	54567.0
716.216553	8.0
723.267822	14415.0
724.081909	88510.0
725.488892	8470.0
728.352051	21518.0
735.806396	52022.0
738.34668	2697.0
744.365234	16205.0
747.456055	19268.0
753.27124	14114.0
761.609131	12373.0
764.462158	19876.0
765.280029	18361.0
769.275635	41999.0
770.328613	16548.0
771.386353	13776.0
787.432617	33003.0
796.139526	9637.0
797.232788	11322.0
806.556885	18639.0
808.442383	6355.0
811.637329	14687.0
812.300049	9904.0
813.149292	15959.0
817.219238	7640.0
820.274048	6246.0
821.288208	15591.0
823.361328	13693.0
824.618286	6895.0
828.517456	32132.0
830.409302	102583.0
831.306763	65294.0
832.105469	9727.0
833.18457	4115.0
835.212891	7606.0
836.078247	8740.0
838.518677	26160.0
839.455811	72006.0
845.612915	21577.0
847.433594	196462.0
848.125854	39637.0
851.380859	246170.0
852.370605	276882.0
853.270508	44216.0
865.5979	44697.0
866.295654	111012.0
867.19043	4120.0
868.372192	78023.0
869.331177	8584.0
871.557007	5374.0
877.137451	30131.0
880.216553	5692.0
883.442261	49241.0
884.216553	8.0
888.167114	41037.0
889.282959	24795.0
892.127808	14925.0
893.467896	23506.0
895.607422	13123.0
899.010132	28633.0
901.351196	13472.0
902.325562	3774.0
909.424438	244136.0
910.515381	43770.0
911.526367	15208.0
914.30896	6532.0
915.217773	28455.0
918.666138	5610.0
919.39624	85829.0
920.06665	3.0
921.123901	16163.0
922.205688	37863.0
925.063721	43395.0
931.132812	61732.0
932.351929	136657.0
933.524048	25202.0
935.493286	33896.0
936.552002	103130.0
937.588623	67605.0
938.471069	35379.0
939.61792	77289.0
946.257812	38584.0
949.370239	85420.0
950.284546	5976.0
951.551758	29995.0
953.396606	545281.0
954.491577	123937.0
963.686768	261578.0
964.524658	318164.0
965.192139	124405.0
982.221924	27147.0
```

**Expected behavior**
The spectrum should be read normally

**Additional context**
Observed first in version 0.22.0


Comments:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8abb2a16..e51ca0b3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - The file structure of metadata_utils was refactored [#503](https://github.com/matchms/matchms/pull/503)
 - interpret_pepmass now removes the pepmass field after entering precursor_mz [#533](https://github.com/matchms/matchms/pull/533)
 - Filters that did not have any effect are also mentioned in processing report [#530](https://github.com/matchms/matchms/pull/530)
+- Added regex to pepmass reading to properly interpret string representations [#539](https://github.com/matchms/matchms/pull/539)
 
 
 ### Fixed
diff --git a/matchms/filtering/metadata_processing/interpret_pepmass.py b/matchms/filtering/metadata_processing/interpret_pepmass.py
index 0e5ef752..7eec4284 100644
--- a/matchms/filtering/metadata_processing/interpret_pepmass.py
+++ b/matchms/filtering/metadata_processing/interpret_pepmass.py
@@ -1,4 +1,5 @@
 import logging
+import re
 import numpy as np
 from .make_charge_int import _convert_charge_to_int
 
@@ -65,6 +66,11 @@ def _interpret_pepmass_metadata(metadata):
 
 def _get_mz_intensity_charge(pepmass):
     try:
+        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(",")
         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 c02f3786..388d534a 100644
--- a/tests/filtering/test_interpret_pepmass.py
+++ b/tests/filtering/test_interpret_pepmass.py
@@ -84,3 +84,15 @@ def test_empty_spectrum():
     spectrum = interpret_pepmass(spectrum_in)
 
     assert spectrum is None, "Expected different handling of None spectrum."
+
+
+@pytest.mark.parametrize("input_pepmass, expected_results", [
+ ["(981.54, None)", (981.54, None, None)],
+ ["(981.54, 44, -2)", (981.54, 44, -2)],
+])
+def test_interpret_pepmass_error_v0_22_0(input_pepmass, expected_results):
+    spectrum = SpectrumBuilder().with_metadata({"PEPMASS": input_pepmass}, metadata_harmonization=True).build()
+
+    assert spectrum.get("precursor_mz") == expected_results[0], "Expected different precursor_mz."
+    assert spectrum.get("precursor_intensity") == expected_results[1], "Expected different precursor_intensity."
+    assert spectrum.get("charge") == expected_results[2], "Expected different charge."