Sunday, March 31, 2013

Random trees for a set of taxa

A phytools user recently asked:

"Is there any function in your package that can randomize the shape of a phylogeny? e.g. randomize the topology of a phylogenetic tree?

I'm going to liberally interpret this to mean - how do we simulate random trees for a set of tip taxa?

This is straightforward using the phytools function pbtree, but we could equally well substitute rtree from ape, birthdeath.tree in geiger, or any of the more flexible tree simulation functions in TreeSim; basically anything that creates an object of class "multiPhylo".

Here's how we do it:

# for our simulations, let's set...
species<-LETTERS

# simulate random trees (using any simulator)
random.trees<-pbtree(n=length(species),nsim=100)
# now apply our tip labels
random.trees<-lapply(random.trees,
   function(x,sp){ x$tip.label<-sample(sp); x },
   sp=sample(species))
class(random.trees)<-"multiPhylo"

That's it.

2 comments:

  1. Also, if anyone is interested in only partially randomizing taxon placement on a cladogram, they could randomly collapse nodes with di2multi in ape (or more simply with degradeTree in paleotree) and then randomly resolve those nodes with multi2di. This would produce trees that would only partially share some splits/nodes.

    ReplyDelete