Movatterモバイル変換


[0]ホーム

URL:


Loading phylogenetic trees into R

Martin R. Smithmartin.smith@durham.ac.uk

2025-09-23

If a tree is available in a text-based format, it should not be toodifficult to load into R.

Manual entry

A straightforward, if cumbersome, way to enter trees into R is bymanual text entry:

myTree<- ape::read.tree(text ="((A, B), ((C, D), (E, F)));")plot(myTree)

Beware common gotchas when entering text:

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.

myTree<- ape::read.tree(text ="((A:1, B:1):2, ((C:1, D:1):2, (E:1, F:1):2):4);")plot(myTree)

From a file

First off, make sure that you are comfortabletelling R where to find afile.

Nexus files

You can load trees from a nexus file using:

filename<-"my_file_name.nex"ape::read.nexus(filename)

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

lapply(ape::read.nexus(filename,force.multiPhylo =TRUE), ape::consensus)

Newick files

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 from TNT

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])).

Other formats

Are there other formats that should be listed here? Please let meknow byopeningan issue.

Export (write) trees

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.

What next?

You might want to:


[8]ページ先頭

©2009-2025 Movatter.jp