load_clinical_data.R 2.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#' @title Loads a table containing clinical data.
#'
#' @description This function loads the clinical data associated with a dataset. It returns an annotated
#' data-frame that contains the clinical data.
#'
#' Note: the function assumes that a TSV file containing the clinical data exists. In
#' particular, it does not check for the existence of folders or files.
#'
#' @param data_dir A string representing the folder that contains the clinical data.
#' @param clinical_file_name A string containing the file name. By default, this is 'ClinicalData.tsv'
11
#' @param use_factors A boolean stating whether the columns should be read as factors (default FALSE).
12
#' @param verbose A boolean representing whether the function should display log information. This
13
#'  is FALSE by default.
14
15
16
#' @return An annotated data-frame that contains the clinical data.
load_clinical_data <- function(data_dir,
                               clinical_file_name = "ClinicalData.tsv",
17
                               use_factors        = TRUE,
18
                               verbose            = FALSE) {
19
20
21
22
23
24
25
26

  # We define the I/Os.
  clinical_data_file <- paste0(data_dir, clinical_file_name)
  if (verbose == TRUE) {
    message(paste0("[", Sys.time(), "] File set to ", clinical_data_file))
  }

  # We load the clinical data.
27
28
29
30
31
32
33
34
35
36
  pheno_data <- NULL
  if (use_factors) {
    pheno_data <- Biobase::AnnotatedDataFrame(utils::read.delim(file       = clinical_data_file,
                                                                row.names  = 1,
                                                                colClasses = "factor"))
  } else {
    # We do not use factors.
    pheno_data <- Biobase::AnnotatedDataFrame(utils::read.delim(file       = clinical_data_file,
                                                                row.names  = 1))
  }
37
38
39
40

  # We clean up and log information.
  rm(clinical_data_file)
  if (verbose == TRUE) {
41
    data_dimensions <- paste0(dim(pheno_data), collapse = " * ")
42
    message(paste0("[", Sys.time(), "] Clinical data read (", data_dimensions, ")."))
43
    rm(data_dimensions)
44
45
46
47
48
  }

  # We return the clinical data.
  return(pheno_data)
}