Constructing trees using phangorn can be executed using the following steps:
- Load in the libraries and sequences, and make an alignment:
library(Biostrings)
library(msa)
library(phangorn)
seqs <- readAAStringSet(file.path(getwd(), "datasets", "ch4", "abc.fa"))
aln <- msa::msa(seqs, method=c("ClustalOmega"))
- Convert the alignment to the phyDat object:
aln <- as.phyDat(aln, type = "AA")
- Make UPGMA and neighbor-joining trees from a distance matrix:
dist_mat <- dist.ml(aln)
upgma_tree <- upgma(dist_mat)
plot(upgma_tree, main="UPGMA")
nj_tree <- NJ(dist_mat)
plot(nj_tree,"unrooted", main="NJ")
- Calculate the bootstraps and plot:
bootstraps <- bootstrap.phyDat(aln,FUN=function(x) { NJ(dist.ml(x)) } , bs=100)
plotBS(nj_tree, bootstraps, p = 10)