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/

Reply via email to