1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
|
#ifndef BIONJ_H
#define BIONJ_H
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; BIONJ program ;
; was obtained from http://www.lirmm.fr/~w3ifa/MAAS/BIONJ/BIONJ.html ; ;
; ;
; Olivier Gascuel ;
; ;
; GERAD - Montreal- Canada ;
; olivierg@crt.umontreal.ca ;
; ;
; LIRMM - Montpellier- France ;
; gascuel@lirmm.fr ;
; ;
; UNIX version, written in C ;
; by Hoa Sien Cuong (Univ. Montreal) ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define PREC 8 /* precision of branch-lengths */
#define PRC 100
#define LEN 1000 /* length of taxon names */
class BioNj {
typedef struct word
{
char name[LEN];
struct word *suiv;
}WORD;
typedef struct pointers
{
WORD *head;
WORD *tail;
}POINTERS;
/*
void Initialize(float **delta, FILE *input, int n, POINTERS *trees);
void Compute_sums_Sx(float **delta, int n);
void Best_pair(float **delta, int r, int *a, int *b, int n);
void Finish(float **delta, int n, POINTERS *trees, FILE *output);
void Concatenate(char chain1[LEN], int ind, POINTERS *trees, int post);
void Print_output(int i, POINTERS *trees, FILE *output);
float Distance(int i, int j, float **delta);
float Variance(int i, int j, float **delta);
float Sum_S(int i, float **delta);
float Agglomerative_criterion(int i, int j, float **delta, int r);
float Branch_length(int a, int b, float **delta, int r);
float Reduction4(int a, float la, int b, float lb, int i, float lamda,
float **delta);
float Reduction10(int a, int b, int i, float lamda, float vab, float
**delta);
float Lamda(int a, int b, float vab, float **delta, int n, int r);
float Finish_branch_length(int i, int j, int k, float **delta);
int Emptied(int i, float **delta);
int Symmetrize(float **delta, int n);
*/
/*;;;;;;;;;;; INPUT, OUTPUT, INITIALIZATION ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; ;
; The delta matrix is read from the input-file. ;
; It is recommended to put it and the executable in ;
; a special directory. The input-file and output-file ;
; can be given as arguments to the executable by ;
; typing them after the executable (Bionj input-file ;
; output-file) or by typing them when asked by the ;
; program. The input-file has to be formated according ;
; the PHYLIP standard. The output file is formated ;
; according to the NEWSWICK standard. ;
; ;
; The lower-half of the delta matrix is occupied by ;
; dissimilarities. The upper-half of the matrix is ;
; occupied by variances. The first column ;
; is initialized as 0; during the algorithm some ;
; indices are no more used, and the corresponding ;
; positions in the first column are set to 1. ;
; ;
; This delta matix is made symmetrical using the rule: ;
; Dij = Dji <- (Dij + Dji)/2. The diagonal is set to 0; ;
; during the further steps of the algorithm, it is used ;
; to store the sums Sx. ;
; ;
; A second array, trees, is used to store taxon names. ;
; During the further steps of the algoritm, some ;
; positions in this array are emptied while the others ;
; are used to store subtrees. ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
/*;;;;;;;;;;;;;;;;;;;;;;;;;; Initialize ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : This function reads an input file and return the ;
; delta matrix and trees: the list of taxa. ;
; ;
; input : ;
; float **delta : delta matrix ;
; FILE *input : pointer to input file ;
; int n : number of taxa ;
; char **trees : list of taxa ;
; ;
; return value: ;
; float **delta : delta matrix ;
; char *trees : list of taxa ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
void Initialize(float **delta, FILE *input, int n, POINTERS *trees)
{
int lig; /* matrix line */
int col; /* matrix column */
float distance;
char name_taxon[LEN]; /* taxon�s name */
WORD *name;
for(lig=1; lig <= n; lig++)
{
fscanf(input,"%s",name_taxon); /* read taxon�s name */
name=(WORD *)calloc(1,sizeof(WORD)); /* taxon�s name is */
if(name == NULL) /* put in trees */
{
printf("Out of memories !!");
exit(0);
}
else
{
strcpy(name->name,name_taxon);
name->suiv=NULL;
trees[lig].head=name;
trees[lig].tail=name;
for(col= 1; col <= n; col++)
{
fscanf(input,"%f",&distance); /* read the distance */
delta[lig][col]=distance;
}
}
}
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;; Print_output;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; ;
; Description : This function prints out the tree in the output file. ;
; ;
; input : ;
; POINTERS *trees : pointer to the subtrees. ;
; int i : indicate the subtree i to be printed. ;
: FILE *output : pointer to the output file. ;
; ;
; return value: The phylogenetic tree in the output file. ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
void Print_output(int i, POINTERS *trees, FILE *output)
{
WORD *parcour;
parcour=trees[i].head;
while(parcour != NULL)
{
fprintf(output,"%s",parcour->name);
parcour=parcour->suiv;
}
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Utilities ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
/*;;;;;;;;;;;;;;;;;;;;;;;;;;; Symmetrize ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : This function verifies if the delta matrix is symmetric; ;
; if not the matrix is made symmetric. ;
; ;
; input : ;
; float **delta : delta matrix ;
; int n : number of taxa ;
; ;
; return value: ;
; int symmetric : indicate if the matrix has been made ;
; symmetric or not ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
int Symmetrize(float **delta, int n)
{
int lig; /* matrix line */
int col; /* matrix column */
float value; /* symmetrized value */
int symmetric;
symmetric=1;
for(lig=1; lig <= n; lig++)
{
for(col=1; col< lig; col++)
{
if(delta[lig][col] != delta[col][lig])
{
value= (delta[lig][col]+delta[col][lig])/2;
delta[lig][col]=value;
delta[col][lig]=value;
symmetric=0;
}
}
}
if(!symmetric)
printf("The matrix is not symmetric");
return(symmetric);
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;; Concatenate ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; ;
; Description : This function concatenates a string to another. ;
; ;
; input : ;
; char *chain1 : the string to be concatenated. ;
; int ind : indicate the subtree to which concatenate the ;
; string ;
; POINTERS *trees : pointer to subtrees. ;
; int post : position to which concatenate (front (0) or ;
; end (1)) ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
void Concatenate(char chain1[LEN], int ind, POINTERS *trees, int post)
{
WORD *bran;
bran=(WORD *)calloc(1,sizeof(WORD));
if(bran == NULL)
{
printf("Out of memories");
exit(0);
}
else
{
strcpy(bran->name,chain1);
bran->suiv=NULL;
}
if(post == 0)
{
bran->suiv=trees[ind].head;
trees[ind].head=bran;
}
else
{
trees[ind].tail->suiv=bran;
trees[ind].tail=trees[ind].tail->suiv;
}
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Distance;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : This function retrieve ant return de distance between taxa ;
; i and j from the delta matrix. ;
; ;
; input : ;
; int i : taxon i ;
; int j : taxon j ;
; float **delta : the delta matrix ;
; ;
; return value: ;
; float distance : dissimilarity between the two taxa ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
float Distance(int i, int j, float **delta)
{
if(i > j)
return(delta[i][j]);
else
return(delta[j][i]);
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Variance;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : This function retrieve and return the variance of the ;
; distance between i and j, from the delta matrix. ;
; ;
; input : ;
; int i : taxon i ;
; int j : taxon j ;
; float **delta : the delta matrix ;
; ;
; return value: ;
; float distance : the variance of Dij ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
float Variance(int i, int j, float **delta)
{
if(i > j)
return(delta[j][i]);
else
return(delta[i][j]);
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Emptied ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : This function verifie if a line is emptied or not. ;
; ;
; input : ;
; int i : subtree (or line) i ;
; float **delta : the delta matrix ;
; ;
; return value: ;
; 0 : if not emptied. ;
; 1 : if emptied. ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
int Emptied(int i, float **delta) /* test if the ith line is emptied */
{
return((int)delta[i][0]);
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Sum_S;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : This function retrieves the sum Sx from the diagonal ;
; of the delta matrix. ;
; ;
; input : ;
; int i : subtree i ;
; float **delta : the delta matrix ;
; ;
; return value: ;
; float delta[i][i] : sum Si ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
float Sum_S(int i, float **delta) /* get sum Si form the diagonal */
{
return(delta[i][i]);
}
/*;;;;;;;;;;;;;;;;;;;;;;;Compute_sums_Sx;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : This function computes the sums Sx and store them in the ;
; diagonal the delta matrix. ;
; ;
; input : ;
; float **delta : the delta matrix. ;
; int n : the number of taxa ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
void Compute_sums_Sx(float **delta, int n)
{
float sum;
sum = 0.0;
int i;
int j;
for(i= 1; i <= n ; i++)
{
if(!Emptied(i,delta))
{
sum=0;
for(j=1; j <=n; j++)
{
if(i != j && !Emptied(j,delta)) /* compute the sum Si */
sum=sum + Distance(i,j,delta);
}
}
delta[i][i]=sum; /* store the sum Si in */
} /* delta�s diagonal */
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Best_pair;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : This function finds the best pair to be agglomerated by ;
; minimizing the agglomerative criterion (1). ;
; ;
; input : ;
; float **delta : the delta matrix ;
; int r : number of subtrees ;
; int *a : contain the first taxon of the pair ;
; int *b : contain the second taxon of the pair ;
; int n : number of taxa ;
; ;
; return value: ;
; int *a : the first taxon of the pair ;
; int *b : the second taxon of the pair ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
void Best_pair(float **delta, int r, int *a, int *b, int n)
{
float Qxy; /* value of the criterion calculated*/
int x,y; /* the pair which is tested */
float Qmin; /* current minimun of the criterion */
Qmin=1.0e300;
for(x=1; x <= n; x++)
{
if(!Emptied(x,delta))
{
for(y=1; y < x; y++)
{
if(!Emptied(y,delta))
{
Qxy=Agglomerative_criterion(x,y,delta,r);
if(Qxy < Qmin-0.000001)
{
Qmin=Qxy;
*a=x;
*b=y;
}
}
}
}
}
}
/*;;;;;;;;;;;;;;;;;;;;;;Finish_branch_length;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : Compute the length of the branch attached ;
; to the subtree i, during the final step ;
; ;
; input : ;
; int i : position of subtree i ;
; int j : position of subtree j ;
; int k : position of subtree k ;
; float **delta : ;
; ;
; return value: ;
; float length : The length of the branch ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
float Finish_branch_length(int i, int j, int k, float **delta)
{
float length;
length=0.5*(Distance(i,j,delta) + Distance(i,k,delta)
-Distance(j,k,delta));
return(length);
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Finish;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Description : This function compute the length of the lasts three ;
; subtrees and write the tree in the output file. ;
; ;
; input : ;
; float **delta : the delta matrix ;
; int n : the number of taxa ;
; WORD *trees : list of subtrees ;
; ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
void Finish(float **delta, int n, POINTERS *trees, FILE *output)
{
int l=1;
int i=0;
float length;
char *str;
WORD *bidon;
WORD *ele;
int last[3]; /* the last three subtrees */
str=(char *)calloc(LEN,sizeof(char));
if(str == NULL)
{
printf("Out of memories !!");
exit(0);
}
while(l <= n)
{ /* find the last tree subtree */
if(!Emptied(l, delta))
{
last[i]=l;
i++;
}
l++;
}
length=Finish_branch_length(last[0],last[1],last[2],delta);
fprintf(output,"(");
Print_output(last[0],trees,output);
fprintf(output,":");
/* gcvt(length,PREC, str); */
/* fprintf(output,"%s,",str); */
fprintf(output,"%10.8f,",length);
length=Finish_branch_length(last[1],last[0],last[2],delta);
Print_output(last[1],trees,output);
fprintf(output,":");
/* gcvt(length,PREC, str); */
/* fprintf(output,"%s,",str); */
fprintf(output,"%10.8f,",length);
length=Finish_branch_length(last[2],last[1],last[0],delta);
Print_output(last[2],trees,output);
fprintf(output,":");
/* gcvt(length,PREC,str); */
/* fprintf(output,"%s",str); */
fprintf(output,"%10.8f",length);
fprintf(output,");");
fprintf(output,"\n");
for(i=0; i < 3; i++)
{
bidon=trees[last[i]].head;
ele=bidon;
while(bidon!=NULL)
{
ele=ele->suiv;
free(bidon);
bidon=ele;
}
}
free(str);
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*\
; ;
; Formulae ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
float Agglomerative_criterion(int i, int j, float **delta, int r)
{
float Qij;
Qij=(r-2)*Distance(i,j,delta) /* Formula (1) */
-Sum_S(i,delta)
-Sum_S(j,delta);
return(Qij);
}
float Branch_length(int a, int b, float **delta, int r)
{
float length;
length=0.5*(Distance(a,b,delta) /* Formula (2) */
+(Sum_S(a,delta)
-Sum_S(b,delta))/(r-2));
return(length);
}
float Reduction4(int a, float la, int b, float lb, int i, float lamda,
float **delta)
{
float Dui;
Dui=lamda*(Distance(a,i,delta)-la)
+(1-lamda)*(Distance(b,i,delta)-lb); /* Formula (4) */
return(Dui);
}
float Reduction10(int a, int b, int i, float lamda, float vab,
float **delta)
{
float Vci;
Vci=lamda*Variance(a,i,delta)+(1-lamda)*Variance(b,i,delta)
-lamda*(1-lamda)*vab; /*Formula (10) */
return(Vci);
}
float Lamda(int a, int b, float vab, float **delta, int n, int r)
{
float lamda=0.0;
int i;
if(vab==0.0)
lamda=0.5;
else
{
for(i=1; i <= n ; i++)
{
if(a != i && b != i && !Emptied(i,delta))
lamda=lamda + (Variance(b,i,delta) - Variance(a,i,delta));
}
lamda=0.5 + lamda/(2*(r-2)*vab);
} /* Formula (9) and the */
if(lamda > 1.0) /* constraint that lamda*/
lamda = 1.0; /* belongs to [0,1] */
if(lamda < 0.0)
lamda=0.0;
return(lamda);
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Main program ;
; ;
; argc is the number of arguments ;
; **argv contains the arguments: ;
; the first argument has to be BIONJ; ;
; the second is the inptu-file; ;
; the third is the output-file. ;
; When the input and output files are ;
; not given, the user is asked for them. ;
; ;
\*;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;*/
public :
int create(const char *inputFile, const char *outputFile) {
FILE *input; /* pointer to input file */
FILE *output; /* pointer to output file */
POINTERS *trees; /* list of subtrees */
char *Name_fich1; /* name of the input file */
char *Name_fich2; /* name of the output file */
char *chain1; /* stringized branch-length */
char *chain2; /* idem */
int *a, *b; /* pair to be agglomerated */
float **delta; /* delta matrix */
float la; /* first taxon�s branch-length */
float lb; /* second taxon�s branch-length*/
float vab; /* variance of Dab */
float lamda;
int i;
int ok;
int r; /* number of subtrees */
int n; /* number of taxa */
int x, y;
//float t;
/* Allocation of memories */
Name_fich1=(char*)calloc(LEN,sizeof(char));
Name_fich2=(char*)calloc(LEN,sizeof(char));
a=(int*)calloc(1,sizeof(int));
b=(int*)calloc(1,sizeof(int));
chain1=(char *)calloc(LEN,sizeof(char));
chain2=(char *)calloc(LEN,sizeof(char));
input= fopen(inputFile,"r");
fscanf(input,"%d",&n);
output= fopen(outputFile,"w");
/* Create the delta matrix */
delta=(float **)calloc(n+1,sizeof(float*));
for(i=1; i<= n; i++)
{
delta[i]=(float *)calloc(n+1, sizeof(float));
if(delta[i] == NULL)
{
printf("Out of memories!!");
exit(0);
}
}
trees=(POINTERS *)calloc(n+1,sizeof(POINTERS));
if(trees == NULL)
{
printf("Out of memories!!");
exit(0);
}
/* initialise and symmetrize the running delta matrix */
rewind(input);
while(fscanf(input,"%d",&n) != EOF )
{
r=n;
*a=0;
*b=0;
Initialize(delta, input, n, trees);
ok=Symmetrize(delta, n);
if(!ok)
printf("\n The matrix is not symmetric.\n ");
while (r > 3) /* until r=3 */
{
Compute_sums_Sx(delta, n); /* compute the sum Sx */
Best_pair(delta, r, a, b, n); /* find the best pair by */
vab=Variance(*a, *b, delta); /* minimizing (1) */
la=Branch_length(*a, *b, delta, r); /* compute branch-lengths */
lb=Branch_length(*b, *a, delta, r); /* using formula (2) */
lamda=Lamda(*a, *b, vab, delta, n, r); /* compute lambda* using (9)*/
for(i=1; i <= n; i++)
{
if(!Emptied(i,delta) && (i != *a) && (i != *b))
{
if(*a > i)
{
x=*a;
y=i;
}
else
{
x=i;
y=*a; /* apply reduction formulae */
} /* 4 and 10 to delta */
delta[x][y]=Reduction4(*a, la, *b, lb, i, lamda, delta);
delta[y][x]=Reduction10(*a, *b, i, lamda, vab, delta);
}
}
strcpy(chain1,""); /* agglomerate the subtrees */
strcat(chain1,"("); /* a and b together with the*/
Concatenate(chain1, *a, trees, 0); /* branch-lengths according */
strcpy(chain1,""); /* to the NEWSWICK format */
strcat(chain1,":");
sprintf(chain1+strlen(chain1),"%10.8f",la);
/* gcvt(la,PREC, chain2); */
/* strcat(chain1, chain2); */
strcat(chain1,",");
Concatenate(chain1,*a, trees, 1);
trees[*a].tail->suiv=trees[*b].head;
trees[*a].tail=trees[*b].tail;
strcpy(chain1,"");
strcat(chain1,":");
sprintf(chain1+strlen(chain1),"%10.8f",lb);
/* gcvt(lb, PREC, chain2); */
/* strcat(chain1, chain2); */
strcat(chain1,")");
Concatenate(chain1, *a, trees, 1);
delta[*b][0]=1.0; /* make the b line empty */
trees[*b].head=NULL;
trees[*b].tail=NULL;
r=r-1; /* decrease r */
}
Finish(delta, n, trees, output); /* compute the branch-lengths*/
for(i=1; i<=n; i++) /* of the last three subtrees*/
{ /* and print the tree in the */
delta[i][0]=0.0; /* output-file */
trees[i].head=NULL;
trees[i].tail=NULL;
}
}
free(trees);
for(i=n; i>=1; i--)
{
free(delta[i]);
}
free(delta);
/* Minh free memory-leak */
free(chain2);
free(chain1);
free(b);
free(a);
free(Name_fich2);
free(Name_fich1);
/* Minh done */
fclose(input);
fclose(output);
return 0;
}
};
#endif
|