#' Generate the Normalizer Values for each Sample
#'
#' \code{get_normalizer} returns the normalizer values for each sample. The
#' normalizer value is based on the geometric mean of the housekeeper genes
#' ACTB, CLTC, and RPLP0.
#'
#' You can change the housekeeper genes by specifying the genes.norm.factor
#' parameter.
#'
#' @param mat The expression matrix
#' @param genes.norm.factor Character vector containing the housekeeper genes
#' to use to calculate the normalizer
#' @return A numeric vector with the samples as names and the normalizer
#' as values
#' @export
#' @examples
#' normalizer <- get_normalizer(mat)
get_normalizer <- function(mat,
genes.norm.factor = c("ACTB", "CLTC", "RPLP0")) {
message("Generating the geometric mean of housekeeper genes")
if (!length(genes.norm.factor) > 1) {
stop("Must have more than 1 housekeeper gene")
}
if (!all(genes.norm.factor %in% rownames(mat))) {
missing.ind <- !genes.norm.factor %in% rownames(mat)
stop(paste0("Matrix is missing the genes: ", genes.norm.factor[missing.ind]))
}
normalizer.log <- log(mat[genes.norm.factor, ])
normalizer.log.mean <- apply(normalizer.log, 2, mean)
normalizer.log.mean.exp <- exp(normalizer.log.mean)
normalizer.log.mean.exp
}
#' Normalize the Expression Matrix
#'
#' This function normalizes the expression matrix.
#'
#' @param mat The expression matrix
#' @param normalizer Normalizer values generated by \code{get_normalizer}
#' @return A normalized expression matrix
#' @export
#' @examples
#' normalizer <- get_normalizer(mat)
#' mat.norm <- normalizer_mat(mat, normalizer)
normalize_mat <- function(mat, normalizer) {
message("Normalizing the expression matrix")
mat.norm <- sweep(mat, 2, normalizer, "/")
mat.norm.scaled <- mat.norm * 1000
# Ensure that any values < 1 do not become negative
mat.norm.scaled[mat.norm.scaled < 1] <- 1
message("Log2 transforming")
mat.norm.scaled.log <- log(mat.norm.scaled, 2)
mat.norm.scaled.log
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.