-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIMHANS_Full_QC.Rmd
executable file
·973 lines (729 loc) · 36.7 KB
/
NIMHANS_Full_QC.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
---
title: "Full Epic Array Quality Control"
output:
html_document:
toc: true
toc_float: true
includes:
before_body: /mnt/data1/Array_Projects/footer.html
after_body: /mnt/data1/Array_Projects/footer.html
params:
Name: NIMHANS
SampleSheet: nihamsSampleSheet.csv
PathToProjectFolder: /home/nClifton/methylation/Epic_methylation_array_QC/
Mset: QC_nimhans/nimhans_Mset.rdat
RGset: QC_nimhans/nimhans_RGset.rdat
IntensityThreshold: 750
Genotype: /mnt/data1/IndiaWorkshop/EXTEND_genotype_QCd_59EPICSNPs.csv
fam:
SamplesPassed: QC_nimhans/SamplesPassedQC.csv
SamplesFailed: QC_nimhans/SamplesFailedQC.csv
Normalised: QC_nimhans/nimhans_Normalised.rdat
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
##Introduction
This is an R Markdown document for the Quality Control (QC) of Illumina EPIC microarray data of all the samples from the `r params$Name` study.
Samples are not removed after each QC step, but a record is kept of samples which have already failed previous steps and the results of all tests are provided as output.
##Loading data
```{r loadingdata, include=FALSE}
library(methylumi)
library(wateRmelon)
library(FlowSorted.Blood.EPIC)
require(gdata)
library(minfi)
library(ggplot2)
library(gdata)
require(gridExtra)
library(glmnet)
library(yarrr)
require(IlluminaHumanMethylationEPICmanifest)
library(tidyr)
library(dplyr)
library(gplots)
library(IlluminaHumanMethylationEPICanno.ilm10b2.hg19)
setwd(params$PathToProjectFolder)
SampleSheet<-read.csv(file = params$SampleSheet, stringsAsFactors = FALSE, sep = ",")
SampleSheet$Sex <- as.factor(SampleSheet$Sex)
rownames(SampleSheet) <- SampleSheet$Basename
SampleSheet$Empty <- is.na(SampleSheet$Sample_Name)
# SampleSheet$Control <- SampleSheet$Sample_ID == "Meth_Control"
## make chip name full (R often changes this to scientific notation)
SampleSheet$Basename2<-SampleSheet$Basename
SampleSheet<-separate(data = SampleSheet, col = Basename2, into = c("SentrixID", "Position"), sep="_")
```
```{r Making Mset and RGset, echo=FALSE, message=FALSE}
setwd(params$PathToProjectFolder)
#First copy the idats over to knight using "scp 201114400019/*.idat gNeilson@knight.ex.ac.uk:/mnt/data1/BDR/Methylation/idats/" and then use "chmod a+r *.idat" to change the permissions so all users can read the files
idatPath<-c("/mnt/data1/NIMHANS/idats/")
#For most steps we need the idats as a methylumiSet but for some we need an RGset
#These take a while to create - so saving as R objects to load faster next time
if(file.exists(file= params$Mset)){
print(paste("Loading Mset:", params$Mset))
load(file = params$Mset)
print(paste("Loaded Mset:", params$Mset))
} else {
msetEPIC <- readEPIC(idatPath=idatPath, barcodes=SampleSheet$Basename, parallel = FALSE, force=T)
save(msetEPIC, file = paste(params$PathToProjectFolder, params$Mset, sep = ""))
print(paste("Mset created and saved as", params$Mset))
}
if(file.exists(file= params$RGset)){
load(file= params$RGset)
print(paste("Loading RGset:", params$RGset))
} else{
RGset <- read.metharray.exp(base = idatPath, targets = SampleSheet, force = TRUE)
save(RGset, file = paste(params$PathToProjectFolder, params$RGset, sep = ""))
print(paste("RGset created and saved as", params$RGset))
}
```
###Study Information
**Study:** `r params$Name`
**Arrays ran by:** `r SampleSheet[1,"Name_lab"]`, The University of Exeter Medical School
**Array used:** Illumina EPIC microarray v1.0
**QC done by:**`r SampleSheet[1,"Name_QC"]`, The University of Exeter Medical School
**Date of QC:** `r format(Sys.Date(), format="%d %B %Y")`
**Sample ages:** `r range(SampleSheet$Age, na.rm = TRUE)`
**Sample Tissue:** `r unique(SampleSheet$Organ)`
##QC Information
```{r createQCmetrics, echo=FALSE}
betas <- betas(msetEPIC)
SampleSheet <- SampleSheet[order(rownames(SampleSheet)),]
msetEPIC <- msetEPIC[,order(colnames(msetEPIC))]
if(print(identical(rownames(SampleSheet), colnames(msetEPIC))) ==TRUE){
print("mSET and pheno samples match")
} else{
print("mSET and pheno samples do not match")
exit()
}
#QC metrics can be be bound on to the end of a copy of the sample sheet
QCmetrics <- SampleSheet
#SamplesFail will be our boolean record of which samples have already failed
SamplesFail <- as.logical(rep("FALSE", nrow(SampleSheet)))
#Entries will be changed to TRUE as samples fail
Stepsummary <- as.data.frame(matrix(ncol=0, nrow=2))
rownames(Stepsummary)<-c("Failed This Step", "Total Failed")
```
The phenotype file containing the sample information was loaded into R. Then the methylation data for the `r nrow(SampleSheet)` samples were loaded into a methylumiset. This contains `r nrow(SampleSheet[SampleSheet$Sample_ID != "Meth_Control",])` `r params$Name` and `r nrow(SampleSheet[SampleSheet$Sample_ID == "Meth_Control",])` fully methylated control samples.
##Check Signal Intensities
The intensity check is the biggest indicator of sample quality. The median methylated signal intensity and unmethylated signal intensity for each sample is calculcated.
```{r medianintensities, include=FALSE}
m_intensities<-methylated(msetEPIC)
u_intensities<-unmethylated(msetEPIC)
M.median<-apply(m_intensities, 2, median)
U.median<-apply(u_intensities, 2, median)
QCmetrics<-cbind(SampleSheet,M.median, U.median)
```
A histogram and scatter plot of the resulting data are plotted to visualise the data quality. Samples are coloured by methylation plate or institute, to make sure there are no batch effects.
```{r plotintensities, echo=FALSE}
## Coloured By Sex
plotfactor <- factor(SampleSheet$Sex, levels=unique(SampleSheet$Sex))
par(mfrow = c(1,1))
plot(M.median, U.median, pch = 16, xlab = "Median M intensity", ylab = "Median U intensity", col = rainbow(nlevels(plotfactor))[factor(plotfactor)], main="Scatter plot of Signal Intensities coloured by Sex")
par(xpd=TRUE)
legend("topright", levels(factor(plotfactor)), col = rainbow(nlevels(plotfactor)), pch = 10, cex=0.5)
##Coloured by Tissue type
plotfactor<-factor(SampleSheet$BrainRegion, levels=unique(SampleSheet$BrainRegion))
par(mfrow = c(1,1))
plot(M.median, U.median, pch = 16, xlab = "Median M intensity", ylab = "Median U intensity", col = rainbow(nlevels(plotfactor))[factor(plotfactor)], main="Scatter plot of Signal Intensities coloured by Brain Region")
par(xpd=TRUE)
legend("topright", levels(factor(plotfactor)), col = rainbow(nlevels(plotfactor)), pch = 10, cex=0.5)
##Coloured by SentrixID
if(!is.na(SampleSheet[1,"SentrixID"])){
plotfactor<-factor(SampleSheet$SentrixID, levels=unique(SampleSheet$SentrixID))
par(mfrow = c(1,1))
plot(M.median, U.median, pch = 16, xlab = "Median M intensity", ylab = "Median U intensity", col = rainbow(nlevels(plotfactor))[factor(plotfactor)], main="Scatter plot of Signal Intensities coloured by SentrixID")
par(xpd=TRUE)
legend("topright", levels(factor(plotfactor)), col = rainbow(nlevels(plotfactor)), pch = 10, cex=0.5)
}
```
Samples with SentrixID 29 have higher M intensity
To further investigate the plates we can plot heatmaps of the intensities, shown below.
```{r intensityheatmap, echo=FALSE}
# QCmetrics$ratio <- QCmetrics$M.median - QCmetrics$U.median
# QCmetrics$position<-factor(QCmetrics$Position)
# QCmetrics$SentrixID<-factor(QCmetrics$SentrixID, levels=rev(unique(QCmetrics$SentrixID))) #keeps the levels of the factor in current order rather than sorting numerically/alphabetically, also reverses this order as heatmaps plot bottom to top
#
# plates<-unique(QCmetrics$Plate)
#
#
# #extract the legend (using a function found online)
# g_legend<-function(a.gplot){
# tmp <- ggplot_gtable(ggplot_build(a.gplot))
# leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
# legend <- tmp$grobs[[leg]]
# legend
# }
#
# for(plate in plates){
# samples<-QCmetrics[which(QCmetrics$Plate == plate),]
# control<-samples[samples$Control,]
#
# plateHeatmap <- ggplot(data=samples, aes(x=Position, y=SentrixID)) +
# scale_fill_gradientn(colours=colorRamps::matlab.like(100), limits=c(min(QCmetrics$U.median),max(QCmetrics$M.median))) +
# labs(x="", y="") +
# theme_minimal() +
# coord_equal() +
# theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_text(angle = 90, hjust=1))
#
# plot1 <- plateHeatmap +
# ggtitle("Median Methylated Intensity") +
# geom_tile(aes(fill=M.median), colour = "white") +
# geom_point(data=control, aes(x=control$Position, y=control$SentrixID)) +
# theme(legend.position = "none")
#
# plot2 <- plateHeatmap +
# ggtitle("Median Unmethylated Intensity") +
# geom_tile(aes(fill=U.median), colour = "white") +
# geom_point(data=control, aes(x=control$Position, y=control$SentrixID)) +
# theme(legend.position = "none")
#
# legendplot<-plateHeatmap +
# geom_tile(aes(fill=U.median), colour = "white") +
# labs(fill="Intensity", alpha="Control") +
# geom_point(data=control, aes(x=control$Position, y=control$SentrixID, alpha=control$Control)) +
# scale_alpha_manual(values=c(1,1,1)) +
# guides(alpha = guide_legend(override.aes = list(colour="black", pch=16)))
#
# legend<-g_legend(legendplot)
#
# grid.arrange(plot1, plot2, legend, ncol=3, widths=c(3/7, 3/7, 1/7), top=paste("", plate))
# }
```
```{r heatmap of Meth and unmeth ratios, echo= FALSE}
# for(plate in plates){
# samples<-QCmetrics[which(QCmetrics$Plate == plate),]
# control<-samples[samples$Control,]
#
# plateHeatmap <- ggplot(data=samples, aes(x=Position, y=SentrixID)) +
# scale_fill_gradientn(colours=colorRamps::matlab.like(100), limits=c(min(QCmetrics$ratio),max(QCmetrics$ratio))) +
# labs(x="", y="") +
# theme_minimal() +
# coord_equal() +
# theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_text(angle = 90, hjust=1))
#
# plot3 <- plateHeatmap +
# ggtitle("Ratio of median methylated and median unmethylated Intensity") +
# geom_tile(aes(fill=ratio), colour = "white") +
# geom_point(data=control, aes(x=control$Position, y=control$SentrixID)) +
# theme(legend.position = "none")
#
# legendplot<-plateHeatmap +
# geom_tile(aes(fill=U.median), colour = "white") +
# labs(fill="Intensity", alpha="Control") +
# geom_point(data=control, aes(x=control$Position, y=control$SentrixID, alpha=control$Control)) +
# scale_alpha_manual(values=c(1,1,1)) +
# guides(alpha = guide_legend(override.aes = list(colour="black", pch=16)))
#
# legend<-g_legend(legendplot)
#
# grid.arrange(plot3, legend, ncol=2, widths=c(6/7, 1/7), top=paste("", plate))
# }
```
```{r removeempty, include=FALSE}
identical(rownames(QCmetrics), colnames(RGset))
RGset <- RGset[,order(colnames(RGset))]
identical(rownames(QCmetrics), colnames(RGset))
## remove the empty 'samples'
M.median <- M.median[!SampleSheet$Empty]
U.median <- U.median[!SampleSheet$Empty]
msetEPIC <- msetEPIC[,!SampleSheet$Empty]
RGset <- RGset[,!SampleSheet$Empty]
SamplesFail <- SamplesFail[!SampleSheet$Empty]
QCmetrics <- QCmetrics[!SampleSheet$Empty,]
SampleSheet <- SampleSheet[!SampleSheet$Empty,]
# save(msetEPIC, file =params$Mset)
# save(RGset, file =params$RGset)
# write.csv(SampleSheet, file = params$SampleSheet)
```
Although signal intensity is the largest predictor of sample quality, the threshold at which to exclude samples can vary from experiment to experiment - for example, signal intensities can vary slightly depending on tissue type or DNA extraction method. Samples which clearly deviate from the main cluster on the signal intensity plots should be removed.
```{r, echo = FALSE}
lowintensitysamples<-which(M.median < params$IntensityThreshold | U.median < params$IntensityThreshold)
```
```{r scatter500, echo=FALSE}
Intensity <- rep("OK", nrow(SampleSheet))
Intensity[lowintensitysamples] <-"LowIntensity"
plotfactor<-as.factor(Intensity)
plot(M.median, U.median, pch = 16, xlab = "Median M intensity", ylab = "Median U intensity", col=rainbow(2)[factor(plotfactor)], main = "Intensity check")
abline(v = params$IntensityThreshold, col = "red")
abline(h = params$IntensityThreshold, col = "red")
legend("topleft", levels(factor(plotfactor)), pch = 16, col = rainbow(2))
```
All signal intensities OK
```{r updateQCmetrics1, include=FALSE}
SamplesFail[which(Intensity=="LowIntensity")]<-TRUE
QCmetrics<-cbind(QCmetrics, Intensity)
Step1<-c(sum(Intensity=="LowIntensity"),sum(SamplesFail))
Stepsummary<-cbind(Stepsummary,Step1)
```
```{r Stepsummary1}
print(Stepsummary)
```
##Bisulphite Conversion
A bisulphite conversion statistic for each sample was calculated, and a histogram of the results plotted.
```{r bisulphiteconversion, echo=FALSE}
Bisulphite <- bscon(msetEPIC)
hist(Bisulphite, xlab = "Median % BS conversion", main = "Histogram of Bisulphite Converstion Statistics")
```
Samples with a conversion < 80% fail the QC, so with this threshold `r sum(Bisulphite<80)` samples fail the QC, and will be removed at a later stage.
```{r updateQCmetrics2, include=FALSE}
QCmetrics<-cbind(QCmetrics, Bisulphite)
SamplesFail[which(Bisulphite<80)]<-TRUE
Step2<-c(sum(Bisulphite<80, na.rm=T),sum(SamplesFail))
Stepsummary<-cbind(Stepsummary,Step2)
bisulphite_fail <- QCmetrics[SamplesFail,]
```
```{r Stepsummary2}
print(Stepsummary)
```
##Sex Check
A principal component analysis of the methylation data can be used to predict the sex of the samples.
The principal components are calculated, and the two which correlate most with sex are found. These can be used to generate a scatter plot where the sexes are clearly separated.
```{r sex check minfi, warning = FALSE, echo = FALSE, message= FALSE}
GRset <- mapToGenome(RGset)
predictedSex1 <- getSex(GRset, cutoff = -2)
Sexplot_data <- as.data.frame(predictedSex1)
Sexplot_data <- Sexplot_data[order(row.names(Sexplot_data)),]
PredictedSex <-Sexplot_data$predictedSex
QCmetrics <- cbind(QCmetrics,PredictedSex)
#here we add the reported sex data
Sexplot_data <- cbind(Sexplot_data, QCmetrics$Sex)
colnames(Sexplot_data)[colnames(Sexplot_data)=="QCmetrics$Sex"] <- "Reported Sex"
#replace blanks with NA
Sexplot_data$`Reported Sex`[Sexplot_data$`Reported Sex` == ""] <- "NA"
ggplot(Sexplot_data, aes(Sexplot_data$xMed,Sexplot_data$yMed,
colour = Sexplot_data$`Reported Sex`)) +
geom_point() +
labs(x= "X Chr, median total intensity (log2)",
y ="Y Chr, median total intensity (log2)",
colour = "Reported Sex")
```
```{r updateQCmetrics3, include=FALSE}
ReportedSex <- as.character(QCmetrics$Sex)
# convert predicted sex to full words
PredictedSex[PredictedSex == "M"] <- "Male"
PredictedSex[PredictedSex == "F"] <- "Female"
QCmetrics$MismatchSex <- PredictedSex!=ReportedSex
SamplesFail[which(PredictedSex!=ReportedSex)] <- TRUE
Step3 <- c(length(which(PredictedSex!=ReportedSex)),sum(SamplesFail))
Stepsummary <- cbind(Stepsummary,Step3)
```
Here `r sum(PredictedSex!=ReportedSex, na.rm=T)` number of sampless predicted sex does not match their reported sex. These samples fail the QC according to the findGenderPC function and, and will be removed.
```{r Stepsummary3}
print(Stepsummary)
```
2 failed sex chack due to NA value. Cluster with females
##Genotype Sex Check
If there is SNP data available the reported sex will be comapred to the sex predicted using the SNP data and samples which do not match will be removed.
If SNP data is not available this step will be ignored.
```{R check sex agaisnt genotype sex, echo=F}
# if(!is.null(SampleSheet[1,"DNA_IID"])){
# fam<-read.table(params$fam)
# fam$V5[fam$V5==0]<-NA
# # (1 = male, 2 = female, 0 = unknown)
# # recode so that it matches the rest of the file for comparison
# fam$V5[fam$V5==1]<-"M"
# fam$V5[fam$V5==2]<-"F"
#
# ### match the datasets
# fam$V2 <- as.character(fam$V2)
# fam<-fam[match(QCmetrics$DNA_IID, fam$V2),]
#
# #### count mis matches between pheno and geno and predicted and geno
#
# MMpred<-fam[which(fam$V5 != PredictedSex),] ## 19
# MMpheno<-fam[which(fam$V5 != ReportedSex),] ## 14
# overlapMM<-MMpred[which(MMpred$V1 %in% MMpheno$V1),] ## 8
#
# QCmetrics<-cbind(QCmetrics, geno_sex=fam$V5)
# QCmetrics$geno_sex[QCmetrics$geno_sex==0]<-NA
# QCmetrics$Mismatch_pheno_geno_Sex<-ReportedSex!= QCmetrics$geno_sex
# QCmetrics$Mismatch_predicted_geno_SeX<-PredictedSex!= QCmetrics$geno_sex
# }
```
##Check Genotypes
On the EPIC array there are 59 SNP probes. If have SNP data is present, we can compare the methylation on these SNP probes to the samples genotypes to confirm samples are from the expected individual.
If SNP data is not available this step will be ignored.
```{r load genotypes, echo=FALSE, message=FALSE}
#Skip Genotyping if there are more than unique(plates) NA's. EG if there are three plates there should only be three NA's. If more than three NA's in the DNA_IID column assumption is made that there are no genotyping data available.
#
# betas <- betas(msetEPIC)
# # load genotypes and match to betas
#
# geno <- read.table(params$Genotype, row.names = 2, h=T) #The second row is where the samples are unique to individuals
# geno[,1:5]<-NULL
# ID <- SampleSheet[match(rownames(geno),SampleSheet$DNA_IID),] #select samples from samplesheet that has genotype data
# geno <- geno[match(SampleSheet$DNA_IID, rownames(geno)),] #select genotype sample that have array data
# id <- geno[-which(rownames(geno) %in% SampleSheet$DNA_IID),] #geno samples which do not have array data
#
# a <- NULL
# for(i in 1:ncol(geno)){
# snp<-unlist(strsplit(colnames(geno)[i], "_"))[1]
# a<-append(a, grep(snp, rownames(betas)))
# }
#
# meth.sub<-betas[a,]
#
#
#
# #finding number of available genotypes for each sample
# nGenos <- rep(NA, length=nrow(SampleSheet))
# for(i in 1:nrow(SampleSheet)){
# nGenos[i]<-sum(!is.na(geno[i,]))
# }
#
# # first check direction of minor alleles
# cors <- vector(length = length(a))
# par(mfrow=c(2,3))
# for(each in 1:nrow(meth.sub)){
# cors[each]<-cor(geno[,each], meth.sub[each,], use = "pairwise.complete.obs")
# plot(geno[,each], meth.sub[each,], xlab = "Genotype", ylab = "Methylation", main = rownames(meth.sub)[each], pch = 16, xlim = c(0,2), ylim = c(0,1), cex = 0.7)
# }
#
# # change minor allele in genotype data if negative correlation
# for(each in which(cors < 0)){
# geno[,each]<-(2-geno[,each])
# }
#
# #samples which seem incorrect
# mismatchsamples<-which(cors<0.8)
# par(mfrow=c(2,3))
# for (i in mismatchsamples){
# plot(geno[,each], meth.sub[each,], xlab = "Genotype", ylab = "Methylation", main = rownames(meth.sub)[each], pch = 16, xlim = c(0,2), ylim = c(0,1), cex = 0.7)
# }
```
To ensure that the correct samples are loaded onto the EPIC array and that it matches their genotyping information, we will calculate the correlation between SNPs on the EPIC array and on the genotyping array. Correlation values lower than 0.8 suggests that the samples from the methylation and genotyping do not match. These samples will need to be further investigated to understanding what issues may have risen such as mislabelling samples or sample switch on either arrays.
First, we will check the number of SNPs that individuals share between the EPIC and the genotyping array. The SNPs on the DNAm array and the SNPs on the genotyping array may not always match for all individuals, the table below is the summary for this study.
```{r ngenos, echo = FALSE, message = FALSE}
if(exists('nGenos')){
tblnGenos <- as.data.frame(table(nGenos))
tblnGenos$nGenos <- as.numeric(as.character(tblnGenos$nGenos))
tblnGenos <- t(tblnGenos)
rownames(tblnGenos) <- c('Snps shared between\nDNAm array & SNP array',
'Number of samples')
grid.table(tblnGenos)
} else {
print('No Genotype data to compare snps on DNAm array and SNP array')
}
```
Now we can check the correlation between the methylation and genotyped samples.
```{r check genotypes correlations, echo=FALSE, message=FALSE}
if(exists('geno')){
#check genotypes correlation with methylation per sample
GenoCor<-rep(NA, nrow(SampleSheet))
for(i in 1:ncol(meth.sub)){
GenoCor[i]<-cor(as.numeric(geno[i,]), meth.sub[,i], use = "pairwise.complete.obs")
}
#plot histogram of correlations
hist(GenoCor)
}
```
A failed sample is where the methyaltion and genotype correlation is low for a SNP, an example is provided here below.
```{r examplefailedgenotypes, echo=FALSE, message=FALSE}
if(!is.null(SampleSheet[1,"DNA_IID"])){
par(mfrow=c(1,2))
examples=c(which(GenoCor>0.8)[1],which(GenoCor<0.8)[1])
titles=c("Passed Sample","Failed Sample")
for (n in 1:2){
i=examples[n]
plot(as.numeric(geno[i,]), meth.sub[,i], main =titles[n], xlab = "Genotype", ylab = "Methylation", xlim = c(0,2), ylim = c(0,1), pch = 16)
legend("topleft", legend=paste("r=",round(GenoCor[i],3)), bty="n")
}
}
```
```{r updateQCmetrics4, include=FALSE}
# QCmetrics<-cbind(QCmetrics, GenoCor)
# SamplesFail[which(GenoCor<0.8)]<-TRUE
# Step4<-c(sum(GenoCor<0.8, na.rm=T),sum(SamplesFail))
# Stepsummary<-cbind(Stepsummary,Step4)
# genocorr<-subset(QCmetrics, GenoCor < 0.8)
```
```{r Stepsummary4}
print(Stepsummary)
```
##Genetic correlations
The 59 SNP probes on the array can also be used to estimate genetic correlations between samples.
This small number of probes means that only identical samples (such as samples from the same individual, MZ twins, individual brainIDs or samples duplicated by error) can be identified and no lower proportion of genetic relatedness (such as siblings) can be inferred.
If your study is not a paired design this step will be skipped
```{r calculate snpCor, include=FALSE}
betas<-betas(msetEPIC)
betas.rs<-betas[grep("rs", rownames(betas)),]
snpCor <- cor(betas.rs, use = "pairwise.complete.obs")
for(i in 1:ncol(betas.rs)){
snpCor[i,i]<-NA
}
```
###Checking samples from unrelated individuals
This study design assumes that each sample is completely unqiue and that there should be no duplicates with any other sample. The SNPs on the DNAm array can be used to predicted that no sample correlates with another. For each sample, we can find its maximum correlation with any other sample, and plot a histogram of the results. Any samples with a threshold greater than 0.8 indicates that they are duplicates.
```{r checkingunrelatedcor, echo=FALSE}
for (region in unique(SampleSheet$BrainRegion)){
sample_names <- unique(SampleSheet$Sample_Name[SampleSheet$BrainRegion == region])
unrelatedcors <- snpCor
for (i in sample_names){
samples <- which(SampleSheet$Sample_Name == i)
unrelatedcors[samples,samples] <- NA
}
unrelatedcors <- unrelatedcors[SampleSheet$Basename[SampleSheet$Sample_Name %in% sample_names], SampleSheet$Basename[SampleSheet$Sample_Name %in% sample_names]]
maxunrelatedcors <- apply(unrelatedcors, 1, max, na.rm = TRUE)
hist(maxunrelatedcors, main=paste("Maximum correlation in samples from unrelated individuals:", region), xlab="Max Correlation")
}
```
`r length(which(maxunrelatedcors>0.8))` samples have a maximum correlation > 0.8 with another unrelated sample.
Sometimes samples are duplicates and we can check that there is also duplicates in the genotype data if necessary.
```{r, echo=FALSE, message=FALSE}
# for (key in names(params)) {
# do.call('Sys.setenv', params[key])
# }
#
# nodupsmeth <- length(rownames(as.matrix(which(maxunrelatedcors > 0.8, arr.ind = T))))
```
```{r genomatrix, echo = FALSE,warning = FALSE}
# if(length(which(maxunrelatedcors > 0.8, arr.ind = T)) != 0){
# mibs <- read.delim(paste(getwd(),"/CheckGenoDuplicates/plink.mibs", sep = ""), sep = "", header = F)
# mibsid <- read.delim(paste(getwd(),"/CheckGenoDuplicates/plink.mibs.id", sep = ""), sep = "", header = F)
# rownames(mibs) <- mibsid$V2
# colnames(mibs) <- mibsid$V2
# #Remove the 1's
# for (i in 1:nrow(mibs)){
# mibs[i,i]<-NA
# }
# maxunrelatedgenocors<-apply(mibs, 1, max, na.rm = TRUE) #over each row
# hist(maxunrelatedgenocors, main="Maximum correlation in samples from individuals (SNPs)", xlab="Max Correlation")
#
# dupsgeno <- rownames(as.matrix(which(maxunrelatedgenocors > 0.9, arr.ind = T)))
#
# mibs2 <- mibs[dupsgeno,dupsgeno]
#
#
# mibs2 <- as.matrix(mibs2)
# heatmap.2(mibs2, trace = "none", key = FALSE, dendrogram = "column",
# labRow =rownames(mibs2), labCol = colnames(mibs2), main = "Sample Duplicates (Genotype)", margin = c(10,10))
# print("These samples are also duplicates in the genotype data. However these samples will not be counted as fails.")
# }
```
```{r update QCmetrics6, include=FALSE}
#To ensure that samples are truly duplicates we will check geno and methylation samples are also duplicates
#
# dupsmeth <- rownames(as.matrix(which(maxunrelatedcors > 0.8, arr.ind = T)))
# dupsmeth <- QCmetrics[which(QCmetrics$Basename %in% dupsmeth), "DNA_IID"]
#
# QCmetrics<-cbind(QCmetrics,maxunrelatedcors)
# if(dupsmeth %in% dupsgeno){
# print("These samples are duplicated")
# Step6<-c(0,sum(SamplesFail))
# } else{
# print("Samples not duplicated in geno and meth snps")
# SamplesFail[which(maxunrelatedcors > 0.8)]<-TRUE
# dup<-QCmetrics[which(QCmetrics$maxunrelatedcors > 0.8),]
# write.csv(dup, paste(params$PathToProjectFolder, "duplicated_samples.csv",sep = ""), row.names=F)
# }
#
# Stepsummary<-cbind(Stepsummary,Step6)
```
```{r Stepsummary6}
# print(Stepsummary)
```
##Age Prediction
The age of samples can be predicted from their methylation data using Horvath's Coefficients and compared to the samples' reported ages.
However, this can be inaccurate and so it is used as a quality check and not as a reason to remove samples. Therefore, the following plot only contains samples which have passed the previous QC steps.
```{r dnamage, echo=FALSE}
QCmetrics$Age <- as.numeric(as.character(QCmetrics$Age))
PredictedAge <- agep(betas)
ReportedAge <- QCmetrics$Age
model <- lm(ReportedAge~PredictedAge)
plot(PredictedAge[!SamplesFail], QCmetrics$Age[!SamplesFail], xlab = "Predicted", ylab = "Reported", main="Reported Age against Predicted Age", pch=16, col="purple")
abline(model)
QCmetrics <- cbind(QCmetrics, PredictedAge)
AgeCorTest <- cor.test(QCmetrics$Age, QCmetrics$PredictedAge)
```
We can see there is a correlation between predicted and reported ages in the samples `r AgeCorTest$estimate`. Generally, Horvath's clock doesn't predict as well in older and brain samples. The predicted ages are added to the QC output.
##Smoking Score
Smoking has well known associations with DNA methylation at sites across the genome. Using a method developed by Elliot et al. 2014 we can calculate a smoking score based on DNA methylation at these known sites, to predict smoking status.
A smoking score is calculated for each sample and a histogram of the scores is plotted below.
This score is most accurate in blood and bucal samples and is less accurate in brain tissue.
```{r calcsmokingscores, echo=FALSE}
#load function
source("/mnt/data1/reference_files/SmokingScore/SmokingScoreFunction.r")
## READ IN SMOKING DATA
#function takes betas matrix and returns a vector of calculated smoking scores
SmokingScore <- smokingscore(betas)
hist(SmokingScore, main="Histogram of Calculated Smoking Scores")
QCmetrics <- cbind(QCmetrics, SmokingScore)
```
##Tissue Prediction
Only differentiates blood, buccal and saliva
```{r Tissue Prediction, echo =FALSE, message=FALSE, results= 'hide'}
#load function
source("/mnt/data1/Array_Projects/Tissue_Predictor/TissuePredictorScripts/Tissue_predictor_function.R")
QCmetrics <- cbind(QCmetrics, tissue_predictor(betas)) #QCmetrics, msetEPIC
ggplot(data = QCmetrics, aes(x=BrainRegion, fill= Predicted_Tissue))+
geom_bar( position= position_dodge())+
labs(x = "Reported Tissue", fill= "Predicted Tissue")
theme_minimal()
```
##Pfilter
The pfilter function in the wateRmelon package filters data sets based on beadcounts and detection p-values.
###Beadcounts
If the percentage of samples with a beadcount less than 3 is greater than 5% for any probe, the probe is removed.
###Detection p-values
If the percentage of probes with a detection p-value less than 0.05 is greater than 1% for any sample, the sample is removed.
Similarly, if the percentage of samples with a detection p-value less than 0.05 is greater than 1% for any probe, the probe is removed.
```{r pfilterdefault, echo = FALSE, message = FALSE}
msetEPIC.pf <- pfilter(msetEPIC)
#save(msetEPIC.pf, file="msetEPIC_pf.rdat")
# if(file.exists(file= "msetEPIC_pf.rdat")){
# print("msetEPIC_pf.rdat exists")
# } else {
# save(msetEPIC, file = paste(params$PathToProjectFolder, params$Mset, sep = ""))
# print("msetEPIC_pf.rdat saved")
# }
```
`r sum(!colnames(betas(msetEPIC)) %in% colnames(betas(msetEPIC.pf)))` out of `r nrow(SampleSheet)` samples fail the pfilter with these thresholds.
```{r update QCmetrics7, include=FALSE}
#remove the probes that failed the pfilter
msetEPIC <- msetEPIC[rownames(betas(msetEPIC)) %in% rownames(betas(msetEPIC.pf)),]
#mark samples that fail the pfilter
pFilterPass <- colnames(betas(msetEPIC)) %in% colnames(betas(msetEPIC.pf))
QCmetrics <- cbind(QCmetrics,pFilterPass)
SamplesFail[which(pFilterPass==FALSE)]<-TRUE
#update QCmetrics and SamplesFailed
Step7 <- c(length(which(pFilterPass==FALSE)),sum(SamplesFail))
Stepsummary <- cbind(Stepsummary,Step7)
```
```{r Stepsummary7}
print(Stepsummary)
```
## Outliers
The outlyx function in the wateRmelon package can be used to check if any of the remaining samples are classed as 'outliers' and should also be removed prior to normalisation.
```{r outliers,echo= FALSE}
betas <- betas(msetEPIC)
outliers <- outlyx(betas)
QCmetrics <- cbind(QCmetrics, outliers$outliers)
SamplesFail[which(outliers$outliers == TRUE)] <- TRUE
Step8 <- c(length(which(outliers$outliers == TRUE)),sum(SamplesFail))
Stepsummary <- cbind(Stepsummary,Step8)
```
```{r Stepsummary8}
print(Stepsummary)
```
`r sum(which(outliers$outliers == TRUE))` samples have been picked as being outliers using the outlyx function and have been removed prior to normalisation.
##Normalisation
The methylation data for just the passed samples and probes is quantile normalised using the dasen function from the wateRmelon package.
```{r normalisation, include=FALSE}
#this removes all the failed samples from the mset (pfiltered probes already removed)
# msetEPIC.previo <- msetEPIC[,!SamplesFail]
# save(msetEPIC.previo, file = "QC_nimhans/mset_removedfailed.rdat")
load("QC_nimhans/mset_removedfailed.rdat")
#then use dasen to normalise
# msetEPIC.dasen.previo <- dasen(msetEPIC.previo)
# save(msetEPIC.dasen.previo, file = "QC_nimhans/mset_dasen.rdat")
load("QC_nimhans/mset_dasen.rdat")
```
The 'qual' function in the wateRmelon package can be used to assess the degree of difference between normalised and raw betas
```{r qual, echo = FALSE}
betas <- betas(msetEPIC.previo)
betas.dasen <- betas(msetEPIC.dasen.previo)
normv <- qual(betas, betas.dasen)
plot(normv[,1:2], main = "Normalisation Violence")
Out<-normv[which(normv[,1]>0.08),]
#SamplesFail<-as.logical(rep("FALSE", nrow(SampleSheet)))
SamplesFail[which(normv[,1] >0.08)]<-TRUE
identical(rownames(QCmetrics), colnames(betas.dasen))
QCmetrics <- as.data.frame(as.matrix(QCmetrics))
QCmetrics$NormV <- normv$rmsd[match(rownames(QCmetrics),rownames(normv))]
Step9<-c(sum(QCmetrics$NormV >0.08, na.rm =TRUE),sum(SamplesFail))
Stepsummary<-cbind(Stepsummary,Step9)
```
```{r Stepsummary9}
print(Stepsummary)
```
Density plots of the beta values are plotted for each sample before and after normalisation below (separated by type I and type II probes). Samples with high normalisation violence will be removed.
```{r plot betas function, echo=FALSE}
#the inputs needed are your methylumiSet and a character string to give the plots a relevant title
plotmset_density<-function(mset, study=""){
onetwo<-fData(mset)$DESIGN
mat<-betas(mset)
plot(density(mat[onetwo=="I",1], na.rm=T, bw=0.03), cex.main=0.8, main=paste(study, "Betas"), ylim=c(0, 5.2), xlab="")
lines(density(mat[onetwo=="II",1], na.rm=T, bw=0.03), col="red")
for(j in 2:ncol(mat)){
lines(density(mat[onetwo=="I",j], na.rm=T, bw=0.03))
lines(density(mat[onetwo=="II",j], na.rm=T, bw=0.03), col="red")
}
legend("topright", legend=c("Type I", "Type II"), lty=1, col=c("black", "red"))
}
#The more samples in your mset the more messy these plots will look
msetEPIC<-msetEPIC[,!SamplesFail]
msetEPIC.dasen<-dasen(msetEPIC)
plotmset_density(msetEPIC, study="Raw data")
plotmset_density(msetEPIC.dasen, study="Normalised Data")
#double check dasen has not produced any values outside 0-1 (the number output is the number of samples that have any values outside 0-1)
raw.weird<-sum(colSums(betas(msetEPIC) > 1 | betas(msetEPIC) <0, na.rm=T) > 0)
#[1] 0
dasen.weird<-sum(colSums(betas(msetEPIC.dasen) > 1 | betas(msetEPIC.dasen) < 0) > 0)
#[1] 0
```
The distributions generally look as we would expect, and dasen has not caused any samples to have beta values outside the expected 0-1 range.
There should be no points failing the second normalisation step as they will have been removed previously
##Cell Composition
DNA methylation varies between different cell types, and so the composition of cell types can significantly affect the methylation of samples. Therefore, estimations of cell-type composition is an important variable to consider when analysing this type of data.In order to estimate the blood cell proportions, we use an function estimateCellCounts in the R package Minfi, developed by Hansen et al. We will estimate cell types CD8T, CD4T, Bcells, Granulocytes, Monocytes and NK cells. Samples that have failed until this stage will not be included in calculated cell proportions.
If your tissue is not blood, there is currently no appropriate algorithm to calculate you cell propotions for your tissue of interest. Therefore, this part of the report will not be run and we will move along to the next part of the report.
```{r cellcomposition, echo=FALSE, warning= FALSE, message = FALSE}
setwd(params$PathToProjectFolder)
if(!is.null(SampleSheet$Organ) && SampleSheet$Organ == "Blood"){
if(file.exists(file= "Cellcounts.rdat")){
print("Cell Counts exists and merge data together")
load("CellCounts.rdat")
} else {
counts <- estimateCellCounts(RGset[,!SamplesFail], compositeCellType ="Blood", cellTypes = c("CD8T","CD4T", "Bcell","Gran", "Mono","NK"), processMethod = "auto")
save(counts, file="CellCounts.rdat")
print("CellCount created and saved as CellCounts.rdat saved")
}
counts<-counts[match(SampleSheet$Basename, rownames(counts)),]
QCmetrics<-cbind(QCmetrics, counts)
#Add cell density plots
counts.m <- melt(counts,"colnames")
i <- sapply(counts.m, is.factor) #gets rid of factos in data frame
counts.m[i] <- lapply(counts.m[i], as.character)
colnames(counts.m) <- c("Samples","Cell","Proportion")
boxplot(counts, use.cols=T, main="Boxplot of Estimated Cell Proportions")
ggplot(counts.m, aes(x = Proportion)) +
geom_density(alpha = 0.5,color="darkblue", fill="lightblue") +
facet_wrap( ~ Cell) +
labs(x = "", y = "Density",
title = ("Cell Distribution")) +
theme(panel.background = element_blank()) +
scale_fill_brewer(palette = "Set1")
}
```
The estimated cell proportions are added to the QC output and can be used as covariates in later analyses.
##QC Summary
The QC results for the `r sum(SamplesFail)` failed samples and `r sum(!SamplesFail)` passed samples are saved to `r params$SamplesFailed` and `r params$SamplesPassed` respectively.
SNP probes, probes containing common SNPs, and probes with non-specific binding are removed at this stage to prevent technical artifacts influencing the results in later analyses.
Finally the QCed, normalised and filtered dataset is saved to `r params$Normalised` ready for subsequent analysis.
```{r saveoutput, include=FALSE}
setwd(params$PathToProjectFolder)
write.csv(QCmetrics[SamplesFail,], params$SamplesFailed, row.names = F)
write.csv(QCmetrics[!SamplesFail,], params$SamplesPassed, row.names = F)
#filter probes from normalised betas
betas <- betas(msetEPIC.dasen)
crosshyb <- read.table("/mnt/data1/EPIC_reference/CrossHydridisingProbes_McCartney.txt", stringsAsFactors = FALSE)
snpProbes <- read.table("/mnt/data1/EPIC_reference/SNPProbes_McCartney.txt", stringsAsFactors = FALSE, header = TRUE)
snpProbes <- snpProbes[which(snpProbes$EUR_AF >= 0.05 & snpProbes$EUR_AF <= 0.95),]
betas <- betas[!(rownames(betas) %in% crosshyb[,1]), ]
betas <- betas[!(rownames(betas) %in% unique(snpProbes$IlmnID)), ]
betas<-betas[-grep("rs", rownames(betas)),]
#only keeping the useful variables in SampleSheet
SampleSheet <- QCmetrics[!SamplesFail,]
SampleSheet <- SampleSheet[,c("Basename","ID","Sample_Name","Sex","Age","BrainRegion","SentrixID","Position","M.median","U.median","SmokingScore", "PredictedAge", "Predicted_Tissue")]
betas<-betas[,match(SampleSheet$Basename, colnames(betas))]
SampleSheet <- SampleSheet[order(rownames(SampleSheet)),]
betas <- betas[,order(colnames(betas))]
if(print(identical(rownames(SampleSheet), colnames(betas)))==TRUE){
save(SampleSheet, betas, file = params$Normalised)
print("Sample Sheet and Betas Match QC'd object is saved")
}else{
exit()
print("Sample Sheet and Betas do not match QC'd object is not saved")
}
```
The final dataset contains `r nrow(SampleSheet)` samples and `r nrow(betas)` probes.