-
Notifications
You must be signed in to change notification settings - Fork 1
/
07c-htmcp_lymphoma_pca.R
141 lines (113 loc) · 5.03 KB
/
07c-htmcp_lymphoma_pca.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
################################################################################
################################################################################
################################################################################
################################################################################
################################ LYMPHOMA PCAs #################################
#################################### SETUP #####################################
library(knitr)
library(tidyverse)
library(matrixStats)
library(data.table)
library(PCAtools)
library(DESeq2)
library(ggplot2)
library(ggsci)
library(edgeR)
library(ashr)
library(cowplot)
library(wesanderson)
library(sva)
################################### LOAD DATA ##################################
load("r_outputs/07-htmcp_all_lymphoma_filt_counts.Rdata")
load("r_outputs/07-htmcp_DLBCL_filt_counts.Rdata")
load("r_outputs/01-refs.Rdata")
################################# FUNCTION SCREE PLOT #################################
pca_standard <- function(tform, metadata, var) {
removeVar <- var
pca.obj <- PCAtools::pca(assay(tform),
metadata=metadata,
removeVar=removeVar)
cat(sprintf('Removed %d pct low variance variables, %d retained\n',
removeVar*100, length(pca.obj$xvars)))
varline <- 50
varline.x <- min(which(cumsum(pca.obj$variance) >= varline))
horn <- PCAtools::parallelPCA(assay(tform), removeVar = removeVar)
elbow <- PCAtools::findElbowPoint(pca.obj$variance)
screeplot <-PCAtools::screeplot(pca.obj,
axisLabSize = 6,
components = getComponents(pca.obj, 1:30),
title=paste("Retrotranscriptome SCREE",
metadata$cancer_type[1],
sep=" "),
hline=varline, vline=c(varline.x, horn$n, elbow)
) +
geom_label(aes(x=varline.x+1, y=50,
label = paste0(varline, '% var'), vjust = -1)) +
geom_label(aes(x = horn$n + 1, y = 50,
label = 'Horn\'s', vjust = -1)) +
geom_label(aes(x = elbow + 1, y = 50,
label = 'Elbow method', vjust = -1))
cat(sprintf('%d PCs for Elbow method\n', elbow))
cat(sprintf('%d PCs for Horn method\n', horn$n))
cat(sprintf('%d PCs needed to explain %d percent of variation\n',
varline.x, varline))
print(screeplot)
return(pca.obj)
}
########################## ALL TOGETHER DESEQ HERVs ############################
### DESeq2 (HERVs Only)
all_metadata <- all_metadata_hiv %>% replace(is.na(.), "Missing")
hervs.to.keep <- intersect(rownames(all.hiv.filt.herv),
retro.annot$locus[retro.annot$chrom != "chrY"])
all.counts.filt.herv.y <- all.hiv.filt.herv[hervs.to.keep,]
all.countdat <- all.counts.filt.herv.y
cat(sprintf('%d variables\n', nrow(all.countdat)))
stopifnot(all(colnames(all.countdat) == rownames(all_metadata)))
all.dds <- DESeq2::DESeqDataSetFromMatrix(countData = all.countdat,
colData = all_metadata,
design = ~ cancer_type + 0)
all.dds <- DESeq2::DESeq(all.dds, parallel=T)
all.tform <- DESeq2::varianceStabilizingTransformation(all.dds, blind=FALSE)
## PCA
all.herv.pca.obj <-
pca_standard(tform = all.tform,
metadata = all_metadata,
var = 0.7)
# No Y: 0.1, 9, 16, 1, With Y:
# No Y: 0.2, 8, 16, 1, With Y:
# No Y: 0.3, 8, 16, 1, With Y:
# No Y: 0.5, 8, 15, 1, With Y: 9, 16, 1
# No Y: 0.7, 8, 11, 1, With Y: 9, 12, 1
############################## ALL LYMPHOMA BIPLOTS HERVs ################################
## Biplot with projects (only HERVs)
biplot(all.herv.pca.obj,
lab = NULL,
showLoadings = FALSE,
boxedLoadingsNames = TRUE,
fillBoxedLoadings = alpha("white", 0.9),
pointSize = 2,
encircle = FALSE,
sizeLoadingsNames =4,
lengthLoadingsArrowsFactor = 3,
drawConnectors = TRUE,
colby = "subtype",
colkey = c("Endemic BL EBV-positive" = wes_palette("Darjeeling1")[2],
"Sporadic BL EBV-positive" = "#006053",
"Sporadic BL EBV-negative" = wes_palette("Darjeeling1")[4],
"Endemic BL EBV-negative" = "#ae5c00",
"GCB" = "royalblue",
"ABC" = "red3",
"Unclass" = "lightblue",
"Missing" = "grey",
"FOLLICULAR GRADE 1" = "#F1BB7B",
"FOLLICULAR GRADE 2" = "#FD6467",
"FOLLICULAR GRADE 3A" = "#5B1A18",
"HIV-positive" = "#fa7de7"),
shape = "cancer_type",
shapekey = c("DLBCL" = 15,
"BL" = 8,
"FL" = 2),
legendPosition = "bottom") +
theme_cowplot() +
theme(aspect.ratio = 1)
ggsave("plots/07c-htmcp_all_lymphoma_hervs_biplot_pc1_pc2_cancertype.pdf", height = 9, width = 9)