#' Generate the Normalizer Values for each Sample
#'
#' `get_sample_normalizer_value` returns the normalizer values for each
#' sample. The normalizer value is based on the geometric mean of the input
#' `normalizer_genes`
#'
#' @param exprs_mat The expression matrix.
#' @param normalizer_genes Character vector containing the genes to use for
#' calculating the normalizer
#' @return A numeric vector with the samples as names and the normalizer
#' as values
#' @export
get_sample_normalizer_value <- function(exprs_mat, normalizer_genes) {
message(
"[get_normalizer]: Generating the geometric mean of housekeeper genes"
)
if (! length(normalizer_genes) > 1) {
stop("[get_normalizer]: Must have more than 1 housekeeper gene")
}
if (! all(normalizer_genes %in% rownames(exprs_mat))) {
missing_ind <- !normalizer_genes %in% rownames(exprs_mat)
stop(
paste0(
"[get_normalizer]: Matrix is missing the genes: ",
normalizer_genes[missing_ind])
)
}
normalizer_log <- log(exprs_mat[normalizer_genes, ])
normalizer_log_mean <- apply(normalizer_log, 2, mean)
normalizer_log_mean_exp <- exp(normalizer_log_mean)
return(normalizer_log_mean_exp)
}
#' Normalize the Expression Matrix
#'
#' This function normalizes the expression matrix.
#'
#' @param exprs_mat The expression matrix
#' @param sample_normalizer_values Sample normalizer values generated by
#' the `get_sample_normalizer_value` function
#' @return A normalized expression matrix
#' @export
normalize_exprs_mat <- function(exprs_mat, sample_normalizer_values) {
samples <- colnames(exprs_mat)
if (! all(samples %in% names(sample_normalizer_values) )) {
stop(
paste(
"[normalize_exprs_mat]: Not all samples in the exprs_mat have sample",
"normalizer values"
)
)
}
sample_normalizer_values <- sample_normalizer_values[samples]
message("[normalize_exprs_mat]: Normalizing the expression exprs_matrix")
exprs_mat_norm <- sweep(exprs_mat, 2, sample_normalizer_values, "/")
exprs_mat_norm_scaled <- exprs_mat_norm * 1000
# Ensure that any values < 1 do not become negative
exprs_mat_norm_scaled[exprs_mat_norm_scaled < 1] <- 1
message("[normalize_exprs_mat]: Log2 transforming")
exprs_mat_norm_scaled_log <- log(exprs_mat_norm_scaled, 2)
return(exprs_mat_norm_scaled_log)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.