-
Notifications
You must be signed in to change notification settings - Fork 28
/
gds.rmd
407 lines (348 loc) · 11.2 KB
/
gds.rmd
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
```{r ewas-init, echo=FALSE, message=F}
library(knitr)
library(Cairo)
opts_chunk$set(warning=FALSE, fig.width=6, fig.height=6, dev="CairoPNG", stop=TRUE)
library(GEOquery)
```
# Using `gdsfmt` for **very** large datasets
## Download example data set
```{r child = 'dataset-450k-lead.rmd'}
```
```{r}
path <- download.450k.lead.dataset()
```
## Normalize dataset
Create samplesheet
```{r}
library(meffil)
options(mc.cores=10)
samplesheet <- meffil.create.samplesheet(path)
samples <- read.csv(file.path(path, "samples.csv"), check.names=F, row.names=1)
samplesheet <- data.frame(
samplesheet,
samples[match(samplesheet$Sample_Name, rownames(samples)),],
stringsAsFactors=F, check.names=F)
samplesheet <- samplesheet[which(samplesheet[["sample type"]] == "HM450K"),]
```
Parameters.
```{r}
qc.file <- "gds/qc-report.html"
author <- "Sen, et al."
study <- "Cord blood DNA methylation and lead exposure (GSE69633)"
norm.file <- "gds/normalization-report.html"
cell.type.reference <- "gervin and lyle cord blood"
```
Generate QC objects for each sample and QC report.
```{r gds-qc, cache=T}
qc.objects <- meffil.qc(
samplesheet,
cell.type.reference=cell.type.reference,
verbose=T)
qc.summary <- meffil.qc.summary(qc.objects, verbose=T)
meffil.qc.report(
qc.summary,
output.file=qc.file,
author=author,
study=study)
```
Remove any low quality samples.
```{r}
if (nrow(qc.summary$bad.samples) > 0)
qc.objects <- meffil.remove.samples(
qc.objects,
qc.summary$bad.samples$sample.name)
samplesheet <- samplesheet[match(names(qc.objects), rownames(samplesheet)),]
```
Check how many principal components to include.
```{r, dev="CairoPNG"}
print(meffil.plot.pc.fit(qc.objects, n.cross=3)$plot)
```
Ten seems about right.
```{r}
number.pcs <- 10
```
Normalize the dataset.
```{r gds-norm, cache=T}
norm.objects <- meffil.normalize.quantiles(
qc.objects,
number.pcs=number.pcs,
verbose=T)
beta <- meffil.normalize.samples(
norm.objects,
just.beta=T,
remove.poor.signal=T,
cpglist.remove=qc.summary$bad.cpgs$name,
verbose=T)
```
Normalize while saving to a GDS file.
```{r}
dir.create("gds")
gds.filename <- "gds/beta.gds"
if (!file.exists(gds.filename))
meffil.normalize.samples(
norm.objects,
just.beta=T,
remove.poor.signal=T,
cpglist.remove=qc.summary$bad.cpgs$name,
gds.filename=gds.filename,
verbose=T)
```
Load the matrix in the GDS file for comparison.
```{r}
beta.gds <- meffil.gds.methylation(gds.filename)
```
It should be the same as the one generated the standard way.
```{r}
identical(beta.gds, beta)
identical(colnames(beta.gds), colnames(beta))
identical(rownames(beta.gds), rownames(beta))
```
Save detection p-values to a GDS file.
```{r}
detp.filename <- "gds/detp.gds"
if (!file.exists(detp.filename))
meffil.save.detection.pvalues(qc.objects, detp.filename, verbose=T)
```
Compare detection p-values to loading them to a matrix from QC objects.
```{r detection-pvalues, cache=T}
detp.gds <- meffil.gds.detection.pvalues(detp.filename)
detp <- meffil.load.detection.pvalues(qc.objects)
identical(detp, detp.gds)
```
Verify that correct signals have been set to missing
in the methylation matrix.
```{r}
idx <- which(
detp[rownames(beta.gds), colnames(beta.gds)]
>
qc.objects[[1]]$bad.probes.detectionp.threshold)
all(is.na(beta.gds[idx]))
```
We can load a subset from the GDS file as well:
```{r}
bw.sites <- c(
"cg20076442", "cg25953130", "cg04521626", "cg14097568",
"cg17133774", "cg00654448", "cg00442282", "cg13696490",
"cg12044213", "cg08817867", "cg00382138", "cg06870470",
"cg25557739", "cg24324628", "cg15783941", "cg14597739",
"cg22962123", "cg05851442", "cg23387597", "cg24973755",
"cg16219283", "cg25799241", "cg06658067")
## PMID: 25869828 (bonferroni p < 0.05)
samples <- colnames(beta)[c(1,3,5,7)]
sub.gds <- meffil.gds.methylation(gds.filename, bw.sites, samples)
```
It should be the same as subsetting the matrix.
```{r}
quantile(beta[bw.sites,samples] - sub.gds, na.rm=T)
identical(colnames(sub.gds), samples)
identical(rownames(sub.gds), bw.sites)
```
Compute principal components for the normalization report.
```{r gds-pcs, cache=T}
pcs <- meffil.methylation.pcs(beta, winsorize.pct=NA, outlier.iqr.factor=3)
pcs.gds <- meffil.methylation.pcs(gds.filename, winsorize.pct=NA, outlier.iqr.factor=3)
```
The resulting components are exactly the same.
```{r}
identical(pcs.gds, pcs)
```
Run an EWAS from beta matrix or from GDS file:
```{r gds-ewas, cache=T}
variable.name <- "birth weight"
variable <- samplesheet[[variable.name]]
covariates <- data.frame(
t(meffil.cell.count.estimates(qc.objects)),
samplesheet[,c("socioeconomic score","gender","smoke ever",
"gestational age","pbconc (ng/dl)")],
stringsAsFactors=F)
## restrict to autosomal CpG sites as both males and females in dataset
auto.sites <- meffil.get.autosomal.sites("450k")
## from beta matrix
ewas.ret <- meffil.ewas(
beta,
variable=variable, covariates=covariates,
sites=auto.sites,
isva=F, sva=F, smartsva=T,
n.sv=10,
winsorize.pct=NA, outlier.iqr.factor=3,
verbose=T)
## from GDS file
ewas.gds <- meffil.ewas(
gds.filename,
variable=variable, covariates=covariates,
sites=auto.sites,
isva=F, sva=F, smartsva=T,
n.sv=10,
winsorize.pct=NA, outlier.iqr.factor=3,
verbose=T)
```
Verify that we evaluated only autosomal CpG sites.
```{r}
length(setdiff(rownames(ewas.gds$analyses$smartsva$table), auto.sites))==0
```
We obtain similar but not identical results
because `limma` is used to test associations using the beta matrix
and, because `limma` requires access to the entire matrix,
`glm` is used to test associations using the GDS file.
```{r}
sapply(names(ewas.ret$analyses), function(model) {
cor(ewas.ret$analyses[[model]]$table[,"coefficient"],
ewas.gds$analyses[[model]]$table[,"coefficient"], use="p")
})
sapply(names(ewas.ret$analyses), function(model) {
table(matrix=ewas.ret$analyses[[model]]$table[,"p.value"] < 1e-5,
gds=ewas.gds$analyses[[model]]$table[,"p.value"] < 1e-5)
}, simplify=F)
```
Is there some evidence of replicating previously
identified associations with `r variable.name`?
```{r}
sapply(ewas.ret$analyses,
function(obj) quantile(obj$table[bw.sites,"p.value"]))
sapply(ewas.gds$analyses,
function(obj) quantile(obj$table[bw.sites,"p.value"]))
```
Generate the EWAS report using either the beta matrix or the GDS file.
```{r gds-ewas-report, message=F, cache=T}
ewas.parameters <- meffil.ewas.parameters(max.plots=10)
## using the beta matrix
summary.ret <- meffil.ewas.summary(
ewas.ret,
beta,
selected.cpg.sites=bw.sites,
parameters=ewas.parameters, verbose=T)
meffil.ewas.report(summary.ret,
output.file="gds/ewas.html",
author="Me",
study=paste(variable.name,
"in cord blood DNA methylation (GEO:GSE69633)"))
## or using the GDS file
summary.gds <- meffil.ewas.summary(
ewas.gds,
gds.filename,
selected.cpg.sites=bw.sites,
parameters=ewas.parameters, verbose=T)
meffil.ewas.report(summary.gds,
output.file="gds/ewas-gds.html",
author="Me",
study=paste(variable.name,
"in cord blood DNA methylation (GEO:GSE69633)"))
```
Robust linear regression in EWAS is also possible.
When analysing the beta matrix, we test associations
using `limma::lmFit` with the 'robust' option
which applies `MASS::rlm` to each CpG site.
When analysing the GDS file, we similarly
test associations using `MASS::rlm`
and then
calculate statistical significance using `lmtest::coeftest`
with `vcov=sandwich::vcovHC(fit, type="HC0")`.
```{r gds-ewas-robust, cache=T}
## from matrix
robust.ret <- meffil.ewas(
beta,
variable=variable, covariates=covariates,
sites=auto.sites,
isva=F, sva=F, smartsva=T,
n.sv=10,
rlm=T,
winsorize.pct=NA, outlier.iqr.factor=3,
verbose=T)
## from GDS file
robust.gds <- meffil.ewas(
gds.filename,
variable=variable, covariates=covariates,
sites=auto.sites,
isva=F, sva=F, smartsva=T,
n.sv=10,
rlm=T,
winsorize.pct=NA, outlier.iqr.factor=3,
verbose=T)
```
As expected, results somewhat different for robust regression.
```{r}
sapply(names(robust.ret$analyses), function(model) {
cor(ewas.ret$analyses[[model]]$table[,"coefficient"],
robust.ret$analyses[[model]]$table[,"coefficient"], use="p")
})
sapply(names(robust.ret$analyses), function(model) {
table(ewas=ewas.ret$analyses[[model]]$table[,"p.value"] < 1e-5,
robust=robust.ret$analyses[[model]]$table[,"p.value"] < 1e-5)
},simplify=F)
```
Between the robust matrix and GDS analyses,
coefficients identical but p-values different because
statistical handled differently in 'limma'.
```{r}
sapply(names(robust.ret$analyses), function(model) {
cor(robust.ret$analyses[[model]]$table[,"coefficient"],
robust.gds$analyses[[model]]$table[,"coefficient"], use="p")
})
sapply(names(robust.ret$analyses), function(model) {
table(matrix=robust.ret$analyses[[model]]$table[,"p.value"] < 1e-5,
gds=robust.gds$analyses[[model]]$table[,"p.value"] < 1e-5)
}, simplify=F)
```
Finally, it is possible to perform EWAS on subsamples of the dataset.
We'll remove some potential lead exposure outliers from analysis.
```{r}
pb <- samplesheet[["pbconc (ng/dl)"]]
samples <- samplesheet[["Sample_Name"]][pb < 8]
```
```{r gds-ewas-subset, cache=T}
## from matrix
sub.ret <- meffil.ewas(
beta,
variable=variable, covariates=covariates,
sites=auto.sites,
samples=samples,
isva=F, sva=F, smartsva=T,
n.sv=10,
winsorize.pct=NA, outlier.iqr.factor=3,
verbose=T)
## from GDS file
sub.gds <- meffil.ewas(
gds.filename,
variable=variable, covariates=covariates,
sites=auto.sites,
samples=samples,
isva=F, sva=F, smartsva=T,
n.sv=10,
winsorize.pct=NA, outlier.iqr.factor=3,
verbose=T)
```
Check that the model used the correct samples.
```{r}
identical(samples, rownames(sub.gds$analyses$none$design))
```
Again, coefficients are identical but statistical
significances slightly different.
```{r}
sapply(names(sub.ret$analyses), function(model) {
cor(sub.ret$analyses[[model]]$table[,"coefficient"],
sub.gds$analyses[[model]]$table[,"coefficient"], use="p")
})
sapply(names(sub.ret$analyses), function(model) {
table(matrix=sub.ret$analyses[[model]]$table[,"p.value"] < 1e-5,
gds=sub.gds$analyses[[model]]$table[,"p.value"] < 1e-5)
}, simplify=F)
```
There are only slight differences from the full dataset analysis.
```{r}
sapply(names(sub.gds$analyses), function(model) {
cor(sub.gds$analyses[[model]]$table[,"coefficient"],
ewas.gds$analyses[[model]]$table[,"coefficient"], use="p")
})
sapply(names(sub.gds$analyses), function(model) {
table(sub=sub.gds$analyses[[model]]$table[,"p.value"] < 1e-5,
full=ewas.gds$analyses[[model]]$table[,"p.value"] < 1e-5)
}, simplify=F)
```
It is also possible to apply a function to each CpG site
or each sample in the dataset.
```{r gds-apply, cache=T}
v.gds <- meffil:::meffil.gds.apply(
gds.filename, bysite=T, type="double", FUN=var, na.rm=T)
v <- rowVars(beta, na.rm=T)
all(abs(v-v.gds) < 2e-16)
```