rm(list=ls())
setwd("D:/DRD_2023/Project")
suppressWarnings(suppressMessages(library(minfi)))
baseDir <- ("D:/DRD_2023/Project") #Set the directory in which the raw data are stored
targets <- read.metharray.sheet(baseDir) #load SampleSheet
## [read.metharray.sheet] Found the following CSV files:
## [1] "D:/DRD_2023/Project/Samplesheet_report_2023.csv"
suppressWarnings(RGset <- read.metharray.exp(targets = targets))
save(RGset,file="RGset.RData")
load("D:/DRD_2023/Project/RGset.RData")
Red <- data.frame(getRed(RGset))
Green <- data.frame(getGreen(RGset))
targets
## Sample Group Sex Age Array Slide
## 1 A WT M 1 R01C01 200400320115
## 2 B MUT F 1 R02C01 200400320115
## 3 C WT M 5 R03C01 200400320115
## 4 D WT M 0 R04C01 200400320115
## 5 E MUT F 3 R02C02 200400320115
## 6 F MUT F 2 R03C02 200400320115
## 7 G WT F 9 R04C02 200400320115
## 8 H MUT F 1 R05C02 200400320115
## Basename
## 1 D:/DRD_2023/Project/200400320115_R01C01
## 2 D:/DRD_2023/Project/200400320115_R02C01
## 3 D:/DRD_2023/Project/200400320115_R03C01
## 4 D:/DRD_2023/Project/200400320115_R04C01
## 5 D:/DRD_2023/Project/200400320115_R02C02
## 6 D:/DRD_2023/Project/200400320115_R03C02
## 7 D:/DRD_2023/Project/200400320115_R04C02
## 8 D:/DRD_2023/Project/200400320115_R05C02
load("D:/DRD_2023/Project/Illumina450Manifest_clean.RData") #First, load Illumina_manifest450_clean file
#check the probe with address 18756452
droplevels(Illumina450Manifest_clean[Illumina450Manifest_clean$AddressA_ID=="18756452",]$IlmnID) #it has IlmnID = cg01523029
## [1] cg01523029
## Levels: cg01523029
Illumina450Manifest_clean[Illumina450Manifest_clean$AddressA_ID=="31763489",]
## IlmnID Name AddressA_ID
## 36 cg01311227 cg01311227 31763489
## AlleleA_ProbeSeq AddressB_ID
## 36 CTCACTCATCTAACTTCCTACRATTTACRACAACAAACTTTAATTACACC
## AlleleB_ProbeSeq Infinium_Design_Type Next_Base Color_Channel
## 36 II
## Forward_Sequence
## 36 GAACTAAACTTCTCACTCATCTGACTTCCTGCGGTTTACGACAACAGACTTTGGTTGCAC[CG]GAAAAGAACAGATTCTCTTCCGTCGCAGAGTTTCGCCATGGTAAGACCGATAGTTCCTAA
## Genome_Build CHR MAPINFO SourceSeq
## 36 37 Y 2709627 CGGTGCAACCAAAGTCTGTTGTCGTAAACCGCAGGAAGTCAGATGAGTGA
## Chromosome_36 Coordinate_36 Strand Probe_SNPs Probe_SNPs_10 Random_Loci
## 36 Y 2769627 R NA
## Methyl27_Loci UCSC_RefGene_Name UCSC_RefGene_Accession UCSC_RefGene_Group
## 36 NA RPS4Y1;RPS4Y1 NM_001008;NM_001008 1stExon;5'UTR
## UCSC_CpG_Islands_Name Relation_to_UCSC_CpG_Island Phantom
## 36 low-CpG:2769596-2769649
## DMR Enhancer HMM_Island Regulatory_Feature_Name
## 36 NA Y:2709386-2709723
## Regulatory_Feature_Group DHS
## 36 Unclassified_Cell_type_specific NA
#display the Type
probe_type = Illumina450Manifest_clean$Infinium_Design_Type[Illumina450Manifest_clean$AddressA_ID == "18756452"]
droplevels(probe_type)
## [1] II
## Levels: II
# or we can do it directly like this
interest_probe = Illumina450Manifest_clean[Illumina450Manifest_clean$AddressA_ID=="18756452",]
interest_probe = droplevels(interest_probe) # as R remembers the deleted information
interest_probe[,7]
## [1] II
## Levels: II
#we can see after running the following commadn, that there isn't an addressB_ID with this address because it is infinium II type
Illumina450Manifest_clean[Illumina450Manifest_clean$AddressB_ID=="18756452",]
## [1] IlmnID Name
## [3] AddressA_ID AlleleA_ProbeSeq
## [5] AddressB_ID AlleleB_ProbeSeq
## [7] Infinium_Design_Type Next_Base
## [9] Color_Channel Forward_Sequence
## [11] Genome_Build CHR
## [13] MAPINFO SourceSeq
## [15] Chromosome_36 Coordinate_36
## [17] Strand Probe_SNPs
## [19] Probe_SNPs_10 Random_Loci
## [21] Methyl27_Loci UCSC_RefGene_Name
## [23] UCSC_RefGene_Accession UCSC_RefGene_Group
## [25] UCSC_CpG_Islands_Name Relation_to_UCSC_CpG_Island
## [27] Phantom DMR
## [29] Enhancer HMM_Island
## [31] Regulatory_Feature_Name Regulatory_Feature_Group
## [33] DHS
## <0 rows> (or 0-length row.names)
#as it belongs to infinium Type II, with IlmnID = cg01523029 ... it has both red and green
red_fluor = Red[rownames(Red)=="18756452",]
green_fluor = Green[rownames(Green)=="18756452",]
#Optional: Making the table for each sample
table_data = data.frame(Sample = colnames(red_fluor),
Red_flour = t(red_fluor[1,]),
Green_flour=t(green_fluor[1,]), Type = probe_type)
#to avoid the non correct naming for the columns
colnames(table_data)[1] = 'Sample'
colnames(table_data)[2] <- "Red_flour"
colnames(table_data)[3] <- "Green_flour"
table_data
## Sample Red_flour Green_flour Type
## X200400320115_R01C01 X200400320115_R01C01 14589 9380 II
## X200400320115_R02C01 X200400320115_R02C01 15674 11743 II
## X200400320115_R03C01 X200400320115_R03C01 15038 11652 II
## X200400320115_R04C01 X200400320115_R04C01 14577 12261 II
## X200400320115_R02C02 X200400320115_R02C02 2038 4335 II
## X200400320115_R03C02 X200400320115_R03C02 1828 3108 II
## X200400320115_R04C02 X200400320115_R04C02 15233 11244 II
## X200400320115_R05C02 X200400320115_R05C02 382 272 II
MSet.raw = preprocessRaw(RGset)
## Loading required package: IlluminaHumanMethylation450kmanifest
MSet.raw # it has 485512 probes and 8 samples
## class: MethylSet
## dim: 485512 8
## metadata(0):
## assays(2): Meth Unmeth
## rownames(485512): cg00050873 cg00212031 ... ch.22.47579720R
## ch.22.48274842R
## rowData names(0):
## colnames(8): 200400320115_R01C01 200400320115_R02C01 ...
## 200400320115_R04C02 200400320115_R05C02
## colData names(8): Sample Group ... Basename filenames
## Annotation
## array: IlluminaHumanMethylation450k
## annotation: ilmn12.hg19
## Preprocessing
## Method: Raw (no normalization or bg correction)
## minfi version: 1.40.0
## Manifest version: 0.4.0
save(MSet.raw,file="MSet_raw.RData")
load("D:/DRD_2023/Project/MSet_raw.RData")
# considering the median of methylation and unmethylation channels for each sample, using the function getQC
qc = getQC(MSet.raw)
plotQC(qc)
all samples have high medians(all are bove the line), methylation and unmethylation signals have good quality!
load("D:/DRD_2023/Project/RGset.RData")
# to plot the intensity values for the negative controls probes in the samples
controlStripPlot(RGset, controls="NEGATIVE")
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
## Warning in type.convert.default(X[[i]], ...): 'as.is' should be specified by
## the caller; using TRUE
all values below 1000 (log2(1000)=10) and the quality is good
#Detection p-value
detP = detectionP(RGset)
failed = detP>0.05
summary(failed)
## 200400320115_R01C01 200400320115_R02C01 200400320115_R03C01
## Mode :logical Mode :logical Mode :logical
## FALSE:485467 FALSE:485486 FALSE:485484
## TRUE :45 TRUE :26 TRUE :28
## 200400320115_R04C01 200400320115_R02C02 200400320115_R03C02
## Mode :logical Mode :logical Mode :logical
## FALSE:485480 FALSE:485322 FALSE:485382
## TRUE :32 TRUE :190 TRUE :130
## 200400320115_R04C02 200400320115_R05C02
## Mode :logical Mode :logical
## FALSE:485495 FALSE:485106
## TRUE :17 TRUE :406
#the table showing the number of failed position for each sample
#with number of probes higher than the threshold(.05)
table2 = data.frame(Failed_positions = summary(failed)[3,])
table2
## Failed_positions
## 200400320115_R01C01 TRUE :45
## 200400320115_R02C01 TRUE :26
## 200400320115_R03C01 TRUE :28
## 200400320115_R04C01 TRUE :32
## 200400320115_R02C02 TRUE :190
## 200400320115_R03C02 TRUE :130
## 200400320115_R04C02 TRUE :17
## 200400320115_R05C02 TRUE :406
M = getM(MSet.raw)
# retrieve beta and M values matrices
Beta = getBeta(MSet.raw)
targets$Group
## [1] "WT" "MUT" "WT" "WT" "MUT" "MUT" "WT" "MUT"
# grouping both M and beta by their subsets
MUT_M = M[,c(2,5,6,8)]
MUT_Beta = Beta[,c(2,5,6,8)]
WT_M = M[,c(1,3,4,7)]
WT_Beta = Beta[,c(1,3,4,7)]
# Calculating the means for M and Beta values
Beta_WT_mean = apply(WT_Beta,1,mean,na.rm=T)
M_WT_mean = apply(WT_M,1,mean,na.rm=T)
beta_MUT_mean = apply(MUT_Beta,1,mean,na.rm=T)
M_MUT_mean = apply(MUT_M,1,mean,na.rm=T)
# Removing NA values if there is any
Beta_WT_mean = Beta_WT_mean[!is.na(Beta_WT_mean)]
M_WT_mean = M_WT_mean[!is.na(M_WT_mean)]
beta_MUT_mean = beta_MUT_mean [!is.na(beta_MUT_mean)]
M_MUT_mean = M_MUT_mean[!is.na(M_MUT_mean)]
# density function for calculating each density distribution
dens_mean_beta_WT = density(Beta_WT_mean)
dens_mean_M_WT = density(M_WT_mean)
dens_mean_beta_MUT = density(beta_MUT_mean)
dens_mean_M_MUT = density(M_MUT_mean )
# Plotting the density distribution
par(mfrow=c(1,2)) #pairing the 2 graphs in the same image
plot(dens_mean_beta_WT,main="Density of Beta values", col='orange')
lines(dens_mean_beta_MUT,col="purple")
plot(dens_mean_M_WT, main="Density M values", col='orange')
lines(dens_mean_M_MUT,col="purple")
WT , with orange, and MWT with purple are mostly the same , but in extreme parts WT have higher intensity
# subseting the Illumina450Manifest_clean in two dataframes, containing only type I (dfI) or type II (dfII) probes
dfI = Illumina450Manifest_clean[Illumina450Manifest_clean$Infinium_Design_Type=="I",]
dfI = droplevels(dfI)
dfII = Illumina450Manifest_clean[Illumina450Manifest_clean$Infinium_Design_Type=="II",]
dfII = droplevels(dfII)
# Now subsetting the beta matrix in order to retain only obtain only the rows whose name is in the first column of dfI or dfII
beta_I = Beta[rownames(Beta) %in% dfI$IlmnID,]
beta_II = Beta[rownames(Beta) %in% dfII$IlmnID,]
# Calculating the mean across the 8 samples
mean_of_beta_I = apply(beta_I,1,mean)
mean_of_beta_II = apply(beta_II,1,mean)
# calculating the density distribution of the 2 vectors of mean values
sd_of_beta_I = apply(beta_I,1,sd,na.rm=T)
sd_of_beta_II = apply(beta_II,1,sd,na.rm=T)
# preprocessNoob Normalization
preprocessNoob_data = preprocessNoob(RGset)
## Loading required package: IlluminaHumanMethylation450kanno.ilmn12.hg19
# Getting the beta_processNoob matrix
beta_preprocessNoob = getBeta(preprocessNoob_data)
# Dividing the obtained matrix according to typeI and typeII probes
beta_preprocessNoob_I = beta_preprocessNoob[rownames(beta_preprocessNoob) %in% dfI$IlmnID,]
beta_preprocessNoob_II = beta_preprocessNoob[rownames(beta_preprocessNoob) %in% dfII$IlmnID,]
# Computing the mean and SD
beta_preprocessNoob_I_mean = apply(beta_preprocessNoob_I, 1, mean)
beta_preprocessNoob_II_mean = apply(beta_preprocessNoob_II, 1, mean)
beta_preprocessNoob_I_sd = apply(beta_preprocessNoob_I, 1, sd)
beta_preprocessNoob_II_sd = apply(beta_preprocessNoob_II, 1, sd)
# Calculate the densities of standard deviations
d_mean_of_beta_I = density(mean_of_beta_I,na.rm=T)
d_mean_of_beta_II = density(mean_of_beta_II,na.rm=T)
d_sd_of_beta_I = density(sd_of_beta_I,)
d_sd_of_beta_II = density(sd_of_beta_II,na.rm=T)
# Plotting
par(mfrow = c(2,3)) #partition
{plot(d_mean_of_beta_I, col = "blue", main = "Mean of raw Beta values", xlim = c(0,1), ylim = c(0,6), xlab = "Beta mean", ylab = "Density")
lines(d_mean_of_beta_II, col = "purple")
legend(legend = c("TypeI", "TypeII"), col = c("blue", "purple"), "topright", lty = 1, bty = "n")}
{plot(d_sd_of_beta_I, col = "blue", main = "SD of raw Beta values", xlim = c(0,0.5), ylim = c(0,45), xlab = "Beta SD", ylab = "Density")
lines(d_sd_of_beta_II, col = "purple")
legend(legend = c("TypeI", "TypeII"), col = c("blue", "purple"), "topright", lty = 1, bty = "n")}
colorbar = c("blue","purple","orange","blue","purple","orange","blue","purple")
boxplot(Beta,ylim=c(0,1),main="Raw Beta values",las = 2, cex.axis = 0.4,col=colorbar)
{plot(density(beta_preprocessNoob_I_mean, na.rm = T), col = "blue", main = "Mean of normalized Beta values", xlim = c(0,1), ylim = c(0,4), xlab = "Beta mean", ylab = "Density")
lines(density(beta_preprocessNoob_II_mean, na.rm = T), col = "purple")
legend(legend = c("TypeI", "TypeII"), col = c("blue", "purple"), "topright", lty = 1:1, bty = "n")}
{plot(density(beta_preprocessNoob_I_sd, na.rm = T), col = "blue", main = "SD of normalize Beta values", xlim = c(0,0.5), ylim = c(0,30), xlab = "Beta SD", ylab = "Density")
lines(density(beta_preprocessNoob_II_sd, na.rm = T), col = "purple")
legend(legend = c("TypeI", "TypeII"), col = c("blue", "purple"), "topright", lty = 1:1, bty = "n")}
boxplot(beta_preprocessNoob, ylim = c(0,1), main = "Normalized Beta values", las = 2, cex.axis = 0.4, col = colorbar)
-With mean plot, type II probes are less sensitive and more centralized(Normalization is needed)
-In SD Plot: After normalization, peaks of type II tend to be closer to those of type I due to increased sensitivity to extreme methylation values.
-for the Box plot: The distributions after normalization are more comparable and more close
# boxplot according to the group
colorbar = c("purple","orange","purple","purple","orange","orange","purple","orange")
boxplot(Beta,ylim=c(0,1),main="Raw Beta values",las = 2, cex.axis = 0.4,col=colorbar)
boxplot(beta_preprocessNoob, ylim = c(0,1), main = "Normalized Beta values", las = 2, cex.axis = 0.4, col = colorbar)
The variability between the two groups is barely discernible, with distributions that are nearly identical
pca = prcomp(beta_preprocessNoob, scale = T)
par(mfrow = c(1,1))
{plot(pca$x[, 1], pca$x[, 2], cex = 2, pch = 18, xlab = "PC1", ylab = "PC2", xlim = c(-500, 800), main = "PCA")
text(pca$x[, 1], pca$x[,2], labels = rownames(pca$x), pos = 4, cex = 0.5)}
suppressMessages(library(ggplot2))
## Warning: package 'ggplot2' was built under R version 4.1.3
pcr = data.frame(pca$rotation[,1:3], Group=targets$Group)
ggplot(pcr,aes(PC1,PC2, color=Group))+geom_point(size=4)
pcr = data.frame(pca$rotation[,1:3], Group=targets$Sex)
ggplot(pcr,aes(PC1,PC2, color=Group))+geom_point(size=4)
In the PCA, we observe that the values are well organized according to both target and sex, as well as target and group.
# t test
t_test_func = function(x){
t_test = t.test(x ~ targets$Group)
return (t_test$p.value)
}
p_value_t_test = apply(beta_preprocessNoob[1:10000,], 1, t_test_func)
head(p_value_t_test, n = 10) # The first 10 entries
## cg00050873 cg00212031 cg00213748 cg00214611 cg00455876 cg01707559 cg02004872
## 0.51815743 0.06315453 0.01370822 0.06285357 0.31328547 0.09371478 0.07341355
## cg02011394 cg02050847 cg02233190
## 0.58534070 0.06261989 0.46896021
t_test_df = data.frame(beta_preprocessNoob[1:10000,], p_value_t_test)
t_test_df = t_test_df[order(t_test_df$p_value_t_test),] # it contains sorted p-value of all probes
# Bonferroni correction
p_value_Bon = p.adjust(t_test_df$p_value_t_test, "bonferroni")
# BH correction
p_value_BH = p.adjust(t_test_df$p_value_t_test, "BH")
t_test_df_corrected = data.frame(t_test_df, p_value_Bon, p_value_BH) # We can see the corrections in this dataframe
#Number of the probes with the p-value less than 0.05
dim(t_test_df_corrected[t_test_df_corrected$p_value_t_test <= 0.05,])
## [1] 915 11
#Number of the probes that has passed the Bonferroni correction
dim(t_test_df_corrected[t_test_df_corrected$p_value_Bon <= 0.05,])
## [1] 2 11
#Number of the probes that has passed the BH correction
dim(t_test_df_corrected[t_test_df_corrected$p_value_BH <= 0.05,])
## [1] 5 11
beta_corrected = t_test_df_corrected[, 1:8]
# Beta matrix with p-value corrected of WTs and MUTs
beta_corrected_wt = beta_corrected[, factor(targets$Group)=="WT"]
beta_corrected_mut = beta_corrected[, factor(targets$Group)=="MUT"]
# Calculate the Mean of WTs and MUTs
mean_beta_corrected_wt = apply(beta_corrected_wt, 1, mean)
mean_beta_corrected_mut = apply(beta_corrected_mut, 1, mean)
#Now we can calculate the difference between average values
delta = mean_beta_corrected_wt - mean_beta_corrected_mut
# Create a data frame with two columns, one containing the delta values and the other with the -log10 of p-values
volc_plot = data.frame(delta, -log10(t_test_df_corrected$p_value_t_test))
#Plot
{plot(volc_plot[, 1], volc_plot[, 2],pch = 18, cex = 0.5, xlab = "Delta", ylab = "-log10(p-value)")
abline(h = -log10(0.05), b = 0, col = "#756bb1")
hyper_met = volc_plot[abs(volc_plot[, 1] > 0.2) & volc_plot[, 2] > (-log10(0.05)),]
points(hyper_met[, 1], hyper_met[, 2], pch = 18, cex = 0.5, col = "#e34a33")
hypo_met = volc_plot[abs(volc_plot[, 1] < (-0.2)) & volc_plot[, 2] > (-log10(0.05)),]
points(hypo_met[, 1], hypo_met[, 2], pch = 18, cex = 0.5, col = "#3182bd")
legend(legend = c("Hypomethylation", "Hypermethylation"), col = c("#3182bd", "#e34a33"), "topright", bty = "n", pch = 16)}
# Converting rows with columns
t_test_df_corrected = data.frame(rownames(t_test_df_corrected), t_test_df_corrected)
colnames(t_test_df_corrected)[1] = "IlmnID"
t_test_df_corrected_annotated = merge(t_test_df_corrected, Illumina450Manifest_clean, by = "IlmnID")
library(qqman)
## Warning: package 'qqman' was built under R version 4.1.3
##
## For example usage please run: vignette('qqman')
##
## Citation appreciated but not required:
## Turner, (2018). qqman: an R package for visualizing GWAS results using Q-Q and manhattan plots. Journal of Open Source Software, 3(25), 731, https://doi.org/10.21105/joss.00731.
##
library(RColorBrewer)
## Warning: package 'RColorBrewer' was built under R version 4.1.3
#data for the plot
Man_data <- t_test_df_corrected_annotated[colnames(t_test_df_corrected_annotated) %in% c("IlmnID","CHR","MAPINFO","p_value_t_test")]
#reorder the levels of the column CHR
order_chr <- c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "X", "Y")
#convert factors of the “CHR” column to numbers
Man_data$CHR <- as.numeric(factor(Man_data$CHR, levels = order_chr))
#Manhattan plot
manhattan(Man_data, snp = "IlmnID", chr = "CHR", bp = "MAPINFO", p = "p_value_t_test", col = brewer.pal(n = 6, "Purples"), genomewideline = F, cex = 0.5, cex.axis = 0.4)
#create a bar of colors for both groups' members
colorbar <- c("#377eb8", "#d95f02", "#377eb8", "#377eb8", "#d95f02", "#d95f02", "#377eb8", "#d95f02")
my_col_2 = colorRampPalette(c("#4daf4a", "black", "#e41a1c"))(100)
library(gplots)
## Warning: package 'gplots' was built under R version 4.1.3
##
## Attaching package: 'gplots'
## The following object is masked from 'package:IRanges':
##
## space
## The following object is masked from 'package:S4Vectors':
##
## space
## The following object is masked from 'package:stats':
##
## lowess
#Creating a matrix containing our input data for the heatmaps
t_test_df_corrected <- data.frame(t_test_df, p_value_Bon, p_value_BH)
#extracting just the top 100 most significant CpG probes, best beta values for these top 100 probes and convert them in a matrix
heatmap_data = as.matrix(t_test_df_corrected[1:100, 1:8])
#Average linkage method
{heatmap.2(heatmap_data, col = my_col_2, Rowv = T, Colv = T, hclustfun = function(x) hclust(x, method = 'average'), dendrogram = "both", key = T, ColSideColors = colorbar, density.info = "none", trace = "none", scale = "none", symm = F, main = "Average linkage")
legend(legend = c("MUT", "WT"), col = c("#d95f02", "#377eb8"), "topright", pch = 15, bty = "n", cex = 0.7)}
#Single linkage method
{heatmap.2(heatmap_data, col = my_col_2, Rowv = T, Colv = T, hclustfun = function(x) hclust(x, method = 'single'), dendrogram = "both", key = T, ColSideColors = colorbar, density.info = "none", trace = "none", scale = "none", symm = F, main = "Single linkage")
legend(legend = c("MUT", "WT"), col = c("#d95f02", "#377eb8"), "topright", pch = 15, bty = "n", cex = 0.7)}
#Complete linkage method
{heatmap.2(heatmap_data, col = my_col_2, Rowv = T, Colv = T, dendrogram = "both", key = T, ColSideColors = colorbar, density.info = "none", trace = "none", scale = "none", symm = F, main = "Complete linkage")
legend(legend = c("MUT", "WT"), col = c("#d95f02", "#377eb8"), "topright", pch = 15, bty = "n", cex = 0.7)}