Switch to unified view

a b/R/DIscBIO-generic-clusteringOrder.R
1
#' @title Pseudo-time ordering based on k-means clusters
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
#' @note This function has been replaced by pseudoTimeOrdering(), but it is
11
#'   being kept for legacy purposes. It will, however, be removed from future
12
#'   versions of DIscBIO.
13
#' @return The DISCBIO-class object input with the kordering slot filled.
14
setGeneric("KmeanOrder", function(
15
    object, quiet = FALSE, export = FALSE,
16
    filename = "Cellular_pseudo-time_ordering_based_on_k-meansc-lusters") {
17
  standardGeneric("KmeanOrder")
18
})
19
20
#' @export
21
#' @rdname KmeanOrder
22
setMethod(
23
  "KmeanOrder",
24
  signature = "DISCBIO",
25
  definition = function(object, quiet, export, filename) {
26
    warning(
27
      "KmeanOrder() has been replaced with pseudoTimeOrdering(), ",
28
      "which performs pseudo-time ordering for both k-means ",
29
      "and model-based clustering. ",
30
      "This function is being kept for legacy purposes, ",
31
      "but will be removed in future versions of DIscBIO. ",
32
      "Please adapt your scripts accordingly."
33
    )
34
    # Validation
35
    if (length(object@kmeans$kpart) == 0) {
36
      stop("run Clustexp before KmeanOrder")
37
    }
38
39
    Obj <- object@fdata
40
    Clusters <- object@cpart
41
    sampleNames <- colnames(object@fdata)
42
    lpsmclust <- Exprmclust(Obj, K = 4, reduce = FALSE, cluster = Clusters)
43
    lpsorder <- TSCANorder(lpsmclust)
44
    orderID <- lpsorder
45
    order <- seq_len(length(lpsorder))
46
    orderTable <- data.frame(order, orderID)
47
    if (export) write.csv(orderTable, file = paste0(filename, ".csv"))
48
    if (!quiet) print(orderTable)
49
    FinalOrder <- orderTable[match(sampleNames, orderTable$orderID), ]
50
    out_order <- FinalOrder[, 1]
51
    names(out_order) <- names(Clusters)
52
    object@kordering <- out_order
53
    return(object)
54
  }
55
)