R/genecoxsurv.R

Defines functions genecoxsurv

Documented in genecoxsurv

#' genecoxsurv function
#'
#' performs cox univariate regression on the gene of interest
#'
#' @param pat2 data frame generated by rnasubset function
#' @param gene character(1) gene of interest
#'
#' @import data.table
#' @import survival
#' @import survminer
#' @import ggplot2
#'
#' @return survminer styled Survival plot
#'
#' @examples
#' data(skcm)
#' gene <- "SOX10"
#' sox10 <- rnasubset(pat, rna, gene, 10)
#' genecoxsurv(sox10, gene)
#'
#' @export
#'
genecoxsurv <- function(pat2, gene) {
  setkey(pat2, gene2)
  pat2.surv <- with(pat2[!("middle")], Surv(TCGA_year, vitalstatus) ~ gene2)
  survplot <- do.call(coxph, args=list(formula = pat2.surv))
  half <- summary(survplot)$table[,"median"]
  n <- summary(survplot)$table[,"records"]
  res <- ggsurvplot(survplot,
                    data = pat2[!("middle")],
                    pval=T,
                    pval.coord = c(1,0.1),
                    conf.int = T,
                    risk.table = T,
                    risk.table.y.text = F,
                    break.time.by = 1,
                    palette = c("#1a1a1a", "#d7191c"),
                    legend = c(0.8, 0.8),
                    legend.title = "Gene level",
                    legend.labs = c(sprintf("%s %s, n=%s\n Median OS %s years\n", gene, levels(pat2$gene2)[1], n[1], round(half[1],2)) ,
                                    sprintf("%s %s, n=%s\n Median OS %s years\n", gene, levels(pat2$gene2)[2], n[2], round(half[2],2))),
                    xlab = "Time Since Biopsy (Years)")
  res$table <- res$table + theme(axis.line = element_blank())
  print(res)
}
pcheng84/TCGAbrowser documentation built on Sept. 7, 2021, 8:28 p.m.