Introduction to msdxr

Andrey Samokhin, Roman Borisov

2026-04-09

Overview

The msdxr package provides tools to read GC-MS data stored in the Chromatec MSDX binary format and to extract commonly used analytical views of GC-MS data, including mass spectra, total ion chromatograms (TIC), and extracted ion chromatograms (XIC).

This vignette uses the example file distributed with the package. The file contains chromatographic peaks for two ketones:

msdx_file <- system.file("extdata", "ketones.msdx", package = "msdxr")

Read an MSDX file

GC-MS data stored in the Chromatec MSDX binary format can be read with the ReadMsdx() function.

gcms <- msdxr::ReadMsdx(msdx_file)

The returned object is a list of mass spectra, where each element corresponds to one scan. Each spectrum contains:

Instrument and scan settings can be accessed through the settings attribute, which is a named list of mass spectrometer settings and scan parameters. Typical entries may include ion source temperature, transfer line temperature, operation mode, scan mode, mass range, and scan time. Gas chromatography settings are absent from this list because they are not stored in MSDX files. Instrument settings are reported only when the MSDX file contains a single segment and a single scan event; for multi-condition files, some metadata fields may be omitted.

settings <- attr(gcms, "settings")
settings
#> $ion_source_temp
#> [1] 200
#> 
#> $transfer_line_temp
#> [1] 250
#> 
#> $operation_mode
#> [1] "segmented"
#> 
#> $n_segments
#> [1] 1
#> 
#> $scan_mode
#> [1] "scan"
#> 
#> $mass_range
#> [1]  10 700
#> 
#> $scan_time
#> [1] 0.2

Extract TIC and XIC

GC-MS data are stored as a list of mass spectra. Therefore, extracting TIC and XIC requires iterating over the elements of the list. To simplify this, the ExtractTic() and ExtractXic() functions can be used.

The following code extracts and plots a TIC segment from 420 to 480 s.

tic <- msdxr::ExtractTic(gcms, rt_range = c(420, 480))

old_par <- par(no.readonly = TRUE)
on.exit(par(old_par))
par(mar = c(4.1, 4.1, 1.1, 1.1), cex = 0.9)
plot(
  tic$rt,
  tic$tic,
  type = "l",
  xlab = "Retention time, s",
  ylab = "Intensity",
  bty = "l"
)
Figure 1. Total ion chromatogram from 420 to 480 s.

Figure 1. Total ion chromatogram from 420 to 480 s.

The molecular weights of the ketones from the example file are 184 and 198 Da, respectively. The following code extracts the corresponding XICs and plots them together with the trace for m/z 85, a fragment ion common to both compounds.

xic_85 <- msdxr::ExtractXic(gcms, mz = 85, rt_range = c(420, 480))
xic_184 <- msdxr::ExtractXic(gcms, mz = 184, rt_range = c(420, 480))
xic_198 <- msdxr::ExtractXic(gcms, mz = 198, rt_range = c(420, 480))

old_par <- par(no.readonly = TRUE)
on.exit(par(old_par))
par(mar = c(4.1, 4.1, 1.1, 1.1), cex = 0.9)
plot(
  xic_85$rt,
  xic_85$xic,
  type = "l",
  col = "gray",
  xlab = "Retention time, s",
  ylab = "Intensity",
  bty = "l"
)
lines(xic_184$rt, xic_184$xic, col = "blue3")
lines(xic_198$rt, xic_198$xic, col = "red3")
legend(
  "top",
  legend = paste0("m/z = ", c(85L, 184L, 198L)),
  col = c("gray", "blue3", "red3"),
  lty = 1
)
Figure 2. Extracted ion chromatograms for m/z 85, 184, and 198.

Figure 2. Extracted ion chromatograms for m/z 85, 184, and 198.

Extract a single mass spectrum

The simplest way to obtain a mass spectrum is to extract the corresponding list element with the standard [[]] accessor.

ms1 <- gcms[[32L]]

old_par <- par(no.readonly = TRUE)
on.exit(par(old_par))
par(mar = c(4.1, 4.1, 1.1, 1.1), cex = 0.9, yaxs = "i")
plot(
  ms1$mz,
  100 * ms1$intst / max(ms1$intst),
  type = "h",
  xlab = "m/z",
  ylab = "Relative intensity, %",
  bty = "l",
  col = "blue3",
  xlim = c(0, 200),
  yaxt = "n"
)
axis(side = 2, at = seq(0, 100, 20), las = 1)
Figure 3. Mass spectrum of 2-dodecanone (423.7 s, scan 32).

Figure 3. Mass spectrum of 2-dodecanone (423.7 s, scan 32).

Alternatively, the ExtractSpectrum() helper function can be used. A single mass spectrum can be extracted either by scan number or by retention time.

ms2 <- msdxr::ExtractSpectrum(gcms, rt = 467.0)

old_par <- par(no.readonly = TRUE)
on.exit(par(old_par))
par(mar = c(4.1, 4.1, 1.1, 1.1), cex = 0.9, yaxs = "i")
plot(
  ms2$mz,
  100 * ms2$intst / max(ms2$intst),
  type = "h",
  xlab = "m/z",
  ylab = "Relative intensity, %",
  bty = "l",
  col = "blue3",
  xlim = c(0, 200),
  yaxt = "n"
)
axis(side = 2, at = seq(0, 100, 20), las = 1)
Figure 4. Mass spectrum of 2-tridecanone (467.0 s, scan 234).

Figure 4. Mass spectrum of 2-tridecanone (467.0 s, scan 234).

Integrating into other workflows

The msdxr package can be integrated into other R workflows. To illustrate this, the example below performs a library search for the ketones present in the example file using the mssearchr package, which is available on CRAN. Mass spectra of several ketones retrieved from the MassBank database were used to build a reference library.

unknown_spectra <- lapply(c(423.7, 467.0), function(rt) {
  ms <- msdxr::ExtractSpectrum(gcms, rt = rt)
  list(
    name = sprintf("rt = %.1f s", ms$rt),
    mz = ms$mz,
    intst = ms$intst
  )
})
massbank <- mssearchr::ReadMsp("data/massbank_ketones.msp")

hitlists <- mssearchr::LibrarySearch(
  mssearchr::PreprocessMassSpectra(unknown_spectra),
  mssearchr::PreprocessMassSpectra(massbank),
  hitlist_columns = c("inchikey", "formula", "mw"),
  algorithm = "identity_normal"
)

# Hitlist for the mass spectrum at 423.7 s (scan 32)
head(hitlists[[1L]], n = 3L)
#>            name       mf idx                    inchikey formula  mw
#> 1  2-DODECANONE 896.2620   3 LSKONYYRONEBKA-UHFFFAOYSA-N C12H24O 184
#> 2  2-UNDECANONE 840.8009   2 KYWIYKKSMDLRDC-UHFFFAOYSA-N C11H22O 170
#> 3 2-TRIDECANONE 820.3274   4 CYIFVRUOHKNECG-UHFFFAOYSA-N C13H26O 198

# Hitlist for the mass spectrum at 467.0 s (scan 234)
head(hitlists[[2L]], n = 3L)
#>              name       mf idx                    inchikey formula  mw
#> 1   2-TRIDECANONE 900.8158   4 CYIFVRUOHKNECG-UHFFFAOYSA-N C13H26O 198
#> 2 2-TETRADECANONE 827.3805   5 POQLVOYRGNFGRM-UHFFFAOYSA-N C14H28O 212
#> 3    2-UNDECANONE 825.7356   2 KYWIYKKSMDLRDC-UHFFFAOYSA-N C11H22O 170

Built with msdxr_0.1.0.