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...)".
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`
[1] FALSE
> lapply (rtrees [1], is.ultrametric)
$` TREE * gen.6720000`
[1] FALSE
I'm new in phylogenetic comparative methods and in R project. So I just can't find a mistake.
1 answer
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)
Log in to answer this question.