R/function.R

Defines functions gs_gmt pancancer_ssGSEA survivalTCGA

########################################
## Pancancer Immune ####################

#####################################################
##http://stackoverflow.com/questions/6602881/text-file-to-list-in-r

 
#' Make gene set list from gmt file
#' Make gene set list from gmt file 
#' @param a gmt file
#' @return geneset list
#' @import GSVA RTCGAToolbox compareGroups
#' @export


gs_gmt <- function(a){
  # Read in the data
  x <- scan(a, what="", sep="\n")
  # Separate elements by tab
  y <- strsplit(x, "\t")
  # Extract the first vector element and set it as the list element name
  names(y) <- sapply(y, `[[`, 2)
  #names(y) <- sapply(y, function(x) x[[1]]) # same as above
  # Remove the first vector element from each list element
  y <- lapply(y, `[`, -1:-2)
  return(y)
}

 
#' Make enrichment score (es) of ssGSEA from GDAC firehose TCGA data
#' Make enrichment score (es) of ssGSEA from GDAC firehose TCGA data
#' @param dataset name of TCGA dataset \code{\link{getFirehoseDatasets}} 
#' @param rdate charcter of run date of GDAC  \code{\link{getFirehoseDatasets}}
#' @param gs gene set result from gs_gmt \code{\link{gs_gmt}} 
#' @return matrix of enrichment score 
#' @export

ssGSEA <- function (dataset, rdate, gs){
  readData = getFirehoseData (dataset=dataset, runDate=rdate, RNAseq2_Gene_Norm=TRUE)
  mRNA <- getData(readData, 'RNASeq2GeneNorm')
  es <- gsva(mRNA, gs, method = 'ssgsea', rnaseq = TRUE )
  return (es)
}


#' Make enrichment score (es) of ssGSEA from multiple GDAC firehose TCGA data
#' Make enrichment score (es) of ssGSEA from multiple GDAC firehose TCGA data
#' @param dataset vector of names of TCGA dataset \code{\link{getFirehoseDatasets}}
#' @param rdate charcter of run date of GDAC  \code{\link{getFirehoseDatasets}}
#' @param gs gene set result from gs_gmt \code{\link{gs_gmt}}
#' @return matrix of enrichment score of multiple TCGA dataset and vector of tumor type
#' @export

pancancer_ssGSEA <- function(dataset, rdate, gs){
  d <- c()
  t <- c()
  for (i in dataset){
    b <- ssGSEA(i, rdate, gs)
    d <- cbind(d, b)
    n <- dim(b)[2]
    t <- c(t, rep(i, n))
  }
  return (list(es = d, type = t))
}

#' Transform survival data of GDAC firehose TCGA
#' Transform survival data of GDAC firehose TCGA
#' @param data clinical data.frame of GDAC firehose TCGA \code{\link{getFirehoseDatasets}}
#' @return trandformed clinical data.frame
#' @export

survivalTCGA <- function(data) {
  data$ID <- toupper(rownames(data))
  
  if (is.numeric(class(data$days_to_death)) == FALSE){
  data$days_to_death <-as.character(data$days_to_death)
  data$days_to_death <- as.numeric(data$days_to_death)}
  
  if (is.numeric(class(data$days_to_last_followup)) == FALSE){
  data$days_to_last_followup <- as.character(data$days_to_last_followup)
  data$days_to_last_followup <- as.numeric(data$days_to_last_followup)}

  data$surv_months <- pmax(data$days_to_death, 
                           data$days_to_last_followup,
                           na.rm = TRUE)/30.4
  return(data)
}

#' make clinical table from TCGA data
#' make clinical table from TCGA data
#' @param data clinical data.frame of TCGA
#' @param variables clinical variables   
#' @return clinical table file
#' @import compareGroups
#' @export

clinical_table_all <- function (data, variables) {
  a <- data.frame(data[,colnames(data) %in% variables])
  compareGroups(~ .,
              data = a) -> table

createTable(table) -> table
}
Jkang-alien/ssGSEA4TCGA documentation built on May 7, 2019, 10:53 a.m.