## ----knitr-opts---------------------------------------------------------------
knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>"
)

## ----install-from-bioc, eval = FALSE------------------------------------------
# if (! require("BiocManager", quietly = TRUE))
#     install.packages("BiocManager")
# 
# BiocManager::install("toppgene")

## ----setup--------------------------------------------------------------------
genes_chd_sym <- c(
    "ADD1", "CITED2", "DTNA", "CKM", "GATA4", "GJA1", "HAND1", "HAND2", "HEY2",
    "HOXC4", "HOXC5", "ITGB3", "JARID2", "MTHFD1", "MTHFR", "MTRR", "NKX2-5",
    "NOS3", "NPPA", "NPPB", "RFC1", "SALL4", "TBX1", "TBX5", "TBX20",
    "TGFB1", "ZFPM2", "ZIC3")
genes_dr_sym <- c(
    "ACE", "ADRB3", "AGT", "AGTR2", "AKR1B1", "APOE", "AR", "CMA1", "EDN1",
    "GNB3", "HFE", "HLA-DPB1", "HLA-DRB1", "ICAM1", "ITGA2B", "ITGB2", "LTA",
    "NOS2A", "NOS3", "NPY", "PECAM1", "PON1", "RAGE", "SELE", "SERPINE1",
    "TIMP3", "TNF")

## ----toppgene-lookup----------------------------------------------------------
library(toppgene)

genes_chd <- lookup(genes_chd_sym)
genes_chd
genes_dr <- lookup(genes_dr_sym)
genes_dr

## ----toppgene-enrich----------------------------------------------------------
enrich_chd <- enrich(genes_chd$Entrez)
enrich_chd
enrich_dr <- enrich(genes_dr$Entrez)
enrich_dr

## ----toppgene-compare---------------------------------------------------------
library(IRanges) # CharacterList
library(DFplyr)  # (DataFrame support for various dplyr functions)
## Show all DataFrame rows of top_results().
orig <- options(showHeadLines = 20L)

top_results <- function(df) {
    df |>
        group_by(Category) |>
        slice(1) |>
        ungroup() |>
        ## Shorten GeneOntology to GO.
        mutate(Category = gsub(x = Category, "GeneOntology", "GO")) |>
        select(Category, ID, Name, GenesSymbol)
}
enrich_chd |>
    filter(any(GenesSymbol %in% CharacterList("HAND2"))) |>
    top_results()
enrich_dr |>
    filter(any(GenesSymbol %in% CharacterList("HLA-DPB1"))) |>
    top_results()

options(showHeadLines = orig)

## ----toppgene-pubchem---------------------------------------------------------
enrich_chd |>
    lookup_pubchem()
enrich_dr |>
    lookup_pubchem()

## ----toppgene-modify-defaults-------------------------------------------------
## Default cut-offs.
cats <- CategoriesDataFrame()
cats

## Limit to 10 results for each category, and lower PValue for GeneOntology.
cats <-
    cats |>
    mutate(
        PValue = case_when(
            grepl("GeneOntology", rownames(cats)) ~ 1e-7,
            .default = PValue),
        MaxResults = 10L)
cats

enrich_chd_mod <-
    enrich(
        genes_chd$Entrez,
        cats)
enrich_chd_mod

## MaxResults limited to at most 10.
enrich_chd_mod |>
    count(Category)

## PValue limited to below 1e-7.
enrich_chd_mod |>
    arrange(desc(PValue)) |>
    filter(grepl(x = Category, "Onto")) |>
    group_by(Category) |>
    slice(1)

## ----session-info-------------------------------------------------------------
sessionInfo()

