emmanuelparadis / ape

Analysis of Phylogenetics and Evolution
https://emmanuelparadis.github.io/
GNU General Public License v2.0
53 stars 11 forks source link

fix the edge.length or height when converting between the hclust and … #59

Closed xiangpin closed 2 years ago

xiangpin commented 2 years ago

The edge.length of as.phylo.hclust method might be incorrect.

The original

library(ape)
set.seed(123)
dd <- matrix(abs(rnorm(100)), 10)
xx <- hclust(as.dist(dd))
tr <- as.phylo(xx)
plot(xx, hang = -1)

x1

plot(tr)
axisPhylo()

x2

The axis of phylo converted from hclust is different with the original hclust.

This request

library(ape)
set.seed(123)
dd <- matrix(abs(rnorm(100)), 10)
xx <- hclust(as.dist(dd))
tr <- as.phylo(xx)
plot(tr)
axisPhylo()

x3 The height of as.hclust.phylo also was adjusted according to the edge.lenght of as.phylo.hclust

yy <- as.hclust(tr)
aplot::plot_list(~plot(xx, hang = -1), ~plot(yy, hang = -1))

x4

emmanuelparadis commented 2 years ago

Hi, This is on purpose: the division by 2 was added in ape 2.5-2 (see NEWS file). The motivation is that the heights of the "hclust" object (see phangorn::upgma) are in units of evolutionary divergence between the pairs of taxa/tips, thus equivalent to the sum of branch lengths between the same pairs. Actually, the correct comparison should be with ape::cophenetic.phylo. Best, Emmanuel

xiangpin commented 2 years ago

Thank you very much. I got it now. But it is confusing when the results (hclust and as.phylo.hclust) are visualized and compared.