## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, prompt = TRUE, highlight = FALSE,
                      comment = NA, dpi = 200, fig.width = 6,
                      out.width = "100%")

## ----setup--------------------------------------------------------------------
library(chromConverter)

## ----files--------------------------------------------------------------------
seq_dir <- system.file("extdata", "benzoxazinoid_standards",
                       package = "chromConverter")
list.files(seq_dir)

## ----read---------------------------------------------------------------------
chroms <- read_chroms(seq_dir, format_in = "chemstation_ch",
                      pattern = "dad1A", parser = "chromconverter",
                      progress_bar = FALSE)

## ----print--------------------------------------------------------------------
chroms

## ----summary------------------------------------------------------------------
summary(chroms, cols = c("run_datetime", "wavelength"))

## ----extract------------------------------------------------------------------
extract_metadata(chroms, "time_range")

## ----subset-------------------------------------------------------------------
subset(chroms, sample_name != "MEOH")

## ----index--------------------------------------------------------------------
c(chroms["MEOH"], chroms["BENZOS_4PPM"])

## ----add-metadata-------------------------------------------------------------
sheet <- data.frame(name = c("MEOH", "BENZOS_1000PPM", "BENZOS_500PPM",
                             "BENZOS_250PPM", "BENZOS_125PPM",
                             "BENZOS_62,5PPM", "BENZOS_31,25PPM",
                             "BENZOS_16PPM", "BENZOS_8PPM", "BENZOS_4PPM"),
                    ppm = c(0, 1000, 500, 250, 125, 62.5, 31.25, 15.625,
                            7.8125, 3.90625))
annotated <- add_metadata(chroms, sheet)
subset(annotated, ppm < 20)

## ----one-chrom----------------------------------------------------------------
x <- chroms$BENZOS_4PPM
head(x)

## ----attr---------------------------------------------------------------------
attr(x, "run_datetime")
attr(x, "detector_model")

## ----read-d-------------------------------------------------------------------
run <- read_chroms(file.path(seq_dir, "BENZOS_250PPM.D"),
                   format_in = "agilent_d",
                   what = c("chroms", "peak_table", "instrument"),
                   progress_bar = FALSE)
print(run, cols = "wavelength")

## ----summary-d----------------------------------------------------------------
summary(run)[c("sample", "trace", "wavelength")]

## ----peaks--------------------------------------------------------------------
peaks <- run$BENZOS_250PPM$peak_table[["254"]]
head(peaks)

## ----plot-peaks, fig.height = 4, fig.alt = "The 254 nm trace of the 250 ppm standard, with the four peaks in the ChemStation report that stand out from the noise labelled DIBOA, DIMBOA, BOA and MBOA, near 12.5, 19, 21 and 28 minutes."----
real <- subset(peaks, rt > 10 & height > 5)
trace <- run$BENZOS_250PPM$chroms$dad1A
plot(as.numeric(rownames(trace)), trace[, 1], type = "l", ylim = c(-20, 500),
     xlab = "Time (min)", ylab = "Absorbance (mAU)")
text(real$rt, real$height, c("DIBOA", "DIMBOA", "BOA", "MBOA"),
     adj = c(0, -0.5), cex = 0.8)

## ----pressure, fig.height = 5, fig.show = "hold", fig.alt = "Two stacked panels sharing a 0 to 60 minute time axis. Top: the 254 nm trace of the 250 ppm standard. Bottom: pump pressure, rising from about 160 to 215 bar between 5 and 40 minutes, then falling to about 95 bar by 54 minutes and holding."----
names(run$BENZOS_250PPM$instrument)
pressure <- run$BENZOS_250PPM$instrument[["PMP1, Pressure"]]
op <- par(mfrow = c(2, 1), mar = c(0.5, 4, 1, 1), oma = c(4, 0, 0, 0))
plot(as.numeric(rownames(trace)), trace[, 1], type = "l", xaxt = "n",
     xlab = "", ylab = "Absorbance (mAU)")
par(mar = c(0, 4, 0.5, 1))
plot(as.numeric(rownames(pressure)), pressure[, 1], type = "l",
     xlab = "", ylab = "Pressure (bar)")
mtext("Time (min)", side = 1, line = 2.5, outer = TRUE)
par(op)

## ----peaklist-----------------------------------------------------------------
peak_list <- read_peaklist(seq_dir, format_in = "chemstation",
                           progress_bar = FALSE)
names(peak_list$BENZOS_250PPM)

## ----dimboa-------------------------------------------------------------------
at254 <- do.call(rbind, lapply(peak_list, `[[`, "254"))
dimboa <- subset(at254, rt > 18.5 & rt < 20.3)
dimboa <- merge(dimboa, extract_metadata(annotated, "ppm"),
                by.x = "sample", by.y = "name")
dimboa[c("sample", "rt", "area", "ppm")]

## ----calibration, fig.alt = "DIMBOA peak area against concentration for the nine standards, from 4 to 1000 ppm, lying close to a fitted straight line with an R-squared of 0.999.", fig.height = 4.5----
fit <- lm(area ~ ppm, data = dimboa)
plot(area ~ ppm, data = dimboa, xlab = "DIMBOA (ppm)",
     ylab = "Peak area (mAU s)", pch = 20)
abline(fit, col = "gray20")
legend("topleft", sprintf("R² = %.4f", summary(fit)$r.squared), bty = "n")

