-
Notifications
You must be signed in to change notification settings - Fork 0
/
DEGSeq2_MV_Only.R
2507 lines (1975 loc) · 113 KB
/
DEGSeq2_MV_Only.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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
################# CIBERSORTx cell type quantification ##########################
# load libraries in R
## @knitr Load_libraries
setwd("/media/owen/Backup Plus/UCSF_Project/02_PROCESSED_DATA/PNOC/ANALYSIS/Bulk_RNA/01_Input_Data/Mouse/")
library(MASS)
library(reshape2)
library("FactoMineR")
library("factoextra")
library(heatmaply)
library(ggpubr)
library(DESeq2)
library(glmGamPoi)
library(EnhancedVolcano)
library(BiocParallel)
register(MulticoreParam(12))
library(ggplot2)
library(scales) # needed for oob parameter
library(viridis)
library("tibble")
library(reshape2)
library(ggdendro)
library(gridExtra)
library(gtable)
library(grid)
library(clusterProfiler)
library(enrichplot)
require(DOSE)
library(europepmc)
library(pathview)
library(survival)
library(survminer)
require("survival")
library("forestmodel")
library(devtools)
#install_github("KatrionaGoldmann/volcano3D", force = TRUE)
library(volcano3D)
#install_github("KatrionaGoldmann/volcano3Ddata")
library(volcano3Ddata)
library(volcano3D)
library(kableExtra)
library("ggrepel")
library("plotly")
library("dplyr")
library(knitr)
library(htmltools)
library(htmlTable)
library(htmlwidgets)
library( digest)
library( DT)
library( fs)
library(future)
library(ggrepel)
library(ggtree)
library(parallel)
library(tibble)
library(data.table)
library(speckle)
library(rstatix)
plan("multicore", workers = 12)
options(future.globals.maxSize = (1014*1024^2)*60)
## @knitr loadData
CIBERSORTx_SingleR_matrix <- read.csv("CIBERSORTx_Corrected_CellTypes_Martrix.csv", header = T, row.names = 1)
CIBERSORTx_SingleR_matrix <- t(CIBERSORTx_SingleR_matrix)
CIBERSORTx_SingleR_matrix <- as.data.frame(CIBERSORTx_SingleR_matrix)
CIBERSORTx_SingleR_matrix <- rownames_to_column(CIBERSORTx_SingleR_matrix, var = "CellType")
## @knitr CIBERSORTx_PCA
iris.pca <- PCA(CIBERSORTx_SingleR_matrix[,-1], graph = FALSE)
fviz_pca_ind(iris.pca, geom.ind = "point", pointshape = 21,
pointsize = 2,
fill.ind = CIBERSORTx_SingleR_matrix$CellType,
col.ind = "black",
palette = c("#0062B4FF", "#FFBF47", "#FF2700FF", "mediumseagreen", "sienna4", "violet", "violetred", "violetred4", "#CC313D", "blue", "limegreen", "#3B9AB2","deeppink", "#BF812D"),
addEllipses = TRUE,
label = "var",
col.var = "black",
repel = TRUE,
legend.title = "Cell Types") +
ggtitle("2D PCA-plot from CIBERSORTx Signature Matrix") +
theme(plot.title = element_text(hjust = 0.5))
CIBERSORTx_SingleR_matrix <- read.csv("CIBERSORTx_Corrected_CellTypes_Martrix.csv", header = T, row.names = 1)
## @knitr CIBERSORTx_Heatmap
data <- CIBERSORTx_SingleR_matrix
data <- as.matrix(data)
library("RColorBrewer")
cellType = c(Malignant = "#CC313D", Neurons = "#3B9AB2", Endothelial = "mediumseagreen", Monocytes = "limegreen", Microglia = "blue", M2_Macrophages = "violetred4", M1_Macrophages = "violetred", M0_Macrophages = "violet", CD4 = "#FF2700FF", B = "#FFBF47", Astrocytes = "#0062B4FF", Oligodendrocytes = "#BF812D", NK_cells = "deeppink", Epithelial = "sienna4")
col<- colorRampPalette(c("cyan", "black", "red"))(256)
heatmap(data, ColSideColors = cellType, col = col)
## @knitr CIBERSORTx_Correlation_Heatmap
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2
)
## @knitr CIBERSORTx_Correlation_Heatmap_pvalues
r <- cor(data)
cor.test.p <- function(x){
FUN <- function(x, y) cor.test(x, y)[["p.value"]]
z <- outer(
colnames(x),
colnames(x),
Vectorize(function(i,j) FUN(x[,i], x[,j]))
)
dimnames(z) <- list(colnames(x), colnames(x))
z
}
p <- cor.test.p(data)
heatmaply_cor(
r,
node_type = "scatter",
point_size_mat = -log10(p),
point_size_name = "-log10(p-value)",
label_names = c("x", "y", "Correlation"))
## @knitr CIBERSORTx_cell_fraction_results
CIBERSORTx_Results_SingleR <- read.csv("CIBERSORTx_Corrected_CellTypes_Results_website.csv", header = T, row.names = 1)
CIBERSORTx_Results_SingleR <- round(CIBERSORTx_Results_SingleR, digits = 2)
heatmaply(CIBERSORTx_Results_SingleR,
cellnote = CIBERSORTx_Results_SingleR
)
theme_set(
theme_minimal() +
theme(legend.position = "right")
)
## @knitr CIBERSORTx_cell_fraction_barplot
CIBERSORTx_Results_SingleR <- read.csv("CIBERSORTx_Corrected_CellTypes_Results_t_test_MV.csv", header = T)
melt_data_propeller <- melt(CIBERSORTx_Results_SingleR, id=c("Mixture","Viral_Status"))
ggplot(melt_data_propeller, mapping = aes(x = Mixture, y = value, fill = variable)) +
geom_bar(position= "stack", stat = "identity") +
scale_fill_manual(labels = c("Malignant", "Neurons", "Endothelial", "Monocytes", "Microglia", "M2_Macrophages", "M1_Macrophages", "M0_Macrophages", "CD4", "B", "Astrocytes", "Oligodendrocytes", "NK_cells", "Epithelial"), values =c( "#CC313D", "#3B9AB2", "mediumseagreen", "limegreen", "blue", "violetred4", "violetred", "violet", "#FF2700FF", "#FFBF47", "#0062B4FF", "#BF812D", "deeppink", "sienna4")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
## @knitr CIBERSORTx_cell_fraction_boxplot
df <- read.csv("CIBERSORTx_Corrected_CellTypes_Results_t_test_MV.csv", header = T)
df$Viral_Status <- factor(df$Viral_Status, levels = c("Control_MV", "HI_MV_NIS", "Mid_treatment_MV", "Post_treatment_MV"))
# Visualize: Specify the comparisons you want
my_comparisons = list(c("Control_MV", "HI_MV_NIS"), c("Control_MV", "Mid_treatment_MV"), c("Control_MV", "Post_treatment_MV"), c("Mid_treatment_MV", "Post_treatment_MV"))
p1 = ggboxplot(df, x = "Viral_Status", y = c("M0_Macrophages", "M1_Macrophages", "M2_Macrophages"), fill = "Viral_Status", legend = "none", combine = TRUE,
bxp.errorbar = TRUE,
bxp.errorbar.width = 0.4, palette = c("#00AFBB", "#E7B800",
"green", "red", "navy", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7",
"#52854C", "#4E84C4", "purple", "black", "blue"),ggtheme = theme_bw())+
rotate_x_text(angle = 45)+
stat_compare_means(comparisons = my_comparisons, paired = FALSE, method = "t.test")+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 0.05) +
ggtitle("CIBERSORTx Cell Type Comparison") + theme(
plot.title = element_text(color="black", size=14),
axis.title.x = element_text(color="black", size=14),
axis.title.y = element_text(color="black", size=14)
)
p1
df$Viral_Status <- factor(df$Viral_Status, levels = c("Control_MV", "HI_MV_NIS", "Mid_treatment_MV", "Post_treatment_MV"))
# Visualize: Specify the comparisons you want
my_comparisons = list(c("Control_MV", "HI_MV_NIS"), c("Control_MV", "Mid_treatment_MV"), c("Control_MV", "Post_treatment_MV"), c("Mid_treatment_MV", "Post_treatment_MV"))
p2 = ggboxplot(df, x = "Viral_Status", y = c("NK_cells", "CD4"), fill = "Viral_Status", legend = "none", combine = TRUE,
bxp.errorbar = TRUE,
bxp.errorbar.width = 0.4, palette = c("#00AFBB", "#E7B800",
"green", "red", "navy", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7",
"#52854C", "#4E84C4", "purple", "black", "blue"),ggtheme = theme_bw())+
rotate_x_text(angle = 45)+
stat_compare_means(comparisons = my_comparisons, paired = FALSE, method = "t.test")+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 0.05) +
ggtitle("CIBERSORTx Cell Type Comparison") + theme(
plot.title = element_text(color="black", size=14),
axis.title.x = element_text(color="black", size=14),
axis.title.y = element_text(color="black", size=14)
)
p2
df$Viral_Status <- factor(df$Viral_Status, levels = c("Control_MV", "HI_MV_NIS", "Mid_treatment_MV", "Post_treatment_MV"))
# Visualize: Specify the comparisons you want
my_comparisons = list(c("Control_MV", "HI_MV_NIS"), c("Control_MV", "Mid_treatment_MV"), c("Control_MV", "Post_treatment_MV"), c("Mid_treatment_MV", "Post_treatment_MV"))
p3 = ggboxplot(df, x = "Viral_Status", y = c("Monocytes", "Microglia", "B"), fill = "Viral_Status", legend = "none", combine = TRUE,
bxp.errorbar = TRUE,
bxp.errorbar.width = 0.4, palette = c("#00AFBB", "#E7B800",
"green", "red", "navy", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7",
"#52854C", "#4E84C4", "purple", "black", "blue"),ggtheme = theme_bw())+
rotate_x_text(angle = 45)+
stat_compare_means(comparisons = my_comparisons, paired = FALSE, method = "t.test")+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 0.1) +
ggtitle("CIBERSORTx Cell Type Comparison") + theme(
plot.title = element_text(color="black", size=14),
axis.title.x = element_text(color="black", size=14),
axis.title.y = element_text(color="black", size=14)
)
p3
df$Viral_Status <- factor(df$Viral_Status, levels = c("Control_MV", "HI_MV_NIS", "Mid_treatment_MV", "Post_treatment_MV"))
# Visualize: Specify the comparisons you want
my_comparisons = list(c("Control_MV", "HI_MV_NIS"), c("Control_MV", "Mid_treatment_MV"), c("Control_MV", "Post_treatment_MV"), c("Mid_treatment_MV", "Post_treatment_MV"))
p4 = ggboxplot(df, x = "Viral_Status", y = c("Malignant", "Neurons"),fill = "Viral_Status", legend = "none", combine = TRUE,
bxp.errorbar = TRUE,
bxp.errorbar.width = 0.4, palette = c("#00AFBB", "#E7B800",
"green", "red", "navy", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7",
"#52854C", "#4E84C4", "purple", "black", "blue"),ggtheme = theme_bw())+
rotate_x_text(angle = 45)+
stat_compare_means(comparisons = my_comparisons, paired = FALSE, method = "t.test")+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 1.2) +
ggtitle("CIBERSORTx Cell Type Comparison") + theme(
plot.title = element_text(color="black", size=14),
axis.title.x = element_text(color="black", size=14),
axis.title.y = element_text(color="black", size=14)
)
p4
df$Viral_Status <- factor(df$Viral_Status, levels = c("Control_MV", "HI_MV_NIS", "Mid_treatment_MV", "Post_treatment_MV"))
# Visualize: Specify the comparisons you want
my_comparisons = list(c("Control_MV", "HI_MV_NIS"), c("Control_MV", "Mid_treatment_MV"), c("Control_MV", "Post_treatment_MV"), c("Mid_treatment_MV", "Post_treatment_MV"))
p5 = ggboxplot(df, x = "Viral_Status", y = c("Endothelial", "Astrocytes", "Oligodendrocytes"), fill = "Viral_Status", legend = "none", combine = TRUE,
bxp.errorbar = TRUE,
bxp.errorbar.width = 0.4, palette = c("#00AFBB", "#E7B800",
"green", "red", "navy", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7",
"#52854C", "#4E84C4", "purple", "black", "blue"),ggtheme = theme_bw())+
rotate_x_text(angle = 45)+
stat_compare_means(comparisons = my_comparisons, paired = FALSE, method = "t.test")+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 0.10) +
ggtitle("CIBERSORTx Cell Type Comparison") + theme(
plot.title = element_text(color="black", size=14),
axis.title.x = element_text(color="black", size=14),
axis.title.y = element_text(color="black", size=14)
)
p5
ggarrange(p1, p2, p3, p4, p5, nrow = 5, common.legend = FALSE)
# stat.test <- melt_data_propeller %>%
# group_by(Viral_Status) %>%
# tukey_hsd(value ~ variable)
# propeller_test_SingleR <-propeller(clusters = melt_data_propeller$variable,
# sample = melt_data_propeller$Mixture,
# group = melt_data_propeller$Viral_Status)
# propeller_test_SingleR <- as.data.frame(propeller_test_SingleR)
# propeller_test_SingleR <- rownames_to_column(propeller_test_SingleR, var = "cellType")
#write.csv(stat.test, file="tukey_hsd_SingleR.csv")
########################################################################################################################################################
############################ Now let's take a look at the viral composition ############################################################################
#setwd("/media/owen/Backup Plus/UCSF_Project/02_PROCESSED_DATA/PNOC/ANALYSIS/Bulk_RNA/05_Output/Mouse/Virus/")
CIBERSORTx_Virus_matrix <- read.table("CIBERSORTx_Virus_Matrix_Normalized_Results.txt", header = T, sep = "\t", row.names = 1)
CIBERSORTx_Virus_matrix <- t(CIBERSORTx_Virus_matrix)
CIBERSORTx_Virus_matrix <- as.data.frame(CIBERSORTx_Virus_matrix)
CIBERSORTx_Virus_matrix <- rownames_to_column(CIBERSORTx_Virus_matrix, var = "CellType")
## @knitr CIBERSORTx_PCA2
res.pca <- prcomp(CIBERSORTx_Virus_matrix[,-1], scale = TRUE)
Viral_Status <- as.factor(CIBERSORTx_Virus_matrix$CellType)
fviz_pca_ind(res.pca, geom.ind = "point",
pointsize = 5,
col.ind = Viral_Status, # color by groups
palette = c("#00AFBB", "#FC4E07"),
addEllipses = TRUE, # Concentration ellipses
legend.title = "Viral_Status",
repel = TRUE
)
CIBERSORTx_Virus_matrix <- read.table("CIBERSORTx_Virus_Matrix_Normalized_Results.txt", header = T, sep = "\t", row.names = 1)
## @knitr CIBERSORTx_Heatmap2
data <- CIBERSORTx_Virus_matrix
data <- as.matrix(data)
heatmap(data, cexRow = 1, cexCol = 1,
main = "CIBERSORTx Virus Matrix")
## @knitr CIBERSORTx_Correlation_Heatmap2
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2
)
## @knitr CIBERSORTx_Correlation_Heatmap_pvalues2
r <- cor(data)
cor.test.p <- function(x){
FUN <- function(x, y) cor.test(x, y)[["p.value"]]
z <- outer(
colnames(x),
colnames(x),
Vectorize(function(i,j) FUN(x[,i], x[,j]))
)
dimnames(z) <- list(colnames(x), colnames(x))
z
}
p <- cor.test.p(data)
heatmaply_cor(
r,
node_type = "scatter",
point_size_mat = -log10(p),
point_size_name = "-log10(p-value)",
label_names = c("x", "y", "Correlation")
)
## @knitr CIBERSORTx_cell_fraction_results2
CIBERSORTx_Results_Virus <- read.csv("CIBERSORTx_Virus_SMode_Normalized_Results_website.csv", header = T, row.names = 1)
heatmaply(
CIBERSORTx_Results_Virus,
cellnote = CIBERSORTx_Results_Virus
)
theme_set(
theme_minimal() +
theme(legend.position = "right")
)
## @knitr CIBERSORTx_cell_fraction_barplot2
CIBERSORTx_Results_Virus <- read.csv("CIBERSORTx_Virus_SMode_Normalized_Results_t_test.csv", header = T)
melt_data <- melt(CIBERSORTx_Results_Virus, id=c("Mixture","Viral_Status"))
ggplot(melt_data, mapping = aes(x = Mixture, y = value, fill = variable)) +
geom_bar(position= "stack", stat = "identity") +
scale_fill_manual(labels = c("Measles_virus_infected", "Non_Infected"), values = c( "#E7B800", "#00AFBB")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
## @knitr CIBERSORTx_cell_fraction_boxplot2
df <- read.csv("CIBERSORTx_Virus_SMode_Normalized_Results_t_test.csv", header = T)
df$Viral_Status <- factor(df$Viral_Status, levels = c("Control_MV", "HI_MV_NIS", "Mid_treatment_MV", "Post_treatment_MV", "Control_Toca", "HI_Toca", "Mid_treatment_Toca", "Post_treatment_Toca"))
# Visualize: Specify the comparisons you want
my_comparisons = list(c("Control_MV", "HI_MV_NIS"), c("Control_MV", "Mid_treatment_MV"), c("Control_MV", "Post_treatment_MV"), c("Mid_treatment_MV", "Post_treatment_MV"), c("Control_Toca", "HI_Toca"), c("Control_Toca", "Mid_treatment_Toca"), c("Control_Toca", "Post_treatment_Toca"), c("Mid_treatment_Toca", "Post_treatment_Toca"))
p6 = ggboxplot(df, x = "Viral_Status", y = c("Measles_virus_infected", "Non_Infected"), add = "jitter", legend = "none", combine = TRUE,
color = "Viral_Status", palette = c("#00AFBB", "#E7B800",
"green", "red", "navy", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7",
"#52854C", "#4E84C4", "purple", "black", "blue"),ggtheme = theme_bw())+
rotate_x_text(angle = 45)+
stat_compare_means(comparisons = my_comparisons, paired = FALSE, method = "t.test")+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 0.5) +
ggtitle("CIBERSORTx Proportion of Infected Cells Comparison") + theme(
plot.title = element_text(color="black", size=14),
axis.title.x = element_text(color="black", size=14),
axis.title.y = element_text(color="black", size=14)
)
p6
###############################################################################
################# DEGSeq2 analysis of viral infections ##########################
## @knitr PrepareData
#setwd("/media/owen/Backup Plus/UCSF_Project/02_PROCESSED_DATA/PNOC/ANALYSIS/Bulk_RNA/01_Input_Data/Mouse")
# Read in the raw read counts
rawCounts <- read.csv("Mouse_Bulk_RNA_seq_Raw_Reads.csv")
#head(rawCounts)
# Read in the sample mappings
sampleData <- read.csv("Meta_Data_SingleR.csv")
#head(sampleData)
# Also save a copy for later
sampleData_v2 <- sampleData
# Convert count data to a matrix of appropriate form that DEseq2 can read
geneID <- rawCounts$gene_name
sampleIndex <- grepl("X\\d+", colnames(rawCounts))
rawCounts <- as.matrix(rawCounts[,sampleIndex])
rownames(rawCounts) <- geneID
#head(rawCounts)
# Convert sample variable mappings to an appropriate form that DESeq2 can read
#head(sampleData)
rownames(sampleData) <- sampleData$Sample_ID
keep <- c("Sample_Number", "Viral_Status")
sampleData <- sampleData[,keep]
colnames(sampleData) <- c("Sample", "Virus")
sampleData$Virus <- factor(sampleData$Virus)
#head(sampleData)
# Put the columns of the count data in the same order as rows names of the sample mapping, then make sure it worked
rawCounts <- rawCounts[,unique(rownames(sampleData))]
all(colnames(rawCounts) == rownames(sampleData))
# rename the tissue types
Virus_Infection <- function(x){
x <- switch(as.character(x), "Control_MV"="Control MV", "Control_Toca"="Control Toca", "HI_MV_NIS"="Heat Inactivated MV", "HI_Toca"="Heat Inactivated Toca", "Mid_treatment_MV"="Mid treatment MV",
"Mid_treatment_Toca"="Mid treatment Toca", "Post_treatment_MV"="Post treatment MV", "Post_treatment_Toca"="Post treatment Toca")
return(x)
}
sampleData$Virus <- unlist(lapply(sampleData$Virus, Virus_Infection))
# Order the tissue types so that it is sensible and make sure the control sample is first: normal sample -> primary tumor -> metastatic tumor
sampleData$Virus <- factor(sampleData$Virus, levels=c("Control MV", "Control Toca", "Heat Inactivated MV", "Heat Inactivated Toca", "Mid treatment MV",
"Mid treatment Toca", "Post treatment MV", "Post treatment Toca"))
# Create the DEseq2DataSet object
deseq2Data <- DESeqDataSetFromMatrix(countData=rawCounts, colData=sampleData, design= ~ Sample + Virus)
dim(deseq2Data)
dim(deseq2Data[rowSums(counts(deseq2Data)) > 5, ])
# Perform pre-filtering of the data
deseq2Data <- deseq2Data[rowSums(counts(deseq2Data)) > 5, ]
# Register the number of cores to use
## @knitr Run_DESeq2
# 1. Run pipeline for differential expression steps (if you set up parallel processing, set parallel = TRUE here)
deseq2Data <- DESeq(deseq2Data, test="LRT", reduced=~1, parallel = TRUE)
# Pull out counts from DESeq object and normalize using the same method to perform differential gene expression
# dds <- estimateSizeFactors(deseq2Data)
# dds <- counts(dds, normalized=TRUE)
# Extract differential expression results
CvMT <- results(deseq2Data, contrast=c("Virus", "Post treatment MV", "Control MV"))
# Coerce to a data frame
CvMT <- as.data.frame(CvMT)
CvMT <- rownames_to_column(CvMT, var = "gene")
CvMT$significant <- ifelse(CvMT$padj < .05, "Significant", NA)
CvMT<-na.omit(CvMT)
write.csv(CvMT, file= "deseq2_control_vs_MV_PT.csv")
cat('\n', '<br>', '\n\n')
## @knitr Print_Tables
# Print the datatable with all markers
print( htmltools::tagList(DT::renderDataTable(CvMT, server = TRUE,
class = "compact",
filter="top",
rownames = FALSE,
colnames = c("gene", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj", "significant"),
extensions = c('Buttons'),
options = list(
pageLength = 15,
dom = 'Bfrtip',
buttons = c('excel','csv','pdf','copy')
))))
## @knitr Contrast1
# Extract differential expression results
CvMHI <- results(deseq2Data, contrast=c("Virus", "Heat Inactivated MV", "Control MV",))
# Coerce to a data frame
CvMHI <- as.data.frame(CvMHI)
CvMHI <- rownames_to_column(CvMHI, var = "gene")
CvMHI$significant <- ifelse(CvMHI$padj < .05, "Significant", NA)
CvMHI<-na.omit(CvMHI)
## @knitr Print_Tables1
# Print the datatable with all markers
print( htmltools::tagList(DT::renderDataTable(CvMHI, server = TRUE,
class = "compact",
filter="top",
rownames = FALSE,
colnames = c("gene", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj", "significant"),
extensions = c('Buttons'),
options = list(
pageLength = 15,
dom = 'Bfrtip',
buttons = c('excel','csv','pdf','copy')
))))
## @knitr Contrast2
# Extract differential expression results
CvM_MT <- results(deseq2Data, contrast=c("Virus", "Mid treatment MV", "Control MV"))
# Coerce to a data frame
CvM_MT <- as.data.frame(CvM_MT)
CvM_MT <- rownames_to_column(CvM_MT, var = "gene")
CvM_MT$significant <- ifelse(CvM_MT$padj < .05, "Significant", NA)
CvM_MT<-na.omit(CvM_MT)
## @knitr Print_Tables2
# Print the datatable with all markers
print( htmltools::tagList(DT::renderDataTable(CvM_MT, server = TRUE,
class = "compact",
filter="top",
rownames = FALSE,
colnames = c("gene", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj", "significant"),
extensions = c('Buttons'),
options = list(
pageLength = 15,
dom = 'Bfrtip',
buttons = c('excel','csv','pdf','copy')
))))
## @knitr Contrast3
# Extract differential expression results
CvPTT <- results(deseq2Data, contrast=c("Virus", "Post treatment Toca", "Control Toca"))
# Coerce to a data frame
CvPTT <- as.data.frame(CvPTT)
CvPTT <- rownames_to_column(CvPTT, var = "gene")
CvPTT$significant <- ifelse(CvPTT$padj < .05, "Significant", NA)
CvPTT<-na.omit(CvPTT)
## @knitr Print_Tables3
# Print the datatable with all markers
print( htmltools::tagList(DT::renderDataTable(CvPTT, server = TRUE,
class = "compact",
filter="top",
rownames = FALSE,
colnames = c("gene", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj", "significant"),
extensions = c('Buttons'),
options = list(
pageLength = 15,
dom = 'Bfrtip',
buttons = c('excel','csv','pdf','copy')
))))
## @knitr Contrast4
# Extract differential expression results
CvHIT <- results(deseq2Data, contrast=c("Virus", "Heat Inactivated Toca", "Control Toca"))
# Coerce to a data frame
CvHIT <- as.data.frame(CvHIT)
CvHIT <- rownames_to_column(CvHIT, var = "gene")
CvHIT$significant <- ifelse(CvHIT$padj < .05, "Significant", NA)
CvHIT<-na.omit(CvHIT)
## @knitr Print_Tables4
# Print the datatable with all markers
print( htmltools::tagList(DT::renderDataTable(CvHIT, server = TRUE,
class = "compact",
filter="top",
rownames = FALSE,
colnames = c("gene", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj", "significant"),
extensions = c('Buttons'),
options = list(
pageLength = 15,
dom = 'Bfrtip',
buttons = c('excel','csv','pdf','copy')
))))
## @knitr Contrast5
# Extract differential expression results
CvMTT <- results(deseq2Data, contrast=c("Virus", "Mid treatment Toca", "Control Toca"))
# Coerce to a data frame
CvMTT <- as.data.frame(CvMTT)
CvMTT <- rownames_to_column(CvMTT, var = "gene")
CvMTT$significant <- ifelse(CvMTT$padj < .05, "Significant", NA)
CvMTT<-na.omit(CvMTT)
## @knitr Print_Tables5
# Print the datatable with all markers
print( htmltools::tagList(DT::renderDataTable(CvMTT, server = TRUE,
class = "compact",
filter="top",
rownames = FALSE,
colnames = c("gene", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj", "significant"),
extensions = c('Buttons'),
options = list(
pageLength = 15,
dom = 'Bfrtip',
buttons = c('excel','csv','pdf','copy')
))))
# Extract differential expression results
## @knitr DEGSeq2_Analysis
deseq2Results <- results(deseq2Data, contrast=c("Virus", "Post treatment MV", "Control MV"))
## @knitr DispersionPlot
plotDispEsts(deseq2Data, ylim = c(1e-6, 1e1) )
# View summary of results
#summary(deseq2Results)
# Using DEseq2 built in method
# plotMA(deseq2Results)
# Load libraries
# install.packages(c("ggplot2", "scales", "viridis"))
# Coerce to a data frame
deseq2ResDF <- as.data.frame(deseq2Results)
# Examine this data frame
#head(deseq2ResDF)
# Set a boolean column for significance
deseq2ResDF$significant <- ifelse(deseq2ResDF$padj < .05, "Significant", NA)
# write these differentially expressed genes as a table and save to directory
write.csv(deseq2ResDF, file= "deseq2_control_vs_MV_PT.csv")
# Plot the results similar to DEseq2
MAPlot1 <- ggplot(deseq2ResDF, aes(baseMean, log2FoldChange, colour=significant)) + geom_point(size=1) + scale_y_continuous(limits=c(-3, 3), oob=squish) + scale_x_log10() + geom_hline(yintercept = 0, colour="tomato1", size=2) + labs(x="mean of normalized counts", y="log fold change") + scale_colour_manual(name="q-value", values=("Significant"="red"), na.value="grey50") + theme_bw() + ggtitle("Control vs MV Treatment")
# Let's add some more detail
MAPlot2 <- ggplot(deseq2ResDF, aes(baseMean, log2FoldChange, colour=padj)) + geom_point(size=1) + scale_y_continuous(limits=c(-3, 3), oob=squish) + scale_x_log10() + geom_hline(yintercept = 0, colour="darkorchid4", size=1, linetype="longdash") + labs(x="mean of normalized counts", y="log fold change") + scale_colour_viridis(direction=-1, trans='sqrt') + theme_bw() + geom_density_2d(colour="black", size=2) + ggtitle("Control vs MV Treatment")
## @knitr MAPlots
MAPlot1 + MAPlot2
# Add rectangle around labels
deseq2ResDF2 <- rownames_to_column(deseq2ResDF, var = "gene")
# Add rectangle around labels
## @knitr MAPlots_Significant_Genes
ggmaplot(deseq2ResDF2, main = expression("Control MV" %->% "Post treatment MV"),
fdr = 0.05, fc = 1, size = 1.2,
palette = c("#B31B21", "#1465AC", "darkgray"),
genenames = as.vector(deseq2ResDF2$gene),
legend = "top", top = 20,
font.label = c("bold", 11), label.rectangle = TRUE,
font.legend = "bold",
font.main = "bold",
ggtheme = ggplot2::theme_minimal())
## @knitr VolcanoPlot
EnhancedVolcano(deseq2ResDF2,
lab = as.character(deseq2ResDF2$gene),
x = 'log2FoldChange',
y = 'padj',
xlim = c(-4, 4),
title = "Volcano Plot DEG (Control and Post treatment MV)",
pCutoff = 0.05,
FCcutoff = 0.5,
cutoffLineType = 'twodash',
cutoffLineWidth = 0.8,
pointSize = 2,
labSize = 4,
colAlpha = 1,
labCol = 'black',
labFace = 'bold',
boxedLabels = TRUE,
legendLabels=c('Not sig.','Log (base 2) FC','p-value',
'p-value & Log (base 2) FC'),
legendPosition = 'top',
legendLabSize = 12,
legendIconSize = 2,
drawConnectors = TRUE,
widthConnectors = 0.75)
# Extract counts for the gene otop2
otop2Counts <- plotCounts(deseq2Data, gene="Arg1", intgroup=c("Virus", "Sample"), returnData=TRUE)
# Plot the data using ggplot2
colourPallette <- c("#7145cd","#bbcfc4","#90de4a","#cd46c1","#77dd8e","#592b79","#d7c847","#6378c9","#619a3c","#d44473","#63cfb6","#dd5d36","#5db2ce","#8d3b28","#b1a4cb","#af8439","#c679c0","#4e703f","#753148","#cac88e","#352b48","#cd8d88","#463d25","#556f73")
## @knitr PlotCounts
ggplot(otop2Counts, aes(x=Sample, y=count, colour=Virus, group=Virus)) + geom_point() + geom_line() + theme_bw() + theme(axis.text.x=element_text(angle=15, hjust=1)) + scale_colour_manual(values=colourPallette) + guides(colour=guide_legend(ncol=3)) + ggtitle("Arginase 1 (M2 Macrophage Marker)")
deseq2ResDF["Arg1",]
rawCounts["Arg1",]
normals=row.names(sampleData[sampleData[,"Virus"]=="Control MV",])
primaries=row.names(sampleData[sampleData[,"Virus"]=="Post treatment MV",])
rawCounts["Arg1",normals]
rawCounts["Arg1",primaries]
## @knitr HeatmapTopGenes
# Transform count data using the variance stablilizing transform
deseq2VST <- vst(deseq2Data)
# Convert the DESeq transformed object to a data frame
deseq2VST <- assay(deseq2VST)
deseq2VST <- as.data.frame(deseq2VST)
deseq2VST$Gene <- rownames(deseq2VST)
#head(deseq2VST)
# Keep only the significantly differentiated genes where the fold-change was at least 3
sigGenes <- rownames(deseq2ResDF[deseq2ResDF$padj <= .05 & abs(deseq2ResDF$log2FoldChange) > 2,])
deseq2VST <- deseq2VST[deseq2VST$Gene %in% sigGenes,]
# Convert the VST counts to long format for ggplot2
# First compare wide vs long version
deseq2VST_wide <- deseq2VST
deseq2VST_long <- melt(deseq2VST, id.vars=c("Gene"))
#head(deseq2VST_wide)
#head(deseq2VST_long)
# Now overwrite our original data frame with the long format
deseq2VST <- melt(deseq2VST, id.vars=c("Gene"))
# Make a heatmap
heatmap <- ggplot(deseq2VST, aes(x=variable, y=Gene, fill=value)) + geom_raster() + scale_fill_viridis(trans="sqrt") + theme(axis.text.x=element_text(angle=65, hjust=1), axis.text.y=element_blank(), axis.ticks.y=element_blank())
# heatmap
# Convert the significant genes back to a matrix for clustering
deseq2VSTMatrix <- dcast(deseq2VST, Gene ~ variable)
rownames(deseq2VSTMatrix) <- deseq2VSTMatrix$Gene
deseq2VSTMatrix$Gene <- NULL
# Compute a distance calculation on both dimensions of the matrix
distanceGene <- dist(deseq2VSTMatrix)
distanceSample <- dist(t(deseq2VSTMatrix))
# Cluster based on the distance calculations
clusterGene <- hclust(distanceGene, method="ward.D2")
clusterSample <- hclust(distanceSample, method="ward.D2")
# Construct a dendogram for samples
# install.packages("ggdendro")
sampleModel <- as.dendrogram(clusterSample)
sampleDendrogramData <- segment(dendro_data(sampleModel, type = "rectangle"))
sampleDendrogram <- ggplot(sampleDendrogramData) + geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) + theme_dendro()
# Re-factor samples for ggplot2
deseq2VST$variable <- factor(deseq2VST$variable, levels=clusterSample$labels[clusterSample$order])
# Construct the heatmap. note that at this point we have only clustered the samples NOT the genes
heatmap <- ggplot(deseq2VST, aes(x=variable, y=Gene, fill=value)) + geom_raster() + scale_fill_viridis(trans="sqrt") + theme(axis.text.x=element_text(angle=65, hjust=1), axis.text.y=element_blank(), axis.ticks.y=element_blank())
#heatmap
# Combine the dendrogram and the heatmap
# install.packages("gridExtra")
#grid.arrange(sampleDendrogram, heatmap, ncol=1, heights=c(1,5))
# Load in libraries necessary for modifying plots
#install.packages("gtable")
# Modify the ggplot objects
sampleDendrogram_1 <- sampleDendrogram + scale_x_continuous(expand=c(.0085, .0085)) + scale_y_continuous(expand=c(0, 0))
heatmap_1 <- heatmap + scale_x_discrete(expand=c(0, 0)) + scale_y_discrete(expand=c(0, 0))
# Convert both grid based objects to grobs
sampleDendrogramGrob <- ggplotGrob(sampleDendrogram_1)
heatmapGrob <- ggplotGrob(heatmap_1)
# Check the widths of each grob
# sampleDendrogramGrob$widths
# heatmapGrob$widths
# Add in the missing columns
sampleDendrogramGrob <- gtable_add_cols(sampleDendrogramGrob, heatmapGrob$widths[7], 6)
sampleDendrogramGrob <- gtable_add_cols(sampleDendrogramGrob, heatmapGrob$widths[8], 7)
# Make sure every width between the two grobs is the same
maxWidth <- unit.pmax(sampleDendrogramGrob$widths, heatmapGrob$widths)
sampleDendrogramGrob$widths <- as.list(maxWidth)
heatmapGrob$widths <- as.list(maxWidth)
# Arrange the grobs into a plot
finalGrob <- arrangeGrob(sampleDendrogramGrob, heatmapGrob, ncol=1, heights=c(2,5))
# Draw the plot
#grid.draw(finalGrob)
# Re-order the sample data to match the clustering we did
sampleData_v2$Sample_ID <- factor(sampleData_v2$Sample_ID, levels=clusterSample$labels[clusterSample$order])
# Construct a plot to show the clinical data
colours <- c("#743B8B", "#8B743B", "#8B3B52", "#00AFBB", "#E7B800", "#FC4E07", "green", "red", "navy")
sampleClinical <- ggplot(sampleData_v2, aes(x=Sample_ID, y=1, fill=Viral_Status)) + geom_tile() + scale_x_discrete(expand=c(0, 0)) + scale_y_discrete(expand=c(0, 0)) + scale_fill_manual(name="Tissue", values=colours) + theme_void()
# Convert the clinical plot to a grob
sampleClinicalGrob <- ggplotGrob(sampleClinical)
# Make sure every width between all grobs is the same
maxWidth <- unit.pmax(sampleDendrogramGrob$widths, heatmapGrob$widths, sampleClinicalGrob$widths)
sampleDendrogramGrob$widths <- as.list(maxWidth)
heatmapGrob$widths <- as.list(maxWidth)
sampleClinicalGrob$widths <- as.list(maxWidth)
# Arrange and output the final plot
finalGrob <- arrangeGrob(sampleDendrogramGrob, sampleClinicalGrob, heatmapGrob, ncol=1, heights=c(2,1,5))
#grid.draw(finalGrob)
###############################################################################
################# Step 1: create dendrogram for genes ##########################
# we already did the clustering for genes in the tutorial, get the data to make a dendrogram with ggplot
geneModel <- as.dendrogram(clusterGene)
geneDendrogramData <- segment(dendro_data(geneModel, type = "rectangle"))
# construct the dendrogram in ggplot
geneDendrogram <- ggplot(geneDendrogramData) + geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) + coord_flip() + scale_y_reverse(expand=c(0, 0)) + scale_x_continuous(expand=c(0, 0)) + theme_dendro()
################################################################################
################# Step 2: Re-arrange the heatmap cells #########################
# re-factor genes for ggplot2
deseq2VST$Gene <- factor(deseq2VST$Gene, levels=clusterGene$labels[clusterGene$order])
# recreate the heatmap with this new factoring
heatmap <- ggplot(deseq2VST, aes(x=variable, y=Gene, fill=value)) + geom_raster() + scale_fill_viridis(trans="sqrt") + theme(axis.text.x=element_text(angle=65, hjust=1), axis.text.y=element_blank(), axis.ticks.y=element_blank())
################################################################################
################# Step 3: convert to everything to grobs #######################
# note! before this step as mentioned you might need to alter the expand parameters in the plot scales for all the plots we do that here
# convert the heatmap to a grob
heatmapGrob <- ggplotGrob(heatmap + scale_x_discrete(expand=c(0, 0)) + scale_y_discrete(expand=c(0, 0)))
# convert the dendrogram to a grob
# note! we flipped the axis above so the x-axis is now displayed as what we would think of as the y-axis
geneDendrogramGrob <- ggplotGrob(geneDendrogram + scale_x_discrete(expand=c(0, 0)))
# we already have a sample Dendrogram, but here it is again
sampleDendrogramGrob <- ggplotGrob(sampleDendrogram + scale_x_continuous(expand=c(.0085, .0085)) + scale_y_continuous(expand=c(0, 0)))
# we already have our sample clinical plot but here it is again
sampleClinicalGrob <- sampleClinicalGrob
################################################################################
######### Step 4: align the gene dendrograms to match the heatmap ##############
# check that the both the heatmap and gene dendrogram have the same number of vertical elements
#length(heatmapGrob$heights) == length(geneDendrogramGrob$heights)
# make sure every height between the two grobs is the same
maxHeight <- unit.pmax(geneDendrogramGrob$heights, heatmapGrob$heights)
geneDendrogramGrob$heights <- as.list(maxHeight)
heatmapGrob$heights <- as.list(maxHeight)
################################################################################
# Step 4b: we have a new heatmap so we need to re-align the horizontal elements #
# repeat the steps in the tutorial
# check the widths of each grob
# sampleDendrogramGrob$widths
# heatmapGrob$widths
# sampleClinicalGrob$widths
# add in the missing columns
sampleDendrogramGrob <- gtable_add_cols(sampleDendrogramGrob, heatmapGrob$widths[7], 6)
sampleDendrogramGrob <- gtable_add_cols(sampleDendrogramGrob, heatmapGrob$widths[8], 7)
# make sure every width between all grobs is the same
maxWidth <- unit.pmax(sampleDendrogramGrob$widths, heatmapGrob$widths, sampleClinicalGrob$widths)
sampleDendrogramGrob$widths <- as.list(maxWidth)
heatmapGrob$widths <- as.list(maxWidth)
sampleClinicalGrob$widths <- as.list(maxWidth)
################################################################################
############### Step 5: create a blank panel ###################################
# we can use grid graphics for this
blankPanel <- grid.rect(gp=gpar(col="white"))
################################################################################
############### Step 6: Arrange the final result ###############################
# arrange all the plots together
finalGrob_v2 <- arrangeGrob(blankPanel, sampleDendrogramGrob, blankPanel, sampleClinicalGrob, geneDendrogramGrob, heatmapGrob, ncol=2, nrow=3, widths=c(1,5), heights=c(2,.8,6))
# draw the final result
grid.draw(finalGrob_v2)
################################################################################
############### Pathway and Enrichment Analysis using DEG ###############################
## @knitr GO_Pathway_Analysis_MV
# we use ggplot2 to add x axis labels (ex: ridgeplot)
# SET THE DESIRED ORGANISM HERE
organism = "org.Mm.eg.db"
#BiocManager::install(organism, character.only = TRUE)
library(organism, character.only = TRUE)
# reading in data from deseq2
df = read.csv("deseq2_control_vs_MV_PT_Pathways.csv", header=TRUE)
# we want the log2 fold change
original_gene_list <- df$log2FoldChange
# name the vector
names(original_gene_list) <- df$gene
# omit any NA values
gene_list<-na.omit(original_gene_list)
# sort the list in decreasing order (required for clusterProfiler)
gene_list = sort(gene_list, decreasing = TRUE)
gse <- gseGO(
geneList=gene_list,
ont = "All",
OrgDb = organism,
keyType = "SYMBOL",
exponent = 1,
minGSSize = 10,
maxGSSize = 500,
eps = 1e-10,
pvalueCutoff = 0.05,
pAdjustMethod = "BH",
verbose = TRUE,
seed = FALSE,
by = "fgsea")
dotplot(gse, showCategory=10, split=".sign") + facet_grid(.~.sign)
## @knitr EMapPlot_MV
x1 <- pairwise_termsim(gse)
emapplot(x1)
## @knitr cMapPlot_MV
# categorySize can be either 'pvalue' or 'geneNum'
cnetplot(x1, categorySize="pvalue", foldChange=gene_list, showCategory = 3)
## @knitr RidglePlot_MV
ridgeplot(gse) + labs(x = "enrichment distribution")
## @knitr GSEA_TopPathway_MV
# Use the `Gene Set` param for the index in the title, and as the value for geneSetId
#trace("gseaplot2", edit = TRUE)
gseaplot2(gse, geneSetID = 27, pvalue_table = TRUE, ES_geom = "line", title = gse$Description[27])
## @knitr Published_Articles_MV
terms <- gse$Description[1:3]
pmcplot(terms, 2010:2023, proportion=FALSE)
## @knitr KEGG_Pathways
# Convert gene IDs for gseKEGG function
# We will lose some genes here because not all IDs will be converted
ids<-bitr(names(original_gene_list), fromType = "SYMBOL", toType = "ENTREZID", OrgDb=organism)
# remove duplicate IDS (here I use "SYMBOL", but it should be whatever was selected as keyType)
dedup_ids = ids[!duplicated(ids[c("SYMBOL")]),]
# Create a new dataframe df2 which has only the genes which were successfully mapped using the bitr function above
df2 = df[df$gene %in% dedup_ids$SYMBOL,]
# Create a new column in df2 with the corresponding ENTREZ IDs
df2$Y = dedup_ids$ENTREZID
# Create a vector of the gene unuiverse
kegg_gene_list <- df2$log2FoldChange
# Name vector with ENTREZ ids
names(kegg_gene_list) <- df2$Y
# omit any NA values
kegg_gene_list<-na.omit(kegg_gene_list)
# sort the list in decreasing order (required for clusterProfiler)
kegg_gene_list = sort(kegg_gene_list, decreasing = TRUE)
kegg_organism = "mouse"
kk2 <- gseKEGG(geneList = kegg_gene_list,
organism = kegg_organism,
nPerm = 10000,
minGSSize = 3,
maxGSSize = 800,
pvalueCutoff = 0.05,
pAdjustMethod = "none",
keyType = "ncbi-geneid")
## @knitr KEGG_TopPathway_MV
dotplot(kk2, showCategory = 10, title = "Enriched Pathways" , split=".sign") + facet_grid(.~.sign)
## @knitr KEGG_Pathway_Enrichment
x2 <- pairwise_termsim(kk2)
emapplot(x2)