-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.R
35 lines (35 loc) · 924 Bytes
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#' Utility function to calculate relative abundances
#'
#' @description
#' The function calculates the column totals and divides each value within the
#' column by the respective column total.
#'
#' @inheritParams lefser
#'
#' @param se A SummarizedExperiment object with counts
#'
#' @examples
#'
#' se <- SummarizedExperiment(
#' assays = list(
#' counts = matrix(
#' rep(1, 4), ncol = 1, dimnames = list(LETTERS[1:4], "SAMP")
#' )
#' )
#' )
#' assay(se)
#' assay(relativeAb(se))
#'
#' @export
relativeAb <- function(se, assay = 1L) {
assay_data <- assay(se, i = assay)
csums <- colSums(assay_data)
div <- matrix(rep(csums, each = nrow(assay_data)), ncol = ncol(assay_data))
res <- assay_data / div
assaylist <- assays(se)
newalist <- append(
assaylist, values = S4Vectors::SimpleList(rel_abs = res), after = 0L
)
assays(se) <- newalist
se
}