-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplot_cluster_bootstrap.R
214 lines (162 loc) · 7.84 KB
/
plot_cluster_bootstrap.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
###########################################################################################################
# This script contains clustering with AU&BP p-values procedure for:
# (A)pre-normalized
# (B)post-normalized
# (C)post-normalized and filtered by pairwise ctrl group comparison
###########################################################################################################
setwd('/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/')
library(RColorBrewer)
library(pvclust)
#####################################################
#*** Generate the pvclustering result by running the
# following section of code on a cluster server
# DON'T try run them as they take a whole day to finish
#####################################################
# (i) Clustering for pre-normalized raw data:
setwd('/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/')
##### Load raw data (modify the path accordingly)
load('/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/Data/All_3_sets.Rdata')
# merge 3 data sets and design
merged.dat <- cbind(APL.dat, ALL.dat, CTRL.dat)
# plot clustering using ward method
# cluster with bootstrapping
result.raw <- pvclust(merged.dat, method.dist="correlation", method.hclust="ward", nboot=100)
save(result.raw, file = '/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/Data/result_raw.Rdata')
# (ii) Clustering for post-normalized raw data:
setwd('/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/')
##### Load normalized data (modify the path accordingly)
load('/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/Data/All_3_sets_normalized.Rdata')
# merge 3 data sets and design
merged.dat <- cbind(APL.norm, ALL.norm, CTRL.norm)
# plot clustering using ward method
# cluster with bootstrapping
result.norm <- pvclust(merged.dat, method.dist="correlation", method.hclust="ward", nboot=100)
save(result.norm, file = '/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/Data/result_norm.Rdata')
# (iii) Clustering for post-normalized & filtered raw data:
setwd('/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/')
##### Load raw data (modify the path accordingly)
load('/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/Data/All_3_sets_normAndFilt.Rdata')
# merge 3 data sets and design
merged.dat <- cbind(APL.dat, ALL.dat, CTRL.dat)
# plot clustering using ward method
# cluster with bootstrapping
result.normFilt <- pvclust(merged.dat, method.dist="correlation", method.hclust="ward", nboot=100)
save(result.normFilt, file = '/ubc/cs/research/irmtraud/people/jingyun/localFile_backup/Documents/stat540/project/Data/result_normFilt.Rdata')
#####################################################
#*** Clustering result for pre-normalized raw data:
#*** 1.) Probe-based (485577 probes for the CPG sites)
#####################################################
### load result from clustering with bootstrapping
load('Data/result_raw.Rdata')
# plot clustering using ward method
pdf("Figures/pvclusterByProbe_raw.pdf", width=30, height=30)
par(mfrow = c(2, 1))
# label the leaf nodes by coloring them according to their groups
result_dendrogram <- as.dendrogram(result.raw$hclust)
# 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
}
# show leaf nodes colors
clusDendro <- dendrapply(result_dendrogram, colLab)
plot(clusDendro, main = "Ward Clustering showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(result.raw$hclust, k = 5, border=c("cyan"))
# show AU & BP values
plot(result.raw, main = "Ward Clustering for raw data with AU/Bp p-values")
rect.hclust(result.raw$hclust, k = 5, border=c("cyan"))
dev.off()
# plot for p-value vs. standard error
pdf("Figures/pvclusterByProbe_raw_stdError.pdf")
seplot(result.raw)
dev.off()
#*********************************************************************************
#####################################################
#*** Clustering result for post-normalized norm data:
#*** 1.) Probe-based (485577 probes for the CPG sites)
#####################################################
### load result from clustering with bootstrapping
load('Data/result_norm.Rdata')
# plot clustering using ward method
pdf("Figures/pvclusterByProbe_norm.pdf", width=30, height=20)
par(mfrow = c(2, 1))
# label the leaf nodes by coloring them according to their groups
result_dendrogram <- as.dendrogram(result.norm$hclust)
# 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
}
# show leaf nodes colors
clusDendro <- dendrapply(result_dendrogram, colLab)
plot(clusDendro, main = "Ward Clustering showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(result.norm$hclust, k = 5, border=c("cyan"))
# show AU & BP values
plot(result.norm, main = "Ward Clustering for normalized data with AU/Bp p-values")
rect.hclust(result.norm$hclust, k = 5, border=c("cyan"))
dev.off()
pdf("Figures/pvclusterByProbe_norm_stdError.pdf")
seplot(result.norm)
dev.off()
#*********************************************************************************
#####################################################
#*** Clustering result for pre-normalized and Filtered data:
#*** 1.) Probe-based (435739 probes for the CPG sites)
#####################################################
### load result from clustering with bootstrapping
load('Data/result_normFilt.Rdata')
# plot clustering using ward method
pdf("Figures/pvclusterByProbe_normFilt.pdf", width=30, height=20)
par(mfrow = c(2, 1))
# label the leaf nodes by coloring them according to their groups
result_dendrogram <- as.dendrogram(result.normFilt$hclust)
# 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
}
# show leaf nodes colors
clusDendro <- dendrapply(result_dendrogram, colLab)
plot(clusDendro, main = "Ward Clustering showing 5 clusters")
legend("topright", legend =c("ALL", "HBC", "HBM", "PLP", "PLR"), fill = colors, title="Sample Groups", box.col="transparent")
rect.hclust(result.normFilt$hclust, k = 5, border=c("cyan"))
# show AU & BP values
plot(result.normFilt, main = "Ward Clustering for normalized and filtered with AU/Bp p-values")
rect.hclust(result.normFilt$hclust, k = 5, border=c("cyan"))
dev.off()
pdf("Figures/pvclusterByProbe_normFilt_stdError.pdf")
seplot(result.normFilt)
dev.off()
#*********************************************************************************