## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>",
    warning = FALSE,
    message = FALSE,
    cache = TRUE,
    tidy = TRUE,
    width.cutoff = 70
)

## ----message=FALSE------------------------------------------------------------
library(curatedPCaData)

# Use a function to extract all known study short identifiers
studies <- curatedPCaData::getPCaStudies()
studies

# List apply across studies to extract all MAE objects corresponding to the 
# short identifiers
maes <- lapply(studies, FUN=\(id) { curatedPCaData::getPCa(id) })
names(maes) <- studies

## ----studies1, message = FALSE------------------------------------------------
# Create a summary table depicting key features in available studies
studytable <- curatedPCaData::getPCaSummaryStudies(maes)

## ----studies2, results = 'asis', echo=FALSE-----------------------------------
knitr::kable(studytable, caption = "Key study characteristics")

## ----template, results='asis', echo=FALSE-------------------------------------
data(template_prad)
template <- template_prad
# Add spaces to |-dividers for linechanges
template <- do.call("cbind", lapply(template, FUN = function(x) {
    gsub("|", " | ", x, fixed = TRUE)
}))
knitr::kable(template, caption = "Template for prostate adenocarcinoma clinical 
metadata")

## ----gleasons1----------------------------------------------------------------
# Create a summary table of Gleason grades
gleasons <- curatedPCaData::getPCaSummaryTable(
    maes, 
    var.name = "gleason_grade", 
    vals=5:10
)

## ----gleasons2, results = 'asis', echo=FALSE----------------------------------
knitr::kable(gleasons, caption = "Gleason grades across datasets in 
curatedPCaData")

## ----groups1------------------------------------------------------------------
# Create a summary table of grade groups
gradegroups <- curatedPCaData::getPCaSummaryTable(
    maes, 
    var.name = "grade_group", 
    vals=c("<=6", "3+4", "4+3", "7", ">=8")
)

## ----groups2, results = 'asis', echo=FALSE------------------------------------
knitr::kable(gradegroups, caption = "Grade groups across datasets in 
curatedPCaData")

## ----recurrences1-------------------------------------------------------------
# Create a summary table of biochemical recurrences
recurrences <- curatedPCaData::getPCaSummarySurv(
    maes, 
    event.name = "disease_specific_recurrence_status", 
    time.name = "days_to_disease_specific_recurrence"
)

## ----recurrences2, results = 'asis', echo=FALSE-------------------------------
knitr::kable(recurrences, caption = "Disease recurrence end point across 
datasets in curatedPCaData")

## ----os1----------------------------------------------------------------------
# Create a summary table of overall survival
survivals <- curatedPCaData::getPCaSummarySurv(
    maes, 
    event.name = "overall_survival_status", 
    time.name = "days_to_overall_survival"
)

## ----os2, results = 'asis', echo=FALSE----------------------------------------
knitr::kable(survivals, caption = "Overall survival end point across datasets 
in curatedPCaData")

## ----tcgaex-------------------------------------------------------------------
tcga_subset <- getPCa(
    dataset = "tcga", 
    assays = c("gex.rsem.log", "xcell", "scores"), 
    timestamp = "20230215"
)

tcga_subset

## ----ehquery------------------------------------------------------------------
mae_tcga <- getPCa("tcga")
mae_taylor <- getPCa("taylor")

## ----access-------------------------------------------------------------------
mae_taylor[["gex.rma"]][1:5, 1:5]

## ----clinical-----------------------------------------------------------------
MultiAssayExperiment::colData(mae_tcga)[1:2, ]

## ----metadat------------------------------------------------------------------
metadata <- read.csv(system.file("extdata", "metadata.csv", package = 
    "curatedPCaData"))
head(metadata)

## ----samplecountsoverlap------------------------------------------------------
# Retrieve samples counts across different unique assay names as well as omics 
# overlap sample counts
samplecounts <- curatedPCaData::getPCaSummarySamples(maes)

## ----samplecounts, results='asis', echo=FALSE---------------------------------
knitr::kable(samplecounts$Samples, caption = "Sample N counts in each omics for 
every MAE object")

## ----sampleoverlap, results='asis', echo=FALSE--------------------------------
knitr::kable(samplecounts$Overlap, caption = "Sample N counts for intersections 
between different omics")

## -----------------------------------------------------------------------------
mae_taylor

## -----------------------------------------------------------------------------
head(mae_taylor[["cibersort"]])[1:5, 1:3]

## -----------------------------------------------------------------------------
head(mae_taylor[["quantiseq"]])[1:5, 1:3]
head(mae_taylor[["xcell"]])[1:5, 1:3]
head(mae_taylor[["epic"]])[1:5, 1:3]
head(mae_taylor[["mcp"]])[1:5, 1:3]

## ----scores-------------------------------------------------------------------
mae_tcga[["scores"]][, 1:4]

## -----------------------------------------------------------------------------
taylor <- getPCa("taylor", 
    assays = c("gex.rma", "cna.gistic"),
    sampletypes = "primary"
)

class(taylor) 

taylor

## -----------------------------------------------------------------------------
library(survival)
# BCR events
taylor_bcr <- colData(taylor)$disease_specific_recurrence_status
# BCR events / censoring follow-up time
taylor_fu <- colData(taylor)$days_to_disease_specific_recurrence

taylor_surv <- Surv(event = taylor_bcr, time = taylor_fu)

class(taylor_surv)

head(taylor_surv)


## -----------------------------------------------------------------------------
library(survminer)

taylor_bcr_gleason <- data.frame(bcr = taylor_surv, 
    gleason = colData(taylor)$gleason_grade)

fit <- survfit(bcr ~ gleason, data = taylor_bcr_gleason)
ggsurvplot(fit, 
    data = taylor_bcr_gleason, 
    ylab = "Biochemical recurrence free proportion",
    risk.table = TRUE,
    size = 1,
    pval = TRUE,
    ggtheme = theme_bw()
    )

## -----------------------------------------------------------------------------
taylor_coxdat <- MultiAssayExperiment::wideFormat(taylor["PTEN",,],
    colDataCols = c("age_at_initial_diagnosis", "gleason_grade",
    "disease_specific_recurrence_status", 
    "days_to_disease_specific_recurrence"))

taylor_coxdat <- as.data.frame(taylor_coxdat)

taylor_coxdat$y <- Surv(
    time = taylor_coxdat$days_to_disease_specific_recurrence,
    event = taylor_coxdat$disease_specific_recurrence_status)
    
head(taylor_coxdat)    

## -----------------------------------------------------------------------------
coxmodel <- coxph(y ~ cna.gistic_PTEN + gex.rma_PTEN + gleason_grade, 
    data = taylor_coxdat)
coxmodel

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

