]> git.donarmstrong.com Git - ape.git/blobdiff - src/tree_phylo.c
various fixes in C files
[ape.git] / src / tree_phylo.c
index 63f677de2ce61e7130138c962ad75805a9efd660..17badca9903a2d76f1d96f2848256478293f8e51 100644 (file)
@@ -1,31 +1,39 @@
-/* tree_phylo.c    2008-01-14 */
+/* tree_phylo.c    2012-04-30 */
 
-/* Copyright 2008 Emmanuel Paradis */
+/* Copyright 2008-2012 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);\
+               ilab[curtip - 1] = EDGE->head->label;\
                iedge++;\
                curtip++;\
        } else {\
                edge2[iedge] = curnod;\
                iedge++;\
-               subtree2phylo(EDGE->head, edge1, edge2, el, tl);\
+               subtree2phylo(EDGE->head, edge1, edge2, el, ilab);\
        }
 
-void subtree2phylo(node *parent, int *edge1, int *edge2, double *el, char **tl)
+int leaf(node *v)
+{
+       int count = 0;
+       if (NULL != v->parentEdge) count++;
+       if (NULL != v->leftEdge) count++;
+       if (NULL != v->rightEdge) count++;
+       if (NULL != v->middleEdge) count++;
+       if (count > 1) return(0);
+       return(1);
+}
+
+void subtree2phylo(node *parent, int *edge1, int *edge2, double *el, int *ilab)
 {
        edge *EDGE; int localnode;
 
@@ -44,9 +52,9 @@ void subtree2phylo(node *parent, int *edge1, int *edge2, double *el, char **tl)
 /*
 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
+assumes labels are int
 */
-void tree2phylo(tree *T, int *edge1, int *edge2, double *el, char **tl, int n)
+void tree2phylo(tree *T, int *edge1, int *edge2, double *el, int *ilab, int n)
 {
        edge *EDGE;
        curnod = n + 1; /* the root for ape */
@@ -57,7 +65,7 @@ void tree2phylo(tree *T, int *edge1, int *edge2, double *el, char **tl, int n)
        EDGE = T->root->leftEdge;
        edge1[0] = curnod;
        edge2[0] = 1; /* <- the 1st tip */
-       strncpy(tl[0], T->root->label, 6);
+       ilab[0] = T->root->label;
        el[0] = EDGE->distance;
        /* now can initialize these two: */
        curtip = 2; /* <- the 2nd tip */
@@ -67,5 +75,5 @@ void tree2phylo(tree *T, int *edge1, int *edge2, double *el, char **tl, int n)
 /* '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);
+       subtree2phylo(EDGE->head, edge1, edge2, el, ilab);
 }