-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_dea.R
708 lines (602 loc) · 26.8 KB
/
sample_dea.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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# Identify Marker gene expression
library(Matrix)
library(Seurat)
library(tibble)
library(plyr)
library(dplyr)
library(xtable)
library(enrichR)
library(pheatmap)
library(RColorBrewer)
library(pheatmap)
library(ggplot2)
library(ggpubr)
input.dir <- "/home/daniel/master_thesis/bassoon_data/Output/final_ct_class///"
output.dir <- "/home/daniel/master_thesis/bassoon_data/Output/final_downstream_analysis/"
analysis.out <- "/home/daniel/master_thesis/bassoon_data/Output/final_downstream_analysis/"
dir.create(output.dir, showWarnings = FALSE, recursive = TRUE)
setwd(output.dir)s
samples <- paste0("Bsn.", seq(221931, 221940))
samples <- samples[-5]
LoadSeuratFiles <- function(
dir,
log.counts = TRUE,
samples,
counts = "logcounts.mtx",
feature = "features.txt",
barcode = "barcodes.txt",
celltype = "celltype.txt"
){
seurat.objects <- list()
ls <- list.files(dir)
setwd(dir)
first <- TRUE
for (sample in samples){
ind <- grep(pattern = sample, ls)
count.file <- grep(counts, ls[ind], value = TRUE)
feature.file <- grep(feature, ls[ind], value = TRUE)
barcode.file <- grep(barcode, ls[ind], value = TRUE)
celltype.file <- grep(celltype, ls[ind], value = TRUE)
count.table <- readMM(count.file)
features <- read.csv(feature.file, stringsAsFactors = F, header = F)
barcodes <- read.csv(barcode.file, stringsAsFactors = F, header = F)
celltypes <- read.csv(celltype.file, stringsAsFactors = F, header = F)
rownames(count.table) <- features$V1
colnames(count.table) <- barcodes$V1
seurat.object <- CreateSeuratObject(project = sample, counts = count.table)
seurat.object$CellType <- celltypes$V1
Idents(seurat.object) <- seurat.object$CellType
seurat.object <- RenameCells(seurat.object, add.cell.id = seurat.object$orig.ident)
if (!log.counts){
seurat.object <- NormalizeData(seurat.object, normalization.method = "LogNormalize", scale.factor = 1000)
}
if (first){
seurat.objects <- seurat.object
first = FALSE
} else {
seurat.objects <- merge(seurat.objects, seurat.object)
}
}
return(seurat.objects)
}
seurat.objects <- LoadSeuratFiles(dir = input.dir, samples = samples)
# Combine Genotypes together
# 1,2: C57BL/6J_WT
# 3,4,5: C57BL/6J Bsn_mut
# 6,7: C57BL/6NJ WT
# 8,9,10: C57BL/6NJ Bsn_KO
seurat.objects$genotype <- NA
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221931"] <- "C57BL/6J_WT"
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221932"] <- "C57BL/6J_WT"
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221933"] <- "C57BL/6J_Bsn_mut"
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221934"] <- "C57BL/6J_Bsn_mut"
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221935"] <- "C57BL/6J_Bsn_mut"
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221936"] <- "C57BL/6NJ_WT"
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221937"] <- "C57BL/6NJ_WT"
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221938"] <- "C57BL/6NJ_Bsn_KO"
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221939"] <- "C57BL/6NJ_Bsn_KO"
seurat.objects$genotype[seurat.objects$orig.ident == "Bsn.221940"] <- "C57BL/6NJ_Bsn_KO"
seurat.objects$CellType <- Idents(seurat.objects)
seurat.objects$ct.gt <- paste(seurat.objects$genotype, seurat.objects$CellType, sep = ".")
Idents(seurat.objects) <- seurat.objects$ct.gt
# generate useful info
table_gen_ct <- table(seurat.objects$genotype, seurat.objects$CellType)
write.table(file = paste0(analysis.out, "/table_gen_ct.csv"), x = table_gen_ct, sep = ",")
# Compare Rods and Cones to each other --> Different vulnerability
deg <- list()
deg[["wt1.rod.cone"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6J_WT.rod",
ident.2 = "C57BL/6J_WT.cone", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
deg[["wt2.rod.cone"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6NJ_WT.rod",
ident.2 = "C57BL/6NJ_WT.cone", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
deg[["mut.rod.cone"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6J_Bsn_mut.rod",
ident.2 = "C57BL/6J_Bsn_mut.cone", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
deg[["ko.rod.cone"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6NJ_Bsn_KO.rod",
ident.2 = "C57BL/6NJ_Bsn_KO.cone", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
deg[["wt1.wt2.rod"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6J_WT.rod",
ident.2 = "C57BL/6NJ_WT.rod", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
deg[["wt1.wt2.cone"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6J_WT.cone",
ident.2 = "C57BL/6NJ_WT.cone", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
# Compare rod vs. rod, cone vs. cone
# mutant strain
deg[["wt.mut.rod"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6J_WT.rod",
ident.2 = "C57BL/6J_Bsn_mut.rod", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
deg[["wt.mut.cone"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6J_WT.cone",
ident.2 = "C57BL/6J_Bsn_mut.cone", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
# KO strain
deg[["wt.ko.rod"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6NJ_WT.rod",
ident.2 = "C57BL/6NJ_Bsn_KO.rod", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
deg[["wt.ko.cone"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6NJ_WT.cone",
ident.2 = "C57BL/6NJ_Bsn_KO.cone", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
# Compare Mutant to KO
deg[["mut.ko.rod"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6J_Bsn_mut.rod",
ident.2 = "C57BL/6NJ_Bsn_KO.rod", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
deg[["mut.ko.cone"]] <- FindMarkers(seurat.objects, ident.1 = "C57BL/6J_Bsn_mut.cone",
ident.2 = "C57BL/6NJ_Bsn_KO.cone", logfc.threshold = 0.1, min.pct = 0.05) %>%
rownames_to_column("gene") %>% dplyr::filter(p_val_adj <= 0.05)
lapply(deg, function(x){write.table(as.data.frame(x),
file = paste0(analysis.out, "/deg.table.csv"),
append = T,
sep = ",")})
save(deg, file = "/home/daniel/master_thesis/bassoon_data/Output/final_downstream_analysis/deg_list.Robj")
# Enrichment analysis
dbs <- listEnrichrDbs()
websiteLive <- ifelse(is.null(dbs), FALSE, TRUE)
if (websiteLive) head(dbs)
databases <- c("GO_Biological_Process_2018", "KEGG_2019_Mouse", "MGI_Mammalian_Phenotype_Level_4_2019", "Mouse_Gene_Atlas", "GO_Molecular_Function_2018")
databases <- c("KEGG_2019_Mouse")
enriched <- list()
i <- 1
deg.sep <- list()
for (table in deg){
name <- names(deg)[i]
table$expr <- ifelse(table$avg_logFC > 0, "up", "down")
up <- dplyr::filter(table, expr == "up")
down <- dplyr::filter(table, expr == "down")
deg.sep[[paste(name, "up", sep = ".")]] <- up
deg.sep[[paste(name, "down", sep = ".")]] <- down
enriched.all <- enrichr(table$gene, databases)
enriched[[paste(name, "all", sep = ".")]] <- enriched.all
enriched.up <- enrichr(up$gene, databases)
enriched[[paste(name, "up", sep = ".")]] <- enriched.up
enriched.down <- enrichr(down$gene, databases)
enriched[[paste(name, "down", sep = ".")]] <- enriched.down
i <- i + 1
}
head(enriched)
i <- 1
names <- names(enriched)
file <- "/home/daniel/master_thesis/bassoon_data/Output/final_downstream_analysis/pathway_analysis.csv"
write("", file = file)
for (x in enriched){
name <- names[i]
x <- as.data.frame(x)
x <- dplyr::select(x, KEGG_2019_Mouse.Term, KEGG_2019_Mouse.Overlap,
KEGG_2019_Mouse.Adjusted.P.value, KEGG_2019_Mouse.Genes)
x <- dplyr::filter(x, KEGG_2019_Mouse.Adjusted.P.value < 0.2)
i <- i + 1
write(name, file = file, append = T)
write.table(x, file = file, append = T)
}
# Generate Heatmap with DE genes of all. LogFC as color -- only cone genes
gene.list <- lapply(deg, "[", , c(1,3))
cone.deg.list <- lapply(gene.list, "[", , 1)
cone.deg.list <- unique(unlist(cone.deg.list[grepl(names(cone.deg.list), pattern = "cone")], use.names = F))
deg.table <- data.frame(matrix(nrow = length(cone.deg.list)))
deg.table$gene <- cone.deg.list
new.list <- list()
new.list$gene <- deg.table
new.list <- append(new.list, gene.list)
full.table <- join_all(new.list, by = "gene", type = "left")
rownames(full.table) <- full.table$gene
colnames(full.table) <- paste0("V", seq(1, length(full.table)))
full.table <- select(full.table, 3:length(full.table))
colnames(full.table) <- names(gene.list)
colors <- colorRampPalette(rev(brewer.pal(n = 10, name = "RdYlBu")))
full.table[is.na(full.table)] <- 0
cone.genes.deg <- pheatmap(full.table, cluster_rows = F, cluster_cols = FALSE,
breaks = seq(-1.5,1.5, by = 0.05),
color = colors(60), fontsize_row = 5)
cone.genes.deg.clusterd <- pheatmap(full.table, cluster_rows = T, cluster_cols = FALSE,
breaks = seq(-1.5,1.5, by = 0.05),
color = colors(60), fontsize_row = 4)
ggsave("deg_cones_involved_heatmap.png", plot = cone.genes.deg.clusterd, height = 30, width = 10, units = "cm")
write.table(x = full.table, file = "/home/daniel/master_thesis/bassoon_data/Output/Tables_Graphs/deg.cones.heatmap.logfc.csv", sep = ",")
# all genes
gene.list <- lapply(deg, "[", , c(1,3))
all.deg.list <- lapply(gene.list, "[", , 1)
all.deg.list <- unique(unlist(all.deg.list))
deg.table <- data.frame(matrix(nrow = length(all.deg.list)))
deg.table$gene <- all.deg.list
new.list <- list()
new.list$gene <- deg.table
new.list <- append(new.list, gene.list)
full.table <- join_all(new.list, by = "gene", type = "left")
rownames(full.table) <- full.table$gene
colnames(full.table) <- paste0("V", seq(1, length(full.table)))
full.table <- select(full.table, 3:length(full.table))
colnames(full.table) <- names(gene.list)
colors <- colorRampPalette(rev(brewer.pal(n = 10, name = "RdYlBu")))
full.table[is.na(full.table)] <- 0
all.genes.deg <- pheatmap(full.table, cluster_cols = F, cluster_rows = F,
breaks = seq(-1.5,1.5, by = 0.05),
color = colors(60))
all.genes.deg.clustered <- pheatmap(full.table, cluster_cols = F,
cluster_rows = T,
breaks = seq(-1.5,1.5, by = 0.05),
color = colors(60), fontsize_row = 4)
ggsave("deg_all_heatmap.png", plot = all.genes.deg.clustered, height = 60, width = 25, units = "cm")
pdf("pathways_heatmap.pdf")
## Pathway analysis Heatmap --> Only upregulated pathways
up.down <- grepl(names(enriched), pattern = "up|down")
new <- enriched[up.down]
kegg.list <- lapply(new, function(x){
x <- as.data.frame(x$KEGG_2019_Mouse)
x <- dplyr::filter(x, Adjusted.P.value < 0.05)
return(x)
})
path.pval <- lapply(kegg.list, "[", , c(1,4))
pathways <- unique(unlist(lapply(kegg.list, "[", , 1)))
path.pval <- lapply(path.pval, function(x) if (nrow(x) == 0){
x <- as.data.frame(matrix(nrow = length(pathways)))
x$Term <- pathways
x$Adjusted.P.value <- c(0)
x$V1 <- NULL
return(x)}
else {return(x)}
)
path.table <- data.frame(matrix(nrow = length(pathways)))
path.table$Term <- pathways
new.list <- list()
new.list$Term <- path.table
new.list <- append(new.list, path.pval)
full.table <- join_all(new.list, by = "Term", type = "left")
rownames(full.table) <- full.table$Term
colnames(full.table) <- paste0("V", seq(1, length(full.table)))
full.table <- select(full.table, 3:length(full.table))
colnames(full.table) <- names(path.pval)
heat.table <- ifelse(full.table == 0 | is.na(full.table), 0, 1)
pathway.sep.heatmap <- pheatmap(heat.table, cluster_rows = T, cluster_cols = F)
ggsave(plot = pathway.sep.heatmap, file = "kegg_pathway.png")
write.table(x = heat.table, file = "pathways.up.down.heatmap.csv", sep = ",")
#all genes in one
all <- grepl(names(enriched), pattern = "all")
new <- enriched[all]
kegg.list <- lapply(new, function(x){
x <- as.data.frame(x$KEGG_2019_Mouse)
x <- dplyr::filter(x, Adjusted.P.value < 0.05)
return(x)
})
path.pval <- lapply(kegg.list, "[", , c(1,4))
pathways <- unique(unlist(lapply(kegg.list, "[", , 1)))
path.pval <- lapply(path.pval, function(x) if (nrow(x) == 0){
x <- as.data.frame(matrix(nrow = length(pathways)))
x$Term <- pathways
x$Adjusted.P.value <- c(0)
x$V1 <- NULL
return(x)}
else {return(x)}
)
path.table <- data.frame(matrix(nrow = length(pathways)))
path.table$Term <- pathways
new.list <- list()
new.list$Term <- path.table
new.list <- append(new.list, path.pval.test)
full.table <- join_all(new.list, by = "Term", type = "left")
rownames(full.table) <- full.table$Term
colnames(full.table) <- paste0("V", seq(1, length(full.table)))
full.table <- select(full.table, 3:length(full.table))
colnames(full.table) <- names(path.pval)
heat.table <- ifelse(is.na(full.table), 0, 1)
pathway.all.heatmap <- pheatmap(heat.table, cluster_rows = FALSE, cluster_cols = FALSE)
write.table(x = heat.table, file = "pathways.all.heatmap.csv", sep = ",")
dev.off()
pdf("pathway_sep_heatmap.pdf")
pathway.sep.heatmap
dev.off()
pdf("pathway_all_heatmap.pdf")
pathway.all.heatmap
dev.off()
pdf("cone_deg_heatmap.pdf", height = 18)
cone.genes.deg.clusterd
dev.off()
pdf("all_deg_heatmap.pdf", height = 35)
all.genes.deg.clustered
dev.off()
## DEG TABLES WITHOUT COMMON GENES
deg.filtered <- list()
# DE genes in WT1 rod vs cones
table <- deg$wt1.rod.cone
genes <- table$gene
# Filter out genes
# genes <- setdiff(genes, deg$mut.rod.cone$gene)
# genes <- setdiff(genes, deg$wt.mut.rod$gene)
# genes <- setdiff(genes, deg$wt.mut.cone$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["wt1.rod.cone"]] <- subset
###############
# DE genes in WT2 rod vs cones
table <- deg$wt2.rod.cone
genes <- table$gene
# Filter out genes, which are upregulated in wt cones
# genes <- setdiff(genes, deg$ko.rod.cone$gene)
# genes <- setdiff(genes, deg$wt.ko.rod$gene)
# genes <- setdiff(genes, deg$wt.ko.cone$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["wt2.rod.cone"]] <- subset
###############
# DE genes in mut rod vs cones
table <- deg$mut.rod.cone
genes <- table$gene
# Filter out genes, which are upregulated in wt cones
genes <- setdiff(genes, deg$wt1.rod.cone$gene)
# genes <- setdiff(genes, deg$wt.mut.rod$gene)
# genes <- setdiff(genes, deg$wt.mut.cone$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["mut.rod.cone"]] <- subset
###############
# DE genes in ko rod vs cones
table <- deg$ko.rod.cone
genes <- table$gene
# Filter out genes, which are upregulated in wt cones
# genes <- setdiff(genes, deg$wt1.rod.cone$gene)
genes <- setdiff(genes, deg$wt2.rod.cone$gene)
# genes <- setdiff(genes, deg$wt.ko.rod$gene)
# genes <- setdiff(genes, deg$wt.ko.cone$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["ko.rod.cone"]] <- subset
###############
deg.filtered[["wt1.wt2.cone"]] <- deg$wt1.wt2.cone
deg.filtered[["wt1.wt2.rod"]] <- deg$wt1.wt2.rod
# DE genes in wt1 cone vs mut cone
table <- deg$wt.mut.cone
genes <- table$gene
# Filter out genes
# genes <- setdiff(genes, deg$wt.mut.rod$gene)
# genes <- setdiff(genes, deg$mut.rod.cone$gene)
# genes <- setdiff(genes, deg$wt1.rod.cone$gene)
# genes <- setdiff(genes, deg$wt1.wt.2.cone$gene)
# genes <- setdiff(genes, deg$wt2.rod.cone$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["wt1.mut.cone"]] <- subset
################
# DE genes in wt1 rod vs mut rod
table <- deg$wt.mut.rod
genes <- table$gene
# Filter out genes
# genes <- setdiff(genes, deg$wt.mut.cone$gene)
# genes <- setdiff(genes, deg$mut.rod.cone$gene)
# genes <- setdiff(genes, deg$wt1.rod.cone$gene)
# genes <- setdiff(genes, deg$wt1.wt.2.rod$gene)
# genes <- setdiff(genes, deg$wt2.rod.cone$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["wt1.mut.rod"]] <- subset
###############
# DE genes in wt2 vs ko cone
table <- deg$wt.ko.cone
genes <- table$gene
# Filter out genes
# genes <- setdiff(genes, deg$wt1.wt2.cone$gene)
# genes <- setdiff(genes, deg$wt.ko.rod$gene)
# genes <- setdiff(genes, deg$ko.rod.cone$gene)
# genes <- setdiff(genes, deg$wt1.rod.cone$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["wt2.ko.cone"]] <- subset
###############
# DE genes in wt2 vs ko cone
table <- deg$wt.ko.rod
genes <- table$gene
# Filter out genes
# genes <- setdiff(genes, deg$wt1.wt2.rod$gene)
# genes <- setdiff(genes, deg$wt.ko.cone$gene)
# genes <- setdiff(genes, deg$wt2.rod.cone$gene)
# genes <- setdiff(genes, deg$ko.rod.cone$gene)
# genes <- setdiff(genes, deg$wt1.rod.cone$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["wt2.ko.rod"]] <- subset
###############
# DE genes in mut cones to ko cones
table <- deg$mut.ko.cone
genes <- table$gene
# Filter out genes
genes <- setdiff(genes, deg$wt1.wt2.cone$gene)
# genes <- setdiff(genes, deg$wt.mut.rod$gene)
# genes <- setdiff(genes, deg$wt.ko.rod$gene)
# genes <- setdiff(genes, deg$wt.mut.cone$gene)
# genes <- setdiff(genes, deg$wt.ko.cone$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["mut.ko.cone"]] <- subset
###############
# DE genes in mut rods to ko rods
table <- deg$mut.ko.rod
genes <- table$gene
# Filter out genes
genes <- setdiff(genes, deg$wt1.wt2.rod$gene)
# genes <- setdiff(genes, deg$wt.mut.cone$gene)
# genes <- setdiff(genes, deg$wt.ko.cone$gene)
# genes <- setdiff(genes, deg$wt.mut.rod$gene)
# genes <- setdiff(genes, deg$wt.ko.rod$gene)
subset <- subset(table, gene %in% genes)
deg.filtered[["mut.ko.rod"]] <- subset
### Generate raw heatmap
gene.list <- lapply(deg, "[", , c(1,3))
all.deg.list <- lapply(gene.list, "[", , 1)
all.deg.list <- unique(unlist(all.deg.list))
deg.table <- data.frame(matrix(nrow = length(all.deg.list)))
deg.table$gene <- all.deg.list
new.list <- list()
new.list$gene <- deg.table
new.list <- append(new.list, gene.list)
full.table <- join_all(new.list, by = "gene", type = "left")
rownames(full.table) <- full.table$gene
colnames(full.table) <- paste0("V", seq(1, length(full.table)))
full.table <- select(full.table, 3:length(full.table))
colnames(full.table) <- names(gene.list)
full.table[is.na(full.table)] <- 0
colors <- colorRampPalette(rev(brewer.pal(n = 10, name = "RdYlBu")))
all.deg.filtered <- pheatmap(full.table, cluster_cols = F,
cluster_rows = T,
breaks = seq(-1.5,1.5, by = 0.05),
color = colors(60), fontsize_row = 5)
mut.and.ko <- full.table %>% select(wt1.wt2.cone, wt.mut.cone, wt.ko.cone, mut.ko.cone)
mut.and.ko <- mut.and.ko[rowSums(mut.and.ko) != 0, ]
genes <- rownames(mut.and.ko)
keep <- c("Ubb", "Opn1sw", "Pde6c", "Slc12a5")
genes[which(!genes %in% keep)] <- ""
mut.and.ko.heat <- pheatmap(mut.and.ko, cluster_cols = F,
cluster_rows = T,
breaks = seq(-1.5,1.5, by = 0.05),
color = colors(60), fontsize_row = 10,
fontsize_col = 8, angle_col = 0,
labels_row = genes,
labels_col = c("B6J WT cones vs. B6N WT cones", "WT cones vs. mutant cones", "WT cones vs. knockout cones", "mutant cones vs. knockout cones"))
ggsave(plot = mut.and.ko.heat, filename = "/home/daniel/master_thesis/bassoon_data/Output/Tables_Graphs/mut_ko_cone_heatmap.png",
width = 20, units = "cm")
# rod.cone <- full.table %>% select(wt1.rod.cone, wt2.rod.cone, mut.rod.cone, ko.rod.cone)
rod.cone <- full.table %>% select(mut.rod.cone, ko.rod.cone)
rod.cone <- rod.cone[rowSums(rod.cone) != 0, ]
col <- colorRampPalette(rev(brewer.pal(n = 6, name = "RdYlGn")))
col <- rev(col(40))
col[20] <- "#F2F4F3"
rod.cone.heat <- pheatmap(rod.cone, cluster_cols = F,
cluster_rows = T,
breaks = seq(-0.5,0.5, by = 0.025),
color = col, fontsize_row = 5, border_color = NA,
labels_col = c("Bsn mutant rods vs. cones", "Bsn knockout rods vs cones"),
angle_col = 0)
ggsave(plot = rod.cone.heat, filename = "/home/daniel/master_thesis/bassoon_data/Output/Tables_Graphs/rod_cone_heatmap.png")
## cone-cone comparison
cone <- full.table %>% select(wt1.wt2.cone, wt.mut.cone, wt.ko.cone, mut.ko.cone)
cone <- rod.cone[rowSums(rod.cone) != 0, ]
col <- colorRampPalette(rev(brewer.pal(n = 6, name = "RdYlGn")))
col <- rev(col(60))
col[30] <- "#F2F4F3"
genes <- rownames(mut.and.ko)
keep <- c("Opn1sw", "Pde6c", "Slc12a5",
"Gngt1", "Gngt2", "mt-Atp6",
"Rho", "mt-Co3", "Scl6a6")
genes[which(!genes %in% keep)] <- ""
rod.cone.heat <- pheatmap(rod.cone, cluster_cols = F,
cluster_rows = T,
breaks = seq(-1.5,1.5, by = 0.05),
color = col, fontsize_row = 7, border_color = NA,
labels_row = genes,
fontsize_col = 10,
labels_col = c("B6J WT vs. B6N WT", "B6J WT vs. Bsn mut", "B6N WT vs. Bsn ko", "Bsn mut vs. Bsn ko"),
angle_col = 0)
mut.ko.rod <- full.table %>% select(wt1.mut.rod, wt2.ko.rod, mut.ko.rod)
mut.ko.rod <- mut.ko.rod[rowSums(mut.ko.rod) != 0, ]
mut.ko.rod.heat <- pheatmap(mut.ko.rod, cluster_cols = F,
cluster_rows = T,
breaks = seq(-1.5,1.5, by = 0.05),
color = colors(60), fontsize_row = 6)
ggsave(plot = mut.ko.rod.heat, filename = "/home/daniel/master_thesis/bassoon_data/Output/Tables_Graphs/mut_ko_rod_heatmap.png",
height = 40, width = 20, units = "cm")
######################
# databases <- c("GO_Biological_Process_2018")
databases <- c("KEGG_2019_Mouse")
enriched.filt <- list()
deg.filtered.sep <- list()
i <- 1
names <- names(enriched)
for (table in deg.filtered){
name <- names(deg.filtered)[i]
table$expr <- ifelse(table$avg_logFC > 0, "up", "down")
up <- dplyr::filter(table, expr == "up")
down <- dplyr::filter(table, expr == "down")
deg.filtered.sep[[paste(name, "up", sep = ".")]] <- up
deg.filtered.sep[[paste(name, "down", sep = ".")]] <- down
enriched.filt.all <- enrichr(table$gene, databases)
enriched.filt[[paste(name, "all", sep = ".")]] <- enriched.filt.all
enriched.filt.up <- enrichr(up$gene, databases)
enriched.filt[[paste(name, "up", sep = ".")]] <- enriched.filt.up
enriched.filt.down <- enrichr(down$gene, databases)
enriched.filt[[paste(name, "down", sep = ".")]] <- enriched.filt.down
i <- i + 1
}
up.down <- grepl(names(enriched.filt), pattern = "up|down")
new <- enriched.filt[up.down]
kegg.list <- lapply(new, function(x){
# x <- as.data.frame(x$GO_Biological_Process_2018)
x <- as.data.frame(x$KEGG_2019_Mouse)
x <- dplyr::filter(x, Adjusted.P.value < 0.2)
return(x)
})
path.pval <- lapply(kegg.list, "[", , c(1,4))
pathways <- unique(unlist(lapply(kegg.list, "[", , 1)))
path.pval <- lapply(path.pval, function(x) if (nrow(x) == 0){
x <- as.data.frame(matrix(nrow = length(pathways)))
x$Term <- pathways
x$Adjusted.P.value <- c(0)
x$V1 <- NULL
return(x)}
else {return(x)}
)
path.table <- data.frame(matrix(nrow = length(pathways)))
path.table$Term <- pathways
new.list <- list()
new.list$Term <- path.table
new.list <- append(new.list, path.pval)
full.table <- join_all(new.list, by = "Term", type = "left")
rownames(full.table) <- full.table$Term
colnames(full.table) <- paste0("V", seq(1, length(full.table)))
full.table <- select(full.table, 3:length(full.table))
colnames(full.table) <- names(path.pval)
heat.table <- ifelse(full.table == 0 | is.na(full.table), 0, 1)
### Select != genetic background
heat.table <- dplyr::select(as.data.frame(heat.table), -wt1.rod.cone.up, -wt1.rod.cone.down, -wt2.rod.cone.down,
-wt1.wt2.cone.up, -wt1.wt2.cone.down, -wt1.wt2.rod.up, -wt1.wt2.rod.down)
pathway.sep.heatmap <- pheatmap(heat.table, cluster_rows = T, cluster_cols = F, fontsize_row = 6)
ggsave(plot = pathway.sep.heatmap, file = "go_term_heatmap.png", height = 20, units = "cm")
#############################
dbs <- "KEGG_2019_Mouse"
enr <- enrichr(deg$rod.cone.wt.1$gene, dbs)
enrichr(setdiff(deg.sep$cone.wt.down$gene, deg.sep$wt.mut.cone.down$gene), dbs)
enriched$rod.cone.wt.1.down$KEGG_2019_Mouse
enriched$rod.cone.mut.down$KEGG_2019_Mouse
enriched$wt.mut.cone.up$KEGG_2019_Mouse
enriched$wt.mut.cone.down$KEGG_2019_Mouse
write("", "enriched.csv")
i <- 1
length(names(enriched))
for (comp in enriched){
name <- names(enriched)[i]
j <- 1
print(name)
for (table in comp){
comp.name <- names(comp)[j]
table <- table %>% filter(Adjusted.P.value < 0.05)
write(name, "enriched.csv", append = T)
write(comp.name, "enriched.csv", append = T)
write.table(table, "enriched.csv", append = T, sep = "\t", row.names = F)
write("", "enriched.csv", append = T)
j <- j + 1
}
i <- i + 1
}
marker.kegg <- inner_join(kegg.ids, markers, by = "gene")
path <- c()
for (id in marker.kegg$PATH){
if (!is.na(id)){
path <- c(path, KEGG[[as.character(id)]])
}
else
path <- c(path, NA)
}
marker.kegg$PATHNAME <- path
table(marker.kegg$PATHNAME, marker.kegg$regulation)
all.sample.ct <- levels(Idents(Bsn.all))
# Generate Heatmap with DE genes -- separated up and downregulated
gene.list <- lapply(deg.sep, "[", , c(1,3))
cone.deg.list <- lapply(gene.list, "[", , 1)
cone.deg.list <- unique(unlist(cone.deg.list[grepl(names(cone.deg.list), pattern = "cone")], use.names = F))
deg.table <- data.frame(matrix(nrow = length(cone.deg.list)))
deg.table$gene <- cone.deg.list
new.list <- list()
new.list$gene <- deg.table
new.list <- append(new.list, gene.list)
full.table <- join_all(new.list, by = "gene", type = "left")
rownames(full.table) <- full.table$gene
colnames(full.table) <- paste0("V", seq(1, length(full.table)))
full.table <- select(full.table, 3:length(full.table))
colnames(full.table) <- names(gene.list)
heat.table <- ifelse(is.na(full.table), 0, 1)
pheatmap(heat.table, cluster_rows = T, cluster_cols = F,
fontsize_row = 6)
write.table(x = heat.table, file = "deg.up.down.heatmap.csv", sep = ",")
dev.off()
m <- rownames(markers)
### Get interesting genes Bsn mutant rods vs cones
intersect(deg.sep$wt1.wt2.rod.up$gene, deg.sep$mut.ko.rod.down$gene)
intersect(deg.sep$wt1.wt2.rod.down$gene, deg.sep$mut.ko.rod.up$gene)
rownames(full.table) <- full.table$Term