Skip to content

Commit

Permalink
Add zero centering and lm drift normalization functions
Browse files Browse the repository at this point in the history
  • Loading branch information
johanna0321 committed May 14, 2024
1 parent 31deaf6 commit 5661eca
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ Suggests: dbplyr,
testthat
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
VignetteBuilder: knitr
98 changes: 97 additions & 1 deletion R/mutate_mzroll_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,16 @@ fill_in_missing_peaks <- function(mzroll_list,
#' value
#' }
#' \item{\code{center batches}: batch centering}
#' \item{\code{center}: zero-center each compound}
#' \item{\code{reference sample}: compare to reference sample}
#' \item{\code{reference condition}: compare each sample to its specified
#' reference sample}
#' \item{\code{loess}: weighted smoothing of IC over time (adds
#' \code{.loess_fit} as a peaks variable in addition to
#' \code{norm_peak_varname})}
#' \item{\code{lm}: linear regression smoothing of IC over time (adds
#' \code{lm_estimate} as feature variable in addition to
#' \code{norm_peak_varname})}
#' }
#' @param quant_peak_varname variable in measurements to use for abundance
#' @param norm_peak_varname variable in measurements to add for normalized
Expand Down Expand Up @@ -269,7 +273,9 @@ normalize_peaks <- function(mzroll_list,
"center batches", "normalize_peaks_batch_center",
"reference sample", "normalize_peaks_reference_sample",
"reference condition", "normalize_peaks_reference_condition",
"loess", "normalize_peaks_loess"
"loess", "normalize_peaks_loess",
"lm", "normalize_peaks_lm",
"center","normalize_peaks_center"
)

checkmate::assertChoice(
Expand Down Expand Up @@ -901,4 +907,94 @@ normalization_refloor <- function(normalized_peaks,
}

return(normalized_peaks)
}

#' @inheritParams normalize_peaks_batch_center
#' @param time_col_varname variable in samples table to use for linear
#' correction
#'
#' @rdname normalize_peaks
normalize_peaks_lm <- function (mzroll_list,
quant_peak_varname,
norm_peak_varname,
time_col_varname)
{
stopifnot(time_col_varname %in% colnames(mzroll_list$samples))

time_col_varname_add <- mzroll_list$samples %>%
dplyr::select(c("sampleId", time_col_varname))

lm_fit <- mzroll_list$measurements %>%
dplyr::left_join(., time_col_varname_add, by = "sampleId") %>%
tidyr::nest(groupData = -groupId) %>%
dplyr::mutate(lm_fits = purrr::map(groupData,
fit_lm,
quant_peak_varname = quant_peak_varname,
norm_peak_varname = norm_peak_varname,
time_col_varname = time_col_varname)) %>%
dplyr::select(-groupData) %>%
tidyr::unnest(lm_fits)

lm_fit_measurements <- lm_fit %>%
dplyr::select(!!!rlang::syms(c(colnames(mzroll_list$measurements), norm_peak_varname)))

lm_fit_features <- lm_fit %>%
dplyr::select(groupId, lm_estimate) %>%
unique() %>%
dplyr::left_join(mzroll_list$features, ., by = "groupId")

mzroll_list <- romic::update_tomic(mzroll_list, lm_fit_measurements)
mzroll_list <- romic::update_tomic(mzroll_list, lm_fit_features)

return(mzroll_list)

}

fit_lm <- function (groupData,
time_col_varname,
quant_peak_varname,
norm_peak_varname,
order = 1)
{

lm_data <- groupData %>%
dplyr::mutate(val_var = !!rlang::sym(quant_peak_varname),
dri_var = !!rlang::sym(time_col_varname))

lm_predict <- lm_data %>%
tidyr::drop_na(val_var)

# Run model
lm_model <- stats::lm(val_var ~ poly(dri_var, degree = order), data = lm_predict)

# Compute corrected values
lm_apply <- lm_data %>%
dplyr::mutate(median_value = median(.data$val_var, na.rm = T)) %>%
dplyr::mutate(`:=`(!!rlang::sym(norm_peak_varname),
.data$val_var - .env$predict(lm_model, newdata = lm_data) + .data$median_value)) %>%
dplyr::mutate(lm_estimate = summary(.env$lm_model)$coefficient[2,1]) %>%
dplyr::select(-c(val_var, dri_var, median_value))

return(lm_apply)

}


#' @inheritParams normalize_peaks_center
#'
#' @rdname normalize_peaks
normalize_peaks_center <- function(mzroll_list,
quant_peak_varname,
norm_peak_varname)
{

updated_measurements <- mzroll_list$measurements %>%
dplyr::group_by(groupId) %>%
dplyr::mutate(!!rlang::sym(norm_peak_varname) :=
scale(!!rlang::sym(quant_peak_varname), scale = F, center = T))

mzroll_list <- romic::update_tomic(mzroll_list, updated_measurements)

return(mzroll_list)

}
17 changes: 17 additions & 0 deletions man/calicomics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/normalize_peaks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/nplug_compounds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/nplug_mzroll_augmented.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/nplug_mzroll_normalized.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/nplug_samples.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5661eca

Please sign in to comment.