## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4
)

## ----installation-cran, eval=FALSE--------------------------------------------
# install.packages("venny")

## ----installation-github, eval=FALSE------------------------------------------
# if (!require("pak")) install.packages("pak")
# pak::pak("P10911004-NPUST/venny")

## ----message=FALSE------------------------------------------------------------
library(venny)

## ----quick-start--------------------------------------------------------------
venny(LGL23$DEGs)

## -----------------------------------------------------------------------------
venny(unname(LGL23$DEGs))

## -----------------------------------------------------------------------------
venny(unname(LGL23$DEGs[-4]))

## ----subset-number------------------------------------------------------------
how_many_subsets(c("A", "B", "C"))
how_many_subsets(c("A", "B", "C"), detail = TRUE)

## ----bits---------------------------------------------------------------------
bits_encoding(c("A", "B", "C"))

## ----summary------------------------------------------------------------------
out <- venn_summary(LGL23$DEGs)
out$table

## ----detail-------------------------------------------------------------------
out <- venny(LGL23$DEGs, detail = TRUE)
names(out)

## ----detail-components--------------------------------------------------------
out$venn

## ----detail-table-------------------------------------------------------------
out$table

## ----detail-elements----------------------------------------------------------
out$subset_elements$ABC

## ----ellipse-style------------------------------------------------------------
venny(
    LGL23$DEGs,
    ellipse.line = ellipse_line(color = c("red", "transparent", "red", "navy"),
                                linetype = "solid", 
                                linewidth = 2, 
                                alpha = 0.8),
    ellipse.fill = ellipse_fill(color = c("gray", "red", "navy", "orange"),
                                alpha = 0.25)
)

## -----------------------------------------------------------------------------
names(LGL23$DEGs)

## ----set-label-hide-----------------------------------------------------------
venny(
    LGL23$DEGs,
    set.label.position = set_label_position(hjust = c(0, -1, 0.5, 0),
                                            hide = c("A", "C"))
)

## ----set-label-font-----------------------------------------------------------
venny(
    LGL23$DEGs,
    set.label.font = set_label_font(face = "italic",
                                    size = 7)
)

## ----subset-label-rename------------------------------------------------------
subset_font_color <- vapply(subset_label_default(length(LGL23$DEGs)), 
                            function(x) 
                                if (x %in% c("A", "B", "C", "D")) "maroon" else "blue",
                            FUN.VALUE = character(1))

venny(
    LGL23$DEGs,
    subset.label = list(A = "Apple",
                        B = "Banana",
                        C = "Coconut",
                        D = "Durian"),
    subset.label.font = subset_label_font(color = subset_font_color)
)

## ----count-percentage---------------------------------------------------------
nm <- subset_label_default(length(LGL23$DEGs))
font_color <- fixed_length(c("red", "blue", "maroon"), length(nm))
font_angle <- fixed_length(c(30, 60, 90, 180), length(nm))
show_perc <- c("A", "B", "C", "D", "AB", "ACD", "BC", "CD", "ABCD")

venny(
    LGL23$DEGs,
    subset.count = TRUE,
    subset.count.font = subset_count_font(family = "serif",
                                          color = font_color,
                                          angle = font_angle),
    subset.percentage = TRUE,
    subset.percentage.rounding = 4,
    subset.percentage.position = subset_percentage_position(show = show_perc)
)

## ----count-font---------------------------------------------------------------
venny(
    LGL23$DEGs,
    subset.count.font = subset_count_font(face = "bold",
                                          size = 4),
    subset.percentage.font = subset_percentage_font(size = 3.5)
)

## ----ellipse-path-------------------------------------------------------------
out <- venny(LGL23$DEGs, detail = TRUE)
names(out$ellipse_path)

## ----one-ellipse--------------------------------------------------------------
head(out$ellipse_path$`KO_low vs KO_mock`)

## ----generate-ellipse---------------------------------------------------------
e1 <- generate_ellipse_path(x0 = 0, y0 = 0, a = 2, b = 1, angle = 45)
head(e1)

## ----out.width="35%"----------------------------------------------------------
ggplot2::ggplot(e1, ggplot2::aes(x, y)) +
    ggplot2::geom_polygon() +
    ggplot2::coord_fixed() +
    ggplot2::theme_void()

## ----eval=FALSE---------------------------------------------------------------
# # Option A: Explicit namespacing
# venny::intersect()
# venny::union()
# venny::setdiff()
# 
# # Option B: Local reassignment
# intersect <- venny::intersect
# union     <- venny::union
# setdiff   <- venny::setdiff

## ----polygon-intersection-----------------------------------------------------
out <- venny(unname(LGL23$DEGs), detail = TRUE)
ep <- out$ellipse_path
setops <- venny::intersect(ep$Set_A, ep$Set_B, ep$Set_D)
highlight(out$venn, setops)

## ----highlight-annotation-----------------------------------------------------
highlight(out$venn, setops, color = "red", fill = "red", alpha = 0.25) +
    ggplot2::annotate("text", x = 0, y = 2.5, label = "A ∩ B ∩ D", fontface = "bold")

## ----polygon-union------------------------------------------------------------
setops <- venny::union(ep$Set_A, ep$Set_C, ep$Set_D)
highlight(out$venn, setops)

## ----polygon-difference-------------------------------------------------------
setops <- venny::setdiff(ep$Set_B, ep$Set_D)
highlight(out$venn, setops)

## ----polygon-chain------------------------------------------------------------
setops <- venny::union(ep$Set_A, ep$Set_B, ep$Set_C) |>
    venny::intersect(ep$Set_D) |>
    venny::setdiff(ep$Set_B)
highlight(out$venn, setops)

## ----message=FALSE------------------------------------------------------------
if (!require(venny)) install.packages("venny")
if (!require(dplyr)) install.packages("dplyr")
if (!require(forcats)) install.packages("forcats")
if (!require(ggplot2)) install.packages("ggplot2")
if (!require(ggtext)) install.packages("ggtext")
if (!require(BiocManager)) install.packages("BiocManager")
if (!require(clusterProfiler)) BiocManager::install("clusterProfiler")
if (!require(org.At.tair.db)) BiocManager::install("org.At.tair.db")
library(venny)
library(dplyr)
library(forcats)
library(ggplot2)
library(ggtext)
library(clusterProfiler)
library(org.At.tair.db)

intersect <- venny::intersect
setdiff <- venny::setdiff
union <- venny::union

## -----------------------------------------------------------------------------
lst <- LGL23$DEGs
print(names(lst))

## ----fig.width=7, fig.height=4------------------------------------------------
subset_names <- names(subset_label_default(length(lst)))
select_subsets <- c("C", "BC", "CD", "ABC", "BCD", "AC", "ABCD", "ACD")
font_color <- sapply(subset_names, \(x) if (x %in% select_subsets) "red" else "navy")

out <- venny(
    data = lst,
    detail = TRUE,
    set.label.position = set_label_position(hjust = c(0.2, -0.5, 0.5, -0.2)),
    subset.label.font = subset_label_font(color = font_color),
    subset.count.font = subset_count_font(color = font_color),
    subset.percentage.font = subset_percentage_font(color = font_color)
)

venn <- out$venn
setops <- out$ellipse_path$`WT_mock vs KO_mock`

highlight(venn, setops, linetype = "solid", color = "red") +
    coord_cartesian(xlim = c(-3, 5)) +
    annotate("richtext",
             x = 3.2, 
             y = -1.9,
             hjust = 0,
             size = 4,
             label = paste(
                 "<b>KO:</b> Knock-out",
                 "<b>WT:</b> Wild-type",
                 "<b>OE:</b> Overexpression",
                 "<b>mock:</b> 0 nM treatment",
                 "<b>low:</b> 1 nM treatment",
                 "<b>high:</b> 5 nM treatment",
                 sep = "<br>")) +
    labs(title = "Awesome title") +
    theme(plot.title = element_text(size = 20, face = "bold"))

## ----message=FALSE------------------------------------------------------------
GO <- clusterProfiler::enrichGO(
    gene = LGL23$DEGs$`WT_mock vs KO_mock`,
    OrgDb = org.At.tair.db,
    keyType = "TAIR",
    ont = "BP"
)

GO@result |>
    dplyr::slice_max(RichFactor, n = 20) |>
    ggplot(aes(RichFactor, forcats::fct_reorder(Description, RichFactor))) +
    theme_bw() +
    geom_point(aes(size = Count, color = FoldEnrichment)) +
    theme(axis.title.y = element_blank())

## -----------------------------------------------------------------------------
select_subsets <- "BCD"
font_color <- sapply(subset_names, \(x) if (x %in% select_subsets) "red" else "navy")

BCD <- venny(
    data = lst,
    detail = TRUE,
    set.label.position = set_label_position(hjust = c(0.2, -0.5, 0.5, -0.2)),
    subset.label.font = subset_label_font(color = font_color),
    subset.count.font = subset_count_font(color = font_color),
    subset.percentage.font = subset_percentage_font(color = font_color)
)

venn <- BCD$venn
ep <- BCD$ellipse_path
setops <- ep$`KO_high vs KO_mock` |>
    intersect(ep$`WT_mock vs KO_mock`, ep$`OE_mock vs KO_mock`) |>
    setdiff(ep$`KO_low vs KO_mock`)

highlight(venn, setops, linetype = "solid", color = "red")

## ----message=FALSE------------------------------------------------------------
GO <- clusterProfiler::enrichGO(
    gene = BCD$subset_elements$BCD,
    OrgDb = org.At.tair.db,
    keyType = "TAIR",
    ont = "BP"
)

GO@result |>
    dplyr::slice_max(RichFactor, n = 20) |>
    ggplot(aes(RichFactor, forcats::fct_reorder(Description, RichFactor))) +
    theme_bw() +
    geom_point(aes(size = Count, color = FoldEnrichment)) +
    theme(axis.title.y = element_blank())

