-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhcluster.R
455 lines (336 loc) · 16.4 KB
/
hcluster.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
###########################################################################################################
# This script contains clustering procedure for:
# (A)pre-normalized
# (B)post-normalized
# (C)post-normalized and filtered by pairwise ctrl group comparison
###########################################################################################################
#####################################################
##### Clustering for pre-normalized raw data:
##### 1.) Probe-based (485577 probes for the CPG sites)
##### 2.) CPG-island-based (median or mean)
##### 3.) (TBA)Gene-based ? DM-genes-based
#####################################################
setwd('/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/')
library(RColorBrewer)
##### Load raw data (modify the path accordingly)
load('Data/All_3_metasets.Rdata')
load('Data/All_3_sets.Rdata')
#####################################################
##### (1) probe level: clustering by Belta values #####
# merge 3 data sets and design
merged.dat <- cbind(APL.dat, ALL.dat, CTRL.dat)
merged.des <- data.frame(Samples=colnames(merged.dat), Group=c(APL.meta$Group, ALL.meta$Group, rep('HBC', 9)))
# compute pairwise distances
pr.dis <- dist(t(merged.dat), method = "euclidean")
# sanity check between the sample name and group assigned
pr.nms <- with(merged.des, paste(Samples, Group, sep = "_"))
all(unlist(lapply(pr.nms, function(x) substr(x, 1, 3)==substr(x, nchar(x)-2, nchar(x))))) # TRUE
# Explore which clustering method performs the best
# compute hierarchical clustering using different linkage types
pr.hc.s <- hclust(pr.dis, method = "single")
pr.hc.c <- hclust(pr.dis, method = "complete")
pr.hc.a <- hclust(pr.dis, method = "average")
pr.hc.w <- hclust(pr.dis, method = "ward")
# plot the dendrograms
# (I) overview of the 4 clustering methods w/o sample labels
pdf("Figures/clusterOverviewByProbe_4Methods_raw.pdf")
par(mar = c(0, 4, 4, 2))
par(mfrow = c(2, 2))
plot(pr.hc.s, labels = FALSE, main = "Single", xlab = "")
plot(pr.hc.c, labels = FALSE, main = "Complete", xlab = "")
plot(pr.hc.a, labels = FALSE, main = "Average", xlab = "")
plot(pr.hc.w, labels = FALSE, main = "Ward", xlab = "")
dev.off()
# (II) plot for each individual method with sample labels to judge which one groups the samples the best
# ward --> FINAL CHOICE
pdf("Figures/clusterByProbe_ward_raw.pdf")
par(mfrow = c(1, 1))
# decide how many clusters we should cut
unique(merged.des$Group) # 5 categories: ALL HBC HBM PLP PLR
# label the leaf nodes by coloring them according to their groups
ward.dendro <- as.dendrogram(pr.hc.w)
# color code each node of the dendrogram according to their group
colors <- brewer.pal(5, 'Set1')
names(colors) <- c("ALL", "HBC", "HBM", "PLP", "PLR")
colLab <- function(n) {
if(is.leaf(n)) {
a <- attributes(n)
# find group name
a.group <- substr(a$label, 1, 3)
# retrieve the corresponding color
attr(n, "nodePar") <-
c(a$nodePar, list(lab.col = colors[a.group],lab.bg='grey50',pch=sample(19:25,1)))
attr(n, "frame.plot") <- TRUE
}
n
}
clusDendro <- dendrapply(ward.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "Ward showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w, k = 5, border=c("cyan"))
dev.off()
# single
pdf("Figures/clusterByProbe_single_raw.pdf")
par(mfrow = c(1, 1))
plot(pr.hc.s, labels = merged.des$Samples, cex = 0.6, main = "single showing 5 clusters")
rect.hclust(pr.hc.s, k = 5)
dev.off()
# complete --> Not as good as Ward, tho better than single and average
pdf("Figures/clusterByProbe_complete_raw.pdf")
par(mfrow = c(1, 1))
# color leaf node of the dendrogram
comp.dendro <- as.dendrogram(pr.hc.c)
clusDendro <- dendrapply(comp.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "complete showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
#plot(pr.hc.c, labels = merged.des$Samples, cex = 0.6, main = "complete showing 5 clusters")
rect.hclust(pr.hc.c, k = 5, border=c("cyan"))
dev.off()
# Average
pdf("Figures/clusterByProbe_Average_raw.pdf")
par(mfrow = c(1, 1))
plot(pr.hc.a, labels = merged.des$Samples, cex = 0.6, main = "Average showing 5 clusters")
rect.hclust(pr.hc.a, k = 5)
dev.off()
# (III) headmap with ward dendrogram
pdf("Figures/heatmap_ClusterByProbe_raw.pdf")
# scale
sMerged.Dat <- t(scale(t(merged.dat)))
heatmap(as.matrix(sMerged.Dat), Rowv = NA, Colv = NULL, hclustfun = function(x) hclust(x,
method = "ward"), distfun = function(x) dist(x, method = "euclidean"), scale = "none",
labCol = pr.nms, labRow = NA, margin = c(8, 1), ColSideColor = brewer.pal(11,
"RdGy")[c(4, 7)][unclass(merged.des$Group)])
dev.off()
# (IV) another overview plot for ward vs. complete using the scaled data
pdf("Figures/clusterOverviewByProbe_wad_com_scaledRaw.pdf")
par(mar = c(0, 4, 4, 2))
par(mfrow = c(2, 1))
# recalculate pairwise distance
pr.dis.s <- dist(t(sMerged.Dat), method = "euclidean")
pr.hc.c.s <- hclust(pr.dis.s, method = "complete")
pr.hc.w.s <- hclust(pr.dis.s, method = "ward")
plot(pr.hc.c.s, labels = merged.des$Samples, cex = 0.6, main = "complete showing 5 clusters (scaled)")
rect.hclust(pr.hc.c.s, k = 5)
plot(pr.hc.w.s, labels = merged.des$Samples, cex = 0.6, main = "Ward showing 5 clusters (scaled)")
rect.hclust(pr.hc.w.s, k = 5)
dev.off()
# (v) another ward plot using the scaled data: the ALL samples have a better clustering here
pdf("Figures/clusterByProbe_ward_scaledRaw.pdf")
par(mfrow = c(1, 1))
# label the leaf nodes by coloring them according to their groups
ward.dendro.s <- as.dendrogram(pr.hc.w.s)
clusDendro <- dendrapply(ward.dendro.s, colLab)
plot(clusDendro, cex = 0.6, main = "Scaled Ward showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w.s, k = 5, border=c("cyan"))
dev.off()
#####################################################
##### (2) cpgi level: clustering by Belta values #####
##### Load the raw probe data
#load('Data/CPGI2Probe_betaList_raw.Rdata')
### (I) ward plot for the mean cpgi belta values
# construct a data frame with rows = cpgi IDs, columns = mean belta value for each sample
load('Data/CPGI_betaMeanList_raw.Rdata')
#### Transpose to place the cpgi features in rows
cpgi.Beta.mean <- lapply(cpgi.Beta.mean, t)
cpgi.Beta.mean.whole <- do.call(cbind, cpgi.Beta.mean)
dim(cpgi.Beta.mean.whole)
# [1] 27176 52
# ward cluster based on the mean cpgi belta values
# compute pairwise distances
pr.dis.mean <- dist(t(cpgi.Beta.mean.whole), method = "euclidean")
pr.hc.w.mean <- hclust(pr.dis.mean, method = "ward")
pdf("Figures/clusterByMeanCPGI_ward_Raw.pdf")
par(mfrow = c(1, 1))
# label the leaf nodes by coloring them according to their groups
mean.ward.dendro <- as.dendrogram(pr.hc.w.mean)
clusDendro <- dendrapply(mean.ward.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "CPGI-based Ward(mean) Clustering showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w.mean, k = 5, border=c("cyan"))
dev.off()
# (II) ward plot for the median cpgi belta values
load('Data/CPGI_betaMedianList_raw.Rdata')
#### Transpose to place the cpgi features in rows
cpgi.Beta.median <- lapply(cpgi.Beta.median, t)
cpgi.Beta.median.whole <- do.call(cbind, cpgi.Beta.median)
dim(cpgi.Beta.median.whole)
# [1] 27176 52
# ward cluster based on the median cpgi belta values
# compute pairwise distances
pr.dis.median <- dist(t(cpgi.Beta.median.whole), method = "euclidean")
pr.hc.w.median <- hclust(pr.dis.median, method = "ward")
pdf("Figures/clusterByMedianCPGI_ward_Raw.pdf")
par(mfrow = c(1, 1))
# label the leaf nodes by coloring them according to their groups
median.ward.dendro <- as.dendrogram(pr.hc.w.median)
clusDendro <- dendrapply(median.ward.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "CPGI-based Ward(median) Clustering showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w.median, k = 5, border=c("cyan"))
dev.off()
#####################################################
##### Clustering for post-normalized data:
##### 1.) Probe-based (485577 probes for the CPG sites)
##### 2.) CPG-island-based
##### 3.) Gene-based ?DM-genes-based
#####################################################
# load the data
load('Data/All_3_sets_normalized.Rdata')
dim(ALL.norm)
# [1] 485577 33
#####################################################
##### (1) probe level: clustering by Belta values #####
# compute pairwise distances
merged.dat <- cbind(APL.norm, ALL.norm, CTRL.norm)
pr.dis <- dist(t(merged.dat), method = "euclidean")
# sanity check between the sample name and group assigned
merged.des <- data.frame(Samples=colnames(merged.dat), Group=c(APL.meta$Group, ALL.meta$Group, rep('HBC', 9)))
pr.nms <- with(merged.des, paste(Samples, Group, sep = "_"))
all(unlist(lapply(pr.nms, function(x) substr(x, 1, 3)==substr(x, nchar(x)-2, nchar(x))))) # TRUE
# compute hierarchical clustering using ward linkage type
pr.hc.w <- hclust(pr.dis, method = "ward")
# ward
pdf("Figures/clusterByProbe_ward_norm.pdf")
par(mfrow = c(1, 1))
# label the leaf nodes by coloring them according to their groups
ward.dendro <- as.dendrogram(pr.hc.w)
# color code each node of the dendrogram according to their group
clusDendro <- dendrapply(ward.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "Ward showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w, k = 5, border=c("cyan"))
dev.off()
#####################################################
##### (2) cpgi level: clustering by Belta values #####
##### Load the raw probe data
#load('Data/CPGI2Probe_betaList_raw.Rdata')
### (I) ward plot for the mean cpgi belta values
# construct a data frame with rows = cpgi IDs, columns = mean belta value for each sample
load('Data/CPGI_betaMeanList_norm.Rdata')
dim(cpgi.Beta.mean$ALL)
# [1] 33 27176
#### Transpose to place the cpgi features in rows
cpgi.Beta.mean <- lapply(cpgi.Beta.mean, t)
cpgi.Beta.mean.whole <- do.call(cbind, cpgi.Beta.mean)
dim(cpgi.Beta.mean.whole)
# [1] 27176 52
# ward cluster based on the mean cpgi belta values
# compute pairwise distances
pr.dis.mean <- dist(t(cpgi.Beta.mean.whole), method = "euclidean")
pr.hc.w.mean <- hclust(pr.dis.mean, method = "ward")
pdf("Figures/clusterByMeanCPGI_ward_Norm.pdf")
par(mfrow = c(1, 1))
# label the leaf nodes by coloring them according to their groups
mean.ward.dendro <- as.dendrogram(pr.hc.w.mean)
clusDendro <- dendrapply(mean.ward.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "CPGI-based Ward(mean) Clustering showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w.mean, k = 5, border=c("cyan"))
dev.off()
# (II) ward plot for the median cpgi belta values
load('Data/CPGI_betaMedianList_raw.Rdata')
#### Transpose to place the cpgi features in rows
cpgi.Beta.median <- lapply(cpgi.Beta.median, t)
cpgi.Beta.median.whole <- do.call(cbind, cpgi.Beta.median)
dim(cpgi.Beta.median.whole)
# [1] 27176 52
# ward cluster based on the median cpgi belta values
# compute pairwise distances
pr.dis.median <- dist(t(cpgi.Beta.median.whole), method = "euclidean")
pr.hc.w.median <- hclust(pr.dis.median, method = "ward")
pdf("Figures/clusterByMedianCPGI_ward_Norm.pdf")
par(mfrow = c(1, 1))
# label the leaf nodes by coloring them according to their groups
median.ward.dendro <- as.dendrogram(pr.hc.w.median)
clusDendro <- dendrapply(median.ward.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "CPGI-based Ward(median) Clustering showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w.median, k = 5, border=c("cyan"))
dev.off()
#####################################################
##### Clustering for post-normalized and filtered data:
##### 1.) Probe-based (435739 probes for the CPG sites)
##### 2.) CPG-island-based
##### 3.) Gene-based ?DM-genes-based
#####################################################
# load the data
load('Data/All_3_sets_normAndFilt.Rdata')
dim(ALL.dat)
# [1] 435739 33
#####################################################
##### (1) probe level: clustering by Belta values #####
# compute pairwise distances
merged.dat <- cbind(APL.dat, ALL.dat, CTRL.dat)
pr.dis <- dist(t(merged.dat), method = "euclidean")
# sanity check between the sample name and group assigned
merged.des <- data.frame(Samples=colnames(merged.dat), Group=c(APL.meta$Group, ALL.meta$Group, rep('HBC', 9)))
pr.nms <- with(merged.des, paste(Samples, Group, sep = "_"))
all(unlist(lapply(pr.nms, function(x) substr(x, 1, 3)==substr(x, nchar(x)-2, nchar(x))))) # TRUE
# compute hierarchical clustering using ward linkage type
pr.hc.w <- hclust(pr.dis, method = "ward")
# ward
pdf("Figures/clusterByProbe_ward_normFilter.pdf")
par(mfrow = c(1, 1))
# label the leaf nodes by coloring them according to their groups
ward.dendro <- as.dendrogram(pr.hc.w)
# color code each node of the dendrogram according to their group
clusDendro <- dendrapply(ward.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "Ward showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w, k = 5, border=c("cyan"))
dev.off()
##################################################### IGNORE THE CODE BELOW. THERE IS A BUG, I WILL FIX IT LATER
##### (2) cpgi level: clustering by Belta values #####
##### Load the raw probe data
#load('Data/CPGI2Probe_betaList_raw.Rdata')
### (I) ward plot for the mean cpgi belta values
# construct a data frame with rows = cpgi IDs, columns = mean belta value for each sample
load('Data/CPGI_betaMeanList_normFilt.Rdata')
length(cpgi.Beta.mean$ALL)
[1] 27176
cpgi.Beta.mean.whole <- do.call(cbind, cpgi.Beta.mean)
dim(cpgi.Beta.mean.whole)
# [1] 27176 3
library (plyr)
mean.whole.expand <- cbind(ldply(cpgi.Beta.mean.whole[1, 1], unlist), ldply(cpgi.Beta.mean.whole[1, 2], unlist), ldply(cpgi.Beta.mean.whole[1, 3], unlist))
for(i in 2: dim(cpgi.Beta.mean.whole)[1]) {
print(i)
new.row <- cbind(ldply(cpgi.Beta.mean.whole[i, 1], unlist), ldply(cpgi.Beta.mean.whole[i, 2], unlist), ldply(cpgi.Beta.mean.whole[i, 3], unlist))
mean.whole.expand <- rbind(mean.whole.expand, new.row)
}
mean.whole.expand <- data.frame(mean.whole.expand)
rownames(mean.whole.expand) <- rownames(cpgi.Beta.mean.whole)
# ward cluster based on the mean cpgi belta values
# compute pairwise distances
pr.dis.mean <- dist(t(cpgi.Beta.mean.whole), method = "euclidean")
pr.hc.w.mean <- hclust(pr.dis.mean, method = "ward")
pdf("Figures/clusterByMeanCPGI_ward_NormFilt.pdf")
par(mfrow = c(1, 1))
# label the leaf nodes by coloring them according to their groups
mean.ward.dendro <- as.dendrogram(pr.hc.w.mean)
clusDendro <- dendrapply(mean.ward.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "CPGI-based Ward(mean) Clustering showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w.mean, k = 5, border=c("cyan"))
dev.off()
# (II) ward plot for the median cpgi belta values
load('Data/CPGI_betaMedianList_raw.Rdata')
#### Transpose to place the cpgi features in rows
cpgi.Beta.median <- lapply(cpgi.Beta.median, t)
cpgi.Beta.median.whole <- do.call(cbind, cpgi.Beta.median)
dim(cpgi.Beta.median.whole)
# [1] 27176 52
# ward cluster based on the median cpgi belta values
# compute pairwise distances
pr.dis.median <- dist(t(cpgi.Beta.median.whole), method = "euclidean")
pr.hc.w.median <- hclust(pr.dis.median, method = "ward")
pdf("Figures/clusterByMedianCPGI_ward_NormFilt.pdf")
par(mfrow = c(1, 1))
# label the leaf nodes by coloring them according to their groups
median.ward.dendro <- as.dendrogram(pr.hc.w.median)
clusDendro <- dendrapply(median.ward.dendro, colLab)
plot(clusDendro, cex = 0.6, main = "CPGI-based Ward(median) Clustering showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(pr.hc.w.median, k = 5, border=c("cyan"))
dev.off()