| 1 |
# library(testthat) |
| 2 |
|
| 3 |
context("Rotate a tree around its hinges") |
| 4 |
|
| 5 |
|
| 6 |
test_that("Rotate a dendrogram", { |
| 7 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 8 |
dend <- as.dendrogram(hc) |
| 9 |
|
| 10 |
# labels(dend) |
| 11 |
# rev(labels(dend)) |
| 12 |
|
| 13 |
expect_equal(rev(labels(dend)), labels(rotate(dend, 3:1))) |
| 14 |
expect_equal(rev(labels(dend)), labels(rotate(dend, rev(labels(dend))))) |
| 15 |
|
| 16 |
# test case where an object other than dendrogram/hclust/phylo is passed in |
| 17 |
x <- matrix(1:4, nrow = 2) |
| 18 |
expect_error(rotate(x)) |
| 19 |
|
| 20 |
# test case where order argument is missing |
| 21 |
expect_warning( |
| 22 |
rotated_dend <- rotate(dend) |
| 23 |
) |
| 24 |
expect_identical(rotated_dend, dend) |
| 25 |
|
| 26 |
# test case where not all leaves are specified in order and order is not numeric |
| 27 |
expect_error(rotate(dend, c("Alaska", "Alabama"))) |
| 28 |
}) |
| 29 |
|
| 30 |
|
| 31 |
test_that("Rotate a dendrogram (but not exactly to what we asked for)", { |
| 32 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 33 |
dend <- as.dendrogram(hc) |
| 34 |
|
| 35 |
new_order <- c(3, 1, 2) # an impossible order, will fail |
| 36 |
expect_false(all(labels(dend)[new_order] == labels(rotate(dend, new_order)))) |
| 37 |
expect_false(all(labels(dend)[new_order] == labels(rotate(dend, labels(dend)[new_order])))) |
| 38 |
# plot(rotate(dend, new_order)) |
| 39 |
# plot(dend) |
| 40 |
}) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
test_that("Rotate a dendrogram - works with either numeric or character", { |
| 45 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 46 |
dend <- as.dendrogram(hc) |
| 47 |
|
| 48 |
new_order <- c(2, 3, 1) # a possible order |
| 49 |
|
| 50 |
expect_equivalent(rotate(dend, new_order), rotate(dend, labels(dend)[new_order])) |
| 51 |
# plot(rotate(dend, new_order)) |
| 52 |
# plot(rotate(dend, labels(dend)[new_order]) ) |
| 53 |
# match(labels(dend)[new_order], labels(dend)) # NEW-OLD |
| 54 |
# new_order |
| 55 |
expect_equivalent( |
| 56 |
labels(rotate(dend, new_order)), |
| 57 |
labels(rotate(dend, labels(dend)[new_order])) |
| 58 |
) |
| 59 |
}) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
test_that("Rotate hclust", { |
| 68 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 69 |
|
| 70 |
# labels(hc) |
| 71 |
# rev(labels(hc)) |
| 72 |
# plot(hc) |
| 73 |
# plot(rotate(hc, 3:1)) |
| 74 |
|
| 75 |
expect_equal(rev(labels(hc)), labels(rotate(hc, 3:1))) |
| 76 |
expect_equal(rev(labels(hc)), labels(rotate(hc, rev(labels(hc))))) |
| 77 |
}) |
| 78 |
|
| 79 |
|
| 80 |
test_that("Rotate a hclust (but not exactly to what we asked for)", { |
| 81 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 82 |
|
| 83 |
new_order <- c(3, 1, 2) # an impossible order, will fail |
| 84 |
expect_false(all(labels(hc)[new_order] == labels(rotate(hc, new_order)))) |
| 85 |
expect_false(all(labels(hc)[new_order] == labels(rotate(hc, labels(hc)[new_order])))) |
| 86 |
# plot(rotate(hc, new_order)) |
| 87 |
# plot(hc) |
| 88 |
}) |
| 89 |
|
| 90 |
|
| 91 |
test_that("rotate.phylo works", { |
| 92 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 93 |
dend <- as.dendrogram(hc) |
| 94 |
|
| 95 |
x <- ape::as.phylo(dend) |
| 96 |
rotated_x <- rotate.phylo(x, c("Alaska", "Alabama")) |
| 97 |
|
| 98 |
dend_labels_reodered <- labels(dend) |
| 99 |
dend_labels_reodered[2:3] <- dend_labels_reodered[3:2] |
| 100 |
rotated_dend <- rotate(dend, order = dend_labels_reodered) |
| 101 |
# same rotation applied, so labels should be ordered the same |
| 102 |
expect_identical(labels(rotated_dend), labels(as.dendrogram(rotated_x))) |
| 103 |
|
| 104 |
# test case where phy argument is provided |
| 105 |
rotated_x <- rotate.phylo(NULL, c("Alaska", "Alabama"), phy = x) |
| 106 |
expect_identical(labels(rotated_dend), labels(as.dendrogram(rotated_x))) |
| 107 |
}) |
| 108 |
|
| 109 |
|
| 110 |
test_that("sort.dendrogram works", { |
| 111 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 112 |
dend <- as.dendrogram(hc) |
| 113 |
|
| 114 |
# sorting should switch order |
| 115 |
expect_identical(labels(dend), c("Arizona", "Alabama", "Alaska")) |
| 116 |
sorted_dend <- sort.dendrogram(dend, type = "nodes") |
| 117 |
expect_identical(labels(sorted_dend), c("Arizona", "Alaska", "Alabama")) |
| 118 |
}) |
| 119 |
|
| 120 |
|
| 121 |
test_that("sort.hclust works", { |
| 122 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 123 |
|
| 124 |
# sorting should switch order |
| 125 |
expect_identical(labels(hc), c("Arizona", "Alabama", "Alaska")) |
| 126 |
sorted_hc <- sort.hclust(hc, decreasing = T) |
| 127 |
expect_identical(labels(sorted_hc), c("Arizona", "Alaska", "Alabama")) |
| 128 |
}) |
| 129 |
|
| 130 |
|
| 131 |
test_that("sort.dendlist works", { |
| 132 |
hc <- hclust(dist(USArrests[1:5, ]), "ave") |
| 133 |
dend1 <- as.dendrogram(hc) |
| 134 |
hc <- hclust(dist(USArrests[6:10, ]), "ave") |
| 135 |
dend2 <- as.dendrogram(hc) |
| 136 |
dends <- dendlist(dend1, dend2) |
| 137 |
|
| 138 |
sorted_dends <- sort.dendlist(dends) |
| 139 |
# sorting should switch order |
| 140 |
expect_identical(labels(dend1), c("Arkansas", "Arizona", "California", "Alabama", "Alaska" )) |
| 141 |
expect_identical(labels(sorted_dends[[1]]), c("Alabama", "Alaska", "Arizona", "California", "Arkansas")) |
| 142 |
|
| 143 |
expect_identical(labels(dend2), c("Florida", "Connecticut", "Delaware", "Colorado", "Georgia")) |
| 144 |
expect_identical(labels(sorted_dends[[2]]), c("Connecticut", "Delaware", "Colorado", "Georgia", "Florida")) |
| 145 |
}) |
| 146 |
|
| 147 |
|
| 148 |
test_that("rev.hclust works", { |
| 149 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 150 |
|
| 151 |
# should reverse order |
| 152 |
ordered_labels <- c("Arizona", "Alabama", "Alaska") |
| 153 |
expect_identical(labels(hc), ordered_labels) |
| 154 |
reversed_hc <- rev.hclust(hc, decreasing = T) |
| 155 |
expect_identical(labels(reversed_hc), ordered_labels[3:1]) |
| 156 |
}) |
| 157 |
|
| 158 |
|
| 159 |
test_that("ladderize works", { |
| 160 |
# test case where an object other than dendrogram is passed in |
| 161 |
x <- matrix(1:4, nrow = 2) |
| 162 |
expect_error(ladderize(x)) |
| 163 |
|
| 164 |
# test for ladderize.phylo |
| 165 |
hc <- hclust(dist(USArrests[10:1, ]), "ave") |
| 166 |
dend <- as.dendrogram(hc) |
| 167 |
x <- ape::as.phylo(dend) |
| 168 |
ladderized_x <- ladderize(x, right = F) |
| 169 |
# order of edge should have changed |
| 170 |
expect_false(identical(ladderized_x$edge, x$edge)) |
| 171 |
|
| 172 |
# test when a dendrogram isn't passed in |
| 173 |
x <- matrix(1:4, nrow = 2) |
| 174 |
expect_error(ladderize.dendrogram(x)) |
| 175 |
|
| 176 |
# test when a phylo isn't passed in |
| 177 |
x <- ape::as.phylo(dend) |
| 178 |
ladderized_x <- ladderize.phylo(NULL, phy = x) |
| 179 |
expect_false(identical(ladderized_x$edge, x$edge)) |
| 180 |
}) |
| 181 |
|
| 182 |
|
| 183 |
test_that("click_rotate works", { |
| 184 |
# test case where an object other than dendrogram is passed in |
| 185 |
x <- matrix(1:4, nrow = 2) |
| 186 |
expect_error(click_rotate(x)) |
| 187 |
|
| 188 |
# test for ladderize.phylo |
| 189 |
hc <- hclust(dist(USArrests[10:1, ]), "ave") |
| 190 |
dend <- as.dendrogram(hc) |
| 191 |
|
| 192 |
# temporarily redefine interactive locator function to click leaf which causes no rotation in click_rotate and returns the same dendrogram |
| 193 |
capture.output(with_mocked_bindings( |
| 194 |
result <- click_rotate(dend, plot = TRUE, horiz = FALSE, continue = TRUE), |
| 195 |
locator = function(n = 1) list(x = 1, y = -11) |
| 196 |
)) |
| 197 |
expect_identical(labels(dend), labels(result)) |
| 198 |
|
| 199 |
}) |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|