| Title: | Base Classes and Functions for Phylogenetic Tree Input and Output |
|---|---|
| Description: | 'treeio' is an R package to make it easier to import and store phylogenetic tree with associated data; and to link external data from different sources to phylogeny. It also supports exporting phylogenetic tree with heterogeneous associated data to a single tree file and can be served as a platform for merging tree with associated data and converting file formats. |
| Authors: | Guangchuang Yu [aut, cre] (ORCID: <https://orcid.org/0000-0002-6485-8781>), Tommy Tsan-Yuk Lam [ctb, ths], Shuangbin Xu [ctb] (ORCID: <https://orcid.org/0000-0003-3513-5362>), Bradley Jones [ctb], Casey Dunn [ctb], Tyler Bradley [ctb], Konstantinos Geles [ctb] |
| Maintainer: | Guangchuang Yu <[email protected]> |
| License: | Artistic-2.0 |
| Version: | 1.37.1 |
| Built: | 2026-09-22 00:23:22 UTC |
| Source: | https://github.com/bioc/treeio |
convert phylo to treedata
## S3 method for class 'phylo' as.treedata(tree, boot = NULL, ...)## S3 method for class 'phylo' as.treedata(tree, boot = NULL, ...)
tree |
input tree, a |
boot |
optional, can be bootstrap value from ape::boot.phylo |
... |
additional parameters |
converting phylo object to treedata object
Guangchuang Yu
convert edge number to node number for EPA/pplacer output
edgeNum2nodeNum(jp, edgeNum)edgeNum2nodeNum(jp, edgeNum)
jp |
a |
edgeNum |
the edge number of a placement, the |
the node number of the reference tree, NA for an edge
number that is not in the tree
Guangchuang Yu
jpfile <- system.file("extdata", "sample.jplace", package="treeio") jp <- read.jplace(jpfile) edgeNum2nodeNum(jp, c(0, 1, 2))jpfile <- system.file("extdata", "sample.jplace", package="treeio") jp <- read.jplace(jpfile) edgeNum2nodeNum(jp, c(0, 1, 2))
find the hierarchical cluster analysis among the nodes of graph based on the length of all the shortest paths in the graph.
find.hclust( x, graph.mst = FALSE, weights = NULL, hclust.method = "average", ... )find.hclust( x, graph.mst = FALSE, weights = NULL, hclust.method = "average", ... )
x |
a igraph object |
graph.mst |
logical whether obtain the minimum spanning tree first then find.hclust, default is FALSE. |
weights |
a numeric vector giving edge weights or a character.
If this is |
hclust.method |
the agglomeration method to be used, This should be (an
unambiguous abbreviation of) one of |
... |
additional parameters |
hclust object
library(igraph) set.seed(123) g <- igraph::sample_gnp(100, .1) %>% set_edge_attr(name='weight', value=abs(rnorm(E(.),3))) tr1 <- find.hclust(g, weights = NA) tr2 <- find.hclust(g) tr3 <- find.hclust(g, graph.mst = TRUE)library(igraph) set.seed(123) g <- igraph::sample_gnp(100, .1) %>% set_edge_attr(name='weight', value=abs(rnorm(E(.),3))) tr1 <- find.hclust(g, weights = NA) tr2 <- find.hclust(g) tr3 <- find.hclust(g, graph.mst = TRUE)
access placement information
get.placements(tree, ...) ## S3 method for class 'jplace' get.placements(tree, by = "best", ...)get.placements(tree, ...) ## S3 method for class 'jplace' get.placements(tree, by = "best", ...)
tree |
tree object |
... |
additional parameters |
by |
one of 'best' and 'all' |
The node column is the node number of the reference tree, that
is the node the branch leads to (a tip or an internal node, numbered
1:Ntip for the tips and Ntip+1:Ntip+Nnode for the internal nodes). It is
translated from the edge_num of the jplace file, which EPA and pplacer
number by a post-order traversal of the tree; edgeNum2nodeNum() does
that translation, #22
placement tibble
access phylo slot
get.tree(x, ...)get.tree(x, ...)
x |
tree object |
... |
additional parameters |
phylo object
Guangchuang Yu
access tree text (newick text) from tree object
get.treetext(object, ...)get.treetext(object, ...)
object |
treedata object |
... |
additional parameter |
phylo object
calculate total number of nodes
getNodeNum(tree) Nnode2(tree)getNodeNum(tree) Nnode2(tree)
tree |
tree object |
number
Guangchuang Yu
getNodeNum(rtree(30)) Nnode2(rtree(30))getNodeNum(rtree(30)) Nnode2(rtree(30))
test whether input object is produced by ggtree function
is.ggtree(x)is.ggtree(x)
x |
object |
TRUE or FALSE
Guangchuang Yu
Class "jplace" This class stores phylogenetic placements
phylophylo object for tree structure
treetextnewick tree string
dataassociated data
extraInfoextra information, reserve for merge_tree
filetree file
placementsreserve for jplace file to store placement information
infoextra information, e.g. metadata, software version etc.
Guangchuang Yu https://guangchuangyu.github.io
label branch for PAML to infer selection pressure using branch model
label_branch_paml(tree, node, label)label_branch_paml(tree, node, label)
tree |
phylo object |
node |
node number |
label |
label of branch, e.g. #1 |
updated phylo object
Guangchuang Yu
site mask
mask(tree_object, field, site, mask_site = FALSE)mask(tree_object, field, site, mask_site = FALSE)
tree_object |
tree object |
field |
selected field |
site |
site |
mask_site |
if TRUE, site will be masked. if FALSE, selected site will not be masked, while other sites will be masked. |
updated tree object
Guangchuang Yu
merge two tree object
merge_tree(obj1, obj2)merge_tree(obj1, obj2)
obj1 |
tree object 1 |
obj2 |
tree object 2 |
tree object
Guangchuang Yu
print information of a list of treedata objects
## S3 method for class 'treedataList' print(x, ...)## S3 method for class 'treedataList' print(x, ...)
x |
a list of treedata objects |
... |
no used |
message
convert raxml bootstrap tree to newick format
raxml2nwk(infile, outfile = "raxml.tree")raxml2nwk(infile, outfile = "raxml.tree")
infile |
input file |
outfile |
output file |
newick file
Guangchuang Yu
parse ASTRAL output newick text
read.astral(file)read.astral(file)
file |
ASTRAL Newick file |
treedata object
Guangchuang Yu
tt <- paste0( "((species1,(species2,species3)'[pp1=0.75;pp2=0.24;pp3=0.01]':", "1.2003685744180805)'[pp1=0.98;pp2=0.02;pp3=0]':0.9679599282730038,", "((species4,species5)'[pp1=0.88;pp2=0.11;pp3=0.01]':1.2454851536484994))" ) read.astral(textConnection(tt))tt <- paste0( "((species1,(species2,species3)'[pp1=0.75;pp2=0.24;pp3=0.01]':", "1.2003685744180805)'[pp1=0.98;pp2=0.02;pp3=0]':0.9679599282730038,", "((species4,species5)'[pp1=0.88;pp2=0.11;pp3=0.01]':1.2454851536484994))" ) read.astral(textConnection(tt))
read beast/mrbayes/mega Nexus output
read beast/mrbayes/mega newick file format
read.beast(file, threads = 1, verbose = FALSE) read.mrbayes(file, threads = 1, verbose = FALSE) read.beast.newick(file, threads = 1, verbose = FALSE) read.mega(file, threads = 1, verbose = FALSE)read.beast(file, threads = 1, verbose = FALSE) read.mrbayes(file, threads = 1, verbose = FALSE) read.beast.newick(file, threads = 1, verbose = FALSE) read.mega(file, threads = 1, verbose = FALSE)
file |
newick file |
threads |
number of threads for multithreading (default: 1) |
verbose |
set TRUE to log progress (default: FALSE) |
treedata object
treedata object
Guangchuang Yu https://guangchuangyu.github.io
Bradley R Jones
file <- system.file("extdata/BEAST", "beast_mcc.tree", package="treeio") read.beast(file) file <- system.file("extdata/MrBayes", "Gq_nxs.tre", package="treeio") read.mrbayes(file) tr <- read.beast.newick( textConnection( '(a[&rate=1]:2,(b[&rate=1.1]:1,c[&rate=0.9]:1)[&rate=1]:1);' ) )file <- system.file("extdata/BEAST", "beast_mcc.tree", package="treeio") read.beast(file) file <- system.file("extdata/MrBayes", "Gq_nxs.tre", package="treeio") read.mrbayes(file) tr <- read.beast.newick( textConnection( '(a[&rate=1]:2,(b[&rate=1.1]:1,c[&rate=0.9]:1)[&rate=1]:1);' ) )
read baseml output
read.codeml(rstfile, mlcfile, tree = "mlc", type = "Joint")read.codeml(rstfile, mlcfile, tree = "mlc", type = "Joint")
rstfile |
rst file |
mlcfile |
mlc file |
tree |
one of 'mlc' or 'rst' |
type |
one of 'Marginal' or 'Joint' |
A treedata object
Guangchuang Yu
rstfile <- system.file("extdata/PAML_Codeml", "rst", package="treeio") mlcfile <- system.file("extdata/PAML_Codeml", "mlc", package="treeio") read.codeml(rstfile, mlcfile)rstfile <- system.file("extdata/PAML_Codeml", "rst", package="treeio") mlcfile <- system.file("extdata/PAML_Codeml", "mlc", package="treeio") read.codeml(rstfile, mlcfile)
read mlc file of codeml output
read.codeml_mlc(mlcfile)read.codeml_mlc(mlcfile)
mlcfile |
mlc file |
A codeml_mlc object
Guangchuang Yu
mlcfile <- system.file("extdata/PAML_Codeml", "mlc", package="treeio") read.codeml_mlc(mlcfile)mlcfile <- system.file("extdata/PAML_Codeml", "mlc", package="treeio") read.codeml_mlc(mlcfile)
read FASTA file
read.fasta(fasta, type = "auto")read.fasta(fasta, type = "auto")
fasta |
fasta file |
type |
sequence type of the input file, one of 'NT' or 'AA'. Default is 'auto' and guess the sequence type automatically |
This function supports both DNA or AA sequences
DNAbin or AAbin object
Guangchuang Yu
read HYPHY output
read.hyphy(nwk, ancseq, tip.fasfile = NULL)read.hyphy(nwk, ancseq, tip.fasfile = NULL)
nwk |
tree file in nwk format, one of hyphy output |
ancseq |
ancestral sequence file in nexus format, one of hyphy output |
tip.fasfile |
tip sequence file |
A hyphy object
Guangchuang Yu https://guangchuangyu.github.io
nwk <- system.file("extdata/HYPHY", "labelledtree.tree", package="treeio") ancseq <- system.file("extdata/HYPHY", "ancseq.nex", package="treeio") read.hyphy(nwk, ancseq)nwk <- system.file("extdata/HYPHY", "labelledtree.tree", package="treeio") ancseq <- system.file("extdata/HYPHY", "ancseq.nex", package="treeio") read.hyphy(nwk, ancseq)
parse sequences from hyphy output
read.hyphy.seq(file)read.hyphy.seq(file)
file |
output of hyphy ancestral sequence inference; nexus format |
DNAbin object
Guangchuang Yu
ancseq <- system.file("extdata/HYPHY", "ancseq.nex", package="treeio") read.hyphy.seq(ancseq)ancseq <- system.file("extdata/HYPHY", "ancseq.nex", package="treeio") read.hyphy.seq(ancseq)
parse IQ-TREE output
read.iqtree(file)read.iqtree(file)
file |
IQ-TREE Newick text |
read.iqtree() parses the tree file of an IQ-TREE run (e.g.
*.treefile, the report file *.iqtree is not a tree file) and splits the
node labels into the SH-aLRT and the UFBoot support values. The two values
are expected to be separated by a /, which is what IQ-TREE writes when
both were requested. If the branch support was assessed with a single
method (e.g. the standard non-parametric bootstrap or SH-aLRT alone), the
node label holds one value only and there is no way to tell which method
produced it; the value is then stored in both columns and a message is
emitted. Use read.newick() to import such a tree without guessing.
treedata object
Guangchuang Yu
read jplace file
read.jplace(file)read.jplace(file)
file |
jplace file |
jplace instance
Guangchuang Yu
jp <- system.file("extdata", "sample.jplace", package="treeio") read.jplace(jp)jp <- system.file("extdata", "sample.jplace", package="treeio") read.jplace(jp)
Import tree data from jtree file, which is JSON-based text and probably output by write.jtree
read.jtree(file)read.jtree(file)
file |
tree file |
treedata object
Guangchuang Yu
read MCMCTree output Tree
read.mcmctree(file, force.ultrametric = FALSE)read.mcmctree(file, force.ultrametric = FALSE)
file |
the output tree file of MCMCTree |
force.ultrametric |
logical whether convert the tree to be ultrametric, if it is not ultrametric, default is FALSE. When the tree is ultrametric, branch times will be calculated automatically. |
The node age is not reported by MCMCTree, it is derived from the
branch lengths and stored in the reltime column. The credibility interval
of a node age, [&95%={lower, upper}] in the file, is stored in the
reltime_0.95_CI column and can be plotted with
geom_range(range='reltime_0.95_CI', center='reltime').
treedata object
file <- system.file("extdata/MCMCTree", "mcmctree_output.tree", package="treeio") tr <- read.mcmctree(file) trfile <- system.file("extdata/MCMCTree", "mcmctree_output.tree", package="treeio") tr <- read.mcmctree(file) tr
parse tabular output of MEGA
read.mega_tabular(file)read.mega_tabular(file)
file |
MEGA tabular file |
treedata object
Guangchuang Yu
read newick tree
read.newick(file, node.label = "label", ...)read.newick(file, node.label = "label", ...)
file |
newick file |
node.label |
parse node label as 'label' or 'support' value |
... |
additional parameter, passed to 'read.tree' |
phylo or treedata object
Guangchuang Yu
read.nextstrain.json
read.nextstrain.json(x)read.nextstrain.json(x)
x |
the json tree file of auspice from nextstrain. |
treedata object
Shuangbin Xu
file1 <- system.file("extdata/nextstrain.json", "minimal_v2.json", package="treeio") tr <- read.nextstrain.json(file1) trfile1 <- system.file("extdata/nextstrain.json", "minimal_v2.json", package="treeio") tr <- read.nextstrain.json(file1) tr
read nhx tree file
read.nhx(file)read.nhx(file)
file |
nhx file |
nhx object
Guangchuang Yu https://guangchuangyu.github.io
nhxfile <- system.file("extdata/NHX", "ADH.nhx", package="treeio") read.nhx(nhxfile)nhxfile <- system.file("extdata/NHX", "ADH.nhx", package="treeio") read.nhx(nhxfile)
read rst file from paml (both baseml and codeml) output
read.paml_rst(rstfile, type = "Joint")read.paml_rst(rstfile, type = "Joint")
rstfile |
rst file |
type |
one of 'Marginal' or 'Joint' |
A treedata object
Guangchuang Yu https://guangchuangyu.github.io
rstfile <- system.file("extdata/PAML_Baseml", "rst", package="treeio") read.paml_rst(rstfile)rstfile <- system.file("extdata/PAML_Baseml", "rst", package="treeio") read.paml_rst(rstfile)
parsing phylip tree format
read.phylip(file)read.phylip(file)
file |
phylip file |
an instance of 'phylip'
Guangchuang Yu
phyfile <- system.file("extdata", "sample.phy", package="treeio") read.phylip(phyfile)phyfile <- system.file("extdata", "sample.phy", package="treeio") read.phylip(phyfile)
read aligned sequences from phylip format
read.phylip.seq(file)read.phylip.seq(file)
file |
phylip file, currently only sequential format is supported |
DNAbin object
Guangchuang Yu
http://evolution.genetics.washington.edu/phylip/doc/sequence.html
parse tree from phylip file
read.phylip.tree(file)read.phylip.tree(file)
file |
phylip file |
phylo or multiPhylo object
Guangchuang Yu
read.phyloxml
read.phyloxml(file)read.phyloxml(file)
file |
phyloxml file |
treedata class or treedataList class
xmlfile1 <- system.file("extdata/phyloxml", "test_x2.xml", package="treeio") px1 <- read.phyloxml(xmlfile1) px1 xmlfile2 <- system.file("extdata/phyloxml", "phyloxml_examples.xml", package="treeio") px2 <- read.phyloxml(xmlfile2) px2xmlfile1 <- system.file("extdata/phyloxml", "test_x2.xml", package="treeio") px1 <- read.phyloxml(xmlfile1) px1 xmlfile2 <- system.file("extdata/phyloxml", "phyloxml_examples.xml", package="treeio") px2 <- read.phyloxml(xmlfile2) px2
parse output from r8s
read.r8s(file)read.r8s(file)
file |
r8s output log file |
multiPhylo object
Guangchuang Yu
read.r8s(system.file("extdata/r8s", "H3_r8s_output.log", package="treeio"))read.r8s(system.file("extdata/r8s", "H3_r8s_output.log", package="treeio"))
parse RAxML bootstrapping analysis output
read.raxml(file = NULL, text = NULL)read.raxml(file = NULL, text = NULL)
file |
RAxML bootstrapping analysis output |
text |
alternatively, the content of the file as a character string or a connection; 'file' is ignored when 'text' is given |
treedata object
Guangchuang Yu
raxml_file <- system.file("extdata/RAxML", "RAxML_bipartitionsBranchLabels.H3", package="treeio") read.raxml(raxml_file) read.raxml(text = "(a:0.1,b:0.2)90:0.3;")raxml_file <- system.file("extdata/RAxML", "RAxML_bipartitionsBranchLabels.H3", package="treeio") read.raxml(raxml_file) read.raxml(text = "(a:0.1,b:0.2)90:0.3;")
read.treeqza
read.treeqza(treeqza, node.label = "label", ...)read.treeqza(treeqza, node.label = "label", ...)
treeqza |
the qiime2 output file contained tree file. |
node.label |
parse node label as 'label' or 'support' value. |
... |
additional parameter, passed to 'read.tree'. |
phylo tree object or treedata object when node.label was parsed 'support'.
qzafile1 <- system.file("extdata/qiime2treeqza", "fasttree-tree.qza", package="treeio") qzafile2 <- system.file("extdata/qiime2treeqza", "iqt-tree.qza", package="treeio") qzafile3 <- system.file("extdata/qiime2treeqza", "raxml-cat-tree.qza", package="treeio") tr1 <- read.treeqza(qzafile1) tr1 tr2 <- read.treeqza(qzafile2) tr2 tr3 <- read.treeqza(qzafile3) tr3 # parse node label as 'support' value. qzafile4 <- system.file("extdata/qiime2treeqza", "raxml-cat-bootstrap-tree.qza", package="treeio") tr4 <- read.treeqza(qzafile4, node.label="support") tr4qzafile1 <- system.file("extdata/qiime2treeqza", "fasttree-tree.qza", package="treeio") qzafile2 <- system.file("extdata/qiime2treeqza", "iqt-tree.qza", package="treeio") qzafile3 <- system.file("extdata/qiime2treeqza", "raxml-cat-tree.qza", package="treeio") tr1 <- read.treeqza(qzafile1) tr1 tr2 <- read.treeqza(qzafile2) tr2 tr3 <- read.treeqza(qzafile3) tr3 # parse node label as 'support' value. qzafile4 <- system.file("extdata/qiime2treeqza", "raxml-cat-bootstrap-tree.qza", package="treeio") tr4 <- read.treeqza(qzafile4, node.label="support") tr4
read timetree output
read.treetime(file) read.timetree(file)read.treetime(file) read.timetree(file)
file |
the output tree file of timetree |
treedata object
rename tip label of phylogenetic tree
rename_taxa(tree, data, key = 1, value = 2)rename_taxa(tree, data, key = 1, value = 2)
tree |
tree object, either treedata or phylo |
data |
data frame |
key |
column in data that match tip label (use 1st column by default) |
value |
column in data for rename tip label (use 2nd column by default) |
tree object
Guangchuang Yu
tree <- rtree(3) d <- data.frame(old = paste0('t', 1:3), new = LETTERS[1:3]) rename_taxa(tree, d) rename_taxa(tree, d, old, new)tree <- rtree(3) d <- data.frame(old = paste0('t', 1:3), new = LETTERS[1:3]) rename_taxa(tree, d) rename_taxa(tree, d, old, new)
rescale branch length of tree object
rescale_tree(tree_object, branch.length)rescale_tree(tree_object, branch.length)
tree_object |
tree object |
branch.length |
numerical features (e.g. dN/dS) |
update tree object
Guangchuang Yu
spt method
spt(x, from, to, weights = NULL, ...)spt(x, from, to, weights = NULL, ...)
x |
a igraph object |
from |
a specific node of network. |
to |
other nodes of the network, length of it must be larger than 2. |
weights |
a numeric vector giving edge weights or a character.
If this is |
... |
additional parameters |
phylo object
library(igraph) set.seed(123) g <- igraph::sample_gnp(100, .1) %>% set_edge_attr(name='weight', value=abs(rnorm(E(.),3))) tr1 <- spt(g, from = 6, to=V(g), weights = 'weight') tr1 tr2 <- spt(g, from = 6, to = V(g), weights = NA) tr2library(igraph) set.seed(123) g <- igraph::sample_gnp(100, .1) %>% set_edge_attr(name='weight', value=abs(rnorm(E(.),3))) tr1 <- spt(g, from = 6, to=V(g), weights = 'weight') tr1 tr2 <- spt(g, from = 6, to = V(g), weights = NA) tr2
Export treedata object to BEAST NEXUS file. This function was adopted and modified from ape::write.nexus
write.beast(treedata, file = "", translate = TRUE, tree.name = NULL)write.beast(treedata, file = "", translate = TRUE, tree.name = NULL)
treedata |
|
file |
output file. If file = "", print the output content on screen |
translate |
whether to translate taxa labels |
tree.name |
names of the trees, NULL to use existing tree names |
output file or file content on screen
Guangchuang Yu
nhxfile <- system.file("extdata/NHX", "phyldog.nhx", package="treeio") nhx <- read.nhx(nhxfile) write.beast(nhx)nhxfile <- system.file("extdata/NHX", "phyldog.nhx", package="treeio") nhx <- read.nhx(nhxfile) write.beast(nhx)
Export treedata object to BEAST Newick file. This is useful for making BEAST starting trees with metadata
write.beast.newick( treedata, file = "", append = FALSE, digits = 10, tree.prefix = "" )write.beast.newick( treedata, file = "", append = FALSE, digits = 10, tree.prefix = "" )
treedata |
|
file |
output file. If file = "", print the output content on screen |
append |
logical. Only used if the argument 'file' is the name of file (and not a connection or "|cmd"). If 'TRUE' output will be appended to 'file'; otherwise, it will overwrite the contents of file. |
digits |
integer, the indicating the number of decimal places, default is 10. |
tree.prefix |
character the tree prefix, default is "". |
output file or file content on screen
Guangchuang Yu
nhxfile <- system.file("extdata/NHX", "phyldog.nhx", package="treeio") nhx <- read.nhx(nhxfile) write.beast.newick(nhx)nhxfile <- system.file("extdata/NHX", "phyldog.nhx", package="treeio") nhx <- read.nhx(nhxfile) write.beast.newick(nhx)
Export jplace object to jplace file.
write.jplace(x, outfile)write.jplace(x, outfile)
x |
a jplace object. |
outfile |
the output file name |
jp <- system.file("extdata", "sample.jplace", package="treeio") tr1 <- read.jplace(jp) outfile <- tempfile() write.jplace(tr1, outfile) tr2 <- read.jplace(outfile) tr2jp <- system.file("extdata", "sample.jplace", package="treeio") tr1 <- read.jplace(jp) outfile <- tempfile() write.jplace(tr1, outfile) tr2 <- read.jplace(outfile) tr2
Export treedata object to json tree file
write.jtree(treedata, file = "")write.jtree(treedata, file = "")
treedata |
|
file |
output file. If file = "", print the output content on screen |
output file or file content on screen
Guangchuang Yu