| 1 |
# library(testthat) |
| 2 |
# library(dendextend) |
| 3 |
|
| 4 |
context("Tanglegram") |
| 5 |
|
| 6 |
|
| 7 |
test_that("plot_horiz.dendrogram works", { |
| 8 |
suppressWarnings(RNGversion("3.5.0")) |
| 9 |
set.seed(1) |
| 10 |
|
| 11 |
hc <- USArrests[1:10, ] %>% |
| 12 |
dist() %>% |
| 13 |
hclust() |
| 14 |
dend <- as.dendrogram(hc) |
| 15 |
|
| 16 |
# covers cases where side = F |
| 17 |
expect_no_error( |
| 18 |
plot_horiz.dendrogram(dend, side = F) |
| 19 |
) |
| 20 |
|
| 21 |
# if horiz = F, should raise error |
| 22 |
expect_error( |
| 23 |
plot_horiz.dendrogram(dend, horiz = F) |
| 24 |
) |
| 25 |
# hclust object will be converted to dend, should work fine |
| 26 |
expect_no_error( |
| 27 |
plot_horiz.dendrogram(hc, side = T, edge.root = T, center = T) |
| 28 |
) |
| 29 |
|
| 30 |
# covers cases where text_pos is used but dleaf is not |
| 31 |
attr(dend, "edgetext") <- "t" |
| 32 |
capture.output(expect_no_error( |
| 33 |
plot_horiz.dendrogram(dend, side = T, edge.root = T, horiz = T, text_pos = 1) |
| 34 |
)) |
| 35 |
# covers cases where text_pos and dleaf used |
| 36 |
attr(dend, "edgetext") <- "t" |
| 37 |
capture.output(expect_no_error( |
| 38 |
plot_horiz.dendrogram(dend, side = T, edge.root = T, horiz = T, text_pos = 1, dLeaf = 1) |
| 39 |
)) |
| 40 |
# if a leaf is passed in |
| 41 |
expect_no_error( |
| 42 |
plot_horiz.dendrogram(dend[[1]], side = T, edge.root = T) |
| 43 |
) |
| 44 |
|
| 45 |
# create custom horiz class to bypass horiz = T requirement while still accessing lines that require horiz = F |
| 46 |
horiz_trick <<- structure(FALSE, class = "horiz_trick") |
| 47 |
`!.horiz_trick` <<- function(x) FALSE |
| 48 |
capture.output(expect_no_error( |
| 49 |
plot_horiz.dendrogram(dend, side = T, edge.root = T, horiz = horiz_trick) |
| 50 |
)) |
| 51 |
|
| 52 |
}) |
| 53 |
|
| 54 |
|
| 55 |
options(verbose = TRUE) |
| 56 |
test_that("plotNode_horiz verbose works", { |
| 57 |
|
| 58 |
hc <- USArrests[1:10, ] %>% |
| 59 |
dist() %>% |
| 60 |
hclust() |
| 61 |
dend <- as.dendrogram(hc) |
| 62 |
attr(dend[[1]], "edgetext") <- "t" |
| 63 |
# general case which covers most lines |
| 64 |
capture.output(expect_no_error( |
| 65 |
plotNode_horiz(1, 1, dend, center = T, nodePar = NULL, leaflab = "perpendicular") |
| 66 |
)) |
| 67 |
|
| 68 |
# specific cases to cover all lines |
| 69 |
capture.output(expect_no_error( |
| 70 |
plotNode_horiz(0.5, 1, dend, center = T, nodePar = 1, horiz = F, leaflab = "perpendicular", edgePar = list(), type = "rectangle", dLeaf = NULL) |
| 71 |
)) |
| 72 |
capture.output(expect_no_error( |
| 73 |
plotNode_horiz(0.5, 1, dend, center = T, nodePar = 1, leaflab = "textlike", edgePar = list(), type = "rectangle", dLeaf = NULL) |
| 74 |
)) |
| 75 |
|
| 76 |
# with type triangle |
| 77 |
capture.output(expect_no_error( |
| 78 |
plotNode_horiz(0.5, 1, dend, center = T, nodePar = 1, leaflab = "textlike", horiz = T, edgePar = list(), type = "triangle", dLeaf = NULL) |
| 79 |
)) |
| 80 |
capture.output(expect_no_error( |
| 81 |
plotNode_horiz(0.5, 1, dend, center = T, nodePar = 1, horiz = F, leaflab = "perpendicular", edgePar = list(), type = "triangle", dLeaf = NULL) |
| 82 |
)) |
| 83 |
|
| 84 |
# if a leaf is passed in |
| 85 |
leaf <- dend[[1]] |
| 86 |
attr(leaf, "label") <- 1 |
| 87 |
capture.output(expect_no_error( |
| 88 |
plotNode_horiz(0.5, 1, leaf, center = T, nodePar = 1, leaflab = "perpendicular", horiz = T, edgePar = list(), type = "triangle", dLeaf = NULL) |
| 89 |
)) |
| 90 |
# if ybot is null |
| 91 |
attr(dend[[1]], "height") <- NULL |
| 92 |
capture.output(expect_no_error( |
| 93 |
plotNode_horiz(0.5, 1, dend, center = T, nodePar = 1, leaflab = "textlike", horiz = T, edgePar = list(), type = "triangle", dLeaf = NULL) |
| 94 |
)) |
| 95 |
|
| 96 |
}) |
| 97 |
options(verbose = FALSE) |
| 98 |
|
| 99 |
|
| 100 |
test_that("tanglegram works", { |
| 101 |
suppressWarnings(RNGversion("3.5.0")) |
| 102 |
set.seed(23235) |
| 103 |
|
| 104 |
ss <- sample(1:150, 10) |
| 105 |
dend1 <- iris[ss, -5] %>% |
| 106 |
dist() %>% |
| 107 |
hclust("com") %>% |
| 108 |
as.dendrogram() |
| 109 |
dend2 <- iris[ss, -5] %>% |
| 110 |
dist() %>% |
| 111 |
hclust("sin") %>% |
| 112 |
as.dendrogram() |
| 113 |
dend12 <- dendlist(dend1, dend2) |
| 114 |
|
| 115 |
expect_no_error( |
| 116 |
tanglegram(dend12, common_subtrees_color_branches = TRUE) |
| 117 |
) |
| 118 |
|
| 119 |
# if non-dendrogram objects are passed in |
| 120 |
expect_no_error( |
| 121 |
tanglegram.hclust(as.hclust(dend1), as.hclust(dend2)) |
| 122 |
) |
| 123 |
expect_no_error( |
| 124 |
tanglegram.phylo(ape::as.phylo(dend1), ape::as.phylo(dend2)) |
| 125 |
) |
| 126 |
expect_error( |
| 127 |
tanglegram.default(1:4, 4:1) |
| 128 |
) |
| 129 |
|
| 130 |
# temporarily overwrite par() function to access error that is otherwise inaccessible |
| 131 |
expect_error(with_mocked_bindings( |
| 132 |
tanglegram.dendlist(dend12, just_one = F), |
| 133 |
par = function(mar = 0) c(1,1) |
| 134 |
)) |
| 135 |
# if only 1 dendrogram is passed in |
| 136 |
expect_error( |
| 137 |
tanglegram.dendlist(dend12[1]) |
| 138 |
) |
| 139 |
# if trying to plot trees that are not in the dendlist |
| 140 |
expect_error( |
| 141 |
tanglegram.dendlist(dend12, which = c(1,2,3)) |
| 142 |
) |
| 143 |
|
| 144 |
# if dendrograms with names are passed in |
| 145 |
names(dend12) <- c("dendrogram1", "dendrogram2") |
| 146 |
expect_no_error( |
| 147 |
tanglegram.dendlist(dend12) |
| 148 |
) |
| 149 |
|
| 150 |
# cover additional parameters |
| 151 |
expect_no_error( |
| 152 |
tanglegram.dendrogram(dend1, dend2, faster = T, remove_nodePar = T, sort = T, rank_branches = T, hang = T, |
| 153 |
lab.cex = 1, edge.lwd = 2, k_labels = 2, k_branches = 2) |
| 154 |
) |
| 155 |
expect_no_error( |
| 156 |
tanglegram.dendrogram(dend1, dend2, highlight_branches_col = T) |
| 157 |
) |
| 158 |
expect_no_error( |
| 159 |
tanglegram.dendrogram(dend1, dend2, dLeaf = 3) |
| 160 |
) |
| 161 |
|
| 162 |
}) |