diff --git a/examples/load-utree/load-utree.c b/examples/load-utree/load-utree.c index b774c82..f9b6323 100644 --- a/examples/load-utree/load-utree.c +++ b/examples/load-utree/load-utree.c @@ -34,23 +34,18 @@ static void fatal(const char * format, ...) exit(EXIT_FAILURE); } -pll_utree_t * load_tree_unrooted(const char * filename, - unsigned int * tip_count) +pll_utree_t * load_tree_unrooted(const char * filename) { pll_utree_t * utree; pll_rtree_t * rtree; - if (!(rtree = pll_rtree_parse_newick(filename, tip_count))) + if (!(rtree = pll_rtree_parse_newick(filename))) { if (!(utree = pll_utree_parse_newick(filename))) { fprintf(stderr, "%s\n", pll_errmsg); return NULL; } - else - { - *tip_count = utree->tip_count; - } } else { @@ -59,7 +54,7 @@ pll_utree_t * load_tree_unrooted(const char * filename, pll_unode_t * root = utree->nodes[utree->tip_count+utree->inner_count-1]; /* optional step if using default PLL clv/pmatrix index assignments */ - pll_utree_reset_template_indices(root, *tip_count); + pll_utree_reset_template_indices(root, utree->tip_count); pll_rtree_destroy(rtree,NULL); } @@ -69,12 +64,10 @@ pll_utree_t * load_tree_unrooted(const char * filename, int main(int argc, char * argv[]) { - unsigned int tip_count; - if (argc != 2) fatal("syntax: %s [newick]", argv[0]); - pll_utree_t * utree = load_tree_unrooted(argv[1], &tip_count); + pll_utree_t * utree = load_tree_unrooted(argv[1]); if (!utree) fatal("Tree must be a rooted or unrooted binary."); diff --git a/examples/newick-fasta-rooted/newick-fasta-rooted.c b/examples/newick-fasta-rooted/newick-fasta-rooted.c index b2b1a70..391e389 100644 --- a/examples/newick-fasta-rooted/newick-fasta-rooted.c +++ b/examples/newick-fasta-rooted/newick-fasta-rooted.c @@ -29,35 +29,37 @@ static void fatal(const char * format, ...) __attribute__ ((noreturn)); +static void * xmalloc(size_t size) +{ + void * t; + t = malloc(size); + if (!t) + fatal("Unable to allocate enough memory."); + + return t; +} + +static char * xstrdup(const char * s) +{ + size_t len = strlen(s); + char * p = (char *)xmalloc(len+1); + return strcpy(p,s); +} + /* a callback function for performing a full traversal */ -static int cb_full_traversal(pll_rtree_t * node) +static int cb_full_traversal(pll_rnode_t * node) { return 1; } -static void set_missing_branch_length_recursive(pll_rtree_t * tree, - double length) -{ - if (!tree) return; - - if (!tree->length) - tree->length = length; - - if (tree->left) - set_missing_branch_length_recursive(tree->left, length); - - if (tree->right) - set_missing_branch_length_recursive(tree->right, length); -} - /* branch lengths not present in the newick file get a value of 0.000001 */ static void set_missing_branch_length(pll_rtree_t * tree, double length) { - if (tree) - { - set_missing_branch_length_recursive(tree->left, length); - set_missing_branch_length_recursive(tree->right, length); - } + unsigned int i; + + for (i = 0; i < tree->tip_count + tree->inner_count; ++i) + if (!tree->nodes[i]->length) + tree->nodes[i]->length = length; } static void fatal(const char * format, ...) @@ -79,20 +81,21 @@ int main(int argc, char * argv[]) double * branch_lengths; pll_partition_t * partition; pll_operation_t * operations; - pll_rtree_t ** travbuffer; + pll_rnode_t ** travbuffer; if (argc != 3) fatal(" syntax: %s [newick] [fasta]", argv[0]); - pll_rtree_t * tree = pll_rtree_parse_newick(argv[1], &tip_nodes_count); + pll_rtree_t * tree = pll_rtree_parse_newick(argv[1]); if (!tree) fatal("Tree must be an rooted binary tree"); - + /* fix all missing branch lengths (i.e. those that did not appear in the newick) to 0.000001 */ set_missing_branch_length(tree, 0.000001); /* compute and show node count information */ + tip_nodes_count = tree->tip_count; inner_nodes_count = tip_nodes_count - 1; nodes_count = inner_nodes_count + tip_nodes_count; branch_count = nodes_count - 1; @@ -114,22 +117,21 @@ int main(int argc, char * argv[]) */ - /* obtain an array of pointers to tip names */ - pll_rtree_t ** tipnodes = (pll_rtree_t **)calloc(tip_nodes_count, - sizeof(pll_rtree_t *)); - pll_rtree_query_tipnodes(tree, tipnodes); - /* create a libc hash table of size tip_count */ hcreate(tip_nodes_count); /* populate a libc hash table with tree tip labels */ - unsigned int * data = (unsigned int *)malloc(tip_nodes_count * - sizeof(unsigned int)); + unsigned int * data = (unsigned int *)xmalloc(tip_nodes_count * + sizeof(unsigned int)); for (i = 0; i < tip_nodes_count; ++i) { - data[i] = tipnodes[i]->clv_index; + data[i] = tree->nodes[i]->clv_index; ENTRY entry; - entry.key = tipnodes[i]->label; +#ifdef __APPLE__ + entry.key = xstrdup(tree->nodes[i]->label); +#else + entry.key = tree->nodes[i]->label; +#endif entry.data = (void *)(data+i); hsearch(entry, ENTER); } @@ -228,7 +230,6 @@ int main(int argc, char * argv[]) /* we no longer need these two arrays (keys and values of hash table... */ free(data); - free(tipnodes); /* ...neither the sequences and the headers as they are already present in the form of probabilities in the tip CLVs */ @@ -243,18 +244,17 @@ int main(int argc, char * argv[]) /* allocate a buffer for storing pointers to nodes of the tree in postorder traversal */ - travbuffer = (pll_rtree_t **)malloc(nodes_count * sizeof(pll_rtree_t *)); - - + travbuffer = (pll_rnode_t **)xmalloc(nodes_count * sizeof(pll_rnode_t *)); - branch_lengths = (double *)malloc(branch_count * sizeof(double)); - matrix_indices = (unsigned int *)malloc(branch_count * sizeof(unsigned int)); - operations = (pll_operation_t *)malloc(inner_nodes_count * - sizeof(pll_operation_t)); + branch_lengths = (double *)xmalloc(branch_count * sizeof(double)); + matrix_indices = (unsigned int *)xmalloc(branch_count * sizeof(unsigned int)); + operations = (pll_operation_t *)xmalloc(inner_nodes_count * + sizeof(pll_operation_t)); /* perform a postorder traversal of the rooted tree */ unsigned int traversal_size; - if (!pll_rtree_traverse(tree, + if (!pll_rtree_traverse(tree->root, + PLL_TREE_TRAVERSE_POSTORDER, cb_full_traversal, travbuffer, &traversal_size)) @@ -345,8 +345,8 @@ int main(int argc, char * argv[]) frequency vector is to be used */ double logl = pll_compute_root_loglikelihood(partition, - tree->clv_index, - tree->scaler_index, + tree->root->clv_index, + tree->root->scaler_index, params_indices, NULL); diff --git a/examples/newick-fasta-unrooted/newick-fasta-unrooted.c b/examples/newick-fasta-unrooted/newick-fasta-unrooted.c index 492a822..ac8edb0 100644 --- a/examples/newick-fasta-unrooted/newick-fasta-unrooted.c +++ b/examples/newick-fasta-unrooted/newick-fasta-unrooted.c @@ -29,11 +29,6 @@ static void fatal(const char * format, ...) __attribute__ ((noreturn)); -typedef struct -{ - int clv_valid; -} node_info_t; - static void * xmalloc(size_t size) { void * t; diff --git a/examples/parsimony/npr-pars.c b/examples/parsimony/npr-pars.c index 110be33..3f19ffc 100644 --- a/examples/parsimony/npr-pars.c +++ b/examples/parsimony/npr-pars.c @@ -29,8 +29,25 @@ static void fatal(const char * format, ...) __attribute__ ((noreturn)); +static void * xmalloc(size_t size) +{ + void * t; + t = malloc(size); + if (!t) + fatal("Unable to allocate enough memory."); + + return t; +} + +static char * xstrdup(const char * s) +{ + size_t len = strlen(s); + char * p = (char *)xmalloc(len+1); + return strcpy(p,s); +} + /* a callback function for performing a full traversal */ -static int cb_full_traversal(pll_rtree_t * node) +static int cb_full_traversal(pll_rnode_t * node) { return 1; } @@ -54,7 +71,7 @@ int main(int argc, char * argv[]) pll_parsimony_t * pars; pll_pars_buildop_t * operations; pll_pars_recop_t * recops; - pll_rtree_t ** travbuffer; + pll_rnode_t ** travbuffer; /* we accept only two arguments - the newick tree (unrooted binary) and the alignment in the form of PHYLIP reads */ @@ -63,11 +80,12 @@ int main(int argc, char * argv[]) /* parse the unrooted binary tree in newick format, and store the number of tip nodes in tip_nodes_count */ - pll_rtree_t * tree = pll_rtree_parse_newick(argv[1], &tip_nodes_count); + pll_rtree_t * tree = pll_rtree_parse_newick(argv[1]); if (!tree) fatal("Tree must be a rooted binary tree"); /* compute and show node count information */ + tip_nodes_count = tree->tip_count; inner_nodes_count = tip_nodes_count - 1; nodes_count = inner_nodes_count + tip_nodes_count; branch_count = nodes_count - 1; @@ -91,22 +109,21 @@ int main(int argc, char * argv[]) */ - /* obtain an array of pointers to tip nodes */ - pll_rtree_t ** tipnodes = (pll_rtree_t **)calloc(tip_nodes_count, - sizeof(pll_rtree_t *)); - pll_rtree_query_tipnodes(tree, tipnodes); - /* create a libc hash table of size tip_nodes_count */ hcreate(tip_nodes_count); /* populate a libc hash table with tree tip labels */ - unsigned int * data = (unsigned int *)malloc(tip_nodes_count * - sizeof(unsigned int)); + unsigned int * data = (unsigned int *)xmalloc(tip_nodes_count * + sizeof(unsigned int)); for (i = 0; i < tip_nodes_count; ++i) { - data[i] = tipnodes[i]->clv_index; + data[i] = tree->nodes[i]->clv_index; ENTRY entry; - entry.key = tipnodes[i]->label; +#ifdef __APPLE__ + entry.key = xstrdup(tree->nodes[i]->label); +#else + entry.key = tree->nodes[i]->label; +#endif entry.data = (void *)(data+i); hsearch(entry, ENTER); } @@ -172,19 +189,19 @@ int main(int argc, char * argv[]) /* we no longer need these two arrays (keys and values of hash table... */ free(data); - free(tipnodes); /* allocate a buffer for storing pointers to nodes of the tree in postorder traversal */ - travbuffer = (pll_rtree_t **)malloc(nodes_count * sizeof(pll_rtree_t *)); + travbuffer = (pll_rnode_t **)xmalloc(nodes_count * sizeof(pll_rnode_t *)); - operations = (pll_pars_buildop_t *)malloc(inner_nodes_count * - sizeof(pll_pars_buildop_t)); + operations = (pll_pars_buildop_t *)xmalloc(inner_nodes_count * + sizeof(pll_pars_buildop_t)); /* perform a postorder traversal of the rooted tree */ unsigned int traversal_size; - if (!pll_rtree_traverse(tree, + if (!pll_rtree_traverse(tree->root, + PLL_TREE_TRAVERSE_POSTORDER, cb_full_traversal, travbuffer, &traversal_size)) @@ -225,15 +242,16 @@ int main(int argc, char * argv[]) } /* perform a preorder traversal of the rooted tree */ - if (!pll_rtree_traverse_preorder(tree, - cb_full_traversal, - travbuffer, - &traversal_size)) + if (!pll_rtree_traverse(tree->root, + PLL_TREE_TRAVERSE_PREORDER, + cb_full_traversal, + travbuffer, + &traversal_size)) fatal("Function pll_rtree_traverse() requires inner nodes as parameters"); /* create the reconstruct operations */ - recops = (pll_pars_recop_t *)malloc(inner_nodes_count * - sizeof(pll_pars_recop_t)); + recops = (pll_pars_recop_t *)xmalloc(inner_nodes_count * + sizeof(pll_pars_recop_t)); pll_rtree_create_pars_recops(travbuffer, traversal_size, recops, diff --git a/src/parse_rtree.y b/src/parse_rtree.y index 3b60402..300a9b8 100644 --- a/src/parse_rtree.y +++ b/src/parse_rtree.y @@ -34,26 +34,52 @@ extern void pll_rtree__delete_buffer(struct pll_rtree_buffer_state * buffer); static unsigned int tip_cnt = 0; -PLL_EXPORT void pll_rtree_destroy(pll_rtree_t * root, - void (*cb_destroy)(void *)) +static void dealloc_data(pll_rnode_t * node, void (*cb_destroy)(void *)) { - if (!root) return; - - pll_rtree_destroy(root->left, cb_destroy); - pll_rtree_destroy(root->right, cb_destroy); - - if (root->data) + if (node->data) { if (cb_destroy) - cb_destroy(root->data); + cb_destroy(node->data); } +} + +PLL_EXPORT void pll_rtree_graph_destroy(pll_rnode_t * root, + void (*cb_destroy)(void *)) +{ + if (!root) return; + + pll_rtree_graph_destroy(root->left, cb_destroy); + pll_rtree_graph_destroy(root->right, cb_destroy); + dealloc_data(root, cb_destroy); free(root->label); free(root); } +PLL_EXPORT void pll_rtree_destroy(pll_rtree_t * tree, + void (*cb_destroy)(void *)) +{ + unsigned int i; + pll_rnode_t * node; + + /* deallocate all nodes */ + for (i = 0; i < tree->tip_count + tree->inner_count; ++i) + { + node = tree->nodes[i]; + dealloc_data(node, cb_destroy); + + if (node->label) + free(node->label); + + free(node); + } + + /* deallocate tree structure */ + free(tree->nodes); + free(tree); +} -static void pll_rtree_error(pll_rtree_t * tree, const char * s) +static void pll_rtree_error(pll_rnode_t * node, const char * s) { pll_errno = PLL_ERROR_NEWICK_SYNTAX; if (pll_rtree_colstart == pll_rtree_colend) @@ -70,12 +96,12 @@ static void pll_rtree_error(pll_rtree_t * tree, const char * s) { char * s; char * d; - struct pll_rtree * tree; + struct pll_rnode_s * tree; } %error-verbose -%parse-param {struct pll_rtree * tree} -%destructor { pll_rtree_destroy($$,NULL); } subtree +%parse-param {struct pll_rnode_s * tree} +%destructor { pll_rtree_graph_destroy($$,NULL); } subtree %destructor { free($$); } STRING %destructor { free($$); } NUMBER %destructor { free($$); } label @@ -108,7 +134,7 @@ input: OPAR subtree COMMA subtree CPAR optional_label optional_length SEMICOLON subtree: OPAR subtree COMMA subtree CPAR optional_label optional_length { - $$ = (pll_rtree_t *)calloc(1, sizeof(pll_rtree_t)); + $$ = (pll_rnode_t *)calloc(1, sizeof(pll_rnode_t)); $$->left = $2; $$->right = $4; $$->label = $6; @@ -121,7 +147,7 @@ subtree: OPAR subtree COMMA subtree CPAR optional_label optional_length } | label optional_length { - $$ = (pll_rtree_t *)calloc(1, sizeof(pll_rtree_t)); + $$ = (pll_rnode_t *)calloc(1, sizeof(pll_rnode_t)); $$->label = $1; $$->length = $2 ? atof($2) : 0; $$->left = NULL; @@ -138,7 +164,7 @@ number: NUMBER {$$=$1;}; %% -static void recursive_assign_indices(pll_rtree_t * node, +static void recursive_assign_indices(pll_rnode_t * node, unsigned int * tip_clv_index, unsigned int * inner_clv_index, int * inner_scaler_index, @@ -176,7 +202,8 @@ static void recursive_assign_indices(pll_rtree_t * node, *inner_node_index = *inner_node_index + 1; } -static void assign_indices(pll_rtree_t * root, unsigned int tip_count) +PLL_EXPORT void pll_rtree_reset_template_indices(pll_rnode_t * root, + unsigned int tip_count) { unsigned int tip_clv_index = 0; unsigned int inner_clv_index = tip_count; @@ -203,33 +230,122 @@ static void assign_indices(pll_rtree_t * root, unsigned int tip_count) root->pmatrix_index = 0; } -PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick(const char * filename, - unsigned int * tip_count) +static void fill_nodes_recursive(pll_rnode_t * node, + pll_rnode_t ** array, + unsigned int * tip_index, + unsigned int * inner_index) { - struct pll_rtree * tree; + if (!node->left) + { + array[*tip_index] = node; + *tip_index = *tip_index + 1; + return; + } - /* reset tip count */ - tip_cnt = 0; + fill_nodes_recursive(node->left, array, tip_index, inner_index); + fill_nodes_recursive(node->right, array, tip_index, inner_index); + + array[*inner_index] = node; + *inner_index = *inner_index + 1; +} - if (!(tree = (pll_rtree_t *)calloc(1, sizeof(pll_rtree_t)))) +static unsigned int rtree_count_tips(pll_rnode_t * root) +{ + unsigned int count = 0; + + if (root->left) + count += rtree_count_tips(root->left); + if (root->right) + count += rtree_count_tips(root->right); + + if (!root->left && !root->right) + return 1; + + return count; +} + +PLL_EXPORT pll_rtree_t * pll_rtree_wraptree(pll_rnode_t * root, + unsigned int tip_count) +{ + pll_rtree_t * tree = (pll_rtree_t *)malloc(sizeof(pll_rtree_t)); + if (!tree) { + snprintf(pll_errmsg, 200, "Unable to allocate enough memory."); pll_errno = PLL_ERROR_MEM_ALLOC; + return PLL_FAILURE; + } + + if (tip_count < 2 && tip_count != 0) + { + snprintf(pll_errmsg, 200, "Invalid tip_count value (%u).", tip_count); + pll_errno = PLL_ERROR_PARAM_INVALID; + return PLL_FAILURE; + } + + if (tip_count == 0) + { + tip_count = rtree_count_tips(root); + if (tip_count < 2) + { + snprintf(pll_errmsg, 200, "Input tree contains no inner nodes."); + pll_errno = PLL_ERROR_PARAM_INVALID; + return PLL_FAILURE; + } + } + + tree->nodes = (pll_rnode_t **)malloc((2*tip_count-1)*sizeof(pll_rnode_t *)); + if (!tree->nodes) + { snprintf(pll_errmsg, 200, "Unable to allocate enough memory."); + pll_errno = PLL_ERROR_MEM_ALLOC; return PLL_FAILURE; } + + unsigned int tip_index = 0; + unsigned int inner_index = tip_count; + + fill_nodes_recursive(root->left, tree->nodes, &tip_index, &inner_index); + fill_nodes_recursive(root->right,tree->nodes, &tip_index, &inner_index); + tree->nodes[inner_index] = root; + + tree->tip_count = tip_count; + tree->edge_count = 2*tip_count-2; + tree->inner_count = tip_count-1; + tree->root = root; + + return tree; +} + +PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick(const char * filename) +{ + pll_rtree_t * tree; + struct pll_rnode_s * root; + + /* reset tip count */ + tip_cnt = 0; + + /* open input file */ pll_rtree_in = fopen(filename, "r"); if (!pll_rtree_in) { - pll_rtree_destroy(tree,NULL); pll_errno = PLL_ERROR_FILE_OPEN; snprintf(pll_errmsg, 200, "Unable to open file (%s)", filename); return PLL_FAILURE; } - else if (pll_rtree_parse(tree)) + + /* create root node */ + if (!(root = (pll_rnode_t *)calloc(1, sizeof(pll_rnode_t)))) { - pll_rtree_destroy(tree,NULL); - tree = NULL; + pll_errno = PLL_ERROR_MEM_ALLOC; + snprintf(pll_errmsg, 200, "Unable to allocate enough memory."); + return PLL_FAILURE; + } + + if (pll_rtree_parse(root)) + { + pll_rtree_graph_destroy(root,NULL); + root = NULL; fclose(pll_rtree_in); pll_rtree_lex_destroy(); return PLL_FAILURE; @@ -239,24 +355,25 @@ PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick(const char * filename, pll_rtree_lex_destroy(); - *tip_count = tip_cnt; - /* initialize clv and scaler indices */ - assign_indices(tree, tip_cnt); + pll_rtree_reset_template_indices(root, tip_cnt); + + /* wrap tree */ + tree = pll_rtree_wraptree(root,tip_cnt); return tree; } -PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick_string(const char * s, - unsigned int * tip_count) +PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick_string(const char * s) { int rc; - struct pll_rtree * tree; + struct pll_rnode_s * root; + pll_rtree_t * tree = NULL; /* reset tip count */ tip_cnt = 0; - if (!(tree = (pll_rtree_t *)calloc(1, sizeof(pll_rtree_t)))) + if (!(root = (pll_rnode_t *)calloc(1, sizeof(pll_rnode_t)))) { pll_errno = PLL_ERROR_MEM_ALLOC; snprintf(pll_errmsg, 200, "Unable to allocate enough memory."); @@ -264,21 +381,20 @@ PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick_string(const char * s, } struct pll_rtree_buffer_state * buffer = pll_rtree__scan_string(s); - rc = pll_rtree_parse(tree); + rc = pll_rtree_parse(root); pll_rtree__delete_buffer(buffer); pll_rtree_lex_destroy(); if (!rc) { - *tip_count = tip_cnt; - /* initialize clv and scaler indices */ - assign_indices(tree, tip_cnt); + pll_rtree_reset_template_indices(root, tip_cnt); - return tree; + tree = pll_rtree_wraptree(root,tip_cnt); } + else + free(root); - free(tree); - return NULL; + return tree; } diff --git a/src/parse_utree.y b/src/parse_utree.y index 3dae6f3..2ca45f0 100644 --- a/src/parse_utree.y +++ b/src/parse_utree.y @@ -48,6 +48,7 @@ static void dealloc_graph_recursive(pll_unode_t * node, { if (!node->next) { + dealloc_data(node,cb_destroy); free(node->label); free(node); return; @@ -73,6 +74,7 @@ PLL_EXPORT void pll_utree_graph_destroy(pll_unode_t * root, if (!root) return; if (!(root->next)) { + dealloc_data(root,cb_destroy); free(root->label); free(root); return; @@ -458,7 +460,13 @@ PLL_EXPORT pll_utree_t * pll_utree_parse_newick(const char * filename) return PLL_FAILURE; } - root = (pll_unode_t *)calloc(1, sizeof(pll_unode_t)); + if (!(root = (pll_unode_t *)calloc(1, sizeof(pll_unode_t)))) + { + pll_errno = PLL_ERROR_MEM_ALLOC; + snprintf(pll_errmsg, 200, "Unable to allocate enough memory."); + return PLL_FAILURE; + } + if (pll_utree_parse(root)) { pll_utree_graph_destroy(root,NULL); @@ -510,8 +518,8 @@ PLL_EXPORT pll_utree_t * pll_utree_parse_newick_string(const char * s) tree = pll_utree_wraptree(root,tip_cnt); } - - free(root); + else + free(root); return tree; } diff --git a/src/pll.h b/src/pll.h index dd19471..42906c6 100644 --- a/src/pll.h +++ b/src/pll.h @@ -190,6 +190,7 @@ typedef struct pll_partition int asc_bias_alloc; } pll_partition_t; + /* Structure for driving likelihood operations */ typedef struct pll_operation @@ -263,7 +264,7 @@ typedef struct pll_utree_s } pll_utree_t; -typedef struct pll_rtree +typedef struct pll_rnode_s { char * label; double length; @@ -271,11 +272,23 @@ typedef struct pll_rtree unsigned int clv_index; int scaler_index; unsigned int pmatrix_index; - struct pll_rtree * left; - struct pll_rtree * right; - struct pll_rtree * parent; + struct pll_rnode_s * left; + struct pll_rnode_s * right; + struct pll_rnode_s * parent; void * data; +} pll_rnode_t; + +typedef struct pll_rtree_s +{ + unsigned int tip_count; + unsigned int inner_count; + unsigned int edge_count; + + pll_rnode_t ** nodes; + + pll_rnode_t * root; + } pll_rtree_t; /* structures for handling topological rearrangement move rollbacks */ @@ -596,15 +609,21 @@ PLL_EXPORT int pll_fasta_rewind(pll_fasta_t * fd); /* functions in parse_rtree.y */ -PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick(const char * filename, - unsigned int * tip_count); +PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick(const char * filename); -PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick_string(const char * s, - unsigned int * tip_count); +PLL_EXPORT pll_rtree_t * pll_rtree_parse_newick_string(const char * s); PLL_EXPORT void pll_rtree_destroy(pll_rtree_t * root, void (*cb_destroy)(void *)); +PLL_EXPORT void pll_rtree_reset_template_indices(pll_rnode_t * node, + unsigned int tip_count); + +PLL_EXPORT void pll_rtree_graph_destroy(pll_rnode_t * root, + void (*cb_destroy)(void *)); + +PLL_EXPORT pll_rtree_t * pll_rtree_wraptree(pll_rnode_t * root, + unsigned int tip_count); /* functions in parse_utree.y */ PLL_EXPORT pll_utree_t * pll_utree_parse_newick(const char * filename); @@ -657,7 +676,7 @@ PLL_EXPORT pll_unode_t * pll_utree_graph_clone(pll_unode_t * root); PLL_EXPORT pll_utree_t * pll_utree_clone(pll_utree_t * root); -PLL_EXPORT pll_utree_t * pll_rtree_unroot(pll_rtree_t * root); +PLL_EXPORT pll_utree_t * pll_rtree_unroot(pll_rtree_t * tree); PLL_EXPORT int pll_utree_every(pll_utree_t * tree, int (*cb)(pll_unode_t *)); @@ -676,22 +695,25 @@ PLL_EXPORT void pll_msa_destroy(pll_msa_t * msa); /* functions in rtree.c */ -PLL_EXPORT void pll_rtree_show_ascii(const pll_rtree_t * tree, int options); +PLL_EXPORT void pll_rtree_show_ascii(const pll_rnode_t * root, int options); -PLL_EXPORT char * pll_rtree_export_newick(const pll_rtree_t * root); +PLL_EXPORT char * pll_rtree_export_newick(const pll_rnode_t * root); -PLL_EXPORT int pll_rtree_traverse(pll_rtree_t * root, - int (*cbtrav)(pll_rtree_t *), - pll_rtree_t ** outbuffer, +PLL_EXPORT int pll_rtree_traverse(pll_rnode_t * root, + int traversal, + int (*cbtrav)(pll_rnode_t *), + pll_rnode_t ** outbuffer, unsigned int * trav_size); +#if 0 PLL_EXPORT unsigned int pll_rtree_query_tipnodes(pll_rtree_t * root, pll_rtree_t ** node_list); PLL_EXPORT unsigned int pll_rtree_query_innernodes(pll_rtree_t * root, pll_rtree_t ** node_list); +#endif -PLL_EXPORT void pll_rtree_create_operations(pll_rtree_t ** trav_buffer, +PLL_EXPORT void pll_rtree_create_operations(pll_rnode_t ** trav_buffer, unsigned int trav_buffer_size, double * branches, unsigned int * pmatrix_indices, @@ -699,17 +721,19 @@ PLL_EXPORT void pll_rtree_create_operations(pll_rtree_t ** trav_buffer, unsigned int * matrix_count, unsigned int * ops_count); +#if 0 PLL_EXPORT int pll_rtree_traverse_preorder(pll_rtree_t * root, int (*cbtrav)(pll_rtree_t *), pll_rtree_t ** outbuffer, unsigned int * trav_size); +#endif -PLL_EXPORT void pll_rtree_create_pars_buildops(pll_rtree_t ** trav_buffer, +PLL_EXPORT void pll_rtree_create_pars_buildops(pll_rnode_t ** trav_buffer, unsigned int trav_buffer_size, pll_pars_buildop_t * ops, unsigned int * ops_count); -PLL_EXPORT void pll_rtree_create_pars_recops(pll_rtree_t ** trav_buffer, +PLL_EXPORT void pll_rtree_create_pars_recops(pll_rnode_t ** trav_buffer, unsigned int trav_buffer_size, pll_pars_recop_t * ops, unsigned int * ops_count); diff --git a/src/rtree.c b/src/rtree.c index 132df31..a6c1230 100644 --- a/src/rtree.c +++ b/src/rtree.c @@ -23,29 +23,29 @@ static int indent_space = 4; -static void print_node_info(const pll_rtree_t * tree, int options) +static void print_node_info(const pll_rnode_t * root, int options) { if (options & PLL_UTREE_SHOW_LABEL) - printf (" %s", tree->label); + printf (" %s", root->label); if (options & PLL_UTREE_SHOW_BRANCH_LENGTH) - printf (" %f", tree->length); + printf (" %f", root->length); if (options & PLL_UTREE_SHOW_CLV_INDEX) - printf (" %d", tree->clv_index); + printf (" %d", root->clv_index); if (options & PLL_UTREE_SHOW_SCALER_INDEX) - printf (" %d", tree->scaler_index); + printf (" %d", root->scaler_index); if (options & PLL_UTREE_SHOW_PMATRIX_INDEX) - printf (" %d", tree->pmatrix_index); + printf (" %d", root->pmatrix_index); printf("\n"); } -static void print_tree_recurse(const pll_rtree_t * tree, +static void print_tree_recurse(const pll_rnode_t * root, int indent_level, int * active_node_order, int options) { int i,j; - if (!tree) return; + if (!root) return; for (i = 0; i < indent_level; ++i) { @@ -73,40 +73,40 @@ static void print_tree_recurse(const pll_rtree_t * tree, printf("+"); for (j = 0; j < indent_space-1; ++j) printf ("-"); - if (tree->left || tree->right) printf("+"); + if (root->left || root->right) printf("+"); - print_node_info(tree, options); + print_node_info(root, options); if (active_node_order[indent_level-1] == 2) active_node_order[indent_level-1] = 0; active_node_order[indent_level] = 1; - print_tree_recurse(tree->left, + print_tree_recurse(root->left, indent_level+1, active_node_order, options); active_node_order[indent_level] = 2; - print_tree_recurse(tree->right, + print_tree_recurse(root->right, indent_level+1, active_node_order, options); } -static unsigned int tree_indent_level(const pll_rtree_t * tree, unsigned int indent) +static unsigned int tree_indent_level(const pll_rnode_t * root, unsigned int indent) { - if (!tree) return indent; + if (!root) return indent; - unsigned int a = tree_indent_level(tree->left, indent+1); - unsigned int b = tree_indent_level(tree->right, indent+1); + unsigned int a = tree_indent_level(root->left, indent+1); + unsigned int b = tree_indent_level(root->right, indent+1); return (a > b ? a : b); } -void pll_rtree_show_ascii(const pll_rtree_t * tree, int options) +PLL_EXPORT void pll_rtree_show_ascii(const pll_rnode_t * root, int options) { - unsigned int indent_max = tree_indent_level(tree,0); + unsigned int indent_max = tree_indent_level(root,0); int * active_node_order = (int *)malloc((indent_max+1) * sizeof(int)); if (!active_node_order) @@ -118,13 +118,13 @@ void pll_rtree_show_ascii(const pll_rtree_t * tree, int options) active_node_order[0] = 1; active_node_order[1] = 1; - print_node_info(tree, options); - print_tree_recurse(tree->left, 1, active_node_order, options); - print_tree_recurse(tree->right, 1, active_node_order, options); + print_node_info(root, options); + print_tree_recurse(root->left, 1, active_node_order, options); + print_tree_recurse(root->right, 1, active_node_order, options); free(active_node_order); } -static char * rtree_export_newick_recursive(const pll_rtree_t * root) +static char * rtree_export_newick_recursive(const pll_rnode_t * root) { char * newick; int size_alloced; @@ -165,7 +165,7 @@ static char * rtree_export_newick_recursive(const pll_rtree_t * root) return newick; } -PLL_EXPORT char * pll_rtree_export_newick(const pll_rtree_t * root) +PLL_EXPORT char * pll_rtree_export_newick(const pll_rnode_t * root) { char * newick; int size_alloced; @@ -211,7 +211,7 @@ PLL_EXPORT char * pll_rtree_export_newick(const pll_rtree_t * root) } -PLL_EXPORT void pll_rtree_create_operations(pll_rtree_t ** trav_buffer, +PLL_EXPORT void pll_rtree_create_operations(pll_rnode_t ** trav_buffer, unsigned int trav_buffer_size, double * branches, unsigned int * pmatrix_indices, @@ -219,7 +219,7 @@ PLL_EXPORT void pll_rtree_create_operations(pll_rtree_t ** trav_buffer, unsigned int * matrix_count, unsigned int * ops_count) { - pll_rtree_t * node; + pll_rnode_t * node; unsigned int i; *ops_count = 0; @@ -255,10 +255,10 @@ PLL_EXPORT void pll_rtree_create_operations(pll_rtree_t ** trav_buffer, } } -static void rtree_traverse(pll_rtree_t * node, - int (*cbtrav)(pll_rtree_t *), - unsigned int * index, - pll_rtree_t ** outbuffer) +static void rtree_traverse_postorder(pll_rnode_t * node, + int (*cbtrav)(pll_rnode_t *), + unsigned int * index, + pll_rnode_t ** outbuffer) { if (!node->left) { @@ -271,39 +271,18 @@ static void rtree_traverse(pll_rtree_t * node, } if (!cbtrav(node)) return; - rtree_traverse(node->left, cbtrav, index, outbuffer); - rtree_traverse(node->right, cbtrav, index, outbuffer); + + rtree_traverse_postorder(node->left, cbtrav, index, outbuffer); + rtree_traverse_postorder(node->right, cbtrav, index, outbuffer); outbuffer[*index] = node; *index = *index + 1; } -int pll_rtree_traverse(pll_rtree_t * root, - int (*cbtrav)(pll_rtree_t *), - pll_rtree_t ** outbuffer, - unsigned int * trav_size) -{ - *trav_size = 0; - if (!root->left) return PLL_FAILURE; - - /* we will traverse an unrooted tree in the following way - - root - /\ - / \ - left right - - at each node the callback function is called to decide whether we - are going to traversing the subtree rooted at the specific node */ - - rtree_traverse(root, cbtrav, trav_size, outbuffer); - return PLL_SUCCESS; -} - -static void rtree_traverse_preorder(pll_rtree_t * node, - int (*cbtrav)(pll_rtree_t *), +static void rtree_traverse_preorder(pll_rnode_t * node, + int (*cbtrav)(pll_rnode_t *), unsigned int * index, - pll_rtree_t ** outbuffer) + pll_rnode_t ** outbuffer) { if (!node->left) { @@ -325,10 +304,11 @@ static void rtree_traverse_preorder(pll_rtree_t * node, } -PLL_EXPORT int pll_rtree_traverse_preorder(pll_rtree_t * root, - int (*cbtrav)(pll_rtree_t *), - pll_rtree_t ** outbuffer, - unsigned int * trav_size) +PLL_EXPORT int pll_rtree_traverse(pll_rnode_t * root, + int traversal, + int (*cbtrav)(pll_rnode_t *), + pll_rnode_t ** outbuffer, + unsigned int * trav_size) { *trav_size = 0; if (!root->left) return PLL_FAILURE; @@ -343,10 +323,21 @@ PLL_EXPORT int pll_rtree_traverse_preorder(pll_rtree_t * root, at each node the callback function is called to decide whether we are going to traversing the subtree rooted at the specific node */ - rtree_traverse_preorder(root, cbtrav, trav_size, outbuffer); + if (traversal == PLL_TREE_TRAVERSE_POSTORDER) + rtree_traverse_postorder(root, cbtrav, trav_size, outbuffer); + else if (traversal == PLL_TREE_TRAVERSE_PREORDER) + rtree_traverse_preorder(root, cbtrav, trav_size, outbuffer); + else + { + snprintf(pll_errmsg, 200, "Invalid traversal value."); + pll_errno = PLL_ERROR_PARAM_INVALID; + return PLL_FAILURE; + } + return PLL_SUCCESS; } +#if 0 static void rtree_query_tipnodes_recursive(pll_rtree_t * node, pll_rtree_t ** node_list, unsigned int * index) @@ -414,13 +405,14 @@ PLL_EXPORT unsigned int pll_rtree_query_innernodes(pll_rtree_t * root, return index; } +#endif -PLL_EXPORT void pll_rtree_create_pars_buildops(pll_rtree_t ** trav_buffer, +PLL_EXPORT void pll_rtree_create_pars_buildops(pll_rnode_t ** trav_buffer, unsigned int trav_buffer_size, pll_pars_buildop_t * ops, unsigned int * ops_count) { - pll_rtree_t * node; + pll_rnode_t * node; unsigned int i; *ops_count = 0; @@ -440,12 +432,12 @@ PLL_EXPORT void pll_rtree_create_pars_buildops(pll_rtree_t ** trav_buffer, } } -PLL_EXPORT void pll_rtree_create_pars_recops(pll_rtree_t ** trav_buffer, +PLL_EXPORT void pll_rtree_create_pars_recops(pll_rnode_t ** trav_buffer, unsigned int trav_buffer_size, pll_pars_recop_t * ops, unsigned int * ops_count) { - pll_rtree_t * node; + pll_rnode_t * node; unsigned int i; *ops_count = 0; diff --git a/src/utree.c b/src/utree.c index 5c5a1af..74f72f4 100644 --- a/src/utree.c +++ b/src/utree.c @@ -288,21 +288,6 @@ PLL_EXPORT void pll_utree_create_operations(pll_unode_t ** trav_buffer, } } -#if 0 -static int utree_every_recursive(pll_utree_t * tree, - int (*cb)(pll_utree_t *)) -{ - if (!node->next) - return cb(node); - - if (!cb(node)) - return 0; - - return (utree_every_recursive(node->next->back,cb) && - utree_every_recursive(node->next->next->back,cb)); -} -#endif - PLL_EXPORT int pll_utree_every(pll_utree_t * tree, int (*cb)(pll_unode_t *)) { @@ -312,10 +297,7 @@ PLL_EXPORT int pll_utree_every(pll_utree_t * tree, for (i = 0; i < tree->tip_count + tree->inner_count; ++i) rc &= cb(tree->nodes[i]); - return rc; - -// return (utree_every_recursive(node,cb) && -// utree_every_recursive(node->back,cb)); + return (rc ? PLL_SUCCESS : PLL_FAILURE); } static void utree_traverse_preorder(pll_unode_t * node, @@ -576,7 +558,7 @@ PLL_EXPORT pll_utree_t * pll_utree_clone(pll_utree_t * tree) return pll_utree_wraptree(root, tree->tip_count); } -static pll_unode_t * rtree_unroot(pll_rtree_t * root, pll_unode_t * back) +static pll_unode_t * rtree_unroot(pll_rnode_t * root, pll_unode_t * back) { pll_unode_t * uroot = (void *)calloc(1,sizeof(pll_unode_t)); if (!uroot) @@ -625,8 +607,10 @@ static pll_unode_t * rtree_unroot(pll_rtree_t * root, pll_unode_t * back) return uroot; } -PLL_EXPORT pll_utree_t * pll_rtree_unroot(pll_rtree_t * root) +PLL_EXPORT pll_utree_t * pll_rtree_unroot(pll_rtree_t * tree) { + pll_rnode_t * root = tree->root; + if (!root->left->left && !root->right->left) { pll_errno = PLL_ERROR_TREE_CONVERSION; @@ -636,7 +620,7 @@ PLL_EXPORT pll_utree_t * pll_rtree_unroot(pll_rtree_t * root) return NULL; } - pll_rtree_t * new_root; + pll_rnode_t * new_root; pll_unode_t * uroot = (void *)calloc(1,sizeof(pll_unode_t)); if (!uroot) diff --git a/test/src/common.c b/test/src/common.c index 90d25ed..7a84118 100644 --- a/test/src/common.c +++ b/test/src/common.c @@ -188,7 +188,7 @@ int cb_full_traversal(pll_unode_t * node) return 1; } -int cb_rfull_traversal(pll_rtree_t * node) +int cb_rfull_traversal(pll_rnode_t * node) { return 1; } diff --git a/test/src/common.h b/test/src/common.h index 5e14049..441ba60 100644 --- a/test/src/common.h +++ b/test/src/common.h @@ -44,7 +44,7 @@ pll_partition_t * parse_msa_reduced(const char * filename, unsigned int attributes, unsigned int max_sites); int cb_full_traversal(pll_unode_t * node); -int cb_rfull_traversal(pll_rtree_t * node); +int cb_rfull_traversal(pll_rnode_t * node); /* print error and exit */ void fatal(const char * format, ...) __attribute__ ((noreturn)); diff --git a/test/src/rooted-tipinner.c b/test/src/rooted-tipinner.c index 49ea700..993d39f 100644 --- a/test/src/rooted-tipinner.c +++ b/test/src/rooted-tipinner.c @@ -25,19 +25,21 @@ int main(int argc, char * argv[]) double * branch_lengths; pll_partition_t * partition; pll_operation_t * operations; - pll_rtree_t ** travbuffer; - pll_rtree_t ** inner_nodes_list; + pll_rnode_t ** travbuffer; + pll_rnode_t ** inner_nodes_list; unsigned int params_indices[N_RATE_CATS] = {0,0,0,0}; /* parse the unrooted binary tree in newick format, and store the number of tip nodes in tip_nodes_count */ - pll_rtree_t * tree = pll_rtree_parse_newick(TREEFILE, &tip_nodes_count); + pll_rtree_t * tree = pll_rtree_parse_newick(TREEFILE); if (!tree) { printf("Error reading tree\n"); exit(1); } + tip_nodes_count = tree->tip_count; + unsigned int attributes = get_attributes(argc, argv); /* compute and show node count information */ @@ -50,18 +52,14 @@ int main(int argc, char * argv[]) printf("Total number of nodes in tree: %d\n", nodes_count); printf("Number of branches in tree: %d\n", branch_count); - pll_rtree_show_ascii(tree, PLL_UTREE_SHOW_LABEL | - PLL_UTREE_SHOW_BRANCH_LENGTH | - PLL_UTREE_SHOW_CLV_INDEX); - char * newick = pll_rtree_export_newick(tree); + pll_rtree_show_ascii(tree->root, + PLL_UTREE_SHOW_LABEL | + PLL_UTREE_SHOW_BRANCH_LENGTH | + PLL_UTREE_SHOW_CLV_INDEX); + char * newick = pll_rtree_export_newick(tree->root); printf("%s\n", newick); free(newick); - /* obtain an array of pointers to tip nodes */ - pll_rtree_t ** tipnodes = (pll_rtree_t **)calloc(tip_nodes_count, - sizeof(pll_rtree_t *)); - pll_rtree_query_tipnodes(tree, tipnodes); - /* create a libc hash table of size tip_nodes_count */ hcreate(tip_nodes_count); @@ -73,9 +71,9 @@ int main(int argc, char * argv[]) data[i] = i; ENTRY entry; #ifdef __APPLE__ - entry.key = xstrdup(tipnodes[i]->label); + entry.key = xstrdup(tree->nodes[i]->label); #else - entry.key = tipnodes[i]->label; + entry.key = tree->nodes[i]->label; #endif entry.data = (void *)(data+i); hsearch(entry, ENTER); @@ -181,7 +179,6 @@ int main(int argc, char * argv[]) /* we no longer need these two arrays (keys and values of hash table... */ free(data); - free(tipnodes); /* ...neither the sequences and the headers as they are already present in the form of probabilities in the tip CLVs */ @@ -195,7 +192,7 @@ int main(int argc, char * argv[]) /* allocate a buffer for storing pointers to nodes of the tree in postorder traversal */ - travbuffer = (pll_rtree_t **)malloc(nodes_count * sizeof(pll_rtree_t *)); + travbuffer = (pll_rnode_t **)malloc(nodes_count * sizeof(pll_rnode_t *)); branch_lengths = (double *)malloc(branch_count * sizeof(double)); matrix_indices = (unsigned int *)malloc(branch_count * sizeof(int)); @@ -203,16 +200,19 @@ int main(int argc, char * argv[]) sizeof(pll_operation_t)); /* get inner nodes */ - inner_nodes_list = (pll_rtree_t **)malloc(inner_nodes_count * - sizeof(pll_rtree_t *)); - pll_rtree_query_innernodes(tree, inner_nodes_list); + inner_nodes_list = (pll_rnode_t **)malloc(inner_nodes_count * + sizeof(pll_rnode_t *)); + memcpy(inner_nodes_list, + tree->nodes+tip_nodes_count, + inner_nodes_count*sizeof(pll_rnode_t *)); unsigned int traversal_size; /* compute a partial traversal starting from the randomly selected inner node */ - if (!pll_rtree_traverse(tree, + if (!pll_rtree_traverse(tree->root, + PLL_TREE_TRAVERSE_POSTORDER, cb_rfull_traversal, travbuffer, &traversal_size)) @@ -271,8 +271,8 @@ int main(int argc, char * argv[]) index for the concrete branch length, and the index of the model of whose frequency vector is to be used */ double logl = pll_compute_root_loglikelihood(partition, - tree->clv_index, - tree->scaler_index, + tree->root->clv_index, + tree->root->scaler_index, params_indices, NULL); diff --git a/test/src/rooted.c b/test/src/rooted.c index d00829f..91792bb 100644 --- a/test/src/rooted.c +++ b/test/src/rooted.c @@ -26,19 +26,21 @@ int main(int argc, char * argv[]) double * branch_lengths; pll_partition_t * partition; pll_operation_t * operations; - pll_rtree_t ** travbuffer; - pll_rtree_t ** inner_nodes_list; + pll_rnode_t ** travbuffer; + pll_rnode_t ** inner_nodes_list; unsigned int params_indices[N_RATE_CATS] = {0,0,0,0}; /* parse the unrooted binary tree in newick format, and store the number of tip nodes in tip_nodes_count */ - pll_rtree_t * tree = pll_rtree_parse_newick(TREEFILE, &tip_nodes_count); + pll_rtree_t * tree = pll_rtree_parse_newick(TREEFILE); if (!tree) { printf("Error reading tree\n"); exit(1); } + tip_nodes_count = tree->tip_count; + unsigned int attributes = get_attributes(argc, argv); /* compute and show node count information */ @@ -51,18 +53,14 @@ int main(int argc, char * argv[]) printf("Total number of nodes in tree: %d\n", nodes_count); printf("Number of branches in tree: %d\n", branch_count); - pll_rtree_show_ascii(tree, PLL_UTREE_SHOW_LABEL | - PLL_UTREE_SHOW_BRANCH_LENGTH | - PLL_UTREE_SHOW_CLV_INDEX); - char * newick = pll_rtree_export_newick(tree); + pll_rtree_show_ascii(tree->root, + PLL_UTREE_SHOW_LABEL | + PLL_UTREE_SHOW_BRANCH_LENGTH | + PLL_UTREE_SHOW_CLV_INDEX); + char * newick = pll_rtree_export_newick(tree->root); printf("%s\n", newick); free(newick); - /* obtain an array of pointers to tip nodes */ - pll_rtree_t ** tipnodes = (pll_rtree_t **)calloc(tip_nodes_count, - sizeof(pll_rtree_t *)); - pll_rtree_query_tipnodes(tree, tipnodes); - /* create a libc hash table of size tip_nodes_count */ hcreate(tip_nodes_count); @@ -74,9 +72,9 @@ int main(int argc, char * argv[]) data[i] = i; ENTRY entry; #ifdef __APPLE__ - entry.key = xstrdup(tipnodes[i]->label); + entry.key = xstrdup(tree->nodes[i]->label); #else - entry.key = tipnodes[i]->label; + entry.key = tree->nodes[i]->label; #endif entry.data = (void *)(data+i); hsearch(entry, ENTER); @@ -182,7 +180,6 @@ int main(int argc, char * argv[]) /* we no longer need these two arrays (keys and values of hash table... */ free(data); - free(tipnodes); /* ...neither the sequences and the headers as they are already present in the form of probabilities in the tip CLVs */ @@ -196,7 +193,7 @@ int main(int argc, char * argv[]) /* allocate a buffer for storing pointers to nodes of the tree in postorder traversal */ - travbuffer = (pll_rtree_t **)malloc(nodes_count * sizeof(pll_rtree_t *)); + travbuffer = (pll_rnode_t **)malloc(nodes_count * sizeof(pll_rnode_t *)); branch_lengths = (double *)malloc(branch_count * sizeof(double)); matrix_indices = (unsigned int *)malloc(branch_count * sizeof(int)); @@ -204,16 +201,19 @@ int main(int argc, char * argv[]) sizeof(pll_operation_t)); /* get inner nodes */ - inner_nodes_list = (pll_rtree_t **)malloc(inner_nodes_count * - sizeof(pll_rtree_t *)); - pll_rtree_query_innernodes(tree, inner_nodes_list); + inner_nodes_list = (pll_rnode_t **)malloc(inner_nodes_count * + sizeof(pll_rnode_t *)); + memcpy(inner_nodes_list, + tree->nodes+tip_nodes_count, + inner_nodes_count*sizeof(pll_rnode_t *)); unsigned int traversal_size; /* compute a partial traversal starting from the randomly selected inner node */ - if (!pll_rtree_traverse(tree, + if (!pll_rtree_traverse(tree->root, + PLL_TREE_TRAVERSE_POSTORDER, cb_rfull_traversal, travbuffer, &traversal_size)) @@ -265,8 +265,8 @@ int main(int argc, char * argv[]) index for the concrete branch length, and the index of the model of whose frequency vector is to be used */ double logl = pll_compute_root_loglikelihood(partition, - tree->clv_index, - tree->scaler_index, + tree->root->clv_index, + tree->root->scaler_index, params_indices, NULL); printf("Log-L: %f (pinv = %f)\n", logl, prop_invar_list[j]);