If a tree is available in a text-based format, it should not be toodifficult to load into R.
A straightforward, if cumbersome, way to enter trees into R is bymanual text entry:
Beware common gotchas when entering text:
Ensure that the tree string ends in a semi-colon.
Make sure that brackets are matched. Some text editors, such asRStudio orNotepad++, contain built-insyntax highlighting that will highlight the counterpart to eachparenthesis as you type.
Look out for ((double brackets)) or brackets enclosing a singlenode: the resultant ‘invisible nodes’ can cause R to crash.
badTree<- ape::read.tree(text ="((A, B), (((C, D), ((E), F))));")plot(badTree)ape::nodelabels(bg =c(3,3,2,2,3,3,2))The text is expected in Newick format, so can contain edge lengths -but edge lengths must be included for every edge of the tree, or theywill be ignored.
First off, make sure that you are comfortabletelling R where to find afile.
You can load trees from a nexus file using:
If the file contains multiple trees, this will return a list of alltrees in the file, with the classmultiPhylo. If there is asingle tree, then this will be returned as a tree of classphylo unless the optionforce.multiPhylo = TRUE. This can be useful when an unknownnumber of trees are to be processed in bulk, for example by
Newick trees can be read withape::read.tree(filename);phytools::read.newick(filename) does not report an errorwhen a ‘node’ has a single descendant (which will often denote amisspecified tree).
Trees saved usingTNT can be opened inR usingReadTntTree().
Trees should be saved in parenthetical format (TNT commandtsav*), rather than TNT’s compressed format (TNT commandtsav).
The TNT commandtaxname= will write taxon names to file,which results in larger but easier to read files. Trees in such a filewill be read using the terminal names saved within the file. The TNTcommandtaxname- saves just the numbers of the terminals.In order for the trees to be reunited with the names of their tips,terminal labels will be read from the linked matrix file listed in thefirst line of the .tre file. Ensure that this file exists in theexpected location. If it does not, useReadTntTree(treeFile, relativePath = '../path/to/tipNameFile'),ortipLabels to manually specify the names of the tips(e.g.ReadTntTree(treeFile, tipLabels = c('outgroup', letters[1:8])).
Are there other formats that should be listed here? Please let meknow byopeningan issue.
To save a tree to a text file, useape::write.tree(tree, file='filename.txt') for Newickformat (widely supported by most phylogenetic software), orape::write.nexus(tree, file='filename.nex') for Nexusformat.
You might want to:
Load phylogenetic data intoR.
Conduct parsimony search using Brazeau, Guillerme & Smith’sapproachto inapplicable data, or usingProfileparsimony.
Calculatedistances between pairs of trees.