Skip to content

Merging Large Trees

Dom Bennett edited this page Jun 13, 2017 · 4 revisions

Click here for full working script

Aim Combine large trees into a single tree

Ingredients

  • Large trees

Problem

We want to combine multiple trees into a single large tree. To do that we can create small trees that can act as building blocks to add trees using addClade(). In this example we will combine the mammal and bird supertrees to create a tree of squamates. Note, this is for demonstrative purposes only.

Steps and Code

Let's first read in the trees using the data() function and calculate the trees ages.

data(mammals)
data(birds)
mml_age <- getAge(mammals)
brd_age <- getAge(birds)

With these trees, we then need to do some node ID renaming because many of the IDs might have the same names across the two trees, which would raise an error if we tried to merge them into a single tree.

mammals['nds'][1:10]
birds['nds'][1:10]  # possible node IDs are not unique
mml_nds <- paste0('mml_', mammals['nds'])
mml_nds[1:10]
avs_nds <- paste0('avs_', birds['nds'])
mammals <- setNdsID(mammals, mammals['nds'], mml_nds)
birds <- setNdsID(birds, birds['nds'], avs_nds)

We can now create the backbone tree to which the mammals and birds will be added. This backbone tree will be a simple two tipped tree. We will then rename the tips, add some taxonyms and add more appropriate node spans so that the resulting tree is ultrametric.

tree <- twoer()
# Add to mammals and birds to tree
tree <- setNdsID(tree, c('t1', 't2'), c('Mammalia', 'Aves'))
# set spans to make tree ultrametric
tree <- setNdSpn(tree, 'Aves', abs(brd_age - mml_age) + 1)
# list of form: id=txnym
txnyms <- list('Aves'='Aves', 'Mammalia'= 'Mammalia',
               'root'='Amniota')
tree <- setTxnyms(tree, txnyms)

Now the tree is set-up, we can use addClade() to add the mammals and birds trees.

tree <- addClade(tree=tree, id='Mammalia', clade=mammals)
tree <- addClade(tree=tree, id='Aves', clade=birds)

Resulting in a tree of over 14,000 tips....

> summary(tree)
Tree (TreeMan Object):
  + 14503 tips
  + 12101 internal nodes
  + With taxonomic names
  + Polytomous
  + PD 149000
  + Root node is "root"

> tree[['root']]
Node (root node):
  + ID: "root"
  + txnym: "Amniota"
  + ptid: "Mammalia", "Aves"
  + nkids: 14503
  + predist: 0
  + pd: 150000

Amniota

Next page: Measuring community turnover

Clone this wiki locally