I'm sure Florian's solution works, though I haven't tried it.
Here is another one that may work that I just posted to my blog: http://blog.phytools.org/2016/09/collapsing-clades-of-foo-huh.html All the best, Liam Liam J. Revell, Associate 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 9/15/2016 1:57 AM, Florian Boucher wrote:
I don't really know what you mean by 'hard-coding', but here is what I could think of (it is probably not optimal, but should work pretty quickly if your tree does not have thousands of tips): First, borrow 'getDescendants' from phytools: getDescendants<-function(tree,node,curr=NULL){ if(is.null(curr)) curr<-vector() daughters<-tree$edge[which(tree$edge[,1]==node),2] curr<-c(curr,daughters) w<-which(daughters>=length(tree$tip)) if(length(w)>0) for(i in 1:length(w)) curr<-getDescendants(tree,daughters[w[i]],curr) return(curr) } Then, traverse the tree and check whether each clade contains only tips named 'foo'. If this is the case, rename the first tip and label others 'to_rm'. # assuming your tree is dichotomic, tips are numbered 1:ntips and internal nodes (ntips+1):(2*ntips-1) ntips=length(tree$tip.label) reorder.phylo(tree,order='cladewise') # to make sure that we always traverse the tree from root to tips for (i in c(ntips+1):(2*ntips-1)){ des= getDescendants(tree,node=i,curr=NULL) tips=des[which(des<(ntips+1))] ; intern=des[-which(des<(ntips+1))] if (all(tree$tip.label[tips]=='foo')){ tree$tip.label[tips[1]]=paste(length(tips),"foo's",sep='_') tree$tip.label[tips[2:length(tips)]]='to_rm' } } plot(tree) # remove tips labelled 'to_rm' tree2=drop.tip(tree,tip='to_rm') plot(tree2) Let's hope this works as you wished. Cheers, Florian 2016-09-14 22:04 GMT+02:00 branchlizard . <branch.liz...@gmail.com <mailto:branch.liz...@gmail.com>>: Florian and list, What is your preferred method to go about this? phy$tip.label? If so, how would one label a tip label from each clade of foo's without having to hard code the clade number? I am trying to prevent any hard coding. BL On Wed, Sep 14, 2016 at 3:46 PM, Florian Boucher <floflobouc...@gmail.com <mailto:floflobouc...@gmail.com>> wrote: Hi Branchlizard and list, in order to do this you would first need to rename one of the foo's in each clade (I would always rename the first one) as '6 foo's', '4 foo's', etc. Then you can apply drop.tip on all the foos, as you did before. I hope this helps. Cheers, Florian 2016-09-14 21:32 GMT+02:00 branchlizard . <branch.liz...@gmail.com <mailto:branch.liz...@gmail.com>>: I would like to turn this http://i.imgur.com/chLdFmZ.jpg into this http://i.imgur.com/vSoe6mu.jpg My dataset and phylogeny is much more complex than this, but this is the basic idea. BL On Mon, Sep 12, 2016 at 8:16 PM, Liam J. Revell <liam.rev...@umb.edu <mailto:liam.rev...@umb.edu>> wrote: > I'm sure this is possible, but I really don't understand the question. > Maybe you could draw what you have in mind on a piece of paper and post a > picture of the paper.... > > All the best, Liam > > Liam J. Revell, Associate Professor of Biology > University of Massachusetts Boston > web: http://faculty.umb.edu/liam.revell/ <http://faculty.umb.edu/liam.revell/> > email: liam.rev...@umb.edu <mailto:liam.rev...@umb.edu> > blog: http://blog.phytools.org > > > On 9/12/2016 2:46 PM, branchlizard . wrote: > >> I have posted this question at Stack Overflow. I hope this doesn't violate >> any community rules about double posting. >> >> I probably could have worded the title better, but I am wanting to >> collapse >> any clade within a phylogenetic tree (even if the clade has one member) >> which has a tip label of "foo" and then count the number of tips which >> were >> dropped from that specific clade and create a branch with a tip label >> displaying 35 foos. >> >> The counting portion is easy; however, when I use >> >> drop.tip(rooted.tree,tip=which(rooted.tree$tip.label=='foo') >> ,subtree=TRUE) >> >> the dropped tips do not maintain their position in the tree. Rather, they >> are all grouped at the end (counted properly however). Is there anyway to >> collapse a clade by tip labels and maintain its position >> >> >> >> BranchLizard >> >> [[alternative HTML version deleted]] >> >> _______________________________________________ >> R-sig-phylo mailing list - R-sig-phylo@r-project.org <mailto:R-sig-phylo@r-project.org> >> https://stat.ethz.ch/mailman/listinfo/r-sig-phylo <https://stat.ethz.ch/mailman/listinfo/r-sig-phylo> >> Searchable archive at http://www.mail-archive.com/r- >> sig-ph...@r-project.org/ <http://sig-ph...@r-project.org/> >> >> [[alternative HTML version deleted]] _______________________________________________ R-sig-phylo mailing list - R-sig-phylo@r-project.org <mailto:R-sig-phylo@r-project.org> https://stat.ethz.ch/mailman/listinfo/r-sig-phylo <https://stat.ethz.ch/mailman/listinfo/r-sig-phylo> Searchable archive at http://www.mail-archive.com/r-sig-phylo@r-project.org/ <http://www.mail-archive.com/r-sig-phylo@r-project.org/>
_______________________________________________ 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/