When creating a twin tree, one expects that the closest related taxa remain the closest related and vice versa for the most distant related.
Multiple tests confirm that create_twin_tree
does that perfectly well, I show only one here:
test_that("node distances should remain in the same order, 4 taxa, hard", {
tree <- ape::read.tree(text = "((A:2, (B:1, C:1):1):1, D:3);")
twin_tree <- create_twin_tree(tree)
n_tips <- ape::Ntip(tree)
expect_equal(
order(ape::dist.nodes(tree)[1:n_tips, 1:n_tips]),
order(ape::dist.nodes(twin_tree)[1:n_tips, 1:n_tips])
)
})
However, for a tree obtained from brute-forcing for errors:
test_that("node distances should remain in the same order, 4 taxa", {
skip("twin tree creation")
tree <- ape::read.tree(text = "(t2:1.9827033,((t4:0.2338486712,t3:0.2338486712):0.4930762889,t1:0.7269249601):1.25577834);") # nolint indeed this is a long line, but it is what the brute-force below generated
twin_tree <- create_twin_tree(tree)
n_tips <- ape::Ntip(tree)
expect_equal(
order(ape::dist.nodes(tree)[1:n_tips, 1:n_tips]),
order(ape::dist.nodes(twin_tree)[1:n_tips, 1:n_tips])
)
})
The trees do look correct:
Original tree:
Twin tree:
So, why does the test think something is wrong? Or: how to test correctly?
Goal of this Issue is to test that, when twinning. the closest related taxa remain the closest related in the correct way. The brute-force test below should always work. If the brute-force test tests correctly and passed, this Issue can be closed.
test_that("node distances should remain in the same order, brute-force", {
skip("twin tree creation")
if (!is_on_travis()) return()
# Or:
# - taxa that are closest, should remain closest in the twin tree
# - taxa that are farthest, should remain farthest in the twin tree
for (i in seq(1, 100)) {
set.seed(i)
tree <- beastier:::create_random_phylogeny(n_taxa = 4)
ape::write.tree(tree)
ape::plot.phylo(tree)
twin_tree <- create_twin_tree(tree)
n_tips <- ape::Ntip(tree)
# Only care about nodes that are tips
expect_equal(
order(ape::dist.nodes(tree)[1:n_tips, 1:n_tips]),
order(ape::dist.nodes(twin_tree)[1:n_tips, 1:n_tips]),
info = paste("seed:", i)
)
}
})