-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_pca-plots_v2.Rmd
558 lines (484 loc) · 16.6 KB
/
3_pca-plots_v2.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
---
title: "Section 3: PCA plots"
date: "`r Sys.Date()`"
author:
- name: Arun Seetharam
- name: Ha Vu
affiliation: Tuteja Lab
affiliation_url: https://www.tutejalab.org
output:
rmdformats::readthedown:
self_contained: true
thumbnails: false
lightbox: true
gallery: true
highlight: tango
---
```{r setup, include=FALSE}
options(max.print = "75")
knitr::opts_chunk$set(
echo = TRUE,
collapse = TRUE,
comment = "#>",
fig.path = "assets/",
fig.width = 8,
prompt = FALSE,
tidy = FALSE,
message = FALSE,
warning = TRUE
)
knitr::opts_knit$set(width = 75)
```
This section uses the count data (all datasets) generated in Section 1 for cluster analyses. Briefly, the count data are imported in R, batch corrected using `ComBat_seq`, `vst` transformation and clustering is performed using `DESeq2`, and results are visualized as PCA plots.
# Prerequisites
R packages required for this section are loaded.
```{r, warnings=TRUE, message=FALSE}
setwd("~/github/BAPvsTrophoblast_Amnion")
library(sva)
library(tidyverse)
library(DESeq2)
library(vsn)
library(pheatmap)
library(ggrepel)
library(RColorBrewer)
library(plotly)
library(PCAtools)
library(scales)
library(htmlwidgets)
library(factoextra)
library(spatstat.core)
```
# PCA plots for all datasets
## Import datasets
The `counts` data and its associated metadata (`coldata`) are imported for analyses.
```{r dataset, warnings=TRUE, message=FALSE}
counts = 'assets/counts-pca-v2.txt'
groupFile = 'assets/batch-pca-v2.txt'
coldata <-
read.csv(
groupFile,
row.names = 1,
sep = "\t",
stringsAsFactors = TRUE
)
cts <- as.matrix(read.csv(counts, sep = "\t", row.names = "gene.ids"))
```
Inspect the `coldata`.
```{r coldata}
DT::datatable(coldata)
```
Reorder columns of `cts` according to `coldata` rows. Check if samples in both files match.
```{r order, warnings=TRUE, message=FALSE}
colnames(cts)
all(rownames(coldata) %in% colnames(cts))
cts <- cts[, rownames(coldata)]
```
## Batch correction
Using `Combat_seq` (SVA package) run batch correction - using bioproject IDs as variable (dataset origin).
```{r batchcorrect, warnings=TRUE, message=TRUE}
cov1 <- as.factor(coldata$authors)
adjusted_counts <- ComBat_seq(cts, batch = cov1, group = NULL)
all(rownames(coldata) %in% colnames(cts))
cts <- cts[, rownames(coldata)]
```
## Run DESeq2
The batch corrected read counts are then used for running DESeq2 analyses.
```{r deseq2, warnings=TRUE, message=FALSE}
dds <- DESeqDataSetFromMatrix(countData = adjusted_counts,
colData = coldata,
design = ~ group)
```
## Transformation:
```{r vst, warnings=TRUE, message=FALSE}
vst <- assay(vst(dds))
vsd <- vst(dds, blind = FALSE)
pcaData <-
plotPCA(vsd,
intgroup = c("group", "authors"),
returnData = TRUE)
percentVar <- round(100 * attr(pcaData, "percentVar"))
```
3D PCA plot function:
```{r pca3d, warnings=TRUE, message=FALSE}
# @ Thomas W. Battaglia
#' Plot DESeq2's PCA plotting with Plotly 3D scatterplot
#'
#' The function will generate a plot_ly 3D scatter plot image for
#' a 3D exploration of the PCA.
#'
#' @param object a DESeqTransform object, with data in assay(x), produced for example by either rlog or varianceStabilizingTransformation.
#' @param intgroup interesting groups: a character vector of names in colData(x) to use for grouping
#' @param ntop number of top genes to use for principal components, selected by highest row variance
#' @param returnData should the function only return the data.frame of PC1, PC2 and PC3 with intgroup covariates for custom plotting (default is FALSE)
#' @return An object created by plot_ly, which can be assigned and further customized.
#' @export
plotPCA3D <-
function (object,
intgroup = "condition",
ntop = 500,
returnData = FALSE) {
rv <- rowVars(assay(object))
select <-
order(rv, decreasing = TRUE)[seq_len(min(ntop, length(rv)))]
pca <- prcomp(t(assay(object)[select,]))
percentVar <- pca$sdev ^ 2 / sum(pca$sdev ^ 2)
if (!all(intgroup %in% names(colData(object)))) {
stop("the argument 'intgroup' should specify columns of colData(dds)")
}
intgroup.df <-
as.data.frame(colData(object)[, intgroup, drop = FALSE])
group <- if (length(intgroup) > 1) {
factor(apply(intgroup.df, 1, paste, collapse = " : "))
}
else {
colData(object)[[intgroup]]
}
d <- data.frame(
PC1 = pca$x[, 1],
PC2 = pca$x[, 2],
PC3 = pca$x[, 3],
group = group,
intgroup.df,
name = colnames(object)
)
if (returnData) {
attr(d, "percentVar") <- percentVar[1:3]
return(d)
}
message("Generating plotly plot")
p <- plotly::plot_ly(
data = d,
x = ~ PC1,
y = ~ PC2,
z = ~ PC3,
color = group,
mode = "markers",
type = "scatter3d"
)
return(p)
}
```
## Process data for PCA (all samples)
PCA plot for the dataset that includes all libraries.
```{r pcaFull_comp, warnings=TRUE, message=FALSE}
rv <- rowVars(assay(vsd))
select <-
order(rv, decreasing = TRUE)[seq_len(min(500, length(rv)))]
pca <- prcomp(t(assay(vsd)[select, ]))
percentVar <- pca$sdev ^ 2 / sum(pca$sdev ^ 2)
intgroup = c("group", "authors")
intgroup.df <- as.data.frame(colData(vsd)[, intgroup, drop = FALSE])
group <- if (length(intgroup) > 1) {
factor(apply(intgroup.df, 1, paste, collapse = " : "))
}
d <- data.frame(
PC1 = pca$x[, 1],
PC2 = pca$x[, 2],
PC3 = pca$x[, 3],
PC4 = pca$x[, 4],
PC5 = pca$x[, 5],
group = group,
intgroup.df,
name = colnames(vsd)
)
```
## Components 1 and 2
```{r pcaFull_C1-C2, fig.cap="Fig 3.1: PCA plots (all samples) - PC1 & PC2", fig.width=8, fig.height=8}
g <- ggplot(d, aes(PC1, PC2, color = group.1, shape = authors)) +
scale_shape_manual(values = 1:8) +
theme_bw() +
theme(legend.title = element_blank()) +
geom_point(size = 2, stroke = 2) +
xlab(paste("PC1", round(percentVar[1] * 100, 2), "% variance")) +
ylab(paste("PC2", round(percentVar[2] * 100, 2), "% variance"))
ggplotly(g)
```
## Components 1 and 3
```{r pcaFull_C1-C3, fig.cap="Fig 3.2: PCA plots (all samples) - PC1 and PC3", fig.width=8, fig.height=8}
g <- ggplot(d, aes(PC1, PC3, color = group.1, shape = authors)) +
scale_shape_manual(values = 1:8) +
theme_bw() +
theme(legend.title = element_blank()) +
geom_point(size = 2, stroke = 2) +
xlab(paste("PC1", round(percentVar[1] * 100, 2), "% variance")) +
ylab(paste("PC3", round(percentVar[3] * 100, 2), "% variance"))
ggplotly(g)
```
## Components 2 and 3
```{r pcaFull_C2-C3, fig.cap="Fig 3.3: PCA plots (all samples) - PC2 and PC3", fig.width=8, fig.height=8}
g <- ggplot(d, aes(PC2, PC3, color = group.1, shape = authors)) +
scale_shape_manual(values = 1:8) +
theme_bw() +
theme(legend.title = element_blank()) +
geom_point(size = 2, stroke = 2) +
xlab(paste("PC2", round(percentVar[2] * 100, 2), "% variance")) +
ylab(paste("PC3", round(percentVar[3] * 100, 2), "% variance"))
ggplotly(g)
```
## Scree plot
```{r pcaFull_scree, fig.cap="Fig 3.4: Scree plot showing variance contributed by each component", fig.width=8, fig.height=5}
scree_plot = data.frame(percentVar)
scree_plot[, 2] <- c(1:97)
colnames(scree_plot) <- c("variance", "component_number")
g <-
ggplot(scree_plot, aes(component_number, variance * 100)) +
geom_bar(stat = 'identity', fill = "slateblue") +
theme_minimal() +
theme(
axis.text.x = element_text(
vjust = 1,
hjust = 1,
size = 12
),
axis.text.y = element_text(size = 12),
plot.margin = margin(10, 10, 10, 100),
legend.position = "none",
plot.title = element_text(color = "black", size = 18, face = "bold.italic"),
axis.title.y = element_text(color = "black", size = 14, face = "bold"),
axis.line.x = element_line(
colour = 'black',
size = 0.5,
linetype = 'solid'
),
axis.line.y = element_line(
colour = 'black',
size = 0.5,
linetype = 'solid'
),
axis.ticks.x = element_line(
colour = 'black',
size = 1,
linetype = 'solid'
),
axis.title.x = element_text(color = "black", size = 14, face = "bold")
) +
xlab("Components") +
ylab("Percent Variance") + ggtitle("All samples")
scale_y_continuous(expand = expansion(mult = c(0, .1)), breaks = pretty_breaks())
ggplotly(g)
```
## Euclidean Distance
```{r pcaFull_euc_partial, warnings=TRUE, message=FALSE, fig.cap="Fig 3.5: Heatmap of Euclidean Distance using first 3 components (all samples)", fig.width=12, fig.height=10}
pca <-
prcomp(t(assay(vsd)[select,]),
center = TRUE,
scale. = FALSE,
rank. = 3)
results <- pca$x
distance <- get_dist(results, method = "euclidean")
write.table(
as.matrix(distance),
"assets/all_samples-PC1-PC2-PC3.tsv",
row.names = T,
qmethod = "double",
sep = "\t"
)
fviz_dist(distance, gradient = list(low = "#00AFBB", mid = "white", high = "#FC4E07"))
```
```{r pcaFull_euc_full, warnings=TRUE, message=FALSE, fig.cap="Fig 3.6: Heatmap of Euclidean Distance using all components (all samples)", fig.width=12, fig.height=10}
pca <- prcomp(t(assay(vsd)[select,]), center = TRUE, scale. = FALSE)
results <- pca$x
distance <- get_dist(results, method = "euclidean")
write.table(
as.matrix(distance),
"assets/all_samples-allPC.tsv",
row.names = T,
qmethod = "double",
sep = "\t"
)
fviz_dist(distance, gradient = list(low = "#00AFBB", mid = "white", high = "#FC4E07"))
```
# PCA plot for differentiated cell libraries (no amnion datasets)
## Import datasets
The `counts` data and its associated metadata (`coldata`) are imported for analyses.
```{r import2, warnings=TRUE, message=FALSE}
setwd("~/github/BAPvsTrophoblast_Amnion")
counts2 = 'assets/counts-pca-v2.2.txt'
groupFile = 'assets/batch-pca-v2.2.txt'
coldata2 <-
read.csv(
groupFile,
row.names = 1,
sep = "\t",
stringsAsFactors = TRUE
)
cts2 <-
as.matrix(read.csv(counts2, sep = "\t", row.names = "gene.ids"))
```
Inspect the `coldata`.
```{r coldata2}
DT::datatable(coldata2)
```
Reorder columns of `cts` according to `coldata` rows. Check if samples in both files match.
```{r order2}
all(rownames(coldata2) %in% colnames(cts2))
cts2 <- cts2[, rownames(coldata2)]
```
## Batch correction
Using `ComBat_seq` (SVA package) run batch correction - using bioproject IDs as variable (dataset origin).
```{r batchcorrect2, warnings=TRUE, message=TRUE}
cov1 <- as.factor(coldata2$authors)
adjusted_counts <- ComBat_seq(cts2, batch = cov1, group = NULL)
all(rownames(coldata2) %in% colnames(cts2))
cts2 <- cts2[, rownames(coldata2)]
```
## Run DESeq2
The batch corrected read counts are then used for running DESeq2 analyses.
```{r deseq2b, warnings=TRUE, message=FALSE}
dds <- DESeqDataSetFromMatrix(countData = adjusted_counts,
colData = coldata2,
design = ~ group)
```
## Transformation
```{r vst2, warnings=TRUE, message=FALSE}
vsd <- vst(dds, blind = FALSE)
pcaData <-
plotPCA(vsd,
intgroup = c("group", "authors"),
returnData = TRUE)
percentVar <- round(100 * attr(pcaData, "percentVar"))
```
## Process data for PCA (differenticated)
```{r pcaDif_comp, warnings=TRUE, message=FALSE}
rv <- rowVars(assay(vsd))
select <-
order(rv, decreasing = TRUE)[seq_len(min(500, length(rv)))]
pca <- prcomp(t(assay(vsd)[select,]))
percentVar <- pca$sdev ^ 2 / sum(pca$sdev ^ 2)
intgroup = c("group", "authors")
intgroup.df <- as.data.frame(colData(vsd)[, intgroup, drop = FALSE])
group <- if (length(intgroup) > 1) {
factor(apply(intgroup.df, 1, paste, collapse = " : "))
}
d <- data.frame(
PC1 = pca$x[, 1],
PC2 = pca$x[, 2],
PC3 = pca$x[, 3],
PC4 = pca$x[, 4],
PC5 = pca$x[, 5],
group = group,
intgroup.df,
name = colnames(vsd)
)
```
## Components 1 and 2
```{r pcaDif_C1-C2, fig.cap="Fig 3.7: PCA plots (differentiated) - PC1 and PC2", fig.width=8, fig.height=8}
g <- ggplot(d, aes(PC1, PC2, color = group.1, shape = authors)) +
scale_shape_manual(values = 1:8) +
theme_bw() +
theme(legend.title = element_blank()) +
geom_point(size = 2, stroke = 2) +
xlab(paste("PC1", round(percentVar[1] * 100, 2), "% variance")) +
ylab(paste("PC2", round(percentVar[2] * 100, 2), "% variance"))
ggplotly(g)
```
## Components 1 and 3
```{r pcaDif_C1-C3, fig.cap="Fig 3.8: PCA plots (differentiated) - PC1 and PC3", fig.width=8, fig.height=8}
g <- ggplot(d, aes(PC1, PC3, color = group.1, shape = authors)) +
scale_shape_manual(values = 1:8) +
theme_bw() +
theme(legend.title = element_blank()) +
geom_point(size = 2, stroke = 2) +
xlab(paste("PC1", round(percentVar[1] * 100, 2), "% variance")) +
ylab(paste("PC3", round(percentVar[3] * 100, 2), "% variance"))
ggplotly(g)
```
## Components 2 and 3
```{r pcaDif_C2-C3, fig.cap="Fig 3.9: PCA plots (differentiated) - PC2 and PC3", fig.width=8, fig.height=8}
g <- ggplot(d, aes(PC2, PC3, color = group.1, shape = authors)) +
scale_shape_manual(values = 1:8) +
theme_bw() +
theme(legend.title = element_blank()) +
geom_point(size = 2, stroke = 2) +
xlab(paste("PC2", round(percentVar[2] * 100, 2), "% variance")) +
ylab(paste("PC3", round(percentVar[3] * 100, 2), "% variance"))
ggplotly(g)
```
## Scree plot
```{r pcaDif_scree, fig.cap="Fig 3.10: Scree plot showing variance contributed by each component (differentiated)", fig.width=8, fig.height=5}
scree_plot = data.frame(percentVar)
end = length(scree_plot$percentVar)
scree_plot[, 2] <- c(1:end)
colnames(scree_plot) <- c("variance", "component_number")
g <- ggplot(scree_plot, aes(component_number, variance * 100)) +
geom_bar(stat = 'identity', fill = "slateblue") +
theme_minimal() +
theme(
axis.text.x = element_text(
vjust = 1,
hjust = 1,
size = 12
),
axis.text.y = element_text(size = 12),
plot.margin = margin(10, 10, 10, 100),
legend.position = "none",
plot.title = element_text(color = "black", size = 18, face = "bold.italic"),
axis.title.y = element_text(color = "black", size = 14, face = "bold"),
axis.line.x = element_line(
colour = 'black',
size = 0.5,
linetype = 'solid'
),
axis.line.y = element_line(
colour = 'black',
size = 0.5,
linetype = 'solid'
),
axis.ticks.x = element_line(
colour = 'black',
size = 1,
linetype = 'solid'
),
axis.title.x = element_text(color = "black", size = 14, face = "bold")
) +
xlab("Components") +
ylab("Percent Variance") + ggtitle("differentiated samples")
scale_y_continuous(expand = expansion(mult = c(0, .1)), breaks = pretty_breaks())
ggplotly(g)
```
## Euclidean Distance
```{r pcadiff_euc_partial, warnings=TRUE, message=FALSE, fig.cap="Fig 3.11: Heatmap of Euclidean Distance using first 3 components (differentiated)", fig.width=12, fig.height=10}
pca <-
prcomp(t(assay(vsd)[select,]),
center = TRUE,
scale. = FALSE,
rank. = 3)
results <- pca$x
distance <- get_dist(results, method = "euclidean")
write.table(
as.matrix(distance),
"assets/diff_samples-PC1-PC2-PC3.tsv",
row.names = T,
qmethod = "double",
sep = "\t"
)
fviz_dist(distance, gradient = list(low = "#00AFBB", mid = "white", high = "#FC4E07"))
```
```{r pcadiff_euc_full, warnings=TRUE, message=FALSE, fig.cap="Fig 3.12: Heatmap of Euclidean Distance using all components (differentiated)", fig.width=12, fig.height=10}
pca <- prcomp(t(assay(vsd)[select,]), center = TRUE, scale. = FALSE)
results <- pca$x
distance <- get_dist(results, method = "euclidean")
write.table(
as.matrix(distance),
"assets/diff_samples-allPC.tsv",
row.names = T,
qmethod = "double",
sep = "\t"
)
fviz_dist(distance, gradient = list(low = "#00AFBB", mid = "white", high = "#FC4E07"))
```
# Interactive 3D PCA plot
```{r pcaFull_3D, warnings=TRUE, message=FALSE}
g <- plotPCA3D(vsd, intgroup = c("group", "authors"))
saveWidget(g, file = "PCA_full.html")
```
```{r pcaDif_3D, warnings=TRUE, message=FALSE}
g <- plotPCA3D(vsd, intgroup = c("group", "authors"))
saveWidget(g, file = "PCA_dif.html")
```
1. [Interactive 3D PCA plot (all samples)](PCA_full.html){target="_blank"}
2. [Interactive 3D PCA plot (differentiated samples)](PCA_dif.html){target="_blank"}
# Session Information
```{r sessioninfo}
sessionInfo()
```