-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4. CAGE HEATMAP T30.R
194 lines (164 loc) · 7.55 KB
/
4. CAGE HEATMAP T30.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
#### Heatmap of DE TCs at 30 minutes
#### Axel Thieffry - November 2020
set.seed(42)
library(tidyverse)
library(tidylog)
library(magrittr)
library(stringr)
library(reshape2)
library(CAGEfightR)
library(RColorBrewer)
library(BiocParallel)
library(edgeR)
library(limma)
library(DESeq2)
library(gprofiler2)
library(patchwork)
library(pheatmap)
register(MulticoreParam(workers=6))
options(scipen=10) # disable scientific notation
'%!in%' <- function(x,y)!('%in%'(x,y))
'select' <- dplyr::select
'rename' <- dplyr::rename
'h' <- head
'l' <- length
# 1. READ DATA ####
# -----------------
TCs <- readRDS('data/RDS files/SE_TCs.rds')
detcs <- readRDS('data/RDS files/DE_TCs.rds')
# 2. PREPARE EXPRESSION MATRICES ####
# -----------------------------------
# 2a. get CAGE TCs that are up & down regulated at t=30 minutes
t30_up_TCs <- detcs %>% filter(coef=='t30' & direction=='up') %>% select(TC_id, logFC, adj.P.Val)
t30_down_TCs <- detcs %>% filter(coef=='t30' & direction=='down') %>% select(TC_id, logFC, adj.P.Val)
# 2b. get TPM matrix for both TC sets
sample_order <- c(paste0(rep(c('wt_', 'hen2_', 'rrp4_'), each=3), rep('0_', 9), rep(c('R1', 'R2', 'R3'), 3)),
paste0(rep(c('wt_', 'hen2_', 'rrp4_'), each=3), rep('30_', 9), rep(c('R1', 'R2', 'R3'), 3)))
t30_up_TPM <- assay(TCs, 'TPM') %>%
as.data.frame() %>%
rownames_to_column('TC_id') %>%
filter(TC_id %in% t30_up_TCs$TC_id) %>%
select(TC_id, all_of(sample_order)) %>%
column_to_rownames('TC_id')
t30_down_TPM <- assay(TCs, 'TPM') %>%
as.data.frame() %>%
rownames_to_column('TC_id') %>%
filter(TC_id %in% t30_down_TCs$TC_id) %>%
select(TC_id, all_of(sample_order)) %>%
column_to_rownames('TC_id')
# 2c. make mean TPM matrix for both TC sets
t30_up_meanTPM <- t30_up_TPM %>%
rownames_to_column('TC_id') %>%
melt(id.vars='TC_id', value.name='TPM', variable.name='sample') %>%
mutate('sample'=str_remove(sample, '_R[123]$')) %>%
group_by(TC_id, sample) %>%
summarise('meanTPM'=mean(TPM)) %>%
spread(sample, meanTPM) %>%
column_to_rownames('TC_id') %>%
select('wt_0', 'hen2_0', 'rrp4_0', 'wt_30', 'hen2_30', 'rrp4_30')
t30_down_meanTPM <- t30_down_TPM %>%
rownames_to_column('TC_id') %>%
melt(id.vars='TC_id', value.name='TPM', variable.name='sample') %>%
mutate('sample'=str_remove(sample, '_R[123]$')) %>%
group_by(TC_id, sample) %>%
summarise('meanTPM'=mean(TPM)) %>%
spread(sample, meanTPM) %>%
column_to_rownames('TC_id') %>%
select('wt_0', 'hen2_0', 'rrp4_0', 'wt_30', 'hen2_30', 'rrp4_30')
# 3. HEATMAPS ####
# ----------------
# 3a. make colors
heatmap_colors <- colorRampPalette(colors=c('navy', 'white', 'red'))
# 3b. plot using averaged TPM values
t30_up_meanTPM %>%
add(1) %>%
log(base=2) %>%
pheatmap(cluster_cols=F, cluster_rows=T, show_rownames=F, show_colnames=T,
gaps_col=3, scale='row', clustering_method='ward.D2', cellwidth=20,
clustering_distance_rows='correlation', color=heatmap_colors(100))
t30_down_meanTPM %>%
add(1) %>%
log(base=2) %>%
pheatmap(cluster_cols=F, cluster_rows=T, show_rownames=F, show_colnames=T,
gaps_col=3, scale='row', clustering_method='ward.D2', cellwidth=20,
clustering_distance_rows='correlation', color=heatmap_colors(100))
# 4. GO ENRICHMENT ANALYSIS ####
# ------------------------------
# 4a. correspondence between TCs and their geneID
TCs_gene_correspondences <- rowRanges(TCs) %>%
as.data.frame() %>%
select(geneID) %>%
filter(!is.na(geneID)) %>%
rownames_to_column('TC_id') %>%
as_tibble()
# 4b. get all genes detected in the experiment (GSEA background)
universe <- unique(TCs_gene_correspondences$geneID) # 18,934 individual genes detected
# 4c. get genes with up/down-regulated CAGE TCs
t30_up_genes <- t30_up_TCs %>%
left_join(TCs_gene_correspondences, by='TC_id') %>%
pull(geneID) %>%
unique()
t30_down_genes <- t30_down_TCs %>%
left_join(TCs_gene_correspondences, by='TC_id') %>%
pull(geneID) %>%
unique()
# 4d. perform GO enrichment analysis
go_up <- gost(query=t30_up_genes, organism='athaliana', significant=T, user_threshold=0.05, correction_method='fdr', custom_bg=universe, ordered_query=F)$result %>% as_tibble()
go_down <- gost(query=t30_down_genes, organism='athaliana', significant=T, user_threshold=0.05, correction_method='fdr', custom_bg=universe, ordered_query=F)$result %>% as_tibble()
# 4e. plot TOP10 GO enrichment
go_up %>%
mutate('p_value_transformed'=-log(p_value, base=10)) %>%
slice_max(order_by=p_value_transformed, n=10, with_ties=F) %>%
ggplot(aes(x=10:1, y=p_value_transformed)) +
geom_col(fill='red', alpha=.5) +
geom_text(aes(label=term_name, y=2), hjust=0, col='black') +
coord_flip() +
cowplot::theme_cowplot() + theme(axis.text.y=element_blank(), aspect.ratio=.75) +
scale_y_continuous(expand=c(0, 0)) +
labs(x='Top10 GO term', y='', title='GSEA t30 min', subtitle='Genes with up-regulated CAGE TCs') +
go_down %>%
mutate('p_value_transformed'=-log(p_value, base=10)) %>%
slice_max(order_by=p_value_transformed, n=10, with_ties=F) %>%
ggplot(aes(x=10:1, y=p_value_transformed)) +
geom_col(fill='navy', alpha=.5) +
geom_text(aes(label=term_name, y=.3), hjust=0, col='black') +
coord_flip() +
cowplot::theme_cowplot() + theme(axis.text.y=element_blank(), aspect.ratio=.75) +
scale_y_continuous(expand=c(0, 0)) +
labs(x='Top10 GO term', y='-log(adj.P.Value, base=10)', subtitle='Genes with down-regulated CAGE TCs') +
plot_layout(ncol=1)
# 5. SCATTER PLOTS ####
# ---------------------
# 5a. get TPM matrix for samples at 0 and 30 minutes for up-regulated TCs at 30 min
meanTPM_scatter <- t30_up_meanTPM %>%
filter(rownames(.) %in% t30_up_TCs$TC_id) %>%
rownames_to_column('TCid')
# 5b. compute fold-change
pseudocount <- 1
scatter_df <- meanTPM_scatter %>%
column_to_rownames('TCid') %>%
add(pseudocount) %>%
mutate('wt_fc'=wt_30/wt_0,
'hen2_fc'=hen2_30/hen2_0,
'rrp4_fc'=rrp4_30/rrp4_0) %>%
select(matches('fc'))
n_TCs <- nrow(scatter_df)
ggplot(scatter_df, aes(x=wt_fc, y=hen2_fc)) +
geom_point(alpha=.5) +
geom_smooth(method='lm', se=F) +
geom_abline(intercept=0, slope=1, col='red', lty=2, lwd=1) +
scale_x_log10() +
scale_y_log10() +
cowplot::theme_cowplot() + theme(aspect.ratio=1) +
labs(x='wt FC (wt30/wt0)', y='hen2 FC (hen2 30/0)',
title='FC scatterplot for CAGE TCs up-regulated at t30',
subtitle=paste0('N(TCs)=', n_TCs,' from left heatmap of Figure 1D')) +
ggplot(scatter_df, aes(x=wt_fc, y=rrp4_fc)) +
geom_point(alpha=.5) +
geom_smooth(method='lm', se=F) +
geom_abline(intercept=0, slope=1, col='red', lty=2, lwd=1) +
scale_x_log10() +
scale_y_log10() +
cowplot::theme_cowplot() + theme(aspect.ratio=1) +
labs(x='wt FC (wt30/wt0)', y='rrp4 FC (rrp4 30/0)') +
plot_layout(ncol=1)