---
title: "Identification of differential methylated predefined regions in individual samles using edgeR"
author: "Tim Meese"
date: "`r format(Sys.Date(), '%d-%m-%Y')`"
output:
  html_document:
    df_print: paged
editor_options:
  chunk_output_type: inline
---

```{r, warning=FALSE}
knitr::opts_chunk$set(echo=FALSE, warning=FALSE)
```

```{r, message=FALSE, warning=FALSE}
# Load packages
library(tidyverse)
library(gtools)
library(edgeR)
library(rtracklayer)
library(writexl)
```


```{r}
# Current date
current_date <- format(Sys.Date(), "%d_%m_%Y")

proj_dir <- "/home/tmeese/projects/20202711_wgbs_an_vanden_broeck"
genome_dir <- file.path(proj_dir, "genome")
data_dir <- file.path(proj_dir, "bismark_methylation")
```

```{r}
results_dir <- file.path(proj_dir, paste(current_date, "results_edger", sep = "_"))

if (! dir.exists(paths = results_dir)){
    dir.create(results_dir)
}
```

We used the genome of Populus trichocarpa v3 release 48

```{r, message=FALSE, warning=FALSE}
# Read in the gtf to define genes and promoters
gff <- rtracklayer::readGFFAsGRanges(filepath = 
                                         file.path(genome_dir,
                                                   "Populus_trichocarpa.Pop_tri_v3.48.gff3.gz"))
```

Identified methylated sites were first assigned to relevant predefined regions with the R-package GenomicRanges
We use the genome of *Populus trichocarpa*. Contigs not mapped are removed.



```{r}
# Clean gff
# Remove contigs
gff_clean <- keepSeqlevels(x = gff, value = 1:19, pruning.mode = "coarse")

# Remove score, phase, parent transcript_id Name constitutive ensembl_end_phase ensembl_phase
# exon_idn, rank, protein_id, external_nale
mcols(gff_clean)[, c(3:4, 6, 11:20)] <- NULL

# Remove parts between brackets in description
gff_clean$description <- str_remove(string = gff_clean$description, 
                                    pattern = " \\[.+\\]")
```


The positions of genes, promoters are obtained from gff

```{r}
# Keep only genes
gff_genes <- gff_clean[gff_clean$type == "gene", ]

# Promoters
gff_prom <- promoters(x  = gff_genes)

# 5 prime UTR
#gff_five_prime_UTR <- gff_clean[gff_clean$type == "five_prime_UTR", ]

# 5 prime UTR don't have gene_id. Create one g  1:nrow()
#gff_five_prime_UTR$gene_id <- paste0("g", 1:length(gff_five_prime_UTR))

# 3' UTR
#gff_three_prime_UTR <- gff_clean[gff_clean$type == "three_prime_UTR", ]

#gff_three_prime_UTR$gene_id <- paste0("g", 1:length(gff_three_prime_UTR))
```

**remove hypotehtical proteins, focus on genes and promoters**     
We focus on gene bodies and promoters as predefined regions to limit the number of pairwise comparisons in the next steps.

```{r}
# Remove hypothetical proteins
gff_genes <- subset(gff_genes, description != "hypothetical protein")

gff_proms <- promoters(x  = gff_genes)
```

the number of genes in the gff file is `r length(gff_genes)`.       
`r sum(gff_genes$description == "hypothetical protein")` ; "hypothetical protein" are removed.

A promoter is defined as 2000 bp before transcription start site en 200 bp after transcription stop site.

 We defined gene-body methylation (gbM) as enrichment of CpG-only methylation within gene bodies with a depletion of methylation at transcription start site (TSS) and transcription termination sites (TTS) [6]. Promoters were defined as the region between 2000 bp upstream and 200 bp downstream the TSS.


```{r}
# Create gene_id2description
genes2description <- setNames(object = gff_genes$description, 
                              nm = gff_genes$gene_id)
```


# Intro

Differential features between groups.

Samples of 16 individual Lombardy poplars grown in a common environment were grouped based on their common ortet; ‘HUN4’, ‘ITS3’, ‘SPC1’, ‘UKD2’ located in Hungary, Italy, Spain and the UK, respectively. The location of the ortet is the covariate.


```{r}
coldata <- data.frame(
    sample = paste0("WGBS", 
                    c("01", "05", "14", "02", "06", "10", "15", "04", 
                      "07", "09", "11", "13", "03", "08", "12", "16")),
    
    F0_mother_tree = rep(c("HUN4", "ITS3", "SPC1", "UKD2"), c(3, 4, 5, 4)),
    
    sampling_date = c("31-03-2017", "25-07-2017", "12-06-2018",
                      "31-03-2017", "25-07-2017", "12-06-2018", "12-06-2018", 
                      "31-03-2017", "25-07-2017", "12-06-2018", "12-06-2018", "12-06-2018",
                      "31-03-2017", "25-07-2017", "12-06-2018", "12-06-2018")
)
```



```{r}
print(coldata)
```

```{r}
table(coldata[["sampling_date"]], coldata[["F0_mother_tree"]])
```


```{r}
# Add year to coldata
# Split sampling date on - and select last part
coldata[["year"]] <- sapply(strsplit(x = coldata[["sampling_date"]], split = "-"), function(x) {x[[3]]})

table(coldata[["year"]], coldata[["F0_mother_tree"]])
```

We identified DMRs by grouping the WGBS data from the DNA samples of individual plants by their corresponding parent-of-origin (ortet located in Hungary, Italy, Spain and the UK, respectively). This resulted in four groups with three (Hungary), four (Italy), five (Spain) and four (UK) biological replicates per group. We identified statistically significant DMRs in between-group pairwise comparisons for two pre-defined regions (genes and promoters) for each of the three sequence contexts (CpG-, CHG, CHH-DMRs), resulting in 36 pairwise comparisons. 

- 3 sequence contexts: CpG, CHG en CHH.       
- 4 groups of ortets (6 pairwise comparisons)   
- predefined regions; genes, promoters, 5' UTR en 3'UTR.    

resulting in 72 output files.


```{r}
# Create vectors with path to data
# Coverate files
files_cpg <- file.path(data_dir, paste0("CpG_coverage_", coldata[["sample"]], ".gz.bismark.cov.gz"))
files_chg <- file.path(data_dir, paste0("CHG_coverage_", coldata[["sample"]], ".gz.bismark.cov.gz"))
files_chh <- file.path(data_dir, paste0("CHH_coverage_", coldata[["sample"]], ".gz.bismark.cov.gz"))

# Give names
names(files_cpg) <- str_extract(string = files_cpg, 
                                pattern = "S\\d{2}")
names(files_chg) <- str_extract(string = files_chg, 
                                pattern = "S\\d{2}")
names(files_chh) <- str_extract(string = files_chh, 
                                pattern = "S\\d{2}")
```

```{r}
# Create vector that links name to group
sample2group <- setNames(nm = coldata[["sample"]], 
                         object = coldata[["F0_mother_tree"]])

# Remove WGBS from names
names(sample2group) <- str_remove_all(string = names(sample2group), pattern = "WGB")
```

```{r}
# Create list with all possible combinations
combn_mother_tree <- combn(x = unique(coldata[["F0_mother_tree"]]), m = 2, simplify = FALSE)
```

```{r}
# Self defined functions #
##########################

# Function to filter DGE with the method described in the dmrseq package, i.e. keep only loci that are present in each sample
filter_dge <- function(Dge){
    # Separate methylation from not methylated
    Methylation <- gl(2, 1, ncol(Dge), labels = c("Me", "Un"))
    Me <- Dge$counts[, Methylation == "Me"]
    Un <- Dge$counts[, Methylation == "Un"]
    Coverage <- Me + Un
    
    # Rules for filtering
    # Apply filter from dmrseq, i.e. at least one read in each sample
    # Coverage == 0 --> TRUE/FALSE --> Keeps only rows that have 0 and those are rows where all not everything is FALSE 
    HasCoverage <- rowSums(Coverage == 0) == 0
    
    # Filter
    dge_fil <- Dge[HasCoverage, , keep.lib.sizes = FALSE]
    
    # Print messages
    cat("Voor filteren waren er", format(nrow(Dge), big.mark = " ", scientific = FALSE), "features\n")
    cat("Na filteren zijn er", format(nrow(dge_fil), big.mark = " ", scientific = FALSE), "features\n")
    
    # Fix lib size
    TotalLibSize <- 0.5*dge_fil$samples$lib.size[Methylation=="Me"] + 0.5*dge_fil$samples$lib.size[Methylation=="Un"]
    
    dge_fil$samples$lib.size <- rep(TotalLibSize, each=2)
    
    # Return dge_fil
    dge_fil
}

# Return M-value and ratio
return_mvalue_ratio <- function(Dge_fil){
    # Calculate Methylation 
    Methylation <- gl(2, 1, ncol(Dge_fil), labels = c("Me", "Un"))
    
    # Get methylation and unmethylated and calculate M value
    Me <- Dge_fil$counts[, Methylation == "Me"]
    Un <- Dge_fil$counts[, Methylation == "Un"]
    M <- log2(Me + 2) - log2(Un + 2)

    colnames(M) <- str_remove(string = colnames(M), pattern = "-Me")
    
    M
    
    # Get ratio Me/Cov
    #ratio <- Me / (Me + Un)
    #colnames(ratio) <- str_remove(string = colnames(ratio), pattern = "-Me")
    
    # Return M value
    #return(list(M_value = M, 
    #            ratio = ratio))
}

group_dge <- function(Dge_fil, Gff){
    # Convert dge to Gr
    gr <- GRanges(seqnames = Dge_fil$genes$Chr, 
                      ranges = IRanges(start = Dge_fil$genes$Locus, width = 1))
    
    # Find overlaps
    fo <- findOverlaps(query = gr, subject = Gff)
    
    # Subset dge
    dge_overlap <- Dge_fil[queryHits(fo), ]
    
    # Add feature name
    dge_overlap$genes$feature <- Gff$gene_id[subjectHits(fo)]
    
    # Group
    dge_group <- rowsum(dge_overlap, dge_overlap$genes$feature, reorder = FALSE)
    
    # Remover feature
    dge_group$genes$feature <- NULL
    
    # Return
    dge_group
}

# plot mds and variance explained
plot_mds <- function(mydata, mydata_type, sample2group){
    par(mfrow=c(1,2))
    
    # Colour points based on group. 
    mds_cpg <- plotMDS(x = mydata, plot = TRUE, 
                       col =  as.numeric(as.factor(sample2group[colnames(mydata)])),
                       main = paste("Based on", mydata_type))

    barplot(mds_cpg$var.explained * 100, main = "Variance Explained")
}

create_volcano_plot <- function(Res){
    # Volcano plot
    p <- ggplot(Res, aes(logFC, -log10(PValue))) + 
        geom_point(aes(colour = is_differential)) +
        scale_color_manual(values = c("black", "red")) + 
        geom_hline(yintercept = -log10(0.05), linetype="dashed") +
        geom_vline(xintercept = -1, linetype="dashed") +
        geom_vline(xintercept = 1, linetype="dashed") +
        xlab("log2FC (effect size)") + 
        ylab("-log10 p-value (statistical significance)") + 
        theme_classic()

    # Return plot
    p
}
```

# Analysis with edgeR

We first summarized the identified methylated sites into known regions; promotors and gene bodies. Thereafter, we tested these predefined regions for differential methylation.  We used R version 4.1.1 (R Core Team, 2021) for all R packages. We applied the Bioconductor package edgeR originally developed for RNA-seq data and later adapted for methylation data of mammalian genomes, on the more complex plant methylome. 

For each analysis, we plot a MDS graph, a histogram with p-values and a MA plot (y-as is the logFoldChange)


## CpG 

```{r}
# Read in the data cpg context
dge_all_cpg <- readBismark2DGE(files = files_cpg, 
                               sample.names = names(files_cpg), 
                               verbose = FALSE)

# Add information to dge$samples
dge_all_cpg[["samples"]][["condition"]] <- sample2group[unlist(
    sapply(strsplit(x = rownames(dge_all_cpg[["samples"]]), split = "-"), function(x) {
        x[[1]]
        }
        ))]

# Filter as described in dmrseq vignette
dge_cpg_fil <- filter_dge(Dge = dge_all_cpg)
```


```{r}
# Remove dge_all_cpg because it is not needed anymore
rm(dge_all_cpg); gc()
```


```{r}
# Create list to store results Cp
# 24 = 4 features and 6 combinations
results_cpg <- vector(mode = "list")
```

### Genniveau

```{r}
dge_cpg_gene <- group_dge(Dge_fil = dge_cpg_fil, 
                          Gff = gff_genes)
```


```{r}
for (comb_ in combn_mother_tree){
    # Create comparison
    comparison <- paste(paste(comb_, collapse = "_"), "CpG", "gene", sep = "_")
    
    # Subset dge
    dge_sub <- dge_cpg_gene[, dge_cpg_gene[["samples"]][["condition"]] %in% comb_]
    
    #Print dge_sub[["samples"]]
    print(dge_sub[["samples"]])
    
    # For practical purposes, create variable Group based on condition (but only pick -Me)
    group <- factor(dge_sub[["samples"]][["condition"]][grepl(pattern = "-Me", 
                                                              rownames(dge_sub[["samples"]]))
                                                        ])
    
    # Create MDS plot
    M_value <- return_mvalue_ratio(Dge_fil = dge_sub)
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    png(file.path(results_dir, paste0(current_date, "_mds_plot_", comparison, ".png")))
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    dev.off()
    
    # Create model matrix, only two groups so with intercept
    # Also print d, should have one column for each sample
    d <- modelMatrixMeth(~group)
    
    print(d)
    
    # Estimate dispersion
    dge_sub <- estimateDisp(y = dge_sub, design = d)
    
    # Fit model 
    fit <- glmFit(y = dge_sub, design = d)
    
    # LRT
    lrt <- glmLRT(fit)
    
    # Toptags
    tt <- topTags(lrt, n = Inf)
    
    # Print decide test
    print(summary(decideTests(lrt)))
    
    # Histogram of pvalues
    hist(tt[["table"]][["PValue"]], 
         main = paste("Histogram of p-values for:", comparison, sep = " "), 
         xlab = "p-value")
    
    # MD plot
    plotMD(lrt)
    
    # Create result to store in Excel
    # Add column is differential. At the moment, just based on FDR value
    res <- tt[["table"]]
    res[["gene"]] <- rownames(res)
    res <- res[, c(7, 1:6)]
    res[["is_differential"]] <- ifelse(res[["FDR"]] <=  0.05, TRUE, FALSE)
    res[["description"]] <- genes2description[res[["gene"]]]
    
    writexl::write_xlsx(x = res, 
                        path = file.path(results_dir, 
                                         paste0(current_date, "_results_", comparison, ".xlsx"))
    )
                            
    
    # Create volcanoplot
    
    vol_p <- create_volcano_plot(Res = res)
    
    ggsave(filename = paste0(current_date, "_volcano_plot_", comparison, ".png"), 
           path = results_dir, 
           plot = vol_p, 
           device = "png")
    
    print(vol_p)
    
    
    # Create list with relevant results to store
    res_one_comp <- list(fit = fit, 
                         res = res)
    
    results_cpg[[comparison]] <- res_one_comp
    
    
    rm(comparison, dge_sub, group, M_value, d, fit, lrt, tt, res, vol_p, res_one_comp)
}

# Remove dge
rm(dge_cpg_gene); gc()
```


### Promotorniveau

```{r}
dge_cpg_prom <- group_dge(Dge_fil = dge_cpg_fil, 
                          Gff = gff_proms)
```


```{r}
for (comb_ in combn_mother_tree){
    # Create comparison
    comparison <- paste(paste(comb_, collapse = "_"), "CpG", "promotor", sep = "_")
    
    # Subset dge
    dge_sub <- dge_cpg_prom[, dge_cpg_prom[["samples"]][["condition"]] %in% comb_]
    
    #Print dge_sub[["samples"]]
    print(dge_sub[["samples"]])
    
    # For practical purposes, create variable Group based on condition (but only pick -Me)
    group <- factor(dge_sub[["samples"]][["condition"]][grepl(pattern = "-Me", 
                                                              rownames(dge_sub[["samples"]]))
                                                        ])
    
    # Create MDS plot
    M_value <- return_mvalue_ratio(Dge_fil = dge_sub)
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    png(file.path(results_dir, paste0(current_date, "_mds_plot_", comparison, ".png")))
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    dev.off()
    
    # Create model matrix, only two groups so with intercept
    # Also print d, should have one column for each sample
    d <- modelMatrixMeth(~group)
    
    print(d)
    
    # Estimate dispersion
    dge_sub <- estimateDisp(y = dge_sub, design = d)
    
    # Fit model 
    fit <- glmFit(y = dge_sub, design = d)
    
    # LRT
    lrt <- glmLRT(fit)
    
    # Toptags
    tt <- topTags(lrt, n = Inf)
    
    # Print decide test
    print(summary(decideTests(lrt)))
    
    # Histogram of pvalues
    hist(tt[["table"]][["PValue"]], 
         main = paste("Histogram of p-values for:", comparison, sep = " "), 
         xlab = "p-value")
    
    # MD plot
    plotMD(lrt)
    
    # Create result to store in Excel
    # Add column is differential. At the moment, just based on FDR value
    res <- tt[["table"]]
    res[["gene"]] <- rownames(res)
    res <- res[, c(7, 1:6)]
    res[["is_differential"]] <- ifelse(res[["FDR"]] <=  0.05, TRUE, FALSE)
    res[["description"]] <- genes2description[res[["gene"]]]
    
    writexl::write_xlsx(x = res, 
                        path = file.path(results_dir, 
                                         paste0(current_date, "_results_", comparison, ".xlsx"))
    )
                            
    
    # Create volcanoplot
    
    vol_p <- create_volcano_plot(Res = res)
    
    ggsave(filename = paste0(current_date, "_volcano_plot_", comparison, ".png"), 
           path = results_dir, 
           plot = vol_p, 
           device = "png")
    
    print(vol_p)
    
    
    # Create list with relevant results to store
    res_one_comp <- list(fit = fit, 
                         res = res)
    
    results_cpg[[comparison]] <- res_one_comp
    
    
    rm(comparison, dge_sub, group, M_value, d, fit, lrt, tt, res, vol_p, res_one_comp)
}

# Remove dge
rm(dge_cpg_prom); gc()
```


```{r}
# Save results_cpg
saveRDS(object = results_cpg, 
        file = file.path(results_dir, paste0(current_date, "_results_cpg.rds"))
        )


# Remove results_cpg
rm(results_cpg); gc() 
```



## CHG

```{r}
# Read in the data cpg context
dge_all_chg <- readBismark2DGE(files = files_chg, 
                               sample.names = names(files_chg), 
                               verbose = FALSE)

# Add information to dge$samples
dge_all_chg[["samples"]][["condition"]] <- sample2group[unlist(
    sapply(strsplit(x = rownames(dge_all_chg[["samples"]]), split = "-"), function(x) {
        x[[1]]
        }
        ))]

# Filter as described in dmrseq vignette
dge_chg_fil <- filter_dge(Dge = dge_all_chg)
```


```{r}
# Remove dge_all_cpg because it is not needed anymore
rm(dge_all_chg); gc()
```


```{r}
# Create list to store results Cp
# 24 = 4 features and 6 combinations
results_chg <- vector(mode = "list")
```

### Genniveau

```{r}
dge_chg_gene <- group_dge(Dge_fil = dge_chg_fil, 
                          Gff = gff_genes)
```


```{r}
for (comb_ in combn_mother_tree){
    # Create comparison
    comparison <- paste(paste(comb_, collapse = "_"), "CHG", "gene", sep = "_")
    
    # Subset dge
    dge_sub <- dge_chg_gene[, dge_chg_gene[["samples"]][["condition"]] %in% comb_]
    
    #Print dge_sub[["samples"]]
    print(dge_sub[["samples"]])
    
    # For practical purposes, create variable Group based on condition (but only pick -Me)
    group <- factor(dge_sub[["samples"]][["condition"]][grepl(pattern = "-Me", 
                                                              rownames(dge_sub[["samples"]]))
                                                        ])
    
    # Create MDS plot
    M_value <- return_mvalue_ratio(Dge_fil = dge_sub)
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    png(file.path(results_dir, paste0(current_date, "_mds_plot_", comparison, ".png")))
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    dev.off()
    
    # Create model matrix, only two groups so with intercept
    # Also print d, should have one column for each sample
    d <- modelMatrixMeth(~group)
    
    print(d)
    
    # Estimate dispersion
    dge_sub <- estimateDisp(y = dge_sub, design = d)
    
    # Fit model 
    fit <- glmFit(y = dge_sub, design = d)
    
    # LRT
    lrt <- glmLRT(fit)
    
    # Toptags
    tt <- topTags(lrt, n = Inf)
    
    # Print decide test
    print(summary(decideTests(lrt)))
    
    # Histogram of pvalues
    hist(tt[["table"]][["PValue"]], 
         main = paste("Histogram of p-values for:", comparison, sep = " "), 
         xlab = "p-value")
    
    # MD plot
    plotMD(lrt)
    
    # Create result to store in Excel
    # Add column is differential. At the moment, just based on FDR value
    res <- tt[["table"]]
    res[["gene"]] <- rownames(res)
    res <- res[, c(7, 1:6)]
    res[["is_differential"]] <- ifelse(res[["FDR"]] <=  0.05, TRUE, FALSE)
    res[["description"]] <- genes2description[res[["gene"]]]
    
    writexl::write_xlsx(x = res, 
                        path = file.path(results_dir, 
                                         paste0(current_date, "_results_", comparison, ".xlsx"))
    )
                            
    
    # Create volcanoplot
    
    vol_p <- create_volcano_plot(Res = res)
    
    ggsave(filename = paste0(current_date, "_volcano_plot_", comparison, ".png"), 
           path = results_dir, 
           plot = vol_p, 
           device = "png")
    
    print(vol_p)
    
    
    # Create list with relevant results to store
    res_one_comp <- list(fit = fit, 
                         res = res)
    
    results_chg[[comparison]] <- res_one_comp
    
    
    rm(comparison, dge_sub, group, M_value, d, fit, lrt, tt, res, vol_p, res_one_comp)
}

# Remove dge
rm(dge_chg_gene); gc()
```


### Promotorniveau

```{r}
dge_chg_prom <- group_dge(Dge_fil = dge_chg_fil, 
                          Gff = gff_proms)
```


```{r}
for (comb_ in combn_mother_tree){
    # Create comparison
    comparison <- paste(paste(comb_, collapse = "_"), "CHG", "promotor", sep = "_")
    
    # Subset dge
    dge_sub <- dge_chg_prom[, dge_chg_prom[["samples"]][["condition"]] %in% comb_]
    
    #Print dge_sub[["samples"]]
    print(dge_sub[["samples"]])
    
    # For practical purposes, create variable Group based on condition (but only pick -Me)
    group <- factor(dge_sub[["samples"]][["condition"]][grepl(pattern = "-Me", 
                                                              rownames(dge_sub[["samples"]]))
                                                        ])
    
    # Create MDS plot
    M_value <- return_mvalue_ratio(Dge_fil = dge_sub)
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    png(file.path(results_dir, paste0(current_date, "_mds_plot_", comparison, ".png")))
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    dev.off()
    
    # Create model matrix, only two groups so with intercept
    # Also print d, should have one column for each sample
    d <- modelMatrixMeth(~group)
    
    print(d)
    
    # Estimate dispersion
    dge_sub <- estimateDisp(y = dge_sub, design = d)
    
    # Fit model 
    fit <- glmFit(y = dge_sub, design = d)
    
    # LRT
    lrt <- glmLRT(fit)
    
    # Toptags
    tt <- topTags(lrt, n = Inf)
    
    # Print decide test
    print(summary(decideTests(lrt)))
    
    # Histogram of pvalues
    hist(tt[["table"]][["PValue"]], 
         main = paste("Histogram of p-values for:", comparison, sep = " "), 
         xlab = "p-value")
    
    # MD plot
    plotMD(lrt)
    
    # Create result to store in Excel
    # Add column is differential. At the moment, just based on FDR value
    res <- tt[["table"]]
    res[["gene"]] <- rownames(res)
    res <- res[, c(7, 1:6)]
    res[["is_differential"]] <- ifelse(res[["FDR"]] <=  0.05, TRUE, FALSE)
    res[["description"]] <- genes2description[res[["gene"]]]
    
    writexl::write_xlsx(x = res, 
                        path = file.path(results_dir, 
                                         paste0(current_date, "_results_", comparison, ".xlsx"))
    )
                            
    
    # Create volcanoplot
    
    vol_p <- create_volcano_plot(Res = res)
    
    ggsave(filename = paste0(current_date, "_volcano_plot_", comparison, ".png"), 
           path = results_dir, 
           plot = vol_p, 
           device = "png")
    
    print(vol_p)
    
    
    # Create list with relevant results to store
    res_one_comp <- list(fit = fit, 
                         res = res)
    
    results_chg[[comparison]] <- res_one_comp
    
    
    rm(comparison, dge_sub, group, M_value, d, fit, lrt, tt, res, vol_p, res_one_comp)
}

# Remove dge
rm(dge_chg_prom); gc()
```


```{r}
# Save results_cpg
saveRDS(object = results_chg, 
        file = file.path(results_dir, paste0(current_date, "_results_chg.rds"))
        )


# Remove results_cpg
rm(results_chg); gc() 
```



## CHH

```{r}
# Read in the data cpg context
dge_all_chh <- readBismark2DGE(files = files_chh, 
                               sample.names = names(files_chh), 
                               verbose = FALSE)

# Add information to dge$samples
dge_all_chh[["samples"]][["condition"]] <- sample2group[unlist(
    sapply(strsplit(x = rownames(dge_all_chh[["samples"]]), split = "-"), function(x) {
        x[[1]]
        }
        ))]

# Filter as described in dmrseq vignette
dge_chh_fil <- filter_dge(Dge = dge_all_chh)
```


```{r}
# Remove dge_all_cpg because it is not needed anymore
rm(dge_all_chh); gc()
```


```{r}
# Create list to store results Cp
# 24 = 4 features and 6 combinations
results_chh <- vector(mode = "list")
```

### Genniveau

```{r}
dge_chh_gene <- group_dge(Dge_fil = dge_chh_fil, 
                          Gff = gff_genes)
```


```{r}
for (comb_ in combn_mother_tree){
    # Create comparison
    comparison <- paste(paste(comb_, collapse = "_"), "CHH", "gene", sep = "_")
    
    # Subset dge
    dge_sub <- dge_chh_gene[, dge_chh_gene[["samples"]][["condition"]] %in% comb_]
    
    #Print dge_sub[["samples"]]
    print(dge_sub[["samples"]])
    
    # For practical purposes, create variable Group based on condition (but only pick -Me)
    group <- factor(dge_sub[["samples"]][["condition"]][grepl(pattern = "-Me", 
                                                              rownames(dge_sub[["samples"]]))
                                                        ])
    
    # Create MDS plot
    M_value <- return_mvalue_ratio(Dge_fil = dge_sub)
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    png(file.path(results_dir, paste0(current_date, "_mds_plot_", comparison, ".png")))
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    dev.off()
    
    # Create model matrix, only two groups so with intercept
    # Also print d, should have one column for each sample
    d <- modelMatrixMeth(~group)
    
    print(d)
    
    # Estimate dispersion
    dge_sub <- estimateDisp(y = dge_sub, design = d)
    
    # Fit model 
    fit <- glmFit(y = dge_sub, design = d)
    
    # LRT
    lrt <- glmLRT(fit)
    
    # Toptags
    tt <- topTags(lrt, n = Inf)
    
    # Print decide test
    print(summary(decideTests(lrt)))
    
    # Histogram of pvalues
    hist(tt[["table"]][["PValue"]], 
         main = paste("Histogram of p-values for:", comparison, sep = " "), 
         xlab = "p-value")
    
    # MD plot
    plotMD(lrt)
    
    # Create result to store in Excel
    # Add column is differential. At the moment, just based on FDR value
    res <- tt[["table"]]
    res[["gene"]] <- rownames(res)
    res <- res[, c(7, 1:6)]
    res[["is_differential"]] <- ifelse(res[["FDR"]] <=  0.05, TRUE, FALSE)
    res[["description"]] <- genes2description[res[["gene"]]]
    
    writexl::write_xlsx(x = res, 
                        path = file.path(results_dir, 
                                         paste0(current_date, "_results_", comparison, ".xlsx"))
    )
                            
    
    # Create volcanoplot
    
    vol_p <- create_volcano_plot(Res = res)
    
    ggsave(filename = paste0(current_date, "_volcano_plot_", comparison, ".png"), 
           path = results_dir, 
           plot = vol_p, 
           device = "png")
    
    print(vol_p)
    
    
    # Create list with relevant results to store
    res_one_comp <- list(fit = fit, 
                         res = res)
    
    results_chh[[comparison]] <- res_one_comp
    
    
    rm(comparison, dge_sub, group, M_value, d, fit, lrt, tt, res, vol_p, res_one_comp)
}

# Remove dge
rm(dge_chh_gene); gc()
```


### Promoter region

```{r}
dge_chh_prom <- group_dge(Dge_fil = dge_chh_fil, 
                          Gff = gff_proms)
```


```{r}
for (comb_ in combn_mother_tree){
    # Create comparison
    comparison <- paste(paste(comb_, collapse = "_"), "CHH", "promotor", sep = "_")
    
    # Subset dge
    dge_sub <- dge_chh_prom[, dge_chh_prom[["samples"]][["condition"]] %in% comb_]
    
    #Print dge_sub[["samples"]]
    print(dge_sub[["samples"]])
    
    # For practical purposes, create variable Group based on condition (but only pick -Me)
    group <- factor(dge_sub[["samples"]][["condition"]][grepl(pattern = "-Me", 
                                                              rownames(dge_sub[["samples"]]))
                                                        ])
    
    # Create MDS plot
    M_value <- return_mvalue_ratio(Dge_fil = dge_sub)
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    png(file.path(results_dir, paste0(current_date, "_mds_plot_", comparison, ".png")))
    
    plotMDS(M_value, 
            col = as.numeric(group), 
            main = comparison)
    
    dev.off()
    
    # Create model matrix, only two groups so with intercept
    # Also print d, should have one column for each sample
    d <- modelMatrixMeth(~group)
    
    print(d)
    
    # Estimate dispersion
    dge_sub <- estimateDisp(y = dge_sub, design = d)
    
    # Fit model 
    fit <- glmFit(y = dge_sub, design = d)
    
    # LRT
    lrt <- glmLRT(fit)
    
    # Toptags
    tt <- topTags(lrt, n = Inf)
    
    # Print decide test
    print(summary(decideTests(lrt)))
    
    # Histogram of pvalues
    hist(tt[["table"]][["PValue"]], 
         main = paste("Histogram of p-values for:", comparison, sep = " "), 
         xlab = "p-value")
    
    # MD plot
    plotMD(lrt)
    
    # Create result to store in Excel
    # Add column is differential. At the moment, just based on FDR value
    res <- tt[["table"]]
    res[["gene"]] <- rownames(res)
    res <- res[, c(7, 1:6)]
    res[["is_differential"]] <- ifelse(res[["FDR"]] <=  0.05, TRUE, FALSE)
    res[["description"]] <- genes2description[res[["gene"]]]
    
    writexl::write_xlsx(x = res, 
                        path = file.path(results_dir, 
                                         paste0(current_date, "_results_", comparison, ".xlsx"))
    )
                            
    
    # Create volcanoplot
    
    vol_p <- create_volcano_plot(Res = res)
    
    ggsave(filename = paste0(current_date, "_volcano_plot_", comparison, ".png"), 
           path = results_dir, 
           plot = vol_p, 
           device = "png")
    
    print(vol_p)
    
    
    # Create list with relevant results to store
    res_one_comp <- list(fit = fit, 
                         res = res)
    
    results_chh[[comparison]] <- res_one_comp
    
    
    rm(comparison, dge_sub, group, M_value, d, fit, lrt, tt, res, vol_p, res_one_comp)
}

# Remove dge
rm(dge_chh_prom); gc()
```


```{r}
# Save results_cpg
saveRDS(object = results_chh, 
        file = file.path(results_dir, paste0(current_date, "_results_chh.rds"))
        )


# Remove results_cpg
rm(results_chh); gc() 
```



# Session Info

```{r}
sessionInfo()
```


