a b/R/DIscBIO-generic-pseudoTimeOrdering.R
1
#' @title Pseudo-time ordering
2
#' @description This function takes the exact output of exprmclust function and
3
#'   construct Pseudo-time ordering by mapping all cells onto the path that
4
#'   connects cluster centers.
5
#' @param object \code{DISCBIO} class object.
6
#' @param quiet if `TRUE`, suppresses intermediary output
7
#' @param export if `TRUE`, exports order table to csv
8
#' @param filename Name of the exported file (if `export=TRUE`)
9
#' @importFrom TSCAN TSCANorder
10
#' @return The DISCBIO-class object input with the kordering slot filled.
11
setGeneric("pseudoTimeOrdering", function(
12
    object,
13
    quiet = FALSE,
14
    export = FALSE,
15
    filename = "Cellular_pseudo-time_ordering") {
16
  standardGeneric("pseudoTimeOrdering")
17
})
18
19
#' @export
20
#' @rdname pseudoTimeOrdering
21
setMethod(
22
  "pseudoTimeOrdering",
23
  signature = "DISCBIO",
24
  definition = function(object, quiet, export, filename) {
25
    # ======================================================================
26
    # Validating
27
    # ======================================================================
28
    ran_k <- length(object@kmeans$kpart) > 0
29
    ran_m <- length(object@MBclusters) > 0
30
    if (ran_k) {
31
      Obj <- object@fdata
32
      Names <- object@cpart
33
      lpsmclust <- Exprmclust(Obj, K = 4, reduce = FALSE, cluster = Names)
34
      lpsorder <- TSCANorder(lpsmclust)
35
    } else if (ran_m) {
36
      Obj <- object@fdata
37
      Names <- names(object@MBclusters$clusterid)
38
      lpsmclust <- object@MBclusters
39
      lpsorder <- TSCANorder(lpsmclust)
40
    } else {
41
      stop("run clustexp before this pseudoTimeOrdering")
42
    }
43
    # ======================================================================
44
    # Ordering
45
    # ======================================================================
46
    sampleNames <- colnames(Obj)
47
    orderID <- lpsorder
48
    order <- seq_len(length(lpsorder))
49
    orderTable <- data.frame(order, orderID)
50
    if (export) write.csv(orderTable, file = paste0(filename, ".csv"))
51
    if (!quiet) print(orderTable)
52
    FinalOrder <- orderTable[match(sampleNames, orderTable$orderID), ]
53
    out_order <- FinalOrder[, 1]
54
    names(out_order) <- names(Names)
55
    if (ran_k) {
56
      object@kordering <- out_order
57
    } else if (ran_m) {
58
      object@MBordering <- out_order
59
    }
60
    return(object)
61
  }
62
)