PageRenderTime 20ms CodeModel.GetById 14ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

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