File: all.equal.R

package info (click to toggle)
r-cran-dendextend 1.19.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,076 kB
  • sloc: sh: 13; makefile: 2
file content (226 lines) | stat: -rw-r--r-- 7,185 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright (C) Tal Galili
#
# This file is part of dendextend.
#
# dendextend is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dendextend is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#  A copy of the GNU General Public License is available at
#  http://www.r-project.org/Licenses/
#





#' @title Global Comparison of two (or more) dendrograms
#' @exportS3Method all.equal dendrogram
#' @method all.equal dendrogram
#' @aliases
#' all.equal.dendlist
#' @description
#' This function makes a global comparison of two or more dendrograms trees.
#'
#' The function can get two \link{dendlist} objects and compare
#' them using \link{all.equal.list}. If a dendlist is in only "target"
#' (and not "current"), it will go through the dendlist and
#' compare all of the dendrograms within it to one another.
#'
#' @param target an object of type \link{dendrogram} or \link{dendlist}
#' @param current an object of type \link{dendrogram}
#' @param use.edge.length logical (TRUE). If to check branches' heights.
#' @param use.tip.label.order logical (FALSE). If to check labels are in the same and in identical order
#' @param use.tip.label logical (TRUE). If to check that labels are the same (regardless of order)
#' @param use.topology logical (TRUE). If to check teh existence of distinct edges
#' @param tolerance the numeric tolerance used to compare the branch lengths.
#' @param scale a positive number (NULL as default), comparison of branch height is made after scaling (i.e., dividing) them by this number.
#' @param ... Ignored.
#'
#' @seealso
#' \link{all.equal}, \link[ape]{all.equal.phylo}, \link{identical}
#' @return
#' Either TRUE (NULL for attr.all.equal) or a vector of mode "character" describing the differences
#' between target and current.
#'
#' @usage \method{all.equal}{dendrogram}(target, current,
#'                                      use.edge.length = TRUE,
#'                                      use.tip.label.order = FALSE,
#'                                      use.tip.label = TRUE,
#'                                      use.topology = TRUE,
#'                                      tolerance = .Machine$double.eps^0.5,
#'                                      scale = NULL, ...)
#'
#' \method{all.equal}{dendlist}(target, current, ...)
#'
#' @examples
#'
#' \dontrun{
#'
#' set.seed(23235)
#' ss <- sample(1:150, 10)
#' dend1 <- iris[ss, -5] %>%
#'   dist() %>%
#'   hclust("com") %>%
#'   as.dendrogram()
#' dend2 <- iris[ss, -5] %>%
#'   dist() %>%
#'   hclust("single") %>%
#'   as.dendrogram()
#' dend3 <- iris[ss, -5] %>%
#'   dist() %>%
#'   hclust("ave") %>%
#'   as.dendrogram()
#' dend4 <- iris[ss, -5] %>%
#'   dist() %>%
#'   hclust("centroid") %>%
#'   as.dendrogram()
#' #    cutree(dend1)
#'
#' all.equal(dend1, dend1)
#' all.equal(dend1, dend2)
#' all.equal(dend1, dend2, use.edge.length = FALSE)
#' all.equal(dend1, dend2, use.edge.length = FALSE, use.topology = FALSE)
#'
#' all.equal(dend2, dend4, use.edge.length = TRUE)
#' all.equal(dend2, dend4, use.edge.length = FALSE)
#'
#' all.equal(dendlist(dend1, dend2, dend3, dend4))
#' all.equal(dendlist(dend1, dend2, dend3, dend4), use.edge.length = FALSE)
#' all.equal(dendlist(dend1, dend1, dend1))
#' }
all.equal.dendrogram <- function(target, current,
                                 use.edge.length = TRUE,
                                 use.tip.label.order = FALSE,
                                 use.tip.label = TRUE,
                                 use.topology = TRUE,
                                 tolerance = .Machine$double.eps^0.5,
                                 scale = NULL, ...) {

  #    are_they_equal <- NULL

  # both should be dendrogram
  if (!(is.dendrogram(target))) stop("target must be a dendrogram")
  if (!(is.dendrogram(current))) {
    return("current is not a dendrogram")
  }


  if (use.edge.length) {

    #       suppressWarnings is in order to deal with the case
    # of a tree with just one item.
    # If Rcpp is on, we would get
    #  In Rcpp_get_dend_heights(tree, branches_heights = TRUE, labels_heights = FALSE) :
    #       'height' attr is missing from node, 0 is returned, please check your tree.

    target_branches_heights <- suppressWarnings(
      get_branches_heights(target)
    )
    current_branches_heights <- suppressWarnings(
      get_branches_heights(current)
    )

    result <- all.equal(target_branches_heights,
      current_branches_heights,
      tolerance = tolerance, scale = scale
    )
    if (!is.logical(result)) {
      return(paste("Difference in branch heights - ", result, collapse = ""))
    }
  }

  if (use.tip.label.order) {
    #       all.equal(c("a","b"),c("b","a"))
    #       all.equal(c("a","b"),c(1))
    result <- all.equal(labels(target), labels(current))
    if (!is.logical(result)) {
      return(paste("Difference in labels order - ", result, collapse = ""))
    }
  }

  if (use.tip.label) {
    #       all.equal(c("a","b"),c("b","a"))
    result <- all.equal(sort(labels(target)), sort(labels(current)))
    if (!is.logical(result)) {
      return(paste("Difference in labels - ", result, collapse = ""))
    }
  }

  # Check topology:
  if (use.topology) {
    target_de <- distinct_edges(target, current)
    current_de <- distinct_edges(current, target)
    if (!(is.null(target_de) & is.null(current_de))) {
      return(paste(
        "Dendrograms contain diffreent edges (i.e.: topology). Unique edges in target:",
        "|",
        paste(target_de, collapse = ", "),
        "|",
        "Unique edges in current:", paste(current_de, collapse = ", ")
      ))
    }
  }



  # It means we couldn't find any difference that matters.
  return(TRUE)
}










#' @export
#' @exportS3Method all.equal dendlist
#' @method all.equal dendlist
all.equal.dendlist <- function(target, current, ...) {
  if (!is.dendlist(target)) stop("target needs to be a dendlist object")

  # If we have only 1 item in the dendlist - then compare the internal items only
  if (!missing(current)) {
    return(base::all.equal.list(target, current))
  }
  # else - if we ONLY have targer, then we must want to
  # compare all of the dendrograms inside it:

  n_list <- length(target)

  if (n_list < 2) {
    warning("Did you really want to compare 'all.equal' with a dendlist which contains only 1 item? (with no 'current' parameter?!)")
    return(TRUE)
  }


  are_equal <- vector("list", n_list)
  pairwise_combn <- combn(n_list, 2)

  for (i in 1:ncol(pairwise_combn)) {
    l1 <- pairwise_combn[1, i]
    l2 <- pairwise_combn[2, i]
    are_equal[[i]] <- all.equal(target[[l1]], target[[l2]], ...)
  }




  if (all(sapply(are_equal, is.logical))) {
    return(TRUE)
  } else {
    names(are_equal) <- apply(pairwise_combn, 2, paste, collapse = "==")
    are_equal <- unlist(are_equal)
    return(are_equal)
  }
}