## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 12,
  fig.height = 6.75
)

## ----setup, message = F-------------------------------------------------------
library(methodical)
library(HDF5Array)

## ----eval=FALSE---------------------------------------------------------------
#  # Installing Methodical
#  if (!require("BiocManager"))
#      install.packages("BiocManager")
#  BiocManager::install("methodical")

## ----eval=TRUE----------------------------------------------------------------
# Load required objects
data("tubb6_tss", package = "methodical")
data("tubb6_meth_rse", package = "methodical"); tubb6_meth_rse <- eval(tubb6_meth_rse)
tubb6_meth_rse <- eval(tubb6_meth_rse)
data("tubb6_transcript_counts", package = "methodical")

## ----eval=TRUE, fig.show='hide'-----------------------------------------------
# Calculate correlation values between methylation values and transcript values for TUBB6
cpg_meth_transcript_cors <- calculateMethSiteTranscriptCors(meth_rse = tubb6_meth_rse, 
  transcript_expression_table = tubb6_transcript_counts, tss_gr = tubb6_tss, 
  expand_upstream = 5000, expand_downstream = 5000, cor_method = "spearman")

# Since cpg_meth_transcript_cors is just a list of with 1 table, we'll extract this table 
# from the list 
tubb6_cpg_meth_transcript_cors <- cpg_meth_transcript_cors$ENST00000591909

# Take a look at the results
head(tubb6_cpg_meth_transcript_cors)

# Extract the location of the TSS from the results
attributes(tubb6_cpg_meth_transcript_cors)$tss_range

## ----eval=TRUE, fig.align="center", fig.show='hide'---------------------------
# Plot methylation-transcription correlation values showing chromosomal coordinates of CpG sites.
tubb6_correlation_plot_chrom_coords <- plotMethSiteCorCoefs(tubb6_cpg_meth_transcript_cors, 
  ylabel = "Spearman Correlation", value_colours = "set2") + 
geom_hline(yintercept = 0, linetype = "dashed")
print(tubb6_correlation_plot_chrom_coords)

# Plot methylation-transcription correlation values showing distance of CpG sites to TSS. 
tubb6_correlation_plot_tss_dist <- plotMethSiteCorCoefs(tubb6_cpg_meth_transcript_cors, 
  ylabel = "Spearman Correlation", value_colours = "set2", 
  reference_tss = attributes(tubb6_cpg_meth_transcript_cors)$tss_range) +
geom_hline(yintercept = 0, linetype = "dashed")
print(tubb6_correlation_plot_tss_dist)

## ----eval=TRUE, fig.show='hide'-----------------------------------------------
# Plot methodical scores for CpGs near TUBB6 TSS
tubb6_methodical_scores_plot <- plotMethodicalScores(
  meth_site_values = tubb6_cpg_meth_transcript_cors, 
  p_value_threshold = 0.005, smooth_scores = FALSE) +
geom_hline(yintercept = 0, linetype = "dashed")
print(tubb6_methodical_scores_plot)

## ----eval=TRUE, fig.show='hide'-----------------------------------------------
# Smooth Methodical scores
tubb6_smoothed_methodical_scores_plot <- plotMethodicalScores(
  meth_site_values = tubb6_cpg_meth_transcript_cors, p_value_threshold = 0.005, 
  smooth_scores = TRUE, smoothed_curve_colour = "hotpink2", offset_length = 10, 
  smoothing_factor = 0.75) +
geom_hline(yintercept = 0, linetype = "dashed")
print(tubb6_smoothed_methodical_scores_plot)

## ----eval=TRUE, fig.show='hide'-----------------------------------------------
# Add significance thresholds to plot
tubb6_smoothed_methodical_scores_plot <- plotMethodicalScores(meth_site_values = 
  tubb6_cpg_meth_transcript_cors, p_value_threshold = 0.005, smooth_scores = TRUE, 
  smoothed_curve_colour = "hotpink2") +
  geom_hline(yintercept = 0, linetype = "dashed")
print(tubb6_smoothed_methodical_scores_plot)

## ----eval=TRUE, fig.show='hide'-----------------------------------------------
# Identify TMRs for TUBB6
tubb6_tmrs <- findTMRs(correlation_df = tubb6_cpg_meth_transcript_cors, offset_length = 10, 
  smoothing_factor = 0.75, min_meth_sites = 5, min_gapwidth = 150)
print(tubb6_tmrs)

## ----eval=TRUE, fig.show='hide'-----------------------------------------------
# Show location of TMRs on Methodical scores plot
plotTMRs(meth_site_plot = tubb6_smoothed_methodical_scores_plot, tmrs_gr = tubb6_tmrs)

# Show location of TMRs on correlation value plot
plotTMRs(meth_site_plot = tubb6_correlation_plot_tss_dist, tmrs_gr = tubb6_tmrs,
  reference_tss = GRanges(tubb6_tmrs$tss_location[1]))

## -----------------------------------------------------------------------------
sessionInfo()