Of course, we could generalize my preceding suggestion with the following function:

add.everywhere<-function(tree,tip.name){
   tree<-unroot(tree)
   tree$edge.length<-rep(1,nrow(tree$edge))
   new.tip<-list(edge=matrix(c(2,1),1,2),tip.label=tip.name,
     edge.length=1,Nnode=1)
   class(new.tip)<-"phylo"
   # add the new tip to all edges of the tree
   trees<-list(); class(trees)<-"multiPhylo"
   for(i in 1:nrow(tree$edge)){
      trees[[i]]<-bind.tree(tree,new.tip,where=tree$edge[i,2],
        position=0.5)
      trees[[i]]$edge.length<-NULL
   }
   return(trees)
}

Try it:

tree<-read.tree(text="((George,Paul),(Ringo,John));")
trees<-add.everywhere(tree,"Pete_Best")
plot(trees,type="unrooted")


--
Liam J. Revell
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
email: liam.rev...@umb.edu
blog: http://phytools.blogspot.com

On 4/1/2011 11:33 PM, Liam J. Revell wrote:
Hi Matthew.

I don't doubt that other members of the list have better suggestions,
but it is possible to add a tip in all possible places using bind.tree()
in "ape."

For instance, starting with a random unrooted tree with, say, 4 taxa:

tree<-rtree(n=4,rooted=FALSE,br=rep(1,5))
# create a 5th species, here "t5", to add as a "phylo" object
# [I don't think this can be avoided with bind.tree()]
new.tip<-list(edge=matrix(c(2,1),1,2),tip.label="t5",edge.length=1,Nnode=1)
class(new.tip)<-"phylo"
# add the new tip to all edges of the tree
trees<-list(); class(trees)<-"multiPhylo"
for(i in 1:nrow(tree$edge))
trees[[i]]<-bind.tree(tree,new.tip,where=tree$edge[i,2],position=0.5)
# now plot them to see what we have done
plot(trees,type="unrooted",use.edge.length=F)

Of course I would be happy to see a more elegant solution!

Sincerely, Liam


_______________________________________________
R-sig-phylo mailing list
R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo

Reply via email to