## ----setup--------------------------------------------------------------------
library(scCertify)
library(SingleCellExperiment)
library(SummarizedExperiment)
library(Seurat)

set.seed(42)

## ----simulate-data------------------------------------------------------------
n_genes <- 50
n_cells <- 40

counts <- matrix(
    rpois(n_genes * n_cells, lambda = 5),
    nrow = n_genes,
    ncol = n_cells,
    dimnames = list(
        paste0("Gene", seq_len(n_genes)),
        paste0("Cell", seq_len(n_cells))
    )
)

sce <- SingleCellExperiment(
    assays = list(
        counts = counts
    )
)

colData(sce)$predicted_label <- rep(
    c("T cell", "B cell"),
    each = n_cells / 2
)

sce

## ----sce-metadata-------------------------------------------------------------
head(colData(sce))

## ----marker-list--------------------------------------------------------------
markers <- list(
    "T cell" = c("Gene1", "Gene2", "Gene3"),
    "B cell" = c("Gene4", "Gene5", "Gene6")
)

markers

## ----sce-pca------------------------------------------------------------------
log_counts <- log1p(
    assay(sce, "counts")
)

assay(sce, "logcounts") <- log_counts

pca <- stats::prcomp(
    t(log_counts),
    center = TRUE,
    scale. = TRUE
)$x[, seq_len(5), drop = FALSE]

reducedDim(sce, "PCA") <- pca

dim(
    reducedDim(sce, "PCA")
)

## ----marker-score-------------------------------------------------------------
marker_scores <- marker_score(
    sce,
    markers,
    label_column = "predicted_label"
)

head(marker_scores)

## ----neighbor-score-----------------------------------------------------------
neighbor_scores <- neighbor_score(
    sce,
    label_column = "predicted_label"
)

head(neighbor_scores)

## ----entropy-score------------------------------------------------------------
entropy_values <- entropy_score(
    t(assay(sce, "counts"))
)

head(entropy_values)

## ----entropy-norm-------------------------------------------------------------
entropy_norm <- entropy_values / max(
    entropy_values,
    na.rm = TRUE
)

colData(sce)$entropy_norm <- entropy_norm

head(colData(sce)$entropy_norm)

## ----doublet-score------------------------------------------------------------
colData(sce)$doublet_score <- runif(
    ncol(sce),
    min = 0,
    max = 1
)

head(colData(sce)$doublet_score)

## ----cell-certify-------------------------------------------------------------
sce <- cell_certify(
    sce,
    markers,
    label_column = "predicted_label"
)

head(
    colData(sce)[,
        c(
            "predicted_label",
            "marker_score",
            "neighbor_score",
            "entropy_norm",
            "doublet_score",
            "confidence_score",
            "confidence_class"
        )
    ]
)

## ----custom-thresholds--------------------------------------------------------
sce_custom <- cell_certify(
    sce,
    markers,
    label_column = "predicted_label",
    moderate_threshold = 0.4,
    high_threshold = 0.75
)

head(
    colData(sce_custom)[,
        c(
            "confidence_score",
            "confidence_class"
        )
    ]
)

## ----explain-confidence-------------------------------------------------------
explain_confidence(
    sce,
    cell_id = "Cell1"
)

## ----explain-cell-------------------------------------------------------------
explain_cell(
    sce,
    cell_id = "Cell1"
)

## ----seurat-data--------------------------------------------------------------
seurat_counts <- matrix(
    rpois(2000, lambda = 5),
    nrow = 100,
    ncol = 20,
    dimnames = list(
        paste0("Gene", seq_len(100)),
        paste0("Cell", seq_len(20))
    )
)

seurat_object <- Seurat::CreateSeuratObject(
    counts = seurat_counts
)

seurat_object$predicted_label <- rep(
    c("T cell", "B cell"),
    each = 10
)

seurat_object$entropy_norm <- runif(
    ncol(seurat_object)
)

seurat_object$doublet_score <- runif(
    ncol(seurat_object)
)

seurat_object

## ----seurat-preprocessing-----------------------------------------------------
seurat_object <- Seurat::NormalizeData(
    seurat_object,
    verbose = FALSE
)

seurat_object <- Seurat::FindVariableFeatures(
    seurat_object,
    verbose = FALSE
)

seurat_object <- Seurat::ScaleData(
    seurat_object,
    verbose = FALSE
)

seurat_object <- Seurat::RunPCA(
    seurat_object,
    npcs = 10,
    verbose = FALSE
)

seurat_object

## ----seurat-certify-----------------------------------------------------------
seurat_object <- cell_certify(
    seurat_object,
    markers,
    label_column = "predicted_label"
)

head(
    seurat_object[[
        c(
            "predicted_label",
            "marker_score",
            "neighbor_score",
            "entropy_norm",
            "doublet_score",
            "confidence_score",
            "confidence_class"
        )
    ]]
)

## ----seurat-explain-----------------------------------------------------------
explain_confidence(
    seurat_object,
    cell_id = "Cell1"
)

explain_cell(
    seurat_object,
    cell_id = "Cell1"
)

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

