Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ouchtree - how many terminal nodes under each internal node? #8

Closed
soungalo opened this issue Apr 23, 2023 · 6 comments
Closed

ouchtree - how many terminal nodes under each internal node? #8

soungalo opened this issue Apr 23, 2023 · 6 comments
Assignees
Labels

Comments

@soungalo
Copy link

soungalo commented Apr 23, 2023

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?

@kingaa kingaa self-assigned this Apr 23, 2023
@kingaa
Copy link
Owner

kingaa commented Apr 23, 2023

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.

@mbutler808
Copy link

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

@mbutler808
Copy link

mbutler808 commented Apr 23, 2023

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)

@soungalo
Copy link
Author

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.

@mbutler808
Copy link

mbutler808 commented Apr 24, 2023 via email

@kingaa
Copy link
Owner

kingaa commented Apr 24, 2023

How about as(otree,"data.frame")?

@kingaa kingaa closed this as completed Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants