Diff of /R/preprocessCELFiles.R [000000] .. [28aa3b]

Switch to unified view

a b/R/preprocessCELFiles.R
1
#' RMA preprocess CEL files
2
#'
3
#' General function for RMA processing microarray data. Automatically downloads
4
#' custom Brainarray chip definition files (cdf) if wanted.
5
#'
6
#' @param cel_files A character vector of .CEL files.
7
#' @param cdf A character specifying the CDF type to use. Should be either
8
#'   \code{"brainarray"} or \code{"affy"}.
9
#' @param target Specifies the "target" CDF to use depending on \code{cdf}.
10
#'   If \code{cdf} is \code{"affy"} then \code{target} is e.g. \code{"core"}.
11
#'   If \code{cdf} is \code{"brainarray"} then \code{target} is
12
#'   e.g. \code{"ensg"}, \code{"entrez"}.
13
#'   See \code{listTargets()} to list the available targets.
14
#' @param version A character giving the brainarray version to be used.
15
#'   Only used if \code{cdf} is \code{"brainarray"}. See \code{listVersions()}.
16
#' @param background boolean. Should RMA background correction be performed?
17
#' @param normalize boolean. Should RMA normalization be performed?
18
#' @return An expression set object.
19
#' @importFrom affy read.affybatch just.rma
20
#' @importFrom affyio read.celfile.header
21
#' @importFrom oligo rma
22
#' @export
23
preprocessCELFiles <- function(cel_files,
24
                               cdf = c("affy", "brainarray"),
25
                               target,
26
                               version = getLatestVersion(),
27
                               background = TRUE,
28
                               normalize = TRUE) {
29
  getCDF <- function(file) {
30
    return(read.celfile.header(file)$cdfName)
31
  }
32
  array_type <- sapply(cel_files, getCDF)
33
  array_types <- unique(array_type)
34
  if (length(array_types) != 1) {
35
    stop("The CEL files are from more that one array platform. The unique ",
36
         "platforms are: ", paste(array_types, collapse = ", "),
37
         ". The arrays should be preprocessed in batches of array platforms.")
38
  }
39
  array_type <- array_type[1]
40
  cel_files <- normalizePath(cel_files)
41
  cdf <- match.arg(cdf)
42
43
  if (tolower(cdf) == "affy") {
44
    # Load expression set
45
    es <- oligo::read.celfiles(cel_files)
46
47
    # RMA normalize
48
    if (class(es) %in% c("ExonFeatureSet","HTAFeatureSet","GeneFeatureSet")) {
49
      if (missing(target)) stop("No target provided.")
50
      es_rma <- oligo::rma(es, background = background,
51
                           normalize = normalize, target = target)
52
      attr(es_rma, "target") <- target
53
    } else {
54
      es_rma <- oligo::rma(es, background = background,
55
                           normalize = normalize)
56
      attr(es_rma, "target") <- NULL
57
    }
58
59
  } else if (tolower(cdf) == "brainarray") {
60
61
    if (missing(target)) stop("No target provided.")
62
63
    req <- requireBrainarray(array_type = array_type,
64
                             custom_cdf = target,
65
                             version = version)
66
    suppressWarnings({
67
      es_rma <- just.rma(filenames = cel_files,
68
                         verbose = TRUE,
69
                         cdfname = getCustomCDFName(req$brain_dat, array_type))
70
    })
71
    attr(es_rma, "target") <- target
72
73
  } else {
74
    stop("cdf == '", cdf,
75
         "' not supported. Should be either 'affy' or 'brainarray'")
76
  }
77
78
  # Add attributes
79
  attr(es_rma, "cdf") <- cdf
80
  attr(es_rma, "version") <- version
81
  attr(es_rma, "background") <- background
82
  attr(es_rma, "normalize") <- normalize
83
84
  return(es_rma)
85
}