Introduction

This code through explores [ape], a useful package for building and analyzing phylogenetic trees.


Content Overview

Specifically, we’ll explore how you can use [ape] to generate, save, and label trees. We’ll also take a peek at some of the analysis that can be done with this package!


Why You Should Care

Phylogenetic trees, as a concept, are a useful way for biologists and geneticists to analyze the (often complex) relationships between different species. They help us to identify how and when related species split off from a common ancestor, and how much genetic change has taken place since then. Historically, this has involved a lot of math!
Although the examples shown here are fairly basic, [ape] is a powerful package that allows us to simplify the math involved and reduce the potential for human error. The package can be downloaded from CRAN.


Learning Objectives

Specifically, you’ll learn how to: * Generate a random phylogenetic tree * Save a phylogenetic tree as a variable * Name the tips (i.e., species) on your tree * Anchor your tree with an outgroup * Use R to calculate the evolutionary distances between the groups on your tree



Exploring [ape]

Here, we’ll show you how to get started with creating and customizing your own phylogenetic trees for basic analysis.


Basic Example

A basic example shows how you can use [ape] to generate and plot a random tree:

rtree(10) # rtree(x) tells [ape] to generate a tree with x tips.
## 
## Phylogenetic tree with 10 tips and 9 internal nodes.
## 
## Tip labels:
##   t6, t9, t3, t1, t4, t10, ...
## 
## Rooted; includes branch length(s).
# it also prints a general description of the tree!
# Or, you can use plot() to print a tree:

plot(rtree(10))


Or, you can save a tree as a variable:

tree <- rtree(6)
tree
## 
## Phylogenetic tree with 6 tips and 5 internal nodes.
## 
## Tip labels:
##   t3, t4, t5, t1, t2, t6
## 
## Rooted; includes branch length(s).
plot(tree)


More Examples

It’s fun to generate phylogenetic trees, but how does that actually help us? Well, once you’ve built your tree, you can name the labels…

tree$tip.label <- c(
  "Tiger",
  "Lion",
  "Leopard",
  "Jaguar",
  "Snow Leopard",
  "Domestic Cat"
)

plot(tree)


Except, this isn’t exactly right – domestic cats are not as closely related to snow leopards & jaguars as this tree would make it seem! Fortunately, you can pick a species to serve as the root:

tree.root <- root(tree, 
                  outgroup = "Domestic Cat",
                  resolve.root = TRUE)

plot(tree.root)


Once you have your tree properly labeled, you can use this package to analyze your data. For example, cophenetic() allows you to calculate the evolutionary distances between species:

kitty.distance <- cophenetic(tree.root)

# This returns the evolutionary distances between each species, gridded as a matrix.

kitty.distance
##                  Tiger      Lion   Leopard   Jaguar Snow Leopard Domestic Cat
## Tiger        0.0000000 0.9278898 0.7216671 1.179288     1.670499    0.2071812
## Lion         0.9278898 0.0000000 0.9813620 1.702746     2.193958    1.0066087
## Leopard      0.7216671 0.9813620 0.0000000 1.496524     1.987735    0.8003860
## Jaguar       1.1792879 1.7027465 1.4965238 0.000000     1.286084    1.2580068
## Snow Leopard 1.6704994 2.1939580 1.9877353 1.286084     0.000000    1.7492183
## Domestic Cat 0.2071812 1.0066087 0.8003860 1.258007     1.749218    0.0000000



Further Resources

Learn more about [ape] with the following:




Works Cited

This code through references and cites the following sources: