matchms.exporting.save_as_mgf module

matchms.exporting.save_as_mgf.save_as_mgf(spectra: list[Spectrum] | Spectrum, filename: str, export_style: str = 'matchms', file_mode: str = 'a') None[source]

Save spectrum(s) as mgf file.

Example:

import numpy as np
from matchms import Spectrum
from matchms.exporting import save_as_mgf

# Create dummy spectrum
spectrum = Spectrum(
    mz=np.array([100, 200, 300], dtype="float"),
    intensities=np.array([10, 10, 500], dtype="float"),
    metadata={"charge": -1, "inchi": '"InChI=1S/C6H12"', "precursor_mz": 222.2},
)

# Write spectrum to test file
save_as_mgf(spectrum, "test.mgf")
Parameters:
  • spectra – Expected input are match.Spectrum.Spectrum() objects.

  • filename – Provide filename to save spectrum(s).

  • export_style – Converts the keys to the required export style. One of [“matchms”, “massbank”, “nist”, “riken”, “gnps”]. Default is “matchms”

  • file_mode (str) – This is an optional parameter which defines the mode the file will be opened in. Default is “a” (append). The other option is “w” (write). If set worngly it will default to “a”.