---
title: "Example for Survival Data -- Breast Invasive Carcinoma"
author: "André Veríssimo"
date: "`r Sys.Date()`"
output: 
  BiocStyle::html_document:
    number_sections: yes
    toc: true
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Example for Survival Data -- Breast Invasive Carcinoma}
  %\VignetteEncoding{UTF-8}
params:
  seed: !r 41
--- 

## Instalation

```{r, eval=FALSE}
if (!require("BiocManager")) {
    install.packages("BiocManager")
}
BiocManager::install("glmSparseNet")
```

# Required Packages

```{r packages, message=FALSE, warning=FALSE, results='hide'}
library(dplyr)
library(ggplot2)
library(survival)
library(futile.logger)
library(curatedTCGAData)
library(TCGAutils)
library(MultiAssayExperiment)
#
library(glmSparseNet)
#
# Some general options for futile.logger the debugging package
flog.layout(layout.format("[~l] ~m"))
options(
    "glmSparseNet.show_message" = FALSE,
    "glmSparseNet.base_dir" = withr::local_tempdir()
)
# Setting ggplot2 default theme as minimal
theme_set(ggplot2::theme_minimal())
```

# Load data

The data is loaded from an online curated dataset downloaded from TCGA using
`curatedTCGAData` bioconductor package and processed.

To accelerate the process we use a very reduced dataset down to 107 variables 
only *(genes)*, which is stored as a data object in this package. However, the 
procedure to obtain the data manually is described in the following chunk.

```{r curated_data, include=FALSE}
# chunk not included as it produces to many unnecessary messages
brca <- tryCatch(
    {
        curatedTCGAData(
            diseaseCode = "BRCA",
            assays = "RNASeq2GeneNorm",
            version = "1.1.38",
            dry.run = FALSE
        )
    },
    error = function(err) {
        NULL
    }
)
```

```{r curated_data_non_eval, eval=FALSE}
brca <- curatedTCGAData(
    diseaseCode = "BRCA", assays = "RNASeq2GeneNorm",
    version = "1.1.38", dry.run = FALSE
)
```

```{r data, warning=FALSE, message=FALSE, eval=!is.null(brca)}
# keep only solid tumour (code: 01)
brcaPrimarySolidTumor <- TCGAutils::TCGAsplitAssays(brca, "01")
xdataRaw <- t(assay(brcaPrimarySolidTumor[[1]]))

# Get survival information
ydataRaw <- colData(brcaPrimarySolidTumor) |>
    as.data.frame() |>
    # Keep only data relative to survival or samples
    dplyr::select(
        patientID, vital_status,
        Days.to.date.of.Death, Days.to.Date.of.Last.Contact,
        days_to_death, days_to_last_followup,
        Vital.Status
    ) |>
    # Convert days to integer
    dplyr::mutate(Days.to.date.of.Death = as.integer(Days.to.date.of.Death)) |>
    dplyr::mutate(
        Days.to.Last.Contact = as.integer(Days.to.Date.of.Last.Contact)
    ) |>
    # Find max time between all days (ignoring missings)
    dplyr::rowwise() |>
    dplyr::mutate(
        time = max(days_to_last_followup, Days.to.date.of.Death,
            Days.to.Last.Contact, days_to_death,
            na.rm = TRUE
        )
    ) |>
    # Keep only survival variables and codes
    dplyr::select(patientID, status = vital_status, time) |>
    # Discard individuals with survival time less or equal to 0
    dplyr::filter(!is.na(time) & time > 0) |>
    as.data.frame()

# Set index as the patientID
rownames(ydataRaw) <- ydataRaw$patientID

# Get matches between survival and assay data
xdataRaw <- xdataRaw[
    TCGAbarcode(rownames(xdataRaw)) %in% rownames(ydataRaw),
]
xdataRaw <- xdataRaw[, apply(xdataRaw, 2, sd) != 0] |>
    scale()

# Order ydata the same as assay
ydataRaw <- ydataRaw[TCGAbarcode(rownames(xdataRaw)), ]

# Using only a subset of genes previously selected to keep this short example.
set.seed(params$seed)
smallSubset <- c(
    "CD5", "CSF2RB", "IRGC", "NEUROG2", "NLRC4", "PDE11A",
    "PTEN", "TP53", "BRAF",
    "PIK3CB", "QARS", "RFC3", "RPGRIP1L", "SDC1", "TMEM31",
    "YME1L1", "ZBTB11", sample(colnames(xdataRaw), 100)
) |>
    unique()

xdata <- xdataRaw[, smallSubset[smallSubset %in% colnames(xdataRaw)]]
ydata <- ydataRaw |> dplyr::select(time, status)
```

# Fit models

Fit model model penalizing by the hubs using the cross-validation function by 
`cv.glmHub`.

```{r fit, eval=!is.null(brca)}
set.seed(params$seed)
fitted <- cv.glmHub(xdata, Surv(ydata$time, ydata$status),
    family = "cox",
    lambda = buildLambda(1),
    network = "correlation",
    options = networkOptions(
        cutoff = .6,
        minDegree = .2
    )
)
```

# Results of Cross Validation

Shows the results of `100` different parameters used to find the optimal value 
in 10-fold cross-validation. The two vertical dotted lines represent the best 
model and a model with less variables selected *(genes)*, but within a standard 
error distance from the best.

```{r results, eval=!is.null(brca)}
plot(fitted)
```

## Coefficients of selected model from Cross-Validation

Taking the best model described by `lambda.min`

```{r show_coefs, eval=!is.null(brca)}
coefsCV <- Filter(function(.x) .x != 0, coef(fitted, s = "lambda.min")[, 1])
data.frame(
    gene.name = names(coefsCV),
    coefficient = coefsCV,
    stringsAsFactors = FALSE
) |>
    arrange(gene.name) |>
    knitr::kable()
```

## Survival curves and Log rank test

```{r, eval=!is.null(brca)}
separate2GroupsCox(as.vector(coefsCV),
    xdata[, names(coefsCV)],
    ydata,
    plotTitle = "Full dataset", legendOutside = FALSE
)
```

# Session Info

```{r sessionInfo}
sessionInfo()
```