-
Notifications
You must be signed in to change notification settings - Fork 1
/
Figure3-rna-fm_merged.Rmd
1367 lines (1103 loc) · 50.4 KB
/
Figure3-rna-fm_merged.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
---
title: "Figure3 - RNAseq"
author: "E Onur Karakaslar"
date: "1/15/2020"
output: html_document
---
```{r setup, include=FALSE}
require(knitr)
knitr::opts_chunk$set(echo = TRUE)
opts_knit$set(root.dir = "/Users/karako/Dropbox (JAX)/MouseAging_clean/") #set root dir!
```
```{r library, message=FALSE}
library(xlsx)
library(plyr) # rbind.fill
library(edgeR) # finding Differentially Expressed genes
library(limma) # for quantile normalization
library(dplyr) # using pipe (%>%) and select
library(fgsea)
library(ggpubr)
library(ggplot2)
library(writexl)
library(tidyverse)
library(RColorBrewer)
library(preprocessCore)
source("code/color_values.R")
```
```{r save_as_RData}
save_as_RData <- function(){
count.matrix <- read.csv("data/RNAseq/F3_input/rna_count_matrix.csv")
# Order genes according to their standard deviation in decreasing order
count.matrix <- count.matrix [rev(order(apply(count.matrix[,-1], 1, sd))),]
# Remove duplicated genes
count.matrix <- count.matrix [!duplicated(count.matrix[,1]),]
# Make the row names Ensembl Gene IDs
rownames(count.matrix) <- count.matrix[,1]
# Filter Ensembl Gene IDs
count.matrix <- count.matrix[,-1]
# Enforce all counts to be integers
count.matrix <- round(count.matrix, 0)
tissues <- colnames(count.matrix) %>% strsplit(".", fixed = T) %>% sapply(function(x){x[4]})
bm <- count.matrix [, which(tissues == "BM")]
pbl <- count.matrix [, which(tissues == "PBL")]
naive <- count.matrix [, which(tissues == "naive")]
spleen <- count.matrix [, which(tissues == "spleen")]
memory <- count.matrix [, which(tissues == "memory")]
save(bm , file = "data/RNAseq/F3_input/rnaseq-expcount-bm.RData")
save(pbl , file = "data/RNAseq/F3_input/rnaseq-expcount-pbl.RData")
save(naive , file = "data/RNAseq/F3_input/rnaseq-expcount-naive.RData")
save(spleen, file = "data/RNAseq/F3_input/rnaseq-expcount-spleen.RData")
save(memory, file = "data/RNAseq/F3_input/rnaseq-expcount-memory.RData")
# Filter low-expressed genes
# Keep the genes that have Count-Per-Million more than k = 0.5 in n = 1 samples
# It is pretty similar to filterByExpr(y, min.count = 0.5) but its choice of n is different.
pbl <- pbl[rowSums(cpm(pbl) >= 1) >= 2,]
naive <- naive[rowSums(cpm(naive) >= 1) >= 2,]
spleen <- spleen[rowSums(cpm(spleen) >= 1) >= 2,]
memory <- memory[rowSums(cpm(memory) >= 1) >= 2,]
pbl.cpm <- cpm(pbl,log = F)
naive.cpm <- cpm(naive, log = T)
spleen.cpm <- cpm(spleen, log = T)
memory.cpm <- cpm(memory, log = T)
save(pbl.cpm , file = "data/RNAseq/F3_input/CPM Normalized Counts/rnaseq-expcount-cpm-pbl.RData")
save(naive.cpm , file = "data/RNAseq/F3_input/CPM Normalized Counts/rnaseq-expcount-cpm-naive.RData")
save(spleen.cpm, file = "data/RNAseq/F3_input/CPM Normalized Counts/rnaseq-expcount-cpm-spleen.RData")
save(memory.cpm, file = "data/RNAseq/F3_input/CPM Normalized Counts/rnaseq-expcount-cpm-memory.RData")
}
```
```{r preprocess-data}
preprocess_data <- function(tissue_cell_type){
cat (paste0("Loading RNAseq data for: ", toupper(tissue_cell_type), "\n"))
name <- load (paste0("./data/RNAseq/F3_input/rnaseq-expcount-",tissue_cell_type,'.RData'))
count.matrix <- get(name)
# Enforce all counts to be integers
count.matrix <- round(count.matrix, 0)
# Filter low-expressed genes
# Keep the genes that have Count-Per-Million more than k = 0.5 in n = 1 samples
# It is pretty similar to filterByExpr(y, min.count = 0.5) but its choice of n is different.
count.matrix.filtered <- count.matrix [rowSums(cpm(count.matrix) >= 0.5) >= 2,]
# normalize with cpm
count.matrix.normalized <- cpm(count.matrix.filtered, log = T)
sample_rna <- colnames(count.matrix) %>% strsplit(".", fixed = T)
STRAIN = TYPE <- sapply(sample_rna, function(x){
x[1]
})
AGE <- sapply(sample_rna, function(x){
as.numeric(gsub("([0-9]+).*$", "\\1", x[2] %>% trimws))
})
GENDER <- sapply(sample_rna, function(x){
x[3]
})
TISSUE <- sapply(sample_rna, function(x){
x[4]
})
SAMPLEMOUSEID <- sapply(sample_rna, function(x){
x[5]
})
specs = NULL
specs$STRAIN <- STRAIN
specs$TYPE <- TYPE
specs$AGE <- AGE
specs$GENDER <- GENDER
specs$TISSUE <- TISSUE
specs$SAMPLEMOUSEID <- SAMPLEMOUSEID
result <- list(count.matrix= count.matrix, count.matrix.normalized = count.matrix.normalized, specs=specs)
return (result)
}
```
```{r load genesets}
load_genesets <- function(){
load('data/genesets/scRNA_and_DICE/geneset.info.RData')
assign("selected_genesets", value = list(
scrnaseq_tcells_specific_10x = geneset.genes.scrnaseq_tcells_specific_10x,
vp2008 = geneset.genes.vp2008,
wp = geneset.genes.wp,
scrnaseq_tcells_expressed_10x = geneset.genes.scrnaseq_tcells_expressed_10x,
scrnaseq_pbmc_top = geneset.genes.scrnaseq_pbmc_top,
scrnaseq_pbmc_simple_exclusive = geneset.genes.scrnaseq_pbmc_simple_exclusive,
scrnaseq_pbmc_simple_specific = geneset.genes.scrnaseq_pbmc_simple_specific,
#gobp = geneset.genes.gobp,
#gomf = geneset.genes.gomf,
dice_major = geneset.genes.dice_major
), envir = .GlobalEnv)
selected_genesets_mice <- lapply(selected_genesets, function(gs){
gs %>% convertHumanGeneList
})
assign("selected_genesets_mice", selected_genesets_mice, .GlobalEnv)
assign("selected_genesets_labels", list(
scrnaseq_tcells_specific_10x = geneset.names.scrnaseq_tcells_specific_10x,
vp2008 = geneset.names.vp2008,
wp = geneset.names.wp,
scrnaseq_tcells_expressed_10x = geneset.names.scrnaseq_tcells_expressed_10x,
scrnaseq_pbmc_top = geneset.names.scrnaseq_pbmc_top,
scrnaseq_pbmc_simple_exclusive = geneset.names.scrnaseq_pbmc_simple_exclusive,
scrnaseq_pbmc_simple_specific = geneset.names.scrnaseq_pbmc_simple_specific,
#gobp = geneset.names.gobp,
#gomf = geneset.names.gomf,
dice_major = geneset.names.dice_major
), envir = .GlobalEnv)
assign("union_size",
lapply(selected_genesets, function(gs){
gs[,"GeneName"]
}) %>% unlist(recursive = F) %>% unique %>% length, envir = .GlobalEnv)
selected_genesets_gsea <- lapply(names(selected_genesets_labels), function(x){
geneset <- selected_genesets_mice[[x]]
geneset.label <- selected_genesets_labels[[x]]
geneset.merged <- merge(geneset, geneset.label, by = "Module.ID")
pathways <- geneset.merged %>%
group_split(Module.Name) %>% sapply(function(x) x[,"Gene.stable.ID"])
pathway.names <-
geneset.merged %>% group_split(Module.Name) %>%
sapply(function(x) x[,"Module.Name"] %>% unique) %>% unlist
names(pathways) <- pathway.names
return(pathways)
})
names(selected_genesets_gsea) <- names(selected_genesets_labels)
}
```
```{r convert_genesets}
# Basic function to convert human gene names to mouse ensembl gene ids
convertHumanGeneList <- function(x){
x_genename <- x [,"GeneName"] %>% unique
require("biomaRt")
if (!exists("human")){
assign( x = "human",
value = useMart("ensembl", dataset = "hsapiens_gene_ensembl"),
envir = .GlobalEnv)
cat("Human Genes are imported...\n")
}
if (!exists("mouse")){
assign( x = "mouse",
value = useMart("ensembl", dataset = "mmusculus_gene_ensembl"),
envir = .GlobalEnv)
cat("Mouse Genes are imported...\n")
}
# map from human to mice
genesV2 = getLDS(attributes = c("hgnc_symbol"),
filters = "hgnc_symbol",
values = x_genename ,
mart = human,
attributesL = c("ensembl_gene_id"),
martL = mouse, uniqueRows=T)
# each human genome should be unique, so 1 to 1 map should be possible
# genesV2 <- genesV2[ !duplicated(genesV2$HGNC.symbol),]
genesV2 <- genesV2[ !duplicated(genesV2$Gene.stable.ID),]
humanx <- merge(x, genesV2, by.x = "GeneName", by.y = "HGNC.symbol")
return(humanx)
}
```
```{r}
#' Differential Expression Analysis
#'
#' This function takes a count matrix, normalizes it with TMM and returns a fit matrix
#'
#' @param data raw count matrix, rownames should be Gene IDs
#' @param specs specialities of mice: age, gender, strain
#' @return fit matrix which can be used later for differential analysis
DE_fit <- function(count.matrix, specs){
y <- DGEList(counts = count.matrix)
keep <- filterByExpr(y, min.count = 1)
y <- y[keep,,keep.lib.sizes = F]
y <- calcNormFactors(object = y, method = "TMM")
group <- factor(paste(specs$TYPE,specs$AGE,sep="."))
design <- model.matrix(~0+group)
colnames(design) <- levels(group)
y <- estimateDisp(y, design)
fit <- glmQLFit(y, design, robust = TRUE) # recommended in edgeR manual 4.4.7
return (fit)
}
```
```{r DE_fit_quantile}
#' Differential Expression Analysis
#'
#' This function takes a count matrix normalizes it with quantile normalization and
#' return fit matrix.
#'
#' @param data raw count matrix, rownames should be Gene IDs
#' @param specs specialities of mice: age, gender, strain
#' @return fit matrix which can be used later for differential analysis
DE_fit_quantile <- function(count.matrix, specs){
y <- DGEList(counts = count.matrix)
keep <- filterByExpr(y, min.count=1)
y <- y[keep,,keep.lib.sizes=F]
y <- calcNormFactors(object = y, method = "none")
group <- factor(paste(specs$TYPE,specs$AGE,sep="."))
design <- model.matrix(~0+group)
colnames(design) <- levels(group)
rownames(design) <- colnames(count.matrix)
v <- voom(y,design, normalize.method ="quantile")
fit <- lmFit(v, design)
fit$aveLogCPM <- aveLogCPM(count.matrix)
return (fit)
}
```
```{r DE_test}
#' @param fit matrix containing model parameters and design matrix
#' @param contrast DE contrast
#' @return qlm f-test in edgeR (qlf)
DE_test <- function(fit, contrast){
contrasts.age_sex_str <- makeContrasts(
Age18vs3_B6 = (B6.18 - B6.3),
Age18vs3_NZO = (NZO.18 - NZO.3),
levels = fit$design)
qlf <- glmQLFTest(fit, contrast = contrasts.age_sex_str[, contrast])
return(qlf)
}
```
```{r DE_test_quantile}
DE_test_quantile <- function (fit, contrast){
contrasts.age_sex_str <- makeContrasts(
Age18vs3_B6 = (B6.18 - B6.3),
Age18vs3_NZO = (NZO.18 - NZO.3),
levels = fit$design)
tmp <- contrasts.fit(fit, contrasts.age_sex_str[, contrast])
tmp <- eBayes(tmp)
return (tmp)
}
```
```{r DE_toptags}
#' @param adjust.method default is BH, check p.adjust doc for more
#' @param p.value determines FDR threshold
#' @param n number of returned genes
DE_toptags <- function(qlf, p.value = 1, adjust.method = "BH", n = Inf){
top.tags <- topTags(qlf, n = n, adjust.method = adjust.method, p.value = p.value)
return (top.tags)
}
```
```{r DE_toptags_quantile}
DE_toptags_quantile <- function (tmp, sort.by = "p", n = Inf, p.value = 1, lfc = 0){
top.table <- topTable(tmp, sort.by = sort.by, n = Inf, p.value = p.value, lfc = lfc)
return(top.table)
}
```
```{r Geneset Enrichment Analyses}
gsea <- function(top.table, tissue_cell_type, contrast){
# use logFC as phenotype of interest
ranks <- top.table$logFC
# make the gene names
names(ranks) <- rownames(top.table)
gsea.result <- lapply(selected_genesets_gsea, function(pathways){
fgsea(pathways, ranks, minSize=15, maxSize = 500, nperm=1000)
})
gsea.list <- map_df(gsea.result, ~as.data.frame(.x), .id="Geneset")
gsea.list$Contrast <- contrast
gsea.list$TCT <- tissue_cell_type
return(gsea.list)
}
```
```{r check_genesets}
check_genesets <- function (top.table, tissue_cell_type, contrast, union_size = 20e3){
# Up regulated genes specs
genes_up_tbl <- top.table[top.table$logFC > 0,]
# Down regulated genes specs
genes_dw_tbl <- top.table[top.table$logFC < 0,]
# Change between TMM and Quantile fitting
loc_up <- match(c("P.Value","adj.P.Val"), colnames(genes_up_tbl))
if(!is.na(loc_up[1])) {
colnames(genes_up_tbl)[c(loc_up)] <- c("PValue","FDR")
colnames(genes_dw_tbl)[c(loc_up)] <- c("PValue","FDR")
}
# These are mice gene names which are differentially expressed.
genes_up <- genes_up_tbl %>% rownames
genes_down <- genes_dw_tbl %>% rownames
# create an empty dataframe for enriched modules,
# so that later we can sort them and prepare excel tables, yey!
enriched_modules_df <- data.frame()
# for each geneset, iterate each module
for (i in 1:length(selected_genesets_mice)){
geneset_name <- names(selected_genesets_labels)[[i]]
module_names <- selected_genesets_labels[[i]]
modules <- selected_genesets_mice [[i]]
modules <- merge(modules, module_names, by = "Module.ID")
# here we create empty p values, geneset name vectors so that we can adjust p values later
module_count <- nrow(module_names)
vector_module_names <- vector_geneset_names <- character(module_count)
vector_overlap_ratio_up <- vector_overlap_ratio_dw <- vector_p_up <- vector_p_down <-
numeric(module_count)
# iterate the modules
cat ("Geneset Name:", geneset_name, "\n")
for (j in 1:nrow(module_names)){
module_ID <- module_names[j, "Module.ID"]
module_name <- module_names[j, "Module.Name"]
module <- modules[modules$Module.ID %in% module_ID, "Gene.stable.ID"]
# module gene count (constant for up/down)
gene_count_module <- unique(module) %>% length
# upregulated gene count
n_up <- length(genes_up)
# overlapped up-regulated genes with the module
q_up <- genes_up %in% module %>% sum
# overlap ratio for upregulated genes with modules
overlap_ratio_up <- q_up / gene_count_module
# here we calculate the probability of having a bigger intersection
# than the count of overlapping genes given the module size and the total gene count.
# we substract 1 for removing the equality when the lower.tail = F, which changes P(X<x) to 1-P(X>=x).
p_up <- phyper(q_up-1, gene_count_module, union_size - gene_count_module, n_up, lower.tail = F, log.p = F)
# downregulated gene count
n_down <- length(genes_down)
# overlapped down-regulated genes with the module
q_down <- genes_down %in% module %>% sum
# overlap ratio for down regulated genes with modules
overlap_ratio_dw <- q_down / gene_count_module
p_down <- phyper(q_down-1, gene_count_module, union_size - gene_count_module, n_down, lower.tail = F, log.p = F)
vector_p_up[j] <- p_up
vector_p_down[j] <- p_down
vector_geneset_names[j] <- names(selected_genesets)[[i]]
vector_module_names[j] <- module_name
vector_overlap_ratio_up[j] <- overlap_ratio_up
vector_overlap_ratio_dw[j] <- overlap_ratio_dw
}
df_up <- data.frame(geneset.name = vector_geneset_names,
module.name = vector_module_names,
overlap.ratio = vector_overlap_ratio_up,
p = vector_p_up,
stringsAsFactors = F)
df_down <- data.frame(geneset.name = vector_geneset_names,
module.name = vector_module_names,
overlap.ratio = vector_overlap_ratio_dw,
p = vector_p_down,
stringsAsFactors = F)
# adjust the p-values for each module
df_up$adj.p <- p.adjust(p = df_up$p , method = "fdr")
df_down$adj.p <- p.adjust(p = df_down$p, method = "fdr")
# sort according to adjusted p-values and then to p-values
df_up <- df_up [order(df_up$adj.p , df_up$p) ,]
df_down <- df_down[order(df_down$adj.p, df_down$p),]
# this is an important parameter since it affects the number of modules that are chosen
fdr.threshold <- 0.05
# check if any modules are enriched for up regulated genes
if (any(df_up$adj.p < fdr.threshold)){
# take the enriched modules
enriched_modules <- df_up[df_up$adj.p < fdr.threshold,]
# add enriched modules to dataframe
enriched_modules_df <- cbind(TCT = tissue_cell_type,
Contrast = contrast,
enriched_modules,
Status="Up",
Overlapping.Genes = NA) %>% rbind(enriched_modules_df)
for (k in 1:nrow(enriched_modules)){
enriched_module_name <- enriched_modules[k, "module.name"]
# Select the genes from modules that are overlapping with up regulated genes for enriched modules
enriched_modules_df$Overlapping.Genes[k] <-
with(modules, Gene.stable.ID[Gene.stable.ID %in% genes_up & Module.Name %in% enriched_module_name]) %>%
paste(collapse = ",")
} # for k
} # if
# check if any modules are enriched for down regulated genes
# To-Do: I know I should make this a function...
if (any(df_down$adj.p < fdr.threshold)){
# take the enriched modules
enriched_modules <- df_down[df_down$adj.p < fdr.threshold,]
# add enriched modules to dataframe
enriched_modules_df <- cbind(TCT = tissue_cell_type,
Contrast = contrast,
enriched_modules,
Status = "Down",
Overlapping.Genes = NA) %>% rbind(enriched_modules_df)
for (k in 1:nrow(enriched_modules)){
enriched_module_name <- enriched_modules[k, "module.name"]
# Select the genes from modules that are overlapping with up regulated genes for enriched modules
enriched_modules_df$Overlapping.Genes[k] <-
with(modules, Gene.stable.ID[Gene.stable.ID %in% genes_down & Module.Name %in% enriched_module_name]) %>%
paste(collapse = ",")
} # for k
} # if
} # for i
if (nrow(enriched_modules_df) == 0) return (NULL)
return (enriched_modules_df)
}
```
```{r Create Bar Plots}
#' bars represent total consensus peaks (up-non-down)
#' n: number of samples for given bar
#' across 5 different tissues
create_bar_plots <- function(tissue_cell_type, fit_cache){
data <- preprocess_data(tissue_cell_type)
count.matrix <- data$count.matrix
specs <- data$specs
fit <- fit_cache[[tissue_cell_type]] # only works if there is a fit_cache!
contrasts <- c("Age18vs3_B6", "Age18vs3_NZO")
bar.plots <- lapply(contrasts, function(contrast_name, tissue_cell_type){
tmp <- DE_test_quantile(fit, contrast_name)
top.table <- DE_toptags_quantile(tmp, p = 0.05, lfc = 1)
genes_total<- nrow(top.table)
genes_up <- (top.table$logFC > 0) %>% sum
genes_down <- (top.table$logFC < 0) %>% sum
#genes_non <- genes_total - (genes_up + genes_down)
n1 <- sum(tmp$design[,tmp$contrast!=0][,1])
n2 <- sum(tmp$design[,tmp$contrast!=0][,2])
df <- data.frame(TCT = tissue_cell_type,
Contrast = contrast_name,
Gene.Count = genes_up,
Reg = "Up",
stringsAsFactors = F) %>%
rbind (c(tissue_cell_type,
contrast_name,
genes_down,
Reg = "Down"))
genes_percent <- df$Gene.Count %>% as.numeric
df$percent <- (genes_percent / genes_percent %>% sum) * 100
return(list(df=df, n1=n1, n2=n2))
}, tissue_cell_type)
names(bar.plots) <- contrasts
return(bar.plots)
}
```
```{r draw_bar_plots}
#' @param all_bar_plots these plots are created with create_bar_plots and they are for 8 contrasts across 5 different tissue/cell types resulting in 40 different plots.
draw_bar_plots <- function(all_bar_plots){
df_parts <- lapply(all_bar_plots, function(x){
x[[1]]$df %>% rbind (x[[2]]$df)
})
df_all <- do.call("rbind", df_parts)
df_all$Reg <- as.factor(df_all$Reg)
df_all$Reg <- relevel(df_all$Reg, 'Up')
df_all <- df_all %>%
group_by(Contrast, TCT) %>%
arrange(TCT, dplyr::desc(Reg)) %>%
mutate(lab_ypos = cumsum(as.numeric(Gene.Count)) - 0.5 * as.numeric(Gene.Count))
df_all$TCT[df_all$TCT == "naive"] <- "CD8+ Naive"
df_all$TCT[df_all$TCT == "memory"] <- "CD8+ Memory"
df_all$TCT[df_all$TCT == "bm"] <- "BM"
df_all$TCT[df_all$TCT == "spleen"] <- "SPLEEN"
df_all$TCT[df_all$TCT == "pbl"] <- "PBL"
df_all$Contrast[df_all$Contrast == "Age18vs3_B6"] <- "B6"
df_all$Contrast[df_all$Contrast == "Age18vs3_NZO"] <- "NZO"
plot.tissues <- ggplot(df_all %>% filter(TCT %in% c("SPLEEN", "PBL")),
aes(x = TCT, y = as.numeric(Gene.Count)))+
geom_bar(stat = "identity", aes(fill = Reg), width = 0.5) +
geom_text(aes(y = lab_ypos, label = as.numeric(Gene.Count), group =Reg),
color = "white", size = 5.5) +
theme_minimal(base_size = 24) +
scale_fill_manual(values = c("#B2182BFF","#2166ACFF", "#c7c7c7")) +
ylab("") + xlab("") +
facet_wrap(~Contrast, nrow = 1) +
theme(legend.position = "none",
strip.text = element_text(size = 20, face = "bold"),
axis.text = element_text(color = "black")) + ylim(c(0,2500))
ggsave(paste0('output/F3/Gene and Peak Counts/Age18vs3_genecounts_tissues.pdf'),
plot = plot.tissues,
units = "in",
width = 8,
height = 5,
useDingbats = FALSE
)
plot.cell.types <- ggplot(df_all %>% filter(TCT %in% c("CD8+ Naive", "CD8+ Memory")),
aes(x = TCT, y = as.numeric(Gene.Count)))+
geom_bar(stat = "identity", aes(fill = Reg), width = 0.5) +
geom_text(aes(y = lab_ypos, label = as.numeric(Gene.Count), group =Reg),
color = "white", size = 5.5) +
theme_minimal(base_size = 24) +
scale_fill_manual(values = c("#B2182BFF","#2166ACFF", "#c7c7c7")) +
ylab("") + xlab("") +
facet_wrap(~Contrast, nrow = 1) +
theme(legend.position = "none",
strip.text = element_text(size = 20, face = "bold"),
axis.text = element_text(color = "black"))
ggsave(paste0('output/F3/Gene and Peak Counts/Age18vs3_genecounts_cell_types.pdf'),
plot = plot.cell.types,
units = "in",
width = 8,
height = 5,
useDingbats = FALSE
)
}
```
```{r clean inflammation module names}
cleannames <- function(data){
# data <- data %>% filter(geneset.name =="vp2008"|geneset.name == "scrnaseq_pbmc_simple_specific") %>%
# mutate(geneset.name = ifelse(geneset.name =="vp2008", "Gene set from Immune Modules (1)",
# "Gene set from Single Cell RNA Modules"))
trigger <- F
if(any(colnames(data) == "pathway")){
colnames(data)[colnames(data) == "pathway"] <- "module.name"
trigger <- T
}
data <- data %>% mutate(module.name = case_when(
(module.name == "B cells") ~ "B cells",
(module.name == "Cytotoxic cells") ~ "Cytotoxic T/Natural killer cells",
(module.name == "Erythrocytes") ~ "Erythrocytes",
(module.name == "Inflammation I" ) ~ "Inflammatory processes" ,
(module.name == "Inflammation II") ~ "Inflammation molecules",
(module.name == "Interferon-inducible") ~ "Antiviral molecules",
(module.name == "MHC/Ribosomal proteins") ~ "MHC proteins",
(module.name == "Myeloid lineage 1") ~ "Myeloid lineage 1",
(module.name == "Myeloid lineage 2") ~ "Myeloid lineage 2",
(module.name == "Neutrophils" ) ~ "Neutrophils",
(module.name == "Plasma cells") ~ "Plasma cells",
(module.name == "Platelets") ~ "Platelets",
(module.name == "Ribosomal proteins") ~ "Ribosomal proteins",
(module.name == "T Cells") ~ "CD4 and CD8 T-cells",
(module.name == "U_cAMP/NF-KB activation") ~ "TNF-alpha cytokine",
(module.name == "U_enzymes") ~ "Metabolic enzymes",
(module.name == "U_hemoglobin") ~ "Hemoglobin",
(module.name == "U_Immsurface/cytokines/signaling") ~ "Immune surface molecules",
(module.name == "U_Immunity/cytoskeleton") ~ "Cytoskeleton/Immunity related",
(module.name == "U_kinases/phosphatases") ~ "Kinases/RAS",
(module.name == "U_metabolism/replication") ~ "Metabolism/Replication",
(module.name == "U_mitochondrial proteins") ~ "Mitochondrial proteins",
(module.name == "U_P53 signaling" ) ~ "Signaling molecules",
(module.name == "U_protphosphatases/PI3K" ) ~ "Protein phosphatases",
(module.name == "U_proteasome/ubiquitin cx" ) ~ "Proteasome/Ubiquitin",
(module.name == "U_RAS/kinases") ~ "Nuclear factor of activated T cells",
(module.name == "U_T cells/cytoskeleton" ) ~ "T-cells/cytoskeleton",
(module.name == "Unknown") ~ "Unknown",
(module.name == "acCD8_Tcells") ~ "Cytotoxic T-Lymphocytes",
(module.name == "Bcells") ~ "B cells",
(module.name == "DCs") ~ "Dendric Cells",
(module.name == "Erythrocytes") ~ "Erythrocytes",
(module.name == "HSCs") ~ "Hematopoietic stem cells",
(module.name == "Megakaryocytes") ~ "Megakaryocytes",
(module.name == "Monocytes") ~ "Monocytes",
(module.name == "Naive_Tcells") ~ "Naive T-cells",
(module.name == "NK_cells") ~ "Natural killer cells",
(module.name == "pDCs") ~ "Plasmacytoid dendritic cells",
(module.name == "Plasma_cells") ~ "Plasma cells",
(module.name == "Tcells") ~ "CD4 T-cells"))
if (trigger){
colnames(data)[colnames(data) == "module.name"] <- "pathway"
}
return(data)
}
```
```{r enrichment_plots}
# PATH: path of the enriched modules files
# PATH_TO_SAVE:
er_plot <- function(path, path_to_save, gsea = F){
file_list <- list.files(path)
all_modules <- lapply(file_list, function(x){
read.csv(paste0(path, x), stringsAsFactors = F) %>% data.frame
})
df <- do.call("rbind", all_modules)
df$TCT[df$TCT == "naive"] <- "CD8+ Naive"
df$TCT[df$TCT == "memory"] <- "CD8+ Memory"
df$TCT[df$TCT == "bm"] <- "BM"
df$TCT[df$TCT == "spleen"] <- "SPLEEN"
df$TCT[df$TCT == "pbl"] <- "PBL"
filter_tissues = T
if(filter_tissues){
df <- df [df$TCT %in% c("BM", "SPLEEN", "PBL"),]
}
if (gsea){
for (var in unique(df$Geneset)){
plot.df <- df[ df$Geneset == var,]
if (var == "vp2008"){
plot.df <- cleannames(plot.df)
}
p <- ggplot( plot.df,
aes(x=pathway,
y=Contrast,
color = ifelse(sign(NES %>% as.numeric) < 0, "Negative", "Positive"),
size = ifelse(pval < 0.1, (-log10(pval %>% as.numeric)), NA)
)
) +
xlab("") + ylab("") +
geom_point() +
coord_flip() +
facet_wrap(~TCT) +
scale_color_manual(values=c("#2166ACFF","#B2182BFF")) +
scale_y_discrete(name ="Strains", breaks=c("Age18vs3_B6","Age18vs3_NZO"),labels=c("B6", "NZO")) +
theme_minimal(base_size = 16) + labs(color = "Sign", size = "-log10(p)")
if (var == 'wp'){
width_ = 20
height_ = 20
} else {
width_ = 6.5
height_ = 6
}
ggsave(paste0(path_to_save, "GSEA_", var, '.pdf'),
plot = p,
units = "in",
width = width_,
height = height_,
useDingbats = FALSE)
}
} else{
for (var in unique(df$geneset.name)){
p <- ggplot(df[ df$geneset.name == var,] %>%
mutate(Status = factor(Status)),
aes(x=module.name, y=Contrast, color = Status, size = -log10(p))) +
xlab("") + ylab("") +
geom_point() +
coord_flip() +
facet_wrap(~TCT) +
scale_color_manual(values=c("#2166ACFF","#B2182BFF")) +
scale_y_discrete(name ="",
breaks=c("Age18vs3_B6","Age18vs3_NZO"), labels=c("B6", "NZO")) +
theme_minimal(base_size = 16) +
labs(color = "Regulation", size = "-log10(p)") +
theme(axis.text = element_text(color = "black"))
if (var == 'wp'){
width_ = 20
height_ = 20
} else {
width_ = 6.5
height_ = 6
}
ggsave(paste0(path_to_save, var, '.pdf'),
plot = p,
units = "in",
width = width_,
height = height_,
useDingbats = FALSE)
} # for
} # else
}
```
```{r Inflammation Module Select Gene}
select_cool_genes <- function(path_to_enrichment){
file_list <- list.files(path_to_enrichment)
all_modules <- lapply(file_list, function(x){
read.csv(paste0(path_to_enrichment, x), stringsAsFactors = F) %>% data.frame
})
names(all_modules) <- file_list
# Just in case if somehow new files are added to the directory
file.pbl <- file_list[grepl(pattern = "pbl", file_list, fixed = T)]
file.spl <- file_list[grepl(pattern = "spleen", file_list, fixed = T)]
enriched.modules <- all_modules[c(file.pbl, file.spl)] %>% do.call("rbind", .)
enriched.modules$Strain <- strsplit(enriched.modules$Contrast, "_", fixed = T) %>%
sapply(function(x){
x[2]
}
)
genes.and.contrasts <- enriched.modules %>% filter(geneset.name == "vp2008") %>%
filter(module.name == "Inflammation I") %>%
dplyr::select(TCT, Strain, Overlapping.Genes) %>% mutate(
Overlapping.Genes = strsplit(Overlapping.Genes, split = ",", fixed=T)
)
pbl.genes <- do.call(c, genes.and.contrasts[genes.and.contrasts$TCT == "pbl","Overlapping.Genes"]) %>% unique
spl.genes <- do.call(c, genes.and.contrasts[genes.and.contrasts$TCT == "spleen","Overlapping.Genes"]) %>% unique
b6.genes <- do.call(c, genes.and.contrasts[genes.and.contrasts$Strain == "B6", "Overlapping.Genes"]) %>% unique
nzo.genes <- do.call(c, genes.and.contrasts[genes.and.contrasts$Strain == "NZO", "Overlapping.Genes"]) %>% unique
common.genes <- intersect(pbl.genes, spl.genes)
pbl.genes.diff <-setdiff(pbl.genes, spl.genes)
spl.genes.diff <-setdiff(spl.genes, pbl.genes)
gene.maps <- rbind(
cbind(common.genes, TCT = rep("Common", length(common.genes))),
cbind(pbl.genes.diff, TCT = rep("PBL", length(pbl.genes.diff))),
cbind(spl.genes.diff, TCT = rep("Spleen", length(spl.genes.diff)))
)
common.genes.str <- intersect(b6.genes, nzo.genes)
b6.genes.diff <-setdiff(b6.genes, nzo.genes)
nzo.genes.diff <-setdiff(nzo.genes, b6.genes)
gene.maps2 <- rbind(
cbind(common.genes.str, STRAIN = rep("Common", length(common.genes.str))),
cbind(b6.genes.diff, STRAIN = rep("B6", length(b6.genes.diff))),
cbind(nzo.genes.diff, STRAIN = rep("NZO", length(nzo.genes.diff)))
)
gene.maps <-merge(gene.maps, gene.maps2, by.x = "common.genes", by.y = "common.genes.str")
return(list (union.genes = gene.maps))
}
```
```{r run per tissue}
#' @param doTMM if set TRUE, do all analyses with TMM which does not require limma package.
#' @param fit_cache given a parameter it will record trained models for each tissue.
#' @param tissue_cell_type pbl, spleen, naive, memory, bm
#' @return fit model matrix
run_tissue <- function(tissue_cell_type, fit_cache = NULL, doTMM = FALSE){
data <- preprocess_data (tissue_cell_type)
specs <- data$specs
count.matrix <- data$count.matrix
if (is.null(fit_cache)){
if (doTMM) fit <- DE_fit(count.matrix, specs)
else fit <- DE_fit_quantile(count.matrix, specs)
} else {
fit <- fit_cache[[tissue_cell_type]]
}
contrasts <- c("Age18vs3_B6", "Age18vs3_NZO")
er_modules_list <- lapply(contrasts, function(contrast, tissue_cell_type){
cat (paste0("For ", contrast,":\n"))
tmp <- DE_test_quantile(fit, contrast)
top.table <- DE_toptags_quantile(tmp, p.value = 1)
write.csv(x = top.table,
file = paste0("output/F3/DE Genes/All Genes/",
toupper(tissue_cell_type), "_", contrast, "_RNAseq.csv"))
# gsea.results <- suppressWarnings(gsea(top.table, tissue_cell_type, contrast))
#
# top.table <- DE_toptags_quantile(tmp, p.value = 0.05, lfc = 1)
# write.csv(x = top.table,
# file = paste0("output/F3/DE Genes/DE_genes_",
# toupper(tissue_cell_type), "_", contrast, "_RNAseq.csv"))
#
# if (!is.null(top.table)){
#
# cat (paste0("\tupreg_genes ----", top.table[top.table$logFC > 0,] %>% nrow))
# cat (paste0("\tdownreg_genes ----", top.table[top.table$logFC < 0,] %>% nrow, "\n"))
#
# er_modules <- check_genesets(top.table, tissue_cell_type, contrast)
# return (list(gsea.results = gsea.results, er_modules=er_modules))
# }
}, tissue_cell_type = tissue_cell_type)
#
# # make the list a table!
# er_modules <- sapply(er_modules_list, function(x){x["er_modules"]})
# er_modules <- do.call("rbind", er_modules)
# filename_er_modules <- paste0("output/F3/Enrichment Files/Hypergeometric/RNAseq/",
# tissue_cell_type, "_er_summary.csv")
# write.csv(er_modules, file = filename_er_modules)
#
# # get GSEA modules
# gsea.modules <- sapply(er_modules_list, function(x){x["gsea.results"]})
# gsea.modules <- do.call("rbind", gsea.modules)
# gsea.modules$leadingEdge <- sapply(gsea.modules$leadingEdge,function(x){paste(x,collapse = ",")})
# filename_gsea_modules <- paste0("output/F3/Enrichment Files/GSEA/RNAseq/",
# tissue_cell_type, "_gsea_summary.csv")
#
# write.csv(gsea.modules, file = filename_gsea_modules)
return(fit)
}
```
```{r run_analyses, message=F}
# Tissue/Cell Type list
list <- c("naive", "memory", "pbl", "spleen")
# color.values <- paletteer_d("ggsci::default_aaas")
# This is an important parameter, if you want to train all fit matrices
# from strach you need to make this FALSE, so it won't use the cached models!
use_fit_cache = FALSE
load_geneset = FALSE
# PART 1
# Run the differential analyses
if (use_fit_cache){
load("analysis/cache/rna_fit_matrices.RData")
load("analysis/cache/enrichment_analysis.Rdata")
lapply(list, function(tissue_cell_type, fit_cache){
run_tissue(tissue_cell_type, fit_cache)
}, fit_cache) %>% invisible
} else {
if (load_geneset) {
load_genesets() # may take a while
} else {
load("analysis/cache/enrichment_analysis.Rdata")
}
fit_cache <- lapply(list, function(tissue_cell_type){
run_tissue(tissue_cell_type)
})
names(fit_cache) <- list
save(fit_cache, file = "analysis/cache/rna_fit_matrices_fm_merged.RData")
save(human, mouse, union_size, selected_genesets, selected_genesets_mice, selected_genesets_labels,
file = "analysis/cache/rna_enrichment_analysis.Rdata")
}
# PART 2
# Barplots
# Before you can use it, you may need to run the PART 1 with use_fit_cache = F
# so that a fit_cache matrix will be generated and saved.
if (use_fit_cache){
load("analysis/cache/rna_fit_matrices_fm_merged.RData")
all_bar_plots <- lapply(list, function(tissue_cell_type, fit_cache){
create_bar_plots(tissue_cell_type, fit_cache)
}, fit_cache) %>% invisible
names(all_bar_plots) <- list
draw_bar_plots(all_bar_plots)
}
# PART 4
er_plot("output/F3/Enrichment Files/Hypergeometric/RNAseq/", "output/F3/Enrichment Figures/RNAseq/fm_merged/")
# PART 5
er_plot("output/F3/Enrichment Files/GSEA/RNAseq/", "output/F3/Enrichment Figures/RNAseq/fm_merged/", gsea = T)
# PART 6
# We have hundreds of DE genes for age comparisons
# and we need to select couple of them to show in the manuscript.
# The way I do it is to determine the genes that are common across all tissue/cell types
# and maybe even across species... (cool, huh?)
cool.genes <- select_cool_genes('output/F3/Enrichment Files/Hypergeometric/RNAseq/')
# PART 7
# Plot cool genes
# We selected IL1B which is DE across all tissue/cell types!
tissues <- c("pbl", "spleen")
df.plot <- lapply(tissues, function(tissue){
data <- preprocess_data(tissue)
cm.norm <- data$count.matrix.normalized
meta <- data$specs %>% do.call("rbind", .) %>% t
# find the ensembl ID for IL1B
selected_genesets_mice$vp2008[selected_genesets_mice$vp2008$GeneName == "IL1B",]
selected_genesets_mice$vp2008[selected_genesets_mice$vp2008$GeneName == "FOSL2",]
# FOS GENE :ENSMUSG00000021250
# JUN GENE :ENSMUSG00000052684
df.gene <- data.frame(cbind(expr = cm.norm["ENSMUSG00000052684",], meta), stringsAsFactors = F)
# df.gene <- df.gene %>% filter (AGE != 12)
df.gene$AGE <- factor(df.gene$AGE, levels = c(3,12,18))
df.gene$expr <- as.numeric((df.gene$expr))
return(df.gene)
}) %>% do.call(rbind, .)
ggplot(df.plot , aes(AGE, expr)) +
geom_boxplot(aes(color = STRAIN, shape = TISSUE)) +
stat_compare_means(comparisons = list(c("3","18")),