[go: up one dir, main page]

File: dpt-helpers.r

package info (click to toggle)
r-bioc-destiny 3.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,436 kB
  • sloc: cpp: 174; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,337 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#' Find a random root cell index
#' 
#' Finds a cell that has the maximum DPT distance from a randomly selected one.
#' 
#' @param dm_or_dpt  A \code{\link{DiffusionMap}} or \code{\link{DPT}} object
#' 
#' @return A cell index
#' 
#' @examples
#' data(guo)
#' dm <- DiffusionMap(guo)
#' random_root(dm)
#' 
#' @export
random_root <- function(dm_or_dpt) {
	dpt <- dummy_dpt(dm_or_dpt)
	random_idx <- sample.int(nrow(dpt), 1L)
	which.max(dpt[random_idx, ])
}


#' Find tips in a DiffusionMap object
#' 
#' @param dm_or_dpt  A \code{\link{DiffusionMap}} or \code{\link{DPT}} object
#' @param root       Root cell index from which to find tips. (default: random)
#' 
#' @return An integer vector of length 3
#' 
#' @examples
#' data(guo)
#' dm <- DiffusionMap(guo)
#' is_tip <- l_which(find_tips(dm), len = ncol(guo))
#' plot(dm, col = factor(is_tip))
#' 
#' @export
find_tips <- function(dm_or_dpt, root = random_root(dm_or_dpt)) {
	dpt <- dummy_dpt(dm_or_dpt)
	tipstats(dpt, seq_len(nrow(dpt)), root)$tips
}

tipstats <- function(dpt, cells, tips) {
	x <- tips[[1L]]
	dx <- dpt[x, cells]
	y <- if (length(tips) >= 2L) tips[[2L]] else cells[[which.max(dx)]]
	dy <- dpt[y, cells]
	z <- if (length(tips) == 3L) tips[[3L]] else cells[[which.max(dx + dy)]]
	
	list(
		tips = c(x, y, z),
		dx = dx, dy = dy,
		g = max(dx + dy) / min(dx + dy))
}