Discussion:
[R-sig-phylo] rotate nodes to match taxonomic order
Jacob Berv
2015-04-08 04:41:06 UTC
Permalink
Hi all,

Is there an easy way to get R to automatically rotate the nodes of a phylogeny to match an arbitrary ordering of the tips?

Consider the following two situations -

Situation 1:

Say I have a particular taxonomic order, such as

SpeciesA, SpeciesC, SpeciesB

And I want to rotate the nodes of ((C,B),A) to match it - ie to automatically rotate the nodes to give (A(C,B))


Situation 2:

Say I have tree 1 ((C,B),A) and I want to rotate it’s nodes to match the order of tree 2 (A,(B,C))

Currently the only way I know how to accomplish either scenario is to use the ape function rotate() on each relevant node, but this quickly becomes a very tedious task when you have hundreds of nodes to go through and want to achieve a particular ordering.

Any thoughts or tips on how to accomplish either of the two scenarios I describe above in a generalizable way that scales to hundreds of tips/nodes?

There is an example in the rotate() documenatation using rotateConstr():
# a simple example for rotateConstr:
A <- read.tree(text = "((A,B),(C,D));")
B <- read.tree(text = "(((D,C),B),A);")
B <- rotateConstr(B, A$tip.label)
plot(A); plot(B, d = "l")

But this doesn’t seem to work when I try it on my larger trees. Not sure where I’m going wrong…

Best,

Jacob Berv

Ph.D. Student
Lovette Lab
Cornell Laboratory of Ornithology


[[alternative HTML version deleted]]

_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-phylo@
Liam J. Revell
2015-04-08 12:51:18 UTC
Permalink
Hi Jacob.

There is a function in phytools that does something like this. It is
called minRotate and it was designed to be used internally by
phylo.to.map but can also be used on its own. I have blogged about your
question and the function here:
http://blog.phytools.org/2015/04/finding-closest-set-of-node-rotations.html.
Let us know if this seems to do what you are attempting.

All the best, Liam

Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
email: ***@umb.edu
blog: http://blog.phytools.org
Post by Jacob Berv
Is there an easy way to get R to automatically rotate the nodes of a phylogeny to match an arbitrary ordering of the tips?
Consider the following two situations -
Say I have a particular taxonomic order, such as
SpeciesA, SpeciesC, SpeciesB
And I want to rotate the nodes of ((C,B),A) to match it - ie to automatically rotate the nodes to give (A(C,B))
_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-***@r-project.org/
Emmanuel Paradis
2015-04-08 14:57:24 UTC
Permalink
Hi Jacob,

Can you send an example of your output with rotateConstr()? Thanks.

Best,

Emmanuel
Post by Jacob Berv
Hi all,
Is there an easy way to get R to automatically rotate the nodes of a phylogeny to match an arbitrary ordering of the tips?
Consider the following two situations -
Say I have a particular taxonomic order, such as
SpeciesA, SpeciesC, SpeciesB
And I want to rotate the nodes of ((C,B),A) to match it - ie to automatically rotate the nodes to give (A(C,B))
Say I have tree 1 ((C,B),A) and I want to rotate it’s nodes to match the order of tree 2 (A,(B,C))
Currently the only way I know how to accomplish either scenario is to use the ape function rotate() on each relevant node, but this quickly becomes a very tedious task when you have hundreds of nodes to go through and want to achieve a particular ordering.
Any thoughts or tips on how to accomplish either of the two scenarios I describe above in a generalizable way that scales to hundreds of tips/nodes?
A <- read.tree(text = "((A,B),(C,D));")
B <- read.tree(text = "(((D,C),B),A);")
B <- rotateConstr(B, A$tip.label)
plot(A); plot(B, d = "l")
But this doesn’t seem to work when I try it on my larger trees. Not sure where I’m going wrong…
Best,
Jacob Berv
Ph.D. Student
Lovette Lab
Cornell Laboratory of Ornithology
[[alternative HTML version deleted]]
_______________________________________________
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-***@r-project.org
Jacob Berv
2015-04-08 16:15:27 UTC
Permalink
The rotateConstr() function does not give me any errors -

Here is how I was trying to use it:

With a 200 taxon tree (tree A), I rotated ~50 nodes using rotate() to get it how I wanted.

With a mostly similar (topologically identical, but different branch lengths, so node numbers are not exactly the same as in tree A) 200 taxon tree (tree B), I tried:

newtree <- rotateConstr(treeB, treeA$tip.label)

this produced a newtree but the ordering looks nothing like treeA - it’s mostly jumbled. The root is changed etc…

Is this the incorrect use of rotateConstr()?

Jake
Post by Emmanuel Paradis
Hi Jacob,
Can you send an example of your output with rotateConstr()? Thanks.
Best,
Emmanuel
Post by Jacob Berv
Hi all,
Is there an easy way to get R to automatically rotate the nodes of a phylogeny to match an arbitrary ordering of the tips?
Consider the following two situations -
Say I have a particular taxonomic order, such as
SpeciesA, SpeciesC, SpeciesB
And I want to rotate the nodes of ((C,B),A) to match it - ie to automatically rotate the nodes to give (A(C,B))
Say I have tree 1 ((C,B),A) and I want to rotate it’s nodes to match the order of tree 2 (A,(B,C))
Currently the only way I know how to accomplish either scenario is to use the ape function rotate() on each relevant node, but this quickly becomes a very tedious task when you have hundreds of nodes to go through and want to achieve a particular ordering.
Any thoughts or tips on how to accomplish either of the two scenarios I describe above in a generalizable way that scales to hundreds of tips/nodes?
A <- read.tree(text = "((A,B),(C,D));")
B <- read.tree(text = "(((D,C),B),A);")
B <- rotateConstr(B, A$tip.label)
plot(A); plot(B, d = "l")
But this doesn’t seem to work when I try it on my larger trees. Not sure where I’m going wrong…
Best,
Jacob Berv
Ph.D. Student
Lovette Lab
Cornell Laboratory of Ornithology
[[alternative HTML version deleted]]
_______________________________________________
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-sig-***@r-pro
Emmanuel Paradis
2015-04-08 17:08:49 UTC
Permalink
Strange... here's some code that seems to do what you describe but I
don't see something unexpected in the output.

A <- rcoal(200)
B <- A
NODES <- Ntip(B) + seq_len(B$Nnode)
for (i in 1:100) B <- rotate(B, sample(NODES, size = 1))
## to avoid line-crossing when plotting:
B <- read.tree(text = write.tree(B))

all.equal(A, B) # TRUE

C <- rotateConstr(B, A$tip.label)

all.equal(A, C) # TRUE
identical(A, C) # FALSE

par(mfcol = c(2, 2), mar = rep(0, 4))
plot(A)
plot(B)
plot(C, d = "l")

C and A appear identical on the plot.

Best,

Emmanuel
Post by Jacob Berv
The rotateConstr() function does not give me any errors -
With a 200 taxon tree (tree A), I rotated ~50 nodes using rotate() to get it how I wanted.
newtree <- rotateConstr(treeB, treeA$tip.label)
this produced a newtree but the ordering looks nothing like treeA - it’s mostly jumbled. The root is changed etc…
Is this the incorrect use of rotateConstr()?
Jake
Post by Emmanuel Paradis
Hi Jacob,
Can you send an example of your output with rotateConstr()? Thanks.
Best,
Emmanuel
Post by Jacob Berv
Hi all,
Is there an easy way to get R to automatically rotate the nodes of a phylogeny to match an arbitrary ordering of the tips?
Consider the following two situations -
Say I have a particular taxonomic order, such as
SpeciesA, SpeciesC, SpeciesB
And I want to rotate the nodes of ((C,B),A) to match it - ie to automatically rotate the nodes to give (A(C,B))
Say I have tree 1 ((C,B),A) and I want to rotate it’s nodes to match the order of tree 2 (A,(B,C))
Currently the only way I know how to accomplish either scenario is to use the ape function rotate() on each relevant node, but this quickly becomes a very tedious task when you have hundreds of nodes to go through and want to achieve a particular ordering.
Any thoughts or tips on how to accomplish either of the two scenarios I describe above in a generalizable way that scales to hundreds of tips/nodes?
A <- read.tree(text = "((A,B),(C,D));")
B <- read.tree(text = "(((D,C),B),A);")
B <- rotateConstr(B, A$tip.label)
plot(A); plot(B, d = "l")
But this doesn’t seem to work when I try it on my larger trees. Not sure where I’m going wrong…
Best,
Jacob Berv
Ph.D. Student
Lovette Lab
Cornell Laboratory of Ornithology
[[alternative HTML version deleted]]
_______________________________________________
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.ma
Jacob Berv
2015-04-08 18:11:15 UTC
Permalink
Emmanuel,
Thanks! I actually realized that the reason this wasn’t working was because there were a few tiny differences between the taxon names in my two trees. Once I corrected this the rotateConstr() function works great, as does Liam’s solution. Thanks both!

Jake
Strange... here's some code that seems to do what you describe but I don't see something unexpected in the output.
A <- rcoal(200)
B <- A
NODES <- Ntip(B) + seq_len(B$Nnode)
for (i in 1:100) B <- rotate(B, sample(NODES, size = 1))
B <- read.tree(text = write.tree(B))
all.equal(A, B) # TRUE
C <- rotateConstr(B, A$tip.label)
all.equal(A, C) # TRUE
identical(A, C) # FALSE
par(mfcol = c(2, 2), mar = rep(0, 4))
plot(A)
plot(B)
plot(C, d = "l")
C and A appear identical on the plot.
Best,
Emmanuel
Post by Jacob Berv
The rotateConstr() function does not give me any errors -
With a 200 taxon tree (tree A), I rotated ~50 nodes using rotate() to get it how I wanted.
newtree <- rotateConstr(treeB, treeA$tip.label)
this produced a newtree but the ordering looks nothing like treeA - it’s mostly jumbled. The root is changed etc…
Is this the incorrect use of rotateConstr()?
Jake
Post by Emmanuel Paradis
Hi Jacob,
Can you send an example of your output with rotateConstr()? Thanks.
Best,
Emmanuel
Post by Jacob Berv
Hi all,
Is there an easy way to get R to automatically rotate the nodes of a phylogeny to match an arbitrary ordering of the tips?
Consider the following two situations -
Say I have a particular taxonomic order, such as
SpeciesA, SpeciesC, SpeciesB
And I want to rotate the nodes of ((C,B),A) to match it - ie to automatically rotate the nodes to give (A(C,B))
Say I have tree 1 ((C,B),A) and I want to rotate it’s nodes to match the order of tree 2 (A,(B,C))
Currently the only way I know how to accomplish either scenario is to use the ape function rotate() on each relevant node, but this quickly becomes a very tedious task when you have hundreds of nodes to go through and want to achieve a particular ordering.
Any thoughts or tips on how to accomplish either of the two scenarios I describe above in a generalizable way that scales to hundreds of tips/nodes?
A <- read.tree(text = "((A,B),(C,D));")
B <- read.tree(text = "(((D,C),B),A);")
B <- rotateConstr(B, A$tip.label)
plot(A); plot(B, d = "l")
But this doesn’t seem to work when I try it on my larger trees. Not sure where I’m going wrong…
Best,
Jacob Berv
Ph.D. Student
Lovette Lab
Cornell Laboratory of Ornithology
[[alternative HTML version deleted]]
_______________________________________________
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Jacob Berv

Ph.D. Student
Lovette Lab
Cornell Laboratory of Ornithology

_______________________________________________
R-sig-phylo mailing list - R-sig-***@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
Searchable archive at http://www.mail-archive.com/r-si

Loading...