## ----chunk, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
    collapse = TRUE,
    fig.width = 7,
    fig.height = 5,
    out.width = "100%",
    comment = "#>",
    dev = "ragg_png"
)

## ----setup--------------------------------------------------------------------
library(mesa)
library(ggplot2)

## ----introduction-------------------------------------------------------------
setMesaParallel(nCores = 2)

## ----chunk-2------------------------------------------------------------------
exampleTumourNormal |>
    getSampleTable() |>
    dplyr::select(sample_name, tumour)

## ----chunk-3------------------------------------------------------------------
exampleTumourNormal |>
    calculateDMRs(variable = "tumour",
                contrasts = "first") 

## ----chunk-4------------------------------------------------------------------
exampleTumourNormal |>
    calculateDMRs(variable = "tumour",
                contrasts = "Tumour_vs_Normal") 

## ----chunk-5------------------------------------------------------------------
DMRs <- exampleTumourNormal |>
    calculateDMRs(variable = "tumour",
                contrasts = tibble(group1 = "Tumour", group2 = "Normal")) 

## ----chunk-6------------------------------------------------------------------
exampleTumourNormal |>
    calculateDMRs(variable = "type",
                contrasts = "all_vs_NormalLung") 

## ----output-------------------------------------------------------------------
exampleTumourNormal |>
    calculateDMRs(variable = "tumour",
                contrasts = tibble(group1 = "Tumour", group2 = "Normal")) |>
    colnames()

## ----chunk-7------------------------------------------------------------------
exampleTumourNormal |>
    calculateDMRs(variable = "tumour",
                contrasts = tibble(group1 = "Tumour", group2 = "Normal"),
                keepData = TRUE) 

## ----covariates---------------------------------------------------------------
DMRsCovariateTissue <- exampleTumourNormal |>
    calculateDMRs(variable = "tumour",
                covariates = "tissue",
                contrasts = tibble(group1 = "Tumour", group2 = "Normal")) |>
    as_tibble()

DMRsCovariateTissue

## ----chunk-8------------------------------------------------------------------
DMRsCovariatePatient <- exampleTumourNormal |>
    calculateDMRs(variable = "tumour",
                covariates = "patient", 
                contrasts = tibble(group1 = "Tumour", group2 = "Normal")) |>
    as_tibble()

DMRsCovariatePatient

## ----chunk-9, eval = FALSE----------------------------------------------------
# # There are no levels of `type` in common between the levels of `tumour`:
# exampleTumourNormal |> getSampleTable() |> dplyr::count(tumour,type)
# 
# # so this will fail with an error
# #exampleTumourNormal |>
# #  calculateDMRs(variable = "tumour",
# #                covariates = "type",
# #                contrasts = "first")

## ----additional-options-------------------------------------------------------
allWindows <- exampleTumourNormal |>
    calculateDMRs(variable = "tumour",
                contrasts = "Tumour_vs_Normal",
                covariates = "patient",
                FDRthres = 1)

allWindows |>
    mutate(significant = (Tumour_vs_Normal_adjPval <= 0.05)) |>
    ggplot(
        aes(
            x = Tumour_vs_Normal_log2FC,
            y = -log10(Tumour_vs_Normal_adjPval),
            col = significant
        )
    ) +
    geom_point() +
    theme_bw() +
    labs(x = "Log2 Fold Change",
        y = "-Log10 Adjusted P Value")

## ----chunk-10-----------------------------------------------------------------
allWindows |>
    mutate(significant = (Tumour_vs_Normal_adjPval <= 0.05)) |>
    ggplot(
        aes(
            x = Tumour_vs_Normal_deltaBeta,
            y = -log10(Tumour_vs_Normal_adjPval),
            col = significant
        )
    ) +
    geom_point() +
    theme_bw() +
    labs(x = "Difference in average Beta Values",
        y = "-Log10 Adjusted P Value")

## ----chunk-11-----------------------------------------------------------------
allWindows |>
    ggplot(aes(x = Tumour_vs_Normal_log2FC, y = Tumour_vs_Normal_deltaBeta)) +
    geom_point() +
    theme_bw() +
    labs(x = "Log2 Fold Change",
        y = "Difference in average Beta Values")

## ----annotation---------------------------------------------------------------
annoDMRs <- DMRsCovariatePatient |>
    annotateWindows(genome = "hg38")

annoDMRs |> 
    colnames()

## ----chunk-12-----------------------------------------------------------------
annoDMRs |> 
    dplyr::select(
        seqnames, start, end, distanceToTSS, ENSEMBL, SYMBOL, GENENAME
    )

## ----chunk-13-----------------------------------------------------------------
annoDMRs |> 
    dplyr::select(start, SYMBOL, shortAnno, annotation)

## ----chunk-14-----------------------------------------------------------------
annoDMRs |> 
    dplyr::select(
        geneChr, geneStart, geneEnd, geneLength, geneStrand,
        geneId, transcriptId
    )

## ----chunk-15-----------------------------------------------------------------
annoDMRs |>
    dplyr::select(
        seqnames, start, end, nIslands, nShore, nShelf, inFantom, landscape
    )

## ----other-genomes------------------------------------------------------------
exampleMouse |>
    getRegions() |>
    annotateWindows(TxDb = "TxDb.Mmusculus.UCSC.mm10.knownGene",
                    annoDb = "org.Mm.eg.db")


## ----chunk-16-----------------------------------------------------------------
setMesaTxDb("TxDb.Mmusculus.UCSC.mm10.knownGene")
setMesaAnnoDb("org.Mm.eg.db")

## ----chunk-17-----------------------------------------------------------------
exampleMouse |>
    getRegions() |>
    annotateWindows()

## ----analysing-dmrs-----------------------------------------------------------
allContrasts <- exampleTumourNormal |>
    calculateDMRs(variable = "type",
                contrasts = "all") 

## ----chunk-18-----------------------------------------------------------------
allContrasts |>
    pivotDMRsLonger() 

## ----chunk-19-----------------------------------------------------------------
allContrasts |>
    pivotDMRsLonger(makePositive = TRUE) 

## ----chunk-20-----------------------------------------------------------------
allContrasts |>
    summariseDMRsByContrast()

## ----chunk-21-----------------------------------------------------------------
allContrasts |>
    summariseDMRsByContrast(
        FDRthres = 0.001,
        log2FCthres = 2,
        deltaBetaThres = 0.2
    )

## ----chunk-22, fig.width = 10, fig.height = 8---------------------------------
allContrasts |>
    plotDMRUpset()

## ----summarising-by-gene------------------------------------------------------
annoDMRs |>
    summariseDMRsByGene()

## ----plotting-windows---------------------------------------------------------
exampleTumourNormal |>
    addLibraryInformation() |> 
    plotRegionsHeatmap(regionsToOverlap = DMRsCovariatePatient)

## ----chunk-23-----------------------------------------------------------------
exampleTumourNormal |>
    addLibraryInformation() |> 
    plotRegionsHeatmap(regionsToOverlap = DMRsCovariatePatient,
                        sampleAnnotation = tumour)

## ----chunk-24-----------------------------------------------------------------
exampleTumourNormal |>
    plotRegionsHeatmap(regionsToOverlap = DMRsCovariatePatient, 
                        sampleAnnotation = c(tumour, type))

## ----chunk-25-----------------------------------------------------------------
exampleTumourNormal |>
    addLibraryInformation() |> 
    plotRegionsHeatmap(regionsToOverlap = DMRsCovariatePatient, 
                        normMethod = "nrpm")

## ----chunk-26-----------------------------------------------------------------
exampleTumourNormal |>
    plotRegionsHeatmap(
        regionsToOverlap = DMRsCovariatePatient,
        sampleAnnotation = c("tumour", "type"),
        annotationColors = list(
            tumour = c("Normal" = "blue", "Tumour" = "firebrick4")
        )
    )

## ----chunk-27-----------------------------------------------------------------
annotColours <- list(tumour = c("Normal" = "blue", "Tumour" = "firebrick4"),
        type = c("CRC" = "orange","LUAD" = "lightgreen", "LUSC" = "darkgreen", 
                "NormalColon" = "tan", "NormalLung" = "grey"))

exampleTumourNormal |>
    plotRegionsHeatmap(regionsToOverlap = DMRs, 
                        sampleAnnotation = c("tumour", "type"),
                        annotationColors = annotColours
                        )

## ----chunk-28-----------------------------------------------------------------
exampleTumourNormal |>
    plotRegionsHeatmap(regionsToOverlap = DMRsCovariatePatient, 
                        sampleAnnotation = c("tumour", "type"),
                        annotationColors = annotColours,
                        clusterRows = TRUE,
                        )

## ----chunk-29-----------------------------------------------------------------
exampleTumourNormal |>
    plotRegionsHeatmap(regionsToOverlap = DMRsCovariatePatient, 
                        sampleAnnotation = c("tumour", "type"),
                        annotationColors = annotColours,
                        clusterNum = 3
                        )

## ----session-info, echo=FALSE-------------------------------------------------
sessionInfo()

