]> git.donarmstrong.com Git - ape.git/blob - src/tree_phylo.c
63f677de2ce61e7130138c962ad75805a9efd660
[ape.git] / src / tree_phylo.c
1 /* tree_phylo.c    2008-01-14 */
2
3 /* Copyright 2008 Emmanuel Paradis */
4
5 /* This file is part of the R-package `ape'. */
6 /* See the file ../COPYING for licensing issues. */
7
8 #include "me.h"
9
10 /* from newick.c */
11 int leaf(node *v);
12
13 static int curnod, curtip, iedge;
14
15 #define DO_EDGE\
16         el[iedge] = EDGE->distance;\
17         if (leaf(EDGE->head)) {\
18                 edge2[iedge] = curtip;\
19                 strncpy(tl[curtip - 1], EDGE->head->label, 6);\
20                 iedge++;\
21                 curtip++;\
22         } else {\
23                 edge2[iedge] = curnod;\
24                 iedge++;\
25                 subtree2phylo(EDGE->head, edge1, edge2, el, tl);\
26         }
27
28 void subtree2phylo(node *parent, int *edge1, int *edge2, double *el, char **tl)
29 {
30         edge *EDGE; int localnode;
31
32         EDGE = parent->leftEdge;
33 /* 'localnode' keeps a copy of the node ancestor # between
34    the two (recursive) calls of subtree2phylo */
35         localnode = edge1[iedge] = curnod;
36         curnod++;
37         DO_EDGE
38
39         EDGE = parent->rightEdge;
40         edge1[iedge] = localnode;
41         DO_EDGE
42 }
43
44 /*
45 transforms a 'tree' struc of pointers into an object of class "phylo"
46 assumes the tree is unrooted and binary, so there are 2n - 3 edges
47 assumes labels are 6-char long
48 */
49 void tree2phylo(tree *T, int *edge1, int *edge2, double *el, char **tl, int n)
50 {
51         edge *EDGE;
52         curnod = n + 1; /* the root for ape */
53
54 /* there's in fact only one edge from the "root" which is
55    a tip in ape's terminology (i.e., a node of degree 1) */
56
57         EDGE = T->root->leftEdge;
58         edge1[0] = curnod;
59         edge2[0] = 1; /* <- the 1st tip */
60         strncpy(tl[0], T->root->label, 6);
61         el[0] = EDGE->distance;
62         /* now can initialize these two: */
63         curtip = 2; /* <- the 2nd tip */
64         iedge = 1; /* <- the 2nd edge */
65         edge1[iedge] = curnod;
66
67 /* 'T->root->leftEdge->head' is the root for ape,
68    so don't need to test if it's a leaf */
69
70         subtree2phylo(EDGE->head, edge1, edge2, el, tl);
71 }