Debian Codegraph @

csrc/phyml_util.h

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
35 #ifndef PHYML_UTIL_H
36 #define PHYML_UTIL_H
37
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <math.h>
42 #include <ctype.h>
43 #include <string.h>
44 #include <time.h>
45
46 #define VERSION "v3.0"
47
48 #define For(i,n) for(i=0; i<n; i++)
49 #define Fors(i,n,s) for(i=0; i<n; i+=s)
50 #define PointGamma(prob,alpha,beta) PointChi2(prob,2.0*(alpha))/(2.0*(beta))
51 #define SHFT2(a,b,c) (a)=(b);(b)=(c);
52 #define SHFT3(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
53 #define MAX(a,b) ((a)>(b)?(a):(b))
54 #define MIN(a,b) ((a)<(b)?(a):(b))
55 #define SIGN(a,b) ((b) > 0.0 ? fabs(a) : -fabs(a))
56 #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
57
58 #define NNI_MOVE 0
59 #define SPR_MOVE 1
60
61
62 #define NT 0 /* nucleotides */
63 #define AA 1 /* amino acids */
64
65 #define ACGT 0 /* A,G,G,T encoding */
66 #define RY 1 /* R,Y encoding */
67
68 #define INTERFACE_DATA_TYPE 0
69 #define INTERFACE_MULTIGENE 1
70 #define INTERFACE_MODEL 2
71 #define INTERFACE_TOPO_SEARCH 3
72 #define INTERFACE_BRANCH_SUPPORT 4
73
74
75 #define N_MAX_OPTIONS 100
76
77
78 #define T_MAX_FILE 100
79 #define T_MAX_LINE 2000000
80 #define T_MAX_SEQ 2000000
81 #define T_MAX_OPTION 100
82
83 #define NODE_DEG_MAX 50
84 #define BRENT_ITMAX 10000
85 #define BRENT_CGOLD 0.3819660
86 #define BRENT_ZEPS 1.e-10
87 #define MNBRAK_GOLD 1.618034
88 #define MNBRAK_GLIMIT 100.0
89 #define MNBRAK_TINY 1.e-20
90 #define ALPHA_MIN 0.04
91 #define ALPHA_MAX 100
92 #define BL_MIN 1.e-06
93 #define BL_START 1.e-04
94 #define BL_MAX 100.0
95 #define GOLDEN_R 0.61803399
96 #define GOLDEN_C (1.0-GOLDEN_R)
97 #define N_MAX_INSERT 20
98 #define N_MAX_OTU 4000
99 #define UNLIKELY -1.e10
100 #define NJ_SEUIL 0.1
101 #define ROUND_MAX 100
102 #define AROUND_LK 50.0
103 #define PROP_STEP 1.0
104 #define T_MAX_ALPHABET 100
105 #define MDBL_MIN 2.225074E-308
106 #define MDBL_MAX 1.797693E+308
107 #define POWELL_ITMAX 200
108 #define LINMIN_TOL 2.0E-04
109 #define LIM_SCALE 3
110 #define LIM_SCALE_VAL 1.E-50
111 /* #define LIM_SCALE 3000 */
112 /* #define LIM_SCALE_VAL 1.E-500 */
113 #define DEFAULT_SIZE_SPR_LIST 20
114 #define OUTPUT_TREE_FORMAT 0 /* 0-->Newick; 1-->Nexus */
115 #define MAX_PARS 1000000000
116
117 #define JC69 1
118 #define K80 2
119 #define F81 3
120 #define HKY85 4
121 #define F84 5
122 #define TN93 6
123 #define GTR 7
124 #define CUSTOM 8
125
126 #define WAG 11
127 #define DAYHOFF 12
128 #define JTT 13
129 #define BLOSUM62 14
130 #define MTREV 15
131 #define RTREV 16
132 #define CPREV 17
133 #define DCMUT 18
134 #define VT 19
135 #define MTMAM 20
136 #define MTART 21
137 #define HIVW 22
138 #define HIVB 23
139 #define CUSTOMAA 24
140
141 typedef double phydbl;
142
143 /*********************************************************/
144
145 typedef struct __Node {
146 struct __Node **v; /* table of pointers to neighbor nodes. Dimension = 2 x n_otu - 3 */
147 struct __Node ***bip_node; /* three lists of pointer to tip nodes. One list for each direction */
148 struct __Edge **b; /* table of pointers to neighbor branches */
149 struct __Node ***list_of_reachable_tips; /* list of tip nodes that can be reached in each direction from that node */
150
151 int *n_of_reachable_tips; /* sizes of the list_of_reachable_tips (in each direction) */
152 int *bip_size; /* Size of each of the three lists from bip_node */
153 int num; /* node number */
154 int tax; /* tax = 1 -> external node, else -> internal node */
155 int check_branch; /* check_branch=1 is the corresponding branch is labelled with '*' */
156 char ***bip_name; /* three lists of tip node names. One list for each direction */
157 char *name; /* taxon name (if exists) */
158
159 phydbl *score; /* score used in BIONJ to determine the best pair of nodes to agglomerate */
160 phydbl *l; /* lengths of the (three or one) branche(s) connected this node */
161 phydbl dist_to_root;
162 }node;
163
164
165 /*********************************************************/
166
167 typedef struct __Edge {
168 /*
169 syntax : (node) [edge]
170 (left_1) . .(right_1)
171 \ (left) (right) /
172 \._____________./
173 / [b_fcus] \
174 / \
175 (left_2) . .(right_2)
176
177 */
178
179 struct __Node *left,*rght; /* node on the left/right side of the edge */
180 int l_r,r_l,l_v1,l_v2,r_v1,r_v2;
181 /* these are directions (i.e., 0, 1 or 2): */
182 /* l_r (left to right) -> left[b_fcus->l_r] = right */
183 /* r_l (right to left) -> right[b_fcus->r_l] = left */
184 /* l_v1 (left node to first node != from right) -> left[b_fcus->l_v1] = left_1 */
185 /* l_v2 (left node to secnd node != from right) -> left[b_fcus->l_v2] = left_2 */
186 /* r_v1 (right node to first node != from left) -> right[b_fcus->r_v1] = right_1 */
187 /* r_v2 (right node to secnd node != from left) -> right[b_fcus->r_v2] = right_2 */
188
189 struct __NNI *nni;
190
191
192 int num; /* branch number */
193 phydbl l; /* branch length */
194 phydbl best_l; /* best branch length found so far */
195 phydbl l_old; /* old branch length */
196
197 int bip_score; /* score of the bipartition generated by the corresponding edge
198 bip_score = 1 iif the branch is fond in both trees to be compared,
199 bip_score = 0 otherwise. */
200
201 phydbl ***p_lk_left,***p_lk_rght; /* likelihoods of the subtree on the left and
202 right side (for each site and each relative rate category) */
203 short int **p_lk_tip_r,**p_lk_tip_l;
204
205 double ***Pij_rr; /* matrix of change probabilities and its first and secnd derivates */
206 int *pars_l,*pars_r; /* parsimony of the subtree on the left and right sides (for each site) */
207 unsigned int *ui_l, *ui_r; /* union - intersection vectors used in Fitch's parsimony algorithm */
208 int **p_pars_l, **p_pars_r; /* conditional parsimony vectors */
209
210 int num_st_left; /* number of the subtree on the left side */
211 int num_st_rght; /* number of the subtree on the right side */
212
213
214 /* Below are the likelihood scaling factors (used in functions
215 `Get_All_Partial_Lk_Scale' in lk.c */
216 int scale_left;
217 int scale_rght;
218 phydbl *sum_scale_f_left;
219 phydbl *sum_scale_f_rght;
220
221 phydbl bootval; /* bootstrap value (if exists) */
222
223 short int is_alive; /* is_alive = 1 if this edge is used in a tree */
224
225 phydbl dist_btw_edges;
226 int topo_dist_btw_edges;
227
228 int has_zero_br_len;
229
230 phydbl ratio_test; /* approximate likelihood ratio test */
231 phydbl alrt_statistic; /* aLRT statistic */
232 int num_tax_left, num_tax_rght; /* number of taxa in subtrees */
233 phydbl avg_dist_left, avg_dist_rght; /* average taxon distance in subtrees */
234
235 int is_p_lk_l_u2d; /* is the conditional likelihood vector on the left up
236 to data ? */
237 int is_p_lk_r_u2d; /* is the conditional likelihood vector on the right up
238 to data ? */
239
240 }edge;
241
242 /*********************************************************/
243
244 typedef struct __Arbre {
245 struct __Node *root; /* root node */
246 struct __Node **noeud; /* array of nodes that defines the tree topology */
247 struct __Edge **t_edges; /* array of edges */
248 struct __Arbre *old_tree; /* old copy of the tree */
249 struct __Arbre *best_tree; /* best tree found so far */
250 struct __Model *mod; /* substitution model */
251 struct __AllSeq *data; /* sequences */
252 struct __Option *input; /* input parameters */
253 struct __Matrix *mat; /* pairwise distance matrix */
254 struct __Edge **t_dead_edges;
255 struct __Node **t_dead_nodes;
256 struct __Node **curr_path; /* list of nodes that form a path in the tree */
257
258
259 int depth_curr_path; /* depth of the node path defined by curr_path */
260 int has_bip; /*if has_bip=1, then the structure to compare
261 tree topologies is allocated, has_bip=0 otherwise */
262 int n_otu; /* number of taxa */
263 int curr_site; /* current site of the alignment to be processed */
264 int curr_catg; /* current class of the discrete gamma rate distribution */
265 int n_swap; /* number of NNIs performed */
266 int n_pattern; /* number of distinct site patterns */
267 int has_branch_lengths; /* =1 iff input tree displays branch lengths */
268 int print_boot_val; /* if print_boot_val=1, the bootstrap values are printed */
269 int print_alrt_val; /* if print_boot_val=1, the bootstrap values are printed */
270 int both_sides; /* both_sides=1 -> a pre-order and a post-order tree
271 traversals are required to compute the likelihood
272 of every subtree in the phylogeny*/
273 int num_curr_branch_available; /*gives the number of the next cell in t_edges that is free to receive a pointer to a branch */
274 int n_dead_edges;
275 int n_dead_nodes;
276 int **t_dir;
277 int n_improvements;
278 int n_moves;
279
280 int dp; /* Data partition */
281 int s_mod_num; /* Substitution model number */
282 int number_of_lk_calls;
283 int number_of_branch_lk_calls;
284
285 phydbl init_lnL;
286 phydbl best_lnL; /* highest value of the loglikelihood found so far */
287 phydbl c_lnL; /* loglikelihood */
288 phydbl *c_lnL_sorted; /* used to compute c_lnL by adding sorted terms to minimize CPU errors */
289 phydbl *site_lk; /* vector of likelihoods at individual sites */
290 phydbl **log_site_lk_cat; /* loglikelihood at individual sites and for each class of rate*/
291 phydbl unconstraint_lk; /* unconstrained (or multinomial) likelihood */
292 phydbl prop_of_sites_to_consider;
293 phydbl lnL_lower_bound;
294 phydbl **log_lks_aLRT; /* used to compute several branch supports */
295
296 int *site_pars;
297 int c_pars;
298 int *step_mat;
299
300 struct __SPR **spr_list;
301 struct __SPR *best_spr;
302 int size_spr_list;
303 int perform_spr_right_away;
304
305 phydbl dnorm_thresh;
306
307
308 time_t t_beg;
309 time_t t_current;
310
311 struct __Triplet *triplet_struct;
312
313 int branch_and_bound;
314 }arbre;
315
316 /*********************************************************/
317
318 typedef struct __Super_Arbre {
319 struct __Arbre *tree;
320 struct __List_Arbre *treelist; /* list of trees. One tree for each data set to be processed */
321 struct __AllSeq *data_of_interest;
322 struct __Option **optionlist; /* list of pointers to input structures (used in supertrees) */
323
324 struct __Node ***match_st_node_in_gt;
325 /* match_st_in_gt_node[subdataset number][supertree node number]
326 * gives the node in tree estimated from 'subdataset number' that corresponds
327 * to 'supertree node number' in the supertree
328 */
329
330 struct __Node *****map_st_node_in_gt;
331 /* mat_st_gt_node[gt_num][st_node_num][direction] gives the
332 * node in gt gt_num that maps node st_node_num in st.
333 */
334
335 struct __Edge ***map_st_edge_in_gt;
336 /* map_st_gt_br[gt_num][st_branch_num] gives the
337 * branch in gt gt_num that maps branch st_branch_num
338 * in st.
339 */
340
341 struct __Edge ****map_gt_edge_in_st;
342 /* mat_gt_st_br[gt_num][gt_branch_num][] is the list of
343 * branches in st that map branch gt_branch_num
344 * in gt gt_num.
345 */
346
347 int **size_map_gt_edge_in_st;
348 /* size_map_gt_st_br[gt_num][gt_branch_num] gives the
349 * size of the list map_gt_st_br[gt_num][gt_branch_num][]
350 */
351
352
353 struct __Edge ***match_st_edge_in_gt;
354 /* match_st_edge_in_gt[gt_num][st_branch_num] gives the
355 * branch in gt gt_num that matches branch st_branch_num
356 */
357
358 struct __Edge ***match_gt_edge_in_st;
359 /* match_gt_edge_in_st[gt_num][gt_branch_num] gives the
360 * branch in st that matches branch gt_branch_num
361 */
362
363 struct __Node ****closest_match;
364 /* closest_match[gt_num][st_node_num][dir] gives the
365 * closest node in st that matches a node in gt gt_num
366 */
367
368 int ***closest_dist;
369 /* closest_dist[gt_num][st_node_num][dir] gives the
370 * number of edges to traverse to get to node
371 * closest_match[gt_num][st_node_num][dir]
372 */
373
374 int n_gt;
375 /* number of trees */
376
377 phydbl **bl;
378 /* bl[gt_num][gt_branch_num] gives the length of
379 * branch gt_branch_num
380 */
381
382 phydbl **bl_cpy;
383 /* copy of bl */
384
385 phydbl **bl0;
386 /* bl estimated during NNI (original topo)
387 * See Mg_NNI.
388 */
389
390 phydbl **bl1;
391 /* bl estimated during NNI (topo conf 1)
392 * See Mg_NNI.
393 */
394
395 phydbl **bl2;
396 /* bl estimated during NNI (topo conf 2)
397 * See Mg_NNI.
398 */
399
400 int *bl_partition;
401 /* partition[gt_num] gives the edge partition number
402 * gt_num belongs to.
403 */
404 int n_bl_partition;
405
406
407
408 struct __Model **s_mod; /* substitution model */
409
410 int n_s_mod;
411 int lock_br_len;
412
413 }superarbre;
414
415 /*********************************************************/
416
417 typedef struct __List_Arbre { /* a list of trees */
418 struct __Arbre **tree;
419 int list_size; /* number of trees in the list */
420 }arbrelist;
421
422 /*********************************************************/
423
424 typedef struct __Seq {
425 char *name; /* sequence name */
426 int len; /* sequence length */
427 char *state; /* sequence itself */
428 short int *is_ambigu; /* is_ambigu[site] = 1 if state[site] is an ambiguous character. 0 otherwise */
429 }seq;
430
431 /*********************************************************/
432
433
434 typedef struct __AllSeq {
435 struct __Seq **c_seq; /* compressed sequences */
436 phydbl *b_frq; /* observed state frequencies */
437 short int *invar; /* 1 -> states are identical, 0 states vary */
438 int *wght; /* # of each site in c_seq */
439 short int *ambigu; /* ambigu[i]=1 is one or more of the sequences at site
440 i display an ambiguous character */
441 phydbl obs_pinvar;
442 int n_otu; /* number of taxa */
443 int clean_len; /* uncrunched sequences lenghts without gaps */
444 int crunch_len; /* crunched sequences lengths */
445 int init_len; /* length of the uncompressed sequences */
446 int *sitepatt; /* this array maps the position of the patterns in the
447 compressed alignment to the positions in the uncompressed
448 one */
449 }allseq;
450
451 /*********************************************************/
452
453 typedef struct __Matrix { /* mostly used in BIONJ */
454 phydbl **P,**Q,**dist; /* observed proportions of transition, transverion and distances
455 between pairs of sequences */
456
457 arbre *tree; /* tree... */
458 int *on_off; /* on_off[i]=1 if column/line i corresponds to a node that has not
459 been agglomerated yet */
460 int n_otu; /* number of taxa */
461 char **name; /* sequence names */
462 int r; /* number of nodes that have not been agglomerated yet */
463 struct __Node **tip_node; /* array of pointer to the leaves of the tree */
464 int curr_int; /* used in the NJ/BIONJ algorithms */
465 int method; /* if method=1->NJ method is used, BIONJ otherwise */
466 }matrix;
467
468 /*********************************************************/
469
470 typedef struct __Model {
471 int whichmodel;
472 /*
473 1 => JC69
474 2 => K80
475 3 => F81
476 4 => HKY85
477 5 => F84
478 6 => TN93
479 7 => GTR
480 11 => Dayhoff
481 12 => JTT
482 13 => MtREV
483 */
484 char *modelname;
485 int ns; /* number of states (4 for ADN, 20 for AA) */
486 phydbl *pi; /* states frequencies */
487 int datatype; /* 0->DNA, 1->AA */
488
489 /* ADN parameters */
490 phydbl kappa; /* transition/transversion rate */
491 phydbl lambda; /* parameter used to define the ts/tv ratios in the F84 and TN93 models */
492 phydbl alpha; /* gamma shapa parameter */
493 phydbl *r_proba; /* probabilities of the substitution rates defined by the discrete gamma distribution */
494 phydbl *rr; /* substitution rates defined by the discrete gamma distribution */
495 int asked_n_catg; /* number of categories in the discrete gamma distribution */
496 int n_catg; /* number of categories in the discrete gamma distribution */
497 phydbl pinvar; /* proportion of invariable sites */
498 int invar; /* =1 iff the substitution model takes into account invariable sites */
499
500 /* Below are 'old' values of some substitution parameters (see the comments above) */
501 phydbl alpha_old;
502 phydbl kappa_old;
503 phydbl lambda_old;
504 phydbl pinvar_old;
505
506 char *custom_mod_string; /* string of characters used to define custom
507 models of substitution */
508 phydbl **rr_param; /* table of pointers to relative rate parameters of the GTR or custom model */
509 phydbl *rr_param_values; /* relative rate parameters of the GTR or custom model */
510 int *rr_param_num;
511 int *n_rr_param_per_cat; /* [3][1][2] for the previous example */
512 int n_diff_rr_param; /* number of different relative substitution rates in the custom model */
513
514 int update_eigen; /* update_eigen=1-> eigen values/vectors need to be updated */
515
516 double ***Pij_rr; /* matrix of change probabilities */
517
518
519 int seq_len; /* sequence length */
520
521 /* AA parameters */
522 /* see PMat_Empirical in models.c for AA algorithm explanation */
523 double *mat_Q; /* 20x20 amino-acids substitution rates matrix */
524 double *mat_Vr; /* 20x20 right eigenvectors of mat_Q */
525 double *mat_Vi; /* 20x20 inverse matrix of mat_Vr */
526 double *vct_ev; /* eigen values */
527 double *expt;
528 double *vct_eDmr; /* diagonal terms of a 20x20 diagonal matrix */
529 double *uexpt;
530
531
532 phydbl mr; /* mean rate = branch length/time interval */
533 /* mr = -sum(i)(vct_pi[i].mat_Q[ii]) */
534
535 /* term n = exp(nth eigenvalue of mat_Q / mr) */
536 int stepsize; /* stepsize=1 for nucleotide models, 3 for codon models */
537 int n_otu; /* number of taxa */
538 struct __Optimiz *s_opt; /* pointer to parameters to optimize */
539 int bootstrap; /* bootstrap values are computed if bootstrap > 0.
540 The value give the number of replicates */
541 phydbl *user_b_freq; /* user-defined nucleotide frequencies */
542
543
544 }model;
545
546 arbre *Make_Tree_From_Scratch(int n_otu, allseq *data);
547 void Bionj(matrix *mat);
548 char *Write_Tree(arbre *tree);
549 void Free_Tree(arbre *tree);
550 void Free_Mat(matrix *mat);
551 matrix *JC69_Dist(allseq *data, model *mod);
552 matrix *Make_Mat(int n_otu);
553 void Init_Mat(matrix *mat, allseq *data);
554 void *mCalloc(int nb, size_t size);
555 int Is_Ambigu(char *state, int datatype, int stepsize);
556
557 #endif