From fc23a549d88857c3f4c82b4e0a9ced45af2ecc57 Mon Sep 17 00:00:00 2001 From: paradis Date: Mon, 14 Jan 2008 13:34:37 +0000 Subject: [PATCH] conversion tree -> phylo git-svn-id: https://svn.mpl.ird.fr/ape/dev/ape@14 6e262413-ae40-0410-9e79-b911bd7a66b7 --- src/tree_phylo.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/tree_phylo.c diff --git a/src/tree_phylo.c b/src/tree_phylo.c new file mode 100644 index 0000000..63f677d --- /dev/null +++ b/src/tree_phylo.c @@ -0,0 +1,71 @@ +/* tree_phylo.c 2008-01-14 */ + +/* Copyright 2008 Emmanuel Paradis */ + +/* This file is part of the R-package `ape'. */ +/* See the file ../COPYING for licensing issues. */ + +#include "me.h" + +/* from newick.c */ +int leaf(node *v); + +static int curnod, curtip, iedge; + +#define DO_EDGE\ + el[iedge] = EDGE->distance;\ + if (leaf(EDGE->head)) {\ + edge2[iedge] = curtip;\ + strncpy(tl[curtip - 1], EDGE->head->label, 6);\ + iedge++;\ + curtip++;\ + } else {\ + edge2[iedge] = curnod;\ + iedge++;\ + subtree2phylo(EDGE->head, edge1, edge2, el, tl);\ + } + +void subtree2phylo(node *parent, int *edge1, int *edge2, double *el, char **tl) +{ + edge *EDGE; int localnode; + + EDGE = parent->leftEdge; +/* 'localnode' keeps a copy of the node ancestor # between + the two (recursive) calls of subtree2phylo */ + localnode = edge1[iedge] = curnod; + curnod++; + DO_EDGE + + EDGE = parent->rightEdge; + edge1[iedge] = localnode; + DO_EDGE +} + +/* +transforms a 'tree' struc of pointers into an object of class "phylo" +assumes the tree is unrooted and binary, so there are 2n - 3 edges +assumes labels are 6-char long +*/ +void tree2phylo(tree *T, int *edge1, int *edge2, double *el, char **tl, int n) +{ + edge *EDGE; + curnod = n + 1; /* the root for ape */ + +/* there's in fact only one edge from the "root" which is + a tip in ape's terminology (i.e., a node of degree 1) */ + + EDGE = T->root->leftEdge; + edge1[0] = curnod; + edge2[0] = 1; /* <- the 1st tip */ + strncpy(tl[0], T->root->label, 6); + el[0] = EDGE->distance; + /* now can initialize these two: */ + curtip = 2; /* <- the 2nd tip */ + iedge = 1; /* <- the 2nd edge */ + edge1[iedge] = curnod; + +/* 'T->root->leftEdge->head' is the root for ape, + so don't need to test if it's a leaf */ + + subtree2phylo(EDGE->head, edge1, edge2, el, tl); +} -- 2.39.2