## ----setup_knitr, include = FALSE---------------------------------------------
knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>",
    warning = FALSE,
    message = FALSE,
    fig.width = 10,
    fig.height = 7
)

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

## ----setup--------------------------------------------------------------------
library(karioCaS)

## ----prep_data----------------------------------------------------------------
proj_dir <- file.path(tempdir(), "karioCaS_Tutorial")
dir.create(file.path(proj_dir, "000_mpa_original"), recursive = TRUE)
mock_files <- list.files(
    system.file("extdata", "your_project_name", "000_mpa_original",
        package = "karioCaS"
    ),
    full.names = TRUE
)
file.copy(mock_files, file.path(proj_dir, "000_mpa_original"))
basename(mock_files)

## ----import_data--------------------------------------------------------------
tse_object <- import_karioCaS(project_dir = proj_dir)
tse_object
head(SummarizedExperiment::colData(tse_object))

## ----optimize_cs--------------------------------------------------------------
retention <- taxa_retention(project_dir = proj_dir, tax_level = "Species")
retention

audit_df <- retention$data
audit_df[
    audit_df$SI_Type %in% "Primary_SI",
    c("Sample", "Domain", "CS", "Taxa_Count", "Pct_Retained", "Step_Loss_Pct")
]

## ----retention_plot-----------------------------------------------------------
retention$plots[[1]]

## ----reads_audit--------------------------------------------------------------
reads <- reads_per_taxa(project_dir = proj_dir, analysis_level = "Species")
head(reads$data[reads$data$SI_Type %in% "Primary_SI", ])

## ----retrieve_taxa------------------------------------------------------------
mosaic <- retrieve_selected_taxa(
    project_dir = proj_dir,
    CS_B = "auto", reads_min_B = "auto", # optimal CS + optimal reads
    CS_A = "secondary", reads_min_A = "auto", # conservative CS, optimal reads
    CS_E = 40, reads_min_E = 10, # manual values for Eukaryota
    CS_V = 0, reads_min_V = 0 # no filtering for Viruses
)
mosaic

# Thresholds applied to each domain and where they came from
unique(mosaic$data[, c(
    "Domain", "CS", "CS_Source", "Min_Reads", "Min_Reads_Source"
)])

## ----upset--------------------------------------------------------------------
ups <- upset_kariocas(proj_dir, tax_level = "Genus", export = FALSE)
ups$plots[["SAMPLE01_Bacteria_Genus"]]

## ----heatmap, fig.height = 10-------------------------------------------------
hm <- heatmaps_karioCaS(
    proj_dir,
    analysis_rank = "Species", confidence_score = 40, top_n = 10,
    export = FALSE
)
hm$plots[[1]]

## ----resolution---------------------------------------------------------------
res <- taxa_resolution(proj_dir, CS = 40, export = FALSE)
head(res$data)
res$plots[[1]]

## ----group_upset, eval = FALSE------------------------------------------------
# group_upset(project_dir = proj_dir)

## ----cleanup, include = FALSE-------------------------------------------------
unlink(proj_dir, recursive = TRUE)

## ----sessionInfo, echo = FALSE------------------------------------------------
sessionInfo()

