## ----setup, warning=FALSE, message=FALSE, include=FALSE-----------------------
knitr::opts_chunk$set( warning = FALSE, message = FALSE)
library(R6)
library(scuttle) 

## ----load-package-------------------------------------------------------------
library(BenchHub)

## ----create-study-------------------------------------------------------------

study <- BenchmarkStudy$new()
 

## ----download-trio, eval=FALSE------------------------------------------------
# # Download an existing Trio from the submission database
# example_trio <- downloadSubmissionTrio("D001", cachePath = tempdir())
# 
# example_trio

## ----mapping-function---------------------------------------------------------
# Define the mapping function 
extract_domains <- function(result) {
  if (is.data.frame(result) && "annotated_domain" %in% colnames(result)) {
    return(result$annotated_domain)
  }
  if (is.list(result) && "annotated_domain" %in% names(result)) {
    return(result$annotated_domain)
  }
  stop("Could not find 'annotated_domain' in the method output.")
}

# Add the mapping function
study$addMappingFunction(
  name = "annotated_domain",
  func = extract_domains,
  inputDescription = "Method output containing one predicted spatial domain label per spot.",
  outputDescription = "A vector of predicted spatial domain labels aligned to spots.",
  exampleUsage = paste(
    "## Minimal example",
    "#result <- list(annotated_domain = c('domain_1', 'domain_1', 'domain_2', 'domain_2'))",
    "#res <- study$runMapping('annotated_domain', result)",
    "#head(res)",
    sep = "\n"
  )
)
 
 

## ----another-mapping----------------------------------------------------------
# Define the mapping function 
extract_celltype_props <- function(result) {
  if (is.data.frame(result) && "celltype_proportions" %in% names(result)) {
    return(result$celltype_proportions)
  }
  if (is.list(result) && "celltype_proportions" %in% names(result)) {
    return(result$celltype_proportions)
  }
  if (is.matrix(result) || is.data.frame(result)) {
    mat <- as.matrix(result)
    rs <- rowSums(mat)
    rs[rs == 0] <- 1
    return(mat / rs)
  }
  stop("Could not extract cell type proportions from the method output.")
}

# Add the mapping function, it is optional but recommended to add example usage 
study$addMappingFunction(
  name = "celltype_proportions",
  func = extract_celltype_props,
  inputDescription = "Method output containing cell type proportions per spot.",
  outputDescription = "A matrix or data frame of cell type proportions aligned to spots.",
  exampleUsage = paste(
    "## Minimal example",
    "#props <- matrix(c(0.9, 0.1, 0.8, 0.2, 0.2, 0.8, 0.1, 0.9), ncol = 2, byrow = TRUE)",
    "#study$runMapping('celltype_proportions', props)",
    sep = "\n"
  )
)


## ----upload-study, eval=FALSE-------------------------------------------------
# # Set name and description manually
# study <- BenchmarkStudy$new(name = "ST toy study")
# study$description <- "Toy spatial transcriptomics study."
# 
# interactivePrepareStudySubmission(study)

## ----download-previous-study, eval=FALSE--------------------------------------
# loaded_study <- downloadSubmissionStudy(studyID = "ST005", cachePath = tempdir())

## ----inspect-study, eval=FALSE------------------------------------------------
# loaded_study
# loaded_study$name
# loaded_study$description
# loaded_study$version
# length(loaded_study$trios)

## ----inspect-trio, eval=FALSE-------------------------------------------------
# length(loaded_study$trios)
# 
# loaded_study$trios[[1]]
# 

## ----inspect-mapping, eval=FALSE----------------------------------------------
# # list the names of the mapping function
# loaded_study$listMappingFunctions()
# 
# # choose one to print the documentation
# loaded_study$printMappingFunctionDocumentation("annotated_domain")
# 

## ----simulate-domain, eval=FALSE----------------------------------------------
# method_output <- list(
#   annotated_domain = c("domain_1", "domain_1", "domain_2", "domain_2"),
#   celltype_proportions = data.frame(
#     celltype_A = c(0.9, 0.8, 0.2, 0.1),
#     celltype_B = c(0.1, 0.2, 0.8, 0.9)
#   )
# )

## ----add-domian, eval=FALSE---------------------------------------------------
# 
# domain_pred <- loaded_study$runMapping("annotated_domain", method_output)
# prop_pred <- loaded_study$runMapping("celltype_proportions", method_output)

## ----eval-domain, eval=FALSE--------------------------------------------------
# 
# result <- loaded_study$evaluate(loaded_study$trios[[1]]$name,  # name of the Trio to compare with
#   list(
#     "annotated_domain" = domain_pred,
#     "celltype_proportions" = prop_pred
#   ))
# 
# result
# 

## ----session-info-------------------------------------------------------------

sessionInfo()


