Question: R:Root {Ape} Problem With Rerooting Ultrametric Trees
gravatar for Alice
7.9 years ago by
Alice300 wrote:

Hello, biostars! I have some problems with miltyPhylo object manipulation and can't understand how to solve it.

I have many trees from MrBayes analysis and want to use it for comparative phylogenetics methods. Trees are ultrametric and UNrooted (also can't understand why, in MrBayes my outgroup sequence was the first in the matrix). Than I did following steps: 1) outgroup deletion (species from other group, which I don't want to include in analysis).

>lapply (ntrees [1], is.ultrametric)
$`    TREE * gen.6720000`
[1] TRUE
>lapply (ntrees [1], is.rooted)
$`    TREE * gen.6720000`
[1] TRUE

Than I looked at one of the trees, it was unrooted.

2) To root trees for the following analysis

>rtrees<-lapply (ntrees, root, "new outgroup")
> lapply (rtrees [1], is.rooted)
$`    TREE * gen.6720000`

> lapply (rtrees [1], is.ultrametric)
$`    TREE * gen.6720000`

I'm new in phylogenetic comparative methods and in R project. So I just can't find a mistake.

phylogeny R • 5.2k views
ADD COMMENTlink modified 5.8 years ago by Biostar ♦♦ 20 • written 7.9 years ago by Alice300
gravatar for David W
7.9 years ago by
David W4.8k
New Zealand
David W4.8k wrote:

Hi Alice,

A couple of things here.

1) the apply family functions are for applying a given function to every member of a list/matrix/vector etc. But you've sliced out the first part of the multiPhyloobject (basically a list). So you are only applying the function to the first part. On top of that, there are two ways to slice lists in R, single or double square brackets. Using single brackets will return a list (with the slice indices you give it), double brackets the particular member of this list you are denoting. (see ?Extract to get your head around the distinction). The has some unexpected results:

trees <- replicate(10, rtree(10), simplify=FALSE)
plot(trees[[1]]) #a tree, works fine
plot(trees[1]) # a list, R doesn't know how to plot it 
plot(trees[1][[1]])# the first element of the list is a tree, plots fine

In your example you are applylng the is.rooted and is.ultrametric functions to lists not trees, and that may have similar unexpected results.

To check all trees in a tree object are rooted just drop the slice:

lapply(ntrees, is.rooted) #screeds of TRUEs
all(lapply(ntrees, is.rooted)) #one TRUE

2) You probably need to specify resolve.root=TRUE when rooting the tree with a single tip. This will add 0-length branch to node below the specified tip (which becomes the root). Check out ?root for the details. You can add additional arguments to the function call to apply family functions:

rtrees <- lapply (ntrees, root, "new outgroup", resolve.root=TRUE)
ADD COMMENTlink modified 7.9 years ago • written 7.9 years ago by David W4.8k

Thank you very much! I'm not sure, if 0-length branches are acceptable for further analysis. Anyway, your answer is very helpful, especially about brackets and "all (lapply...)".

ADD REPLYlink written 7.9 years ago by Alice300
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1217 users visited in the last hour