Skip to main content
Topic: Question regarding the plot-methods {MSnbase}, type = "XIC" (Read 4048 times) previous topic - next topic

Question regarding the plot-methods {MSnbase}, type = "XIC"

Dear Maintainers,

after having get acquainted with xcms, i.e. rather MSnbase, here specifically with regard to the wonderful plotting functions (plot-methods {MSnbase}) available in MSnbase, a question came up, which I am not able to solve by myself...

More specifically, I am referring to this adopted demo code available from:

plot-methods {MSnbase} - R Documentation
Plotting 'MSnExp' and 'Spectrum' object(s)

I am using R.3.5.3 under Windows7 with packageVersion("MSnbase") ‘2.8.3’ and packageVersion("xcms") ‘3.4.4’.

Here is the demo code (last 2 lines adopted):

## Load profile-mode LC-MS files
library(msdata)
od <- readMSData(dir(system.file("sciex", package = "msdata"),
                     full.names = TRUE), mode = "onDisk")
## Restrict the MS data to signal for serine
serine <- filterMz(filterRt(od, rt = c(175, 190)), mz = c(106.04, 106.06))
plot(serine, type = "XIC")
abline(v = 181.0, col = "red", lty = 2)
abline(h = 106.05, col = "red", lty = 2)

The question is: why are the two lines (vertival and horizontal) only drawn for the last sample and not for all samples?
I think this is related to the underling layout function used in this plot, i.e. plot(..., type = "XIC"), but I am not able to solve the problem by myself...

thanks
Tony

Re: Question regarding the plot-methods {MSnbase}, type = "XIC"

Reply #1
Hi Tony,

There are potentially a few different ways of getting around this kind of problem. But I can't quite think of anything elegant...
At first, I was hoping that you could pass 'panel.last=c(abline(v = 181.0, col = "red", lty = 2),abline(h = 106.05, col = "red", lty = 2))' as another parameter, but that doesn't work.

As a pretty poor work around, I've pulled out the code from the MSnbase github page and edited the plotting functions.
Code: [Select]
plotXIC_MSnExp_ex <- function(x, ...) {
  ## Restrict to MS level 1
  x <- filterMsLevel(x, 1L)
  if (!length(x))
    stop("No MS1 data available")
  fns <- basename(fileNames(x))
  if (isMSnbaseVerbose())
    message("Retrieving data ...", appendLF = FALSE)
  x <- as(x, "data.frame")
  x <- split(x, x$file)
  if (isMSnbaseVerbose())
    message("OK")
  ## Check if we are greedy and plot a too large area
  if (any(unlist(lapply(x, nrow)) > 20000))
    warning("The MS area to be plotted seems rather large. It is suggested",
            " to restrict the data first using 'filterRt' and 'filterMz'. ",
            "See also ?chromatogram and ?Chromatogram for more efficient ",
            "functions to plot a total ion chromatogram or base peak ",
            "chromatogram.",
            immediate = TRUE, call = FALSE)
  ## Define the layout.
  dots <- list(...)
  if (any(names(dots) == "layout")) {
    if (!is.null(dots$layout))
      layout(layout)
    dots$layout <- NULL
  } else
    layout(.vertical_sub_layout_ex(length(x)))
  tmp <- mapply(x, fns, FUN = function(z, main, ...) {
    .plotXIC_ex(x = z, main = main, layout = NULL, ...)
  }, MoreArgs = dots)
}


.plotXIC_ex <- function(x, main = "", col = "grey", colramp = topo.colors,
                     grid.color = "lightgrey", pch = 21,
                     layout = matrix(1:2, ncol = 1), ...) {
  # start edit
  plot_strip<-function(..., v, h) plot(...)
  dots <- list(...)
  # end edit
 
  print(list(...))
  if (is.matrix(layout))
    layout(layout)
  ## Chromatogram.
  bpi <- unlist(lapply(split(x$i, x$rt), max, na.rm = TRUE))
  brks <- lattice::do.breaks(range(x$i), nint = 256)
  par(mar = c(0, 4, 2, 1))
 
  # start edit
  plot_strip(as.numeric(names(bpi)), bpi, xaxt = "n", col = col, main = main,
       bg = lattice::level.colors(bpi, at = brks, col.regions = colramp), xlab = "",
       pch = pch, ylab = "", las = 2, ...)
  # end edit
 
  if(!is.null(dots$v)) abline(v=dots$v,col='red',lty=2)
 
  mtext(side = 4, line = 0, "Intensity", cex = par("cex.lab"))
  grid(col = grid.color)
  par(mar = c(3.5, 4, 0, 1))
 
  # start edit
  plot_strip(x$rt, x$mz, main = "", pch = pch, col = col, xlab = "", ylab = "",
       yaxt = "n", bg = lattice::level.colors(x$i, at = brks, col.regions = colramp),
       ...)
 
  if(!is.null(dots$h)) abline(h=dots$h,col='red',lty=2)
  if(!is.null(dots$v)) abline(v=dots$v,col='red',lty=2)
  # end edit
 
  axis(side = 2, las = 2)
  grid(col = grid.color)
  mtext(side = 1, line = 2.5, "Retention time", cex = par("cex.lab"))
  mtext(side = 4, line = 0, "m/z", cex = par("cex.lab"))
}

.vertical_sub_layout_ex <- function(x, sub_plot = 2) {
  sqrt_x <- sqrt(x)
  ncol <- ceiling(sqrt_x)
  nrow <- round(sqrt_x)
  rws <- split(1:(ncol * nrow * sub_plot), f = rep(1:nrow,
                                                   each = sub_plot * ncol))
  do.call(rbind, lapply(rws, matrix, ncol = ncol))
}

If you run these functions, then you can plot using 'plotXIC_MSnExp_ex(serine,v=181.0,h=106.05)'.
As you would expect, this isn't a great solution. But if it is particularly helpful, perhaps suggest it on the MSnBase github page and it might get implemented in a future release.

Of course, if anybody else has a better solution, please feel free to post it.

Cheers,
Corey

 

Re: Question regarding the plot-methods {MSnbase}, type = "XIC"

Reply #2
Hello Corey,

that's nice.

Thank you for your proposals and the code adaptations.

I will also follow your suggestion to make a proposal/wish on the MSnBase github page.

kind regards
Tony