-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDNA_methylation_QC_417993.Rmd
351 lines (295 loc) · 16.9 KB
/
DNA_methylation_QC_417993.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
---
title: "GDC QC DNA Methylation"
output:
html_document:
toc: true
toc_float: true
---
```{r setup, include=FALSE}
## default options
## this is "GDC QC DNA Methylation" report
## on http://rpubs.com/zhouwanding/417993
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(cache = TRUE)
library(tidyverse)
library(GenomicRanges)
library(knitr)
library(treemap)
library(scales)
outputdir <- '/secondary/projects/laird/projects/2018_05_02_Wanding_tools/GDC_DNA_methylation_QC/output/'
```
The output can be found here:
https://rpubs.com/zhouwanding/417993
# hg19 Legacy (GDC r1.0-3.0)
## File Counts
```{r message=FALSE}
df_legacy <- list(
hm27=read_tsv(url('https://raw.githubusercontent.com/zwdzwd/GDC_DNA_methylation_QC/master/file_lists/20180410_GDC_manifest_legacy.tsv_HM27_lvl3.tsv')),
hm450=read_tsv(url('https://raw.githubusercontent.com/zwdzwd/GDC_DNA_methylation_QC/master/file_lists/20180410_GDC_manifest_legacy.tsv_HM450_lvl3.tsv'))
)
sapply(df_legacy, nrow)
```
Cases/patients (first 12 letters of TCGA barcode)
```{r}
sapply(df_legacy, function(x) length(unique(substr(x$TCGA_barcode,1,12))))
```
## Data
```{r message=FALSE}
dfdata_legacy <- list(
hm27=read_tsv(url('https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/sample_data/release1-3_legacy/07e1bc0f-b2d9-41cb-bf4d-1a9ac769654a/jhu-usc.edu_COAD.HumanMethylation27.4.lvl-3.TCGA-AA-A004-01A-01D-A00B-05.txt'),skip=1),
hm450=read_tsv(url('https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/sample_data/release1-3_legacy/3d4a102e-4a4c-48c3-ad51-8cc9f9b879e7/jhu-usc.edu_KIRC.HumanMethylation450.7.lvl-3.TCGA-DV-A4W0-05A-11D-A264-05.txt'),skip=1))
dfdata_legacy$hm27 %>% head
dfdata_legacy$hm450 %>% head
```
# hg38 LiftOver (GDC r4.0-12.0)
## File Counts
```{r message=FALSE}
df_R4 <- list(
hm27=read_tsv(url('https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/file_lists/20180410_GDC_manifest_liftOver_workflow.tsv_HM27_lvl3.tsv')),
hm450=read_tsv(url('https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/file_lists/20180410_GDC_manifest_liftOver_workflow.tsv_HM450_lvl3.tsv'))
)
sapply(df_R4, nrow)
```
Cases/patients (first 12 letters of TCGA barcode)
```{r}
sapply(df_R4, function(x) length(unique(substr(x$TCGA_barcode,1,12))))
```
## Data
```{r message=FALSE}
dfdata_R4 <- list(
hm27=read_tsv(url('https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/sample_data/release4_plus_hg38/df4a53cd-6d07-41be-a7b7-c381a658e7ae/jhu-usc.edu_COAD.HumanMethylation27.4.lvl-3.TCGA-AA-A004-01A-01D-A00B-05.gdc_hg38.txt')),
hm450=read_tsv(gzcon(url('https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/sample_data/release4_plus_hg38/ef164350-d911-4a3d-a53a-b2efed5e682c/jhu-usc.edu_KIRC.HumanMethylation450.7.lvl-3.TCGA-DV-A4W0-05A-11D-A264-05.gdc_hg38.txt.gz'))))
dfdata_R4$hm27 %>% head
dfdata_R4$hm450 %>% head
```
# Compare hg19 and hg38
## Measurements
Measurements are the same, only the coordinates and gene annotation have changed.
```{r}
sapply(dfdata_R4, nrow)
sapply(dfdata_legacy, nrow)
```
```{r}
all(dfdata_legacy$hm450$Beta_value == dfdata_R4$hm450$Beta_value, na.rm = TRUE)
```
## Coordinates
Unmapped probes on hg38 are annotated with "*".
## Fraction of probes unmapped in hg38
#### HM27
```{r}
tb <- table(dfdata_R4$hm27$Chromosome)
kable(tb)
(1-unname(tb['*'])/sum(tb)) * 100 # percentage of mapped probes
```
#### HM450
```{r}
tb <- table(dfdata_R4$hm450$Chromosome)
kable(tb)
(1-unname(tb['*'])/sum(tb)) * 100 # percentage of mapped probes
```
## Fraction of unmapped
```{r}
kable(sapply(dfdata_R4, function(x) table(x$Chromosome=='*')))
# legacy doesn't have any unmapped probes
sapply(dfdata_legacy, function(x) table(x$Chromosome=='*'))
```
## Probe Mapping by Chromosome
Most probes are mapped to the same chromosome.
```{r}
kable(table(dfdata_legacy$hm450$Chromosome, dfdata_R4$hm450$Chromosome))
kable(table(dfdata_legacy$hm27$Chromosome, dfdata_R4$hm27$Chromosome))
```
## CpGs Targeted by Multiple Probes
### HM450
```{r}
dfdata_R4$hm450 %>% filter(Chromosome!='*') %>% arrange(Chromosome, Start) -> dfdata_R4_bycoord
dup_index <- which(dfdata_R4_bycoord$Start[-1] == dfdata_R4_bycoord$Start[-nrow(dfdata_R4_bycoord)])
dup_probe_index <- sort(unique(c(dup_index, dup_index+1)))
dfdata_R4_bycoord[dup_probe_index,] %>% dplyr::select(Chromosome, Start, End, 'Composite Element REF')
```
```{r eval=FALSE, include=FALSE}
dfdata_R4_bycoord[dup_probe_index,] %>% write_tsv(file.path(outputdir, 'HM450_hg38_multiprobe_cpgs.tsv'))
```
Under GRCh38, `r nrow(unique(dfdata_R4_bycoord[dup_probe_index,'Start']))` CpGs become interrogated with multiple probes (`r length(dup_probe_index)` probes). Note this is after excluding MASK_mapping. The full list is available at [here](https://github.com/zwdzwd/GDC_DNA_methylation_QC/blob/master/output/HM450_hg38_multiprobe_cpgs.tsv).
### HM27
No such CpGs exist for HM27
```{r}
dfdata_R4$hm27 %>% filter(Chromosome!='*') %>% arrange(Chromosome, Start) -> dfdata_R4_bycoord
which(dfdata_R4_bycoord$Start[-1] == dfdata_R4_bycoord$Start[-nrow(dfdata_R4_bycoord)])
```
## Probe Mapping by Quality
```{r eval=FALSE, include=FALSE}
dir.create('~/Downloads/tmp/20180808/', showWarnings = FALSE)
download.file('http://zwdzwd.io/InfiniumAnnotation/20180808/hm450/hm450.hg38.manifest.rds', '~/Downloads/tmp/20180808/hm450.hg38.manifest.rds')
download.file('http://zwdzwd.io/InfiniumAnnotation/20180808/hm450/hm450.hg19.manifest.rds', '~/Downloads/tmp/20180808/hm450.hg19.manifest.rds')
```
```{r}
HM450.hg38.manifest <- readRDS('~/Downloads/tmp/20180808/hm450.hg38.manifest.rds')
HM450.hg19.manifest <- readRDS('~/Downloads/tmp/20180808/hm450.hg19.manifest.rds')
```
#### All probes
```{r}
da <- HM450.hg19.manifest
db <- HM450.hg38.manifest[names(da)]
a <- cut(da[da$designType=='I']$mapQ_A, c(0,10,40,59,60), include.lowest = T)
b <- cut(db[da$designType=='I']$mapQ_A, c(0,10,40,59,60), include.lowest = T)
tble <- table(a, b)
tble['(59,60]','(59,60]'] / sum(tble) # Fraction of unique mapping
```
#### Type-I
```{r}
a <- cut(da[da$designType=='I']$mapQ_A, c(0,10,40,59,60), include.lowest = T)
b <- cut(db[da$designType=='I']$mapQ_A, c(0,10,40,59,60), include.lowest = T)
tble <- table(a, b)
kable(tble, caption='Mapping quality cross comparison type cg Type-I A')
a <- cut(da[da$designType=='I']$mapQ_B, c(0,10,40,59,60), include.lowest = T)
b <- cut(db[da$designType=='I']$mapQ_B, c(0,10,40,59,60), include.lowest = T)
tble <- table(a, b)
kable(tble, caption='Mapping quality cross comparison type cg Type-I B')
```
#### Type-II
```{r}
a <- cut(da[da$designType=='II']$mapQ_A, c(0,10,40,59,60), include.lowest = T)
b <- cut(db[da$designType=='II']$mapQ_A, c(0,10,40,59,60), include.lowest = T)
tble <- table(a, b)
kable(tble, caption='Mapping quality cross comparison type cg type II')
```
## Probe Mapping by Mismatch
#### A-allele
```{r}
tble <- table(da[da$probeType!='rs']$NM_A, db[db$probeType!='rs']$NM_A)
kable(tble)
tble['0','0'] / sum(tble) # Fraction of perfect mapping
tble_decoy <- table(da[da$probeType!='rs']$wDecoy_NM_A, db[db$probeType!='rs']$wDecoy_NM_A)
kable(tble_decoy)
tble_decoy['0','0'] / sum(tble_decoy) # Fraction of perfect mapping with decoy
tble_decoy['0','0'] - tble['0','0'] # Number of probes moved to decoy
```
#### B-allele
```{r}
tble <- table(da[da$probeType!='rs']$NM_B, db[db$probeType!='rs']$NM_B)
kable(tble)
tble['0','0'] / sum(tble) # Fraction of perfect mapping
tble_decoy <- table(da[da$probeType!='rs']$wDecoy_NM_B, db[db$probeType!='rs']$wDecoy_NM_B)
kable(tble_decoy)
tble_decoy['0','0'] / sum(tble_decoy) # Fraction of perfect mapping with decoy
tble_decoy['0','0'] - tble['0','0'] # Number of probes moved to decoy
```
#### SNP probes
One probe switched the REF and ALT.
```{r}
kable(table(da[da$designType=='I' & da$probeType=='rs']$NM_A, db[db$designType=='I' & db$probeType=='rs']$NM_A))
kable(table(da[da$designType=='I' & da$probeType=='rs']$NM_B, db[db$designType=='I' & db$probeType=='rs']$NM_B))
kable(table(da[da$designType=='II' & da$probeType=='rs']$NM_A, db[db$designType=='II' & db$probeType=='rs']$NM_A))
```
## Gene Association
#### HM450
```{r}
all(dfdata_R4$hm450$`Composite Element REF` == dfdata_legacy$hm450$`Composite Element REF`)
dfdata_R4$hm450$geneUniq <- sapply(strsplit(dfdata_R4$hm450$Gene_Symbol, ';'), function(x) paste0(sort(unique(x)), collapse = ';'))
dfdata_R4$hm450$geneType <- sapply(strsplit(dfdata_R4$hm450$Gene_Type, ';'), function(x) paste0(unique(x), collapse = ';'))
df <- dfdata_R4$hm450 %>%
dplyr::rename(probe='Composite Element REF') %>%
dplyr::select(probe, geneUniq, geneType) %>%
cbind(dfdata_legacy$hm450) %>%
mutate(geneUniq=replace(geneUniq, geneUniq=='.',NA)) %>%
dplyr::rename(gene_hg38=geneUniq) %>%
mutate(gene_hg19 = sapply(strsplit(Gene_Symbol, ';'), function(x) paste0(sort(unique(x)), collapse = ';'))) %>%
mutate(gene_hg19 = replace(gene_hg19, gene_hg19=='',NA))
```
More probes are associated with gene in GRCh38.
```{r}
tb <- table(!is.na(df$gene_hg38), !is.na(df$gene_hg19))
rownames(tb) <- c('Not Annotated in hg38','Annotated in hg38')
colnames(tb) <- c('Not Annotated in hg19','Annotated in hg19')
kable(tb)
kable(tb/sum(tb))
```
`r sum(!is.na(df$gene_hg19))` in `r nrow(df)` probes are associated with gene in hg19. `r sum(!is.na(df$gene_hg38))` in `r nrow(df)` probes are associated with gene in hg38. `r df %>% filter(!is.na(gene_hg38), !is.na(gene_hg19)) %>% summarise(sum(gene_hg38==gene_hg19))` probes are annotated with exactly the same genes.
```{r}
df$hg19_in_hg38 <- apply(df, 1, function(x) all(sapply(strsplit(x['gene_hg19'],';')[[1]], function(xx) grepl(xx,x['gene_hg38']))))
df$hg19_in_hg38 <- ifelse(is.na(df$gene_hg19), TRUE, df$hg19_in_hg38)
df$hg38_in_hg19 <- apply(df, 1, function(x) all(sapply(strsplit(x['gene_hg38'],';')[[1]], function(xx) grepl(xx,x['gene_hg19']))))
df$hg38_in_hg19 <- ifelse(is.na(df$gene_hg38), TRUE, df$hg38_in_hg19)
```
```{r}
df_identical <- (df %>% filter((is.na(gene_hg19) & is.na(gene_hg38)) | (hg19_in_hg38 & hg38_in_hg19)))
df_different <- (df %>% filter(!((is.na(gene_hg19) & is.na(gene_hg38)) | (hg19_in_hg38 & hg38_in_hg19))))
```
`r nrow(df_identical)` (`r nrow(df_identical)/nrow(df)*100`%) probes probes have identical gene annotations. `r df_different %>% filter(hg19_in_hg38) %>% nrow` (`r (df_different %>% filter(hg19_in_hg38) %>% nrow)/nrow(df)*100`%) probes annotated in hg19 is not included in genes annotated hg38. Only `r df_different %>% filter(!hg19_in_hg38) %>% nrow` (`r (df_different %>% filter(!hg19_in_hg38) %>% nrow)/nrow(df)*100`%) probes lost gene annotation from hg19 in hg38. `r df_different %>% filter(!hg19_in_hg38) %>% filter(grepl('LOC', gene_hg19) | grepl('orf', gene_hg19)) %>% nrow` probes are either with 'orf' or 'LOC' names, indicating an update of gene annotation from resolving these identifiers. `r df_different %>% filter(!hg38_in_hg19) %>% nrow` (`r (df_different %>% filter(!hg38_in_hg19) %>% nrow)/nrow(df)*100`%) probes gain new annotation in hg38 compared to hg19. A substantial parts are due to the addition of non-coding genes.
```{r}
df_different %>% filter(!hg38_in_hg19) %>% summarise(
antisense=sum(grepl('antisense', geneType))/n()*100,
lincRNA=sum(grepl('lincRNA', geneType))/n()*100,
miRNA=sum(grepl('miRNA', geneType))/n()*100,
processed_transcript=sum(grepl('processed_transcript', geneType))/n()*100,
pseudogene=sum(grepl('pseudogene', geneType))/n()*100)
df_different %>% head
```
```{r eval=FALSE, include=FALSE}
df %>% mutate(different_gene_annotation=!((is.na(gene_hg19) & is.na(gene_hg38)) | (hg19_in_hg38 & hg38_in_hg19))) %>% write_tsv(file.path(outputdir,'HM450_probes_with_different_gene_annotation_column.tsv'))
```
The complete gene discordance table can be downloaded at [https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/output/HM450_probes_with_different_gene_annotation_column.tsv](https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/output/HM450_probes_with_different_gene_annotation_column.tsv)
```{r}
df %>% mutate(same=ifelse(hg19_in_hg38&hg38_in_hg19, 'CONCORDANT', ifelse(hg19_in_hg38, 'AUGMENTED', 'DISCORDANT')), geneType=sapply(strsplit(geneType,';'), function(x) paste0(sort(x), collapse=';'))) %>% mutate(geneType=replace(geneType, geneType=='.', 'NA')) -> df1
df1 %>% count(same) %>% mutate(prop=prop.table(n))
df1 %>% group_by(same, geneType) %>% summarise(freq=n()) %>% ungroup() %>% mutate(geneType = sprintf('%s (%1.2f%%)', sub(';',' ',geneType), freq/sum(freq)*100)) -> df2
# vocabulary_hg19 <- do.call(c,strsplit(df$gene_hg19,';'))
# pdf('~/gallery/20180819_gdc_qc_treemap_hm450.pdf')
treemap(df2, index=c('same','geneType'),vSize='freq',palette='Blues')
```
#### HM27
```{r}
all(dfdata_R4$hm27$`Composite Element REF` == dfdata_legacy$hm27$`Composite Element REF`)
dfdata_R4$hm27$geneUniq <- sapply(strsplit(dfdata_R4$hm27$Gene_Symbol, ';'), function(x) paste0(sort(unique(x)), collapse = ';'))
dfdata_R4$hm27$geneType <- sapply(strsplit(dfdata_R4$hm27$Gene_Type, ';'), function(x) paste0(unique(x), collapse = ';'))
df <- dfdata_R4$hm27 %>%
dplyr::rename(probe='Composite Element REF') %>%
dplyr::select(probe, geneUniq, geneType) %>%
cbind(dfdata_legacy$hm27) %>%
mutate(geneUniq=replace(geneUniq, geneUniq=='.',NA)) %>%
dplyr::rename(gene_hg38=geneUniq) %>%
mutate(gene_hg19 = sapply(strsplit(Gene_Symbol, ';'), function(x) paste0(sort(unique(x)), collapse = ';'))) %>%
mutate(gene_hg19 = replace(gene_hg19, gene_hg19=='',NA))
```
More probes are associated with gene in GRCh38.
```{r}
tb <- table(!is.na(df$gene_hg38), !is.na(df$gene_hg19))
rownames(tb) <- c('Not Annotated in hg38','Annotated in hg38')
colnames(tb) <- c('Not Annotated in hg19','Annotated in hg19')
kable(tb)
kable(tb/sum(tb))
```
`r sum(!is.na(df$gene_hg19))` in `r nrow(df)` probes are associated with gene in hg19. `r sum(!is.na(df$gene_hg38))` in `r nrow(df)` probes are associated with gene in hg38. `r df %>% filter(!is.na(gene_hg38), !is.na(gene_hg19)) %>% summarise(sum(gene_hg38==gene_hg19))` probes are annotated with exactly the same genes.
```{r}
df$hg19_in_hg38 <- apply(df, 1, function(x) all(sapply(strsplit(x['gene_hg19'],';')[[1]], function(xx) grepl(xx,x['gene_hg38']))))
df$hg19_in_hg38 <- ifelse(is.na(df$gene_hg19), TRUE, df$hg19_in_hg38)
df$hg38_in_hg19 <- apply(df, 1, function(x) all(sapply(strsplit(x['gene_hg38'],';')[[1]], function(xx) grepl(xx,x['gene_hg19']))))
df$hg38_in_hg19 <- ifelse(is.na(df$gene_hg38), TRUE, df$hg38_in_hg19)
```
```{r}
df_identical <- (df %>% filter((is.na(gene_hg19) & is.na(gene_hg38)) | (hg19_in_hg38 & hg38_in_hg19)))
df_different <- (df %>% filter(!((is.na(gene_hg19) & is.na(gene_hg38)) | (hg19_in_hg38 & hg38_in_hg19))))
```
`r nrow(df_identical)` (`r nrow(df_identical)/nrow(df)*100`%) probes have identical gene annotations. `r df_different %>% filter(hg19_in_hg38) %>% nrow` (`r (df_different %>% filter(hg19_in_hg38) %>% nrow)/nrow(df)*100`%) probes annotated in hg19 is not included in genes annotated hg38. Only `r df_different %>% filter(!hg19_in_hg38) %>% nrow` (`r (df_different %>% filter(!hg19_in_hg38) %>% nrow)/nrow(df)*100`%) probes lost gene annotation from hg19 in hg38. `r df_different %>% filter(!hg19_in_hg38) %>% filter(grepl('LOC', gene_hg19) | grepl('orf', gene_hg19)) %>% nrow` probes are either with 'orf' or 'LOC' names, indicating an update of gene annotation from resolving these identifiers. `r df_different %>% filter(!hg38_in_hg19) %>% nrow` (`r (df_different %>% filter(!hg38_in_hg19) %>% nrow)/nrow(df)*100`%) probes gain new annotation in hg38 compared to hg19. A substantial parts are due to the addition of non-coding genes.
```{r}
df_different %>% filter(!hg38_in_hg19) %>% summarise(
antisense=sum(grepl('antisense', geneType))/n()*100,
lincRNA=sum(grepl('lincRNA', geneType))/n()*100,
miRNA=sum(grepl('miRNA', geneType))/n()*100,
processed_transcript=sum(grepl('processed_transcript', geneType))/n()*100,
pseudogene=sum(grepl('pseudogene', geneType))/n()*100)
df_different %>% head
```
```{r eval=FALSE, include=FALSE}
df %>% mutate(different_gene_annotation=!((is.na(gene_hg19) & is.na(gene_hg38)) | (hg19_in_hg38 & hg38_in_hg19))) %>% write_tsv(file.path(outputdir,'HM27_probes_with_different_gene_annotation_column.tsv'))
```
The complete gene discordance table can be downloaded at [https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/output/HM27_probes_with_different_gene_annotation_column.tsv](https://github.com/zwdzwd/GDC_DNA_methylation_QC/raw/master/output/HM27_probes_with_different_gene_annotation_column.tsv)
```{r}
df %>% mutate(same=ifelse(hg19_in_hg38&hg38_in_hg19, 'CONCORDANT', ifelse(hg19_in_hg38, 'AUGMENTED', 'DISCORDANT')), geneType=sapply(strsplit(geneType,';'), function(x) paste0(sort(x), collapse=';'))) %>% mutate(geneType=replace(geneType, geneType=='.', 'NA')) -> df1
df1 %>% count(same) %>% mutate(prop=prop.table(n))
df1 %>% group_by(same, geneType) %>% summarise(freq=n()) %>% ungroup() %>% mutate(geneType = sprintf('%s (%1.2f%%)', sub(';',' ',geneType), freq/sum(freq)*100)) -> df2
# vocabulary_hg19 <- do.call(c,strsplit(df$gene_hg19,';'))
# pdf('~/gallery/20180819_gdc_qc_treemap_hm450.pdf')
treemap(df2, index=c('same','geneType'),vSize='freq',palette='Blues')
```