Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

reorder.phylo

Internal Reordering of Trees


Description

reorder changes the internal structure of a phylogeny stored as an object of class "phylo". The tree returned is the same than the one input, but the ordering of the edges could be different.

cladewise and postorder are convenience functions to return only the indices of the reordered edge matrices (see examples).

Usage

## S3 method for class 'phylo'
reorder(x, order = "cladewise", index.only = FALSE, ...)
## S3 method for class 'multiPhylo'
reorder(x, order = "cladewise", ...)
cladewise(x)
postorder(x)

Arguments

x

an object of class "phylo" or "multiPhylo".

order

a character string: either "cladewise" (the default), "postorder", "pruningwise", or any unambiguous abbreviation of these.

index.only

should the function return only the ordered indices of the rows of the edge matrix?

...

further arguments passed to or from other methods.

Details

Because in a tree coded as an object of class "phylo" each branch is represented by a row in the element ‘edge’, there is an arbitrary choice for the ordering of these rows. reorder allows to reorder these rows according to three rules: in the "cladewise" order each clade is formed by a series of contiguous rows. In the "postorder" order, the rows are arranged so that computations following pruning-like algorithm the tree (or postorder tree traversal) can be done by descending along these rows (conversely, a preorder tree traversal can be performed by moving from the last to the first row). The "pruningwise" order is an alternative “pruning” order which is actually a bottom-up traversal order (Valiente 2002). (This third choice might be removed in the future as it merely duplicates the second one which is more efficient.) The possible multichotomies and branch lengths are preserved.

Note that for a given order, there are several possible orderings of the rows of ‘edge’.

Value

an object of class "phylo" (with the attribute "order" set accordingly), or a numeric vector if index.only = TRUE; if x is of class "multiPhylo", then an object of the same class.

Author(s)

Emmanuel Paradis

References

Valiente, G. (2002) Algorithms on Trees and Graphs. New York: Springer.

See Also

read.tree to read tree files in Newick format, reorder for the generic function

Examples

data(bird.families)
tr <- reorder(bird.families, "postorder")
all.equal(bird.families, tr) # uses all.equal.phylo actually
all.equal.list(bird.families, tr) # bypasses the generic

## get the number of descendants for each tip or node:
nr_desc <-  function(x) {
    res <- numeric(max(x$edge))
    res[1:Ntip(x)] <- 1L
    for (i in postorder(x)) {
       tmp <- x$edge[i,1]
       res[tmp] <- res[tmp] + res[x$edge[i, 2]]
   }
   res
}
## apply it to a random tree:
tree <- rtree(10)
plot(tree, show.tip.label = FALSE)
tiplabels()
nodelabels()
nr_desc(tree)

ape

Analyses of Phylogenetics and Evolution

v5.5
GPL-2 | GPL-3
Authors
Emmanuel Paradis [aut, cre, cph] (<https://orcid.org/0000-0003-3092-2199>), Simon Blomberg [aut, cph] (<https://orcid.org/0000-0003-1062-0839>), Ben Bolker [aut, cph] (<https://orcid.org/0000-0002-2127-0443>), Joseph Brown [aut, cph] (<https://orcid.org/0000-0002-3835-8062>), Santiago Claramunt [aut, cph] (<https://orcid.org/0000-0002-8926-5974>), Julien Claude [aut, cph] (<https://orcid.org/0000-0002-9267-1228>), Hoa Sien Cuong [aut, cph], Richard Desper [aut, cph], Gilles Didier [aut, cph] (<https://orcid.org/0000-0003-0596-9112>), Benoit Durand [aut, cph], Julien Dutheil [aut, cph] (<https://orcid.org/0000-0001-7753-4121>), RJ Ewing [aut, cph], Olivier Gascuel [aut, cph], Thomas Guillerme [aut, cph] (<https://orcid.org/0000-0003-4325-1275>), Christoph Heibl [aut, cph] (<https://orcid.org/0000-0002-7655-3299>), Anthony Ives [aut, cph] (<https://orcid.org/0000-0001-9375-9523>), Bradley Jones [aut, cph] (<https://orcid.org/0000-0003-4498-1069>), Franz Krah [aut, cph] (<https://orcid.org/0000-0001-7866-7508>), Daniel Lawson [aut, cph] (<https://orcid.org/0000-0002-5311-6213>), Vincent Lefort [aut, cph], Pierre Legendre [aut, cph] (<https://orcid.org/0000-0002-3838-3305>), Jim Lemon [aut, cph], Guillaume Louvel [aut, cph] (<https://orcid.org/0000-0002-7745-0785>), Eric Marcon [aut, cph] (<https://orcid.org/0000-0002-5249-321X>), Rosemary McCloskey [aut, cph] (<https://orcid.org/0000-0002-9772-8553>), Johan Nylander [aut, cph], Rainer Opgen-Rhein [aut, cph], Andrei-Alin Popescu [aut, cph], Manuela Royer-Carenzi [aut, cph], Klaus Schliep [aut, cph] (<https://orcid.org/0000-0003-2941-0161>), Korbinian Strimmer [aut, cph] (<https://orcid.org/0000-0001-7917-2056>), Damien de Vienne [aut, cph] (<https://orcid.org/0000-0001-9532-5251>)
Initial release
2021-04-24

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.