Debian Codegraph @

R/distinct_edges.R

raw
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 # Re-coding phylo.diff from the distory package, but for dendrograms
21 # https://cran.r-project.org/package=distory
22
23
24
25
26 #' @title A list with labels for each subtree (edge)
27 #' @export
28 #' @description
29 #' Returns the set of all bipartitions from all edges, that is: a list with the labels
30 #' for each of the nodes in the dendrogram.
31 #' @param dend a dendrogram
32 #' @param ... Ignored.
33 #'
34 #' @seealso
35 #' \link{distinct_edges}, \link{highlight_distinct_edges},
36 #' \link{dist.dendlist}, \link{tanglegram},
37 #' \link[distory]{partition.leaves}
38 #'
39 #' @return
40 #' A list with the labels for each of the nodes in the dendrogram.
41 #'
42 #' @source
43 #' A \link{dendrogram} implementation for \link[distory]{partition.leaves} from the {distory} package
44 #'
45 #' @examples
46 #'
47 #' x <- 1:3 %>%
48 #' dist() %>%
49 #' hclust() %>%
50 #' as.dendrogram()
51 #' plot(x)
52 #' partition_leaves(x)
53 #' \dontrun{
54 #' set.seed(23235)
55 #' ss <- sample(1:150, 10)
56 #' dend1 <- iris[ss, -5] %>%
57 #' dist() %>%
58 #' hclust("com") %>%
59 #' as.dendrogram()
60 #' dend2 <- iris[ss, -5] %>%
61 #' dist() %>%
62 #' hclust("single") %>%
63 #' as.dendrogram()
64 #'
65 #' partition_leaves(dend1)
66 #' partition_leaves(dend2)
67 #' }
68 #'
69 partition_leaves <- function(dend, ...) {
70 if (!is.dendrogram(dend)) stop("'dend' is not a dendrogram")
71
72 nodes_labels <- vector("list", length = nnodes(dend))
73
74 i_counter <- 0
75 push_node_labels <- function(dend_node) {
76 i_counter <<- i_counter + 1
77
78 nodes_labels[[i_counter]] <<- labels(dend_node)
79 return(NULL)
80 }
81 dendrapply(dend, push_node_labels)
82
83 return(nodes_labels)
84 }
85
86
87
88
89
90 #' @title Finds distinct edges in one tree compared to another
91 #' @description Finds the edges present in the first tree but not in the second
92 #' @export
93 #'
94 #' @param dend a dendrogram to find unique edges in
95 #' @param dend2 a dendrogram to compare with
96 #' @param ... Ignored.
97 #'
98 #' @seealso
99 #' \link{distinct_edges}, \link{highlight_distinct_edges},
100 #' \link{dist.dendlist}, \link{tanglegram}
101 #' \link[distory]{distinct.edges}
102 #'
103 #' @return
104 #' A numeric vector of edge ids for the first tree (dend) that are not present in the second tree (dend2).
105 #'
106 #' @source
107 #' A \link{dendrogram} implementation for \link[distory]{distinct.edges} from the {distory} package
108 #'
109 #' @examples
110 #'
111 #' x <- 1:5 %>%
112 #' dist() %>%
113 #' hclust() %>%
114 #' as.dendrogram()
115 #' y <- set(x, "labels", 5:1)
116 #' distinct_edges(x, y)
117 #' distinct_edges(y, x)
118 #' dend_diff(x, y)
119 #' # tanglegram(x, y)
120 distinct_edges <- function(dend, dend2, ...) {
121 sort_a_character <- function(dend) dend %>%
122 as.character() %>%
123 sort()
124
125 bp1 <- partition_leaves(dend)
126 bp1 <- lapply(bp1, sort_a_character)
127 bp2 <- partition_leaves(dend2)
128 bp2 <- lapply(bp2, sort_a_character)
129 p <- c()
130 for (i in 1:length(bp1)) {
131 # if (!(list(bp1[[i]]) %in% bp2)) {
132 if (!(bp1[i] %in% bp2)) {
133 p <- append(p, i)
134 }
135 }
136 p
137 }
138
139
140
141
142 #' @title Highlight distint edges in a tree (compared to another one)
143 #' @rdname highlight_distinct_edges
144 #' @export
145 #'
146 #' @description
147 #' Highlight distint edges in a tree (compared to another one) by changing
148 #' the branches' color, line width, or line type.
149 #'
150 #' This function enables this feature in \link{dend_diff} and \link{tanglegram}
151 #'
152 #' @param dend a dendrogram or \link{dendlist} to find unique edges in (to highlight)
153 #' @param dend2 a dendrogram to compare with
154 #' @param value a new value scalar for the edgePar attribute.
155 #' @param edgePar a character indicating the value inside edgePar to adjust.
156 #' Can be either "col", "lty", or "lwd".
157 #' @param ... Ignored.
158 #' @param which an integer vector indicating, in the case "dend" is a dendlist,
159 #' on which of the trees should the modification be performed.
160 #' If missing - the change will be performed on all of objects in the dendlist.
161 #'
162 #' @seealso
163 #' \link{distinct_edges}, \link{highlight_distinct_edges},
164 #' \link{dist.dendlist}, \link{tanglegram}
165 #' \link{assign_values_to_branches_edgePar},
166 #' \link[distory]{distinct.edges},
167 #'
168 #' @return
169 #' A dendrogram with modified edges - the distinct ones are changed (color, line width, or line type)
170 #'
171 #' @examples
172 #'
173 #' x <- 1:5 %>%
174 #' dist() %>%
175 #' hclust() %>%
176 #' as.dendrogram()
177 #' y <- set(x, "labels", 5:1)
178 #' distinct_edges(x, y)
179 #' distinct_edges(y, x)
180 #'
181 #' par(mfrow = c(1, 2))
182 #' plot(highlight_distinct_edges(x, y))
183 #' plot(y)
184 #'
185 #' # tanglegram(highlight_distinct_edges(x, y),y)
186 #' # dend_diff(x, y)
187 #' \dontrun{
188 #'
189 #' # using highlight_distinct_edges combined with dendlist and set
190 #' # to clearly highlight "stable" branches.
191 #' data(iris)
192 #' ss <- c(1:5, 51:55, 101:105)
193 #' iris1 <- iris[ss, -5] %>%
194 #' dist() %>%
195 #' hclust(method = "single") %>%
196 #' as.dendrogram()
197 #' iris2 <- iris[ss, -5] %>%
198 #' dist() %>%
199 #' hclust(method = "complete") %>%
200 #' as.dendrogram()
201 #' iris12 <- dendlist(iris1, iris2) %>%
202 #' set("branches_k_color", k = 3) %>%
203 #' set("branches_lwd", 3) %>%
204 #' highlight_distinct_edges(value = 1, edgePar = "lwd")
205 #' iris12 %>%
206 #' untangle(method = "step2side") %>%
207 #' tanglegram(
208 #' sub = "Iris dataset", main_left = "'single' clustering",
209 #' main_right = "'complete' clustering"
210 #' )
211 #' }
212 #'
213 highlight_distinct_edges <- function(dend, ...) {
214 UseMethod("highlight_distinct_edges")
215 }
216
217
218 #' @export
219 #' @rdname highlight_distinct_edges
220 highlight_distinct_edges.dendrogram <- function(dend, dend2,
221 value = 2, edgePar = c("col", "lty", "lwd"), ...) {
222 edgePar <- match.arg(edgePar)
223
224 the_distinct_edges <- distinct_edges(dend, dend2)
225 dend_nnoded <- nnodes(dend)
226 new_value <- rep(Inf, dend_nnoded)
227 new_value[the_distinct_edges] <- value
228
229 new_dend <- assign_values_to_branches_edgePar(dend, value = new_value, edgePar = edgePar)
230
231 return(new_dend)
232 }
233
234
235 #' @export
236 #' @rdname highlight_distinct_edges
237 highlight_distinct_edges.dendlist <- function(dend, ..., which = c(1L, 2L)) {
238 l1 <- which[1]
239 l2 <- which[2]
240 dend[[l1]] <- highlight_distinct_edges(dend[[l1]], dend[[l2]], ...)
241 dend[[l2]] <- highlight_distinct_edges(dend[[l2]], dend[[l1]], ...)
242
243 dend
244 }
245
246
247
248
249 #' @title Plots two trees side by side, highlighting edges unique to each tree in red.
250 #' @rdname dend_diff
251 #' @export
252 #'
253 #' @description
254 #' Plots two trees side by side, highlighting edges unique to each tree in red.
255 #'
256 #' @param dend a dendrogram or \link{dendlist} to compre with
257 #' @param dend2 a dendrogram to compare with
258 #' @param horiz logical (TRUE) indicating if the dendrogram should be drawn horizontally or not.
259 #' @param ... passed to \link{plot.dendrogram}
260 #' @param which an integer vector indicating, in the case "dend" is a dendlist,
261 #' on which of the trees should the modification be performed.
262 #' If missing - the change will be performed on all of objects in the dendlist.
263 #'
264 #' @source
265 #' A \link{dendrogram} implementation for \link[distory]{phylo.diff} from the {distory} package
266 #'
267 #' @seealso
268 #' \link{distinct_edges}, \link{highlight_distinct_edges},
269 #' \link{dist.dendlist}, \link{tanglegram}
270 #' \link{assign_values_to_branches_edgePar},
271 #' \link[distory]{distinct.edges},
272 #'
273 #' @return
274 #' Invisible \link{dendlist} of both trees.
275 #'
276 #' @examples
277 #'
278 #' x <- 1:5 %>%
279 #' dist() %>%
280 #' hclust() %>%
281 #' as.dendrogram()
282 #' y <- set(x, "labels", 5:1)
283 #'
284 #' dend_diff(x, y)
285 #' dend_diff(dendlist(x, y))
286 #' dend_diff(dendlist(y, x))
287 #'
288 #' dend1 <- 1:10 %>%
289 #' dist() %>%
290 #' hclust() %>%
291 #' as.dendrogram()
292 #' dend2 <- dend1 %>% set("labels", c(1, 3, 2, 4, 5:10))
293 #' dend_diff(dend1, dend2)
294 dend_diff <- function(dend, ...) {
295 UseMethod("dend_diff")
296 }
297
298
299 #' @export
300 #' @rdname dend_diff
301 dend_diff.dendrogram <- function(dend, dend2, horiz = TRUE, ...) {
302 dend12 <- highlight_distinct_edges(dend, dend2)
303 dend22 <- highlight_distinct_edges(dend2, dend)
304
305 # change mfrow
306 op <- par()$mfrow
307 par(mfrow = c(1, 2))
308
309 plot(dend12, horiz = horiz, ...)
310 plot(dend22, horiz = horiz, ...)
311
312 # return mfrow to what it was before.
313 # op <- par()$mfrow
314 par(mfrow = op)
315
316 invisible(dendlist(dend, dend2))
317 }
318
319
320 #' @export
321 #' @rdname dend_diff
322 dend_diff.dendlist <- function(dend, ..., which = c(1L, 2L)) {
323 l1 <- which[1]
324 l2 <- which[2]
325 dend_diff(dend[[l1]], dend[[l2]], ...)
326
327 invisible(dend)
328 }
329
330
331
332
333
334
335
336
337 edgeset_dist <- function(dend, dend2, ...) {
338 length(distinct_edges(dend, dend2)) + length(distinct_edges(dend2, dend))
339 }
340
341
342
343 #' @title Topological Distances Between Two dendrograms
344 #' @export
345 #'
346 #' @description
347 #'
348 #' This function seems to bring different results than ape -
349 #' checking this out is still an open issue: \href{https://github.com/talgalili/dendextend/issues/97}{github issue}
350 #'
351 #' This function computes the \href{https://en.wikipedia.org/wiki/Robinson–Foulds_metric}{Robinson-Foulds distance}
352 #' (also known as symmetric difference)
353 #' between two dendrograms. This is the number of edges (branches) in tree_1
354 #' with a combination of labels that exist in
355 #' it but not in any subtree of tree2,
356 #' plus the same calculation of tree2 when compared to tree1.
357 #' This is the sum of length of \link{distinct_edges}(x,y) with
358 #' \link{distinct_edges}(y,x).
359 #'
360 #' This function might implement other topological distances in the future.
361 #'
362 #' @param dend a \link{dendlist}
363 #' @param method currently only 'edgeset' is implemented.
364 #' @param ... Ignored.
365 #'
366 #' @seealso
367 #' \link{distinct_edges},
368 #' \link[ape]{dist.topo},
369 #' \link[distory]{dist.multiPhylo},
370 #' \link[phangorn]{treedist},
371 #'
372 #' @return
373 #' A \link{dist} object with topological distances between all trees
374 #'
375 #' @examples
376 #'
377 #' x <- 1:5 %>%
378 #' dist() %>%
379 #' hclust() %>%
380 #' as.dendrogram()
381 #' y <- set(x, "labels", 5:1)
382 #'
383 #' dist.dendlist(dendlist(x1 = x, x2 = x, y1 = y))
384 #' dend_diff(x, y)
385 #'
386 #' # Larger trees
387 #' x <- 1:6 %>%
388 #' dist() %>%
389 #' hclust() %>%
390 #' as.dendrogram()
391 #' y <- set(x, "labels", c(1:3, 6, 4, 5))
392 #'
393 #' dend_diff(x, y)
394 #' dist.dendlist(dendlist(x, y))
395 #' distinct_edges(x, y)
396 #' distinct_edges(y, x)
397 #' length(distinct_edges(x, y)) + length(distinct_edges(y, x)) # dist.dendlist
398 dist.dendlist <- function(dend, method = c("edgeset"), ...) {
399 if (!is.dendlist(dend)) stop("dend needs to be a dendlist object")
400 method <- match.arg(method)
401
402 n_list <- length(dend)
403 the_dist <- matrix(0, n_list, n_list)
404 pairwise_combn <- combn(n_list, 2)
405
406 for (i in 1:ncol(pairwise_combn)) {
407 l1 <- pairwise_combn[1, i]
408 l2 <- pairwise_combn[2, i]
409 the_dist[l1, l2] <- the_dist[l2, l1] <- edgeset_dist(dend[[l1]], dend[[l2]])
410 }
411
412 rownames(the_dist) <- colnames(the_dist) <- names(dend)
413
414 as.dist(the_dist)
415 }