Re: [R-sig-phylo] collapse descendants of a node to a polytomy

2013-06-22 Thread David Bapst
Hi John,

I'm not familiar with any such function either. However, a handy trick
for doing this sort of thing is changing the branch lengths of
descendant nodes to zero and using the ape function di2multi to
collapse those edges, creating a polytomy.

Now, it isn't clear to me from your email whether you want to collapse
just all the edges immediately descended from your selected node or
all the branches descended from that node. Here's a worked example for
both cases:

library(ape)
set.seed(444)
tree-rtree(10)
plot(tree)
node-mrca(tree)[t4,t2]

#collapse immediate branches
tree1-tree
tree1$edge.length-rep(1,
Nedge(tree1))#replace all edge lengths with 1
tree1$edge.length[tree1$edge[,1]==node]-0#replace descendent edge
lengths with 0
tree1-di2multi(tree1)
tree1$edge.length-NULL#get rid of branch lengths

plot(tree1)

#collapse all descendant branches
tree2-tree
library(phangorn)
descNodes-Descendants(tree2,node,all)
tree2$edge.length-rep(1,Nedge(tree2))#replace all edge lengths with 1
descEdges-sapply(tree2$edge[,2],function(x) any(x==descNodes))
tree2$edge.length[descEdges]-0
tree2-di2multi(tree2)
tree2$edge.length-NULL#get rid of branch lengths

plot(tree2)

The only downside is that this distorts the edge lengths of the
original tree, such that I just remove the edge lengths in the end.
Partly, this is because I initially replace all the edges with length
1, so di2multi didn't collapse any particularly short branches
(shorter than di2multi's threshold) that might have existed
originally. However, even without doing that safety measure, I dropped
the edge lengths mostly because we have to take some edges and
dropping them to length zero. The tips descended from that node will
be pulled back toward the root, which is simply artificial and why I
think its best practice to just get rid of the edge lengths.

An alternative to getting rid of the edge lengths would be to adjust
the distance between the tips and the new polytomy so that the clade
is the same depth as the original node, essentially fixing the tips in
place. That's a little more complicated (and I don't know of any
function that does it, although Liam's bind.tip in phytools does
something similar, by default, for adding a new tip). You didn't say
anything about whether you wanted to retain edge.length information,
so I won't go into further into that.

Cheers from Snowbird,
-Dave Bapst

On Fri, Jun 21, 2013 at 8:11 PM, John Denton jden...@amnh.org wrote:
 Hi folks,

 I'd like to collapse the descendants of a node, identified using something 
 like node - mrca(tree)[A, B]. I did not see a function in ape, geiger, 
 phyloch, or picante to do something like collapse.descendants(node). Is there 
 a package with a function like this?

 Thanks!

 ~John


 John S. S. Denton
 Ph.D. Candidate
 Department of Ichthyology and Richard Gilder Graduate School
 American Museum of Natural History
 www.johnssdenton.com
 ___
 R-sig-phylo mailing list - R-sig-phylo@r-project.org
 https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
 Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/



-- 
David W. Bapst, PhD
Adjunct Asst. Professor, Geology and Geol. Eng.
South Dakota School of Mines and Technology
501 E. St. Joseph
Rapid City, SD 57701

http://home.uchicago.edu/~dwbapst/
http://cran.r-project.org/web/packages/paleotree/index.html

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


Re: [R-sig-phylo] collapse descendants of a node to a polytomy

2013-06-22 Thread John Denton
Hi Liam and David,

Thanks for the code! It will be very useful for the trees I'm working with. Two 
clades are large, and I need to collapse them for diversitree.

Best,

John

John S. S. Denton
Ph.D. Candidate
Department of Ichthyology and Richard Gilder Graduate School
American Museum of Natural History
www.johnssdenton.com

From: Liam J. Revell [liam.rev...@umb.edu]
Sent: Saturday, June 22, 2013 8:09 AM
To: John Denton
Cc: David Bapst; r-sig-phylo@r-project.org
Subject: Re: [R-sig-phylo] collapse descendants of a node to a polytomy

Hi John.

Here is an alternative to David's second solution that also retains the
branch lengths everywhere else in the tree  the height above the root
for the tips in the star subtree. It uses phytools (and its dependencies).

collapse.to.star-function(tree,node){
tt-splitTree(tree,split=list(node=node,bp=tree$edge.length[which(tree$edge[,2]==node)]))
ss-starTree(species=tt[[2]]$tip.label,branch.lengths=diag(vcv(tt[[2]])))
ss$root.edge-0
tree-paste.tree(tt[[1]],ss)
return(tree)
}

# e.g.,
library(phytools)
set.seed(1)
tree-pbtree(n=30)
plotTree(tree,node.numbers=T)
node-fastMRCA(tree,t15,t5)
tree2-collapse.to.star(tree,node)
plotTree(tree2)


Liam J. Revell, Assistant Professor of Biology
University of Massachusetts Boston
web: http://faculty.umb.edu/liam.revell/
email: liam.rev...@umb.edu
blog: http://blog.phytools.org

On 6/22/2013 5:21 AM, David Bapst wrote:
 library(ape)
 set.seed(444)
 tree-rtree(10)
 plot(tree)
 node-mrca(tree)[t4,t2]

 #collapse immediate branches
 tree1-tree
 tree1$edge.length-rep(1,
 Nedge(tree1))#replace all edge lengths with 1
 tree1$edge.length[tree1$edge[,1]==node]-0#replace descendent edge
 lengths with 0
 tree1-di2multi(tree1)
 tree1$edge.length-NULL#get rid of branch lengths

 plot(tree1)

 #collapse all descendant branches
 tree2-tree
 library(phangorn)
 descNodes-Descendants(tree2,node,all)
 tree2$edge.length-rep(1,Nedge(tree2))#replace all edge lengths with 1
 descEdges-sapply(tree2$edge[,2],function(x) any(x==descNodes))
 tree2$edge.length[descEdges]-0
 tree2-di2multi(tree2)
 tree2$edge.length-NULL#get rid of branch lengths

 plot(tree2)

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