Debian Codegraph @

R/all.equal.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
21
22
23 #' @title Global Comparison of two (or more) dendrograms
24 #' @exportS3Method all.equal dendrogram
25 #' @method all.equal dendrogram
26 #' @aliases
27 #' all.equal.dendlist
28 #' @description
29 #' This function makes a global comparison of two or more dendrograms trees.
30 #'
31 #' The function can get two \link{dendlist} objects and compare
32 #' them using \link{all.equal.list}. If a dendlist is in only "target"
33 #' (and not "current"), it will go through the dendlist and
34 #' compare all of the dendrograms within it to one another.
35 #'
36 #' @param target an object of type \link{dendrogram} or \link{dendlist}
37 #' @param current an object of type \link{dendrogram}
38 #' @param use.edge.length logical (TRUE). If to check branches' heights.
39 #' @param use.tip.label.order logical (FALSE). If to check labels are in the same and in identical order
40 #' @param use.tip.label logical (TRUE). If to check that labels are the same (regardless of order)
41 #' @param use.topology logical (TRUE). If to check teh existence of distinct edges
42 #' @param tolerance the numeric tolerance used to compare the branch lengths.
43 #' @param scale a positive number (NULL as default), comparison of branch height is made after scaling (i.e., dividing) them by this number.
44 #' @param ... Ignored.
45 #'
46 #' @seealso
47 #' \link{all.equal}, \link[ape]{all.equal.phylo}, \link{identical}
48 #' @return
49 #' Either TRUE (NULL for attr.all.equal) or a vector of mode "character" describing the differences
50 #' between target and current.
51 #'
52 #' @usage \method{all.equal}{dendrogram}(target, current,
53 #' use.edge.length = TRUE,
54 #' use.tip.label.order = FALSE,
55 #' use.tip.label = TRUE,
56 #' use.topology = TRUE,
57 #' tolerance = .Machine$double.eps^0.5,
58 #' scale = NULL, ...)
59 #'
60 #' \method{all.equal}{dendlist}(target, current, ...)
61 #'
62 #' @examples
63 #'
64 #' \dontrun{
65 #'
66 #' set.seed(23235)
67 #' ss <- sample(1:150, 10)
68 #' dend1 <- iris[ss, -5] %>%
69 #' dist() %>%
70 #' hclust("com") %>%
71 #' as.dendrogram()
72 #' dend2 <- iris[ss, -5] %>%
73 #' dist() %>%
74 #' hclust("single") %>%
75 #' as.dendrogram()
76 #' dend3 <- iris[ss, -5] %>%
77 #' dist() %>%
78 #' hclust("ave") %>%
79 #' as.dendrogram()
80 #' dend4 <- iris[ss, -5] %>%
81 #' dist() %>%
82 #' hclust("centroid") %>%
83 #' as.dendrogram()
84 #' # cutree(dend1)
85 #'
86 #' all.equal(dend1, dend1)
87 #' all.equal(dend1, dend2)
88 #' all.equal(dend1, dend2, use.edge.length = FALSE)
89 #' all.equal(dend1, dend2, use.edge.length = FALSE, use.topology = FALSE)
90 #'
91 #' all.equal(dend2, dend4, use.edge.length = TRUE)
92 #' all.equal(dend2, dend4, use.edge.length = FALSE)
93 #'
94 #' all.equal(dendlist(dend1, dend2, dend3, dend4))
95 #' all.equal(dendlist(dend1, dend2, dend3, dend4), use.edge.length = FALSE)
96 #' all.equal(dendlist(dend1, dend1, dend1))
97 #' }
98 all.equal.dendrogram <- function(target, current,
99 use.edge.length = TRUE,
100 use.tip.label.order = FALSE,
101 use.tip.label = TRUE,
102 use.topology = TRUE,
103 tolerance = .Machine$double.eps^0.5,
104 scale = NULL, ...) {
105
106 # are_they_equal <- NULL
107
108 # both should be dendrogram
109 if (!(is.dendrogram(target))) stop("target must be a dendrogram")
110 if (!(is.dendrogram(current))) {
111 return("current is not a dendrogram")
112 }
113
114
115 if (use.edge.length) {
116
117 # suppressWarnings is in order to deal with the case
118 # of a tree with just one item.
119 # If Rcpp is on, we would get
120 # In Rcpp_get_dend_heights(tree, branches_heights = TRUE, labels_heights = FALSE) :
121 # 'height' attr is missing from node, 0 is returned, please check your tree.
122
123 target_branches_heights <- suppressWarnings(
124 get_branches_heights(target)
125 )
126 current_branches_heights <- suppressWarnings(
127 get_branches_heights(current)
128 )
129
130 result <- all.equal(target_branches_heights,
131 current_branches_heights,
132 tolerance = tolerance, scale = scale
133 )
134 if (!is.logical(result)) {
135 return(paste("Difference in branch heights - ", result, collapse = ""))
136 }
137 }
138
139 if (use.tip.label.order) {
140 # all.equal(c("a","b"),c("b","a"))
141 # all.equal(c("a","b"),c(1))
142 result <- all.equal(labels(target), labels(current))
143 if (!is.logical(result)) {
144 return(paste("Difference in labels order - ", result, collapse = ""))
145 }
146 }
147
148 if (use.tip.label) {
149 # all.equal(c("a","b"),c("b","a"))
150 result <- all.equal(sort(labels(target)), sort(labels(current)))
151 if (!is.logical(result)) {
152 return(paste("Difference in labels - ", result, collapse = ""))
153 }
154 }
155
156 # Check topology:
157 if (use.topology) {
158 target_de <- distinct_edges(target, current)
159 current_de <- distinct_edges(current, target)
160 if (!(is.null(target_de) & is.null(current_de))) {
161 return(paste(
162 "Dendrograms contain diffreent edges (i.e.: topology). Unique edges in target:",
163 "|",
164 paste(target_de, collapse = ", "),
165 "|",
166 "Unique edges in current:", paste(current_de, collapse = ", ")
167 ))
168 }
169 }
170
171
172
173 # It means we couldn't find any difference that matters.
174 return(TRUE)
175 }
176
177
178
179
180
181
182
183
184
185
186 #' @export
187 #' @exportS3Method all.equal dendlist
188 #' @method all.equal dendlist
189 all.equal.dendlist <- function(target, current, ...) {
190 if (!is.dendlist(target)) stop("target needs to be a dendlist object")
191
192 # If we have only 1 item in the dendlist - then compare the internal items only
193 if (!missing(current)) {
194 return(base::all.equal.list(target, current))
195 }
196 # else - if we ONLY have targer, then we must want to
197 # compare all of the dendrograms inside it:
198
199 n_list <- length(target)
200
201 if (n_list < 2) {
202 warning("Did you really want to compare 'all.equal' with a dendlist which contains only 1 item? (with no 'current' parameter?!)")
203 return(TRUE)
204 }
205
206
207 are_equal <- vector("list", n_list)
208 pairwise_combn <- combn(n_list, 2)
209
210 for (i in 1:ncol(pairwise_combn)) {
211 l1 <- pairwise_combn[1, i]
212 l2 <- pairwise_combn[2, i]
213 are_equal[[i]] <- all.equal(target[[l1]], target[[l2]], ...)
214 }
215
216
217
218
219 if (all(sapply(are_equal, is.logical))) {
220 return(TRUE)
221 } else {
222 names(are_equal) <- apply(pairwise_combn, 2, paste, collapse = "==")
223 are_equal <- unlist(are_equal)
224 return(are_equal)
225 }
226 }