-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThesis_HNSCC.Rmd
More file actions
1244 lines (976 loc) · 40.4 KB
/
Thesis_HNSCC.Rmd
File metadata and controls
1244 lines (976 loc) · 40.4 KB
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
Set Seed and load the data directory where the file is saved
```{r}
set.seed(2023)
# set up a path to the data directory
dataDirectory <- "~/Downloads/idat files 19/Thesis-idat"
# list the files
list.files(dataDirectory, recursive = TRUE)
```
Load library of packages that are required
```{r}
library(knitr)
library(limma)
library(minfi)
library(IlluminaHumanMethylation450kanno.ilmn12.hg19)
library(IlluminaHumanMethylation450kmanifest)
library(RColorBrewer)
library(missMethyl)
library(minfiData)
library(Gviz)
library(DMRcate)
library(stringr)
library(GenomicFeatures)
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
library(maxprobes)
library(GenomicRanges)
library(ggplot2)
library(missMethyl)
library(heatmaply)
library(RColorBrewer)
```
Annotation data for the Illumina Human Methylation 450k BeadChip, which is a microarray platform commonly used for DNA methylation analysis. The annotation data typically includes information about the genomic locations, gene names, and other relevant details associated with the probes on the BeadChip.
```{r}
# get the 450k annotation data
ann450k <- getAnnotation(IlluminaHumanMethylation450kanno.ilmn12.hg19)
head(ann450k)
```
Reading in the Sample sheet which has paramters regarding the samples like Sample Number, sample subtype etc.
```{r}
# read in the sample sheet for the experiment
targets <- read.metharray.sheet(dataDirectory, pattern="GB160121-KP Sample Sheet 450k_alltissues copy 2.csv")
```
Read and load DNA methylation data from an Illumina methylation array experiment called rgSet. The function read.metharray.exp() takes the targets file as input and processes it to extract the necessary information and load the methylation data into rgSet.
```{r}
# read in the raw data from the IDAT files
rgSet <- read.metharray.exp(targets=targets)
rgSet
```
Giving descriptive names to the samples
```{r}
# give the samples descriptive names
targets$ID <- paste(targets$Sample_Name,targets$Sample_Group,sep=".")
sampleNames(rgSet) <- targets$ID
rgSet
```
Calculating detection p values: It is idicative of quality of signal. If the p values are small, then it is indicative of reliable signal. If the p values are larger than 0.01 then it indicates poor quality of signal
```{r}
# calculate the detection p-values
detP <- detectionP(rgSet)
head(detP)
```
Visual analysis of detetction p values to filter out values that are above the treshold, helps visualise the genral quality of the samples in terms of the signal.
```{r}
# examine mean detection p-values across all samples to identify any failed samples
pal <- brewer.pal(8,"Dark2")
par(mfrow=c(1,2))
barplot(colMeans(detP), col=pal[factor(targets$Sample_Group)], las=2,
cex.names=0.8, ylab="Mean detection p-values")
abline(h=0.005,col="red")
legend("topleft", legend=levels(factor(targets$Sample_Group)), fill=pal,
bg="white",cex=0.5)
barplot(colMeans(detP), col=pal[factor(targets$Sample_Group)], las=2,
cex.names=0.8, ylim=c(0,0.002), ylab="Mean detection p-values")
abline(h=0.005,col="red")
legend("topleft", legend=levels(factor(targets$Sample_Group)), cex=0.5,fill=pal)
```
Writing a QC report and storing it as a PDDF document
```{r}
qcReport(rgSet, sampNames=targets$ID, sampGroups=targets$Sample_Group,
pdf="qcReport.pdf")
```
Filtering out poor quality samples and keeping samples that have detection p <0.05
```{r}
# remove poor quality samples
keep <- colMeans(detP) < 0.05
rgSet <- rgSet[,keep]
rgSet
```
Updating the smaple sheet to have only those samples that satisfy the treshold (p<0.05)
```{r}
# remove poor quality samples from targets data
targets <- targets[keep,]
targets[,1:5]
```
Filter out detection p values table for only those that are present in the sample sheet (i.e only those that have satisfied the treshold of p<0.05)
```{r}
# remove poor quality samples from detection p-value table
detP <- detP[,keep]
dim(detP)
```
We now have 485512 probes and 24 samples
Normlaise the data using Fucntional Normalisation
```{r}
# normalize the data; this results in a GenomicRatioSet object
mSetSq <- preprocessFunnorm(rgSet)
```
```{r}
# create a MethylSet object from the raw data for plotting
mSetRaw <- preprocessRaw(rgSet)
```
```{r}
# visualise what the data looks like before and after normalisation
par(mfrow=c(1,2))
densityPlot(rgSet, sampGroups=targets$Sample_Group,main="Raw", legend=FALSE)
legend("top", legend = levels(factor(targets$Sample_Group)),
text.col=brewer.pal(8,"Dark2"))
densityPlot(getBeta(mSetSq), sampGroups=targets$Sample_Group,
main="Normalized", legend=FALSE)
legend("top", legend = levels(factor(targets$Sample_Group)),
text.col=brewer.pal(8,"Dark2"))
```
PCA to see how the samples cluster based on sample group
```{r}
# MDS plots to look at largest sources of variation
plotMDS(getM(mSetSq), top=1000, gene.selection="common", cex =1,
col=pal[factor(targets$Sample_Group)],labels = targets$Sample_Name)
legend("bottomright", legend=levels(factor(targets$Sample_Group)), text.col=pal,
bg="white", cex=0.5)
```
```{r}
# Examine higher dimensions to look at other sources of variation
par(mfrow=c(1,3))
plotMDS(getM(mSetSq), top=1000, gene.selection="common",
col=pal[factor(targets$Sample_Group)], dim=c(1,3))
legend("top", legend=levels(factor(targets$Sample_Group)), text.col=pal,
cex=0.7, bg="white")
plotMDS(getM(mSetSq), top=1000, gene.selection="common",
col=pal[factor(targets$Sample_Group)], dim=c(2,3))
legend("topleft", legend=levels(factor(targets$Sample_Group)), text.col=pal,
cex=0.7, bg="white")
plotMDS(getM(mSetSq), top=1000, gene.selection="common",
col=pal[factor(targets$Sample_Group)], dim=c(3,4))
legend("topright", legend=levels(factor(targets$Sample_Group)), text.col=pal,
cex=0.5, bg="white")
```
```{r}
# ensure probes are in the same order in the mSetSq and detP objects
detP <- detP[match(featureNames(mSetSq),rownames(detP)),]
# remove any probes that have failed in one or more samples
keep <- rowSums(detP < 0.01) == ncol(mSetSq)
table(keep)
```
```{r}
mSetSqFlt <- mSetSq[keep,]
mSetSqFlt
```
```{r}
# if your data includes males and females, remove probes on the sex chromosomes
keep <- !(featureNames(mSetSqFlt) %in% ann450k$Name[ann450k$chr %in%
c("chrX","chrY")])
table(keep)
mSetSqFlt <- mSetSqFlt[keep,]
# remove probes with SNPs at CpG site
mSetSqFlt <- dropLociWithSnps(mSetSqFlt)
mSetSqFlt
```
Remove cross reactive probes: These are probes that bind to multiple regions in the genome and so not have unique binding sites. These are to be removed since their signal creates bias. The file containing the cross reactive probes was taken from : https://github.com/IARCbioinfo/Methylation_analysis_scripts/blob/master/48639-non-specific-probes-Illumina450k.csv
```{r}
# exclude cross reactive probes
xReactiveProbes <- read.csv(file=paste(dataDirectory,"48639-non-specific-probes-Illumina450k.csv",sep="/"), stringsAsFactors=FALSE)
keep <- !(featureNames(mSetSqFlt) %in% xReactiveProbes$TargetID)
table(keep)
mSetSqFlt <- mSetSqFlt[keep,]
mSetSqFlt
```
```{r}
#Revisiting MDS post normalisation and filtering
plotMDS(getM(mSetSqFlt), top=1000, gene.selection="common",
col=pal[factor(targets$Sample_Group)], cex=0.75)
legend("topright", legend=levels(factor(targets$Sample_Group)), text.col=pal,
cex=0.65, bg="white",)
```
```{r}
# calculate M-values for statistical analysis
mVals <- getM(mSetSqFlt)
head(mVals[,1:5])
```
```{r}
bVals <- getBeta(mSetSqFlt)
head(bVals[,1:5])
```
Correlation matrix for beta values
```{r}
corr_mtx<-cor(bVals ,method = "pearson")
corr_mtx
library(viridis)
heatmap_res<-heatmaply(corr_mtx,
colors = viridis(100),
main = "Correlation Heatmap")
heatmap_res
```
```{r}
PCA_Bval<-prcomp(bVals[])
plot(PCA_Bval$x[,1],PCA_Bval$x[,2])
```
```{r}
par(mfrow=c(1,2))
densityPlot(bVals, sampGroups=targets$Sample_Group, main="Beta values",
legend=FALSE, xlab="Beta values")
legend("top", legend = levels(factor(targets$Sample_Group)),
text.col=brewer.pal(8,"Dark2"),cex=0.5)
densityPlot(mVals, sampGroups=targets$Sample_Group, main="M-values",
legend=FALSE, xlab="M values")
legend("top", legend = levels(factor(targets$Sample_Group)),
text.col=brewer.pal(8,"Dark2"),cex=0.5)
```
----------------------------------------------Margin-Normal------------------------------------------------------------
We begin with analysing the deferentially methylated probes and regions between marginal tumour tissues and normal tissues. Here it is Margin-Normal, all the results produces are wrt Margin in comparison
```{r}
# this is the factor of interest
cellType <- factor(targets$Sample_Group)
# use the above to create a design matrix
design <- model.matrix(~0+cellType, data=targets)
colnames(design) <- levels(cellType)
# fit the linear model
fit <- lmFit(mVals, design)
contMatrix <- makeContrasts(Margin-Normal,
levels=design)
contMatrix
```
```{r}
# fit the contrasts
fit2 <- contrasts.fit(fit, contMatrix)
fit2 <- eBayes(fit2)
# look at the numbers of DM CpGs at FDR < 0.05
summary(decideTests(fit2))
```
```{r}
# get the table of results for the first contrast (naive - rTreg)
ann450kSub <- ann450k[match(rownames(mVals),ann450k$Name),
c(1:4,12:19,24:ncol(ann450k))]
DMPs <- topTable(fit2, num=Inf, coef=1, genelist=ann450kSub)
head(DMPs)
DMP_ord<-DMPs[order(DMPs$t),]
DMP_ord<-subset.data.frame(DMP_ord,DMP_ord$P.Value<0.05)
tail(DMP_ord)
head(DMP_ord)
DMP_probes<-DMP_ord$Name
mvals_dmp<-mVals[rownames(mVals) %in% DMP_probes, ]
```
```{r}
write.table(DMP_ord, file="DMPs.csv", sep=",", row.names=FALSE)
```
```{r}
# plot the top 4 most significantly differential methylated CpGs
par(mfrow=c(2,2))
sapply(rownames(DMP_ord)[1:4], function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
dim(DMP_ord)
```
```{r}
#Plot the bottom 4 differentially methylated CpGs
par(mfrow=c(2,2))
sapply(tail(rownames(DMP_ord)), function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
```
```{r}
myAnnotation <- cpg.annotate(object = mVals, datatype = "array", what = "M",
analysis.type = "differential", design = design,
contrasts = TRUE, cont.matrix = contMatrix,
coef = colnames(contMatrix), arraytype = "450K")
```
```{r}
str(myAnnotation)
```
```{r}
#identify differentially methylated regions
DMRs <- dmrcate(myAnnotation, lambda=1000, C=2)
results.ranges <- extractRanges(DMRs)
head(results.ranges,6)
results.ranges
```
```{r}
# set up the grouping variables and colours
groups <- pal[1:length(unique(targets$Sample_Group))]
names(groups) <- levels(factor(targets$Sample_Group))
cols <- groups[as.character(factor(targets$Sample_Group))]
cols
```
```{r}
# draw the plot for the top DMR
par(mfrow=c(1,1))
DMR.plot(ranges = results.ranges, dmr = 2, CpGs = bVals, phen.col = cols,
what = "Beta", arraytype = "450K", genome = "hg19")
```
Visualizing the distribution of DMR across chromosomes and the distribution of length of the ranges
```{r}
range_DMR<-ranges(results.ranges)
range_DMR<-data.frame(range_DMR)
ggplot(range_DMR, aes(x=width)) + geom_histogram(color="darkblue", fill="lightblue",binwidth =100)
chr_names<-table(seqnames(results.ranges))
dmr_chr_countdf<-data.frame(chromosome=names(chr_names),count=as.numeric(chr_names))
ggplot(dmr_chr_countdf, aes(x=chromosome,y=count)) + geom_bar(stat="identity",fill="red",width=0.5 )+labs(x = "Chromosome", y = "DMR Count", title = "DMR Counts per Chromosome")+ theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
```{r}
DMP_Data<-data.frame(Probe.ID<-DMP_ord$Name, Chromosome=DMP_ord$chr, Position=DMP_ord$pos, p_value=DMP_ord$P.Value)
DMR_Data<-data.frame(DMR_ID=range_DMR)
```
GSEA- Gene Set Enrichment Analysis.
This is performed to identify the gene sets enriched in Margin as compared to Normal tissues in the given samples
```{r}
gst.region <- goregion(results.ranges, all.cpg=rownames(mVals),
collection="GO", array.type="450K", plot.bias=TRUE,sig.genes = TRUE)
```
We extract the top 10 gene sets that are statistically significant Gene sets in Margin as compared to normal
```{r}
Margin_Normal_topGSA<-topGSA(gst.region, n=10)
Margin_Normal_topGSA
ggplot(as.data.frame(Margin_Normal_topGSA), aes(x = reorder(TERM, P.DE), y =P.DE)) +
geom_point(aes(color = P.DE), size = 10) +
labs(title = "Significantly Enriched Gene Sets in Margin- Normal",
x = "Gene Set",
y = "p-value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 15))
```
```{r}
gst.region.kegg <- goregion(results.ranges, all.cpg=rownames(mVals),
collection="KEGG", array.type="450K",sig.genes = TRUE)
top_20_pathways<-topGSA(gst.region.kegg, n=10)
top_20_pathways
ggplot(as.data.frame(top_20_pathways), aes(x = reorder(Description, P.DE), y =P.DE)) +
geom_point(aes(color = P.DE), size = 10) +
labs(title = "Significantly Enriched Pathways in Margin- Normal",
x = "Gene Set",
y = "p-value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 15))
```
-------------------------------------------------------------PlasmaDNA-Normal----------------------------------------------------------------------
```{r}
contMatrix_ct_normal<- makeContrasts(Plasma_DNA-Normal,
levels=design)
contMatrix_ct_normal
```
```{r}
# fit the contrasts
fit2_ct_normal <- contrasts.fit(fit, contMatrix_ct_normal)
fit2_ct_normal <- eBayes(fit2_ct_normal)
```
```{r}
# look at the numbers of DM CpGs at FDR < 0.05
summary(decideTests(fit2_ct_normal))
```
```{r}
# get the table of results for the first contrast (naive - rTreg)
ann450kSub_ct_normal <- ann450k[match(rownames(mVals),ann450k$Name),
c(1:4,12:19,24:ncol(ann450k))]
DMPs_ct_normal <- topTable(fit2_ct_normal, num=Inf, coef=1, genelist=ann450kSub_ct_normal)
DMP_ord_ct_normal<-DMPs_ct_normal[order(DMPs_ct_normal$t),]
DMP_ord_ct_normal<-subset.data.frame(DMP_ord_ct_normal,DMP_ord_ct_normal$P.Value<0.05)
tail(DMPs_ct_normal)
head(DMPs_ct_normal)
dim(DMP_ord_ct_normal)
```
```{r}
write.table(DMP_ord_ct_normal, file="DMPs_ct_normal.csv", sep=",", row.names=FALSE)
```
```{r}
# plot the top 4 most significantly differentially methylated CpGs
par(mfrow=c(2,2))
sapply(rownames(DMP_ord_ct_normal)[1:4], function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
dim(DMP_ord_ct_normal)
```
```{r}
#Plot the bottom 4 differentially methylated CpGs
par(mfrow=c(2,2))
sapply(tail(rownames(DMP_ord_ct_normal,4)), function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
tail(DMP_ord_ct_normal,4)
```
```{r}
myAnnotation_ct_normal <- cpg.annotate(object = mVals, datatype = "array", what = "M",
analysis.type = "differential", design = design,
contrasts = TRUE, cont.matrix = contMatrix_ct_normal,
coef = colnames(contMatrix_ct_normal), arraytype = "450K" )
```
```{r}
str(myAnnotation_ct_normal)
```
```{r}
#identify differential methylated regions
DMRs_ct_normal <- dmrcate(myAnnotation_ct_normal, lambda=1000, C=2)
results.ranges_ct_normal <- extractRanges(DMRs_ct_normal)
head(results.ranges_ct_normal,6)
results.ranges_ct_normal
```
```{r}
# set up the grouping variables and colours
groups <- pal[1:length(unique(targets$Sample_Group))]
names(groups) <- levels(factor(targets$Sample_Group))
cols <- groups[as.character(factor(targets$Sample_Group))]
cols
```
```{r}
# draw the plot for the top DMR
par(mfrow=c(1,1))
DMR.plot(ranges = results.ranges_ct_normal, dmr = 2, CpGs = bVals, phen.col = cols,
what = "Beta", arraytype = "450K", genome = "hg19")
```
```{r}
library(GenomicRanges)
library(ggplot2)
range_DMR_ct_normal<-ranges(results.ranges_ct_normal)
range_DMR_ct_normal<-data.frame(range_DMR_ct_normal)
ggplot(range_DMR_ct_normal, aes(x=width)) + geom_histogram(color="darkblue", fill="lightblue",binwidth =100)
chr_names_ct_normal<-table(seqnames(results.ranges_ct_normal))
dmr_chr_countdf_ct_normal<-data.frame(chromosome=names(chr_names_ct_normal),count=as.numeric(chr_names_ct_normal))
ggplot(dmr_chr_countdf_ct_normal, aes(x=chromosome,y=count)) + geom_bar(stat="identity",fill="red",width=0.5 )+labs(x = "Chromosome", y = "DMR Count", title = "DMR Counts per Chromosome")+ theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
GSEA
```{r}
library(missMethyl)
gst.region_ct_normal <- goregion(results.ranges_ct_normal, all.cpg=rownames(mVals),
collection="GO", array.type="450K", plot.bias=TRUE,sig.genes = TRUE)
```
```{r}
topGSA(gst.region_ct_normal, n=10)
```
```{r}
gst.region.kegg_ct_normal <- goregion(results.ranges_ct_normal, all.cpg=rownames(mVals),
collection="KEGG", array.type="450K",sig.genes = TRUE)
top_20_pathways_ct_normal<-topGSA(gst.region.kegg_ct_normal, n=10)
top_20_pathways_ct_normal
```
----------------------------------------------------------------------------------Core-Normal------------------------------------------------------------------------
```{r}
contMatrix_core_normal<- makeContrasts(Core-Normal,
levels=design)
contMatrix_core_normal
```
```{r}
# fit the contrasts
fit2_core_normal <- contrasts.fit(fit, contMatrix_core_normal)
fit2_core_normal <- eBayes(fit2_core_normal)
```
```{r}
# look at the numbers of DM CpGs at FDR < 0.05
summary(decideTests(fit2_core_normal))
```
```{r}
# get the table of results for the first contrast (naive - rTreg)
ann450kSub_core_normal <- ann450k[match(rownames(mVals),ann450k$Name),
c(1:4,12:19,24:ncol(ann450k))]
DMPs_core_normal <- topTable(fit2_core_normal, num=Inf, coef=1, genelist=ann450kSub_core_normal)
DMP_ord_core_normal<-DMPs_core_normal[order(DMPs_core_normal$t),]
DMP_ord_core_normal<-subset.data.frame(DMP_ord_core_normal,DMP_ord_core_normal$P.Value<0.05)
tail(DMPs_core_normal)
head(DMPs_core_normal)
```
```{r}
write.table(DMP_ord_core_normal, file="DMPs_core_normal.csv", sep=",", row.names=FALSE)
```
```{r}
# plot the top 4 most significantly differentially methylated CpGs
par(mfrow=c(2,2))
sapply(rownames(DMP_ord_core_normal)[1:4], function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
dim(DMP_ord_core_normal)
```
```{r}
#Plot the bottom 4 differentially methylated CpGs
par(mfrow=c(2,2))
sapply(tail(rownames(DMP_ord_core_normal,4)), function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
tail(DMP_ord_core_normal,4)
```
```{r}
myAnnotation_core_normal <- cpg.annotate(object = mVals, datatype = "array", what = "M",
analysis.type = "differential", design = design,
contrasts = TRUE, cont.matrix = contMatrix_core_normal,
coef = colnames(contMatrix_core_normal), arraytype = "450K" )
```
```{r}
str(myAnnotation_core_normal)
```
```{r}
#identify differential methylated regions
DMRs_core_normal <- dmrcate(myAnnotation_core_normal, lambda=1000, C=2)
results.ranges_core_normal <- extractRanges(DMRs_core_normal)
head(results.ranges_core_normal,6)
results.ranges_core_normal
```
```{r}
# set up the grouping variables and colours
groups <- pal[1:length(unique(targets$Sample_Group))]
names(groups) <- levels(factor(targets$Sample_Group))
cols <- groups[as.character(factor(targets$Sample_Group))]
cols
```
```{r}
# draw the plot for the top DMR
par(mfrow=c(1,1))
DMR.plot(ranges = results.ranges_core_normal, dmr = 4, CpGs = bVals, phen.col = cols,
what = "Beta", arraytype = "450K", genome = "hg19")
```
```{r}
library(GenomicRanges)
library(ggplot2)
range_DMR_core_normal<-ranges(results.ranges_core_normal)
range_DMR_core_normal<-data.frame(range_DMR_core_normal)
ggplot(range_DMR_core_normal, aes(x=width)) + geom_histogram(color="darkblue", fill="lightblue",binwidth =100)
chr_names_core_normal<-table(seqnames(results.ranges_core_normal))
dmr_chr_countdf_core_normal<-data.frame(chromosome=names(chr_names_core_normal),count=as.numeric(chr_names_core_normal))
ggplot(dmr_chr_countdf_core_normal, aes(x=chromosome,y=count)) + geom_bar(stat="identity",fill="red",width=0.5 )+labs(x = "Chromosome", y = "DMR Count", title = "DMR Counts per Chromosome")+ theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
GSEA
```{r}
library(missMethyl)
gst.region_core_normal <- goregion(results.ranges_core_normal, all.cpg=rownames(mVals),
collection="GO", array.type="450K", plot.bias=TRUE,sig.genes = TRUE)
```
```{r}
topGSA(gst.region_core_normal, n=10)
reuslt_core_norm_df<-as.data.frame(topGSA(gst.region_core_normal, n=10))
ggplot(reuslt_core_norm_df, aes(x = reorder(TERM, P.DE), y = P.DE)) +
geom_dotplot(stat = "identity", fill = "dodgerblue") +
labs(title = "Significant Gene Sets",
x = "Gene Set",
y = "p-value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggplot(reuslt_core_norm_df, aes(x = reorder(TERM, P.DE), y = P.DE)) +
geom_point(aes(color = P.DE), size = 5) +
labs(title = "Significant Gene Sets",
x = "Gene Set",
y = "p-value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10))
```
```{r}
gst.region.kegg_core_normal <- goregion(results.ranges_core_normal, all.cpg=rownames(mVals),
collection="KEGG", array.type="450K",sig.genes = TRUE)
top_20_pathways_core_normal<-topGSA(gst.region.kegg_core_normal, n=10)
top_20_pathways_core_normal
ggplot(as.data.frame(top_20_pathways_core_normal), aes(x = reorder(Description, P.DE), y =P.DE)) +
geom_point(aes(color = P.DE), size = 5) +
labs(title = "Significantly Enriched Pathways in Core- Normal",
x = "Gene Set",
y = "p-value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10))
```
--------------------------------------------------------------Venn diagram to see overlap in DMR---------------------------------------------------------------------
```{r}
library(VennDiagram)
Mar_Core_DMR<-intersect(as.character(results.ranges),as.character(results.ranges_core_normal))
Core_Ct_DMR<-intersect(as.character(results.ranges_ct_normal),as.character(results.ranges_core_normal))
Mar_Ct_DMR<-intersect(as.character(results.ranges),as.character(results.ranges_ct_normal))
Mar_Core_Ct_DMR<-intersect(as.character(results.ranges_core_normal),Mar_Ct_DMR)
length(Mar_Core_DMR)
length(Core_Ct_DMR)
length(Mar_Ct_DMR)
venn_DMR <- venn.diagram(list(as.character(
results.ranges),as.character(results.ranges_core_normal),as.character(results.ranges_ct_normal)),
category = c("Margin", "Core", "Plasma"),
fill = c("skyblue", "mediumorchid", "lightgreen"),
alpha = 0.5, # Transparency of set regions
filename = NULL # Set this to a file name if you want to save the plot as an image
)
subsetByOverlaps(results.ranges_core_normal,results.ranges_ct_normal)
grid.newpage()
grid.draw(venn_DMR)
#Convert overlapping region back into GRange object
convert_to_granges <- function(Mar_Core_Ct_DMR) {
# Split the character ranges into sequence names, start positions, and end positions
split_ranges <- strsplit(Mar_Core_Ct_DMR, "[:-]")
seqnames <- sapply(split_ranges, "[[", 1)
start <- as.numeric(sapply(split_ranges, "[[", 2))
end <- as.numeric(sapply(split_ranges, "[[", 3))
# Create the GRanges object
granges_object <- GRanges(seqnames = seqnames, ranges = IRanges(start = start, end = end))
return(granges_object)
}
# Convert character ranges to GRanges--All 3 overlaps
granges_object_3_overlap <- convert_to_granges(Mar_Core_Ct_DMR)
granges_object_3_overlap
overlap_DMR_3<-paste(results.ranges_core_normal[4,],results.ranges_core_normal[8,])
overlap_DMR_3
#GSEA KEGG all 3 overlap
gst.region.kegg_3_overlap <- goregion(granges_object_3_overlap, all.cpg=rownames(mVals),
collection="KEGG", array.type="450K",sig.genes = TRUE)
top_5_pathways_3_overlap_KEGG<-topGSA(gst.region.kegg_3_overlap, n=5)
top_5_pathways_3_overlap_KEGG
gst.region.GO_3_overlap <- goregion(granges_object_3_overlap, all.cpg=rownames(mVals),
collection="GO", array.type="450K",sig.genes = TRUE)
top_5_pathways_3_overlap_GO<-topGSA(gst.region.GO_3_overlap, n=5)
top_5_pathways_3_overlap_GO
```
Core-Margin-Overlap
```{r}
granges_object_C_M_overlap <- convert_to_granges(Mar_Core_DMR)
#GSEA KEGG all 3 overlap
gst.region.kegg_C_M_overlap <- goregion(granges_object_C_M_overlap, all.cpg=rownames(mVals),
collection="KEGG", array.type="450K",sig.genes = TRUE)
top_5_pathways_C_M_overlap_KEGG<-topGSA(gst.region.kegg_C_M_overlap, n=5)
top_5_pathways_C_M_overlap_KEGG
gst.region.GO_C_M_overlap <- goregion(granges_object_C_M_overlap, all.cpg=rownames(mVals),
collection="GO", array.type="450K",sig.genes = TRUE)
top_5_pathways_C_M_overlap_GO<-topGSA(gst.region.GO_C_M_overlap, n=5)
top_5_pathways_C_M_overlap_GO
```
Plasma-Margin Overlap
```{r}
granges_object_Ct_M_overlap <- convert_to_granges(Mar_Ct_DMR)
granges_object_Ct_M_overlap
##GSEA KEGG all 3 overlap
#gst.region.kegg_Ct_M_overlap <- goregion(granges_object_Ct_M_overlap, all.cpg=rownames(mVals),
# collection="KEGG", array.type="450K",sig.genes = TRUE)
#top_5_pathways_Ct_M_overlap_KEGG<-topGSA(gst.region.kegg_Ct_M_overlap, n=5)
#top_5_pathways_Ct_M_overlap_KEGG
#gst.region.GO_Ct_M_overlap <- goregion(granges_object_Ct_M_overlap, all.cpg=rownames(mVals),
# collection="GO", array.type="450K",sig.genes = TRUE)
#top_5_pathways_Ct_M_overlap_GO<-topGSA(gst.region.GO_Ct_M_overlap, n=5)
#top_5_pathways_Ct_M_overlap_GO
```
Plasma-Core Overlap
```{r}
granges_object_Ct_C_overlap <- convert_to_granges(Core_Ct_DMR)
#GSEA KEGG all 3 overlap
#gst.region.kegg_Ct_C_overlap <- goregion(granges_object_Ct_C_overlap, all.cpg=rownames(mVals),
# collection="KEGG", array.type="450K",sig.genes = TRUE)
#top_5_pathways_Ct_C_overlap_KEGG<-topGSA(gst.region.kegg_Ct_C_overlap, n=5)
#top_5_pathways_Ct_C_overlap_KEGG
#gst.region.GO_Ct_C_overlap <- goregion(granges_object_Ct_C_overlap, all.cpg=rownames(mVals),
# collection="GO", array.type="450K",sig.genes = TRUE)
#top_5_pathways_Ct_C_overlap_GO<-topGSA(gst.region.GO_Ct_C_overlap, n=5)
#top_5_pathways_Ct_C_overlap_GO
```
-----------------------------------------------------------------------PlasmaDNA-Margin------------------------------------------------------------------------------
```{r}
contMatrix_ct_margin <- makeContrasts(Plasma_DNA-Margin,
levels=design)
contMatrix_ct_margin
```
```{r}
# fit the contrasts
fit2_ct_margin <- contrasts.fit(fit, contMatrix_ct_margin)
fit2_ct_margin <- eBayes(fit2_ct_margin)
```
```{r}
# look at the numbers of DM CpGs at FDR < 0.05
summary(decideTests(fit2_ct_margin))
```
```{r}
# get the table of results for the first contrast (naive - rTreg)
ann450kSub_ct_margin <- ann450k[match(rownames(mVals),ann450k$Name),
c(1:4,12:19,24:ncol(ann450k))]
DMPs_ct_margin <- topTable(fit2_ct_margin, num=Inf, coef=1, genelist=ann450kSub_ct_margin)
DMP_ord_ct_margin<-DMPs_ct_margin[order(DMPs_ct_margin$t),]
DMP_ord_ct_margin<-subset.data.frame(DMP_ord_ct_margin,DMP_ord_ct_margin$P.Value<0.05)
tail(DMPs_ct_margin)
head(DMPs_ct_margin)
```
```{r}
write.table(DMP_ord_ct_margin, file="DMPs_ct_margin.csv", sep=",", row.names=FALSE)
```
```{r}
# plot the top 4 most significantly differentially methylated CpGs
par(mfrow=c(2,2))
sapply(rownames(DMP_ord_ct_margin)[1:4], function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
dim(DMP_ord_ct_margin)
```
```{r}
#Plot the bottom 4 differentially methylated CpGs
par(mfrow=c(2,2))
sapply(tail(rownames(DMP_ord_ct_margin,4)), function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
tail(DMP_ord_ct_margin,4)
```
```{r}
myAnnotation_ct_margin <- cpg.annotate(object = mVals, datatype = "array", what = "M",
analysis.type = "differential", design = design,
contrasts = TRUE, cont.matrix = contMatrix_ct_margin,
coef = colnames(contMatrix_ct_margin), arraytype = "450K")
```
```{r}
str(myAnnotation_ct_margin)
```
```{r}
#identify differentially methylated regions
DMRs_ct_margin <- dmrcate(myAnnotation_ct_margin, lambda=1000, C=2)
results.ranges_ct_margin <- extractRanges(DMRs_ct_margin)
head(results.ranges_ct_margin,6)
results.ranges_ct_margin
```
```{r}
# draw the plot for the top DMR
install.packages(XQuartz)
par(mfrow=c(1,1))
ct_margin_DMRplot<-DMR.plot(ranges = results.ranges_ct_margin, dmr = 2, CpGs = bVals, phen.col = cols,
what = "Beta", arraytype = "450K", genome = "hg19")
```
```{r}
range_DMR_ct_margin<-ranges(results.ranges_ct_margin)
range_DMR_ct_margin<-data.frame(range_DMR_ct_margin)
ggplot(range_DMR_ct_margin, aes(x=width)) + geom_histogram(color="darkblue", fill="lightblue",binwidth =100)
chr_names_ct_margin<-table(seqnames(results.ranges_ct_margin))
dmr_chr_countdf_ct_margin<-data.frame(chromosome=names(chr_names_ct_margin),count=as.numeric(chr_names_ct_margin))
ggplot(dmr_chr_countdf_ct_margin, aes(x=chromosome,y=count)) + geom_bar(stat="identity",fill="red",width=0.5 )+labs(x = "Chromosome", y = "DMR Count", title = "DMR Counts per Chromosome")+ theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
GSEA
```{r}
gst.region_ct_margin <- goregion(results.ranges_ct_margin, all.cpg=rownames(mVals),
collection="GO", array.type="450K", plot.bias=TRUE,sig.genes = TRUE)
```
```{r}
topGSA(gst.region_ct_margin, n=10)
```
```{r}
gst.region.kegg_ct_margin <- goregion(results.ranges_ct_margin, all.cpg=rownames(mVals),
collection="KEGG", array.type="450K",sig.genes = TRUE)
top_20_pathways_ct_margin<-topGSA(gst.region.kegg_ct_margin, n=10)
top_20_pathways_ct_margin
```
----------------------------------------------------------------------------Core-Margin------------------------------------------------------------------------------
```{r}
contMatrix_core_margin<- makeContrasts(Core-Margin,
levels=design)
contMatrix_core_margin
```
```{r}
# fit the contrasts
fit2_core_margin <- contrasts.fit(fit, contMatrix_core_margin)
fit2_core_margin <- eBayes(fit2_core_margin)
```
```{r}
# look at the numbers of DM CpGs at FDR < 0.05
summary(decideTests(fit2_core_margin))
```
```{r}
# get the table of results for the first contrast (naive - rTreg)
ann450kSub_core_margin <- ann450k[match(rownames(mVals),ann450k$Name),
c(1:4,12:19,24:ncol(ann450k))]
DMPs_core_margin <- topTable(fit2_core_margin, num=Inf, coef=1, genelist=ann450kSub_core_margin)
DMP_ord_core_margin<-DMPs_core_margin[order(DMPs_core_margin$t),]
DMP_ord_core_margin<-subset.data.frame(DMP_ord_core_margin,DMP_ord_core_margin$P.Value<0.05)
tail(DMPs_core_margin)
head(DMPs_core_margin)
```
```{r}
write.table(DMP_ord_core_margin, file="DMPs_core_margin.csv", sep=",", row.names=FALSE)
```
```{r}
# plot the top 4 most significantly differentially methylated CpGs
par(mfrow=c(2,2))
sapply(rownames(DMP_ord_core_margin)[1:4], function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
dim(DMP_ord_core_margin)
```
```{r}
#Plot the bottom 4 differentially methylated CpGs
par(mfrow=c(2,2))
sapply(tail(rownames(DMP_ord_core_margin,4)), function(cpg){
plotCpg(bVals, cpg=cpg, pheno=targets$Sample_Group, ylab = "Beta values")
})
tail(DMP_ord_core_margin,4)
dim(DMP_ord_core_margin)
```
```{r}
myAnnotation_core_margin <- cpg.annotate(object = mVals, datatype = "array", what = "M",
analysis.type = "differential", design = design,
contrasts = TRUE, cont.matrix = contMatrix_core_margin,
coef = colnames(contMatrix_core_margin), arraytype = "450K" )
myAnnotation_core_margin_FDR <- cpg.annotate(object = mVals, datatype = "array", what = "M",
analysis.type = "differential", design = design,
contrasts = TRUE, cont.matrix = contMatrix_core_margin,
coef = colnames(contMatrix_core_margin), arraytype = "450K",fdr = 0.5 )
```
```{r}
str(myAnnotation_core_margin)
str(myAnnotation_core_margin_FDR)
```
```{r}
#identify differential methylated regions
DMRs_core_margin <- dmrcate(myAnnotation_core_margin, lambda=500, C=2)
results.ranges_core_margin <- extractRanges(DMRs_core_margin)
head(results.ranges_core_margin,6)
```
--------------------------------------------------PlasmaDNA-Core------------------------------------------