| 1 | #include "phylip.h" |
|---|
| 2 | |
|---|
| 3 | /* version 3.52c. (c) Copyright 1993 by Joseph Felsenstein. |
|---|
| 4 | Written by Joseph Felsenstein, Akiko Fuseki, Sean Lamont, and Andrew Keeffe. |
|---|
| 5 | Permission is granted to copy and use this program provided no fee is |
|---|
| 6 | charged for it and provided that this copyright notice is not removed. */ |
|---|
| 7 | |
|---|
| 8 | #define nmlngth 10 /* number of characters in species name */ |
|---|
| 9 | #define maxtrees 100 /* maximum number of trees to be printed out */ |
|---|
| 10 | #define often 100 /* how often to notify how many trees examined */ |
|---|
| 11 | #define many 1000 /* how many multiples of howoften before stop */ |
|---|
| 12 | |
|---|
| 13 | #define ibmpc0 false |
|---|
| 14 | #define ansi0 true |
|---|
| 15 | #define vt520 false |
|---|
| 16 | #define down 2 |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | typedef short *steptr; |
|---|
| 20 | typedef long *bitptr; |
|---|
| 21 | typedef short *treenumbers; |
|---|
| 22 | |
|---|
| 23 | /* nodes will form a binary tree */ |
|---|
| 24 | |
|---|
| 25 | typedef struct node { /* describes a tip species or an ancestor */ |
|---|
| 26 | struct node *next, *back; |
|---|
| 27 | short index; |
|---|
| 28 | boolean tip; /* present species are tips of tree */ |
|---|
| 29 | bitptr stateone, statezero; /* see in PROCEDURE fillin */ |
|---|
| 30 | short xcoord, ycoord, ymin; /* used by printree */ |
|---|
| 31 | short ymax; |
|---|
| 32 | } node; |
|---|
| 33 | |
|---|
| 34 | typedef node **pointptr; |
|---|
| 35 | |
|---|
| 36 | typedef struct gbit { |
|---|
| 37 | bitptr bits_; |
|---|
| 38 | struct gbit *next; |
|---|
| 39 | } gbit; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | Static node *root; |
|---|
| 43 | Static FILE *infile, *outfile, *treefile; |
|---|
| 44 | Static short spp, nonodes, chars, words, howmany, howoften, col, datasets, |
|---|
| 45 | ith, j; |
|---|
| 46 | /* spp = number of species |
|---|
| 47 | nonodes = number of nodes in tree |
|---|
| 48 | chars = number of binary characters |
|---|
| 49 | words = number of words needed |
|---|
| 50 | to represent characters of one organism */ |
|---|
| 51 | Static boolean weights, thresh, ancvar, questions, dollo, simple, trout, |
|---|
| 52 | printdata, progress, treeprint, stepbox, ancseq, |
|---|
| 53 | mulsets, firstset, ibmpc, vt52, ansi; |
|---|
| 54 | Static steptr extras, weight; |
|---|
| 55 | Static boolean *ancone, *anczero, *ancone0, *anczero0; |
|---|
| 56 | Static pointptr treenode; /* pointers to all nodes in tree */ |
|---|
| 57 | Static Char **name; /* names of species */ |
|---|
| 58 | Static double threshold, fracdone, fracinc; |
|---|
| 59 | Static double *threshwt; |
|---|
| 60 | Static boolean *added; |
|---|
| 61 | Static Char *guess; |
|---|
| 62 | Static steptr numsteps, numsone, numszero; |
|---|
| 63 | Static gbit *garbage; |
|---|
| 64 | Static short **bestorders, **bestrees; |
|---|
| 65 | Static short bits = 8*sizeof(long) - 1; |
|---|
| 66 | |
|---|
| 67 | /* Local variables for maketree, propagated globally for C version: */ |
|---|
| 68 | short nextree, examined, mults; |
|---|
| 69 | boolean firsttime, done; |
|---|
| 70 | double like, bestyet; |
|---|
| 71 | treenumbers current, order; |
|---|
| 72 | long fullset; |
|---|
| 73 | bitptr zeroanc, oneanc; |
|---|
| 74 | bitptr stps; |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | openfile(fp,filename,mode,application,perm) |
|---|
| 78 | FILE **fp; |
|---|
| 79 | char *filename; |
|---|
| 80 | char *mode; |
|---|
| 81 | char *application; |
|---|
| 82 | char *perm; |
|---|
| 83 | { |
|---|
| 84 | FILE *of; |
|---|
| 85 | char file[100]; |
|---|
| 86 | strcpy(file,filename); |
|---|
| 87 | while (1){ |
|---|
| 88 | of = fopen(file,mode); |
|---|
| 89 | if (of) |
|---|
| 90 | break; |
|---|
| 91 | else { |
|---|
| 92 | switch (*mode){ |
|---|
| 93 | case 'r': |
|---|
| 94 | printf("%s: can't read %s\n",application,file); |
|---|
| 95 | file[0] = '\0'; |
|---|
| 96 | while (file[0] =='\0'){ |
|---|
| 97 | printf("Please enter a new filename>"); |
|---|
| 98 | gets(file);} |
|---|
| 99 | break; |
|---|
| 100 | case 'w': |
|---|
| 101 | printf("%s: can't write %s\n",application,file); |
|---|
| 102 | file[0] = '\0'; |
|---|
| 103 | while (file[0] =='\0'){ |
|---|
| 104 | printf("Please enter a new filename>"); |
|---|
| 105 | gets(file);} |
|---|
| 106 | break; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | *fp=of; |
|---|
| 111 | if (perm != NULL) |
|---|
| 112 | strcpy(perm,file); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void gnu(p) |
|---|
| 116 | gbit **p; |
|---|
| 117 | { |
|---|
| 118 | /* this and the following are do-it-yourself garbage collectors. |
|---|
| 119 | Make a new node or pull one off the garbage list */ |
|---|
| 120 | if (garbage != NULL) { |
|---|
| 121 | *p = garbage; |
|---|
| 122 | garbage = garbage->next; |
|---|
| 123 | } else { |
|---|
| 124 | *p = (gbit *)Malloc(sizeof(gbit)); |
|---|
| 125 | (*p)->bits_ = (bitptr)Malloc(words*sizeof(long)); |
|---|
| 126 | } |
|---|
| 127 | (*p)->next = NULL; |
|---|
| 128 | } /* gnu */ |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | void chuck(p) |
|---|
| 132 | gbit *p; |
|---|
| 133 | { |
|---|
| 134 | /* collect garbage on p -- put it on front of garbage list */ |
|---|
| 135 | p->next = garbage; |
|---|
| 136 | garbage = p; |
|---|
| 137 | } /* chuck */ |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | void uppercase(ch) |
|---|
| 142 | Char *ch; |
|---|
| 143 | { /* convert a character to upper case -- either ASCII or EBCDIC */ |
|---|
| 144 | *ch = (islower (*ch) ? toupper(*ch) : (*ch)); |
|---|
| 145 | } /* uppercase */ |
|---|
| 146 | |
|---|
| 147 | void newline(i, j, k) |
|---|
| 148 | short i, j, k; |
|---|
| 149 | { |
|---|
| 150 | /* go to new line if i is a multiple of j, indent k spaces */ |
|---|
| 151 | short m; |
|---|
| 152 | |
|---|
| 153 | if ((i - 1) % j != 0 || i <= 1) |
|---|
| 154 | return; |
|---|
| 155 | putchar('\n'); |
|---|
| 156 | for (m = 1; m <= k; m++) |
|---|
| 157 | putchar(' '); |
|---|
| 158 | } /* newline */ |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | void getoptions() |
|---|
| 162 | { |
|---|
| 163 | /* interactively set options */ |
|---|
| 164 | Char ch; |
|---|
| 165 | boolean done, done1; |
|---|
| 166 | |
|---|
| 167 | fprintf(outfile, "\nPenny algorithm for Dollo or polymorphism"); |
|---|
| 168 | fprintf(outfile, " parsimony, version %s\n",VERSION); |
|---|
| 169 | fprintf(outfile, " branch-and-bound to find all"); |
|---|
| 170 | fprintf(outfile, " most parsimonious trees\n\n"); |
|---|
| 171 | howoften = often; |
|---|
| 172 | howmany = many; |
|---|
| 173 | simple = true; |
|---|
| 174 | thresh = false; |
|---|
| 175 | threshold = spp; |
|---|
| 176 | trout = true; |
|---|
| 177 | weights = false; |
|---|
| 178 | ancvar = false; |
|---|
| 179 | dollo = true; |
|---|
| 180 | printdata = false; |
|---|
| 181 | progress = true; |
|---|
| 182 | treeprint = true; |
|---|
| 183 | stepbox = false; |
|---|
| 184 | ancseq = false; |
|---|
| 185 | do { |
|---|
| 186 | printf(ansi ? "\033[2J\033[H" : |
|---|
| 187 | vt52 ? "\033E\033H" : "\n"); |
|---|
| 188 | printf("\nPenny algorithm for Dollo or polymorphism parsimony,"); |
|---|
| 189 | printf(" version %s\n",VERSION); |
|---|
| 190 | printf(" branch-and-bound to find all most parsimonious trees\n\n"); |
|---|
| 191 | printf("Settings for this run:\n"); |
|---|
| 192 | printf(" P Parsimony method? %s\n", |
|---|
| 193 | (dollo ? "Dollo" : "Polymorphism")); |
|---|
| 194 | printf(" H How many groups of %4hd trees:%6hd\n",howoften,howmany); |
|---|
| 195 | printf(" F How often to report, in trees:%5hd\n",howoften); |
|---|
| 196 | printf(" S Branch and bound is simple? %s\n", |
|---|
| 197 | (simple ? "Yes" : "No. Reconsiders order of species")); |
|---|
| 198 | printf(" T Use Threshold parsimony?"); |
|---|
| 199 | if (thresh) |
|---|
| 200 | printf(" Yes, count steps up to%4.1f per char.\n", threshold); |
|---|
| 201 | else |
|---|
| 202 | printf(" No, use ordinary parsimony\n"); |
|---|
| 203 | printf(" A Use ancestral states in input file? %s\n", |
|---|
| 204 | (ancvar ? "Yes" : "No")); |
|---|
| 205 | printf(" M Analyze multiple data sets?"); |
|---|
| 206 | if (mulsets) |
|---|
| 207 | printf(" Yes, %2hd sets\n", datasets); |
|---|
| 208 | else |
|---|
| 209 | printf(" No\n"); |
|---|
| 210 | printf(" 0 Terminal type (IBM PC, VT52, ANSI)? %s\n", |
|---|
| 211 | (ibmpc ? "IBM PC" : |
|---|
| 212 | ansi ? "ANSI" : |
|---|
| 213 | vt52 ? "VT52" : "(none)")); |
|---|
| 214 | |
|---|
| 215 | printf(" 1 Print out the data at start of run %s\n", |
|---|
| 216 | (printdata ? "Yes" : "No")); |
|---|
| 217 | printf(" 2 Print indications of progress of run %s\n", |
|---|
| 218 | (progress ? "Yes" : "no")); |
|---|
| 219 | printf(" 3 Print out tree %s\n", |
|---|
| 220 | (treeprint ? "Yes": "No")); |
|---|
| 221 | printf(" 4 Print out steps in each character %s\n", |
|---|
| 222 | (stepbox ? "Yes" : "No")); |
|---|
| 223 | printf(" 5 Print states at all nodes of tree %s\n", |
|---|
| 224 | (ancseq ? "Yes" : "No")); |
|---|
| 225 | printf(" 6 Write out trees onto tree file? %s\n", |
|---|
| 226 | (trout ? "Yes" : "No")); |
|---|
| 227 | printf("\nAre these settings correct? (type Y or the letter for one to change)\n"); |
|---|
| 228 | scanf("%c%*[^\n]", &ch); |
|---|
| 229 | getchar(); |
|---|
| 230 | uppercase(&ch); |
|---|
| 231 | done = (ch == 'Y'); |
|---|
| 232 | if (!done) { |
|---|
| 233 | if (strchr("HMSTAPF1234560",ch)){ |
|---|
| 234 | switch (ch) { |
|---|
| 235 | |
|---|
| 236 | case 'H': |
|---|
| 237 | do { |
|---|
| 238 | printf("How many cycles of %4hd trees?\n", howoften); |
|---|
| 239 | scanf("%hd%*[^\n]", &howmany); |
|---|
| 240 | getchar(); |
|---|
| 241 | } while (howmany <= 0); |
|---|
| 242 | break; |
|---|
| 243 | |
|---|
| 244 | case 'F': |
|---|
| 245 | do { |
|---|
| 246 | printf("How trees per cycle?\n"); |
|---|
| 247 | scanf("%hd%*[^\n]", &howoften); |
|---|
| 248 | getchar(); |
|---|
| 249 | } while (howoften <= 0); |
|---|
| 250 | break; |
|---|
| 251 | |
|---|
| 252 | case 'A': |
|---|
| 253 | ancvar = !ancvar; |
|---|
| 254 | break; |
|---|
| 255 | |
|---|
| 256 | case 'P': |
|---|
| 257 | dollo = !dollo; |
|---|
| 258 | break; |
|---|
| 259 | |
|---|
| 260 | case 'S': |
|---|
| 261 | simple = !simple; |
|---|
| 262 | break; |
|---|
| 263 | |
|---|
| 264 | case 'T': |
|---|
| 265 | thresh = !thresh; |
|---|
| 266 | if (thresh) { |
|---|
| 267 | done1 = false; |
|---|
| 268 | do { |
|---|
| 269 | printf("What will be the threshold value?\n"); |
|---|
| 270 | scanf("%lg%*[^\n]", &threshold); |
|---|
| 271 | getchar(); |
|---|
| 272 | done1 = (threshold >= 1.0); |
|---|
| 273 | if (!done1) |
|---|
| 274 | printf("BAD THRESHOLD VALUE: it must be greater than 1\n"); |
|---|
| 275 | else |
|---|
| 276 | threshold = (long)(threshold * 10.0 + 0.5) / 10.0; |
|---|
| 277 | } while (done1 != true); |
|---|
| 278 | } |
|---|
| 279 | break; |
|---|
| 280 | |
|---|
| 281 | case 'M': |
|---|
| 282 | mulsets = !mulsets; |
|---|
| 283 | if (mulsets) { |
|---|
| 284 | done1 = false; |
|---|
| 285 | do { |
|---|
| 286 | printf("How many data sets?\n"); |
|---|
| 287 | scanf("%hd%*[^\n]", &datasets); |
|---|
| 288 | getchar(); |
|---|
| 289 | done1 = (datasets >= 1); |
|---|
| 290 | if (!done1) |
|---|
| 291 | printf("BAD DATA SETS NUMBER: it must be greater than 1\n"); |
|---|
| 292 | } while (done1 != true); |
|---|
| 293 | } |
|---|
| 294 | break; |
|---|
| 295 | |
|---|
| 296 | case '0': |
|---|
| 297 | if (ibmpc) { |
|---|
| 298 | ibmpc = false; |
|---|
| 299 | vt52 = true; |
|---|
| 300 | } else { |
|---|
| 301 | if (vt52) { |
|---|
| 302 | vt52 = false; |
|---|
| 303 | ansi = true; |
|---|
| 304 | } else if (ansi) |
|---|
| 305 | ansi = false; |
|---|
| 306 | else |
|---|
| 307 | ibmpc = true; |
|---|
| 308 | } |
|---|
| 309 | break; |
|---|
| 310 | |
|---|
| 311 | case '1': |
|---|
| 312 | printdata = !printdata; |
|---|
| 313 | break; |
|---|
| 314 | |
|---|
| 315 | case '2': |
|---|
| 316 | progress = !progress; |
|---|
| 317 | break; |
|---|
| 318 | |
|---|
| 319 | case '3': |
|---|
| 320 | treeprint = !treeprint; |
|---|
| 321 | break; |
|---|
| 322 | |
|---|
| 323 | case '4': |
|---|
| 324 | stepbox = !stepbox; |
|---|
| 325 | break; |
|---|
| 326 | |
|---|
| 327 | case '5': |
|---|
| 328 | ancseq = !ancseq; |
|---|
| 329 | break; |
|---|
| 330 | |
|---|
| 331 | case '6': |
|---|
| 332 | trout = !trout; |
|---|
| 333 | break; |
|---|
| 334 | } |
|---|
| 335 | } else |
|---|
| 336 | printf("Not a possible option!\n"); |
|---|
| 337 | } |
|---|
| 338 | } while (!done); |
|---|
| 339 | } /* getoptions */ |
|---|
| 340 | |
|---|
| 341 | void inputnumbers() |
|---|
| 342 | { |
|---|
| 343 | /* input the numbers of species and of characters */ |
|---|
| 344 | fscanf(infile, "%hd%hd", &spp, &chars); |
|---|
| 345 | if (printdata) |
|---|
| 346 | fprintf(outfile, "%2hd species, %3hd characters\n", spp, chars); |
|---|
| 347 | if (printdata) |
|---|
| 348 | putc('\n', outfile); |
|---|
| 349 | if (progress) |
|---|
| 350 | putchar('\n'); |
|---|
| 351 | words = chars / bits + 1; |
|---|
| 352 | nonodes = spp * 2 - 1; |
|---|
| 353 | } /* inputnumbers */ |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | void doinit() |
|---|
| 357 | { |
|---|
| 358 | /* initializes variables */ |
|---|
| 359 | short i, j; |
|---|
| 360 | node *p, *q; |
|---|
| 361 | |
|---|
| 362 | inputnumbers(); |
|---|
| 363 | getoptions(); |
|---|
| 364 | treenode = (pointptr)Malloc(nonodes*sizeof(node *)); |
|---|
| 365 | for (i = 0; i < (spp); i++) { |
|---|
| 366 | treenode[i] = (node *)Malloc(sizeof(node)); |
|---|
| 367 | treenode[i]->stateone = (bitptr)Malloc(words*sizeof(long)); |
|---|
| 368 | treenode[i]->statezero = (bitptr)Malloc(words*sizeof(long)); |
|---|
| 369 | } |
|---|
| 370 | for (i = spp; i < (nonodes); i++) { |
|---|
| 371 | q = NULL; |
|---|
| 372 | for (j = 1; j <= 3; j++) { |
|---|
| 373 | p = (node *)Malloc(sizeof(node)); |
|---|
| 374 | p->stateone = (bitptr)Malloc(words*sizeof(long)); |
|---|
| 375 | p->statezero = (bitptr)Malloc(words*sizeof(long)); |
|---|
| 376 | p->next = q; |
|---|
| 377 | q = p; |
|---|
| 378 | } |
|---|
| 379 | p->next->next->next = p; |
|---|
| 380 | treenode[i] = p; |
|---|
| 381 | } |
|---|
| 382 | } /* doinit */ |
|---|
| 383 | |
|---|
| 384 | void inputweights() |
|---|
| 385 | { |
|---|
| 386 | /* input the character weights, 0-9 and A-Z for weights 0 - 35 */ |
|---|
| 387 | Char ch; |
|---|
| 388 | short i; |
|---|
| 389 | |
|---|
| 390 | for (i = 1; i < nmlngth; i++) |
|---|
| 391 | ch = getc(infile); |
|---|
| 392 | for (i = 0; i < (chars); i++) { |
|---|
| 393 | do { |
|---|
| 394 | if (eoln(infile)) { |
|---|
| 395 | fscanf(infile, "%*[^\n]"); |
|---|
| 396 | getc(infile); |
|---|
| 397 | } |
|---|
| 398 | ch = getc(infile); |
|---|
| 399 | } while (ch == ' '); |
|---|
| 400 | weight[i] = 1; |
|---|
| 401 | if (isdigit(ch)) |
|---|
| 402 | weight[i] = ch - '0'; |
|---|
| 403 | else if (isalpha(ch)) { |
|---|
| 404 | uppercase(&ch); |
|---|
| 405 | if (ch >= 'A' && ch <= 'I') |
|---|
| 406 | weight[i] = ch - 55; |
|---|
| 407 | else if (ch >= 'J' && ch <= 'R') |
|---|
| 408 | weight[i] = ch - 55; |
|---|
| 409 | else |
|---|
| 410 | weight[i] = ch - 55; |
|---|
| 411 | } else { |
|---|
| 412 | printf("BAD WEIGHT CHARACTER: %c\n", ch); |
|---|
| 413 | exit(-1); |
|---|
| 414 | } |
|---|
| 415 | } |
|---|
| 416 | fscanf(infile, "%*[^\n]"); |
|---|
| 417 | getc(infile); |
|---|
| 418 | weights = true; |
|---|
| 419 | } /* inputweights */ |
|---|
| 420 | |
|---|
| 421 | void printweights() |
|---|
| 422 | { |
|---|
| 423 | /* print out the weights of characters */ |
|---|
| 424 | short i, j, k; |
|---|
| 425 | |
|---|
| 426 | fprintf(outfile, " Characters are weighted as follows:\n"); |
|---|
| 427 | fprintf(outfile, " "); |
|---|
| 428 | for (i = 0; i <= 9; i++) |
|---|
| 429 | fprintf(outfile, "%3hd", i); |
|---|
| 430 | fprintf(outfile, "\n *---------------------------------\n"); |
|---|
| 431 | for (j = 0; j <= (chars / 10); j++) { |
|---|
| 432 | fprintf(outfile, "%4hd! ", j * 10); |
|---|
| 433 | for (i = 0; i <= 9; i++) { |
|---|
| 434 | k = j * 10 + i; |
|---|
| 435 | if (k > 0 && k <= chars) |
|---|
| 436 | fprintf(outfile, "%3hd", weight[k - 1]); |
|---|
| 437 | else |
|---|
| 438 | fprintf(outfile, " "); |
|---|
| 439 | } |
|---|
| 440 | putc('\n', outfile); |
|---|
| 441 | } |
|---|
| 442 | putc('\n', outfile); |
|---|
| 443 | } /* printweights */ |
|---|
| 444 | |
|---|
| 445 | void inputancestors() |
|---|
| 446 | { |
|---|
| 447 | /* reads the ancestral states for each character */ |
|---|
| 448 | short i; |
|---|
| 449 | Char ch; |
|---|
| 450 | |
|---|
| 451 | for (i = 1; i < nmlngth; i++) |
|---|
| 452 | ch = getc(infile); |
|---|
| 453 | for (i = 0; i < (chars); i++) { |
|---|
| 454 | anczero0[i] = true; |
|---|
| 455 | ancone0[i] = true; |
|---|
| 456 | do { |
|---|
| 457 | if (eoln(infile)) { |
|---|
| 458 | fscanf(infile, "%*[^\n]"); |
|---|
| 459 | getc(infile); |
|---|
| 460 | } |
|---|
| 461 | ch = getc(infile); |
|---|
| 462 | } while (ch == ' '); |
|---|
| 463 | if (ch == 'p') |
|---|
| 464 | ch = 'P'; |
|---|
| 465 | if (ch == 'b') |
|---|
| 466 | ch = 'B'; |
|---|
| 467 | if (ch == '0' || ch == '1' || ch == 'P' || ch == 'B' || ch == '?') { |
|---|
| 468 | switch (ch) { |
|---|
| 469 | |
|---|
| 470 | case '1': |
|---|
| 471 | anczero0[i] = false; |
|---|
| 472 | break; |
|---|
| 473 | |
|---|
| 474 | case '0': |
|---|
| 475 | ancone0[i] = false; |
|---|
| 476 | break; |
|---|
| 477 | |
|---|
| 478 | case 'P': |
|---|
| 479 | /* blank case */ |
|---|
| 480 | break; |
|---|
| 481 | |
|---|
| 482 | case 'B': |
|---|
| 483 | /* blank case */ |
|---|
| 484 | break; |
|---|
| 485 | |
|---|
| 486 | case '?': |
|---|
| 487 | /* blank case */ |
|---|
| 488 | break; |
|---|
| 489 | } |
|---|
| 490 | } else { |
|---|
| 491 | printf("BAD ANCESTOR STATE: %c AT CHARACTER %4hd\n", ch, i + 1); |
|---|
| 492 | exit(-1); |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | fscanf(infile, "%*[^\n]"); |
|---|
| 496 | getc(infile); |
|---|
| 497 | } /* inputancestors */ |
|---|
| 498 | |
|---|
| 499 | void printancestors() |
|---|
| 500 | { |
|---|
| 501 | /* print out list of ancestral states */ |
|---|
| 502 | short i; |
|---|
| 503 | |
|---|
| 504 | fprintf(outfile, "Ancestral states:\n"); |
|---|
| 505 | for (i = 1; i <= nmlngth + 3; i++) |
|---|
| 506 | putc(' ', outfile); |
|---|
| 507 | for (i = 1; i <= (chars); i++) { |
|---|
| 508 | newline(i, 55, (int)(nmlngth + 3)); |
|---|
| 509 | if (ancone[i - 1] && anczero[i - 1]) |
|---|
| 510 | putc('?', outfile); |
|---|
| 511 | else if (ancone[i - 1]) |
|---|
| 512 | putc('1', outfile); |
|---|
| 513 | else |
|---|
| 514 | putc('0', outfile); |
|---|
| 515 | if (i % 5 == 0) |
|---|
| 516 | putc(' ', outfile); |
|---|
| 517 | } |
|---|
| 518 | fprintf(outfile, "\n\n"); |
|---|
| 519 | } /* printancestor */ |
|---|
| 520 | |
|---|
| 521 | void inputoptions() |
|---|
| 522 | { |
|---|
| 523 | /* input the information on the options */ |
|---|
| 524 | Char ch; |
|---|
| 525 | short extranum, i, cursp, curchs; |
|---|
| 526 | boolean avar; |
|---|
| 527 | |
|---|
| 528 | if (!firstset) { |
|---|
| 529 | if (eoln(infile)) { |
|---|
| 530 | fscanf(infile, "%*[^\n]"); |
|---|
| 531 | getc(infile); |
|---|
| 532 | } |
|---|
| 533 | fscanf(infile, "%hd%hd", &cursp, &curchs); |
|---|
| 534 | if (cursp != spp) { |
|---|
| 535 | printf("\nERROR: INCONSISTENT NUMBER OF SPECIES IN DATA SET %4hd\n", ith); |
|---|
| 536 | exit(-1); |
|---|
| 537 | } |
|---|
| 538 | chars = curchs; |
|---|
| 539 | } |
|---|
| 540 | extranum = 0; |
|---|
| 541 | avar = false; |
|---|
| 542 | while (!(eoln(infile))) { |
|---|
| 543 | ch = getc(infile); |
|---|
| 544 | uppercase(&ch); |
|---|
| 545 | if (ch == 'A' || ch == 'W') |
|---|
| 546 | extranum++; |
|---|
| 547 | else if (ch != ' ') { |
|---|
| 548 | printf("BAD OPTION CHARACTER: %c\n", ch); |
|---|
| 549 | exit(-1); |
|---|
| 550 | } |
|---|
| 551 | } |
|---|
| 552 | fscanf(infile, "%*[^\n]"); |
|---|
| 553 | getc(infile); |
|---|
| 554 | for (i = 0; i < (chars); i++) |
|---|
| 555 | weight[i] = 1; |
|---|
| 556 | for (i = 1; i <= extranum; i++) { |
|---|
| 557 | ch = getc(infile); |
|---|
| 558 | uppercase(&ch); |
|---|
| 559 | if ((ch != 'A' && ch != 'W')){ |
|---|
| 560 | printf("ERROR: INCORRECT AUXILIARY OPTIONS LINE WHICH STARTS WITH %c\n", |
|---|
| 561 | ch); |
|---|
| 562 | exit(-1);} |
|---|
| 563 | if (ch == 'A') { |
|---|
| 564 | avar = true; |
|---|
| 565 | if (!ancvar) { |
|---|
| 566 | printf("ERROR: ANCESTOR OPTION NOT CHOSEN IN MENU"); |
|---|
| 567 | printf(" WITH OPTION %c IN INPUT\n", ch); |
|---|
| 568 | exit(-1); |
|---|
| 569 | } else |
|---|
| 570 | inputancestors(); |
|---|
| 571 | } |
|---|
| 572 | if (ch == 'W') |
|---|
| 573 | inputweights(); |
|---|
| 574 | } |
|---|
| 575 | if (ancvar && !avar) { |
|---|
| 576 | printf("ERROR: ANCESTOR OPTION CHOSEN IN MENU"); |
|---|
| 577 | printf(" WITH NO OPTION A IN INPUT\n"); |
|---|
| 578 | exit(-1); |
|---|
| 579 | } |
|---|
| 580 | fprintf(outfile,"%s parsimony method\n\n",dollo ? "Dollo" : "Polymorphism"); |
|---|
| 581 | if (weights && printdata) |
|---|
| 582 | printweights(); |
|---|
| 583 | for (i = 0; i < (chars); i++) { |
|---|
| 584 | if (!ancvar) { |
|---|
| 585 | anczero[i] = true; |
|---|
| 586 | ancone[i] = false; |
|---|
| 587 | } else { |
|---|
| 588 | anczero[i] = anczero0[i]; |
|---|
| 589 | ancone[i] = ancone0[i]; |
|---|
| 590 | } |
|---|
| 591 | } |
|---|
| 592 | if (ancvar && printdata) |
|---|
| 593 | printancestors(); |
|---|
| 594 | questions = false; |
|---|
| 595 | if (!thresh) |
|---|
| 596 | threshold = spp; |
|---|
| 597 | for (i = 0; i < (chars); i++) { |
|---|
| 598 | questions = (questions || (ancone[i] && anczero[i])); |
|---|
| 599 | threshwt[i] = threshold * weight[i]; |
|---|
| 600 | } |
|---|
| 601 | } /* inputoptions */ |
|---|
| 602 | |
|---|
| 603 | void inputdata() |
|---|
| 604 | { |
|---|
| 605 | /* input the names and character state data for species */ |
|---|
| 606 | short i, j, l; |
|---|
| 607 | char k; |
|---|
| 608 | Char charstate; |
|---|
| 609 | /* possible states are |
|---|
| 610 | '0', '1', 'P', 'B', and '?' */ |
|---|
| 611 | node *p; |
|---|
| 612 | |
|---|
| 613 | j = nmlngth + (chars + (chars - 1) / 10) / 2 - 4; |
|---|
| 614 | if (j < nmlngth - 1) |
|---|
| 615 | j = nmlngth - 1; |
|---|
| 616 | if (j > 36) |
|---|
| 617 | j = 36; |
|---|
| 618 | if (printdata) { |
|---|
| 619 | fprintf(outfile, "Name"); |
|---|
| 620 | for (i = 1; i <= j; i++) |
|---|
| 621 | putc(' ', outfile); |
|---|
| 622 | fprintf(outfile, "Characters\n"); |
|---|
| 623 | fprintf(outfile, "----"); |
|---|
| 624 | for (i = 1; i <= j; i++) |
|---|
| 625 | putc(' ', outfile); |
|---|
| 626 | fprintf(outfile, "----------\n\n"); |
|---|
| 627 | } |
|---|
| 628 | for (i = 0; i < (chars); i++) |
|---|
| 629 | extras[i] = 0; |
|---|
| 630 | for (i = 1; i <= (nonodes); i++) { |
|---|
| 631 | treenode[i - 1]->back = NULL; |
|---|
| 632 | treenode[i - 1]->tip = (i <= spp); |
|---|
| 633 | treenode[i - 1]->index = i; |
|---|
| 634 | if (i > spp) { |
|---|
| 635 | p = treenode[i - 1]->next; |
|---|
| 636 | while (p != treenode[i - 1]) { |
|---|
| 637 | p->back = NULL; |
|---|
| 638 | p->tip = false; |
|---|
| 639 | p->index = i; |
|---|
| 640 | p = p->next; |
|---|
| 641 | } |
|---|
| 642 | } else { |
|---|
| 643 | for (j = 0; j < nmlngth; j++) { |
|---|
| 644 | name[i - 1][j] = getc(infile); |
|---|
| 645 | if (eof(infile) || eoln(infile)){ |
|---|
| 646 | printf("ERROR: END-OF-LINE OR END-OF-FILE"); |
|---|
| 647 | printf(" IN THE MIDDLE OF A SPECIES NAME\n"); |
|---|
| 648 | exit(-1);} |
|---|
| 649 | } |
|---|
| 650 | if (printdata) { |
|---|
| 651 | for (j = 0; j < nmlngth; j++) |
|---|
| 652 | putc(name[i - 1][j], outfile); |
|---|
| 653 | } |
|---|
| 654 | fprintf(outfile, " "); |
|---|
| 655 | for (j = 0; j < (words); j++) { |
|---|
| 656 | treenode[i - 1]->stateone[j] = 0; |
|---|
| 657 | treenode[i - 1]->statezero[j] = 0; |
|---|
| 658 | } |
|---|
| 659 | for (j = 1; j <= (chars); j++) { |
|---|
| 660 | k = (j - 1) % bits + 1; |
|---|
| 661 | l = (j - 1) / bits + 1; |
|---|
| 662 | do { |
|---|
| 663 | if (eoln(infile)) { |
|---|
| 664 | fscanf(infile, "%*[^\n]"); |
|---|
| 665 | getc(infile); |
|---|
| 666 | } |
|---|
| 667 | charstate = getc(infile); |
|---|
| 668 | } while (charstate == ' '); |
|---|
| 669 | if (charstate == 'b') |
|---|
| 670 | charstate = 'B'; |
|---|
| 671 | if (charstate == 'p') |
|---|
| 672 | charstate = 'P'; |
|---|
| 673 | if (charstate != '0' && charstate != '1' && charstate != '?' && |
|---|
| 674 | charstate != 'P' && charstate != 'B') { |
|---|
| 675 | printf("WARNING -- BAD CHARACTER STATE: %c ",charstate); |
|---|
| 676 | printf("AT CHARACTER %5hd OF SPECIES %3hd\n",j,i); |
|---|
| 677 | exit(-1); |
|---|
| 678 | } |
|---|
| 679 | if (printdata) { |
|---|
| 680 | newline(j, 55, (int)(nmlngth + 3)); |
|---|
| 681 | putc(charstate, outfile); |
|---|
| 682 | if (j % 5 == 0) |
|---|
| 683 | putc(' ', outfile); |
|---|
| 684 | } |
|---|
| 685 | if (charstate == '1') |
|---|
| 686 | treenode[i - 1]->stateone[l - 1] = |
|---|
| 687 | ((long)treenode[i - 1]->stateone[l - 1]) | (1L << k); |
|---|
| 688 | if (charstate == '0') |
|---|
| 689 | treenode[i - 1]->statezero[l - 1] = |
|---|
| 690 | ((long)treenode[i - 1]->statezero[l - 1]) | (1L << k); |
|---|
| 691 | if (charstate == 'P' || charstate == 'B') { |
|---|
| 692 | if (dollo) |
|---|
| 693 | extras[j - 1] += weight[j - 1]; |
|---|
| 694 | else { |
|---|
| 695 | treenode[i - 1]->stateone[l - 1] = |
|---|
| 696 | ((long)treenode[i - 1]->stateone[l - 1]) | (1L << k); |
|---|
| 697 | treenode[i - 1]->statezero[l - 1] = |
|---|
| 698 | ((long)treenode[i - 1]->statezero[l - 1]) | (1L << k); |
|---|
| 699 | } |
|---|
| 700 | } |
|---|
| 701 | } |
|---|
| 702 | fscanf(infile, "%*[^\n]"); |
|---|
| 703 | getc(infile); |
|---|
| 704 | if (printdata) |
|---|
| 705 | putc('\n', outfile); |
|---|
| 706 | } |
|---|
| 707 | } |
|---|
| 708 | fprintf(outfile, "\n\n"); |
|---|
| 709 | } /* inputdata */ |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | void doinput() |
|---|
| 713 | { |
|---|
| 714 | /* reads the input data */ |
|---|
| 715 | inputoptions(); |
|---|
| 716 | inputdata(); |
|---|
| 717 | } /* doinput */ |
|---|
| 718 | |
|---|
| 719 | typedef double *valptr; |
|---|
| 720 | typedef short *placeptr; |
|---|
| 721 | |
|---|
| 722 | void add(below, newtip, newfork) |
|---|
| 723 | node **below, **newtip, **newfork; |
|---|
| 724 | { |
|---|
| 725 | /* inserts the nodes newfork and its left descendant, newtip, |
|---|
| 726 | to the tree. below becomes newfork's right descendant. |
|---|
| 727 | The global variable root is also updated */ |
|---|
| 728 | if (*below != treenode[(*below)->index - 1]) |
|---|
| 729 | *below = treenode[(*below)->index - 1]; |
|---|
| 730 | if ((*below)->back != NULL) |
|---|
| 731 | (*below)->back->back = *newfork; |
|---|
| 732 | (*newfork)->back = (*below)->back; |
|---|
| 733 | (*below)->back = (*newfork)->next->next; |
|---|
| 734 | (*newfork)->next->next->back = *below; |
|---|
| 735 | (*newfork)->next->back = *newtip; |
|---|
| 736 | (*newtip)->back = (*newfork)->next; |
|---|
| 737 | if (root == *below) |
|---|
| 738 | root = *newfork; |
|---|
| 739 | } /* add */ |
|---|
| 740 | |
|---|
| 741 | void re_move(item, fork) |
|---|
| 742 | node **item, **fork; |
|---|
| 743 | { |
|---|
| 744 | /* removes nodes item and its ancestor, fork, from the tree. |
|---|
| 745 | the new descendant of fork's ancestor is made to be |
|---|
| 746 | fork's second descendant (other than item). Also |
|---|
| 747 | returns pointers to the deleted nodes, item and fork. |
|---|
| 748 | The global variable root is also updated */ |
|---|
| 749 | node *p, *q; |
|---|
| 750 | |
|---|
| 751 | if ((*item)->back == NULL) { |
|---|
| 752 | *fork = NULL; |
|---|
| 753 | return; |
|---|
| 754 | } |
|---|
| 755 | *fork = treenode[(*item)->back->index - 1]; |
|---|
| 756 | if (root == *fork) { |
|---|
| 757 | if (*item == (*fork)->next->back) |
|---|
| 758 | root = (*fork)->next->next->back; |
|---|
| 759 | else |
|---|
| 760 | root = (*fork)->next->back; |
|---|
| 761 | } |
|---|
| 762 | p = (*item)->back->next->back; |
|---|
| 763 | q = (*item)->back->next->next->back; |
|---|
| 764 | if (p != NULL) |
|---|
| 765 | p->back = q; |
|---|
| 766 | if (q != NULL) |
|---|
| 767 | q->back = p; |
|---|
| 768 | (*fork)->back = NULL; |
|---|
| 769 | p = (*fork)->next; |
|---|
| 770 | while (p != *fork) { |
|---|
| 771 | p->back = NULL; |
|---|
| 772 | p = p->next; |
|---|
| 773 | } |
|---|
| 774 | (*item)->back = NULL; |
|---|
| 775 | } /* remove */ |
|---|
| 776 | |
|---|
| 777 | void fillin(p) |
|---|
| 778 | node *p; |
|---|
| 779 | { |
|---|
| 780 | /* Sets up for each node in the tree two statesets. |
|---|
| 781 | stateone and statezero are the sets of character |
|---|
| 782 | states that must be 1 or must be 0, respectively, |
|---|
| 783 | in a most parsimonious reconstruction, based on the |
|---|
| 784 | information at or above this node. Note that this |
|---|
| 785 | state assignment may change based on information further |
|---|
| 786 | down the tree. If a character is in both sets it is in |
|---|
| 787 | state "P". If in neither, it is "?". */ |
|---|
| 788 | short i; |
|---|
| 789 | |
|---|
| 790 | for (i = 0; i < (words); i++) { |
|---|
| 791 | p->stateone[i] = p->next->back->stateone[i] | |
|---|
| 792 | p->next->next->back->stateone[i]; |
|---|
| 793 | p->statezero[i] = p->next->back->statezero[i] | |
|---|
| 794 | p->next->next->back->statezero[i]; |
|---|
| 795 | } |
|---|
| 796 | } /* fillin */ |
|---|
| 797 | |
|---|
| 798 | void correct(p) |
|---|
| 799 | node *p; |
|---|
| 800 | { /* get final states for intermediate nodes */ |
|---|
| 801 | short i; |
|---|
| 802 | long z0, z1, s0, s1, temp; |
|---|
| 803 | |
|---|
| 804 | if (p->tip) |
|---|
| 805 | return; |
|---|
| 806 | for (i = 0; i < (words); i++) { |
|---|
| 807 | if (p->back == NULL) { |
|---|
| 808 | s0 = zeroanc[i]; |
|---|
| 809 | s1 = fullset & (~zeroanc[i]); |
|---|
| 810 | } else { |
|---|
| 811 | s0 = treenode[p->back->index - 1]->statezero[i]; |
|---|
| 812 | s1 = treenode[p->back->index - 1]->stateone[i]; |
|---|
| 813 | } |
|---|
| 814 | z0 = (s0 & p->statezero[i]) | |
|---|
| 815 | (p->next->back->statezero[i] & p->next->next->back->statezero[i]); |
|---|
| 816 | z1 = (s1 & p->stateone[i]) | |
|---|
| 817 | (p->next->back->stateone[i] & p->next->next->back->stateone[i]); |
|---|
| 818 | if (dollo) { |
|---|
| 819 | temp = z0 & (~(zeroanc[i] & z1)); |
|---|
| 820 | z1 &= ~(fullset & (~zeroanc[i]) & z0); |
|---|
| 821 | z0 = temp; |
|---|
| 822 | } |
|---|
| 823 | temp = fullset & (~z0) & (~z1); |
|---|
| 824 | p->statezero[i] = z0 | (temp & s0 & (~s1)); |
|---|
| 825 | p->stateone[i] = z1 | (temp & s1 & (~s0)); |
|---|
| 826 | } |
|---|
| 827 | } /* correct */ |
|---|
| 828 | |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | void postorder(p) |
|---|
| 832 | node *p; |
|---|
| 833 | { |
|---|
| 834 | /* traverses a binary tree, calling PROCEDURE fillin at a |
|---|
| 835 | node's descendants before calling fillin at the node */ |
|---|
| 836 | if (p->tip) |
|---|
| 837 | return; |
|---|
| 838 | postorder(p->next->back); |
|---|
| 839 | postorder(p->next->next->back); |
|---|
| 840 | fillin(p); |
|---|
| 841 | } /* postorder */ |
|---|
| 842 | |
|---|
| 843 | void count(stps) |
|---|
| 844 | long *stps; |
|---|
| 845 | { |
|---|
| 846 | /* counts the number of steps in a branch of the tree. |
|---|
| 847 | The program spends much of its time in this PROCEDURE */ |
|---|
| 848 | short i, j, l; |
|---|
| 849 | |
|---|
| 850 | j = 1; |
|---|
| 851 | l = 0; |
|---|
| 852 | for (i = 0; i < (chars); i++) { |
|---|
| 853 | l++; |
|---|
| 854 | if (l > bits) { |
|---|
| 855 | l = 1; |
|---|
| 856 | j++; |
|---|
| 857 | } |
|---|
| 858 | if ((unsigned)l < 32 && ((1L << l) & stps[j - 1]) != 0) { |
|---|
| 859 | if ((unsigned)l < 32 && ((1L << l) & zeroanc[j - 1]) != 0) |
|---|
| 860 | numszero[i] += weight[i]; |
|---|
| 861 | else |
|---|
| 862 | numsone[i] += weight[i]; |
|---|
| 863 | } |
|---|
| 864 | } |
|---|
| 865 | } /* count */ |
|---|
| 866 | |
|---|
| 867 | void preorder(p) |
|---|
| 868 | node *p; |
|---|
| 869 | { |
|---|
| 870 | /* go back up tree setting up and counting interior node |
|---|
| 871 | states */ |
|---|
| 872 | short i; |
|---|
| 873 | |
|---|
| 874 | if (!p->tip) { |
|---|
| 875 | correct(p); |
|---|
| 876 | preorder(p->next->back); |
|---|
| 877 | preorder(p->next->next->back); |
|---|
| 878 | } |
|---|
| 879 | if (p->back == NULL) |
|---|
| 880 | return; |
|---|
| 881 | if (dollo) { |
|---|
| 882 | for (i = 0; i < (words); i++) |
|---|
| 883 | stps[i] = (treenode[p->back->index - 1]->stateone[i] & p->statezero[i] & |
|---|
| 884 | zeroanc[i]) | |
|---|
| 885 | (treenode[p->back->index - 1]->statezero[i] & p->stateone[i] & |
|---|
| 886 | fullset & (~zeroanc[i])); |
|---|
| 887 | } else { |
|---|
| 888 | for (i = 0; i < (words); i++) |
|---|
| 889 | stps[i] = treenode[p->back->index - 1]->stateone[i] & |
|---|
| 890 | treenode[p->back->index - 1]->statezero[i] & p->stateone[i] & |
|---|
| 891 | p->statezero[i]; |
|---|
| 892 | } |
|---|
| 893 | count(stps); |
|---|
| 894 | } /* preorder */ |
|---|
| 895 | |
|---|
| 896 | void evaluate(r) |
|---|
| 897 | node *r; |
|---|
| 898 | { |
|---|
| 899 | /* Determines the number of losses or polymorphisms needed |
|---|
| 900 | for a tree. This is the minimum number needed to evolve |
|---|
| 901 | chars on this tree */ |
|---|
| 902 | short i, stepnum, smaller; |
|---|
| 903 | double sum; |
|---|
| 904 | |
|---|
| 905 | sum = 0.0; |
|---|
| 906 | for (i = 0; i < (chars); i++) { |
|---|
| 907 | numszero[i] = 0; |
|---|
| 908 | numsone[i] = 0; |
|---|
| 909 | } |
|---|
| 910 | for (i = 0; i < (words); i++) |
|---|
| 911 | zeroanc[i] = fullset; |
|---|
| 912 | postorder(r); |
|---|
| 913 | preorder(r); |
|---|
| 914 | for (i = 0; i < (words); i++) |
|---|
| 915 | zeroanc[i] = 0; |
|---|
| 916 | postorder(r); |
|---|
| 917 | preorder(r); |
|---|
| 918 | for (i = 0; i < (chars); i++) { |
|---|
| 919 | smaller = spp * weight[i]; |
|---|
| 920 | numsteps[i] = smaller; |
|---|
| 921 | if (anczero[i]) { |
|---|
| 922 | numsteps[i] = numszero[i]; |
|---|
| 923 | smaller = numszero[i]; |
|---|
| 924 | } |
|---|
| 925 | if (ancone[i] && numsone[i] < smaller) |
|---|
| 926 | numsteps[i] = numsone[i]; |
|---|
| 927 | stepnum = numsteps[i] + extras[i]; |
|---|
| 928 | if (stepnum <= threshwt[i]) |
|---|
| 929 | sum += stepnum; |
|---|
| 930 | else |
|---|
| 931 | sum += threshwt[i]; |
|---|
| 932 | guess[i] = '?'; |
|---|
| 933 | if (!ancone[i] || (anczero[i] && numszero[i] < numsone[i])) |
|---|
| 934 | guess[i] = '0'; |
|---|
| 935 | else if (!anczero[i] || (ancone[i] && numsone[i] < numszero[i])) |
|---|
| 936 | guess[i] = '1'; |
|---|
| 937 | } |
|---|
| 938 | if (examined == 0 && mults == 0) |
|---|
| 939 | bestyet = -1.0; |
|---|
| 940 | like = sum; |
|---|
| 941 | } /* evaluate */ |
|---|
| 942 | |
|---|
| 943 | |
|---|
| 944 | |
|---|
| 945 | void addtraverse(a, b, c,place,valyew,n) |
|---|
| 946 | node *a, *b, *c; |
|---|
| 947 | valptr valyew; |
|---|
| 948 | placeptr place; |
|---|
| 949 | short *n; |
|---|
| 950 | { |
|---|
| 951 | /* traverse all places to add b */ |
|---|
| 952 | if (done) |
|---|
| 953 | return; |
|---|
| 954 | add(&a, &b, &c); |
|---|
| 955 | (*n)++; |
|---|
| 956 | evaluate(root); |
|---|
| 957 | examined++; |
|---|
| 958 | if (examined == howoften) { |
|---|
| 959 | examined = 0; |
|---|
| 960 | mults++; |
|---|
| 961 | if (mults == howmany) |
|---|
| 962 | done = true; |
|---|
| 963 | if (progress) { |
|---|
| 964 | printf("%6hd",mults); |
|---|
| 965 | if (bestyet >= 0) |
|---|
| 966 | printf("%18.5f", bestyet); |
|---|
| 967 | else |
|---|
| 968 | printf(" - "); |
|---|
| 969 | printf("%17hd%20.2f\n", nextree - 1, fracdone * 100); |
|---|
| 970 | } |
|---|
| 971 | } |
|---|
| 972 | valyew[*n - 1] = like; |
|---|
| 973 | place[*n - 1] = a->index; |
|---|
| 974 | re_move(&b, &c); |
|---|
| 975 | if (!a->tip) { |
|---|
| 976 | addtraverse(a->next->back, b, c, place,valyew,n); |
|---|
| 977 | addtraverse(a->next->next->back, b, c, place,valyew,n); |
|---|
| 978 | } |
|---|
| 979 | } /* addtraverse */ |
|---|
| 980 | |
|---|
| 981 | void shellsort(a, b, n) |
|---|
| 982 | double *a; |
|---|
| 983 | short *b; |
|---|
| 984 | short n; |
|---|
| 985 | { |
|---|
| 986 | /* Shell sort keeping a, b in same order */ |
|---|
| 987 | short gap, i, j, itemp; |
|---|
| 988 | double rtemp; |
|---|
| 989 | |
|---|
| 990 | gap = n / 2; |
|---|
| 991 | while (gap > 0) { |
|---|
| 992 | for (i = gap + 1; i <= n; i++) { |
|---|
| 993 | j = i - gap; |
|---|
| 994 | while (j > 0) { |
|---|
| 995 | if (a[j - 1] > a[j + gap - 1]) { |
|---|
| 996 | rtemp = a[j - 1]; |
|---|
| 997 | a[j - 1] = a[j + gap - 1]; |
|---|
| 998 | a[j + gap - 1] = rtemp; |
|---|
| 999 | itemp = b[j - 1]; |
|---|
| 1000 | b[j - 1] = b[j + gap - 1]; |
|---|
| 1001 | b[j + gap - 1] = itemp; |
|---|
| 1002 | } |
|---|
| 1003 | j -= gap; |
|---|
| 1004 | } |
|---|
| 1005 | } |
|---|
| 1006 | gap /= 2; |
|---|
| 1007 | } |
|---|
| 1008 | } /* shellsort */ |
|---|
| 1009 | |
|---|
| 1010 | void addit(m) |
|---|
| 1011 | short m; |
|---|
| 1012 | { |
|---|
| 1013 | /* adds the species one by one, recursively */ |
|---|
| 1014 | short n; |
|---|
| 1015 | valptr valyew; |
|---|
| 1016 | placeptr place; |
|---|
| 1017 | short i, j, n1, besttoadd; |
|---|
| 1018 | valptr bestval; |
|---|
| 1019 | placeptr bestplace; |
|---|
| 1020 | double oldfrac, oldfdone, sum, bestsum; |
|---|
| 1021 | |
|---|
| 1022 | valyew = (valptr)Malloc(nonodes*sizeof(double)); |
|---|
| 1023 | bestval = (valptr)Malloc(nonodes*sizeof(double)); |
|---|
| 1024 | place = (placeptr)Malloc(nonodes*sizeof(short)); |
|---|
| 1025 | bestplace = (placeptr)Malloc(nonodes*sizeof(short)); |
|---|
| 1026 | if (simple && !firsttime) { |
|---|
| 1027 | n = 0; |
|---|
| 1028 | added[order[m - 1] - 1] = true; |
|---|
| 1029 | addtraverse(root, treenode[order[m - 1] - 1], treenode[spp + m - 2], |
|---|
| 1030 | place,valyew,&n); |
|---|
| 1031 | besttoadd = order[m - 1]; |
|---|
| 1032 | memcpy(bestplace, place, nonodes*sizeof(short)); |
|---|
| 1033 | memcpy(bestval, valyew, nonodes*sizeof(double)); |
|---|
| 1034 | } else { |
|---|
| 1035 | bestsum = -1.0; |
|---|
| 1036 | for (i = 1; i <= (spp); i++) { |
|---|
| 1037 | if (!added[i - 1]) { |
|---|
| 1038 | n = 0; |
|---|
| 1039 | added[i - 1] = true; |
|---|
| 1040 | addtraverse(root, treenode[i - 1], treenode[spp + m - 2], |
|---|
| 1041 | place,valyew,&n); |
|---|
| 1042 | added[i - 1] = false; |
|---|
| 1043 | sum = 0.0; |
|---|
| 1044 | for (j = 0; j < n; j++) |
|---|
| 1045 | sum += valyew[j]; |
|---|
| 1046 | if (sum > bestsum) { |
|---|
| 1047 | bestsum = sum; |
|---|
| 1048 | besttoadd = i; |
|---|
| 1049 | memcpy(bestplace, place, nonodes*sizeof(short)); |
|---|
| 1050 | memcpy(bestval, valyew, nonodes*sizeof(double)); |
|---|
| 1051 | } |
|---|
| 1052 | } |
|---|
| 1053 | } |
|---|
| 1054 | } |
|---|
| 1055 | order[m - 1] = besttoadd; |
|---|
| 1056 | memcpy(place, bestplace, nonodes*sizeof(short)); |
|---|
| 1057 | memcpy(valyew, bestval, nonodes*sizeof(double)); |
|---|
| 1058 | shellsort(valyew, place, n); |
|---|
| 1059 | oldfrac = fracinc; |
|---|
| 1060 | oldfdone = fracdone; |
|---|
| 1061 | n1 = 0; |
|---|
| 1062 | for (i = 0; i < (n); i++) { |
|---|
| 1063 | if (valyew[i] <= bestyet || bestyet < 0.0) |
|---|
| 1064 | n1++; |
|---|
| 1065 | } |
|---|
| 1066 | if (n1 > 0) |
|---|
| 1067 | fracinc /= n1; |
|---|
| 1068 | for (i = 0; i < n; i++) { |
|---|
| 1069 | if (valyew[i] <=bestyet ||bestyet < 0.0) { |
|---|
| 1070 | current[m - 1] = place[i]; |
|---|
| 1071 | add(&treenode[place[i] - 1], &treenode[besttoadd - 1], |
|---|
| 1072 | &treenode[spp + m - 2]); |
|---|
| 1073 | added[besttoadd - 1] = true; |
|---|
| 1074 | if (m < spp) |
|---|
| 1075 | addit(m + 1); |
|---|
| 1076 | else { |
|---|
| 1077 | if (valyew[i] < bestyet || bestyet < 0.0) { |
|---|
| 1078 | nextree = 1; |
|---|
| 1079 | bestyet = valyew[i]; |
|---|
| 1080 | } |
|---|
| 1081 | if (nextree <= maxtrees) { |
|---|
| 1082 | memcpy(bestorders[nextree - 1], order, |
|---|
| 1083 | spp*sizeof(short)); |
|---|
| 1084 | memcpy(bestrees[nextree - 1], current, |
|---|
| 1085 | spp*sizeof(short)); |
|---|
| 1086 | } |
|---|
| 1087 | nextree++; |
|---|
| 1088 | firsttime = false; |
|---|
| 1089 | } |
|---|
| 1090 | re_move(&treenode[besttoadd - 1], &treenode[spp + m - 2]); |
|---|
| 1091 | added[besttoadd - 1] = false; |
|---|
| 1092 | } |
|---|
| 1093 | fracdone += fracinc; |
|---|
| 1094 | } |
|---|
| 1095 | fracinc = oldfrac; |
|---|
| 1096 | fracdone = oldfdone; |
|---|
| 1097 | free(valyew); |
|---|
| 1098 | free(bestval); |
|---|
| 1099 | free(place); |
|---|
| 1100 | free(bestplace); |
|---|
| 1101 | } /* addit */ |
|---|
| 1102 | |
|---|
| 1103 | |
|---|
| 1104 | void coordinates(p, tipy) |
|---|
| 1105 | node *p; |
|---|
| 1106 | short *tipy; |
|---|
| 1107 | { |
|---|
| 1108 | /* establishes coordinates of nodes */ |
|---|
| 1109 | node *q, *first, *last; |
|---|
| 1110 | |
|---|
| 1111 | if (p->tip) { |
|---|
| 1112 | p->xcoord = 0; |
|---|
| 1113 | p->ycoord = *tipy; |
|---|
| 1114 | p->ymin = *tipy; |
|---|
| 1115 | p->ymax = (*tipy); |
|---|
| 1116 | (*tipy) += down; |
|---|
| 1117 | return; |
|---|
| 1118 | } |
|---|
| 1119 | q = p->next; |
|---|
| 1120 | do { |
|---|
| 1121 | coordinates(q->back, tipy); |
|---|
| 1122 | q = q->next; |
|---|
| 1123 | } while (p != q); |
|---|
| 1124 | first = p->next->back; |
|---|
| 1125 | q = p->next; |
|---|
| 1126 | while (q->next != p) |
|---|
| 1127 | q = q->next; |
|---|
| 1128 | last = q->back; |
|---|
| 1129 | p->xcoord = last->ymax - first->ymin; |
|---|
| 1130 | p->ycoord = (first->ycoord + last->ycoord) / 2; |
|---|
| 1131 | p->ymin = first->ymin; |
|---|
| 1132 | p->ymax = last->ymax; |
|---|
| 1133 | } /* coordinates */ |
|---|
| 1134 | |
|---|
| 1135 | void drawline(i, scale) |
|---|
| 1136 | short i; |
|---|
| 1137 | double scale; |
|---|
| 1138 | { |
|---|
| 1139 | /* draws one row of the tree diagram by moving up tree */ |
|---|
| 1140 | node *p, *q, *r, *first, *last; |
|---|
| 1141 | short n, j; |
|---|
| 1142 | boolean extra, done; |
|---|
| 1143 | |
|---|
| 1144 | p = root; |
|---|
| 1145 | q = root; |
|---|
| 1146 | extra = false; |
|---|
| 1147 | if (i == p->ycoord && p == root) { |
|---|
| 1148 | if (p->index - spp >= 10) |
|---|
| 1149 | fprintf(outfile, "-%2hd", p->index - spp); |
|---|
| 1150 | else |
|---|
| 1151 | fprintf(outfile, "--%hd", p->index - spp); |
|---|
| 1152 | extra = true; |
|---|
| 1153 | } else |
|---|
| 1154 | fprintf(outfile, " "); |
|---|
| 1155 | do { |
|---|
| 1156 | if (!p->tip) { |
|---|
| 1157 | r = p->next; |
|---|
| 1158 | done = false; |
|---|
| 1159 | do { |
|---|
| 1160 | if (i >= r->back->ymin && i <= r->back->ymax) { |
|---|
| 1161 | q = r->back; |
|---|
| 1162 | done = true; |
|---|
| 1163 | } |
|---|
| 1164 | r = r->next; |
|---|
| 1165 | } while (!(done ||r == p)); |
|---|
| 1166 | first = p->next->back; |
|---|
| 1167 | r = p->next; |
|---|
| 1168 | while (r->next != p) |
|---|
| 1169 | r = r->next; |
|---|
| 1170 | last = r->back; |
|---|
| 1171 | } |
|---|
| 1172 | done = (p == q); |
|---|
| 1173 | n = (long)(scale * (p->xcoord - q->xcoord) + 0.5); |
|---|
| 1174 | if (n < 3 && !q->tip) |
|---|
| 1175 | n = 3; |
|---|
| 1176 | if (extra) { |
|---|
| 1177 | n--; |
|---|
| 1178 | extra = false; |
|---|
| 1179 | } |
|---|
| 1180 | if (q->ycoord == i && !done) { |
|---|
| 1181 | putc('+', outfile); |
|---|
| 1182 | if (!q->tip) { |
|---|
| 1183 | for (j = 1; j <= n - 2; j++) |
|---|
| 1184 | putc('-', outfile); |
|---|
| 1185 | if (q->index - spp >= 10) |
|---|
| 1186 | fprintf(outfile, "%2hd", q->index - spp); |
|---|
| 1187 | else |
|---|
| 1188 | fprintf(outfile, "-%hd", q->index - spp); |
|---|
| 1189 | extra = true; |
|---|
| 1190 | } else { |
|---|
| 1191 | for (j = 1; j < n; j++) |
|---|
| 1192 | putc('-', outfile); |
|---|
| 1193 | } |
|---|
| 1194 | } else if (!p->tip) { |
|---|
| 1195 | if (last->ycoord > i && first->ycoord < i && i != p->ycoord) { |
|---|
| 1196 | putc('!', outfile); |
|---|
| 1197 | for (j = 1; j < n; j++) |
|---|
| 1198 | putc(' ', outfile); |
|---|
| 1199 | } else { |
|---|
| 1200 | for (j = 1; j <= n; j++) |
|---|
| 1201 | putc(' ', outfile); |
|---|
| 1202 | } |
|---|
| 1203 | } else { |
|---|
| 1204 | for (j = 1; j <= n; j++) |
|---|
| 1205 | putc(' ', outfile); |
|---|
| 1206 | } |
|---|
| 1207 | if (p != q) |
|---|
| 1208 | p = q; |
|---|
| 1209 | } while (!done); |
|---|
| 1210 | if (p->ycoord == i && p->tip) { |
|---|
| 1211 | for (j = 0; j < nmlngth; j++) |
|---|
| 1212 | putc(name[p->index - 1][j], outfile); |
|---|
| 1213 | } |
|---|
| 1214 | putc('\n', outfile); |
|---|
| 1215 | } /* drawline */ |
|---|
| 1216 | |
|---|
| 1217 | void printree() |
|---|
| 1218 | { |
|---|
| 1219 | /* prints out diagram of the tree */ |
|---|
| 1220 | short i,tipy; |
|---|
| 1221 | double scale; |
|---|
| 1222 | |
|---|
| 1223 | putc('\n', outfile); |
|---|
| 1224 | if (!treeprint) |
|---|
| 1225 | return; |
|---|
| 1226 | putc('\n', outfile); |
|---|
| 1227 | tipy = 1; |
|---|
| 1228 | coordinates(root, &tipy); |
|---|
| 1229 | scale = 1.5; |
|---|
| 1230 | putc('\n', outfile); |
|---|
| 1231 | for (i = 1; i <= (tipy - down); i++) |
|---|
| 1232 | drawline(i, scale); |
|---|
| 1233 | putc('\n', outfile); |
|---|
| 1234 | } /* printree */ |
|---|
| 1235 | |
|---|
| 1236 | |
|---|
| 1237 | void filltrav(r) |
|---|
| 1238 | node *r; |
|---|
| 1239 | { |
|---|
| 1240 | /* traverse to fill in interior node states */ |
|---|
| 1241 | if (r->tip) |
|---|
| 1242 | return; |
|---|
| 1243 | filltrav(r->next->back); |
|---|
| 1244 | filltrav(r->next->next->back); |
|---|
| 1245 | fillin(r); |
|---|
| 1246 | } /* filltrav */ |
|---|
| 1247 | |
|---|
| 1248 | /* Local variables for hyptrav: */ |
|---|
| 1249 | |
|---|
| 1250 | void hyprint(dohyp,unknown,bottom,nonzero,zerobelow,onebelow,r) |
|---|
| 1251 | bitptr dohyp; |
|---|
| 1252 | boolean unknown,bottom,nonzero; |
|---|
| 1253 | gbit *zerobelow,*onebelow; |
|---|
| 1254 | node *r; |
|---|
| 1255 | { |
|---|
| 1256 | /* print out states at node */ |
|---|
| 1257 | short i, j, k; |
|---|
| 1258 | char l; |
|---|
| 1259 | boolean dot, a0, a1, s0, s1; |
|---|
| 1260 | |
|---|
| 1261 | if (bottom) |
|---|
| 1262 | fprintf(outfile, "root "); |
|---|
| 1263 | else |
|---|
| 1264 | fprintf(outfile, "%3hd ", r->back->index - spp); |
|---|
| 1265 | if (r->tip) { |
|---|
| 1266 | for (i = 0; i < nmlngth; i++) |
|---|
| 1267 | putc(name[r->index - 1][i], outfile); |
|---|
| 1268 | } else |
|---|
| 1269 | fprintf(outfile, "%4hd ", r->index - spp); |
|---|
| 1270 | if (nonzero) |
|---|
| 1271 | fprintf(outfile, " yes "); |
|---|
| 1272 | else if (unknown) |
|---|
| 1273 | fprintf(outfile, " ? "); |
|---|
| 1274 | else |
|---|
| 1275 | fprintf(outfile, " no "); |
|---|
| 1276 | for (j = 1; j <= (chars); j++) { |
|---|
| 1277 | newline(j, 40, (int)(nmlngth + 17)); |
|---|
| 1278 | k = (j - 1) / bits + 1; |
|---|
| 1279 | l = (j - 1) % bits + 1; |
|---|
| 1280 | dot = (((1L << l) & dohyp[k - 1]) == 0 && guess[j - 1] == '?'); |
|---|
| 1281 | s0 = (((1L << l) & r->statezero[k - 1]) != 0); |
|---|
| 1282 | s1 = (((1L << l) & r->stateone[k - 1]) != 0); |
|---|
| 1283 | a0 = (((1L << l) & zerobelow->bits_[k - 1]) != 0); |
|---|
| 1284 | a1 = (((1L << l) & onebelow->bits_[k - 1]) != 0); |
|---|
| 1285 | dot = (dot || (a1 == s1 && a0 == s0)); |
|---|
| 1286 | if (dot) |
|---|
| 1287 | putc('.', outfile); |
|---|
| 1288 | else { |
|---|
| 1289 | if (s0) { |
|---|
| 1290 | if (s1) |
|---|
| 1291 | putc('P', outfile); |
|---|
| 1292 | else |
|---|
| 1293 | putc('0', outfile); |
|---|
| 1294 | } else if (s1) |
|---|
| 1295 | putc('1', outfile); |
|---|
| 1296 | else |
|---|
| 1297 | putc('?', outfile); |
|---|
| 1298 | } |
|---|
| 1299 | if (j % 5 == 0) |
|---|
| 1300 | putc(' ', outfile); |
|---|
| 1301 | } |
|---|
| 1302 | putc('\n', outfile); |
|---|
| 1303 | } /* hyprint */ |
|---|
| 1304 | |
|---|
| 1305 | void hyptrav(r_, unknown,dohyp) |
|---|
| 1306 | node *r_; |
|---|
| 1307 | boolean unknown; |
|---|
| 1308 | bitptr dohyp; |
|---|
| 1309 | { |
|---|
| 1310 | /* compute, print out states at one interior node */ |
|---|
| 1311 | node *r; |
|---|
| 1312 | boolean bottom, nonzero; |
|---|
| 1313 | gbit *zerobelow, *onebelow; |
|---|
| 1314 | |
|---|
| 1315 | short i; |
|---|
| 1316 | r = r_; |
|---|
| 1317 | gnu(&zerobelow); |
|---|
| 1318 | gnu(&onebelow); |
|---|
| 1319 | if (!r->tip) |
|---|
| 1320 | correct(r); |
|---|
| 1321 | bottom = (r->back == NULL); |
|---|
| 1322 | nonzero = false; |
|---|
| 1323 | if (bottom) { |
|---|
| 1324 | memcpy(zerobelow->bits_, zeroanc, words*sizeof(long)); |
|---|
| 1325 | memcpy(onebelow->bits_, oneanc, words*sizeof(long)); |
|---|
| 1326 | } else { |
|---|
| 1327 | memcpy(zerobelow->bits_, treenode[r->back->index - 1]->statezero, |
|---|
| 1328 | words*sizeof(long)); |
|---|
| 1329 | memcpy(onebelow->bits_, treenode[r->back->index - 1]->stateone, |
|---|
| 1330 | words*sizeof(long)); |
|---|
| 1331 | } |
|---|
| 1332 | for (i = 0; i < (words); i++) |
|---|
| 1333 | nonzero = (nonzero || ((r->stateone[i] & zerobelow->bits_[i]) | |
|---|
| 1334 | (r->statezero[i] & onebelow->bits_[i])) != 0); |
|---|
| 1335 | hyprint(dohyp,unknown,bottom,nonzero,zerobelow,onebelow,r); |
|---|
| 1336 | if (!r->tip) { |
|---|
| 1337 | hyptrav(r->next->back, unknown,dohyp); |
|---|
| 1338 | hyptrav(r->next->next->back, unknown,dohyp); |
|---|
| 1339 | } |
|---|
| 1340 | chuck(zerobelow); |
|---|
| 1341 | chuck(onebelow); |
|---|
| 1342 | } /* hyptrav */ |
|---|
| 1343 | |
|---|
| 1344 | void hypstates() |
|---|
| 1345 | { |
|---|
| 1346 | /* fill in and describe states at interior nodes */ |
|---|
| 1347 | /* Local variables for hypstates: */ |
|---|
| 1348 | boolean unknown; |
|---|
| 1349 | bitptr dohyp; |
|---|
| 1350 | short i, j, k; |
|---|
| 1351 | |
|---|
| 1352 | for (i = 0; i < (words); i++) { |
|---|
| 1353 | zeroanc[i] = 0; |
|---|
| 1354 | oneanc[i] = 0; |
|---|
| 1355 | } |
|---|
| 1356 | unknown = false; |
|---|
| 1357 | for (i = 0; i < (chars); i++) { |
|---|
| 1358 | j = i / bits + 1; |
|---|
| 1359 | k = i % bits + 1; |
|---|
| 1360 | if (guess[i] == '0') |
|---|
| 1361 | zeroanc[j - 1] = ((long)zeroanc[j - 1]) | (1L << k); |
|---|
| 1362 | if (guess[i] == '1') |
|---|
| 1363 | oneanc[j - 1] = ((long)oneanc[j - 1]) | (1L << k); |
|---|
| 1364 | unknown = (unknown || guess[i] == '?'); |
|---|
| 1365 | } |
|---|
| 1366 | dohyp = (bitptr)Malloc(words*sizeof(long)); |
|---|
| 1367 | for (i = 0; i < (words); i++) |
|---|
| 1368 | dohyp[i] = zeroanc[i] | oneanc[i]; |
|---|
| 1369 | filltrav(root); |
|---|
| 1370 | fprintf(outfile, "From To Any Steps? "); |
|---|
| 1371 | fprintf(outfile, "State at upper node\n"); |
|---|
| 1372 | fprintf(outfile, " "); |
|---|
| 1373 | fprintf(outfile, "( . means same as in the node below it on tree)\n\n"); |
|---|
| 1374 | hyptrav(root,unknown,dohyp); |
|---|
| 1375 | free(dohyp); |
|---|
| 1376 | } /* hypstates */ |
|---|
| 1377 | |
|---|
| 1378 | void treeout(p) |
|---|
| 1379 | node *p; |
|---|
| 1380 | { |
|---|
| 1381 | /* write out file with representation of final tree */ |
|---|
| 1382 | short i, n; |
|---|
| 1383 | Char c; |
|---|
| 1384 | |
|---|
| 1385 | if (p->tip) { |
|---|
| 1386 | n = 0; |
|---|
| 1387 | for (i = 1; i <= nmlngth; i++) { |
|---|
| 1388 | if (name[p->index - 1][i - 1] != ' ') |
|---|
| 1389 | n = i; |
|---|
| 1390 | } |
|---|
| 1391 | for (i = 0; i < n; i++) { |
|---|
| 1392 | c = name[p->index - 1][i]; |
|---|
| 1393 | if (c == ' ') |
|---|
| 1394 | c = '_'; |
|---|
| 1395 | putc(c, treefile); |
|---|
| 1396 | } |
|---|
| 1397 | col += n; |
|---|
| 1398 | } else { |
|---|
| 1399 | putc('(', treefile); |
|---|
| 1400 | col++; |
|---|
| 1401 | treeout(p->next->back); |
|---|
| 1402 | putc(',', treefile); |
|---|
| 1403 | col++; |
|---|
| 1404 | if (col > 65) { |
|---|
| 1405 | putc('\n', treefile); |
|---|
| 1406 | col = 0; |
|---|
| 1407 | } |
|---|
| 1408 | treeout(p->next->next->back); |
|---|
| 1409 | putc(')', treefile); |
|---|
| 1410 | col++; |
|---|
| 1411 | } |
|---|
| 1412 | if (p != root) |
|---|
| 1413 | return; |
|---|
| 1414 | if (nextree > 2) |
|---|
| 1415 | fprintf(treefile, "[%6.4f];\n", 1.0 / (nextree - 1)); |
|---|
| 1416 | else |
|---|
| 1417 | fprintf(treefile, ";\n"); |
|---|
| 1418 | } /* treeout */ |
|---|
| 1419 | |
|---|
| 1420 | void describe() |
|---|
| 1421 | { |
|---|
| 1422 | /* prints ancestors, steps and table of numbers of steps in |
|---|
| 1423 | each character */ |
|---|
| 1424 | short i, j, k; |
|---|
| 1425 | |
|---|
| 1426 | if (stepbox) { |
|---|
| 1427 | putc('\n', outfile); |
|---|
| 1428 | if (weights) |
|---|
| 1429 | fprintf(outfile, "weighted"); |
|---|
| 1430 | if (dollo) |
|---|
| 1431 | fprintf(outfile, " reversions "); |
|---|
| 1432 | else |
|---|
| 1433 | fprintf(outfile, " polymorphisms "); |
|---|
| 1434 | fprintf(outfile, "in each character:\n"); |
|---|
| 1435 | fprintf(outfile, " "); |
|---|
| 1436 | for (i = 0; i <= 9; i++) |
|---|
| 1437 | fprintf(outfile, "%4hd", i); |
|---|
| 1438 | fprintf(outfile, "\n *-----------------------------------------\n"); |
|---|
| 1439 | for (i = 0; i <= (chars / 10); i++) { |
|---|
| 1440 | fprintf(outfile, "%5hd", i * 10); |
|---|
| 1441 | putc('!', outfile); |
|---|
| 1442 | for (j = 0; j <= 9; j++) { |
|---|
| 1443 | k = i * 10 + j; |
|---|
| 1444 | if (k == 0 || k > chars) |
|---|
| 1445 | fprintf(outfile, " "); |
|---|
| 1446 | else |
|---|
| 1447 | fprintf(outfile, "%4hd", numsteps[k - 1] + extras[k - 1]); |
|---|
| 1448 | } |
|---|
| 1449 | putc('\n', outfile); |
|---|
| 1450 | } |
|---|
| 1451 | putc('\n', outfile); |
|---|
| 1452 | } |
|---|
| 1453 | if (questions) { |
|---|
| 1454 | fprintf(outfile, "best guesses of ancestral states:\n"); |
|---|
| 1455 | fprintf(outfile, " "); |
|---|
| 1456 | for (i = 0; i <= 9; i++) |
|---|
| 1457 | fprintf(outfile, "%2hd", i); |
|---|
| 1458 | fprintf(outfile, "\n *--------------------\n"); |
|---|
| 1459 | for (i = 0; i <= (chars / 10); i++) { |
|---|
| 1460 | fprintf(outfile, "%5hd!", i * 10); |
|---|
| 1461 | for (j = 0; j <= 9; j++) { |
|---|
| 1462 | if (i * 10 + j == 0 || i * 10 + j > chars) |
|---|
| 1463 | fprintf(outfile, " "); |
|---|
| 1464 | else |
|---|
| 1465 | fprintf(outfile, " %c", guess[i * 10 + j - 1]); |
|---|
| 1466 | } |
|---|
| 1467 | putc('\n', outfile); |
|---|
| 1468 | } |
|---|
| 1469 | putc('\n', outfile); |
|---|
| 1470 | } |
|---|
| 1471 | if (ancseq) { |
|---|
| 1472 | hypstates(); |
|---|
| 1473 | putc('\n', outfile); |
|---|
| 1474 | } |
|---|
| 1475 | putc('\n', outfile); |
|---|
| 1476 | if (trout) { |
|---|
| 1477 | col = 0; |
|---|
| 1478 | treeout(root); |
|---|
| 1479 | } |
|---|
| 1480 | } /* describe */ |
|---|
| 1481 | |
|---|
| 1482 | |
|---|
| 1483 | void maketree() |
|---|
| 1484 | { |
|---|
| 1485 | /* tree construction recursively by branch and bound */ |
|---|
| 1486 | short i, j, k; |
|---|
| 1487 | node *dummy; |
|---|
| 1488 | |
|---|
| 1489 | fullset = (1L << (bits + 1)) - (1L << 1); |
|---|
| 1490 | if (progress) { |
|---|
| 1491 | printf("\nHow many\n"); |
|---|
| 1492 | printf("trees looked Approximate\n"); |
|---|
| 1493 | printf("at so far Length of How many percentage\n"); |
|---|
| 1494 | printf("(multiples shortest tree trees this long searched\n"); |
|---|
| 1495 | printf("of %4hd): found so far found so far so far\n", |
|---|
| 1496 | howoften); |
|---|
| 1497 | printf("---------- ------------ ------------ ------------\n"); |
|---|
| 1498 | } |
|---|
| 1499 | done = false; |
|---|
| 1500 | mults = 0; |
|---|
| 1501 | examined = 0; |
|---|
| 1502 | nextree = 1; |
|---|
| 1503 | root = treenode[0]; |
|---|
| 1504 | firsttime = true; |
|---|
| 1505 | for (i = 0; i < (spp); i++) |
|---|
| 1506 | added[i] = false; |
|---|
| 1507 | added[0] = true; |
|---|
| 1508 | order[0] = 1; |
|---|
| 1509 | k = 2; |
|---|
| 1510 | fracdone = 0.0; |
|---|
| 1511 | fracinc = 1.0; |
|---|
| 1512 | bestyet = -1.0; |
|---|
| 1513 | free(stps); |
|---|
| 1514 | stps = (bitptr)Malloc(words*sizeof(long)); |
|---|
| 1515 | addit(k); |
|---|
| 1516 | if (done) { |
|---|
| 1517 | if (progress) { |
|---|
| 1518 | printf("Search broken off! Not guaranteed to\n"); |
|---|
| 1519 | printf(" have found the most parsimonious trees.\n"); |
|---|
| 1520 | } |
|---|
| 1521 | if (treeprint) { |
|---|
| 1522 | fprintf(outfile, "Search broken off! Not guaranteed to\n"); |
|---|
| 1523 | fprintf(outfile, " have found the most parsimonious\n"); |
|---|
| 1524 | fprintf(outfile, " trees, but here is what we found:\n"); |
|---|
| 1525 | } |
|---|
| 1526 | } |
|---|
| 1527 | if (treeprint) { |
|---|
| 1528 | fprintf(outfile, "\nrequires a total of %18.3f\n\n", bestyet); |
|---|
| 1529 | if (nextree == 2) |
|---|
| 1530 | fprintf(outfile, "One most parsimonious tree found:\n"); |
|---|
| 1531 | else |
|---|
| 1532 | fprintf(outfile, "%5hd trees in all found\n", nextree - 1); |
|---|
| 1533 | } |
|---|
| 1534 | if (nextree > maxtrees + 1) { |
|---|
| 1535 | if (treeprint) |
|---|
| 1536 | fprintf(outfile, "here are the first%4ld of them\n", (long)maxtrees); |
|---|
| 1537 | nextree = maxtrees + 1; |
|---|
| 1538 | } |
|---|
| 1539 | if (treeprint) |
|---|
| 1540 | putc('\n', outfile); |
|---|
| 1541 | for (i = 0; i < (spp); i++) |
|---|
| 1542 | added[i] = true; |
|---|
| 1543 | for (i = 0; i <= (nextree - 2); i++) { |
|---|
| 1544 | for (j = k; j <= (spp); j++) |
|---|
| 1545 | add(&treenode[bestrees[i][j - 1] - 1], |
|---|
| 1546 | &treenode[bestorders[i][j - 1] - 1], &treenode[spp + j - 2]); |
|---|
| 1547 | evaluate(root); |
|---|
| 1548 | printree(); |
|---|
| 1549 | describe(); |
|---|
| 1550 | for (j = k - 1; j < (spp); j++) |
|---|
| 1551 | re_move(&treenode[bestorders[i][j] - 1], &dummy); |
|---|
| 1552 | } |
|---|
| 1553 | if (progress) { |
|---|
| 1554 | printf("\nOutput written to output file\n\n"); |
|---|
| 1555 | if (trout) |
|---|
| 1556 | printf("Trees also written onto tree file\n\n"); |
|---|
| 1557 | } |
|---|
| 1558 | free(stps); |
|---|
| 1559 | } /* maketree */ |
|---|
| 1560 | |
|---|
| 1561 | |
|---|
| 1562 | main(argc, argv) |
|---|
| 1563 | int argc; |
|---|
| 1564 | Char *argv[]; |
|---|
| 1565 | { /* branch-and-bound method for Dollo, polymorphism parsimony */ |
|---|
| 1566 | char infilename[100],outfilename[100],trfilename[100]; |
|---|
| 1567 | /* Reads in the number of species, number of characters, |
|---|
| 1568 | options and data. Then finds all most parsimonious trees */ |
|---|
| 1569 | #ifdef MAC |
|---|
| 1570 | macsetup("Dolpenny",""); |
|---|
| 1571 | #endif |
|---|
| 1572 | openfile(&infile,INFILE,"r",argv[0],infilename); |
|---|
| 1573 | openfile(&outfile,OUTFILE,"w",argv[0],outfilename); |
|---|
| 1574 | ibmpc = ibmpc0; |
|---|
| 1575 | ansi = ansi0; |
|---|
| 1576 | vt52 = vt520; |
|---|
| 1577 | garbage = NULL; |
|---|
| 1578 | mulsets = false; |
|---|
| 1579 | datasets = 1; |
|---|
| 1580 | firstset = true; |
|---|
| 1581 | doinit(); |
|---|
| 1582 | extras = (short *)Malloc(chars*sizeof(short)); |
|---|
| 1583 | weight = (short *)Malloc(chars*sizeof(short)); |
|---|
| 1584 | threshwt = (double *)Malloc(chars*sizeof(double)); |
|---|
| 1585 | guess = (Char *)Malloc(chars*sizeof(Char)); |
|---|
| 1586 | numsteps = (short *)Malloc(chars*sizeof(short)); |
|---|
| 1587 | numszero = (short *)Malloc(chars*sizeof(short)); |
|---|
| 1588 | numsone = (short *)Malloc(chars*sizeof(short)); |
|---|
| 1589 | bestorders = (short **)Malloc(maxtrees*sizeof(short *)); |
|---|
| 1590 | bestrees = (short **)Malloc(maxtrees*sizeof(short *)); |
|---|
| 1591 | for (j = 1; j <= maxtrees; j++) { |
|---|
| 1592 | bestorders[j - 1] = (short *)Malloc(spp*sizeof(short)); |
|---|
| 1593 | bestrees[j - 1] = (short *)Malloc(spp*sizeof(short)); |
|---|
| 1594 | } |
|---|
| 1595 | current = (treenumbers)Malloc(spp*sizeof(short)); |
|---|
| 1596 | order = (treenumbers)Malloc(spp*sizeof(short)); |
|---|
| 1597 | name = (Char **)Malloc(spp*sizeof(Char *)); |
|---|
| 1598 | for (j = 1; j <= spp; j++) |
|---|
| 1599 | name[j - 1] = (Char *)Malloc(nmlngth*sizeof(Char)); |
|---|
| 1600 | added = (boolean *)Malloc(nonodes*sizeof(boolean)); |
|---|
| 1601 | ancone = (boolean *)Malloc(chars*sizeof(boolean)); |
|---|
| 1602 | anczero = (boolean *)Malloc(chars*sizeof(boolean)); |
|---|
| 1603 | ancone0 = (boolean *)Malloc(chars*sizeof(boolean)); |
|---|
| 1604 | anczero0 = (boolean *)Malloc(chars*sizeof(boolean)); |
|---|
| 1605 | zeroanc = (bitptr)Malloc(words*sizeof(long)); |
|---|
| 1606 | oneanc = (bitptr)Malloc(words*sizeof(long)); |
|---|
| 1607 | if (trout) |
|---|
| 1608 | openfile(&treefile,TREEFILE,"w",argv[0],trfilename); |
|---|
| 1609 | for (ith = 1; ith <= datasets; ith++) { |
|---|
| 1610 | doinput(); |
|---|
| 1611 | if (ith == 1) |
|---|
| 1612 | firstset = false; |
|---|
| 1613 | if (datasets > 1) { |
|---|
| 1614 | fprintf(outfile, "Data set # %hd:\n\n",ith); |
|---|
| 1615 | if (progress) |
|---|
| 1616 | printf("\nData set # %hd:\n",ith); |
|---|
| 1617 | } |
|---|
| 1618 | maketree(); |
|---|
| 1619 | } |
|---|
| 1620 | FClose(infile); |
|---|
| 1621 | FClose(outfile); |
|---|
| 1622 | FClose(treefile); |
|---|
| 1623 | #ifdef MAC |
|---|
| 1624 | fixmacfile(outfilename); |
|---|
| 1625 | fixmacfile(trfilename); |
|---|
| 1626 | #endif |
|---|
| 1627 | exit(0); |
|---|
| 1628 | } /* branch-and-bound method for Dollo, polymorphism parsimony */ |
|---|
| 1629 | |
|---|
| 1630 | |
|---|
| 1631 | int eof(f) |
|---|
| 1632 | FILE *f; |
|---|
| 1633 | { |
|---|
| 1634 | register int ch; |
|---|
| 1635 | |
|---|
| 1636 | if (feof(f)) |
|---|
| 1637 | return 1; |
|---|
| 1638 | if (f == stdin) |
|---|
| 1639 | return 0; |
|---|
| 1640 | ch = getc(f); |
|---|
| 1641 | if (ch == EOF) |
|---|
| 1642 | return 1; |
|---|
| 1643 | ungetc(ch, f); |
|---|
| 1644 | return 0; |
|---|
| 1645 | } |
|---|
| 1646 | |
|---|
| 1647 | |
|---|
| 1648 | int eoln(f) |
|---|
| 1649 | FILE *f; |
|---|
| 1650 | { |
|---|
| 1651 | register int ch; |
|---|
| 1652 | |
|---|
| 1653 | ch = getc(f); |
|---|
| 1654 | if (ch == EOF) |
|---|
| 1655 | return 1; |
|---|
| 1656 | ungetc(ch, f); |
|---|
| 1657 | return (ch == '\n'); |
|---|
| 1658 | } |
|---|
| 1659 | |
|---|
| 1660 | void memerror() |
|---|
| 1661 | { |
|---|
| 1662 | printf("Error allocating memory\n"); |
|---|
| 1663 | exit(-1); |
|---|
| 1664 | } |
|---|
| 1665 | |
|---|
| 1666 | MALLOCRETURN *mymalloc(x) |
|---|
| 1667 | long x; |
|---|
| 1668 | { |
|---|
| 1669 | MALLOCRETURN *mem; |
|---|
| 1670 | mem = (MALLOCRETURN *)malloc(x); |
|---|
| 1671 | if (!mem) |
|---|
| 1672 | memerror(); |
|---|
| 1673 | else |
|---|
| 1674 | return (MALLOCRETURN *)mem; |
|---|
| 1675 | } |
|---|
| 1676 | |
|---|