Skip to content

Commit

Permalink
pll_utree_newick_export/pll_rtree_newick_export now accepts a callbac…
Browse files Browse the repository at this point in the history
…k function for customizing the format of exported newick tree, added an example, minor memleak fix for deallocating the data element on tip nodes - issue #131
  • Loading branch information
flouris authored and flouris committed Apr 12, 2017
1 parent c50df20 commit 1cda40b
Show file tree
Hide file tree
Showing 18 changed files with 381 additions and 53 deletions.
2 changes: 1 addition & 1 deletion examples/lg4/lg4.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int main(int argc, char * argv[])
pll_utree_show_ascii(tree, PLL_UTREE_SHOW_LABEL |
PLL_UTREE_SHOW_BRANCH_LENGTH |
PLL_UTREE_SHOW_CLV_INDEX);
char * newick = pll_utree_export_newick(tree);
char * newick = pll_utree_export_newick(tree,NULL);
printf("%s\n", newick);
free(newick);
*/
Expand Down
2 changes: 1 addition & 1 deletion examples/load-utree/load-utree.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int main(int argc, char * argv[])

/* export the tree to newick format with the selected inner node as the root
of the unrooted binary tree */
char * newick = pll_utree_export_newick(root);
char * newick = pll_utree_export_newick(root,NULL);

printf("%s\n", newick);

Expand Down
44 changes: 44 additions & 0 deletions examples/newick-export/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright (C) 2015 Tomas Flouri
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact: Tomas Flouri <Tomas.Flouri@h-its.org>,
# Heidelberg Institute for Theoretical Studies,
# Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany

# Profiling
#PROFILING=-g -pg
PROFILING=-g

# Compiler warnings
WARN=-Wall -Wsign-compare

CC = gcc
CFLAGS = -Wall -g -O3 -D_GNU_SOURCE $(WARN)
LINKFLAGS = -L. $(PROFILING)
LIBS=-lpll -lm
PROG=newick-export

all: $(PROG)

OBJS=newick-export.o

$(PROG): $(OBJS)
$(CC) $(LINKFLAGS) -o $@ $+ $(LIBS) $(LDFLAGS)

%.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

clean:
rm -f *~ $(OBJS) gmon.out $(PROG)
191 changes: 191 additions & 0 deletions examples/newick-export/newick-export.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
Copyright (C) 2015 Tomas Flouri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact: Tomas Flouri <Tomas.Flouri@h-its.org>,
Exelixis Lab, Heidelberg Instutute for Theoretical Studies
Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
*/

#include "pll.h"
#include <stdarg.h>
#include <time.h>

static void fatal(const char * format, ...) __attribute__ ((noreturn));

static void fatal(const char * format, ...)
{
va_list argptr;
va_start(argptr, format);
vfprintf(stderr, format, argptr);
va_end(argptr);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}

typedef struct nodedata_s
{
double support;
double rvalue;
} nodedata_t;

/* a callback function that prints the following information for the two types
* of nodes, tips and inners:
for tip nodes:
label, branch length and support value
for inner nodes:
label, branch length, support value, and a random value
Note that this is not a standard format that can be read by all tree viewers
Check the extended newick format for a more widespread notation:
http://dmi.uib.es/~gcardona/BioInfo/enewick.html
*/
static char * cb_serialize(const pll_unode_t * node)
{
char * s = NULL;
nodedata_t * nd;


/* inner node */
if (node->next)
{
/* find which node of the round-about has the data element */
if (node->data)
nd = (nodedata_t *)(node->data);
else if (node->next->data)
nd = (nodedata_t *)(node->next->data);
else
nd = (nodedata_t *)(node->next->next->data);

asprintf(&s,
"%s[&support=%f,rvalue=%f]:%f",
node->label ? node->label : "",
nd->support,
nd->rvalue,
node->length);
}
else
{
nd = (nodedata_t *)(node->data);
asprintf(&s,
"%s[&support=%f]:%f",
node->label ? node->label : "",
nd->support,
node->length);
}

return s;
}

pll_utree_t * load_tree_unrooted(const char * filename)
{
pll_utree_t * utree;
pll_rtree_t * rtree;

if (!(rtree = pll_rtree_parse_newick(filename)))
{
if (!(utree = pll_utree_parse_newick(filename)))
{
fprintf(stderr, "%s\n", pll_errmsg);
return NULL;
}
}
else
{
utree = pll_rtree_unroot(rtree);

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, utree->tip_count);

pll_rtree_destroy(rtree,NULL);
}

return utree;
}

int main(int argc, char * argv[])
{
unsigned int i;

if (argc != 2)
fatal("syntax: %s [newick]", argv[0]);

/* initialize pseudo-random number generator */
srandom(time(NULL));

/* load tree as unrooted */
pll_utree_t * utree = load_tree_unrooted(argv[1]);
if (!utree)
fatal("Tree must be a rooted or unrooted binary.");

/* set random support values for tip nodes */
for (i = 0; i < utree->tip_count; ++i)
{
pll_unode_t * node = utree->nodes[i];
nodedata_t * data;

node->data = malloc(sizeof(nodedata_t));

data = (nodedata_t *)(node->data);

data->support = random() / (double)RAND_MAX;
}

/* set random support values and a random value to each inner node, but
allocate the structure in only one of the three round-about nodes that make
up an inner node */
for (i = utree->tip_count; i < utree->tip_count + utree->inner_count; ++i)
{
pll_unode_t * node = utree->nodes[i];
nodedata_t * data;

node->data = malloc(sizeof(nodedata_t));

data = (nodedata_t *)(node->data);

data->support = random() / (double)RAND_MAX;
data->rvalue = data->support * random();
}

/* select a random inner node */
long int r = random() % utree->inner_count;
pll_unode_t * root = utree->nodes[utree->tip_count + r];

/* export the tree to newick format with the selected inner node as the root
of the unrooted binary tree.
If we pass NULL as the callback function, then the tree is printed in newick
format with branch lengths only, i.e.
char * newick = pll_utree_export_newick(root,NULL);
*/

char * newick = pll_utree_export_newick(root,cb_serialize);

printf("%s\n", newick);

free(newick);

pll_utree_destroy(utree,free);

return 0;
}
2 changes: 2 additions & 0 deletions examples/newick-export/target.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
newick_exportdir = $(docdir)/examples/newick-export
dist_newick_export_DATA = newick-export/newick-export.c newick-export/Makefile
2 changes: 1 addition & 1 deletion examples/newick-fasta-rooted/newick-fasta-rooted.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ int main(int argc, char * argv[])
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);
char * newick = pll_rtree_export_newick(tree,NULL);
printf("%s\n", newick);
free(newick);
Expand Down
2 changes: 1 addition & 1 deletion examples/newick-fasta-unrooted/newick-fasta-unrooted.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ int main(int argc, char * argv[])
pll_utree_show_ascii(tree, PLL_UTREE_SHOW_LABEL |
PLL_UTREE_SHOW_BRANCH_LENGTH |
PLL_UTREE_SHOW_CLV_INDEX);
char * newick = pll_utree_export_newick(tree);
char * newick = pll_utree_export_newick(tree,NULL);
printf("%s\n", newick);
free(newick);
Expand Down
2 changes: 1 addition & 1 deletion examples/newick-phylip-unrooted/newick-phylip-unrooted.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ int main(int argc, char * argv[])
pll_utree_show_ascii(tree, PLL_UTREE_SHOW_LABEL |
PLL_UTREE_SHOW_BRANCH_LENGTH |
PLL_UTREE_SHOW_CLV_INDEX);
char * newick = pll_utree_export_newick(tree);
char * newick = pll_utree_export_newick(tree,NULL);
printf("%s\n", newick);
free(newick);
Expand Down
2 changes: 1 addition & 1 deletion examples/parsimony/npr-pars.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ int main(int argc, char * argv[])
pll_utree_show_ascii(tree, PLL_UTREE_SHOW_LABEL |
PLL_UTREE_SHOW_BRANCH_LENGTH |
PLL_UTREE_SHOW_CLV_INDEX);
char * newick = pll_utree_export_newick(tree);
char * newick = pll_utree_export_newick(tree,NULL);
printf("%s\n", newick);
free(newick);
Expand Down
2 changes: 1 addition & 1 deletion examples/partial-traversal/partial.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ int main(int argc, char * argv[])
pll_utree_show_ascii(tree, PLL_UTREE_SHOW_LABEL |
PLL_UTREE_SHOW_BRANCH_LENGTH |
PLL_UTREE_SHOW_CLV_INDEX);
char * newick = pll_utree_export_newick(tree);
char * newick = pll_utree_export_newick(tree,NULL);
printf("%s\n", newick);
free(newick);
Expand Down
2 changes: 1 addition & 1 deletion examples/protein-list/protein-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ int main(int argc, char * argv[])
pll_utree_show_ascii(tree, PLL_UTREE_SHOW_LABEL |
PLL_UTREE_SHOW_BRANCH_LENGTH |
PLL_UTREE_SHOW_CLV_INDEX);
char * newick = pll_utree_export_newick(tree);
char * newick = pll_utree_export_newick(tree,NULL);
printf("%s\n", newick);
free(newick);
Expand Down
2 changes: 1 addition & 1 deletion examples/stepwise/stepwise.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ int main(int argc, char * argv[])

/* export the tree to newick format with the selected inner node as the root
of the unrooted binary tree */
char * newick = pll_utree_export_newick(root);
char * newick = pll_utree_export_newick(root,NULL);
printf("%s\n", newick);
free(newick);

Expand Down
1 change: 1 addition & 0 deletions src/parse_utree.y
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ PLL_EXPORT void pll_utree_destroy(pll_utree_t * tree,
/* deallocate tip nodes */
for (i = 0; i < tree->tip_count; ++i)
{
dealloc_data(tree->nodes[i], cb_destroy);
if (tree->nodes[i]->label)
free(tree->nodes[i]->label);
free(tree->nodes[i]);
Expand Down
6 changes: 4 additions & 2 deletions src/pll.h
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,8 @@ PLL_EXPORT pll_utree_t * pll_utree_wraptree(pll_unode_t * root,

PLL_EXPORT void pll_utree_show_ascii(const pll_unode_t * tree, int options);

PLL_EXPORT char * pll_utree_export_newick(const pll_unode_t * root);
PLL_EXPORT char * pll_utree_export_newick(const pll_unode_t * root,
char * (*cb_serialize)(const pll_unode_t *));

PLL_EXPORT int pll_utree_traverse(pll_unode_t * root,
int traversal,
Expand Down Expand Up @@ -697,7 +698,8 @@ PLL_EXPORT void pll_msa_destroy(pll_msa_t * msa);

PLL_EXPORT void pll_rtree_show_ascii(const pll_rnode_t * root, int options);

PLL_EXPORT char * pll_rtree_export_newick(const pll_rnode_t * root);
PLL_EXPORT char * pll_rtree_export_newick(const pll_rnode_t * root,
char * (*cb_serialize)(const pll_rnode_t *));

PLL_EXPORT int pll_rtree_traverse(pll_rnode_t * root,
int traversal,
Expand Down
Loading

0 comments on commit 1cda40b

Please sign in to comment.