CpGmarker CoefficientTraining CoefficientTrainingShrunk varByCpG minByCpG
1 (Intercept) 0.695507258 0.8869198 NA NA
2 cg00075967 0.129336610 0.1080961 0.02600 0.0160
3 cg00374717 0.005017857 NA 0.01200 0.0031
4 cg00864867 1.599764050 1.8639686 0.00087 0.0000
5 cg00945507 0.056852418 NA 0.01600 0.0000
Step 1: Read methylation data
dat0 <-read.csv.sql("Horvath2013_scripts/AdditionalFile26_MethylationDataExample55.csv")nSamples <-dim(dat0)[[2]] -1nProbes <-dim(dat0)[[1]]# the following command may not be needed. But it is sometimes useful when you use read.csv.sqldat0[, 1] <-gsub(x = dat0[, 1], pattern ="\"", replacement ="")dat0[1:5,1:5]
# the following command may not be needed. But it is sometimes useful when# you use read.csv.sqldat0[, 1] <-gsub(x = dat0[, 1], pattern ="\"", replacement ="")# Create a log file which will be output into your directory# The code looks a bit complicated because it serves to create a log file (for error checks etc).# It will automatically create a log file.if (file.exists("LogFile.txt")) { file.remove("LogFile.txt")}
[1] TRUE
file.create("LogFile.txt")
[1] TRUE
DoNotProceed <-FALSEcat(paste("The methylation data set contains", nSamples,"samples (e.g. arrays) and ", nProbes, " probes."), file ="LogFile.txt")if (nSamples ==0) { DoNotProceed <-TRUEcat(paste("\n ERROR: There must be a data input error since there seem to be no samples.\n Make sure that you input a comma delimited file (.csv file)\n that can be read using the R command read.csv.sql . Samples correspond to columns in that file ."), file ="LogFile.txt", append =TRUE)}if (nProbes ==0) { DoNotProceed <-TRUEcat(paste("\n ERROR: There must be a data input error since there seem to be zero probes.\n Make sure that you input a comma delimited file (.csv file)\n that can be read using the R command read.csv.sql CpGs correspond to rows."), file ="LogFile.txt", append =TRUE)}if (nSamples > nProbes) {cat(paste("\n MAJOR WARNING: It worries me a lot that there are more samples than CpG probes.\n Make sure that probes correspond to rows and samples to columns.\n I wonder whether you want to first transpose the data and then resubmit them? In any event, I will proceed with the analysis."), file ="LogFile.txt", append =TRUE)}if (is.numeric(dat0[, 1])) { DoNotProceed <-TRUEcat(paste("\n Error: The first column does not seem to contain probe identifiers (cg numbers from Illumina) since these entries are numeric values. Make sure that the first column of the file contains probe identifiers such as cg00000292. Instead it contains ", dat0[1:3, 1]), file ="LogFile.txt", append =TRUE)}if (!is.character(dat0[, 1])) {cat(paste("\n Major Warning: The first column does not seem to contain probe identifiers (cg numbers from Illumina) since these entries are numeric values. Make sure that the first column of the file contains CpG probe identifiers such as cg00000292. Instead it contains ", dat0[1:3, 1]), file ="LogFile.txt", append =TRUE)}datout <-data.frame(Error =c("Input error. Please check the log file for details", "Please read the instructions carefully."),Comment =c("", "email Steve Horvath."))
Steps 2-4: Analysis
There are three main steps in the analysis:
Restrict the data to 21k probes and ensure they are numeric
Normalize the data
Run the methylation clock
if (!DoNotProceed) { nonNumericColumn <-rep(FALSE, dim(dat0)[[2]] -1)for (i in2:dim(dat0)[[2]]) { nonNumericColumn[i -1] <-!is.numeric(dat0[, i]) }if (sum(nonNumericColumn) >0) {cat(paste("\n MAJOR WARNING: Possible input error. The following samples contain non-numeric beta values: ", colnames(dat0)[-1][nonNumericColumn], "\n Hint: Maybe you use the wrong symbols for missing data. Make sure to code missing values as NA in the Excel file. To proceed, I will force the entries into numeric values but make sure this makes sense.\n"), file ="LogFile.txt", append =TRUE) } XchromosomalCpGs <-as.character(probeAnnotation27k$Name[probeAnnotation27k$Chr =="X"]) selectXchromosome <-is.element(dat0[, 1], XchromosomalCpGs) selectXchromosome[is.na(selectXchromosome)] <-FALSEif (sum(selectXchromosome) >=500) { meanXchromosome <-rep(NA, dim(dat0)[[2]] -1) meanXchromosome <-as.numeric(apply(as.matrix(dat0[selectXchromosome, -1]), 2, mean, na.rm =TRUE)) }if (sum(is.na(meanXchromosome)) >0) {cat(paste("\n\n Comment: There are lots of missing values for X chromosomal probes for some of the samples. This is not a problem when it comes to estimating age but I cannot predict the gender of these samples.\n "), file ="LogFile.txt", append =TRUE) } match1 <-match(probeAnnotation21kdatMethUsed$Name, dat0[, 1])if (sum(is.na(match1)) >0) { missingProbes <- probeAnnotation21kdatMethUsed$Name[!is.element(probeAnnotation21kdatMethUsed$Name, dat0[, 1])] DoNotProceed <-TRUEcat(paste("\n\n Input error: You forgot to include the following ", length(missingProbes), " CpG probes (or probe names):\n ", paste(missingProbes, sep ="", collapse =", ")), file ="LogFile.txt", append =TRUE) }# STEP 2: Restrict the data to 21k probes and ensure they are numeric match1 <-match(probeAnnotation21kdatMethUsed$Name, dat0[, 1])if (sum(is.na(match1)) >0) {stop(paste(sum(is.na(match1)), "CpG probes cannot be matched")) } dat1 <- dat0[match1, ] asnumeric1 <-function(x) {as.numeric(as.character(x)) } dat1[, -1] <-apply(as.matrix(dat1[, -1]), 2, asnumeric1)# STEP 3: Create the output file called datoutset.seed(1)# Do you want to normalize the data (recommended)? normalizeData <-TRUEsource("Horvath2013_scripts/AdditionalFile25_StepWiseAnalysis.R")# STEP 4: Output the resultsif (sum(datout$Comment !="") ==0) {cat(paste("\n The individual samples appear to be fine. "), file ="LogFile.txt", append =TRUE) }if (sum(datout$Comment !="") >0) {cat(paste("\n Warnings were generated for the following samples.\n", datout[, 1][datout$Comment !=""], "\n Hint: Check the output file for more details."), file ="LogFile.txt", append =TRUE) }}