Skip to content

Commit

Permalink
Working on update_curve_data function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hefin Rhys committed Apr 8, 2024
1 parent ee6ff94 commit 3647ddb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
8 changes: 4 additions & 4 deletions R/plot_curves.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -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 |>
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion R/refit_curves.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ]
Expand Down
78 changes: 53 additions & 25 deletions R/update_curve_data.R
Original file line number Diff line number Diff line change
@@ -1,68 +1,96 @@
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
) |>
dplyr::pull(Expected) |>
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()

}

0 comments on commit 3647ddb

Please sign in to comment.