| 1 |
# Copyright (C) Tal Galili |
| 2 |
# |
| 3 |
# This file is part of dendextend. |
| 4 |
# |
| 5 |
# dendextend is free software: you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# dendextend is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# A copy of the GNU General Public License is available at |
| 16 |
# http://www.r-project.org/Licenses/ |
| 17 |
# |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#' @title Counts the number of leaves in a tree |
| 24 |
#' @rdname nleaves |
| 25 |
#' @export |
| 26 |
#' @description |
| 27 |
#' Counts the number of leaves in a tree (dendrogram or hclust). |
| 28 |
#' |
| 29 |
#' @param x tree object (dendrogram/hclust/phylo,\link{dendlist}) |
| 30 |
#' @param method a character scalar (default is "members"). If "order" |
| 31 |
#' than nleaves is based on length of \link{order.dendrogram}. |
| 32 |
#' If "members", than length is trusting what is written in the |
| 33 |
#' dendrogram's root \link{attr}. |
| 34 |
#' "members" is about 4 times faster than "order". |
| 35 |
#' |
| 36 |
#' @param ... not used |
| 37 |
#' @details |
| 38 |
#' The idea for the name is from functions like ncol, and nrow. |
| 39 |
#' |
| 40 |
#' Also, it is worth noting that the nleaves.dendrogram is based on |
| 41 |
#' order.dendrogram instead of labels.dendrogram since the first is |
| 42 |
#' MUCH faster than the later. |
| 43 |
#' |
| 44 |
#' The phylo method is based on turning the phylo to hclust and than to |
| 45 |
#' dendrogram. It may not work for complex phylo trees. |
| 46 |
#' |
| 47 |
#' @return The number of leaves in the tree |
| 48 |
#' @seealso \link{nrow}, \link{count_terminal_nodes} |
| 49 |
#' @examples |
| 50 |
#' hc <- hclust(dist(USArrests[1:5, ]), "ave") |
| 51 |
#' dend <- as.dendrogram(hc) |
| 52 |
#' |
| 53 |
#' nleaves(dend) # 5 |
| 54 |
#' nleaves(hc) # 5 |
| 55 |
nleaves <- function(x, ...) { |
| 56 |
UseMethod("nleaves") |
| 57 |
} |
| 58 |
|
| 59 |
#' @export |
| 60 |
#' @rdname nleaves |
| 61 |
nleaves.default <- function(x, ...) { |
| 62 |
stop("object x must be a dendrogram/hclust/phylo object") |
| 63 |
} |
| 64 |
|
| 65 |
#' @export |
| 66 |
#' @rdname nleaves |
| 67 |
nleaves.dendrogram <- function(x, method = c("members", "order"), ...) { |
| 68 |
if (method[1] == "members") { |
| 69 |
return(as.integer(attr(x, "members"))) |
| 70 |
} else { |
| 71 |
# "order" |
| 72 |
return(length(order.dendrogram(x))) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
#' @export |
| 78 |
#' @rdname nleaves |
| 79 |
nleaves.dendlist <- function(x, ...) { |
| 80 |
sapply(x, nleaves, ...) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
# library(microbenchmark) |
| 86 |
# big_dend <- USArrests %>% dist %>% hclust %>% as.dendrogram |
| 87 |
# microbenchmark( |
| 88 |
# nleaves.dendrogram(big_dend, "order"), |
| 89 |
# nleaves.dendrogram(big_dend, "members") |
| 90 |
# ) |
| 91 |
|
| 92 |
|
| 93 |
# TODO: add count by number of labels/ or by the number of is.leaf==TRUE |
| 94 |
|
| 95 |
|
| 96 |
#' @export |
| 97 |
#' @rdname nleaves |
| 98 |
nleaves.hclust <- function(x, ...) { |
| 99 |
length(x$order) |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
#' @export |
| 104 |
#' @rdname nleaves |
| 105 |
nleaves.phylo <- function(x, ...) { |
| 106 |
# nleaves(as.dendrogram(x)) |
| 107 |
length(labels(x)) |
| 108 |
} |
| 109 |
|
| 110 |
# TODO: there is probably a better way for getting the tree size for a phylo object. |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#' @title Counts the number of nodes (Vertices) in a tree |
| 116 |
#' @export |
| 117 |
#' @rdname nnodes |
| 118 |
#' @description Counts the number of nodes in a tree (dendrogram, hclust, phylo). |
| 119 |
#' |
| 120 |
#' @param x tree object (dendrogram or hclust) |
| 121 |
#' @param ... not used |
| 122 |
#' @details |
| 123 |
#' The idea for the name is from functions like ncol, and nrow. |
| 124 |
#' |
| 125 |
#' The phylo method is based on turning the phylo to hclust and than to |
| 126 |
#' dendrogram. It may not work for complex phylo trees. |
| 127 |
#' |
| 128 |
#' @return The number of leaves in the tree |
| 129 |
#' @seealso \link{nrow}, \link{count_terminal_nodes}, \link{nleaves} |
| 130 |
#' @examples |
| 131 |
#' hc <- hclust(dist(USArrests[1:5, ]), "ave") |
| 132 |
#' dend <- as.dendrogram(hc) |
| 133 |
#' |
| 134 |
#' nnodes(dend) # 9 |
| 135 |
#' nnodes(hc) # 9 |
| 136 |
nnodes <- function(x, ...) { |
| 137 |
UseMethod("nnodes") |
| 138 |
} |
| 139 |
|
| 140 |
#' @export |
| 141 |
#' @rdname nnodes |
| 142 |
nnodes.default <- function(x, ...) { |
| 143 |
stop("object x must be a dendrogram/hclust/phylo object") |
| 144 |
} |
| 145 |
|
| 146 |
#' @export |
| 147 |
#' @rdname nnodes |
| 148 |
nnodes.dendrogram <- function(x, ...) { |
| 149 |
if (!inherits(x, "dendrogram")) warning("'object' should be a dendrogram.") |
| 150 |
|
| 151 |
i_node_counter <- 0 |
| 152 |
|
| 153 |
go_through_nodes <- function(dend_node) { |
| 154 |
i_node_counter <<- i_node_counter + 1 |
| 155 |
} |
| 156 |
|
| 157 |
dendrapply(x, go_through_nodes) |
| 158 |
|
| 159 |
return(i_node_counter) |
| 160 |
} |
| 161 |
# nnodes(dend) |
| 162 |
|
| 163 |
|
| 164 |
#' @export |
| 165 |
#' @rdname nnodes |
| 166 |
nnodes.hclust <- function(x, ...) { |
| 167 |
nnodes(as.dendrogram(x)) |
| 168 |
} |
| 169 |
|
| 170 |
#' @export |
| 171 |
#' @rdname nnodes |
| 172 |
nnodes.phylo <- function(x, ...) { |
| 173 |
nnodes(as.dendrogram(x)) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#' @title Counts the number of terminal nodes (merging 0 nodes!) |
| 181 |
#' @export |
| 182 |
#' @description This function counts the number of "practical" terminal nodes (nodes which are not leaves, but has 0 height to them are considered "terminal" nodes). |
| 183 |
#' If the tree is standard, that would simply be the number of leaves (only the leaves will have height 0). |
| 184 |
#' However, in cases where the tree has several nodes (before the leaves) with 0 height, |
| 185 |
#' the count_terminal_nodes counts such nodes as terminal nodes |
| 186 |
#' |
| 187 |
#' The function is recursive in that it either returns 1 if it reached a terminal node (either a leaf or a 0 height node), |
| 188 |
#' else: it will count the number of terminal nodes in each of its sub-nodes, sum them up, and return them. |
| 189 |
#' |
| 190 |
#' @param dend_node a dendrogram object for which to count its number of terminal nodes (leaves or 0 height nodes). |
| 191 |
#' @param ... not used |
| 192 |
#' @return The number of terminal nodes (excluding the leaves of nodes of height 0) |
| 193 |
#' @examples |
| 194 |
#' # define dendrogram object to play with: |
| 195 |
#' hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 196 |
#' dend <- as.dendrogram(hc) |
| 197 |
#' |
| 198 |
#' ### |
| 199 |
#' # Trivial case |
| 200 |
#' count_terminal_nodes(dend) # 3 terminal nodes |
| 201 |
#' length(labels(dend)) # 3 - the same number |
| 202 |
#' plot(dend, |
| 203 |
#' main = "This is considered a tree \n with THREE terminal nodes (leaves)" |
| 204 |
#' ) |
| 205 |
#' |
| 206 |
#' ### |
| 207 |
#' # NON-Trivial case |
| 208 |
#' str(dend) |
| 209 |
#' attr(dend[[2]], "height") <- 0 |
| 210 |
#' count_terminal_nodes(dend) # 2 terminal nodes, why? see this plot: |
| 211 |
#' plot(dend, |
| 212 |
#' main = "This is considered a tree \n with TWO terminal nodes only" |
| 213 |
#' ) |
| 214 |
#' # while we have 3 leaves, in practice we have only 2 terminal nodes |
| 215 |
#' # (this is a feature, not a bug.) |
| 216 |
count_terminal_nodes <- function(dend_node, ...) { |
| 217 |
|
| 218 |
# a recursive function for counting the number of first encountered zero nodes |
| 219 |
# if you are a leaf, or a node with height 0, return 1 |
| 220 |
if (is.leaf(dend_node) | attr(dend_node, "height") == 0) { |
| 221 |
return(1L) |
| 222 |
} |
| 223 |
|
| 224 |
# else : # go through all of the childrens and apply count.first.zero.nodes |
| 225 |
terminal_nodes_counter <- 0 |
| 226 |
for (i in seq_len(length(dend_node))) { |
| 227 |
terminal_nodes_counter <- terminal_nodes_counter + # let each sub-node add its counts |
| 228 |
count_terminal_nodes(dend_node[[i]], ...) |
| 229 |
} |
| 230 |
return(terminal_nodes_counter) |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
#' @title unclass an entire dendrogram tree |
| 237 |
#' @description unclass all the nodes in a dendrogram tree. (Helps in cases when a dendrapply function was used wrongly) |
| 238 |
#' @export |
| 239 |
#' @param dend a dendrogram object |
| 240 |
#' @param ... not used |
| 241 |
#' @return The list which was the dendrogram (but without a class) |
| 242 |
#' @seealso \link{nleaves} |
| 243 |
#' @examples |
| 244 |
#' # define dendrogram object to play with: |
| 245 |
#' hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 246 |
#' dend <- as.dendrogram(hc) |
| 247 |
#' |
| 248 |
#' itself <- function(x) x |
| 249 |
#' dend <- dendrapply(dend, itself) |
| 250 |
#' unclass(dend) # this only returns a list with |
| 251 |
#' # two dendrogram objects inside it. |
| 252 |
#' str(dend) # this is a great way to show a dendrogram, |
| 253 |
#' # but it doesn't help us understand how the R object is built. |
| 254 |
#' str(unclass(dend)) # this is a great way to show a dendrogram, |
| 255 |
#' # but it doesn't help us understand how the R object is built. |
| 256 |
#' unclass_dend(dend) # this only returns a list |
| 257 |
#' # with two dendrogram objects inside it. |
| 258 |
#' str(unclass_dend(dend)) # NOW we can more easily understand |
| 259 |
#' # how the dendrogram object is structured... |
| 260 |
unclass_dend <- function(dend, ...) { |
| 261 |
# I could have also made this into a method |
| 262 |
# unclass_dendrogram |
| 263 |
# But decided not to, so to allow unclass to work as usual in case someone wants to use it only on one branch. |
| 264 |
# if(!is.leaf(dend)) |
| 265 |
# { |
| 266 |
# for(i in seq_len(length(dend))) |
| 267 |
# { |
| 268 |
# dend[[i]] <- unclass_dend(dend[[i]]) |
| 269 |
# } |
| 270 |
# } |
| 271 |
# dend <- unclass(dend) |
| 272 |
dend <- dendrapply(dend, unclass) |
| 273 |
return(dend) |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
# example(count_terminal_nodes) |
| 279 |
# example(labels_colors) |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
#' @export |
| 285 |
head.dendrogram <- function(x, n = 3L, ...) { |
| 286 |
# library(utils) |
| 287 |
utils::str(x, max.leve = n, ...) |
| 288 |
cat("etc...", "\n") |
| 289 |
} |
| 290 |
# hc <- hclust(dist(USArrests), "ave") |
| 291 |
# dend <- as.dendrogram(hc) |
| 292 |
# str(dend) |
| 293 |
# head(dend) |
| 294 |
# Some thoughts on imports: http://obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff/ |
| 295 |
# and: https://stackoverflow.com/questions/8637993/better-explanation-of-when-to-use-imports-depends |
| 296 |
|
| 297 |
# methods(nleaves) |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
# This is what should have been done for dendrograms |
| 303 |
# order.default <- order |
| 304 |
# order <- function(...) UseMethod("order") |