## ----style, echo = FALSE, results = 'asis'------------------------------------
BiocStyle::markdown()

## ----env, include=FALSE, echo=FALSE, cache=FALSE------------------------------
library("knitr")
opts_chunk$set(stop_on_error = 1L)
suppressPackageStartupMessages(library("MsQuality"))
suppressPackageStartupMessages(library("Spectra"))

## ----eval = FALSE-------------------------------------------------------------
# if (!requireNamespace("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")
# 
# if (!requireNamespace("remotes", quietly = TRUE))
#     install.packages("remotes")
# 
# ## to install from Bioconductor
# BiocManager::install("MsQuality")
# 
# ## to install from GitHub
# BiocManager::install("tnaake/MsQuality")

## ----load_Spectra-------------------------------------------------------------
library("Spectra")
library("MsExperiment")
library("Chromatograms")
library("MsQuality")
library("BiocParallel")

register(SerialParam())

## ----spectra_sciex, eval = FALSE, echo = TRUE---------------------------------
# library(MsDataHub)
# ## this example is taken from the Spectra vignette
# fls <- c(MsDataHub::X20171016_POOL_POS_1_105.134.mzML(),
#          MsDataHub::X20171016_POOL_POS_3_105.134.mzML())
# sps_sciex <- Spectra(fls, backend = MsBackendMzR())

## ----msexp_sciex, eval = FALSE, echo = TRUE-----------------------------------
# ## this example is taken from the Spectra vignette
# lmse <- MsExperiment()
# sd <- DataFrame(sample_id = c("QC1", "QC2"),
#     sample_name = c("QC Pool", "QC Pool"),
#     injection_idx = c(1, 3))
# sampleData(lmse) <- sd
# ## add mzML files to the experiment
# experimentFiles(lmse) <- MsExperimentFiles(mzML_files = fls)
# ## add the Spectra object to the experiment
# spectra(lmse) <- sps_sciex
# ## use linkSampleData to establish and define relationships between sample
# ## annotations and MS data
# lmse <- linkSampleData(lmse, with = "experimentFiles.mzML_files",
#     sampleIndex = c(1, 2), withIndex = c(1, 2))

## ----chromatograms_example, eval = FALSE, echo = TRUE-------------------------
# ## create a Chromatograms object from manually defined chromatographic data
# cdata <- data.frame(
#     msLevel = c(1L, 1L),
#     mz = c(112.2, 123.3),
#     dataOrigin = c("sample_1", "sample_1")
# )
# pdata <- list(
#     data.frame(rtime = c(1.0, 1.5, 2.0, 2.5, 3.0),
#                intensity = c(100, 250, 400, 300, 150)),
#     data.frame(rtime = c(1.2, 1.8, 2.4, 3.0),
#                intensity = c(50, 150, 300, 200))
# )
# chr <- Chromatograms(ChromBackendMemory(), chromData = cdata, peaksData = pdata)
# 
# ## calculate metrics on the Chromatograms object (disabled during build)
# metrics_chr <- calculateMetrics(object = chr, metrics = qualityMetrics(chr))
# metrics_chr

## ----load_Lee2019, eval = TRUE, echo = TRUE-----------------------------------
data("Lee2019", package = "MsQuality")

## ----combine_sps_rplc_sps_hilic, eval = TRUE, echo = TRUE---------------------
sps_comb <- c(sps_rplc, sps_hilic)

## -----------------------------------------------------------------------------
qualityMetrics(sps_comb)

## ----eval = FALSE-------------------------------------------------------------
# qualityMetrics(chr)

## ----calculateMetrics---------------------------------------------------------
## subset the Spectra objects
sps_comb_subset <- sps_comb[grep("Sample.1_", sps_comb$dataOrigin), ]

## for RPLC and HILIC
metrics_sps_Q1 <- calculateMetrics(object = sps_comb_subset,
    metrics = qualityMetrics(sps_comb_subset), filterEmptyObject = FALSE,
    relativeTo = "Q1", msLevel = 1L)
metrics_sps_Q1
metrics_sps_previous <- calculateMetrics(object = sps_comb_subset,
    metrics = qualityMetrics(sps_comb_subset), filterEmptyObject = FALSE,
    relativeTo = "previous", msLevel = 1L)
metrics_sps_previous

## ----metrics_rplc_hilic_lee2019-----------------------------------------------
## subset the MsExperiment objects
msexp_rplc_subset <- msexp_rplc[1:20]
msexp_hilic_subset <- msexp_hilic[1:20]

## for RPLC-derived MsExperiment
metrics_rplc_msexp <- calculateMetrics(object = msexp_rplc_subset,
    metrics = qualityMetrics(msexp_rplc_subset), filterEmptyObject = TRUE,
    relativeTo = "Q1", msLevel = 1L)

## for HILIC-derived MsExperiment
metrics_hilic_msexp <- calculateMetrics(object = msexp_hilic_subset,
    metrics = qualityMetrics(msexp_hilic_subset), filterEmptyObject = TRUE,
    relativeTo = "Q1", msLevel = 1L)

## ----paged_table_metrics, eval = TRUE, echo = FALSE---------------------------
print("metrics_rplc_msexp")
rmarkdown::paged_table(as.data.frame(metrics_rplc_msexp))
print("metrics_hilic_msexp")
rmarkdown::paged_table(as.data.frame(metrics_hilic_msexp))

## -----------------------------------------------------------------------------
metrics_msexp <- rbind(metrics_rplc_msexp, metrics_hilic_msexp)
plotMetric(qc = metrics_msexp, metric = "numberSpectra")

## -----------------------------------------------------------------------------
plotMetric(qc = metrics_msexp, metric = "ticQuartileToQuartileLogRatio")

## ----eval = FALSE-------------------------------------------------------------
# shinyMsQuality(qc = metrics_msexp)

## ----session,eval=TRUE, echo=FALSE--------------------------------------------
sessionInfo()

