Switch to unified view

a b/R/DIscBIO-generic-FinalPreprocessing.R
1
#' @title Final Preprocessing
2
#' @description This function generates the final filtered normalized dataset.
3
#' @param object \code{DISCBIO} class object.
4
#' @param GeneFlitering GeneFlitering has to be one of the followings:
5
#'   ["NoiseF","ExpF"]. Default is "NoiseF"
6
#' @param export A logical vector that allows writing the final gene list in
7
#'   excel file. Default is TRUE.
8
#' @param quiet if `TRUE`, intermediary output is suppressed
9
#' @param fileName File name for exporting (if `export = TRUE`)
10
#' @return The DISCBIO-class object input with the FinalGeneList slot filled.
11
#' @examples
12
#' #sc <- DISCBIO(valuesG1msTest)
13
#' #sc <- NoiseFiltering(sc, percentile = 0.9, CV = 0.2, export = FALSE)
14
#' #sc <- FinalPreprocessing(sc, GeneFlitering = "NoiseF", export = FALSE)
15
#'
16
setGeneric(
17
  "FinalPreprocessing",
18
  function(object,
19
           GeneFlitering = "NoiseF",
20
           export = FALSE,
21
           quiet = FALSE,
22
           fileName = "filteredDataset") {
23
    standardGeneric("FinalPreprocessing")
24
  }
25
)
26
27
#' @export
28
#' @rdname FinalPreprocessing
29
setMethod(
30
  "FinalPreprocessing",
31
  signature = "DISCBIO",
32
  definition = function(object, GeneFlitering, export, quiet, fileName) {
33
    if (GeneFlitering == "NoiseF") {
34
      if (length(object@noiseF) < 1) {
35
        stop("run NoiseFiltering before running FinalPreprocessing")
36
      }
37
      if (nrow(object@fdata) < 1) {
38
        stop("run Normalizedata before running FinalPreprocessing")
39
      }
40
      gene_list <- object@noiseF
41
      gene_names <- rownames(object@fdata)
42
      idx_genes <- is.element(gene_names, gene_list)
43
      gene_names2 <- gene_names[idx_genes]
44
      filteredDataset <- object@fdata[gene_names2, ]
45
      object@fdata <- filteredDataset
46
      object@FinalGeneList <- rownames(filteredDataset)
47
    }
48
    if (GeneFlitering == "ExpF") {
49
      if (nrow(object@fdata) < 1) {
50
        stop("run Normalizedata before running FinalPreprocessing")
51
      }
52
      filteredDataset <- object@fdata
53
      object@FinalGeneList <- rownames(filteredDataset)
54
    }
55
    if (!quiet) {
56
      message(
57
        "The gene filtering method = Noise filtering\n\n",
58
        "The Filtered Normalized dataset contains:\n",
59
        "Genes: ", length(filteredDataset[, 1]), "\n",
60
        "cells: ", length(filteredDataset[1, ]), "\n\n"
61
      )
62
    }
63
    if (export) {
64
      fileNameExt <- paste0(fileName, ".Rdata")
65
      if (!quiet) {
66
        message(
67
          "The Filtered Normalized dataset was saved as: ",
68
          fileNameExt
69
        )
70
      }
71
      save(filteredDataset, file = fileNameExt)
72
    }
73
    return(object)
74
  }
75
)