matchms.exporting package
Functions for exporting mass spectral data
Individual Spectrum(), or lists of Spectrum()
can be exported to json, mgf, or msp files.
- matchms.exporting.save_as_json(spectra: list[Spectrum], filename: str, export_style: str = 'matchms') None[source]
Save spectrum(s) as json file.
Example:
import numpy as np from matchms import Spectrum from matchms.exporting import save_as_json # 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_json(spectrum, "test.json")
- Parameters:
spectra – Expected input is a list of
Spectrumobjects.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”
- matchms.exporting.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”.
- matchms.exporting.save_as_msp(spectra: list[Spectrum], filename: str, write_peak_comments: bool = True, file_mode: str = 'a', style: str = 'matchms', peak_sep: str = '\t')[source]
Save spectrum(s) as msp file.
Example:
import numpy as np from matchms import Spectrum from matchms.exporting import save_as_msp # 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_msp(spectrum, "test.msp")
- Parameters:
spectra – Expected input are match.Spectrum.Spectrum() objects.
filename – Provide filename to save spectrum(s).
write_peak_comments – Writes peak comments to individual peaks after the respective mz/intensity pair when set to True. Default is True.
file_mode – Mode on how to write to file. One of [“w”, “a”] (write/append). Default is append.
style – Converts the keys to required Export style. One of [“massbank”, “nist”, “riken”, “gnps”]. Default is “matchms”
peak_sep – Separator to use for writing the msp file.
- matchms.exporting.save_as_mzspeclib(spectra: list[Spectrum], filename: str) None[source]
Save a list of spectra to a file in mzSpecLib format.
Parameters: spectra (List[Spectrum]): List of Spectrum objects to save. filename (str): The name of the file to save the spectra to.
- matchms.exporting.save_spectra(spectra: list[Spectrum], file: str, export_style: str = 'matchms', append: bool = False) None[source]
Saves spectra as the file type specified.
The following file extensions can be used: .json, .mgf, and .msp.
Args:
- spectra:
The spectra that are saved.
- file:
Path to file containing spectra, with file extension “.json”, “.mgf”, “.msp”.
- export_style:
Converts the keys to the required export style. One of [“matchms”, “massbank”, “nist”, “riken”, “gnps”]. Default is “matchms”.
- append:
Only supported for “.mgf”, and “.msp” filetypes. If True, will try to append to an existing file, instead of creating a new file. Default is False.