-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyD88_DGE.Rmd
1246 lines (1033 loc) · 42.2 KB
/
MyD88_DGE.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: ' Differential Gene Expression Analysis Notebook'
output:
html_document:
toc: yes
fig_width: 12
fig_height: 12
fig_caption: yes
number_sections: yes
toc_depth: 4
pdf_document:
toc: yes
number_sections: yes
---
```{r, message = FALSE, warning = FALSE}
suppressPackageStartupMessages(library("DESeq2"))
suppressPackageStartupMessages(library("pheatmap"))
suppressPackageStartupMessages(library("PoiClaClu"))
suppressPackageStartupMessages(library("RColorBrewer"))
suppressPackageStartupMessages(library('tidyverse'))
suppressPackageStartupMessages(library("PoiClaClu"))
suppressPackageStartupMessages(library("vsn"))
suppressPackageStartupMessages(library('EnhancedVolcano'))
suppressPackageStartupMessages(library('gplots'))
suppressPackageStartupMessages(library('org.Mm.eg.db'))
suppressPackageStartupMessages(library('stringr'))
suppressPackageStartupMessages(library("genefilter"))
suppressPackageStartupMessages(library("dplyr"))
suppressPackageStartupMessages(library("ggplot2"))
suppressPackageStartupMessages(library("glmpca"))
suppressPackageStartupMessages(library('org.Mm.eg.db'))
suppressPackageStartupMessages(library("AnnotationDbi"))
suppressPackageStartupMessages(library("apeglm"))
suppressPackageStartupMessages(library("ComplexHeatmap"))
suppressPackageStartupMessages(library("clusterProfiler"))
suppressPackageStartupMessages(library('ggrepel'))
suppressPackageStartupMessages(library('corrplot'))
suppressPackageStartupMessages(library("GO.db"))
suppressPackageStartupMessages(library('edgeR'))
suppressPackageStartupMessages(library('GOstats'))
#suppressPackageStartupMessages(library('pathview'))
suppressPackageStartupMessages(library("gage"))
suppressPackageStartupMessages(library("gageData"))
suppressPackageStartupMessages(library('GOSemSim'))
suppressPackageStartupMessages(library('DOSE'))
suppressPackageStartupMessages(library('enrichplot'))
suppressPackageStartupMessages(library('ggnewscale'))
suppressPackageStartupMessages(library('glue'))
```
# Differential Gene Expression Analysis
## Creating metadata for the DGE Analysis
DESeq2 needs sample information (metadata) for performing DGE analysis.
Let's create the sample information
```{r}
# Read the csv file and change the column name
sample_ID <- read.csv("~/R/Alina_RNAseq/MyD88_Samples.csv")
head(sample_ID)
```
```{r}
condition <- c("Infected", "Infected", "Infected", "Infected", "Infected", "Infected",
"Infected", "Infected", "control", "control")
coldata <- data.frame(sample_ID, condition)
colnames(coldata) <- c('Sample_Name','condition') # change name of one of the columns
# The metadata can be found in a df called coldata!
head(coldata)
```
### Tidying up the names for plots later!
#### First from coldata
```{r}
# tidying up the names od samples in both columns that list of samples
#coldata$Samples <- str_remove_all(coldata$Sampls, pattern = "run6_trimmed_|_.bam|_S\\d\\d|_S\\d")
coldata$Sample_Name <- str_remove_all(coldata$Sample_Name,
pattern = "_001.fastq.gz.Aligned.sortedByCoord.out.bam|_S\\d\\d" )
coldata$condition <- as.factor(coldata$condition)
# convert column1 with sample names to row.names of coldata
rownames(coldata) <- coldata$Sample_Name
coldata
```
### Changing the names of samples (as per Alina)
```{r}
# coldata[coldata == '476_R1'] <- 'T'
# coldata[coldata == '754_R1'] <- 'S54'
# coldata[coldata == '755_R1'] <- 'S55'
# coldata[coldata == '757_R1'] <- 'L57'
# coldata[coldata == '758_R1'] <- 'A58'
# coldata[coldata == '760_R1'] <- 'L60'
# coldata[coldata == '761_R1'] <- 'S61'
# coldata[coldata == '762_R1'] <- 'A62'
# coldata[coldata == '763_R1'] <- 'L63'
# coldata[coldata == '764_R1'] <- 'A64'
# coldata[coldata == '765_R1'] <- 'S65'
# coldata[coldata == '766_R1'] <- 'L66'
# coldata[coldata == '768_R1'] <- 'A68'
# coldata[coldata == '769_R1'] <- 'L69'
# coldata[coldata == 'Ctrl1_R1'] <- 'C1'
# coldata[coldata == 'Ctrl2_R2'] <- 'C2'
# # convert column1 with sample names to row.names of coldata
# rownames(coldata) <- coldata$Sample_Name
# coldata
```
## Adding the groupings by Alina for further Metadata Information
```{r}
# coldata$Epithelial_response <- c("LowInducer", "LowInducer", "HighInducer",
# "HighInducer", "LowInducer", "LowInducer",
# "HighInducer", "HighInducer", "LowInducer",
# "HighInducer", "LowInducer", "HighInducer",
# "LowInducer", "LowInducer", 'NR', 'NR')
# coldata$clinical_outcome <- c('symptomatic', 'symptomatic', 'symptomatic',
# 'Lethal', 'asymptomatic', 'Lethal', 'symptomatic',
# 'asymptomatic', 'Lethal', 'symptomatic', 'symptomatic',
# 'Lethal', 'asymptomatic', 'Lethal', 'NR', 'NR')
# coldata$microcolonies <- c('Low', 'Low', 'Low', 'High', 'Low', 'Low',
# 'High', 'High', 'Low', 'High', 'Low', 'High', 'Low',
# 'Low', 'NR', 'NR')
# coldata$ER_microcolonies <- c("LI_LM", "LI_LM", "HI_LM", "HI_HM", "LI_LM", "LI_LM",
# "HI_HM", "HI_HM", "LI_LM", "HI_HM", "LI_LM", "HI_HM",
# "LI_LM", "LI_LM", 'NR', 'NR')
# coldata$phylogenomic_lineage <- c("EPEC1", "EPEC10", "EPEC9", "EPEC9", "NC", "EPEC5",
# "EPEC8", "NC", "EPEC7", "NC", "EPEC2", "EPEC9",
# "EPEC2", "EPEC2", 'NR', 'NR')
# coldata$phylogroup <- c("B2", "A", "B2", "B2", "B1", "A", "B2", "B2", "B1", "B2", "B1",
# "B2", "B1", "B2", 'NR', 'NR')
# coldata$Intimin_Type <- c("alpha", "ND", "lambda", "lambda", "epsilon", "epsilon",
# "mu", "lambda", "beta", "kappa", "beta", "alpha", "beta",
# "beta", 'NR', 'NR')
```
#### then fix Countsmatrix:
NOTE:
1. From the manuals the countsData must be a numeric matrix
2. It is IMPORTANT to keep the names of the genes in the rownames
```{r}
# Readin countsmatrix
#countsmatrix <-as.matrix(read.csv("~/R/Rtuts/Data/Alina_EPEC_project/counts.csv"))
countsmatrix <- read.csv("~/R/Alina_RNAseq/MyD88_groupedcounts.csv")
#countsmatrix <- as.data.frame(countsmatrix)
nrow(countsmatrix)
colnames(countsmatrix)
```
```{r}
## Removal of Gender Genes from ENSEMBL ID itself
countsmatrix <- countsmatrix %>% filter(countsmatrix$X != "ENSMUSG00000086503",
countsmatrix$X != "ENSMUSG00000097571",
countsmatrix$X != "ENSMUSG00000086370",
countsmatrix$X != "ENSMUSG00000031329")
nrow(countsmatrix)
countsmatrix <- as.matrix(countsmatrix)
```
```{r}
#tidying up these names again
colnames(countsmatrix) <- str_remove_all(colnames(countsmatrix),
pattern = "_001.fastq.gz.Aligned.sortedByCoord.out.bam|_S\\d\\d|x")
```
```{r}
rownames(countsmatrix) <- countsmatrix[,1] #converting first column of gene names into rownames, to be used for sanity check later
# It is IMPORTANT to keep the names of the genes in the rownames
countsmatrix <- subset(countsmatrix, select = - X)#dropping the X column
# the elements from Sample_Name from coldata must the the colnames of countsmatrix
colnames(countsmatrix) <- coldata$Sample_Name
# Display the column names
colnames(countsmatrix)
# Convert the countsmatrix elements to be of numeric type in order to be in correct format to be fed into DESeq2 functions
class(countsmatrix) <- "numeric"
```
# Calculating CPM Values
```{r}
# as DGEList
#dge_er <- DGEList(counts = countsmatrix)
# dim(dge_er)
# colnames(dge_er)
# dge_er$samples
```
```{r}
## calculate norm. factors
# nr <- calcNormFactors(dge_er)
# nr
```
```{r}
## get normalized counts
# cpmvalues <- cpm(nr)
# cpmvalues_d <- cpm.default(nr)
```
```{r}
# Function to save generic plots
saveplot <- function(plot,name ){
# Function to save the plots
ggsave(filename =
glue('~/R/Alina_RNAseq/MyD88_InfectedVsControl/{name}.png'),
plot = plot,
dpi = 300,
width = 10,
height = 10,
units = "in")
}
```
# Differential Gene Expression analysis using DESeq2
Now, construct DESeqDataSet for DGE analysis.
But before that, a sanity check : It is essential to have the name of
the columns in the count matrix in the same order as that in name of the
samples (rownames in coldata).
```{r}
all(rownames(coldata) %in% colnames(countsmatrix))
ncol(countsmatrix) == nrow(coldata)
dim(countsmatrix)
```
## Creating the DESeq Data set Object
```{r}
dds_infectedMyD88 <- DESeqDataSetFromMatrix(countData = countsmatrix,
colData = coldata,
design = ~ condition)
nrow(dds_infectedMyD88)
```
## Exploratory Data Analysis and Visualization
### Pre-filtering the dataset
Our count matrix with our DESeqDataSet contains many rows with only zeros, and additionally many rows with only a few
fragments total. In order to reduce the size of the object, and to increase the speed of our functions, we can remove
the rows that have no or nearly no information about the amount of gene expression.
Applying the most minimal filtering rule: removing rows of the DESeqDataSet that have no counts, or only a single count
across all samples. Additional weighting/filtering to improve power is applied at a later step in the workflow.
```{r}
keep <- rowSums(counts(dds_infectedMyD88)) > 1
dds_infectedMyD88 <- dds_infectedMyD88[keep,]
nrow(dds_infectedMyD88)
```
### The variance stabilizing transformation
## Applying VST transformation
```{r}
vsd <- vst(dds_infectedMyD88, blind = FALSE)
head(assay(vsd), 3)
colData(vsd)
vsd_coldata <- colData(vsd)
```
```{r}
dds_infectedMyD88 <- estimateSizeFactors(dds_infectedMyD88)
print(dds_infectedMyD88)
```
## Sample Distances
useful first step in an RNA-seq analysis is often to assess overall
similarity between samples:
1. Which samples are similar to each other, which are different?
2. Does this fit to the expectation from the experiment's design?
### Euclidean Distance between samples
dist to calculate the Euclidean distance between samples - useful for
ONLY normalized data. To ensure we have a roughly equal contribution
from all genes, we use it on the VST data.
```{r}
sampleDists <- dist(t(assay(vsd)))
head(sampleDists)
```
visualize the distances in a heatmap
```{r}
sampleDistMatrix <- as.matrix( sampleDists )
rownames(sampleDistMatrix) <- vsd$Sample_Name
colnames(sampleDistMatrix) <- vsd$Sample_Name
colors <- colorRampPalette( rev(brewer.pal(9, "RdYlBu")) )(255)
distance_plot <- pheatmap(sampleDistMatrix,
clustering_distance_rows = sampleDists,
clustering_distance_cols = sampleDists,
main = "Sample-to-Sample Euclidean Distance of MyD88 - Infected Vs Control",
col = colors)
distance_plot
```
### Poisson Distance between Samples
```{r}
poisd <- PoissonDistance(t(counts(dds_infectedMyD88))) # raw counts or unnormalised data
samplePoisDistMatrix <- as.matrix( poisd$dd )
rownames(samplePoisDistMatrix) <- dds_infectedMyD88$Sample_Name
colnames(samplePoisDistMatrix) <- dds_infectedMyD88$Sample_Name
colors <- colorRampPalette( rev(brewer.pal(9, "RdYlBu")) )(255)
poisson_dist_plot <- pheatmap(samplePoisDistMatrix,
clustering_distance_rows = poisd$dd,
clustering_distance_cols = poisd$dd,
main = "Sample-to-Sample Poisson Distance of MyD88 - Infected Vs Control",
col = colors)
poisson_dist_plot
```
## PCA Plot
### Calculating all PCA Values
```{r}
# calculate the variance for each gene
rv <- rowVars(assay(vsd))
ntop <- 500
# select the ntop genes by variance
select <- order(rv, decreasing = TRUE)[seq_len(min(ntop, length(rv)))]
# perform a PCA on the data in assay(x) for the selected genes
pca_infectedMyD88 <- prcomp(t(assay(vsd)[select,]))
summary(pca_infectedMyD88)
# the contribution to the total variance for each component
#percentVar_infectedMyD88 <- (pca_infectedMyD88$sdev^2 / sum( pca_infectedMyD88$sdev^2 )) * 100
#percentVar_infectedMyD88
```
### PCA Plot with VST Data
```{r}
percentvar_calculation <- function(pcaData_variable){
# function to calculate percentvar for different variables
percentvar_variable <- round(100 * attr(pcaData_variable, "percentVar"), digits = 3 )
return(percentvar_variable)
}
savingFunction <- function(plotname, metadatacolumn){
# Function to save the PCA plots
ggsave(filename =
glue('~/R/Alina_RNAseq/MyD88_InfectedVsControl/PCAplot_infectedMyD88VsControl_{metadatacolumn}.png'),
plot = plotname,
dpi = 300,
width = 10,
height = 10,
units = "in")
}
```
```{r}
pcaData_infectedMyD88 <- plotPCA(vsd, intgroup = c("condition","Sample_Name"), returnData = TRUE)
head(pcaData_infectedMyD88)
percentVar_infectedMyD88 <- percentvar_calculation(pcaData_infectedMyD88)
```
### Functions for Plot aethetics and saving PCA Plots
```{r}
color_values <- c("blue", "blue","red", "red","red","red", "red","red","black","black")
# the basic set of common aesthetic settings for PCA plots,
theme.my.own <- list(theme_bw() ,
geom_point(size = 3),
coord_fixed() ,
xlab(paste0("PC1: ", percentVar_infectedMyD88[1], "% variance")),
ylab(paste0("PC2: ", percentVar_infectedMyD88[2], "% variance")),
scale_y_continuous(breaks = seq(-10, 10, 5), sec.axis = sec_axis(~. *1)) ,
scale_x_continuous(breaks = seq(-20, 20, 5), sec.axis = sec_axis(~. *1)) ,
theme_classic() ,
geom_hline(yintercept = 0, color = "gray", size = 1) ,
geom_vline(xintercept = 0, color = "gray", size = 1) ,
theme(text = element_text(size = 15),
axis.text = element_text(size = 15),
legend.position = "right",
aspect.ratio = 50/50) ,
#geom_text(size = 4, hjust = 0, vjust = 0)
geom_text_repel(size = 5,min.segment.length = 0.01)
)
```
```{r fig.height=8, fig.width=8}
(PCAplot_vst <- ggplot(pcaData_infectedMyD88,
aes(x = PC1,
y = PC2,
color = Sample_Name,
label = Sample_Name)) +
ggtitle("PCA Plot: MyD88-Infected Vs Control") +
scale_colour_manual(values = color_values) +
theme.my.own )
savingFunction(PCAplot_vst, "Sample_Name")
```
## PCA Plot for different groupings of metadata
### PCA for Epithelial Response
```{r fig.height=8, fig.width=8}
# PCAdata_infectedMyD88_ER <- plotPCA(vsd, intgroup = c("Sample_Name",
# "Epithelial_response"),
# returnData = TRUE)
# percentVar_infectedMyD88 <- percentvar_calculation(PCAdata_infectedMyD88_ER)
#
# (PCAplot_ER <- ggplot(PCAdata_infectedMyD88_ER,
# aes(x = PC1,
# y = PC2,
# color = Epithelial_response,
# label = Sample_Name)) +
# ggtitle("PCA Plot: MYD88-Infected Vs Control - Epithelial_response") +
# theme.my.own )
#
# savingFunction(PCAplot_ER, "Epithelial_response")
```
### PCA for Microcolonies
```{r}
# PCAdata_infectedMyD88_MC <- plotPCA(vsd, intgroup = c("Sample_Name", "microcolonies"), returnData = TRUE)
# #percentVar_infectedMyD88 <- percentvar_calculation(PCAdata_infectedMyD88_ER)
#
# (PCAplot_MC <- ggplot(PCAdata_infectedMyD88_MC,
# aes(x = PC1,
# y = PC2,
# color = microcolonies,
# label = Sample_Name)) +
# ggtitle("PCA Plot: MYD88-Infected Vs Control - Microcolonies") +
# theme.my.own )
#
# savingFunction(PCAplot_MC, "microcolonies")
```
### PCA for Clinical Outcome
```{r}
# PCAdata_infectedMyD88_CO <- plotPCA(vsd, intgroup = c("Sample_Name", "clinical_outcome"), returnData = TRUE)
# #percentVar_infectedMyD88 <- percentvar_calculation(PCAdata_infectedMyD88_ER)
#
# (PCAplot_CO <- ggplot(PCAdata_infectedMyD88_CO,
# aes(x = PC1,
# y = PC2,
# color = clinical_outcome,
# label = Sample_Name)) +
# ggtitle("PCA Plot: MYD88-Infected Vs Control - Clinical Outcome") +
# theme.my.own )
#
# savingFunction(PCAplot_CO, "clinical_outcome")
```
### PCA for ER_Microcolonies
```{r}
# PCAdata_infectedMyD88_ERMC <- plotPCA(vsd, intgroup = c("Sample_Name",
# "ER_microcolonies"),
# returnData = TRUE)
#
# (PCAplot_ERMC <- ggplot(PCAdata_infectedMyD88_ERMC,
# aes(x = PC1,
# y = PC2,
# color = ER_microcolonies,
# label = Sample_Name)) +
# ggtitle("PCA Plot: MYD88-Infected Vs Control - ER_Microcolonies") +
# theme.my.own )
#
# savingFunction(PCAplot_ERMC, "ER_microcolonies")
```
### PCA for Phylogenomic Lineage
```{r}
# PCAdata_infectedMyD88_PL <- plotPCA(vsd, intgroup = c("Sample_Name",
# "phylogenomic_lineage"),
# returnData = TRUE)
# (PCAplot_PL <- ggplot(PCAdata_infectedMyD88_PL,
# aes(x = PC1,
# y = PC2,
# color = phylogenomic_lineage,
# label = Sample_Name)) +
# ggtitle("PCA Plot: MYD88-Infected Vs Control - Phylogenomic Lineage") +
# theme.my.own )
#
# savingFunction(PCAplot_PL, "phylogenomic_lineage")
```
### PCA for Phylogroup
```{r}
# PCAdata_infectedMyD88_PG <- plotPCA(vsd, intgroup = c("Sample_Name",
# "phylogroup"),
# returnData = TRUE)
# (PCAplot_PG <- ggplot(PCAdata_infectedMyD88_PG,
# aes(x = PC1,
# y = PC2,
# color = phylogroup,
# label = Sample_Name)) +
# ggtitle("PCA Plot: MYD88-Infected Vs Control - Phylogroup") +
# theme.my.own )
#
# savingFunction(PCAplot_PG, "phylogroup")
```
### PCA for Intimin Group
```{r}
# PCAdata_infectedMyD88_IT <- plotPCA(vsd, intgroup = c("Sample_Name",
# "Intimin_Type"),
# returnData = TRUE)
# (PCAplot_IT <- ggplot(PCAdata_infectedMyD88_IT,
# aes(x = PC1,
# y = PC2,
# color = Intimin_Type,
# label = Sample_Name)) +
# ggtitle("PCA Plot: MYD88-Infected Vs Control - Intimin Type") +
# theme.my.own )
#
# savingFunction(PCAplot_IT, "Intimin_Type")
```
## Hierarchical Clustering
### applying rlog Transformation
```{r}
rld <- rlog(dds_infectedMyD88, blind = FALSE)
head(assay(rld), 3)
### Extract the rlog matrix from the object
rld_mat <- assay(rld) #assay() is function from the "SummarizedExperiment" package that was loaded when you loaded DESeq2
```
```{r}
### Compute pairwise correlation values
rld_cor <- cor(rld_mat) ## cor() is a base R function
head(rld_cor) ## check the output of cor(), make note of the rownames and colnames
```
```{r}
### Plot heatmap
heat.colors <- brewer.pal(6, "RdYlBu")
Hclust_plot <- pheatmap(rld_cor,
color = heat.colors,
main = 'Heirarchical Clustering of Samples - MyD88_InfectedVsControl - Correlation Matrix'
#filename = '~/R/Alina_RNAseq/MyD88_InfectedVsControl/Hclust_plot.tiff'
)
Hclust_plot
```
## DGE Results
### Running the differential expression pipeline
```{r}
dds1_infectedMyD88 <- DESeq(dds_infectedMyD88)
#str(dds1)
```
### Building the results table
```{r}
res_infectedMyD88 <- results(dds1_infectedMyD88)
head(res_infectedMyD88, 30)
```
```{r}
summary(res_infectedMyD88)
```
A Problem: there a number of rows with padj that has NA.
Reaons:
1. If within a row, all samples have zero counts, the baseMean column
will be zero, and the log2 fold change estimates, p value and
adjusted p value will all be set to NA. ------\>\>\>\>\>\> checked
and not true!
2. If a row contains a sample with an extreme count outlier then the p
value and adjusted p value will be set to NA. These outlier counts
are detected by Cook's distance. Customization of this outlier
filtering and description of functionality for replacement of
outlier counts and refitting is described below. ------\>\>\>\>\>\>
checked and not true!
3. If a row is filtered by automatic independent filtering, for having
a low mean normalized count, then only the adjusted p value will be
set to NA. Description and customization of independent filtering is
described below. ------\>\>\>\>\>\> could be true!
=====\>\>\>\>\>\> Most likely caused by automatic independent filtering
due to the presence of low mean normalized counts.
Solution: Obtain unfiltered DESeq2 results! (counts without automatic
independent filtering and outlier removal). Then, only the genes with
ALL counts set to zero will have NA for pvalues.
```{r}
dds2_infectedMyD88 <- DESeq(dds_infectedMyD88, minReplicatesForReplace = Inf)
res2_infectedMyD88 <- results(dds2_infectedMyD88,
cooksCutoff = FALSE,
independentFiltering = FALSE)
```
```{r}
head(res2_infectedMyD88, 30)
```
### Results with thresholds
Both thresholds Applied!
```{r}
res2df_infectedMyD88 <- as.data.frame(res2_infectedMyD88) # convert the results table to a df
head(res2df_infectedMyD88)
```
## MA Plot
```{r}
resultsNames(dds2_infectedMyD88)
```
```{r}
plotMA_res2_infectedMyD88 <- plotMA(res2_infectedMyD88, ylim = c(-2, 2))
```
```{r}
# res3_infectedMyD88 <- lfcShrink(dds2_infectedMyD88,
# coef = "condition_infectedMyD88_vs_control",
# type = "apeglm")
# plotMA_res3 <- plotMA(res3_infectedMyD88, ylim = c(-2, 2))
```
### Histogram of p-values
```{r}
hist(res2_infectedMyD88$pvalue, breaks = 50, col = "grey50", border = "blue")
```
Further Filtering: baseMean \> 1
```{r}
hist(res2_infectedMyD88$pvalue[res2_infectedMyD88$baseMean > 50], breaks = 50, col = "grey50", border = "blue")
```
## Annotating and Exporting Results
- adding gene annotation to results table
- adding ENTREZ Id to results table
```{r}
res2df_infectedMyD88$symbol <- mapIds(org.Mm.eg.db,
keys = rownames(dds_infectedMyD88),
column = "SYMBOL",
keytype = "ENSEMBL",
multiVals = "first")
res2df_infectedMyD88$entrez <- mapIds(org.Mm.eg.db,
keys = rownames(dds_infectedMyD88),
column = "ENTREZID",
keytype = "ENSEMBL",
multiVals = "first")
```
```{r}
head(res2df_infectedMyD88)
```
```{r}
str(res2df_infectedMyD88)
nrow(res2df_infectedMyD88)
```
Omit NA values from symbol and respective rows!
```{r}
res3df_infectedMyD88 <- res2df_infectedMyD88 %>% filter(!is.na(symbol) & !is.na(entrez))
```
```{r}
nrow(res3df_infectedMyD88)
```
## Saving the Results
```{r}
resOrdered_infectedMyD88 <- res3df_infectedMyD88[order(res3df_infectedMyD88$pvalue),]
head(resOrdered_infectedMyD88)
```
```{r}
write.csv(as.data.frame(resOrdered_infectedMyD88),
file = "~/R/Alina_RNAseq/MyD88_InfectedVsControl/results_DGE_infectedMyD88VsControl.csv")
```
## Heatmap of count matrix
To explore a count matrix, it is often instructive to look at it as a heatmap.
```{r}
select <- order(rowMeans(counts(dds2_infectedMyD88,normalized = TRUE)),
decreasing = TRUE)[1:20]
df <- as.data.frame(colData(dds2_infectedMyD88)[,c("condition","Sample_Name")])
```
```{r}
pheatmap(assay(vsd)[select,],
cluster_cols = TRUE,
annotation_col = df,
color = heat.colors,
show_rownames = FALSE)
```
## Effect of Transformations on Variance
- These set of plots depict the standard deviation of transformed data (across samples), against mean.
### Based on Shifted Log Transformation
```{r}
ntd <- normTransform(dds2_infectedMyD88)
meanSdPlot(assay(ntd))
```
### Based on Rlog Transformation
```{r}
meanSdPlot(assay(rld))
```
### Based on Variance Stabilizing Transformation
```{r}
meanSdPlot(assay(vsd))
```
## Dispersion Plots
- Its a useful diagnostic to plot the dispersion estimates.
```{r}
plotDispEsts(dds2_infectedMyD88)
```
# Volcano Plots
## Volcano Plots based on Enhanced Volcano
```{r}
p1 <- EnhancedVolcano(res3df_infectedMyD88,
lab = res3df_infectedMyD88$symbol,
x = 'log2FoldChange',
y = 'pvalue',
xlab = bquote(~Log[2]~'FoldChange'),
pCutoff = 0.05,
FCcutoff = 1.0,
title = 'Volcano Plot for DE genes: Log2FoldChange Vs -Log10 Pvalue',
pointSize = 2.0,
labSize = 5.0,
boxedLabels = FALSE,
gridlines.major = FALSE,
gridlines.minor = FALSE,
colAlpha = 0.5,
xlim = c(-6, 9),
ylim = c(-2, 12),
legendPosition = 'bottom',
legendLabSize = 12,
legendIconSize = 4.0,
drawConnectors = TRUE,
widthConnectors = 0.75
)
(volcano1 <- p1 + scale_y_continuous(limits = c(0, 6),
breaks = seq(0, 6, 1),
sec.axis = sec_axis(~. *1,
labels = NULL,
breaks = NULL)) +
scale_x_continuous(limits = c(-4, 6),
breaks = seq(-4, 6, 1),
sec.axis = sec_axis(~. *1,
labels = NULL,
breaks = NULL)))
ggsave(filename = '~/R/Alina_RNAseq/MyD88_InfectedVsControl/Volcano2_L2fcVsPvalue.png',
plot = volcano1,
dpi = 300,
width = 10,
height = 10,
units = "in")
```
## volcano Plot of Log2FC vs Padj
```{r}
p2 <- EnhancedVolcano(res3df_infectedMyD88,
lab = res3df_infectedMyD88$symbol,
x = 'log2FoldChange',
y = 'padj',
xlab = bquote(~Log[2]~'FoldChange'),
ylab = bquote(~Log[10]~'padj'),
pCutoff = 0.05,
FCcutoff = 1.0,
title = 'Volcano Plot for DE genes: Log2FoldChange Vs -Log10 Padj',
pointSize = 2.0,
labSize = 5.0,
boxedLabels = FALSE,
gridlines.major = FALSE,
gridlines.minor = FALSE,
colAlpha = 0.5,
xlim = c(-6, 9),
ylim = c(-2, 12),
legendPosition = 'bottom',
legendLabSize = 12,
legendIconSize = 4.0,
drawConnectors = TRUE,
widthConnectors = 0.75
)
(volcano2 <- p2 + scale_y_continuous(limits = c(0, 6),
breaks = seq(0, 6, 1),
sec.axis = sec_axis(~. *1,
labels = NULL,
breaks = NULL)) +
scale_x_continuous(limits = c(-4, 6),
breaks = seq(-4, 6, 1),
sec.axis = sec_axis(~. *1,
labels = NULL,
breaks = NULL))
)
ggsave(filename = '~/R/Alina_RNAseq/MyD88_InfectedVsControl/Volcano2_L2fcVsPadj.png',
plot = volcano2,
dpi = 300,
width = 10,
height = 10,
units = "in")
```
## Significant Differentially Expressed Genes
```{r}
res3df_infectedMyD88$diffexpressed <- "NS"
# if log2Foldchange > 1.0 and pvalue < 0.05, set as "UP"
res3df_infectedMyD88$diffexpressed[res3df_infectedMyD88$log2FoldChange > 1.0 & res3df_infectedMyD88$pvalue < 0.05] <- "UP"
# if log2Foldchange < 1.0 and pvalue < 0.05, set as "UP"
res3df_infectedMyD88$diffexpressed[res3df_infectedMyD88$log2FoldChange < -1.0 & res3df_infectedMyD88$pvalue < 0.05] <- "DOWN"
# Create a new column "delabel" to de, that will contain the name of genes differentially expressed (NA in case they are not)
res3df_infectedMyD88$delabel <- NA
res3df_infectedMyD88$delabel[res3df_infectedMyD88$diffexpressed != "NS"] <- res3df_infectedMyD88$symbol[res3df_infectedMyD88$diffexpressed != "NS"]
```
```{r}
#ggplot(res3.df, aes(x=log2FoldChange , y = log10(baseMean),
# col=diffexpressed, label=delabel)
# ) +
# geom_point() +
# scale_color_manual(values = mycolors)+
# geom_text_repel()
```
Arrive at relevant genes by imposing thresholds.
```{r}
sigsdf_infectedMyD88 <- res3df_infectedMyD88[(abs(res3df_infectedMyD88$log2FoldChange) > 1 ) &
(res3df_infectedMyD88$pvalue < 0.05),]
```
```{r}
nrow(sigsdf_infectedMyD88)
```
## Log2FC Table
```{r}
summary(res3df_infectedMyD88)
```
### Gender Genes Removed from Table
```{r}
sigsdf_infectedMyD88 <- filter(sigsdf_infectedMyD88,
symbol != "Xist",
symbol != "Jpx",
symbol != "Ftx",
symbol != "Tsx",
symbol != "Cnbp2" )
```
```{r}
nrow(sigsdf_infectedMyD88)
#head(sigsdf_infectedMyD88)
```
Therefore, gender genes werent so much in play in terms of significance!
```{r}
write.csv(sigsdf_infectedMyD88 ,"~/R/Alina_RNAseq/MyD88_InfectedVsControl/SignificantGenes_infectedMyD88VsControl.csv")
```
### Number of Genes from different strains that are contributing to UP/DOWN regulation.
```{r}
sigsdf_infectedMyD88_UP <- sigsdf_infectedMyD88[(sigsdf_infectedMyD88$log2FoldChange) > 1, ] #UP Regulation Table
sigsdf_infectedMyD88_DOWN <- sigsdf_infectedMyD88[(sigsdf_infectedMyD88$log2FoldChange) < -1, ] #DOWN Regulation Table
```
```{r}
nrow(sigsdf_infectedMyD88_UP)
nrow(sigsdf_infectedMyD88_DOWN)
```
### Determine Top20 UP Genes and Top20 DOWN Genes
```{r}
UpGene_infectedMyD88 <- sigsdf_infectedMyD88_UP[order(-sigsdf_infectedMyD88_UP$log2FoldChange), ]$symbol
DownGene_infectedMyD88 <- sigsdf_infectedMyD88_DOWN[order(-sigsdf_infectedMyD88_DOWN$log2FoldChange), ]$symbol
DE_Genes_table_infectedMyD88 <- as.data.frame(cbind(UpGene_infectedMyD88,DownGene_infectedMyD88)) # sorted based on highest Log2FC value
```
```{r}
write.csv(DE_Genes_table_infectedMyD88 ,"~/R/Alina_RNAseq/MyD88_InfectedVsControl/DE_GenesTable_infectedMyD88VsControl.csv")
head(DE_Genes_table_infectedMyD88, 20)
```
## Z-score based Gene Heatmaps
```{r}
head(sigsdf_infectedMyD88)
```
### with Whole table (all genes together!)
```{r}
mat <- counts(dds2_infectedMyD88, normalized = TRUE)[rownames(sigsdf_infectedMyD88),]
mat.zs <- t(apply(mat, 1, scale)) # Calculating the zscore for each row
colnames(mat.zs) <- coldata$Sample_Name # need to provide correct sample names for each of the columns
head(mat.zs)
```
```{r}
Heatmap_ALL_DEGene <- pheatmap(mat.zs,
cluster_cols = TRUE,
cluster_rows = FALSE,
show_rownames = FALSE)
Heatmap_ALL_DEGene
```
```{r}
sigs2df_infectedMyD88 <- res2df_infectedMyD88[(abs(res2df_infectedMyD88$log2FoldChange) > 1) &
(res2df_infectedMyD88$pvalue < 0.05),]
mat2 <- counts(dds2_infectedMyD88, normalized = TRUE)[rownames(sigs2df_infectedMyD88),]
mat2.zs <- t(apply(mat2, 1, scale)) # Calculating the zscore for each row
colnames(mat2.zs) <- coldata$Sample_Name # need to provide correct sample names for each of the columns
head(mat2.zs)
#pheatmap(mat2.zs, cluster_cols = TRUE, cluster_rows = FALSE, show_rownames = FALSE)
```
```{r fig.height= 15, fig.width= 15}
newHP <- Heatmap(mat2.zs,
cluster_columns = TRUE,
cluster_rows = TRUE,
column_labels = colnames(mat2.zs),
name = 'Z-Score Heatmap of DE Genes',
show_row_names = FALSE,
use_raster = TRUE,
raster_quality = 5,
#width = unit(8, "in"),
#height = unit(8, "in")
row_labels = sigs2df_infectedMyD88[rownames(mat2.zs),]$symbol
)
newHP
```
```{r fig.height= 40, fig.width= 10}
LongHeatMap_Allgenes <- Heatmap(mat2.zs,
cluster_columns = TRUE,
cluster_rows = TRUE,
column_labels = colnames(mat2.zs),
row_labels = sigs2df_infectedMyD88[rownames(mat2.zs),]$symbol,
name = 'Z-Score Heatmap of DE Genes - Infected Vs Control',
show_row_names = TRUE,
use_raster = TRUE,
raster_quality = 5,
)
LongHeatMap_Allgenes
```
------------------------------------------------------------------------
# GO Terms with clusterProfiler
## GO Terms for UP Regulated Genes
### GO over-representation analysis for UP Regulated Genes