| 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 |
# Creating the "dendlist" class for more easily dealing with tanglegrams and so on |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#' @title Checks if the value is and empty list() |
| 24 |
#' @description Checks if the value is and empty list(). Can be useful. |
| 25 |
#' @param x whatever object to check |
| 26 |
#' @return |
| 27 |
#' logical |
| 28 |
#' @examples |
| 29 |
#' # I can run this only if I'd make is_null_list exported |
| 30 |
#' \dontrun{ |
| 31 |
#' # TRUE: |
| 32 |
#' is_null_list(list()) |
| 33 |
#' # FALSE |
| 34 |
#' is_null_list(list(1)) |
| 35 |
#' is_null_list(1) |
| 36 |
#' |
| 37 |
#' x <- list(1, list(), 123) |
| 38 |
#' ss_list <- sapply(x, is_null_list) |
| 39 |
#' x <- x[!ss_list] |
| 40 |
#' x |
| 41 |
#' |
| 42 |
#' x <- list(1, list(), 123) |
| 43 |
#' ss_list <- sapply(x, is_null_list) |
| 44 |
#' x <- list(list()) |
| 45 |
#' x |
| 46 |
#' } |
| 47 |
#' |
| 48 |
#' \dontrun{ |
| 49 |
#' # error |
| 50 |
#' is_null_list() |
| 51 |
#' } |
| 52 |
is_null_list <- function(x) { |
| 53 |
identical(x, list()) |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
#' @title Creating a dendlist object from several dendrograms |
| 60 |
#' @rdname dendlist |
| 61 |
#' @export |
| 62 |
#' |
| 63 |
#' @description |
| 64 |
#' It accepts several dendrograms and or dendlist objects |
| 65 |
#' and chain them all together. |
| 66 |
#' This function aim to help with the usability of |
| 67 |
#' comparing two or more dendrograms. |
| 68 |
#' |
| 69 |
#' @param ... several dendrogram/hclust/phylo or dendlist objects |
| 70 |
#' If an object is hclust or phylo - it will be converted |
| 71 |
#' into a dendrogram. |
| 72 |
#' @param x a dendlist object |
| 73 |
#' @param which an integer vector of length 2, indicating |
| 74 |
#' which of the trees in the dendlist object should be plotted (relevant for dendlist) |
| 75 |
#' |
| 76 |
#' When used inside dendlist, which is still an integer, but it can be of any length, |
| 77 |
#' and it can be used to create a smaller dendlist. |
| 78 |
#' |
| 79 |
#' @details |
| 80 |
#' It there are list() in the ..., they are omitted. |
| 81 |
#' If ... is missing, it returns an empty dendlist. |
| 82 |
#' |
| 83 |
#' @return |
| 84 |
#' A list of class dendlist where each item |
| 85 |
#' is a dendrogram |
| 86 |
#' @examples |
| 87 |
#' |
| 88 |
#' \dontrun{ |
| 89 |
#' |
| 90 |
#' dend <- iris[, -5] %>% |
| 91 |
#' dist() %>% |
| 92 |
#' hclust() %>% |
| 93 |
#' as.dendrogram() |
| 94 |
#' dend2 <- iris[, -5] %>% |
| 95 |
#' dist() %>% |
| 96 |
#' hclust(method = "single") %>% |
| 97 |
#' as.dendrogram() |
| 98 |
#' dendlist(1:4, 5, a = dend) # Error |
| 99 |
#' # dendlist <- function (...) list(...) |
| 100 |
#' dendlist(dend) |
| 101 |
#' dendlist(dend, dend) |
| 102 |
#' dendlist(dend, dend, dendlist(dend)) |
| 103 |
#' # notice how the order of |
| 104 |
#' dendlist(dend, dend2) |
| 105 |
#' dendlist(dend) %>% dendlist(dend2) |
| 106 |
#' dendlist(dend) %>% |
| 107 |
#' dendlist(dend2) %>% |
| 108 |
#' dendlist(dend) |
| 109 |
#' dendlist(dend, dend2) %>% tanglegram() |
| 110 |
#' tanglegram(tree1 = dendlist(dend, dend2)) |
| 111 |
#' |
| 112 |
#' dend <- iris[1:20, -5] %>% |
| 113 |
#' dist() %>% |
| 114 |
#' hclust() %>% |
| 115 |
#' as.dendrogram() |
| 116 |
#' dend2 <- iris[1:20, -5] %>% |
| 117 |
#' dist() %>% |
| 118 |
#' hclust(method = "single") %>% |
| 119 |
#' as.dendrogram() |
| 120 |
#' |
| 121 |
#' x <- dendlist(dend, dend2) |
| 122 |
#' plot(x) |
| 123 |
#' } |
| 124 |
#' |
| 125 |
dendlist <- function(..., which) { |
| 126 |
x <- list(...) |
| 127 |
|
| 128 |
if (!missing(which)) { |
| 129 |
the_names <- names(x[[1]])[which] |
| 130 |
} |
| 131 |
|
| 132 |
# dendlist on a list will return a length 0 dendlist |
| 133 |
# First, let's remove all "list()" from the object |
| 134 |
ss_list <- sapply(x, is_null_list) |
| 135 |
x <- x[!ss_list] |
| 136 |
# if we are left with nothing, return an empty dendlist |
| 137 |
if (identical(x, list())) { |
| 138 |
# warning("You entered an empty (or no) list to dendlist, a dendlist of length 0 is created.") |
| 139 |
x_final <- list() |
| 140 |
class(x_final) <- "dendlist" |
| 141 |
return(x_final) |
| 142 |
} |
| 143 |
|
| 144 |
# object <- as.list(substitute(x))[-1L] |
| 145 |
n <- length(x) |
| 146 |
|
| 147 |
# If we have objects which are not dend or dendlist - STOP! |
| 148 |
x_classes <- sapply(x, class) |
| 149 |
|
| 150 |
if (!all(x_classes %in% c("dendrogram", "hclust", "phylo", "dendlist"))) { |
| 151 |
print_x_classes <- paste(x_classes, collapse = ", ") |
| 152 |
stop( |
| 153 |
paste( |
| 154 |
"Some of your object's classes are not of the type dendrogram/hclust/phylo/dendlist. Please review and fix. Their classes are:\n", |
| 155 |
print_x_classes |
| 156 |
) |
| 157 |
) |
| 158 |
} |
| 159 |
|
| 160 |
# if some objects are hclust/phylo - then turn them into dend: |
| 161 |
x_classes_hclust_phylo <- x_classes %in% c("hclust", "phylo") |
| 162 |
if (any(x_classes_hclust_phylo)) { |
| 163 |
for (i in seq_len(n)) { |
| 164 |
if (x_classes_hclust_phylo[i]) { |
| 165 |
x[[i]] <- as.dendrogram(x[[i]]) |
| 166 |
x_classes[[i]] <- "dendrogram" |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
# If all objects are dend - just list them, and return |
| 172 |
x_classes_dend <- x_classes == "dendrogram" |
| 173 |
if (all(x_classes_dend)) { |
| 174 |
# skip... |
| 175 |
x_final <- x |
| 176 |
} else { |
| 177 |
n_final <- sum(x_classes == "dendrogram") + |
| 178 |
x[x_classes == "dendlist"] %>% |
| 179 |
sapply(length) %>% |
| 180 |
sum() |
| 181 |
x_final <- vector("list", n_final) |
| 182 |
|
| 183 |
i_counter <- 1 |
| 184 |
for (i in seq_len(n)) { |
| 185 |
xi <- x[[i]] |
| 186 |
|
| 187 |
if (is.dendrogram(xi)) { |
| 188 |
x_final[[i_counter]] <- xi |
| 189 |
i_counter <- i_counter + 1 |
| 190 |
} else { |
| 191 |
for (j in seq_len(length(xi))) { |
| 192 |
x_final[[i_counter]] <- xi[[j]] |
| 193 |
i_counter <- i_counter + 1 |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
# else - everything is either c("dendrogram", "dendlist")) |
| 201 |
# let's make it |
| 202 |
|
| 203 |
if (!missing(which)) { |
| 204 |
x_final <- x_final[which] |
| 205 |
names(x_final) <- the_names |
| 206 |
} |
| 207 |
|
| 208 |
class(x_final) <- "dendlist" |
| 209 |
return(x_final) |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
# @title A plot S3 method for dendlist |
| 215 |
# @export |
| 216 |
# @description |
| 217 |
# If the dendlist if of length 1 - then use a normal plotting method. |
| 218 |
# If it is of length 2 or more, use a tangelgram. |
| 219 |
# @param x a dendlist |
| 220 |
# @param which an integer vector of length 2, indicating |
| 221 |
# which of the trees in the dendlist object should be plotted |
| 222 |
# @param ... parameters to be passed to the plot/tanglegram function |
| 223 |
# @return |
| 224 |
# A dendlist object |
| 225 |
# @examples |
| 226 |
# |
| 227 |
# \dontrun{ |
| 228 |
# |
| 229 |
# dend <- iris[1:20, -5] %>% dist %>% hclust %>% as.dendrogram |
| 230 |
# dend2 <- iris[1:20, -5] %>% dist %>% hclust(method = "single") %>% as.dendrogram |
| 231 |
# |
| 232 |
# x <- dendlist(dend, dend2) |
| 233 |
# plot(x) |
| 234 |
# |
| 235 |
# } |
| 236 |
# |
| 237 |
|
| 238 |
|
| 239 |
#' @export |
| 240 |
#' @rdname dendlist |
| 241 |
plot.dendlist <- function(x, which = c(1L, 2L), ...) { |
| 242 |
if (!is.dendlist(x)) stop("x is not a dendlist") |
| 243 |
|
| 244 |
x_length <- length(x) |
| 245 |
if (x_length == 1) { |
| 246 |
plot(x[[1]], ...) |
| 247 |
return(x) |
| 248 |
} |
| 249 |
if (x_length >= 2) tanglegram(x[[which[1]]], x[[which[2]]], ...) |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
#' @title Try to coerce something into a dendlist |
| 255 |
#' @export |
| 256 |
#' @description |
| 257 |
#' It removes stuff that are not dendgrogram/dendlist |
| 258 |
#' and turns what is left into a dendlist |
| 259 |
#' @param x a list with several dendrogram/hclust/phylo or dendlist objects |
| 260 |
#' and other junk that should be omitted. |
| 261 |
#' @param ... NOT USED |
| 262 |
#' @return |
| 263 |
#' A list of class dendlist where each item |
| 264 |
#' is a dendrogram |
| 265 |
#' @examples |
| 266 |
#' |
| 267 |
#' \dontrun{ |
| 268 |
#' |
| 269 |
#' dend <- iris[, -5] %>% |
| 270 |
#' dist() %>% |
| 271 |
#' hclust() %>% |
| 272 |
#' as.dendrogram() |
| 273 |
#' dend2 <- iris[, -5] %>% |
| 274 |
#' dist() %>% |
| 275 |
#' hclust(method = "single") %>% |
| 276 |
#' as.dendrogram() |
| 277 |
#' |
| 278 |
#' x <- list(dend, 1, dend2) |
| 279 |
#' as.dendlist(x) |
| 280 |
#' } |
| 281 |
#' |
| 282 |
as.dendlist <- function(x, ...) { |
| 283 |
# x <- list(...) |
| 284 |
# If we have objects which are not dend or dendlist - STOP! |
| 285 |
x_classes <- sapply(x, class) |
| 286 |
ss <- x_classes %in% c("dendrogram", "hclust", "phylo", "dendlist") |
| 287 |
# keep only the parts which are relevant |
| 288 |
x <- x[ss] |
| 289 |
x_final <- list() |
| 290 |
for (i in seq_len(length(x))) { |
| 291 |
x_final <- dendlist(x_final, x[[i]]) |
| 292 |
} |
| 293 |
|
| 294 |
return(x_final) |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#' @export |
| 302 |
head.dendlist <- function(x, ...) { |
| 303 |
for (i in seq_len(length(x))) { |
| 304 |
cat("============\n", "dend ", i, "\n", "---------\n") |
| 305 |
head(x[[i]], ...) |
| 306 |
} |
| 307 |
} |