| 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 |
|
| 24 |
#' @title Trims one leaf from a dendrogram |
| 25 |
#' @description Trims (prunes) one leaf from a dendrogram. |
| 26 |
#' @export |
| 27 |
#' @param dend dendrogram object |
| 28 |
#' @param leaf_name a character string as the label of the tip we wish to prune |
| 29 |
#' @param ... passed on |
| 30 |
#' @details |
| 31 |
#' Used through \link{prune} |
| 32 |
#' @return A dendrogram with a leaf pruned |
| 33 |
#' @examples |
| 34 |
#' hc <- hclust(dist(USArrests[1:5, ]), "ave") |
| 35 |
#' dend <- as.dendrogram(hc) |
| 36 |
#' |
| 37 |
#' par(mfrow = c(1, 2)) |
| 38 |
#' plot(dend, main = "original tree") |
| 39 |
#' plot(prune_leaf(dend, "Alaska"), main = "tree without Alaska") |
| 40 |
prune_leaf <- function(dend, leaf_name, ...) { |
| 41 |
labels_dend <- labels(dend) |
| 42 |
|
| 43 |
if (length(labels_dend) != length(unique(labels_dend))) warning("Found duplicate labels in the tree (this might indicate a problem in the tree you supplied)") |
| 44 |
|
| 45 |
if (!(leaf_name %in% labels_dend)) { # what to do if there is no such leaf inside the tree |
| 46 |
warning(paste("There is no leaf with the label", leaf_name, "in the tree you supplied", "\n", "Returning original tree", "\n")) |
| 47 |
return(dend) |
| 48 |
} |
| 49 |
|
| 50 |
if (sum(labels_dend %in% leaf_name) > 1) { # what to do if there is no such leaf inside the tree |
| 51 |
warning(paste( |
| 52 |
"There are multiple leaves by the name of '", leaf_name, "' in the tree you supplied. Their locations is:", |
| 53 |
paste(which(labels_dend %in% leaf_name), collapse = ","), "\n", "Returning original tree", "\n" |
| 54 |
)) |
| 55 |
return(dend) |
| 56 |
} |
| 57 |
|
| 58 |
is.father.of.leaf.to.remove <- function(dend, leaf_name) { |
| 59 |
# this function checks if the leaf we wish to remove is the direct child of the current branch (dend) we entered the function |
| 60 |
is.father <- FALSE |
| 61 |
for (i in seq_len(length(dend))) |
| 62 |
{ |
| 63 |
if (is.leaf(dend[[i]]) == TRUE && labels(dend[[i]]) == leaf_name) is.father <- TRUE |
| 64 |
} |
| 65 |
return(is.father) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
remove_leaf_if_child <- function(dend, leaf_name) { |
| 70 |
# print(labels(dend)) |
| 71 |
if (all(labels(dend) != leaf_name)) { # if the leaf we want to remove is not in this branch, simply return the branch without going deeper into it. |
| 72 |
return(dend) |
| 73 |
} else { # but if the leaf we want to remove is here somewhere, go on searching |
| 74 |
attr(dend, "members") <- attr(dend, "members") - 1 |
| 75 |
|
| 76 |
if (!is.father.of.leaf.to.remove(dend, leaf_name)) # if you are not the father, then go on and make this function work on each child |
| 77 |
{ |
| 78 |
for (i in seq_len(length(dend))) |
| 79 |
{ |
| 80 |
dend[[i]] <- remove_leaf_if_child(dend[[i]], leaf_name) |
| 81 |
} |
| 82 |
} else { # we'll merge |
| 83 |
if (length(dend) == 2) { |
| 84 |
leaf_location <- 1 |
| 85 |
# if leaf location is 1, then move branch in leaf 2 to be the new x |
| 86 |
if (is.leaf(dend[[leaf_location]]) == T && labels(dend[[leaf_location]]) == leaf_name) { |
| 87 |
branch_to_bumpup <- 2 |
| 88 |
dend <- dend[[branch_to_bumpup]] |
| 89 |
} else { # else - the leaf location must be located in position "2" |
| 90 |
|
| 91 |
branch_to_bumpup <- 1 |
| 92 |
dend <- dend[[branch_to_bumpup]] |
| 93 |
} |
| 94 |
} else if (length(dend) > 2) { |
| 95 |
# If more than 2 branches, check if any are leaves |
| 96 |
dend_leaves <- unlist(lapply(dend, is.leaf)) |
| 97 |
dend_labels <- character(length = length(dend_leaves)) |
| 98 |
dend_labels[!dend_leaves] <- NA |
| 99 |
if (sum(dend_leaves) > 0) { |
| 100 |
# If so, check for matching labels to the leaf to prune |
| 101 |
dend_labels[dend_leaves] <- unlist(lapply(dend, function(x) attr(x, "label"))) |
| 102 |
dend_matches <- dend_labels == leaf_name |
| 103 |
dend_keep <- which(!(dend_leaves & dend_matches)) |
| 104 |
# Filter for only the non-matching members |
| 105 |
pruned <- dend[dend_keep] |
| 106 |
# Transfer attributes to the pruned list |
| 107 |
attributes(pruned) <- attributes(dend) |
| 108 |
# Adjust the "members" attribute of the dend. |
| 109 |
attr(pruned, "members") <- length(dend_keep) |
| 110 |
dend <- pruned |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
return(dend) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
new_dend <- remove_leaf_if_child(dend, leaf_name) |
| 120 |
new_dend <- suppressWarnings(stats_midcache.dendrogram(new_dend)) # fixes the attributes |
| 121 |
# new_x <- fix_members_attr.dendrogram(new_x) # fix the number of memebers attr for each node |
| 122 |
return(new_dend) |
| 123 |
} |
| 124 |
|
| 125 |
#' @title Prunes a tree (using leaves' labels) |
| 126 |
#' @rdname prune |
| 127 |
#' @export |
| 128 |
#' |
| 129 |
#' @description Trimms a tree (dendrogram, hclust) from a set of leaves based on their labels. |
| 130 |
#' |
| 131 |
#' @param dend tree object (dendrogram/hclust/phylo) |
| 132 |
#' @param leaves a character vector of the label(S) of the tip(s) (leaves) we wish to prune off the tree. |
| 133 |
#' @param reindex_dend logical (default is TRUE). If TRUE, the leaves of the new dendrograms |
| 134 |
#' include the rank of the old order.dendrogram. |
| 135 |
#' This insures that their values are just like the number of leaves. |
| 136 |
#' When FALSE, the values in the leaves is that of the original dendrogram. Thie is useful |
| 137 |
#' if prunning a dendrogram but then wanting to use \link{order.dendrogram} with the original values. |
| 138 |
#' When using prune.hclust, then reindex_dend is used by default since otherwise the \link{as.hclust} function |
| 139 |
#' would return an error. |
| 140 |
#' @param ... passed on |
| 141 |
#' @details |
| 142 |
#' I was not sure if to call this function drop.tip (from ape), snip/prune (from rpart) or just remove.leaves. I ended up deciding on prune. |
| 143 |
#' |
| 144 |
#' @return A pruned tree |
| 145 |
#' @seealso \link{prune_leaf}, \link[ape]{drop.tip} |
| 146 |
#' @examples |
| 147 |
#' hc <- hclust(dist(USArrests[1:5, ]), "ave") |
| 148 |
#' dend <- as.dendrogram(hc) |
| 149 |
#' |
| 150 |
#' par(mfrow = c(1, 2)) |
| 151 |
#' plot(dend, main = "original tree") |
| 152 |
#' plot(prune(dend, c("Alaska", "California")), main = "tree without Alaska and California") |
| 153 |
#' |
| 154 |
#' |
| 155 |
#' # this works because prune uses reindex_dend = TRUE by default |
| 156 |
#' as.hclust(prune(dend, c("Alaska", "California"))) |
| 157 |
#' prune(hc, c("Alaska", "California")) |
| 158 |
prune <- function(dend, ...) { |
| 159 |
UseMethod("prune") |
| 160 |
} |
| 161 |
|
| 162 |
#' @export |
| 163 |
#' @rdname prune |
| 164 |
prune.default <- function(dend, ...) { |
| 165 |
stop("object dend must be a dendrogram/hclust/phylo object") |
| 166 |
} |
| 167 |
|
| 168 |
# ' @S3method prune dendrogram |
| 169 |
#' @export |
| 170 |
#' @rdname prune |
| 171 |
prune.dendrogram <- function(dend, leaves, reindex_dend = TRUE, ...) { |
| 172 |
leaves <- as.character(leaves) |
| 173 |
|
| 174 |
for (i in seq_along(leaves)) |
| 175 |
{ |
| 176 |
# this function is probably not the fastest - but it works... |
| 177 |
dend <- prune_leaf(dend, leaves[i]) # move step by stem to remove all of these leaves... |
| 178 |
} |
| 179 |
|
| 180 |
if (reindex_dend) dend <- reindex_dend(dend) |
| 181 |
|
| 182 |
return(dend) |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
# ' @S3method prune hclust |
| 187 |
#' @export |
| 188 |
#' @rdname prune |
| 189 |
prune.hclust <- function(dend, leaves, ...) { |
| 190 |
x_dend <- as.dendrogram(dend) |
| 191 |
x_dend_pruned <- x_dend %>% |
| 192 |
prune(leaves, ...) %>% |
| 193 |
reindex_dend() |
| 194 |
x_pruned <- as_hclust_fixed(x_dend_pruned, dend) |
| 195 |
|
| 196 |
return(x_pruned) |
| 197 |
} |
| 198 |
|
| 199 |
# ' @S3method prune phylo |
| 200 |
#' @export |
| 201 |
#' @rdname prune |
| 202 |
prune.phylo <- function(dend, ...) { |
| 203 |
# library(ape) |
| 204 |
ape::drop.tip(phy = dend, ...) |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
#' @export |
| 209 |
#' @rdname prune |
| 210 |
prune.rpart <- function(dend, ...) { |
| 211 |
# library(ape) |
| 212 |
rpart::prune.rpart(tree = dend, ...) |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
#' Prune trees to their common subtrees |
| 219 |
#' |
| 220 |
#' @param dend a \link{dendlist} of length two |
| 221 |
#' @param ... ignored |
| 222 |
#' |
| 223 |
#' @return |
| 224 |
#' A dendlist after prunning the labels to only include |
| 225 |
#' those that are part of common subtrees in both dendrograms. |
| 226 |
#' |
| 227 |
#' @export |
| 228 |
#' @seealso \link{common_subtrees_clusters} |
| 229 |
#' |
| 230 |
#' @examples |
| 231 |
#' |
| 232 |
#' # NULL |
| 233 |
prune_common_subtrees.dendlist <- function(dend, ...) { |
| 234 |
if (!length(dend) == 2) stop("The dend must of be of length 2") |
| 235 |
if (!is.dendlist(dend)) stop("The dend must of be of class dendlist") |
| 236 |
|
| 237 |
# dend <- d_train_test |
| 238 |
clusters <- common_subtrees_clusters(dend[[1]], dend[[2]]) |
| 239 |
labels_to_prune <- labels(dend[[1]])[clusters == 0] |
| 240 |
dend1 <- prune(dend[[1]], labels_to_prune) |
| 241 |
dend2 <- prune(dend[[2]], labels_to_prune) |
| 242 |
dend_12 <- dendlist(dend1, dend2) |
| 243 |
names(dend_12) <- names(dend) |
| 244 |
dend_12 |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
#' @title Intersect trees |
| 254 |
#' @description |
| 255 |
#' Return two trees after pruning them so that the only leaves left are the intersection of their labels. |
| 256 |
#' @export |
| 257 |
#' @param dend1 tree object (dendrogram/hclust/phylo) |
| 258 |
#' @param dend2 tree object (dendrogram/hclust/phylo) |
| 259 |
#' @param warn logical (default from dendextend_options("warn") is FALSE). |
| 260 |
#' Set if warning are to be issued, it is safer to keep this at TRUE, |
| 261 |
#' but for keeping the noise down, the default is FALSE. |
| 262 |
#' Should a warning be issued if there was a need to perform intersaction. |
| 263 |
#' @param ... passed on |
| 264 |
#' @return A \link{dendlist} with two pruned trees |
| 265 |
#' @seealso \link{prune}, \link{intersect}, \link{labels} |
| 266 |
#' @examples |
| 267 |
#' hc <- hclust(dist(USArrests[1:5, ]), "ave") |
| 268 |
#' dend <- as.dendrogram(hc) |
| 269 |
#' labels(dend) <- 1:5 |
| 270 |
#' dend1 <- prune(dend, 1) |
| 271 |
#' dend2 <- prune(dend, 5) |
| 272 |
#' intersect_dend <- intersect_trees(dend1, dend2) |
| 273 |
#' |
| 274 |
#' layout(matrix(c(1, 1, 2, 3, 4, 5), 3, 2, byrow = TRUE)) |
| 275 |
#' plot(dend, main = "Original tree") |
| 276 |
#' plot(dend1, main = "Tree 1:\n original with label 1 pruned") |
| 277 |
#' plot(dend2, main = "Tree 2:\n original with label 2 pruned") |
| 278 |
#' plot(intersect_dend[[1]], |
| 279 |
#' main = "Tree 1 pruned |
| 280 |
#' with the labels that intersected with those of Tree 2" |
| 281 |
#' ) |
| 282 |
#' plot(intersect_dend[[2]], |
| 283 |
#' main = "Tree 2 pruned |
| 284 |
#' with the labels that intersected with those of Tree 1" |
| 285 |
#' ) |
| 286 |
intersect_trees <- function(dend1, dend2, warn = dendextend_options("warn"), ...) { |
| 287 |
labels_dend1 <- labels(dend1) |
| 288 |
labels_dend2 <- labels(dend2) |
| 289 |
intersected_labels <- intersect(labels_dend1, labels_dend2) |
| 290 |
|
| 291 |
if (length(intersected_labels) == 0) { |
| 292 |
warning("The two trees had no common labels!") |
| 293 |
return(dendlist()) |
| 294 |
} |
| 295 |
|
| 296 |
# prune tree 1 |
| 297 |
ss_labels_to_keep <- labels_dend1 %in% intersected_labels |
| 298 |
ss_labels_to_prune_1 <- !ss_labels_to_keep |
| 299 |
pruned_dend1 <- prune(dend1, labels_dend1[ss_labels_to_prune_1]) |
| 300 |
|
| 301 |
# prune tree 2 |
| 302 |
ss_labels_to_keep <- labels_dend2 %in% intersected_labels |
| 303 |
ss_labels_to_prune_2 <- !ss_labels_to_keep |
| 304 |
pruned_dend2 <- prune(dend2, labels_dend2[ss_labels_to_prune_2]) |
| 305 |
|
| 306 |
if (warn && any(c(ss_labels_to_prune_1, ss_labels_to_prune_2))) { |
| 307 |
warning("The labels in both tree had different values - trees were pruned.") |
| 308 |
} |
| 309 |
|
| 310 |
return(dendlist(pruned_dend1, pruned_dend2)) |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
#' @title Reindexing a pruned dendrogram |
| 316 |
#' @export |
| 317 |
#' |
| 318 |
#' @description \code{prune_leaf} does not update leaf indices as it prune |
| 319 |
#' leaves. As a result, some leaves of the pruned dendrogram may have leaf |
| 320 |
#' indeices larger than the number of leaves in the pruned dendrogram, which may |
| 321 |
#' cause errors in downstream functions such as \code{as.hclust}. |
| 322 |
#' |
| 323 |
#' This function re-indexes the leaves such that the leaf indices are no larger |
| 324 |
#' than the total number of leaves. |
| 325 |
#' |
| 326 |
#' @param dend dendrogram object |
| 327 |
#' |
| 328 |
#' @return A \code{dendrogram} object with the leaf reindexed |
| 329 |
#' |
| 330 |
#' |
| 331 |
#' @examples |
| 332 |
#' hc <- hclust(dist(USArrests[1:5, ]), "ave") |
| 333 |
#' dend <- as.dendrogram(hc) |
| 334 |
#' |
| 335 |
#' dend_pruned <- prune(dend, c("Alaska", "California"), reindex_dend = FALSE) |
| 336 |
#' |
| 337 |
#' ## A leave have an index larger than the number of leaves: |
| 338 |
#' unlist(dend_pruned) |
| 339 |
#' # [1] 4 3 1 |
| 340 |
#' #' |
| 341 |
#' dend_pruned_reindexed <- reindex_dend(dend_pruned) |
| 342 |
#' |
| 343 |
#' ## All leaf indices are no larger than the number of leaves: |
| 344 |
#' unlist(dend_pruned_reindexed) |
| 345 |
#' # [1] 3 2 1 |
| 346 |
#' |
| 347 |
#' ## The dendrograms are equal: |
| 348 |
#' all.equal(dend_pruned, dend_pruned_reindexed) |
| 349 |
#' # TRUE |
| 350 |
reindex_dend <- function(dend) { |
| 351 |
order.dendrogram(dend) <- dend %>% |
| 352 |
order.dendrogram() %>% |
| 353 |
rank() %>% |
| 354 |
as.integer() |
| 355 |
# as.integer(rank(order.dendrogram(dend))) |
| 356 |
return(dend) |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
# methods(prune) |
| 363 |
# example(rotate) |
| 364 |
# example(prune) |
| 365 |
# example(intersect_trees) |