--- title: "Analyzing Targeted Bisulfite Sequencing data with SOMNiBUS" author: - name: Kaiqiong Zhao affiliation: Department of Epidemiology, Biostatistics and Occupational Health, McGill University, Montreal, Canada date: "`r BiocStyle::doc_date()`" output: BiocStyle::html_document: highlight: pygments toc_float: true fig_width: 10 keep_md: true fig_caption: yes bibliography: bibliography.bib vignette: > %\VignetteIndexEntry{Analyzing Targeted Bisulfite Sequencing data with SOMNiBUS} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include=FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` # Installation Install the package SOMNiBUS from Bioconductor. ```{r installation, echo=TRUE, eval=FALSE} if (!requireNamespace("BiocManager", quietly=TRUE)) install.packages("BiocManager") BiocManager::install("SOMNiBUS") ``` ```{r, eval=FALSE, echo=FALSE} ROOT_PACKAGE_PATH <- paste(getwd(), "/", sep = "") devtools::document(ROOT_PACKAGE_PATH) devtools::load_all(ROOT_PACKAGE_PATH) ``` # Introduction **SOMNiBUS** aims to analyze count-based methylation data on predefined genomic regions, such as those obtained by targeted sequencing, and thus to identify differentially methylated regions (DMRs) that are associated with phenotypes or traits surch as cell types. Major advantages of **SOMNiBUS** - enable complex associations with multiple phenotypes / traits using a Generalized Additive Model approach - the modeling strategy incorporates count-based methylation error rate arguments (i.e., p0 or false positive rate and p1 or true positive rate) For a more comprehensive introduction of the **SOMNiBUS** approach, please read our SOMNiBUS paper [@Zhao2020]. ## Citation If you use this package, please cite our SOMNiBUS paper [@Zhao2020]. # Application ## Rheumatoid arthritis study Throughout this vignette, we illustrate the **SOMNiBUS** approach with analysis of a targeted region from a rheumatoid arthritis (RA) study. See `help(RAdat)` for further details. In this example, the phenotype of major interest is the RA status (coded as `RA`) and the adjusting variable is the cell type status (coded as `T_cell`) which is binary because the experiment used cell-type-separated blood samples, and methylation profiles were characterized for both T-cells and Monocytes. We will refer to both `RA` and `T_cell` as *covariates*. We are going to use the package `SOMNiBUS` to investigate the methylation patterns in this region and study association with RA status and cell type. ```{r load package, echo=TRUE, message=FALSE, warning=FALSE} library(SOMNiBUS) ``` # Input data Currently, we require a matrix-type input of the methylated reads (`Meth_Counts`) and the read depth (`Total_Counts`) for CpG sites of each sample. Inputs in another format, such as Bismark or a `BSeq` object from the **bsseq** package, will be incorporated in the future. Before using the package, the input data matrix (or data frame) should be formatted such that: 1. each row represents a CpG site 2. the first 4 columns should contain the information of `Meth_Counts` (methylated counts), `Total_Counts` (read depths), `Position` (Genomic position for the CpG site) and `ID` (sample ID) 3. the covariate(s), such as disease status or cell type composition are listed in column 5 and onwards. An example of the input data: ```{r} head(RAdat) ``` ## Filtering CpGs and samples To better use the information in the methylation dataset, on one hand, **SOMNiBUS** uses a smoothing technique (regression splines) to borrow information from the nearby CpG sites; on the other hand, our approach uses regression-based modelling to take advantage of information contained across samples. Therefore, this algorithm does not require filtering out the CpG sites that have methylation levels measured only in a small part of the samples, or the samples that have overall poor read-depths and many missing values. Our analysis of differentially methylated regions (DMRs) requires filtering only on the following two conditions: - individual CpGs that have zero reads in a particular sample (no observation available) - samples that have missing values in any of the covariates of interest (i.e missing values for `T_cell` or `RA` in the data set `RAdat`) ```{r} RAdat.f <- na.omit(RAdat[RAdat$Total_Counts != 0, ]) ``` ## Adjusting for covariates and adding interactions - we currently only accept numeric input for the covariates used to fit the model. we recommend that first you transform your categorical variables into appropriate dummy variables - interaction terms can be added in the analysis model. To do that, the program requires that users add another column of covariate values into the input data set calculated as the product of two existing covariates whose interaction is of interest. # Analysis The smooth covariate estimation and the region-wise test steps are wrapped into a function `binomRegMethModel`. See `help(binomRegMethModel)` for more details. We can use the following code to run the analysis with both covariates `T_cell` and `RA`. ```{r} out <- binomRegMethModel(data = RAdat.f, n.k = rep(5, 3), p0 = 0.003, p1 = 0.9, Quasi = FALSE, RanEff = FALSE) ``` Or, we can use the argument `covs` to specify that we only want the covariate `T_cell` in the model. ```{r, eval=FALSE} out.ctype <- binomRegMethModel(data = RAdat.f, n.k = rep(5, 3), p0 = 0.003, p1 = 0.9, covs = "T_cell") ``` ## Error rates p0 and p1 In the example data set, we have cell type separated samples. The error rates for individual samples can be estimated by a E-M algorithm [@lakhal2017smoothed] using the package `SmoothMSC`. The error rate default values, $p_0=0.003$ and $p_1=0.9$, were estimated as the average incomplete ($p_0$) or over- conversion ($1-p_1$) of the metabisulfite. These two estimated values coincide roughly with the incomplete and over conversion rates related to bisulfite sequencing experiment reported in @prochenka2015cautionary. Both parameters, p0 and p1, correspond to the false positive rate and the true positive rate respectively, where 1-p1 being the false negative rate. For experiments with samples from a tissue containing a mixture of cell types, the user could consider the following ways to specify the error rates p0 and 1-p1. - If users have conducted experiments for measuring error/conversion rates, such as adding spike-in sequences of DNA that are known in advance to be methylated or unmethylated into the bisulfite sequencing procedure, they can use the measured error rates for the input of `p0` and `p1` - One can also use the error rates (incomplete and over conversion rates) that have been previous reported in the literature. - Another option is to use our default values. ## Basis dimensions n.k: Argument `n.k` in the `binomRegMethModel` is the dimension of the basis expansion for smooth covariate effects. The exact number `n.k` used for each functional parameter is not crucial, because it only sets an upper bound. We recommend choosing a basis dimension approximately equal to the number of unique CpGs in the region divided by 20. ```{r} as.integer(length(unique(RAdat.f$Position)) / 20) ``` # Results ## testing the null hypothesis Under the null hypothesis, we are expecting no effects of the covariates over the region-wide methylation status. ```{r} out$reg.out ``` ## Estimation of the smooth covariate effects ```{r , fig.cap="The estimates (solid red lines) and 95% pointwise confidence intervals (dashed red lines) of the intercept, the smooth effect of cell type and RA on methylation levels. ", fig.height= 4, fig.width=9} binomRegMethModelPlot(out) ``` We can also force the covariate effect plots to have the same vertical range, for all covariates, by specifying `same.range= T`. ```{r , fig.cap="The estimates (solid red lines) and 95% pointwise confidence intervals (dashed red lines) of the intercept, the smooth effect of cell type and RA on methylation levels. (Same ranges of Y axis.)", fig.height= 4, fig.width=9} binomRegMethModelPlot(out, same.range = TRUE) ``` ## Predicted methylation levels First, construct a new data set for prediction. Make sure that the Position in the new data set is the same as the original input `data` in `binomRegMethModel`. ```{r} pos <- out$uni.pos my.p <- length(pos) newdata <- expand.grid(pos, c(0, 1), c(0, 1)) colnames(newdata) <- c("Position", "T_cell", "RA") ``` The predicted methylation levels can be calculated from function `binomRegMethModelPred` ```{r} my.pred <- binomRegMethModelPred(out, newdata, type = "link.scale") ``` ```{r, fig.height= 6, fig.width=6, fig.cap="The predicted methylation levels in the logit scale for the 4 groups of samples with different disease and cell type status."} plot(pos[order(pos)], (my.pred[(newdata$RA == 0 & newdata$T_cell == 0)])[order(pos)], type = "l", xlab = "Position", ylab = "Predicted methylation levels (in logit scale)", col = "blue", main = "Logit scale", ylim = c(min(my.pred), max(my.pred)), lwd = 2 ) lines(pos[order(pos)], (my.pred[(newdata$RA == 0 & newdata$T_cell == 1)])[order(pos)], type = "l", xlab = "Position", ylab = "predicted", col = "green", lwd = 2 ) lines(pos[order(pos)], (my.pred[(newdata$RA == 1 & newdata$T_cell == 0)])[order(pos)], type = "l", xlab = "Position", ylab = "predicted", col = "red", lwd = 2 ) lines(pos[order(pos)], (my.pred[(newdata$RA == 1 & newdata$T_cell == 1)])[order(pos)], type = "l", xlab = "Position", ylab = "predicted", col = "black", lwd = 2 ) legend("top", c("RA MONO", "RA TCELL", "CTRL MONO", "CTRL TCELL"), fill = c("red", "black", "blue", "green"), title = "Disease and Cell Type", bty = "n", cex = 0.8 ) ``` ```{r, fig.height= 6, fig.width=6 , fig.cap="The predicted methylation levels proportion scale (right) for the 4 groups of samples with different disease and cell type status."} my.pred <- binomRegMethModelPred(out, newdata, type = "proportion") plot(pos[order(pos)], (my.pred[(newdata$RA == 0 & newdata$T_cell == 0)])[order(pos)], type = "l", xlab = "Position", ylab = "Predicted methylation levels (in logit scale)", col = "blue", main = "Proportion scale", ylim = c(min(my.pred), max(my.pred)), lwd = 2 ) lines(pos[order(pos)], (my.pred[(newdata$RA == 0 & newdata$T_cell == 1)])[order(pos)], type = "l", xlab = "Position", ylab = "predicted", col = "green", lwd = 2 ) lines(pos[order(pos)], (my.pred[(newdata$RA == 1 & newdata$T_cell == 0)])[order(pos)], type = "l", xlab = "Position", ylab = "predicted", col = "red", lwd = 2 ) lines(pos[order(pos)], (my.pred[(newdata$RA == 1 & newdata$T_cell == 1)])[order(pos)], type = "l", xlab = "Position", ylab = "predicted", col = "black", lwd = 2 ) legend("top", c("RA MONO", "RA TCELL", "CTRL MONO", "CTRL TCELL"), fill = c("red", "black", "blue", "green"), title = "Disease and Cell Type", bty = "n", cex = 0.8 ) ``` # Session info {.unnumbered} Here is the output of `sessionInfo()` on the system on which this document was compiled running pandoc `r rmarkdown::pandoc_version()`: ```{r sessionInfo, echo=FALSE} sessionInfo() ``` # References {.unnumbered}