Mass Spectrometry Imaging#

pyOpenMS reads and writes imzML, the standard format for mass spectrometry imaging data. An imzML data set consists of two files with the same name: the .imzML file holds the metadata and the position of every pixel, the .ibd file the peak data. Keep both in the same directory.

Loading an imzML file#

ImzMLFile loads a data set into an MSImagingExperiment, which holds one spectrum per pixel and the geometry of the image. FileHandler does not read imzML.

The geometry is two-dimensional and covers the first plane (z = 1) of a data set. Spectra of other planes are loaded too, and getMSExperiment() returns them, but pixel access, regions and ion images leave them out.

 1import pyopenms as oms
 2from urllib.request import urlretrieve
 3
 4url = "https://raw.githubusercontent.com/OpenMS/OpenMS/develop/src/tests/class_tests/openms/data/"
 5for ext in (".imzML", ".ibd"):
 6    urlretrieve(url + "ImzMLFile_1_Example_Continuous" + ext, "example" + ext)
 7
 8img = oms.MSImagingExperiment()
 9oms.ImzMLFile().load("example.imzML", img)
10geometry = img.getGeometry()
11print(geometry.getWidth(), geometry.getHeight(), img.getNumberOfSpectra())
3 3 9

Pixel coordinates start at 0. The spectrum of the top-left pixel is:

1spectrum = img.getSpectrum(0, 0)
2print(spectrum.size())
8399

Ion images#

extractIonImage sums, for every pixel, the intensities within a tolerance (in ppm) around an m/z value. The result is an IonImage, whose get_data() returns the image as a numpy array of shape (height, width), for example to show it with matplotlib’s imshow:

1ion = img.extractIonImage(450.0, 10.0)
2data = ion.get_data()
3print(data.shape)
(3, 3)

A region limits the image to part of the pixels. Regions belong to the geometry, so add them to a copy of it and set the copy:

1left = oms.MSImagingRegion.rectangle(1, "left", 0, 0, 1, 2)  # id, name, min x, min y, max x, max y
2geometry.addRegion(left)
3img.setGeometry(geometry)
4ion_left = img.extractIonImage(450.0, 10.0, 1)
5print(ion_left.get_mask().sum())
6

get_mask() tells which pixels of the image belong to the region.

Large data sets#

An OnDiscImzMLExperiment keeps the peak data in the .ibd file and reads a spectrum only when it is needed, so it can open data sets larger than the memory. It extracts ion images the same way:

1on_disc = oms.OnDiscImzMLExperiment()
2on_disc.open("example.imzML")
3ion = on_disc.extractIonImage(450.0, 10.0)
4spectrum = on_disc.getSpectrumAtCoord(1, 1)

getSpectrumAtCoord takes the coordinates as imzML stores them, starting at 1, so (1, 1) is the pixel that MSImagingExperiment calls (0, 0).

Like MSImagingExperiment, it maps only the plane z = 1 to pixels: getSpectrumAtCoord raises an error for other values of z, and extractIonImage leaves those spectra out. getSpectrum(i) reads any spectrum by its index, and getIndex(i) returns its coordinates (x, y and z) without reading the peaks.

Writing imzML#

ImzMLFile().store() writes an MSImagingExperiment as a pair of .imzML and .ibd files:

1oms.ImzMLFile().store("copy.imzML", img)

The TOPP tools and TOPPView do not read imzML files yet.