diff --git a/R/plot_curves.R b/R/plot_curves.R index 52e7b9e..75af336 100644 --- a/R/plot_curves.R +++ b/R/plot_curves.R @@ -12,7 +12,8 @@ #' for each facet. One of \code{"fixed"} (default), \code{"free_y"}, \code{"free_x"}, #' or \code{"free"}. #' @param rug If \code{TRUE} (default) will add a "rug" of tickmarks to the x and y -#' axes to denote Unknown and Control samples. +#' axes to denote Unknown and Control samples. Setting this to \code{FALSE} may speed +#' up rendering. #' @param model_limits If \code{TRUE} (default) will annotate the plots with lower #' limits of quantitation (LLoQ), limits of detection (LoD), and minimum #' detectable dose (MDD) if available. @@ -34,7 +35,7 @@ plot_curves <- function(.intelliframe, analytes = NULL, type = "individual", int if(!is.null(analytes)) { stopifnot("Analyte not found in data" = all(analytes %in% unique(dat$Analyte))) - dat <- dplyr::filter(dat, "Analyte" %in% analytes) + dat <- dplyr::filter(dat, .data[["Analyte"]] %in% analytes) } standard_data <- dplyr::filter(dat, .data[["Type"]] == "Standard") @@ -44,7 +45,7 @@ plot_curves <- function(.intelliframe, analytes = NULL, type = "individual", int curve_data <- S7::`@`(.intelliframe, "curve_data") if(!is.null(analytes)) { - curve_data <- dplyr::filter(curve_data, "Analyte" %in% analytes) + curve_data <- dplyr::filter(curve_data, .data[["Analyte"]] %in% analytes) } curve_data_long <- curve_data |> @@ -75,7 +76,6 @@ plot_curves <- function(.intelliframe, analytes = NULL, type = "individual", int ggplot2::aes(.data[["Result"]], .data[["Predicted"]]), inherit.aes = FALSE ) + - # ggplot2::scale_colour_brewer(type = "qual", palette = "Set1") + ggplot2::scale_colour_manual(values = c("Control" = "#E41A1C", "Standard" = "#377EB8", "Unknown" = "#4DAF4A")) + suppressWarnings( ggplot2::geom_point( diff --git a/R/refit_curves.R b/R/refit_curves.R index bef197f..b8ee414 100644 --- a/R/refit_curves.R +++ b/R/refit_curves.R @@ -105,7 +105,7 @@ refit_curves <- function( standard_list <- split(standard_list, standard_list$Analyte) - fits <- lapply(standard_list, function(analyte) { + fits <<- lapply(standard_list, function(analyte) { if(!use_excluded) { analyte <- analyte[-excluded_wells, ] diff --git a/R/update_curve_data.R b/R/update_curve_data.R index b3515f9..f772f92 100644 --- a/R/update_curve_data.R +++ b/R/update_curve_data.R @@ -1,10 +1,13 @@ -lapply(names(fits), function(x) { +update_well_data <- function(.summary_data, .well_data, .fits, .standard_list, .silent) { + +lapply(names(.fits), function(x) { lloq <- dplyr::filter( - u5plex@summary_data, + .summary_data, Analyte == x, Type == "Standard", Result_CV <= 0.2, + dplyr::n() > 1, Result_Avg / Expected > 0.8, Result_Avg / Expected < 1.2 ) |> @@ -12,57 +15,82 @@ lapply(names(fits), function(x) { min() blank <- dplyr::filter( - u5plex@well_data, + .summary_data, Analyte == x, Type == "Standard", Expected == 0 - ) |> - dplyr::pull(MFI) + ) - blank_targets <- - (mean(blank) + 2.5*sd(blank)) / - max(dplyr::filter(u5plex@well_data, Analyte == x)$MFI) + analyte <- dplyr::filter(.well_data, Analyte == x) + analyte <- analyte[!analyte$Excluded, ] + standard <- dplyr::filter(analyte, Type == "Standard") + + blank_targets <- (blank$MFI_Avg + 2.5 * blank$MFI_SD) / max(standard$MFI) suppressWarnings({ suppressMessages({ mdd <- nplr::getEstimates( - fits[[x]], + .fits[[x]], targets = blank_targets )$x }) }) df_low <- data.frame( - x = 10^(fits[[x]]@x), - y = fits[[x]]@y - )[10^(fits[[x]]@x) %in% unique(10^(fits[[x]]@x))[1:3],] + x = 10^(.fits[[x]]@x), + y = .fits[[x]]@y + )[10^(.fits[[x]]@x) %in% unique(10^(.fits[[x]]@x))[2:4],] fit_low <- nplr::nplr( x = df_low$x, - y = df_low$y#, - # npars = npars, - # method = weight_method, - # LPweight = LPweight, - # silent = silent + y = df_low$y, + silent = TRUE ) suppressWarnings({ suppressMessages({ - lod <- nplr::getEstimates( - fit_low, - targets = blank_targets - )$x + lod <- nplr::getEstimates(fit_low, targets = blank_targets)$x }) }) + if(!silent) { + warning("LLoQ, MDD, and LoD may be calculated differently than in Belysa. See ?refit_curves for details.") + } + + if(.fits[[x]]@weightMethod == "res") { + weight_meth <- paste0("(1/residual)^", .fits[[x]]@LPweight) + } else if(.fits[[x]]@weightMethod == "sdw") { + weight_meth <- "1/Var(y)" + } else if(.fits[[x]]@weightMethod == "gw") { + weight_meth <- paste0("1/fitted^", .fits[[x]]@LPweight) + } + + model_eqn <- paste0( + "y = ", + .fits[[x]]@pars["bottom"], + " + (", + .fits[[x]]@pars["top"], + " - ", + .fits[[x]]@pars["bottom"], + ") / (1 + 10^(", + .fits[[x]]@pars["bottom"], + " * (", + .fits[[x]]@pars["xmid"], + " - x)))^", + .fits[[x]]@pars["s"] + ) + tibble::tibble( + Group = analyte$Group, Analyte = x, - Fit = paste0("nplr ", fits[[x]]@npars, "PL"), + Fit = paste0("nplr ", .fits[[x]]@npars, "PL"), LLoQ = lloq, MDD = mdd, LoD = lod, - `R Squared` = fits[[x]]@goodness$gof, - Slope = fits[[x]]@pars$s + `R Squared` = .fits[[x]]@goodness$gof, + Slope = .fits[[x]]@pars$s, + Weighting = weight_meth, + Equation = model_eqn ) }) |> dplyr::bind_rows() - +}