I have recently been in a position to want an efficient way to obtain a binary Newick-format tree from a Newick-format tree that may or may not have polytomies. Using a few functions from ape, this was fairly simple to obtain:
makebinary = function(newick) {
tree = read.tree(text = newick)
if(is.binary.tree(tree)) {
return(newick)
} else {
return(write.tree(multi2di(tree)))
}
}
This contains three very useful functions from ape: read.tree, write.tree, is.binary.tree, and multi2di. The first two are used to read/write Newick-format trees. The functions read.nexus and write.nexus can be used for Nexus-format trees. read.tree can be used for files as well. is.binary.tree checks whether a tree has any polytomies, and multi2di converts a tree with multichotomies to a fully dichotomous tree with some branches of length 0. There is also a function di2multi that will collapse any branches less than a tolerance level to a polytomy.
No comments:
Post a Comment