Debian Codegraph @

csrc/bionj.c

raw symbols SCIP
1 /* modified 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
36 The code below is an implementation of the building tree algorithm
37 described in "BIONJ: an improved version of the NJ algorithm based
38 on a simple model of sequence data." (1997) O. Gascuel. Mol Biol Evol.
39 14:685-95.
40
41 */
42
43 #include "phyml_util.h"
44
45 /* included functions */
46 void Bionj(matrix *mat);
47 void Finish(matrix *mat);
48 void Compute_Sx(matrix *mat);
49 phydbl Sum_S(matrix *mat, int i);
50 phydbl Dist(matrix *mat, int x, int y);
51 phydbl Q_Agglo(matrix *mat, int x, int y);
52 phydbl Variance(matrix *mat, int x, int y);
53 phydbl Br_Length(matrix *mat, int x, int y);
54 void Update_Dist(matrix *mat, int x, int y);
55 phydbl Lamda(matrix *mat, int x, int y, phydbl vxy);
56 void Best_Pair(matrix *mat, int *x, int *y, phydbl *score);
57 phydbl Var_Red(matrix *mat, int x, int y, int i, phydbl lamda, phydbl vxy);
58 void Update_Tree(matrix *mat, int x, int y, phydbl lx, phydbl ly, phydbl score);
59 void Update_Mat(matrix *mat, int x, int y,
60 phydbl lx, phydbl ly, phydbl vxy, phydbl lamda);
61 phydbl Dist_Red(matrix *mat, int x, phydbl lx, int y,
62 phydbl ly, int i, phydbl lamda);
63 int Bionj_Br_Length_Post(node *a, node *d, matrix *mat);
64 void Bionj_Br_Length(matrix *mat);
65
66 extern void Clean_Tree_Connections(arbre *tree);
67 extern void Connect_One_Edge_To_Two_Nodes(node *a, node *d, edge *b, arbre *tree);
68 extern void Free(void *p);
69
70
71 void Bionj(matrix *mat)
72 {
73 int x,y,i;
74 phydbl vxy,lx,ly,lamda,score;
75
76 Clean_Tree_Connections(mat->tree);
77 For(i,mat->tree->n_otu) mat->tip_node[i] = mat->tree->noeud[i];
78 mat->tree->num_curr_branch_available = 0;
79
80 while(mat->r > 3)
81 {
82 x = y = 0;
83 score = .0;
84 Compute_Sx(mat);
85 Best_Pair(mat,&x,&y,&score);
86 vxy=Variance(mat,x,y);
87 lx=Br_Length(mat,x,y);
88 ly=Br_Length(mat,y,x);
89 lamda=Lamda(mat,x,y,vxy);
90 Update_Mat(mat,x,y,lx,ly,vxy,lamda);
91 Update_Tree(mat,x,y,lx,ly,score);
92 }
93 Finish(mat);
94 }
95
96 /*********************************************************/
97
98 void Finish(matrix *mat)
99 {
100 phydbl dxy,dxz,dyz;
101 int x,y,z;
102 node *nx,*ny,*nz,*new;
103 int i;
104
105 x = y = z = -1;
106
107 For(i,mat->n_otu)
108 {
109 if(mat->on_off[i])
110 {
111 if(x < 0) x=i;
112 else if(y < 0) y = i;
113 else if(z < 0) z = i;
114 }
115 }
116
117 dxy = Dist(mat,x,y);
118 dxz = Dist(mat,x,z);
119 dyz = Dist(mat,y,z);
120
121 nx = mat->tip_node[x];
122 ny = mat->tip_node[y];
123 nz = mat->tip_node[z];
124
125 new = mat->tree->noeud[mat->curr_int];
126 new->num = mat->curr_int;
127 new->v[0] = nx;
128 new->v[1] = ny;
129 new->v[2] = nz;
130
131 nx->v[0] = new;
132 ny->v[0] = new;
133 nz->v[0] = new;
134
135
136 Connect_One_Edge_To_Two_Nodes(new,nx,mat->tree->t_edges[mat->tree->num_curr_branch_available],mat->tree);
137 mat->tree->num_curr_branch_available++;
138 Connect_One_Edge_To_Two_Nodes(new,ny,mat->tree->t_edges[mat->tree->num_curr_branch_available],mat->tree);
139 mat->tree->num_curr_branch_available++;
140 Connect_One_Edge_To_Two_Nodes(new,nz,mat->tree->t_edges[mat->tree->num_curr_branch_available],mat->tree);
141 mat->tree->num_curr_branch_available++;
142
143
144 nx->b[0]->l = .5*(dxy-dyz+dxz);
145 ny->b[0]->l = .5*(dyz-dxz+dxy);
146 nz->b[0]->l = .5*(dxz-dxy+dyz);
147
148 new->b[0]->l = nx->b[0]->l;
149 new->b[1]->l = ny->b[0]->l;
150 new->b[2]->l = nz->b[0]->l;
151 }
152
153 /*********************************************************/
154
155 void Update_Mat(matrix *mat, int x, int y, phydbl lx, phydbl ly, phydbl vxy, phydbl lamda)
156 {
157 int i;
158 int a,b;
159
160 For(i,mat->n_otu)
161 {
162 if((mat->on_off[i]) && (i != x) && (i != y))
163 {
164 if(x > i)
165 {
166 a=x;
167 b=i;
168 }
169 else
170 {
171 a=i;
172 b=x;
173 }
174 mat->dist[a][b]=Dist_Red(mat,x,lx,y,ly,i,lamda);
175 mat->dist[b][a]=Var_Red(mat,x,y,i,lamda,vxy);
176 }
177 }
178 }
179
180 /*********************************************************/
181
182 void Update_Tree(matrix *mat, int x, int y, phydbl lx, phydbl ly, phydbl score)
183 {
184 node *new, *nx, *ny;
185
186 nx = mat->tip_node[x];
187 ny = mat->tip_node[y];
188 new = mat->tree->noeud[mat->curr_int];
189 nx->v[0] = new;
190 ny->v[0] = new;
191 new->v[1] = nx;
192 new->v[2] = ny;
193 new->num = mat->curr_int;
194
195
196 Connect_One_Edge_To_Two_Nodes(new,nx,mat->tree->t_edges[mat->tree->num_curr_branch_available],mat->tree);
197 mat->tree->num_curr_branch_available++;
198 Connect_One_Edge_To_Two_Nodes(new,ny,mat->tree->t_edges[mat->tree->num_curr_branch_available],mat->tree);
199 mat->tree->num_curr_branch_available++;
200
201 nx->b[0]->l = lx;
202 ny->b[0]->l = ly;
203
204 new->b[1]->l = lx;
205 new->b[2]->l = ly;
206 new->score[0] = score;
207
208 nx->l[0] = lx;
209 ny->l[0] = ly;
210
211 new->l[1] = lx;
212 new->l[2] = ly;
213
214 mat->tip_node[x] = new;
215 mat->on_off[y] = 0;
216 mat->curr_int++;
217 mat->r--;
218 }
219
220 /*********************************************************/
221
222 void Best_Pair(matrix *mat, int *x, int *y,phydbl *score)
223 {
224 int i,j/* ,n_ties */;
225 phydbl Qmin,Qmin2;
226 phydbl *t_Qij;
227 /* int *ties; */
228
229 t_Qij = (phydbl *)mCalloc(mat->n_otu * mat->n_otu,sizeof(phydbl ));
230 /* ties = (int *)mCalloc(mat->n_otu * mat->n_otu,sizeof(int )); */
231
232 Qmin = 1.e+10;
233
234 For(i,mat->n_otu)
235 {
236 if(mat->on_off[i])
237 {
238 for(j=0;j<i;j++)
239 {
240 if(mat->on_off[j])
241 {
242 t_Qij[mat->n_otu*i+j] = Q_Agglo(mat,i,j);
243
244 if(t_Qij[mat->n_otu*i+j] < Qmin - 1.E-05)
245 {
246 *x = i;
247 *y = j;
248 Qmin = t_Qij[mat->n_otu*i+j];
249 }
250 }
251 }
252 }
253 }
254
255 /* n_ties = 0; */
256 /* For(i,mat->n_otu) */
257 /* { */
258 /* if(mat->on_off[i]) */
259 /* { */
260 /* for(j=0;j<i;j++) */
261 /* { */
262 /* if(mat->on_off[j]) */
263 /* { */
264 /* if((t_Qij[mat->n_otu*i+j] < Qmin + 1.E-05) && (t_Qij[mat->n_otu*i+j] > Qmin - 1.E-05)) */
265 /* { */
266 /* ties[n_ties] = mat->n_otu*i+j; */
267 /* n_ties++; */
268 /* } */
269 /* } */
270 /* } */
271 /* } */
272 /* } */
273
274 /* if(!n_ties) */
275 /* { */
276 /* printf("\n. Err in file %s at line %d\n",__FILE__,__LINE__); */
277 /* Warn_And_Exit(""); */
278 /* } */
279
280
281 /* /\* Useful if some pairwise distances are null *\/ */
282 /* if(n_ties > 1) */
283 /* { */
284 /* int cand; */
285 /* *x = *y = -1; */
286 /* cand = (int)rint(rand()/(phydbl)(RAND_MAX) * (n_ties-1)); */
287 /* *x = (int)(ties[cand] / mat->n_otu); */
288 /* *y = (int)(ties[cand] % mat->n_otu); */
289 /* } */
290
291
292
293 Qmin2 = 1e+10;
294
295 For(i,mat->n_otu)
296 {
297 if((i != *y) && (i != *x) && (t_Qij[mat->n_otu*(*x)+i] < Qmin2)) Qmin2 = t_Qij[mat->n_otu*(*x)+i];
298 }
299
300 For(i,mat->n_otu)
301 {
302 if((i != *y) && (i != *x) && (t_Qij[mat->n_otu*i+(*y)] < Qmin2)) Qmin2 = t_Qij[mat->n_otu*i+(*y)];
303 }
304
305 *score = fabs(Qmin2 - Qmin)/fabs(Qmin);
306
307 Free(t_Qij);
308 /* Free(ties); */
309 }
310
311 /*********************************************************/
312
313 void Compute_Sx(matrix *mat)
314 {
315 int i,j;
316
317 For(i,mat->n_otu)
318 {
319 mat->dist[i][i] = .0;
320 if(mat->on_off[i])
321 {
322 For(j,mat->n_otu)
323 {
324 if((i != j) && (mat->on_off[j]))
325 {
326 mat->dist[i][i] += Dist(mat,i,j);
327 }
328 }
329 }
330 }
331 }
332
333 /*********************************************************/
334
335 phydbl Sum_S(matrix *mat, int i)
336 {
337 return mat->dist[i][i];
338 }
339
340 /*********************************************************/
341
342 phydbl Dist(matrix *mat, int x, int y)
343 {
344 if(x > y)
345 return(mat->dist[x][y]);
346 else
347 return(mat->dist[y][x]);
348 }
349
350 /*********************************************************/
351
352 phydbl Variance(matrix *mat, int x, int y)
353 {
354 if(x > y)
355 {
356 return(mat->dist[y][x]);
357 }
358 else
359 {
360 return(mat->dist[x][y]);
361 }
362 }
363
364 /*********************************************************/
365
366 phydbl Br_Length(matrix *mat, int x, int y)
367 {
368 return .5*(Dist(mat,x,y)+
369 (Sum_S(mat,x)-Sum_S(mat,y))/(phydbl)(mat->r-2.));
370 }
371
372 /*********************************************************/
373
374 phydbl Dist_Red(matrix *mat, int x, phydbl lx, int y, phydbl ly, int i, phydbl lamda)
375 {
376 phydbl Dui;
377 Dui=lamda*(Dist(mat,x,i)-lx)
378 +(1.-lamda)*(Dist(mat,y,i)-ly);
379 return(Dui);
380 }
381
382 /*********************************************************/
383
384 phydbl Var_Red(matrix *mat, int x, int y, int i, phydbl lamda, phydbl vxy)
385 {
386 phydbl Vui;
387 Vui=lamda*(Variance(mat,x,i))
388 +(1.-lamda)*(Variance(mat,y,i))
389 -lamda*(1.-lamda)*vxy;
390 return(Vui);
391 }
392
393 /*********************************************************/
394
395 phydbl Lamda(matrix *mat, int x, int y, phydbl vxy)
396 {
397 phydbl lamda=0.0;
398 int i;
399
400 if(mat->method == 0) /* NJ (Saitou & Nei, 1987) */
401 lamda = 0.5;
402 else /* BioNJ (Gascuel, 1997) */
403 {
404 if(vxy==0.0)
405 lamda=0.5;
406 else
407 {
408 For(i,mat->n_otu)
409 {
410 if((x != i) && (y != i) && (mat->on_off[i]))
411 lamda = lamda + Variance(mat,y,i) - Variance(mat,x,i);
412 }
413 lamda = 0.5 + lamda/(2.*(mat->r-2)*vxy);
414 }
415
416 if(lamda > 1.0)
417 lamda = 0.5;/*1.0;*/
418 else if(lamda < 0.0)
419 lamda = 0.5;/*0.0;*/
420 }
421
422 return(lamda);
423 }
424
425 /*********************************************************/
426
427 phydbl Q_Agglo(matrix *mat, int x, int y)
428 {
429 phydbl Qxy;
430
431 Qxy=(mat->r-2.)*Dist(mat,x,y)-Sum_S(mat,x)-Sum_S(mat,y);
432 return(Qxy);
433 }
434
435 /*********************************************************/
436
437 void Bionj_Br_Length(matrix *mat)
438 {
439 int x;
440
441 x = Bionj_Br_Length_Post(mat->tree->noeud[0],
442 mat->tree->noeud[0]->v[0],
443 mat);
444 mat->tree->noeud[0]->b[0]->l = Dist(mat,0,x);
445 }
446
447 /*********************************************************/
448
449 int Bionj_Br_Length_Post(node *a, node *d, matrix *mat)
450 {
451 int i;
452
453 if(d->tax)
454 {
455 return d->num;
456 }
457 else
458 {
459 int d_v1, d_v2;
460 phydbl lx, ly, vxy,lamda;
461 int x,y;
462
463 d_v1 = d_v2 = -1;
464 For(i,3)
465 if(d->v[i] != a) {(d_v1 < 0)?(d_v1 = i):(d_v2 = i);}
466
467
468 x = Bionj_Br_Length_Post(d,d->v[d_v1],mat);
469 y = Bionj_Br_Length_Post(d,d->v[d_v2],mat);
470
471 Compute_Sx(mat);
472 vxy=Variance(mat,(x),(y));
473 lx=Br_Length(mat,(x),(y));
474 ly=Br_Length(mat,(y),(x));
475 lamda=Lamda(mat,(x),(y),vxy);
476 Update_Mat(mat,(x),(y),lx,ly,vxy,lamda);
477
478 d->b[d_v1]->l = lx;
479 d->b[d_v2]->l = ly;
480
481 mat->on_off[y] = 0;
482 mat->r--;
483
484 return x;
485 }
486 }
487
488 /*********************************************************/
489
490
491
492
493
494