-
Notifications
You must be signed in to change notification settings - Fork 1
/
Figure2E_PCA_plot.Rmd
182 lines (144 loc) · 5.57 KB
/
Figure2E_PCA_plot.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
---
title: "PCA plots from RNA seq data using top AP1 genes"
author: "E Onur Karakaslar, Neerja Katiyar"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
---
# Prepare the environment
```{r setup, include=FALSE}
require(knitr)
knitr::opts_chunk$set(echo = TRUE)
opts_knit$set(root.dir = "/Users/katiyn/Dropbox (JAX)/MouseAging_clean/Mice_aging_NK_resubmission/code/") #set root dir!
```
```{r libraries, include=TRUE, echo=TRUE}
library(readr)
library(VennDiagram)
library(ggplot2)
library(grDevices)
library(edgeR)
library(dplyr)
```
```{r convert_geneID, include=TRUE, echo=TRUE}
source("./color_values.R")
gene2ens <- function(genes){
genome <- annotables::grcm38
# ens to gene symbol mapping
mapping <-
base::subset(genome,
genome$symbol %in% genes,
select = c('ensgene', 'symbol'))
m <- match(genes, mapping$symbol)
ens.genes <- mapping$ensgene[m]
names(ens.genes) <- genes
return(ens.genes)
}
```
```{r plot_PCA, include=TRUE, echo=TRUE}
ap1.genes <- c("Fos", "Fosb", "Jun", "Junb", "Maff")
ap1.genes.ens <- gene2ens(ap1.genes)
## Load Onur's data
count.matrix <- read.csv('./data/rna_count_matrix.csv', row.names = 1, check.names = F, stringsAsFactors = F)
# Order genes according to their standard deviation in decreasing order
count.matrix <- count.matrix [rev(order(apply(count.matrix, 1, sd))),]
# Remove duplicated genes
count.matrix <- count.matrix [!duplicated(rownames(count.matrix)),]
# Enforce all counts to be integers
count.matrix <- round(count.matrix, 0)
# remove low expressed genes
count.matrix <- count.matrix [rowSums(cpm(count.matrix) >= 0.5) >= 2,]
# filter BM
BM.loc <- colnames(count.matrix) %>% sapply(function(x){grepl("BM", x, fixed = TRUE)})
count.matrix <- count.matrix[,!BM.loc]
# normalize with cpm
count.matrix.normalized <- cpm(count.matrix, log = T)
meta.data <- colnames(count.matrix) %>% strsplit("-", fixed = T) %>% do.call(rbind, .) %>% as.data.frame
colnames(meta.data) <- c("Strain", "Age", "Sex", "TCT", "SampleID")
pca.plot <- function(x, overlaid.info, sample.names = NULL, show.names = TRUE, color.vals = NULL){
if(is.null(sample.names)){
sample.names <- colnames(x)
} else{
if(length(sample.names) != ncol(x)){
stop("The length of `sample.names` should be equal to number of samples.")
}
}
# eliminate NaN values before-hand if there is any.
pca <- stats::prcomp(t(stats::na.omit(x)), center = TRUE)
d <- round(pca$sdev^2/sum(pca$sdev^2)*100, digits=1)
xl <- sprintf("PC 1: %.1f %%", d[1])
yl <- sprintf("PC 2: %.1f %%", d[2])
plot.df <- data.frame(PC1 = as.numeric(pca$x[,1]),
PC2 = as.numeric(pca$x[,2]),
overlaid.info = overlaid.info,
names = sample.names
)
plot.pca <- ggplot2::ggplot(plot.df, ggplot2::aes(PC1, PC2, color = overlaid.info)) +
ggplot2::geom_point(size = 4) +
ggplot2::labs(x=xl,y=yl) +
ggplot2::theme_minimal() +
ggplot2::labs(color = "Status") +
ggplot2::coord_fixed(ratio = 1) +
ggplot2::theme_light()
if (typeof(overlaid.info) %in% c("character", "factor")){
if (!is.null(color.vals)){
plot.pca <- plot.pca +
ggplot2::scale_color_manual(values = color.vals)
} else{
plot.pca <- plot.pca +
ggplot2::scale_color_manual(values = RColorBrewer::brewer.pal(n = 9, name = "Set1"))
}
}
if(show.names){
plot.pca <- plot.pca + ggrepel::geom_text_repel(ggplot2::aes(label = names))
}
return(plot.pca)
}
color.vals <- c("memory-3mo" = "#45b575",
"memory-18mo"= "#1a9850",
"naive-3mo"= "#99d594",
"naive-18mo" = "#80e378",
"PBL-3mo" = "#d73027",
"PBL-18mo" = "#a62019",
"spleen-3mo"="#6e93c2",
"spleen-12mo"="#4575b4",
"spleen-18mo"="#265591")
meta.data$TCT_Age<- paste0(meta.data$TCT, "-", meta.data$Age)
meta.data$TCT[meta.data$TCT == "memory"] <- "CD8 memory"
meta.data$TCT[meta.data$TCT == "naive"] <- "CD8 naive"
# eliminate NaN values before-hand if there is any.
pca <- stats::prcomp(t(stats::na.omit(count.matrix.normalized[(na.omit(ap1.genes.ens)),])), center = TRUE)
d <- round(pca$sdev^2/sum(pca$sdev^2)*100, digits=1)
xl <- sprintf("PC 1: %.1f %%", d[1])
yl <- sprintf("PC 2: %.1f %%", d[2])
plot.df <- data.frame(PC1 = as.numeric(pca$x[,1]),
PC2 = as.numeric(pca$x[,2]),
TCT = meta.data$TCT,
Age = gsub("mo", "", meta.data$Age)
)
plot.pca.ap1.mice <- ggplot(plot.df, aes(PC1, PC2, color = Age, shape = TCT)) +
geom_point(size = 4) +
labs(x=xl,y=yl) +
theme_minimal() +
labs(shape = "Tissue/Cell Type") +
#coord_fixed(ratio = 1) +
theme_light(base_size = 16) +
scale_color_manual(values = color_values) +
scale_shape_manual(values=c(15:18))
ggsave(filename = "./output/plot_PCA_AP1_mice.pdf",
plot = plot.pca.ap1.mice,
useDingbats = FALSE, width = 7, height = 5)
plot.pca.ap1.mice.wo12 <- ggplot(plot.df %>% filter(Age != 12),
aes(PC1, PC2, color = Age, shape = TCT)) +
geom_point(size = 4) +
labs(x=xl,y=yl) +
theme_minimal() +
labs(shape = "Tissue/Cell Type") +
#coord_fixed(ratio = 1) +
theme_light(base_size = 16) +
scale_color_manual(values = color_values) +
scale_shape_manual(values=c(15:18))
pdf("./output/plot_PCA_AP1_mice_wo12.pdf", width=7, height=5)
print(plot.pca.ap1.mice.wo12)
dev.off()
```