Reading and writing tree formats with ape and treeio can be executed using the following steps:
- Load the ape library and load in trees:
library(ape)
newick <-ape::read.tree(file.path(getwd(), "datasets", "ch4", "mammal_tree.nwk"))
nexus <-ape::read.nexus(file.path(getwd(), "datasets", "ch4", "mammal_tree.nexus"))
- Load the treeio library and load in BEAST/RAxML output:
library(treeio)
beast <- read.beast(file.path(getwd(), "datasets", "ch4", "beast_mcc.tree"))
raxml <- read.raxml(file.path(getwd(), "datasets", "ch4", "RAxML_bipartitionsBranchLabels.H3"))
- Check the object types that the different functions return:
class(newick)
class(nexus)
class(beast)
class(raxml)
- Convert tidytree to phylo, and vice versa:
beast_phylo <- treeio::as.phylo(beast)
newick_tidytree <- treeio::as.treedata(newick)
- Write output files using the following code:
treeio::write.beast(newick_tidytree,file = "mammal_tree.beast")
ape::write.nexus(beast_phylo, file = "beast_mcc.nexus")