preprocess_data_affymetrix_gcrma.R 4.13 KB
Newer Older
1
#' @title Preprocess an Affymetrix dataset with GC-RMA.
2
3
4
5
6
7
#'
#' @description This function preprocess an Affymetrix dataset using RMA and saves the
#' results in a given TSV file. In addition, it returns the ESET object.
#'
#' The function assumes that a folder containing the raw data exists (as cel files).
#'
8
#' Note: the function does not check for the existence of folders or files.
9
10
#'
#' @param input_data_dir A string representing the folder that contains the input data.
11
12
#' @param output_data_files An array of strings representing the files that should contain the
#' preprocessed data. At least one value, maximum two if batch_correction is "BOTH".
13
#' @param compressed A boolean representing whether the cel files are compressed. This
14
15
16
#' is FALSE by default.
#' @param batch_correction A String indicating whether batch correction should
#' be performed. Options are "TRUE", "FALSE", "BOTH", default to "FALSE".
17
#' @param batch_filename A string indicating where the batch information can be found,
18
#' default to 'Batch.tsv'. Note: not yet suported.
19
#' @param clean_samples A boolean indicating whether the dataset should be cleaned by removing
20
#' the samples that do not have clinical data. Default to FALSE.
21
#' @param verbose A boolean representing whether the function should display log information. This
22
23
24
25
26
27
28
29
#' is TRUE by default.
#' @return The expression data as ESET objects. Potentially only one object (therefore unlisted).
preprocess_data_affymetrix_gcrma <- function(input_data_dir, output_data_files,
                                             compressed       = FALSE,
                                             batch_correction = "FALSE",
                                             batch_filename   = "Batch.tsv",
                                             clean_samples    = FALSE,
                                             verbose          = TRUE) {
30
31
32
33
34
35
36
37

  # We define the I/Os.
  raw_data_input_dir <- paste0(input_data_dir, "RAW/")

  # We run the RMA pre-processing method on the data.
  input_data_files <- affy::list.celfiles(raw_data_input_dir, full.names = TRUE)
  remove(raw_data_input_dir)
  batch <- affy::ReadAffy(filenames = input_data_files, compress = compressed, verbose = verbose)
38
  eset  <- gcrma::gcrma(batch)
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  # We remove the probes that have 0 variance accross the samples.
  exp_data    <- Biobase::exprs(eset)
  probe_vars  <- apply(exp_data, 1, var)
  probe_var_0 <- names(probe_vars[probe_vars == 0])
  if (length(probe_var_0) > 0) {
    clean_probe_list <- setdiff(rownames(exp_data), probe_var_0)
    eset <- Biobase::ExpressionSet(exp_data[clean_probe_list, ])
    remove(clean_probe_list)
  }
  remove(exp_data, probe_vars, probe_var_0)

  # We correct for the batch effect if necesary.
  eset_bc <- NULL
  if (batch_correction != "FALSE") {
    eset_bc <- correct_batch_effect(eset           = eset,
                                    input_data_dir = input_data_dir)
    if (batch_correction == "TRUE") {
      eset <- eset_bc
      remove(eset_bc)
    }
  } else {
    remove(eset_bc)
  }

64
65
66
  # If necessary, we remove the samples that do not have clinical data.
  if (clean_samples) {
    # We load the clinical data as to get the samples to keep.
67
68
    samples <- rownames(Biobase::pData(ArrayUtils::load_clinical_data(input_data_dir,
                                                                      verbose = FALSE)))
69
70
    # We only keep the samples with clinical data.
    eset <- eset[, samples]
71
72
73
    if (batch_correction == "BOTH") {
      eset_bc <- eset_bc[, samples]
    }
74
75
76
  }

  # We save the eset data as TSV file.
77
78
79
80
81
82
83
  utils::write.table(Biobase::exprs(eset), file = output_data_files[1], sep = "\t", quote = FALSE)
  if (batch_correction == "BOTH") {
    utils::write.table(Biobase::exprs(eset_bc),
                       file  = output_data_files[2],
                       sep   = "\t",
                       quote = FALSE)
  }
84
85
86
87
88
89
90

  # We clean up and log information.
  remove(input_data_files, batch)
  if (verbose == TRUE) {
    message(paste0("[", Sys.time(), "] Expression data pre-processed with RMA."))
  }

91
92
93
94
95
96
  # We return the created ESET(s).
  if (batch_correction == "BOTH") {
    return(list(eset, eset_bc))
  } else {
    return(eset)
  }
97
}