ouchtree - how many terminal nodes under each internal node?
soungalo opened this issue · 6 comments
Could you please provide some guidelines on working with ouchtree objects?
I'm trying to loop on all internal nodes and determine the number of terminal nodes under each. How can this be achieved?
The ouchtree
format is designed for internal use and not with a view to outside examination. That is, it is designed solely to make the internal ouch algorithms efficient. My advice would be to convert an ouchtree
into a data frame using as.data.frame()
and then to perform analysis on that.
The resulting data frame is pretty self-explanatory. Each node (internal or terminal) gets a line. That line contains the following information: the name of the node, the name of its immediate ancestor, its time (the positive direction being the future), and an optional label.
Aloha @soungalo As Aaron says ouchtree does not have accessor functions for returning descendants, etc. But if you want to see the internal components, you can use the attributes
function:
require(ouch)
x <- with(
bimac,
ouchtree(nodes=node,times=time/max(time),ancestors=ancestor,labels=species)
)
attributes(x)
attributes(x)$nterm
and access the elements by name (or other standard methods) on the return object of attributes().
However, there are other packages that have treewalking functions such as tidytree
and ape
. Lately Iʻve been using tidytree
(to work with the treedata object) and treeio
(to read in the phylogenetic tree in many formats into treedata), which has functions for ancestors, descendants, MRCA, etc. https://yulab-smu.top/treedata-book/chapter2.html#accesor-tidytree
Marguerite
I an not sure what youʻre doing, but if you want to get it your tree back into ouchtree
after editing it, you can get it into phylo
format and then use ape2ouch()
require(treeio)
require(tidytree)
require(ouch)
td_tree <- read.newick("mytree.tree") # or read.iqtree, read.nexus, read.beast, etc.
# ... some tree manipulations ...
phylo_tree <- td_tree %>% as.phylo
ouch_tree <- ape2ouch(phylo_tree)
Thank you both!
@kingaa - for some reason I couldn't convert the ouch tree to a data frame:
> as.data.frame(otree)
Error in as.data.frame.default(otree): cannot coerce class ‘structure("ouchtree", package = "ouch")’ to a data.frame
Traceback:
1. as.data.frame(otree)
2. as.data.frame.default(otree)
3. stop(gettextf("cannot coerce class %s to a data.frame", sQuote(deparse(class(x))[1L])),
. domain = NA)
I should have probably mentioned that the ouch tree was created using the convertTreeData function from SURFACE, so there might be something weird with the object I'm working with. In any case, it sounds like the easy way would be to go back to the original tree (phylo class) and work with it.
How about as(otree,"data.frame")
?