S4 class for representing phylogenetic trees as a list of nodes.
Usage
# S4 method for class 'TreeMan,character'
x[[i]]
# S4 method for class 'TreeMan,character,missing,missing'
x[i, j, ..., drop = TRUE]
# S4 method for class 'TreeMan'
as.character(x)
# S4 method for class 'TreeMan'
show(object)
# S4 method for class 'TreeMan'
print(x)
# S4 method for class 'TreeMan'
str(object, max.level = 2L, ...)
# S4 method for class 'TreeMan'
summary(object)
# S4 method for class 'TreeMan'
cTrees(x, ...)Arguments
- x
TreeManobject- i
node ID or slot name
- j
missing
- ...
additional tree objects
- drop
missing
- object
TreeManobject- max.level
str()maximum number of levels to show
Details
A TreeMan object holds a list of nodes. The idea of the TreeMan
class is to make adding and removing nodes as similar as possible to adding
and removing elements in a list. Note that internal nodes and tips are
both considered nodes. Trees can be polytomous but not unrooted.
Each node within the TreeMan ndlst contains the following data slots:
id: character string for the node IDtxnym: name of taxonomic clade (optional)spn: length of the preceding branchprid: ID of the immediately preceding node, NULL if rootptid: IDs of the immediately connecting nodes
See below in 'Examples' for these methods in use.
Slots
ndlstlist of nodes
ndsvector of node ids that are internal nodes
nndsnumeric of number of internal nodes in tree
tipsvector of node ids that are tips
ntipsnumeric of number of internal nodes in tree
allvector of all node ids
nallnumeric of number of all nodes in tree
pdnumeric of total branch length of tree
tindsindexes of all tip nodes in tree
prindsindexes of all pre-nodes in tree
wspnlogical, do nodes have spans
wtxnymslogical, do nodes have txnyms
plylogical, is tree bifurcating
rootcharacter of node id of root, if no root then empty character
updtdlogical, if tree slots have been updated since initiation or change
othr_slt_nmsvector, character list of additional data slots added to nodes
ndmtrxmatrix, T/Fs representing tree structure
Examples
# Generate random tree
tree <- randTree(10)
# Print to get basic stats
summary(tree)
#> Tree (TreeMan Object):
#> + 10 tips
#> + 9 internal nodes
#> + Binary
#> + PD 9.62
#> + Root node is "n1"
# Slots....
tree["tips"] # return all tips IDs
#> [1] "t1" "t10" "t2" "t3" "t4" "t5" "t6" "t7" "t8" "t9"
tree["nds"] # return all internal node IDs
#> [1] "n1" "n2" "n3" "n4" "n5" "n6" "n7" "n8" "n9"
tree["ntips"] # count all tips
#> [1] 10
tree["nnds"] # count all internal nodes
#> [1] 9
tree["root"] # identify root node
#> [1] "n1"
tree[["t1"]] # return t1 node object
#> Node (tip node):
#> + ID: "t1"
#> + prid: "n3"
#> + spn: 0.77
#> + predist: 0.99
#> + pd: 0
tree["pd"] # return phylogenetic diversity
#> [1] 9.615549
tree["ply"] # is polytomous?
#> [1] FALSE
# Additional special slots (calculated upon call)
tree["age"] # get tree's age
#> [1] 3.093546
tree["ultr"] # determine if tree is ultrametric
#> [1] FALSE
tree["spns"] # get all the spans of the tree IDs
#> n1 n2 n3 n4 n5 n6 n7
#> 0.00000000 0.18033877 0.21689988 0.68016292 0.49884561 0.64167935 0.66028435
#> n8 n9 t1 t2 t3 t4 t5
#> 0.09602416 0.76560016 0.76967480 0.99071231 0.97052090 0.38918276 0.46118646
#> t6 t7 t8 t9 t10
#> 0.31524175 0.17467589 0.53157354 0.49363702 0.77930863
tree["prids"] # get all the IDs of preceding nodes
#> n1 n2 n3 n4 n5 n6 n7 n8 n9 t1 t2 t3 t4 t5 t6 t7
#> "n1" "n1" "n1" "n2" "n3" "n5" "n4" "n6" "n6" "n3" "n8" "n9" "n9" "n7" "n4" "n7"
#> t8 t9 t10
#> "n2" "n8" "n5"
tree["ptids"] # get all the IDs of following nodes
#> $n1
#> [1] "n2" "n3"
#>
#> $n2
#> [1] "n4" "t8"
#>
#> $n3
#> [1] "n5" "t1"
#>
#> $n4
#> [1] "n7" "t6"
#>
#> $n5
#> [1] "n6" "t10"
#>
#> $n6
#> [1] "n8" "n9"
#>
#> $n7
#> [1] "t5" "t7"
#>
#> $n8
#> [1] "t2" "t9"
#>
#> $n9
#> [1] "t3" "t4"
#>
#> $t1
#> character(0)
#>
#> $t2
#> character(0)
#>
#> $t3
#> character(0)
#>
#> $t4
#> character(0)
#>
#> $t5
#> character(0)
#>
#> $t6
#> character(0)
#>
#> $t7
#> character(0)
#>
#> $t8
#> character(0)
#>
#> $t9
#> character(0)
#>
#> $t10
#> character(0)
#>
tree["txnyms"] # get all the taxonyms of all nodes
#> n1 n2 n3 n4 n5 n6 n7 n8 n9 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10
#> NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
# In addition [] can be used for any user-defined slot
# Because all nodes are lists with metadata we can readily
# get specific information on nodes of interest
nd <- tree[["n2"]]
summary(nd)
#> Node (internal node):
#> + ID: "n2"
#> + prid: "n1"
#> + ptid: "n4", "t8"
#> + nkids: 4
#> + spn: 0.18
#> + predist: 0.18
#> + pd: 2.8
# And then use the same syntax for the tree
nd["nkids"] # .... nkids, pd, etc.
#> [1] 4
# Convert to phylo and plot
library(ape)
tree <- as(tree, "phylo")
plot(tree)
