a b/R/DIscBIO-generic-comptSNE.R
1
#' @title Computing tSNE
2
#' @description This function is used to compute the t-Distributed Stochastic
3
#'   Neighbor Embedding (t-SNE).
4
#' @param object \code{DISCBIO} class object.
5
#' @param rseed Random integer to to yield reproducible maps across different
6
#' runs
7
#' @param max_iter maximum number of iterations to perform.
8
#' @param epoch The number of iterations in between update messages.
9
#' @param quiet if `TRUE`, suppresses intermediate output
10
#' @param ... other parameters to be passed to `tsne::tsne`
11
#' @importFrom tsne tsne
12
#' @importFrom stats as.dist cor
13
#' @return The DISCBIO-class object input with the tsne slot filled.
14
#' @examples
15
#' sc <- DISCBIO(valuesG1msTest) # changes signature of data
16
#' sc <- Clustexp(sc, cln = 2) # data must be clustered before plottin
17
#' sc <- comptSNE(sc, max_iter = 30)
18
#' head(sc@tsne)
19
#'
20
setGeneric(
21
  name = "comptSNE",
22
  def = function(
23
      object, rseed = NULL, max_iter = 5000, epoch = 500, quiet = FALSE, ...) {
24
    standardGeneric("comptSNE")
25
  }
26
)
27
28
#' @rdname comptSNE
29
#' @export
30
setMethod(
31
  f = "comptSNE",
32
  signature = "DISCBIO",
33
  definition = function(object, rseed, max_iter, epoch, quiet, ...) {
34
    # ======================================================================
35
    # Validating
36
    # ======================================================================
37
    ran_k <- length(object@kmeans$kpart) > 0
38
    ran_m <- length(object@MBclusters) > 0
39
    if (ran_k) {
40
      di <- dist.gen(as.matrix(object@distances))
41
    } else if (ran_m) {
42
      di <- dist.gen(as.matrix(t(object@fdata)))
43
    } else {
44
      stop("run clustexp before comptSNE")
45
    }
46
    # ======================================================================
47
    # Computing
48
    # ======================================================================
49
    set.seed(rseed)
50
    if (quiet) {
51
      ts <- suppressMessages(
52
        tsne(di, max_iter = max_iter, epoch = epoch, ...)
53
      )
54
    } else {
55
      message("This function may take time")
56
      ts <- tsne(di, max_iter = max_iter, epoch = epoch, ...)
57
    }
58
    # ======================================================================
59
    # Filling output
60
    # ======================================================================
61
    if (ran_k) {
62
      object@tsne <- as.data.frame(ts)
63
    } else if (ran_m) {
64
      object@MBtsne <- as.data.frame(ts)
65
    }
66
    return(object)
67
  }
68
)