## ----biocstyle, echo = FALSE, results = "asis"--------------------------------
BiocStyle::markdown()

## ----init, message = FALSE, warning = FALSE, echo = FALSE, results = "hide"----
## Silently loading all packages
library(BiocStyle)
library(xcms)
library(lcmsPlot)

register(SerialParam())

## ----install-lcmsPlot, eval = FALSE-------------------------------------------
# if (!requireNamespace("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")
# BiocManager::install("lcmsPlot")

## ----load-packages, message = FALSE-------------------------------------------
library(xcms)
library(lcmsPlot)

## ----load-data, message = FALSE-----------------------------------------------
xdata <- loadXcmsData("xdata")

## ----sample-data--------------------------------------------------------------
Biobase::pData(xdata)

## ----plot-bpc, fig.height=6, fig.width=10-------------------------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_chromatogram(aggregation_fun = "max") +
    lp_facets(facets = "sample_name", ncol = 4)

## ----plot-tic, fig.height=6, fig.width=10-------------------------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_chromatogram(aggregation_fun = "sum") +
    lp_facets(facets = "sample_name", ncol = 4)

## ----bpc-overlapped-----------------------------------------------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_chromatogram(aggregation_fun = "max") +
    lp_arrange(group_by = "sample_group") +
    lp_labels(title = "Base peak chromatograms", legend = "Sample group") +
    lp_legend(position = "bottom")

## ----total-ion-current--------------------------------------------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_total_ion_current(type = "violin") +
    lp_arrange(group_by = "sample_group") +
    lp_labels(title = "Total ion current", legend = "Sample group")

## ----plot-xics----------------------------------------------------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_chromatogram(features = rbind(c(
        mzmin = 334.9,
        mzmax = 335.1,
        rtmin = 2700,
        rtmax = 2900))) +
    lp_arrange(group_by = "sample_group") +
    lp_labels(legend = "Sample group")

## ----plot-xic-faceted, fig.height=5, fig.width=14-----------------------------
lcmsPlot(xdata, sample_id_column = 'sample_name') +
    lp_chromatogram(
        features = rbind(
            mz334 = c(
                mzmin = 334.9, mzmax = 335.1,
                rtmin = 2700, rtmax = 2900),
            mz278 = c(
                mzmin = 278.99721, mzmax = 279.00279,
                rtmin = 2740, rtmax = 2840)
        ),
        highlight_peaks = TRUE,
        highlight_peaks_color = '#f00') +
    lp_grid(rows = 'feature_id', cols = 'sample_id', free_y = TRUE)

## ----plot-xic-base-r, fig.height=5, fig.width=12------------------------------
mzr_xic <- rbind(c(334.9, 335.1), c(278.99721, 279.00279))
rtr_xic <- rbind(c(2700, 2900), c(2740, 2840))

chrs_xic <- chromatogram(xdata, mz = mzr_xic, rt = rtr_xic)

## Define one color per sample group
group_colors <- RColorBrewer::brewer.pal(3, "Set1")[1:2]
names(group_colors) <- c("KO", "WT")
sample_colors <- group_colors[chrs_xic$sample_group]

## Label the rows the same way lcmsPlot names its features
feature_ids <- sprintf("M%dT%d", round(rowMeans(mzr_xic)),
                       round(rowMeans(rtr_xic)))

## One panel per feature (row) and sample (column)
par(mfrow = c(nrow(chrs_xic), ncol(chrs_xic)),
    mar = c(3, 3, 1.8, 0.4), mgp = c(1.9, 0.6, 0), cex = 0.7)
for (i in seq_len(nrow(chrs_xic))) {
    for (j in seq_len(ncol(chrs_xic))) {
        plot(chrs_xic[i, j], col = sample_colors[j],
             peakType = "polygon", peakCol = paste0(sample_colors[j], 60),
             peakBg = paste0(sample_colors[j], 40),
             main = paste(feature_ids[i], chrs_xic$sample_name[j]))
    }
}

## ----plot-XChromatograms, message=FALSE, fig.height=4, fig.width=10-----------
mzr <- matrix(c(344, 344, 360, 360), ncol = 2, byrow = TRUE)

rtr <- matrix(c(2500, 2880, 2600, 2880), ncol = 2, byrow = TRUE)

chrs <- chromatogram(xdata, mz = mzr, rt = rtr)
rownames(chrs) <- c("mz344", "mz360")

lcmsPlot(chrs) +
    lp_chromatogram(na.rm = TRUE) +
    lp_grid(rows = "feature_id", cols = "sample_id")

## ----peak-count-image, fig.height=4, fig.width=9------------------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_peak_count_image(bin_size = 30) +
    lp_labels(title = "Chromatographic peaks per 30 s bin")

## ----chrom-peak-rects-standalone, fig.height=5, fig.width=9-------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_chrom_peak_rects(
        sample_ids = c("ko15", "wt15"),
        rt_range = c(3200, 3700),
        mz_range = c(480, 540),
        fill = "#c0392b60")

## ----plot-features------------------------------------------------------------
lcmsPlot(xdata, sample_id_column = 'sample_name') +
    lp_chromatogram(
        features = c('FT002', 'FT004', 'FT019', 'FT027'),
        ppm = 10,
        rt_tol = 80,
        highlight_peaks = TRUE,
        highlight_peaks_factor = "sample_group") +
    lp_arrange(group_by = 'sample_group') +
    lp_facets(facets = 'feature_id', ncol = 2, free_x = TRUE, free_y = TRUE) +
    lp_labels(title = "Four selected features", legend = "Sample") +
    lp_legend(position = "bottom")

## ----peak-density-------------------------------------------------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_chromatogram(
        sample_ids = c("ko15", "ko16", "wt16", "wt21"),
        features = rbind(c(
            mzmin = 334.9, mzmax = 335.1, rtmin = 2700, rtmax = 2900)),
        highlight_peaks = TRUE
    ) +
    lp_peak_density(
        bw = 30,
        min_fraction = 0.5
    ) +
    lp_labels(legend = "Sample ID")

## ----plot-spectra-closest-rt, fig.height=5, fig.width=9-----------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_chromatogram(
        features = "FT034",
        sample_ids = "ko15",
        ppm = 20,
        rt_tol = 60,
        highlight_peaks = TRUE) +
    lp_spectra(rt = 2789, mode = "closest", ms_level = 1) +
    lp_labels(title = "Feature FT034", legend = "Sample") +
    lp_legend(position = "bottom")

## ----plot-spectra-closest-apex, fig.height=5, fig.width=10--------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_chromatogram(
        features = c("FT219", "FT250"),
        sample_ids = "ko15",
        ppm = 20,
        rt_tol = 60,
        highlight_peaks = TRUE) +
    lp_spectra(mode = "closest_apex", ms_level = 1) +
    lp_facets(facets = "feature_id", ncol = 2) +
    lp_labels(legend = "Sample") +
    lp_legend(position = "bottom") +
    lp_layout(design = "C\nS\nS")

## ----plot-spectra-across-peak, fig.height=10, fig.width=6---------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_chromatogram(
        features = "FT083",
        sample_ids = "ko15",
        ppm = 20,
        rt_tol = 60,
        highlight_peaks = TRUE) +
    lp_spectra(mode = "across_peak", interval = 15, ms_level = 1) +
    lp_labels(legend = "Sample") +
    lp_legend(position = "bottom") +
    lp_layout(design = "C\nS\nS")

## ----plot-standalone-spectra, fig.height=5, fig.width=9-----------------------
lcmsPlot(xdata, sample_id_column = "sample_name") +
    lp_spectra(
        sample_ids = c("ko15", "wt15"),
        rt = 3323,
        mode = "closest",
        ms_level = 1) +
    lp_labels(title = "MS1 spectra at 3323 seconds") +
    lp_legend(position = "bottom")

## ----sessionInfo--------------------------------------------------------------
sessionInfo()

