| 1 | /* Phylogenetic trees. |
| 2 | * |
| 3 | * SRE, Tue May 2 13:54:30 2006 [St. Louis] |
| 4 | */ |
| 5 | #ifndef eslTREE_INCLUDED |
| 6 | #define eslTREE_INCLUDED |
| 7 | #include <esl_config.h> |
| 8 | |
| 9 | #include "esl_dmatrix.h" |
| 10 | #include "esl_random.h" |
| 11 | |
| 12 | /* Object: ESL_TREE |
| 13 | * |
| 14 | * All trees are represented as rooted trees, starting from |
| 15 | * node 0. For N taxa, there are N-1 internal nodes, numbered |
| 16 | * 0..N-2. Taxa on leaves are numbered 0..N-1, and represented |
| 17 | * in <left> and <right> as negative numbers. |
| 18 | * |
| 19 | */ |
| 20 | typedef struct { |
| 21 | int N; /* number of taxa */ |
| 22 | |
| 23 | /* (Mandatory) information for the internal nodes of a rooted tree. |
| 24 | * There are N-1 nodes, numbered 0..N-2, with the root at 0, |
| 25 | * so each array below is indexed [0..N-2]. |
| 26 | * When an internal node has a left or right branch to a taxon, |
| 27 | * <left>/<right> are <= 0, negative <taxon #>; if they're to |
| 28 | * be used as array indices, flip their sign. |
| 29 | * There is no ambiguity between taxon 0/root node 0, because |
| 30 | * a taxon can't be a parent, and the root node can't be a child. |
| 31 | * For an unrooted tree, by convention, taxon 0 is the outgroup: T->left[0] = 0, |
| 32 | * and T->rd[0] = 0.0. |
| 33 | */ |
| 34 | int *parent; /* index of parent of node: values are 0..N-2; parent of root 0 = 0 */ |
| 35 | int *left; /* index of left child: values are -(N-1)..0=taxa; 1..N-2=nodes */ |
| 36 | int *right; /* index of right child: values are -(N-1)..0=taxa; 1..N-2=nodes */ |
| 37 | double *ld; /* left branch length under node: values are >= 0 */ |
| 38 | double *rd; /* right branch length under node: values are >= 0 */ |
| 39 | /* in linkage trees, ld[x]=rd[x]= "height" (linkage value) of node, not branch lengths */ |
| 40 | |
| 41 | /* Derived (optional) information, that we can reconstruct if |
| 42 | * we need to from the mandatory info above. |
| 43 | */ |
| 44 | int *taxaparent; /* for taxa [0..N-1]: index of its parent node, 0..N-2. [esl_tree_SetTaxaParents()] */ |
| 45 | int *cladesize; /* for nodes [0..N-2]: # taxa in this clade, 1..N [esl_tree_SetCladesizes()] */ |
| 46 | |
| 47 | /* Optional information */ |
| 48 | char **taxonlabel; /* labels for taxa: [0..N-1] array of char strings */ |
| 49 | char **nodelabel; /* labels for nodes: [0..N-2] array of char strings */ |
| 50 | |
| 51 | /* Tree mode options. */ |
| 52 | int is_linkage_tree; /* TRUE if this is a linkage tree; if FALSE, it's an additive tree */ |
| 53 | |
| 54 | |
| 55 | /* Tree output options. */ |
| 56 | int show_unrooted; /* TRUE to output 'root' as a trifurcation (a la PHYLIP) */ |
| 57 | int show_node_labels; /* TRUE to output labels for interior nodes */ |
| 58 | int show_root_branchlength; /* TRUE to show 0.0 branch length to root node (a la TreeAlign) */ |
| 59 | int show_branchlengths; /* TRUE to output branch lengths */ |
| 60 | int show_quoted_labels; /* TRUE to output ALL labels as quoted labels */ |
| 61 | int show_numeric_taxonlabels;/* TRUE to output taxa labels as their 0..N-1 indices if no other taxonlabel is present */ |
| 62 | |
| 63 | /* Memory allocation information, when growing a tree (on input, for example) |
| 64 | */ |
| 65 | int nalloc; /* current allocated # of taxa */ |
| 66 | |
| 67 | } ESL_TREE; |
| 68 | |
| 69 | /* UPGMA, average-link, minimum-link, and maximum-link clustering are |
| 70 | * all implemented by one algorithm, cluster_engine(), in esl_tree.c. |
| 71 | * We define some flags (within the scope of the tree module) to |
| 72 | * control the behavior, as we call the algorithm engine from four |
| 73 | * different API functions. |
| 74 | */ |
| 75 | #define eslUPGMA 0 |
| 76 | #define eslWPGMA 1 |
| 77 | #define eslSINGLE_LINKAGE 2 |
| 78 | #define eslCOMPLETE_LINKAGE 3 |
| 79 | |
| 80 | |
| 81 | |
| 82 | /* 1. The ESL_TREE object. |
| 83 | */ |
| 84 | extern ESL_TREE *esl_tree_Create(int ntaxa); |
| 85 | extern ESL_TREE *esl_tree_CreateGrowable(int nalloc); |
| 86 | extern ESL_TREE *esl_tree_CreateFromString(char *s); |
| 87 | extern int esl_tree_Grow(ESL_TREE *T); |
| 88 | extern int esl_tree_SetTaxaParents(ESL_TREE *T); |
| 89 | extern int esl_tree_SetCladesizes(ESL_TREE *T); |
| 90 | extern int esl_tree_SetTaxonlabels(ESL_TREE *T, char **names); |
| 91 | extern int esl_tree_RenumberNodes(ESL_TREE *T); |
| 92 | extern int esl_tree_VerifyUltrametric(ESL_TREE *T); |
| 93 | extern int esl_tree_Validate(ESL_TREE *T, char *errbuf); |
| 94 | extern void esl_tree_Destroy(ESL_TREE *T); |
| 95 | |
| 96 | /* 2. Newick format i/o |
| 97 | */ |
| 98 | extern int esl_tree_WriteNewick(FILE *fp, ESL_TREE *T); |
| 99 | extern int esl_tree_ReadNewick(FILE *fp, char *errbuf, ESL_TREE **ret_T); |
| 100 | |
| 101 | /* 3. Tree comparison algorithms. |
| 102 | */ |
| 103 | extern int esl_tree_Compare(ESL_TREE *T1, ESL_TREE *T2); |
| 104 | |
| 105 | /* 4. Clustering algorithms for distance-based tree construction. |
| 106 | */ |
| 107 | extern int esl_tree_UPGMA(ESL_DMATRIX *D, ESL_TREE **ret_T); |
| 108 | extern int esl_tree_WPGMA(ESL_DMATRIX *D, ESL_TREE **ret_T); |
| 109 | extern int esl_tree_SingleLinkage(ESL_DMATRIX *D, ESL_TREE **ret_T); |
| 110 | extern int esl_tree_CompleteLinkage(ESL_DMATRIX *D, ESL_TREE **ret_T); |
| 111 | |
| 112 | /* 5. Generating simulated trees. |
| 113 | */ |
| 114 | extern int esl_tree_Simulate(ESL_RANDOMNESS *r, int N, ESL_TREE **ret_T); |
| 115 | extern int esl_tree_ToDistanceMatrix(ESL_TREE *T, ESL_DMATRIX **ret_D); |
| 116 | |
| 117 | |
| 118 | #endif /*eslTREE_INCLUDED*/ |
| 119 |