Debian Codegraph @

csrc/phyml_util.c

raw symbols SCIP
1 /* extracted from
2
3 PHYML : a program that computes maximum likelihood phylogenies from
4 DNA or AA homologous sequences
5
6 Copyright (C) Stephane Guindon. Oct 2003 onward
7
8 This program is free software; you can redistribute it and/or modify it under the terms of the
9 GNU General Public License as published by the Free Software Foundation; either version 2 of
10 the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
13 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along with this program; if not,
17 see <http://www.gnu.org/licenses>.
18
19 Linking PHYML statically or dynamically with other modules is making a combined work based on PHYML.
20 Thus, the terms and conditions of the GNU General Public License cover the whole combination.
21
22 In addition, as a special exception, the copyright holders of PHYML give you permission to combine
23 PHYML with free software programs or libraries that are released under the GNU LGPL and with code
24 included in the standard release of PHYLIP under the University of Washington license (or modified
25 versions of such code, with unchanged license). You may copy and distribute such a system following
26 the terms of the GNU GPL for PHYML and the licenses of the other code concerned.
27
28 Note that people who make modified versions of PHYML are not obligated to grant this special exception
29 for their modified versions; it is their choice whether to do so. The GNU General Public License gives
30 permission to release a modified version without this exception; this exception also makes it possible
31 to release a modified version which carries forward this exception.
32
33 */
34 #include "phyml_util.h"
35
36 extern int tree_build_interrupted;
37
38 /*********************************************************/
39
40 void Warn_And_Exit(char *s)
41 {
42 fprintf(stdout,"%s",s);
43 fflush(NULL);
44 #ifndef BATCH
45 char c;
46 fprintf(stdout,"\n. Type any key to exit.\n");
47 fscanf(stdin,"%c",&c);
48 #endif
49 exit(1);
50 }
51
52 /*********************************************************/
53
54 void *mCalloc(int nb, size_t size)
55 {
56 void *allocated;
57
58 if((allocated = calloc((size_t)nb,(size_t)size)) != NULL)
59 {
60 return allocated;
61 }
62 else
63 Warn_And_Exit("\n. Err: low memory\n");
64
65 return NULL;
66 }
67
68 /*********************************************************/
69
70 matrix *Make_Mat(int n_otu)
71 {
72 matrix *mat;
73 int i;
74
75 mat = (matrix *)mCalloc(1,sizeof(matrix));
76
77 mat->n_otu = n_otu;
78
79 mat->P = (phydbl **)mCalloc(n_otu,sizeof(phydbl *));
80 mat->Q = (phydbl **)mCalloc(n_otu,sizeof(phydbl *));
81 mat->dist = (phydbl **)mCalloc(n_otu,sizeof(phydbl *));
82 mat->on_off = (int *)mCalloc(n_otu,sizeof(int));
83 mat->name = (char **)mCalloc(n_otu,sizeof(char *));
84 mat->tip_node = (node **)mCalloc(n_otu,sizeof(node *));
85
86
87 For(i,n_otu)
88 {
89 mat->P[i] = (phydbl *)mCalloc(n_otu,sizeof(phydbl));
90 mat->Q[i] = (phydbl *)mCalloc(n_otu,sizeof(phydbl));
91 mat->dist[i] = (phydbl *)mCalloc(n_otu,sizeof(phydbl));
92 }
93
94 return mat;
95 }
96
97
98 /*********************************************************/
99
100 void Init_Mat(matrix *mat, allseq *data)
101 {
102 int i;
103
104 mat->n_otu = data->n_otu;
105 mat->r = mat->n_otu;
106 mat->curr_int = mat->n_otu;
107 mat->method = 1;
108
109 For(i,data->n_otu)
110 {
111 mat->name[i] = strdup(data->c_seq[i]->name);
112 mat->on_off[i] = 1;
113 }
114 }
115
116 /*********************************************************/
117
118 matrix *JC69_Dist(allseq *data, model *mod)
119 {
120 int site,i,j,k;
121 phydbl x;
122 matrix *mat;
123 phydbl **len;
124
125
126 len = (phydbl **)mCalloc(data->n_otu,sizeof(phydbl *));
127 For(i,data->n_otu)
128 len[i] = (phydbl *)mCalloc(data->n_otu,sizeof(phydbl));
129
130
131 mat = Make_Mat(data->n_otu);
132 Init_Mat(mat,data);
133
134 Fors(site,data->c_seq[0]->len,mod->stepsize)
135 {
136 if (tree_build_interrupted) {
137 break;
138 }
139 For(j,data->n_otu-1)
140 {
141 for(k=j+1;k<data->n_otu;k++)
142 {
143 if((!Is_Ambigu(data->c_seq[j]->state+site,mod->datatype,mod->stepsize)) &&
144 (!Is_Ambigu(data->c_seq[k]->state+site,mod->datatype,mod->stepsize)))
145 {
146 len[j][k]+=data->wght[site];
147 len[k][j]=len[j][k];
148 if(strncmp(data->c_seq[j]->state+site,
149 data->c_seq[k]->state+site,
150 mod->stepsize))
151 mat->P[j][k]+=data->wght[site];
152 }
153 }
154 }
155 }
156
157
158 For(i,data->n_otu-1)
159 for(j=i+1;j<data->n_otu;j++)
160 {
161 if(len[i][j])
162 {
163 mat->P[i][j] /= len[i][j];
164 }
165 else
166 {
167 mat->P[i][j] = 1.;
168 }
169
170 mat->P[j][i] = mat->P[i][j];
171
172 x = 1.-(mod->ns/(mod->ns-1.))*mat->P[i][j];
173 if(x <= .0)
174 {
175 Free_Mat(mat);
176 mat = NULL;
177 goto out;
178 }
179 else
180 mat->dist[i][j] = -((mod->ns-1.)/mod->ns)*(phydbl)log(x);
181
182 mat->dist[j][i] = mat->dist[i][j];
183 }
184
185 out:For(i,data->n_otu) free(len[i]);
186 free(len);
187
188 return mat;
189 }
190
191 /*********************************************************/
192
193 void Clean_Tree_Connections(arbre *tree)
194 {
195
196 int i;
197 For(i,2*tree->n_otu-2)
198 {
199 tree->noeud[i]->v[0] = NULL;
200 tree->noeud[i]->v[1] = NULL;
201 tree->noeud[i]->v[2] = NULL;
202 tree->noeud[i]->b[0] = NULL;
203 tree->noeud[i]->b[1] = NULL;
204 tree->noeud[i]->b[2] = NULL;
205 }
206 }
207
208 /*********************************************************/
209
210 void Make_Tree_Path(arbre *tree)
211 {
212 tree->curr_path = (node **)mCalloc(tree->n_otu,sizeof(node *));
213 }
214
215 /*********************************************************/
216
217 void Init_Node_Light(node *n, int num)
218 {
219 n->list_of_reachable_tips = NULL;
220 n->num = num;
221 n->tax = -1;
222 }
223
224 /*********************************************************/
225
226 node *Make_Node_Light(int num)
227 {
228 node *n;
229 n = (node *)mCalloc(1,sizeof(node));
230 n->v = (node **)mCalloc(3,sizeof(node *));
231 n->l = (phydbl *)mCalloc(3,sizeof(phydbl));
232 n->b = (edge **)mCalloc(3,sizeof(edge *));
233 n->score = (phydbl *)mCalloc(3,sizeof(phydbl));
234 Init_Node_Light(n,num);
235 return n;
236 }
237
238 /*********************************************************/
239
240 void Make_All_Tree_Nodes(arbre *tree)
241 {
242 int i;
243 tree->noeud = (node **)mCalloc(2*tree->n_otu-2,sizeof(node *));
244 tree->t_dead_nodes = (node **)mCalloc(2*tree->n_otu-2,sizeof(node *));
245
246 For(i,2*tree->n_otu-2)
247 {
248 tree->noeud[i] = (node *)Make_Node_Light(i);
249 if(i < tree->n_otu) tree->noeud[i]->tax = 1;
250 else tree->noeud[i]->tax = 0;
251 }
252 }
253
254 /*********************************************************/
255
256 void Init_Tree(arbre *tree, int n_otu)
257 {
258 tree->n_otu = n_otu;
259 tree->best_tree = NULL;
260 tree->old_tree = NULL;
261 tree->mat = NULL;
262
263 tree->depth_curr_path = 0;
264 tree->has_bip = 0;
265 tree->n_moves = 0;
266 tree->n_improvements = 0;
267 tree->number_of_lk_calls = 0;
268 tree->number_of_branch_lk_calls = 0;
269
270 tree->init_lnL = UNLIKELY;
271 tree->best_lnL = UNLIKELY;
272 tree->c_lnL = UNLIKELY;
273 tree->lnL_lower_bound = UNLIKELY;
274 tree->n_swap = 0;
275
276 tree->n_pattern = -1;
277 tree->prop_of_sites_to_consider = 1.;
278
279 tree->branch_and_bound = 0;
280 tree->print_boot_val = 0;
281 tree->print_alrt_val = 0;
282 tree->num_curr_branch_available = 0;
283 }
284
285 /*********************************************************/
286
287 arbre *Make_Tree(int n_otu)
288 {
289 arbre *tree;
290 int i;
291 tree = (arbre *)mCalloc(1,sizeof(arbre ));
292 Init_Tree(tree,n_otu);
293 tree->t_dir = (int **)mCalloc(2*n_otu-2,sizeof(int *));
294 For(i,2*n_otu-2) tree->t_dir[i] = (int *)mCalloc(2*n_otu-2,sizeof(int));
295 return tree;
296 }
297
298 /*********************************************************/
299
300 void Copy_Tax_Names_To_Tip_Labels(arbre *tree, allseq *data)
301 {
302 int i;
303
304 For(i,tree->n_otu)
305 {
306 tree->noeud[i]->name = strdup(data->c_seq[i]->name);
307 tree->noeud[i]->tax = 1;
308 tree->noeud[i]->num = i;
309 }
310 }
311
312 /*********************************************************/
313
314 void Make_List_Of_Reachable_Tips(arbre *tree)
315 {
316 int i,j;
317
318 For(i,2*tree->n_otu-2)
319 {
320 tree->noeud[i]->list_of_reachable_tips = (node ***)mCalloc(3,sizeof(node **));
321 tree->noeud[i]->n_of_reachable_tips = (int *)mCalloc(3,sizeof(int));
322 For(j,3)
323 tree->noeud[i]->list_of_reachable_tips[j] = (node **)mCalloc(tree->n_otu,sizeof(node *));
324 }
325 }
326
327 /*********************************************************/
328
329 void Init_Edge_Light(edge *b, int num)
330 {
331 b->num = num;
332 b->bip_score = 0;
333 b->dist_btw_edges = .0;
334 b->topo_dist_btw_edges = 0;
335 b->has_zero_br_len = 0;
336 b->is_p_lk_l_u2d = 0;
337 b->is_p_lk_r_u2d = 0;
338
339 b->p_lk_left = NULL;
340 b->p_lk_rght = NULL;
341 b->Pij_rr = NULL;
342 }
343
344 /*********************************************************/
345
346 void Make_Edge_Dirs(edge *b, node *a, node *d)
347 {
348 int i;
349
350 if(a == b->rght)
351 {
352 printf("\n. a->num = %3d ; d->num = %3d",a->num,d->num);
353 printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__);
354 Warn_And_Exit("");
355 }
356 if(d == b->left)
357 {
358 printf("\n. a->num = %3d ; d->num = %3d",a->num,d->num);
359 printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__);
360 Warn_And_Exit("");
361 }
362
363 b->l_r = b->r_l = -1;
364 For(i,3)
365 {
366 if((a->v[i]) && (a->v[i] == d))
367 {
368 b->l_r = i; /* we consider here that 'a' is on the left handside of 'b'*/
369 a->b[i] = b;
370 }
371 if((d->v[i]) && (d->v[i] == a))
372 {
373 b->r_l = i; /* we consider here that 'd' is on the right handside of 'b'*/
374 d->b[i] = b;
375 }
376 }
377
378 if(a->tax) {b->r_l = 0; For(i,3) if(d->v[i]==a) {b->l_r = i; break;}}
379
380
381 b->l_v1 = b->l_v2 = b->r_v1 = b->r_v2 = -1;
382 For(i,3)
383 {
384 if(b->left->v[i] != b->rght)
385 {
386 if(b->l_v1 < 0) b->l_v1 = i;
387 else b->l_v2 = i;
388 }
389
390 if(b->rght->v[i] != b->left)
391 {
392 if(b->r_v1 < 0) b->r_v1 = i;
393 else b->r_v2 = i;
394 }
395 }
396 }
397
398 /*********************************************************/
399
400 edge *Make_Edge_Light(node *a, node *d, int num)
401 {
402 edge *b;
403
404 b = (edge *)mCalloc(1,sizeof(edge));
405
406 Init_Edge_Light(b,num);
407
408 if(a && b)
409 {
410 b->left = a; b->rght = d;
411 if(a->tax) {b->rght = a; b->left = d;} /* root */
412 /* a tip is necessary on the right side of the edge */
413
414 (b->left == a)?
415 (Make_Edge_Dirs(b,a,d)):
416 (Make_Edge_Dirs(b,d,a));
417
418 b->l = a->l[b->l_r];
419 if(a->tax) b->l = a->l[b->r_l];
420 if(b->l < BL_MIN) b->l = BL_MIN;
421 else if(b->l > BL_MAX) b->l = BL_MAX;
422 b->l_old = b->l;
423 }
424 else
425 {
426 b->left = NULL;
427 b->rght = NULL;
428 }
429
430 return b;
431
432 }
433
434 /*********************************************************/
435
436 void Make_All_Tree_Edges(arbre *tree)
437 {
438 int i;
439
440 tree->t_edges = (edge **)mCalloc(2*tree->n_otu-3,sizeof(edge *));
441 tree->t_dead_edges = (edge **)mCalloc(2*tree->n_otu-3,sizeof(edge *));
442
443 For(i,2*tree->n_otu-3) tree->t_edges[i] = (edge *)Make_Edge_Light(NULL,NULL,i);
444 }
445
446 /*********************************************************/
447
448 arbre *Make_Tree_From_Scratch(int n_otu, allseq *data)
449 {
450 arbre *tree;
451
452 tree = Make_Tree(n_otu);
453 Make_All_Tree_Nodes(tree);
454 Make_All_Tree_Edges(tree);
455 Make_Tree_Path(tree);
456 Make_List_Of_Reachable_Tips(tree);
457 if(data)
458 {
459 Copy_Tax_Names_To_Tip_Labels(tree,data);
460 tree->data = data;
461 }
462 return tree;
463 }
464
465 /*********************************************************/
466
467 int Is_Ambigu(char *state, int datatype, int stepsize)
468 {
469 int i;
470
471 if(datatype == NT)
472 {
473 For(i,stepsize)
474 {
475 if(strchr("MRWSYKBDHVNXO?-.",state[i]))
476 return 1;
477 }
478 }
479 else
480 {
481 if(strchr("X?-.",state[0])) return 1;
482 }
483
484 return 0;
485 }
486
487 /*********************************************************/
488
489 void R_wtree(node *pere, node *fils, char *s_tree, arbre *tree)
490 {
491 int i,p;
492
493 p = -1;
494 if(fils->tax)
495 {
496 if(OUTPUT_TREE_FORMAT == 0)
497 strcat(s_tree,fils->name);
498 else
499 snprintf(s_tree+(int)strlen(s_tree), 10,"%d",fils->num+1);
500
501 if((fils->b[0]) && (fils->b[0]->l != -1))
502 {
503 strcat(s_tree,":");
504 /* snprintf(s_tree+(int)strlen(s_tree),10,"%.10f",fils->b[0]->l); */
505 snprintf(s_tree+(int)strlen(s_tree), 15,"%f",fils->b[0]->l);
506 }
507 strcpy(s_tree+(int)strlen(s_tree), ",");
508 }
509 else
510 {
511 s_tree[(int)strlen(s_tree)]='(';
512 For(i,3)
513 {
514 if(fils->v[i] != pere)
515 R_wtree(fils,fils->v[i],s_tree,tree);
516 else p=i;
517 }
518 s_tree[(int)strlen(s_tree)-1]=')';
519 if(fils->b[0]->l != -1)
520 {
521 if(tree->print_boot_val)
522 snprintf(s_tree+(int)strlen(s_tree),10,"%d",fils->b[p]->bip_score);
523 else if(tree->print_alrt_val)
524 snprintf(s_tree+(int)strlen(s_tree), 15,"%f",fils->b[p]->ratio_test);
525
526 strcat(s_tree,":");
527 /* snprintf(s_tree+(int)strlen(s_tree),10,"%.10f",fils->b[p]->l); */
528 snprintf(s_tree+(int)strlen(s_tree),15,"%f",fils->b[p]->l);
529
530 strcat(s_tree,",");
531 }
532 }
533 }
534
535 /*********************************************************/
536
537 char *Write_Tree(arbre *tree)
538 {
539
540 char *s;
541 int i;
542
543 s=(char *)mCalloc(T_MAX_LINE,sizeof(char));
544
545 s[0]='(';
546
547 i = 0;
548 while((!tree->noeud[tree->n_otu+i]->v[0]) ||
549 (!tree->noeud[tree->n_otu+i]->v[1]) ||
550 (!tree->noeud[tree->n_otu+i]->v[2])) i++;
551
552 R_wtree(tree->noeud[tree->n_otu+i],tree->noeud[tree->n_otu+i]->v[0],s,tree);
553 R_wtree(tree->noeud[tree->n_otu+i],tree->noeud[tree->n_otu+i]->v[1],s,tree);
554 R_wtree(tree->noeud[tree->n_otu+i],tree->noeud[tree->n_otu+i]->v[2],s,tree);
555
556
557 s[(int)strlen(s)-1]=')';
558 s[(int)strlen(s)]=';';
559
560
561 return s;
562 }
563
564 /*********************************************************/
565
566 void Connect_One_Edge_To_Two_Nodes(node *a, node *d, edge *b, arbre *tree)
567 {
568 int i,dir_a_d;
569
570 dir_a_d = -1;
571 For(i,3) if(a->v[i] == d) {dir_a_d = i; break;}
572
573
574 a->b[dir_a_d] = b;
575 b->num = tree->num_curr_branch_available;
576 b->left = a;
577 b->rght = d;
578 if(a->tax) {b->rght = a; b->left = d;} /* root */
579 /* a tip is necessary on the right side of the edge */
580
581 (b->left == a)?
582 (Make_Edge_Dirs(b,a,d)):
583 (Make_Edge_Dirs(b,d,a));
584
585 b->l = a->l[b->l_r];
586 if(a->tax) b->l = a->l[b->r_l];
587 if(b->l < BL_MIN) b->l = BL_MIN;
588 else if(b->l > BL_MAX) b->l = BL_MAX;
589 b->l_old = b->l;
590 }
591
592 /*********************************************************/
593
594 void Free(void *p)
595 {
596 if(p != NULL) free(p);
597 }
598
599 /*********************************************************/
600
601 void Free_Mat(matrix *mat)
602 {
603 int i;
604
605 For(i,mat->n_otu)
606 {
607 Free(mat->P[i]);
608 Free(mat->Q[i]);
609 Free(mat->dist[i]);
610 Free(mat->name[i]);
611 }
612
613 Free(mat->P);
614 Free(mat->Q);
615 Free(mat->dist);
616 Free(mat->name);
617 Free(mat->tip_node);
618
619 Free(mat->on_off);
620 Free(mat);
621 }
622
623 /*********************************************************/
624
625 void Free_Node(node *n)
626 {
627 int i;
628
629 Free(n->b);
630 Free(n->v);
631 Free(n->l);
632 Free(n->score);
633 Free(n->name);
634
635 if(n->list_of_reachable_tips)
636 {
637 For(i,3) Free(n->list_of_reachable_tips[i]);
638 Free(n->list_of_reachable_tips);
639 Free(n->n_of_reachable_tips);
640 }
641
642 Free(n);
643 }
644
645 /*********************************************************/
646
647 void Free_Edge(edge *b)
648 {
649 Free(b);
650 }
651
652 /*********************************************************/
653
654 void Free_Tree(arbre *tree)
655 {
656 int i,j,k;
657 edge *b;
658 node *n;
659
660 For(i,2*tree->n_otu-2) Free(tree->t_dir[i]);
661 Free(tree->t_dir);
662
663 if(tree->has_bip)
664 {
665 For(i,2*tree->n_otu-2)
666 {
667 Free(tree->noeud[i]->bip_size);
668 For(j,3)
669 {
670 Free(tree->noeud[i]->bip_node[j]);
671 For(k,tree->n_otu) Free(tree->noeud[i]->bip_name[j][k]);
672 Free(tree->noeud[i]->bip_name[j]);
673 }
674 Free(tree->noeud[i]->bip_node);
675 Free(tree->noeud[i]->bip_name);
676 }
677 }
678
679 Free(tree->curr_path);
680
681 For(i,2*tree->n_otu-3)
682 {
683 b = tree->t_edges[i];
684 Free_Edge(b);
685 }
686 Free(tree->t_edges);
687
688
689 For(i,2*tree->n_otu-2)
690 {
691 n = tree->noeud[i];
692 Free_Node(n);
693 }
694 Free(tree->noeud);
695
696
697 For(i,tree->n_dead_edges)
698 Free_Edge(tree->t_dead_edges[i]);
699
700 For(i,tree->n_dead_nodes)
701 Free_Node(tree->t_dead_nodes[i]);
702
703 Free(tree->t_dead_edges);
704 Free(tree->t_dead_nodes);
705
706
707 Free(tree);
708 }