R/normalization.R

Defines functions convert_to_cpm

Documented in convert_to_cpm

#' Convert expression matrix to CPM (library size normalization).
#'
#' @param M Expression matrix (with genes as rows) in sparse or dense format.
#' @param scale_factor Cell counts sum to this value after normalization
#' (1e6 by default).
#' @return Matrix in same format as input.
#'
#' @export
convert_to_cpm = function(M, scale_factor=1000000) {
    normalization_factor = Matrix::colSums(M) / scale_factor
    if (methods::is(M, "dgCMatrix")) {
        M@x = M@x / rep.int(normalization_factor, diff(M@p))
        return(M)
    } else {
        return(scale(M, center = FALSE, scale = normalization_factor))
    }
}
gillislab/MetaMarkers documentation built on April 24, 2021, 9:25 p.m.