An object of class phylo can be converted to an object of class tibble and vice versa. The given function adds columns of ncbi taxonomy levels to the tibble created from class phylo. Resultant tibble can be passed to ggtree::ggtree() to visualize phylogenetic tree. Added taxonomy columns can be used for aesthetics such as tip color, node color etc.

tidy_taxonomy_tree(
  tree_data,
  ncbi_accession_colname = "ncbi_accession",
  taxonomy_levels = c("species", "kingdom")
)

Arguments

tree_data

a tbl containing minimum columns required to create an object of class phylo.

ncbi_accession_colname

a string (default : "ncbi_accession") denoting column name of ncbi accession.

taxonomy_levels

a character vector containing levels of ncbi taxonomy. For each of these levels corresponding values will be mapped for ncbi accession.

Value

a tbl containing all columns from tree_data + columns of assigned taxonomy levels.

Examples

if (FALSE) { ## read tree from newick string tree_string <- "((XP_005187699_1__Musca_domestica:0.070627277,(XP_019893806_1__Musca_domestica:0.071069674,((XP_013113221_1__Stomoxys_calcitrans:0.1494662042,ACB98719_1__Glossina_morsitans_morsitans:0.3489851076)67.4/100:0.0470213767,XP_013102958_1__Stomoxys_calcitrans:0.1794878827)98.1/100:0.0959227604)88.2/99:0.0323598861)93/99:0.0435291148,((XP_017472861_1__Rhagoletis_zephyria:0.0049337059,XP_017472862_1__Rhagoletis_zephyria:0.0112391294)97.3/100:0.0860969479,(XP_020713236_1__Ceratitis_capitata:0.2642805176,(XP_014102010_1__Bactrocera_oleae:0.1183517872,XP_018784523_1__Bactrocera_latifrons:0.1137567198)29.6/88:0.0758551876)99.9/100:0.247740081)92/100:0.0716529011)34.3/66:2.487103817;" tree_objct <- read.tree(text = tree_string) tree_tbl <- tree_objct %>% ggtree::fortify() tree_tbl <- tree_tbl %>% dplyr::mutate( seqid = dplyr::case_when(isTip ~ stringr::str_replace(label , pattern = "__.*","" ) %>% ## split by '__' stringr::str_replace(pattern = "_\\d$" , ""), ## remove trailing digits from seqid TRUE ~ label ) ) ## add taxonomy tree_tbl_with_taxonomy <- tree_tbl %>% phyloR::tidy_taxonomy_tree(ncbi_accession_colname = "seqid",taxonomy_levels = c("species" ,"kingdom","family")) ## visualize tree # tips colored by species tree_tbl_with_taxonomy %>% ggtree::ggtree() + ggtree::geom_tiplab(aes(color = species)) # tips colored by family tree_tbl_with_taxonomy %>% ggtree::ggtree() + ggtree::geom_tiplab(aes(color = family)) # tips colored by kingdom tree_tbl_with_taxonomy %>% ggtree::ggtree() + ggtree::geom_tiplab(aes(color = kingdom)) }