(This article was first published on R on Guangchuang YU, and kindly contributed to R-bloggers)
I don’t know whether ‘rename taxa’ is a common task or not. It seems not a good idea to rename taxa in Newick tree text, since it may introduce problems when mapping the original sequence alignment to the tree.
If you just want to show different or additional information when plotting the tree, it is fine and easy to do it using ggtree
:
require(treeio)
## Loading required package: treeio
require(ggtree)
## Loading required package: ggtree
## ggtree v1.11.6 For help: https://guangchuangyu.github.io/software/ggtree## ## If you use ggtree in published research, please cite:## Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36, doi:10.1111/2041-210X.12628
tr <- read.tree(text = "((a,(b,c)),d);")genus <- c("Gorilla", "Pan", "Homo", "Pongo")species <- c("gorilla", "spp.", "sapiens", "pygmaeus")geo <- c("Africa", "Africa", "World", "Asia")d <- data.frame(label = tr$tip.label, genus = genus, species = species, geo = geo)d
## label genus species geo## 1 a Gorilla gorilla Africa## 2 b Pan spp. Africa## 3 c Homo sapiens World## 4 d Pongo pygmaeus Asia
ggtree(tr) %<+% d + xlim(NA, 5) + geom_tiplab(aes(label=paste0('italic(', genus, ')~bolditalic(', species, ')~', geo)), parse=T)
## Warning: package 'bindrcpp' was built under R version 3.4.4
However, it is also possible to rename taxa of the tree object (either treedata
or phylo
) in treeio
:
tr2 = rename_taxa(tr, d, label, genus)write.tree(tr2)
## [1] "((Gorilla,(Pan,Homo)),Pongo);"
d2 = dplyr::mutate(d, newlab = paste(genus, species, sep='|'))d2
## label genus species geo newlab## 1 a Gorilla gorilla Africa Gorilla|gorilla## 2 b Pan spp. Africa Pan|spp.## 3 c Homo sapiens World Homo|sapiens## 4 d Pongo pygmaeus Asia Pongo|pygmaeus
tr3 = rename_taxa(tr, d2, label, newlab)write.tree(tr3)
## [1] "((Gorilla|gorilla,(Pan|spp.,Homo|sapiens)),Pongo|pygmaeus);"
If the input tree object is a treedata
instance, you can use write.beast
to export the tree with associated data to a BEAST compatible NEXUS file.
To leave a comment for the author, please follow the link and comment on their blog: R on Guangchuang YU.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...