-
Notifications
You must be signed in to change notification settings - Fork 4
/
extractFromRGSet450k.R
142 lines (122 loc) · 5.46 KB
/
extractFromRGSet450k.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
extractFromRGSet450k <- function(rgSet) {
rgSet <- updateObject(rgSet)
controlType <- c("BISULFITE CONVERSION I",
"BISULFITE CONVERSION II",
"EXTENSION",
"HYBRIDIZATION",
"NEGATIVE",
"NON-POLYMORPHIC",
"NORM_A",
"NORM_C",
"NORM_G",
"NORM_T",
"SPECIFICITY I",
"SPECIFICITY II",
"TARGET REMOVAL",
"STAINING")
MSet.raw <- preprocessRaw(rgSet)
r <- getRed(rgSet)
g <- getGreen(rgSet)
meth <- getMeth(MSet.raw)
unmeth <- getUnmeth(MSet.raw)
beta <- getBeta(MSet.raw)
m <- getM(MSet.raw)
cn <- meth + unmeth
## Extraction of the controls
greenControls = vector("list", length(controlType))
redControls = vector("list", length(controlType))
names(greenControls) <- controlType
names(redControls) <- controlType
for (i in 1:length(controlType)) {
if (controlType[i] != "STAINING") {
ctrlAddress <- getControlAddress(
rgSet, controlType = controlType[i])
} else {
ctrlAddress <- getControlAddress(
rgSet, controlType = controlType[i])[c(2,3,4,6)]
}
redControls[[i]] = r[ctrlAddress,]
greenControls[[i]] = g[ctrlAddress,]
}
## Extraction of the undefined negative control probes
locusNames <- getManifestInfo(rgSet, "locusNames")
TypeI.Red <- getProbeInfo(rgSet, type = "I-Red")
TypeI.Green <- getProbeInfo(rgSet, type = "I-Green")
numberQuantiles <- 100
probs <- 1:numberQuantiles/100
## Function to extract quantiles
extractQuantiles <- function(matrix, probs) {
result <- apply(matrix, 2, function(x) quantile(x, probs = probs, na.rm = TRUE))
result
} ## KDH: do we have a function in matrixStats?
greenOOB <- rbind(getGreen(rgSet)[TypeI.Red$AddressA,], getGreen(rgSet)[TypeI.Red$AddressB,])
redOOB <- rbind(getRed(rgSet)[TypeI.Green$AddressA,], getRed(rgSet)[TypeI.Green$AddressB,])
greenOOB <- extractQuantiles(greenOOB, probs = probs)
redOOB <- extractQuantiles(redOOB, probs = probs)
oob <- list(greenOOB = greenOOB, redOOB = redOOB)
## Defining the Type I, II Green and II Red probes:
probesI <- getProbeInfo(IlluminaHumanMethylation450kmanifest, type = "I")
probesII <- getProbeInfo(IlluminaHumanMethylation450kmanifest, type = "II")
## Chr probes:
locations <- getLocations(rgSet)
chrs <- as.character(seqnames(locations))
names(chrs) <- names(locations)
autosomal <- names(chrs[chrs %in% paste0("chr", 1:22)])
chrY <- names(chrs[chrs %in% paste0("chr", "Y")])
chrX <- names(chrs[chrs %in% paste0("chr", "X")])
probesIGrn <- intersect( probesI$Name[probesI$Color == "Grn"], autosomal )
probesIRed <- intersect( probesI$Name[probesI$Color == "Red"], autosomal )
probesII <- intersect( probesII$Name, autosomal )
uProbeNames <- rownames(beta)
uProbesIGrn <- intersect(uProbeNames, probesIGrn)
uProbesIRed <- intersect(uProbeNames, probesIRed)
uProbesII <- intersect(uProbeNames, probesII)
uProbesX <- intersect(uProbeNames, chrX)
uProbesY <- intersect(uProbeNames, chrY)
indicesIGrn <- match(uProbesIGrn, uProbeNames)
indicesIRed <- match(uProbesIRed, uProbeNames)
indicesII <- match(uProbesII, uProbeNames)
indicesX <- match(uProbesX, uProbeNames)
indicesY <- match(uProbesY, uProbeNames)
indList <- list(indicesIGrn, indicesIRed, indicesII, indicesX, indicesY)
names(indList) <- c("IGrn", "IRed", "II","X","Y")
## Extraction of the quantiles
mQuantiles <- vector("list",5)
betaQuantiles <- vector("list", 5)
methQuantiles <- vector("list", 5)
unmethQuantiles <- vector("list", 5)
cnQuantiles <- vector("list", 5)
names(mQuantiles) <- c("IGrn", "IRed", "II","X","Y")
names(betaQuantiles) <- c("IGrn", "IRed", "II","X","Y")
names(methQuantiles) <- c("IGrn", "IRed", "II","X","Y")
names(unmethQuantiles) <- c("IGrn", "IRed", "II","X","Y")
names(cnQuantiles) <- c("IGrn", "IRed", "II","X","Y")
nq <- 500
probs <- seq(0,1,1/(nq-1))
for (i in 1:5) {
mQuantiles[[i]] <- extractQuantiles(m[indList[[i]],], probs = probs)
betaQuantiles[[i]] <- extractQuantiles(beta[indList[[i]],], probs = probs)
methQuantiles[[i]] <- extractQuantiles(meth[indList[[i]],], probs = probs)
unmethQuantiles[[i]] <- extractQuantiles(unmeth[indList[[i]],], probs = probs)
cnQuantiles[[i]] <- extractQuantiles(cn[indList[[i]],], probs = probs)
}
medianXU <- unmethQuantiles$X[250,]
medianXM <- methQuantiles$X[250,]
medianYU <- unmethQuantiles$Y[250,]
medianYM <- methQuantiles$Y[250,]
XYMedians <- list(medianXU = medianXU,
medianXM = medianXM,
medianYU = medianYU,
medianYM = medianYM
)
return(list(
mQuantiles = mQuantiles,
betaQuantiles = betaQuantiles,
methQuantiles = methQuantiles,
unmethQuantiles = unmethQuantiles,
cnQuantiles = cnQuantiles,
greenControls = greenControls,
redControls = redControls,
XYMedians = XYMedians,
oob = oob))
}