Question: R:Root {Ape} Problem With Rerooting Ultrametric Trees
2
Alice290 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 , is.ultrametric)
\$`    TREE * gen.6720000`
 TRUE
>lapply (ntrees , is.rooted)
\$`    TREE * gen.6720000`
 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 , is.rooted)
\$`    TREE * gen.6720000`
 FALSE

> lapply (rtrees , is.ultrametric)
\$`    TREE * gen.6720000`
 FALSE
``````

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

phylogeny R • 4.9k views
modified 5.4 years ago by Biostar ♦♦ 20 • written 7.4 years ago by Alice290
5
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 `multiPhylo`object (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[]) #a tree, works fine
plot(trees) # a list, R doesn't know how to plot it
plot(trees[])# 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)
``````