/working/auteur.extended/R/get.descendants.of.node.R

http://github.com/eastman/auteur · R · 31 lines · 20 code · 7 blank · 4 comment · 4 complexity · de7cf87cc44a14a99c2909711346adb2 MD5 · raw file

  1. #general phylogenetic utility for returning the first (usually, unless a polytomy exists) two descendants the supplied node
  2. #author: JM EASTMAN 2010
  3. get.desc.of.node <-
  4. function(node, phy) {
  5. return(phy$edge[which(phy$edge[,1]==node),2])
  6. }
  7. #general phylogenetic utility for returning all descendants (listed as given in phy$edge[,2]) of a node (excluding the supplied node)
  8. #author: JM EASTMAN 2010
  9. get.descendants.of.node <-
  10. function(node, phy, tips=FALSE) {
  11. storage=list()
  12. if(is.null(node)) node=phy$edge[1,1]
  13. if(node%in%phy$edge[,1]) {
  14. desc=c(sapply(node, function(x) {return(phy$edge[which(phy$edge[,1]==x),2])}))
  15. while(any(desc%in%phy$edge[,1])) {
  16. desc.true=desc[which(desc%in%phy$edge[,1])]
  17. desc.desc=lapply(desc.true, function(x) return(get.desc.of.node(x, phy)))
  18. storage=list(storage,union(desc,desc.desc))
  19. desc=unlist(desc.desc)
  20. }
  21. storage=as.numeric(sort(unique(unlist(union(desc,storage)))))
  22. }
  23. if(tips) return(storage[storage<=Ntip(phy)]) else return(storage)
  24. }