-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_normalization_rowWiseNorm.R
32 lines (27 loc) · 1.07 KB
/
02_normalization_rowWiseNorm.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
#' @param x a matrix where rows are genes/proteins and columns are samples
#' @param batch a factor or vecter has the same length as ncol(x) to indicate
#' the batch assignment of samples
#' @param ref a logical vector has the same length as ncol(x) to indicated which
#' columns are the common references among batches. If it is NULL (by default),
#' the mean of all channels will be used as batch reference.
#' @importFrom matrixStats rowMeans
rowshift <- function(x, batch, ref=NULL, useMean = FALSE) {
if (is.data.frame(x))
x <- apply(x, 2, as.numeric)
if (is.null(ref))
ref <- 1:ncol(x)
b_ref <- batch[ref]
expr_ref <- x[, ref, drop=FALSE]
grandmeans <- rowMeans(expr_ref, na.rm = TRUE)
grandmeans2 <- rowMeans(x, na.rm = TRUE)
for (i in unique(batch)) {
off <- rowMeans(expr_ref[, b_ref == i, drop = FALSE], na.rm = TRUE) - grandmeans
if (useMean) {
off2 <- rowMeans(x[, batch == i, drop = FALSE], na.rm = TRUE) - grandmeans2
ona <- is.na(off)
off[ona] <- off2[ona]
}
x[, batch == i] <- x[, batch == i] - off
}
x
}