Skip to content

Commit

Permalink
round on survival tools
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola-calonaci committed Mar 21, 2024
1 parent 02727d7 commit bf25046
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 57 deletions.
7 changes: 6 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ export(classification)
export(classify)
export(compute_likelihood)
export(compute_posterior)
export(cox_fit)
export(init)
export(kaplan_meier_fit)
export(parameters)
export(plot_class_fraction)
export(plot_classification)
export(plot_survival_analysis)
export(posterior)
export(survival_fit)
import(cli)
import(crayon)
importFrom(dplyr,"%>%")
importFrom(dplyr,filter)
importFrom(dplyr,mutate)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(survival,Surv)
importFrom(survival,coxph)
importFrom(survival,survfit)
43 changes: 43 additions & 0 deletions R/cox_fit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Multivariate Cox regression

#' Fit multivariate Cox regression model based on INCOMMON classes.
#'
#' @param x A list of objects of class \code{'INCOMMON'} containing the classification results for
#' multiple samples, as produced by using function `classify`.
#' @param tumor_type The selected tumor type.
#' @param gene The selected gene.
#' @param covariates Covariates used in the multivariate regression.
#' @return An object of class \code{coxph}.
#' @export
#' @importFrom dplyr filter mutate rename select %>%
#' @importFrom survival Surv coxph

cox_fit = function(x, gene, tumor_type, covariates = c('age', 'sex', 'tmb')){

x = prepare_km_fit_input(x, tumor_type, gene)

formula = 'survival::Surv(OS_MONTHS, OS_STATUS) ~ group'

for(c in covariates) {
what = grep(c, colnames(x), ignore.case = T, value = TRUE)
for(w in what) {
if(is.numeric(x[[w]])){
q = quantile(x[w], na.rm = T)['50%']
x[[w]] = ifelse(x[[w]] > q, paste0('>', round(q, 0)), paste0('<=', round(q, 0)))
x[[w]] = factor(x[[w]])
x[[w]] = relevel(x[[w]], ref = grep('<=', unique(x[[w]]), value = T))
}
formula = paste(formula, w, sep = ' + ')
}
}

fit = survival::coxph(
formula = formula %>% as.formula(),
data = x %>%
dplyr::mutate(group = factor(group)) %>%
dplyr::mutate(group = relevel(group, ref = grep('WT', unique(x$group), value = T))) %>%
as.data.frame()
)

return(fit)
}
26 changes: 26 additions & 0 deletions R/kaplan_meier_fit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' Fit Kaplan-Meier survival model based on INCOMMON classes.
#'
#' @param x A list of objects of class \code{'INCOMMON'} containing the classification results for
#' multiple samples, as produced by using function `classify`.
#' @param tumor_type The selected tumor type.
#' @param gene The selected gene.
#' @return An table containing the selected gene, tumor_type, the data table used to
#' fit the model and an object of class \code{'survfit'}.
#' @export
#' @importFrom dplyr filter mutate rename select %>%
#' @importFrom survival Surv survfit

kaplan_meier_fit = function(x, tumor_type, gene) {

data = prepare_km_fit_input(x, tumor_type, gene)
data = data %>%
dplyr::mutate(group = factor(group,
levels = c(
grep('WT', unique(data$group), value = T),
grep('Mutant', unique(data$group), value = T) %>% grep('with', ., invert = T, value = T),
grep('Mutant', unique(data$group), value = T) %>% grep('with', ., value = T)
)))
fit = survival::survfit(formula = survival::Surv(OS_MONTHS, OS_STATUS) ~ group, data = data)

return(tibble(gene = gene, tumor_type = tumor_type, data = list(data), fit = list(fit)))
}
5 changes: 3 additions & 2 deletions R/plot_survival_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#'
#' @param x A list of objects of class \code{'INCOMMON'} containing the classification results for
#' multiple samples, as produced by using function `classify`.
#' @return An object or a list of class \code{'ggplot2'}.
#' @return An object or a list of class \code{'ggplot2'} showing Kaplan-Meier curves and
#' Cox regression forest plot.
#' @export
#' @importFrom dplyr filter mutate rename select %>%

survival_fit = function(x, tumor_type, gene, cox_covariates = c('age', 'sex', 'tmb')){
plot_survival_analysis = function(x, tumor_type, gene, cox_covariates = c('age', 'sex', 'tmb')){

km_fit = kaplan_meier_fit(x, tumor_type, gene)

Expand Down
49 changes: 0 additions & 49 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -329,55 +329,6 @@ prepare_km_fit_input = function(x, tumor_type, gene){
dplyr::select('sample', 'tumor_type', 'gene', 'gene_role', dplyr::everything())
}

# Kaplan-Meier fit

kaplan_meier_fit = function(x, tumor_type, gene) {

data = prepare_km_fit_input(x, tumor_type, gene)
data = data %>%
dplyr::mutate(group = factor(group,
levels = c(
grep('WT', unique(data$group), value = T),
grep('Mutant', unique(data$group), value = T) %>% grep('with', ., invert = T, value = T),
grep('Mutant', unique(data$group), value = T) %>% grep('with', ., value = T)
)))
fit = survival::survfit(formula = survival::Surv(OS_MONTHS, OS_STATUS) ~ group, data = data)

return(tibble(gene = gene, tumor_type = tumor_type, data = list(data), fit = list(fit)))
}

# Multivariate Cox regression

cox_fit = function(x, gene, tumor_type, covariates = c('age', 'sex', 'tmb')){

x = prepare_km_fit_input(x, tumor_type, gene)

formula = 'survival::Surv(OS_MONTHS, OS_STATUS) ~ group'

for(c in covariates) {
what = grep(c, colnames(x), ignore.case = T, value = TRUE)
for(w in what) {
if(is.numeric(x[[w]])){
q = quantile(x[w], na.rm = T)['50%']
x[[w]] = ifelse(x[[w]] > q, paste0('>', round(q, 0)), paste0('<=', round(q, 0)))
x[[w]] = factor(x[[w]])
x[[w]] = relevel(x[[w]], ref = grep('<=', unique(x[[w]]), value = T))
}
formula = paste(formula, w, sep = ' + ')
}
}

fit = survival::coxph(
formula = formula %>% as.formula(),
data = x %>%
dplyr::mutate(group = factor(group)) %>%
dplyr::mutate(group = relevel(group, ref = grep('WT', unique(x$group), value = T))) %>%
as.data.frame()
)

return(fit)
}


forest_plot = function(x, tumor_types = FALSE){

Expand Down
24 changes: 24 additions & 0 deletions man/cox_fit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/kaplan_meier_fit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions man/survival_fit.Rd → man/plot_survival_analysis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/survival_analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ cox_fit(x = classified_data, tumor_type = 'PAAD', gene = 'KRAS', covariates = c(
Plot survival fit

```{r}
survival_fit(x = classified_data, tumor_type = 'PAAD', gene = 'KRAS', cox_covariates = c('age', 'sex', 'tmb'))
plot_survival_analysis(x = classified_data, tumor_type = 'PAAD', gene = 'KRAS', cox_covariates = c('age', 'sex', 'tmb'))
```


Expand Down

0 comments on commit bf25046

Please sign in to comment.