## ----opts, echo = FALSE-------------------------------------------------------
suppressWarnings(library(knitr))

opts_chunk$set(tidy = FALSE, message = FALSE, warning = FALSE)

## ----installation, eval=FALSE-------------------------------------------------
# if (!requireNamespace("BiocManager", quietly = TRUE)) {
#     install.packages("BiocManager")
# }
# BiocManager::install("scToppR")

## ----setup--------------------------------------------------------------------
library(scToppR)

suppressMessages({
    library(airway)
    library(DESeq2)
})
data("airway")

se <- airway
rownames(se) <- rowData(se)$gene_name

dds <- DESeqDataSet(se, design = ~ cell + dex)

smallestGroupSize <- 3
keep <- rowSums(counts(dds) >= 10) >= smallestGroupSize
dds <- dds[keep, ]

dds <- DESeq(dds)
res <- results(dds)

# add the gene names as a column in the results
res$gene <- rownames(res)

# add cluster column - here, with this bulk RNAseq data, we will only have 1 cluster
res$cluster <- "cluster0"

## ----toppFun------------------------------------------------------------------
# This is how you would run the analysis with live data (requires internet)
if (curl::has_internet()) {
    toppdata.airway <- toppFun(res,
        type = "degs",
        gene_col = "gene",
        cluster_col = "cluster",
        p_val_col = "padj",
        logFC_col = "log2FoldChange"
    )
} else {
    data("toppdata.airway")
}
head(toppdata.airway)

## ----save_to_se---------------------------------------------------------------

se <- addToppData(se, toppdata.airway)
print(head(metadata(se)$toppData))

## ----plot1, fig.dim=c(8,8)----------------------------------------------------
toppPlot(toppdata.airway,
    category = "GeneOntologyMolecularFunction",
    clusters = "cluster0"  
)

## ----plot_se, fig.dim=c(8,8)--------------------------------------------------
toppPlot(se,
    category = "GeneOntologyMolecularFunction",
    clusters = "cluster0"  
)

## ----plot_list, fig.dim=c(8,8)------------------------------------------------
plot_list <- toppPlot(toppdata.airway,
    category = "GeneOntologyMolecularFunction",
    clusters = NULL
)
plot_list[1]

## ----plot_save----------------------------------------------------------------
plot_list <- toppPlot(toppdata.airway,
    category = "GeneOntologyMolecularFunction",
    clusters = NULL,
    save = TRUE,
    save_dir = tempdir(),
    file_prefix = "GO_molecular_function"
)

## ----plot_balloon, fig.dim=c(8,5)---------------------------------------------
toppBalloon(toppdata.airway,
    categories = "GeneOntologyBiologicalProcess"
)

## ----plot_balloon2, fig.dim=c(8,5)--------------------------------------------
toppBalloon(toppdata.airway,
    categories = "Pathway"
)

## ----plot_balloon3, fig.dim=c(8,5)--------------------------------------------
toppBalloon(toppdata.airway,
    categories = "Pubmed"
)

## ----save---------------------------------------------------------------------
toppSave(toppdata.airway,
    filename = "airway_toppData",
    save_dir = tempdir(),
    split = TRUE,
    format = "xlsx"
)

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

