R/guided_MST.R

Defines functions guided_MST

Documented in guided_MST

#' guided_MST
#' 
#' Minimum spanning tree to guide pseudotime construction
#'
#' This function should be used when users have already performed PCA or other dimension reductions, and cell clustering. This function will plot a minimum-spanning-tree that helps users to determine which cell clusters should be linked in what order. The result can be fed into guided_tscan function to get the actual pseudotime.
#' 
#' @param dr A numeric matrix of reduced dimensions. Can be from PCA, t-sne, UMAP, diffusion map, and many others. Each row is a cell and each column is a reduced dimension. Row names should be the cell names.
#' @param clu A named vector of cell clustering. Names are the cell names. Need to correspond to dr.
#' @return A minimum-spanning-tree plot generated by igraph package
#' @import igraph
#' @export
#' @author Zhicheng Ji <zhicheng.ji@@duke.edu>
#' @examples
#' # see vignette

guided_MST <- function(dr,clu) {
      n <- names(clu)
      clu <- as.character(clu)
      names(clu) <- n
      cm <- sapply(unique(clu),function(i) colMeans(dr[clu==i,]))
      dp <- as.matrix(dist(t(cm)))
      gp <- graph.adjacency(dp, mode = "undirected", weighted = TRUE)
      dp_mst <- minimum.spanning.tree(gp)
      plot(dp_mst)
}
zji90/TSCAN documentation built on Sept. 14, 2022, 10:56 a.m.