knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(chromConverter) library(ggplot2) library(data.table)
MS chromatograms are returned by default in long
format with three columns: retention time, m/z, and intensity.
As an example, we can load the 'Varian' SMS chromatogram included in the chromConverterExtraTests
package.
# download example file from the web path_sms <- tempfile(fileext = ".sms") download.file("https://raw.github.com/ethanbass/chromConverterExtraTests/master/inst/STRD15.SMS", destfile = path_sms) dat <- read_chroms(path_sms, format_in = "varian_sms", format_out = "data.frame")
x <- dat[[1]]$MS1 # derive TIC using aggregate tic <- aggregate(intensity ~ rt, data = x, FUN = sum) # plot TIC matplot(tic$rt, tic$intensity, type = 'l', ylab = "Total intensity", xlab = "Time (min)")
Here is a simple plot function you could use to plot mass spectra in base R:
plot_spec <- function(spec, lab_int=0.2, digits=1){ plot(spec, type = "h", xlab = "m/z", ylab = "Intensity") lab.idx <- which(spec$intensity > lab_int * max(spec$intensity)) text(spec$mz[lab.idx], spec$intensity[lab.idx], round(spec$mz[lab.idx], digits), offset = 0.25, pos = 3, cex = 0.5) }
You can extract mass spectra by filtering on time, e.g., to get the mass spectrum of the first scan, you could do:
times <- unique(x$rt) spec <- x[x$rt == times[100], -1] plot_spec(spec)
Plot TIC with dplyr:
tic <- x |> dplyr::group_by(rt) |> dplyr::summarize_at("intensity", sum) plot(intensity ~ rt, data=tic, type = 'l', ylab = "Total intensity", xlab = "Time (min)")
Plot spectrum with dplyr:
dplyr::filter(x, rt == 7.26355) |> dplyr::select(mz, intensity) |> plot_spec()
Convert to data.table
:
x <- data.table::as.data.table(x)
chromConverter can also return chromatograms in data.table format directly:
dat <- read_chroms(path_sms, format_in = "varian_sms", format_out = "data.table")
Extract the total ion chromatogram:
tic <- x[, .(intensity = sum(intensity)), by = rt] matplot(tic$rt, tic$intensity, type = 'l', ylab = "Total intensity", xlab = "Time (min)")
Extract the base ion chromatogram:
bpc <- x[, .(intensity = max(intensity)), by = rt] matplot(bpc$rt, bpc$intensity, type = 'l', ylab = "Base ion chromatogram", xlab = "Time (min)")
To obtain a mass spectrum we just filter by retention time as before:
plot_spec(x[rt == 7.26355, c('mz','intensity')])
library(ggplot2) ggplot(data = tic, aes(x=rt, y=intensity)) + geom_line() + xlab("Retention time (min)") + ylab("Intensity")
Plot mass spectrum with ggplot:
lab_int <- 0.2 digits <- 1 dplyr::filter(x, rt == 7.26355) |> dplyr::select(mz, intensity) |> ggplot(aes(x = mz, y = intensity)) + geom_segment(aes(xend = mz, yend = 0), linewidth = 0.5) + geom_text(data = subset(spec, intensity > lab_int * max(intensity)), aes(label = round(mz, digits)), vjust = -0.5, size = 2) + labs(x = "m/z", y = "Intensity") + theme_minimal()
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.