emmanuelparadis / ape

analysis of phylogenetics and evolution
http://ape-package.ird.fr/
GNU General Public License v2.0
52 stars 11 forks source link

Inheritance does not quite work with as.phylo #121

Closed lerouzic closed 1 month ago

lerouzic commented 1 month ago

Hello,

It seems that as.phylo() does not fall back to as.phylo.default for multiple class objects (below an example with chronos, ggtree calls as.phylo() on an object of class c("chronos","phylo"), and as.phylo crashes without calling as.phylo.default).

Note that the ggtree example was for the "real life" case, but this happens when simply calling as.phylo() in any context.

I suspect a problem with the S3 method export, but I have not checked.

R version 4.4.0 (2024-04-24), ape_5.7-1

library(ape)
x <- "(((Strix_aluco:4.2,Asio_otus:4.2):3.1,Athene_noctua:7.3):6.3,Tyto_alba:5);"
tree.owls <- read.tree(text= x)
c.tree.owls <- chronos(tree.owls, lambda=1)

ggtree::ggtree(tree.owls)
ggtree::ggtree(c.tree.owls)
# No method for as.phylo
# > traceback()
# 6: as.phylo(model)
# 5: fortify.phylo(data, ...)
# 4: fortify(data, ...)
# 3: ggplot.default [...]
# 2: ggplot [...]
# 1: ggtree::ggtree(c.tree.owls)

class(c.tree.owls) <- "phylo"
ggtree::ggtree(c.tree.owls)
emmanuelparadis commented 1 month ago

Hello @lerouzic,

Do you have an example without loading ggtree? Here's what I got:

R> library(ape)
R> x <- "(((Strix_aluco:4.2,Asio_otus:4.2):3.1,Athene_noctua:7.3):6.3,Tyto_alba:5);"
R> tree.owls <- read.tree(text= x)
R> c.tree.owls <- chronos(tree.owls, lambda=1, quiet=TRUE)
R> class(c.tree.owls)
[1] "chronos" "phylo"
R> class(as.phylo(c.tree.owls))
[1] "chronos" "phylo"

which is as expected. Maybe the issue comes after loading ggtree. Also the current version of ape on CRAN is 5.8.

Cheers,

Emmanuel

lerouzic commented 1 month ago

Also the current version of ape on CRAN is 5.8.

I could reproduce the bug without ggtree, but the problem has indeed vanished after an update of ape from 5.7 to 5.8.

Sorry for not noticing I was not running the last version of ape! I think the issue can be safely closed.