Background work

In order to gather the data I first had to blast the sequences of 5 different chimp species. I compared their sequences using a MSA on Microsoft excel. After creating the MSA I picked 10 polymorphic locis for analysis at random (i juist picked them randomly). Then i tallied up the numbers of alleles per locus which were only SNPs or Single Nucleotide Polymorphisms. After that i setup pair-wise comparisons between all unique comparisons, I assigned the number 1 to different nucleotides and 0 to similar nucleotides. Lastly I built a matrix to represent these similarities with each comparison unique score which can be found in the similarity matrix outline down below.

Preliminaries

# install.packages(ape)
# install.packages(phangorn)

library(ape)
library(phangorn)

Example 1: 3 Sequences

Similarity matrix

This matrix is based on the proportion of bases that are identical between sequence. This is often referred to as PID for Proportion Identical or Percentage Identical.

BLAST reports PID in its main output. PID is a very simple metric of similarity; more sophisticated measures are used in pratice.

Make a similarity matrix with the matrix() command. Note that I have to declare the number of rows

# Good matrix
matrix(c(1.0, 0.5, 0.3,
         0.5, 1.0, 0.4,
         0.3, 0.4, 1.0),
       nrow = 3)
##      [,1] [,2] [,3]
## [1,]  1.0  0.5  0.3
## [2,]  0.5  1.0  0.4
## [3,]  0.3  0.4  1.0

Store the matrix

my_sim_mat <- matrix(c(1.0, 0.5, 0.3,
                       0.5, 1.0, 0.4,
                       0.3, 0.4, 1.0),
                 nrow = 3,
                 byrow = T)

Label the matrix with row.names() and colnames()

row.names(my_sim_mat) <- c("G","T","M")
colnames(my_sim_mat) <- c("G","T","M")

Disimilarity matrix

Similarity, disimilarity, and distance are all related. Most methods use distance, not similarity.

We can do vectorized math to recalculate the matrix

my_dist_mat <- 1-my_sim_mat

Convert to R’s distance format

my_dist_mat2 <- as.dist(my_dist_mat)

Build a neighbor-joining (nj) tree

Neighbor Joining is one of the most common ways to build a tree using molecular data that’s been converted to sequences; its one of the options within BLAST.

Build the tree with nj()

my_nj <- ape::nj(my_dist_mat2)

Plot the tree as an “unrooted” tree

plot(my_nj, "unrooted")

Plot the tree as an “rooted” tree

plot(my_nj)

UPGMA/WPGMA are other algorithms that work with distance matrices. They are not commonly used now but are useful for teaching becaues they can easily be done by hand on small datasets.

my_upgma <- phangorn::upgma(my_dist_mat2)

Plot the UPGMA tree

plot(my_upgma)

Compare the rooted NJ and the UPGMA

par(mfrow = c(1,2))
plot(my_nj)
plot(my_upgma)

WPGMA tree

plot(wpgma(my_dist_mat2))

Minimum evolution tree

plot(fastme.ols(my_dist_mat2))

Example 2: 5 Sequences

Build the matrix.

Be sure to add the nrow = … statemetn.

five_sim_mat <- matrix(c(1.0,     0.0,  0.0,  0.0,  0.0,        
                         1  ,   1.0,    0.0,  0.0,  0.0,                
                         0.6,   0.6,    1.0,    0.0,  0.0,      
                         0.4,     0.4,  0.9,    1.0,    0.0,        
                         0.5,   0.5,    0.8,    0.3,    1.0),
                       nrow = 5,
                       byrow = T)

Name things

row.names(five_sim_mat) <- c("ME" , "B" , "G" , "T", "MW")
colnames(five_sim_mat)   <- c("ME" , "B" , "G" , "T", "MW")

Turn into a distnace matrix. This is 2 steps and requires the as.dist() command

five_dist_mat <- 1-five_sim_mat   
five_dist_mat2 <- as.dist(five_sim_mat)

Neighbor-Joining tree with nj()

five_nj <- nj(five_dist_mat2)

Plot unrooted NJ tree

plot(five_nj , "unrooted")

Plot rooted NJ tree

plot(five_nj)

Build UPGMA tree

five_upgma <- phangorn::upgma(five_dist_mat2)

Plot UPGMA tree

plot(five_upgma)

Compare rooted NJ and UPGMA plots

par(mfrow = c(1,2))
plot(five_nj)
plot(five_upgma)

Build WPGMA tree

five_wpgma <- wpgma(five_dist_mat2)

WPGMA Tree Plot

plot(five_wpgma)

Compare rooted WPGMA and UPGMA plots

par(mfrow = c(1,2))
plot(five_upgma)
plot(five_wpgma)

Build Minimum evolution tree

plot(fastme.ols(five_dist_mat2))