1 /* tree_phylo.c 2008-01-14 */
3 /* Copyright 2008 Emmanuel Paradis */
5 /* This file is part of the R-package `ape'. */
6 /* See the file ../COPYING for licensing issues. */
13 static int curnod, curtip, iedge;
16 el[iedge] = EDGE->distance;\
17 if (leaf(EDGE->head)) {\
18 edge2[iedge] = curtip;\
19 strncpy(tl[curtip - 1], EDGE->head->label, 6);\
23 edge2[iedge] = curnod;\
25 subtree2phylo(EDGE->head, edge1, edge2, el, tl);\
28 void subtree2phylo(node *parent, int *edge1, int *edge2, double *el, char **tl)
30 edge *EDGE; int localnode;
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;
39 EDGE = parent->rightEdge;
40 edge1[iedge] = localnode;
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
49 void tree2phylo(tree *T, int *edge1, int *edge2, double *el, char **tl, int n)
52 curnod = n + 1; /* the root for ape */
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) */
57 EDGE = T->root->leftEdge;
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;
67 /* 'T->root->leftEdge->head' is the root for ape,
68 so don't need to test if it's a leaf */
70 subtree2phylo(EDGE->head, edge1, edge2, el, tl);