-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtw_cluster_cut_run_DEGs_new.R
352 lines (261 loc) · 14.3 KB
/
dtw_cluster_cut_run_DEGs_new.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
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
library(Seurat)
library(ggplot2)
library(ggfortify)
library(jcolors)
require(gridExtra)
library(grid)
library(gam)
library(princurve)
library(parallel)
library(tidyverse)
library(MyEllipsefit)
library(sctransform)
library(openxlsx)
library(doParallel)
library(tidytext)
library(ggrepel)
library(dtwclust)
library(geomtextpath)
library(bigmemory)
source('./util_funcs.R')
source('./util_funcs_YR.R')
## IDs
prod.desc <- read.xlsx('../Input/toxo_genomics/genes/ProductDescription_GT1.xlsx')
TGGT1_ME49 <- read.xlsx('../Input/toxo_genomics/Orthologs/TGGT1_ME49 Orthologs.xlsx')
## scDATA
rna_sub <- readRDS('../Input/toxo_cdc/rds_ME49_59/S.O_intra_lables_pt.rds')
atac_sub <- readRDS('../Input/toxo_cdc/rds_ME49_59/S.O_intra_atac_lables_pt.rds')
## Splines
sc.rna.spline.fits <- readRDS('../Input/toxo_cdc/rds_ME49_59/sc_rna_spline_fits_all_genes.rds')
sc.atac.spline.fits <- readRDS('../Input/toxo_cdc/rds_ME49_59/sc_atac_spline_fits_all_genes.rds')
## Turn the data into wide format (time by gene) and center & scale each gene
sc.rna.dtw.wide <- sc.rna.spline.fits %>%
pivot_wider(names_from = 'GeneID', values_from = 'y') %>%
mutate_at(vars(matches("TGME")), ~scale(., center = T, scale = T)) %>%
as.data.frame()
na.ind <- which(apply(sc.rna.dtw.wide, 2, function(x) any(is.na(x))))
if(length(na.ind)){
sc.rna.dtw.wide <- sc.rna.dtw.wide[,-na.ind]
}
sc.atac.dtw.wide <- sc.atac.spline.fits %>%
pivot_wider(names_from = 'GeneID', values_from = 'y') %>%
mutate_at(vars(matches("TGME")), ~scale(., center = T, scale = T)) %>%
as.data.frame()
na.ind <- which(apply(sc.atac.dtw.wide, 2, function(x) any(is.na(x))))
if(length(na.ind)){
sc.atac.dtw.wide <- sc.atac.dtw.wide[,-na.ind]
}
sc.rna.mu.scale <- sc.rna.dtw.wide %>%
pivot_longer(-x, names_to = 'GeneID', values_to = 'expr')
sc.atac.mu.scale <- sc.atac.dtw.wide %>%
pivot_longer(-x, names_to = 'GeneID', values_to = 'expr')
##
## This function performs dynamic time warping clustering for both rna and atac profiles
## it gets a table of genes,
## the table should include the product description (gene ID and gene Name)
## you can specify the number of clusters you are looking for (cannot be less than 2)
clust.df <- function(tab , num.clust) {
k <- num.clust
sc.rna <- sc.rna.dtw.wide[,colnames(sc.rna.dtw.wide) %in% tab$gene_name ]
sc.atac <- sc.atac.dtw.wide[,colnames(sc.atac.dtw.wide) %in% tab$gene_name]
sc.atac<- sc.atac.dtw.wide[,colnames(sc.atac.dtw.wide) %in% colnames(sc.rna)]
sc.rna.markers.hc_dtw <- dtwClustCurves(sc.rna, nclust = k)
sc.atac.markers.hc_dtw <- dtwClustCurves(sc.atac, nclust = k)
tab <- tab[tab$gene_name %in% colnames(sc.rna),]
sc.rna.long <- inner_join(sc.rna.mu.scale, tab, by = c('GeneID' = 'gene_name'))
sc.rna.long <- sc.rna.long %>%
transmute(time = x, GeneID = GeneID, normExpr = expr, Name = ProductDescription) %>% distinct()
sc.atac.long <- inner_join(sc.atac.mu.scale, tab, by = c('GeneID' = 'gene_name'))
sc.atac.long <- sc.atac.long %>%
transmute(time = x, GeneID = GeneID, normExpr = expr, Name = ProductDescription) %>% distinct()
sc.rna.clust.info <- data.frame(GeneID = colnames(sc.rna), cluster = cutree(sc.rna.markers.hc_dtw, k = k))
sc.atac.clust.info <- data.frame(GeneID = colnames(sc.atac), cluster = cutree(sc.atac.markers.hc_dtw, k = k))
sc.rna.long.clust <- inner_join(sc.rna.long, sc.rna.clust.info, by = 'GeneID')
sc.atac.long.clust <- inner_join(sc.atac.long, sc.atac.clust.info, by = 'GeneID')
sc.rna.sc.atac.joint <- inner_join(sc.rna.long.clust, sc.atac.long.clust,
by = c("time", "GeneID", "Name"))
colnames(sc.rna.sc.atac.joint) <- c("time", "GeneID", "scRNA", "Name", "cluster.RNA", "scATAC", "cluster.ATAC")
sc.rna.sc.atac.joint.long <- sc.rna.sc.atac.joint %>%
pivot_longer(-c('time', "GeneID", "Name", "cluster.RNA", "cluster.ATAC"),
names_to = 'data', values_to = 'normExpr')
sc.rna.sc.atac.joint.long$cluster.RNA <- paste('C', sc.rna.sc.atac.joint.long$cluster.RNA)
sc.rna.sc.atac.joint.long$cluster.ATAC <- paste('C', sc.rna.sc.atac.joint.long$cluster.ATAC)
return(sc.rna.sc.atac.joint.long)
}
## plot the expression and accessibility of genes within each cluster
# facet by rna cluster
plot_rna_atac_trends <- function(sc.rna.sc.atac.joint.long.sub){
p <- ggplot(sc.rna.sc.atac.joint.long.sub, aes(x= time,y=normExpr)) +
geom_path(aes(color = GeneID),alpha = 0.8, size = 0.8)+
theme_bw(base_size = 14) +
theme(legend.position = "right") +
ylab('normExpr') + xlab('Time') +
theme(axis.text.x = element_text(angle = 0, hjust = 1, size = 18, face="bold", colour = "black")) +
theme(axis.text.y = element_text(angle = 0, hjust = 1, size = 18, face="bold", colour = "black")) +
theme(strip.background = element_rect(colour="black", fill="white",size=0.5, linetype="solid")) +
theme(strip.text = element_text(size = 18, face="bold", angle = 0)) +
coord_cartesian(xlim = c(0,6.5)) +
facet_grid(cluster.RNA ~ data, scales = 'free', space = 'free') +
theme(
plot.title = element_text(size=20, face = "bold.italic", color = 'red'),
axis.title.x = element_text(size=18, face="bold", hjust = 1),
axis.title.y = element_text(size=18, face="bold")
) +
theme(#legend.position = c(0.15, 0.85),
legend.position = 'none',
legend.title = element_text(colour="black", size=12,
face="bold"),
legend.text = element_text(colour="black", size=12,
face="bold"))
return(p)
}
## plot the expression and accessibility of genes within each cluster
#facet by atac cluster
plot_atac_trends <- function(sc.rna.sc.atac.joint.long.sub){
p <- ggplot(sc.rna.sc.atac.joint.long.sub, aes(x= time,y=normExpr)) +
geom_path(aes(color = GeneID),alpha = 0.8, size = 0.8)+
theme_bw(base_size = 14) +
theme(legend.position = "right") +
ylab('normExpr') + xlab('Time') +
theme(axis.text.x = element_text(angle = 0, hjust = 1, size = 18, face="bold", colour = "black")) +
theme(axis.text.y = element_text(angle = 0, hjust = 1, size = 18, face="bold", colour = "black")) +
theme(strip.background = element_rect(colour="black", fill="white",size=0.5, linetype="solid")) +
theme(strip.text = element_text(size = 18, face="bold", angle = 0)) +
coord_cartesian(xlim = c(0,6.5)) +
facet_grid(cluster.ATAC ~ data, scales = 'free', space = 'free') +
theme(
plot.title = element_text(size=20, face = "bold.italic", color = 'red'),
axis.title.x = element_text(size=18, face="bold", hjust = 1),
axis.title.y = element_text(size=18, face="bold")
) +
theme(#legend.position = c(0.15, 0.85),
legend.position = 'none',
legend.title = element_text(colour="black", size=12,
face="bold"),
legend.text = element_text(colour="black", size=12,
face="bold"))
return(p)
}
##########################################################
# intersection of 4 CUT&RUN + DEG (KD_vs_WT, phase based)
##########################################################
tab <- readRDS("../Input/toxo_cdc/rds_ME49_59/cut_run_union_new_peaks_march_motif_modulated_genes_lfs_v5.rds")
HC.peaks <- tab %>%
filter(intersection_CutRun_dataSets == "yes" & KD_vs_WT_phase_based %in% c('down_reg', 'up_reg', 'modulated') ) %>%
dplyr::select(chr, start_peak, end_peak, V4, V5, V6, TGME49, intersection_CutRun_dataSets, KD_vs_WT_phase_based,
ProductDescription , Category ) %>%
distinct()
names(HC.peaks)[9] <- "dir"
### cluster up_reg genes
HC.peaks <- HC.peaks %>% filter(dir == "up_reg")
HC.peaks$gene_name <- gsub("_", "-", HC.peaks$TGME49)
HC.peaks.clust <- clust.df(HC.peaks, num.clust = 3)
#HC.peaks.clust <- HC.peaks.clust[HC.peaks.clust$data == "scRNA",]
p <- plot_rna_atac_trends(HC.peaks.clust)
p
ggsave('../Output/toxo_cdc/ME49_59/figures_paper/High_conf_peaks_up_reg_KD_vs_WT_phase_based_3_clust.pdf',
plot = p, width = 6, height = 6, dpi = 300)
##
HC.peaks.clust.df <- HC.peaks.clust %>% dplyr::select(GeneID, Name, cluster.RNA) %>% distinct()
write.xlsx(HC.peaks.clust.df, "../Output/toxo_cdc/ME49_59/tables/High_conf_peaks_up_reg_KD_vs_WT_phase_based_3_clust.xlsx")
### cluster down_reg genes
tab <- readRDS("../Input/toxo_cdc/rds_ME49_59/cut_run_union_new_peaks_march_motif_modulated_genes_lfs_v5.rds")
names(tab)[1] <- "chr"
HC.peaks <- tab %>%
filter(intersection_CutRun_dataSets == "yes" & KD_vs_WT_phase_based %in% c('down_reg', 'up_reg', 'modulated') ) %>%
dplyr::select(chr, start_peak, end_peak, V4, V5, V6,TGME49,intersection_CutRun_dataSets,
KD_vs_WT_phase_based,ProductDescription , Category ) %>%
distinct()
names(HC.peaks)[9] <- "dir"
HC.peaks <- HC.peaks %>% filter(dir == "down_reg")
colnames(HC.peaks) <- gsub("ProductDescription.x", "ProductDescription", colnames(HC.peaks))
HC.peaks$gene_name <- gsub("_", "-", HC.peaks$TGME49)
HC.peaks.clust <- clust.df(HC.peaks, num.clust = 3)
#HC.peaks.clust <- HC.peaks.clust[HC.peaks.clust$data == "scRNA",]
p <- plot_rna_atac_trends(HC.peaks.clust)
p
ggsave('../Output/toxo_cdc/ME49_59/figures_paper/High_conf_peaks_down_reg_KD_vs_WT_phase_based_3_clust.pdf',
plot = p, width = 6, height = 6, dpi = 300)
HC.peaks.clust.df <- HC.peaks.clust %>% dplyr::select(GeneID, Name, cluster.RNA) %>% distinct()
write.xlsx(HC.peaks.clust.df, "../Output/toxo_cdc/ME49_59/tables/High_conf_peaks_down_reg_KD_vs_WT_phase_based_3_clust.xlsx")
### cluster ribosomal genes
tab <- readRDS("../Input/toxo_cdc/rds_ME49_59/cut_run_union_new_peaks_march_motif_modulated_genes_lfs_v5.rds")
names(tab)[1] <- "chr"
HC.peaks <- tab %>%
filter(intersection_CutRun_dataSets == "yes" & KD_vs_WT_phase_based %in% c('down_reg', 'up_reg', 'modulated') ) %>%
dplyr::select(chr, start_peak, end_peak, V4, V5, V6,TGME49,intersection_CutRun_dataSets,
KD_vs_WT_phase_based,ProductDescription , Category ) %>%
distinct()
names(HC.peaks)[9] <- "dir"
HC.peaks <- HC.peaks %>% filter(Category == "ribosomal")
colnames(HC.peaks) <- gsub("ProductDescription.x", "ProductDescription", colnames(HC.peaks))
HC.peaks$gene_name <- gsub("_", "-", HC.peaks$TGME49)
HC.peaks.clust <- clust.df(HC.peaks, num.clust = 3)
HC.peaks.clust <- HC.peaks.clust[HC.peaks.clust$data == "scRNA",]
p <- plot_rna_atac_trends(HC.peaks.clust)
p
ggsave('../Output/toxo_cdc/ME49_59/figures_paper/High_conf_peaks_down_reg_KD_vs_WT_ribosomal_3_clust.pdf',
plot = p, width = 6, height = 6, dpi = 300)
###########################################################
# intersection of 4 CUT&RUN + DEG (KD_vs_WT, Global)
##########################################################
tab <- readRDS("../Input/toxo_cdc/rds_ME49_59/cut_run_union_new_peaks_march_motif_modulated_genes_lfs_v4.rds")
names(tab)[1] <- "chr"
tab <- tab %>%
filter(intersection == "yes" & Global_KD_vs_WT %in% c('down_reg', 'up_reg', 'modulated') ) %>%
dplyr::select(chr, start_peak, end_peak, V4, V5, V11,gene_name,intersection, Global_KD_vs_WT, ProductDescription.x , Category ) %>%
distinct()
names(tab)[9] <- "dir"
direct <- "down_reg"
HC.peaks <- tab %>% filter(dir == direct)
colnames(HC.peaks) <- gsub("ProductDescription.x", "ProductDescription", colnames(HC.peaks))
HC.peaks$gene_name <- gsub("_", "-", HC.peaks$gene_name)
HC.peaks.clust <- clust.df(HC.peaks, num.clust = 2)
HC.peaks.clust <- HC.peaks.clust[HC.peaks.clust$data == "scRNA",]
p <- plot_rna_atac_trends(HC.peaks.clust) + ggtitle(direct)
p
ggsave('../Output/toxo_cdc/ME49_59/figures_paper/High_conf_peaks_down_reg_Global_KD_vs_WT_2_clust.pdf',
plot = p, width = 6, height = 6, dpi = 300)
HC.peaks.clust.df.down <- HC.peaks.clust %>% dplyr::select(GeneID, Name, cluster.RNA) %>% distinct()
write.xlsx(HC.peaks.clust.df.down, "../Output/toxo_cdc/ME49_59/tables/High_conf_peaks_down_reg_Global_KD_vs_WT_2_clust.xlsx")
# up-reg
tab <- readRDS("../Input/toxo_cdc/rds_ME49_59/cut_run_union_new_peaks_march_motif_modulated_genes_lfs_v4.rds")
names(tab)[1] <- "chr"
tab <- tab %>%
filter(intersection == "yes" & Global_KD_vs_WT %in% c('down_reg', 'up_reg', 'modulated') ) %>%
dplyr::select(chr, start_peak, end_peak, V4, V5, V11,gene_name,intersection, Global_KD_vs_WT, ProductDescription.x , Category ) %>%
distinct()
names(tab)[9] <- "dir"
direct <- "up_reg"
HC.peaks <- tab %>% filter(dir == direct)
colnames(HC.peaks) <- gsub("ProductDescription.x", "ProductDescription", colnames(HC.peaks))
HC.peaks$gene_name <- gsub("_", "-", HC.peaks$gene_name)
HC.peaks.clust <- clust.df(HC.peaks, num.clust = 2)
HC.peaks.clust <- HC.peaks.clust[HC.peaks.clust$data == "scRNA",]
p <- plot_rna_atac_trends(HC.peaks.clust) + ggtitle(direct)
p
ggsave('../Output/toxo_cdc/ME49_59/figures_paper/High_conf_peaks_up_reg_Global_KD_vs_WT_2_clust.pdf',
plot = p, width = 6, height = 6, dpi = 300)
HC.peaks.clust.df.up <- HC.peaks.clust %>% dplyr::select(GeneID, Name, cluster.RNA) %>% distinct()
write.xlsx(HC.peaks.clust.df, "../Output/toxo_cdc/ME49_59/tables/High_conf_peaks_up_reg_Global_KD_vs_WT_2_clust.xlsx")
up.down.global <- rbind(HC.peaks.clust.df.up, HC.peaks.clust.df.down)
## ribosomals (which are only present in down-reg list)
tab <- readRDS("../Input/toxo_cdc/rds_ME49_59/cut_run_union_new_peaks_march_motif_modulated_genes_lfs_v4.rds")
names(tab)[1] <- "chr"
tab <- tab %>%
filter(intersection == "yes" & Global_KD_vs_WT %in% c('down_reg', 'up_reg', 'modulated') ) %>%
dplyr::select(chr, start_peak, end_peak, V4, V5, V11,gene_name,intersection, Global_KD_vs_WT, ProductDescription.x , Category ) %>%
distinct()
names(tab)[9] <- "dir"
HC.peaks <- tab %>% filter(Category == "ribosomal")
colnames(HC.peaks) <- gsub("ProductDescription.x", "ProductDescription", colnames(HC.peaks))
HC.peaks$gene_name <- gsub("_", "-", HC.peaks$gene_name)
HC.peaks.clust <- clust.df(HC.peaks, num.clust = 2)
HC.peaks.clust <- HC.peaks.clust[HC.peaks.clust$data == "scRNA",]
#p <- plot_rna_atac_trends(HC.peaks.clust) + ggtitle(direct)
p <- plot_rna_atac_trends(HC.peaks.clust) + ggtitle("ribosomals")
p
# ribosomals
write.xlsx(HC.peaks.clust, "../Output/toxo_cdc/ME49_59/tables/High_conf_peaks_down_reg_Global_KD_vs_WT_ribosomal_2_clust.xlsx")
ggsave('../Output/toxo_cdc/ME49_59/figures_paper/High_conf_peaks_down_reg_Global_KD_vs_WT_ribosomal_2_clust.pdf',
plot = p, width = 6, height = 6, dpi = 300)