forked from dy-lin/stat540-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCA_final.R
254 lines (208 loc) · 7.25 KB
/
PCA_final.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
## Author: Diana Lin
## Date: March 30, 2020
library(tidyverse)
library(here)
library(glue)
library(cowplot)
# read in the metadata rds
metadata <-
readRDS(file = here("data", "processed_data", "metadata.rds"))
# read in the raw normalized data
expr <- readRDS(file = here("data","raw_data", "data.rds")) %>%
column_to_rownames(var = "CG")
# PCA: with automatic scalling using prcomp
pcs <- prcomp(na.omit(expr), center = TRUE, scale = TRUE)
# save scree plot
png(filename = here("results","scratch", "PCA_auto_scree.png"))
plot(pcs)
dev.off()
# binds metadata to pcs rotations
prinComp <-
cbind(metadata, pcs$rotation[metadata$sample, 1:10]) %>%
dplyr::select(-sample)
# save scatter plot
png(filename = here("results", "scratch", "PCA_auto_scatter.png"))
plot(prinComp[, c("cancer", "cancer_type", "PC1", "PC2", "PC3")], pch = 19, cex = 0.8)
dev.off()
# function to plot PC1 vs PC2 by different characters
gen_pca <- function(var = "cancer_type") {
ggplot(prinComp, aes(x = PC1, y = PC2)) +
geom_point(aes(colour = !!sym(var))) +
labs(title = glue("PCA: {var}"),
subtitle = "scaled and recentered with prcomp") +
ggsave(here("results", "scratch", glue("PCA_auto_{var}.png")),width = 12, height = 8, device = "png")
}
# PCA, with manual scaling using scale
if (!file.exists(here("data", "processed_data", "scaled.rds"))) {
expr_scaled <- t(scale(t(expr)))
saveRDS(expr_scaled, file = here("data", "processed_data", "scaled.rds"))
} else {
expr_scaled <- readRDS(here("data", "processed_data", "scaled.rds"))
}
# do same thing but with manual scaled
pcs_scaled <- prcomp(na.omit(expr_scaled), center = FALSE, scale = FALSE)
# save scree plot
png(filename = here("results","scratch", "PCA_manual_scree.png"))
plot(pcs_scaled)
dev.off()
# bind to metadata
prinComp_scaled <-
cbind(metadata, pcs_scaled$rotation[metadata$sample, 1:10]) %>%
dplyr::select(-sample)
# save scatter plot
png(filename = here("results", "scratch", "PCA_manual_scatter.png"))
plot(prinComp_scaled[, c("cancer", "cancer_type", "PC1", "PC2", "PC3")], pch = 19, cex = 0.8)
dev.off()
# function for plotting PC1 and PC2
gen_pca_scaled <- function(var = "cancer_type") {
ggplot(prinComp_scaled, aes(x = PC1, y = PC2)) +
geom_point(aes(colour = !!sym(var))) +
labs(title = glue("PCA: {var}"),
subtitle = "manually scaled") +
ggsave(here("results", "scratch", glue("PCA_manual_{var}.png")),width = 12, height = 8, device = "png")
}
# calls to the function with different covariates
gen_pca("cancer_type")
gen_pca_scaled("cancer_type")
gen_pca("cancer")
gen_pca_scaled("cancer")
gen_pca("source")
gen_pca_scaled("source")
gen_pca("sex")
gen_pca_scaled("sex")
gen_pca("age")
gen_pca_scaled("age")
gen_pca("smoking_status")
gen_pca_scaled("smoking_status")
gen_pca("packs_per_year")
gen_pca_scaled("packs_per_year")
gen_pca("hpv")
gen_pca_scaled("hpv")
gen_pca("p16")
gen_pca_scaled("p16")
gen_pca("dna_ng")
gen_pca_scaled("dna_ng")
gen_pca("tumour_percent")
gen_pca_scaled("tumour_percent")
gen_pca("origin")
gen_pca_scaled("origin")
## PCA combined plot
pca <- function(pc1 = "PC1", pc2 = "PC2", var = "cancer_type") {
ggplot(prinComp_scaled, aes(x = !!sym(pc1), y = !!sym(pc2))) +
geom_point(aes(colour = !!sym(var))) +
labs(title = var,
subtitle = glue("{pc1} and {pc2}")) +
# scale_colour_discrete(labels = function(x) str_wrap(x, width = 13)) +
# guides(colour = guide_legend(nrow = 2)) +
theme_cowplot(12) #+
# xlim(c(-0.4,0.4))
}
# plot the PCA plots
p1 <- pca(pc1 = "PC1", pc2 = "PC2", var = "cancer_type")
p2 <- pca(pc1 = "PC1", pc2 = "PC3", var = "cancer_type")
p3 <- pca(pc1 = "PC2", pc2 = "PC3", var = "cancer_type")
p4 <- pca(pc1 = "PC1", pc2 = "PC2", var = "cancer")
p5 <- pca(pc1 = "PC1", pc2 = "PC3", var = "cancer")
p6 <- pca(pc1 = "PC2", pc2 = "PC3", var = "cancer")
# combine the three cancer_type ones
p7 <- plot_grid(
p1 + theme(legend.position = 'none'),
p2 + theme(legend.position = 'none'),
p3 + theme(legend.position = 'none'),
labels = c('A', 'B', 'C'),
align = 'h',
nrow = 1)
# combine the three cancer ones
p8 <- plot_grid(
p4 + theme(legend.position = 'none'),
p5 + theme(legend.position = 'none'),
p6 + theme(legend.position = 'none'),
labels = c('D', 'E', 'F'),
align = 'h',
nrow = 1
)
# get the legend from the cancer_type plots
legend1 <- get_legend(
p1 + theme(legend.box.margin = margin(0, 0, 0, 12),
legend.title = element_blank())
)
# get the legend from the cancer plots
legend2 <- get_legend(
p4 + theme(legend.box.margin = margin(0, 0, 0, 12),
legend.title = element_blank()) +
scale_colour_discrete(labels = function(x) str_wrap(x, width = 20))# +
# guides(colour = guide_legend(nrow = 2))
)
# plots + legend
p9 <- plot_grid(p7, legend1, rel_widths = c(3, 1.5))
p10 <- plot_grid(p8, legend2, rel_widths = c(3, 1.5))
# main title
title <- ggdraw() +
draw_label(
"PCA of Differential Methylation Beta-values",
fontface = 'bold',
x = 0,
hjust = 0
) +
theme(
plot.margin = margin(0, 0, 0, 7)
)
# final combined plot
final <- plot_grid(title, p9, p10, ncol = 1, rel_heights = c(0.1, 1,1))
ggsave(plot = final, filename = here("results","final", "PCA_manual_combined.png"), width = 12, height = 8, device = "png")
## the same as above, but with the auto-scaled PCA
pca <- function(pc1 = "PC1", pc2 = "PC2", var = "cancer_type") {
ggplot(prinComp, aes(x = !!sym(pc1), y = !!sym(pc2))) +
geom_point(aes(colour = !!sym(var))) +
labs(title = var,
subtitle = glue("{pc1} and {pc2}")) +
# scale_colour_discrete(labels = function(x) str_wrap(x, width = 13)) +
# guides(colour = guide_legend(nrow = 2)) +
theme_cowplot(12) #+
#xlim(c(-0.4,0.4))
}
p1 <- pca(pc1 = "PC1", pc2 = "PC2", var = "cancer_type")
p2 <- pca(pc1 = "PC1", pc2 = "PC3", var = "cancer_type")
p3 <- pca(pc1 = "PC2", pc2 = "PC3", var = "cancer_type")
p4 <- pca(pc1 = "PC1", pc2 = "PC2", var = "cancer")
p5 <- pca(pc1 = "PC1", pc2 = "PC3", var = "cancer")
p6 <- pca(pc1 = "PC2", pc2 = "PC3", var = "cancer")
p7 <- plot_grid(
p1 + theme(legend.position = 'none'),
p2 + theme(legend.position = 'none'),
p3 + theme(legend.position = 'none'),
labels = c('A', 'B', 'C'),
align = 'h',
nrow = 1)
p8 <- plot_grid(
p4 + theme(legend.position = 'none'),
p5 + theme(legend.position = 'none'),
p6 + theme(legend.position = 'none'),
labels = c('D', 'E', 'F'),
align = 'h',
nrow = 1
)
legend1 <- get_legend(
p1 + theme(legend.box.margin = margin(0, 0, 0, 12),
legend.title = element_blank())
)
legend2 <- get_legend(
p4 + theme(legend.box.margin = margin(0, 0, 0, 12),
legend.title = element_blank()) +
scale_colour_discrete(labels = function(x) str_wrap(x, width = 20))# +
# guides(colour = guide_legend(nrow = 2))
)
p9 <- plot_grid(p7, legend1, rel_widths = c(3, 1.5))
p10 <- plot_grid(p8, legend2, rel_widths = c(3, 1.5))
title <- ggdraw() +
draw_label(
"PCA of Differential Methylation Beta-values",
fontface = 'bold',
x = 0,
hjust = 0
) +
theme(
plot.margin = margin(0, 0, 0, 7)
)
final <- plot_grid(title, p9, p10, ncol = 1, rel_heights = c(0.1, 1,1))
ggsave(plot = final, filename = here("results","scratch", "PCA_auto_combined.png"), width = 12, height = 8, device = "png")