]> git.donarmstrong.com Git - ape.git/blob - R/subtrees.R
changes in reorder(, "cladewise")
[ape.git] / R / subtrees.R
1 ## subtrees.R (2008-04-14)
2
3 ##  All subtrees of a Phylogenetic Tree
4
5 ## Copyright 2008 Damien de Vienne
6
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
9
10 subtrees<-function(tree, wait = FALSE)
11 {
12 N.tip<-Ntip (tree)
13 N.node<-Nnode(tree)
14 limit<-N.tip+N.node
15 sub<-list(N.node)
16 u<-0
17
18   for (k in (N.tip+1):limit)
19   {
20  u<-u+1
21         if (wait==TRUE) cat("wait... Node",u,"out of", N.node, "treated\n")
22   fils<-NULL
23   pere<-res <- k
24         repeat
25         {
26         for (i in 1: length(pere)) fils<-c(fils, tree$edge[,2][tree$edge[,1]==pere[i]])
27         res<-c(res, fils)
28       pere<-fils
29         fils<-NULL
30         if (length(pere)==0) break
31         }
32
33   len<-res[res>N.tip]
34    if (u==1) {
35         tree2<-tree
36         len<-(N.tip+1):limit
37         }
38    else {
39   len.tip<-res[res<N.tip+1]
40   vec<-1:length(tree$tip.label)
41   len.tip.stay<-setdiff(vec, len.tip)
42   tree2<-drop.tip(tree, len.tip.stay)
43           }
44   sub[[u]]<-tree2
45   sub[[u]]$name<-k
46   #sub[[u]]$name<-tree2$node.label[1]
47   sub[[u]]$Ntip<-Ntip(tree2)
48   sub[[u]]$Nnode<-Nnode(tree2)
49   if (is.null(tree$node.label))
50         sub[[u]]$node.label<-len
51
52   }
53 return(sub)
54 cat("\n")
55 }