| 1 | #define programName "AxMl" |
|---|
| 2 | #define programVersion "1.1.0" |
|---|
| 3 | #define programVersionInt 10202 |
|---|
| 4 | #define programDate "February, 2002" |
|---|
| 5 | #define programDateInt 20000212 |
|---|
| 6 | |
|---|
| 7 | /*Time measurment */ |
|---|
| 8 | #include <sys/times.h> |
|---|
| 9 | #include <sys/types.h> |
|---|
| 10 | #include <sys/time.h> |
|---|
| 11 | #include <time.h> |
|---|
| 12 | |
|---|
| 13 | #include <stdlib.h> |
|---|
| 14 | #include <unistd.h> |
|---|
| 15 | #include <string.h> |
|---|
| 16 | |
|---|
| 17 | double accTime = 0.0; |
|---|
| 18 | double globTime = 0.0; |
|---|
| 19 | struct timeval ttime; |
|---|
| 20 | |
|---|
| 21 | long flopBlocks = 0; |
|---|
| 22 | |
|---|
| 23 | double gettime() |
|---|
| 24 | { |
|---|
| 25 | gettimeofday(&ttime , NULL); |
|---|
| 26 | return ttime.tv_sec + ttime.tv_usec * 0.000001; |
|---|
| 27 | } |
|---|
| 28 | /*Time measurment*/ |
|---|
| 29 | |
|---|
| 30 | /* AxMl, a program for estimation of phylogenetic trees from sequences |
|---|
| 31 | * implementing SEVs (Subtree Equality Vectors) Copyright (C) 2002 by |
|---|
| 32 | * Alexandros P. Stamatakis. |
|---|
| 33 | * Derived from: |
|---|
| 34 | * fastDNAml, a program for estimation of phylogenetic trees from sequences. |
|---|
| 35 | * Copyright (C) 1998, 1999, 2000 by Gary J. Olsen |
|---|
| 36 | * |
|---|
| 37 | * This program is free software; you may redistribute it and/or modify it |
|---|
| 38 | * under the terms of the GNU General Public License as published by the Free |
|---|
| 39 | * Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 40 | * any later version. |
|---|
| 41 | * |
|---|
| 42 | * This program is distributed in the hope that it will be useful, but |
|---|
| 43 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|---|
| 44 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|---|
| 45 | * for more details. |
|---|
| 46 | * |
|---|
| 47 | * You should have received a copy of the GNU General Public License along |
|---|
| 48 | * with this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 49 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 50 | * |
|---|
| 51 | * For any other enquiries write to Gary J. Olsen, Department of Microbiology, |
|---|
| 52 | * University of Illinois, Urbana, IL 61801, USA |
|---|
| 53 | * |
|---|
| 54 | * Or send E-mail to gary@phylo.life.uiuc.edu |
|---|
| 55 | * |
|---|
| 56 | * |
|---|
| 57 | * fastDNAml is based in part on the program dnaml by Joseph Felsenstein. |
|---|
| 58 | * |
|---|
| 59 | * Copyright notice from dnaml: |
|---|
| 60 | * |
|---|
| 61 | * version 3.3. (c) Copyright 1986, 1990 by the University of Washington |
|---|
| 62 | * and Joseph Felsenstein. Written by Joseph Felsenstein. Permission is |
|---|
| 63 | * granted to copy and use this program provided no fee is charged for it |
|---|
| 64 | * and provided that this copyright notice is not removed. |
|---|
| 65 | * |
|---|
| 66 | * |
|---|
| 67 | * When publishing work that based on results from fastDNAml please cite: |
|---|
| 68 | * |
|---|
| 69 | * Felsenstein, J. 1981. Evolutionary trees from DNA sequences: |
|---|
| 70 | * A maximum likelihood approach. J. Mol. Evol. 17: 368-376. |
|---|
| 71 | * |
|---|
| 72 | * and |
|---|
| 73 | * |
|---|
| 74 | * Olsen, G. J., Matsuda, H., Hagstrom, R., and Overbeek, R. 1994. |
|---|
| 75 | * fastDNAml: A tool for construction of phylogenetic trees of DNA |
|---|
| 76 | * sequences using maximum likelihood. Comput. Appl. Biosci. 10: 41-48. |
|---|
| 77 | */ |
|---|
| 78 | |
|---|
| 79 | /* Conversion to C and changes in sequential code by Gary Olsen, 1991-1994 |
|---|
| 80 | * |
|---|
| 81 | * p4 version by Hideo Matsuda and Ross Overbeek, 1991-1993 |
|---|
| 82 | */ |
|---|
| 83 | |
|---|
| 84 | /* |
|---|
| 85 | * 1.0 March 14, 1992 |
|---|
| 86 | * Initial "release" version |
|---|
| 87 | * |
|---|
| 88 | * 1.0.1 March 18, 1992 |
|---|
| 89 | * Add ntaxa to tree comments |
|---|
| 90 | * Set minimum branch length on reading tree |
|---|
| 91 | * Add blanks around operators in treeString (for prolog parsing) |
|---|
| 92 | * Add program version to treeString comments |
|---|
| 93 | * |
|---|
| 94 | * 1.0.2 April 6, 1992 |
|---|
| 95 | * Improved option line diagnostics |
|---|
| 96 | * Improved auxiliary line diagnostics |
|---|
| 97 | * Removed some trailing blanks from output |
|---|
| 98 | * |
|---|
| 99 | * 1.0.3 April 6, 1992 |
|---|
| 100 | * Checkpoint trees that do not need any optimization |
|---|
| 101 | * Print restart tree likelihood before optimizing |
|---|
| 102 | * Fix treefile option so that it really toggles |
|---|
| 103 | * |
|---|
| 104 | * 1.0.4 July 13, 1992 |
|---|
| 105 | * Add support for tree fact (instead of true Newick tree) in |
|---|
| 106 | * processTreeCom, treeReadLen, str_processTreeCom and |
|---|
| 107 | * str_treeReadLen |
|---|
| 108 | * Use bit operations in randum |
|---|
| 109 | * Correct error in bootstrap mask used with weighting mask |
|---|
| 110 | * |
|---|
| 111 | * 1.0.5 August 22, 1992 |
|---|
| 112 | * Fix reading of underscore as first nonblank character in name |
|---|
| 113 | * Add strchr and strstr functions to source code |
|---|
| 114 | * Add output treefile name to message "Tree also written ..." |
|---|
| 115 | * |
|---|
| 116 | * 1.0.6 November 20, 1992 |
|---|
| 117 | * Change (! nsites) test in setupTopol to (nsites == 0) for MIPS R4000 |
|---|
| 118 | * Add vectorizing compiler directives for CRAY |
|---|
| 119 | * Include updates and corrections to parallel code from H. Matsuda |
|---|
| 120 | * |
|---|
| 121 | * 1.0.7 March 25, 1993 |
|---|
| 122 | * Remove translation of underlines in taxon names |
|---|
| 123 | * |
|---|
| 124 | * 1.0.8 April 30, 1993 |
|---|
| 125 | * Remove version number from fastDNAml.h file name |
|---|
| 126 | * |
|---|
| 127 | * 1.0.9 August 12, 1993 |
|---|
| 128 | * Version was never released. |
|---|
| 129 | * Redefine treefile formats and default: |
|---|
| 130 | * 0 None |
|---|
| 131 | * 1 Newick |
|---|
| 132 | * 2 Prolog |
|---|
| 133 | * 3 PHYLIP (Default) |
|---|
| 134 | * Remove quote marks and comment from PHYLIP treefile format. |
|---|
| 135 | * |
|---|
| 136 | * 1.1.0 September 3-5, 1993 |
|---|
| 137 | * Arrays of size maxpatterns moved from stack to heap (mallocs) in |
|---|
| 138 | * evaluateslope, makenewz, and cmpBestTrees. |
|---|
| 139 | * Correct [maxsites] to [maxpatterns] in temporary array definitions |
|---|
| 140 | * in Vectorize code of newview and evaluate. (These should also |
|---|
| 141 | * get converted to use malloc() at some point.) |
|---|
| 142 | * Change randum to use 12 bit segments, not 6. Change its seed |
|---|
| 143 | * parameter to long *. |
|---|
| 144 | * Remove the code that took the absolute value of random seeds. |
|---|
| 145 | * Correct integer divide artifact in setting default transition/ |
|---|
| 146 | * transversion parameter values. |
|---|
| 147 | * When transition/transversion ratio is "reset", change to small |
|---|
| 148 | * value, not the program default. |
|---|
| 149 | * Report the "reset" transition/transversion ratio in the output. |
|---|
| 150 | * Move global data into analdef, rawDNA, and crunchedDNA structures. |
|---|
| 151 | * Change names of routines white and digit to whitechar and digitchar. |
|---|
| 152 | * Convert y[] to yType, which is normally char, but is int if the |
|---|
| 153 | * Vectorize flag is set. |
|---|
| 154 | * Split option line reading out of getoptions routine. |
|---|
| 155 | * |
|---|
| 156 | * 1.1.1 September 30, 1994 |
|---|
| 157 | * Incorporate changes made in 1.0.A (Feb. 11, 1994): |
|---|
| 158 | * Remove processing of quotation marks within comments. |
|---|
| 159 | * Break label finding into copy to string and find tip. |
|---|
| 160 | * Generalize tree reading to read trees when names are and are not |
|---|
| 161 | * already known. |
|---|
| 162 | * Remove absolute value from randum seed reading. |
|---|
| 163 | * Include integer version number and program date. |
|---|
| 164 | * Remove maxsite, maxpatterns and maxsp limitations. |
|---|
| 165 | * Incorporate code for retaining multiple trees. |
|---|
| 166 | * Activate code for Hasegawa & Kishino test of tree differences. |
|---|
| 167 | * Make quick add the default, with Q turning it off. |
|---|
| 168 | * Make emperical frequencies the option with F turning it off. |
|---|
| 169 | * Allow a residue frequency option line anywhere in the options. |
|---|
| 170 | * Increase string length passed to treeString (should be length |
|---|
| 171 | * checked, but ...) |
|---|
| 172 | * Introduce (Sept.30) and fix (Oct. 26) bug in bootstrap sampling. |
|---|
| 173 | * Fix error when user frequencies are last line and start with F. |
|---|
| 174 | * |
|---|
| 175 | * 1.2 September 5, 1997 |
|---|
| 176 | * Move likelihood components into structure. |
|---|
| 177 | * Change rawDNA to rawdata. |
|---|
| 178 | * Change crunchedDNA to cruncheddata. |
|---|
| 179 | * Recast the likelihoods per site into an array of stuctures, |
|---|
| 180 | * where each stucture (likelivector) includes the likelihoods |
|---|
| 181 | * of each residue type at the site, and a magnitude scale |
|---|
| 182 | * factor (exp). This requires changing the space allocation, |
|---|
| 183 | * newview, makenewz, evaluate, and sigma. |
|---|
| 184 | * Change code of newview to rescale likelihoods up by 2**256 when |
|---|
| 185 | * the largest value falls below 2**-256. This should solve |
|---|
| 186 | * floating point underflow for all practical sized trees. |
|---|
| 187 | * No changes are necessary in makenewz or sigma, since only |
|---|
| 188 | * relative likelihoods are necessary. |
|---|
| 189 | * |
|---|
| 190 | * 1.2.1 March 9, 1998 |
|---|
| 191 | * Convert likelihood adjustment factor (2**256) to a constant. |
|---|
| 192 | * Fix vectorized calculation of likelihood (error introduced in 1.2) |
|---|
| 193 | * |
|---|
| 194 | * 1.2.2 December 23, 1998 |
|---|
| 195 | * General code clean-up. |
|---|
| 196 | * Convert to function definitions with parameter type lists |
|---|
| 197 | * |
|---|
| 198 | * 1.2.2 January 3, 2000 |
|---|
| 199 | * Add copyright and license information |
|---|
| 200 | * Make this the current release version |
|---|
| 201 | */ |
|---|
| 202 | |
|---|
| 203 | #ifdef Master |
|---|
| 204 | # undef Master |
|---|
| 205 | # define Master 1 |
|---|
| 206 | # define Slave 0 |
|---|
| 207 | # define Sequential 0 |
|---|
| 208 | #else |
|---|
| 209 | # ifdef Slave |
|---|
| 210 | # undef Slave |
|---|
| 211 | # define Master 0 |
|---|
| 212 | # define Slave 1 |
|---|
| 213 | # define Sequential 0 |
|---|
| 214 | # else |
|---|
| 215 | # ifdef Sequential |
|---|
| 216 | # undef Sequential |
|---|
| 217 | # endif |
|---|
| 218 | # define Master 0 |
|---|
| 219 | # define Slave 0 |
|---|
| 220 | # define Sequential 1 |
|---|
| 221 | # endif |
|---|
| 222 | #endif |
|---|
| 223 | |
|---|
| 224 | #ifdef CRAY |
|---|
| 225 | # define Vectorize |
|---|
| 226 | #endif |
|---|
| 227 | |
|---|
| 228 | #ifdef Vectorize |
|---|
| 229 | # define maxpatterns 10000 /* maximum number of different site patterns */ |
|---|
| 230 | #endif |
|---|
| 231 | |
|---|
| 232 | #include <stdio.h> |
|---|
| 233 | #include <math.h> |
|---|
| 234 | #include "axml.h" /* Requires version 1.2 */ |
|---|
| 235 | |
|---|
| 236 | #if Master || Slave |
|---|
| 237 | # include "p4.h" |
|---|
| 238 | # include "comm_link.h" |
|---|
| 239 | #endif |
|---|
| 240 | |
|---|
| 241 | /* Global variables */ |
|---|
| 242 | |
|---|
| 243 | xarray *usedxtip, *freextip; |
|---|
| 244 | |
|---|
| 245 | #ifdef FLOP_BLOCK_COUNTER |
|---|
| 246 | unsigned long flopBlock = 0; |
|---|
| 247 | #endif |
|---|
| 248 | |
|---|
| 249 | #if Sequential /* Use standard input */ |
|---|
| 250 | # undef DNAML_STEP |
|---|
| 251 | # define DNAML_STEP 0 |
|---|
| 252 | # define INFILE stdin |
|---|
| 253 | #endif |
|---|
| 254 | |
|---|
| 255 | #if Master |
|---|
| 256 | # define MAX_SEND_AHEAD 400 |
|---|
| 257 | char *best_tr_recv = NULL; /* these are used for flow control */ |
|---|
| 258 | double best_lk_recv; |
|---|
| 259 | int send_ahead = 0; /* number of outstanding sends */ |
|---|
| 260 | |
|---|
| 261 | # ifdef DNAML_STEP |
|---|
| 262 | # define DNAML_STEP 1 |
|---|
| 263 | # endif |
|---|
| 264 | # define INFILE Seqf |
|---|
| 265 | # define OUTFILE Outf |
|---|
| 266 | FILE *INFILE, *OUTFILE; |
|---|
| 267 | comm_block comm_slave; |
|---|
| 268 | #endif |
|---|
| 269 | |
|---|
| 270 | #if Slave |
|---|
| 271 | # undef DNAML_STEP |
|---|
| 272 | # define DNAML_STEP 0 |
|---|
| 273 | # define INFILE Seqf |
|---|
| 274 | # define OUTFILE Outf |
|---|
| 275 | FILE *INFILE, *OUTFILE; |
|---|
| 276 | comm_block comm_master; |
|---|
| 277 | #endif |
|---|
| 278 | |
|---|
| 279 | #if Debug |
|---|
| 280 | FILE *debug; |
|---|
| 281 | #endif |
|---|
| 282 | |
|---|
| 283 | #if DNAML_STEP |
|---|
| 284 | int begin_step_time, end_step_time; |
|---|
| 285 | # define REPORT_ADD_SPECS p4_send(DNAML_ADD_SPECS, DNAML_HOST_ID, NULL, 0) |
|---|
| 286 | # define REPORT_SEND_TREE p4_send(DNAML_SEND_TREE, DNAML_HOST_ID, NULL, 0) |
|---|
| 287 | # define REPORT_RECV_TREE p4_send(DNAML_RECV_TREE, DNAML_HOST_ID, NULL, 0) |
|---|
| 288 | # define REPORT_STEP_TIME \ |
|---|
| 289 | {\ |
|---|
| 290 | char send_buf[80]; \ |
|---|
| 291 | end_step_time = p4_clock(); \ |
|---|
| 292 | (void) sprintf(send_buf, "%d", end_step_time-begin_step_time); \ |
|---|
| 293 | p4_send(DNAML_STEP_TIME, DNAML_HOST_ID, send_buf,strlen(send_buf)+1); \ |
|---|
| 294 | begin_step_time = end_step_time; \ |
|---|
| 295 | } |
|---|
| 296 | #else |
|---|
| 297 | # define REPORT_ADD_SPECS |
|---|
| 298 | # define REPORT_SEND_TREE |
|---|
| 299 | # define REPORT_RECV_TREE |
|---|
| 300 | # define REPORT_STEP_TIME |
|---|
| 301 | #endif |
|---|
| 302 | |
|---|
| 303 | /*=======================================================================*/ |
|---|
| 304 | /* PROGRAM */ |
|---|
| 305 | /*=======================================================================*/ |
|---|
| 306 | /* Best tree handling for dnaml */ |
|---|
| 307 | /*=======================================================================*/ |
|---|
| 308 | |
|---|
| 309 | /* Tip value comparisons |
|---|
| 310 | * |
|---|
| 311 | * Use void pointers to hide type from other routines. Only tipValPtr and |
|---|
| 312 | * cmpTipVal need to be changed to alter the nature of the values compared |
|---|
| 313 | * (e.g., names instead of node numbers). |
|---|
| 314 | * |
|---|
| 315 | * cmpTipVal(tipValPtr(nodeptr p), tipValPtr(nodeptr q)) == -1, 0 or 1. |
|---|
| 316 | * |
|---|
| 317 | * This provides direct comparison of tip values (for example, for |
|---|
| 318 | * definition of tr->start). |
|---|
| 319 | */ |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | void *tipValPtr (nodeptr p) |
|---|
| 323 | { return (void *) & p->number; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | int cmpTipVal (void *v1, void *v2) |
|---|
| 328 | { /* cmpTipVal */ |
|---|
| 329 | int i1, i2; |
|---|
| 330 | |
|---|
| 331 | i1 = *((int *) v1); |
|---|
| 332 | i2 = *((int *) v2); |
|---|
| 333 | return (i1 < i2) ? -1 : ((i1 == i2) ? 0 : 1); |
|---|
| 334 | } /* cmpTipVal */ |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | /* These are the only routines that need to UNDERSTAND topologies */ |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | topol *setupTopol (int maxtips, int nsites) |
|---|
| 341 | { /* setupTopol */ |
|---|
| 342 | topol *tpl; |
|---|
| 343 | |
|---|
| 344 | if (! (tpl = (topol *) Malloc(sizeof(topol))) || |
|---|
| 345 | ! (tpl->links = (connptr) Malloc((2*maxtips-3) * sizeof(connect))) || |
|---|
| 346 | (nsites && ! (tpl->log_f |
|---|
| 347 | = (double *) Malloc(nsites * sizeof(double))))) { |
|---|
| 348 | printf("ERROR: Unable to get topology memory"); |
|---|
| 349 | tpl = (topol *) NULL; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | else { |
|---|
| 353 | if (nsites == 0) tpl->log_f = (double *) NULL; |
|---|
| 354 | tpl->likelihood = unlikely; |
|---|
| 355 | tpl->start = (node *) NULL; |
|---|
| 356 | tpl->nextlink = 0; |
|---|
| 357 | tpl->ntips = 0; |
|---|
| 358 | tpl->nextnode = 0; |
|---|
| 359 | tpl->opt_level = 0; /* degree of branch swapping explored */ |
|---|
| 360 | tpl->scrNum = 0; /* position in sorted list of scores */ |
|---|
| 361 | tpl->tplNum = 0; /* position in sorted list of trees */ |
|---|
| 362 | tpl->log_f_valid = 0; /* log_f value sites */ |
|---|
| 363 | tpl->prelabeled = TRUE; |
|---|
| 364 | tpl->smoothed = FALSE; /* branch optimization converged? */ |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | return tpl; |
|---|
| 368 | } /* setupTopol */ |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | void freeTopol (topol *tpl) |
|---|
| 372 | { /* freeTopol */ |
|---|
| 373 | Free(tpl->links); |
|---|
| 374 | if (tpl->log_f) Free(tpl->log_f); |
|---|
| 375 | Free(tpl); |
|---|
| 376 | } /* freeTopol */ |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | int saveSubtree (nodeptr p, topol *tpl) |
|---|
| 380 | /* Save a subtree in a standard order so that earlier branches |
|---|
| 381 | * from a node contain lower value tips than do second branches from |
|---|
| 382 | * the node. This code works with arbitrary furcations in the tree. |
|---|
| 383 | */ |
|---|
| 384 | { /* saveSubtree */ |
|---|
| 385 | connptr r, r0; |
|---|
| 386 | nodeptr q, s; |
|---|
| 387 | int t, t0, t1; |
|---|
| 388 | |
|---|
| 389 | r0 = tpl->links; |
|---|
| 390 | r = r0 + (tpl->nextlink)++; |
|---|
| 391 | r->p = p; |
|---|
| 392 | r->q = q = p->back; |
|---|
| 393 | r->z = p->z; |
|---|
| 394 | r->descend = 0; /* No children (yet) */ |
|---|
| 395 | |
|---|
| 396 | if (q->tip) { |
|---|
| 397 | r->valptr = tipValPtr(q); /* Assign value */ |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | else { /* Internal node, look at children */ |
|---|
| 401 | s = q->next; /* First child */ |
|---|
| 402 | do { |
|---|
| 403 | t = saveSubtree(s, tpl); /* Generate child's subtree */ |
|---|
| 404 | |
|---|
| 405 | t0 = 0; /* Merge child into list */ |
|---|
| 406 | t1 = r->descend; |
|---|
| 407 | while (t1 && (cmpTipVal(r0[t1].valptr, r0[t].valptr) < 0)) { |
|---|
| 408 | t0 = t1; |
|---|
| 409 | t1 = r0[t1].sibling; |
|---|
| 410 | } |
|---|
| 411 | if (t0) r0[t0].sibling = t; else r->descend = t; |
|---|
| 412 | r0[t].sibling = t1; |
|---|
| 413 | |
|---|
| 414 | s = s->next; /* Next child */ |
|---|
| 415 | } while (s != q); |
|---|
| 416 | |
|---|
| 417 | r->valptr = r0[r->descend].valptr; /* Inherit first child's value */ |
|---|
| 418 | } /* End of internal node processing */ |
|---|
| 419 | |
|---|
| 420 | return r - r0; |
|---|
| 421 | } /* saveSubtree */ |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | nodeptr minSubtreeTip (nodeptr p0) |
|---|
| 425 | { /* minTreeTip */ |
|---|
| 426 | nodeptr minTip, p, testTip; |
|---|
| 427 | |
|---|
| 428 | if (p0->tip) return p0; |
|---|
| 429 | |
|---|
| 430 | p = p0->next; |
|---|
| 431 | minTip = minSubtreeTip(p->back); |
|---|
| 432 | while ((p = p->next) != p0) { |
|---|
| 433 | testTip = minSubtreeTip(p->back); |
|---|
| 434 | if (cmpTipVal(tipValPtr(testTip), tipValPtr(minTip)) < 0) |
|---|
| 435 | minTip = testTip; |
|---|
| 436 | } |
|---|
| 437 | return minTip; |
|---|
| 438 | } /* minTreeTip */ |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | nodeptr minTreeTip (nodeptr p) |
|---|
| 442 | { /* minTreeTip */ |
|---|
| 443 | nodeptr minp, minpb; |
|---|
| 444 | |
|---|
| 445 | minp = minSubtreeTip(p); |
|---|
| 446 | minpb = minSubtreeTip(p->back); |
|---|
| 447 | return cmpTipVal(tipValPtr(minp), tipValPtr(minpb)) < 0 ? minp : minpb; |
|---|
| 448 | } /* minTreeTip */ |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | void saveTree (tree *tr, topol *tpl) |
|---|
| 452 | /* Save a tree topology in a standard order so that first branches |
|---|
| 453 | * from a node contain lower value tips than do second branches from |
|---|
| 454 | * the node. The root tip should have the lowest value of all. |
|---|
| 455 | */ |
|---|
| 456 | { /* saveTree */ |
|---|
| 457 | connptr r; |
|---|
| 458 | double *tr_log_f, *tpl_log_f; |
|---|
| 459 | int i; |
|---|
| 460 | |
|---|
| 461 | tpl->nextlink = 0; /* Reset link pointer */ |
|---|
| 462 | r = tpl->links + saveSubtree(minTreeTip(tr->start), tpl); /* Save tree */ |
|---|
| 463 | r->sibling = 0; |
|---|
| 464 | |
|---|
| 465 | tpl->likelihood = tr->likelihood; |
|---|
| 466 | tpl->start = tr->start; |
|---|
| 467 | tpl->ntips = tr->ntips; |
|---|
| 468 | tpl->nextnode = tr->nextnode; |
|---|
| 469 | tpl->opt_level = tr->opt_level; |
|---|
| 470 | tpl->prelabeled = tr->prelabeled; |
|---|
| 471 | tpl->smoothed = tr->smoothed; |
|---|
| 472 | |
|---|
| 473 | if (tpl_log_f = tpl->log_f) { |
|---|
| 474 | tr_log_f = tr->log_f; |
|---|
| 475 | i = tpl->log_f_valid = tr->log_f_valid; |
|---|
| 476 | while (--i >= 0) *tpl_log_f++ = *tr_log_f++; |
|---|
| 477 | } |
|---|
| 478 | else { |
|---|
| 479 | tpl->log_f_valid = 0; |
|---|
| 480 | } |
|---|
| 481 | } /* saveTree */ |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | void copyTopol (topol *tpl1, topol *tpl2) |
|---|
| 485 | { /* copyTopol */ |
|---|
| 486 | connptr r1, r2, r10, r20; |
|---|
| 487 | double *tpl1_log_f, *tpl2_log_f; |
|---|
| 488 | int i; |
|---|
| 489 | |
|---|
| 490 | r10 = tpl1->links; |
|---|
| 491 | r20 = tpl2->links; |
|---|
| 492 | tpl2->nextlink = tpl1->nextlink; |
|---|
| 493 | |
|---|
| 494 | r1 = r10; |
|---|
| 495 | r2 = r20; |
|---|
| 496 | i = 2 * tpl1->ntips - 3; |
|---|
| 497 | while (--i >= 0) { |
|---|
| 498 | r2->z = r1->z; |
|---|
| 499 | r2->p = r1->p; |
|---|
| 500 | r2->q = r1->q; |
|---|
| 501 | r2->valptr = r1->valptr; |
|---|
| 502 | r2->descend = r1->descend; |
|---|
| 503 | r2->sibling = r1->sibling; |
|---|
| 504 | r1++; |
|---|
| 505 | r2++; |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | if (tpl1->log_f_valid && tpl2->log_f) { |
|---|
| 509 | tpl1_log_f = tpl1->log_f; |
|---|
| 510 | tpl2_log_f = tpl2->log_f; |
|---|
| 511 | tpl2->log_f_valid = i = tpl1->log_f_valid; |
|---|
| 512 | while (--i >= 0) *tpl2_log_f++ = *tpl1_log_f++; |
|---|
| 513 | } |
|---|
| 514 | else { |
|---|
| 515 | tpl2->log_f_valid = 0; |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | tpl2->likelihood = tpl1->likelihood; |
|---|
| 519 | tpl2->start = tpl1->start; |
|---|
| 520 | tpl2->ntips = tpl1->ntips; |
|---|
| 521 | tpl2->nextnode = tpl1->nextnode; |
|---|
| 522 | tpl2->opt_level = tpl1->opt_level; |
|---|
| 523 | tpl2->prelabeled = tpl1->prelabeled; |
|---|
| 524 | tpl2->scrNum = tpl1->scrNum; |
|---|
| 525 | tpl2->tplNum = tpl1->tplNum; |
|---|
| 526 | tpl2->smoothed = tpl1->smoothed; |
|---|
| 527 | } /* copyTopol */ |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | boolean restoreTree (topol *tpl, tree *tr) |
|---|
| 531 | { /* restoreTree */ |
|---|
| 532 | void hookup(); |
|---|
| 533 | boolean initrav(); |
|---|
| 534 | |
|---|
| 535 | connptr r; |
|---|
| 536 | nodeptr p, p0; |
|---|
| 537 | double *tr_log_f, *tpl_log_f; |
|---|
| 538 | int i; |
|---|
| 539 | |
|---|
| 540 | /* Clear existing connections */ |
|---|
| 541 | |
|---|
| 542 | for (i = 1; i <= 2*(tr->mxtips) - 2; i++) { /* Uses p = p->next at tip */ |
|---|
| 543 | p0 = p = tr->nodep[i]; |
|---|
| 544 | do { |
|---|
| 545 | p->back = (nodeptr) NULL; |
|---|
| 546 | p = p->next; |
|---|
| 547 | } while (p != p0); |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | /* Copy connections from topology */ |
|---|
| 551 | |
|---|
| 552 | for (r = tpl->links, i = 0; i < tpl->nextlink; r++, i++) { |
|---|
| 553 | hookup(r->p, r->q, r->z); |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | tr->likelihood = tpl->likelihood; |
|---|
| 557 | tr->start = tpl->start; |
|---|
| 558 | tr->ntips = tpl->ntips; |
|---|
| 559 | tr->nextnode = tpl->nextnode; |
|---|
| 560 | tr->opt_level = tpl->opt_level; |
|---|
| 561 | tr->prelabeled = tpl->prelabeled; |
|---|
| 562 | tr->smoothed = tpl->smoothed; |
|---|
| 563 | |
|---|
| 564 | if (tpl_log_f = tpl->log_f) { |
|---|
| 565 | tr_log_f = tr->log_f; |
|---|
| 566 | i = tr->log_f_valid = tpl->log_f_valid; |
|---|
| 567 | while (--i >= 0) *tr_log_f++ = *tpl_log_f++; |
|---|
| 568 | } |
|---|
| 569 | else { |
|---|
| 570 | tr->log_f_valid = 0; |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | return (initrav(tr, tr->start) && initrav(tr, tr->start->back)); |
|---|
| 574 | } /* restoreTree */ |
|---|
| 575 | |
|---|
| 576 | |
|---|
| 577 | int initBestTree (bestlist *bt, int newkeep, int numsp, int sites) |
|---|
| 578 | { /* initBestTree */ |
|---|
| 579 | int i, nlogf; |
|---|
| 580 | |
|---|
| 581 | |
|---|
| 582 | bt->nkeep = 0; |
|---|
| 583 | |
|---|
| 584 | if (bt->ninit <= 0) { |
|---|
| 585 | if (! (bt->start = setupTopol(numsp, sites))) return 0; |
|---|
| 586 | bt->ninit = -1; |
|---|
| 587 | bt->nvalid = 0; |
|---|
| 588 | bt->numtrees = 0; |
|---|
| 589 | bt->best = unlikely; |
|---|
| 590 | bt->improved = FALSE; |
|---|
| 591 | bt->byScore = (topol **) Malloc((newkeep+1) * sizeof(topol *)); |
|---|
| 592 | bt->byTopol = (topol **) Malloc((newkeep+1) * sizeof(topol *)); |
|---|
| 593 | if (! bt->byScore || ! bt->byTopol) { |
|---|
| 594 | fprintf(stderr, "initBestTree: Malloc failure\n"); |
|---|
| 595 | return 0; |
|---|
| 596 | } |
|---|
| 597 | } |
|---|
| 598 | else if (ABS(newkeep) > bt->ninit) { |
|---|
| 599 | if (newkeep < 0) newkeep = -(bt->ninit); |
|---|
| 600 | else newkeep = bt->ninit; |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | if (newkeep < 1) { /* Use negative newkeep to clear list */ |
|---|
| 604 | newkeep = -newkeep; |
|---|
| 605 | if (newkeep < 1) newkeep = 1; |
|---|
| 606 | bt->nvalid = 0; |
|---|
| 607 | bt->best = unlikely; |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | if (bt->nvalid >= newkeep) { |
|---|
| 611 | bt->nvalid = newkeep; |
|---|
| 612 | bt->worst = bt->byScore[newkeep]->likelihood; |
|---|
| 613 | } |
|---|
| 614 | else { |
|---|
| 615 | bt->worst = unlikely; |
|---|
| 616 | } |
|---|
| 617 | |
|---|
| 618 | for (i = bt->ninit + 1; i <= newkeep; i++) { |
|---|
| 619 | nlogf = (i <= maxlogf) ? sites : 0; |
|---|
| 620 | if (! (bt->byScore[i] = setupTopol(numsp, nlogf))) break; |
|---|
| 621 | bt->byTopol[i] = bt->byScore[i]; |
|---|
| 622 | bt->ninit = i; |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | return (bt->nkeep = MIN(newkeep, bt->ninit)); |
|---|
| 626 | } /* initBestTree */ |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | int resetBestTree (bestlist *bt) |
|---|
| 631 | { /* resetBestTree */ |
|---|
| 632 | bt->best = unlikely; |
|---|
| 633 | bt->worst = unlikely; |
|---|
| 634 | bt->nvalid = 0; |
|---|
| 635 | bt->improved = FALSE; |
|---|
| 636 | } /* resetBestTree */ |
|---|
| 637 | |
|---|
| 638 | |
|---|
| 639 | boolean freeBestTree(bestlist *bt) |
|---|
| 640 | { /* freeBestTree */ |
|---|
| 641 | while (bt->ninit >= 0) freeTopol(bt->byScore[(bt->ninit)--]); |
|---|
| 642 | freeTopol(bt->start); |
|---|
| 643 | return TRUE; |
|---|
| 644 | } /* freeBestTree */ |
|---|
| 645 | |
|---|
| 646 | |
|---|
| 647 | /* Compare two trees, assuming that each is in standard order. Return |
|---|
| 648 | * -1 if first preceeds second, 0 if they are identical, or +1 if first |
|---|
| 649 | * follows second in standard order. Lower number tips preceed higher |
|---|
| 650 | * number tips. A tip preceeds a corresponding internal node. Internal |
|---|
| 651 | * nodes are ranked by their lowest number tip. |
|---|
| 652 | */ |
|---|
| 653 | |
|---|
| 654 | int cmpSubtopol (connptr p10, connptr p1, connptr p20, connptr p2) |
|---|
| 655 | { /* cmpSubtopol */ |
|---|
| 656 | connptr p1d, p2d; |
|---|
| 657 | int cmp; |
|---|
| 658 | |
|---|
| 659 | if (! p1->descend && ! p2->descend) /* Two tips */ |
|---|
| 660 | return cmpTipVal(p1->valptr, p2->valptr); |
|---|
| 661 | |
|---|
| 662 | if (! p1->descend) return -1; /* p1 = tip, p2 = node */ |
|---|
| 663 | if (! p2->descend) return 1; /* p2 = tip, p1 = node */ |
|---|
| 664 | |
|---|
| 665 | p1d = p10 + p1->descend; |
|---|
| 666 | p2d = p20 + p2->descend; |
|---|
| 667 | while (1) { /* Two nodes */ |
|---|
| 668 | if (cmp = cmpSubtopol(p10, p1d, p20, p2d)) return cmp; /* Subtrees */ |
|---|
| 669 | if (! p1d->sibling && ! p2d->sibling) return 0; /* Lists done */ |
|---|
| 670 | if (! p1d->sibling) return -1; /* One done, other not */ |
|---|
| 671 | if (! p2d->sibling) return 1; /* One done, other not */ |
|---|
| 672 | p1d = p10 + p1d->sibling; /* Neither done */ |
|---|
| 673 | p2d = p20 + p2d->sibling; |
|---|
| 674 | } |
|---|
| 675 | } /* cmpSubtopol */ |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | int cmpTopol (void *tpl1, void *tpl2) |
|---|
| 680 | { /* cmpTopol */ |
|---|
| 681 | connptr r1, r2; |
|---|
| 682 | int cmp; |
|---|
| 683 | |
|---|
| 684 | r1 = ((topol *) tpl1)->links; |
|---|
| 685 | r2 = ((topol *) tpl2)->links; |
|---|
| 686 | cmp = cmpTipVal(tipValPtr(r1->p), tipValPtr(r2->p)); |
|---|
| 687 | if (cmp) return cmp; |
|---|
| 688 | return cmpSubtopol(r1, r1, r2, r2); |
|---|
| 689 | } /* cmpTopol */ |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | int cmpTplScore (void *tpl1, void *tpl2) |
|---|
| 694 | { /* cmpTplScore */ |
|---|
| 695 | double l1, l2; |
|---|
| 696 | |
|---|
| 697 | l1 = ((topol *) tpl1)->likelihood; |
|---|
| 698 | l2 = ((topol *) tpl2)->likelihood; |
|---|
| 699 | return (l1 > l2) ? -1 : ((l1 == l2) ? 0 : 1); |
|---|
| 700 | } /* cmpTplScore */ |
|---|
| 701 | |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | /* Find an item in a sorted list of n items. If the item is in the list, |
|---|
| 705 | * return its index. If it is not in the list, return the negative of the |
|---|
| 706 | * position into which it should be inserted. |
|---|
| 707 | */ |
|---|
| 708 | |
|---|
| 709 | int findInList (void *item, void *list[], int n, int (* cmpFunc)()) |
|---|
| 710 | { /* findInList */ |
|---|
| 711 | int mid, hi, lo, cmp; |
|---|
| 712 | |
|---|
| 713 | if (n < 1) return -1; /* No match; first index */ |
|---|
| 714 | |
|---|
| 715 | lo = 1; |
|---|
| 716 | mid = 0; |
|---|
| 717 | hi = n; |
|---|
| 718 | while (lo < hi) { |
|---|
| 719 | mid = (lo + hi) >> 1; |
|---|
| 720 | cmp = (* cmpFunc)(item, list[mid-1]); |
|---|
| 721 | if (cmp) { |
|---|
| 722 | if (cmp < 0) hi = mid; |
|---|
| 723 | else lo = mid + 1; |
|---|
| 724 | } |
|---|
| 725 | else return mid; /* Exact match */ |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | if (lo != mid) { |
|---|
| 729 | cmp = (* cmpFunc)(item, list[lo-1]); |
|---|
| 730 | if (cmp == 0) return lo; |
|---|
| 731 | } |
|---|
| 732 | if (cmp > 0) lo++; /* Result of step = 0 test */ |
|---|
| 733 | return -lo; |
|---|
| 734 | } /* findInList */ |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | int findTreeInList (bestlist *bt, tree *tr) |
|---|
| 739 | { /* findTreeInList */ |
|---|
| 740 | topol *tpl; |
|---|
| 741 | |
|---|
| 742 | tpl = bt->byScore[0]; |
|---|
| 743 | saveTree(tr, tpl); |
|---|
| 744 | return findInList((void *) tpl, (void **) (& (bt->byTopol[1])), |
|---|
| 745 | bt->nvalid, cmpTopol); |
|---|
| 746 | } /* findTreeInList */ |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | int saveBestTree (bestlist *bt, tree *tr) |
|---|
| 750 | { /* saveBestTree */ |
|---|
| 751 | double *tr_log_f, *tpl_log_f; |
|---|
| 752 | topol *tpl, *reuse; |
|---|
| 753 | int tplNum, scrNum, reuseScrNum, reuseTplNum, i, oldValid, newValid; |
|---|
| 754 | |
|---|
| 755 | tplNum = findTreeInList(bt, tr); |
|---|
| 756 | tpl = bt->byScore[0]; |
|---|
| 757 | oldValid = newValid = bt->nvalid; |
|---|
| 758 | |
|---|
| 759 | if (tplNum > 0) { /* Topology is in list */ |
|---|
| 760 | reuse = bt->byTopol[tplNum]; /* Matching topol */ |
|---|
| 761 | reuseScrNum = reuse->scrNum; |
|---|
| 762 | reuseTplNum = reuse->tplNum; |
|---|
| 763 | } |
|---|
| 764 | /* Good enough to keep? */ |
|---|
| 765 | else if (tr->likelihood < bt->worst) return 0; |
|---|
| 766 | |
|---|
| 767 | else { /* Topology is not in list */ |
|---|
| 768 | tplNum = -tplNum; /* Add to list (not replace) */ |
|---|
| 769 | if (newValid < bt->nkeep) bt->nvalid = ++newValid; |
|---|
| 770 | reuseScrNum = newValid; /* Take worst tree */ |
|---|
| 771 | reuse = bt->byScore[reuseScrNum]; |
|---|
| 772 | reuseTplNum = (newValid > oldValid) ? newValid : reuse->tplNum; |
|---|
| 773 | if (tr->likelihood > bt->start->likelihood) bt->improved = TRUE; |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | scrNum = findInList((void *) tpl, (void **) (& (bt->byScore[1])), |
|---|
| 777 | oldValid, cmpTplScore); |
|---|
| 778 | scrNum = ABS(scrNum); |
|---|
| 779 | |
|---|
| 780 | if (scrNum < reuseScrNum) |
|---|
| 781 | for (i = reuseScrNum; i > scrNum; i--) |
|---|
| 782 | (bt->byScore[i] = bt->byScore[i-1])->scrNum = i; |
|---|
| 783 | |
|---|
| 784 | else if (scrNum > reuseScrNum) { |
|---|
| 785 | scrNum--; |
|---|
| 786 | for (i = reuseScrNum; i < scrNum; i++) |
|---|
| 787 | (bt->byScore[i] = bt->byScore[i+1])->scrNum = i; |
|---|
| 788 | } |
|---|
| 789 | |
|---|
| 790 | if (tplNum < reuseTplNum) |
|---|
| 791 | for (i = reuseTplNum; i > tplNum; i--) |
|---|
| 792 | (bt->byTopol[i] = bt->byTopol[i-1])->tplNum = i; |
|---|
| 793 | |
|---|
| 794 | else if (tplNum > reuseTplNum) { |
|---|
| 795 | tplNum--; |
|---|
| 796 | for (i = reuseTplNum; i < tplNum; i++) |
|---|
| 797 | (bt->byTopol[i] = bt->byTopol[i+1])->tplNum = i; |
|---|
| 798 | } |
|---|
| 799 | |
|---|
| 800 | if (tpl_log_f = tpl->log_f) { |
|---|
| 801 | tr_log_f = tr->log_f; |
|---|
| 802 | i = tpl->log_f_valid = tr->log_f_valid; |
|---|
| 803 | while (--i >= 0) *tpl_log_f++ = *tr_log_f++; |
|---|
| 804 | } |
|---|
| 805 | else { |
|---|
| 806 | tpl->log_f_valid = 0; |
|---|
| 807 | } |
|---|
| 808 | |
|---|
| 809 | tpl->scrNum = scrNum; |
|---|
| 810 | tpl->tplNum = tplNum; |
|---|
| 811 | bt->byTopol[tplNum] = bt->byScore[scrNum] = tpl; |
|---|
| 812 | bt->byScore[0] = reuse; |
|---|
| 813 | |
|---|
| 814 | if (scrNum == 1) bt->best = tr->likelihood; |
|---|
| 815 | if (newValid == bt->nkeep) bt->worst = bt->byScore[newValid]->likelihood; |
|---|
| 816 | |
|---|
| 817 | return scrNum; |
|---|
| 818 | } /* saveBestTree */ |
|---|
| 819 | |
|---|
| 820 | |
|---|
| 821 | int startOpt (bestlist *bt, tree *tr) |
|---|
| 822 | { /* startOpt */ |
|---|
| 823 | int scrNum; |
|---|
| 824 | |
|---|
| 825 | scrNum = saveBestTree(bt, tr); |
|---|
| 826 | copyTopol(bt->byScore[scrNum], bt->start); |
|---|
| 827 | bt->improved = FALSE; |
|---|
| 828 | return scrNum; |
|---|
| 829 | } /* startOpt */ |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | int setOptLevel (bestlist *bt, int opt_level) |
|---|
| 833 | { /* setOptLevel */ |
|---|
| 834 | int tplNum, scrNum; |
|---|
| 835 | |
|---|
| 836 | tplNum = findInList((void *) bt->start, (void **) (&(bt->byTopol[1])), |
|---|
| 837 | bt->nvalid, cmpTopol); |
|---|
| 838 | if (tplNum > 0) { |
|---|
| 839 | bt->byTopol[tplNum]->opt_level = opt_level; |
|---|
| 840 | scrNum = bt->byTopol[tplNum]->scrNum; |
|---|
| 841 | } |
|---|
| 842 | else { |
|---|
| 843 | scrNum = 0; |
|---|
| 844 | } |
|---|
| 845 | |
|---|
| 846 | return scrNum; |
|---|
| 847 | } /* setOptLevel */ |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | int recallBestTree (bestlist *bt, int rank, tree *tr) |
|---|
| 851 | { /* recallBestTree */ |
|---|
| 852 | if (rank < 1) rank = 1; |
|---|
| 853 | if (rank > bt->nvalid) rank = bt->nvalid; |
|---|
| 854 | if (rank > 0) if (! restoreTree(bt->byScore[rank], tr)) return FALSE; |
|---|
| 855 | return rank; |
|---|
| 856 | } /* recallBestTree */ |
|---|
| 857 | |
|---|
| 858 | |
|---|
| 859 | /*=======================================================================*/ |
|---|
| 860 | /* End of best tree routines */ |
|---|
| 861 | /*=======================================================================*/ |
|---|
| 862 | |
|---|
| 863 | |
|---|
| 864 | #if 0 |
|---|
| 865 | void hang(char *msg) |
|---|
| 866 | { printf("Hanging around: %s\n", msg); |
|---|
| 867 | while(1); |
|---|
| 868 | } |
|---|
| 869 | #endif |
|---|
| 870 | |
|---|
| 871 | |
|---|
| 872 | boolean getnums (rawdata *rdta) |
|---|
| 873 | /* input number of species, number of sites */ |
|---|
| 874 | { /* getnums */ |
|---|
| 875 | printf("\n%s, version %s, %s,\nCopyright (C) 2002 by Alexandros P. Stamatakis\n\n", |
|---|
| 876 | programName, |
|---|
| 877 | programVersion, |
|---|
| 878 | programDate); |
|---|
| 879 | printf("Based in part on Joseph Felsenstein's\n\n"); |
|---|
| 880 | printf(" Nucleic acid sequence Maximum Likelihood method, version 3.3\n\n\n"); |
|---|
| 881 | |
|---|
| 882 | if (fscanf(INFILE, "%d %d", & rdta->numsp, & rdta->sites) != 2) { |
|---|
| 883 | printf("ERROR: Problem reading number of species and sites\n"); |
|---|
| 884 | return FALSE; |
|---|
| 885 | } |
|---|
| 886 | printf("%d Species, %d Sites\n\n", rdta->numsp, rdta->sites); |
|---|
| 887 | |
|---|
| 888 | if (rdta->numsp < 4) { |
|---|
| 889 | printf("TOO FEW SPECIES\n"); |
|---|
| 890 | return FALSE; |
|---|
| 891 | } |
|---|
| 892 | |
|---|
| 893 | if (rdta->sites < 1) { |
|---|
| 894 | printf("TOO FEW SITES\n"); |
|---|
| 895 | return FALSE; |
|---|
| 896 | } |
|---|
| 897 | |
|---|
| 898 | return TRUE; |
|---|
| 899 | } /* getnums */ |
|---|
| 900 | |
|---|
| 901 | |
|---|
| 902 | boolean digitchar (int ch) {return (ch >= '0' && ch <= '9'); } |
|---|
| 903 | |
|---|
| 904 | |
|---|
| 905 | boolean whitechar (int ch) |
|---|
| 906 | { return (ch == ' ' || ch == '\n' || ch == '\t'); |
|---|
| 907 | } |
|---|
| 908 | |
|---|
| 909 | |
|---|
| 910 | void uppercase (int *chptr) |
|---|
| 911 | /* convert character to upper case -- either ASCII or EBCDIC */ |
|---|
| 912 | { /* uppercase */ |
|---|
| 913 | int ch; |
|---|
| 914 | |
|---|
| 915 | ch = *chptr; |
|---|
| 916 | if ((ch >= 'a' && ch <= 'i') || (ch >= 'j' && ch <= 'r') |
|---|
| 917 | || (ch >= 's' && ch <= 'z')) |
|---|
| 918 | *chptr = ch + 'A' - 'a'; |
|---|
| 919 | } /* uppercase */ |
|---|
| 920 | |
|---|
| 921 | |
|---|
| 922 | int base36 (int ch) |
|---|
| 923 | { /* base36 */ |
|---|
| 924 | if (ch >= '0' && ch <= '9') return (ch - '0'); |
|---|
| 925 | else if (ch >= 'A' && ch <= 'I') return (ch - 'A' + 10); |
|---|
| 926 | else if (ch >= 'J' && ch <= 'R') return (ch - 'J' + 19); |
|---|
| 927 | else if (ch >= 'S' && ch <= 'Z') return (ch - 'S' + 28); |
|---|
| 928 | else if (ch >= 'a' && ch <= 'i') return (ch - 'a' + 10); |
|---|
| 929 | else if (ch >= 'j' && ch <= 'r') return (ch - 'j' + 19); |
|---|
| 930 | else if (ch >= 's' && ch <= 'z') return (ch - 's' + 28); |
|---|
| 931 | else return -1; |
|---|
| 932 | } /* base36 */ |
|---|
| 933 | |
|---|
| 934 | |
|---|
| 935 | int itobase36 (int i) |
|---|
| 936 | { /* itobase36 */ |
|---|
| 937 | if (i < 0) return '?'; |
|---|
| 938 | else if (i < 10) return (i + '0'); |
|---|
| 939 | else if (i < 19) return (i - 10 + 'A'); |
|---|
| 940 | else if (i < 28) return (i - 19 + 'J'); |
|---|
| 941 | else if (i < 36) return (i - 28 + 'S'); |
|---|
| 942 | else return '?'; |
|---|
| 943 | } /* itobase36 */ |
|---|
| 944 | |
|---|
| 945 | |
|---|
| 946 | int findch (int c) |
|---|
| 947 | { /* findch */ |
|---|
| 948 | int ch; |
|---|
| 949 | |
|---|
| 950 | while ((ch = getc(INFILE)) != EOF && ch != c) ; |
|---|
| 951 | return ch; |
|---|
| 952 | } /* findch */ |
|---|
| 953 | |
|---|
| 954 | |
|---|
| 955 | #if Master || Slave |
|---|
| 956 | int str_findch (char **treestrp, int c) |
|---|
| 957 | { /* str_findch */ |
|---|
| 958 | int ch; |
|---|
| 959 | |
|---|
| 960 | while ((ch = *(*treestrp)++) != NULL && ch != c) ; |
|---|
| 961 | return ch; |
|---|
| 962 | } /* str_findch */ |
|---|
| 963 | #endif |
|---|
| 964 | |
|---|
| 965 | |
|---|
| 966 | boolean inputboot(analdef *adef) |
|---|
| 967 | /* read the bootstrap auxilliary info */ |
|---|
| 968 | { /* inputboot */ |
|---|
| 969 | if (! adef->boot) { |
|---|
| 970 | printf("ERROR: Unexpected Bootstrap auxiliary data line\n"); |
|---|
| 971 | return FALSE; |
|---|
| 972 | } |
|---|
| 973 | else if (fscanf(INFILE, "%ld", & adef->boot) != 1 || |
|---|
| 974 | findch('\n') == EOF) { |
|---|
| 975 | printf("ERROR: Problem reading bootstrap random seed value\n"); |
|---|
| 976 | return FALSE; |
|---|
| 977 | } |
|---|
| 978 | |
|---|
| 979 | return TRUE; |
|---|
| 980 | } /* inputboot */ |
|---|
| 981 | |
|---|
| 982 | |
|---|
| 983 | boolean inputcategories (rawdata *rdta) |
|---|
| 984 | /* read the category rates and the categories for each site */ |
|---|
| 985 | { /* inputcategories */ |
|---|
| 986 | int i, j, ch, ci; |
|---|
| 987 | |
|---|
| 988 | if (rdta->categs >= 0) { |
|---|
| 989 | printf("ERROR: Unexpected Categories auxiliary data line\n"); |
|---|
| 990 | return FALSE; |
|---|
| 991 | } |
|---|
| 992 | if (fscanf(INFILE, "%d", & rdta->categs) != 1) { |
|---|
| 993 | printf("ERROR: Problem reading number of rate categories\n"); |
|---|
| 994 | return FALSE; |
|---|
| 995 | } |
|---|
| 996 | if (rdta->categs < 1 || rdta->categs > maxcategories) { |
|---|
| 997 | printf("ERROR: Bad number of categories: %d\n", rdta->categs); |
|---|
| 998 | printf("Must be in range 1 - %d\n", maxcategories); |
|---|
| 999 | return FALSE; |
|---|
| 1000 | } |
|---|
| 1001 | |
|---|
| 1002 | for (j = 1; j <= rdta->categs |
|---|
| 1003 | && fscanf(INFILE, "%lf", &(rdta->catrat[j])) == 1; j++) ; |
|---|
| 1004 | |
|---|
| 1005 | if ((j <= rdta->categs) || (findch('\n') == EOF)) { |
|---|
| 1006 | printf("ERROR: Problem reading rate values\n"); |
|---|
| 1007 | return FALSE; |
|---|
| 1008 | } |
|---|
| 1009 | |
|---|
| 1010 | for (i = 1; i <= nmlngth; i++) (void) getc(INFILE); |
|---|
| 1011 | |
|---|
| 1012 | i = 1; |
|---|
| 1013 | while (i <= rdta->sites) { |
|---|
| 1014 | ch = getc(INFILE); |
|---|
| 1015 | ci = base36(ch); |
|---|
| 1016 | if (ci >= 0 && ci <= rdta->categs) |
|---|
| 1017 | rdta->sitecat[i++] = ci; |
|---|
| 1018 | else if (! whitechar(ch)) { |
|---|
| 1019 | printf("ERROR: Bad category character (%c) at site %d\n", ch, i); |
|---|
| 1020 | return FALSE; |
|---|
| 1021 | } |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | if (findch('\n') == EOF) { /* skip to end of line */ |
|---|
| 1025 | printf("ERROR: Missing newline at end of category data\n"); |
|---|
| 1026 | return FALSE; |
|---|
| 1027 | } |
|---|
| 1028 | |
|---|
| 1029 | return TRUE; |
|---|
| 1030 | } /* inputcategories */ |
|---|
| 1031 | |
|---|
| 1032 | |
|---|
| 1033 | boolean inputextra (analdef *adef) |
|---|
| 1034 | { /* inputextra */ |
|---|
| 1035 | if (fscanf(INFILE,"%d", & adef->extra) != 1 || |
|---|
| 1036 | findch('\n') == EOF) { |
|---|
| 1037 | printf("ERROR: Problem reading extra info value\n"); |
|---|
| 1038 | return FALSE; |
|---|
| 1039 | } |
|---|
| 1040 | |
|---|
| 1041 | return TRUE; |
|---|
| 1042 | } /* inputextra */ |
|---|
| 1043 | |
|---|
| 1044 | |
|---|
| 1045 | boolean inputfreqs (rawdata *rdta) |
|---|
| 1046 | { /* inputfreqs */ |
|---|
| 1047 | if (fscanf(INFILE, "%lf%lf%lf%lf", |
|---|
| 1048 | & rdta->freqa, & rdta->freqc, |
|---|
| 1049 | & rdta->freqg, & rdta->freqt) != 4 || |
|---|
| 1050 | findch('\n') == EOF) { |
|---|
| 1051 | printf("ERROR: Problem reading user base frequencies data\n"); |
|---|
| 1052 | return FALSE; |
|---|
| 1053 | } |
|---|
| 1054 | |
|---|
| 1055 | rdta->freqread = TRUE; |
|---|
| 1056 | return TRUE; |
|---|
| 1057 | } /* inputfreqs */ |
|---|
| 1058 | |
|---|
| 1059 | |
|---|
| 1060 | boolean inputglobal (tree *tr) |
|---|
| 1061 | /* input the global option information */ |
|---|
| 1062 | { /* inputglobal */ |
|---|
| 1063 | int ch; |
|---|
| 1064 | |
|---|
| 1065 | if (tr->global != -2) { |
|---|
| 1066 | printf("ERROR: Unexpected Global auxiliary data line\n"); |
|---|
| 1067 | return FALSE; |
|---|
| 1068 | } |
|---|
| 1069 | if (fscanf(INFILE, "%d", &(tr->global)) != 1) { |
|---|
| 1070 | printf("ERROR: Problem reading rearrangement region size\n"); |
|---|
| 1071 | return FALSE; |
|---|
| 1072 | } |
|---|
| 1073 | if (tr->global < 0) { |
|---|
| 1074 | printf("WARNING: Global region size too small;\n"); |
|---|
| 1075 | printf(" value reset to local\n\n"); |
|---|
| 1076 | tr->global = 1; |
|---|
| 1077 | } |
|---|
| 1078 | else if (tr->global == 0) tr->partswap = 0; |
|---|
| 1079 | else if (tr->global > tr->mxtips - 3) { |
|---|
| 1080 | tr->global = tr->mxtips - 3; |
|---|
| 1081 | } |
|---|
| 1082 | |
|---|
| 1083 | while ((ch = getc(INFILE)) != '\n') { /* Scan for second value */ |
|---|
| 1084 | if (! whitechar(ch)) { |
|---|
| 1085 | if (ch != EOF) (void) ungetc(ch, INFILE); |
|---|
| 1086 | if (ch == EOF || fscanf(INFILE, "%d", &(tr->partswap)) != 1 |
|---|
| 1087 | || findch('\n') == EOF) { |
|---|
| 1088 | printf("ERROR: Problem reading insert swap region size\n"); |
|---|
| 1089 | return FALSE; |
|---|
| 1090 | } |
|---|
| 1091 | else if (tr->partswap < 0) tr->partswap = 1; |
|---|
| 1092 | else if (tr->partswap > tr->mxtips - 3) { |
|---|
| 1093 | tr->partswap = tr->mxtips - 3; |
|---|
| 1094 | } |
|---|
| 1095 | |
|---|
| 1096 | if (tr->partswap > tr->global) tr->global = tr->partswap; |
|---|
| 1097 | break; /* Break while loop */ |
|---|
| 1098 | } |
|---|
| 1099 | } |
|---|
| 1100 | |
|---|
| 1101 | return TRUE; |
|---|
| 1102 | } /* inputglobal */ |
|---|
| 1103 | |
|---|
| 1104 | |
|---|
| 1105 | boolean inputjumble (analdef *adef) |
|---|
| 1106 | { /* inputjumble */ |
|---|
| 1107 | if (! adef->jumble) { |
|---|
| 1108 | printf("ERROR: Unexpected Jumble auxiliary data line\n"); |
|---|
| 1109 | return FALSE; |
|---|
| 1110 | } |
|---|
| 1111 | else if (fscanf(INFILE, "%ld", & adef->jumble) != 1 || |
|---|
| 1112 | findch('\n') == EOF) { |
|---|
| 1113 | printf("ERROR: Problem reading jumble random seed value\n"); |
|---|
| 1114 | return FALSE; |
|---|
| 1115 | } |
|---|
| 1116 | else if (adef->jumble == 0) { |
|---|
| 1117 | printf("WARNING: Jumble random number seed is zero\n\n"); |
|---|
| 1118 | } |
|---|
| 1119 | |
|---|
| 1120 | return TRUE; |
|---|
| 1121 | } /* inputjumble */ |
|---|
| 1122 | |
|---|
| 1123 | |
|---|
| 1124 | boolean inputkeep (analdef *adef) |
|---|
| 1125 | { /* inputkeep */ |
|---|
| 1126 | if (fscanf(INFILE, "%d", & adef->nkeep) != 1 || |
|---|
| 1127 | findch('\n') == EOF || adef->nkeep < 1) { |
|---|
| 1128 | printf("ERROR: Problem reading number of kept trees\n"); |
|---|
| 1129 | return FALSE; |
|---|
| 1130 | } |
|---|
| 1131 | |
|---|
| 1132 | return TRUE; |
|---|
| 1133 | } /* inputkeep */ |
|---|
| 1134 | |
|---|
| 1135 | |
|---|
| 1136 | boolean inputoutgroup (analdef *adef, tree *tr) |
|---|
| 1137 | { /* inputoutgroup */ |
|---|
| 1138 | if (! adef->root || tr->outgr > 0) { |
|---|
| 1139 | printf("ERROR: Unexpected Outgroup auxiliary data line\n"); |
|---|
| 1140 | return FALSE; |
|---|
| 1141 | } |
|---|
| 1142 | else if (fscanf(INFILE, "%d", &(tr->outgr)) != 1 || |
|---|
| 1143 | findch('\n') == EOF) { |
|---|
| 1144 | printf("ERROR: Problem reading outgroup number\n"); |
|---|
| 1145 | return FALSE; |
|---|
| 1146 | } |
|---|
| 1147 | else if ((tr->outgr < 1) || (tr->outgr > tr->mxtips)) { |
|---|
| 1148 | printf("ERROR: Bad outgroup: '%d'\n", tr->outgr); |
|---|
| 1149 | return FALSE; |
|---|
| 1150 | } |
|---|
| 1151 | |
|---|
| 1152 | return TRUE; |
|---|
| 1153 | } /* inputoutgroup */ |
|---|
| 1154 | |
|---|
| 1155 | |
|---|
| 1156 | boolean inputratio (rawdata *rdta) |
|---|
| 1157 | { /* inputratio */ |
|---|
| 1158 | if (rdta->ttratio >= 0.0) { |
|---|
| 1159 | printf("ERROR: Unexpected Transition/transversion auxiliary data\n"); |
|---|
| 1160 | return FALSE; |
|---|
| 1161 | } |
|---|
| 1162 | else if (fscanf(INFILE,"%lf", & rdta->ttratio)!=1 || |
|---|
| 1163 | findch('\n') == EOF) { |
|---|
| 1164 | printf("ERROR: Problem reading transition/transversion ratio\n"); |
|---|
| 1165 | return FALSE; |
|---|
| 1166 | } |
|---|
| 1167 | |
|---|
| 1168 | return TRUE; |
|---|
| 1169 | } /* inputratio */ |
|---|
| 1170 | |
|---|
| 1171 | |
|---|
| 1172 | /* Y 0 is treeNone (no tree) |
|---|
| 1173 | Y 1 is treeNewick |
|---|
| 1174 | Y 2 is treeProlog |
|---|
| 1175 | Y 3 is treePHYLIP |
|---|
| 1176 | */ |
|---|
| 1177 | |
|---|
| 1178 | boolean inputtreeopt (analdef *adef) |
|---|
| 1179 | { /* inputtreeopt */ |
|---|
| 1180 | if (! adef->trout) { |
|---|
| 1181 | printf("ERROR: Unexpected Treefile auxiliary data\n"); |
|---|
| 1182 | return FALSE; |
|---|
| 1183 | } |
|---|
| 1184 | else if (fscanf(INFILE,"%d", & adef->trout) != 1 || |
|---|
| 1185 | findch('\n') == EOF) { |
|---|
| 1186 | printf("ERROR: Problem reading output tree-type number\n"); |
|---|
| 1187 | return FALSE; |
|---|
| 1188 | } |
|---|
| 1189 | else if ((adef->trout < 0) || (adef->trout > treeMaxType)) { |
|---|
| 1190 | printf("ERROR: Bad output tree-type number: '%d'\n", adef->trout); |
|---|
| 1191 | return FALSE; |
|---|
| 1192 | } |
|---|
| 1193 | |
|---|
| 1194 | return TRUE; |
|---|
| 1195 | } /* inputtreeopt */ |
|---|
| 1196 | |
|---|
| 1197 | |
|---|
| 1198 | boolean inputweights (analdef *adef, rawdata *rdta, cruncheddata *cdta) |
|---|
| 1199 | /* input the character weights 0, 1, 2 ... 9, A, B, ... Y, Z */ |
|---|
| 1200 | { /* inputweights */ |
|---|
| 1201 | int i, ch, wi; |
|---|
| 1202 | |
|---|
| 1203 | if (! adef->userwgt || cdta->wgtsum > 0) { |
|---|
| 1204 | printf("ERROR: Unexpected Weights auxiliary data\n"); |
|---|
| 1205 | return FALSE; |
|---|
| 1206 | } |
|---|
| 1207 | |
|---|
| 1208 | for (i = 2; i <= nmlngth; i++) (void) getc(INFILE); |
|---|
| 1209 | cdta->wgtsum = 0; |
|---|
| 1210 | i = 1; |
|---|
| 1211 | while (i <= rdta->sites) { |
|---|
| 1212 | ch = getc(INFILE); |
|---|
| 1213 | wi = base36(ch); |
|---|
| 1214 | if (wi >= 0) |
|---|
| 1215 | cdta->wgtsum += rdta->wgt[i++] = wi; |
|---|
| 1216 | else if (! whitechar(ch)) { |
|---|
| 1217 | printf("ERROR: Bad weight character: '%c'", ch); |
|---|
| 1218 | printf(" Weights in dnaml must be a digit or a letter.\n"); |
|---|
| 1219 | return FALSE; |
|---|
| 1220 | } |
|---|
| 1221 | } |
|---|
| 1222 | |
|---|
| 1223 | if (findch('\n') == EOF) { /* skip to end of line */ |
|---|
| 1224 | printf("ERROR: Missing newline at end of weight data\n"); |
|---|
| 1225 | return FALSE; |
|---|
| 1226 | } |
|---|
| 1227 | |
|---|
| 1228 | return TRUE; |
|---|
| 1229 | } /* inputweights */ |
|---|
| 1230 | |
|---|
| 1231 | boolean inputdepth (tree *tr) |
|---|
| 1232 | { |
|---|
| 1233 | if (fscanf(INFILE, "%d", & tr->eqDepth) != 1 || |
|---|
| 1234 | findch('\n') == EOF || tr->eqDepth < 0) { |
|---|
| 1235 | printf("Depth (H - option) has to be greater than 0 \n "); |
|---|
| 1236 | |
|---|
| 1237 | return FALSE; |
|---|
| 1238 | } |
|---|
| 1239 | fprintf(stderr,"set to: %d\n", tr->eqDepth); |
|---|
| 1240 | return TRUE; |
|---|
| 1241 | } |
|---|
| 1242 | |
|---|
| 1243 | |
|---|
| 1244 | boolean getoptions (analdef *adef, rawdata *rdta, cruncheddata *cdta, tree *tr) |
|---|
| 1245 | { /* getoptions */ |
|---|
| 1246 | int ch, i, extranum; |
|---|
| 1247 | |
|---|
| 1248 | adef->boot = 0; /* Don't bootstrap column weights */ |
|---|
| 1249 | adef->empf = TRUE; /* Use empirical base frequencies */ |
|---|
| 1250 | adef->extra = 0; /* No extra runtime info unless requested */ |
|---|
| 1251 | adef->interleaved = TRUE; /* By default, data format is interleaved */ |
|---|
| 1252 | adef->jumble = FALSE; /* Use random addition sequence */ |
|---|
| 1253 | adef->nkeep = 0; /* Keep only the one best tree */ |
|---|
| 1254 | adef->prdata = FALSE; /* Don't echo data to output stream */ |
|---|
| 1255 | adef->qadd = TRUE; /* Smooth branches globally in add */ |
|---|
| 1256 | adef->restart = FALSE; /* Restart from user tree */ |
|---|
| 1257 | adef->root = FALSE; /* User-defined outgroup rooting */ |
|---|
| 1258 | adef->trout = treeDefType; /* Output tree file */ |
|---|
| 1259 | adef->trprint = TRUE; /* Print tree to output stream */ |
|---|
| 1260 | rdta->categs = 0; /* No rate categories */ |
|---|
| 1261 | rdta->catrat[1] = 1.0; /* Rate values */ |
|---|
| 1262 | rdta->freqread = FALSE; /* User-defined frequencies not read yet */ |
|---|
| 1263 | rdta->ttratio = 2.0; /* Transition/transversion rate ratio */ |
|---|
| 1264 | tr->global = -1; /* Default search locale for optimum */ |
|---|
| 1265 | tr->mxtips = rdta->numsp; |
|---|
| 1266 | tr->outgr = 1; /* Outgroup number */ |
|---|
| 1267 | tr->partswap = 1; /* Default to swap locally after insert */ |
|---|
| 1268 | tr->userlen = FALSE; /* User-supplied branch lengths */ |
|---|
| 1269 | tr->eqDepth = 0; |
|---|
| 1270 | adef->usertree = FALSE; /* User-defined tree topologies */ |
|---|
| 1271 | adef->userwgt = FALSE; /* User-defined position weights */ |
|---|
| 1272 | extranum = 0; |
|---|
| 1273 | |
|---|
| 1274 | while ((ch = getc(INFILE)) != '\n' && ch != EOF) { |
|---|
| 1275 | uppercase(& ch); |
|---|
| 1276 | switch (ch) { |
|---|
| 1277 | case '1' : adef->prdata = ! adef->prdata; break; |
|---|
| 1278 | case '3' : adef->trprint = ! adef->trprint; break; |
|---|
| 1279 | case '4' : adef->trout = treeDefType - adef->trout; break; |
|---|
| 1280 | case 'B' : adef->boot = 1; extranum++; break; |
|---|
| 1281 | case 'C' : rdta->categs = -1; extranum++; break; |
|---|
| 1282 | case 'E' : adef->extra = -1; break; |
|---|
| 1283 | case 'F' : adef->empf = ! adef->empf; break; |
|---|
| 1284 | case 'G' : tr->global = -2; break; |
|---|
| 1285 | //stm |
|---|
| 1286 | case 'H' : extranum++;break; |
|---|
| 1287 | //stm |
|---|
| 1288 | |
|---|
| 1289 | |
|---|
| 1290 | case 'I' : adef->interleaved = ! adef->interleaved; break; |
|---|
| 1291 | case 'J' : adef->jumble = 1; extranum++; break; |
|---|
| 1292 | case 'K' : extranum++; break; |
|---|
| 1293 | case 'L' : tr->userlen = TRUE; break; |
|---|
| 1294 | case 'O' : adef->root = TRUE; tr->outgr = 0; extranum++; break; |
|---|
| 1295 | case 'Q' : adef->qadd = FALSE; break; |
|---|
| 1296 | case 'R' : adef->restart = TRUE; break; |
|---|
| 1297 | case 'T' : rdta->ttratio = -1.0; extranum++; break; |
|---|
| 1298 | case 'U' : adef->usertree = TRUE; break; |
|---|
| 1299 | case 'W' : adef->userwgt = TRUE; cdta->wgtsum = 0; extranum++; break; |
|---|
| 1300 | case 'Y' : adef->trout = treeDefType - adef->trout; break; |
|---|
| 1301 | case ' ' : break; |
|---|
| 1302 | case '\t': break; |
|---|
| 1303 | default : |
|---|
| 1304 | printf("ERROR: Bad option character: '%c'\n", ch); |
|---|
| 1305 | return FALSE; |
|---|
| 1306 | } |
|---|
| 1307 | } |
|---|
| 1308 | |
|---|
| 1309 | if (ch == EOF) { |
|---|
| 1310 | printf("ERROR: End-of-file in options list\n"); |
|---|
| 1311 | return FALSE; |
|---|
| 1312 | } |
|---|
| 1313 | |
|---|
| 1314 | if (adef->usertree && adef->restart) { |
|---|
| 1315 | printf("ERROR: The restart and user-tree options conflict:\n"); |
|---|
| 1316 | printf(" Restart adds rest of taxa to a starting tree;\n"); |
|---|
| 1317 | printf(" User-tree does not add any taxa.\n\n"); |
|---|
| 1318 | return FALSE; |
|---|
| 1319 | } |
|---|
| 1320 | |
|---|
| 1321 | if (adef->usertree && adef->jumble) { |
|---|
| 1322 | printf("WARNING: The jumble and user-tree options conflict:\n"); |
|---|
| 1323 | printf(" Jumble adds taxa to a tree in random order;\n"); |
|---|
| 1324 | printf(" User-tree does not use taxa addition.\n"); |
|---|
| 1325 | printf(" Jumble option cancelled for this run.\n\n"); |
|---|
| 1326 | adef->jumble = FALSE; |
|---|
| 1327 | } |
|---|
| 1328 | |
|---|
| 1329 | if (tr->userlen && tr->global != -1) { |
|---|
| 1330 | printf("ERROR: The global and user-lengths options conflict:\n"); |
|---|
| 1331 | printf(" Global optimizes a starting tree;\n"); |
|---|
| 1332 | printf(" User-lengths constrain the starting tree.\n\n"); |
|---|
| 1333 | return FALSE; |
|---|
| 1334 | } |
|---|
| 1335 | |
|---|
| 1336 | if (tr->userlen && ! adef->usertree) { |
|---|
| 1337 | printf("WARNING: User lengths required user tree option.\n"); |
|---|
| 1338 | printf(" User-tree option set for this run.\n\n"); |
|---|
| 1339 | adef->usertree = TRUE; |
|---|
| 1340 | } |
|---|
| 1341 | |
|---|
| 1342 | rdta->wgt = (int *) Malloc((rdta->sites + 1) * sizeof(int)); |
|---|
| 1343 | rdta->wgt2 = (int *) Malloc((rdta->sites + 1) * sizeof(int)); |
|---|
| 1344 | rdta->sitecat = (int *) Malloc((rdta->sites + 1) * sizeof(int)); |
|---|
| 1345 | cdta->alias = (int *) Malloc((rdta->sites + 1) * sizeof(int)); |
|---|
| 1346 | cdta->aliaswgt = (int *) Malloc((rdta->sites + 1) * sizeof(int)); |
|---|
| 1347 | cdta->patcat = (int *) Malloc((rdta->sites + 1) * sizeof(int)); |
|---|
| 1348 | cdta->patrat = (double *) Malloc((rdta->sites + 1) * sizeof(double)); |
|---|
| 1349 | cdta->wr = (double *) Malloc((rdta->sites + 1) * sizeof(double)); |
|---|
| 1350 | cdta->wr2 = (double *) Malloc((rdta->sites + 1) * sizeof(double)); |
|---|
| 1351 | if ( ! rdta->wgt || ! rdta->wgt2 || ! rdta->sitecat || ! cdta->alias |
|---|
| 1352 | || ! cdta->aliaswgt || ! cdta->patcat || ! cdta->patrat |
|---|
| 1353 | || ! cdta->wr || ! cdta->wr2) { |
|---|
| 1354 | fprintf(stderr, "getoptions: Malloc failure\n"); |
|---|
| 1355 | return 0; |
|---|
| 1356 | } |
|---|
| 1357 | |
|---|
| 1358 | /* process lines with auxiliary data */ |
|---|
| 1359 | |
|---|
| 1360 | while (extranum--) { |
|---|
| 1361 | ch = getc(INFILE); |
|---|
| 1362 | uppercase(& ch); |
|---|
| 1363 | switch (ch) { |
|---|
| 1364 | case 'B': if (! inputboot(adef)) return FALSE; break; |
|---|
| 1365 | case 'C': if (! inputcategories(rdta)) return FALSE; break; |
|---|
| 1366 | case 'E': if (! inputextra(adef)) return FALSE; extranum++; break; |
|---|
| 1367 | case 'F': if (! inputfreqs(rdta)) return FALSE; break; |
|---|
| 1368 | case 'G': if (! inputglobal(tr)) return FALSE; extranum++; break; |
|---|
| 1369 | //stm |
|---|
| 1370 | case 'H': if(!inputdepth(tr)) return FALSE; break; |
|---|
| 1371 | //stm |
|---|
| 1372 | case 'J': if (! inputjumble(adef)) return FALSE; break; |
|---|
| 1373 | case 'K': if (! inputkeep(adef)) return FALSE; break; |
|---|
| 1374 | case 'O': if (! inputoutgroup(adef, tr)) return FALSE; break; |
|---|
| 1375 | case 'T': if (! inputratio(rdta)) return FALSE; break; |
|---|
| 1376 | case 'W': if (! inputweights(adef, rdta, cdta)) return FALSE; break; |
|---|
| 1377 | case 'Y': if (! inputtreeopt(adef)) return FALSE; extranum++; break; |
|---|
| 1378 | |
|---|
| 1379 | default: |
|---|
| 1380 | printf("ERROR: Auxiliary options line starts with '%c'\n", ch); |
|---|
| 1381 | return FALSE; |
|---|
| 1382 | } |
|---|
| 1383 | } |
|---|
| 1384 | |
|---|
| 1385 | if (! adef->userwgt) { |
|---|
| 1386 | for (i = 1; i <= rdta->sites; i++) rdta->wgt[i] = 1; |
|---|
| 1387 | cdta->wgtsum = rdta->sites; |
|---|
| 1388 | } |
|---|
| 1389 | |
|---|
| 1390 | if (adef->userwgt && cdta->wgtsum < 1) { |
|---|
| 1391 | printf("ERROR: Missing or bad user-supplied weight data.\n"); |
|---|
| 1392 | return FALSE; |
|---|
| 1393 | } |
|---|
| 1394 | |
|---|
| 1395 | if (adef->boot) { |
|---|
| 1396 | printf("Bootstrap random number seed = %ld\n\n", adef->boot); |
|---|
| 1397 | } |
|---|
| 1398 | |
|---|
| 1399 | if (adef->jumble) { |
|---|
| 1400 | printf("Jumble random number seed = %ld\n\n", adef->jumble); |
|---|
| 1401 | } |
|---|
| 1402 | |
|---|
| 1403 | if (adef->qadd) { |
|---|
| 1404 | printf("Quick add (only local branches initially optimized) in effect\n\n"); |
|---|
| 1405 | } |
|---|
| 1406 | |
|---|
| 1407 | if (rdta->categs > 0) { |
|---|
| 1408 | printf("Site category Rate of change\n\n"); |
|---|
| 1409 | for (i = 1; i <= rdta->categs; i++) |
|---|
| 1410 | printf(" %c%13.3f\n", itobase36(i), rdta->catrat[i]); |
|---|
| 1411 | putchar('\n'); |
|---|
| 1412 | for (i = 1; i <= rdta->sites; i++) { |
|---|
| 1413 | if ((rdta->wgt[i] > 0) && (rdta->sitecat[i] < 1)) { |
|---|
| 1414 | printf("ERROR: Bad category (%c) at site %d\n", |
|---|
| 1415 | itobase36(rdta->sitecat[i]), i); |
|---|
| 1416 | return FALSE; |
|---|
| 1417 | } |
|---|
| 1418 | } |
|---|
| 1419 | } |
|---|
| 1420 | else if (rdta->categs < 0) { |
|---|
| 1421 | printf("ERROR: Category auxiliary data missing from input\n"); |
|---|
| 1422 | return FALSE; |
|---|
| 1423 | } |
|---|
| 1424 | else { /* rdta->categs == 0 */ |
|---|
| 1425 | for (i = 1; i <= rdta->sites; i++) rdta->sitecat[i] = 1; |
|---|
| 1426 | rdta->categs = 1; |
|---|
| 1427 | } |
|---|
| 1428 | |
|---|
| 1429 | if (tr->outgr < 1) { |
|---|
| 1430 | printf("ERROR: Outgroup auxiliary data missing from input\n"); |
|---|
| 1431 | return FALSE; |
|---|
| 1432 | } |
|---|
| 1433 | |
|---|
| 1434 | if (rdta->ttratio < 0.0) { |
|---|
| 1435 | printf("ERROR: Transition/transversion auxiliary data missing from input\n"); |
|---|
| 1436 | return FALSE; |
|---|
| 1437 | } |
|---|
| 1438 | |
|---|
| 1439 | if (tr->global < 0) { |
|---|
| 1440 | if (tr->global == -2) tr->global = tr->mxtips - 3; /* Default global */ |
|---|
| 1441 | else tr->global = adef->usertree ? 0 : 1;/* No global */ |
|---|
| 1442 | } |
|---|
| 1443 | |
|---|
| 1444 | if (adef->restart) { |
|---|
| 1445 | printf("Restart option in effect. "); |
|---|
| 1446 | printf("Sequence addition will start from appended tree.\n\n"); |
|---|
| 1447 | } |
|---|
| 1448 | |
|---|
| 1449 | if (adef->usertree && ! tr->global) { |
|---|
| 1450 | printf("User-supplied tree topology%swill be used.\n\n", |
|---|
| 1451 | tr->userlen ? " and branch lengths " : " "); |
|---|
| 1452 | } |
|---|
| 1453 | else { |
|---|
| 1454 | if (! adef->usertree) { |
|---|
| 1455 | printf("Rearrangements of partial trees may cross %d %s.\n", |
|---|
| 1456 | tr->partswap, tr->partswap == 1 ? "branch" : "branches"); |
|---|
| 1457 | } |
|---|
| 1458 | printf("Rearrangements of full tree may cross %d %s.\n\n", |
|---|
| 1459 | tr->global, tr->global == 1 ? "branch" : "branches"); |
|---|
| 1460 | } |
|---|
| 1461 | |
|---|
| 1462 | if (! adef->usertree && adef->nkeep == 0) adef->nkeep = 1; |
|---|
| 1463 | |
|---|
| 1464 | return TRUE; |
|---|
| 1465 | } /* getoptions */ |
|---|
| 1466 | |
|---|
| 1467 | |
|---|
| 1468 | boolean getbasefreqs (rawdata *rdta) |
|---|
| 1469 | { /* getbasefreqs */ |
|---|
| 1470 | int ch; |
|---|
| 1471 | |
|---|
| 1472 | if (rdta->freqread) return TRUE; |
|---|
| 1473 | |
|---|
| 1474 | ch = getc(INFILE); |
|---|
| 1475 | if (! ((ch == 'F') || (ch == 'f'))) (void) ungetc(ch, INFILE); |
|---|
| 1476 | |
|---|
| 1477 | if (fscanf(INFILE, "%lf%lf%lf%lf", |
|---|
| 1478 | & rdta->freqa, & rdta->freqc, |
|---|
| 1479 | & rdta->freqg, & rdta->freqt) != 4 || |
|---|
| 1480 | findch('\n') == EOF) { |
|---|
| 1481 | printf("ERROR: Problem reading user base frequencies\n"); |
|---|
| 1482 | return FALSE; |
|---|
| 1483 | } |
|---|
| 1484 | |
|---|
| 1485 | return TRUE; |
|---|
| 1486 | } /* getbasefreqs */ |
|---|
| 1487 | |
|---|
| 1488 | |
|---|
| 1489 | boolean getyspace (rawdata *rdta) |
|---|
| 1490 | { /* getyspace */ |
|---|
| 1491 | long size; |
|---|
| 1492 | int i; |
|---|
| 1493 | yType *y0; |
|---|
| 1494 | |
|---|
| 1495 | if (! (rdta->y = (yType **) Malloc((rdta->numsp + 1) * sizeof(yType *)))) { |
|---|
| 1496 | printf("ERROR: Unable to obtain space for data array pointers\n"); |
|---|
| 1497 | return FALSE; |
|---|
| 1498 | } |
|---|
| 1499 | |
|---|
| 1500 | size = 4 * (rdta->sites / 4 + 1); |
|---|
| 1501 | if (! (y0 = (yType *) Malloc((rdta->numsp + 1) * size * sizeof(yType)))) { |
|---|
| 1502 | printf("ERROR: Unable to obtain space for data array\n"); |
|---|
| 1503 | return FALSE; |
|---|
| 1504 | } |
|---|
| 1505 | |
|---|
| 1506 | for (i = 0; i <= rdta->numsp; i++) { |
|---|
| 1507 | rdta->y[i] = y0; |
|---|
| 1508 | y0 += size; |
|---|
| 1509 | } |
|---|
| 1510 | |
|---|
| 1511 | return TRUE; |
|---|
| 1512 | } /* getyspace */ |
|---|
| 1513 | |
|---|
| 1514 | |
|---|
| 1515 | |
|---|
| 1516 | boolean setupTree (tree *tr, int nsites) |
|---|
| 1517 | { /* setupTree */ |
|---|
| 1518 | nodeptr p0, p, q; |
|---|
| 1519 | int i, j, tips, inter; |
|---|
| 1520 | |
|---|
| 1521 | tips = tr->mxtips; |
|---|
| 1522 | inter = tr->mxtips - 1; |
|---|
| 1523 | |
|---|
| 1524 | if (!(p0 = (nodeptr) Malloc((tips + 3*inter) * sizeof(node)))) { |
|---|
| 1525 | printf("ERROR: Unable to obtain sufficient tree memory\n"); |
|---|
| 1526 | return FALSE; |
|---|
| 1527 | } |
|---|
| 1528 | |
|---|
| 1529 | |
|---|
| 1530 | if (!(tr->nodep = (nodeptr *) Malloc((2*tr->mxtips) * sizeof(nodeptr)))) { |
|---|
| 1531 | printf("ERROR: Unable to obtain sufficient tree memory, too\n"); |
|---|
| 1532 | return FALSE; |
|---|
| 1533 | } |
|---|
| 1534 | |
|---|
| 1535 | |
|---|
| 1536 | tr->nodep[0] = (node *) NULL; /* Use as 1-based array */ |
|---|
| 1537 | |
|---|
| 1538 | for (i = 1; i <= tips; i++) { /* Set-up tips */ |
|---|
| 1539 | p = p0++; |
|---|
| 1540 | p->x = (xarray *) NULL; |
|---|
| 1541 | p->tip = (yType *) NULL; |
|---|
| 1542 | |
|---|
| 1543 | /* AxML modification start*/ |
|---|
| 1544 | |
|---|
| 1545 | p->equalityVector = (char *) NULL; /*initialize eq vector with zero */ |
|---|
| 1546 | |
|---|
| 1547 | /* AxML modification end*/ |
|---|
| 1548 | |
|---|
| 1549 | |
|---|
| 1550 | p->number = i; |
|---|
| 1551 | p->next = p; |
|---|
| 1552 | p->back = (node *) NULL; |
|---|
| 1553 | |
|---|
| 1554 | tr->nodep[i] = p; |
|---|
| 1555 | } |
|---|
| 1556 | |
|---|
| 1557 | for (i = tips + 1; i <= tips + inter; i++) { /* Internal nodes */ |
|---|
| 1558 | q = (node *) NULL; |
|---|
| 1559 | for (j = 1; j <= 3; j++) { |
|---|
| 1560 | p = p0++; |
|---|
| 1561 | p->x = (xarray *) NULL; |
|---|
| 1562 | p->tip = (yType *) NULL; |
|---|
| 1563 | p->number = i; |
|---|
| 1564 | |
|---|
| 1565 | /* AxML modification start*/ |
|---|
| 1566 | |
|---|
| 1567 | p->equalityVector = (char *) NULL; /*initialize eq vector with zero */ |
|---|
| 1568 | /* AxML modification end*/ |
|---|
| 1569 | |
|---|
| 1570 | |
|---|
| 1571 | p->next = q; |
|---|
| 1572 | p->back = (node *) NULL; |
|---|
| 1573 | q = p; |
|---|
| 1574 | } |
|---|
| 1575 | p->next->next->next = p; |
|---|
| 1576 | tr->nodep[i] = p; |
|---|
| 1577 | } |
|---|
| 1578 | |
|---|
| 1579 | tr->likelihood = unlikely; |
|---|
| 1580 | tr->start = (node *) NULL; |
|---|
| 1581 | tr->outgrnode = tr->nodep[tr->outgr]; |
|---|
| 1582 | tr->ntips = 0; |
|---|
| 1583 | tr->nextnode = 0; |
|---|
| 1584 | tr->opt_level = 0; |
|---|
| 1585 | tr->prelabeled = TRUE; |
|---|
| 1586 | tr->smoothed = FALSE; |
|---|
| 1587 | tr->log_f_valid = 0; |
|---|
| 1588 | |
|---|
| 1589 | tr->log_f = (double *) Malloc(nsites * sizeof(double)); |
|---|
| 1590 | if (! tr->log_f) { |
|---|
| 1591 | printf("ERROR: Unable to obtain sufficient tree memory, trey\n"); |
|---|
| 1592 | return FALSE; |
|---|
| 1593 | } |
|---|
| 1594 | |
|---|
| 1595 | return TRUE; |
|---|
| 1596 | } /* setupTree */ |
|---|
| 1597 | |
|---|
| 1598 | void freeTreeNode (nodeptr p) /* Free tree node (sector) associated data */ |
|---|
| 1599 | { /* freeTreeNode */ |
|---|
| 1600 | if (p) { |
|---|
| 1601 | if (p->x) { |
|---|
| 1602 | if (p->x->lv) Free(p->x->lv); |
|---|
| 1603 | Free(p->x); |
|---|
| 1604 | } |
|---|
| 1605 | /* AxML modification start*/ |
|---|
| 1606 | |
|---|
| 1607 | Free(p->equalityVector); /*free equality vector of node*/ |
|---|
| 1608 | |
|---|
| 1609 | /* AxML modification end*/ |
|---|
| 1610 | } |
|---|
| 1611 | } /* freeTree */ |
|---|
| 1612 | |
|---|
| 1613 | |
|---|
| 1614 | |
|---|
| 1615 | /* AxML modification start*/ |
|---|
| 1616 | |
|---|
| 1617 | |
|---|
| 1618 | boolean setupEqualityVector(tree *tr) /* new function for initializing and mapping equality vectors */ |
|---|
| 1619 | { |
|---|
| 1620 | int siteNum = tr->cdta->endsite; |
|---|
| 1621 | nodeptr upper = tr->nodep[tr->mxtips * 2 - 1]; |
|---|
| 1622 | nodeptr p = tr->nodep[1]; |
|---|
| 1623 | char *ev, *end; |
|---|
| 1624 | char *tip; |
|---|
| 1625 | |
|---|
| 1626 | int j; |
|---|
| 1627 | homType *mlvP; |
|---|
| 1628 | int ref[EQUALITIES]; |
|---|
| 1629 | int code; |
|---|
| 1630 | |
|---|
| 1631 | for(; p < upper; p++) |
|---|
| 1632 | { |
|---|
| 1633 | if( ! (p->equalityVector = |
|---|
| 1634 | (ev = (char *)malloc(siteNum * sizeof(char))))) |
|---|
| 1635 | { |
|---|
| 1636 | printf("ERROR: Failure to get node equality vector memory\n"); |
|---|
| 1637 | return FALSE; |
|---|
| 1638 | } |
|---|
| 1639 | |
|---|
| 1640 | end = & ev[siteNum]; |
|---|
| 1641 | |
|---|
| 1642 | if(p->tip) |
|---|
| 1643 | { |
|---|
| 1644 | int i; |
|---|
| 1645 | tip = p->tip; |
|---|
| 1646 | |
|---|
| 1647 | for(i = 0; i < equalities; i++) |
|---|
| 1648 | ref[i] = -1; |
|---|
| 1649 | |
|---|
| 1650 | for(i = 0; i < siteNum; i++) |
|---|
| 1651 | { |
|---|
| 1652 | ev[i] = map[tip[i]]; |
|---|
| 1653 | if(ref[ev[i]] == -1) |
|---|
| 1654 | ref[ev[i]] = 1; |
|---|
| 1655 | } |
|---|
| 1656 | for(i = 0; i < equalities; i++) |
|---|
| 1657 | { |
|---|
| 1658 | for(j = 1; j <= tr->rdta->categs; j++) |
|---|
| 1659 | { |
|---|
| 1660 | mlvP = &p->mlv[i][j]; |
|---|
| 1661 | code = map2[i]; |
|---|
| 1662 | // if(ref[i] == 1) |
|---|
| 1663 | //{ |
|---|
| 1664 | mlvP->a = code & 1; |
|---|
| 1665 | mlvP->c = (code >> 1) & 1; |
|---|
| 1666 | mlvP->g = (code >> 2) & 1; |
|---|
| 1667 | mlvP->t = (code >> 3) & 1; |
|---|
| 1668 | mlvP->exp = 0; |
|---|
| 1669 | mlvP->set = TRUE; |
|---|
| 1670 | //} |
|---|
| 1671 | //else |
|---|
| 1672 | //mlvP->set = FALSE; |
|---|
| 1673 | |
|---|
| 1674 | } |
|---|
| 1675 | } |
|---|
| 1676 | |
|---|
| 1677 | } |
|---|
| 1678 | else |
|---|
| 1679 | { |
|---|
| 1680 | for(; ev < end; ev++) |
|---|
| 1681 | { |
|---|
| 1682 | *ev = 0; |
|---|
| 1683 | } |
|---|
| 1684 | } |
|---|
| 1685 | } |
|---|
| 1686 | |
|---|
| 1687 | return TRUE; |
|---|
| 1688 | } |
|---|
| 1689 | |
|---|
| 1690 | /* AxML modification end*/ |
|---|
| 1691 | |
|---|
| 1692 | |
|---|
| 1693 | |
|---|
| 1694 | |
|---|
| 1695 | |
|---|
| 1696 | |
|---|
| 1697 | |
|---|
| 1698 | void freeTree (tree *tr) |
|---|
| 1699 | { /* freeTree */ |
|---|
| 1700 | nodeptr p, q; |
|---|
| 1701 | int i, tips, inter; |
|---|
| 1702 | |
|---|
| 1703 | tips = tr->mxtips; |
|---|
| 1704 | inter = tr->mxtips - 1; |
|---|
| 1705 | |
|---|
| 1706 | for (i = 1; i <= tips; i++) freeTreeNode(tr->nodep[i]); |
|---|
| 1707 | |
|---|
| 1708 | for (i = tips + 1; i <= tips + inter; i++) { |
|---|
| 1709 | if (p = tr->nodep[i]) { |
|---|
| 1710 | if (q = p->next) { |
|---|
| 1711 | freeTreeNode(q->next); |
|---|
| 1712 | freeTreeNode(q); |
|---|
| 1713 | } |
|---|
| 1714 | freeTreeNode(p); |
|---|
| 1715 | } |
|---|
| 1716 | } |
|---|
| 1717 | |
|---|
| 1718 | Free(tr->nodep[1]); /* Free the actual nodes */ |
|---|
| 1719 | } /* freeTree */ |
|---|
| 1720 | |
|---|
| 1721 | |
|---|
| 1722 | boolean getdata (analdef *adef, rawdata *rdta, tree *tr) |
|---|
| 1723 | /* read sequences */ |
|---|
| 1724 | { /* getdata */ |
|---|
| 1725 | int i, j, k, l, basesread, basesnew, ch; |
|---|
| 1726 | int meaning[256]; /* meaning of input characters */ |
|---|
| 1727 | char *nameptr; |
|---|
| 1728 | boolean allread, firstpass; |
|---|
| 1729 | |
|---|
| 1730 | for (i = 0; i <= 255; i++) meaning[i] = 0; |
|---|
| 1731 | meaning['A'] = 1; |
|---|
| 1732 | meaning['B'] = 14; |
|---|
| 1733 | meaning['C'] = 2; |
|---|
| 1734 | meaning['D'] = 13; |
|---|
| 1735 | meaning['G'] = 4; |
|---|
| 1736 | meaning['H'] = 11; |
|---|
| 1737 | meaning['K'] = 12; |
|---|
| 1738 | meaning['M'] = 3; |
|---|
| 1739 | meaning['N'] = 15; |
|---|
| 1740 | meaning['O'] = 15; |
|---|
| 1741 | meaning['R'] = 5; |
|---|
| 1742 | meaning['S'] = 6; |
|---|
| 1743 | meaning['T'] = 8; |
|---|
| 1744 | meaning['U'] = 8; |
|---|
| 1745 | meaning['V'] = 7; |
|---|
| 1746 | meaning['W'] = 9; |
|---|
| 1747 | meaning['X'] = 15; |
|---|
| 1748 | meaning['Y'] = 10; |
|---|
| 1749 | meaning['?'] = 15; |
|---|
| 1750 | meaning['-'] = 15; |
|---|
| 1751 | |
|---|
| 1752 | basesread = basesnew = 0; |
|---|
| 1753 | |
|---|
| 1754 | allread = FALSE; |
|---|
| 1755 | firstpass = TRUE; |
|---|
| 1756 | ch = ' '; |
|---|
| 1757 | |
|---|
| 1758 | while (! allread) { |
|---|
| 1759 | for (i = 1; i <= tr->mxtips; i++) { /* Read data line */ |
|---|
| 1760 | |
|---|
| 1761 | if (firstpass) { /* Read species names */ |
|---|
| 1762 | j = 1; |
|---|
| 1763 | while (whitechar(ch = getc(INFILE))) { /* Skip blank lines */ |
|---|
| 1764 | if (ch == '\n') j = 1; else j++; |
|---|
| 1765 | } |
|---|
| 1766 | |
|---|
| 1767 | if (j > nmlngth) { |
|---|
| 1768 | printf("ERROR: Blank name for species %d; ", i); |
|---|
| 1769 | printf("check number of species,\n"); |
|---|
| 1770 | printf(" number of sites, and interleave option.\n"); |
|---|
| 1771 | return FALSE; |
|---|
| 1772 | } |
|---|
| 1773 | |
|---|
| 1774 | nameptr = tr->nodep[i]->name; |
|---|
| 1775 | for (k = 1; k < j; k++) *nameptr++ = ' '; |
|---|
| 1776 | |
|---|
| 1777 | while (ch != '\n' && ch != EOF) { |
|---|
| 1778 | if (whitechar(ch)) ch = ' '; |
|---|
| 1779 | *nameptr++ = ch; |
|---|
| 1780 | if (++j > nmlngth) break; |
|---|
| 1781 | ch = getc(INFILE); |
|---|
| 1782 | } |
|---|
| 1783 | |
|---|
| 1784 | while (*(--nameptr) == ' ') ; /* remove trailing blanks */ |
|---|
| 1785 | *(++nameptr) = '\0'; /* add null termination */ |
|---|
| 1786 | |
|---|
| 1787 | if (ch == EOF) { |
|---|
| 1788 | printf("ERROR: End-of-file in name of species %d\n", i); |
|---|
| 1789 | return FALSE; |
|---|
| 1790 | } |
|---|
| 1791 | } /* if (firstpass) */ |
|---|
| 1792 | |
|---|
| 1793 | j = basesread; |
|---|
| 1794 | while ((j < rdta->sites) |
|---|
| 1795 | && ((ch = getc(INFILE)) != EOF) |
|---|
| 1796 | && ((! adef->interleaved) || (ch != '\n'))) { |
|---|
| 1797 | uppercase(& ch); |
|---|
| 1798 | if (meaning[ch] || ch == '.') { |
|---|
| 1799 | j++; |
|---|
| 1800 | if (ch == '.') { |
|---|
| 1801 | if (i != 1) ch = rdta->y[1][j]; |
|---|
| 1802 | else { |
|---|
| 1803 | printf("ERROR: Dot (.) found at site %d of sequence 1\n", j); |
|---|
| 1804 | return FALSE; |
|---|
| 1805 | } |
|---|
| 1806 | } |
|---|
| 1807 | rdta->y[i][j] = ch; |
|---|
| 1808 | } |
|---|
| 1809 | else if (whitechar(ch) || digitchar(ch)) ; |
|---|
| 1810 | else { |
|---|
| 1811 | printf("ERROR: Bad base (%c) at site %d of sequence %d\n", |
|---|
| 1812 | ch, j, i); |
|---|
| 1813 | return FALSE; |
|---|
| 1814 | } |
|---|
| 1815 | } |
|---|
| 1816 | |
|---|
| 1817 | if (ch == EOF) { |
|---|
| 1818 | printf("ERROR: End-of-file at site %d of sequence %d\n", j, i); |
|---|
| 1819 | return FALSE; |
|---|
| 1820 | } |
|---|
| 1821 | |
|---|
| 1822 | if (! firstpass && (j == basesread)) i--; /* no data on line */ |
|---|
| 1823 | else if (i == 1) basesnew = j; |
|---|
| 1824 | else if (j != basesnew) { |
|---|
| 1825 | printf("ERROR: Sequences out of alignment\n"); |
|---|
| 1826 | printf("%d (instead of %d) residues read in sequence %d\n", |
|---|
| 1827 | j - basesread, basesnew - basesread, i); |
|---|
| 1828 | return FALSE; |
|---|
| 1829 | } |
|---|
| 1830 | |
|---|
| 1831 | while (ch != '\n' && ch != EOF) ch = getc(INFILE); /* flush line */ |
|---|
| 1832 | } /* next sequence */ |
|---|
| 1833 | firstpass = FALSE; |
|---|
| 1834 | basesread = basesnew; |
|---|
| 1835 | allread = (basesread >= rdta->sites); |
|---|
| 1836 | } |
|---|
| 1837 | |
|---|
| 1838 | /* Print listing of sequence alignment */ |
|---|
| 1839 | |
|---|
| 1840 | if (adef->prdata) { |
|---|
| 1841 | j = nmlngth - 5 + ((rdta->sites + ((rdta->sites - 1)/10))/2); |
|---|
| 1842 | if (j < nmlngth - 1) j = nmlngth - 1; |
|---|
| 1843 | if (j > 37) j = 37; |
|---|
| 1844 | printf("Name"); for (i=1;i<=j;i++) putchar(' '); printf("Sequences\n"); |
|---|
| 1845 | printf("----"); for (i=1;i<=j;i++) putchar(' '); printf("---------\n"); |
|---|
| 1846 | putchar('\n'); |
|---|
| 1847 | |
|---|
| 1848 | for (i = 1; i <= rdta->sites; i += 60) { |
|---|
| 1849 | l = i + 59; |
|---|
| 1850 | if (l > rdta->sites) l = rdta->sites; |
|---|
| 1851 | |
|---|
| 1852 | if (adef->userwgt) { |
|---|
| 1853 | printf("Weights "); |
|---|
| 1854 | for (j = 11; j <= nmlngth+3; j++) putchar(' '); |
|---|
| 1855 | for (k = i; k <= l; k++) { |
|---|
| 1856 | putchar(itobase36(rdta->wgt[k])); |
|---|
| 1857 | if (((k % 10) == 0) && (k < l)) putchar(' '); |
|---|
| 1858 | } |
|---|
| 1859 | putchar('\n'); |
|---|
| 1860 | } |
|---|
| 1861 | |
|---|
| 1862 | if (rdta->categs > 1) { |
|---|
| 1863 | printf("Categories"); |
|---|
| 1864 | for (j = 11; j <= nmlngth+3; j++) putchar(' '); |
|---|
| 1865 | for (k = i; k <= l; k++) { |
|---|
| 1866 | putchar(itobase36(rdta->sitecat[k])); |
|---|
| 1867 | if (((k % 10) == 0) && (k < l)) putchar(' '); |
|---|
| 1868 | } |
|---|
| 1869 | putchar('\n'); |
|---|
| 1870 | } |
|---|
| 1871 | |
|---|
| 1872 | for (j = 1; j <= tr->mxtips; j++) { |
|---|
| 1873 | nameptr = tr->nodep[j]->name; |
|---|
| 1874 | k = nmlngth+3; |
|---|
| 1875 | while (ch = *nameptr++) {putchar(ch); k--;} |
|---|
| 1876 | while (--k >= 0) putchar(' '); |
|---|
| 1877 | |
|---|
| 1878 | for (k = i; k <= l; k++) { |
|---|
| 1879 | ch = rdta->y[j][k]; |
|---|
| 1880 | if ((j > 1) && (ch == rdta->y[1][k])) ch = '.'; |
|---|
| 1881 | putchar(ch); |
|---|
| 1882 | if (((k % 10) == 0) && (k < l)) putchar(' '); |
|---|
| 1883 | } |
|---|
| 1884 | putchar('\n'); |
|---|
| 1885 | } |
|---|
| 1886 | putchar('\n'); |
|---|
| 1887 | } |
|---|
| 1888 | } |
|---|
| 1889 | |
|---|
| 1890 | for (j = 1; j <= tr->mxtips; j++) /* Convert characters to meanings */ |
|---|
| 1891 | for (i = 1; i <= rdta->sites; i++) { |
|---|
| 1892 | rdta->y[j][i] = meaning[rdta->y[j][i]]; |
|---|
| 1893 | } |
|---|
| 1894 | |
|---|
| 1895 | return TRUE; |
|---|
| 1896 | } /* getdata */ |
|---|
| 1897 | |
|---|
| 1898 | |
|---|
| 1899 | boolean getntrees (analdef *adef) |
|---|
| 1900 | { /* getntrees */ |
|---|
| 1901 | |
|---|
| 1902 | if (fscanf(INFILE, "%d", &(adef->numutrees)) != 1 || findch('\n') == EOF) { |
|---|
| 1903 | printf("ERROR: Problem reading number of user trees\n"); |
|---|
| 1904 | return FALSE; |
|---|
| 1905 | } |
|---|
| 1906 | |
|---|
| 1907 | if (adef->nkeep == 0) adef->nkeep = adef->numutrees; |
|---|
| 1908 | |
|---|
| 1909 | return TRUE; |
|---|
| 1910 | } /* getntrees */ |
|---|
| 1911 | |
|---|
| 1912 | |
|---|
| 1913 | boolean getinput (analdef *adef, rawdata *rdta, cruncheddata *cdta, tree *tr) |
|---|
| 1914 | { /* getinput */ |
|---|
| 1915 | if (! getnums(rdta)) return FALSE; |
|---|
| 1916 | if (! getoptions(adef, rdta, cdta, tr)) return FALSE; |
|---|
| 1917 | if (! adef->empf && ! getbasefreqs(rdta)) return FALSE; |
|---|
| 1918 | if (! getyspace(rdta)) return FALSE; |
|---|
| 1919 | if (! setupTree(tr, rdta->sites)) return FALSE; |
|---|
| 1920 | if (! getdata(adef, rdta, tr)) return FALSE; |
|---|
| 1921 | if (adef->usertree && ! getntrees(adef)) return FALSE; |
|---|
| 1922 | |
|---|
| 1923 | return TRUE; |
|---|
| 1924 | } /* getinput */ |
|---|
| 1925 | |
|---|
| 1926 | |
|---|
| 1927 | void makeboot (analdef *adef, rawdata *rdta, cruncheddata *cdta) |
|---|
| 1928 | { /* makeboot */ |
|---|
| 1929 | int i, j, nonzero; |
|---|
| 1930 | double randum(); |
|---|
| 1931 | |
|---|
| 1932 | nonzero = 0; |
|---|
| 1933 | for (i = 1; i <= rdta->sites; i++) if (rdta->wgt[i] > 0) nonzero++; |
|---|
| 1934 | |
|---|
| 1935 | for (j = 1; j <= nonzero; j++) cdta->aliaswgt[j] = 0; |
|---|
| 1936 | for (j = 1; j <= nonzero; j++) |
|---|
| 1937 | cdta->aliaswgt[(int) (nonzero*randum(& adef->boot)) + 1]++; |
|---|
| 1938 | |
|---|
| 1939 | j = 0; |
|---|
| 1940 | cdta->wgtsum = 0; |
|---|
| 1941 | for (i = 1; i <= rdta->sites; i++) { |
|---|
| 1942 | if (rdta->wgt[i] > 0) |
|---|
| 1943 | cdta->wgtsum += (rdta->wgt2[i] = rdta->wgt[i] * cdta->aliaswgt[++j]); |
|---|
| 1944 | else |
|---|
| 1945 | rdta->wgt2[i] = 0; |
|---|
| 1946 | } |
|---|
| 1947 | } /* makeboot */ |
|---|
| 1948 | |
|---|
| 1949 | |
|---|
| 1950 | void sitesort (rawdata *rdta, cruncheddata *cdta) |
|---|
| 1951 | /* Shell sort keeping sites with identical residues and weights in |
|---|
| 1952 | * the original order (i.e., a stable sort). |
|---|
| 1953 | * The index created in cdta->alias is 1 based. The |
|---|
| 1954 | * sitecombcrunch routine packs it to a 0 based index. |
|---|
| 1955 | */ |
|---|
| 1956 | { /* sitesort */ |
|---|
| 1957 | int gap, i, j, jj, jg, k, n, nsp; |
|---|
| 1958 | int *index, *category; |
|---|
| 1959 | boolean flip, tied; |
|---|
| 1960 | yType **data; |
|---|
| 1961 | |
|---|
| 1962 | index = cdta->alias; |
|---|
| 1963 | category = rdta->sitecat; |
|---|
| 1964 | data = rdta->y; |
|---|
| 1965 | n = rdta->sites; |
|---|
| 1966 | nsp = rdta->numsp; |
|---|
| 1967 | |
|---|
| 1968 | for (gap = n / 2; gap > 0; gap /= 2) { |
|---|
| 1969 | for (i = gap + 1; i <= n; i++) { |
|---|
| 1970 | j = i - gap; |
|---|
| 1971 | |
|---|
| 1972 | do { |
|---|
| 1973 | jj = index[j]; |
|---|
| 1974 | jg = index[j+gap]; |
|---|
| 1975 | flip = (category[jj] > category[jg]); |
|---|
| 1976 | tied = (category[jj] == category[jg]); |
|---|
| 1977 | for (k = 1; (k <= nsp) && tied; k++) { |
|---|
| 1978 | flip = (data[k][jj] > data[k][jg]); |
|---|
| 1979 | tied = (data[k][jj] == data[k][jg]); |
|---|
| 1980 | } |
|---|
| 1981 | if (flip) { |
|---|
| 1982 | index[j] = jg; |
|---|
| 1983 | index[j+gap] = jj; |
|---|
| 1984 | j -= gap; |
|---|
| 1985 | } |
|---|
| 1986 | } while (flip && (j > 0)); |
|---|
| 1987 | |
|---|
| 1988 | } /* for (i ... */ |
|---|
| 1989 | } /* for (gap ... */ |
|---|
| 1990 | } /* sitesort */ |
|---|
| 1991 | |
|---|
| 1992 | |
|---|
| 1993 | void sitecombcrunch (rawdata *rdta, cruncheddata *cdta) |
|---|
| 1994 | /* combine sites that have identical patterns (and nonzero weight) */ |
|---|
| 1995 | { /* sitecombcrunch */ |
|---|
| 1996 | int i, sitei, j, sitej, k; |
|---|
| 1997 | boolean tied; |
|---|
| 1998 | |
|---|
| 1999 | i = 0; |
|---|
| 2000 | cdta->alias[0] = cdta->alias[1]; |
|---|
| 2001 | cdta->aliaswgt[0] = 0; |
|---|
| 2002 | |
|---|
| 2003 | for (j = 1; j <= rdta->sites; j++) { |
|---|
| 2004 | sitei = cdta->alias[i]; |
|---|
| 2005 | sitej = cdta->alias[j]; |
|---|
| 2006 | tied = (rdta->sitecat[sitei] == rdta->sitecat[sitej]); |
|---|
| 2007 | |
|---|
| 2008 | for (k = 1; tied && (k <= rdta->numsp); k++) |
|---|
| 2009 | tied = (rdta->y[k][sitei] == rdta->y[k][sitej]); |
|---|
| 2010 | |
|---|
| 2011 | if (tied) { |
|---|
| 2012 | cdta->aliaswgt[i] += rdta->wgt2[sitej]; |
|---|
| 2013 | } |
|---|
| 2014 | else { |
|---|
| 2015 | if (cdta->aliaswgt[i] > 0) i++; |
|---|
| 2016 | cdta->aliaswgt[i] = rdta->wgt2[sitej]; |
|---|
| 2017 | cdta->alias[i] = sitej; |
|---|
| 2018 | } |
|---|
| 2019 | } |
|---|
| 2020 | |
|---|
| 2021 | cdta->endsite = i; |
|---|
| 2022 | if (cdta->aliaswgt[i] > 0) cdta->endsite++; |
|---|
| 2023 | } /* sitecombcrunch */ |
|---|
| 2024 | |
|---|
| 2025 | |
|---|
| 2026 | boolean makeweights (analdef *adef, rawdata *rdta, cruncheddata *cdta) |
|---|
| 2027 | /* make up weights vector to avoid duplicate computations */ |
|---|
| 2028 | { /* makeweights */ |
|---|
| 2029 | int i; |
|---|
| 2030 | |
|---|
| 2031 | if (adef->boot) makeboot(adef, rdta, cdta); |
|---|
| 2032 | else for (i = 1; i <= rdta->sites; i++) rdta->wgt2[i] = rdta->wgt[i]; |
|---|
| 2033 | |
|---|
| 2034 | for (i = 1; i <= rdta->sites; i++) cdta->alias[i] = i; |
|---|
| 2035 | sitesort(rdta, cdta); |
|---|
| 2036 | sitecombcrunch(rdta, cdta); |
|---|
| 2037 | |
|---|
| 2038 | printf("Total weight of positions in analysis = %d\n", cdta->wgtsum); |
|---|
| 2039 | printf("There are %d distinct data patterns (columns)\n\n", cdta->endsite); |
|---|
| 2040 | |
|---|
| 2041 | return TRUE; |
|---|
| 2042 | } /* makeweights */ |
|---|
| 2043 | |
|---|
| 2044 | |
|---|
| 2045 | |
|---|
| 2046 | |
|---|
| 2047 | |
|---|
| 2048 | |
|---|
| 2049 | boolean makevalues (rawdata *rdta, cruncheddata *cdta) |
|---|
| 2050 | /* set up fractional likelihoods at tips */ |
|---|
| 2051 | { /* makevalues */ |
|---|
| 2052 | double temp, wtemp; |
|---|
| 2053 | int i, j; |
|---|
| 2054 | |
|---|
| 2055 | for (i = 1; i <= rdta->numsp; i++) { /* Pack and move tip data */ |
|---|
| 2056 | for (j = 0; j < cdta->endsite; j++) { |
|---|
| 2057 | rdta->y[i-1][j] = rdta->y[i][cdta->alias[j]]; |
|---|
| 2058 | } |
|---|
| 2059 | } |
|---|
| 2060 | |
|---|
| 2061 | for (j = 0; j < cdta->endsite; j++) { |
|---|
| 2062 | cdta->patcat[j] = i = rdta->sitecat[cdta->alias[j]]; |
|---|
| 2063 | cdta->patrat[j] = temp = rdta->catrat[i]; |
|---|
| 2064 | cdta->wr[j] = wtemp = temp * cdta->aliaswgt[j]; |
|---|
| 2065 | cdta->wr2[j] = temp * wtemp; |
|---|
| 2066 | } |
|---|
| 2067 | |
|---|
| 2068 | |
|---|
| 2069 | |
|---|
| 2070 | return TRUE; |
|---|
| 2071 | } /* makevalues */ |
|---|
| 2072 | |
|---|
| 2073 | |
|---|
| 2074 | boolean empiricalfreqs (rawdata *rdta, cruncheddata *cdta) |
|---|
| 2075 | /* Get empirical base frequencies from the data */ |
|---|
| 2076 | { /* empiricalfreqs */ |
|---|
| 2077 | double sum, suma, sumc, sumg, sumt, wj, fa, fc, fg, ft; |
|---|
| 2078 | int i, j, k, code; |
|---|
| 2079 | yType *yptr; |
|---|
| 2080 | |
|---|
| 2081 | rdta->freqa = 0.25; |
|---|
| 2082 | rdta->freqc = 0.25; |
|---|
| 2083 | rdta->freqg = 0.25; |
|---|
| 2084 | rdta->freqt = 0.25; |
|---|
| 2085 | for (k = 1; k <= 8; k++) { |
|---|
| 2086 | suma = 0.0; |
|---|
| 2087 | sumc = 0.0; |
|---|
| 2088 | sumg = 0.0; |
|---|
| 2089 | sumt = 0.0; |
|---|
| 2090 | for (i = 0; i < rdta->numsp; i++) { |
|---|
| 2091 | yptr = rdta->y[i]; |
|---|
| 2092 | for (j = 0; j < cdta->endsite; j++) { |
|---|
| 2093 | code = *yptr++; |
|---|
| 2094 | fa = rdta->freqa * ( code & 1); |
|---|
| 2095 | fc = rdta->freqc * ((code >> 1) & 1); |
|---|
| 2096 | fg = rdta->freqg * ((code >> 2) & 1); |
|---|
| 2097 | ft = rdta->freqt * ((code >> 3) & 1); |
|---|
| 2098 | wj = cdta->aliaswgt[j] / (fa + fc + fg + ft); |
|---|
| 2099 | suma += wj * fa; |
|---|
| 2100 | sumc += wj * fc; |
|---|
| 2101 | sumg += wj * fg; |
|---|
| 2102 | sumt += wj * ft; |
|---|
| 2103 | } |
|---|
| 2104 | } |
|---|
| 2105 | sum = suma + sumc + sumg + sumt; |
|---|
| 2106 | rdta->freqa = suma / sum; |
|---|
| 2107 | rdta->freqc = sumc / sum; |
|---|
| 2108 | rdta->freqg = sumg / sum; |
|---|
| 2109 | rdta->freqt = sumt / sum; |
|---|
| 2110 | } |
|---|
| 2111 | |
|---|
| 2112 | return TRUE; |
|---|
| 2113 | } /* empiricalfreqs */ |
|---|
| 2114 | |
|---|
| 2115 | |
|---|
| 2116 | void reportfreqs (analdef *adef, rawdata *rdta) |
|---|
| 2117 | { /* reportfreqs */ |
|---|
| 2118 | double suma, sumb; |
|---|
| 2119 | |
|---|
| 2120 | if (adef->empf) printf("Empirical "); |
|---|
| 2121 | printf("Base Frequencies:\n\n"); |
|---|
| 2122 | printf(" A %10.5f\n", rdta->freqa); |
|---|
| 2123 | printf(" C %10.5f\n", rdta->freqc); |
|---|
| 2124 | printf(" G %10.5f\n", rdta->freqg); |
|---|
| 2125 | printf(" T(U) %10.5f\n\n", rdta->freqt); |
|---|
| 2126 | rdta->freqr = rdta->freqa + rdta->freqg; |
|---|
| 2127 | rdta->invfreqr = 1.0/rdta->freqr; |
|---|
| 2128 | rdta->freqar = rdta->freqa * rdta->invfreqr; |
|---|
| 2129 | rdta->freqgr = rdta->freqg * rdta->invfreqr; |
|---|
| 2130 | rdta->freqy = rdta->freqc + rdta->freqt; |
|---|
| 2131 | rdta->invfreqy = 1.0/rdta->freqy; |
|---|
| 2132 | rdta->freqcy = rdta->freqc * rdta->invfreqy; |
|---|
| 2133 | rdta->freqty = rdta->freqt * rdta->invfreqy; |
|---|
| 2134 | printf("Transition/transversion ratio = %10.6f\n\n", rdta->ttratio); |
|---|
| 2135 | suma = rdta->ttratio*rdta->freqr*rdta->freqy |
|---|
| 2136 | - (rdta->freqa*rdta->freqg + rdta->freqc*rdta->freqt); |
|---|
| 2137 | sumb = rdta->freqa*rdta->freqgr + rdta->freqc*rdta->freqty; |
|---|
| 2138 | rdta->xi = suma/(suma+sumb); |
|---|
| 2139 | rdta->xv = 1.0 - rdta->xi; |
|---|
| 2140 | if (rdta->xi <= 0.0) { |
|---|
| 2141 | printf("WARNING: This transition/transversion ratio\n"); |
|---|
| 2142 | printf(" is impossible with these base frequencies!\n"); |
|---|
| 2143 | printf("Transition/transversion parameter reset\n\n"); |
|---|
| 2144 | rdta->xi = 0.000001; |
|---|
| 2145 | rdta->xv = 1.0 - rdta->xi; |
|---|
| 2146 | rdta->ttratio = (sumb * rdta->xi / rdta->xv |
|---|
| 2147 | + rdta->freqa * rdta->freqg |
|---|
| 2148 | + rdta->freqc * rdta->freqt) |
|---|
| 2149 | / (rdta->freqr * rdta->freqy); |
|---|
| 2150 | printf("Transition/transversion ratio = %10.6f\n\n", rdta->ttratio); |
|---|
| 2151 | } |
|---|
| 2152 | printf("(Transition/transversion parameter = %10.6f)\n\n", |
|---|
| 2153 | rdta->xi/rdta->xv); |
|---|
| 2154 | rdta->fracchange = 2.0 * rdta->xi * (rdta->freqa * rdta->freqgr |
|---|
| 2155 | + rdta->freqc * rdta->freqty) |
|---|
| 2156 | + rdta->xv * (1.0 - rdta->freqa * rdta->freqa |
|---|
| 2157 | - rdta->freqc * rdta->freqc |
|---|
| 2158 | - rdta->freqg * rdta->freqg |
|---|
| 2159 | - rdta->freqt * rdta->freqt); |
|---|
| 2160 | } /* reportfreqs */ |
|---|
| 2161 | |
|---|
| 2162 | |
|---|
| 2163 | boolean linkdata2tree (rawdata *rdta, cruncheddata *cdta, tree *tr) |
|---|
| 2164 | /* Link data array to the tree tips */ |
|---|
| 2165 | { /* linkdata2tree */ |
|---|
| 2166 | int i; |
|---|
| 2167 | int j; |
|---|
| 2168 | yType *tip; |
|---|
| 2169 | int ref[EQUALITIES]; |
|---|
| 2170 | tr->rdta = rdta; |
|---|
| 2171 | tr->cdta = cdta; |
|---|
| 2172 | |
|---|
| 2173 | for(i = 0; i < EQUALITIES; i++) |
|---|
| 2174 | ref[i] = 0; |
|---|
| 2175 | equalities = 0; |
|---|
| 2176 | |
|---|
| 2177 | |
|---|
| 2178 | for (i = 1; i <= tr->mxtips; i++) { /* Associate data with tips */ |
|---|
| 2179 | tr->nodep[i]->tip = &(rdta->y[i-1][0]); |
|---|
| 2180 | tip = tr->nodep[i]->tip; |
|---|
| 2181 | for(j = 0; j < tr->cdta->endsite; j++) |
|---|
| 2182 | { |
|---|
| 2183 | if(ref[tip[j]] == 0) |
|---|
| 2184 | ref[tip[j]] = 1; |
|---|
| 2185 | } |
|---|
| 2186 | } |
|---|
| 2187 | |
|---|
| 2188 | for(i = 0; i < EQUALITIES; i++) |
|---|
| 2189 | { |
|---|
| 2190 | if(ref[i] == 1) |
|---|
| 2191 | { |
|---|
| 2192 | map[i] = equalities++; |
|---|
| 2193 | } |
|---|
| 2194 | else |
|---|
| 2195 | map[i] = -1; |
|---|
| 2196 | } |
|---|
| 2197 | |
|---|
| 2198 | map2 = (char *)malloc(equalities * sizeof(char)); |
|---|
| 2199 | |
|---|
| 2200 | j = 0; |
|---|
| 2201 | |
|---|
| 2202 | for(i = 0; i < EQUALITIES; i++) |
|---|
| 2203 | { |
|---|
| 2204 | if(ref[i] == 1) |
|---|
| 2205 | { |
|---|
| 2206 | map2[j] = i; |
|---|
| 2207 | j++; |
|---|
| 2208 | } |
|---|
| 2209 | } |
|---|
| 2210 | |
|---|
| 2211 | |
|---|
| 2212 | |
|---|
| 2213 | return TRUE; |
|---|
| 2214 | } /* linkdata2tree */ |
|---|
| 2215 | |
|---|
| 2216 | |
|---|
| 2217 | xarray *setupxarray (int npat) |
|---|
| 2218 | { /* setupxarray */ |
|---|
| 2219 | xarray *x; |
|---|
| 2220 | likelivector *data; |
|---|
| 2221 | |
|---|
| 2222 | x = (xarray *) Malloc(sizeof(xarray)); |
|---|
| 2223 | if (x) { |
|---|
| 2224 | data = (likelivector *) Malloc(npat * sizeof(likelivector)); |
|---|
| 2225 | if (data) { |
|---|
| 2226 | x->lv = data; |
|---|
| 2227 | x->prev = x->next = x; |
|---|
| 2228 | x->owner = (node *) NULL; |
|---|
| 2229 | } |
|---|
| 2230 | else { |
|---|
| 2231 | Free(x); |
|---|
| 2232 | return (xarray *) NULL; |
|---|
| 2233 | } |
|---|
| 2234 | } |
|---|
| 2235 | return x; |
|---|
| 2236 | } /* setupxarray */ |
|---|
| 2237 | |
|---|
| 2238 | |
|---|
| 2239 | boolean linkxarray (int req, int min, int npat, |
|---|
| 2240 | xarray **freexptr, xarray **usedxptr) |
|---|
| 2241 | /* Link a set of xarrays */ |
|---|
| 2242 | { /* linkxarray */ |
|---|
| 2243 | xarray *first, *prev, *x; |
|---|
| 2244 | int i; |
|---|
| 2245 | |
|---|
| 2246 | first = prev = (xarray *) NULL; |
|---|
| 2247 | i = 0; |
|---|
| 2248 | |
|---|
| 2249 | do { |
|---|
| 2250 | x = setupxarray(npat); |
|---|
| 2251 | if (x) { |
|---|
| 2252 | if (! first) first = x; |
|---|
| 2253 | else { |
|---|
| 2254 | prev->next = x; |
|---|
| 2255 | x->prev = prev; |
|---|
| 2256 | } |
|---|
| 2257 | prev = x; |
|---|
| 2258 | i++; |
|---|
| 2259 | } |
|---|
| 2260 | else { |
|---|
| 2261 | printf("ERROR: Failure to get requested xarray memory\n"); |
|---|
| 2262 | if (i < min) return FALSE; |
|---|
| 2263 | } |
|---|
| 2264 | } while ((i < req) && x); |
|---|
| 2265 | |
|---|
| 2266 | if (first) { |
|---|
| 2267 | first->prev = prev; |
|---|
| 2268 | prev->next = first; |
|---|
| 2269 | } |
|---|
| 2270 | |
|---|
| 2271 | *freexptr = first; |
|---|
| 2272 | *usedxptr = (xarray *) NULL; |
|---|
| 2273 | |
|---|
| 2274 | return TRUE; |
|---|
| 2275 | } /* linkxarray */ |
|---|
| 2276 | |
|---|
| 2277 | |
|---|
| 2278 | boolean setupnodex (tree *tr) |
|---|
| 2279 | { /* setupnodex */ |
|---|
| 2280 | nodeptr p; |
|---|
| 2281 | int i; |
|---|
| 2282 | |
|---|
| 2283 | for (i = tr->mxtips + 1; (i <= 2*(tr->mxtips) - 2); i++) { |
|---|
| 2284 | p = tr->nodep[i]; |
|---|
| 2285 | if (! (p->x = setupxarray(tr->cdta->endsite))) { |
|---|
| 2286 | printf("ERROR: Failure to get internal node xarray memory\n"); |
|---|
| 2287 | return FALSE; |
|---|
| 2288 | } |
|---|
| 2289 | } |
|---|
| 2290 | |
|---|
| 2291 | return TRUE; |
|---|
| 2292 | } /* setupnodex */ |
|---|
| 2293 | |
|---|
| 2294 | |
|---|
| 2295 | xarray *getxtip (nodeptr p) |
|---|
| 2296 | { /* getxtip */ |
|---|
| 2297 | xarray *new; |
|---|
| 2298 | boolean splice; |
|---|
| 2299 | |
|---|
| 2300 | if (! p) return (xarray *) NULL; |
|---|
| 2301 | |
|---|
| 2302 | splice = FALSE; |
|---|
| 2303 | |
|---|
| 2304 | if (p->x) { /* array is there; move to tail of list */ |
|---|
| 2305 | new = p->x; |
|---|
| 2306 | if (new == new->prev) ; /* linked to self; leave it */ |
|---|
| 2307 | else if (new == usedxtip) usedxtip = usedxtip->next; /* at head */ |
|---|
| 2308 | else if (new == usedxtip->prev) ; /* already at tail */ |
|---|
| 2309 | else { /* move to tail of list */ |
|---|
| 2310 | new->prev->next = new->next; |
|---|
| 2311 | new->next->prev = new->prev; |
|---|
| 2312 | splice = TRUE; |
|---|
| 2313 | } |
|---|
| 2314 | } |
|---|
| 2315 | |
|---|
| 2316 | else if (freextip) { /* take from unused list */ |
|---|
| 2317 | p->x = new = freextip; |
|---|
| 2318 | new->owner = p; |
|---|
| 2319 | if (new->prev != new) { /* not only member of freelist */ |
|---|
| 2320 | new->prev->next = new->next; |
|---|
| 2321 | new->next->prev = new->prev; |
|---|
| 2322 | freextip = new->next; |
|---|
| 2323 | } |
|---|
| 2324 | else |
|---|
| 2325 | freextip = (xarray *) NULL; |
|---|
| 2326 | |
|---|
| 2327 | splice = TRUE; |
|---|
| 2328 | } |
|---|
| 2329 | |
|---|
| 2330 | else if (usedxtip) { /* take from head of used list */ |
|---|
| 2331 | usedxtip->owner->x = (xarray *) NULL; |
|---|
| 2332 | p->x = new = usedxtip; |
|---|
| 2333 | new->owner = p; |
|---|
| 2334 | usedxtip = usedxtip->next; |
|---|
| 2335 | } |
|---|
| 2336 | |
|---|
| 2337 | else { |
|---|
| 2338 | printf("ERROR: Unable to locate memory for tip %d.\n", p->number); |
|---|
| 2339 | return (xarray *) NULL; |
|---|
| 2340 | } |
|---|
| 2341 | |
|---|
| 2342 | if (splice) { |
|---|
| 2343 | if (usedxtip) { /* list is not empty */ |
|---|
| 2344 | usedxtip->prev->next = new; |
|---|
| 2345 | new->prev = usedxtip->prev; |
|---|
| 2346 | usedxtip->prev = new; |
|---|
| 2347 | new->next = usedxtip; |
|---|
| 2348 | } |
|---|
| 2349 | else |
|---|
| 2350 | usedxtip = new->prev = new->next = new; |
|---|
| 2351 | } |
|---|
| 2352 | |
|---|
| 2353 | return new; |
|---|
| 2354 | } /* getxtip */ |
|---|
| 2355 | |
|---|
| 2356 | |
|---|
| 2357 | xarray *getxnode (nodeptr p) |
|---|
| 2358 | /* Ensure that internal node p has memory */ |
|---|
| 2359 | { /* getxnode */ |
|---|
| 2360 | nodeptr s; |
|---|
| 2361 | |
|---|
| 2362 | if (! (p->x)) { /* Move likelihood array on this node to sector p */ |
|---|
| 2363 | if ((s = p->next)->x || (s = s->next)->x) { |
|---|
| 2364 | p->x = s->x; |
|---|
| 2365 | s->x = (xarray *) NULL; |
|---|
| 2366 | } |
|---|
| 2367 | else { |
|---|
| 2368 | printf("ERROR: Unable to locate memory at node %d.\n", p->number); |
|---|
| 2369 | exit(1); |
|---|
| 2370 | } |
|---|
| 2371 | } |
|---|
| 2372 | return p->x; |
|---|
| 2373 | } /* getxnode */ |
|---|
| 2374 | |
|---|
| 2375 | |
|---|
| 2376 | /* AxML modification start*/ |
|---|
| 2377 | /* optimized version of function newview(), implementing homogeneous and */ |
|---|
| 2378 | /* partially heterogeneous subtree equality vectors */ |
|---|
| 2379 | |
|---|
| 2380 | |
|---|
| 2381 | void printhq(long *hq, tree *tr) |
|---|
| 2382 | { |
|---|
| 2383 | int i; |
|---|
| 2384 | for(i = 0; i < tr->cdta->endsite; i++) |
|---|
| 2385 | fprintf(stderr,"%ld ", hq[i]); |
|---|
| 2386 | fprintf(stderr,"\n"); |
|---|
| 2387 | } |
|---|
| 2388 | |
|---|
| 2389 | |
|---|
| 2390 | |
|---|
| 2391 | boolean newviewOptimized(tree *tr, nodeptr p) /* Update likelihoods at node */ |
|---|
| 2392 | { /* newviewOptimized */ |
|---|
| 2393 | double zq, lzq, xvlzq, zr, lzr, xvlzr; |
|---|
| 2394 | nodeptr q, r, realP; |
|---|
| 2395 | likelivector *lp, *lq, *lr; |
|---|
| 2396 | homType *mlvP; |
|---|
| 2397 | int i, j; |
|---|
| 2398 | |
|---|
| 2399 | if (p->tip) { /* Make sure that data are at tip */ |
|---|
| 2400 | |
|---|
| 2401 | if (p->x) return TRUE; /* They are already there */ |
|---|
| 2402 | |
|---|
| 2403 | if (! getxtip(p)) return FALSE; /* They are not, so get memory */ |
|---|
| 2404 | |
|---|
| 2405 | return TRUE; |
|---|
| 2406 | } |
|---|
| 2407 | |
|---|
| 2408 | /* Internal node needs update */ |
|---|
| 2409 | |
|---|
| 2410 | q = p->next->back; |
|---|
| 2411 | r = p->next->next->back; |
|---|
| 2412 | |
|---|
| 2413 | while ((! p->x) || (! q->x) || (! r->x)) { |
|---|
| 2414 | /* rekursiver aufruf */ |
|---|
| 2415 | if (! q->x) if (! newviewOptimized(tr, q)) return FALSE; |
|---|
| 2416 | if (! r->x) if (! newviewOptimized(tr, r)) return FALSE; |
|---|
| 2417 | if (! p->x) if (! getxnode(p)) return FALSE; |
|---|
| 2418 | } |
|---|
| 2419 | |
|---|
| 2420 | |
|---|
| 2421 | |
|---|
| 2422 | lp = p->x->lv; |
|---|
| 2423 | |
|---|
| 2424 | lq = q->x->lv; |
|---|
| 2425 | zq = q->z; |
|---|
| 2426 | lzq = (zq > zmin) ? log(zq) : log(zmin); |
|---|
| 2427 | xvlzq = tr->rdta->xv * lzq; |
|---|
| 2428 | |
|---|
| 2429 | lr = r->x->lv; |
|---|
| 2430 | zr = r->z; |
|---|
| 2431 | lzr = (zr > zmin) ? log(zr) : log(zmin); |
|---|
| 2432 | xvlzr = tr->rdta->xv * lzr; |
|---|
| 2433 | |
|---|
| 2434 | { double zzq, zvq, |
|---|
| 2435 | zzr, zvr; |
|---|
| 2436 | double zzqtable[maxcategories+1], zvqtable[maxcategories+1], |
|---|
| 2437 | zzrtable[maxcategories+1], zvrtable[maxcategories+1], |
|---|
| 2438 | *zzqptr, *zvqptr, *zzrptr, *zvrptr, *rptr; |
|---|
| 2439 | double fxqr, fxqy, fxqn, sumaq, sumgq, sumcq, sumtq, |
|---|
| 2440 | fxrr, fxry, fxrn, ki, tempi, tempj; |
|---|
| 2441 | register char *s1; |
|---|
| 2442 | register char *s2; |
|---|
| 2443 | register char *eqptr; |
|---|
| 2444 | int *cptr; |
|---|
| 2445 | int cat; |
|---|
| 2446 | homType *mlvR, *mlvQ; |
|---|
| 2447 | poutsa calc[EQUALITIES][maxcategories],calcR[EQUALITIES][maxcategories], *calcptr, *calcptrR; |
|---|
| 2448 | long ref, ref2, mexp; |
|---|
| 2449 | nodeptr realQ, realR; |
|---|
| 2450 | |
|---|
| 2451 | |
|---|
| 2452 | rptr = &(tr->rdta->catrat[1]); |
|---|
| 2453 | zzqptr = &(zzqtable[1]); |
|---|
| 2454 | zvqptr = &(zvqtable[1]); |
|---|
| 2455 | zzrptr = &(zzrtable[1]); |
|---|
| 2456 | zvrptr = &(zvrtable[1]); |
|---|
| 2457 | /* get equality vectors of p, q, and r */ |
|---|
| 2458 | |
|---|
| 2459 | realP = tr->nodep[p->number]; |
|---|
| 2460 | |
|---|
| 2461 | |
|---|
| 2462 | realQ = tr->nodep[q->number]; |
|---|
| 2463 | realR = tr->nodep[r->number]; |
|---|
| 2464 | |
|---|
| 2465 | eqptr = realP->equalityVector; |
|---|
| 2466 | s1 = realQ->equalityVector; |
|---|
| 2467 | s2 = realR->equalityVector; |
|---|
| 2468 | |
|---|
| 2469 | |
|---|
| 2470 | for (j = 1; j <= tr->rdta->categs; j++) |
|---|
| 2471 | { |
|---|
| 2472 | ki = *rptr++; |
|---|
| 2473 | *zzqptr++ = zzq = exp(ki * lzq); |
|---|
| 2474 | *zvqptr++ = zvq = exp(ki * xvlzq); |
|---|
| 2475 | *zzrptr++ = zzr = exp(ki * lzr); |
|---|
| 2476 | *zvrptr++ = zvr = exp(ki * xvlzr); |
|---|
| 2477 | |
|---|
| 2478 | for(i = 0; i < equalities; i++) |
|---|
| 2479 | { |
|---|
| 2480 | |
|---|
| 2481 | mlvQ = &realQ->mlv[i][j]; |
|---|
| 2482 | |
|---|
| 2483 | if(mlvQ->set) |
|---|
| 2484 | { |
|---|
| 2485 | calcptr = &calc[i][j]; |
|---|
| 2486 | fxqr = tr->rdta->freqa * mlvQ->a + tr->rdta->freqg * mlvQ->g; |
|---|
| 2487 | fxqy = tr->rdta->freqc * mlvQ->c + tr->rdta->freqt * mlvQ->t; |
|---|
| 2488 | fxqn = fxqr + fxqy; |
|---|
| 2489 | tempi = fxqr * tr->rdta->invfreqr; |
|---|
| 2490 | tempj = zvq * (tempi-fxqn) + fxqn; |
|---|
| 2491 | calcptr->sumaq = zzq * (mlvQ->a - tempi) + tempj; |
|---|
| 2492 | calcptr->sumgq = zzq * (mlvQ->g - tempi) + tempj; |
|---|
| 2493 | tempi = fxqy * tr->rdta->invfreqy; |
|---|
| 2494 | tempj = zvq * (tempi-fxqn) + fxqn; |
|---|
| 2495 | calcptr->sumcq = zzq * (mlvQ->c - tempi) + tempj; |
|---|
| 2496 | calcptr->sumtq = zzq * (mlvQ->t - tempi) + tempj; |
|---|
| 2497 | calcptr->exp = mlvQ->exp; |
|---|
| 2498 | } |
|---|
| 2499 | |
|---|
| 2500 | mlvR = &realR->mlv[i][j]; |
|---|
| 2501 | |
|---|
| 2502 | if(mlvR->set) |
|---|
| 2503 | { |
|---|
| 2504 | calcptrR = &calcR[i][j]; |
|---|
| 2505 | fxrr = tr->rdta->freqa * mlvR->a + tr->rdta->freqg * mlvR->g; |
|---|
| 2506 | fxry = tr->rdta->freqc * mlvR->c + tr->rdta->freqt * mlvR->t; |
|---|
| 2507 | fxrn = fxrr + fxry; |
|---|
| 2508 | tempi = fxrr * tr->rdta->invfreqr; |
|---|
| 2509 | tempj = zvr * (tempi-fxrn) + fxrn; |
|---|
| 2510 | calcptrR->sumaq = zzr * (mlvR->a - tempi) + tempj; |
|---|
| 2511 | calcptrR->sumgq = zzr * (mlvR->g - tempi) + tempj; |
|---|
| 2512 | tempi = fxry * tr->rdta->invfreqy; |
|---|
| 2513 | tempj = zvr * (tempi-fxrn) + fxrn; |
|---|
| 2514 | calcptrR->sumcq = zzr * (mlvR->c - tempi) + tempj; |
|---|
| 2515 | calcptrR->sumtq = zzr * (mlvR->t - tempi) + tempj; |
|---|
| 2516 | calcptrR->exp = mlvR->exp; |
|---|
| 2517 | } |
|---|
| 2518 | |
|---|
| 2519 | if(mlvR->set && mlvQ->set) |
|---|
| 2520 | { |
|---|
| 2521 | mlvP = &realP->mlv[i][j]; |
|---|
| 2522 | |
|---|
| 2523 | mlvP->a = calcptrR->sumaq * calcptr->sumaq; |
|---|
| 2524 | mlvP->g = calcptrR->sumgq * calcptr->sumgq; |
|---|
| 2525 | mlvP->c = calcptrR->sumcq * calcptr->sumcq; |
|---|
| 2526 | mlvP->t = calcptrR->sumtq * calcptr->sumtq; |
|---|
| 2527 | mlvP->exp = calcptrR->exp + calcptr->exp; |
|---|
| 2528 | if (mlvP->a < minlikelihood && mlvP->g < minlikelihood |
|---|
| 2529 | && mlvP->c < minlikelihood && mlvP->t < minlikelihood) { |
|---|
| 2530 | mlvP->a *= twotothe256; |
|---|
| 2531 | mlvP->g *= twotothe256; |
|---|
| 2532 | mlvP->c *= twotothe256; |
|---|
| 2533 | mlvP->t *= twotothe256; |
|---|
| 2534 | mlvP->exp += 1; |
|---|
| 2535 | } |
|---|
| 2536 | |
|---|
| 2537 | mlvP->set = TRUE; |
|---|
| 2538 | } |
|---|
| 2539 | else |
|---|
| 2540 | mlvP->set = FALSE; |
|---|
| 2541 | |
|---|
| 2542 | } |
|---|
| 2543 | } |
|---|
| 2544 | |
|---|
| 2545 | cptr = &(tr->cdta->patcat[0]); |
|---|
| 2546 | |
|---|
| 2547 | if(r->tip && q->tip) |
|---|
| 2548 | { |
|---|
| 2549 | for (i = 0; i < tr->cdta->endsite; i++) |
|---|
| 2550 | { |
|---|
| 2551 | ref = *s2++; |
|---|
| 2552 | ref2 = *s1++; |
|---|
| 2553 | cat = *cptr++; |
|---|
| 2554 | |
|---|
| 2555 | |
|---|
| 2556 | if(ref2 != ref) |
|---|
| 2557 | { |
|---|
| 2558 | *eqptr++ = -1; |
|---|
| 2559 | calcptrR = &calcR[ref][cat]; |
|---|
| 2560 | calcptr = &calc[ref2][cat]; |
|---|
| 2561 | lp->a = calcptrR->sumaq * calcptr->sumaq; |
|---|
| 2562 | lp->g = calcptrR->sumgq * calcptr->sumgq; |
|---|
| 2563 | lp->c = calcptrR->sumcq * calcptr->sumcq; |
|---|
| 2564 | lp->t = calcptrR->sumtq * calcptr->sumtq; |
|---|
| 2565 | lp->exp = calcptrR->exp + calcptr->exp; |
|---|
| 2566 | if (lp->a < minlikelihood && lp->g < minlikelihood |
|---|
| 2567 | && lp->c < minlikelihood && lp->t < minlikelihood) { |
|---|
| 2568 | lp->a *= twotothe256; |
|---|
| 2569 | lp->g *= twotothe256; |
|---|
| 2570 | lp->c *= twotothe256; |
|---|
| 2571 | lp->t *= twotothe256; |
|---|
| 2572 | lp->exp += 1; |
|---|
| 2573 | } |
|---|
| 2574 | |
|---|
| 2575 | } |
|---|
| 2576 | else |
|---|
| 2577 | { |
|---|
| 2578 | *eqptr++ = ref; |
|---|
| 2579 | } |
|---|
| 2580 | lp++; |
|---|
| 2581 | |
|---|
| 2582 | } |
|---|
| 2583 | } |
|---|
| 2584 | else |
|---|
| 2585 | { |
|---|
| 2586 | |
|---|
| 2587 | |
|---|
| 2588 | for (i = 0; i < tr->cdta->endsite; i++) |
|---|
| 2589 | { |
|---|
| 2590 | ref = *s2++; |
|---|
| 2591 | ref2 = *s1++; |
|---|
| 2592 | cat = *cptr++; |
|---|
| 2593 | |
|---|
| 2594 | if(ref2 >= 0) |
|---|
| 2595 | { |
|---|
| 2596 | if(ref2 == ref) |
|---|
| 2597 | { |
|---|
| 2598 | *eqptr++ = ref; |
|---|
| 2599 | goto incTip; |
|---|
| 2600 | } |
|---|
| 2601 | calcptr = &calc[ref2][cat]; |
|---|
| 2602 | sumaq = calcptr->sumaq; |
|---|
| 2603 | sumgq = calcptr->sumgq; |
|---|
| 2604 | sumcq = calcptr->sumcq; |
|---|
| 2605 | sumtq = calcptr->sumtq; |
|---|
| 2606 | mexp = calcptr->exp; |
|---|
| 2607 | |
|---|
| 2608 | } |
|---|
| 2609 | else |
|---|
| 2610 | { |
|---|
| 2611 | zzq = zzqtable[cat]; |
|---|
| 2612 | zvq = zvqtable[cat]; |
|---|
| 2613 | fxqr = tr->rdta->freqa * lq->a + tr->rdta->freqg * lq->g; |
|---|
| 2614 | fxqy = tr->rdta->freqc * lq->c + tr->rdta->freqt * lq->t; |
|---|
| 2615 | fxqn = fxqr + fxqy; |
|---|
| 2616 | tempi = fxqr * tr->rdta->invfreqr; |
|---|
| 2617 | tempj = zvq * (tempi-fxqn) + fxqn; |
|---|
| 2618 | sumaq = zzq * (lq->a - tempi) + tempj; |
|---|
| 2619 | sumgq = zzq * (lq->g - tempi) + tempj; |
|---|
| 2620 | tempi = fxqy * tr->rdta->invfreqy; |
|---|
| 2621 | tempj = zvq * (tempi-fxqn) + fxqn; |
|---|
| 2622 | sumcq = zzq * (lq->c - tempi) + tempj; |
|---|
| 2623 | sumtq = zzq * (lq->t - tempi) + tempj; |
|---|
| 2624 | mexp = lq->exp; |
|---|
| 2625 | } |
|---|
| 2626 | |
|---|
| 2627 | |
|---|
| 2628 | if(ref >= 0) |
|---|
| 2629 | { |
|---|
| 2630 | calcptr = &calcR[ref][cat]; |
|---|
| 2631 | lp->a = sumaq * calcptr->sumaq; |
|---|
| 2632 | lp->g = sumgq * calcptr->sumgq; |
|---|
| 2633 | lp->c = sumcq * calcptr->sumcq; |
|---|
| 2634 | lp->t = sumtq * calcptr->sumtq; |
|---|
| 2635 | lp->exp = mexp + calcptr->exp; |
|---|
| 2636 | } |
|---|
| 2637 | else |
|---|
| 2638 | { |
|---|
| 2639 | zzr = zzrtable[cat]; |
|---|
| 2640 | zvr = zvrtable[cat]; |
|---|
| 2641 | fxrr = tr->rdta->freqa * lr->a + tr->rdta->freqg * lr->g; |
|---|
| 2642 | fxry = tr->rdta->freqc * lr->c + tr->rdta->freqt * lr->t; |
|---|
| 2643 | fxrn = fxrr + fxry; |
|---|
| 2644 | tempi = fxrr * tr->rdta->invfreqr; |
|---|
| 2645 | tempj = zvr * (tempi-fxrn) + fxrn; |
|---|
| 2646 | lp->a = sumaq * (zzr * (lr->a - tempi) + tempj); |
|---|
| 2647 | lp->g = sumgq * (zzr * (lr->g - tempi) + tempj); |
|---|
| 2648 | tempi = fxry * tr->rdta->invfreqy; |
|---|
| 2649 | tempj = zvr * (tempi-fxrn) + fxrn; |
|---|
| 2650 | lp->c = sumcq * (zzr * (lr->c - tempi) + tempj); |
|---|
| 2651 | lp->t = sumtq * (zzr * (lr->t - tempi) + tempj); |
|---|
| 2652 | lp->exp = mexp + lr->exp; |
|---|
| 2653 | } |
|---|
| 2654 | |
|---|
| 2655 | if (lp->a < minlikelihood && lp->g < minlikelihood |
|---|
| 2656 | && lp->c < minlikelihood && lp->t < minlikelihood) { |
|---|
| 2657 | lp->a *= twotothe256; |
|---|
| 2658 | lp->g *= twotothe256; |
|---|
| 2659 | lp->c *= twotothe256; |
|---|
| 2660 | lp->t *= twotothe256; |
|---|
| 2661 | lp->exp += 1; |
|---|
| 2662 | } |
|---|
| 2663 | |
|---|
| 2664 | *eqptr++ = -1; |
|---|
| 2665 | incTip: |
|---|
| 2666 | lp++; |
|---|
| 2667 | lq++; |
|---|
| 2668 | lr++; |
|---|
| 2669 | } |
|---|
| 2670 | } |
|---|
| 2671 | } |
|---|
| 2672 | |
|---|
| 2673 | |
|---|
| 2674 | return TRUE; |
|---|
| 2675 | } /* newviewOptimized */ |
|---|
| 2676 | |
|---|
| 2677 | /* AxML modification end*/ |
|---|
| 2678 | |
|---|
| 2679 | double evaluate (tree *tr, nodeptr p) |
|---|
| 2680 | { /* evaluate */ |
|---|
| 2681 | double sum, z, lz, xvlz, |
|---|
| 2682 | ki, fx1a, fx1c, fx1g, fx1t, fx1r, fx1y, fx2r, fx2y, |
|---|
| 2683 | suma, sumb, sumc, term; |
|---|
| 2684 | |
|---|
| 2685 | |
|---|
| 2686 | double zz, zv; |
|---|
| 2687 | double zztable[maxcategories+1], zvtable[maxcategories+1], |
|---|
| 2688 | *zzptr, *zvptr; |
|---|
| 2689 | double *log_f, *rptr; |
|---|
| 2690 | likelivector *lp, *lq; |
|---|
| 2691 | homType *mlvP, *mlvQ; |
|---|
| 2692 | nodeptr q, realQ, realP; |
|---|
| 2693 | int i, *wptr, cat, *cptr, j; |
|---|
| 2694 | register char *sQ; |
|---|
| 2695 | register char *sP; |
|---|
| 2696 | long refQ, refP; |
|---|
| 2697 | memP calcP[EQUALITIES][maxcategories], *calcptrP; |
|---|
| 2698 | memQ calcQ[EQUALITIES][maxcategories], *calcptrQ; |
|---|
| 2699 | |
|---|
| 2700 | q = p->back; |
|---|
| 2701 | |
|---|
| 2702 | while ((! p->x) || (! q->x)) { |
|---|
| 2703 | if (! (p->x)) if (! newviewOptimized(tr, p)) return badEval; |
|---|
| 2704 | if (! (q->x)) if (! newviewOptimized(tr, q)) return badEval; |
|---|
| 2705 | } |
|---|
| 2706 | |
|---|
| 2707 | lp = p->x->lv; |
|---|
| 2708 | lq = q->x->lv; |
|---|
| 2709 | |
|---|
| 2710 | z = p->z; |
|---|
| 2711 | if (z < zmin) z = zmin; |
|---|
| 2712 | lz = log(z); |
|---|
| 2713 | xvlz = tr->rdta->xv * lz; |
|---|
| 2714 | |
|---|
| 2715 | rptr = &(tr->rdta->catrat[1]); |
|---|
| 2716 | zzptr = &(zztable[1]); |
|---|
| 2717 | zvptr = &(zvtable[1]); |
|---|
| 2718 | |
|---|
| 2719 | |
|---|
| 2720 | cptr = &(tr->cdta->patcat[0]); |
|---|
| 2721 | wptr = &(tr->cdta->aliaswgt[0]); |
|---|
| 2722 | log_f = tr->log_f; |
|---|
| 2723 | tr->log_f_valid = tr->cdta->endsite; |
|---|
| 2724 | sum = 0.0; |
|---|
| 2725 | |
|---|
| 2726 | realQ = tr->nodep[q->number]; |
|---|
| 2727 | realP = tr->nodep[p->number]; |
|---|
| 2728 | sQ = realQ->equalityVector; |
|---|
| 2729 | sP = realP->equalityVector; |
|---|
| 2730 | |
|---|
| 2731 | |
|---|
| 2732 | for (j = 1; j <= tr->rdta->categs; j++) |
|---|
| 2733 | { |
|---|
| 2734 | ki = *rptr++; |
|---|
| 2735 | *zzptr++ = exp(ki * lz); |
|---|
| 2736 | *zvptr++ = exp(ki * xvlz); |
|---|
| 2737 | |
|---|
| 2738 | for(i = 0; i < equalities; i++) |
|---|
| 2739 | { |
|---|
| 2740 | |
|---|
| 2741 | mlvP = &realP->mlv[i][j]; |
|---|
| 2742 | if(mlvP->set) |
|---|
| 2743 | { |
|---|
| 2744 | calcptrP = &calcP[i][j]; |
|---|
| 2745 | calcptrP->fx1a = tr->rdta->freqa * mlvP->a; |
|---|
| 2746 | calcptrP->fx1g = tr->rdta->freqg * mlvP->g; |
|---|
| 2747 | calcptrP->fx1c = tr->rdta->freqc * mlvP->c; |
|---|
| 2748 | calcptrP->fx1t = tr->rdta->freqt * mlvP->t; |
|---|
| 2749 | calcptrP->sumag = calcptrP->fx1a + calcptrP->fx1g; |
|---|
| 2750 | calcptrP->sumct = calcptrP->fx1c + calcptrP->fx1t; |
|---|
| 2751 | calcptrP->sumagct = calcptrP->sumag + calcptrP->sumct; |
|---|
| 2752 | calcptrP->exp = mlvP->exp; |
|---|
| 2753 | } |
|---|
| 2754 | mlvQ = &realQ->mlv[i][j]; |
|---|
| 2755 | if(mlvQ->set) |
|---|
| 2756 | { |
|---|
| 2757 | calcptrQ = &calcQ[i][j]; |
|---|
| 2758 | calcptrQ->fx2r = tr->rdta->freqa * mlvQ->a + tr->rdta->freqg * mlvQ->g; |
|---|
| 2759 | calcptrQ->fx2y = tr->rdta->freqc * mlvQ->c + tr->rdta->freqt * mlvQ->t; |
|---|
| 2760 | calcptrQ->sumfx2rfx2y = calcptrQ->fx2r + calcptrQ->fx2y; |
|---|
| 2761 | calcptrQ->exp = mlvQ->exp; |
|---|
| 2762 | calcptrQ->a = mlvQ->a; |
|---|
| 2763 | calcptrQ->c = mlvQ->c; |
|---|
| 2764 | calcptrQ->g = mlvQ->g; |
|---|
| 2765 | calcptrQ->t = mlvQ->t; |
|---|
| 2766 | } |
|---|
| 2767 | |
|---|
| 2768 | } |
|---|
| 2769 | } |
|---|
| 2770 | |
|---|
| 2771 | |
|---|
| 2772 | |
|---|
| 2773 | |
|---|
| 2774 | for (i = 0; i < tr->cdta->endsite; i++) { |
|---|
| 2775 | refQ = *sQ++; |
|---|
| 2776 | refP = *sP++; |
|---|
| 2777 | cat = *cptr++; |
|---|
| 2778 | zz = zztable[cat]; |
|---|
| 2779 | zv = zvtable[cat]; |
|---|
| 2780 | if(refP >= 0) |
|---|
| 2781 | { |
|---|
| 2782 | if(refQ >= 0) |
|---|
| 2783 | { |
|---|
| 2784 | calcptrP = &calcP[refP][cat]; |
|---|
| 2785 | calcptrQ = &calcQ[refQ][cat]; |
|---|
| 2786 | suma = calcptrP->fx1a * calcptrQ->a + |
|---|
| 2787 | calcptrP->fx1c * calcptrQ->c + |
|---|
| 2788 | calcptrP->fx1g * calcptrQ->g + calcptrP->fx1t * calcptrQ->t; |
|---|
| 2789 | |
|---|
| 2790 | sumc = calcptrP->sumagct * calcptrQ->sumfx2rfx2y; |
|---|
| 2791 | sumb = calcptrP->sumag * calcptrQ->fx2r * tr->rdta->invfreqr + calcptrP->sumct * calcptrQ->fx2y * tr->rdta->invfreqy; |
|---|
| 2792 | suma -= sumb; |
|---|
| 2793 | sumb -= sumc; |
|---|
| 2794 | term = log(zz * suma + zv * sumb + sumc) + |
|---|
| 2795 | (calcptrP->exp + calcptrQ->exp)*log(minlikelihood); |
|---|
| 2796 | sum += *wptr++ * term; |
|---|
| 2797 | |
|---|
| 2798 | *log_f++ = term; |
|---|
| 2799 | lp++; |
|---|
| 2800 | lq++; |
|---|
| 2801 | } |
|---|
| 2802 | else |
|---|
| 2803 | { |
|---|
| 2804 | calcptrP = &calcP[refP][cat]; |
|---|
| 2805 | suma = calcptrP->fx1a * lq->a + calcptrP->fx1c * lq->c + |
|---|
| 2806 | calcptrP->fx1g * lq->g + calcptrP->fx1t * lq->t; |
|---|
| 2807 | |
|---|
| 2808 | fx2r = tr->rdta->freqa * lq->a + tr->rdta->freqg * lq->g; |
|---|
| 2809 | fx2y = tr->rdta->freqc * lq->c + tr->rdta->freqt * lq->t; |
|---|
| 2810 | |
|---|
| 2811 | sumc = calcptrP->sumagct * (fx2r + fx2y); |
|---|
| 2812 | sumb = calcptrP->sumag * fx2r * tr->rdta->invfreqr + calcptrP->sumct * fx2y * tr->rdta->invfreqy; |
|---|
| 2813 | suma -= sumb; |
|---|
| 2814 | sumb -= sumc; |
|---|
| 2815 | term = log(zz * suma + zv * sumb + sumc) + |
|---|
| 2816 | (calcptrP->exp + lq->exp)*log(minlikelihood); |
|---|
| 2817 | sum += *wptr++ * term; |
|---|
| 2818 | |
|---|
| 2819 | *log_f++ = term; |
|---|
| 2820 | lp++; |
|---|
| 2821 | lq++; |
|---|
| 2822 | } |
|---|
| 2823 | } |
|---|
| 2824 | else |
|---|
| 2825 | { |
|---|
| 2826 | if(refQ >= 0) |
|---|
| 2827 | { |
|---|
| 2828 | calcptrQ = &calcQ[refQ][cat]; |
|---|
| 2829 | fx1a = tr->rdta->freqa * lp->a; |
|---|
| 2830 | fx1g = tr->rdta->freqg * lp->g; |
|---|
| 2831 | fx1c = tr->rdta->freqc * lp->c; |
|---|
| 2832 | fx1t = tr->rdta->freqt * lp->t; |
|---|
| 2833 | suma = fx1a * calcptrQ->a + fx1c * calcptrQ->c + |
|---|
| 2834 | fx1g * calcptrQ->g + fx1t * calcptrQ->t; |
|---|
| 2835 | fx1r = fx1a + fx1g; |
|---|
| 2836 | fx1y = fx1c + fx1t; |
|---|
| 2837 | sumc = (fx1r + fx1y) * calcptrQ->sumfx2rfx2y; |
|---|
| 2838 | sumb = fx1r * calcptrQ->fx2r * tr->rdta->invfreqr + fx1y * calcptrQ->fx2y * tr->rdta->invfreqy; |
|---|
| 2839 | suma -= sumb; |
|---|
| 2840 | sumb -= sumc; |
|---|
| 2841 | term = log(zz * suma + zv * sumb + sumc) + |
|---|
| 2842 | (lp->exp + calcptrQ->exp)*log(minlikelihood); |
|---|
| 2843 | sum += *wptr++ * term; |
|---|
| 2844 | *log_f++ = term; |
|---|
| 2845 | lp++; |
|---|
| 2846 | lq++; |
|---|
| 2847 | } |
|---|
| 2848 | else |
|---|
| 2849 | { |
|---|
| 2850 | fx1a = tr->rdta->freqa * lp->a; |
|---|
| 2851 | fx1g = tr->rdta->freqg * lp->g; |
|---|
| 2852 | fx1c = tr->rdta->freqc * lp->c; |
|---|
| 2853 | fx1t = tr->rdta->freqt * lp->t; |
|---|
| 2854 | suma = fx1a * lq->a + fx1c * lq->c + fx1g * lq->g + fx1t * lq->t; |
|---|
| 2855 | fx2r = tr->rdta->freqa * lq->a + tr->rdta->freqg * lq->g; |
|---|
| 2856 | fx2y = tr->rdta->freqc * lq->c + tr->rdta->freqt * lq->t; |
|---|
| 2857 | fx1r = fx1a + fx1g; |
|---|
| 2858 | fx1y = fx1c + fx1t; |
|---|
| 2859 | sumc = (fx1r + fx1y) * (fx2r + fx2y); |
|---|
| 2860 | sumb = fx1r * fx2r * tr->rdta->invfreqr + fx1y * fx2y * tr->rdta->invfreqy; |
|---|
| 2861 | suma -= sumb; |
|---|
| 2862 | sumb -= sumc; |
|---|
| 2863 | term = log(zz * suma + zv * sumb + sumc) + |
|---|
| 2864 | (lp->exp + lq->exp)*log(minlikelihood); |
|---|
| 2865 | sum += *wptr++ * term; |
|---|
| 2866 | *log_f++ = term; |
|---|
| 2867 | lp++; |
|---|
| 2868 | lq++; |
|---|
| 2869 | } |
|---|
| 2870 | } |
|---|
| 2871 | } |
|---|
| 2872 | |
|---|
| 2873 | |
|---|
| 2874 | tr->likelihood = sum; |
|---|
| 2875 | return sum; |
|---|
| 2876 | } /* evaluate */ |
|---|
| 2877 | |
|---|
| 2878 | |
|---|
| 2879 | long countEQ = 0, countNEQ = 0; |
|---|
| 2880 | |
|---|
| 2881 | double makenewz (tree *tr, nodeptr p, nodeptr q, double z0, int maxiter) |
|---|
| 2882 | { /* makenewz */ |
|---|
| 2883 | likelivector *lp, *lq; |
|---|
| 2884 | double *abi, *bci, *sumci, *abptr, *bcptr, *sumcptr; |
|---|
| 2885 | double dlnLidlz, dlnLdlz, d2lnLdlz2, z, zprev, zstep, lz, xvlz, |
|---|
| 2886 | ki, suma, sumb, sumc, ab, bc, inv_Li, t1, t2, |
|---|
| 2887 | fx1a, fx1c, fx1g, fx1t, fx1r, fx1y, fx2r, fx2y; |
|---|
| 2888 | double *wrptr, *wr2ptr, *rptr; |
|---|
| 2889 | int i, curvatOK, j , cat, *cptr; |
|---|
| 2890 | memP calcP[EQUALITIES][maxcategories], *calcptrP; |
|---|
| 2891 | memQ calcQ[EQUALITIES][maxcategories], *calcptrQ; |
|---|
| 2892 | homType *mlvP, *mlvQ; |
|---|
| 2893 | register char *sQ; |
|---|
| 2894 | register char *sP; |
|---|
| 2895 | nodeptr realQ, realP; |
|---|
| 2896 | long refQ, refP; |
|---|
| 2897 | double zztable[maxcategories+1], zvtable[maxcategories+1], |
|---|
| 2898 | *zzptr, *zvptr; |
|---|
| 2899 | |
|---|
| 2900 | |
|---|
| 2901 | while ((! p->x) || (! q->x)) { |
|---|
| 2902 | if (! (p->x)) if (! newviewOptimized(tr, p)) return badZ; |
|---|
| 2903 | if (! (q->x)) if (! newviewOptimized(tr, q)) return badZ; |
|---|
| 2904 | } |
|---|
| 2905 | |
|---|
| 2906 | realQ = tr->nodep[q->number]; |
|---|
| 2907 | realP = tr->nodep[p->number]; |
|---|
| 2908 | sQ = realQ->equalityVector; |
|---|
| 2909 | sP = realP->equalityVector; |
|---|
| 2910 | lp = p->x->lv; |
|---|
| 2911 | lq = q->x->lv; |
|---|
| 2912 | |
|---|
| 2913 | |
|---|
| 2914 | |
|---|
| 2915 | { |
|---|
| 2916 | unsigned scratch_size; |
|---|
| 2917 | scratch_size = sizeof(double) * tr->cdta->endsite; |
|---|
| 2918 | if ((abi = (double *) Malloc(scratch_size)) && |
|---|
| 2919 | (bci = (double *) Malloc(scratch_size)) && |
|---|
| 2920 | (sumci = (double *) Malloc(scratch_size))) ; |
|---|
| 2921 | else { |
|---|
| 2922 | printf("ERROR: makenewz unable to obtain space for arrays\n"); |
|---|
| 2923 | return badZ; |
|---|
| 2924 | } |
|---|
| 2925 | |
|---|
| 2926 | } |
|---|
| 2927 | abptr = abi; |
|---|
| 2928 | bcptr = bci; |
|---|
| 2929 | sumcptr = sumci; |
|---|
| 2930 | |
|---|
| 2931 | # ifdef Vectorize |
|---|
| 2932 | # pragma IVDEP |
|---|
| 2933 | # endif |
|---|
| 2934 | |
|---|
| 2935 | |
|---|
| 2936 | |
|---|
| 2937 | for (j = 1; j <= tr->rdta->categs; j++) |
|---|
| 2938 | { |
|---|
| 2939 | for(i = 0; i < equalities; i++) |
|---|
| 2940 | { |
|---|
| 2941 | mlvP = &realP->mlv[i][j]; |
|---|
| 2942 | if(mlvP->set) |
|---|
| 2943 | { |
|---|
| 2944 | calcptrP = &calcP[i][j]; |
|---|
| 2945 | calcptrP->fx1a = tr->rdta->freqa * mlvP->a; |
|---|
| 2946 | calcptrP->fx1g = tr->rdta->freqg * mlvP->g; |
|---|
| 2947 | calcptrP->fx1c = tr->rdta->freqc * mlvP->c; |
|---|
| 2948 | calcptrP->fx1t = tr->rdta->freqt * mlvP->t; |
|---|
| 2949 | calcptrP->sumag = calcptrP->fx1a + calcptrP->fx1g; |
|---|
| 2950 | calcptrP->sumct = calcptrP->fx1c + calcptrP->fx1t; |
|---|
| 2951 | calcptrP->sumagct = calcptrP->sumag + calcptrP->sumct; |
|---|
| 2952 | } |
|---|
| 2953 | mlvQ = &realQ->mlv[i][j]; |
|---|
| 2954 | if(mlvQ->set) |
|---|
| 2955 | { |
|---|
| 2956 | calcptrQ = &calcQ[i][j]; |
|---|
| 2957 | calcptrQ->fx2r = tr->rdta->freqa * mlvQ->a + tr->rdta->freqg * mlvQ->g; |
|---|
| 2958 | calcptrQ->fx2y = tr->rdta->freqc * mlvQ->c + tr->rdta->freqt * mlvQ->t; |
|---|
| 2959 | calcptrQ->sumfx2rfx2y = calcptrQ->fx2r + calcptrQ->fx2y; |
|---|
| 2960 | calcptrQ->a = mlvQ->a; |
|---|
| 2961 | calcptrQ->c = mlvQ->c; |
|---|
| 2962 | calcptrQ->g = mlvQ->g; |
|---|
| 2963 | calcptrQ->t = mlvQ->t; |
|---|
| 2964 | } |
|---|
| 2965 | } |
|---|
| 2966 | } |
|---|
| 2967 | cptr = &(tr->cdta->patcat[0]); |
|---|
| 2968 | |
|---|
| 2969 | for (i = 0; i < tr->cdta->endsite; i++) { |
|---|
| 2970 | refQ = *sQ++; |
|---|
| 2971 | refP = *sP++; |
|---|
| 2972 | |
|---|
| 2973 | cat = *cptr++; |
|---|
| 2974 | |
|---|
| 2975 | if(refP >= 0) |
|---|
| 2976 | { |
|---|
| 2977 | calcptrP = &calcP[refP][cat]; |
|---|
| 2978 | |
|---|
| 2979 | if(refQ >= 0) |
|---|
| 2980 | { |
|---|
| 2981 | |
|---|
| 2982 | calcptrQ = &calcQ[refQ][cat]; |
|---|
| 2983 | suma = calcptrP->fx1a * calcptrQ->a + calcptrP->fx1c * calcptrQ->c + |
|---|
| 2984 | calcptrP->fx1g * calcptrQ->g + calcptrP->fx1t * calcptrQ->t; |
|---|
| 2985 | |
|---|
| 2986 | |
|---|
| 2987 | *sumcptr++ = sumc = calcptrP->sumagct * calcptrQ->sumfx2rfx2y; |
|---|
| 2988 | sumb = calcptrP->sumag * calcptrQ->fx2r * tr->rdta->invfreqr + calcptrP->sumct * calcptrQ->fx2y * tr->rdta->invfreqy; |
|---|
| 2989 | *abptr++ = suma - sumb; |
|---|
| 2990 | *bcptr++ = sumb - sumc; |
|---|
| 2991 | lp++; |
|---|
| 2992 | lq++; |
|---|
| 2993 | } |
|---|
| 2994 | else |
|---|
| 2995 | { |
|---|
| 2996 | suma = calcptrP->fx1a * lq->a + calcptrP->fx1c * lq->c + |
|---|
| 2997 | calcptrP->fx1g * lq->g + calcptrP->fx1t * lq->t; |
|---|
| 2998 | |
|---|
| 2999 | fx2r = tr->rdta->freqa * lq->a + tr->rdta->freqg * lq->g; |
|---|
| 3000 | fx2y = tr->rdta->freqc * lq->c + tr->rdta->freqt * lq->t; |
|---|
| 3001 | |
|---|
| 3002 | *sumcptr++ = sumc = calcptrP->sumagct * (fx2r + fx2y); |
|---|
| 3003 | sumb = calcptrP->sumag * fx2r * tr->rdta->invfreqr + calcptrP->sumct * fx2y * tr->rdta->invfreqy ; |
|---|
| 3004 | *abptr++ = suma - sumb; |
|---|
| 3005 | *bcptr++ = sumb - sumc; |
|---|
| 3006 | lp++; |
|---|
| 3007 | lq++; |
|---|
| 3008 | } |
|---|
| 3009 | } |
|---|
| 3010 | else |
|---|
| 3011 | { |
|---|
| 3012 | if(refQ >= 0) |
|---|
| 3013 | { |
|---|
| 3014 | calcptrQ = &calcQ[refQ][cat]; |
|---|
| 3015 | fx1a = tr->rdta->freqa * lp->a; |
|---|
| 3016 | fx1g = tr->rdta->freqg * lp->g; |
|---|
| 3017 | fx1c = tr->rdta->freqc * lp->c; |
|---|
| 3018 | fx1t = tr->rdta->freqt * lp->t; |
|---|
| 3019 | suma = fx1a * calcptrQ->a + fx1c * calcptrQ->c + |
|---|
| 3020 | fx1g * calcptrQ->g + fx1t * calcptrQ->t; |
|---|
| 3021 | fx1r = fx1a + fx1g; |
|---|
| 3022 | fx1y = fx1c + fx1t; |
|---|
| 3023 | *sumcptr++ = sumc = (fx1r + fx1y) * calcptrQ->sumfx2rfx2y; |
|---|
| 3024 | sumb = fx1r * calcptrQ->fx2r * tr->rdta->invfreqr + fx1y * calcptrQ->fx2y * tr->rdta->invfreqy; |
|---|
| 3025 | *abptr++ = suma - sumb; |
|---|
| 3026 | *bcptr++ = sumb - sumc; |
|---|
| 3027 | lp++; |
|---|
| 3028 | lq++; |
|---|
| 3029 | } |
|---|
| 3030 | else |
|---|
| 3031 | { |
|---|
| 3032 | |
|---|
| 3033 | fx1a = tr->rdta->freqa * lp->a; |
|---|
| 3034 | fx1g = tr->rdta->freqg * lp->g; |
|---|
| 3035 | fx1c = tr->rdta->freqc * lp->c; |
|---|
| 3036 | fx1t = tr->rdta->freqt * lp->t; |
|---|
| 3037 | suma = fx1a * lq->a + fx1c * lq->c + fx1g * lq->g + fx1t * lq->t; |
|---|
| 3038 | fx2r = tr->rdta->freqa * lq->a + tr->rdta->freqg * lq->g; |
|---|
| 3039 | //nur von q abh |
|---|
| 3040 | fx2y = tr->rdta->freqc * lq->c + tr->rdta->freqt * lq->t; |
|---|
| 3041 | //nur von q abh |
|---|
| 3042 | fx1r = fx1a + fx1g; |
|---|
| 3043 | fx1y = fx1c + fx1t; |
|---|
| 3044 | *sumcptr++ = sumc = (fx1r + fx1y) * (fx2r + fx2y); |
|---|
| 3045 | //zweiter term nur von q abh |
|---|
| 3046 | sumb = fx1r * fx2r * tr->rdta->invfreqr + fx1y * fx2y * tr->rdta->invfreqy; |
|---|
| 3047 | |
|---|
| 3048 | |
|---|
| 3049 | |
|---|
| 3050 | *abptr++ = suma - sumb; |
|---|
| 3051 | *bcptr++ = sumb - sumc; |
|---|
| 3052 | lp++; |
|---|
| 3053 | lq++; |
|---|
| 3054 | } |
|---|
| 3055 | } |
|---|
| 3056 | |
|---|
| 3057 | } |
|---|
| 3058 | |
|---|
| 3059 | z = z0; |
|---|
| 3060 | do { |
|---|
| 3061 | zprev = z; |
|---|
| 3062 | zstep = (1.0 - zmax) * z + zmin; |
|---|
| 3063 | curvatOK = FALSE; |
|---|
| 3064 | |
|---|
| 3065 | do { |
|---|
| 3066 | if (z < zmin) z = zmin; |
|---|
| 3067 | else if (z > zmax) z = zmax; |
|---|
| 3068 | |
|---|
| 3069 | lz = log(z); |
|---|
| 3070 | xvlz = tr->rdta->xv * lz; |
|---|
| 3071 | rptr = &(tr->rdta->catrat[1]); |
|---|
| 3072 | zzptr = &(zztable[1]); |
|---|
| 3073 | zvptr = &(zvtable[1]); |
|---|
| 3074 | |
|---|
| 3075 | # ifdef Vectorize |
|---|
| 3076 | # pragma IVDEP |
|---|
| 3077 | # endif |
|---|
| 3078 | |
|---|
| 3079 | for (i = 1; i <= tr->rdta->categs; i++) { |
|---|
| 3080 | ki = *rptr++; |
|---|
| 3081 | *zzptr++ = exp(ki * lz); |
|---|
| 3082 | *zvptr++ = exp(ki * xvlz); |
|---|
| 3083 | } |
|---|
| 3084 | |
|---|
| 3085 | abptr = abi; |
|---|
| 3086 | bcptr = bci; |
|---|
| 3087 | sumcptr = sumci; |
|---|
| 3088 | cptr = &(tr->cdta->patcat[0]); |
|---|
| 3089 | wrptr = &(tr->cdta->wr[0]); |
|---|
| 3090 | wr2ptr = &(tr->cdta->wr2[0]); |
|---|
| 3091 | dlnLdlz = 0.0; /* = d(ln(likelihood))/d(lz) */ |
|---|
| 3092 | d2lnLdlz2 = 0.0; /* = d2(ln(likelihood))/d(lz)2 */ |
|---|
| 3093 | |
|---|
| 3094 | # ifdef Vectorize |
|---|
| 3095 | # pragma IVDEP |
|---|
| 3096 | # endif |
|---|
| 3097 | |
|---|
| 3098 | for (i = 0; i < tr->cdta->endsite; i++) { |
|---|
| 3099 | cat = *cptr++; /* ratecategory(i) */ |
|---|
| 3100 | ab = *abptr++ * zztable[cat]; |
|---|
| 3101 | bc = *bcptr++ * zvtable[cat]; |
|---|
| 3102 | sumc = *sumcptr++; |
|---|
| 3103 | inv_Li = 1.0/(ab + bc + sumc); |
|---|
| 3104 | t1 = ab * inv_Li; |
|---|
| 3105 | t2 = tr->rdta->xv * bc * inv_Li; |
|---|
| 3106 | dlnLidlz = t1 + t2; |
|---|
| 3107 | dlnLdlz += *wrptr++ * dlnLidlz; |
|---|
| 3108 | d2lnLdlz2 += *wr2ptr++ * (t1 + tr->rdta->xv * t2 - dlnLidlz * dlnLidlz); |
|---|
| 3109 | } |
|---|
| 3110 | |
|---|
| 3111 | if ((d2lnLdlz2 >= 0.0) && (z < zmax)) |
|---|
| 3112 | zprev = z = 0.37 * z + 0.63; /* Bad curvature, shorten branch */ |
|---|
| 3113 | else |
|---|
| 3114 | curvatOK = TRUE; |
|---|
| 3115 | |
|---|
| 3116 | } while (! curvatOK); |
|---|
| 3117 | |
|---|
| 3118 | if (d2lnLdlz2 < 0.0) { |
|---|
| 3119 | z *= exp(-dlnLdlz / d2lnLdlz2); |
|---|
| 3120 | if (z < zmin) z = zmin; |
|---|
| 3121 | if (z > 0.25 * zprev + 0.75) /* Limit steps toward z = 1.0 */ |
|---|
| 3122 | z = 0.25 * zprev + 0.75; |
|---|
| 3123 | } |
|---|
| 3124 | |
|---|
| 3125 | if (z > zmax) z = zmax; |
|---|
| 3126 | |
|---|
| 3127 | } while ((--maxiter > 0) && (ABS(z - zprev) > zstep)); |
|---|
| 3128 | |
|---|
| 3129 | |
|---|
| 3130 | Free(abi); |
|---|
| 3131 | Free(bci); |
|---|
| 3132 | Free(sumci); |
|---|
| 3133 | |
|---|
| 3134 | |
|---|
| 3135 | |
|---|
| 3136 | return z; |
|---|
| 3137 | } /* makenewz */ |
|---|
| 3138 | |
|---|
| 3139 | |
|---|
| 3140 | boolean update (tree *tr, nodeptr p) |
|---|
| 3141 | { /* update */ |
|---|
| 3142 | nodeptr q; |
|---|
| 3143 | double z0, z; |
|---|
| 3144 | |
|---|
| 3145 | q = p->back; |
|---|
| 3146 | z0 = q->z; |
|---|
| 3147 | if ((z = makenewz(tr, p, q, z0, newzpercycle)) == badZ) return FALSE; |
|---|
| 3148 | p->z = q->z = z; |
|---|
| 3149 | if (ABS(z - z0) > deltaz) tr->smoothed = FALSE; |
|---|
| 3150 | return TRUE; |
|---|
| 3151 | } /* update */ |
|---|
| 3152 | |
|---|
| 3153 | |
|---|
| 3154 | boolean smooth (tree *tr, nodeptr p) |
|---|
| 3155 | { /* smooth */ |
|---|
| 3156 | nodeptr q; |
|---|
| 3157 | |
|---|
| 3158 | if (! update(tr, p)) return FALSE; /* Adjust branch */ |
|---|
| 3159 | if (! p->tip) { /* Adjust descendants */ |
|---|
| 3160 | q = p->next; |
|---|
| 3161 | while (q != p) { |
|---|
| 3162 | if (! smooth(tr, q->back)) return FALSE; |
|---|
| 3163 | q = q->next; |
|---|
| 3164 | } |
|---|
| 3165 | |
|---|
| 3166 | # if ReturnSmoothedView |
|---|
| 3167 | |
|---|
| 3168 | if (! newviewOptimized(tr, p)) return FALSE; |
|---|
| 3169 | # endif |
|---|
| 3170 | } |
|---|
| 3171 | |
|---|
| 3172 | return TRUE; |
|---|
| 3173 | } /* smooth */ |
|---|
| 3174 | |
|---|
| 3175 | |
|---|
| 3176 | boolean smoothTree (tree *tr, int maxtimes) |
|---|
| 3177 | { /* smoothTree */ |
|---|
| 3178 | nodeptr p, q; |
|---|
| 3179 | |
|---|
| 3180 | p = tr->start; |
|---|
| 3181 | |
|---|
| 3182 | while (--maxtimes >= 0) { |
|---|
| 3183 | tr->smoothed = TRUE; |
|---|
| 3184 | if (! smooth(tr, p->back)) return FALSE; |
|---|
| 3185 | if (! p->tip) { |
|---|
| 3186 | q = p->next; |
|---|
| 3187 | while (q != p) { |
|---|
| 3188 | if (! smooth(tr, q->back)) return FALSE; |
|---|
| 3189 | q = q->next; |
|---|
| 3190 | } |
|---|
| 3191 | } |
|---|
| 3192 | if (tr->smoothed) break; |
|---|
| 3193 | } |
|---|
| 3194 | |
|---|
| 3195 | return TRUE; |
|---|
| 3196 | } /* smoothTree */ |
|---|
| 3197 | |
|---|
| 3198 | |
|---|
| 3199 | boolean localSmooth (tree *tr, nodeptr p, int maxtimes) |
|---|
| 3200 | { /* localSmooth -- Smooth branches around p */ |
|---|
| 3201 | nodeptr q; |
|---|
| 3202 | |
|---|
| 3203 | if (p->tip) return FALSE; /* Should be an error */ |
|---|
| 3204 | |
|---|
| 3205 | while (--maxtimes >= 0) { |
|---|
| 3206 | tr->smoothed = TRUE; |
|---|
| 3207 | q = p; |
|---|
| 3208 | do { |
|---|
| 3209 | if (! update(tr, q)) return FALSE; |
|---|
| 3210 | q = q->next; |
|---|
| 3211 | } while (q != p); |
|---|
| 3212 | if (tr->smoothed) break; |
|---|
| 3213 | } |
|---|
| 3214 | |
|---|
| 3215 | tr->smoothed = FALSE; /* Only smooth locally */ |
|---|
| 3216 | return TRUE; |
|---|
| 3217 | } /* localSmooth */ |
|---|
| 3218 | |
|---|
| 3219 | |
|---|
| 3220 | void hookup (nodeptr p, nodeptr q, double z) |
|---|
| 3221 | { /* hookup */ |
|---|
| 3222 | p->back = q; |
|---|
| 3223 | q->back = p; |
|---|
| 3224 | p->z = q->z = z; |
|---|
| 3225 | } /* hookup */ |
|---|
| 3226 | |
|---|
| 3227 | |
|---|
| 3228 | /* Insert node p into branch q <-> q->back */ |
|---|
| 3229 | |
|---|
| 3230 | |
|---|
| 3231 | boolean insert (tree *tr, nodeptr p, nodeptr q, boolean glob) |
|---|
| 3232 | /* glob -- Smooth tree globally? */ |
|---|
| 3233 | |
|---|
| 3234 | /* q |
|---|
| 3235 | /. |
|---|
| 3236 | add/ . |
|---|
| 3237 | / . |
|---|
| 3238 | pn . |
|---|
| 3239 | s ---- p .remove |
|---|
| 3240 | pnn . |
|---|
| 3241 | \ . |
|---|
| 3242 | add\ . |
|---|
| 3243 | \. pn = p->next; |
|---|
| 3244 | r pnn = p->next->next; |
|---|
| 3245 | */ |
|---|
| 3246 | |
|---|
| 3247 | { /* insert */ |
|---|
| 3248 | nodeptr r, s; |
|---|
| 3249 | |
|---|
| 3250 | r = q->back; |
|---|
| 3251 | s = p->back; |
|---|
| 3252 | |
|---|
| 3253 | # if BestInsertAverage && ! Master |
|---|
| 3254 | { double zqr, zqs, zrs, lzqr, lzqs, lzrs, lzsum, lzq, lzr, lzs, lzmax; |
|---|
| 3255 | |
|---|
| 3256 | if ((zqr = makenewz(tr, q, r, q->z, iterations)) == badZ) return FALSE; |
|---|
| 3257 | if ((zqs = makenewz(tr, q, s, defaultz, iterations)) == badZ) return FALSE; |
|---|
| 3258 | if ((zrs = makenewz(tr, r, s, defaultz, iterations)) == badZ) return FALSE; |
|---|
| 3259 | |
|---|
| 3260 | |
|---|
| 3261 | |
|---|
| 3262 | |
|---|
| 3263 | lzqr = (zqr > zmin) ? log(zqr) : log(zmin); /* long branches */ |
|---|
| 3264 | lzqs = (zqs > zmin) ? log(zqs) : log(zmin); |
|---|
| 3265 | lzrs = (zrs > zmin) ? log(zrs) : log(zmin); |
|---|
| 3266 | lzsum = 0.5 * (lzqr + lzqs + lzrs); |
|---|
| 3267 | |
|---|
| 3268 | lzq = lzsum - lzrs; |
|---|
| 3269 | lzr = lzsum - lzqs; |
|---|
| 3270 | lzs = lzsum - lzqr; |
|---|
| 3271 | lzmax = log(zmax); |
|---|
| 3272 | |
|---|
| 3273 | if (lzq > lzmax) {lzq = lzmax; lzr = lzqr; lzs = lzqs;} /* short */ |
|---|
| 3274 | else if (lzr > lzmax) {lzr = lzmax; lzq = lzqr; lzs = lzrs;} |
|---|
| 3275 | else if (lzs > lzmax) {lzs = lzmax; lzq = lzqs; lzr = lzrs;} |
|---|
| 3276 | |
|---|
| 3277 | hookup(p->next, q, exp(lzq)); |
|---|
| 3278 | hookup(p->next->next, r, exp(lzr)); |
|---|
| 3279 | hookup(p, s, exp(lzs)); |
|---|
| 3280 | } |
|---|
| 3281 | |
|---|
| 3282 | # else |
|---|
| 3283 | { double z; |
|---|
| 3284 | z = sqrt(q->z); |
|---|
| 3285 | hookup(p->next, q, z); |
|---|
| 3286 | hookup(p->next->next, r, z); |
|---|
| 3287 | } |
|---|
| 3288 | |
|---|
| 3289 | # endif |
|---|
| 3290 | |
|---|
| 3291 | |
|---|
| 3292 | if (! newviewOptimized(tr, p)) return FALSE; |
|---|
| 3293 | |
|---|
| 3294 | |
|---|
| 3295 | |
|---|
| 3296 | tr->opt_level = 0; |
|---|
| 3297 | |
|---|
| 3298 | # if ! Master /* Smoothings are done by slave */ |
|---|
| 3299 | if (glob) { /* Smooth whole tree */ |
|---|
| 3300 | if (! smoothTree(tr, smoothings)) return FALSE; |
|---|
| 3301 | } |
|---|
| 3302 | else { /* Smooth locale of p */ |
|---|
| 3303 | if (! localSmooth(tr, p, smoothings)) return FALSE; |
|---|
| 3304 | } |
|---|
| 3305 | |
|---|
| 3306 | # else |
|---|
| 3307 | tr->likelihood = unlikely; |
|---|
| 3308 | # endif |
|---|
| 3309 | |
|---|
| 3310 | return TRUE; |
|---|
| 3311 | } /* insert */ |
|---|
| 3312 | |
|---|
| 3313 | |
|---|
| 3314 | nodeptr removeNode (tree *tr, nodeptr p) |
|---|
| 3315 | |
|---|
| 3316 | /* q |
|---|
| 3317 | .| |
|---|
| 3318 | remove. | |
|---|
| 3319 | . | |
|---|
| 3320 | pn | |
|---|
| 3321 | s ---- p |add |
|---|
| 3322 | pnn | |
|---|
| 3323 | . | |
|---|
| 3324 | remove. | |
|---|
| 3325 | .| pn = p->next; |
|---|
| 3326 | r pnn = p->next->next; |
|---|
| 3327 | */ |
|---|
| 3328 | |
|---|
| 3329 | /* remove p and return where it was */ |
|---|
| 3330 | { /* removeNode */ |
|---|
| 3331 | double zqr; |
|---|
| 3332 | nodeptr q, r; |
|---|
| 3333 | |
|---|
| 3334 | q = p->next->back; |
|---|
| 3335 | r = p->next->next->back; |
|---|
| 3336 | zqr = q->z * r->z; |
|---|
| 3337 | # if ! Master |
|---|
| 3338 | if ((zqr = makenewz(tr, q, r, zqr, iterations)) == badZ) return (node *) NULL; |
|---|
| 3339 | # endif |
|---|
| 3340 | hookup(q, r, zqr); |
|---|
| 3341 | |
|---|
| 3342 | p->next->next->back = p->next->back = (node *) NULL; |
|---|
| 3343 | return q; |
|---|
| 3344 | } /* removeNode */ |
|---|
| 3345 | |
|---|
| 3346 | |
|---|
| 3347 | boolean initrav (tree *tr, nodeptr p) |
|---|
| 3348 | { /* initrav */ |
|---|
| 3349 | nodeptr q; |
|---|
| 3350 | |
|---|
| 3351 | if (! p->tip) { |
|---|
| 3352 | q = p->next; |
|---|
| 3353 | do { |
|---|
| 3354 | if (! initrav(tr, q->back)) return FALSE; |
|---|
| 3355 | q = q->next; |
|---|
| 3356 | } while (q != p); |
|---|
| 3357 | |
|---|
| 3358 | if (! newviewOptimized(tr, p)) return FALSE; |
|---|
| 3359 | |
|---|
| 3360 | } |
|---|
| 3361 | |
|---|
| 3362 | return TRUE; |
|---|
| 3363 | } /* initrav */ |
|---|
| 3364 | |
|---|
| 3365 | |
|---|
| 3366 | nodeptr buildNewTip (tree *tr, nodeptr p) |
|---|
| 3367 | { /* buildNewTip */ |
|---|
| 3368 | nodeptr q; |
|---|
| 3369 | |
|---|
| 3370 | q = tr->nodep[(tr->nextnode)++]; |
|---|
| 3371 | hookup(p, q, defaultz); |
|---|
| 3372 | return q; |
|---|
| 3373 | } /* buildNewTip */ |
|---|
| 3374 | |
|---|
| 3375 | |
|---|
| 3376 | boolean buildSimpleTree (tree *tr, int ip, int iq, int ir) |
|---|
| 3377 | { /* buildSimpleTree */ |
|---|
| 3378 | /* p, q and r are tips meeting at s */ |
|---|
| 3379 | nodeptr p, s; |
|---|
| 3380 | int i; |
|---|
| 3381 | |
|---|
| 3382 | i = MIN(ip, iq); |
|---|
| 3383 | if (ir < i) i = ir; |
|---|
| 3384 | tr->start = tr->nodep[i]; |
|---|
| 3385 | tr->ntips = 3; |
|---|
| 3386 | p = tr->nodep[ip]; |
|---|
| 3387 | hookup(p, tr->nodep[iq], defaultz); |
|---|
| 3388 | s = buildNewTip(tr, tr->nodep[ir]); |
|---|
| 3389 | |
|---|
| 3390 | return insert(tr, s, p, FALSE); /* Smoothing is local to s */ |
|---|
| 3391 | } /* buildSimpleTree */ |
|---|
| 3392 | |
|---|
| 3393 | #if 0 |
|---|
| 3394 | char * strchr (char *str, int chr) |
|---|
| 3395 | { /* strchr */ |
|---|
| 3396 | int c; |
|---|
| 3397 | |
|---|
| 3398 | while (c = *str) {if (c == chr) return str; str++;} |
|---|
| 3399 | return (char *) NULL; |
|---|
| 3400 | } /* strchr */ |
|---|
| 3401 | |
|---|
| 3402 | |
|---|
| 3403 | char * strstr (char *str1, char *str2) |
|---|
| 3404 | { /* strstr */ |
|---|
| 3405 | char *s1, *s2; |
|---|
| 3406 | int c; |
|---|
| 3407 | |
|---|
| 3408 | while (*(s1 = str1)) { |
|---|
| 3409 | s2 = str2; |
|---|
| 3410 | do { |
|---|
| 3411 | if (! (c = *s2++)) return str1; |
|---|
| 3412 | } |
|---|
| 3413 | while (*s1++ == c); |
|---|
| 3414 | str1++; |
|---|
| 3415 | } |
|---|
| 3416 | return (char *) NULL; |
|---|
| 3417 | } /* strstr */ |
|---|
| 3418 | #endif |
|---|
| 3419 | |
|---|
| 3420 | boolean readKeyValue (char *string, const char *key, const char *format, void *value) |
|---|
| 3421 | { /* readKeyValue */ |
|---|
| 3422 | |
|---|
| 3423 | if (! (string = strstr(string, key))) return FALSE; |
|---|
| 3424 | string += strlen(key); |
|---|
| 3425 | if (! (string = strchr(string, '='))) return FALSE; |
|---|
| 3426 | string++; |
|---|
| 3427 | return sscanf(string, format, value); /* 1 if read, otherwise 0 */ |
|---|
| 3428 | } /* readKeyValue */ |
|---|
| 3429 | |
|---|
| 3430 | |
|---|
| 3431 | #if Master || Slave |
|---|
| 3432 | |
|---|
| 3433 | double str_readTreeLikelihood (char *treestr) |
|---|
| 3434 | { /* str_readTreeLikelihood */ |
|---|
| 3435 | double lk1; |
|---|
| 3436 | char *com, *com_end; |
|---|
| 3437 | boolean readKeyValue(); |
|---|
| 3438 | |
|---|
| 3439 | if ((com = strchr(treestr, '[')) && (com < strchr(treestr, '(')) |
|---|
| 3440 | && (com_end = strchr(com, ']'))) { |
|---|
| 3441 | com++; |
|---|
| 3442 | *com_end = 0; |
|---|
| 3443 | if (readKeyValue(com, likelihood_key, "%lg", (void *) &(lk1))) { |
|---|
| 3444 | *com_end = ']'; |
|---|
| 3445 | return lk1; |
|---|
| 3446 | } |
|---|
| 3447 | } |
|---|
| 3448 | |
|---|
| 3449 | fprintf(stderr, "ERROR reading likelihood in receiveTree\n"); |
|---|
| 3450 | return badEval; |
|---|
| 3451 | } /* str_readTreeLikelihood */ |
|---|
| 3452 | |
|---|
| 3453 | |
|---|
| 3454 | boolean sendTree (comm_block *comm, tree *tr) |
|---|
| 3455 | { /* sendTree */ |
|---|
| 3456 | char *treestr; |
|---|
| 3457 | char *treeString(); |
|---|
| 3458 | # if Master |
|---|
| 3459 | void sendTreeNum(); |
|---|
| 3460 | # endif |
|---|
| 3461 | |
|---|
| 3462 | comm->done_flag = tr->likelihood > 0.0; |
|---|
| 3463 | if (comm->done_flag) |
|---|
| 3464 | write_comm_msg(comm, NULL); |
|---|
| 3465 | |
|---|
| 3466 | else { |
|---|
| 3467 | treestr = (char *) Malloc((tr->ntips * (nmlngth+32)) + 256); |
|---|
| 3468 | if (! treestr) { |
|---|
| 3469 | fprintf(stderr, "sendTree: Malloc failure\n"); |
|---|
| 3470 | return 0; |
|---|
| 3471 | } |
|---|
| 3472 | |
|---|
| 3473 | # if Master |
|---|
| 3474 | if (send_ahead >= MAX_SEND_AHEAD) { |
|---|
| 3475 | double new_likelihood; |
|---|
| 3476 | int n_to_get; |
|---|
| 3477 | |
|---|
| 3478 | n_to_get = (send_ahead+1)/2; |
|---|
| 3479 | sendTreeNum(n_to_get); |
|---|
| 3480 | send_ahead -= n_to_get; |
|---|
| 3481 | read_comm_msg(& comm_slave, treestr); |
|---|
| 3482 | new_likelihood = str_readTreeLikelihood(treestr); |
|---|
| 3483 | if (new_likelihood == badEval) return FALSE; |
|---|
| 3484 | if (! best_tr_recv || (new_likelihood > best_lk_recv)) { |
|---|
| 3485 | if (best_tr_recv) Free(best_tr_recv); |
|---|
| 3486 | best_tr_recv = Malloc(strlen(treestr) + 1); |
|---|
| 3487 | strcpy(best_tr_recv, treestr); |
|---|
| 3488 | best_lk_recv = new_likelihood; |
|---|
| 3489 | } |
|---|
| 3490 | } |
|---|
| 3491 | send_ahead++; |
|---|
| 3492 | # endif /* End #if Master */ |
|---|
| 3493 | |
|---|
| 3494 | REPORT_SEND_TREE; |
|---|
| 3495 | (void) treeString(treestr, tr, tr->start->back, 1); |
|---|
| 3496 | write_comm_msg(comm, treestr); |
|---|
| 3497 | |
|---|
| 3498 | Free(treestr); |
|---|
| 3499 | } |
|---|
| 3500 | |
|---|
| 3501 | return TRUE; |
|---|
| 3502 | } /* sendTree */ |
|---|
| 3503 | |
|---|
| 3504 | |
|---|
| 3505 | boolean receiveTree (comm_block *comm, tree *tr) |
|---|
| 3506 | { /* receiveTree */ |
|---|
| 3507 | char *treestr; |
|---|
| 3508 | boolean status; |
|---|
| 3509 | boolean str_treeReadLen(); |
|---|
| 3510 | |
|---|
| 3511 | treestr = (char *) Malloc((tr->ntips * (nmlngth+32)) + 256); |
|---|
| 3512 | if (! treestr) { |
|---|
| 3513 | fprintf(stderr, "receiveTree: Malloc failure\n"); |
|---|
| 3514 | return 0; |
|---|
| 3515 | } |
|---|
| 3516 | |
|---|
| 3517 | read_comm_msg(comm, treestr); |
|---|
| 3518 | if (comm->done_flag) { |
|---|
| 3519 | tr->likelihood = 1.0; |
|---|
| 3520 | status = TRUE; |
|---|
| 3521 | } |
|---|
| 3522 | |
|---|
| 3523 | else { |
|---|
| 3524 | # if Master |
|---|
| 3525 | if (best_tr_recv) { |
|---|
| 3526 | if (str_readTreeLikelihood(treestr) < best_lk_recv) { |
|---|
| 3527 | strcpy(treestr, best_tr_recv); /* Overwrite new tree with best */ |
|---|
| 3528 | } |
|---|
| 3529 | Free(best_tr_recv); |
|---|
| 3530 | best_tr_recv = NULL; |
|---|
| 3531 | } |
|---|
| 3532 | # endif /* End #if Master */ |
|---|
| 3533 | |
|---|
| 3534 | status = str_treeReadLen(treestr, tr); |
|---|
| 3535 | } |
|---|
| 3536 | |
|---|
| 3537 | Free(treestr); |
|---|
| 3538 | return status; |
|---|
| 3539 | } /* receiveTree */ |
|---|
| 3540 | |
|---|
| 3541 | |
|---|
| 3542 | void requestForWork (void) |
|---|
| 3543 | { /* requestForWork */ |
|---|
| 3544 | p4_send(DNAML_REQUEST, DNAML_DISPATCHER_ID, NULL, 0); |
|---|
| 3545 | } /* requestForWork */ |
|---|
| 3546 | #endif /* End #if Master || Slave */ |
|---|
| 3547 | |
|---|
| 3548 | |
|---|
| 3549 | #if Master |
|---|
| 3550 | void sendTreeNum(int n_to_get) |
|---|
| 3551 | { /* sendTreeNum */ |
|---|
| 3552 | char scr[512]; |
|---|
| 3553 | |
|---|
| 3554 | sprintf(scr, "%d", n_to_get); |
|---|
| 3555 | p4_send(DNAML_NUM_TREE, DNAML_MERGER_ID, scr, strlen(scr)+1); |
|---|
| 3556 | } /* sendTreeNum */ |
|---|
| 3557 | |
|---|
| 3558 | |
|---|
| 3559 | boolean getReturnedTrees (tree *tr, bestlist *bt, int n_tree_sent) |
|---|
| 3560 | /* n_tree_sent -- number of trees sent to slaves */ |
|---|
| 3561 | { /* getReturnedTrees */ |
|---|
| 3562 | void sendTreeNum(); |
|---|
| 3563 | boolean receiveTree(); |
|---|
| 3564 | |
|---|
| 3565 | sendTreeNum(send_ahead); |
|---|
| 3566 | send_ahead = 0; |
|---|
| 3567 | |
|---|
| 3568 | if (! receiveTree(& comm_slave, tr)) return FALSE; |
|---|
| 3569 | tr->smoothed = TRUE; |
|---|
| 3570 | (void) saveBestTree(bt, tr); |
|---|
| 3571 | |
|---|
| 3572 | return TRUE; |
|---|
| 3573 | } /* getReturnedTrees */ |
|---|
| 3574 | #endif |
|---|
| 3575 | |
|---|
| 3576 | |
|---|
| 3577 | void cacheZ (tree *tr) |
|---|
| 3578 | { /* cacheZ */ |
|---|
| 3579 | nodeptr p; |
|---|
| 3580 | int nodes; |
|---|
| 3581 | |
|---|
| 3582 | nodes = tr->mxtips + 3 * (tr->mxtips - 2); |
|---|
| 3583 | p = tr->nodep[1]; |
|---|
| 3584 | while (nodes-- > 0) {p->z0 = p->z; p++;} |
|---|
| 3585 | } /* cacheZ */ |
|---|
| 3586 | |
|---|
| 3587 | |
|---|
| 3588 | void restoreZ (tree *tr) |
|---|
| 3589 | { /* restoreZ */ |
|---|
| 3590 | nodeptr p; |
|---|
| 3591 | int nodes; |
|---|
| 3592 | |
|---|
| 3593 | nodes = tr->mxtips + 3 * (tr->mxtips - 2); |
|---|
| 3594 | p = tr->nodep[1]; |
|---|
| 3595 | while (nodes-- > 0) {p->z = p->z0; p++;} |
|---|
| 3596 | } /* restoreZ */ |
|---|
| 3597 | |
|---|
| 3598 | |
|---|
| 3599 | boolean testInsert (tree *tr, nodeptr p, nodeptr q, bestlist *bt, boolean fast) |
|---|
| 3600 | { /* testInsert */ |
|---|
| 3601 | double qz; |
|---|
| 3602 | nodeptr r; |
|---|
| 3603 | |
|---|
| 3604 | r = q->back; /* Save original connection */ |
|---|
| 3605 | qz = q->z; |
|---|
| 3606 | if (! insert(tr, p, q, ! fast)) return FALSE; |
|---|
| 3607 | |
|---|
| 3608 | # if ! Master |
|---|
| 3609 | if (evaluate(tr, fast ? p->next->next : tr->start) == badEval) return FALSE; |
|---|
| 3610 | (void) saveBestTree(bt, tr); |
|---|
| 3611 | # else /* Master */ |
|---|
| 3612 | tr->likelihood = unlikely; |
|---|
| 3613 | if (! sendTree(& comm_slave, tr)) return FALSE; |
|---|
| 3614 | # endif |
|---|
| 3615 | |
|---|
| 3616 | /* remove p from this branch */ |
|---|
| 3617 | |
|---|
| 3618 | hookup(q, r, qz); |
|---|
| 3619 | p->next->next->back = p->next->back = (nodeptr) NULL; |
|---|
| 3620 | if (! fast) { /* With fast add, other values are still OK */ |
|---|
| 3621 | restoreZ(tr); /* Restore branch lengths */ |
|---|
| 3622 | # if ! Master /* Regenerate x values */ |
|---|
| 3623 | if (! initrav(tr, p->back)) return FALSE; |
|---|
| 3624 | if (! initrav(tr, q)) return FALSE; |
|---|
| 3625 | if (! initrav(tr, r)) return FALSE; |
|---|
| 3626 | # endif |
|---|
| 3627 | } |
|---|
| 3628 | |
|---|
| 3629 | return TRUE; |
|---|
| 3630 | } /* testInsert */ |
|---|
| 3631 | |
|---|
| 3632 | |
|---|
| 3633 | int addTraverse (tree *tr, nodeptr p, nodeptr q, |
|---|
| 3634 | int mintrav, int maxtrav, bestlist *bt, boolean fast) |
|---|
| 3635 | { /* addTraverse */ |
|---|
| 3636 | int tested, newtested; |
|---|
| 3637 | |
|---|
| 3638 | tested = 0; |
|---|
| 3639 | if (--mintrav <= 0) { /* Moved minimum distance? */ |
|---|
| 3640 | if (! testInsert(tr, p, q, bt, fast)) return badRear; |
|---|
| 3641 | tested++; |
|---|
| 3642 | } |
|---|
| 3643 | |
|---|
| 3644 | if ((! q->tip) && (--maxtrav > 0)) { /* Continue traverse? */ |
|---|
| 3645 | newtested = addTraverse(tr, p, q->next->back, |
|---|
| 3646 | mintrav, maxtrav, bt, fast); |
|---|
| 3647 | if (newtested == badRear) return badRear; |
|---|
| 3648 | tested += newtested; |
|---|
| 3649 | newtested = addTraverse(tr, p, q->next->next->back, |
|---|
| 3650 | mintrav, maxtrav, bt, fast); |
|---|
| 3651 | if (newtested == badRear) return badRear; |
|---|
| 3652 | tested += newtested; |
|---|
| 3653 | } |
|---|
| 3654 | |
|---|
| 3655 | return tested; |
|---|
| 3656 | } /* addTraverse */ |
|---|
| 3657 | |
|---|
| 3658 | |
|---|
| 3659 | int rearrange (tree *tr, nodeptr p, int mintrav, int maxtrav, bestlist *bt) |
|---|
| 3660 | /* rearranges the tree, globally or locally */ |
|---|
| 3661 | { /* rearrange */ |
|---|
| 3662 | double p1z, p2z, q1z, q2z; |
|---|
| 3663 | nodeptr p1, p2, q, q1, q2; |
|---|
| 3664 | int tested, mintrav2, newtested; |
|---|
| 3665 | |
|---|
| 3666 | tested = 0; |
|---|
| 3667 | if (maxtrav < 1 || mintrav > maxtrav) return tested; |
|---|
| 3668 | |
|---|
| 3669 | /* Moving subtree forward in tree. */ |
|---|
| 3670 | |
|---|
| 3671 | if (! p->tip) { |
|---|
| 3672 | p1 = p->next->back; |
|---|
| 3673 | p2 = p->next->next->back; |
|---|
| 3674 | if (! p1->tip || ! p2->tip) { |
|---|
| 3675 | p1z = p1->z; |
|---|
| 3676 | p2z = p2->z; |
|---|
| 3677 | if (! removeNode(tr, p)) return badRear; |
|---|
| 3678 | cacheZ(tr); |
|---|
| 3679 | if (! p1->tip) { |
|---|
| 3680 | newtested = addTraverse(tr, p, p1->next->back, |
|---|
| 3681 | mintrav, maxtrav, bt, FALSE); |
|---|
| 3682 | if (newtested == badRear) return badRear; |
|---|
| 3683 | tested += newtested; |
|---|
| 3684 | newtested = addTraverse(tr, p, p1->next->next->back, |
|---|
| 3685 | mintrav, maxtrav, bt, FALSE); |
|---|
| 3686 | if (newtested == badRear) return badRear; |
|---|
| 3687 | tested += newtested; |
|---|
| 3688 | } |
|---|
| 3689 | |
|---|
| 3690 | if (! p2->tip) { |
|---|
| 3691 | newtested = addTraverse(tr, p, p2->next->back, |
|---|
| 3692 | mintrav, maxtrav, bt, FALSE); |
|---|
| 3693 | if (newtested == badRear) return badRear; |
|---|
| 3694 | tested += newtested; |
|---|
| 3695 | newtested = addTraverse(tr, p, p2->next->next->back, |
|---|
| 3696 | mintrav, maxtrav, bt, FALSE); |
|---|
| 3697 | if (newtested == badRear) return badRear; |
|---|
| 3698 | tested += newtested; |
|---|
| 3699 | } |
|---|
| 3700 | |
|---|
| 3701 | hookup(p->next, p1, p1z); /* Restore original tree */ |
|---|
| 3702 | hookup(p->next->next, p2, p2z); |
|---|
| 3703 | if (! (initrav(tr, tr->start) |
|---|
| 3704 | && initrav(tr, tr->start->back))) return badRear; |
|---|
| 3705 | } |
|---|
| 3706 | } /* if (! p->tip) */ |
|---|
| 3707 | |
|---|
| 3708 | /* Moving subtree backward in tree. Minimum move is 2 to avoid duplicates */ |
|---|
| 3709 | |
|---|
| 3710 | q = p->back; |
|---|
| 3711 | if (! q->tip && maxtrav > 1) { |
|---|
| 3712 | q1 = q->next->back; |
|---|
| 3713 | q2 = q->next->next->back; |
|---|
| 3714 | if (! q1->tip && (!q1->next->back->tip || !q1->next->next->back->tip) || |
|---|
| 3715 | ! q2->tip && (!q2->next->back->tip || !q2->next->next->back->tip)) { |
|---|
| 3716 | q1z = q1->z; |
|---|
| 3717 | q2z = q2->z; |
|---|
| 3718 | if (! removeNode(tr, q)) return badRear; |
|---|
| 3719 | cacheZ(tr); |
|---|
| 3720 | mintrav2 = mintrav > 2 ? mintrav : 2; |
|---|
| 3721 | |
|---|
| 3722 | if (! q1->tip) { |
|---|
| 3723 | newtested = addTraverse(tr, q, q1->next->back, |
|---|
| 3724 | mintrav2 , maxtrav, bt, FALSE); |
|---|
| 3725 | if (newtested == badRear) return badRear; |
|---|
| 3726 | tested += newtested; |
|---|
| 3727 | newtested = addTraverse(tr, q, q1->next->next->back, |
|---|
| 3728 | mintrav2 , maxtrav, bt, FALSE); |
|---|
| 3729 | if (newtested == badRear) return badRear; |
|---|
| 3730 | tested += newtested; |
|---|
| 3731 | } |
|---|
| 3732 | |
|---|
| 3733 | if (! q2->tip) { |
|---|
| 3734 | newtested = addTraverse(tr, q, q2->next->back, |
|---|
| 3735 | mintrav2 , maxtrav, bt, FALSE); |
|---|
| 3736 | if (newtested == badRear) return badRear; |
|---|
| 3737 | tested += newtested; |
|---|
| 3738 | newtested = addTraverse(tr, q, q2->next->next->back, |
|---|
| 3739 | mintrav2 , maxtrav, bt, FALSE); |
|---|
| 3740 | if (newtested == badRear) return badRear; |
|---|
| 3741 | tested += newtested; |
|---|
| 3742 | } |
|---|
| 3743 | |
|---|
| 3744 | hookup(q->next, q1, q1z); /* Restore original tree */ |
|---|
| 3745 | hookup(q->next->next, q2, q2z); |
|---|
| 3746 | if (! (initrav(tr, tr->start) |
|---|
| 3747 | && initrav(tr, tr->start->back))) return badRear; |
|---|
| 3748 | } |
|---|
| 3749 | } /* if (! q->tip && maxtrav > 1) */ |
|---|
| 3750 | |
|---|
| 3751 | /* Move other subtrees */ |
|---|
| 3752 | |
|---|
| 3753 | if (! p->tip) { |
|---|
| 3754 | newtested = rearrange(tr, p->next->back, mintrav, maxtrav, bt); |
|---|
| 3755 | if (newtested == badRear) return badRear; |
|---|
| 3756 | tested += newtested; |
|---|
| 3757 | newtested = rearrange(tr, p->next->next->back, mintrav, maxtrav, bt); |
|---|
| 3758 | if (newtested == badRear) return badRear; |
|---|
| 3759 | tested += newtested; |
|---|
| 3760 | } |
|---|
| 3761 | |
|---|
| 3762 | return tested; |
|---|
| 3763 | } /* rearrange */ |
|---|
| 3764 | |
|---|
| 3765 | |
|---|
| 3766 | FILE *fopen_pid (const char *filenm, const char *mode, char *name_pid) |
|---|
| 3767 | { /* fopen_pid */ |
|---|
| 3768 | |
|---|
| 3769 | (void) sprintf(name_pid, "%s.%d", filenm, getpid()); |
|---|
| 3770 | return fopen(name_pid, mode); |
|---|
| 3771 | } /* fopen_pid */ |
|---|
| 3772 | |
|---|
| 3773 | |
|---|
| 3774 | #if DeleteCheckpointFile |
|---|
| 3775 | void unlink_pid (char *filenm) |
|---|
| 3776 | { /* unlink_pid */ |
|---|
| 3777 | char scr[512]; |
|---|
| 3778 | |
|---|
| 3779 | (void) sprintf(scr, "%s.%d", filenm, getpid()); |
|---|
| 3780 | unlink(scr); |
|---|
| 3781 | } /* unlink_pid */ |
|---|
| 3782 | #endif |
|---|
| 3783 | |
|---|
| 3784 | |
|---|
| 3785 | void writeCheckpoint (tree *tr) |
|---|
| 3786 | { /* writeCheckpoint */ |
|---|
| 3787 | char filename[128]; |
|---|
| 3788 | FILE *checkpointf; |
|---|
| 3789 | void treeOut(); |
|---|
| 3790 | |
|---|
| 3791 | checkpointf = fopen_pid(checkpointname, "a", filename); |
|---|
| 3792 | if (checkpointf) { |
|---|
| 3793 | treeOut(checkpointf, tr, treeNewick); |
|---|
| 3794 | (void) fclose(checkpointf); |
|---|
| 3795 | } |
|---|
| 3796 | } /* writeCheckpoint */ |
|---|
| 3797 | |
|---|
| 3798 | |
|---|
| 3799 | node * findAnyTip(nodeptr p) |
|---|
| 3800 | { /* findAnyTip */ |
|---|
| 3801 | return p->tip ? p : findAnyTip(p->next->back); |
|---|
| 3802 | } /* findAnyTip */ |
|---|
| 3803 | |
|---|
| 3804 | |
|---|
| 3805 | boolean optimize (tree *tr, int maxtrav, bestlist *bt) |
|---|
| 3806 | { /* optimize */ |
|---|
| 3807 | nodeptr p; |
|---|
| 3808 | int mintrav, tested; |
|---|
| 3809 | |
|---|
| 3810 | if (tr->ntips < 4) return TRUE; |
|---|
| 3811 | |
|---|
| 3812 | writeCheckpoint(tr); /* checkpoint the starting tree */ |
|---|
| 3813 | |
|---|
| 3814 | if (maxtrav > tr->ntips - 3) maxtrav = tr->ntips - 3; |
|---|
| 3815 | if (maxtrav <= tr->opt_level) return TRUE; |
|---|
| 3816 | |
|---|
| 3817 | printf(" Doing %s rearrangements\n", |
|---|
| 3818 | (maxtrav == 1) ? "local" : |
|---|
| 3819 | (maxtrav < tr->ntips - 3) ? "regional" : "global"); |
|---|
| 3820 | |
|---|
| 3821 | /* loop while tree gets better */ |
|---|
| 3822 | |
|---|
| 3823 | do { |
|---|
| 3824 | (void) startOpt(bt, tr); |
|---|
| 3825 | mintrav = tr->opt_level + 1; |
|---|
| 3826 | |
|---|
| 3827 | /* rearrange must start from a tip or it will miss some trees */ |
|---|
| 3828 | |
|---|
| 3829 | p = findAnyTip(tr->start); |
|---|
| 3830 | tested = rearrange(tr, p->back, mintrav, maxtrav, bt); |
|---|
| 3831 | if (tested == badRear) return FALSE; |
|---|
| 3832 | |
|---|
| 3833 | # if Master |
|---|
| 3834 | if (! getReturnedTrees(tr, bt, tested)) return FALSE; |
|---|
| 3835 | # endif |
|---|
| 3836 | |
|---|
| 3837 | bt->numtrees += tested; |
|---|
| 3838 | (void) setOptLevel(bt, maxtrav); |
|---|
| 3839 | if (! recallBestTree(bt, 1, tr)) return FALSE; /* recover best tree */ |
|---|
| 3840 | |
|---|
| 3841 | printf(" Tested %d alternative trees\n", tested); |
|---|
| 3842 | |
|---|
| 3843 | if (bt->improved) { |
|---|
| 3844 | printf(" Ln Likelihood =%14.5f\n", tr->likelihood); |
|---|
| 3845 | } |
|---|
| 3846 | |
|---|
| 3847 | writeCheckpoint(tr); /* checkpoint the new tree */ |
|---|
| 3848 | } while (maxtrav > tr->opt_level); |
|---|
| 3849 | |
|---|
| 3850 | return TRUE; |
|---|
| 3851 | } /* optimize */ |
|---|
| 3852 | |
|---|
| 3853 | |
|---|
| 3854 | void coordinates (tree *tr, nodeptr p, double lengthsum, drawdata *tdptr) |
|---|
| 3855 | { /* coordinates */ |
|---|
| 3856 | /* establishes coordinates of nodes */ |
|---|
| 3857 | double x, z; |
|---|
| 3858 | nodeptr q, first, last; |
|---|
| 3859 | |
|---|
| 3860 | if (p->tip) { |
|---|
| 3861 | p->xcoord = NINT(over * lengthsum); |
|---|
| 3862 | p->ymax = p->ymin = p->ycoord = tdptr->tipy; |
|---|
| 3863 | tdptr->tipy += down; |
|---|
| 3864 | if (lengthsum > tdptr->tipmax) tdptr->tipmax = lengthsum; |
|---|
| 3865 | } |
|---|
| 3866 | |
|---|
| 3867 | else { |
|---|
| 3868 | q = p->next; |
|---|
| 3869 | do { |
|---|
| 3870 | z = q->z; |
|---|
| 3871 | if (z < zmin) z = zmin; |
|---|
| 3872 | x = lengthsum - tr->rdta->fracchange * log(z); |
|---|
| 3873 | coordinates(tr, q->back, x, tdptr); |
|---|
| 3874 | q = q->next; |
|---|
| 3875 | } while (p == tr->start->back ? q != p->next : q != p); |
|---|
| 3876 | |
|---|
| 3877 | first = p->next->back; |
|---|
| 3878 | q = p; |
|---|
| 3879 | while (q->next != p) q = q->next; |
|---|
| 3880 | last = q->back; |
|---|
| 3881 | p->xcoord = NINT(over * lengthsum); |
|---|
| 3882 | p->ycoord = (first->ycoord + last->ycoord)/2; |
|---|
| 3883 | p->ymin = first->ymin; |
|---|
| 3884 | p->ymax = last->ymax; |
|---|
| 3885 | } |
|---|
| 3886 | } /* coordinates */ |
|---|
| 3887 | |
|---|
| 3888 | |
|---|
| 3889 | void drawline (tree *tr, int i, double scale) |
|---|
| 3890 | /* draws one row of the tree diagram by moving up tree */ |
|---|
| 3891 | /* Modified to handle 1000 taxa, October 16, 1991 */ |
|---|
| 3892 | { /* drawline */ |
|---|
| 3893 | nodeptr p, q, r, first, last; |
|---|
| 3894 | int n, j, k, l, extra; |
|---|
| 3895 | boolean done; |
|---|
| 3896 | |
|---|
| 3897 | p = q = tr->start->back; |
|---|
| 3898 | extra = 0; |
|---|
| 3899 | |
|---|
| 3900 | if (i == p->ycoord) { |
|---|
| 3901 | k = q->number - tr->mxtips; |
|---|
| 3902 | for (j = k; j < 1000; j *= 10) putchar('-'); |
|---|
| 3903 | printf("%d", k); |
|---|
| 3904 | extra = 1; |
|---|
| 3905 | } |
|---|
| 3906 | else printf(" "); |
|---|
| 3907 | |
|---|
| 3908 | do { |
|---|
| 3909 | if (! p->tip) { |
|---|
| 3910 | r = p->next; |
|---|
| 3911 | done = FALSE; |
|---|
| 3912 | do { |
|---|
| 3913 | if ((i >= r->back->ymin) && (i <= r->back->ymax)) { |
|---|
| 3914 | q = r->back; |
|---|
| 3915 | done = TRUE; |
|---|
| 3916 | } |
|---|
| 3917 | r = r->next; |
|---|
| 3918 | } while (! done && (p == tr->start->back ? r != p->next : r != p)); |
|---|
| 3919 | |
|---|
| 3920 | first = p->next->back; |
|---|
| 3921 | r = p; |
|---|
| 3922 | while (r->next != p) r = r->next; |
|---|
| 3923 | last = r->back; |
|---|
| 3924 | if (p == tr->start->back) last = p->back; |
|---|
| 3925 | } |
|---|
| 3926 | |
|---|
| 3927 | done = (p->tip) || (p == q); |
|---|
| 3928 | n = NINT(scale*(q->xcoord - p->xcoord)); |
|---|
| 3929 | if ((n < 3) && (! q->tip)) n = 3; |
|---|
| 3930 | n -= extra; |
|---|
| 3931 | extra = 0; |
|---|
| 3932 | |
|---|
| 3933 | if ((q->ycoord == i) && (! done)) { |
|---|
| 3934 | if (p->ycoord != q->ycoord) putchar('+'); |
|---|
| 3935 | else putchar('-'); |
|---|
| 3936 | |
|---|
| 3937 | if (! q->tip) { |
|---|
| 3938 | k = q->number - tr->mxtips; |
|---|
| 3939 | l = n - 3; |
|---|
| 3940 | for (j = k; j < 100; j *= 10) l++; |
|---|
| 3941 | for (j = 1; j <= l; j++) putchar('-'); |
|---|
| 3942 | printf("%d", k); |
|---|
| 3943 | extra = 1; |
|---|
| 3944 | } |
|---|
| 3945 | else for (j = 1; j <= n-1; j++) putchar('-'); |
|---|
| 3946 | } |
|---|
| 3947 | |
|---|
| 3948 | else if (! p->tip) { |
|---|
| 3949 | if ((last->ycoord > i) && (first->ycoord < i) && (i != p->ycoord)) { |
|---|
| 3950 | putchar('!'); |
|---|
| 3951 | for (j = 1; j <= n-1; j++) putchar(' '); |
|---|
| 3952 | } |
|---|
| 3953 | else for (j = 1; j <= n; j++) putchar(' '); |
|---|
| 3954 | } |
|---|
| 3955 | |
|---|
| 3956 | else |
|---|
| 3957 | for (j = 1; j <= n; j++) putchar(' '); |
|---|
| 3958 | |
|---|
| 3959 | p = q; |
|---|
| 3960 | } while (! done); |
|---|
| 3961 | |
|---|
| 3962 | if ((p->ycoord == i) && p->tip) { |
|---|
| 3963 | printf(" %s", p->name); |
|---|
| 3964 | } |
|---|
| 3965 | |
|---|
| 3966 | putchar('\n'); |
|---|
| 3967 | } /* drawline */ |
|---|
| 3968 | |
|---|
| 3969 | |
|---|
| 3970 | void printTree (tree *tr, analdef *adef) |
|---|
| 3971 | /* prints out diagram of the tree */ |
|---|
| 3972 | { /* printTree */ |
|---|
| 3973 | drawdata tipdata; |
|---|
| 3974 | double scale; |
|---|
| 3975 | int i, imax; |
|---|
| 3976 | |
|---|
| 3977 | if (adef->trprint) { |
|---|
| 3978 | putchar('\n'); |
|---|
| 3979 | tipdata.tipy = 1; |
|---|
| 3980 | tipdata.tipmax = 0.0; |
|---|
| 3981 | coordinates(tr, tr->start->back, (double) 0.0, & tipdata); |
|---|
| 3982 | scale = 1.0 / tipdata.tipmax; |
|---|
| 3983 | imax = tipdata.tipy - down; |
|---|
| 3984 | for (i = 1; i <= imax; i++) drawline(tr, i, scale); |
|---|
| 3985 | printf("\nRemember: "); |
|---|
| 3986 | if (adef->root) printf("(although rooted by outgroup) "); |
|---|
| 3987 | printf("this is an unrooted tree!\n\n"); |
|---|
| 3988 | } |
|---|
| 3989 | } /* printTree */ |
|---|
| 3990 | |
|---|
| 3991 | |
|---|
| 3992 | double sigma (tree *tr, nodeptr p, double *sumlrptr) |
|---|
| 3993 | /* compute standard deviation */ |
|---|
| 3994 | { /* sigma */ |
|---|
| 3995 | likelivector *lp, *lq; |
|---|
| 3996 | homType *mlvP, *mlvQ; |
|---|
| 3997 | double slope, sum, sumlr, z, zv, zz, lz, |
|---|
| 3998 | rat, suma, sumb, sumc, d2, d, li, temp, abzz, bczv, t3, |
|---|
| 3999 | fx1a, fx1c, fx1g, fx1t, fx1r, fx1y, fx2r, fx2y, w; |
|---|
| 4000 | double *rptr; |
|---|
| 4001 | nodeptr q, realQ, realP; |
|---|
| 4002 | int i, *wptr, j, cat, *cptr; |
|---|
| 4003 | register char *sQ; |
|---|
| 4004 | register char *sP; |
|---|
| 4005 | long refQ, refP; |
|---|
| 4006 | memP calcP[EQUALITIES][maxcategories], *calcptrP; |
|---|
| 4007 | memQ calcQ[EQUALITIES][maxcategories], *calcptrQ; |
|---|
| 4008 | |
|---|
| 4009 | q = p->back; |
|---|
| 4010 | while ((! p->x) || (! q->x)) { |
|---|
| 4011 | /*stamatak*/ |
|---|
| 4012 | |
|---|
| 4013 | if (! (p->x)) if (! newviewOptimized(tr, p)) return -1.0; |
|---|
| 4014 | if (! (q->x)) if (! newviewOptimized(tr, q)) return -1.0; |
|---|
| 4015 | } |
|---|
| 4016 | |
|---|
| 4017 | lp = p->x->lv; |
|---|
| 4018 | lq = q->x->lv; |
|---|
| 4019 | |
|---|
| 4020 | z = p->z; |
|---|
| 4021 | if (z < zmin) z = zmin; |
|---|
| 4022 | lz = log(z); |
|---|
| 4023 | cptr = &(tr->cdta->patcat[0]); |
|---|
| 4024 | wptr = &(tr->cdta->aliaswgt[0]); |
|---|
| 4025 | rptr = &(tr->cdta->patrat[0]); |
|---|
| 4026 | sum = sumlr = slope = 0.0; |
|---|
| 4027 | |
|---|
| 4028 | # ifdef Vectorize |
|---|
| 4029 | # pragma IVDEP |
|---|
| 4030 | # endif |
|---|
| 4031 | |
|---|
| 4032 | realQ = tr->nodep[q->number]; |
|---|
| 4033 | realP = tr->nodep[p->number]; |
|---|
| 4034 | sQ = realQ->equalityVector; |
|---|
| 4035 | sP = realP->equalityVector; |
|---|
| 4036 | |
|---|
| 4037 | for (j = 1; j <= tr->rdta->categs; j++) |
|---|
| 4038 | { |
|---|
| 4039 | |
|---|
| 4040 | for(i = 0; i < equalities; i++) |
|---|
| 4041 | { |
|---|
| 4042 | mlvP = &realP->mlv[i][j]; |
|---|
| 4043 | if(mlvP->set) |
|---|
| 4044 | { |
|---|
| 4045 | calcptrP = &calcP[i][j]; |
|---|
| 4046 | calcptrP->fx1a = tr->rdta->freqa * mlvP->a; |
|---|
| 4047 | calcptrP->fx1g = tr->rdta->freqg * mlvP->g; |
|---|
| 4048 | calcptrP->fx1c = tr->rdta->freqc * mlvP->c; |
|---|
| 4049 | calcptrP->fx1t = tr->rdta->freqt * mlvP->t; |
|---|
| 4050 | calcptrP->sumag = calcptrP->fx1a + calcptrP->fx1g; |
|---|
| 4051 | calcptrP->sumct = calcptrP->fx1c + calcptrP->fx1t; |
|---|
| 4052 | calcptrP->sumagct = calcptrP->sumag + calcptrP->sumct; |
|---|
| 4053 | calcptrP->exp = mlvP->exp; |
|---|
| 4054 | } |
|---|
| 4055 | mlvQ = &realQ->mlv[i][j]; |
|---|
| 4056 | if(mlvQ->set) |
|---|
| 4057 | { |
|---|
| 4058 | calcptrQ = &calcQ[i][j]; |
|---|
| 4059 | calcptrQ->fx2r = tr->rdta->freqa * mlvQ->a + tr->rdta->freqg * mlvQ->g; |
|---|
| 4060 | calcptrQ->fx2y = tr->rdta->freqc * mlvQ->c + tr->rdta->freqt * mlvQ->t; |
|---|
| 4061 | calcptrQ->sumfx2rfx2y = calcptrQ->fx2r + calcptrQ->fx2y; |
|---|
| 4062 | calcptrQ->exp = mlvQ->exp; |
|---|
| 4063 | calcptrQ->a = mlvQ->a; |
|---|
| 4064 | calcptrQ->c = mlvQ->c; |
|---|
| 4065 | calcptrQ->g = mlvQ->g; |
|---|
| 4066 | calcptrQ->t = mlvQ->t; |
|---|
| 4067 | } |
|---|
| 4068 | } |
|---|
| 4069 | } |
|---|
| 4070 | |
|---|
| 4071 | |
|---|
| 4072 | |
|---|
| 4073 | |
|---|
| 4074 | |
|---|
| 4075 | for (i = 0; i < tr->cdta->endsite; i++) { |
|---|
| 4076 | rat = *rptr++; |
|---|
| 4077 | cat = *cptr++; |
|---|
| 4078 | refQ = *sQ++; |
|---|
| 4079 | refP = *sP++; |
|---|
| 4080 | |
|---|
| 4081 | zz = exp(rat * lz); |
|---|
| 4082 | zv = exp(rat * tr->rdta->xv * lz); |
|---|
| 4083 | |
|---|
| 4084 | |
|---|
| 4085 | if(refP >= 0) |
|---|
| 4086 | { |
|---|
| 4087 | if(refQ >= 0) |
|---|
| 4088 | { |
|---|
| 4089 | calcptrP = &calcP[refP][cat]; |
|---|
| 4090 | calcptrQ = &calcQ[refQ][cat]; |
|---|
| 4091 | suma = calcptrP->fx1a * calcptrQ->a + |
|---|
| 4092 | calcptrP->fx1c * calcptrQ->c + |
|---|
| 4093 | calcptrP->fx1g * calcptrQ->g + calcptrP->fx1t * calcptrQ->t; |
|---|
| 4094 | |
|---|
| 4095 | |
|---|
| 4096 | sumc = calcptrP->sumagct * calcptrQ->sumfx2rfx2y; |
|---|
| 4097 | sumb = calcptrP->sumag * calcptrQ->fx2r * tr->rdta->invfreqr + calcptrP->sumct * calcptrQ->fx2y * tr->rdta->invfreqy; |
|---|
| 4098 | |
|---|
| 4099 | } |
|---|
| 4100 | else |
|---|
| 4101 | { |
|---|
| 4102 | calcptrP = &calcP[refP][cat]; |
|---|
| 4103 | suma = calcptrP->fx1a * lq->a + calcptrP->fx1c * lq->c + |
|---|
| 4104 | calcptrP->fx1g * lq->g + calcptrP->fx1t * lq->t; |
|---|
| 4105 | |
|---|
| 4106 | fx2r = tr->rdta->freqa * lq->a + tr->rdta->freqg * lq->g; |
|---|
| 4107 | fx2y = tr->rdta->freqc * lq->c + tr->rdta->freqt * lq->t; |
|---|
| 4108 | |
|---|
| 4109 | sumc = calcptrP->sumagct * (fx2r + fx2y); |
|---|
| 4110 | sumb = calcptrP->sumag * fx2r * tr->rdta->invfreqr + calcptrP->sumct * fx2y * tr->rdta->invfreqy; |
|---|
| 4111 | |
|---|
| 4112 | } |
|---|
| 4113 | } |
|---|
| 4114 | else |
|---|
| 4115 | { |
|---|
| 4116 | if(refQ >= 0) |
|---|
| 4117 | { |
|---|
| 4118 | calcptrQ = &calcQ[refQ][cat]; |
|---|
| 4119 | fx1a = tr->rdta->freqa * lp->a; |
|---|
| 4120 | fx1g = tr->rdta->freqg * lp->g; |
|---|
| 4121 | fx1c = tr->rdta->freqc * lp->c; |
|---|
| 4122 | fx1t = tr->rdta->freqt * lp->t; |
|---|
| 4123 | suma = fx1a * calcptrQ->a + fx1c * calcptrQ->c + |
|---|
| 4124 | fx1g * calcptrQ->g + fx1t * calcptrQ->t; |
|---|
| 4125 | fx1r = fx1a + fx1g; |
|---|
| 4126 | fx1y = fx1c + fx1t; |
|---|
| 4127 | sumc = (fx1r + fx1y) * calcptrQ->sumfx2rfx2y; |
|---|
| 4128 | sumb = fx1r * calcptrQ->fx2r * tr->rdta->invfreqr + fx1y * calcptrQ->fx2y * tr->rdta->invfreqy; |
|---|
| 4129 | |
|---|
| 4130 | } |
|---|
| 4131 | else |
|---|
| 4132 | { |
|---|
| 4133 | fx1a = tr->rdta->freqa * lp->a; |
|---|
| 4134 | fx1g = tr->rdta->freqg * lp->g; |
|---|
| 4135 | fx1c = tr->rdta->freqc * lp->c; |
|---|
| 4136 | fx1t = tr->rdta->freqt * lp->t; |
|---|
| 4137 | suma = fx1a * lq->a + fx1c * lq->c + fx1g * lq->g + fx1t * lq->t; |
|---|
| 4138 | fx2r = tr->rdta->freqa * lq->a + tr->rdta->freqg * lq->g; |
|---|
| 4139 | fx2y = tr->rdta->freqc * lq->c + tr->rdta->freqt * lq->t; |
|---|
| 4140 | fx1r = fx1a + fx1g; |
|---|
| 4141 | fx1y = fx1c + fx1t; |
|---|
| 4142 | sumc = (fx1r + fx1y) * (fx2r + fx2y); |
|---|
| 4143 | sumb = fx1r * fx2r * tr->rdta->invfreqr + fx1y * fx2y * tr->rdta->invfreqy; |
|---|
| 4144 | |
|---|
| 4145 | } |
|---|
| 4146 | } |
|---|
| 4147 | |
|---|
| 4148 | |
|---|
| 4149 | |
|---|
| 4150 | abzz = zz * (suma - sumb); |
|---|
| 4151 | bczv = zv * (sumb - sumc); |
|---|
| 4152 | li = sumc + abzz + bczv; |
|---|
| 4153 | t3 = tr->rdta->xv * bczv; |
|---|
| 4154 | d = abzz + t3; |
|---|
| 4155 | d2 = rat * (abzz*(rat-1.0) + t3*(rat * tr->rdta->xv - 1.0)); |
|---|
| 4156 | |
|---|
| 4157 | temp = rat * d / li; |
|---|
| 4158 | w = *wptr++; |
|---|
| 4159 | slope += w * temp; |
|---|
| 4160 | sum += w * (temp * temp - d2/li); |
|---|
| 4161 | sumlr += w * log(li / (suma + 1.0E-300)); |
|---|
| 4162 | lp++; |
|---|
| 4163 | lq++; |
|---|
| 4164 | } |
|---|
| 4165 | |
|---|
| 4166 | *sumlrptr = sumlr; |
|---|
| 4167 | return (sum > 1.0E-300) ? z*(-slope + sqrt(slope*slope + 3.841*sum))/sum |
|---|
| 4168 | : 1.0; |
|---|
| 4169 | } /* sigma */ |
|---|
| 4170 | |
|---|
| 4171 | |
|---|
| 4172 | void describe (tree *tr, nodeptr p) |
|---|
| 4173 | /* print out information for one branch */ |
|---|
| 4174 | { /* describe */ |
|---|
| 4175 | double z, s, sumlr; |
|---|
| 4176 | nodeptr q; |
|---|
| 4177 | char *nameptr; |
|---|
| 4178 | int k, ch; |
|---|
| 4179 | |
|---|
| 4180 | q = p->back; |
|---|
| 4181 | printf("%4d ", q->number - tr->mxtips); |
|---|
| 4182 | if (p->tip) { |
|---|
| 4183 | nameptr = p->name; |
|---|
| 4184 | k = nmlngth; |
|---|
| 4185 | while (ch = *nameptr++) {putchar(ch); k--;} |
|---|
| 4186 | while (--k >= 0) putchar(' '); |
|---|
| 4187 | } |
|---|
| 4188 | else { |
|---|
| 4189 | printf("%4d", p->number - tr->mxtips); |
|---|
| 4190 | for (k = 4; k < nmlngth; k++) putchar(' '); |
|---|
| 4191 | } |
|---|
| 4192 | |
|---|
| 4193 | z = q->z; |
|---|
| 4194 | if (z <= zmin) printf(" infinity"); |
|---|
| 4195 | else printf("%15.5f", -log(z) * tr->rdta->fracchange); |
|---|
| 4196 | |
|---|
| 4197 | s = sigma(tr, q, & sumlr); |
|---|
| 4198 | printf(" ("); |
|---|
| 4199 | if (z + s >= zmax) printf(" zero"); |
|---|
| 4200 | else printf("%9.5f", (double) -log(z + s) * tr->rdta->fracchange); |
|---|
| 4201 | putchar(','); |
|---|
| 4202 | if (z - s <= zmin) printf(" infinity"); |
|---|
| 4203 | else printf("%12.5f", (double) -log(z - s) * tr->rdta->fracchange); |
|---|
| 4204 | putchar(')'); |
|---|
| 4205 | |
|---|
| 4206 | if (sumlr > 2.995 ) printf(" **"); |
|---|
| 4207 | else if (sumlr > 1.9205) printf(" *"); |
|---|
| 4208 | putchar('\n'); |
|---|
| 4209 | |
|---|
| 4210 | if (! p->tip) { |
|---|
| 4211 | describe(tr, p->next->back); |
|---|
| 4212 | describe(tr, p->next->next->back); |
|---|
| 4213 | } |
|---|
| 4214 | } /* describe */ |
|---|
| 4215 | |
|---|
| 4216 | |
|---|
| 4217 | void summarize (tree *tr) |
|---|
| 4218 | /* print out branch length information and node numbers */ |
|---|
| 4219 | { /* summarize */ |
|---|
| 4220 | printf("Ln Likelihood =%14.5f\n", tr->likelihood); |
|---|
| 4221 | putchar('\n'); |
|---|
| 4222 | printf(" Between And Length"); |
|---|
| 4223 | printf(" Approx. Confidence Limits\n"); |
|---|
| 4224 | printf(" ------- --- ------"); |
|---|
| 4225 | printf(" ------- ---------- ------\n"); |
|---|
| 4226 | |
|---|
| 4227 | describe(tr, tr->start->back->next->back); |
|---|
| 4228 | describe(tr, tr->start->back->next->next->back); |
|---|
| 4229 | describe(tr, tr->start); |
|---|
| 4230 | putchar('\n'); |
|---|
| 4231 | printf(" * = significantly positive, P < 0.05\n"); |
|---|
| 4232 | printf(" ** = significantly positive, P < 0.01\n\n\n"); |
|---|
| 4233 | globTime = gettime() - globTime; |
|---|
| 4234 | fprintf(stderr, "Acc time global: %f\n", globTime); |
|---|
| 4235 | printf("Acc time global: %f\n", globTime); |
|---|
| 4236 | fprintf(stderr, "FLOPBS: %ld\n", flopBlocks); |
|---|
| 4237 | fprintf(stderr,"depth: %d\n", tr->eqDepth); |
|---|
| 4238 | printf("FLOPPBS: %ld\n", flopBlocks); |
|---|
| 4239 | fprintf(stderr,"countEQ: %ld, countNEQ: %ld\n", countEQ, countNEQ); |
|---|
| 4240 | |
|---|
| 4241 | } /* summarize */ |
|---|
| 4242 | |
|---|
| 4243 | |
|---|
| 4244 | /*=========== This is a problem if tr->start->back is a tip! ===========*/ |
|---|
| 4245 | /* All routines should be contrived so that tr->start->back is not a tip */ |
|---|
| 4246 | |
|---|
| 4247 | char *treeString (char *treestr, tree *tr, nodeptr p, int form) |
|---|
| 4248 | /* write string with representation of tree */ |
|---|
| 4249 | /* form == 1 -> Newick tree */ |
|---|
| 4250 | /* form == 2 -> Prolog fact */ |
|---|
| 4251 | /* form == 3 -> PHYLIP tree */ |
|---|
| 4252 | { /* treeString */ |
|---|
| 4253 | double x, z; |
|---|
| 4254 | char *nameptr; |
|---|
| 4255 | int c; |
|---|
| 4256 | |
|---|
| 4257 | if (p == tr->start->back) { |
|---|
| 4258 | if (form != treePHYLIP) { |
|---|
| 4259 | if (form == treeProlog) { |
|---|
| 4260 | (void) sprintf(treestr, "phylip_tree("); |
|---|
| 4261 | while (*treestr) treestr++; /* move pointer to null */ |
|---|
| 4262 | } |
|---|
| 4263 | |
|---|
| 4264 | (void) sprintf(treestr, "[&&%s: version = '%s'", |
|---|
| 4265 | programName, programVersion); |
|---|
| 4266 | while (*treestr) treestr++; |
|---|
| 4267 | |
|---|
| 4268 | (void) sprintf(treestr, ", %s = %15.13g", |
|---|
| 4269 | likelihood_key, tr->likelihood); |
|---|
| 4270 | while (*treestr) treestr++; |
|---|
| 4271 | |
|---|
| 4272 | (void) sprintf(treestr, ", %s = %d", ntaxa_key, tr->ntips); |
|---|
| 4273 | while (*treestr) treestr++; |
|---|
| 4274 | |
|---|
| 4275 | (void) sprintf(treestr,", %s = %d", opt_level_key, tr->opt_level); |
|---|
| 4276 | while (*treestr) treestr++; |
|---|
| 4277 | |
|---|
| 4278 | (void) sprintf(treestr, ", %s = %d", smoothed_key, tr->smoothed); |
|---|
| 4279 | while (*treestr) treestr++; |
|---|
| 4280 | |
|---|
| 4281 | (void) sprintf(treestr, "]%s", form == treeProlog ? ", " : " "); |
|---|
| 4282 | while (*treestr) treestr++; |
|---|
| 4283 | } |
|---|
| 4284 | } |
|---|
| 4285 | |
|---|
| 4286 | if (p->tip) { |
|---|
| 4287 | if (form != treePHYLIP) *treestr++ = '\''; |
|---|
| 4288 | nameptr = p->name; |
|---|
| 4289 | while (c = *nameptr++) { |
|---|
| 4290 | if (form != treePHYLIP) {if (c == '\'') *treestr++ = '\'';} |
|---|
| 4291 | else if (c == ' ') {c = '_';} |
|---|
| 4292 | *treestr++ = c; |
|---|
| 4293 | } |
|---|
| 4294 | if (form != treePHYLIP) *treestr++ = '\''; |
|---|
| 4295 | } |
|---|
| 4296 | |
|---|
| 4297 | else { |
|---|
| 4298 | *treestr++ = '('; |
|---|
| 4299 | treestr = treeString(treestr, tr, p->next->back, form); |
|---|
| 4300 | *treestr++ = ','; |
|---|
| 4301 | treestr = treeString(treestr, tr, p->next->next->back, form); |
|---|
| 4302 | if (p == tr->start->back) { |
|---|
| 4303 | *treestr++ = ','; |
|---|
| 4304 | treestr = treeString(treestr, tr, p->back, form); |
|---|
| 4305 | } |
|---|
| 4306 | *treestr++ = ')'; |
|---|
| 4307 | } |
|---|
| 4308 | |
|---|
| 4309 | if (p == tr->start->back) { |
|---|
| 4310 | (void) sprintf(treestr, ":0.0%s\n", (form != treeProlog) ? ";" : ")."); |
|---|
| 4311 | } |
|---|
| 4312 | else { |
|---|
| 4313 | z = p->z; |
|---|
| 4314 | if (z < zmin) z = zmin; |
|---|
| 4315 | x = -log(z) * tr->rdta->fracchange; |
|---|
| 4316 | (void) sprintf(treestr, ": %8.6f", x); /* prolog needs the space */ |
|---|
| 4317 | } |
|---|
| 4318 | |
|---|
| 4319 | while (*treestr) treestr++; /* move pointer up to null termination */ |
|---|
| 4320 | return treestr; |
|---|
| 4321 | } /* treeString */ |
|---|
| 4322 | |
|---|
| 4323 | |
|---|
| 4324 | void treeOut (FILE *treefile, tree *tr, int form) |
|---|
| 4325 | /* write out file with representation of final tree */ |
|---|
| 4326 | { /* treeOut */ |
|---|
| 4327 | int c; |
|---|
| 4328 | char *cptr, *treestr; |
|---|
| 4329 | |
|---|
| 4330 | treestr = (char *) Malloc((tr->ntips * (nmlngth+32)) + 256); |
|---|
| 4331 | if (! treestr) { |
|---|
| 4332 | fprintf(stderr, "treeOut: Malloc failure\n"); |
|---|
| 4333 | exit(1); |
|---|
| 4334 | } |
|---|
| 4335 | |
|---|
| 4336 | (void) treeString(treestr, tr, tr->start->back, form); |
|---|
| 4337 | cptr = treestr; |
|---|
| 4338 | while (c = *cptr++) putc(c, treefile); |
|---|
| 4339 | |
|---|
| 4340 | Free(treestr); |
|---|
| 4341 | } /* treeOut */ |
|---|
| 4342 | |
|---|
| 4343 | |
|---|
| 4344 | /*=======================================================================*/ |
|---|
| 4345 | /* Read a tree from a file */ |
|---|
| 4346 | /*=======================================================================*/ |
|---|
| 4347 | |
|---|
| 4348 | |
|---|
| 4349 | /* 1.0.A Processing of quotation marks in comment removed |
|---|
| 4350 | */ |
|---|
| 4351 | |
|---|
| 4352 | int treeFinishCom (FILE *fp, char **strp) |
|---|
| 4353 | { /* treeFinishCom */ |
|---|
| 4354 | int ch; |
|---|
| 4355 | |
|---|
| 4356 | while ((ch = getc(fp)) != EOF && ch != ']') { |
|---|
| 4357 | if (strp != NULL) *(*strp)++ = ch; /* save character */ |
|---|
| 4358 | if (ch == '[') { /* nested comment; find its end */ |
|---|
| 4359 | if ((ch = treeFinishCom(fp, strp)) == EOF) break; |
|---|
| 4360 | if (strp != NULL) *(*strp)++ = ch; /* save closing ] */ |
|---|
| 4361 | } |
|---|
| 4362 | } |
|---|
| 4363 | |
|---|
| 4364 | if (strp != NULL) **strp = '\0'; /* terminate string */ |
|---|
| 4365 | return ch; |
|---|
| 4366 | } /* treeFinishCom */ |
|---|
| 4367 | |
|---|
| 4368 | |
|---|
| 4369 | int treeGetCh (FILE *fp) /* get next nonblank, noncomment character */ |
|---|
| 4370 | { /* treeGetCh */ |
|---|
| 4371 | int ch; |
|---|
| 4372 | |
|---|
| 4373 | while ((ch = getc(fp)) != EOF) { |
|---|
| 4374 | if (whitechar(ch)) ; |
|---|
| 4375 | else if (ch == '[') { /* comment; find its end */ |
|---|
| 4376 | if ((ch = treeFinishCom(fp, (char **) NULL)) == EOF) break; |
|---|
| 4377 | } |
|---|
| 4378 | else break; |
|---|
| 4379 | } |
|---|
| 4380 | |
|---|
| 4381 | return ch; |
|---|
| 4382 | } /* treeGetCh */ |
|---|
| 4383 | |
|---|
| 4384 | |
|---|
| 4385 | boolean treeLabelEnd (int ch) |
|---|
| 4386 | { /* treeLabelEnd */ |
|---|
| 4387 | switch (ch) { |
|---|
| 4388 | case EOF: case '\0': case '\t': case '\n': case ' ': |
|---|
| 4389 | case ':': case ',': case '(': case ')': case '[': |
|---|
| 4390 | case ';': |
|---|
| 4391 | return TRUE; |
|---|
| 4392 | default: |
|---|
| 4393 | break; |
|---|
| 4394 | } |
|---|
| 4395 | return FALSE; |
|---|
| 4396 | } /* treeLabelEnd */ |
|---|
| 4397 | |
|---|
| 4398 | |
|---|
| 4399 | boolean treeGetLabel (FILE *fp, char *lblPtr, int maxlen) |
|---|
| 4400 | { /* treeGetLabel */ |
|---|
| 4401 | int ch; |
|---|
| 4402 | boolean done, quoted, lblfound; |
|---|
| 4403 | |
|---|
| 4404 | if (--maxlen < 0) lblPtr = (char *) NULL; /* reserves space for '\0' */ |
|---|
| 4405 | else if (lblPtr == NULL) maxlen = 0; |
|---|
| 4406 | |
|---|
| 4407 | ch = getc(fp); |
|---|
| 4408 | done = treeLabelEnd(ch); |
|---|
| 4409 | |
|---|
| 4410 | lblfound = ! done; |
|---|
| 4411 | quoted = (ch == '\''); |
|---|
| 4412 | if (quoted && ! done) {ch = getc(fp); done = (ch == EOF);} |
|---|
| 4413 | |
|---|
| 4414 | while (! done) { |
|---|
| 4415 | if (quoted) { |
|---|
| 4416 | if (ch == '\'') {ch = getc(fp); if (ch != '\'') break;} |
|---|
| 4417 | } |
|---|
| 4418 | |
|---|
| 4419 | else if (treeLabelEnd(ch)) break; |
|---|
| 4420 | |
|---|
| 4421 | else if (ch == '_') ch = ' '; /* unquoted _ goes to space */ |
|---|
| 4422 | |
|---|
| 4423 | if (--maxlen >= 0) *lblPtr++ = ch; |
|---|
| 4424 | ch = getc(fp); |
|---|
| 4425 | if (ch == EOF) break; |
|---|
| 4426 | } |
|---|
| 4427 | |
|---|
| 4428 | if (ch != EOF) (void) ungetc(ch, fp); |
|---|
| 4429 | |
|---|
| 4430 | if (lblPtr != NULL) *lblPtr = '\0'; |
|---|
| 4431 | |
|---|
| 4432 | return lblfound; |
|---|
| 4433 | } /* treeGetLabel */ |
|---|
| 4434 | |
|---|
| 4435 | |
|---|
| 4436 | boolean treeFlushLabel (FILE *fp) |
|---|
| 4437 | { /* treeFlushLabel */ |
|---|
| 4438 | return treeGetLabel(fp, (char *) NULL, (int) 0); |
|---|
| 4439 | } /* treeFlushLabel */ |
|---|
| 4440 | |
|---|
| 4441 | |
|---|
| 4442 | int treeFindTipByLabel (char *str, tree *tr) |
|---|
| 4443 | /* str -- label string pointer */ |
|---|
| 4444 | { /* treeFindTipByLabel */ |
|---|
| 4445 | nodeptr q; |
|---|
| 4446 | char *nameptr; |
|---|
| 4447 | int ch, i, n; |
|---|
| 4448 | boolean found; |
|---|
| 4449 | |
|---|
| 4450 | for (n = 1; n <= tr->mxtips; n++) { |
|---|
| 4451 | q = tr->nodep[n]; |
|---|
| 4452 | if (! (q->back)) { /* Only consider unused tips */ |
|---|
| 4453 | i = 0; |
|---|
| 4454 | nameptr = q->name; |
|---|
| 4455 | while ((found = (str[i++] == (ch = *nameptr++))) && ch) ; |
|---|
| 4456 | if (found) return n; |
|---|
| 4457 | } |
|---|
| 4458 | } |
|---|
| 4459 | |
|---|
| 4460 | printf("ERROR: Cannot find tree species: %s\n", str); |
|---|
| 4461 | |
|---|
| 4462 | return 0; |
|---|
| 4463 | } /* treeFindTipByLabel */ |
|---|
| 4464 | |
|---|
| 4465 | |
|---|
| 4466 | int treeFindTipName (FILE *fp, tree *tr) |
|---|
| 4467 | { /* treeFindTipName */ |
|---|
| 4468 | char *nameptr, str[nmlngth+2]; |
|---|
| 4469 | int n; |
|---|
| 4470 | |
|---|
| 4471 | if (tr->prelabeled) { |
|---|
| 4472 | if (treeGetLabel(fp, str, nmlngth+2)) |
|---|
| 4473 | n = treeFindTipByLabel(str, tr); |
|---|
| 4474 | else |
|---|
| 4475 | n = 0; |
|---|
| 4476 | } |
|---|
| 4477 | |
|---|
| 4478 | else if (tr->ntips < tr->mxtips) { |
|---|
| 4479 | n = tr->ntips + 1; |
|---|
| 4480 | nameptr = tr->nodep[n]->name; |
|---|
| 4481 | if (! treeGetLabel(fp, nameptr, nmlngth+1)) n = 0; |
|---|
| 4482 | } |
|---|
| 4483 | |
|---|
| 4484 | else { |
|---|
| 4485 | n = 0; |
|---|
| 4486 | } |
|---|
| 4487 | |
|---|
| 4488 | return n; |
|---|
| 4489 | } /* treeFindTipName */ |
|---|
| 4490 | |
|---|
| 4491 | |
|---|
| 4492 | void treeEchoContext (FILE *fp1, FILE *fp2, int n) |
|---|
| 4493 | { /* treeEchoContext */ |
|---|
| 4494 | int ch; |
|---|
| 4495 | boolean waswhite; |
|---|
| 4496 | |
|---|
| 4497 | waswhite = TRUE; |
|---|
| 4498 | |
|---|
| 4499 | while (n > 0 && ((ch = getc(fp1)) != EOF)) { |
|---|
| 4500 | if (whitechar(ch)) { |
|---|
| 4501 | ch = waswhite ? '\0' : ' '; |
|---|
| 4502 | waswhite = TRUE; |
|---|
| 4503 | } |
|---|
| 4504 | else { |
|---|
| 4505 | waswhite = FALSE; |
|---|
| 4506 | } |
|---|
| 4507 | |
|---|
| 4508 | if (ch > '\0') {putc(ch, fp2); n--;} |
|---|
| 4509 | } |
|---|
| 4510 | } /* treeEchoContext */ |
|---|
| 4511 | |
|---|
| 4512 | |
|---|
| 4513 | boolean treeProcessLength (FILE *fp, double *dptr) |
|---|
| 4514 | { /* treeProcessLength */ |
|---|
| 4515 | int ch; |
|---|
| 4516 | |
|---|
| 4517 | if ((ch = treeGetCh(fp)) == EOF) return FALSE; /* Skip comments */ |
|---|
| 4518 | (void) ungetc(ch, fp); |
|---|
| 4519 | |
|---|
| 4520 | if (fscanf(fp, "%lf", dptr) != 1) { |
|---|
| 4521 | printf("ERROR: treeProcessLength: Problem reading branch length\n"); |
|---|
| 4522 | treeEchoContext(fp, stdout, 40); |
|---|
| 4523 | printf("\n"); |
|---|
| 4524 | return FALSE; |
|---|
| 4525 | } |
|---|
| 4526 | |
|---|
| 4527 | return TRUE; |
|---|
| 4528 | } /* treeProcessLength */ |
|---|
| 4529 | |
|---|
| 4530 | |
|---|
| 4531 | boolean treeFlushLen (FILE *fp) |
|---|
| 4532 | { /* treeFlushLen */ |
|---|
| 4533 | double dummy; |
|---|
| 4534 | int ch; |
|---|
| 4535 | |
|---|
| 4536 | if ((ch = treeGetCh(fp)) == ':') return treeProcessLength(fp, & dummy); |
|---|
| 4537 | |
|---|
| 4538 | if (ch != EOF) (void) ungetc(ch, fp); |
|---|
| 4539 | return TRUE; |
|---|
| 4540 | } /* treeFlushLen */ |
|---|
| 4541 | |
|---|
| 4542 | |
|---|
| 4543 | boolean treeNeedCh (FILE *fp, int c1, const char *where) |
|---|
| 4544 | { /* treeNeedCh */ |
|---|
| 4545 | int c2; |
|---|
| 4546 | |
|---|
| 4547 | if ((c2 = treeGetCh(fp)) == c1) return TRUE; |
|---|
| 4548 | |
|---|
| 4549 | printf("ERROR: Expecting '%c' %s tree; found:", c1, where); |
|---|
| 4550 | if (c2 == EOF) { |
|---|
| 4551 | printf("End-of-File"); |
|---|
| 4552 | } |
|---|
| 4553 | else { |
|---|
| 4554 | ungetc(c2, fp); |
|---|
| 4555 | treeEchoContext(fp, stdout, 40); |
|---|
| 4556 | } |
|---|
| 4557 | putchar('\n'); |
|---|
| 4558 | return FALSE; |
|---|
| 4559 | } /* treeNeedCh */ |
|---|
| 4560 | |
|---|
| 4561 | |
|---|
| 4562 | boolean addElementLen (FILE *fp, tree *tr, nodeptr p) |
|---|
| 4563 | { /* addElementLen */ |
|---|
| 4564 | double z, branch; |
|---|
| 4565 | nodeptr q; |
|---|
| 4566 | int n, ch; |
|---|
| 4567 | |
|---|
| 4568 | if ((ch = treeGetCh(fp)) == '(') { /* A new internal node */ |
|---|
| 4569 | n = (tr->nextnode)++; |
|---|
| 4570 | if (n > 2*(tr->mxtips) - 2) { |
|---|
| 4571 | if (tr->rooted || n > 2*(tr->mxtips) - 1) { |
|---|
| 4572 | printf("ERROR: Too many internal nodes. Is tree rooted?\n"); |
|---|
| 4573 | printf(" Deepest splitting should be a trifurcation.\n"); |
|---|
| 4574 | return FALSE; |
|---|
| 4575 | } |
|---|
| 4576 | else { |
|---|
| 4577 | tr->rooted = TRUE; |
|---|
| 4578 | } |
|---|
| 4579 | } |
|---|
| 4580 | q = tr->nodep[n]; |
|---|
| 4581 | if (! addElementLen(fp, tr, q->next)) return FALSE; |
|---|
| 4582 | if (! treeNeedCh(fp, ',', "in")) return FALSE; |
|---|
| 4583 | if (! addElementLen(fp, tr, q->next->next)) return FALSE; |
|---|
| 4584 | if (! treeNeedCh(fp, ')', "in")) return FALSE; |
|---|
| 4585 | (void) treeFlushLabel(fp); |
|---|
| 4586 | } |
|---|
| 4587 | |
|---|
| 4588 | else { /* A new tip */ |
|---|
| 4589 | ungetc(ch, fp); |
|---|
| 4590 | if ((n = treeFindTipName(fp, tr)) <= 0) return FALSE; |
|---|
| 4591 | q = tr->nodep[n]; |
|---|
| 4592 | if (tr->start->number > n) tr->start = q; |
|---|
| 4593 | (tr->ntips)++; |
|---|
| 4594 | } /* End of tip processing */ |
|---|
| 4595 | |
|---|
| 4596 | if (tr->userlen) { |
|---|
| 4597 | if (! treeNeedCh(fp, ':', "in")) return FALSE; |
|---|
| 4598 | if (! treeProcessLength(fp, & branch)) return FALSE; |
|---|
| 4599 | z = exp(-branch / tr->rdta->fracchange); |
|---|
| 4600 | if (z > zmax) z = zmax; |
|---|
| 4601 | hookup(p, q, z); |
|---|
| 4602 | } |
|---|
| 4603 | else { |
|---|
| 4604 | if (! treeFlushLen(fp)) return FALSE; |
|---|
| 4605 | hookup(p, q, defaultz); |
|---|
| 4606 | } |
|---|
| 4607 | |
|---|
| 4608 | return TRUE; |
|---|
| 4609 | } /* addElementLen */ |
|---|
| 4610 | |
|---|
| 4611 | |
|---|
| 4612 | int saveTreeCom (char **comstrp) |
|---|
| 4613 | { /* saveTreeCom */ |
|---|
| 4614 | int ch; |
|---|
| 4615 | boolean inquote; |
|---|
| 4616 | |
|---|
| 4617 | inquote = FALSE; |
|---|
| 4618 | while ((ch = getc(INFILE)) != EOF && (inquote || ch != ']')) { |
|---|
| 4619 | *(*comstrp)++ = ch; /* save character */ |
|---|
| 4620 | if (ch == '[' && ! inquote) { /* comment; find its end */ |
|---|
| 4621 | if ((ch = saveTreeCom(comstrp)) == EOF) break; |
|---|
| 4622 | *(*comstrp)++ = ch; /* add ] */ |
|---|
| 4623 | } |
|---|
| 4624 | else if (ch == '\'') inquote = ! inquote; /* start or end of quote */ |
|---|
| 4625 | } |
|---|
| 4626 | |
|---|
| 4627 | return ch; |
|---|
| 4628 | } /* saveTreeCom */ |
|---|
| 4629 | |
|---|
| 4630 | |
|---|
| 4631 | boolean processTreeCom (FILE *fp, tree *tr) |
|---|
| 4632 | { /* processTreeCom */ |
|---|
| 4633 | int text_started, functor_read, com_open; |
|---|
| 4634 | |
|---|
| 4635 | /* Accept prefatory "phylip_tree(" or "pseudoNewick(" */ |
|---|
| 4636 | |
|---|
| 4637 | functor_read = text_started = 0; |
|---|
| 4638 | (void) fscanf(fp, " p%nhylip_tree(%n", & text_started, & functor_read); |
|---|
| 4639 | if (text_started && ! functor_read) { |
|---|
| 4640 | (void) fscanf(fp, "seudoNewick(%n", & functor_read); |
|---|
| 4641 | if (! functor_read) { |
|---|
| 4642 | printf("Start of tree 'p...' not understood.\n"); |
|---|
| 4643 | return FALSE; |
|---|
| 4644 | } |
|---|
| 4645 | } |
|---|
| 4646 | |
|---|
| 4647 | com_open = 0; |
|---|
| 4648 | (void) fscanf(fp, " [%n", & com_open); |
|---|
| 4649 | |
|---|
| 4650 | if (com_open) { /* comment; read it */ |
|---|
| 4651 | char com[1024], *com_end; |
|---|
| 4652 | |
|---|
| 4653 | com_end = com; |
|---|
| 4654 | if (treeFinishCom(fp, & com_end) == EOF) { /* omits enclosing []s */ |
|---|
| 4655 | printf("Missing end of tree comment\n"); |
|---|
| 4656 | return FALSE; |
|---|
| 4657 | } |
|---|
| 4658 | |
|---|
| 4659 | (void) readKeyValue(com, likelihood_key, "%lg", |
|---|
| 4660 | (void *) &(tr->likelihood)); |
|---|
| 4661 | (void) readKeyValue(com, opt_level_key, "%d", |
|---|
| 4662 | (void *) &(tr->opt_level)); |
|---|
| 4663 | (void) readKeyValue(com, smoothed_key, "%d", |
|---|
| 4664 | (void *) &(tr->smoothed)); |
|---|
| 4665 | |
|---|
| 4666 | if (functor_read) (void) fscanf(fp, " ,"); /* remove trailing comma */ |
|---|
| 4667 | } |
|---|
| 4668 | |
|---|
| 4669 | return (functor_read > 0); |
|---|
| 4670 | } /* processTreeCom */ |
|---|
| 4671 | |
|---|
| 4672 | |
|---|
| 4673 | nodeptr uprootTree (tree *tr, nodeptr p) |
|---|
| 4674 | { /* uprootTree */ |
|---|
| 4675 | nodeptr q, r, s, start; |
|---|
| 4676 | int n; |
|---|
| 4677 | |
|---|
| 4678 | if (p->tip || p->back) { |
|---|
| 4679 | printf("ERROR: Unable to uproot tree.\n"); |
|---|
| 4680 | printf(" Inappropriate node marked for removal.\n"); |
|---|
| 4681 | return (nodeptr) NULL; |
|---|
| 4682 | } |
|---|
| 4683 | |
|---|
| 4684 | n = --(tr->nextnode); /* last internal node added */ |
|---|
| 4685 | if (n != tr->mxtips + tr->ntips - 1) { |
|---|
| 4686 | printf("ERROR: Unable to uproot tree. Inconsistent\n"); |
|---|
| 4687 | printf(" number of tips and nodes for rooted tree.\n"); |
|---|
| 4688 | return (nodeptr) NULL; |
|---|
| 4689 | } |
|---|
| 4690 | |
|---|
| 4691 | q = p->next->back; /* remove p from tree */ |
|---|
| 4692 | r = p->next->next->back; |
|---|
| 4693 | hookup(q, r, tr->userlen ? (q->z * r->z) : defaultz); |
|---|
| 4694 | |
|---|
| 4695 | start = (r->tip || (! q->tip)) ? r : r->next->next->back; |
|---|
| 4696 | |
|---|
| 4697 | if (tr->ntips > 2 && p->number != n) { |
|---|
| 4698 | q = tr->nodep[n]; /* transfer last node's conections to p */ |
|---|
| 4699 | r = q->next; |
|---|
| 4700 | s = q->next->next; |
|---|
| 4701 | hookup(p, q->back, q->z); /* move connections to p */ |
|---|
| 4702 | hookup(p->next, r->back, r->z); |
|---|
| 4703 | hookup(p->next->next, s->back, s->z); |
|---|
| 4704 | if (start->number == q->number) start = start->back->back; |
|---|
| 4705 | q->back = r->back = s->back = (nodeptr) NULL; |
|---|
| 4706 | } |
|---|
| 4707 | else { |
|---|
| 4708 | p->back = p->next->back = p->next->next->back = (nodeptr) NULL; |
|---|
| 4709 | } |
|---|
| 4710 | |
|---|
| 4711 | tr->rooted = FALSE; |
|---|
| 4712 | return start; |
|---|
| 4713 | } /* uprootTree */ |
|---|
| 4714 | |
|---|
| 4715 | |
|---|
| 4716 | boolean treeReadLen (FILE *fp, tree *tr) |
|---|
| 4717 | { /* treeReadLen */ |
|---|
| 4718 | nodeptr p; |
|---|
| 4719 | int i, ch; |
|---|
| 4720 | boolean is_fact; |
|---|
| 4721 | |
|---|
| 4722 | for (i = 1; i <= tr->mxtips; i++) tr->nodep[i]->back = (node *) NULL; |
|---|
| 4723 | tr->start = tr->nodep[tr->mxtips]; |
|---|
| 4724 | tr->ntips = 0; |
|---|
| 4725 | tr->nextnode = tr->mxtips + 1; |
|---|
| 4726 | tr->opt_level = 0; |
|---|
| 4727 | tr->log_f_valid = 0; |
|---|
| 4728 | tr->smoothed = FALSE; |
|---|
| 4729 | tr->rooted = FALSE; |
|---|
| 4730 | |
|---|
| 4731 | is_fact = processTreeCom(fp, tr); |
|---|
| 4732 | |
|---|
| 4733 | p = tr->nodep[(tr->nextnode)++]; |
|---|
| 4734 | if (! treeNeedCh(fp, '(', "at start of")) return FALSE; |
|---|
| 4735 | if (! addElementLen(fp, tr, p)) return FALSE; |
|---|
| 4736 | if (! treeNeedCh(fp, ',', "in")) return FALSE; |
|---|
| 4737 | if (! addElementLen(fp, tr, p->next)) return FALSE; |
|---|
| 4738 | if (! tr->rooted) { |
|---|
| 4739 | if ((ch = treeGetCh(fp)) == ',') { /* An unrooted format */ |
|---|
| 4740 | if (! addElementLen(fp, tr, p->next->next)) return FALSE; |
|---|
| 4741 | } |
|---|
| 4742 | else { /* A rooted format */ |
|---|
| 4743 | tr->rooted = TRUE; |
|---|
| 4744 | if (ch != EOF) (void) ungetc(ch, fp); |
|---|
| 4745 | } |
|---|
| 4746 | } |
|---|
| 4747 | else { |
|---|
| 4748 | p->next->next->back = (nodeptr) NULL; |
|---|
| 4749 | } |
|---|
| 4750 | if (! treeNeedCh(fp, ')', "in")) return FALSE; |
|---|
| 4751 | (void) treeFlushLabel(fp); |
|---|
| 4752 | if (! treeFlushLen(fp)) return FALSE; |
|---|
| 4753 | if (is_fact) { |
|---|
| 4754 | if (! treeNeedCh(fp, ')', "at end of")) return FALSE; |
|---|
| 4755 | if (! treeNeedCh(fp, '.', "at end of")) return FALSE; |
|---|
| 4756 | } |
|---|
| 4757 | else { |
|---|
| 4758 | if (! treeNeedCh(fp, ';', "at end of")) return FALSE; |
|---|
| 4759 | } |
|---|
| 4760 | |
|---|
| 4761 | if (tr->rooted) { |
|---|
| 4762 | p->next->next->back = (nodeptr) NULL; |
|---|
| 4763 | tr->start = uprootTree(tr, p->next->next); |
|---|
| 4764 | if (! tr->start) return FALSE; |
|---|
| 4765 | } |
|---|
| 4766 | else { |
|---|
| 4767 | tr->start = p->next->next->back; /* This is start used by treeString */ |
|---|
| 4768 | } |
|---|
| 4769 | |
|---|
| 4770 | return (initrav(tr, tr->start) && initrav(tr, tr->start->back)); |
|---|
| 4771 | } /* treeReadLen */ |
|---|
| 4772 | |
|---|
| 4773 | |
|---|
| 4774 | /*=======================================================================*/ |
|---|
| 4775 | /* Read a tree from a string */ |
|---|
| 4776 | /*=======================================================================*/ |
|---|
| 4777 | |
|---|
| 4778 | |
|---|
| 4779 | #if Master || Slave |
|---|
| 4780 | int str_treeFinishCom (char **treestrp, char **strp) |
|---|
| 4781 | /* treestrp -- tree string pointer */ |
|---|
| 4782 | /* strp -- comment string pointer */ |
|---|
| 4783 | { /* str_treeFinishCom */ |
|---|
| 4784 | int ch; |
|---|
| 4785 | |
|---|
| 4786 | while ((ch = *(*treestrp)++) != NULL && ch != ']') { |
|---|
| 4787 | if (strp != NULL) *(*strp)++ = ch; /* save character */ |
|---|
| 4788 | if (ch == '[') { /* nested comment; find its end */ |
|---|
| 4789 | if ((ch = str_treeFinishCom(treestrp)) == NULL) break; |
|---|
| 4790 | if (strp != NULL) *(*strp)++ = ch; /* save closing ] */ |
|---|
| 4791 | } |
|---|
| 4792 | } |
|---|
| 4793 | if (strp != NULL) **strp = '\0'; /* terminate string */ |
|---|
| 4794 | return ch; |
|---|
| 4795 | } /* str_treeFinishCom */ |
|---|
| 4796 | |
|---|
| 4797 | |
|---|
| 4798 | int str_treeGetCh (char **treestrp) |
|---|
| 4799 | /* get next nonblank, noncomment character */ |
|---|
| 4800 | { /* str_treeGetCh */ |
|---|
| 4801 | int ch; |
|---|
| 4802 | |
|---|
| 4803 | while ((ch = *(*treestrp)++) != NULL) { |
|---|
| 4804 | if (whitechar(ch)) ; |
|---|
| 4805 | else if (ch == '[') { /* comment; find its end */ |
|---|
| 4806 | if ((ch = str_treeFinishCom(treestrp, (char *) NULL)) == NULL) break; |
|---|
| 4807 | } |
|---|
| 4808 | else break; |
|---|
| 4809 | } |
|---|
| 4810 | |
|---|
| 4811 | return ch; |
|---|
| 4812 | } /* str_treeGetCh */ |
|---|
| 4813 | |
|---|
| 4814 | |
|---|
| 4815 | boolean str_treeGetLabel (char **treestrp, char *lblPtr, int maxlen) |
|---|
| 4816 | { /* str_treeGetLabel */ |
|---|
| 4817 | int ch; |
|---|
| 4818 | boolean done, quoted, lblfound; |
|---|
| 4819 | |
|---|
| 4820 | if (--maxlen < 0) lblPtr = (char *) NULL; /* reserves space for '\0' */ |
|---|
| 4821 | else if (lblPtr == NULL) maxlen = 0; |
|---|
| 4822 | |
|---|
| 4823 | ch = *(*treestrp)++; |
|---|
| 4824 | done = treeLabelEnd(ch); |
|---|
| 4825 | |
|---|
| 4826 | lblfound = ! done; |
|---|
| 4827 | quoted = (ch == '\''); |
|---|
| 4828 | if (quoted && ! done) {ch = *(*treestrp)++; done = (ch == '\0');} |
|---|
| 4829 | |
|---|
| 4830 | while (! done) { |
|---|
| 4831 | if (quoted) { |
|---|
| 4832 | if (ch == '\'') {ch = *(*treestrp)++; if (ch != '\'') break;} |
|---|
| 4833 | } |
|---|
| 4834 | |
|---|
| 4835 | else if (treeLabelEnd(ch)) break; |
|---|
| 4836 | |
|---|
| 4837 | else if (ch == '_') ch = ' '; /* unquoted _ goes to space */ |
|---|
| 4838 | |
|---|
| 4839 | if (--maxlen >= 0) *lblPtr++ = ch; |
|---|
| 4840 | ch = *(*treestrp)++; |
|---|
| 4841 | if (ch == '\0') break; |
|---|
| 4842 | } |
|---|
| 4843 | |
|---|
| 4844 | (*treestrp)--; |
|---|
| 4845 | |
|---|
| 4846 | if (lblPtr != NULL) *lblPtr = '\0'; |
|---|
| 4847 | |
|---|
| 4848 | return lblfound; |
|---|
| 4849 | } /* str_treeGetLabel */ |
|---|
| 4850 | |
|---|
| 4851 | |
|---|
| 4852 | boolean str_treeFlushLabel (char **treestrp) |
|---|
| 4853 | { /* str_treeFlushLabel */ |
|---|
| 4854 | return str_treeGetLabel(treestrp, (char *) NULL, (int) 0); |
|---|
| 4855 | } /* str_treeFlushLabel */ |
|---|
| 4856 | |
|---|
| 4857 | |
|---|
| 4858 | int str_treeFindTipName (char **treestrp, tree *tr) |
|---|
| 4859 | { /* str_treeFindTipName */ |
|---|
| 4860 | nodeptr q; |
|---|
| 4861 | char *nameptr, str[nmlngth+2]; |
|---|
| 4862 | int ch, i, n; |
|---|
| 4863 | |
|---|
| 4864 | if (tr->prelabeled) { |
|---|
| 4865 | if (str_treeGetLabel(treestrp, str, nmlngth+2)) |
|---|
| 4866 | n = treeFindTipByLabel(str, tr); |
|---|
| 4867 | else |
|---|
| 4868 | n = 0; |
|---|
| 4869 | } |
|---|
| 4870 | |
|---|
| 4871 | else if (tr->ntips < tr->mxtips) { |
|---|
| 4872 | n = tr->ntips + 1; |
|---|
| 4873 | nameptr = tr->nodep[n]->name; |
|---|
| 4874 | if (! str_treeGetLabel(treestrp, nameptr, nmlngth+1)) n = 0; |
|---|
| 4875 | } |
|---|
| 4876 | |
|---|
| 4877 | else { |
|---|
| 4878 | n = 0; |
|---|
| 4879 | } |
|---|
| 4880 | |
|---|
| 4881 | return n; |
|---|
| 4882 | } /* str_treeFindTipName */ |
|---|
| 4883 | |
|---|
| 4884 | |
|---|
| 4885 | boolean str_treeProcessLength (char **treestrp, double *dptr) |
|---|
| 4886 | { /* str_treeProcessLength */ |
|---|
| 4887 | int used; |
|---|
| 4888 | |
|---|
| 4889 | if(! str_treeGetCh(treestrp)) return FALSE; /* Skip comments */ |
|---|
| 4890 | (*treestrp)--; |
|---|
| 4891 | |
|---|
| 4892 | if (sscanf(*treestrp, "%lf%n", dptr, & used) != 1) { |
|---|
| 4893 | printf("ERROR: str_treeProcessLength: Problem reading branch length\n"); |
|---|
| 4894 | printf("%40s\n", *treestrp); |
|---|
| 4895 | *dptr = 0.0; |
|---|
| 4896 | return FALSE; |
|---|
| 4897 | } |
|---|
| 4898 | else { |
|---|
| 4899 | *treestrp += used; |
|---|
| 4900 | } |
|---|
| 4901 | |
|---|
| 4902 | return TRUE; |
|---|
| 4903 | } /* str_treeProcessLength */ |
|---|
| 4904 | |
|---|
| 4905 | |
|---|
| 4906 | boolean str_treeFlushLen (char **treestrp) |
|---|
| 4907 | { /* str_treeFlushLen */ |
|---|
| 4908 | int ch; |
|---|
| 4909 | |
|---|
| 4910 | if ((ch = str_treeGetCh(treestrp)) == ':') |
|---|
| 4911 | return str_treeProcessLength(treestrp, (double *) NULL); |
|---|
| 4912 | else { |
|---|
| 4913 | (*treestrp)--; |
|---|
| 4914 | return TRUE; |
|---|
| 4915 | } |
|---|
| 4916 | } /* str_treeFlushLen */ |
|---|
| 4917 | |
|---|
| 4918 | |
|---|
| 4919 | boolean str_treeNeedCh (char **treestrp, int c1, char *where) |
|---|
| 4920 | { /* str_treeNeedCh */ |
|---|
| 4921 | int c2, i; |
|---|
| 4922 | |
|---|
| 4923 | if ((c2 = str_treeGetCh(treestrp)) == c1) return TRUE; |
|---|
| 4924 | |
|---|
| 4925 | printf("ERROR: Missing '%c' %s tree; ", c1, where); |
|---|
| 4926 | if (c2 == '\0') |
|---|
| 4927 | printf("end-of-string"); |
|---|
| 4928 | else { |
|---|
| 4929 | putchar('"'); |
|---|
| 4930 | for (i = 24; i-- && (c2 != '\0'); c2 = *(*treestrp)++) putchar(c2); |
|---|
| 4931 | putchar('"'); |
|---|
| 4932 | } |
|---|
| 4933 | |
|---|
| 4934 | printf(" found instead\n"); |
|---|
| 4935 | return FALSE; |
|---|
| 4936 | } /* str_treeNeedCh */ |
|---|
| 4937 | |
|---|
| 4938 | |
|---|
| 4939 | boolean str_addElementLen (char **treestrp, tree *tr, nodeptr p) |
|---|
| 4940 | { /* str_addElementLen */ |
|---|
| 4941 | double z, branch; |
|---|
| 4942 | nodeptr q; |
|---|
| 4943 | int n, ch; |
|---|
| 4944 | |
|---|
| 4945 | if ((ch = str_treeGetCh(treestrp)) == '(') { /* A new internal node */ |
|---|
| 4946 | n = (tr->nextnode)++; |
|---|
| 4947 | if (n > 2*(tr->mxtips) - 2) { |
|---|
| 4948 | if (tr->rooted || n > 2*(tr->mxtips) - 1) { |
|---|
| 4949 | printf("ERROR: too many internal nodes. Is tree rooted?\n"); |
|---|
| 4950 | printf("Deepest splitting should be a trifurcation.\n"); |
|---|
| 4951 | return FALSE; |
|---|
| 4952 | } |
|---|
| 4953 | else { |
|---|
| 4954 | tr->rooted = TRUE; |
|---|
| 4955 | } |
|---|
| 4956 | } |
|---|
| 4957 | q = tr->nodep[n]; |
|---|
| 4958 | if (! str_addElementLen(treestrp, tr, q->next)) return FALSE; |
|---|
| 4959 | if (! str_treeNeedCh(treestrp, ',', "in")) return FALSE; |
|---|
| 4960 | if (! str_addElementLen(treestrp, tr, q->next->next)) return FALSE; |
|---|
| 4961 | if (! str_treeNeedCh(treestrp, ')', "in")) return FALSE; |
|---|
| 4962 | if (! str_treeFlushLabel(treestrp)) return FALSE; |
|---|
| 4963 | } |
|---|
| 4964 | |
|---|
| 4965 | else { /* A new tip */ |
|---|
| 4966 | n = str_treeFindTipName(treestrp, tr, ch); |
|---|
| 4967 | if (n <= 0) return FALSE; |
|---|
| 4968 | q = tr->nodep[n]; |
|---|
| 4969 | if (tr->start->number > n) tr->start = q; |
|---|
| 4970 | (tr->ntips)++; |
|---|
| 4971 | } /* End of tip processing */ |
|---|
| 4972 | |
|---|
| 4973 | /* Master and Slave always use lengths */ |
|---|
| 4974 | |
|---|
| 4975 | if (! str_treeNeedCh(treestrp, ':', "in")) return FALSE; |
|---|
| 4976 | if (! str_treeProcessLength(treestrp, & branch)) return FALSE; |
|---|
| 4977 | z = exp(-branch / tr->rdta->fracchange); |
|---|
| 4978 | if (z > zmax) z = zmax; |
|---|
| 4979 | hookup(p, q, z); |
|---|
| 4980 | |
|---|
| 4981 | return TRUE; |
|---|
| 4982 | } /* str_addElementLen */ |
|---|
| 4983 | |
|---|
| 4984 | |
|---|
| 4985 | boolean str_processTreeCom(tree *tr, char **treestrp) |
|---|
| 4986 | { /* str_processTreeCom */ |
|---|
| 4987 | char *com, *com_end; |
|---|
| 4988 | int text_started, functor_read, com_open; |
|---|
| 4989 | |
|---|
| 4990 | com = *treestrp; |
|---|
| 4991 | |
|---|
| 4992 | functor_read = text_started = 0; |
|---|
| 4993 | sscanf(com, " p%nhylip_tree(%n", & text_started, & functor_read); |
|---|
| 4994 | if (functor_read) { |
|---|
| 4995 | com += functor_read; |
|---|
| 4996 | } |
|---|
| 4997 | else if (text_started) { |
|---|
| 4998 | com += text_started; |
|---|
| 4999 | sscanf(com, "seudoNewick(%n", & functor_read); |
|---|
| 5000 | if (! functor_read) { |
|---|
| 5001 | printf("Start of tree 'p...' not understood.\n"); |
|---|
| 5002 | return FALSE; |
|---|
| 5003 | } |
|---|
| 5004 | else { |
|---|
| 5005 | com += functor_read; |
|---|
| 5006 | } |
|---|
| 5007 | } |
|---|
| 5008 | |
|---|
| 5009 | com_open = 0; |
|---|
| 5010 | sscanf(com, " [%n", & com_open); |
|---|
| 5011 | com += com_open; |
|---|
| 5012 | |
|---|
| 5013 | if (com_open) { /* comment; read it */ |
|---|
| 5014 | if (!(com_end = strchr(com, ']'))) { |
|---|
| 5015 | printf("Missing end of tree comment.\n"); |
|---|
| 5016 | return FALSE; |
|---|
| 5017 | } |
|---|
| 5018 | |
|---|
| 5019 | *com_end = 0; |
|---|
| 5020 | (void) readKeyValue(com, likelihood_key, "%lg", |
|---|
| 5021 | (void *) &(tr->likelihood)); |
|---|
| 5022 | (void) readKeyValue(com, opt_level_key, "%d", |
|---|
| 5023 | (void *) &(tr->opt_level)); |
|---|
| 5024 | (void) readKeyValue(com, smoothed_key, "%d", |
|---|
| 5025 | (void *) &(tr->smoothed)); |
|---|
| 5026 | *com_end = ']'; |
|---|
| 5027 | com_end++; |
|---|
| 5028 | |
|---|
| 5029 | if (functor_read) { /* remove trailing comma */ |
|---|
| 5030 | text_started = 0; |
|---|
| 5031 | sscanf(com_end, " ,%n", & text_started); |
|---|
| 5032 | com_end += text_started; |
|---|
| 5033 | } |
|---|
| 5034 | |
|---|
| 5035 | *treestrp = com_end; |
|---|
| 5036 | } |
|---|
| 5037 | |
|---|
| 5038 | return (functor_read > 0); |
|---|
| 5039 | } /* str_processTreeCom */ |
|---|
| 5040 | |
|---|
| 5041 | |
|---|
| 5042 | boolean str_treeReadLen (char *treestr, tree *tr) |
|---|
| 5043 | /* read string with representation of tree */ |
|---|
| 5044 | { /* str_treeReadLen */ |
|---|
| 5045 | nodeptr p; |
|---|
| 5046 | int i; |
|---|
| 5047 | boolean is_fact, found; |
|---|
| 5048 | |
|---|
| 5049 | for (i = 1; i <= tr->mxtips; i++) tr->nodep[i]->back = (node *) NULL; |
|---|
| 5050 | tr->start = tr->nodep[tr->mxtips]; |
|---|
| 5051 | tr->ntips = 0; |
|---|
| 5052 | tr->nextnode = tr->mxtips + 1; |
|---|
| 5053 | tr->opt_level = 0; |
|---|
| 5054 | tr->log_f_valid = 0; |
|---|
| 5055 | tr->smoothed = Master; |
|---|
| 5056 | tr->rooted = FALSE; |
|---|
| 5057 | |
|---|
| 5058 | is_fact = str_processTreeCom(tr, & treestr); |
|---|
| 5059 | |
|---|
| 5060 | p = tr->nodep[(tr->nextnode)++]; |
|---|
| 5061 | if (! str_treeNeedCh(& treestr, '(', "at start of")) return FALSE; |
|---|
| 5062 | if (! str_addElementLen(& treestr, tr, p)) return FALSE; |
|---|
| 5063 | if (! str_treeNeedCh(& treestr, ',', "in")) return FALSE; |
|---|
| 5064 | if (! str_addElementLen(& treestr, tr, p->next)) return FALSE; |
|---|
| 5065 | if (! tr->rooted) { |
|---|
| 5066 | if (str_treeGetCh(& treestr) == ',') { /* An unrooted format */ |
|---|
| 5067 | if (! str_addElementLen(& treestr, tr, p->next->next)) return FALSE; |
|---|
| 5068 | } |
|---|
| 5069 | else { /* A rooted format */ |
|---|
| 5070 | p->next->next->back = (nodeptr) NULL; |
|---|
| 5071 | tr->rooted = TRUE; |
|---|
| 5072 | treestr--; |
|---|
| 5073 | } |
|---|
| 5074 | } |
|---|
| 5075 | if (! str_treeNeedCh(& treestr, ')', "in")) return FALSE; |
|---|
| 5076 | if (! str_treeFlushLabel(& treestr)) return FALSE; |
|---|
| 5077 | if (! str_treeFlushLen(& treestr)) return FALSE; |
|---|
| 5078 | if (is_fact) { |
|---|
| 5079 | if (! str_treeNeedCh(& treestr, ')', "at end of")) return FALSE; |
|---|
| 5080 | if (! str_treeNeedCh(& treestr, '.', "at end of")) return FALSE; |
|---|
| 5081 | } |
|---|
| 5082 | else { |
|---|
| 5083 | if (! str_treeNeedCh(& treestr, ';', "at end of")) return FALSE; |
|---|
| 5084 | } |
|---|
| 5085 | |
|---|
| 5086 | if (tr->rooted) if (! uprootTree(tr, p->next->next)) return FALSE; |
|---|
| 5087 | tr->start = p->next->next->back; /* This is start used by treeString */ |
|---|
| 5088 | |
|---|
| 5089 | return (initrav(tr, tr->start) && initrav(tr, tr->start->back)); |
|---|
| 5090 | } /* str_treeReadLen */ |
|---|
| 5091 | #endif |
|---|
| 5092 | |
|---|
| 5093 | |
|---|
| 5094 | boolean treeEvaluate (tree *tr, bestlist *bt) /* Evaluate a user tree */ |
|---|
| 5095 | { /* treeEvaluate */ |
|---|
| 5096 | |
|---|
| 5097 | if (Slave || ! tr->userlen) { |
|---|
| 5098 | if (! smoothTree(tr, 4 * smoothings)) return FALSE; |
|---|
| 5099 | } |
|---|
| 5100 | |
|---|
| 5101 | if (evaluate(tr, tr->start) == badEval) return FALSE; |
|---|
| 5102 | |
|---|
| 5103 | # if ! Slave |
|---|
| 5104 | (void) saveBestTree(bt, tr); |
|---|
| 5105 | # endif |
|---|
| 5106 | return TRUE; |
|---|
| 5107 | } /* treeEvaluate */ |
|---|
| 5108 | |
|---|
| 5109 | |
|---|
| 5110 | #if Master || Slave |
|---|
| 5111 | FILE *freopen_pid (char *filenm, char *mode, FILE *stream) |
|---|
| 5112 | { /* freopen_pid */ |
|---|
| 5113 | char scr[512]; |
|---|
| 5114 | |
|---|
| 5115 | (void) sprintf(scr, "%s.%d", filenm, getpid()); |
|---|
| 5116 | return freopen(scr, mode, stream); |
|---|
| 5117 | } /* freopen_pid */ |
|---|
| 5118 | #endif |
|---|
| 5119 | |
|---|
| 5120 | |
|---|
| 5121 | boolean showBestTrees (bestlist *bt, tree *tr, analdef *adef, FILE *treefile) |
|---|
| 5122 | { /* showBestTrees */ |
|---|
| 5123 | int rank; |
|---|
| 5124 | |
|---|
| 5125 | for (rank = 1; rank <= bt->nvalid; rank++) { |
|---|
| 5126 | if (rank > 1) { |
|---|
| 5127 | if (rank != recallBestTree(bt, rank, tr)) break; |
|---|
| 5128 | } |
|---|
| 5129 | if (evaluate(tr, tr->start) == badEval) return FALSE; |
|---|
| 5130 | if (tr->outgrnode->back) tr->start = tr->outgrnode; |
|---|
| 5131 | printTree(tr, adef); |
|---|
| 5132 | summarize(tr); |
|---|
| 5133 | if (treefile) treeOut(treefile, tr, adef->trout); |
|---|
| 5134 | } |
|---|
| 5135 | |
|---|
| 5136 | return TRUE; |
|---|
| 5137 | } /* showBestTrees */ |
|---|
| 5138 | |
|---|
| 5139 | |
|---|
| 5140 | boolean cmpBestTrees (bestlist *bt, tree *tr) |
|---|
| 5141 | { /* cmpBestTrees */ |
|---|
| 5142 | double sum, sum2, sd, temp, wtemp, bestscore; |
|---|
| 5143 | double *log_f0, *log_f0_ptr; /* Save a copy of best log_f */ |
|---|
| 5144 | double *log_f_ptr; |
|---|
| 5145 | int i, j, num, besttips; |
|---|
| 5146 | |
|---|
| 5147 | num = bt->nvalid; |
|---|
| 5148 | if ((num <= 1) || (tr->cdta->wgtsum <= 1)) return TRUE; |
|---|
| 5149 | |
|---|
| 5150 | if (! (log_f0 = (double *) Malloc(sizeof(double) * tr->cdta->endsite))) { |
|---|
| 5151 | printf("ERROR: cmpBestTrees unable to obtain space for log_f0\n"); |
|---|
| 5152 | return FALSE; |
|---|
| 5153 | } |
|---|
| 5154 | |
|---|
| 5155 | printf("Tree Ln L Diff Ln L Its S.D."); |
|---|
| 5156 | printf(" Significantly worse?\n\n"); |
|---|
| 5157 | |
|---|
| 5158 | for (i = 1; i <= num; i++) { |
|---|
| 5159 | if (i != recallBestTree(bt, i, tr)) break; |
|---|
| 5160 | if (! (tr->log_f_valid)) { |
|---|
| 5161 | if (evaluate(tr, tr->start) == badEval) return FALSE; |
|---|
| 5162 | } |
|---|
| 5163 | |
|---|
| 5164 | printf("%3d%14.5f", i, tr->likelihood); |
|---|
| 5165 | if (i == 1) { |
|---|
| 5166 | printf(" <------ best\n"); |
|---|
| 5167 | besttips = tr->ntips; |
|---|
| 5168 | bestscore = tr->likelihood; |
|---|
| 5169 | log_f0_ptr = log_f0; |
|---|
| 5170 | log_f_ptr = tr->log_f; |
|---|
| 5171 | for (j = 0; j < tr->cdta->endsite; j++) *log_f0_ptr++ = *log_f_ptr++; |
|---|
| 5172 | } |
|---|
| 5173 | else if (tr->ntips != besttips) |
|---|
| 5174 | printf(" (different number of species)\n"); |
|---|
| 5175 | else { |
|---|
| 5176 | sum = sum2 = 0.0; |
|---|
| 5177 | log_f0_ptr = log_f0; |
|---|
| 5178 | log_f_ptr = tr->log_f; |
|---|
| 5179 | for (j = 0; j < tr->cdta->endsite; j++) { |
|---|
| 5180 | temp = *log_f0_ptr++ - *log_f_ptr++; |
|---|
| 5181 | wtemp = tr->cdta->aliaswgt[j] * temp; |
|---|
| 5182 | sum += wtemp; |
|---|
| 5183 | sum2 += wtemp * temp; |
|---|
| 5184 | } |
|---|
| 5185 | sd = sqrt( tr->cdta->wgtsum * (sum2 - sum*sum / tr->cdta->wgtsum) |
|---|
| 5186 | / (tr->cdta->wgtsum - 1) ); |
|---|
| 5187 | printf("%14.5f%14.4f", tr->likelihood - bestscore, sd); |
|---|
| 5188 | printf(" %s\n", (sum > 1.95996 * sd) ? "Yes" : " No"); |
|---|
| 5189 | } |
|---|
| 5190 | } |
|---|
| 5191 | |
|---|
| 5192 | Free(log_f0); |
|---|
| 5193 | printf("\n\n"); |
|---|
| 5194 | |
|---|
| 5195 | return TRUE; |
|---|
| 5196 | } /* cmpBestTrees */ |
|---|
| 5197 | |
|---|
| 5198 | |
|---|
| 5199 | boolean makeUserTree (tree *tr, bestlist *bt, analdef *adef) |
|---|
| 5200 | { /* makeUserTree */ |
|---|
| 5201 | char filename[128]; |
|---|
| 5202 | FILE *treefile; |
|---|
| 5203 | int nusertrees, which; |
|---|
| 5204 | |
|---|
| 5205 | nusertrees = adef->numutrees; |
|---|
| 5206 | |
|---|
| 5207 | printf("User-defined %s:\n\n", (nusertrees == 1) ? "tree" : "trees"); |
|---|
| 5208 | |
|---|
| 5209 | treefile = adef->trout ? fopen_pid("treefile", "w", filename) : (FILE *) NULL; |
|---|
| 5210 | |
|---|
| 5211 | for (which = 1; which <= nusertrees; which++) { |
|---|
| 5212 | if (! treeReadLen(INFILE, tr)) return FALSE; |
|---|
| 5213 | if (! treeEvaluate(tr, bt)) return FALSE; |
|---|
| 5214 | if (tr->global <= 0) { |
|---|
| 5215 | if (tr->outgrnode->back) tr->start = tr->outgrnode; |
|---|
| 5216 | printTree(tr, adef); |
|---|
| 5217 | summarize(tr); |
|---|
| 5218 | if (treefile) treeOut(treefile, tr, adef->trout); |
|---|
| 5219 | } |
|---|
| 5220 | else { |
|---|
| 5221 | printf("%6d: Ln Likelihood =%14.5f\n", which, tr->likelihood); |
|---|
| 5222 | } |
|---|
| 5223 | } |
|---|
| 5224 | |
|---|
| 5225 | if (tr->global > 0) { |
|---|
| 5226 | putchar('\n'); |
|---|
| 5227 | if (! recallBestTree(bt, 1, tr)) return FALSE; |
|---|
| 5228 | printf(" Ln Likelihood =%14.5f\n", tr->likelihood); |
|---|
| 5229 | if (! optimize(tr, tr->global, bt)) return FALSE; |
|---|
| 5230 | if (tr->outgrnode->back) tr->start = tr->outgrnode; |
|---|
| 5231 | printTree(tr, adef); |
|---|
| 5232 | summarize(tr); |
|---|
| 5233 | if (treefile) treeOut(treefile, tr, adef->trout); |
|---|
| 5234 | } |
|---|
| 5235 | |
|---|
| 5236 | if (treefile) { |
|---|
| 5237 | (void) fclose(treefile); |
|---|
| 5238 | printf("Tree also written to %s\n", filename); |
|---|
| 5239 | } |
|---|
| 5240 | |
|---|
| 5241 | putchar('\n'); |
|---|
| 5242 | |
|---|
| 5243 | (void) cmpBestTrees(bt, tr); |
|---|
| 5244 | return TRUE; |
|---|
| 5245 | } /* makeUserTree */ |
|---|
| 5246 | |
|---|
| 5247 | |
|---|
| 5248 | #if Slave |
|---|
| 5249 | boolean slaveTreeEvaluate (tree *tr, bestlist *bt) |
|---|
| 5250 | { /* slaveTreeEvaluate */ |
|---|
| 5251 | boolean done; |
|---|
| 5252 | |
|---|
| 5253 | do { |
|---|
| 5254 | requestForWork(); |
|---|
| 5255 | if (! receiveTree(& comm_master, tr)) return FALSE; |
|---|
| 5256 | done = tr->likelihood > 0.0; |
|---|
| 5257 | if (! done) { |
|---|
| 5258 | if (! treeEvaluate(tr, bt)) return FALSE; |
|---|
| 5259 | if (! sendTree(& comm_master, tr)) return FALSE; |
|---|
| 5260 | } |
|---|
| 5261 | } while (! done); |
|---|
| 5262 | |
|---|
| 5263 | return TRUE; |
|---|
| 5264 | } /* slaveTreeEvaluate */ |
|---|
| 5265 | #endif |
|---|
| 5266 | |
|---|
| 5267 | |
|---|
| 5268 | double randum (long *seed) |
|---|
| 5269 | /* random number generator, modified to use 12 bit chunks */ |
|---|
| 5270 | { /* randum */ |
|---|
| 5271 | long sum, mult0, mult1, seed0, seed1, seed2, newseed0, newseed1, newseed2; |
|---|
| 5272 | |
|---|
| 5273 | mult0 = 1549; |
|---|
| 5274 | seed0 = *seed & 4095; |
|---|
| 5275 | sum = mult0 * seed0; |
|---|
| 5276 | newseed0 = sum & 4095; |
|---|
| 5277 | sum >>= 12; |
|---|
| 5278 | seed1 = (*seed >> 12) & 4095; |
|---|
| 5279 | mult1 = 406; |
|---|
| 5280 | sum += mult0 * seed1 + mult1 * seed0; |
|---|
| 5281 | newseed1 = sum & 4095; |
|---|
| 5282 | sum >>= 12; |
|---|
| 5283 | seed2 = (*seed >> 24) & 255; |
|---|
| 5284 | sum += mult0 * seed2 + mult1 * seed1; |
|---|
| 5285 | newseed2 = sum & 255; |
|---|
| 5286 | |
|---|
| 5287 | *seed = newseed2 << 24 | newseed1 << 12 | newseed0; |
|---|
| 5288 | return 0.00390625 * (newseed2 |
|---|
| 5289 | + 0.000244140625 * (newseed1 |
|---|
| 5290 | + 0.000244140625 * newseed0)); |
|---|
| 5291 | } /* randum */ |
|---|
| 5292 | |
|---|
| 5293 | |
|---|
| 5294 | boolean makeDenovoTree (tree *tr, bestlist *bt, analdef *adef) |
|---|
| 5295 | { /* makeDenovoTree */ |
|---|
| 5296 | char filename[128]; |
|---|
| 5297 | FILE *treefile; |
|---|
| 5298 | nodeptr p; |
|---|
| 5299 | int *enterorder; /* random entry order */ |
|---|
| 5300 | int i, j, k, nextsp, newsp, maxtrav, tested; |
|---|
| 5301 | |
|---|
| 5302 | double randum(); |
|---|
| 5303 | |
|---|
| 5304 | |
|---|
| 5305 | enterorder = (int *) Malloc(sizeof(int) * (tr->mxtips + 1)); |
|---|
| 5306 | if (! enterorder) { |
|---|
| 5307 | fprintf(stderr, "makeDenovoTree: Malloc failure for enterorder\n"); |
|---|
| 5308 | return 0; |
|---|
| 5309 | } |
|---|
| 5310 | |
|---|
| 5311 | if (adef->restart) { |
|---|
| 5312 | printf("Restarting from tree with the following sequences:\n"); |
|---|
| 5313 | tr->userlen = TRUE; |
|---|
| 5314 | if (! treeReadLen(INFILE, tr)) return FALSE; |
|---|
| 5315 | if (! smoothTree(tr, smoothings)) return FALSE; |
|---|
| 5316 | if (evaluate(tr, tr->start) == badEval) return FALSE; |
|---|
| 5317 | if (saveBestTree(bt, tr) < 1) return FALSE; |
|---|
| 5318 | |
|---|
| 5319 | for (i = 1, j = tr->ntips; i <= tr->mxtips; i++) { /* find loose tips */ |
|---|
| 5320 | if (! tr->nodep[i]->back) { |
|---|
| 5321 | enterorder[++j] = i; |
|---|
| 5322 | } |
|---|
| 5323 | else { |
|---|
| 5324 | printf(" %s\n", tr->nodep[i]->name); |
|---|
| 5325 | |
|---|
| 5326 | # if Master |
|---|
| 5327 | if (i>3) REPORT_ADD_SPECS; |
|---|
| 5328 | # endif |
|---|
| 5329 | } |
|---|
| 5330 | } |
|---|
| 5331 | putchar('\n'); |
|---|
| 5332 | } |
|---|
| 5333 | |
|---|
| 5334 | else { /* start from scratch */ |
|---|
| 5335 | tr->ntips = 0; |
|---|
| 5336 | for (i = 1; i <= tr->mxtips; i++) enterorder[i] = i; |
|---|
| 5337 | } |
|---|
| 5338 | |
|---|
| 5339 | if (adef->jumble) for (i = tr->ntips + 1; i <= tr->mxtips; i++) { |
|---|
| 5340 | j = randum(&(adef->jumble))*(tr->mxtips - tr->ntips) + tr->ntips + 1; |
|---|
| 5341 | k = enterorder[j]; |
|---|
| 5342 | enterorder[j] = enterorder[i]; |
|---|
| 5343 | enterorder[i] = k; |
|---|
| 5344 | } |
|---|
| 5345 | |
|---|
| 5346 | bt->numtrees = 1; |
|---|
| 5347 | if (tr->ntips < tr->mxtips) printf("Adding species:\n"); |
|---|
| 5348 | |
|---|
| 5349 | if (tr->ntips == 0) { |
|---|
| 5350 | for (i = 1; i <= 3; i++) { |
|---|
| 5351 | printf(" %s\n", tr->nodep[enterorder[i]]->name); |
|---|
| 5352 | } |
|---|
| 5353 | tr->nextnode = tr->mxtips + 1; |
|---|
| 5354 | if (! buildSimpleTree(tr, enterorder[1], enterorder[2], enterorder[3])) |
|---|
| 5355 | return FALSE; |
|---|
| 5356 | } |
|---|
| 5357 | |
|---|
| 5358 | while (tr->ntips < tr->mxtips || tr->opt_level < tr->global) { |
|---|
| 5359 | maxtrav = (tr->ntips == tr->mxtips) ? tr->global : tr->partswap; |
|---|
| 5360 | if (maxtrav > tr->ntips - 3) maxtrav = tr->ntips - 3; |
|---|
| 5361 | |
|---|
| 5362 | if (tr->opt_level >= maxtrav) { |
|---|
| 5363 | nextsp = ++(tr->ntips); |
|---|
| 5364 | newsp = enterorder[nextsp]; |
|---|
| 5365 | p = tr->nodep[newsp]; |
|---|
| 5366 | printf(" %s\n", p->name); |
|---|
| 5367 | |
|---|
| 5368 | # if Master |
|---|
| 5369 | if (nextsp % DNAML_STEP_TIME_COUNT == 1) { |
|---|
| 5370 | REPORT_STEP_TIME; |
|---|
| 5371 | } |
|---|
| 5372 | REPORT_ADD_SPECS; |
|---|
| 5373 | # endif |
|---|
| 5374 | |
|---|
| 5375 | (void) buildNewTip(tr, p); |
|---|
| 5376 | |
|---|
| 5377 | resetBestTree(bt); |
|---|
| 5378 | cacheZ(tr); |
|---|
| 5379 | tested = addTraverse(tr, p->back, findAnyTip(tr->start)->back, |
|---|
| 5380 | 1, tr->ntips - 2, bt, adef->qadd); |
|---|
| 5381 | if (tested == badRear) return FALSE; |
|---|
| 5382 | bt->numtrees += tested; |
|---|
| 5383 | |
|---|
| 5384 | # if Master |
|---|
| 5385 | getReturnedTrees(tr, bt, tested); |
|---|
| 5386 | # endif |
|---|
| 5387 | |
|---|
| 5388 | printf(" Tested %d alternative trees\n", tested); |
|---|
| 5389 | |
|---|
| 5390 | |
|---|
| 5391 | |
|---|
| 5392 | (void) recallBestTree(bt, 1, tr); |
|---|
| 5393 | if (! tr->smoothed) { |
|---|
| 5394 | if (! smoothTree(tr, smoothings)) return FALSE; |
|---|
| 5395 | if (evaluate(tr, tr->start) == badEval) return FALSE; |
|---|
| 5396 | (void) saveBestTree(bt, tr); |
|---|
| 5397 | } |
|---|
| 5398 | |
|---|
| 5399 | if (tr->ntips == 4) tr->opt_level = 1; /* All 4 taxon trees done */ |
|---|
| 5400 | maxtrav = (tr->ntips == tr->mxtips) ? tr->global : tr->partswap; |
|---|
| 5401 | if (maxtrav > tr->ntips - 3) maxtrav = tr->ntips - 3; |
|---|
| 5402 | } |
|---|
| 5403 | |
|---|
| 5404 | printf(" Ln Likelihood =%14.5f\n", tr->likelihood); |
|---|
| 5405 | if (! optimize(tr, maxtrav, bt)) return FALSE; |
|---|
| 5406 | } |
|---|
| 5407 | |
|---|
| 5408 | printf("\nExamined %d %s\n", bt->numtrees, |
|---|
| 5409 | bt->numtrees != 1 ? "trees" : "tree"); |
|---|
| 5410 | |
|---|
| 5411 | treefile = adef->trout ? fopen_pid("treefile", "w", filename) : (FILE *) NULL; |
|---|
| 5412 | (void) showBestTrees(bt, tr, adef, treefile); |
|---|
| 5413 | if (treefile) { |
|---|
| 5414 | (void) fclose(treefile); |
|---|
| 5415 | printf("Tree also written to %s\n\n", filename); |
|---|
| 5416 | } |
|---|
| 5417 | |
|---|
| 5418 | (void) cmpBestTrees(bt, tr); |
|---|
| 5419 | |
|---|
| 5420 | # if DeleteCheckpointFile |
|---|
| 5421 | unlink_pid(checkpointname); |
|---|
| 5422 | # endif |
|---|
| 5423 | |
|---|
| 5424 | Free(enterorder); |
|---|
| 5425 | |
|---|
| 5426 | return TRUE; |
|---|
| 5427 | } /* makeDenovoTree */ |
|---|
| 5428 | |
|---|
| 5429 | /*==========================================================================*/ |
|---|
| 5430 | /* "main" routine */ |
|---|
| 5431 | /*==========================================================================*/ |
|---|
| 5432 | |
|---|
| 5433 | #if Sequential |
|---|
| 5434 | int main () |
|---|
| 5435 | #else |
|---|
| 5436 | slave () |
|---|
| 5437 | #endif |
|---|
| 5438 | { /* DNA Maximum Likelihood */ |
|---|
| 5439 | # if Master |
|---|
| 5440 | int starttime, inputtime, endtime; |
|---|
| 5441 | # endif |
|---|
| 5442 | |
|---|
| 5443 | # if Master || Slave |
|---|
| 5444 | int my_id, nprocs, type, from, sz; |
|---|
| 5445 | char *msg; |
|---|
| 5446 | # endif |
|---|
| 5447 | |
|---|
| 5448 | analdef *adef; |
|---|
| 5449 | rawdata *rdta; |
|---|
| 5450 | cruncheddata *cdta; |
|---|
| 5451 | tree *tr; /* current tree */ |
|---|
| 5452 | bestlist *bt; /* topology of best found tree */ |
|---|
| 5453 | |
|---|
| 5454 | globTime = gettime(); |
|---|
| 5455 | |
|---|
| 5456 | # if Debug |
|---|
| 5457 | { |
|---|
| 5458 | char debugfilename[128]; |
|---|
| 5459 | debug = fopen_pid("dnaml_debug", "w", debugfilename); |
|---|
| 5460 | } |
|---|
| 5461 | # endif |
|---|
| 5462 | |
|---|
| 5463 | # if Master |
|---|
| 5464 | starttime = p4_clock(); |
|---|
| 5465 | nprocs = p4_num_total_slaves(); |
|---|
| 5466 | |
|---|
| 5467 | if ((OUTFILE = freopen_pid("master.out", "w", stdout)) == NULL) { |
|---|
| 5468 | fprintf(stderr, "Could not open output file\n"); |
|---|
| 5469 | exit(1); |
|---|
| 5470 | } |
|---|
| 5471 | |
|---|
| 5472 | /* Receive input file name from host */ |
|---|
| 5473 | type = DNAML_FILE_NAME; |
|---|
| 5474 | from = DNAML_HOST_ID; |
|---|
| 5475 | msg = NULL; |
|---|
| 5476 | p4_recv(& type, & from, & msg, & sz); |
|---|
| 5477 | if ((INFILE = fopen(msg, "r")) == NULL) { |
|---|
| 5478 | fprintf(stderr, "master could not open input file %s\n", msg); |
|---|
| 5479 | exit(1); |
|---|
| 5480 | } |
|---|
| 5481 | p4_msg_free(msg); |
|---|
| 5482 | |
|---|
| 5483 | open_link(& comm_slave); |
|---|
| 5484 | # endif |
|---|
| 5485 | |
|---|
| 5486 | # if DNAML_STEP |
|---|
| 5487 | begin_step_time = starttime; |
|---|
| 5488 | # endif |
|---|
| 5489 | |
|---|
| 5490 | # if Slave |
|---|
| 5491 | my_id = p4_get_my_id(); |
|---|
| 5492 | nprocs = p4_num_total_slaves(); |
|---|
| 5493 | |
|---|
| 5494 | /* Receive input file name from host */ |
|---|
| 5495 | type = DNAML_FILE_NAME; |
|---|
| 5496 | from = DNAML_HOST_ID; |
|---|
| 5497 | msg = NULL; |
|---|
| 5498 | p4_recv(& type, & from, & msg, & sz); |
|---|
| 5499 | if ((INFILE = fopen(msg, "r")) == NULL) { |
|---|
| 5500 | fprintf(stderr, "slave could not open input file %s\n",msg); |
|---|
| 5501 | exit(1); |
|---|
| 5502 | } |
|---|
| 5503 | p4_msg_free(msg); |
|---|
| 5504 | |
|---|
| 5505 | # ifdef P4DEBUG |
|---|
| 5506 | if ((OUTFILE = freopen_pid("slave.out", "w", stdout)) == NULL) { |
|---|
| 5507 | fprintf(stderr, "Could not open output file\n"); |
|---|
| 5508 | exit(1); |
|---|
| 5509 | } |
|---|
| 5510 | # else |
|---|
| 5511 | if ((OUTFILE = freopen("/dev/null", "w", stdout)) == NULL) { |
|---|
| 5512 | fprintf(stderr, "Could not open output file\n"); |
|---|
| 5513 | exit(1); |
|---|
| 5514 | } |
|---|
| 5515 | # endif |
|---|
| 5516 | |
|---|
| 5517 | open_link(& comm_master); |
|---|
| 5518 | # endif |
|---|
| 5519 | |
|---|
| 5520 | |
|---|
| 5521 | /* Get data structure memory */ |
|---|
| 5522 | |
|---|
| 5523 | if (! (adef = (analdef *) Malloc(sizeof(analdef)))) { |
|---|
| 5524 | printf("ERROR: Unable to get memory for analysis definition\n\n"); |
|---|
| 5525 | return 1; |
|---|
| 5526 | } |
|---|
| 5527 | |
|---|
| 5528 | if (! (rdta = (rawdata *) Malloc(sizeof(rawdata)))) { |
|---|
| 5529 | printf("ERROR: Unable to get memory for raw DNA\n\n"); |
|---|
| 5530 | return 1; |
|---|
| 5531 | } |
|---|
| 5532 | |
|---|
| 5533 | if (! (cdta = (cruncheddata *) Malloc(sizeof(cruncheddata)))) { |
|---|
| 5534 | printf("ERROR: Unable to get memory for crunched DNA\n\n"); |
|---|
| 5535 | return 1; |
|---|
| 5536 | } |
|---|
| 5537 | |
|---|
| 5538 | if ((tr = (tree *) Malloc(sizeof(tree))) && |
|---|
| 5539 | (bt = (bestlist *) Malloc(sizeof(bestlist)))) ; |
|---|
| 5540 | else { |
|---|
| 5541 | printf("ERROR: Unable to get memory for trees\n\n"); |
|---|
| 5542 | return 1; |
|---|
| 5543 | } |
|---|
| 5544 | bt->ninit = 0; |
|---|
| 5545 | |
|---|
| 5546 | if (! getinput(adef, rdta, cdta, tr)) return 1; |
|---|
| 5547 | |
|---|
| 5548 | # if Master |
|---|
| 5549 | inputtime = p4_clock(); |
|---|
| 5550 | printf("Input time %d milliseconds\n", inputtime - starttime); |
|---|
| 5551 | REPORT_STEP_TIME; |
|---|
| 5552 | # endif |
|---|
| 5553 | |
|---|
| 5554 | # if Slave |
|---|
| 5555 | (void) fclose(INFILE); |
|---|
| 5556 | # endif |
|---|
| 5557 | |
|---|
| 5558 | /* The material below would be a loop over jumbles and/or boots */ |
|---|
| 5559 | |
|---|
| 5560 | if (! makeweights(adef, rdta, cdta)) return 1; |
|---|
| 5561 | if (! makevalues(rdta, cdta)) return 1; |
|---|
| 5562 | if (adef->empf && ! empiricalfreqs(rdta, cdta)) return 1; |
|---|
| 5563 | reportfreqs(adef, rdta); |
|---|
| 5564 | if (! linkdata2tree(rdta, cdta, tr)) return 1; |
|---|
| 5565 | |
|---|
| 5566 | if (! linkxarray(3, 3, cdta->endsite, & freextip, & usedxtip)) return 1; |
|---|
| 5567 | if (! setupnodex(tr)) return 1; |
|---|
| 5568 | |
|---|
| 5569 | |
|---|
| 5570 | /* AxML modification start*/ |
|---|
| 5571 | |
|---|
| 5572 | if( ! setupEqualityVector(tr)) return 1; |
|---|
| 5573 | |
|---|
| 5574 | /* initialize equality vectors */ |
|---|
| 5575 | |
|---|
| 5576 | |
|---|
| 5577 | /* AxML modification end*/ |
|---|
| 5578 | |
|---|
| 5579 | # if Slave |
|---|
| 5580 | if (! slaveTreeEvaluate(tr, bt)) return 1; |
|---|
| 5581 | # else |
|---|
| 5582 | if (! initBestTree(bt, adef->nkeep, tr->mxtips, tr->cdta->endsite)) return 1; |
|---|
| 5583 | if (! adef->usertree) { |
|---|
| 5584 | if (! makeDenovoTree(tr, bt, adef)) return 1; |
|---|
| 5585 | } |
|---|
| 5586 | else { |
|---|
| 5587 | if (! makeUserTree(tr, bt, adef)) return 1; |
|---|
| 5588 | } |
|---|
| 5589 | if (! freeBestTree(bt)) return 1; |
|---|
| 5590 | # endif |
|---|
| 5591 | |
|---|
| 5592 | /* Endpoint for jumble and/or boot loop */ |
|---|
| 5593 | |
|---|
| 5594 | # if Master |
|---|
| 5595 | tr->likelihood = 1.0; /* terminate slaves */ |
|---|
| 5596 | (void) sendTree(& comm_slave, tr); |
|---|
| 5597 | # endif |
|---|
| 5598 | |
|---|
| 5599 | freeTree(tr); |
|---|
| 5600 | |
|---|
| 5601 | # if Master |
|---|
| 5602 | close_link(& comm_slave); |
|---|
| 5603 | (void) fclose(INFILE); |
|---|
| 5604 | |
|---|
| 5605 | REPORT_STEP_TIME; |
|---|
| 5606 | endtime = p4_clock(); |
|---|
| 5607 | printf("Execution time %d milliseconds\n", endtime - inputtime); |
|---|
| 5608 | (void) fclose(OUTFILE); |
|---|
| 5609 | # endif |
|---|
| 5610 | |
|---|
| 5611 | # if Slave |
|---|
| 5612 | close_link(& comm_master); |
|---|
| 5613 | (void) fclose(OUTFILE); |
|---|
| 5614 | # endif |
|---|
| 5615 | |
|---|
| 5616 | # if Debug |
|---|
| 5617 | (void) fclose(debug); |
|---|
| 5618 | # endif |
|---|
| 5619 | |
|---|
| 5620 | # if Master || Slave |
|---|
| 5621 | p4_send(DNAML_DONE, DNAML_HOST_ID, NULL, 0); |
|---|
| 5622 | # else |
|---|
| 5623 | return 0; |
|---|
| 5624 | # endif |
|---|
| 5625 | } /* DNA Maximum Likelihood */ |
|---|