Repository: matchms/matchms
Pull request #304 (2022-01-17 09:17:17)

Expand import export testing
- add save -> load test for mgf
- fix bug with Metadata charge harminzation


Patch:
diff --git a/matchms/Metadata.py b/matchms/Metadata.py
index b0713a3e..b0e15a8b 100644
--- a/matchms/Metadata.py
+++ b/matchms/Metadata.py
@@ -60,9 +60,8 @@ class Metadata:
             self._data["ionmode"] = "n/a"
         self._data = _add_precursor_mz_metadata(self._data)
         charge = self.get("charge")
-        if not isinstance(charge, int):
-            if not _convert_charge_to_int(charge) is None:
-                self._data["charge"] = charge
+        if not isinstance(charge, int) and not _convert_charge_to_int(charge) is None:
+            self._data["charge"] = _convert_charge_to_int(charge)
 
     # ------------------------------
     # Getters and Setters


Test patch:
diff --git a/tests/test_metadata.py b/tests/test_metadata.py
index a03b5973..ef2199e5 100644
--- a/tests/test_metadata.py
+++ b/tests/test_metadata.py
@@ -9,6 +9,9 @@ from matchms import Metadata
     [{"precursor_mz": 101.01}, True, {"ionmode": "n/a", "precursor_mz": 101.01}],
     [{"precursormz": 101.01}, True, {"ionmode": "n/a", "precursor_mz": 101.01}],
     [{"precursormz": 101.01}, False, {"precursormz": 101.01}],
+    [{"charge": "2+"}, True, {"charge": 2, "ionmode": "n/a"}],
+    [{"charge": -1}, True, {"charge": -1, "ionmode": "n/a"}],
+    [{"charge": [-1, 0]}, True, {"charge": -1, "ionmode": "n/a"}],
     [{"ionmode": "Negative"}, True, {"ionmode": "negative"}]])
 def test_metadata_init(input_dict, harmonize, expected):
     metadata = Metadata(input_dict, harmonize_defaults=harmonize)
diff --git a/tests/test_save_as_mgf.py b/tests/test_save_as_mgf.py
index 5a405495..db2fc1b4 100644
--- a/tests/test_save_as_mgf.py
+++ b/tests/test_save_as_mgf.py
@@ -1,7 +1,9 @@
 import os
 import tempfile
 import numpy
+import pytest
 from matchms.exporting import save_as_mgf
+from matchms.importing import load_from_mgf
 from .builder_Spectrum import SpectrumBuilder
 
 
@@ -54,3 +56,34 @@ def test_save_as_mgf_spectrum_list():
         assert mgf_content[5] == mgf_content[12] == "END IONS\n"
         assert mgf_content[1].split("=")[1] == "test1\n"
         assert mgf_content[8].split("=")[1] == "test2\n"
+
+
+@pytest.mark.parametrize("charge, ionmode, parent_mass",
+                         [(-1, "negative", 218.5),
+                          (2, "positive", "n/a"),
+                          (None, "n/a", 250)])
+def test_save_load_mgf_consistency(tmpdir, charge, ionmode, parent_mass):
+    """Test saving and loading spectrum to .mgf file"""
+    mz = numpy.array([100.1, 200.02, 300.003], dtype="float")
+    intensities = numpy.array([0.01, 0.02, 1.0], dtype="float")
+    metadata = {"precursor_mz": 200.5,
+                "charge": charge,
+                "ionmode": ionmode,
+                "parent_mass": parent_mass}
+    builder = SpectrumBuilder().with_mz(mz).with_intensities(intensities)
+    spectrum1 = builder.with_metadata(metadata).build(harmonize_defaults=True)
+    spectrum2 = builder.with_metadata(metadata).build(harmonize_defaults=True)
+
+    # Write to test file
+    filename = os.path.join(tmpdir, "test.mgf")
+    save_as_mgf([spectrum1, spectrum2], filename)
+
+    # Test if file exists
+    assert os.path.isfile(filename)
+
+    # Test importing spectra again
+    spectrum_imports = list(load_from_mgf(filename))
+    assert spectrum_imports[0].get("precursor_mz") == 200.5
+    assert spectrum_imports[0].get("charge") == charge
+    assert spectrum_imports[0].get("ionmode") == ionmode
+    assert spectrum_imports[0].get("parent_mass") == str(parent_mass)