---
title: "CellBench User's Guide"
author: "Shian Su"
date: "`r Sys.Date()`"
output: BiocStyle::pdf_document
vignette: >
  %\VignetteIndexEntry{Vignette Title}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(CellBench)
library(dplyr)
library(ggplot2)

set_cellbench_threads(4)

cellbench_mrna_mix_data <- load_mrna_mix_data()

data <- list(
    mrna_mix_celseq = cellbench_mrna_mix_data$mrna_mix_celseq
)

norm_method <- list(
    scran = scran_norm_expr
)

impute_method <- fn_arg_seq(
    impute_knn_smooth,
    k = c(4, 8, 16, 32)
)

res <- data %>%
    apply_methods(norm_method)

res <- res %>%
    apply_methods(impute_method)

dim_red <- list(
    pca = compute_pca
)

res <- res %>%
    apply_methods(dim_red)

append_anno <- function(data_key, result) {
    mRNA_amount <- colData(cellbench_mrna_mix_data$mrna_mix_celseq)$mRNA_amount
    truth <- with(
        colData(cellbench_mrna_mix_data$mrna_mix_celseq),
        paste(H2228_prop, H1975_prop, HCC827_prop)
    )

    result %>%
        tibble::add_column(mRNA_amount, .before = TRUE) %>%
        tibble::add_column(truth, .before = TRUE)
}

annotated_res <- res %>%
    dplyr::mutate(data_key = paste(data)) %>%
    dplyr::mutate(result = map2(data_key, result, append_anno)) %>%
    dplyr::select(-data_key)

plot_df <- tidyr::unnest(annotated_res)

plot_df %>%
    ggplot(aes(x = Dim1, y = Dim2, col = truth)) +
    geom_point() +
    facet_wrap(~impute_method, nrow = 2) +
    ggtitle("KNN Smooth Imputation")

```


