## ----knitr-options, include=FALSE-----------------------------------
# Knitr chunk defaults used throughout this vignette.
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  eval     = TRUE,
  cache    = FALSE,
  fig.width  = 10,
  fig.height = 6,
  out.width  = "100%",
  tidy       = FALSE
)
options(width = 70)

## ----locate-data----------------------------------------------------
toy_dir <- system.file("extdata", "toy", package = "epiRomics")
dir.exists(toy_dir)

list.files(toy_dir)

## ----install, eval = FALSE------------------------------------------
# if (!requireNamespace("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")
# BiocManager::install("epiRomics")

## ----load-pkg, message = FALSE, warning = FALSE---------------------
library(epiRomics)
library(TxDb.Hsapiens.UCSC.hg38.knownGene)
library(org.Hs.eg.db)

## ----show-manifest--------------------------------------------------
db_sheet_path <- file.path(toy_dir, "example_epiRomics_Db_sheet.csv")
db_sheet <- read.csv(db_sheet_path)
db_sheet

## ----build-db, eval = FALSE-----------------------------------------
# database <- build_database(
#   db_file = db_sheet_path,
#   txdb_organism = paste0(
#     "TxDb.Hsapiens.UCSC.hg38.knownGene::",
#     "TxDb.Hsapiens.UCSC.hg38.knownGene"),
#   genome   = "hg38",
#   organism = "org.Hs.eg.db",
#   data_dir = toy_dir)

## ----load-prebuilt-db, echo = FALSE---------------------------------
database <- readRDS(file.path(toy_dir, "toy_database.rds"))
database

## ----bigwig-sheet---------------------------------------------------
track_connection <- read.csv(
  file.path(toy_dir, "example_epiRomics_BW_sheet.csv"))
track_connection$path <- file.path(toy_dir, track_connection$path)

## Reorder: beta tracks first, alpha second
beta_idx  <- grep("Beta",  track_connection$name)
alpha_idx <- grep("Alpha", track_connection$name)
track_connection <- track_connection[c(beta_idx, alpha_idx), ]

## Named vector of BigWig paths and matching colours
bw_paths  <- setNames(track_connection$path, track_connection$name)
bw_colors <- track_connection$color

## Display-friendly view (basenames only); the real data frame
## still holds fully resolved paths for plot_tracks() below.
display_tc <- track_connection
display_tc$path <- basename(display_tc$path)
display_tc

## ----plot-quick-view-ins, fig.align = "center", message = FALSE, warning = FALSE----
plot_quick_view("INS",
  bw_paths = bw_paths,
  colors   = bw_colors,
  mirror   = TRUE,
  genome   = "hg38")

## ----putative-enhancers---------------------------------------------
putative_enhancers <- find_enhancers_by_comarks(
  database,
  histone_mark_1 = "h3k4me1",
  histone_mark_2 = "h3k27ac")

head(as.data.frame(annotations(putative_enhancers)), 5)

## ----filter-fantom--------------------------------------------------
fantom_enhancers <- filter_enhancers(
  putative_enhancers,
  database,
  type = "hg38_custom_fantom")

head(as.data.frame(annotations(fantom_enhancers)), 5)

## ----filter-regulome-active-----------------------------------------
regulome_enhancers <- filter_enhancers(
  putative_enhancers,
  database,
  type = "hg38_custom_regulome_active")

head(as.data.frame(annotations(regulome_enhancers)), 5)

## ----filter-regulome-super------------------------------------------
regulome_super_enhancers <- filter_enhancers(
  putative_enhancers,
  database,
  type = "hg38_custom_regulome_super")

head(as.data.frame(annotations(regulome_super_enhancers)), 5)

## ----enhanceosomes--------------------------------------------------
enhanceosomes <- find_enhanceosomes(
  putative_enhancers,
  database)

head(as.data.frame(annotations(enhanceosomes)), 5)

length(annotations(enhanceosomes))

## ----cobinding------------------------------------------------------
tf_stats <- analyze_tf_cobinding(enhanceosomes, database)
tf_stats$pairwise

## ----pick-enhanceosome-index----------------------------------------
enh_df <- as.data.frame(annotations(enhanceosomes))
in_window <- which(
  enh_df$seqnames == "chr11" &
    enh_df$start >= 1900000 &
    enh_df$end   <= 2300000)
length(in_window)
selected_index <- in_window[1]
selected_index
enh_df[selected_index, c("seqnames", "start", "end")]

## ----plot-enhanceosome, fig.align = "center", message = FALSE, warning = FALSE----
plot_tracks(
  enhanceosomes,
  index = selected_index,
  database = database,
  track_connection = track_connection)

## ----plot-gene-ins, fig.align = "center", message = FALSE, warning = FALSE----
plot_gene_tracks("INS", database, track_connection)

## ----chromatin-states-----------------------------------------------
chromatin_states <- classify_chromatin_states(database)
table(chromatin_states$chromatin_state)
table(chromatin_states$genomic_context)

## ----fig-chromatin-ins, fig.align = "center", message = FALSE, warning = FALSE----
plot_gene_tracks("INS",
  database,
  track_connection,
  chromatin_states = chromatin_states,
  show_chromatin = TRUE,
  show_enhancer_highlight = TRUE)

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

