LSMA

LONDON SCHOOL OF MULTIOMICS & AI

LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 31 lessons

DNA methylation with methylKit

DNA methylation with methylKit

DNA methylation with methylKit

Bisulfite sequencing turns methylation into counts of C and T reads at each CpG. This lesson reads, filters and compares four samples with methylKit, and is plain about what its default test assumes.

This lesson follows ChIP-seq peaks and annotation.

Methylation, bisulfite and percentages

DNA methylation is a small chemical mark, a methyl group, added to cytosine (C), one of the four DNA bases. In mammals it is found mostly where a C is followed by a G, a pair written CpG (the "p" is the link between them). Methylation at a gene's promoter usually goes with the gene being switched off, and stretches of CpG-rich DNA called CpG islands sit at many promoters. Methylation is copied when cells divide, so it acts as a memory of cell identity, and it is often disturbed in cancer.

The standard way to measure it is bisulfite sequencing. Treating DNA with sodium bisulfite turns unmethylated C into uracil, which is read as T after amplification, while methylated C stays C. Sequence the treated DNA and align the reads, and at each CpG you count:

  • reads showing C: the CpG was methylated on those DNA molecules;
  • reads showing T: it was not.

The total of the two is the coverage, and C ÷ coverage × 100 is the percent methylation, sometimes called the beta value when written as a fraction. Each sample is a mix of many cells, so 60% means about 60% of the DNA copies carried the mark at that site. Whole-genome bisulfite sequencing (WGBS) covers almost every CpG; reduced-representation bisulfite sequencing (RRBS) uses an enzyme to enrich CpG-rich regions, which is cheaper and covers fewer sites.

Coverage matters as much as the percentage. 1 methylated read out of 2 and 500 out of 1,000 are both 50%, but only the second is precise. This is the same point as with low-count genes in Differential expression with DESeq2, and methylKit handles it in a similar spirit: it works with the counts, not only the percentages.

Reading the methylKit test files

methylKit ships four small example files covering part of chromosome 21: two "test" samples and two "control" samples. They are already summarised to one line per CpG, in a simple text format methylKit reads directly; bisulfite aligners such as Bismark produce similar per-CpG summaries.

R
library(methylKit)
ext <- system.file("extdata", package = "methylKit")
list.files(ext, pattern = "myCpG")
f <- file.path(ext, c("test1.myCpG.txt", "test2.myCpG.txt",
                      "control1.myCpG.txt", "control2.myCpG.txt"))
readLines(f[1], n = 4)
[1] "control1.myCpG.txt" "control2.myCpG.txt" "test1.myCpG.txt" [4] "test2.myCpG.txt" [1] "chrBase\tchr\tbase\tstrand\tcoverage\tfreqC\tfreqT" [2] "chr21.9764539\tchr21\t9764539\tR\t12\t25.00\t75.00" [3] "chr21.9764513\tchr21\t9764513\tR\t12\t0.00\t100.00" [4] "chr21.9820622\tchr21\t9820622\tF\t13\t0.00\t100.00"

Each line is one CpG: its position, the strand (F forward, R reverse), the coverage and the percentages of C and T reads. The first CpG had 12 reads, 25% of them C, so 3 methylated and 9 unmethylated. Now read all four into R.

R
meth_list <- methRead(as.list(f),
  sample.id  = list("test1", "test2", "ctrl1", "ctrl2"),
  assembly   = "hg18",
  treatment  = c(1, 1, 0, 0),
  context    = "CpG",
  mincov     = 10)
sapply(meth_list, nrow)
head(getData(meth_list[[1]]), 3)
getMethylationStats(meth_list[[1]], plot = FALSE, both.strands = FALSE)
getCoverageStats(meth_list[[1]], plot = FALSE, both.strands = FALSE)
[1] 1904 2006 2377 1697 chr start end strand coverage numCs numTs 1 chr21 9764513 9764513 - 12 0 12 2 chr21 9764539 9764539 - 12 3 9 3 chr21 9820622 9820622 + 13 0 13 methylation statistics per base summary: Min. 1st Qu. Median Mean 3rd Qu. Max. 0.00 0.00 58.15 48.49 91.32 100.00 … read coverage statistics per base summary: Min. 1st Qu. Median Mean 3rd Qu. Max. 10.00 16.00 43.00 62.15 86.00 818.00 …

What this does

  • methRead() reads the files into a methylRawList, one methylRaw object per sample. Each row is a CpG with its coverage and the number of C (numCs) and T (numTs) reads.
  • treatment = c(1, 1, 0, 0) says which samples are which group: 1 for test, 0 for control. The comparison later is always group 1 minus group 0.
  • assembly = "hg18" records the genome build. These test files use hg18, an even older build than hg19, so their positions cannot be compared with hg19 or hg38 annotation without converting them first.
  • mincov = 10 drops CpGs with fewer than 10 reads, so every sample keeps between 1,697 and 2,377 sites.
  • getMethylationStats() and getCoverageStats() summarise one sample. With plot = TRUE they draw histograms.

The methylation summary has a lower quartile of 0 and an upper quartile of 91%: the values pile up at the two ends. In this sample, 39% of CpGs are below 10% methylated and 26% above 90%. That two-peaked shape is typical, because most CpGs are either almost always methylated or almost never, with CpG islands in the unmethylated group. A sample without it would be worth checking for a technical problem. The coverage ranges from the minimum of 10 to 818 reads, with a median of 43: very uneven, which is why the next steps filter and normalise it.

Filter, normalise and unite

R
filt <- filterByCoverage(meth_list, lo.count = 10, lo.perc = NULL,
                         hi.count = NULL, hi.perc = 99.9)
sapply(filt, nrow)
norm <- normalizeCoverage(filt, method = "median")
meth <- unite(norm, destrand = FALSE)
nrow(meth)
head(percMethylation(meth), 3)
[1] 1902 2003 2374 1695 uniting... [1] 959 test1 test2 ctrl1 ctrl2 [1,] 58.82353 80.51471 87.87879 86.32193 [2,] 70.58824 75.93284 86.66667 74.89140 [3,] 97.43590 94.11765 100.00000 97.58065

What this does

  • filterByCoverage() removes CpGs below 10 reads and those above the 99.9th percentile of coverage in each sample. Unusually high coverage often comes from PCR duplicates or repeated sequence rather than real depth, so the very top is dropped. Here it removes two or three sites per sample.
  • normalizeCoverage() scales the read counts so that every sample has the same median coverage, the methylation equivalent of the size factors in RNA-seq counts and exploration. Percentages are unchanged; only the weights given to samples in the test change.
  • unite() keeps the CpGs measured in all four samples and puts them in one table, a methylBase object. Only 959 CpGs pass, from around 2,000 per sample: RRBS samples overlap imperfectly, and a site missing from one sample is lost. min.per.group relaxes this, keeping sites measured in at least that many samples per group.
  • destrand = FALSE keeps the two strands of a CpG separate. A CpG reads CG on both strands, and its two Cs are usually methylated alike, so destrand = TRUE merges them to gain coverage. It is recommended only for CpG context.
  • percMethylation() returns a CpG-by-sample matrix of percentages.
R
getCorrelation(meth, plot = FALSE)
clusterSamples(meth, dist = "correlation", method = "ward.D", plot = FALSE)
pc <- PCASamples(meth, obj.return = TRUE, screeplot = FALSE)
summary(pc)$importance[, 1:3]
round(pc$x[, 1:2], 1)
test1 test2 ctrl1 ctrl2 test1 1.0000000 0.9249909 0.8766425 0.8734230 test2 0.9249909 1.0000000 0.8789836 0.8798168 ctrl1 0.8766425 0.8789836 1.0000000 0.9466738 ctrl2 0.8734230 0.8798168 0.9466738 1.0000000 … PC1 PC2 PC3 Standard deviation 15.41738 11.40656 10.54489 Proportion of Variance 0.49623 0.27163 0.23214 Cumulative Proportion 0.49623 0.76786 1.00000 PC1 PC2 test1 -17.0 11.3 test2 -9.1 -15.3 ctrl1 13.3 -1.1 ctrl2 12.8 5.1

These are the same sample checks as for RNA-seq in Clustering, PCA and heatmaps. The two controls correlate at 0.95 and the two test samples at 0.92, while every test–control pair is lower, at 0.87 to 0.88. PC1, with half of the variance, puts both test samples on the negative side and both controls on the positive side. The groups separate, so the differential test that follows is not fighting a hidden structure. With plotting on, clusterSamples() draws the dendrogram and PCASamples() the PCA plot.

Differential methylation

A differentially methylated cytosine (DMC) is a CpG whose methylation differs between groups by more than chance would explain.

R
dm <- calculateDiffMeth(meth, mc.cores = 1)
head(getData(dm), 3)

dm25 <- getMethylDiff(dm, difference = 25, qvalue = 0.01)
nrow(dm25)
nrow(getMethylDiff(dm, difference = 25, qvalue = 0.01, type = "hyper"))
nrow(getMethylDiff(dm, difference = 25, qvalue = 0.01, type = "hypo"))
two groups detected: will calculate methylation difference as the difference of treatment (group: 1) - control (group: 0) chr start end strand pvalue qvalue meth.diff 1 chr21 9853296 9853296 + 0.0005553605 0.0009984839 -6.5062389 2 chr21 9853326 9853326 + 0.7943791444 0.4893144163 0.5780188 3 chr21 9860126 9860126 + 0.0096183586 0.0130824321 -4.0598247 [1] 135 [1] 84 [1] 51

What this does

  • calculateDiffMeth() fits, for each CpG, a logistic regression: a model for the chance that a read is methylated, with group as the predictor, and tests whether group matters. It is the kind of model you met in Counts and proportions.
  • meth.diff is test minus control, in percentage points. getMethylDiff() keeps the CpGs whose difference is larger than 25 points in either direction and whose q-value is below 0.01.
  • type = "hyper" keeps CpGs more methylated in the test group (84 here), and type = "hypo" those less methylated (51).

Both thresholds are choices. Relaxing them to a 10-point difference and q < 0.05 gives 297 CpGs instead of 135. As with fold-change cut-offs in Volcano plot and interpretation, decide them before you look and report them.

What methylKit does by default, honestly stated

Three defaults are worth knowing, because they shape the numbers above.

  • No overdispersion. By default the model treats every read as an independent coin toss, using a chi-squared test. Real replicates vary more than that, for the same biological reasons RNA-seq replicates do (Differential expression with DESeq2). Pooling reads this way makes a deep sample count as a lot of evidence, and p-values come out too small. overdispersion = "MN" adds a correction; run with it, the same thresholds give 61 CpGs instead of 135.
  • Weighted differences. meth.diff is computed from the pooled reads of each group, so a sample with many reads counts for more. The worked example below shows how much that can matter.
  • SLIM q-values. The p-values are adjusted with methylKit's own SLIM method, not Benjamini–Hochberg. SLIM estimates the share of true nulls differently, and its q-values can be smaller than the raw p-values, as in row 2 above (p 0.79, q 0.49). adjust = "BH" gives the familiar adjustment.

None of these is wrong, but with only two samples per group the defaults are generous. Use the overdispersion correction when you have replicates, and say which settings you used.

Worked example · the first CpG's difference

The first CpG after normalisation: test1 has 10 methylated reads out of 17, test2 438 of 544; ctrl1 29 of 33, ctrl2 1,035 of 1,199.

pooled test: (10 + 438) ÷ (17 + 544) = 448 ÷ 561 = 79.86%
pooled control: (29 + 1,035) ÷ (33 + 1,199) = 1,064 ÷ 1,232 = 86.36%
weighted difference = 79.86 − 86.36 = −6.51 points (methylKit's meth.diff)
plain means: (58.82 + 80.51) ÷ 2 = 69.67 and (87.88 + 86.32) ÷ 2 = 87.10
plain difference = 69.67 − 87.10 = −17.43 points

Step by step. methylKit adds up the reads within each group before dividing, so test2, with 544 reads, almost decides the test group's value, and test1, with 17, hardly counts. The plain mean gives each sample an equal vote.

What the answer means. The two summaries differ by more than 10 points at the same site. Weighting by reads is sensible when depth reflects precision, but it lets one deep sample stand in for a group. When samples differ widely in coverage, look at the per-sample percentages before you trust a difference.

Regions rather than single CpGs

Neighbouring CpGs tend to be methylated alike, and a change across a stretch of DNA is more convincing, and easier to link to a gene, than a change at one site. A simple way to work with regions is to cut the genome into fixed windows, or tiles, and add up the reads in each.

R
tiles <- tileMethylCounts(meth_list, win.size = 1000, step.size = 1000, cov.bases = 10)
sapply(tiles, nrow)
meth_t <- unite(tiles, destrand = FALSE)
nrow(meth_t)
dm_t <- calculateDiffMeth(meth_t, mc.cores = 1)
dmr <- getMethylDiff(dm_t, difference = 25, qvalue = 0.01)
nrow(dmr)

dmc_gr <- as(dm25, "GRanges")
[1] 35 46 48 39 [1] 27 [1] 4

tileMethylCounts() sums the C and T reads of all CpGs in each 1,000-base window, keeping windows with at least 10 CpGs (cov.bases = 10). step.size equal to win.size makes the windows touch without overlapping. After uniting, 27 windows are measured in all four samples, and 4 pass the same thresholds: these are differentially methylated regions (DMRs) in the simplest sense. Fixed tiles are crude, because a real DMR rarely lines up with a window; dedicated DMR callers find the boundaries from the data. They are a quick, transparent start. Their q-values here are extremely small because thousands of reads are pooled per tile with no overdispersion, which is the first default above, magnified.

The last line turns the DMCs into a GRanges, the form you met in Genomic ranges with Bioconductor. From there, findOverlaps() against promoters or annotatePeak() from ChIP-seq peaks and annotation tell you which genes the changes sit near, provided the annotation uses the same build as the data.

Common mistakes

  • Treating percentages as if coverage did not matter. A CpG at 0% from 10 reads and one at 0% from 500 are not equally certain.
  • Ignoring the treatment coding. meth.diff is group 1 minus group 0; swap the codes and every sign flips.
  • Annotating with the wrong build. The test files are hg18. Positions must be converted before they meet hg19 or hg38 genes.
  • Calling one CpG a regulatory change. Look for several neighbouring CpGs, or a region, moving together.

methylKit is described in Akalin et al. 2012, "methylKit: a comprehensive R package for the analysis of genome-wide DNA methylation profiles" (Genome Biology). The SLIM adjustment is from Wang, Tuominen & Tsai 2011 (Bioinformatics). Bismark, a widely used bisulfite aligner and methylation caller, is described in Krueger & Andrews 2011 (Bioinformatics). For methods that model replicate variation in more depth, see DSS (Feng, Conneely & Wu 2014, Nucleic Acids Research). Report the package version, the coverage filters, the test, the overdispersion setting and the multiple-testing method.

Further reading · Module 7Akalin et al. 2012: methylKit and its statistical choices. · All reading for this course

Check your understanding

1. At one CpG a sample has 30 C reads and 10 T reads after bisulfite sequencing. What is its percent methylation?

2. Why did unite() keep only 959 CpGs when each sample had about 2,000?

3. Adding overdispersion = "MN" cut the CpGs passing the thresholds from 135 to 61. Why?

Ask LSMA