| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "pll.h" |
| 23 |
#include <stdarg.h> |
| 24 |
#include <search.h> |
| 25 |
#include <time.h> |
| 26 |
|
| 27 |
#define STATES 4 |
| 28 |
#define RATE_CATS 4 |
| 29 |
|
| 30 |
static void fatal(const char * format, ...) __attribute__ ((noreturn)); |
| 31 |
|
| 32 |
static void * xmalloc(size_t size) |
| 33 |
{ |
| 34 |
void * t; |
| 35 |
t = malloc(size); |
| 36 |
if (!t) |
| 37 |
fatal("Unable to allocate enough memory."); |
| 38 |
|
| 39 |
return t; |
| 40 |
} |
| 41 |
|
| 42 |
static char * xstrdup(const char * s) |
| 43 |
{ |
| 44 |
size_t len = strlen(s); |
| 45 |
char * p = (char *)xmalloc(len+1); |
| 46 |
return strcpy(p,s); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
static int cb_full_traversal(pll_unode_t * node) |
| 51 |
{ |
| 52 |
return 1; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
static void set_missing_branch_length(pll_utree_t * tree, double length) |
| 57 |
{ |
| 58 |
unsigned int i; |
| 59 |
|
| 60 |
for (i = 0; i < tree->tip_count; ++i) |
| 61 |
if (!tree->nodes[i]->length) |
| 62 |
tree->nodes[i]->length = length; |
| 63 |
|
| 64 |
for (i = tree->tip_count; i < tree->tip_count + tree->inner_count; ++i) |
| 65 |
{ |
| 66 |
if (!tree->nodes[i]->length) |
| 67 |
tree->nodes[i]->length = length; |
| 68 |
if (!tree->nodes[i]->next->length) |
| 69 |
tree->nodes[i]->next->length = length; |
| 70 |
if (!tree->nodes[i]->next->next->length) |
| 71 |
tree->nodes[i]->next->next->length = length; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
static void fatal(const char * format, ...) |
| 76 |
{ |
| 77 |
va_list argptr; |
| 78 |
va_start(argptr, format); |
| 79 |
vfprintf(stderr, format, argptr); |
| 80 |
va_end(argptr); |
| 81 |
fprintf(stderr, "\n"); |
| 82 |
exit(EXIT_FAILURE); |
| 83 |
} |
| 84 |
|
| 85 |
int main(int argc, char * argv[]) |
| 86 |
{ |
| 87 |
unsigned int i; |
| 88 |
unsigned int tip_nodes_count, inner_nodes_count, nodes_count, branch_count; |
| 89 |
unsigned int matrix_count, ops_count; |
| 90 |
unsigned int * matrix_indices; |
| 91 |
double * branch_lengths; |
| 92 |
pll_partition_t * partition; |
| 93 |
pll_operation_t * operations; |
| 94 |
pll_unode_t ** travbuffer; |
| 95 |
unsigned int params_indices[RATE_CATS] = {0,0,0,0}; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
if (argc != 3) |
| 100 |
fatal(" syntax: %s [newick] [fasta]", argv[0]); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
pll_utree_t * tree = pll_utree_parse_newick(argv[1]); |
| 105 |
if (!tree) |
| 106 |
fatal("Tree must be an unrooted binary tree"); |
| 107 |
|
| 108 |
tip_nodes_count = tree->tip_count; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
set_missing_branch_length(tree, 0.000001); |
| 113 |
|
| 114 |
|
| 115 |
inner_nodes_count = tip_nodes_count - 2; |
| 116 |
nodes_count = inner_nodes_count + tip_nodes_count; |
| 117 |
branch_count = nodes_count - 1; |
| 118 |
|
| 119 |
printf("Number of tip/leaf nodes in tree: %d\n", tip_nodes_count); |
| 120 |
printf("Number of inner nodes in tree: %d\n", inner_nodes_count); |
| 121 |
printf("Total number of nodes in tree: %d\n", nodes_count); |
| 122 |
printf("Number of branches in tree: %d\n", branch_count); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
hcreate(tip_nodes_count); |
| 140 |
|
| 141 |
|
| 142 |
unsigned int * data = (unsigned int *)xmalloc(tip_nodes_count * |
| 143 |
sizeof(unsigned int)); |
| 144 |
for (i = 0; i < tip_nodes_count; ++i) |
| 145 |
{ |
| 146 |
data[i] = tree->nodes[i]->clv_index; |
| 147 |
ENTRY entry; |
| 148 |
#ifdef __APPLE__ |
| 149 |
entry.key = xstrdup(tree->nodes[i]->label); |
| 150 |
#else |
| 151 |
entry.key = tree->nodes[i]->label; |
| 152 |
#endif |
| 153 |
entry.data = (void *)(data+i); |
| 154 |
hsearch(entry, ENTER); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
pll_fasta_t * fp = pll_fasta_open(argv[2], pll_map_fasta); |
| 159 |
if (!fp) |
| 160 |
fatal("Error opening file %s", argv[2]); |
| 161 |
|
| 162 |
char * seq = NULL; |
| 163 |
char * hdr = NULL; |
| 164 |
long seqlen; |
| 165 |
long hdrlen; |
| 166 |
long seqno; |
| 167 |
|
| 168 |
|
| 169 |
char ** headers = (char **)calloc(tip_nodes_count, sizeof(char *)); |
| 170 |
char ** seqdata = (char **)calloc(tip_nodes_count, sizeof(char *)); |
| 171 |
|
| 172 |
|
| 173 |
int sites = -1; |
| 174 |
for (i = 0; pll_fasta_getnext(fp,&hdr,&hdrlen,&seq,&seqlen,&seqno); ++i) |
| 175 |
{ |
| 176 |
if (i >= tip_nodes_count) |
| 177 |
fatal("FASTA file contains more sequences than expected"); |
| 178 |
|
| 179 |
if (sites != -1 && sites != seqlen) |
| 180 |
fatal("FASTA file does not contain equal size sequences\n"); |
| 181 |
|
| 182 |
if (sites == -1) sites = seqlen; |
| 183 |
|
| 184 |
headers[i] = hdr; |
| 185 |
seqdata[i] = seq; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
if (pll_errno != PLL_ERROR_FILE_EOF) |
| 190 |
fatal("Error while reading file %s", argv[2]); |
| 191 |
|
| 192 |
|
| 193 |
pll_fasta_close(fp); |
| 194 |
|
| 195 |
if (sites == -1) |
| 196 |
fatal("Unable to read alignment"); |
| 197 |
|
| 198 |
if (i != tip_nodes_count) |
| 199 |
fatal("Some taxa are missing from FASTA file"); |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
partition = pll_partition_create(tip_nodes_count, |
| 216 |
inner_nodes_count, |
| 217 |
STATES, |
| 218 |
(unsigned int)sites, |
| 219 |
1, |
| 220 |
branch_count, |
| 221 |
RATE_CATS, |
| 222 |
inner_nodes_count, |
| 223 |
PLL_ATTRIB_PATTERN_TIP | PLL_ATTRIB_ARCH_AVX); |
| 224 |
|
| 225 |
|
| 226 |
double frequencies[4] = { 0.17, 0.19, 0.25, 0.39 }; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
double subst_params[6] = {1,1,1,1,1,1}; |
| 231 |
|
| 232 |
|
| 233 |
double rate_cats[4] = {0}; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
pll_compute_gamma_cats(1, 4, rate_cats, PLL_GAMMA_RATES_MEAN); |
| 238 |
|
| 239 |
|
| 240 |
pll_set_frequencies(partition, 0, frequencies); |
| 241 |
|
| 242 |
|
| 243 |
pll_set_subst_params(partition, 0, subst_params); |
| 244 |
|
| 245 |
|
| 246 |
pll_set_category_rates(partition, rate_cats); |
| 247 |
|
| 248 |
|
| 249 |
for (i = 0; i < tip_nodes_count; ++i) |
| 250 |
{ |
| 251 |
ENTRY query; |
| 252 |
query.key = headers[i]; |
| 253 |
ENTRY * found = NULL; |
| 254 |
|
| 255 |
found = hsearch(query,FIND); |
| 256 |
|
| 257 |
if (!found) |
| 258 |
fatal("Sequence with header %s does not appear in the tree", headers[i]); |
| 259 |
|
| 260 |
unsigned int tip_clv_index = *((unsigned int *)(found->data)); |
| 261 |
|
| 262 |
pll_set_tip_states(partition, tip_clv_index, pll_map_nt, seqdata[i]); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
hdestroy(); |
| 267 |
|
| 268 |
|
| 269 |
free(data); |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
for(i = 0; i < tip_nodes_count; ++i) |
| 274 |
{ |
| 275 |
free(seqdata[i]); |
| 276 |
free(headers[i]); |
| 277 |
} |
| 278 |
free(seqdata); |
| 279 |
free(headers); |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
travbuffer = (pll_unode_t **)xmalloc(nodes_count * sizeof(pll_unode_t *)); |
| 285 |
|
| 286 |
branch_lengths = (double *)xmalloc(branch_count * sizeof(double)); |
| 287 |
matrix_indices = (unsigned int *)xmalloc(branch_count * sizeof(unsigned int)); |
| 288 |
operations = (pll_operation_t *)xmalloc(inner_nodes_count * |
| 289 |
sizeof(pll_operation_t)); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
pll_unode_t * root = tree->nodes[tip_nodes_count+inner_nodes_count-1]; |
| 296 |
unsigned int traversal_size; |
| 297 |
|
| 298 |
if (!pll_utree_traverse(root, |
| 299 |
PLL_TREE_TRAVERSE_POSTORDER, |
| 300 |
cb_full_traversal, |
| 301 |
travbuffer, |
| 302 |
&traversal_size)) |
| 303 |
fatal("Function pll_utree_traverse() requires inner nodes as parameters"); |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
pll_utree_create_operations(travbuffer, |
| 309 |
traversal_size, |
| 310 |
branch_lengths, |
| 311 |
matrix_indices, |
| 312 |
operations, |
| 313 |
&matrix_count, |
| 314 |
&ops_count); |
| 315 |
|
| 316 |
|
| 317 |
printf ("Traversal size: %d\n", traversal_size); |
| 318 |
printf ("Operations: %d\n", ops_count); |
| 319 |
printf ("Probability Matrices: %d\n", matrix_count); |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
pll_update_prob_matrices(partition, |
| 326 |
params_indices, |
| 327 |
matrix_indices, |
| 328 |
branch_lengths, |
| 329 |
matrix_count); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
pll_update_partials(partition, operations, ops_count); |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
double logl = pll_compute_edge_loglikelihood(partition, |
| 363 |
root->clv_index, |
| 364 |
root->scaler_index, |
| 365 |
root->back->clv_index, |
| 366 |
root->back->scaler_index, |
| 367 |
root->pmatrix_index, |
| 368 |
params_indices, |
| 369 |
NULL); |
| 370 |
printf("Log-L: %f\n", logl); |
| 371 |
|
| 372 |
|
| 373 |
pll_partition_destroy(partition); |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
free(travbuffer); |
| 378 |
free(branch_lengths); |
| 379 |
free(matrix_indices); |
| 380 |
free(operations); |
| 381 |
|
| 382 |
|
| 383 |
pll_utree_destroy(tree,NULL); |
| 384 |
|
| 385 |
return (EXIT_SUCCESS); |
| 386 |
} |