# set global chunk options: images will be bigger knitr::opts_chunk$set(fig.width=6, fig.height=4) #, global.par=TRUE options(digits = 4) suppressPackageStartupMessages(library(phangorn))
These notes describe the ancestral sequence reconstruction using the phangorn package [@Schliep2011]. phangorn provides several methods to estimate ancestral character states with either Maximum Parsimony (MP) or Maximum Likelihood (ML). For more background on all the methods see e.g. [@Felsenstein2004] or [@Yang2006].
To reconstruct ancestral sequences we first load some data and reconstruct a tree:
library(phangorn) fdir <- system.file("extdata/trees", package = "phangorn") primates <- read.phyDat(file.path(fdir, "primates.dna"), format = "interleaved") tree <- pratchet(primates, trace=0) |> acctran(primates) |> makeNodeLabel() parsimony(tree, primates)
For parsimony analysis of the edge length represent the observed number of changes. Reconstructing ancestral states therefore defines also the edge lengths of a tree. However there can exist several equally parsimonious reconstructions or states can be ambiguous and therefore edge length can differ. "MPR" reconstructs the ancestral states for each (internal) node as if the tree would be rooted in that node. However the nodes are not independent of each other. If one chooses one state for a specific node, this can restrict the choice of neighboring nodes (figures 2 and 3). The function acctran (accelerated transformation) assigns edge length and internal nodes to the tree [@Swofford1987].
anc.acctran <- ancestral.pars(tree, primates, "ACCTRAN") anc.mpr <- ancestral.pars(tree, primates, "MPR")
All the ancestral reconstructions for parsimony are based on the fitch algorithm and so far only bifurcating trees are allowed. However trees can get pruned afterwards using the function multi2di
from ape.
The plotSeqLogo
function is a wrapper around the from the ggseqlogo function the the ggseqlogo package [@ggseqlogo] and provides a simple way to show proportions of a nucleotides of ancestral states (see figure 1).
#library(seqLogo) #seqLogo( t(subset(anc.mpr, getRoot(tree), 1:20)[[1]]), ic.scale=FALSE) plotSeqLogo(anc.mpr, node=getRoot(tree), 1, 20)
plotAnc(anc.mpr, 17) title("MPR")
plotAnc(anc.acctran, 17) title("ACCTRAN")
phangorn also offers the possibility to estimate ancestral states using ML. The advantages of ML over parsimony is that the reconstruction accounts for different edge lengths. So far only a marginal construction is implemented (see [@Yang2006][@Koshi1996]) and no joint reconstruction [@Pupko2000].
fit <- pml(tree, primates) fit <- optim.pml(fit, model="F81", control = pml.control(trace=0))
We can assign the ancestral states according to the highest likelihood ("ml"): [ P(x_r = A) = \frac{L(x_r=A)}{\sum_{k \in {A,C,G,T}}L(x_r=k)} ] and the highest posterior probability ("bayes") criterion: [ P(x_r=A) = \frac{\pi_A L(x_r=A)}{\sum_{k \in {A,C,G,T}}\pi_k L(x_r=k)}, ] where $L(x_r)$ is the joint probability of states at the tips and the state at the root $x_r$ and $\pi_i$ are the estimated base frequencies of state $i$. Both methods agree if all states (base frequencies) have equal probabilities.
anc.ml <- ancestral.pml(fit, "ml") anc.bayes <- ancestral.pml(fit, "bayes")
The differences of the two approaches for a specific site (17) are represented in the following figures.
plotAnc(anc.ml, 17) title("ML")
plotAnc(anc.bayes, 17) title("Bayes")
Often have already a phylogeny and only want estimate the ancestral reconstruction for this tree. This is a common problem in phylogentic comparative methods and we can use the function ace in the ape [@Paradis2018], fitDiscrete in the geiger [@Pennell2014] or fitMK in the phytools [@Revell2012] package. Here we want to show how to fit these models using optim.pml.
First we load a tree and create some data.
data("bird.orders") x <- c(rep(0, 5), rep(1, 18)) x[c(20,22,23)] <- 2 x <- factor(x) names(x) <- bird.orders$tip.label dat <- phyDat(x, "USER", levels=c(0,1,2))
We than set up the pml object and optimize the model. Instead of optimizing the edge length we only optimize the rate.
fit <- pml(bird.orders, dat) fit_ER <- optim.pml(fit, optEdge = FALSE, optRate=TRUE, control = pml.control(trace=0)) fit_ER
We can also fit the symmetric (model="SYM") or ordered metristic model (model="ORDERED").
fit_SYM <- optim.pml(fit, optEdge = FALSE, optRate=TRUE, model="SYM", control = pml.control(trace=0)) fit_SYM
We can compare the estimate with the one from ace from ape.
fit_ace <- ace(x, bird.orders, model="SYM", type = "d")
The log-likelihood values differ slightly as in phangorn the values get multiplied
by the state frequencies. Thus if we add log(1/3)
as we have three states to ace estimate the two estimates are almost identical.
fit_SYM$logLik fit_ace$loglik+log(1/3) all.equal(fit_SYM$logLik, fit_ace$loglik+log(1/3))
anc_SYM <- ancestral.pml(fit_SYM, "ml") plotAnc(anc_SYM)
More complicated models can be applied using defining the rate matrix as shown in the vignette Markov models and transition rate matrices. The "ARD" model is currently not available as phangorn only fits reversible models.
sessionInfo()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.