forked from denniscwylie/maclearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNormalizeData.R
36 lines (31 loc) · 1.12 KB
/
NormalizeData.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
36
## -----------------------------------------------------------------
## normalization
## -----------------------------------------------------------------
rleSizeFactors = function(x) {
require(matrixStats)
xno0 = x[ , colMins(x) > 0]
geoMeans = exp(colMeans(log(xno0)))
sizeFactors = rowMedians(sweep(xno0, 2, geoMeans, `/`))
names(sizeFactors) = rownames(x)
return(sizeFactors)
}
xnorms = list()
## shen set already normalizezd
xnorms$shen = xs$shen
## patel set already normalized
xnorms$patel = xs$patel
meanCenter = function(x, MARGIN=1) {
geneHasNAs = apply(x, 3-MARGIN, function(z) {any(is.na(z))})
means = apply(x, MARGIN, function(z) {mean(z[!geneHasNAs])})
return(sweep(x, MARGIN, means, `-`))
}
meanCenterAndImpute = function(x, MARGIN=1,
imputeAt=ceiling(max(x, na.rm=TRUE))) {
geneHasNAs = apply(x, 3-MARGIN, function(z) {any(is.na(z))})
means = apply(x, MARGIN, function(z) {mean(z[!geneHasNAs])})
x[is.na(x)] = imputeAt
return(sweep(x, MARGIN, means, `-`))
}
xnorms$montastier = meanCenterAndImpute(xs$montastier)
## hess set already normalized
xnorms$hess = xs$hess