This morning I received the following message:
“I am trying to write a program that reroots a phylogenetic tree at every edge(branch)
of the tree and saves each rerooted tree. I am using reroot
under phytools.
When I run for a single specified branch I am able to get a rerooted tree but when I run
for all branches in a tree, I am getting… (Ed. an error).”
I'm not sure the user ran into difficulties, but the following is a simple function to
re-root a tree along all edges that uses phytools::reroot
internally:
reroot.all<-function(tree,unroot=TRUE){
if(unroot) tree<-unroot(tree)
if(is.null(tree$edge.length)){
edge.lengths<-FALSE
tree$edge.length<-rep(1,nrow(tree$edge))
} else edge.lengths<-TRUE
trees<-vector(mode="list",length=nrow(tree$edge))
for(i in 1:nrow(tree$edge)){
trees[[i]]<-reroot(tree,tree$edge[i,2],
0.5*tree$edge.length[i])
if(!edge.lengths) trees[[i]]$edge.length<-NULL
}
class(trees)<-"multiPhylo"
trees
}
Let's try it:
library(phytools)
## Loading required package: ape
## Loading required package: maps
tree<-rtree(n=26,tip.label=LETTERS)
trees<-reroot.all(tree)
trees
## 49 phylogenetic trees
par(mfrow=c(7,7))
plotTree(trees,fsize=0.5,lwd=1)
It works!
No comments:
Post a Comment
Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.