1 /*
2 * MrBayes 3
3 *
4 * (c) 2002-2013
5 *
6 * John P. Huelsenbeck
7 * Dept. Integrative Biology
8 * University of California, Berkeley
9 * Berkeley, CA 94720-3140
10 * johnh@berkeley.edu
11 *
12 * Fredrik Ronquist
13 * Swedish Museum of Natural History
14 * Box 50007
15 * SE-10405 Stockholm, SWEDEN
16 * fredrik.ronquist@nrm.se
17 *
18 * With important contributions by
19 *
20 * Paul van der Mark (paulvdm@sc.fsu.edu)
21 * Maxim Teslenko (maxkth@gmail.com)
22 * Chi Zhang (zhangchicool@gmail.com)
23 *
24 * and by many users (run 'acknowledgments' to see more info)
25 *
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version 2
29 * of the License, or (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details (www.gnu.org).
35 *
36 */
37
38 #undef DEBUG_TIME
39
40 #if defined (DEBUG_TIME)
41 #define _POSIX_C_SOURCE 199309L
42 #include <sys/time.h>
43 #endif
44
45 #include "bayes.h"
46 #include "best.h"
47 #include "command.h"
48 #include "likelihood.h"
49 #include "mbbeagle.h"
50 #include "mcmc.h"
51 #include "model.h"
52 #include "proposal.h"
53 #include "sumpt.h"
54 #include "utils.h"
55 #if defined(__MWERKS__)
56 #include "SIOUX.h"
57 #endif
58 #include <signal.h>
59
60 #if defined (WIN_VERSION) && !defined (__GNUC__)
61 #define VISUAL
62 #else
63 typedef void (*sighandler_t) (int);
64 #endif
65
66 #define GIBBS_SAMPLE_FREQ 100 /* generations between gibbs sampling of gamma cats */
67 #define MAX_SMALL_JUMP 10 /* threshold for precalculating trans probs of adgamma model */
68 #define BIG_JUMP 100 /* threshold for using stationary approximation */
69 #define MAX_RUNS 120 /* maximum number of independent runs */
70 #define PFILE 0
71 #define TFILE 1
72 #define CALFILE 2
73 #define MCMCFILE 3
74 #define MAXTUNINGPARAM 10000 /* limit to ensure convergence for autotuning */
75 #define SAMPLE_ALL_SS /* if defined makes ss sample every generation instead of every sample frequency */
76 #define BEAGLE_RESCALE_FREQ 160
77 #define BEAGLE_RESCALE_FREQ_DOUBLE 10 /* The factor by which BEAGLE_RESCALE_FREQ get multiplied if double presicion is used */
78 #define TARGETLENDELTA 100
79
80 /* debugging compiler statements */
81 #undef DEBUG_SETUPTERMSTATE
82 #undef DEBUG_RUNCHAIN
83 #undef DEBUG_NOSHORTCUTS
84 #undef DEBUG_NOSCALING
85 #undef DEBUG_TIPROBS_STD
86 #undef DEBUG_RUN_WITHOUT_DATA
87 #undef DEBUG_CONSTRAINTS
88 #undef DEBUG_LNLIKELIHOOD /* slow if defined!! */
89 #undef DEBUG_LIKELIHOOD
90 #undef DEBUG_FBDPR // #undef FBDPR_CondOnN
91 #undef SHOW_MOVE
92
93 #if defined (MPI_ENABLED)
94 #define ERROR_TEST2(failString,X1,X2) \
95 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);\
96 if (sumErrors > 0)\
97 {\
98 MrBayesPrint ("%s "failString"\n", spacer);\
99 X1;X2;\
100 }
101 #else
102 #define ERROR_TEST2(failString,X1,X2) \
103 if (nErrors > 0)\
104 {\
105 MrBayesPrint ("%s "failString"\n", spacer);\
106 X1;X2;\
107 }
108 #endif
109
110 /* local (to this file) data types */
111 typedef struct pfnode
112 {
113 struct pfnode *left;
114 struct pfnode *right;
115 int *count;
116 BitsLong *partition;
117 } PFNODE;
118
119 /* local prototypes */
120 int AddTreeSamples (int from, int to, int saveToList);
121 PFNODE *AddPartition (PFNODE *r, BitsLong *p, int runId);
122 int AddTreeToPartitionCounters (Tree *tree, int treeId, int runId);
123 int AttemptSwap (int swapA, int swapB, RandLong *seed);
124 void BuildExhaustiveSearchTree (Tree *t, int chain, int nTaxInTree, TreeInfo *tInfo);
125 int BuildStepwiseTree (Tree *t, int chain, RandLong *seed);
126 int CalcLikeAdgamma (int d, Param *param, int chain, MrBFlt *lnL);
127 void CalcPartFreqStats (PFNODE *p, STATS *stat);
128 void CalcTopoConvDiagn (int numSamples);
129 #ifdef VISUAL
130 BOOL WINAPI CatchInterrupt (DWORD signum);
131 #else
132 void CatchInterrupt (int signum);
133 #endif
134 int CheckTemperature (void);
135 void CloseMBPrintFiles (void);
136 PFNODE *CompactTree (PFNODE *p);
137 int ConfirmAbortRun(void);
138 void CopyParams (int chain);
139 void CopyPFNodeDown (PFNODE *p);
140 void CopySiteScalers (ModelInfo *m, int chain);
141 void CopyTrees (int chain);
142 int ExtendChainQuery (void);
143 int FillNumSitesOfPat (void);
144 TreeNode *FindBestNode (Tree *t, TreeNode *p, TreeNode *addNode, CLFlt *minLength, int chain);
145 void FlipCijkSpace (ModelInfo *m, int chain);
146 void FlipCondLikeSpace (ModelInfo *m, int chain, int nodeIndex);
147 void FlipNodeScalerSpace (ModelInfo *m, int chain, int nodeIndex);
148 void FlipSiteScalerSpace (ModelInfo *m, int chain);
149 void FlipTiProbsSpace (ModelInfo *m, int chain, int nodeIndex);
150 void FreeChainMemory (void);
151 MrBFlt GetFitchPartials (ModelInfo *m, int chain, int source1, int source2, int destination);
152 void GetStamp (void);
153 void GetSwappers (int *swapA, int *swapB, int curGen);
154 void GetTempDownPassSeq (TreeNode *p, int *i, TreeNode **dp);
155 int GetTotalRateShifts (Model *mp, MrBFlt *shiftTimes);
156 MrBFlt GibbsSampleGamma (int chain, int division, RandLong *seed);
157 int InitAdGamma(void);
158 int InitChainCondLikes (void);
159 int InitClockBrlens (Tree *t);
160 int InitEigenSystemInfo (ModelInfo *m);
161 int InitInvCondLikes (void);
162 int InitParsSets (void);
163 int InitPrintParams (void);
164 int IsPFNodeEmpty (PFNODE *p);
165 PFNODE *LargestNonemptyPFNode (PFNODE *p, int *i, int j);
166 MrBFlt LogLike (int chain);
167 MrBFlt LogPrior (int chain);
168 int LnBirthDeathPriorPrRandom (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt sR, MrBFlt eR, MrBFlt sF);
169 int LnBirthDeathPriorPrDiversity (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt sR, MrBFlt eR, MrBFlt sF);
170 int LnBirthDeathPriorPrCluster (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt sR, MrBFlt eR, MrBFlt sF);
171 int LnFossilizedBDPriorFossilTip (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt *sR, MrBFlt *eR, MrBFlt *fR, MrBFlt sF);
172 int LnFossilizedBDPriorRandom (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt *sR, MrBFlt *eR, MrBFlt *fR, MrBFlt sF);
173 int LnFossilizedBDPriorDiversity (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt *sR, MrBFlt *eR, MrBFlt *fR, MrBFlt sF);
174 MrBFlt LnP0 (MrBFlt t, MrBFlt l, MrBFlt m);
175 MrBFlt LnP0Subsample (MrBFlt t, MrBFlt l, MrBFlt m, MrBFlt f);
176 MrBFlt LnP1 (MrBFlt t, MrBFlt l, MrBFlt m);
177 MrBFlt LnP1Subsample (MrBFlt t, MrBFlt l, MrBFlt m, MrBFlt f);
178 MrBFlt LnP0_fossil (MrBFlt t, MrBFlt lambda, MrBFlt mu, MrBFlt psi, MrBFlt c1, MrBFlt c2);
179 MrBFlt LnP1_fossil (MrBFlt t, MrBFlt rho, MrBFlt c1, MrBFlt c2);
180 MrBFlt LnQi_fossil (MrBFlt t, MrBFlt *t_f, int sl, MrBFlt *c1, MrBFlt *c2);
181 MrBFlt LnPi_fossil (MrBFlt t, MrBFlt *t_f, int sl, MrBFlt *c1, MrBFlt *c2, MrBFlt *lambda, MrBFlt *mu, MrBFlt *psi);
182 int NewtonRaphsonBrlen (Tree *t, TreeNode *p, int chain);
183 void NodeToNodeDistances (Tree *t, TreeNode *fromNode);
184 int PickProposal (RandLong *seed, int chainIndex);
185 int NumCppEvents (Param *p, int chain);
186 int PosSelProbs (TreeNode *p, int division, int chain);
187 #if defined (SSE_ENABLED)
188 int PosSelProbs_SSE (TreeNode *p, int division, int chain);
189 #endif
190 int PreparePrintFiles (void);
191 int PrintAncStates_Bin (TreeNode *p, int division, int chain);
192 int PrintAncStates_Gen (TreeNode *p, int division, int chain);
193 int PrintAncStates_NUC4 (TreeNode *p, int division, int chain);
194 int PrintAncStates_Std (TreeNode *p, int division, int chain);
195 int PrintCalTree (int curGen, Tree *tree);
196 int PrintCheckPoint (int gen);
197 int PrintMCMCDiagnosticsToFile (int curGen);
198 #if defined (MPI_ENABLED)
199 int PrintMPISlaves (FILE *fp);
200 #endif
201 void PrintParamValues (Param *p, int chain, char *s);
202 int PrintParsMatrix (void);
203 int PrintSiteRates_Gen (TreeNode *p, int division, int chain);
204 int PrintSiteRates_Std (TreeNode *p, int division, int chain);
205 int PrintStates (int curGen, int coldId);
206 int PrintStatesToFiles (int n);
207 int PrintSwapInfo (void);
208 int PrintTermState (void);
209 void PrintTiProbs (CLFlt *tP, MrBFlt *bs, int nStates);
210 int PrintTopConvInfo (void);
211 void PrintToScreen (int curGen, int startGen, time_t endingT, time_t startingT);
212 int PrintTree (int curGen, Param *treeParam, int chain, int showBrlens, MrBFlt clockRate);
213 MrBFlt PropAncFossil (Param *param, int chain);
214 #if defined (MPI_ENABLED)
215 int ReassembleMoveInfo (void);
216 int ReassembleParamVals (int *curId);
217 int ReassembleSwapInfo (void);
218 int ReassembleTuningParams (void);
219 void RedistributeMoveInfo (void);
220 int RedistributeParamVals (void);
221 int RedistributeTuningParams (void);
222 #endif
223 int RemovePartition (PFNODE *r, BitsLong *p, int runId);
224 int RemoveTreeFromPartitionCounters (Tree *tree, int treeId, int runId);
225 int RemoveTreeSamples (int from, int to);
226 int ReopenMBPrintFiles (void);
227 void ResetChainIds (void);
228 void ResetFlips(int chain);
229 void ResetSiteScalers (ModelInfo *m, int chain);
230 int ReusePreviousResults(int *numSamples, int);
231 int RunChain (RandLong *seed);
232 int SafeSprintf (char **target, int *targetLen, char *fmt, ...);
233 void SetChainIds (void);
234 void SetFileNames (void);
235 int SetLikeFunctions (void);
236 int SetLocalChainsAndDataSplits (void);
237 int SetModelInfo (void);
238 int SetMoves (void);
239 int SetBinaryQMatrix (MrBFlt **a, int whichChain, int division);
240 int SetNucQMatrix (MrBFlt **a, int n, int whichChain, int division, MrBFlt rateMult, MrBFlt *rA, MrBFlt *rS);
241 int SetProteinQMatrix (MrBFlt **a, int n, int whichChain, int division, MrBFlt rateMult);
242 int SetStdQMatrix (MrBFlt **a, int nStates, MrBFlt *bs, int cType);
243 int SetUpPartitionCounters (void);
244 int SetUpTermState (void);
245 int SetUsedMoves (void);
246 int ShowMoveSummary (void);
247 void ShowValuesForChain (int chn);
248 int SiteOmegas (TreeNode *p, int division, int chain);
249 #if defined (SSE_ENABLED)
250 int SiteOmegas_SSE (TreeNode *p, int division, int chain);
251 #endif
252 PFNODE *SmallestNonemptyPFNode (PFNODE *p, int *i, int j);
253 PFNODE *Talloc (void);
254 void Tfree (PFNODE *r);
255 MrBFlt Temperature (int x);
256 void TouchAllCijks (int chain);
257 void TouchAllPartitions (void);
258 void TouchAllTrees (int chain);
259 void TouchEverything (int chain);
260
261 /* globals declared here and used elsewhere */
262 int *bsIndex; /* compressed std stat freq index */
263 Chain chainParams; /* parameters of Markov chain */
264 int *compCharPos; /* char position in compressed matrix */
265 int *compColPos; /* column position in compressed matrix */
266 BitsLong *compMatrix; /* compressed character matrix */
267 int compMatrixRowSize; /* row size of compressed matrix */
268 char inputFileName[100]; /* input (NEXUS) file name */
269 MoveType moveTypes[NUM_MOVE_TYPES]; /* holds information on the move types */
270 int numCompressedChars; /* number of compressed characters */
271 int numMoveTypes; /* the number of move types */
272 CLFlt *numSitesOfPat; /* no. sites of each pattern */
273 int *origChar; /* index from compressed char to original char */
274 char stamp[11]; /* holds a unique identifier for each analysis */
275 MrBFlt *stdStateFreqs; /* std char state frequencies */
276 int *stdType; /* compressed std char type: ord, unord, irrev */
277 int *tiIndex; /* compressed std char ti index */
278
279 #if defined (BEAGLE_ENABLED)
280 int recalcScalers; /* shoud we recalculate scalers for current state YES/NO */
281 #endif
282
283 /* globals used here but declared elsewhere (in likelihood.c) */
284 extern CLFlt *preLikeL; /* precalculated cond likes for left descendant */
285 extern CLFlt *preLikeR; /* precalculated cond likes for right descendant*/
286 extern CLFlt *preLikeA; /* precalculated cond likes for ancestor */
287
288 /* local (to this file) variables */
289 int numLocalChains; /* number of Markov chains */
290 int *chainId = NULL; /* information on the id (0 ...) of the chain */
291 MrBFlt *curLnL = NULL; /* stores log likelihood */
292 MrBFlt *curLnPr = NULL; /* stores log prior probability */
293 int stepRelativeBurninSS; /* Should we use relative burn in within each step or not */
294 MrBFlt powerSS; /* power (betta) in power posterior destribution used in SS */
295 MrBFlt *marginalLnLSS = NULL; /* marginal liklihood obtained using stepppingstone sampling */
296 MrBFlt *stepAcumulatorSS = NULL; /* accumulates liklihoods for current step in SS */
297 MrBFlt *stepScalerSS = NULL; /* scaler of stepAcumulatorSS in log scale in SS */
298 MrBFlt *splitfreqSS = NULL; /* array holding split frequencis for each step in SS */
299 int *sympiIndex; /* sympi state freq index for multistate chars */
300 int stdStateFreqsRowSize; /* row size for std state frequencies */
301 int *weight; /* weight of each compressed char */
302 int *chainTempId; /* info ton temp, change to float holding temp? */
303 int state[MAX_CHAINS]; /* state of chain */
304 int augmentData; /* are data being augmented for any division? */
305 int *nAccepted; /* counter of accepted moves */
306 int *termState = NULL; /* stores character states of tips */
307 int *isPartAmbig = NULL; /* records whether tips are partially ambiguous */
308 BitsLong **parsPtrSpace = NULL; /* space holding pointers to parsimony sets */
309 BitsLong ***parsPtr = NULL; /* pointers to pars state sets for chain & node */
310 CLFlt *parsNodeLengthSpace = NULL; /* space for parsimony node lengths */
311 CLFlt **parsNodeLen = NULL; /* pointers to pars node lengths for chains */
312 char *printString; /* string for printing to a file */
313 size_t printStringSize; /* length of printString */
314 MCMCMove **usedMoves; /* vector of pointers to used moves */
315 int numUsedMoves; /* the number of moves used by chain */
316 Param **printParam; /* vector of pointers to normal params to print */
317 int numPrintParams; /* the number of normal params to print */
318 Param **printTreeParam; /* vector of pointers to tree params to print */
319 Param **topologyParam; /* vector of pointers to topology params */
320 int numPrintTreeParams; /* the number of tree params to print */
321 int codon[6][64]; /* holds info on amino acids coded in code */
322 int chainHasAdgamma; /* indicates if chain has adgamma HMMs */
323 int inferPosSel; /* indicates if positive selection is inferred */
324 MrBFlt *posSelProbs; /* probs. for positive selection */
325 int hasMarkovTi[MAX_SMALL_JUMP]; /* vector marking size of observed HMM jumps */
326 int *siteJump; /* vector of sitejumps for adgamma model */
327 MrBFlt **rateProbs; /* pointers to rate probs used by adgamma model */
328 MrBFlt *rateProbSpace; /* space for rate probs used by adgamma model */
329 int rateProbRowSize; /* size of rate probs for one chain one state */
330 MrBFlt **markovTi[MAX_SMALL_JUMP]; /* trans prob matrices used in calc of adgamma */
331 MrBFlt **markovTiN; /* trans prob matrices used in calc of adgamma */
332 int whichReweightNum; /* used for setting reweighting of char pats */
333 int ***swapInfo; /* keeps track of attempts & successes of swaps */
334 int tempIndex; /* keeps track of which user temp is specified */
335 int abortMove; /* flag determining whether to abort move */
336 PFNODE **partFreqTreeRoot; /* root of tree(s) holding partition freqs */
337 int nLongsNeeded; /* number of longs needed for partitions */
338 BitsLong **partition; /* matrix holding partitions */
339 MrBFlt *maxLnL0 = NULL; /* maximum likelihood */
340 FILE *fpMcmc = NULL; /* pointer to .mcmc file */
341 FILE **fpParm = NULL; /* pointer to .p file(s) */
342 FILE ***fpTree = NULL; /* pointer to .t file(s) */
343 FILE *fpSS = NULL; /* pointer to .ss file */
344 static int requestAbortRun; /* flag for aborting mcmc analysis */
345 int *topologyPrintIndex; /* print file index of each topology */
346 int *printTreeTopologyIndex; /* topology index of each tree print file */
347 int numPreviousGen; /* number of generations in run to append to */
348
349 #if defined (MPI_ENABLED)
350 int lowestLocalRunId; /* lowest local run Id */
351 int highestLocalRunId; /* highest local run Id */
352 #endif
353
354 #if defined (PRINT_DUMP)
355 FILE **fpDump = NULL; /* pointer to .dump file(s) */
356 #endif
357
358 /* AddPartition: Add a partition to the tree keeping track of partition frequencies */
AddPartition(PFNODE * r,BitsLong * p,int runId)359 PFNODE *AddPartition (PFNODE *r, BitsLong *p, int runId)
360 {
361 int i, comp;
362
363 if (r == NULL)
364 {
365 /* new partition */
366 r = Talloc (); /* create a new node */
367 if (r == NULL)
368 return NULL;
369 for (i=0; i<nLongsNeeded; i++)
370 r->partition[i] = p[i];
371 for (i=0; i<chainParams.numRuns; i++)
372 r->count[i] = 0;
373 r->count[runId] = 1;
374 r->left = r->right = NULL;
375 }
376 else
377 {
378 for (i=0; i<nLongsNeeded; i++)
379 {
380 if (r->partition[i] != p[i])
381 break;
382 }
383
384 if (i == nLongsNeeded)
385 comp = 0;
386 else if (r->partition[i] < p[i])
387 comp = -1;
388 else
389 comp = 1;
390
391 if (comp == 0) /* repeated partition */
392 r->count[runId]++;
393 else if (comp < 0) /* greater than -> into left subtree */
394 {
395 if ((r->left = AddPartition (r->left, p, runId)) == NULL)
396 {
397 Tfree (r);
398 return NULL;
399 }
400 }
401 else
402 {
403 /* smaller than -> into right subtree */
404 if ((r->right = AddPartition (r->right, p, runId)) == NULL)
405 {
406 Tfree (r);
407 return NULL;
408 }
409 }
410 }
411
412 return r;
413 }
414
415
AddToPrintString(char * tempStr)416 int AddToPrintString (char *tempStr)
417 {
418 size_t len1, len2;
419
420 len1 = strlen(printString);
421 len2 = strlen(tempStr);
422 if (len1 + len2 + 5 > printStringSize)
423 {
424 printStringSize += len1 + len2 - printStringSize + 200;
425 printString = (char*)SafeRealloc((void*)printString, printStringSize * sizeof(char));
426 if (!printString)
427 {
428 MrBayesPrint ("%s Problem reallocating printString (%d)\n", spacer, printStringSize * sizeof(char));
429 goto errorExit;
430 }
431 }
432 strcat(printString, tempStr);
433 // printf ("printString(%d) -> \"%s\"\n", printStringSize, printString);
434 return (NO_ERROR);
435
436 errorExit:
437 return (ERROR);
438 }
439
440
441 /* AddTreeSamples: Add tree samples from .t files to partition counters. if saveToList == YES then also save trees in tree list */
AddTreeSamples(int from,int to,int saveToList)442 int AddTreeSamples (int from, int to, int saveToList)
443 {
444 int i, j, k, longestLine;
445 BitsLong lastBlock;
446 char *word, *s, *lineBuf;
447 FILE *fp;
448 Tree *t;
449 char *tempStr;
450 int tempStrSize = TEMPSTRSIZE;
451
452 if (from > to)
453 return (NO_ERROR);
454
455 # if defined (MPI_ENABLED)
456 if (proc_id != 0)
457 return (NO_ERROR);
458 # endif
459
460 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
461 if (!tempStr)
462 {
463 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
464 return (ERROR);
465 }
466
467 for (i=0; i<numTopologies; i++)
468 {
469 t = chainParams.dtree;
470
471 for (j=0; j<chainParams.numRuns; j++)
472 {
473 if (numPrintTreeParams == 1)
474 {
475 if (chainParams.numRuns == 1)
476 SafeSprintf (&tempStr, &tempStrSize, "%s.t", chainParams.chainFileName);
477 else
478 SafeSprintf (&tempStr, &tempStrSize, "%s.run%d.t", chainParams.chainFileName, j+1);
479 }
480 else
481 {
482 if (chainParams.numRuns == 1)
483 SafeSprintf (&tempStr, &tempStrSize, "%s.tree%d.t", chainParams.chainFileName, i+1);
484 else
485 SafeSprintf (&tempStr, &tempStrSize, "%s.tree%d.run%d.t", chainParams.chainFileName, i+1, j+1);
486 }
487
488 if ((fp = OpenBinaryFileR (tempStr)) == NULL)
489 {
490 MrBayesPrint ("%s Problem opening file %s.\n", spacer, tempStr);
491 free (tempStr);
492 return (ERROR);
493 }
494
495 longestLine = LongestLine (fp);
496 SafeFclose (&fp);
497
498 if ((fp = OpenTextFileR (tempStr)) == NULL)
499 {
500 free (tempStr);
501 return (ERROR);
502 }
503
504 lineBuf = (char *) SafeCalloc (longestLine + 10, sizeof (char));
505 if (!lineBuf)
506 {
507 SafeFclose (&fp);
508 free (tempStr);
509 return (ERROR);
510 }
511
512 lastBlock = LastBlock (fp, lineBuf, longestLine);
513 fseek (fp, lastBlock, SEEK_SET);
514
515 for (k=1; k<=to; k++)
516 {
517 do {
518 if (fgets (lineBuf, longestLine, fp) == NULL)
519 {
520 SafeFclose (&fp);
521 free (lineBuf);
522 free (tempStr);
523 return ERROR;
524 }
525 word = strtok (lineBuf, " ");
526 } while (strcmp (word, "tree") != 0);
527 if (k>=from)
528 {
529 s = strtok (NULL, ";");
530 while (*s != '(')
531 s++;
532 StripComments(s);
533 if (ResetTopology (t, s) == ERROR)
534 {
535 SafeFclose (&fp);
536 free (lineBuf);
537 free (tempStr);
538 return ERROR;
539 }
540 if (AddTreeToPartitionCounters (t, i, j) == ERROR)
541 {
542 SafeFclose (&fp);
543 free (lineBuf);
544 free (tempStr);
545 return ERROR;
546 }
547 if (saveToList == YES)
548 if (AddToTreeList(&chainParams.treeList[numTopologies*j+i],t) == ERROR)
549 return (ERROR);
550 }
551 }
552 SafeFclose (&fp);
553 free (lineBuf);
554 } /* next run */
555 } /* next tree */
556 free (tempStr);
557 return (NO_ERROR);
558 }
559
560
561 /* AddTreeToPartitionCounters: Break a tree into partitions and add those to counters */
AddTreeToPartitionCounters(Tree * tree,int treeId,int runId)562 int AddTreeToPartitionCounters (Tree *tree, int treeId, int runId)
563 {
564 int i, j, nTaxa;
565 TreeNode *p;
566
567 if (tree->isRooted == YES)
568 nTaxa = tree->nNodes - tree->nIntNodes - 1;
569 else
570 nTaxa = tree->nNodes - tree->nIntNodes;
571
572 for (i=0; i<nTaxa; i++)
573 {
574 ClearBits(partition[i], nLongsNeeded);
575 SetBit(i, partition[i]);
576 }
577
578 for (i=0; i<tree->nIntNodes-1; i++)
579 {
580 p = tree->intDownPass[i];
581 assert (p->index >= tree->nNodes - tree->nIntNodes - (tree->isRooted == YES ? 1 : 0));
582 for (j=0; j<nLongsNeeded; j++)
583 {
584 partition[p->index][j] = partition[p->left->index][j] | partition[p->right->index][j];
585 }
586
587 if ((partFreqTreeRoot[treeId] = AddPartition (partFreqTreeRoot[treeId], partition[p->index], runId)) == NULL)
588 {
589 MrBayesPrint ("%s Could not allocate space for new partition in AddTreeToPartitionCounters\n", spacer);
590 return ERROR;
591 }
592 }
593
594 return NO_ERROR;
595 }
596
597
AttemptSwap(int swapA,int swapB,RandLong * seed)598 int AttemptSwap (int swapA, int swapB, RandLong *seed)
599 {
600 int d, tempX, reweightingChars, isSwapSuccessful, chI, chJ, runId;
601 MrBFlt tempA, tempB, lnLikeA, lnLikeB, lnPriorA, lnPriorB, lnR, r,
602 lnLikeStateAonDataB=0.0, lnLikeStateBonDataA=0.0, lnL;
603 ModelInfo *m;
604 Tree *tree;
605 # if defined (MPI_ENABLED)
606 int numChainsForProc, tempIdA=0, tempIdB=0, proc, procIdForA=0, procIdForB=0, tempIdMy=0, procIdPartner=0,
607 whichElementA=0, whichElementB=0, lower, upper, areWeA, doISwap, ierror,
608 myId, partnerId, i, run;
609 MrBFlt swapRan;
610 MPI_Status status[2];
611 MPI_Request request[2];
612 # endif
613
614 # if defined (MPI_ENABLED)
615
616 /* get the number of chains handled by this proc */
617 /* the number will be corrected further down for unbalanced scenarios */
618 numChainsForProc = (int) (chainParams.numChains * chainParams.numRuns / num_procs);
619
620 # endif
621
622 /* are we using character reweighting? */
623 reweightingChars = NO;
624 if ((chainParams.weightScheme[0] + chainParams.weightScheme[1]) > 0.00001)
625 reweightingChars = YES;
626
627 # if defined (MPI_ENABLED)
628
629 /* figure out processors involved in swap */
630 lower = upper = 0;
631 for (proc=0; proc<num_procs; proc++)
632 {
633 /* assign or increment chain id */
634 if (proc < (chainParams.numChains * chainParams.numRuns) % num_procs)
635 upper += numChainsForProc+1;
636 else
637 upper += numChainsForProc;
638
639 /* if swapA lies between lower and upper
640 * chain id's we know that this is the proc
641 * swapA is in */
642 if (swapA >= lower && swapA < upper)
643 {
644 procIdForA = proc;
645 whichElementA = swapA - lower;
646 }
647 if (swapB >= lower && swapB < upper)
648 {
649 procIdForB = proc;
650 whichElementB = swapB - lower;
651 }
652 lower = upper;
653 }
654
655 /* NOTE: at this point, procIdForA and procIdForB *
656 * store the proc id's of swapping procs. Also, *
657 * whichElementA and whichElementB store the *
658 * chainId[] index of swapping procs */
659
660 /* figure out if I am involved in the swap */
661 doISwap = areWeA = NO;
662 if (proc_id == procIdForA)
663 {
664 doISwap = YES;
665 areWeA = YES;
666 }
667 else if (proc_id == procIdForB)
668 {
669 doISwap = YES;
670 }
671
672 /* chain's that do not swap, continue to the next iteration */
673 if (doISwap == YES)
674 {
675
676 /* no need to communicate accross processors if swapping chains are in the same proc */
677 if (procIdForA == procIdForB)
678 {
679 if (reweightingChars == YES)
680 {
681 /* use character reweighting */
682 lnLikeStateAonDataB = 0.0;
683 for (d=0; d<numCurrentDivisions; d++)
684 {
685 m = &modelSettings[d];
686 tree = GetTree(m->brlens, whichElementA, state[whichElementA]);
687 lnL = 0.0;
688 m->Likelihood (tree->root->left, d, whichElementA, &lnL, chainId[whichElementB] % chainParams.numChains);
689 lnLikeStateAonDataB += lnL;
690 }
691 lnLikeStateBonDataA = 0.0;
692 for (d=0; d<numCurrentDivisions; d++)
693 {
694 m = &modelSettings[d];
695 tree = GetTree(m->brlens, whichElementB, state[whichElementB]);
696 lnL = 0.0;
697 m->Likelihood (tree->root->left, d, whichElementB, &lnL, chainId[whichElementA] % chainParams.numChains);
698 lnLikeStateBonDataA += lnL;
699 }
700 }
701
702 /*curLnPr[whichElementA] = LogPrior(whichElementA);
703 curLnPr[whichElementB] = LogPrior(whichElementB);*/
704
705 /* then do the serial thing - simply swap chain id's */
706 tempA = Temperature (chainId[whichElementA]);
707 tempB = Temperature (chainId[whichElementB]);
708 lnLikeA = curLnL[whichElementA];
709 lnLikeB = curLnL[whichElementB];
710 if (chainParams.isSS == YES)
711 {
712 lnLikeA *= powerSS;
713 lnLikeB *= powerSS;
714 }
715 lnPriorA = curLnPr[whichElementA];
716 lnPriorB = curLnPr[whichElementB];
717 if (reweightingChars == YES)
718 {
719 if (chainParams.isSS == YES)
720 lnR = (tempB * (lnLikeStateAonDataB*powerSS + lnPriorA) + tempA * (lnLikeStateBonDataA*powerSS + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
721 else
722 lnR = (tempB * (lnLikeStateAonDataB + lnPriorA) + tempA * (lnLikeStateBonDataA + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
723 }
724 else
725 lnR = (tempB * (lnLikeA + lnPriorA) + tempA * (lnLikeB + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
726 if (lnR < -100.0)
727 r = 0.0;
728 else if (lnR > 0.0)
729 r = 1.0;
730 else
731 r = exp(lnR);
732
733 isSwapSuccessful = NO;
734 if (RandomNumber(seed) < r)
735 {
736 /* swap chain id's (heats) */
737 tempX = chainId[whichElementA];
738 chainId[whichElementA] = chainId[whichElementB];
739 chainId[whichElementB] = tempX;
740 if (reweightingChars == YES)
741 {
742 curLnL[whichElementA] = lnLikeStateAonDataB;
743 curLnL[whichElementB] = lnLikeStateBonDataA;
744 }
745 isSwapSuccessful = YES;
746 }
747
748 chI = chainId[whichElementA];
749 chJ = chainId[whichElementB];
750 if (chainId[whichElementB] < chainId[whichElementA])
751 {
752 chI = chainId[whichElementB];
753 chJ = chainId[whichElementA];
754 }
755 runId = chI / chainParams.numChains;
756 chI = chI % chainParams.numChains;
757 chJ = chJ % chainParams.numChains;
758 swapInfo[runId][chJ][chI]++;
759 if (isSwapSuccessful == YES)
760 swapInfo[runId][chI][chJ]++;
761 }
762 /* we need to communicate across processors */
763 else
764 {
765 if (reweightingChars == YES)
766 {
767 /* If we are reweighting characters, then we need to do an additional communication to
768 figure out the chainId's of the partner. We need to have this information so we can
769 properly calculate likelihoods with switched observations. */
770 if (areWeA == YES)
771 {
772 lnLikeStateAonDataB = 0.0;
773 myId = chainId[whichElementA];
774 ierror = MPI_Isend (&myId, 1, MPI_INT, procIdForB, 0, MPI_COMM_WORLD, &request[0]);
775 if (ierror != MPI_SUCCESS)
776 {
777 return (ERROR);
778 }
779 ierror = MPI_Irecv (&partnerId, 1, MPI_INT, procIdForB, 0, MPI_COMM_WORLD, &request[1]);
780 if (ierror != MPI_SUCCESS)
781 {
782 return (ERROR);
783 }
784 ierror = MPI_Waitall (2, request, status);
785 if (ierror != MPI_SUCCESS)
786 {
787 return (ERROR);
788 }
789 for (d=0; d<numCurrentDivisions; d++)
790 {
791 m = &modelSettings[d];
792 tree = GetTree(m->brlens, whichElementA, state[whichElementA]);
793 lnL = 0.0;
794 m->Likelihood (tree->root->left, d, whichElementA, &lnL, partnerId);
795 lnLikeStateAonDataB = lnL;
796 }
797 }
798 else
799 {
800 lnLikeStateBonDataA = 0.0;
801 myId = chainId[whichElementB];
802 ierror = MPI_Isend (&myId, 1, MPI_INT, procIdForA, 0, MPI_COMM_WORLD, &request[0]);
803 if (ierror != MPI_SUCCESS)
804 {
805 return (ERROR);
806 }
807 ierror = MPI_Irecv (&partnerId, 1, MPI_INT, procIdForA, 0, MPI_COMM_WORLD, &request[1]);
808 if (ierror != MPI_SUCCESS)
809 {
810 return (ERROR);
811 }
812 ierror = MPI_Waitall (2, request, status);
813 if (ierror != MPI_SUCCESS)
814 {
815 return (ERROR);
816 }
817 for (d=0; d<numCurrentDivisions; d++)
818 {
819 m = &modelSettings[d];
820 tree = GetTree(m->brlens, whichElementB, state[whichElementB]);
821 lnL = 0.0;
822 m->Likelihood (tree->root->left, d, whichElementB, &lnL, partnerId);
823 lnLikeStateBonDataA = lnL;
824 }
825 }
826 }
827
828 if (areWeA == YES)
829 {
830 /*curLnPr[whichElementA] = LogPrior(whichElementA);*/
831
832 /* we are processor A */
833 tempIdA = chainId[whichElementA];
834 lnLikeA = curLnL[whichElementA];
835 lnPriorA = curLnPr[whichElementA];
836 swapRan = RandomNumber(seed);
837
838 myStateInfo[0] = lnLikeA;
839 myStateInfo[1] = lnPriorA;
840 myStateInfo[2] = tempIdA;
841 myStateInfo[3] = swapRan;
842 myStateInfo[4] = 0.0;
843 if (reweightingChars == YES)
844 {
845 myStateInfo[2] = lnLikeStateAonDataB;
846 tempIdB = partnerId;
847 }
848
849 ierror = MPI_Isend (&myStateInfo, 5, MPI_DOUBLE, procIdForB, 0, MPI_COMM_WORLD, &request[0]);
850 if (ierror != MPI_SUCCESS)
851 {
852 return (ERROR);
853 }
854 ierror = MPI_Irecv (&partnerStateInfo, 5, MPI_DOUBLE, procIdForB, 0, MPI_COMM_WORLD, &request[1]);
855 if (ierror != MPI_SUCCESS)
856 {
857 return (ERROR);
858 }
859 ierror = MPI_Waitall (2, request, status);
860 if (ierror != MPI_SUCCESS)
861 {
862 return (ERROR);
863 }
864
865 lnLikeA = curLnL[whichElementA];
866 lnLikeB = partnerStateInfo[0];
867 if (chainParams.isSS == YES)
868 {
869 lnLikeA *= powerSS;
870 lnLikeB *= powerSS;
871 }
872 lnPriorA = curLnPr[whichElementA];
873 lnPriorB = partnerStateInfo[1];
874 if (reweightingChars == YES)
875 lnLikeStateBonDataA = partnerStateInfo[2];
876 else
877 tempIdB = partnerStateInfo[2];
878
879 tempA = Temperature (tempIdA);
880 tempB = Temperature (tempIdB);
881
882 if (reweightingChars == YES)
883 {
884 if (chainParams.isSS == YES)
885 lnR = (tempB * (lnLikeStateAonDataB*powerSS + lnPriorA) + tempA * (lnLikeStateBonDataA*powerSS + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
886 else
887 lnR = (tempB * (lnLikeStateAonDataB + lnPriorA) + tempA * (lnLikeStateBonDataA + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
888 }
889 else
890 lnR = (tempB * (lnLikeA + lnPriorA) + tempA * (lnLikeB + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
891 if (lnR < -100.0)
892 r = 0.0;
893 else if (lnR > 0.0)
894 r = 1.0;
895 else
896 r = exp(lnR);
897
898 /* process A's random number is used to make the swap decision */
899 isSwapSuccessful = NO;
900 if (swapRan < r)
901 {
902 /* swap chain id's (heats) */
903 isSwapSuccessful = YES;
904 tempIdMy = chainId[whichElementA];
905 procIdPartner = procIdForB;
906
907 if (reweightingChars == YES)
908 chainId[whichElementA] = tempIdB;
909 else
910 chainId[whichElementA] = (int)(partnerStateInfo[2]);
911 if (reweightingChars == YES)
912 {
913 curLnL[whichElementA] = lnLikeStateAonDataB;
914 }
915 }
916
917 /* only processor A keeps track of the swap success/failure */
918 chI = tempIdA;
919 chJ = tempIdB;
920 if (tempIdB < tempIdA)
921 {
922 chI = tempIdB;
923 chJ = tempIdA;
924 }
925 runId = chI / chainParams.numChains;
926 chI = chI % chainParams.numChains;
927 chJ = chJ % chainParams.numChains;
928 swapInfo[runId][chJ][chI]++;
929 if (isSwapSuccessful == YES)
930 {
931 swapInfo[runId][chI][chJ]++;
932 /* exchange the move info */
933 for (i=0; i<numUsedMoves; i++)
934 {
935 myStateInfo[0] = usedMoves[i]->nAccepted[tempIdA];
936 myStateInfo[1] = usedMoves[i]->nTried[tempIdA];
937 myStateInfo[2] = usedMoves[i]->nBatches[tempIdA];
938 myStateInfo[3] = usedMoves[i]->nTotAccepted[tempIdA];
939 myStateInfo[4] = usedMoves[i]->nTotTried[tempIdA];
940 myStateInfo[5] = usedMoves[i]->lastAcceptanceRate[tempIdA];
941 if (usedMoves[i]->moveType->numTuningParams > 0)
942 myStateInfo[6] = usedMoves[i]->tuningParam[tempIdA][0];
943 else
944 myStateInfo[6] = 0.0;
945
946 ierror = MPI_Isend (&myStateInfo, 7, MPI_DOUBLE, procIdForB, 0, MPI_COMM_WORLD, &request[0]);
947 if (ierror != MPI_SUCCESS)
948 {
949 return (ERROR);
950 }
951 ierror = MPI_Irecv (&partnerStateInfo, 7, MPI_DOUBLE, procIdForB, 0, MPI_COMM_WORLD, &request[1]);
952 if (ierror != MPI_SUCCESS)
953 {
954 return (ERROR);
955 }
956 ierror = MPI_Waitall (2, request, status);
957 if (ierror != MPI_SUCCESS)
958 {
959 return (ERROR);
960 }
961
962 usedMoves[i]->nAccepted[tempIdB] = (int)partnerStateInfo[0];
963 usedMoves[i]->nTried[tempIdB] = (int)partnerStateInfo[1];
964 usedMoves[i]->nBatches[tempIdB] = (int)partnerStateInfo[2];
965 usedMoves[i]->nTotAccepted[tempIdB] = (int)partnerStateInfo[3];
966 usedMoves[i]->nTotTried[tempIdB] = (int)partnerStateInfo[4];
967 usedMoves[i]->lastAcceptanceRate[tempIdB] = partnerStateInfo[5];
968 if (usedMoves[i]->moveType->numTuningParams > 0)
969 usedMoves[i]->tuningParam[tempIdB][0] = partnerStateInfo[6];
970
971 usedMoves[i]->nAccepted[tempIdA] = 0;
972 usedMoves[i]->nTried[tempIdA] = 0;
973 usedMoves[i]->nBatches[tempIdA] = 0;
974 usedMoves[i]->lastAcceptanceRate[tempIdA] = 0.0;
975 usedMoves[i]->nTotAccepted[tempIdA] = 0;
976 usedMoves[i]->nTotTried[tempIdA] = 0;
977 if (usedMoves[i]->moveType->numTuningParams > 0)
978 usedMoves[i]->tuningParam[tempIdA][0] = 0.0;
979
980 }
981 }
982 }
983 else
984 {
985 /*curLnPr[whichElementB] = LogPrior(whichElementB);*/
986
987 /* we are processor B */
988 tempIdB = chainId[whichElementB];
989 lnLikeB = curLnL[whichElementB];
990 lnPriorB = curLnPr[whichElementB];
991 swapRan = -1.0;
992
993 myStateInfo[0] = lnLikeB;
994 myStateInfo[1] = lnPriorB;
995 myStateInfo[2] = tempIdB;
996 myStateInfo[3] = swapRan;
997 myStateInfo[4] = 0.0;
998 if (reweightingChars == YES)
999 {
1000 myStateInfo[2] = lnLikeStateBonDataA;
1001 tempIdA = partnerId;
1002 }
1003
1004 ierror = MPI_Isend (&myStateInfo, 5, MPI_DOUBLE, procIdForA, 0, MPI_COMM_WORLD, &request[0]);
1005 if (ierror != MPI_SUCCESS)
1006 {
1007 return (ERROR);
1008 }
1009 ierror = MPI_Irecv (&partnerStateInfo, 5, MPI_DOUBLE, procIdForA, 0, MPI_COMM_WORLD, &request[1]);
1010 if (ierror != MPI_SUCCESS)
1011 {
1012 return (ERROR);
1013 }
1014 ierror = MPI_Waitall (2, request, status);
1015 if (ierror != MPI_SUCCESS)
1016 {
1017 return (ERROR);
1018 }
1019
1020 lnLikeB = curLnL[whichElementB];
1021 lnLikeA = partnerStateInfo[0];
1022 lnPriorB = curLnPr[whichElementB];
1023 lnPriorA = partnerStateInfo[1];
1024 if (reweightingChars == YES)
1025 lnLikeStateAonDataB = partnerStateInfo[2];
1026 else
1027 tempIdA = partnerStateInfo[2];
1028
1029 tempB = Temperature (tempIdB);
1030 tempA = Temperature (tempIdA);
1031
1032 if (chainParams.isSS == YES)
1033 {
1034 lnLikeA *= powerSS;
1035 lnLikeB *= powerSS;
1036 }
1037
1038 if (reweightingChars == YES)
1039 {
1040 if (chainParams.isSS == YES)
1041 lnR = (tempB * (lnLikeStateAonDataB*powerSS + lnPriorA) + tempA * (lnLikeStateBonDataA*powerSS + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
1042 else
1043 lnR = (tempB * (lnLikeStateAonDataB + lnPriorA) + tempA * (lnLikeStateBonDataA + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
1044 }
1045 else
1046 lnR = (tempB * (lnLikeA + lnPriorA) + tempA * (lnLikeB + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
1047 if (lnR < -100.0)
1048 r = 0.0;
1049 else if (lnR > 0.0)
1050 r = 1.0;
1051 else
1052 r = exp(lnR);
1053
1054 /* we use process A's random number to make the swap decision */
1055 isSwapSuccessful = NO;
1056 if (partnerStateInfo[3] < r)
1057 {
1058 isSwapSuccessful = YES;
1059 tempIdMy = chainId[whichElementB];
1060 procIdPartner = procIdForA;
1061
1062 if (reweightingChars == YES)
1063 chainId[whichElementB] = tempIdA;
1064 else
1065 chainId[whichElementB] = (int)(partnerStateInfo[2]);
1066 if (reweightingChars == YES)
1067 {
1068 curLnL[whichElementB] = lnLikeStateBonDataA;
1069 }
1070 /* swap the move info */
1071 for (i=0; i<numUsedMoves; i++)
1072 {
1073 myStateInfo[0] = usedMoves[i]->nAccepted[tempIdB];
1074 myStateInfo[1] = usedMoves[i]->nTried[tempIdB];
1075 myStateInfo[2] = usedMoves[i]->nBatches[tempIdB];
1076 myStateInfo[3] = usedMoves[i]->nTotAccepted[tempIdB];
1077 myStateInfo[4] = usedMoves[i]->nTotTried[tempIdB];
1078 myStateInfo[5] = usedMoves[i]->lastAcceptanceRate[tempIdB];
1079 if (usedMoves[i]->moveType->numTuningParams > 0)
1080 myStateInfo[6] = usedMoves[i]->tuningParam[tempIdB][0];
1081 else
1082 myStateInfo[6] = 0.0;
1083
1084 ierror = MPI_Isend (&myStateInfo, 7, MPI_DOUBLE, procIdForA, 0, MPI_COMM_WORLD, &request[0]);
1085 if (ierror != MPI_SUCCESS)
1086 {
1087 return (ERROR);
1088 }
1089 ierror = MPI_Irecv (&partnerStateInfo, 7, MPI_DOUBLE, procIdForA, 0, MPI_COMM_WORLD, &request[1]);
1090 if (ierror != MPI_SUCCESS)
1091 {
1092 return (ERROR);
1093 }
1094 ierror = MPI_Waitall (2, request, status);
1095 if (ierror != MPI_SUCCESS)
1096 {
1097 return (ERROR);
1098 }
1099
1100 usedMoves[i]->nAccepted[tempIdA] = (int)partnerStateInfo[0];
1101 usedMoves[i]->nTried[tempIdA] = (int)partnerStateInfo[1];
1102 usedMoves[i]->nBatches[tempIdA] = (int)partnerStateInfo[2];
1103 usedMoves[i]->nTotAccepted[tempIdA] = (int)partnerStateInfo[3];
1104 usedMoves[i]->nTotTried[tempIdA] = (int)partnerStateInfo[4];
1105 usedMoves[i]->lastAcceptanceRate[tempIdA] = partnerStateInfo[5];
1106 if (usedMoves[i]->moveType->numTuningParams > 0)
1107 usedMoves[i]->tuningParam[tempIdA][0] = partnerStateInfo[6];
1108
1109 usedMoves[i]->nAccepted[tempIdB] = 0;
1110 usedMoves[i]->nTried[tempIdB] = 0;
1111 usedMoves[i]->nBatches[tempIdB] = 0;
1112 usedMoves[i]->nTotAccepted[tempIdB] = 0;
1113 usedMoves[i]->nTotTried[tempIdB] = 0;
1114 usedMoves[i]->lastAcceptanceRate[tempIdB] = 0.0;
1115 if (usedMoves[i]->moveType->numTuningParams > 0)
1116 usedMoves[i]->tuningParam[tempIdB][0] = 0.0;
1117 }
1118 }
1119 }
1120
1121 /*We exchange only if swap successful and (my id is cold or patner id is cold)*/
1122 if (chainParams.isSS == YES && isSwapSuccessful == YES && (tempIdMy % chainParams.numChains == 0 || (areWeA == YES && chainId[whichElementA] % chainParams.numChains == 0) || (areWeA == NO && chainId[whichElementB] % chainParams.numChains == 0)))
1123 {
1124 run = tempIdMy/chainParams.numChains;
1125
1126 myStateInfo[0] = tempIdMy;
1127 myStateInfo[1] = marginalLnLSS [ run ];
1128 myStateInfo[2] = stepAcumulatorSS[ run ];
1129 myStateInfo[3] = stepScalerSS [ run ];
1130
1131 ierror = MPI_Isend (&myStateInfo, 4, MPI_DOUBLE, procIdPartner, 0, MPI_COMM_WORLD, &request[0]);
1132 if (ierror != MPI_SUCCESS)
1133 {
1134 return (ERROR);
1135 }
1136
1137 ierror = MPI_Irecv (&partnerStateInfo, 4, MPI_DOUBLE, procIdPartner, 0, MPI_COMM_WORLD, &request[1]);
1138 if (ierror != MPI_SUCCESS)
1139 {
1140 return (ERROR);
1141 }
1142 ierror = MPI_Waitall (2, request, status);
1143 if (ierror != MPI_SUCCESS)
1144 {
1145 return (ERROR);
1146 }
1147
1148 /*we swap chains from the same run*/
1149 assert (run == (int)partnerStateInfo[0]/chainParams.numChains);
1150
1151 /*If my chain is the cold chain then send current SS values of corresponding run*/
1152 if (tempIdMy % chainParams.numChains == 0)
1153 {
1154 assert ((int)partnerStateInfo[0] % chainParams.numChains != 0);
1155 assert (partnerStateInfo[1] == 0.0);
1156 marginalLnLSS [ run ] = (MrBFlt) 0.0;
1157 stepAcumulatorSS[ run ] = (MrBFlt) 0.0;
1158 stepScalerSS [ run ] = (MrBFlt) 0.0;
1159 }
1160 else if ((int)partnerStateInfo[0] % chainParams.numChains == 0)
1161 {
1162 marginalLnLSS [ run ] = (MrBFlt) partnerStateInfo[1];
1163 stepAcumulatorSS[ run ] = (MrBFlt) partnerStateInfo[2];
1164 stepScalerSS [ run ] = (MrBFlt) partnerStateInfo[3];
1165 }
1166 }
1167
1168 }
1169 }
1170 # else
1171 if (reweightingChars == YES)
1172 {
1173 /* use character reweighting */
1174 lnLikeStateAonDataB = 0.0;
1175 for (d=0; d<numCurrentDivisions; d++)
1176 {
1177 m = &modelSettings[d];
1178 tree = GetTree(m->brlens, swapA, state[swapA]);
1179 lnL = 0.0;
1180 m->Likelihood (tree->root->left, d, swapA, &lnL, chainId[swapB] % chainParams.numChains);
1181 lnLikeStateAonDataB += lnL;
1182 }
1183 lnLikeStateBonDataA = 0.0;
1184 for (d=0; d<numCurrentDivisions; d++)
1185 {
1186 m = &modelSettings[d];
1187 tree = GetTree(m->brlens, swapB, state[swapB]);
1188 lnL = 0.0;
1189 m->Likelihood (tree->root->left, d, swapB, &lnL, chainId[swapA] % chainParams.numChains);
1190 lnLikeStateBonDataA += lnL;
1191 }
1192 }
1193
1194 assert (fabs((curLnPr[swapA]-LogPrior(swapA))/curLnPr[swapA]) < 0.0001);
1195 assert (fabs((curLnPr[swapB]-LogPrior(swapB))/curLnPr[swapB]) < 0.0001);
1196
1197 tempA = Temperature (chainId[swapA]);
1198 tempB = Temperature (chainId[swapB]);
1199 lnLikeA = curLnL[swapA];
1200 lnLikeB = curLnL[swapB];
1201 lnPriorA = curLnPr[swapA];
1202 lnPriorB = curLnPr[swapB];
1203
1204 if (chainParams.isSS == YES)
1205 {
1206 lnLikeA *= powerSS;
1207 lnLikeB *= powerSS;
1208 }
1209
1210 if (reweightingChars == YES)
1211 {
1212 if (chainParams.isSS == YES)
1213 lnR = (tempB * (lnLikeStateAonDataB*powerSS + lnPriorA) + tempA * (lnLikeStateBonDataA*powerSS + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
1214 else
1215 lnR = (tempB * (lnLikeStateAonDataB + lnPriorA) + tempA * (lnLikeStateBonDataA + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
1216 }
1217 else
1218 lnR = (tempB * (lnLikeA + lnPriorA) + tempA * (lnLikeB + lnPriorB)) - (tempA * (lnLikeA + lnPriorA) + tempB * (lnLikeB + lnPriorB));
1219 if (lnR < -100.0)
1220 r = 0.0;
1221 else if (lnR > 0.0)
1222 r = 1.0;
1223 else
1224 r = exp (lnR);
1225
1226 isSwapSuccessful = NO;
1227 if (RandomNumber(seed) < r)
1228 {
1229 tempX = chainId[swapA];
1230 chainId[swapA] = chainId[swapB];
1231 chainId[swapB] = tempX;
1232
1233 if (reweightingChars == YES)
1234 {
1235 curLnL[swapA] = lnLikeStateAonDataB;
1236 curLnL[swapB] = lnLikeStateBonDataA;
1237 }
1238 isSwapSuccessful = YES;
1239 }
1240
1241 chI = chainId[swapA];
1242 chJ = chainId[swapB];
1243 if (chainId[swapB] < chainId[swapA])
1244 {
1245 chI = chainId[swapB];
1246 chJ = chainId[swapA];
1247 }
1248 runId = chI / chainParams.numChains;
1249 chI = chI % chainParams.numChains;
1250 chJ = chJ % chainParams.numChains;
1251 swapInfo[runId][chJ][chI]++;
1252 if (isSwapSuccessful == YES)
1253 swapInfo[runId][chI][chJ]++;
1254 # endif
1255
1256 return (NO_ERROR);
1257 }
1258
1259
1260 /* Autotune Dirichlet move */
AutotuneDirichlet(MrBFlt acceptanceRate,MrBFlt targetRate,int batch,MrBFlt * alphaPi,MrBFlt minTuning,MrBFlt maxTuning)1261 void AutotuneDirichlet (MrBFlt acceptanceRate, MrBFlt targetRate, int batch, MrBFlt *alphaPi, MrBFlt minTuning, MrBFlt maxTuning)
1262 {
1263 MrBFlt delta, logTuning, newTuning;
1264
1265 delta = 1.0 / sqrt(batch);
1266 delta = 0.01 < delta ? 0.01 : delta;
1267
1268 logTuning = log(*alphaPi);
1269
1270 if (acceptanceRate > targetRate)
1271 logTuning -= delta;
1272 else
1273 logTuning += delta;
1274
1275 newTuning = exp(logTuning);
1276 if (newTuning > minTuning && newTuning < maxTuning)
1277 *alphaPi = newTuning;
1278 }
1279
1280
1281 /* Autotune multiplier move */
AutotuneMultiplier(MrBFlt acceptanceRate,MrBFlt targetRate,int batch,MrBFlt * lambda,MrBFlt minTuning,MrBFlt maxTuning)1282 void AutotuneMultiplier (MrBFlt acceptanceRate, MrBFlt targetRate, int batch, MrBFlt *lambda, MrBFlt minTuning, MrBFlt maxTuning)
1283 {
1284 MrBFlt delta, logTuning, newTuning;
1285
1286 delta = 1.0 / sqrt(batch);
1287 delta = 0.01 < delta ? 0.01 : delta;
1288
1289 logTuning = log(*lambda);
1290
1291 if (acceptanceRate > targetRate)
1292 logTuning += delta;
1293 else
1294 logTuning -= delta;
1295
1296 newTuning = exp(logTuning);
1297 if (newTuning > minTuning && newTuning < maxTuning)
1298 *lambda = newTuning;
1299 }
1300
1301
1302 /* Autotune sliding window move */
AutotuneSlider(MrBFlt acceptanceRate,MrBFlt targetRate,int batch,MrBFlt * width,MrBFlt minTuning,MrBFlt maxTuning)1303 void AutotuneSlider (MrBFlt acceptanceRate, MrBFlt targetRate, int batch, MrBFlt *width, MrBFlt minTuning, MrBFlt maxTuning)
1304 {
1305 MrBFlt delta, logTuning, newTuning;
1306
1307 delta = 1.0 / sqrt(batch);
1308 delta = 0.01 < delta ? 0.01 : delta;
1309
1310 logTuning = log(*width);
1311
1312 if (acceptanceRate > targetRate)
1313 logTuning += delta;
1314 else
1315 logTuning -= delta;
1316
1317 newTuning = exp(logTuning);
1318 if (newTuning > minTuning && newTuning < maxTuning)
1319 *width = newTuning;
1320 }
1321
1322
BuildExhaustiveSearchTree(Tree * t,int chain,int nTaxInTree,TreeInfo * tInfo)1323 void BuildExhaustiveSearchTree (Tree *t, int chain, int nTaxInTree, TreeInfo *tInfo)
1324 {
1325 int i;
1326 TreeNode *p, *q, *r;
1327
1328 if (nTaxInTree == t->nIntNodes + 1) {
1329
1330 /* Get downpass */
1331 GetDownPass (t);
1332
1333 /* Calculate cost of this tree and add to counter */
1334 tInfo->curScore = GetParsimonyLength (t, chain);
1335 if (tInfo->curScore < tInfo->minScore)
1336 {
1337 tInfo->totalScore *= pow ((tInfo->warp/3.0) / (1.0 - tInfo->warp), tInfo->minScore - tInfo->curScore);
1338 tInfo->totalScore += 1.0;
1339 tInfo->minScore = tInfo->curScore;
1340 }
1341 else
1342 tInfo->totalScore += pow (tInfo->warp/3.0, tInfo->curScore - tInfo->minScore) * pow (1.0-tInfo->warp, tInfo->minScore - tInfo->curScore);
1343 }
1344
1345 else {
1346
1347 /* find node to connect */
1348 q=tInfo->leaf[nTaxInTree];
1349
1350 /* add using this ancestral node */
1351 p=tInfo->vertex[nTaxInTree-1];
1352 q->anc=p;
1353 p->right=q;
1354
1355 for (i=0;i<2*nTaxInTree-1;i++) {
1356 /* find node to connect to */
1357 if (i>=nTaxInTree)
1358 r=tInfo->vertex[i-nTaxInTree];
1359 else
1360 r=tInfo->leaf[i];
1361
1362 /* add to this node */
1363 p->left=r;
1364 if (r->anc==NULL)
1365 p->anc=NULL;
1366 else {
1367 p->anc=r->anc;
1368 if (r->anc->left==r)
1369 r->anc->left=p;
1370 else
1371 r->anc->right=p;
1372 }
1373 r->anc=p;
1374
1375 /* next level */
1376 BuildExhaustiveSearchTree (t, chain, nTaxInTree+1, tInfo);
1377
1378 if (tInfo->stopScore > 0.0 && tInfo->totalScore >= tInfo->stopScore)
1379 return;
1380
1381 /* restore tree before trying next possibility */
1382 r->anc=p->anc;
1383 if (r->anc!=NULL) {
1384 if (r->anc->left==p)
1385 r->anc->left=r;
1386 else
1387 r->anc->right=r;
1388 }
1389 }
1390 }
1391 }
1392
1393
1394 /*------------------------------------------------------------------
1395 |
1396 | BuildParsTrees: Fill in trees using random add seq with parsimony
1397 |
1398 ------------------------------------------------------------------*/
BuildParsTrees(RandLong * seed,int fromChain,int toChain)1399 int BuildParsTrees (RandLong *seed, int fromChain, int toChain)
1400 {
1401 int k, chn;
1402 Param *p, *q;
1403 Tree *tree;
1404
1405 /* Build starting trees for state 0 */
1406 for (chn=fromChain; chn<toChain; chn++)
1407 {
1408 for (k=0; k<numParams; k++)
1409 {
1410 p = ¶ms[k];
1411 if (p->paramType == P_TOPOLOGY)
1412 {
1413 assert (p->nSubParams == 1);
1414 q = p->subParams[0];
1415 tree = GetTree (q, chn, 0);
1416 /* fixed topology */
1417 if (p->paramId == TOPOLOGY_RCL_FIXED ||
1418 p->paramId == TOPOLOGY_RCCL_FIXED ||
1419 p->paramId == TOPOLOGY_CL_FIXED ||
1420 p->paramId == TOPOLOGY_CCL_FIXED ||
1421 p->paramId == TOPOLOGY_NCL_FIXED ||
1422 p->paramId == TOPOLOGY_PARSIMONY_FIXED)
1423 {
1424 MrBayesPrint ("%s Tree %s is fixed so a parsimony-based starting tree is not built\n", spacer, p->name);
1425 return (NO_ERROR);
1426 }
1427 /* constrained topology */
1428 else if (tree->nConstraints > 0)
1429 {
1430 MrBayesPrint ("%s Tree %s is constrained and parsimony-based starting trees are not implemented for constrained trees (yet)\n", spacer, p->name);
1431 return (NO_ERROR);
1432 }
1433 /* random topology */
1434 else
1435 {
1436 if (BuildStepwiseTree (tree, chn, &globalSeed) == ERROR)
1437 return (ERROR);
1438 }
1439 if (InitializeTreeCalibrations (tree) == ERROR)
1440 return (ERROR);
1441 FillTopologySubParams(p, chn, 0, seed);
1442 }
1443 }
1444 }
1445
1446 return (NO_ERROR);
1447 }
1448
1449
1450 /* build (starting) topology stepwise */
BuildStepwiseTree(Tree * t,int chain,RandLong * seed)1451 int BuildStepwiseTree (Tree *t, int chain, RandLong *seed) {
1452
1453 int i, j, nTips;
1454 TreeNode *p, *q, *r;
1455 CLFlt length;
1456
1457 // Allocate parsimony matrix if not done already
1458
1459 /* get the tips */
1460 for (i=j=0; i<t->nNodes; i++) {
1461 p = t->allDownPass[i];
1462 if (p->left == NULL && p->right == NULL)
1463 t->allDownPass[j++] = p;
1464 else if (p->right == NULL && p->anc == NULL && t->isRooted == NO)
1465 t->allDownPass[j++] = p;
1466 }
1467 nTips = j;
1468
1469 /* order the tips randomly, use last as root */
1470 for (i=0; i<nTips-1; i++) {
1471 j = (int) (RandomNumber(seed)*(nTips-1-i));
1472 j += i;
1473 p = t->allDownPass[i];
1474 t->allDownPass[i] = t->allDownPass[j];
1475 t->allDownPass[j] = p;
1476 }
1477
1478 /* build first tree */
1479 j = 0;
1480 q = t->allDownPass[0];
1481 r = t->allDownPass[1];
1482 p = t->intDownPass[j++];
1483 q->anc = p;
1484 r->anc = p;
1485 p->left = q;
1486 p->right = r;
1487 q = t->allDownPass[nTips-1];
1488 q->anc = NULL;
1489 q->right = NULL;
1490 q->left = p;
1491 p->anc = q;
1492 t->root = q;
1493
1494 /* add nodes one at a time */
1495 for (i=2; i<nTips-1; i++) {
1496 r = t->allDownPass[i];
1497 p = t->intDownPass[j++];
1498 GetParsDP(t, t->root->left, chain);
1499 GetParsFP(t, t->root->left, chain);
1500 q = FindBestNode(t, t->root->left, r, &length, chain);
1501 p->left = q;
1502 p->right = r;
1503 p->anc = q->anc;
1504 if (q->anc->left == q)
1505 q->anc->left = p;
1506 else
1507 q->anc->right = p;
1508 q->anc = p;
1509 r->anc = p;
1510 }
1511
1512 /* take care of the root */
1513 if (t->isRooted == YES) {
1514 r = t->root;
1515 q = t->allDownPass[t->nNodes-1];
1516 p = t->intDownPass[j];
1517 q->anc = q->right = NULL;
1518 q->left = p;
1519 p->anc = q;
1520 p->left = r->left;
1521 p->right = r;
1522 p->left->anc = p;
1523 r->left = r->right = NULL;
1524 r->anc = p;
1525 t->root = q;
1526 }
1527 GetDownPass (t);
1528
1529 return (NO_ERROR);
1530 }
1531
1532
1533 /*------------------------------------------------------------------
1534 |
1535 | CalcLikeAdgamma: calc likelihood for one adgamma correlation HMM
1536 |
1537 -------------------------------------------------------------------*/
CalcLikeAdgamma(int d,Param * param,int chain,MrBFlt * lnL)1538 int CalcLikeAdgamma (int d, Param *param, int chain, MrBFlt *lnL)
1539 {
1540 int c, i, j, nRates, posit, lastCharId;
1541 MrBFlt logScaler, max, prob, *F,
1542 *oldF, *tempF, fSpace[2][MAX_RATE_CATS];
1543 MrBFlt *rP;
1544 CLFlt freq, *lnScaler;
1545 ModelInfo *m;
1546 ModelParams *mp;
1547 BitsLong *inHMM;
1548
1549 /* find nRates for first division in HMM */
1550 m = &modelSettings[d];
1551 mp = &modelParams[d];
1552 nRates = m->numRateCats;
1553
1554 /* calculate rate category frequencies */
1555 freq = (CLFlt) ((CLFlt) 1.0 / nRates);
1556
1557 /* find Markov trans probs */
1558 F = GetParamSubVals (param,chain, state[chain]);
1559 for (i=posit=0; i<nRates; i++)
1560 for (j=0; j<nRates; j++)
1561 markovTi[0][i][j] = F[posit++];
1562
1563 /* precalculate Markov trans probs up to largest small jump */
1564 /* but only if needed */
1565 for (i=1; i<MAX_SMALL_JUMP; i++)
1566 {
1567 if (hasMarkovTi[i] == YES)
1568 {
1569 if (hasMarkovTi[i-1] == YES || i == 1)
1570 MultiplyMatrices(nRates, markovTi[i-1], markovTi[0], markovTi[i]);
1571 else
1572 MultiplyMatrixNTimes(nRates, markovTi[0], i+1, markovTi[i]);
1573 }
1574 }
1575
1576 /* find site scaler for this chain and state */
1577 lnScaler = m->scalers[m->siteScalerIndex[chain]];
1578
1579 /* find rate probs for this chain and state */
1580 rP = rateProbs[chain] + state[chain] * rateProbRowSize;
1581
1582 /* set bit vector indicating divisions in this HMM */
1583 inHMM = (BitsLong *) SafeCalloc (((param->nRelParts/nBitsInALong) + 1), sizeof(BitsLong));
1584 for (i=0; i<param->nRelParts; i++)
1585 {
1586 if (modelSettings[param->relParts[i]].shape ==
1587 modelSettings[d].shape)
1588 {
1589 SetBit(param->relParts[i], inHMM);
1590 }
1591 }
1592
1593 /* Perform the so-called forward algorithm of HMMs */
1594 /* set up the space for f(c,i) */
1595 F = fSpace[0];
1596 oldF = fSpace[1];
1597
1598 for (c=0; c<numChar; c++)
1599 {
1600 if (IsBitSet(partitionId[c][partitionNum] - 1, inHMM) == YES)
1601 break;
1602 }
1603
1604 /* fill in fi(0) */
1605 max = 0.0;
1606 m = &modelSettings[partitionId[c][partitionNum] - 1];
1607 posit = m->rateProbStart + (compCharPos[c] - m->compCharStart) * m->numRateCats;
1608
1609 for (i=0; i<nRates; i++)
1610 {
1611 F[i] = rP[posit++];
1612 if (F[i] > max)
1613 max = F[i];
1614 }
1615
1616 for (i=0; i<nRates; i++)
1617 F[i] /= max;
1618
1619 /* set logscaler to the value from the first character */
1620 logScaler = lnScaler[compCharPos[c]] + log(max);
1621
1622 /* now step along the sequence to the end */
1623 lastCharId = charInfo[c].charId;
1624 for (c++; c<numChar; c++)
1625 {
1626 /* skip if excluded */
1627 if (charInfo[c].isExcluded == YES)
1628 continue;
1629
1630 /* skip if part of same codon in translated protein model */
1631 if ((mp->dataType == DNA || mp->dataType == RNA) && m->dataType == PROTEIN && charInfo[c].charId == lastCharId)
1632 continue;
1633 else
1634 lastCharId = charInfo[c].charId;
1635
1636 /* skip if not in HMM */
1637 if (IsBitSet(partitionId[c][partitionNum] - 1, inHMM) == NO)
1638 continue;
1639
1640 /* switch F and oldF, since the previous F is now old */
1641 tempF = F;
1642 F = oldF;
1643 oldF = tempF;
1644
1645 /* find the position of the rate probs */
1646 m = &modelSettings[partitionId[c][partitionNum] - 1];
1647 posit = m->rateProbStart + (compCharPos[c] - m->compCharStart) * m->numRateCats;
1648
1649 /* calculate the HMM forward probs fi(x) at site x in HMM */
1650 if (siteJump[c] <= MAX_SMALL_JUMP)
1651 {
1652 max = 0.0;
1653 for (i=0; i<nRates; i++)
1654 {
1655 prob = 0.0;
1656 for (j=0; j<nRates; j++)
1657 prob += markovTi[siteJump[c]-1][i][j] * oldF[j];
1658 F[i] = rP[posit++] * prob;
1659 if (F[i] > max)
1660 max = F[i];
1661 }
1662 }
1663 else if (siteJump[c] < BIG_JUMP) /* intermediate jump, calculate trans probs */
1664 {
1665 MultiplyMatrixNTimes(nRates, markovTi[0], siteJump[c], markovTiN);
1666 max = 0.0;
1667 for (i=0; i<nRates; i++)
1668 {
1669 prob = 0.0;
1670 for (j=0; j<nRates; j++)
1671 prob += markovTiN[i][j] * oldF[j];
1672 F[i] = rP[posit++] * prob;
1673 if (F[i] > max)
1674 max = F[i];
1675 }
1676 }
1677 else /* big jump, use stationary freqs */
1678 {
1679 max = 0.0;
1680 for (i=0; i<nRates; i++)
1681 {
1682 prob = 0.0;
1683 for (j=0; j<nRates; j++)
1684 prob += (oldF[j] / freq);
1685 F[i] = rP[posit++] * prob;
1686 if (F[i] > max)
1687 max = F[i];
1688 }
1689 }
1690
1691 /* rescale and adjust total scaler with HMM scaler and site scaler */
1692 for (i=0; i<nRates; i++)
1693 F[i] /= max;
1694
1695 logScaler += lnScaler[compCharPos[c]] + log(max);
1696
1697 }
1698
1699 /* now pull the rate probs together at the end, F contains the vals needed */
1700 prob = 0.0;
1701 for (i=0; i<nRates; i++)
1702 prob += (freq * F[i]);
1703
1704 (*lnL) = logScaler + log(prob);
1705
1706 free (inHMM);
1707
1708 return (NO_ERROR);
1709 }
1710
1711
1712 /* CalcPartFreqStats: Calculate standard deviation of partition frequencies */
CalcPartFreqStats(PFNODE * p,STATS * stat)1713 void CalcPartFreqStats (PFNODE *p, STATS *stat)
1714 {
1715 int i, j, n, min;
1716 MrBFlt f, sum, sumsq, stdev;
1717
1718 n = chainParams.numRuns;
1719 min = (int)(chainParams.minPartFreq * stat->numSamples);
1720 if ((MrBFlt)min != chainParams.minPartFreq * stat->numSamples)
1721 min++;
1722
1723 /* recursively compute partition frequencies for all subpartitions */
1724 if (p->left != NULL)
1725 CalcPartFreqStats (p->left, stat);
1726 if (p->right != NULL)
1727 CalcPartFreqStats (p->right, stat);
1728
1729 for (i=0; i<n; i++)
1730 {
1731 if (p->count[i] >= min)
1732 break;
1733 }
1734
1735 if (i == n)
1736 return;
1737
1738 sum = 0.0;
1739 sumsq = 0.0;
1740 for (i=0; i<n; i++)
1741 {
1742 f = (MrBFlt) (p->count[i]) / (MrBFlt) (stat->numSamples);
1743 sum += f;
1744 sumsq += f * f;
1745 }
1746
1747 f = (sumsq - sum * sum / n) / (n - 1);
1748 if (f < 0.0)
1749 stdev = 0.0;
1750 else
1751 stdev = sqrt (f);
1752
1753 stat->sum += stdev;
1754 if (stdev > stat->max)
1755 stat->max = stdev;
1756
1757 stat->numPartitions++;
1758
1759 if (chainParams.allComps == YES)
1760 {
1761 for (i=0; i<n; i++)
1762 {
1763 for (j=i+1; j<n; j++)
1764 {
1765 if (p->count[i] < min && p->count[j] < min)
1766 continue;
1767
1768 sum = 0.0;
1769 sumsq = 0.0;
1770
1771 f = (MrBFlt) (p->count[i]) / (MrBFlt) (stat->numSamples);
1772 sum += f;
1773 sumsq += f * f;
1774
1775 f = (MrBFlt) (p->count[j]) / (MrBFlt) (stat->numSamples);
1776 sum += f;
1777 sumsq += f * f;
1778
1779 f = (sumsq - sum * sum / 2.0);
1780 if (f < 0.0)
1781 stdev = 0.0;
1782 else
1783 stdev = sqrt (f);
1784
1785 if (chainParams.diagnStat == AVGSTDDEV)
1786 stat->pair[i][j] += stdev;
1787 else if (stdev > stat->pair[i][j])
1788 stat->pair[i][j] = stdev;
1789 stat->pair[j][i]++;
1790 }
1791 }
1792 }
1793 }
1794
1795
1796 /*----------------------------------------------------------------
1797 |
1798 | CalcTopoConvDiagn: Calculate average and max standard deviation
1799 | in clade credibility (partition frequency) values
1800 |
1801 ----------------------------------------------------------------*/
CalcTopoConvDiagn(int numSamples)1802 void CalcTopoConvDiagn (int numSamples)
1803 {
1804 int i, j, n;
1805 STATS *stat;
1806
1807 for (n=0; n<numTopologies; n++)
1808 {
1809 stat = &chainParams.stat[n];
1810 stat->numSamples = numSamples;
1811 stat->numPartitions = 0.0;
1812 stat->sum = 0.0;
1813 stat->max = 0.0;
1814
1815 if (chainParams.allComps == YES)
1816 {
1817 for (i=0; i<chainParams.numRuns; i++)
1818 for (j=0; j<chainParams.numRuns; j++)
1819 stat->pair[i][j] = 0.0;
1820 }
1821
1822 CalcPartFreqStats (partFreqTreeRoot[n], stat);
1823
1824 stat->avgStdDev = stat->sum / stat->numPartitions;
1825 }
1826 }
1827
1828
1829 /* used in DoCompRefTree */
PartFreq(PFNODE * p,STATS * stat,int * ntrees)1830 void PartFreq (PFNODE *p, STATS *stat, int *ntrees)
1831 {
1832 int i, n = chainParams.numRuns;
1833 MrBFlt f, sum, sumsq, stdev;
1834
1835 /* recursively compute partition frequencies for all subpartitions */
1836 if (p->left != NULL)
1837 PartFreq (p->left, stat, ntrees);
1838 if (p->right != NULL)
1839 PartFreq (p->right, stat, ntrees);
1840
1841 sum = sumsq = 0.0;
1842 for (i=0; i<chainParams.numRuns; i++)
1843 {
1844 f = (MrBFlt)(p->count[i]) / (MrBFlt)ntrees[i];
1845 sum += f;
1846 sumsq += f * f;
1847 }
1848
1849 f = (sumsq - sum * sum / n) / (n - 1);
1850 if (f < 0.0)
1851 stdev = 0.0;
1852 else
1853 stdev = sqrt (f);
1854
1855 stat->sum += stdev;
1856 if (stat->max < stdev)
1857 stat->max = stdev;
1858
1859 stat->numPartitions++;
1860 }
CalcTopoConvDiagn2(int * nTrees)1861 void CalcTopoConvDiagn2 (int *nTrees)
1862 {
1863 int n;
1864 STATS *stat;
1865
1866 for (n=0; n<numTopologies; n++)
1867 {
1868 stat = &chainParams.stat[n];
1869 stat->numPartitions = 0.0;
1870 stat->sum = stat->max = 0.0;
1871
1872 PartFreq (partFreqTreeRoot[n], stat, nTrees);
1873
1874 stat->avgStdDev = stat->sum / stat->numPartitions;
1875 }
1876 }
1877
1878
CheckTemperature(void)1879 int CheckTemperature (void)
1880 {
1881 if (chainParams.userDefinedTemps == YES)
1882 {
1883 if (AreDoublesEqual(chainParams.userTemps[0], 1.0, ETA)==NO)
1884 {
1885 MrBayesPrint ("%s The first user-defined temperature must be 1.0.\n", spacer);
1886 return (ERROR);
1887 }
1888 }
1889
1890 return (NO_ERROR);
1891 }
1892
1893
CloseMBPrintFiles(void)1894 void CloseMBPrintFiles (void)
1895 {
1896 int i, n;
1897
1898 # if defined (MPI_ENABLED)
1899 if (proc_id != 0)
1900 return;
1901 # endif
1902
1903 for (n=0; n<chainParams.numRuns; n++)
1904 {
1905 SafeFclose (&fpParm[n]);
1906 #if defined (PRINT_DUMP)
1907 SafeFclose (&fpDump[n]);
1908 #endif
1909
1910 for (i=0; i<numTrees; i++)
1911 {
1912 if (fpTree[n][i])
1913 {
1914 fprintf (fpTree[n][i], "end;\n");
1915 SafeFclose (&fpTree[n][i]);
1916 }
1917 }
1918 }
1919
1920 if (chainParams.mcmcDiagn == YES)
1921 SafeFclose (&fpMcmc);
1922
1923 if (chainParams.isSS == YES)
1924 SafeFclose (&fpSS);
1925 }
1926
1927
1928 /* CompactTree: prune partition tree */
CompactTree(PFNODE * p)1929 PFNODE *CompactTree (PFNODE *p)
1930 {
1931 int i, j;
1932 PFNODE *q, *r;
1933
1934 if (p == NULL)
1935 return NULL;
1936
1937 i = j = 0;
1938 if (IsPFNodeEmpty(p) == YES)
1939 {
1940 /* steal info from terminal on the way up */
1941 q = SmallestNonemptyPFNode (p->left, &i, 0);
1942 r = LargestNonemptyPFNode (p->right, &j, 0);
1943
1944 if (q != NULL || r != NULL)
1945 {
1946 if (i < j)
1947 q = r;
1948
1949 for (i=0; i<chainParams.numRuns; i++)
1950 {
1951 p->count[i] = q->count[i];
1952 q->count[i] = 0;
1953 }
1954 for (i=0; i<nLongsNeeded; i++)
1955 p->partition[i] = q->partition[i];
1956 }
1957 }
1958
1959 p->left = CompactTree (p->left);
1960 p->right = CompactTree (p->right);
1961
1962 /* delete on the way down if empty */
1963 if (IsPFNodeEmpty(p) == YES)
1964 {
1965 Tfree (p);
1966 return NULL;
1967 }
1968 else
1969 return p;
1970 }
1971
1972
1973 /*-----------------------------------------------------------------
1974 |
1975 | CopyParams: copy parameters of touched divisions
1976 |
1977 -----------------------------------------------------------------*/
CopyParams(int chain)1978 void CopyParams (int chain)
1979 {
1980 int i, j, k, fromState, toState, *fromInt, *toInt;
1981 MrBFlt *from, *to;
1982 ModelInfo *m;
1983 Param *p;
1984
1985 /* copy all params */
1986 /* now done for all vars, can also be done for only touched vars */
1987 /* but then m->upDateCl must be kept separate for each chain! */
1988 for (i=0; i<numParams; i++)
1989 {
1990 p = ¶ms[i];
1991
1992 from = GetParamVals (p, chain, state[chain]);
1993 to = GetParamVals (p, chain, (state[chain] ^ 1));
1994
1995 for (j=0; j<p->nValues; j++)
1996 to[j] = from[j];
1997
1998 from = GetParamSubVals (p, chain, state[chain]);
1999 to = GetParamSubVals (p, chain, (state[chain] ^ 1));
2000
2001 for (j=0; j<p->nSubValues; j++)
2002 to[j] = from[j];
2003
2004 fromInt = GetParamIntVals (p, chain, state[chain]);
2005 toInt = GetParamIntVals (p, chain, (state[chain] ^ 1));
2006
2007 for (j=0; j<p->nIntValues; j++)
2008 toInt[j] = fromInt[j];
2009
2010 if (p->nStdStateFreqs > 0)
2011 {
2012 from = GetParamStdStateFreqs (p, chain, state[chain]);
2013 to = GetParamStdStateFreqs (p, chain, state[chain] ^ 1);
2014 for (j=0; j<p->nStdStateFreqs; j++)
2015 to[j] = from[j];
2016 }
2017
2018 if (p->paramType == P_CPPEVENTS)
2019 {
2020 fromState = 2*chain + state[chain];
2021 toState = 2*chain + (state[chain] ^ 1);
2022 for (j=0; j<2*numLocalTaxa-2; j++)
2023 {
2024 if (p->nEvents[toState][j] != p->nEvents[fromState][j])
2025 {
2026 if (p->nEvents[fromState][j] == 0)
2027 {
2028 free (p->position[toState][j]);
2029 p->position[toState][j] = NULL;
2030 free (p->rateMult[toState][j]);
2031 p->rateMult[toState][j] = NULL;
2032 }
2033 else if (p->nEvents[toState][j] == 0)
2034 {
2035 p->position[toState][j] = (MrBFlt *) SafeCalloc (p->nEvents[fromState][j], sizeof (MrBFlt));
2036 p->rateMult[toState][j] = (MrBFlt *) SafeCalloc (p->nEvents[fromState][j], sizeof (MrBFlt));
2037 }
2038 else
2039 {
2040 p->position[toState][j] = (MrBFlt *) SafeRealloc ((void *)p->position[toState][j], p->nEvents[fromState][j] * sizeof (MrBFlt));
2041 p->rateMult[toState][j] = (MrBFlt *) SafeRealloc ((void *)p->rateMult[toState][j], p->nEvents[fromState][j] * sizeof (MrBFlt));
2042 }
2043 p->nEvents[toState][j] = p->nEvents[fromState][j];
2044 }
2045 if (p->nEvents[fromState][j] > 0)
2046 {
2047 for (k=0; k<p->nEvents[fromState][j]; k++)
2048 {
2049 p->position[toState][j][k] = p->position[fromState][j][k];
2050 p->rateMult[toState][j][k] = p->rateMult[fromState][j][k];
2051 }
2052 }
2053 }
2054 }
2055 }
2056
2057 /* copy division params (model settings) for chain */
2058 /* reset division update flags */
2059 fromState = 2 * chain + state[chain];
2060 toState = 2 * chain + (state[chain] ^ 1);
2061 for (i=0; i<numCurrentDivisions; i++)
2062 {
2063 m = &modelSettings[i];
2064 m->lnLike[toState] = m->lnLike[fromState];
2065 if (m->parsModelId == YES)
2066 m->parsTreeLength[toState] = m->parsTreeLength[fromState];
2067 m->upDateCl = NO;
2068 m->upDateCijk = NO;
2069 m->upDateAll = NO;
2070 }
2071
2072 return;
2073 }
2074
2075
2076 /* CopySiteScalers: Copy site scalers from scratch space into current space */
CopySiteScalers(ModelInfo * m,int chain)2077 void CopySiteScalers (ModelInfo *m, int chain)
2078 {
2079 CLFlt *from, *to;
2080 # if defined (BEAGLE_ENABLED)
2081 int i, j;
2082 # endif
2083
2084 # if defined (BEAGLE_ENABLED)
2085 if (m->useBeagle == YES)
2086 {
2087 j = m->siteScalerScratchIndex;
2088 for (i=0; i<m->nCijkParts; i++)
2089 {
2090 if (m->useBeagleMultiPartitions == NO)
2091 {
2092 beagleResetScaleFactors (m->beagleInstance,
2093 m->siteScalerIndex[chain] + i);
2094 beagleAccumulateScaleFactors (m->beagleInstance,
2095 &j,
2096 1,
2097 m->siteScalerIndex[chain] + i);
2098 }
2099 else
2100 # if defined (BEAGLE_V3_ENABLED)
2101 {
2102 beagleResetScaleFactorsByPartition (m->beagleInstance,
2103 m->siteScalerIndex[chain] + i,
2104 m->divisionIndex);
2105 beagleAccumulateScaleFactorsByPartition (m->beagleInstance,
2106 &j,
2107 1,
2108 m->siteScalerIndex[chain] + i,
2109 m->divisionIndex);
2110 }
2111 # endif /* BEAGLE_V3_ENABLED */
2112 j++;
2113 }
2114 return;
2115 }
2116 # endif
2117 from = m->scalers[m->siteScalerScratchIndex];
2118 to = m->scalers[m->siteScalerIndex[chain]];
2119 memcpy ((void*) to, (void*) from, (size_t)(m->numChars) * sizeof(CLFlt));
2120 }
2121
2122
2123 /*-----------------------------------------------------------------
2124 |
2125 | CopyTrees: copies touched trees for chain
2126 | resets division and node update flags in the process
2127 | Note: partition information of nodes are not copied if
2128 | either source or destination tree does not have bitsets allocated
2129 |
2130 -----------------------------------------------------------------*/
CopyTrees(int chain)2131 void CopyTrees (int chain)
2132 {
2133 int i, j, n, nTaxa, nLongsNeeded;
2134 TreeNode *p, *q;
2135 Tree *from, *to;
2136
2137 /* reset division update flags */
2138 for (i=0; i<numCurrentDivisions; i++)
2139 modelSettings[i].upDateCl = NO;
2140
2141 for (n=0; n<numTrees; n++)
2142 {
2143 from = GetTreeFromIndex (n, chain, state[chain]);
2144 to = GetTreeFromIndex (n, chain, (state[chain] ^ 1));
2145 if (from->bitsets != NULL && to->bitsets != NULL)
2146 {
2147 if (from->isRooted == NO)
2148 nTaxa = from->nNodes - from->nIntNodes;
2149 else
2150 nTaxa = from->nNodes - from->nIntNodes - 1;
2151 nLongsNeeded = (int)((nTaxa - 1) / nBitsInALong) + 1;
2152 }
2153 else
2154 nLongsNeeded = 0;
2155
2156 /* copy nodes */
2157 for (j=0; j<from->nNodes; j++)
2158 {
2159 /* copy pointers */
2160 p = from->nodes + j;
2161 q = to->nodes + j;
2162
2163 if (p->anc != NULL)
2164 q->anc = to->nodes + p->anc->memoryIndex;
2165 else
2166 q->anc = NULL;
2167
2168 if (p->left != NULL)
2169 q->left = to->nodes + p->left->memoryIndex;
2170 else
2171 q->left = NULL;
2172
2173 if (p->right != NULL)
2174 q->right = to->nodes + p->right->memoryIndex;
2175 else
2176 q->right = NULL;
2177
2178 CopyTreeNodes (q, p, nLongsNeeded);
2179 q->upDateCl = q->upDateTi = NO; /* reset update flags */
2180 }
2181
2182 for (i=0; i<from->nIntNodes; i++)
2183 {
2184 to->intDownPass[i] = to->nodes + from->intDownPass[i]->memoryIndex;
2185 #if defined (BEAGLE_V3_ENABLED)
2186 if (from->levelPassEnabled)
2187 {
2188 to->intDownPassLevel[i] = to->nodes + from->intDownPassLevel[i]->memoryIndex;
2189 }
2190 #endif
2191 }
2192 for (i=0; i<from->nNodes; i++)
2193 {
2194 to->allDownPass[i] = to->nodes + from->allDownPass[i]->memoryIndex;
2195 }
2196
2197 to->root = to->nodes + from->root->memoryIndex;
2198
2199 /* rest of tree info is constant and need not be copied */
2200 }
2201
2202 return;
2203 }
2204
2205
2206 #ifdef VISUAL
CatchInterrupt(DWORD signum)2207 BOOL WINAPI CatchInterrupt (DWORD signum)
2208 {
2209 /* set up signal handler to do the same */
2210 MrBayesPrint ("\n Ctrl-C detected\n");
2211 requestAbortRun = YES;
2212 return TRUE;
2213 }
2214 #else
CatchInterrupt(int signum)2215 void CatchInterrupt (int signum)
2216 {
2217 /* set up signal handler to do the same */
2218 signal (signum, CatchInterrupt);
2219 requestAbortRun = YES;
2220 MrBayesPrint ("\n Ctrl-C detected\n");
2221 }
2222 #endif
2223
2224
2225 /*----------------------------------------------------------------
2226 |
2227 | DebugNodeScalers: Calculate node scalers sum
2228 |
2229 -----------------------------------------------------------------*/
DebugNodeScalers(TreeNode * p,int division,int chain)2230 CLFlt DebugNodeScalers (TreeNode *p, int division, int chain)
2231 {
2232 int c;
2233 CLFlt *scP;
2234 CLFlt sum=0.0;
2235 ModelInfo *m;
2236
2237 m = &modelSettings[division];
2238
2239 /* find scalers */
2240 scP = m->scalers[m->nodeScalerIndex[chain][p->index]];
2241
2242 /* remove scalers */
2243 for (c=0; c<m->numChars; c++)
2244 sum += scP[c];
2245
2246 return sum;
2247 }
2248
2249
2250 /*----------------------------------------------------------------
2251 |
2252 | DebugTreeScalers: Calculate DebugNodeScalers for each node and printit
2253 |
2254 -----------------------------------------------------------------*/
DebugTreeScalers(int chain,int d)2255 void DebugTreeScalers(int chain, int d)
2256 {
2257 int i;
2258 TreeNode *p;
2259 ModelInfo *m;
2260 Tree *tree;
2261
2262 m = &modelSettings[d];
2263 tree = GetTree(m->brlens, chain, state[chain]);
2264
2265 if (m->parsModelId == NO)
2266 {
2267 for (i=0; i<tree->nIntNodes; i++)
2268 {
2269 p = tree->intDownPass[i];
2270
2271 if (m->unscaledNodes[chain][p->index] == 0)
2272 {
2273 printf ("Node:%d Sum scalers:%f\n",p->index,DebugNodeScalers(p, d, chain));
2274 }
2275 }
2276 }
2277 }
2278
2279
DoMcmc(void)2280 int DoMcmc (void)
2281 {
2282 RandLong seed;
2283 int rc, i, j, run;
2284 char c;
2285 FILE *tempFile;
2286 char temp[20];
2287 char *strBuf,*tmpcp;
2288 double tmp;
2289
2290 # if defined (BEST_MPI_ENABLED)
2291 Tree *tree;
2292 # endif
2293
2294 # if !defined (VISUAL) && !defined (MPI_ENABLED)
2295 sighandler_t sigint_oldhandler, sigterm_oldhandler;
2296 # endif
2297
2298 numPreviousGen = 0; /* Make sure this is reset */
2299
2300 /* Check to see that we have a data matrix. Otherwise, the MCMC is rather
2301 pointless. */
2302 if (defMatrix == NO)
2303 {
2304 MrBayesPrint ("%s A character matrix must be defined first\n", spacer);
2305 goto errorExit;
2306 }
2307
2308 if (setUpAnalysisSuccess == NO)
2309 {
2310 MrBayesPrint ("%s The analysis could not be started because there was an error during its setup.\n", spacer);
2311 MrBayesPrint ("%s Refer to error messages printed during model set up to address the problem.\n", spacer);
2312 goto errorExit;
2313 }
2314
2315 /* set file names */
2316 sumtParams.numRuns = chainParams.numRuns;
2317 sumpParams.numRuns = chainParams.numRuns;
2318 sumssParams.numRuns = chainParams.numRuns;
2319
2320 if (fileNameChanged == YES)
2321 {
2322 SetFileNames();
2323 fileNameChanged = NO;
2324 }
2325
2326 MrBayesPrint ("%s Running Markov chain\n", spacer);
2327
2328 /* Check the chain temperature parameters */
2329 if (CheckTemperature () == ERROR)
2330 goto errorExit;
2331
2332 /* Set the chain random number seeds here. We have two seeds. One
2333 (called swapSeed) is only used to determine which two chains
2334 will swap states in the next trial. The other (called seed) is
2335 the seed for our work-horse pseudorandom number generator. Note
2336 that if we are doing MPI, we want the swap seed to be the same
2337 for every processor. This is taken care of when we initialize
2338 things in the program. If we are doing MPI, we also want to make
2339 certain that seed is different for every processor. */
2340 # if defined (MPI_ENABLED)
2341 seed = globalSeed + (proc_id + 1);
2342 if (seed < 0)
2343 seed = -seed;
2344 # else
2345 seed = globalSeed;
2346 # endif
2347
2348 /* Get a unique identifier (stamp) for this run. This is used as
2349 an identifier for each mcmc analysis. It uses runIDSeed to initialize
2350 the stamp. All of the processors should have the same seed, so
2351 this should be safe. */
2352 GetStamp ();
2353
2354 MrBayesPrint ("%s Seed = %d\n", spacer, seed);
2355 MrBayesPrint ("%s Swapseed = %d\n", spacer, swapSeed);
2356
2357 /* Show the model to make sure the user sees it before running the analysis */
2358 if (ShowModel() == ERROR)
2359 goto errorExit;
2360 MrBayesPrint ("\n");
2361
2362 /* Warn the user or stop analysis in case the model is strange */
2363 if (CheckModel() == ERROR)
2364 goto errorExit;
2365
2366 /* Determine the number of local chains and data splits */
2367 if (SetLocalChainsAndDataSplits() == ERROR)
2368 goto errorExit;
2369
2370 /* Set up the moves to be used */
2371 if (SetUsedMoves () == ERROR)
2372 goto errorExit;
2373
2374 /* Check to see that we have at least one move. Otherwise, the MCMC is rather
2375 pointless. */
2376 if (numUsedMoves == 0)
2377 {
2378 MrBayesPrint ("%s No move is currently switched on.\n", spacer);
2379 MrBayesPrint ("%s There must be at least one move to run an MCMC analysis.\n", spacer);
2380 MrBayesPrint ("%s Switch on moves using the 'propset' command.\n", spacer);
2381 goto errorExit;
2382 }
2383
2384 /* Show summary table of moves that will be used */
2385 if (ShowMoveSummary () == ERROR)
2386 goto errorExit;
2387
2388 /* Set the likelihood function pointers. */
2389 if (SetLikeFunctions () == ERROR)
2390 goto errorExit;
2391
2392 /* Set up number of characters of each character pattern. */
2393 if (FillNumSitesOfPat () == ERROR)
2394 goto errorExit;
2395
2396 /* Initialize parsimony sets. */
2397 if (InitParsSets() == ERROR)
2398 goto errorExit;
2399
2400 /* Set up a terminal state index matrix for local compression. */
2401 if (SetUpTermState() == ERROR)
2402 goto errorExit;
2403
2404 /* Initialize conditional likelihoods and transition probabilities for chain (the working space). */
2405 if (InitChainCondLikes () == ERROR)
2406 goto errorExit;
2407
2408 /* Initialize adgamma conditional likelihoods */
2409 if (InitAdGamma () == ERROR)
2410 goto errorExit;
2411
2412 /* Initialize invariable conditional likelihoods. */
2413 if (InitInvCondLikes() == ERROR)
2414 goto errorExit;
2415
2416 /* Allocate BEST chain variables */
2417 if (numTopologies > 1 && !strcmp(modelParams[0].topologyPr,"Speciestree"))
2418 AllocateBestChainVariables();
2419
2420 /* allocate SS memory for the chains if needed */
2421 if (chainParams.isSS == YES)
2422 {
2423 if (memAllocs[ALLOC_SS] == YES)
2424 {
2425 MrBayesPrint ("%s SS is already allocated\n", spacer);
2426 goto errorExit;
2427 }
2428 else if ((marginalLnLSS = (MrBFlt *) SafeCalloc (chainParams.numRuns, sizeof(MrBFlt))) == NULL)
2429 {
2430 MrBayesPrint ("%s Problem allocating marginalLnLSS\n", spacer);
2431 goto errorExit;
2432 }
2433 else if ((stepScalerSS = (MrBFlt *) SafeCalloc (chainParams.numRuns, sizeof(MrBFlt))) == NULL)
2434 {
2435 MrBayesPrint ("%s Problem allocating stepScalerSS\n", spacer);
2436 free (marginalLnLSS);
2437 goto errorExit;
2438 }
2439 else if ((stepAcumulatorSS = (MrBFlt *) SafeCalloc (chainParams.numRuns, sizeof(MrBFlt))) == NULL)
2440 {
2441 MrBayesPrint ("%s Problem allocating stepAcumulatorSS\n", spacer);
2442 free (stepScalerSS);
2443 free (marginalLnLSS);
2444 goto errorExit;
2445 }
2446 else if ((splitfreqSS = (MrBFlt *) SafeCalloc (chainParams.numStepsSS*numTopologies, sizeof(MrBFlt))) == NULL)
2447 {
2448 MrBayesPrint ("%s Problem allocating splitfreqSS\n", spacer);
2449 free (stepScalerSS);
2450 free (marginalLnLSS);
2451 free (stepAcumulatorSS);
2452 goto errorExit;
2453 }
2454 else
2455 memAllocs[ALLOC_SS] = YES;
2456 }
2457
2458 /* Either append to previous run or deal with starting values */
2459 if (chainParams.append == YES)
2460 {
2461 /* Continue old run */
2462
2463 /* Get starting values from checkpoint file */
2464 MrBayesPrint ("%s Getting values from previous run\n", spacer);
2465 strcpy(inputFileName, chainParams.chainFileName);
2466 strcat(inputFileName, ".ckp");
2467 if (OpenTextFileR(inputFileName) == NULL)
2468 {
2469 MrBayesPrint ("%s Could not find the checkpoint file '%s'.\n", spacer, inputFileName);
2470 MrBayesPrint ("%s Make sure it is in the working directory.\n", spacer);
2471 goto errorExit;
2472 }
2473
2474 if (DoExecute () == ERROR)
2475 goto errorExit;
2476
2477 /* Get number of generations to start from and SS information if needed */
2478 temp[0] = '\0';
2479 numPreviousGen = 0;
2480 # if defined (MPI_ENABLED)
2481 if (proc_id == 0) {
2482 # endif
2483 tempFile = OpenBinaryFileR (inputFileName);
2484 do { c = fgetc(tempFile);
2485 } while (c!=':' && c!=EOF);
2486 if (c!=EOF)
2487 {
2488 do { c = fgetc(tempFile);
2489 } while (c!=':' && c!=EOF);
2490 }
2491 if (c!=EOF)
2492 {
2493 do { c = fgetc(tempFile);
2494 } while (!isdigit(c) && c!=EOF);
2495 }
2496 if (c!=EOF)
2497 {
2498 i=0;
2499 while (c >= '0' && c <= '9' && i < 18)
2500 {
2501 temp[i++] = c;
2502 c = fgetc(tempFile);
2503 }
2504 temp[i] = '\0';
2505 numPreviousGen = atoi(temp);
2506 }
2507 if (chainParams.isSS==YES && c!=EOF)
2508 {
2509 do { c = fgetc(tempFile);
2510 } while (c!=':' && c!=EOF);
2511 strBuf = (char *) SafeCalloc (chainParams.numRuns*20,sizeof(char));
2512 if (fgets(strBuf,chainParams.numRuns*20,tempFile)==NULL)
2513 {
2514 MrBayesPrint ("%s Error: Reading SsAcumulators from .ckp file fails.\n", spacer);
2515 free(strBuf);
2516 goto errorExit;
2517 }
2518
2519 tmpcp=strtok(strBuf," ");
2520 for (run=0; run<chainParams.numRuns; run++)
2521 {
2522 if (tmpcp == NULL)
2523 {
2524 MrBayesPrint ("%s Error: Not enough values in SsAcumulators comment of .ckp file. \n", spacer);
2525 free(strBuf);
2526 goto errorExit;
2527 }
2528
2529 tmp=atof(tmpcp);
2530 stepScalerSS[run]=tmp-10;
2531 stepAcumulatorSS[run]=exp(10);
2532 tmpcp=strtok(NULL," ]");
2533 }
2534
2535 free(strBuf);
2536 }
2537 # if defined (MPI_ENABLED)
2538 }
2539 MPI_Bcast (&numPreviousGen, 1, MPI_INT, 0, MPI_COMM_WORLD);
2540 # endif
2541 if (numPreviousGen == 0)
2542 {
2543 MrBayesPrint ("%s Could not find the number of generations in previous run.\n", spacer);
2544 goto errorExit;
2545 }
2546 else if (numPreviousGen >= chainParams.numGen)
2547 {
2548 MrBayesPrint ("%s The specified number of generations (%d) was already finished in\n", spacer, chainParams.numGen);
2549 MrBayesPrint ("%s the previous run you are trying to append to.\n", spacer);
2550 goto errorExit;
2551 }
2552 else
2553 MrBayesPrint ("%s Using samples up to generation %d from previous analysis.\n", spacer, numPreviousGen);
2554 }
2555 else
2556 {
2557 /* New run */
2558
2559 /* deal with starting param values */
2560 if (!strcmp(chainParams.startParams,"Reset"))
2561 {
2562 MrBayesPrint ("%s Resetting starting values for substitution model parameters\n", spacer);
2563 FillNormalParams (&seed, 0, numLocalChains);
2564 }
2565
2566 /* deal with starting treeparam values */
2567 if (!strcmp(chainParams.startTree,"Random"))
2568 {
2569 MrBayesPrint ("%s Resetting starting trees and tree parameters\n", spacer);
2570 FillTreeParams (&seed, 0, numLocalChains);
2571 }
2572 else if (!strcmp(chainParams.startTree,"Parsimony"))
2573 {
2574 MrBayesPrint ("%s Rebuilding starting trees using random addition sequences and parsimony\n", spacer);
2575 BuildParsTrees (&seed, 0, numLocalChains);
2576 }
2577
2578 /* Perturb start trees if requested */
2579 if (chainParams.numStartPerts > 0)
2580 {
2581 MrBayesPrint ("%s Randomly perturbing starting trees\n", spacer);
2582 for (i=0; i<numTrees; i++)
2583 {
2584 for (j=0; j<numGlobalChains; j++)
2585 RandPerturb (GetTreeFromIndex(i, j, 0), chainParams.numStartPerts, &seed);
2586 }
2587 }
2588 }
2589
2590 /* Set clockRate if we have calibration */
2591 for (j=0; j<numGlobalChains; j++)
2592 {
2593 if (UpdateClockRate(0.0, j) == ERROR)
2594 goto errorExit;
2595 }
2596 /*
2597 for (i=0; i<numParams; i++)
2598 {
2599 for (j=0; j<numGlobalChains; j++)
2600 assert (IsTreeConsistent(¶ms[i], j, 0) == YES);
2601 } */
2602
2603 /* Initialize vectors of print parameters */
2604 if (InitPrintParams () == ERROR)
2605 goto errorExit;
2606
2607 /*! setup a signal handler to catch interrupts, ignore failure */
2608 # ifdef VISUAL
2609 SetConsoleCtrlHandler (CatchInterrupt, TRUE);
2610 # else
2611 # if !defined (MPI_ENABLED)
2612 /* we do not want to mess with the signal handling in MPI version */
2613 sigint_oldhandler = signal(SIGINT, CatchInterrupt);
2614 sigterm_oldhandler = signal(SIGTERM, CatchInterrupt);
2615 # endif
2616 # endif
2617 requestAbortRun = NO;
2618
2619 /* Run the Markov chain. */
2620 rc = RunChain (&seed);
2621 if (rc == ERROR)
2622 {
2623 # ifdef VISUAL
2624 SetConsoleCtrlHandler (CatchInterrupt, FALSE);
2625 # else
2626 # if !defined (MPI_ENABLED)
2627 signal(SIGINT, sigint_oldhandler);
2628 signal(SIGTERM, sigterm_oldhandler);
2629 # endif
2630 # endif
2631 goto errorExit;
2632 }
2633 else if (rc == ABORT)
2634 {
2635 ResetChainIds();
2636 FreeChainMemory();
2637 # ifdef VISUAL
2638 SetConsoleCtrlHandler (CatchInterrupt, FALSE);
2639 # else
2640 # if !defined (MPI_ENABLED)
2641 signal(SIGINT, sigint_oldhandler);
2642 signal(SIGTERM, sigterm_oldhandler);
2643 # endif
2644 # endif
2645 return ABORT;
2646 }
2647
2648 /*! restore the default signal handler */
2649 # ifdef VISUAL
2650 SetConsoleCtrlHandler (CatchInterrupt, FALSE);
2651 # else
2652 # if !defined (MPI_ENABLED)
2653 signal(SIGINT, sigint_oldhandler);
2654 signal(SIGTERM, sigterm_oldhandler);
2655 # endif
2656 # endif
2657
2658 /* Reset the global seed at end of chain. We don't want successive
2659 chains to all start with the same random number seed. */
2660 globalSeed = seed;
2661
2662 /* Free up all memory allocated for the chain. */
2663 FreeChainMemory ();
2664
2665 return (NO_ERROR);
2666
2667 errorExit:
2668 FreeChainMemory ();
2669 return (ERROR);
2670 }
2671
2672
DoMcmcp(void)2673 int DoMcmcp (void)
2674 {
2675 if (defMatrix == NO)
2676 {
2677 MrBayesPrint ("%s A character matrix must be defined first\n", spacer);
2678 return (ERROR);
2679 }
2680
2681 sumtParams.numRuns = chainParams.numRuns;
2682 sumpParams.numRuns = chainParams.numRuns;
2683
2684 if (fileNameChanged == YES)
2685 {
2686 SetFileNames();
2687 fileNameChanged = NO;
2688 }
2689
2690 MrBayesPrint ("%s Successfully set chain parameters\n", spacer);
2691
2692 return (NO_ERROR);
2693 }
2694
2695
DoSsParm(char * parmName,char * tkn)2696 int DoSsParm (char *parmName, char *tkn)
2697 {
2698 int tempI;
2699 MrBFlt tempD;
2700 char tempStr[5];
2701 static int negBurninss;
2702
2703 if (defMatrix == NO)
2704 {
2705 MrBayesPrint ("%s A character matrix must be defined first\n", spacer);
2706 return (ERROR);
2707 }
2708
2709 if (expecting == Expecting(PARAMETER))
2710 {
2711 expecting = Expecting(EQUALSIGN);
2712 }
2713 else
2714 {
2715 if (!strcmp(parmName, "Burninss"))
2716 {
2717 if (expecting == Expecting(EQUALSIGN))
2718 {
2719 negBurninss = NO;
2720 expecting = Expecting(NUMBER) | Expecting(DASH);
2721 }
2722 else if (expecting == Expecting(DASH))
2723 {
2724 negBurninss = YES;
2725 expecting = Expecting(NUMBER);
2726 }
2727 else if (expecting == Expecting(NUMBER))
2728 {
2729 sscanf (tkn, "%d", &tempI);
2730 if (negBurninss == NO)
2731 chainParams.burninSS = tempI;
2732 else
2733 chainParams.burninSS = -tempI;
2734 MrBayesPrint ("%s Setting burnin for stepping-stone sampling to %d\n", spacer, chainParams.burninSS);
2735 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2736 }
2737 else
2738 {
2739 return (ERROR);
2740 }
2741 }
2742 else if (!strcmp(parmName, "Nsteps"))
2743 {
2744 if (expecting == Expecting(EQUALSIGN))
2745 expecting = Expecting(NUMBER);
2746 else if (expecting == Expecting(NUMBER))
2747 {
2748 sscanf (tkn, "%d", &tempI);
2749 chainParams.numStepsSS = tempI;
2750 MrBayesPrint ("%s Setting number of steps in stepping-stone sampling to %d\n", spacer, chainParams.numStepsSS);
2751 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2752 }
2753 else
2754 {
2755 return (ERROR);
2756 }
2757 }
2758 else if (!strcmp(parmName, "FromPrior"))
2759 {
2760 if (expecting == Expecting(EQUALSIGN))
2761 expecting = Expecting(ALPHA);
2762 else if (expecting == Expecting(ALPHA))
2763 {
2764 if (IsArgValid(tkn, tempStr) == NO_ERROR)
2765 {
2766 if (!strcmp(tempStr, "Yes"))
2767 chainParams.startFromPriorSS = YES;
2768 else
2769 chainParams.startFromPriorSS = NO;
2770 }
2771 else
2772 {
2773 MrBayesPrint ("%s Invalid argument for FromPrior parameter\n", spacer);
2774 return (ERROR);
2775 }
2776 MrBayesPrint ("%s Setting FromPrior=%s\n", spacer, (chainParams.startFromPriorSS==YES)?"Yes":"No");
2777 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2778 }
2779 else
2780 {
2781 return (ERROR);
2782 }
2783 }
2784 else if (!strcmp(parmName, "Alpha"))
2785 {
2786 if (expecting == Expecting(EQUALSIGN))
2787 expecting = Expecting(NUMBER);
2788 else if (expecting == Expecting(NUMBER))
2789 {
2790 sscanf (tkn, "%lf", &tempD);
2791 chainParams.alphaSS = tempD;
2792 MrBayesPrint ("%s Setting alpha in stepping-stone sampling to %lf\n", spacer, chainParams.alphaSS);
2793 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2794 }
2795 else
2796 {
2797 return (ERROR);
2798 }
2799 }
2800 else
2801 {
2802 return (ERROR);
2803 }
2804 }
2805 return (NO_ERROR);
2806 }
2807
2808
DoMcmcParm(char * parmName,char * tkn)2809 int DoMcmcParm (char *parmName, char *tkn)
2810 {
2811 int tempI;
2812 MrBFlt tempD;
2813 char *tempStr;
2814 int tempStrSize = TEMPSTRSIZE;
2815
2816 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
2817 if (!tempStr)
2818 {
2819 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
2820 return (ERROR);
2821 }
2822 *tempStr='\0';
2823
2824 if (defMatrix == NO)
2825 {
2826 MrBayesPrint ("%s A character matrix must be defined first\n", spacer);
2827 return (ERROR);
2828 }
2829
2830 if (expecting == Expecting(PARAMETER))
2831 {
2832 expecting = Expecting(EQUALSIGN);
2833 }
2834 else
2835 {
2836 /* set Seed (globalSeed) ***************************************************************/
2837 if (!strcmp(parmName, "Seed"))
2838 {
2839 MrBayesPrint ("%s Error: Setting \"Seed\" in mcmc command is deprecated. Use \"set\" command instead.\n", spacer);
2840 MrBayesPrint ("%s For more information type \"help set\";\n", spacer);
2841 free (tempStr);
2842 return (ERROR);
2843 /*
2844 if (expecting == Expecting(EQUALSIGN))
2845 expecting = Expecting(NUMBER);
2846 else if (expecting == Expecting(NUMBER))
2847 {
2848 sscanf (tkn, "%d", &tempI);
2849 globalSeed = tempI;
2850 MrBayesPrint ("%s Setting seed to %ld\n", spacer, globalSeed);
2851 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2852 }
2853 else
2854 {
2855 free (tempStr);
2856 return (ERROR);
2857 }
2858 */
2859 }
2860 /* set Swapseed (global variable swapSeed) ***************************************************************/
2861 else if (!strcmp(parmName, "Swapseed"))
2862 {
2863 MrBayesPrint ("%s Error: Setting \"Swapseed\" in mcmc command is deprecated. Use \"set\" command instead.\n", spacer);
2864 MrBayesPrint ("%s For more information type \"help set\";\n", spacer);
2865 free (tempStr);
2866 return (ERROR);
2867 /*
2868 if (expecting == Expecting(EQUALSIGN))
2869 expecting = Expecting(NUMBER);
2870 else if (expecting == Expecting(NUMBER))
2871 {
2872 sscanf (tkn, "%d", &tempI);
2873 swapSeed = tempI;
2874 MrBayesPrint ("%s Setting swapseed to %ld\n", spacer, swapSeed);
2875 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2876 }
2877 else
2878 {
2879 free (tempStr);
2880 return (ERROR);
2881 }
2882 */
2883 }
2884 /* set run ID */
2885 /* this setting is provided for GRID use only, so that identical runs can be generated */
2886 else if (!strcmp(parmName, "Runidseed"))
2887 {
2888 if (expecting == Expecting(EQUALSIGN))
2889 expecting = Expecting(NUMBER);
2890 else if (expecting == Expecting(NUMBER))
2891 {
2892 sscanf (tkn, "%d", &tempI);
2893 runIDSeed = tempI;
2894 MrBayesPrint ("%s Setting run ID [stamp] seed to %ld [for GRID use]\n", spacer, runIDSeed);
2895 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2896 }
2897 else
2898 {
2899 free (tempStr);
2900 return (ERROR);
2901 }
2902 }
2903 /* set Ngen (numGen) ******************************************************************/
2904 else if (!strcmp(parmName, "Ngen"))
2905 {
2906 if (expecting == Expecting(EQUALSIGN))
2907 expecting = Expecting(NUMBER);
2908 else if (expecting == Expecting(NUMBER))
2909 {
2910 sscanf (tkn, "%d", &tempI);
2911 if (tempI < 1)
2912 {
2913 MrBayesPrint ("%s Too few generations\n", spacer);
2914 return (ERROR);
2915 }
2916 chainParams.numGen = tempI;
2917 MrBayesPrint ("%s Setting number of generations to %d\n", spacer, chainParams.numGen);
2918 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2919 }
2920 else
2921 {
2922 free (tempStr);
2923 return (ERROR);
2924 }
2925 }
2926 /* set Samplefreq (sampleFreq) ********************************************************/
2927 else if (!strcmp(parmName, "Samplefreq"))
2928 {
2929 if (expecting == Expecting(EQUALSIGN))
2930 expecting = Expecting(NUMBER);
2931 else if (expecting == Expecting(NUMBER))
2932 {
2933 sscanf (tkn, "%d", &tempI);
2934 if (tempI < 1)
2935 {
2936 MrBayesPrint ("%s Sampling chain too infrequently\n", spacer);
2937 free (tempStr);
2938 return (ERROR);
2939 }
2940 chainParams.sampleFreq = tempI;
2941 MrBayesPrint ("%s Setting sample frequency to %d\n", spacer, chainParams.sampleFreq);
2942 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2943 }
2944 else
2945 {
2946 free (tempStr);
2947 return (ERROR);
2948 }
2949 }
2950 /* set Printfreq (printFreq) **********************************************************/
2951 else if (!strcmp(parmName, "Printfreq"))
2952 {
2953 if (expecting == Expecting(EQUALSIGN))
2954 expecting = Expecting(NUMBER);
2955 else if (expecting == Expecting(NUMBER))
2956 {
2957 sscanf (tkn, "%d", &tempI);
2958 if (tempI < 1)
2959 {
2960 MrBayesPrint ("%s Printing to screen too infrequently\n", spacer);
2961 free(tempStr);
2962 return (ERROR);
2963 }
2964 chainParams.printFreq = tempI;
2965 MrBayesPrint ("%s Setting print frequency to %d\n", spacer, chainParams.printFreq);
2966 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2967 }
2968 else
2969 {
2970 free(tempStr);
2971 return (ERROR);
2972 }
2973 }
2974 /* set Printmax (printMax) **********************************************************/
2975 else if (!strcmp(parmName, "Printmax"))
2976 {
2977 if (expecting == Expecting(EQUALSIGN))
2978 expecting = Expecting(NUMBER);
2979 else if (expecting == Expecting(NUMBER))
2980 {
2981 sscanf (tkn, "%d", &tempI);
2982 if (tempI < 1)
2983 {
2984 MrBayesPrint ("%s You need to print at least one chain\n", spacer);
2985 return (ERROR);
2986 }
2987 chainParams.printMax = tempI;
2988 MrBayesPrint ("%s Setting maximum number of chains to print to screen to %d\n", spacer, chainParams.printMax);
2989 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
2990 }
2991 else
2992 {
2993 free(tempStr);
2994 return (ERROR);
2995 }
2996 }
2997 /* set Printall (printAll) ********************************************************/
2998 else if (!strcmp(parmName, "Printall"))
2999 {
3000 if (expecting == Expecting(EQUALSIGN))
3001 expecting = Expecting(ALPHA);
3002 else if (expecting == Expecting(ALPHA))
3003 {
3004 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3005 {
3006 if (!strcmp(tempStr, "Yes"))
3007 chainParams.printAll = YES;
3008 else
3009 chainParams.printAll = NO;
3010 }
3011 else
3012 {
3013 MrBayesPrint ("%s Invalid argument for Printall\n", spacer);
3014 free (tempStr);
3015 return (ERROR);
3016 }
3017 if (chainParams.allChains == YES)
3018 MrBayesPrint ("%s Printing all chains to screen\n", spacer);
3019 else
3020 MrBayesPrint ("%s Printing only cold chains to screen\n", spacer);
3021 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3022 }
3023 else
3024 {
3025 free(tempStr);
3026 return (ERROR);
3027 }
3028 }
3029 /* set Swapfreq (swapFreq) ************************************************************/
3030 else if (!strcmp(parmName, "Swapfreq"))
3031 {
3032 if (expecting == Expecting(EQUALSIGN))
3033 expecting = Expecting(NUMBER);
3034 else if (expecting == Expecting(NUMBER))
3035 {
3036 sscanf (tkn, "%d", &tempI);
3037 if (tempI < 1)
3038 {
3039 MrBayesPrint ("%s Swapping states too infrequently\n", spacer);
3040 free(tempStr);
3041 return (ERROR);
3042 }
3043 chainParams.swapFreq = tempI;
3044 MrBayesPrint ("%s Setting swap frequency to %d\n", spacer, chainParams.swapFreq);
3045 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3046 }
3047 else
3048 {
3049 free (tempStr);
3050 return (ERROR);
3051 }
3052 }
3053 /* set Nswaps (numSwaps) ************************************************************/
3054 else if (!strcmp(parmName, "Nswaps"))
3055 {
3056 if (expecting == Expecting(EQUALSIGN))
3057 expecting = Expecting(NUMBER);
3058 else if (expecting == Expecting(NUMBER))
3059 {
3060 sscanf (tkn, "%d", &tempI);
3061 if (tempI < 1)
3062 {
3063 MrBayesPrint ("%s There must be at least one swap per swapping cycle\n", spacer);
3064 free (tempStr);
3065 return (ERROR);
3066 }
3067 chainParams.numSwaps = tempI;
3068 MrBayesPrint ("%s Setting number of swaps per swapping cycle to %d\n", spacer, chainParams.numSwaps);
3069 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3070 }
3071 else
3072 {
3073 free(tempStr);
3074 return (ERROR);
3075 }
3076 }
3077 /* set Allchains (allChains) ********************************************************/
3078 else if (!strcmp(parmName, "Allchains"))
3079 {
3080 if (expecting == Expecting(EQUALSIGN))
3081 expecting = Expecting(ALPHA);
3082 else if (expecting == Expecting(ALPHA))
3083 {
3084 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3085 {
3086 if (!strcmp(tempStr, "Yes"))
3087 chainParams.allChains = YES;
3088 else
3089 chainParams.allChains = NO;
3090 }
3091 else
3092 {
3093 MrBayesPrint ("%s Invalid argument for Allchains\n", spacer);
3094 free (tempStr);
3095 return (ERROR);
3096 }
3097 if (chainParams.allChains == YES)
3098 MrBayesPrint ("%s Calculating MCMC diagnostics for all chains\n", spacer);
3099 else
3100 MrBayesPrint ("%s Calculating MCMC diagnostics only for cold chain(s)\n", spacer);
3101 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3102 }
3103 else
3104 {
3105 free (tempStr);
3106 return (ERROR);
3107 }
3108 }
3109 /* set Allcomps (allComps) ************************************************************/
3110 else if (!strcmp(parmName, "Allcomps"))
3111 {
3112 if (expecting == Expecting(EQUALSIGN))
3113 expecting = Expecting(ALPHA);
3114 else if (expecting == Expecting(ALPHA))
3115 {
3116 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3117 {
3118 if (!strcmp(tempStr, "Yes"))
3119 chainParams.allComps = YES;
3120 else
3121 chainParams.allComps = NO;
3122 }
3123 else
3124 {
3125 MrBayesPrint ("%s Invalid argument for Allcomps\n", spacer);
3126 free (tempStr);
3127 return (ERROR);
3128 }
3129 if (chainParams.allComps == YES)
3130 MrBayesPrint ("%s Calculating MCMC diagnostics for all pairwise run comparisons\n", spacer);
3131 else
3132 MrBayesPrint ("%s Only calculating overall MCMC diagnostics\n", spacer);
3133 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3134 }
3135 else
3136 {
3137 free(tempStr);
3138 return (ERROR);
3139 }
3140 }
3141 /* set Mcmcdiagn (mcmcDiagn) ********************************************************/
3142 else if (!strcmp(parmName, "Mcmcdiagn"))
3143 {
3144 if (expecting == Expecting(EQUALSIGN))
3145 expecting = Expecting(ALPHA);
3146 else if (expecting == Expecting(ALPHA))
3147 {
3148 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3149 {
3150 if (!strcmp(tempStr, "Yes"))
3151 chainParams.mcmcDiagn = YES;
3152 else
3153 chainParams.mcmcDiagn = NO;
3154 }
3155 else
3156 {
3157 MrBayesPrint ("%s Invalid argument for mcmc diagnostics\n", spacer);
3158 free(tempStr);
3159 return (ERROR);
3160 }
3161 if (chainParams.mcmcDiagn == YES)
3162 MrBayesPrint ("%s Setting calculation of MCMC diagnostics ('Mcmcdiagn') to yes\n", spacer);
3163 else
3164 MrBayesPrint ("%s Setting calculation of MCMC diagnostics ('Mcmcdiagn') to no\n", spacer);
3165 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3166 }
3167 else
3168 {
3169 free(tempStr);
3170 return (ERROR);
3171 }
3172 }
3173 /* set Diagnfreq (diagnFreq) ************************************************************/
3174 else if (!strcmp(parmName, "Diagnfreq"))
3175 {
3176 if (expecting == Expecting(EQUALSIGN))
3177 expecting = Expecting(NUMBER);
3178 else if (expecting == Expecting(NUMBER))
3179 {
3180 sscanf (tkn, "%d", &tempI);
3181 if (tempI < 1)
3182 {
3183 MrBayesPrint ("%s Diagnosing MCMC behavior too infrequently\n", spacer);
3184 free(tempStr);
3185 return (ERROR);
3186 }
3187 chainParams.diagnFreq = tempI;
3188 MrBayesPrint ("%s Setting diagnosing frequency to %d\n", spacer, chainParams.diagnFreq);
3189 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3190 }
3191 else
3192 {
3193 free(tempStr);
3194 return (ERROR);
3195 }
3196 }
3197 /* set Savetrees (saveTrees) ********************************************************/
3198 else if (!strcmp(parmName, "Savetrees"))
3199 {
3200 if (expecting == Expecting(EQUALSIGN))
3201 expecting = Expecting(ALPHA);
3202 else if (expecting == Expecting(ALPHA))
3203 {
3204 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3205 {
3206 if (!strcmp(tempStr, "Yes"))
3207 chainParams.saveTrees = YES;
3208 else
3209 chainParams.saveTrees = NO;
3210 }
3211 else
3212 {
3213 MrBayesPrint ("%s Invalid argument for Savetrees\n", spacer);
3214 free(tempStr);
3215 return (ERROR);
3216 }
3217 if (chainParams.saveTrees == YES)
3218 MrBayesPrint ("%s Saving trees for MCMC diagnostics in memory (if needed)\n", spacer);
3219 else
3220 MrBayesPrint ("%s Not saving trees for MCMC diagnostics in memory\n", spacer);
3221 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3222 }
3223 else
3224 {
3225 free(tempStr);
3226 return (ERROR);
3227 }
3228 }
3229 /* set Diagnstat (diagnStat) ********************************************************/
3230 else if (!strcmp(parmName, "Diagnstat"))
3231 {
3232 if (expecting == Expecting(EQUALSIGN))
3233 expecting = Expecting(ALPHA);
3234 else if (expecting == Expecting(ALPHA))
3235 {
3236 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3237 {
3238 if (!strcmp(tempStr, "Avgstddev"))
3239 chainParams.diagnStat = AVGSTDDEV;
3240 else /* if (!strcmp(tempStr, "Maxstddev")) */
3241 chainParams.diagnStat = MAXSTDDEV;
3242 }
3243 else
3244 {
3245 MrBayesPrint ("%s Invalid argument for Savetrees\n", spacer);
3246 free(tempStr);
3247 return (ERROR);
3248 }
3249 if (chainParams.diagnStat == AVGSTDDEV)
3250 MrBayesPrint ("%s Setting diagnostics statistic to Avgstddev\n", spacer);
3251 else /* if (chainParams.diagnStat == MAXSTDDEV) */
3252 MrBayesPrint ("%s Setting diagnostics statistic to Maxstddev\n", spacer);
3253 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3254 }
3255 else
3256 {
3257 free(tempStr);
3258 return (ERROR);
3259 }
3260 }
3261 /* set Checkpoint (checkPoint) ********************************************************/
3262 else if (!strcmp(parmName, "Checkpoint"))
3263 {
3264 if (expecting == Expecting(EQUALSIGN))
3265 expecting = Expecting(ALPHA);
3266 else if (expecting == Expecting(ALPHA))
3267 {
3268 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3269 {
3270 if (!strcmp(tempStr, "Yes"))
3271 chainParams.checkPoint = YES;
3272 else
3273 chainParams.checkPoint = NO;
3274 }
3275 else
3276 {
3277 MrBayesPrint ("%s Invalid argument for 'Checkpoint' (check-pointing)\n", spacer);
3278 free(tempStr);
3279 return (ERROR);
3280 }
3281 if (chainParams.checkPoint == YES)
3282 MrBayesPrint ("%s Setting check-pointing ('Checkpoint') to yes\n", spacer);
3283 else
3284 MrBayesPrint ("%s Setting check-pointing ('Checkpoint') to no\n", spacer);
3285 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3286 }
3287 else
3288 {
3289 free(tempStr);
3290 return (ERROR);
3291 }
3292 }
3293 /* set Checkfreq (checkFreq) ************************************************************/
3294 else if (!strcmp(parmName, "Checkfreq"))
3295 {
3296 if (expecting == Expecting(EQUALSIGN))
3297 expecting = Expecting(NUMBER);
3298 else if (expecting == Expecting(NUMBER))
3299 {
3300 sscanf (tkn, "%d", &tempI);
3301 if (tempI < 100)
3302 {
3303 MrBayesPrint ("%s Check-pointing frequency must be at least 100\n", spacer);
3304 free(tempStr);
3305 return (ERROR);
3306 }
3307 chainParams.checkFreq = tempI;
3308 MrBayesPrint ("%s Setting check-pointing frequency to %d\n", spacer, chainParams.checkFreq);
3309 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3310 }
3311 else
3312 {
3313 free(tempStr);
3314 return (ERROR);
3315 }
3316 }
3317 /* set Minpartfreq (minPartFreq) ************************************************************/
3318 else if (!strcmp(parmName, "Minpartfreq"))
3319 {
3320 if (expecting == Expecting(EQUALSIGN))
3321 expecting = Expecting(NUMBER);
3322 else if (expecting == Expecting(NUMBER))
3323 {
3324 sscanf (tkn, "%lf", &tempD);
3325 if (tempD < 0.01)
3326 {
3327 MrBayesPrint ("%s Minimum partition frequency too low (< 0.01)\n", spacer);
3328 free (tempStr);
3329 return (ERROR);
3330 }
3331 if (tempD > 0.8)
3332 {
3333 MrBayesPrint ("%s Minimum partition frequency too high (> 0.8)\n", spacer);
3334 free (tempStr);
3335 return (ERROR);
3336 }
3337 chainParams.minPartFreq = tempD;
3338 MrBayesPrint ("%s Setting minimum partition frequency to %.2f\n", spacer, chainParams.minPartFreq);
3339 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3340 }
3341 else
3342 {
3343 free(tempStr);
3344 return (ERROR);
3345 }
3346 }
3347 /* set Nruns (numRuns) ****************************************************************/
3348 else if (!strcmp(parmName, "Nruns"))
3349 {
3350 if (expecting == Expecting(EQUALSIGN))
3351 expecting = Expecting(NUMBER);
3352 else if (expecting == Expecting(NUMBER))
3353 {
3354 sscanf (tkn, "%d", &tempI);
3355 if (tempI < 1)
3356 {
3357 MrBayesPrint ("%s Too few runs (minimum of 1 run)\n", spacer);
3358 free(tempStr);
3359 return (ERROR);
3360 }
3361 if (tempI > MAX_RUNS)
3362 {
3363 MrBayesPrint ("%s Too many runs (maximum of %d runs)\n", spacer, MAX_RUNS);
3364 free(tempStr);
3365 return (ERROR);
3366 }
3367 if (ChangeNumRuns (chainParams.numRuns, tempI) == ERROR)
3368 return (ERROR);
3369 chainParams.numRuns = tempI;
3370 fileNameChanged = YES;
3371 MrBayesPrint ("%s Setting number of runs to %d\n", spacer, chainParams.numRuns);
3372 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3373 }
3374 else
3375 {
3376 free(tempStr);
3377 return (ERROR);
3378 }
3379 }
3380 /* set Nchains (numChains) ************************************************************/
3381 else if (!strcmp(parmName, "Nchains"))
3382 {
3383 if (expecting == Expecting(EQUALSIGN))
3384 expecting = Expecting(NUMBER);
3385 else if (expecting == Expecting(NUMBER))
3386 {
3387 sscanf (tkn, "%d", &tempI);
3388 if (tempI < 1)
3389 {
3390 MrBayesPrint ("%s Too few chains (minimum of 1 chain)\n", spacer);
3391 free(tempStr);
3392 return (ERROR);
3393 }
3394 if (tempI > MAX_CHAINS)
3395 {
3396 MrBayesPrint ("%s Too many chains (maximum of %d chains)\n", spacer, MAX_CHAINS);
3397 free(tempStr);
3398 return (ERROR);
3399 }
3400 if (ChangeNumChains (chainParams.numChains, tempI) == ERROR)
3401 return (ERROR);
3402 chainParams.numChains = tempI;
3403 MrBayesPrint ("%s Setting number of chains to %d\n", spacer, chainParams.numChains);
3404 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3405 }
3406 else
3407 {
3408 free(tempStr);
3409 return (ERROR);
3410 }
3411 }
3412 /* set Temp (chainTemp) ***************************************************************/
3413 else if (!strcmp(parmName, "Temp"))
3414 {
3415 if (expecting == Expecting(EQUALSIGN))
3416 {
3417 tempIndex = 0;
3418 expecting = Expecting(NUMBER) | Expecting(LEFTPAR);
3419 }
3420 else if (expecting == Expecting(LEFTPAR))
3421 {
3422 chainParams.userDefinedTemps = YES;
3423 expecting = Expecting(NUMBER);
3424 }
3425 else if (expecting == Expecting(RIGHTPAR))
3426 {
3427 MrBayesPrint ("%s Setting user-defined temperatures\n", spacer);
3428 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3429 }
3430 else if (expecting == Expecting(COMMA))
3431 {
3432 expecting = Expecting(NUMBER);
3433 }
3434 else if (expecting == Expecting(NUMBER))
3435 {
3436 if (chainParams.userDefinedTemps == NO)
3437 {
3438 sscanf (tkn, "%lf", &tempD);
3439 chainParams.chainTemp = tempD;
3440 MrBayesPrint ("%s Setting heating parameter to %lf\n", spacer, chainParams.chainTemp);
3441 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3442 }
3443 else
3444 {
3445 if (tempIndex >= MAX_CHAINS)
3446 {
3447 MrBayesPrint ("%s Too many user-defined temperatures (%d maximum)\n", spacer, MAX_CHAINS);
3448 free(tempStr);
3449 return (ERROR);
3450 }
3451 sscanf (tkn, "%lf", &tempD);
3452 chainParams.userTemps[tempIndex++] = tempD;
3453 expecting = Expecting(COMMA) | Expecting(RIGHTPAR);
3454 }
3455 }
3456 else
3457 {
3458 free(tempStr);
3459 return (ERROR);
3460 }
3461 }
3462 /* set Reweight (weightScheme) ********************************************************/
3463 else if (!strcmp(parmName, "Reweight"))
3464 {
3465 if (expecting == Expecting(EQUALSIGN))
3466 expecting = Expecting(LEFTPAR);
3467 else if (expecting == Expecting(LEFTPAR))
3468 {
3469 expecting = Expecting(NUMBER);
3470 whichReweightNum = 0;
3471 }
3472 else if (expecting == Expecting(NUMBER))
3473 {
3474 if (whichReweightNum < 0 || whichReweightNum > 2)
3475 {
3476 free(tempStr);
3477 return (ERROR);
3478 }
3479 sscanf (tkn, "%lf", &tempD);
3480 chainParams.weightScheme[whichReweightNum] = tempD;
3481 if (whichReweightNum < 2)
3482 {
3483 if (tempD < 0.0 || tempD > 100.0)
3484 {
3485 MrBayesPrint ("%s The reweighting parameter must be between 0 and 100\n", spacer);
3486 chainParams.weightScheme[0] = chainParams.weightScheme[1] = 0.0;
3487 chainParams.weightScheme[2] = 1.0;
3488 free(tempStr);
3489 return (ERROR);
3490 }
3491 }
3492 else
3493 {
3494 if (tempD <= 0.0 || tempD > 1.0)
3495 {
3496 MrBayesPrint ("%s The reweighting increment must be between 0 and 1\n", spacer);
3497 chainParams.weightScheme[0] = chainParams.weightScheme[1] = 0.0;
3498 chainParams.weightScheme[2] = 1.0;
3499 free(tempStr);
3500 return (ERROR);
3501 }
3502 }
3503 if (whichReweightNum == 0)
3504 {
3505 expecting = Expecting(COMMA);
3506 }
3507 else if (whichReweightNum == 1)
3508 {
3509 if (chainParams.weightScheme[0] + chainParams.weightScheme[1] > 100.0)
3510 {
3511 MrBayesPrint ("%s The sum of the reweighting parameters cannot exceed 100 %%\n", spacer);
3512 chainParams.weightScheme[0] = chainParams.weightScheme[1] = 0.0;
3513 chainParams.weightScheme[2] = 1.0;
3514 free(tempStr);
3515 return (ERROR);
3516 }
3517 expecting = Expecting(COMMA) | Expecting(RIGHTPAR);
3518 }
3519 else
3520 {
3521 expecting = Expecting(RIGHTPAR);
3522 }
3523 whichReweightNum++;
3524 }
3525 else if ((expecting & Expecting(COMMA)) == Expecting(COMMA))
3526 expecting = Expecting(NUMBER);
3527 else if ((expecting & Expecting(RIGHTPAR)) == Expecting(RIGHTPAR))
3528 {
3529 if (chainParams.weightScheme[0] >= 100.0)
3530 {
3531 MrBayesPrint ("%s Cannot decrease weight of all characters\n", spacer);
3532 chainParams.weightScheme[0] = chainParams.weightScheme[1] = 0.0;
3533 chainParams.weightScheme[2] = 1.0;
3534 free(tempStr);
3535 return (ERROR);
3536 }
3537 MrBayesPrint ("%s Setting reweighting parameter to (%1.2lf v, %1.2lf ^) increment = %1.2lf\n",
3538 spacer, chainParams.weightScheme[0], chainParams.weightScheme[1], chainParams.weightScheme[2]);
3539 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3540 }
3541 else
3542 {
3543 free(tempStr);
3544 return (ERROR);
3545 }
3546 }
3547 /* set Filename (chainFileName) *******************************************************/
3548 else if (!strcmp(parmName, "Filename"))
3549 {
3550 if (expecting == Expecting(EQUALSIGN))
3551 {
3552 expecting = Expecting(ALPHA);
3553 readWord = YES;
3554 }
3555 else if (expecting == Expecting(ALPHA))
3556 {
3557 sscanf (tkn, "%s", tempStr);
3558 if (strlen(tempStr)>99)
3559 {
3560 MrBayesPrint ("%s Maximum allowed length of chain file name is 99 characters. The given name:\n", spacer);
3561 MrBayesPrint ("%s '%s'\n", spacer,tempStr);
3562 MrBayesPrint ("%s has %d characters.\n", spacer,strlen(tempStr));
3563 return (ERROR);
3564 }
3565 strcpy (chainParams.chainFileName, tempStr);
3566 fileNameChanged = YES;
3567 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3568 }
3569 else
3570 {
3571 free(tempStr);
3572 return (ERROR);
3573 }
3574 }
3575 /* set Relburnin (relativeBurnin) ********************************************************/
3576 else if (!strcmp(parmName, "Relburnin"))
3577 {
3578 if (expecting == Expecting(EQUALSIGN))
3579 expecting = Expecting(ALPHA);
3580 else if (expecting == Expecting(ALPHA))
3581 {
3582 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3583 {
3584 if (!strcmp(tempStr, "Yes"))
3585 chainParams.relativeBurnin = YES;
3586 else
3587 chainParams.relativeBurnin = NO;
3588 }
3589 else
3590 {
3591 MrBayesPrint ("%s Invalid argument for Relburnin\n", spacer);
3592 free(tempStr);
3593 return (ERROR);
3594 }
3595 if (chainParams.relativeBurnin == YES)
3596 MrBayesPrint ("%s Using relative burnin (a fraction of samples discarded).\n", spacer);
3597 else
3598 MrBayesPrint ("%s Using absolute burnin (a fixed number of samples discarded).\n", spacer);
3599 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3600 }
3601 else
3602 {
3603 free (tempStr);
3604 return (ERROR);
3605 }
3606 }
3607 /* set Burnin (chainBurnIn) ***********************************************************/
3608 else if (!strcmp(parmName, "Burnin"))
3609 {
3610 if (expecting == Expecting(EQUALSIGN))
3611 expecting = Expecting(NUMBER);
3612 else if (expecting == Expecting(NUMBER))
3613 {
3614 sscanf (tkn, "%d", &tempI);
3615 chainParams.chainBurnIn = tempI;
3616 MrBayesPrint ("%s Setting chain burn-in to %d\n", spacer, chainParams.chainBurnIn);
3617 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3618 }
3619 else
3620 {
3621 free(tempStr);
3622 return (ERROR);
3623 }
3624 }
3625 /* set Burninfrac (burninFraction) ************************************************************/
3626 else if (!strcmp(parmName, "Burninfrac"))
3627 {
3628 if (expecting == Expecting(EQUALSIGN))
3629 expecting = Expecting(NUMBER);
3630 else if (expecting == Expecting(NUMBER))
3631 {
3632 sscanf (tkn, "%lf", &tempD);
3633 if (tempD < 0.01)
3634 {
3635 MrBayesPrint ("%s Burnin fraction too low (< 0.01)\n", spacer);
3636 free(tempStr);
3637 return (ERROR);
3638 }
3639 if (tempD > 0.50)
3640 {
3641 MrBayesPrint ("%s Burnin fraction too high (> 0.50)\n", spacer);
3642 free(tempStr);
3643 return (ERROR);
3644 }
3645 chainParams.burninFraction = tempD;
3646 MrBayesPrint ("%s Setting burnin fraction to %.2f\n", spacer, chainParams.burninFraction);
3647 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3648 }
3649 else
3650 {
3651 free(tempStr);
3652 return (ERROR);
3653 }
3654 }
3655 /* set Stoprule (stopRule) ********************************************************/
3656 else if (!strcmp(parmName, "Stoprule"))
3657 {
3658 if (expecting == Expecting(EQUALSIGN))
3659 expecting = Expecting(ALPHA);
3660 else if (expecting == Expecting(ALPHA))
3661 {
3662 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3663 {
3664 if (!strcmp(tempStr, "Yes"))
3665 chainParams.stopRule = YES;
3666 else
3667 chainParams.stopRule = NO;
3668 }
3669 else
3670 {
3671 MrBayesPrint ("%s Invalid argument for Stoprule\n", spacer);
3672 free(tempStr);
3673 return (ERROR);
3674 }
3675 if (chainParams.stopRule == YES)
3676 MrBayesPrint ("%s Using stopping rule.\n", spacer);
3677 else
3678 MrBayesPrint ("%s Not using stopping rule.\n", spacer);
3679 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3680 }
3681 else
3682 {
3683 free(tempStr);
3684 return (ERROR);
3685 }
3686 }
3687 /* set Stopval (stopVal) ************************************************************/
3688 else if (!strcmp(parmName, "Stopval"))
3689 {
3690 if (expecting == Expecting(EQUALSIGN))
3691 expecting = Expecting(NUMBER);
3692 else if (expecting == Expecting(NUMBER))
3693 {
3694 sscanf (tkn, "%lf", &tempD);
3695 if (tempD < 0.000001)
3696 {
3697 MrBayesPrint ("%s Stop value too low (< 0.000001)\n", spacer);
3698 free(tempStr);
3699 return (ERROR);
3700 }
3701 if (tempD > 0.20)
3702 {
3703 MrBayesPrint ("%s Stop value too high (> 0.20)\n", spacer);
3704 free(tempStr);
3705 return (ERROR);
3706 }
3707 chainParams.stopVal = tempD;
3708 MrBayesPrint ("%s Setting burnin fraction to %.2f\n", spacer, chainParams.burninFraction);
3709 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3710 }
3711 else
3712 {
3713 free(tempStr);
3714 return (ERROR);
3715 }
3716 }
3717 /* set Starttree (startTree) **************************************************/
3718 else if (!strcmp(parmName, "Starttree"))
3719 {
3720 if (expecting == Expecting(EQUALSIGN))
3721 expecting = Expecting(ALPHA);
3722 else if (expecting == Expecting(ALPHA))
3723 {
3724 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3725 {
3726 if (!strcmp(tempStr,"User"))
3727 {
3728 MrBayesPrint ("%s The 'user' setting of 'Starttree' is deprecated. Set starting trees using 'Startvals' instead.\n", spacer);
3729 free (tempStr);
3730 return (ERROR);
3731 }
3732 else
3733 strcpy(chainParams.startTree, tempStr);
3734 }
3735 else
3736 {
3737 MrBayesPrint ("%s Invalid 'Starttree' argument '%s'. This is a bug -- please report.\n", spacer, tempStr);
3738 free(tempStr);
3739 return (ERROR);
3740 }
3741 MrBayesPrint ("%s Setting 'Starttree' to \"%s\"\n", spacer, chainParams.startTree);
3742 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3743 }
3744 else
3745 {
3746 free(tempStr);
3747 return (ERROR);
3748 }
3749 }
3750 /* set Startingtrees (deprecated) **************************************************/
3751 else if (!strcmp(parmName, "Startingtrees"))
3752 {
3753 free (tempStr);
3754 MrBayesPrint ("%s Parameter 'Startingtrees' is deprecated. Use the 'Starttree' parameter or the 'Startvals' command instead.\n", spacer);
3755 return (ERROR);
3756 }
3757 /* set Nperts (numStartPerts) *********************************************************/
3758 else if (!strcmp(parmName, "Nperts"))
3759 {
3760 if (expecting == Expecting(EQUALSIGN))
3761 expecting = Expecting(NUMBER);
3762 else if (expecting == Expecting(NUMBER))
3763 {
3764 sscanf (tkn, "%d", &tempI);
3765 chainParams.numStartPerts = tempI;
3766 MrBayesPrint ("%s Setting number of perturbations to start tree to %d\n", spacer, chainParams.numStartPerts);
3767 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3768 }
3769 else
3770 {
3771 free(tempStr);
3772 return (ERROR);
3773 }
3774 }
3775 /* set Startparams (startParams) **************************************************/
3776 else if (!strcmp(parmName, "Startparams"))
3777 {
3778 if (expecting == Expecting(EQUALSIGN))
3779 expecting = Expecting(ALPHA);
3780 else if (expecting == Expecting(ALPHA))
3781 {
3782 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3783 strcpy(chainParams.startParams, tempStr);
3784 else
3785 {
3786 MrBayesPrint ("%s Invalid 'Startparams' argument\n", spacer);
3787 free(tempStr);
3788 return (ERROR);
3789 }
3790 MrBayesPrint ("%s Setting 'Startparams' to \"%s\"\n", spacer, chainParams.startParams);
3791 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3792 }
3793 else
3794 {
3795 free(tempStr);
3796 return (ERROR);
3797 }
3798 }
3799 /* set Savebrlens (saveBrlens) ********************************************************/
3800 else if (!strcmp(parmName, "Savebrlens"))
3801 {
3802 if (expecting == Expecting(EQUALSIGN))
3803 expecting = Expecting(ALPHA);
3804 else if (expecting == Expecting(ALPHA))
3805 {
3806 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3807 {
3808 if (!strcmp(tempStr, "Yes"))
3809 ; /* This is the only option in version 3.2 */
3810 else
3811 MrBayesPrint ("%s WARNING: Ignoring savebrlens setting; since version 3.2, branch lengths are always saved\n", spacer);
3812 }
3813 else
3814 {
3815 MrBayesPrint ("%s Invalid argument for savebrlens\n", spacer);
3816 free(tempStr);
3817 return (ERROR);
3818 }
3819 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3820 }
3821 }
3822 /* set Redirect (redirect) ********************************************************/
3823 else if (!strcmp(parmName, "Redirect"))
3824 {
3825 if (expecting == Expecting(EQUALSIGN))
3826 expecting = Expecting(ALPHA);
3827 else if (expecting == Expecting(ALPHA))
3828 {
3829 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3830 {
3831 if (!strcmp(tempStr, "Yes"))
3832 chainParams.redirect = YES;
3833 else
3834 chainParams.redirect = NO;
3835 }
3836 else
3837 {
3838 MrBayesPrint ("%s Invalid argument for redirecting output\n", spacer);
3839 free(tempStr);
3840 return (ERROR);
3841 }
3842 if (chainParams.redirect == YES)
3843 MrBayesPrint ("%s Setting program to redirect output\n", spacer);
3844 else
3845 MrBayesPrint ("%s Setting program not to redirect output\n", spacer);
3846 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3847 }
3848 else
3849 {
3850 free(tempStr);
3851 return (ERROR);
3852 }
3853 }
3854 /* set Data (runWithData) ************************************************************/
3855 else if (!strcmp(parmName, "Data"))
3856 {
3857 if (expecting == Expecting(EQUALSIGN))
3858 expecting = Expecting(ALPHA);
3859 else if (expecting == Expecting(ALPHA))
3860 {
3861 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3862 {
3863 if (!strcmp(tempStr, "Yes"))
3864 chainParams.runWithData = YES;
3865 else
3866 chainParams.runWithData = NO;
3867 }
3868 else
3869 {
3870 MrBayesPrint ("%s Invalid argument for Data\n", spacer);
3871 free(tempStr);
3872 return (ERROR);
3873 }
3874 if (chainParams.runWithData == NO)
3875 MrBayesPrint ("%s Running without data (WARNING: use this only for checking priors!)\n", spacer);
3876 else
3877 MrBayesPrint ("%s Running with data (standard analysis)\n", spacer);
3878 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3879 }
3880 else
3881 {
3882 free(tempStr);
3883 return (ERROR);
3884 }
3885 }
3886 /* set Ordertaxa (chainParams.orderTaxa) *********************************************/
3887 else if (!strcmp(parmName, "Ordertaxa"))
3888 {
3889 if (expecting == Expecting(EQUALSIGN))
3890 expecting = Expecting(ALPHA);
3891 else if (expecting == Expecting(ALPHA))
3892 {
3893 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3894 {
3895 if (!strcmp(tempStr, "Yes"))
3896 chainParams.orderTaxa = YES;
3897 else
3898 chainParams.orderTaxa = NO;
3899 }
3900 else
3901 {
3902 MrBayesPrint ("%s Invalid argument for ordertaxa\n", spacer);
3903 free(tempStr);
3904 return (ERROR);
3905 }
3906 if (chainParams.orderTaxa == YES)
3907 MrBayesPrint ("%s Setting ordertaxa to yes\n", spacer);
3908 else
3909 MrBayesPrint ("%s Setting ordertaxa to no\n", spacer);
3910 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3911 }
3912 else
3913 {
3914 free(tempStr);
3915 return (ERROR);
3916 }
3917 }
3918 /* set Append (chainParams.append) *********************************************/
3919 else if (!strcmp(parmName, "Append"))
3920 {
3921 if (expecting == Expecting(EQUALSIGN))
3922 expecting = Expecting(ALPHA);
3923 else if (expecting == Expecting(ALPHA))
3924 {
3925 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3926 {
3927 if (!strcmp(tempStr, "Yes"))
3928 chainParams.append = YES;
3929 else
3930 chainParams.append = NO;
3931 }
3932 else
3933 {
3934 MrBayesPrint ("%s Invalid argument for append\n", spacer);
3935 free(tempStr);
3936 return (ERROR);
3937 }
3938 if (chainParams.append == YES)
3939 MrBayesPrint ("%s Setting append to yes\n", spacer);
3940 else
3941 MrBayesPrint ("%s Setting append to no\n", spacer);
3942 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3943 }
3944 else
3945 {
3946 free(tempStr);
3947 return (ERROR);
3948 }
3949 }
3950 /* set Autotune (chainParams.autotune) *********************************************/
3951 else if (!strcmp(parmName, "Autotune"))
3952 {
3953 if (expecting == Expecting(EQUALSIGN))
3954 expecting = Expecting(ALPHA);
3955 else if (expecting == Expecting(ALPHA))
3956 {
3957 if (IsArgValid(tkn, tempStr) == NO_ERROR)
3958 {
3959 if (!strcmp(tempStr, "Yes"))
3960 chainParams.autotune = YES;
3961 else
3962 chainParams.autotune = NO;
3963 }
3964 else
3965 {
3966 MrBayesPrint ("%s Invalid argument for Autotune\n", spacer);
3967 free(tempStr);
3968 return (ERROR);
3969 }
3970 if (chainParams.autotune == YES)
3971 MrBayesPrint ("%s Setting Autotune to yes\n", spacer);
3972 else
3973 MrBayesPrint ("%s Setting Autotune to no\n", spacer);
3974 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3975 }
3976 else
3977 {
3978 free(tempStr);
3979 return (ERROR);
3980 }
3981 }
3982 /* set Tunefreq (tuneFreq) ************************************************************/
3983 else if (!strcmp(parmName, "Tunefreq"))
3984 {
3985 if (expecting == Expecting(EQUALSIGN))
3986 expecting = Expecting(NUMBER);
3987 else if (expecting == Expecting(NUMBER))
3988 {
3989 sscanf (tkn, "%d", &tempI);
3990 if (tempI < 10)
3991 {
3992 MrBayesPrint ("%s Autotuning frequency must be at least 10\n", spacer);
3993 free(tempStr);
3994 return (ERROR);
3995 }
3996 chainParams.tuneFreq = tempI;
3997 MrBayesPrint ("%s Setting autotuning frequency to %d\n", spacer, chainParams.tuneFreq);
3998 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
3999 }
4000 else
4001 {
4002 free(tempStr);
4003 return (ERROR);
4004 }
4005 }
4006 /* set Swapadjacent (swapAdjacentOnly) **************************************************/
4007 else if (!strcmp(parmName, "Swapadjacent"))
4008 {
4009 if (expecting == Expecting(EQUALSIGN))
4010 expecting = Expecting(ALPHA);
4011 else if (expecting == Expecting(ALPHA))
4012 {
4013 if (IsArgValid(tkn, tempStr) == NO_ERROR)
4014 {
4015 if (!strcmp(tempStr, "Yes"))
4016 chainParams.swapAdjacentOnly = YES;
4017 else
4018 chainParams.swapAdjacentOnly = NO;
4019 }
4020 else
4021 {
4022 MrBayesPrint ("%s Invalid argument for Swapadjacent\n", spacer);
4023 free(tempStr);
4024 return (ERROR);
4025 }
4026 if (chainParams.swapAdjacentOnly == YES)
4027 MrBayesPrint ("%s Setting program to attempt swaps only between chains of adjacent temperatures\n", spacer);
4028 else
4029 MrBayesPrint ("%s Setting program to attempt all possible swaps between chains\n", spacer);
4030 expecting = Expecting(PARAMETER) | Expecting(SEMICOLON);
4031 }
4032 else
4033 {
4034 free(tempStr);
4035 return (ERROR);
4036 }
4037 }
4038 else
4039 {
4040 free(tempStr);
4041 return (ERROR);
4042 }
4043 }
4044 free(tempStr);
4045 return (NO_ERROR);
4046 }
4047
4048
DoSs(void)4049 int DoSs (void)
4050 {
4051 int ret, oldBurnin;
4052
4053 if (chainParams.numGen/chainParams.sampleFreq <= chainParams.burninSS)
4054 {/*Do not change print out to generations vs samples because of danger of overflow*/
4055 MrBayesPrint ("%s ERROR: Burnin %d samples is too large compared with requested total %d samples (%d generations).\n", spacer ,chainParams.burninSS, chainParams.numGen/chainParams.sampleFreq, chainParams.numGen);
4056 return ERROR;
4057 }
4058
4059 oldBurnin = chainParams.burninSS;;
4060 stepRelativeBurninSS = chainParams.relativeBurnin;
4061
4062 chainParams.relativeBurnin = YES;
4063
4064 if (chainParams.burninSS < 0)
4065 chainParams.burninSS = chainParams.numGen / ((chainParams.numStepsSS-chainParams.burninSS)*chainParams.sampleFreq);
4066 chainParams.isSS = YES;
4067
4068 ret=DoMcmc();
4069
4070 chainParams.isSS = NO;
4071 chainParams.burninSS = oldBurnin;
4072 chainParams.relativeBurnin = stepRelativeBurninSS;
4073
4074 return ret;
4075 }
4076
4077
DoSsp(void)4078 int DoSsp (void)
4079 {
4080 return NO_ERROR;
4081 }
4082
4083
ExhaustiveParsimonySearch(Tree * t,int chain,TreeInfo * tInfo)4084 int ExhaustiveParsimonySearch (Tree *t, int chain, TreeInfo *tInfo)
4085 {
4086 int i, j, k;
4087 TreeNode *p;
4088
4089 for (i=j=k=0; i<t->nNodes; i++)
4090 {
4091 p = t->allDownPass[i];
4092 if (p->left == NULL || p->right == NULL)
4093 tInfo->leaf[j++] = p;
4094 else
4095 tInfo->vertex[k++] = p;
4096 }
4097
4098 tInfo->leaf[0]->anc = tInfo->leaf[1]->anc = tInfo->vertex[0];
4099 tInfo->vertex[0]->left = tInfo->leaf[0];
4100 tInfo->vertex[0]->right = tInfo->leaf[1];
4101 tInfo->leaf[t->nIntNodes+1]->left = tInfo->vertex[0];
4102 tInfo->vertex[0]->anc = tInfo->leaf[t->nIntNodes+1];
4103
4104 BuildExhaustiveSearchTree (t, chain, 2, tInfo);
4105
4106 return (NO_ERROR);
4107 }
4108
4109
ExtendChainQuery()4110 int ExtendChainQuery ()
4111 {
4112 int extendChain, additionalCycles;
4113 char s[100];
4114
4115 # if defined (MPI_ENABLED)
4116 if (proc_id == 0)
4117 {
4118 MrBayesPrint ("\n");
4119 extendChain = WantTo ("Continue with analysis");
4120 }
4121 MPI_Bcast (&extendChain, 1, MPI_INT, 0, MPI_COMM_WORLD);
4122 if (extendChain == YES)
4123 {
4124 if (proc_id == 0)
4125 {
4126 additionalCycles = 0;
4127 do
4128 {
4129 if (additionalCycles < 0)
4130 MrBayesPrint ("%s Number must be greater than or equal to 0: ", spacer);
4131 else
4132 MrBayesPrint ("%s Additional number of generations: ", spacer);
4133
4134 if (fgets (s, 100, stdin) == NULL)
4135 {
4136 printf ("Error in function: %s at line: %d in file: %s", __func__, __LINE__, __FILE__);
4137 }
4138 sscanf (s, "%d", &additionalCycles);
4139
4140 } while (additionalCycles < 0);
4141 MrBayesPrint ("\n");
4142 }
4143 MPI_Bcast (&additionalCycles, 1, MPI_INT, 0, MPI_COMM_WORLD);
4144
4145 return (additionalCycles);
4146 }
4147 else
4148 return (0);
4149 # else
4150 MrBayesPrint ("\n");
4151 extendChain = WantTo ("Continue with analysis");
4152
4153 if (extendChain == YES)
4154 {
4155 additionalCycles = 0;
4156 do
4157 {
4158 if (additionalCycles < 0)
4159 MrBayesPrint ("%s Number must be greater than or equal to 0: ", spacer);
4160 else
4161 MrBayesPrint ("%s Additional number of generations: ", spacer);
4162
4163 if (fgets (s, 20, stdin) == NULL)
4164 {
4165 printf ("Error in function: %s at line: %d in file: %s", __func__, __LINE__, __FILE__);
4166 }
4167 sscanf (s, "%d", &additionalCycles);
4168
4169 } while (additionalCycles < 0);
4170 MrBayesPrint ("\n");
4171 return (additionalCycles);
4172 }
4173 else
4174 return (0);
4175 # endif
4176
4177 }
4178
4179
FillNumSitesOfPat(void)4180 int FillNumSitesOfPat (void)
4181 {
4182 int i, j, n, *increased, *decreased, nToDecrease, nToIncrease, whichToChange;
4183 MrBFlt ran, sum;
4184 CLFlt wtIncrement;
4185
4186 wtIncrement = (CLFlt) chainParams.weightScheme[2];
4187 increased = decreased = NULL;
4188
4189 /* reallocate numSitesOfPat */
4190 if (memAllocs[ALLOC_NUMSITESOFPAT] == NO)
4191 {
4192 MrBayesPrint ("%s numSitesOfPat is not allocated\n", spacer);
4193 goto errorExit;
4194 }
4195 memAllocs[ALLOC_NUMSITESOFPAT] = NO;
4196 numSitesOfPat = (CLFlt *) SafeRealloc((void *) numSitesOfPat, numCompressedChars * chainParams.numChains * sizeof(MrBFlt));
4197 if (!numSitesOfPat)
4198 {
4199 MrBayesPrint ("%s Problem reallocating numSitesOfPat (%d)\n", spacer, numCompressedChars * chainParams.numChains * sizeof(MrBFlt));
4200 goto errorExit;
4201 }
4202 memAllocs[ALLOC_NUMSITESOFPAT] = YES;
4203
4204 /* copy first numCompressedChars into the remaining bits */
4205 if (chainParams.numChains > 1)
4206 {
4207 for (i=0; i<numCompressedChars; i++)
4208 {
4209 for (j=1; j<chainParams.numChains; j++)
4210 {
4211 numSitesOfPat[j * numCompressedChars + i] = numSitesOfPat[i];
4212 }
4213 }
4214 }
4215
4216 /* reweight characters for each chain */
4217 if (chainParams.numChains > 1)
4218 {
4219 if (chainParams.weightScheme[0] + chainParams.weightScheme[1] > 0.0001)
4220 MrBayesPrint ("%s Reweighting of characters for chains 1 to %d\n", spacer, chainParams.numChains);
4221
4222 /* check that we don't have an HMM */
4223 if (chainHasAdgamma == YES && chainParams.weightScheme[0] + chainParams.weightScheme[1] > 0.0001)
4224 {
4225 MrBayesPrint ("%s Reweighting of characters is not allowed with an autocorrelated gamma model\n", spacer);
4226 goto errorExit;
4227 }
4228
4229 /* how many characters */
4230 n = 0;
4231 for (i=0; i<numCompressedChars; i++)
4232 n += (int)numSitesOfPat[0 * numCompressedChars + i];
4233 nToDecrease = (int)(n * chainParams.weightScheme[0] / 100.0);
4234 nToIncrease = (int)(n * chainParams.weightScheme[1] / 100.0);
4235 if (chainParams.weightScheme[0] + chainParams.weightScheme[1] > 0.0001)
4236 {
4237 MrBayesPrint ("%s Decreasing weight of %d characters\n", spacer, nToDecrease);
4238 MrBayesPrint ("%s Increasing weight of %d characters\n", spacer, nToIncrease);
4239 }
4240
4241 /* allocate memory */
4242 increased = (int *)SafeMalloc(2 * (size_t)numCompressedChars * sizeof(int));
4243 if (!increased)
4244 {
4245 MrBayesPrint ("%s Problem reallocating increased (%d)\n", spacer, numCompressedChars * chainParams.numChains * sizeof(int));
4246 goto errorExit;
4247 }
4248 decreased = increased + numCompressedChars;
4249
4250 /* reweight characters for each chain */
4251 for (j=1; j<chainParams.numChains; j++)
4252 {
4253 for (i=0; i<numCompressedChars; i++)
4254 increased[i] = decreased[i] = 0;
4255
4256 /* decrease weight of characters */
4257 for (i=0; i<nToDecrease; i++)
4258 {
4259 do
4260 {
4261 ran = RandomNumber(&swapSeed);
4262 sum = 0.0;
4263 for (whichToChange=0; whichToChange<numCompressedChars; whichToChange++)
4264 {
4265 sum += numSitesOfPat[0 * numCompressedChars + whichToChange] / n;
4266 if (ran < sum)
4267 break;
4268 }
4269 if (whichToChange < 0 || whichToChange >= numCompressedChars)
4270 continue;
4271 } while (decreased[whichToChange] >= numSitesOfPat[0 * numCompressedChars + whichToChange]);
4272 decreased[whichToChange]++;
4273 numSitesOfPat[j * numCompressedChars + whichToChange] -= wtIncrement;
4274 if (numSitesOfPat[j * numCompressedChars + whichToChange] < 0)
4275 {
4276 MrBayesPrint ("%s Problem reweighting characters\n", spacer);
4277 goto errorExit;
4278 }
4279 }
4280
4281 /* increase weight of characters */
4282 for (i=0; i<nToDecrease; i++)
4283 {
4284 do
4285 {
4286 ran = RandomNumber(&swapSeed);
4287 sum = 0.0;
4288 for (whichToChange=0; whichToChange<numCompressedChars; whichToChange++)
4289 {
4290 sum += numSitesOfPat[0 * numCompressedChars + whichToChange] / n;
4291 if (ran < sum)
4292 break;
4293 }
4294 if (whichToChange < 0 || whichToChange >= numCompressedChars)
4295 continue;
4296 } while ((increased[whichToChange] + decreased[whichToChange]) >= numSitesOfPat[0 * numCompressedChars + whichToChange]);
4297 increased[whichToChange]++;
4298 numSitesOfPat[j * numCompressedChars + whichToChange] += wtIncrement;
4299 if (numSitesOfPat[j * numCompressedChars + whichToChange] < 0)
4300 {
4301 MrBayesPrint ("%s Problem reweighting characters\n", spacer);
4302 goto errorExit;
4303 }
4304 }
4305
4306 }
4307
4308 /* free allocated memory */
4309 free (increased);
4310 }
4311
4312 # if 0
4313 /* print site patterns for each chain */
4314 for (i=0; i<numCompressedChars; i++)
4315 {
4316 MrBayesPrint ("%4d -- ", i);
4317 for (j=0; j<chainParams.numChains; j++)
4318 {
4319 MrBayesPrint ("%4.1lf ", numSitesOfPat[j * numCompressedChars + i]);
4320 }
4321 MrBayesPrint ("\n");
4322 }
4323 # endif
4324
4325 return (NO_ERROR);
4326
4327 errorExit:
4328 if (increased)
4329 free (increased);
4330 return (ERROR);
4331 }
4332
4333
4334 /* FindBestNode: Recursive function for finding best attachment point */
FindBestNode(Tree * t,TreeNode * p,TreeNode * addNode,CLFlt * minLength,int chain)4335 TreeNode *FindBestNode (Tree *t, TreeNode *p, TreeNode *addNode, CLFlt *minLength, int chain) {
4336
4337 int c, n, division;
4338 TreeNode *q=NULL, *r=NULL;
4339 BitsLong *pA, *pP, *pX;
4340 CLFlt *nSitesOfPat, fpLength, length;
4341 ModelInfo *m;
4342
4343 /* Calculate length, looping over divisions */
4344 fpLength = 0;
4345 for (n=0; n<t->nRelParts; n++)
4346 {
4347 division = t->relParts[n];
4348
4349 /* Find model settings */
4350 m = &modelSettings[division];
4351
4352 /* Find number of site patterns */
4353 nSitesOfPat = numSitesOfPat + ((1 % chainParams.numChains) * numCompressedChars) + m->compCharStart;
4354
4355 /* Find final-pass parsimony sets for the node and its ancestor */
4356 pP = m->parsSets[p->index ];
4357 pA = m->parsSets[p->anc->index ];
4358 pX = m->parsSets[addNode->index];
4359
4360 for (c=0; c<m->numChars; c++)
4361 {
4362 if (((pP[c] | pA[c])&pX[c]) == 0)
4363 fpLength += nSitesOfPat[c];
4364 }
4365 }
4366
4367 /* If tip, this is the best node and its length is the min length */
4368 if (p->left == NULL)
4369 {
4370 *minLength = fpLength;
4371 return p;
4372 }
4373
4374 /* Find best node and min length above this node */
4375 if (p->left != NULL) {
4376 q = FindBestNode(t, p->left, addNode, minLength, chain);
4377 r = FindBestNode(t, p->right, addNode, &length, chain);
4378 if (length < *minLength) {
4379 *minLength = length;
4380 q = r;
4381 }
4382 }
4383
4384 /* Return best node and min length */
4385 if (*minLength < fpLength)
4386 return q;
4387 else /* This node is the best */
4388 {
4389 *minLength = fpLength;
4390 return p;
4391 }
4392 }
4393
4394
4395 /* FlipCijkSpace: Flip space for cijks with scratch area */
FlipCijkSpace(ModelInfo * m,int chain)4396 void FlipCijkSpace (ModelInfo* m, int chain)
4397 {
4398 int temp;
4399
4400 temp = m->cijkIndex[chain];
4401 m->cijkIndex[chain] = m->cijkScratchIndex;
4402 m->cijkScratchIndex = temp;
4403 }
4404
4405
4406 /* FlipCondLikeSpace: Flip space for conditional likelihoods with scratch area */
FlipCondLikeSpace(ModelInfo * m,int chain,int nodeIndex)4407 void FlipCondLikeSpace (ModelInfo* m, int chain, int nodeIndex)
4408 {
4409 int temp;
4410
4411 temp = m->condLikeIndex[chain][nodeIndex];
4412 m->condLikeIndex[chain][nodeIndex] = m->condLikeScratchIndex[nodeIndex];
4413 m->condLikeScratchIndex[nodeIndex] = temp;
4414 }
4415
4416
4417 /* FlipNodeScalerSpace: Flip space for node scalers and scaler flag with scratch area */
FlipNodeScalerSpace(ModelInfo * m,int chain,int nodeIndex)4418 void FlipNodeScalerSpace (ModelInfo* m, int chain, int nodeIndex)
4419 {
4420 int temp;
4421
4422 temp = m->nodeScalerIndex[chain][nodeIndex];
4423 m->nodeScalerIndex[chain][nodeIndex] = m->nodeScalerScratchIndex[nodeIndex];
4424 m->nodeScalerScratchIndex[nodeIndex] = temp;
4425
4426 temp = m->unscaledNodes[chain][nodeIndex];
4427 m->unscaledNodes[chain][nodeIndex] = m->unscaledNodesScratch[nodeIndex];
4428 m->unscaledNodesScratch[nodeIndex] = temp;
4429 }
4430
4431
4432 /* FlipSiteScalerSpace: Flip space for ln site scalers */
FlipSiteScalerSpace(ModelInfo * m,int chain)4433 void FlipSiteScalerSpace (ModelInfo *m, int chain)
4434 {
4435 int temp;
4436
4437 # if defined (BEAGLE_ENABLED)
4438 int *tempp;
4439 # endif
4440
4441 temp = m->siteScalerIndex[chain];
4442 m->siteScalerIndex[chain] = m->siteScalerScratchIndex;
4443 m->siteScalerScratchIndex = temp;
4444
4445 # if defined (BEAGLE_ENABLED)
4446 if (m->useBeagle == YES)
4447 {
4448 tempp = m->isScalerNode[chain];
4449 m->isScalerNode[chain] = m->isScalerNodeScratch ;
4450 m->isScalerNodeScratch = tempp;
4451 }
4452 # endif
4453 }
4454
4455
4456 /* FlipTiProbsSpace: Flip space for ti probs with scratch area */
FlipTiProbsSpace(ModelInfo * m,int chain,int nodeIndex)4457 void FlipTiProbsSpace (ModelInfo* m, int chain, int nodeIndex)
4458 {
4459 int temp;
4460
4461 temp = m->tiProbsIndex[chain][nodeIndex];
4462 m->tiProbsIndex[chain][nodeIndex] = m->tiProbsScratchIndex[nodeIndex];
4463 m->tiProbsScratchIndex[nodeIndex] = temp;
4464 }
4465
4466
FreeChainMemory(void)4467 void FreeChainMemory (void)
4468 {
4469 int i, j, k, nRates;
4470 ModelInfo *m;
4471
4472 /* free model variables for Gibbs gamma */
4473 for (i=0; i<numCurrentDivisions; i++)
4474 {
4475 if (modelSettings[i].gibbsGamma == YES)
4476 {
4477 if (modelSettings[i].pInvar != NULL)
4478 nRates = modelSettings[i].numRateCats + 1;
4479 else
4480 nRates = modelSettings[i].numRateCats;
4481 for (j=0; j<numLocalChains; j++)
4482 {
4483 for (k=0; k<nRates; k++)
4484 {
4485 free(modelSettings[i].catLnScaler[j][k]);
4486 free(modelSettings[i].catLike[j][k]);
4487 }
4488 free (modelSettings[i].catLnScaler[j]);
4489 free (modelSettings[i].catLike[j]);
4490 }
4491 free (modelSettings[i].tiIndex);
4492 free (modelSettings[i].catLike);
4493 free (modelSettings[i].catLnScaler);
4494 }
4495 }
4496
4497 /* free parsimony sets and node lens */
4498 for (i=0; i<numCurrentDivisions; i++)
4499 {
4500 m = &modelSettings[i];
4501 if (m->parsSets)
4502 {
4503 for (j=0; j<m->numParsSets; j++)
4504 free (m->parsSets[j]);
4505 free (m->parsSets);
4506 m->parsSets = NULL;
4507 }
4508 if (m->parsNodeLens)
4509 {
4510 free(m->parsNodeLens);
4511 m->parsNodeLens = NULL;
4512 }
4513 }
4514
4515 /* free model variables for conditional likelihoods */
4516 for (i=0; i<numCurrentDivisions; i++)
4517 {
4518 m = &modelSettings[i];
4519 if (m->condLikes)
4520 {
4521 for (j=0; j<m->numCondLikes; j++)
4522 {
4523 # if defined (SSE_ENABLED)
4524 if (m->useVec != NO)
4525 ALIGNEDSAFEFREE (m->condLikes[j]);
4526 else
4527 free (m->condLikes[j]);
4528 # else
4529 free (m->condLikes[j]);
4530 # endif
4531 }
4532 free (m->condLikes);
4533 m->condLikes = NULL;
4534 }
4535
4536 if (m->scalers)
4537 {
4538 for (j=0; j<m->numScalers; j++)
4539 # if defined (SSE_ENABLED)
4540 if (m->useVec != VEC_NONE)
4541 ALIGNEDSAFEFREE (m->scalers[j]);
4542 else
4543 free (m->scalers[j]);
4544 # else
4545 free (m->scalers[j]);
4546
4547 # endif
4548 free (m->scalers);
4549 m->scalers = NULL;
4550 }
4551
4552 if (m->clP)
4553 {
4554 free (m->clP);
4555 m->clP = NULL;
4556 }
4557 # if defined (SSE_ENABLED)
4558 if (m->useVec != VEC_NONE)
4559 {
4560 if (m->clP_SSE)
4561 {
4562 free (m->clP_SSE);
4563 m->clP_SSE = NULL;
4564 }
4565 #if defined (AVX_ENABLED)
4566 if (m->clP_AVX)
4567 {
4568 free (m->clP_AVX);
4569 m->clP_AVX = NULL;
4570 }
4571 #endif
4572 if (m->lnL_Vec)
4573 ALIGNEDSAFEFREE (m->lnL_Vec);
4574 if (m->lnLI_Vec)
4575 ALIGNEDSAFEFREE (m->lnLI_Vec);
4576 }
4577 # endif
4578
4579 if (m->tiProbs)
4580 {
4581 for (j=0; j<m->numTiProbs; j++)
4582 free (m->tiProbs[j]);
4583 free (m->tiProbs);
4584 m->tiProbs = NULL;
4585 }
4586
4587 if (m->cijks)
4588 {
4589 for (j=0; j<numLocalChains+1; j++)
4590 free (m->cijks[j]);
4591 free (m->cijks);
4592 m->cijks = NULL;
4593 }
4594
4595 if (m->condLikeIndex)
4596 {
4597 for (j=0; j<numLocalChains; j++)
4598 free (m->condLikeIndex[j]);
4599 free (m->condLikeIndex);
4600 m->condLikeIndex = NULL;
4601 }
4602
4603 if (m->condLikeScratchIndex)
4604 {
4605 free (m->condLikeScratchIndex);
4606 m->condLikeScratchIndex=NULL;
4607 }
4608
4609 if (m->tiProbsIndex)
4610 {
4611 for (j=0; j<numLocalChains; j++)
4612 free (m->tiProbsIndex[j]);
4613 free (m->tiProbsIndex);
4614 m->tiProbsIndex = NULL;
4615 }
4616 if (m->tiProbsScratchIndex)
4617 {
4618 free (m->tiProbsScratchIndex);
4619 m->tiProbsScratchIndex = NULL;
4620 }
4621 if (m->nodeScalerIndex)
4622 {
4623 for (j=0; j<numLocalChains; j++)
4624 free (m->nodeScalerIndex[j]);
4625 free (m->nodeScalerIndex);
4626 m->nodeScalerIndex = NULL;
4627 }
4628 if (m->nodeScalerScratchIndex)
4629 {
4630 free (m->nodeScalerScratchIndex);
4631 m->nodeScalerScratchIndex = NULL;
4632 }
4633 if (m->unscaledNodes)
4634 {
4635 for (j=0; j<numLocalChains; j++)
4636 free (m->unscaledNodes[j]);
4637 free (m->unscaledNodes);
4638 m->unscaledNodes = NULL;
4639 }
4640 if (m->unscaledNodesScratch)
4641 {
4642 free (m->unscaledNodesScratch);
4643 m->unscaledNodesScratch = NULL;
4644 }
4645 if (m->siteScalerIndex)
4646 {
4647 free (m->siteScalerIndex);
4648 m->siteScalerIndex = NULL;
4649 }
4650 if (m->cijkIndex)
4651 {
4652 free (m->cijkIndex);
4653 m->cijkIndex = NULL;
4654 }
4655 if (m->ancStateCondLikes)
4656 {
4657 free (m->ancStateCondLikes);
4658 m->ancStateCondLikes = NULL;
4659 }
4660
4661 # if defined (BEAGLE_ENABLED)
4662 if (m->useBeagle == NO)
4663 continue;
4664
4665 if (!(m->useBeagleMultiPartitions == YES && m->divisionIndex > 0)) {
4666 beagleFinalizeInstance(m->beagleInstance);
4667 m->beagleInstance = -99; // TODO: allow consecutive 'mcmc' runs using BEAGLE
4668 }
4669 SAFEFREE(m->logLikelihoods);
4670 SAFEFREE(m->inRates);
4671 SAFEFREE(m->branchLengths);
4672 SAFEFREE(m->tiProbIndices);
4673 SAFEFREE(m->inWeights);
4674 SAFEFREE(m->bufferIndices);
4675 SAFEFREE(m->eigenIndices);
4676 SAFEFREE(m->childBufferIndices);
4677 SAFEFREE(m->childTiProbIndices);
4678 SAFEFREE(m->cumulativeScaleIndices);
4679
4680 m->isScalerNodeScratch += numLocalTaxa;
4681 SAFEFREE(m->isScalerNodeScratch);
4682 for (j=0; j<numLocalChains; j++)
4683 {
4684 m->isScalerNode[j] += numLocalTaxa;
4685 SAFEFREE(m->isScalerNode[j]);
4686 }
4687 SAFEFREE(m->isScalerNode);
4688
4689 SAFEFREE(m->beagleComputeCount);
4690 SAFEFREE(m->successCount);
4691 SAFEFREE(m->rescaleFreq);
4692 SAFEFREE(m->operations);
4693 SAFEFREE(m->scaleFactorsOps);
4694 # if defined (BEAGLE_V3_ENABLED)
4695 if (m->useBeagleMultiPartitions == YES)
4696 {
4697 if (m->divisionIndex == 0)
4698 {
4699 SAFEFREE(m->logLikelihoodsAll);
4700 SAFEFREE(m->cijkIndicesAll);
4701 SAFEFREE(m->categoryRateIndicesAll);
4702 SAFEFREE(m->operationsAll);
4703 }
4704 SAFEFREE(m->operationsByPartition);
4705 }
4706 # endif /* BEAGLE_V3_ENABLED */
4707 # endif /* BEAGLE_ENABLED */
4708 }
4709
4710 if (memAllocs[ALLOC_CURLNL] == YES) /*alloc in RunChain()*/
4711 {
4712 free (maxLnL0);
4713 free (curLnL);
4714 memAllocs[ALLOC_CURLNL] = NO;
4715 }
4716 if (memAllocs[ALLOC_SS] == YES) /*alloc in mcmc()*/
4717 {
4718 free (marginalLnLSS);
4719 free (stepScalerSS);
4720 free (stepAcumulatorSS);
4721 free (splitfreqSS);
4722 memAllocs[ALLOC_SS] = NO;
4723 }
4724 if (memAllocs[ALLOC_CURLNPR] == YES) /*alloc in RunChain()*/
4725 {
4726 free (curLnPr);
4727 memAllocs[ALLOC_CURLNPR] = NO;
4728 }
4729 if (memAllocs[ALLOC_CHAINID] == YES) /*alloc in RunChain()*/
4730 {
4731 free (chainId);
4732 memAllocs[ALLOC_CHAINID] = NO;
4733 }
4734 if (memAllocs[ALLOC_USEDMOVES] == YES) /*alloc in setUsedMoves()*/
4735 {
4736 free (usedMoves);
4737 memAllocs[ALLOC_USEDMOVES] = NO;
4738 }
4739 if (memAllocs[ALLOC_TERMSTATE] == YES) /*alloc in SetUpTermState()*/
4740 {
4741 free (termState);
4742 termState = NULL;
4743 memAllocs[ALLOC_TERMSTATE] = NO;
4744 }
4745 if (memAllocs[ALLOC_ISPARTAMBIG] == YES) /*alloc in SetUpTermState()*/
4746 {
4747 free (isPartAmbig);
4748 isPartAmbig = NULL;
4749 memAllocs[ALLOC_ISPARTAMBIG] = NO;
4750 }
4751 if (memAllocs[ALLOC_PRELIKES] == YES) /*alloc in InitCondLike()*/
4752 {
4753 free (preLikeL);
4754 preLikeL = NULL;
4755 memAllocs[ALLOC_PRELIKES] = NO;
4756 }
4757 if (memAllocs[ALLOC_RATEPROBS] == YES) /*alloc in InitAdGamma() */
4758 {
4759 free (rateProbSpace);
4760 free (rateProbs);
4761 rateProbs = NULL;
4762 memAllocs[ALLOC_RATEPROBS] = NO;
4763 }
4764 if (memAllocs[ALLOC_SITEJUMP] == YES) /*alloc in InitAdGamma() */
4765 {
4766 free (siteJump);
4767 siteJump = NULL;
4768 memAllocs[ALLOC_SITEJUMP] = NO;
4769 }
4770 if (memAllocs[ALLOC_MARKOVTIS] == YES) /*alloc in InitAdGamma() */
4771 {
4772 for (i=0; i<MAX_SMALL_JUMP; i++)
4773 if (markovTi[i] != NULL)
4774 FreeSquareDoubleMatrix(markovTi[i]);
4775 FreeSquareDoubleMatrix(markovTiN);
4776 memAllocs[ALLOC_MARKOVTIS] = NO;
4777 }
4778 if (memAllocs[ALLOC_SWAPINFO] == YES) /*alloc in RunChain()*/
4779 {
4780 for (i=0; i<chainParams.numRuns; i++)
4781 FreeSquareIntegerMatrix(swapInfo[i]);
4782 free (swapInfo);
4783 memAllocs[ALLOC_SWAPINFO] = NO;
4784 }
4785 if (memAllocs[ALLOC_POSSELPROBS] == YES) /*alloc in PrintStates() <- RunChain()*/
4786 {
4787 free (posSelProbs);
4788 memAllocs[ALLOC_POSSELPROBS] = NO;
4789 }
4790 if (memAllocs[ALLOC_PFCOUNTERS] == YES) /*alloc in SetUpParitionCounters() <- RunChain()*/
4791 {
4792 free (partition[0]);
4793 free (partition);
4794 for (i=0; i<numTopologies; i++)
4795 Tfree (partFreqTreeRoot[i]);
4796 free (partFreqTreeRoot);
4797 memAllocs[ALLOC_PFCOUNTERS] = NO;
4798 }
4799 if (memAllocs[ALLOC_FILEPOINTERS] == YES) /* alloc in (PreparePrintFiles(), ReusePreviousResults()) <- RunChain() */
4800 {
4801 CloseMBPrintFiles ();
4802 if (fpTree != NULL)
4803 {
4804 free (fpTree[0]);
4805 free (fpTree);
4806 }
4807 if (fpParm != NULL)
4808 free (fpParm);
4809 fpParm = NULL;
4810 fpTree = NULL;
4811 fpMcmc = NULL;
4812 fpSS = NULL;
4813 memAllocs[ALLOC_FILEPOINTERS] = NO;
4814 }
4815 if (memAllocs[ALLOC_STATS] == YES) /* alloc in RunChain() */
4816 {
4817 if (chainParams.allComps == YES)
4818 {
4819 for (i=0; i<numTopologies; i++)
4820 FreeSquareDoubleMatrix (chainParams.stat[i].pair);
4821 }
4822 free (chainParams.stat);
4823 memAllocs[ALLOC_STATS] = NO;
4824 }
4825 if (memAllocs[ALLOC_DIAGNTREE] == YES)
4826 {
4827 FreeTree (chainParams.dtree);
4828 memAllocs[ALLOC_DIAGNTREE] = NO;
4829 }
4830 if (memAllocs[ALLOC_PRINTPARAM] == YES)
4831 {
4832 free (printParam);
4833 free (topologyPrintIndex);
4834 memAllocs[ALLOC_PRINTPARAM] = NO;
4835 }
4836 if (memAllocs[ALLOC_TFILEPOS] == YES)
4837 {
4838 free (chainParams.tFilePos);
4839 chainParams.tFilePos = NULL;
4840 memAllocs[ALLOC_TFILEPOS] = NO;
4841 }
4842 if (memAllocs[ALLOC_TREELIST] == YES)
4843 {
4844 for (i=0; i<chainParams.numRuns * numTopologies; i++)
4845 EraseTreeList (&chainParams.treeList[i]);
4846 free (chainParams.treeList);
4847 chainParams.treeList = NULL;
4848 memAllocs[ALLOC_TREELIST] = NO;
4849 }
4850 if (memAllocs[ALLOC_BEST] == YES)
4851 {
4852 FreeBestChainVariables();
4853 memAllocs[ALLOC_BEST] = NO;
4854 }
4855 }
4856
4857
GetFitchPartials(ModelInfo * m,int chain,int source1,int source2,int destination)4858 MrBFlt GetFitchPartials (ModelInfo *m, int chain, int source1, int source2, int destination)
4859 {
4860 int c, i;
4861 BitsLong x[2], *pS1, *pS2, *pD;
4862 MrBFlt length = 0.0;
4863 CLFlt *nSitesOfPat;
4864
4865 assert (m->nParsIntsPerSite <= 2 && m->nParsIntsPerSite > 0);
4866 assert (source1 >= 0 && source1 < m->numParsSets);
4867 assert (source2 >= 0 && source2 < m->numParsSets);
4868 assert (destination >= 0 && destination < m->numParsSets);
4869
4870 /* find parsimony sets for the nodes */
4871 pS1 = m->parsSets[source1 ];
4872 pS2 = m->parsSets[source2 ];
4873 pD = m->parsSets[destination];
4874
4875 /* Find number of site patterns */
4876 nSitesOfPat = numSitesOfPat + ((1 % chainParams.numChains) * numCompressedChars) + m->compCharStart;//chainId[chain]
4877
4878 if (m->nParsIntsPerSite == 1)
4879 {
4880 for (c=0; c<m->numChars; c++)
4881 {
4882 x[0] = pS1[c] & pS2[c];
4883 if (x[0] == 0)
4884 {
4885 length += nSitesOfPat[c];
4886 x[0] = pS1[c] | pS2[c];
4887 }
4888 pD[c] = x[0];
4889 }
4890 }
4891 else /* if (m->nParsIntsPerSite == 2) */
4892 {
4893 assert (m->nParsIntsPerSite == 2);
4894 for (c=i=0; c<m->numChars; c++)
4895 {
4896 x[0] = pS1[i] & pS2[i];
4897 x[1] = pS1[i+1] & pS2[i+1];
4898 if ((x[0] | x[1]) == 0)
4899 {
4900 length += nSitesOfPat[c];
4901 x[0] = pS1[i] | pS2[i];
4902 x[1] = pS1[i+1] | pS2[i+1];
4903 }
4904 pD[i++] = x[0];
4905 pD[i++] = x[1];
4906 }
4907 }
4908
4909 return length;
4910 }
4911
4912
GetParsDP(Tree * t,TreeNode * p,int chain)4913 MrBFlt GetParsDP (Tree *t, TreeNode *p, int chain)
4914 {
4915 int n, division;
4916 MrBFlt length;
4917 ModelInfo *m;
4918
4919 length = 0.0;
4920 if (p->left != NULL)
4921 {
4922 length += GetParsDP (t, p->left, chain);
4923 length += GetParsDP (t, p->right, chain);
4924
4925 for (n=0; n<t->nRelParts; n++)
4926 {
4927 division = t->relParts[n];
4928
4929 /* Find model settings */
4930 m = &modelSettings[division];
4931
4932 /* Get Fitch partials and length */
4933 length += GetFitchPartials(m,
4934 chain,
4935 p->left->index,
4936 p->right->index,
4937 p->index);
4938 }
4939 }
4940
4941 return length;
4942 }
4943
4944
GetParsFP(Tree * t,TreeNode * p,int chain)4945 void GetParsFP (Tree *t, TreeNode *p, int chain)
4946 {
4947 int i, c, n, division;
4948 BitsLong *pL, *pR, *pP, *pA, x[2];
4949 ModelInfo *m;
4950
4951 if (p->left != NULL)
4952 {
4953 for (n=0; n<t->nRelParts; n++)
4954 {
4955 division = t->relParts[n];
4956
4957 /* Find model settings */
4958 m = &modelSettings[division];
4959 assert (m->nParsIntsPerSite == 1 || m->nParsIntsPerSite == 2);
4960
4961 /* find parsimony sets for the node and its environment */
4962 pL = m->parsSets[p->left->index ];
4963 pR = m->parsSets[p->right->index];
4964 pP = m->parsSets[p->index ];
4965 pA = m->parsSets[p->anc->index ];
4966
4967 if (m->nParsIntsPerSite == 1)
4968 {
4969 for (c=0; c<m->numChars; c++)
4970 {
4971 x[0] = pP[c] & pA[c];
4972
4973 if (x[0] != pA[c])
4974 { /* means that we allow change of state from p to a */
4975 if ((pL[c] & pR[c]) != 0)
4976 x[0] = ((pL[c] | pR[c]) & pA[c]) | pP[c] ;
4977 /* We still allow only one change from both children of p through p to a.
4978 So state from a that belong to one of the children of p can be added to pP[c],
4979 if p assume the state then the only change would be on the other child. */
4980 else
4981 x[0] = pP[c] | pA[c];
4982 /* Here we allow two change from both children of p through p to a.
4983 Adding pA[c] to pP[c] means that if p assume state exclusive in a then even if
4984 both children will be in different state from p we still get optimal parsimony */
4985 }
4986 pP[c] = x[0];
4987 }
4988 }
4989 else /* if (m->nParsIntsPerSite == 2) */
4990 {
4991 for (c=i=0; c<m->numChars; c++)
4992 {
4993 x[0] = pP[i] & pA[i];
4994 x[1] = pP[i+1] & pA[i+1];
4995 if (x[0] != pA[i] || x[1] != pA[i+1])
4996 {
4997 x[0] = pL[i] & pR[i];
4998 x[1] = pL[i+1] & pR[i+1];
4999 if ((x[0] | x[1]) != 0)
5000 {
5001 x[0] = ((pL[i] | pR[i]) & pA[i]) | pP[i] ;
5002 x[1] = ((pL[i+1] | pR[i+1]) & pA[i+1]) | pP[i+1] ;
5003 }
5004 else
5005 {
5006 x[0] = pP[i] | pA[i];
5007 x[1] = pP[i+1] | pA[i+1];
5008 }
5009 }
5010 pP[i++] = x[0];
5011 pP[i++] = x[1];
5012 }
5013 }
5014 }
5015 GetParsFP (t, p->left, chain);
5016 GetParsFP (t, p->right, chain);
5017 }
5018 }
5019
5020
GetParsimonyBrlens(Tree * t,int chain,MrBFlt * brlens)5021 int GetParsimonyBrlens (Tree *t, int chain, MrBFlt *brlens)
5022 {
5023 int c, i, j, n, division;
5024 BitsLong *pP, *pA;
5025 CLFlt *nSitesOfPat;
5026 TreeNode *p;
5027 ModelInfo *m;
5028
5029 /* Reset all brlens */
5030 for (i=0; i<t->nNodes-1; i++)
5031 brlens[i] = 0.0;
5032
5033 /* Get final parsimony state sets */
5034 GetParsDP(t, t->root->left, chain);
5035 GetParsFP(t, t->root, chain);
5036
5037 /* Get all branch lengths, looping over divisions */
5038 for (n=0; n<t->nRelParts; n++)
5039 {
5040 division = t->relParts[n];
5041
5042 /* Find model settings */
5043 m = &modelSettings[division];
5044
5045 /* Find number of site patterns */
5046 nSitesOfPat = numSitesOfPat + ((chainId[chain] % chainParams.numChains) * numCompressedChars) + m->compCharStart;
5047
5048 /* Record branch lengths in downpass */
5049 for (i=0; i<t->nNodes-1; i++)
5050 {
5051 p = t->allDownPass[i];
5052
5053 /* Find final-pass parsimony sets for the node and its ancestor */
5054 pP = m->parsSets[p->index ];
5055 pA = m->parsSets[p->anc->index];
5056
5057 if (m->nParsIntsPerSite == 1)
5058 {
5059 for (c=0; c<m->numChars; c++)
5060 {
5061 if ((pP[c] & pA[c]) == 0)
5062 brlens[i] += nSitesOfPat[c];
5063 }
5064 }
5065 else
5066 {
5067 for (c=j=0; c<m->numChars; c++, j+=2)
5068 {
5069 if ((pP[j] & pA[j]) == 0 && (pP[j+1] & pA[j+1]) == 0)
5070 brlens[i] += nSitesOfPat[c];
5071 }
5072 }
5073 }
5074 }
5075
5076 return (NO_ERROR);
5077 }
5078
5079
GetParsimonyLength(Tree * t,int chain)5080 MrBFlt GetParsimonyLength (Tree *t, int chain)
5081 {
5082 int c, i, n, division;
5083 BitsLong *pP, *pA;
5084 CLFlt *nSitesOfPat;
5085 MrBFlt length;
5086 TreeNode *p;
5087 ModelInfo *m;
5088
5089 /* Get length down to internal root */
5090 length = GetParsDP(t, t->root->left, chain);
5091
5092 /* return if rooted */
5093 if (t->isRooted == NO)
5094 return length;
5095
5096 /* add in the last terminal if not rooted */
5097 for (n=0; n<t->nRelParts; n++)
5098 {
5099 division = t->relParts[n];
5100
5101 /* Find model settings */
5102 m = &modelSettings[division];
5103
5104 /* Find number of site patterns */
5105 nSitesOfPat = numSitesOfPat + ((chainId[chain] % chainParams.numChains) * numCompressedChars) + m->compCharStart;
5106
5107 /* Deal with last branch */
5108 p = t->intDownPass[t->nIntNodes-1];
5109
5110 /* find downpass parsimony sets for the node and its environment */
5111 pP = m->parsSets[p->index ];
5112 pA = m->parsSets[p->anc->index];
5113
5114 if (m->nParsIntsPerSite == 1)
5115 {
5116 for (c=0; c<m->numChars; c++)
5117 {
5118 if ((pP[c] & pA[c]) == 0)
5119 {
5120 length += nSitesOfPat[c];
5121 }
5122 }
5123 }
5124 else /* if (m->nParsIntsPerSite == 2) */
5125 {
5126 for (c=i=0; c<m->numChars; c++, i+=2)
5127 {
5128 if ((pP[i] & pA[i]) == 0 && (pP[i+1] & pA[i+1]) == 0)
5129 {
5130 length += nSitesOfPat[c];
5131 }
5132 }
5133 }
5134 }
5135
5136 return length;
5137 }
5138
5139
GetParsimonySubtreeRootstate(Tree * t,TreeNode * root,int chain)5140 void GetParsimonySubtreeRootstate (Tree *t, TreeNode *root, int chain)
5141 {
5142 int c, i, n, division;
5143 BitsLong *pD, *pP, *pA, x[2];
5144 TreeNode *p;
5145 ModelInfo *m;
5146
5147 /* Loop over divisions */
5148 for (n=0; n<t->nRelParts; n++)
5149 {
5150 division = t->relParts[n];
5151
5152 /* Find model settings */
5153 m = &modelSettings[division];
5154 assert (m->nParsIntsPerSite == 1 || m->nParsIntsPerSite == 2);
5155
5156 for (i=0; i<t->nNodes; i++)
5157 {
5158 p = t->allDownPass[i];
5159 p->marked = NO;
5160 }
5161
5162 p = root;
5163 while (p->anc != NULL)
5164 {
5165 p->marked = YES;
5166 p = p->anc;
5167 }
5168
5169 /* Make uppass node by node */
5170 for (i=t->nIntNodes-1; i>=0; i--)
5171 {
5172 p = t->intDownPass[i];
5173
5174 /* continue if no work needs to be done */
5175 if (p->marked == NO)
5176 continue;
5177
5178 /* find downpass and uppass parsimony sets for the node and its environment */
5179 pP = m->parsSets[p->index ];
5180 if (p->left->marked == YES)
5181 pD = m->parsSets[p->right->index];
5182 else
5183 pD = m->parsSets[p->left->index ];
5184 pA = m->parsSets[p->anc->index ];
5185
5186 if (m->nParsIntsPerSite == 1)
5187 {
5188 for (c=0; c<m->numChars; c++)
5189 {
5190 x[0] = pD[c] & pA[c];
5191 if (x[0] == 0)
5192 {
5193 x[0] = (pD[c] | pA[c]);
5194 }
5195 pP[c] = x[0];
5196 }
5197 }
5198 else if (m->nParsIntsPerSite == 2)
5199 {
5200 for (c=i=0; c<m->numChars; c++, i+=2)
5201 {
5202 x[0] = pD[i ] & pA[i ];
5203 x[1] = pD[i+1] & pA[i+1];
5204 if (x[0] + x[1] == 0)
5205 {
5206 x[0] = (pD[i ] | pA[i ]);
5207 x[1] = (pD[i+1] | pA[i+1]);
5208 }
5209 pP[i ] = x[0];
5210 pP[i+1] = x[1];
5211 }
5212 }
5213 if (p == root)
5214 break;
5215 }
5216 }
5217
5218 return;
5219 }
5220
5221
5222 /* GetRate: retrieve the base rate for the division and chain in current state */
GetRate(int division,int chain)5223 MrBFlt GetRate (int division, int chain)
5224 {
5225 Param *p;
5226 MrBFlt *values, rate;
5227 int i;
5228
5229 rate = 0.0;
5230
5231 p = modelSettings[division].rateMult;
5232 values = GetParamVals (p, chain, state[chain]);
5233 if (p->nValues == 1)
5234 rate = values[0];
5235 else
5236 {
5237 for (i=0; i<p->nRelParts; i++)
5238 {
5239 if (p->relParts[i] == division)
5240 {
5241 rate = values[i];
5242 break;
5243 }
5244 }
5245 }
5246
5247 p = modelSettings[division].geneTreeRateMult;
5248 if (p != NULL)
5249 {
5250 values = GetParamVals (p, chain, state[chain]);
5251 for (i=0; i<p->nRelParts; i++)
5252 {
5253 if (p->relParts[i] == division)
5254 {
5255 rate *= values[i];
5256 break;
5257 }
5258 }
5259 }
5260
5261 return rate;
5262 }
5263
5264
GetStamp(void)5265 void GetStamp (void)
5266 {
5267 int i;
5268
5269 for (i=0; i<10; i++)
5270 stamp[i] = '0' + (int)(RandomNumber(&runIDSeed) * 10);
5271 stamp[10] = '\0';
5272
5273 MrBayesPrint ("%s MCMC stamp = %s\n", spacer, stamp);
5274 }
5275
5276
GetSwappers(int * swapA,int * swapB,int run)5277 void GetSwappers (int *swapA, int *swapB, int run)
5278 {
5279 int i;
5280
5281 /* this works for both the serial and parallel versions because the swapSeed is identical for all
5282 processors, ensuring they all get the same sequence of chainIds to swap */
5283 # if defined (MPI_ENABLED)
5284
5285 /* For now, we wonly allow random swaps in the MPI version. Other schemes require
5286 tagging of messages, or a dedicated server node doing message processing. */
5287 (*swapA) = (int) (RandomNumber(&swapSeed) * chainParams.numChains);
5288 (*swapB) = (int) (RandomNumber(&swapSeed) * (chainParams.numChains - 1));
5289 if ((*swapB) == (*swapA))
5290 (*swapB) = chainParams.numChains - 1;
5291
5292 # else
5293
5294 if (chainParams.swapAdjacentOnly == NO)
5295 {
5296 (*swapA) = (int) (RandomNumber(&swapSeed) * chainParams.numChains);
5297 (*swapB) = (int) (RandomNumber(&swapSeed) * (chainParams.numChains - 1));
5298 if ((*swapB) == (*swapA))
5299 (*swapB) = chainParams.numChains - 1;
5300 }
5301 else
5302 {
5303 (*swapA) = (int) (RandomNumber(&swapSeed) * (chainParams.numChains - 1));
5304 (*swapB) = (*swapA) + 1;
5305 }
5306 # endif
5307
5308 i = run * chainParams.numChains;
5309 (*swapA) += i;
5310 (*swapB) += i;
5311
5312 return;
5313 }
5314
5315
GetTempDownPassSeq(TreeNode * p,int * i,TreeNode ** dp)5316 void GetTempDownPassSeq (TreeNode *p, int *i, TreeNode **dp)
5317 {
5318 if (p != NULL)
5319 {
5320 GetTempDownPassSeq (p->left, i, dp);
5321 GetTempDownPassSeq (p->right, i, dp);
5322 dp[(*i)++] = p;
5323 }
5324 }
5325
5326
GibbsSampleGamma(int chain,int division,RandLong * seed)5327 MrBFlt GibbsSampleGamma (int chain, int division, RandLong *seed)
5328 {
5329 int c, i, k, *rateCat, nStates, nRateCats, nPosRateCats, id;
5330 CLFlt **catLike, **catLnScaler, *lnScaler, maxLnScaler,
5331 *clRoot, f, bs[64], *clInvar, pInvar, freq;
5332 MrBFlt ran, lnL, *bsVals, deltaLnL, temp;
5333 ModelInfo *m;
5334 Tree *t;
5335 TreeNode *p;
5336
5337 m = &modelSettings[division];
5338
5339 /* find base frequencies */
5340 bsVals = GetParamSubVals (m->stateFreq, chain, state[chain]);
5341 nStates = m->numModelStates;
5342 for (i=0; i<nStates; i++)
5343 bs[i] = (CLFlt) bsVals[i];
5344
5345 /* find tree scaler */
5346 lnScaler = m->scalers[m->siteScalerIndex[chain]];
5347
5348 /* find category like array and associated sccaler */
5349 catLike = m->catLike[chain];
5350 catLnScaler = m->catLnScaler[chain];
5351
5352 /* find rate category index */
5353 rateCat = m->tiIndex + chain*m->numChars;
5354
5355 /* find number of rate cats and gamma cats */
5356 nRateCats = nPosRateCats = m->numRateCats;
5357 if (m->pInvar != NULL)
5358 nRateCats++;
5359
5360 /* find tree */
5361 t = GetTree (m->brlens, chain, state[chain]);
5362
5363 /* find invar cond likes (if we have invariable rate category) */
5364 clInvar = m->invCondLikes;
5365
5366 /* get pInvar */
5367 if (m->pInvar == NULL)
5368 pInvar = 0.0;
5369 else
5370 pInvar = (CLFlt) *GetParamVals (m->pInvar, chain, state[chain]);
5371 freq = ((CLFlt)1.0 - pInvar) / nPosRateCats;
5372
5373 /* get chain temperature */
5374 temp = Temperature (chainId[chain]);
5375 id = chainId[chain] % chainParams.numChains;
5376
5377 /* calculate rate probs */
5378 for (k=0; k<nPosRateCats; k++)
5379 {
5380 FlipSiteScalerSpace(m, chain);
5381 ResetSiteScalers(m, chain);
5382 for (c=0; c<m->numChars; c++)
5383 rateCat[c] = k;
5384 for (i=0; i<t->nIntNodes; i++)
5385 {
5386 p = t->intDownPass[i];
5387 if (t->isRooted == NO && p->anc->anc == NULL)
5388 m->CondLikeRoot (p, division, chain);
5389 else
5390 m->CondLikeDown (p, division, chain);
5391 if (m->unscaledNodes[chain][p->index] == 0)
5392 m->CondLikeScaler (p, division, chain);
5393 }
5394 /* find root conditional likes */
5395 p = t->root->left;
5396 clRoot = m->condLikes[m->condLikeIndex[chain][p->index]];
5397 for (c=0; c<m->numChars; c++)
5398 {
5399 catLike[k][c] = 0.0;
5400 for (i=0; i<nStates; i++)
5401 catLike[k][c] += bs[i]*clRoot[i];
5402 catLike[k][c] *= freq;
5403 catLnScaler[k][c] = lnScaler[c];
5404 clRoot += nStates;
5405 }
5406 FlipSiteScalerSpace(m, chain);
5407 }
5408
5409 /* fill in the invar cond likes if needed */
5410 if (m->pInvar != NULL)
5411 {
5412 k = nPosRateCats;
5413 for (c=0; c<m->numChars; c++)
5414 {
5415 catLike[k][c] = 0.0;
5416 for (i=0; i<nStates; i++)
5417 catLike[k][c] += bs[i]*clInvar[i];
5418 catLike[k][c] *= pInvar;
5419 clInvar += nStates;
5420 catLnScaler[k][c] = 0.0;
5421 }
5422 }
5423
5424 /* Now Gibbs sample the rate categories */
5425 for (c=0; c<m->numChars; c++)
5426 {
5427 /* find max scaler */
5428 maxLnScaler = catLnScaler[0][c];
5429 for (k=1; k<nRateCats; k++)
5430 {
5431 if (catLnScaler[k][c] > maxLnScaler && catLike[k][c] > 0.0)
5432 maxLnScaler = catLnScaler[k][c];
5433 }
5434 /* scale values */
5435 for (k=0; k<nRateCats; k++)
5436 {
5437 f = catLnScaler[k][c] - maxLnScaler;
5438 if (f < -100.0)
5439 catLike[k][c] = 0.0;
5440 else
5441 {
5442 catLike[k][c] *= (CLFlt) exp (f);
5443 /* take the temperature into account */
5444 if (id != 0)
5445 catLike[k][c] = (CLFlt) pow(catLike[k][c], temp);
5446 }
5447 }
5448 /* get cumulative sum */
5449 for (k=1; k<nRateCats; k++)
5450 catLike[k][c] += catLike[k-1][c];
5451 /* randomly sample a category; multiply by total to avoid scaling probs */
5452 ran = RandomNumber(seed) * catLike[nRateCats-1][c];
5453 for (k=0; k<nRateCats; k++)
5454 {
5455 if (ran < catLike[k][c])
5456 break;
5457 }
5458 rateCat[c] = k;
5459 }
5460
5461 /* recalculate everything */
5462 FlipSiteScalerSpace(m, chain);
5463 ResetSiteScalers(m, chain);
5464 for (i=0; i<t->nIntNodes; i++)
5465 {
5466 p = t->intDownPass[i];
5467 if (t->isRooted == NO && p->anc->anc == NULL)
5468 m->CondLikeRoot (p, division, chain);
5469 else
5470 m->CondLikeDown (p, division, chain);
5471 if (m->unscaledNodes[chain][p->index] == 0)
5472 m->CondLikeScaler (p, division, chain);
5473 }
5474 lnL = 0.0;
5475 m->Likelihood (t->root->left, division, chain, &lnL, (chainId[chain] % chainParams.numChains));
5476
5477 deltaLnL = lnL - m->lnLike[2*chain + state[chain]];
5478 m->lnLike[2*chain + state[chain]] = lnL;
5479
5480 return (deltaLnL);
5481 }
5482
5483
5484 /*------------------------------------------------------------------------
5485 |
5486 | InitAdGamma: initialize variables for adgamma model.
5487 |
5488 -------------------------------------------------------------------------*/
InitAdGamma(void)5489 int InitAdGamma (void)
5490 {
5491 int d, c, i, j, k, maxRates, *corrModel;
5492 ModelInfo *m;
5493
5494 /* take care of adgamma model */
5495 if (chainHasAdgamma == NO)
5496 return (NO_ERROR);
5497
5498 MrBayesPrint ("%s Initializing autocorrelated discrete gamma model\n", spacer);
5499
5500 /* allocate corr space */
5501 corrModel = (int *) SafeCalloc (numCurrentDivisions, sizeof(int));
5502 if (!corrModel)
5503 return ERROR;
5504
5505 /* allocate siteJump */
5506 if (memAllocs[ALLOC_SITEJUMP] == YES)
5507 {
5508 MrBayesPrint ("%s siteJump not free in InitAdGamma\n", spacer);
5509 free (corrModel);
5510 return ERROR;
5511 }
5512 siteJump = (int *) SafeCalloc (numChar, sizeof(int));
5513 if (siteJump)
5514 memAllocs[ALLOC_SITEJUMP] = YES;
5515 else
5516 {
5517 MrBayesPrint ("%s Problem allocating siteJump in InitAdGamma (%d ints)\n", spacer, numChar);
5518 free (corrModel);
5519 return ERROR;
5520 }
5521
5522 /* reset vector indicating the matrices needed */
5523 for (i=0; i<MAX_SMALL_JUMP; i++)
5524 hasMarkovTi[i] = NO;
5525
5526 /* fill in siteJump */
5527 for (i=0; i<numCurrentDivisions; i++)
5528 corrModel[i] = 0;
5529
5530 k = 1; /* index to corr model, 0 means no corr model */
5531 maxRates = 0; /* max no. rates */
5532 for (d=0; d<numCurrentDivisions; d++)
5533 modelSettings[d].mark = NO;
5534
5535 for (d=0; d<numCurrentDivisions; d++)
5536 {
5537 m = &modelSettings[d];
5538
5539 if (m->correlation == NULL || m->mark == YES)
5540 continue;
5541
5542 m->mark = YES;
5543 for (i=0; i<m->correlation->nRelParts; i++)
5544 {
5545 if (modelSettings[m->correlation->relParts[i]].shape ==
5546 modelSettings[d].shape)
5547 {
5548 modelSettings[m->correlation->relParts[i]].mark = YES;
5549 corrModel[m->correlation->relParts[i]] = k;
5550 }
5551 }
5552 k++;
5553
5554 if (m->numRateCats > maxRates)
5555 maxRates = m->numRateCats;
5556
5557 }
5558
5559 for (c=0; c<numChar; c++)
5560 {
5561 if (charInfo[c].isExcluded == YES)
5562 continue;
5563
5564 if ((k=corrModel[partitionId[c][partitionNum] - 1]) == 0)
5565 continue;
5566
5567 /* How far back is last char in this HMM? */
5568 for (j=c-1; j>=0; j--)
5569 {
5570 if (corrModel[partitionId[j][partitionNum] - 1] == k)
5571 break;
5572 }
5573
5574 if (j<0)
5575 siteJump[c] = 0;
5576 else if (charInfo[j].bigBreakAfter == YES)
5577 siteJump[c] = BIG_JUMP;
5578 else
5579 {
5580 siteJump[c] = c - j;
5581 hasMarkovTi[c-j-1] = YES;
5582 }
5583 }
5584
5585 /* check if any HMM is empty */
5586 k=0;
5587 for (i=0; i<numCurrentDivisions; i++)
5588 {
5589 if (corrModel[i] > k)
5590 k = corrModel[i];
5591 }
5592 for (i=1; i<=k; i++)
5593 {
5594 for (c=j=0; c<numChar; c++)
5595 {
5596 if (charInfo[c].isExcluded == NO && corrModel[partitionId[c][partitionNum] - 1] == i)
5597 j = c;
5598 }
5599 if (j == 0)
5600 {
5601 MrBayesPrint ("%s ERROR: HMM model %d is empty.\n",spacer,i);
5602 free (corrModel);
5603 return (ERROR);
5604 }
5605 }
5606
5607 /* allocate MarkovTis (space needed for calculations) */
5608 if (memAllocs[ALLOC_MARKOVTIS] == YES)
5609 {
5610 MrBayesPrint ("%s markovTis not free in InitAdGamma\n", spacer);
5611 free (corrModel);
5612 return ERROR;
5613 }
5614
5615 for (i=0; i<MAX_SMALL_JUMP; i++)
5616 {
5617 if (hasMarkovTi[i] == YES || i == 0) /* base matrix always needed */
5618 {
5619 markovTi[i] = AllocateSquareDoubleMatrix(maxRates);
5620 if (markovTi[i] == NULL)
5621 break;
5622 }
5623 else
5624 markovTi[i] = NULL;
5625 }
5626
5627 markovTiN = AllocateSquareDoubleMatrix(maxRates);
5628 if (i >= MAX_SMALL_JUMP && markovTiN)
5629 memAllocs[ALLOC_MARKOVTIS] = YES;
5630 else
5631 {
5632 MrBayesPrint ("%s Problem allocating MarkovTis in InitAdGamma (%d MrBFlt)\n", spacer, 2 * MAX_RATE_CATS * MAX_RATE_CATS);
5633 for (i=0; i<MAX_SMALL_JUMP; i++)
5634 if (markovTi[i] != NULL)
5635 FreeSquareDoubleMatrix (markovTi[i]);
5636 if (markovTiN != NULL)
5637 FreeSquareDoubleMatrix(markovTiN);
5638 free (corrModel);
5639 return ERROR;
5640 }
5641
5642 /* allocate space for rateProbs needed by adgamma model */
5643
5644 /* calculate size needed */
5645 i = 0;
5646 for (j=0; j<numCurrentDivisions; j++)
5647 {
5648 m = &modelSettings[j];
5649 if (m->correlation != NULL)
5650 {
5651 m->rateProbStart = i;
5652 i += m->numRateCats * m->numChars;
5653 }
5654 }
5655 rateProbRowSize = i;
5656
5657 /* allocate space */
5658 if (memAllocs[ALLOC_RATEPROBS] == YES)
5659 {
5660 MrBayesPrint ("%s Space for rate probs not free in InitAdGamma\n", spacer);
5661 free (corrModel);
5662 return ERROR;
5663 }
5664 rateProbSpace = (MrBFlt *) SafeMalloc (2 * numLocalChains * rateProbRowSize * sizeof(MrBFlt));
5665 rateProbs = (MrBFlt **) SafeMalloc (numLocalChains * sizeof(MrBFlt *));
5666 if (!rateProbSpace || !rateProbs)
5667 {
5668 MrBayesPrint ("%s Problem allocating rate probs\n", spacer);
5669 if (rateProbSpace)
5670 free (rateProbSpace);
5671 if (rateProbs)
5672 free (rateProbs);
5673 free (corrModel);
5674 return ERROR;
5675 }
5676 else
5677 memAllocs[ALLOC_RATEPROBS] = YES;
5678
5679 /* set chain rateProbs pointers */
5680 for (i=j=0; i<numLocalChains; i++)
5681 {
5682 rateProbs[i] = rateProbSpace + j;
5683 j += 2 * rateProbRowSize;
5684 }
5685
5686 free (corrModel);
5687
5688 return (NO_ERROR);
5689 }
5690
5691
5692 /*------------------------------------------------------------------------
5693 |
5694 | InitAugmentedModels: allocate and initialize space for augmented
5695 | models
5696 |
5697 -------------------------------------------------------------------------*/
InitAugmentedModels(void)5698 int InitAugmentedModels (void)
5699 {
5700 int d, i, j, useAugmentedModels, nRateCats;
5701 ModelInfo *m;
5702
5703 useAugmentedModels = NO;
5704 for (d=0; d<numCurrentDivisions; d++)
5705 {
5706 if (modelSettings[d].gibbsGamma == YES)
5707 useAugmentedModels = YES;
5708 }
5709
5710 if (useAugmentedModels == NO)
5711 return (NO_ERROR);
5712
5713 MrBayesPrint ("%s Initializing variables for model augmentation\n", spacer);
5714
5715 for (d=0; d<numCurrentDivisions; d++)
5716 {
5717 m = &modelSettings[d];
5718 if (m->gibbsGamma == NO)
5719 continue;
5720 m->tiIndex = (int *) SafeMalloc (numLocalChains * m->numChars * sizeof (int));
5721 if (!m->tiIndex)
5722 return ERROR;
5723 m->catLike = (CLFlt ***) SafeMalloc (numLocalChains * sizeof (CLFlt **));
5724 if (!m->catLike)
5725 return ERROR;
5726 m->catLnScaler = (CLFlt ***) SafeMalloc (numLocalChains * sizeof (CLFlt **));
5727 if (!m->catLnScaler)
5728 return ERROR;
5729 if (m->pInvar == NULL)
5730 nRateCats = m->numRateCats;
5731 else
5732 nRateCats = m->numRateCats + 1;
5733 for (i=0; i<numLocalChains; i++)
5734 {
5735 m->catLike[i] = (CLFlt **) SafeCalloc (nRateCats, sizeof (CLFlt *));
5736 if (!m->catLike[i])
5737 return ERROR;
5738 m->catLnScaler[i] = (CLFlt **) SafeCalloc (nRateCats, sizeof (CLFlt *));
5739 if (!m->catLnScaler[i])
5740 return ERROR;
5741 for (j=0; j<nRateCats; j++)
5742 {
5743 m->catLike[i][j] = (CLFlt *) SafeCalloc (m->numChars, sizeof (CLFlt));
5744 if (!m->catLike[i][j])
5745 return ERROR;
5746 m->catLnScaler[i][j] = (CLFlt *) SafeCalloc (m->numChars, sizeof (CLFlt));
5747 if (!m->catLnScaler[i][j])
5748 return ERROR;
5749 }
5750 }
5751 }
5752
5753 return NO_ERROR;
5754 }
5755
5756
5757 /*------------------------------------------------------------------------
5758 |
5759 | InitChainCondLikes: (1) calculate size of cond like arrays, tiprob arrays
5760 | and scaler arrays; (2) allocate space for cond like, tiprob and
5761 | scaler arrays; (3) allocate and set node indices pointing to
5762 | cond like and scaler arrays; (4) initialize tip cond likes;
5763 | (5) allocate space for precalculated cond likes; (6) allocate
5764 | space for adgamma probs, if used.
5765 |
5766 -------------------------------------------------------------------------*/
InitChainCondLikes(void)5767 int InitChainCondLikes (void)
5768 {
5769 int c, d, i, j, k, s, t, numReps, condLikesUsed, nIntNodes, nNodes, useBeagle,
5770 clIndex, tiIndex, scalerIndex, indexStep;
5771 BitsLong *charBits;
5772 CLFlt *cL;
5773 ModelInfo *m;
5774 # if defined (SSE_ENABLED)
5775 int j1;
5776 # endif
5777 # if defined (BEAGLE_ENABLED)
5778 int useBeagleMultiPartitions, divisionOffset;
5779 double *nSitesOfPat;
5780 MrBFlt freq;
5781 # endif
5782 # if defined (BEAGLE_V3_ENABLED)
5783 int *nPartsOfPat;
5784 # endif
5785
5786 /* Figure out how large cond like array is needed, and how many cond like, scaler and tiprob arrays are needed.
5787 Also check for possible use of Beagle */
5788 condLikesUsed = NO;
5789 for (d=0; d<numCurrentDivisions; d++)
5790 {
5791 m = &modelSettings[d];
5792
5793 MrBayesPrint ("%s Division %d has %d unique site patterns\n", spacer, d+1, m->numChars);
5794
5795 /* initialize model settings for chain cond likes */
5796 m->condLikeLength = 0;
5797 m->numCondLikes = 0;
5798
5799 if (m->parsModelId == YES)
5800 continue;
5801
5802 condLikesUsed = YES;
5803
5804 /* figure out length of cond like array */
5805 if (m->dataType == STANDARD)
5806 {
5807 # if defined (BEAGLE_ENABLED)
5808 m->useBeagle = NO;
5809 # endif
5810 for (c=0; c<m->numChars; c++)
5811 {
5812 numReps = m->numRateCats;
5813 if (m->nStates[c] == 2)
5814 numReps *= m->numBetaCats;
5815 m->condLikeLength += m->nStates[c] * numReps;
5816 }
5817 }
5818 else
5819 {
5820 if (m->gibbsGamma == YES)
5821 m->condLikeLength = m->numChars * m->numModelStates;
5822 else
5823 m->condLikeLength = m->numChars * m->numRateCats * m->numOmegaCats * m->numModelStates;
5824 # if defined (BEAGLE_ENABLED)
5825 /* tentatively decide on whether to use Beagle */
5826 if (tryToUseBEAGLE == YES)
5827 {
5828 if (m->printAncStates == YES || m->printSiteRates == YES || m->printPosSel ==YES || m->printSiteOmegas==YES)
5829 {
5830 MrBayesPrint ("%s Non-beagle version of conditional likelihood calculator will be used for division %d due to\n", spacer, d+1);
5831 MrBayesPrint ("%s request of reporting 'ancestral states', 'site rates', 'pos selection' or 'site omegas'.\n", spacer);
5832 }
5833 else if (m->gibbsGamma == NO)
5834 m->useBeagle = YES;
5835 }
5836 # endif
5837 }
5838
5839 /* find size of tree */
5840 nIntNodes = GetTree(m->brlens, 0, 0)->nIntNodes;
5841 nNodes = GetTree(m->brlens, 0, 0)->nNodes;
5842
5843 /* figure out number of cond like arrays */
5844 m->numCondLikes = (numLocalChains + 1) * (nIntNodes);
5845 m->numCondLikes += numLocalTaxa;
5846 /*
5847 # if !defined (DEBUG_NOSHORTCUTS)
5848 for (i=0; i<numLocalTaxa; i++)
5849 {
5850 if (m->isPartAmbig[i] == NO && m->dataType != STANDARD)
5851 m->numCondLikes--;
5852 }
5853 # endif
5854 */
5855
5856 /* figure out number of node and site scalers */
5857 m->numScalers = (numLocalChains + 1) * (nIntNodes + 1); /* add 1 for site scalers */
5858
5859 /* figure out length of ti prob array and number of ti prob arrays */
5860 m->tiProbLength = 0;
5861 if (m->dataType == STANDARD)
5862 {
5863 m->numTiCats = 0; /* We do not have repeated similar transition probability matrices */
5864 if (m->stateFreq->paramId == SYMPI_EQUAL)
5865 {
5866 for (k=0; k<9; k++)
5867 {
5868 if (m->isTiNeeded[k] == YES)
5869 m->tiProbLength += (k + 2) * (k + 2) * m->numRateCats;
5870 }
5871 for (k=9; k<13; k++)
5872 {
5873 if (m->isTiNeeded[k] == YES)
5874 m->tiProbLength += (k - 6) * (k - 6) * m->numRateCats;
5875 }
5876 for (k=13; k<18; k++)
5877 {
5878 if (m->isTiNeeded[k] == YES)
5879 m->tiProbLength += (k - 11) * (k - 11) * m->numRateCats;
5880 }
5881 }
5882 else
5883 {
5884 /* deal with unequal state frequencies */
5885 if (m->isTiNeeded[0] == YES)
5886 m->tiProbLength += 4 * m->numRateCats * m->numBetaCats;
5887 for (c=0; c<m->numChars; c++)
5888 {
5889 if (m->nStates[c] > 2 && (m->cType[c] == UNORD || m->cType[c] == ORD))
5890 {
5891 m->tiProbLength += (m->nStates[c] * m->nStates[c]) * m->numRateCats;
5892 }
5893 }
5894 }
5895 }
5896 else
5897 {
5898 m->numTiCats = m->numRateCats * m->numBetaCats * m->numOmegaCats; /* A single partition has either gamma, beta or omega categories */
5899 m->tiProbLength = m->numModelStates * m->numModelStates * m->numTiCats;
5900 }
5901 m->numTiProbs = (numLocalChains + 1) * nNodes;
5902
5903 /* set info about eigen systems */
5904 if (InitEigenSystemInfo (m) == ERROR)
5905 return (ERROR);
5906 }
5907
5908 /* check if conditional likelihoods are needed */
5909 if (condLikesUsed == YES)
5910 MrBayesPrint ("%s Initializing conditional likelihoods\n", spacer);
5911 else
5912 return NO_ERROR;
5913
5914 # if defined (BEAGLE_ENABLED)
5915 useBeagleMultiPartitions = NO;
5916 # endif
5917
5918 # if defined (BEAGLE_V3_ENABLED)
5919 if (beagleResourceNumber != 0 && numCurrentDivisions > 1 && InitBeagleMultiPartitionInstance() != ERROR && m->useBeagle == YES)
5920 useBeagleMultiPartitions = YES;
5921 # endif
5922
5923 /* allocate space and fill in info for tips */
5924 for (d=0; d<numCurrentDivisions; d++)
5925 {
5926 m = &modelSettings[d];
5927
5928 /* allocate space for conditional likelihoods */
5929 useBeagle = NO;
5930 # if defined (BEAGLE_ENABLED)
5931 if (m->useBeagle == YES)
5932 {
5933 if (useBeagleMultiPartitions == YES)
5934 useBeagle = YES;
5935 else if (InitBeagleInstance(m, d) != ERROR)
5936 useBeagle = YES;
5937 else
5938 m->useBeagle = NO;
5939 }
5940 # endif
5941 # if defined (SSE_ENABLED)
5942 /*if (useBeagle == NO && m->dataType != STANDARD)
5943 m->useSSE = YES;*/
5944 if (useBeagle == YES)
5945 {
5946 m->useVec = VEC_NONE;
5947 m->numFloatsPerVec = 0;
5948 }
5949
5950 # endif
5951 if (m->useBeagle == NO && m->useVec == VEC_NONE)
5952 MrBayesPrint ("%s Using standard non-SSE likelihood calculator for division %d (%s-precision)\n", spacer, d+1, (sizeof(CLFlt) == 4 ? "single" : "double"));
5953 else if (m->useBeagle == NO && m->useVec == VEC_SSE)
5954 MrBayesPrint ("%s Using standard SSE likelihood calculator for division %d (single-precision)\n", spacer, d+1);
5955 else if (m->useBeagle == NO && m->useVec == VEC_AVX)
5956 MrBayesPrint ("%s Using standard AVX likelihood calculator for division %d (single-precision)\n", spacer, d+1);
5957 else if (m->useBeagle == NO && m->useVec == VEC_FMA)
5958 MrBayesPrint ("%s Using standard FMA likelihood calculator for division %d (single-precision)\n", spacer, d+1);
5959 else if (m->useBeagle == NO)
5960 {
5961 MrBayesPrint ("%s WARNING! Using unknown SIMD likelihood calculator for division %d (single-precision)\n", spacer, d+1);
5962 return (ERROR);
5963 }
5964
5965 if (useBeagle == NO)
5966 {
5967 /* allocate cond like space */
5968 m->condLikes = (CLFlt**) SafeMalloc(m->numCondLikes * sizeof(CLFlt*));
5969 if (!m->condLikes)
5970 return (ERROR);
5971 for (i=0; i<m->numCondLikes; i++)
5972 {
5973 # if defined (SSE_ENABLED)
5974 if (m->useVec != VEC_NONE)
5975 {
5976 /* calculate number SSE chars */
5977 m->numVecChars = ((m->numChars - 1) / m->numFloatsPerVec) + 1;
5978
5979 /* allocate space with padding (m->condLikeLength is without padding) */
5980 if (m->gibbsGamma == YES)
5981 numReps = 1;
5982 else
5983 numReps = m->numRateCats * m->numOmegaCats;
5984 k = m->numVecChars * m->numFloatsPerVec * m->numModelStates * numReps;
5985
5986 if (m->useVec == VEC_AVX || m->useVec == VEC_FMA)
5987 m->condLikes[i] = (CLFlt*) AlignedMalloc (k * sizeof(CLFlt), 32);
5988 else
5989 m->condLikes[i] = (CLFlt*) AlignedMalloc (k * sizeof(CLFlt), 16);
5990
5991 if (!m->condLikes[i])
5992 return (ERROR);
5993
5994 /* start by filling all with 0.0f; pad when filling in tips */
5995 for (j=0; j<k; j++)
5996 m->condLikes[i][j] = 0.0f;
5997 }
5998 else
5999 {
6000 m->condLikes[i] = (CLFlt*) SafeMalloc(m->condLikeLength * sizeof(CLFlt));
6001 if (!m->condLikes[i])
6002 return (ERROR);
6003 }
6004 # else
6005 m->condLikes[i] = (CLFlt*) SafeMalloc(m->condLikeLength * sizeof(CLFlt));
6006 if (!m->condLikes[i])
6007 return (ERROR);
6008 # endif
6009 }
6010
6011 /* allocate scaler space and pointers for scaling */
6012 m->scalers = (CLFlt**) SafeMalloc(m->numScalers * sizeof(CLFlt*));
6013 if (!m->scalers)
6014 return (ERROR);
6015 for (i=0; i<m->numScalers; i++)
6016 {
6017 # if defined (SSE_ENABLED)
6018 if (m->useVec != VEC_NONE)
6019 {
6020 /* allocate space with padding */
6021 if (m->useVec == VEC_SSE)
6022 m->scalers[i] = (CLFlt*) AlignedMalloc (m->numVecChars * m->numFloatsPerVec * sizeof(CLFlt), 16);
6023 else if (m->useVec == VEC_AVX || m->useVec == VEC_FMA)
6024 m->scalers[i] = (CLFlt*) AlignedMalloc (m->numVecChars * m->numFloatsPerVec * sizeof(CLFlt), 32);
6025 if (!m->scalers[i])
6026 return (ERROR);
6027 for (j=0; j<m->numVecChars*m->numFloatsPerVec; j++)
6028 m->scalers[i][j] = 0.0f;
6029 }
6030 else
6031 {
6032 m->scalers[i] = (CLFlt*) SafeMalloc (m->numChars * sizeof(CLFlt));
6033 if (!m->scalers[i])
6034 return (ERROR);
6035 }
6036 # else
6037 m->scalers[i] = (CLFlt*) SafeMalloc (m->numChars * sizeof(CLFlt));
6038 if (!m->scalers[i])
6039 return (ERROR);
6040 # endif
6041 }
6042
6043 /* allocate stuff for facilitating scaling and accumulation of cond likes */
6044 if (m->dataType == STANDARD)
6045 {
6046 m->clP = (CLFlt **) SafeMalloc(m->numRateCats * sizeof(CLFlt *));
6047 if (!m->clP)
6048 return (ERROR);
6049 }
6050 else
6051 {
6052 m->clP = (CLFlt **) SafeMalloc(m->numTiCats * sizeof(CLFlt *));
6053 if (!m->clP)
6054 return (ERROR);
6055 # if defined (SSE_ENABLED)
6056 if (m->useVec == VEC_SSE)
6057 {
6058 m->clP_SSE = (__m128 **) SafeMalloc(m->numTiCats * sizeof(__m128 *));
6059 if (!m->clP_SSE)
6060 return (ERROR);
6061 m->lnL_Vec = AlignedMalloc (m->numVecChars * m->numFloatsPerVec * sizeof(CLFlt*), 16);
6062 m->lnLI_Vec = AlignedMalloc (m->numVecChars * m->numFloatsPerVec * sizeof(CLFlt*), 16);
6063 if (!m->lnL_Vec || !m->lnLI_Vec)
6064 return (ERROR);
6065 }
6066 #if defined (AVX_ENABLED)
6067 else if (m->useVec == VEC_AVX)
6068 {
6069 m->clP_AVX = (__m256 **) SafeMalloc(m->numTiCats * sizeof(__m256 *));
6070 if (!m->clP_AVX)
6071 return (ERROR);
6072 m->lnL_Vec = AlignedMalloc (m->numVecChars * m->numFloatsPerVec * sizeof(CLFlt*), 32);
6073 m->lnLI_Vec = AlignedMalloc (m->numVecChars * m->numFloatsPerVec * sizeof(CLFlt*), 32);
6074 if (!m->lnL_Vec || !m->lnLI_Vec)
6075 return (ERROR);
6076 }
6077 #endif
6078 #if defined (FMA_ENABLED)
6079 else if (m->useVec == VEC_FMA)
6080 {
6081 m->clP_AVX = (__m256 **) SafeMalloc(m->numTiCats * sizeof(__m256 *));
6082 if (!m->clP_AVX)
6083 return (ERROR);
6084 m->lnL_Vec = AlignedMalloc (m->numVecChars * m->numFloatsPerVec * sizeof(CLFlt*), 32);
6085 m->lnLI_Vec = AlignedMalloc (m->numVecChars * m->numFloatsPerVec * sizeof(CLFlt*), 32);
6086 if (!m->lnL_Vec || !m->lnLI_Vec)
6087 return (ERROR);
6088 }
6089 #endif
6090 # endif
6091 }
6092
6093 /* allocate tiprob space */
6094 m->tiProbs = (CLFlt**) SafeMalloc(m->numTiProbs * sizeof(CLFlt*));
6095 if (!m->tiProbs)
6096 return (ERROR);
6097 for (i=0; i<m->numTiProbs; i++)
6098 {
6099 m->tiProbs[i] = (CLFlt*) SafeMalloc(m->tiProbLength * sizeof(CLFlt));
6100 if (!m->tiProbs[i])
6101 return (ERROR);
6102 }
6103 }
6104
6105 /* allocate eigen system space (needed also for Beagle version */
6106 if (m->nCijkParts > 0)
6107 {
6108 m->cijks = (MrBFlt**) SafeMalloc((numLocalChains + 1) * sizeof(MrBFlt*));
6109 if (!m->cijks)
6110 return ERROR;
6111 for (i=0; i<numLocalChains+1; i++)
6112 {
6113 m->cijks[i] = (MrBFlt*) SafeMalloc(m->cijkLength * sizeof(MrBFlt));
6114 if (!m->cijks[i])
6115 return (ERROR);
6116 }
6117 }
6118
6119 /* get size of tree */
6120 nIntNodes = GetTree(m->brlens,0,0)->nIntNodes;
6121 nNodes = GetTree(m->brlens,0,0)->nNodes;
6122
6123 /* allocate and set indices from tree nodes to cond like arrays */
6124 m->condLikeIndex = (int **) SafeMalloc (numLocalChains * sizeof(int *));
6125 if (!m->condLikeIndex)
6126 return (ERROR);
6127 for (i=0; i<numLocalChains; i++)
6128 {
6129 m->condLikeIndex[i] = (int *) SafeMalloc (nNodes * sizeof(int));
6130 if (!m->condLikeIndex[i])
6131 return (ERROR);
6132 }
6133 for (i=0; i<numLocalChains; i++)
6134 for (j=0; j<nNodes; j++)
6135 m->condLikeIndex[i][j] = -1;
6136
6137 /* set up indices for terminal nodes */
6138 clIndex = 0;
6139 if (useBeagle == YES)
6140 indexStep = m->nCijkParts;
6141 else
6142 indexStep = 1;
6143 for (i=0; i<numLocalTaxa; i++)
6144 {
6145 # if !defined (DEBUG_NOSHORTCUTS)
6146 /* TODO: Untill CondLikeRoot_XXX are fixed (case 4 when one of the children is non-ambig) we allocate space for non-ambig tips. if fixed also uncoment down the function */
6147 /* if (useBeagle == NO && useSSE == NO && m->isPartAmbig[i] == NO && m->dataType != STANDARD)
6148 continue;
6149 */
6150 # endif
6151 for (j=0; j<numLocalChains; j++)
6152 m->condLikeIndex[j][i] = clIndex;
6153 clIndex += 1; /* even for multiple omega cat we need only one set of conditional likelihoods for terminals for all chains.*/
6154 }
6155
6156 /* reserve private space for parsimony-based moves if parsimony model is used */
6157 if (m->parsModelId == YES && m->parsimonyBasedMove == YES)
6158 clIndex += nIntNodes;
6159
6160 /* set up indices for internal nodes */
6161 for (j=0; j<numLocalChains; j++)
6162 {
6163 for (i=0; i<nIntNodes; i++)
6164 {
6165 m->condLikeIndex[j][i+numLocalTaxa] = clIndex;
6166 clIndex += indexStep;
6167 }
6168 }
6169
6170 /* allocate and set up scratch cond like indices */
6171 m->condLikeScratchIndex = (int *) SafeMalloc (nNodes * sizeof(int));
6172 if (!m->condLikeScratchIndex)
6173 return (ERROR);
6174 for (i=0; i<nNodes; i++)
6175 m->condLikeScratchIndex[i] = -1;
6176 for (i=0; i<nIntNodes; i++)
6177 {
6178 m->condLikeScratchIndex[i+numLocalTaxa] = clIndex;
6179 clIndex += indexStep;
6180 }
6181
6182 /* allocate and set indices from tree edges to ti prob arrays */
6183 m->tiProbsIndex = (int **) SafeMalloc (numLocalChains * sizeof(int *));
6184 if (!m->tiProbsIndex)
6185 return (ERROR);
6186 for (i=0; i<numLocalChains; i++)
6187 {
6188 m->tiProbsIndex[i] = (int *) SafeMalloc (nNodes * sizeof(int));
6189 if (!m->tiProbsIndex[i])
6190 return (ERROR);
6191 }
6192
6193 /* set up indices for nodes */
6194 tiIndex = 0;
6195 for (i=0; i<numLocalChains; i++)
6196 {
6197 for (j=0; j<nNodes; j++)
6198 {
6199 m->tiProbsIndex[i][j] = tiIndex;
6200 tiIndex += indexStep;
6201 }
6202 }
6203
6204 /* allocate and set up scratch transition prob indices */
6205 m->tiProbsScratchIndex = (int *) SafeMalloc (nNodes * sizeof(int));
6206 if (!m->tiProbsScratchIndex)
6207 return (ERROR);
6208 for (i=0; i<nNodes; i++)
6209 {
6210 m->tiProbsScratchIndex[i] = tiIndex;
6211 tiIndex += indexStep;
6212 }
6213
6214 /* allocate and set up rescale frequency */
6215 m->rescaleFreq = (int*) SafeMalloc((numLocalChains) * sizeof(int));
6216 for (i=0; i<numLocalChains; ++i)
6217 {
6218 if (m->numModelStates == 4 )
6219 m->rescaleFreq[i] = 1;
6220 else
6221 m->rescaleFreq[i] = 1;
6222 }
6223
6224 /* allocate and set up number of unscaled nodes + scratch space */
6225 m->unscaledNodes = (int **) SafeCalloc (numLocalChains, sizeof(int *));
6226 if (!m->unscaledNodes)
6227 return (ERROR);
6228 for (i=0; i<numLocalChains; i++)
6229 {
6230 m->unscaledNodes[i] = (int *) SafeCalloc (nNodes, sizeof(int));
6231 if (!m->unscaledNodes[i])
6232 return (ERROR);
6233 }
6234 m->unscaledNodesScratch = (int *) SafeCalloc (nNodes, sizeof (int));
6235 if (!m->unscaledNodesScratch)
6236 return (ERROR);
6237
6238 /* allocate and set up node scaler indices */
6239 scalerIndex = 0;
6240 m->nodeScalerIndex = (int **) SafeMalloc (numLocalChains * sizeof(int *));
6241 if (!m->nodeScalerIndex)
6242 return (ERROR);
6243 for (i=0; i<numLocalChains; i++)
6244 {
6245 m->nodeScalerIndex[i] = (int *) SafeMalloc (nNodes * sizeof(int));
6246 if (!m->nodeScalerIndex[i])
6247 return (ERROR);
6248 for (j=0; j<nNodes; j++)
6249 m->nodeScalerIndex[i][j] = -1;
6250 for (j=0; j<nIntNodes; j++)
6251 {
6252 m->nodeScalerIndex[i][j+numLocalTaxa] = scalerIndex;
6253 scalerIndex += indexStep;
6254 }
6255 }
6256 m->nodeScalerScratchIndex = (int *) SafeMalloc (nNodes * sizeof (int));
6257 if (!m->nodeScalerScratchIndex)
6258 return (ERROR);
6259 for (i=0; i<nNodes; i++)
6260 m->nodeScalerScratchIndex[i] = -1;
6261 for (i=0; i<nIntNodes; i++)
6262 {
6263 m->nodeScalerScratchIndex[i+numLocalTaxa] = scalerIndex;
6264 scalerIndex += indexStep;
6265 }
6266
6267 /* allocate and set up site scaler indices */
6268 m->siteScalerIndex = (int *) SafeMalloc (numLocalChains * sizeof(int));
6269 if (!m->siteScalerIndex)
6270 return (ERROR);
6271 for (i=0; i<numLocalChains; i++)
6272 {
6273 m->siteScalerIndex[i] = scalerIndex;
6274 scalerIndex += indexStep;
6275 }
6276 m->siteScalerScratchIndex = scalerIndex;
6277
6278 # if defined (BEAGLE_ENABLED)
6279 /* used only with Beagle advanced dynamic rescaling where we set scaler nodes for each partition */
6280 if (m->useBeagle == YES)
6281 {
6282 m->successCount = (int*) SafeMalloc((numLocalChains) * sizeof(int));
6283 m->beagleComputeCount = (long *) SafeMalloc(sizeof(long) * numLocalChains);
6284 t=BEAGLE_RESCALE_FREQ/m->numModelStates;
6285 if (beagleFlags & BEAGLE_FLAG_PRECISION_DOUBLE) /*if double presition is used*/
6286 t*=BEAGLE_RESCALE_FREQ_DOUBLE;
6287 for (i=0; i<numLocalChains; i++)
6288 {
6289 m->rescaleFreq[i] = t;
6290 }
6291 m->isScalerNode = (int**) SafeMalloc((numLocalChains) * sizeof(int*));
6292 /* we will use m->isScalerNode[chain][node->index] to determine whether the node is scaled or not. We do it only for internal nodes whose indexes start from numLocalTaxa thus we skew the pointer */
6293 m->isScalerNodeScratch = (int*) SafeMalloc(nIntNodes * sizeof(int)) - numLocalTaxa;
6294 assert (NO == 0); /* SafeMalloc set the allocated memmory to 0 while we need to set it to NO */
6295 for (i=0; i<numLocalChains; i++)
6296 {
6297 m->isScalerNode[i] = (int*) SafeMalloc(nIntNodes * sizeof(int)) - numLocalTaxa;
6298 }
6299 }
6300 # endif
6301
6302 /* allocate and set up cijk indices */
6303 if (m->nCijkParts > 0)
6304 {
6305 m->cijkIndex = (int *) SafeMalloc (numLocalChains * sizeof(int));
6306 if (!m->cijkIndex)
6307 return (ERROR);
6308 for (i=0; i<numLocalChains; i++)
6309 m->cijkIndex[i] = i*indexStep;
6310 m->cijkScratchIndex = numLocalChains*indexStep;
6311 }
6312
6313 # if defined (BEAGLE_ENABLED)
6314 /* Set up nSitesOfPat for Beagle */
6315 if (m->useBeagle == YES)
6316 {
6317 if (useBeagleMultiPartitions == NO)
6318 {
6319 nSitesOfPat = (double *) SafeMalloc (m->numChars * sizeof(double));
6320 for (c=0; c<m->numChars; c++)
6321 nSitesOfPat[c] = numSitesOfPat[m->compCharStart + c];
6322 beagleSetPatternWeights(m->beagleInstance,
6323 nSitesOfPat);
6324 free (nSitesOfPat);
6325 nSitesOfPat = NULL;
6326 /* Set up scalers for Beagle */
6327 for (i=0; i<m->numScalers*m->nCijkParts; i++)
6328 beagleResetScaleFactors(m->beagleInstance, i);
6329 }
6330
6331 /* find category frequencies */
6332 if (m->pInvar == NO)
6333 {
6334 freq = 1.0 / m->numRateCats;
6335
6336 /* set category frequencies in beagle instance */
6337 if (m->numOmegaCats <= 1)
6338 {
6339 divisionOffset = 0;
6340 if (m->useBeagleMultiPartitions == YES)
6341 divisionOffset = (numLocalChains + 1) * m->nCijkParts * m->divisionIndex;
6342
6343 for (i=0; i<m->numRateCats; i++)
6344 m->inWeights[i] = freq;
6345 for (i=0; i< (numLocalChains); i++)
6346 {
6347 beagleSetCategoryWeights(m->beagleInstance,
6348 m->cijkIndex[i] + divisionOffset,
6349 m->inWeights);
6350 }
6351 beagleSetCategoryWeights(m->beagleInstance,
6352 m->cijkScratchIndex + divisionOffset,
6353 m->inWeights);
6354 }
6355 }
6356
6357 }
6358 # endif
6359
6360 /* fill in tip conditional likelihoods */
6361 if (m->dataType == STANDARD)
6362 {
6363 clIndex = 0;
6364 for (i=0; i<numLocalTaxa; i++)
6365 {
6366 cL = m->condLikes[clIndex++];
6367 for (t=0; t<m->numRateCats;t++)
6368 {
6369 charBits = m->parsSets[i];
6370 for (c=0; c<m->numChars; c++)
6371 {
6372 if (m->nStates[c] == 2)
6373 numReps = m->numBetaCats;
6374 else
6375 numReps = 1;
6376 for (k=0; k<numReps; k++)
6377 {
6378 for (s=0; s<m->nStates[c]; s++)
6379 {
6380 if (IsBitSet(s, charBits))
6381 (*cL) = 1.0;
6382 cL++;
6383 }
6384 }
6385 charBits += m->nParsIntsPerSite;
6386 }
6387 }
6388 }
6389 }
6390 else if (useBeagle == NO)
6391 {
6392 if (m->gibbsGamma == YES)
6393 numReps = m->numTiCats / m->numRateCats;
6394 else
6395 numReps = m->numTiCats;
6396
6397 clIndex = 0;
6398 for (i=0; i<numLocalTaxa; i++)
6399 {
6400 # if !defined (DEBUG_NOSHORTCUTS) && !defined (SSE_ENABLED)
6401 /* TODO: Untill CondLikeRoot_XXX are fixed (case 4 when one of the children is non-ambig) we allocate space for non-ambig tips. if fixed also uncomment up the function */
6402 /* if (m->isPartAmbig[i] == NO && m->dataType != RESTRICTION)
6403 continue;
6404 */
6405 # endif
6406 cL = m->condLikes[clIndex++];
6407 # if defined (SSE_ENABLED) /* deal with setup for SIMD code */
6408 if (m->useVec != VEC_NONE)
6409 {
6410 for (k=0; k<numReps; k++)
6411 {
6412 charBits = m->parsSets[i];
6413 for (c=0; c<m->numChars/m->numFloatsPerVec; c++)
6414 {
6415 for (j=0; j<m->numModelStates/m->numStates; j++)
6416 {
6417 for (s=0; s<m->numStates; s++)
6418 {
6419 for (j1=0; j1<m->numFloatsPerVec; j1++)
6420 {
6421 if (IsBitSet(s, charBits + j1*m->nParsIntsPerSite))
6422 (*cL) = 1.0;
6423 cL++;
6424 }
6425 }
6426 }
6427 charBits += m->numFloatsPerVec * m->nParsIntsPerSite;
6428 }
6429 if (m->numChars % m->numFloatsPerVec > 0)
6430 {
6431 /* add last characters and padd */
6432 for (j=0; j<m->numModelStates/m->numStates; j++)
6433 {
6434 for (s=0; s<m->numStates; s++)
6435 {
6436 for (j1=0; j1<m->numChars%m->numFloatsPerVec; j1++)
6437 {
6438 if (IsBitSet(s, charBits + j1*m->nParsIntsPerSite))
6439 (*cL) = 1.0;
6440 cL++;
6441 }
6442 for (; j1<m->numFloatsPerVec; j1++)
6443 {
6444 (*cL) = 1.0;
6445 cL++;
6446 }
6447 }
6448 }
6449 }
6450 }
6451 }
6452 else
6453 {
6454 for (k=0; k<numReps; k++)
6455 {
6456 charBits = m->parsSets[i];
6457 for (c=0; c<m->numChars; c++)
6458 {
6459 for (j=0; j<m->numModelStates/m->numStates; j++)
6460 {
6461 for (s=0; s<m->numStates; s++)
6462 {
6463 if (IsBitSet(s, charBits))
6464 (*cL) = 1.0;
6465 cL++;
6466 }
6467 }
6468 charBits += m->nParsIntsPerSite;
6469 }
6470 }
6471 }
6472
6473 # else
6474 for (k=0; k<numReps; k++)
6475 {
6476 charBits = m->parsSets[i];
6477 for (c=0; c<m->numChars; c++)
6478 {
6479 for (j=0; j<m->numModelStates/m->numStates; j++)
6480 {
6481 for (s=0; s<m->numStates; s++)
6482 {
6483 if (IsBitSet(s, charBits))
6484 (*cL) = 1.0;
6485 cL++;
6486 }
6487 }
6488 charBits += m->nParsIntsPerSite;
6489 }
6490 }
6491 # endif
6492 }
6493 }
6494
6495 if (m->printAncStates == YES)
6496 {
6497 m->ancStateCondLikes = (CLFlt *) SafeMalloc (m->condLikeLength * sizeof(CLFlt));
6498 if (!m->ancStateCondLikes)
6499 return (ERROR);
6500 }
6501 }
6502
6503 #if defined (BEAGLE_V3_ENABLED)
6504 if (useBeagleMultiPartitions == YES)
6505 {
6506 nSitesOfPat = (double *) SafeMalloc (modelSettings[0].numCharsAll * sizeof(double));
6507 nPartsOfPat = (int *) SafeMalloc (modelSettings[0].numCharsAll * sizeof(int ));
6508 j = 0;
6509 for (d=0; d<numCurrentDivisions; d++)
6510 {
6511 m = &modelSettings[d];
6512 for (c=0; c<m->numChars; c++)
6513 {
6514 nSitesOfPat[j] = numSitesOfPat[m->compCharStart + c];
6515 nPartsOfPat[j] = m->divisionIndex;
6516 j++;
6517 }
6518 }
6519 beagleSetPatternWeights(modelSettings[0].beagleInstance,
6520 nSitesOfPat);
6521 beagleSetPatternPartitions(modelSettings[0].beagleInstance,
6522 numCurrentDivisions,
6523 nPartsOfPat);
6524 free (nSitesOfPat);
6525 free (nPartsOfPat);
6526 nSitesOfPat = NULL;
6527 nPartsOfPat = NULL;
6528 /* Set up scalers for Beagle */
6529 for (i=0; i<modelSettings[0].numScalers*modelSettings[0].nCijkParts; i++)
6530 beagleResetScaleFactors(modelSettings[0].beagleInstance, i);
6531 }
6532 #endif
6533
6534 /* allocate space for precalculated likelihoods */
6535 j = 0;
6536 for (d=0; d<numCurrentDivisions; d++)
6537 {
6538 m = &modelSettings[d];
6539 if (m->dataType == STANDARD || m->parsModelId == YES)
6540 continue;
6541
6542 i = (m->numModelStates + 1) * m->numModelStates * m->numTiCats;
6543 if (i > j)
6544 j = i;
6545 }
6546 if (j > 0) /* don't bother allocating precalculated likelihoods if we only have parsimony model or morphological characters */
6547 {
6548 if (memAllocs[ALLOC_PRELIKES] == YES)
6549 {
6550 MrBayesPrint ("%s Space for preLikes not free in InitChainCondLikes\n", spacer);
6551 return ERROR;
6552 }
6553 preLikeL = (CLFlt *) SafeMalloc (3 * j * sizeof(CLFlt));
6554 if (!preLikeL)
6555 {
6556 MrBayesPrint ("%s Problem allocating preLikes\n", spacer);
6557 return ERROR;
6558 }
6559 memAllocs[ALLOC_PRELIKES] = YES;
6560 preLikeR = preLikeL + j;
6561 preLikeA = preLikeR + j;
6562 }
6563
6564 return NO_ERROR;
6565 }
6566
6567
6568 /*------------------------------------------------------------------------
6569 |
6570 | InitEigenSystemInfo: set info about eigen decompositions
6571 |
6572 -------------------------------------------------------------------------*/
InitEigenSystemInfo(ModelInfo * m)6573 int InitEigenSystemInfo (ModelInfo *m)
6574 {
6575 int ts;
6576
6577 if (m->dataType == STANDARD)
6578 {
6579 /* dealt with in ProcessStdChars */
6580 return (NO_ERROR);
6581 }
6582
6583 m->cijkLength = 0;
6584 m->nCijkParts = 0;
6585 if (m->dataType == PROTEIN)
6586 {
6587 ts = m->numModelStates;
6588 m->cijkLength = (ts * ts * ts) + (2 * ts);
6589 m->nCijkParts = 1;
6590 if (m->switchRates != NULL) /* covarion model */
6591 {
6592 m->cijkLength *= m->numRateCats;
6593 m->nCijkParts = m->numRateCats;
6594 }
6595 }
6596 else if (m->dataType == DNA || m->dataType == RNA)
6597 {
6598 if (m->nucModelId == NUCMODEL_4BY4)
6599 {
6600 if (m->switchRates==NULL && m->nst != 6 && m->nst != NST_MIXED)
6601 {
6602 m->cijkLength = 0;
6603 m->nCijkParts = 0;
6604 # if defined (BEAGLE_ENABLED)
6605 ts = m->numModelStates;
6606 m->cijkLength = (ts * ts * ts) + (2 * ts);
6607 m->nCijkParts = 1;
6608 # endif
6609 }
6610 else
6611 {
6612 ts = m->numModelStates;
6613 m->cijkLength = (ts * ts * ts) + (2 * ts);
6614 m->nCijkParts = 1;
6615 }
6616 if (m->switchRates != NULL)
6617 {
6618 m->cijkLength *= m->numRateCats;
6619 m->nCijkParts = m->numRateCats;
6620 }
6621 }
6622 else if (m->nucModelId == NUCMODEL_DOUBLET)
6623 {
6624 ts = m->numModelStates;
6625 m->cijkLength = (ts * ts * ts) + (2 * ts);
6626 m->nCijkParts = 1;
6627 }
6628 else if (m->nucModelId == NUCMODEL_CODON)
6629 {
6630 ts = m->numModelStates;
6631 m->cijkLength = (ts * ts * ts) + (2 * ts);
6632 m->cijkLength *= m->numOmegaCats;
6633 m->nCijkParts = m->numOmegaCats;
6634 }
6635 else
6636 {
6637 MrBayesPrint ("%s ERROR: Something is wrong if you are here.\n", spacer);
6638 return ERROR;
6639 }
6640 }
6641 # if defined (BEAGLE_ENABLED)
6642 else if (m->dataType == RESTRICTION)
6643 {
6644 assert (m->numModelStates == 2);
6645 ts = 2;
6646 m->cijkLength = (ts * ts * ts) + (2 * ts);
6647 m->nCijkParts = 1;
6648 }
6649 # endif
6650 return (NO_ERROR);
6651 }
6652
6653
6654 /*------------------------------------------------------------------------
6655 |
6656 | InitFinalStateCondLikes: allocate space for final conditional
6657 | likelihoods if needed
6658 |
6659 -------------------------------------------------------------------------*/
InitFinalStateCondLikes(void)6660 int InitFinalStateCondLikes (void)
6661 {
6662 int d;
6663 ModelInfo *m;
6664
6665 for (d=0; d<numCurrentDivisions; d++)
6666 {
6667 m = &modelSettings[d];
6668 if (m->printAncStates == YES)
6669 {
6670 m->ancStateCondLikes = (CLFlt *) SafeMalloc (m->condLikeLength * sizeof(CLFlt));
6671 if (!m->ancStateCondLikes)
6672 return ERROR;
6673 }
6674 }
6675 return (NO_ERROR);
6676 }
6677
6678
6679 /*------------------------------------------------------------------------
6680 |
6681 | InitInvCondLikes: allocate and initialize invariable conditional
6682 | likelihoods if needed
6683 |
6684 | NB! Fills in invariable cond likes for all hidden states; this
6685 | is convenient although some space is wasted
6686 |
6687 -------------------------------------------------------------------------*/
InitInvCondLikes(void)6688 int InitInvCondLikes (void)
6689 {
6690 int c, d, i, s, isConstant, usingInvCondLikes;
6691 BitsLong *charBits;
6692 CLFlt *cI;
6693 ModelInfo *m;
6694
6695 # if defined (SSE_ENABLED)
6696 int c1;
6697 # endif
6698
6699 /* allocate space for invariable cond likes */
6700 usingInvCondLikes = NO;
6701 for (d=0; d<numCurrentDivisions; d++)
6702 {
6703 m = &modelSettings[d];
6704
6705 if (m->pInvar == NULL)
6706 continue;
6707
6708 usingInvCondLikes = YES;
6709 # if defined (SSE_ENABLED)
6710 if ( m->useVec != VEC_NONE )
6711 {
6712 c1 = m->numVecChars * m->numFloatsPerVec * m->numModelStates;
6713 if (m->useVec == VEC_AVX || m->useVec == VEC_FMA)
6714 m->invCondLikes = (CLFlt *) AlignedMalloc (c1 * sizeof(CLFlt), 32);
6715 else
6716 m->invCondLikes = (CLFlt *) AlignedMalloc (c1 * sizeof(CLFlt), 16);
6717 for (i=0; i<c1; i++)
6718 m->invCondLikes[i] = 0.0f;
6719 }
6720 else
6721 # endif
6722 {
6723 m->invCondLikes = (CLFlt *) SafeMalloc (m->numChars * m->numModelStates * sizeof(CLFlt));
6724 }
6725
6726 if (!m->invCondLikes)
6727 return ERROR;
6728 }
6729
6730 if (usingInvCondLikes == NO)
6731 return NO_ERROR;
6732
6733 MrBayesPrint ("%s Initializing invariable-site conditional likelihoods\n", spacer);
6734
6735 /* fill in invariable-site conditional likelihoods */
6736 for (d=0; d<numCurrentDivisions; d++)
6737 {
6738
6739 m = &modelSettings[d];
6740
6741 if (m->pInvar == NULL)
6742 continue;
6743
6744 cI = m->invCondLikes;
6745 if (m->dataType == STANDARD)
6746 {
6747 for (c=0; c<m->numChars; c++)
6748 {
6749 for (s=0; s<m->nStates[c]; s++)
6750 {
6751 isConstant = YES;
6752 for (i=0; i<numLocalTaxa; i++)
6753 {
6754 charBits = &m->parsSets[i][c*m->nParsIntsPerSite];
6755 if (IsBitSet(s, charBits) == NO)
6756 {
6757 isConstant = NO;
6758 break;
6759 }
6760 }
6761 if (isConstant == YES)
6762 *cI = 1.0;
6763 else
6764 *cI = 0.0;
6765 cI++;
6766 }
6767 }
6768 }
6769 else /* all other models for which pInvar is applicable */
6770 {
6771 assert (m->numModelStates == m->numStates);
6772 # if defined (SSE_ENABLED)
6773 if ( m->useVec != VEC_NONE )
6774 {
6775 for (c=0; c<m->numChars/m->numFloatsPerVec; c++)
6776 {
6777 for (s=0; s<m->numModelStates; s++)
6778 {
6779 for (c1=0; c1<m->numFloatsPerVec; c1++)
6780 {
6781 isConstant = YES;
6782 //charBits = parsMatrix + m->parsMatrixStart + ((c * m->numFloatsPerVec) + c1) * m->nParsIntsPerSite;
6783 for (i=0; i<numLocalTaxa; i++)
6784 {
6785 charBits = &m->parsSets[i][((c * m->numFloatsPerVec) + c1) *m->nParsIntsPerSite];
6786 if (IsBitSet(s, charBits) == NO)
6787 {
6788 isConstant = NO;
6789 break;
6790 }
6791 //charBits += parsMatrixRowSize;
6792 }
6793 if (isConstant == YES)
6794 *cI = 1.0;
6795 cI++;
6796 }
6797 }
6798 }
6799 if (m->numChars % m->numFloatsPerVec != 0)
6800 {
6801 for (s=0; s<m->numModelStates; s++)
6802 {
6803 for (c1=0; c1<m->numChars%m->numFloatsPerVec; c1++)
6804 {
6805 isConstant = YES;
6806 //charBits = parsMatrix + m->parsMatrixStart + (((m->numChars / m->numFloatsPerVec) * m->numFloatsPerVec) + c1) * m->nParsIntsPerSite;
6807 for (i=0; i<numLocalTaxa; i++)
6808 {
6809 charBits = &m->parsSets[i][(((m->numChars / m->numFloatsPerVec) * m->numFloatsPerVec) + c1) *m->nParsIntsPerSite];
6810 if (IsBitSet(s, charBits) == NO)
6811 {
6812 isConstant = NO;
6813 break;
6814 }
6815 //charBits += parsMatrixRowSize;
6816 }
6817 if (isConstant == YES)
6818 *cI = 1.0;
6819 cI++;
6820 }
6821 for (; c1<m->numFloatsPerVec; c1++)
6822 {
6823 *cI = 1.0;
6824 cI++;
6825 }
6826 }
6827 }
6828 }
6829 else
6830 # endif
6831 {
6832 for (c=0; c<m->numChars; c++)
6833 {
6834 for (s=0; s<m->numModelStates; s++)
6835 {
6836 isConstant = YES;
6837 for (i=0; i<numLocalTaxa; i++)
6838 {
6839 charBits = &m->parsSets[i][c*m->nParsIntsPerSite];
6840 if (IsBitSet(s, charBits) == NO)
6841 {
6842 isConstant = NO;
6843 break;
6844 }
6845 }
6846 if (isConstant == YES)
6847 *cI = 1.0;
6848 cI++;
6849 }
6850 }
6851 }
6852 }
6853 } /* next division */
6854
6855 # if 0
6856 invCondLikeSize = 0;
6857 for (d=0; d<numCurrentDivisions; d++)
6858 {
6859 m = &modelSettings[d];
6860 if (m->pInvar == NULL)
6861 continue;
6862 cI = m->invCondLikes;
6863 if (m->dataType == STANDARD)
6864 {
6865 }
6866 else
6867 {
6868 for (c=0; c<m->numChars; c++)
6869 {
6870 printf ("%4d -- ", c);
6871 for (s=0; s<m->numModelStates; s++)
6872 {
6873 printf ("%1.0lf", *cI);
6874 cI++;
6875 }
6876 printf ("\n");
6877 }
6878 }
6879 }
6880 # endif
6881
6882 return NO_ERROR;
6883 }
6884
6885
6886 /*------------------------------------------------------------------------
6887 |
6888 | InitParsSets: allocate space for and set parsimony state sets
6889 |
6890 -------------------------------------------------------------------------*/
InitParsSets(void)6891 int InitParsSets (void)
6892 {
6893 int c, i, j, k, d, nParsStatesForCont, nIntNodes, nNodes,
6894 nuc1, nuc2, nuc3, codingNucCode, allNucCode;
6895 BitsLong allAmbig, x, x1, x2, x3, *longPtr, bitsLongOne;
6896 ModelInfo *m;
6897 ModelParams *mp;
6898
6899 bitsLongOne = 1;
6900
6901 /* this variable determines how many parsimony states are used */
6902 /* to represent continuous characters (determines weight of these chars) */
6903 nParsStatesForCont = 3;
6904
6905 /* find number and size of parsimony sets and node lengths */
6906 for (d=0; d<numCurrentDivisions; d++)
6907 {
6908 m = &modelSettings[d];
6909 mp = &modelParams[d];
6910
6911 /* find how many parsimony ints (BitsLong) are needed for each model site */
6912 if (mp->dataType == CONTINUOUS)
6913 {
6914 /* scale continuous characters down to an ordered parsimony character */
6915 /* with nParsStatesForCont states, represent this character as a set */
6916 /* of binary characters by additive binary coding */
6917 m->nParsIntsPerSite = nParsStatesForCont - 1;
6918 }
6919 else
6920 m->nParsIntsPerSite = 1 + m->numStates / nBitsInALong;
6921
6922 /* Calculate number of nodes and number of internal nodes */
6923 nIntNodes = GetTree(m->brlens,0,0)->nIntNodes;
6924 nNodes = GetTree(m->brlens,0,0)->nNodes;
6925
6926 /* Calculate number of parsimony sets */
6927 m->numParsSets = numLocalTaxa;
6928 if (m->parsimonyBasedMove == YES || !strcmp(chainParams.startTree, "Parsimony"))
6929 m->numParsSets += nIntNodes;
6930 if (m->parsModelId == YES)
6931 m->numParsSets += (numLocalChains + 1) * nIntNodes;
6932
6933 if (m->parsModelId == YES)
6934 m->numParsNodeLens = (numLocalChains + 1) * nNodes;
6935 else
6936 m->numParsNodeLens = 0;
6937 }
6938
6939 /* then allocate space for the sets and node lengths */
6940 for (d=0; d<numCurrentDivisions; d++)
6941 {
6942 m = &modelSettings[d];
6943
6944 m->parsSets = (BitsLong **) SafeCalloc (m->numParsSets, sizeof(BitsLong*));
6945 if (!m->parsSets)
6946 return (ERROR);
6947 for (i=0; i<m->numParsSets; i++)
6948 {
6949 m->parsSets[i] = (BitsLong *) SafeCalloc (m->numChars*m->nParsIntsPerSite, sizeof(BitsLong));
6950 if (!m->parsSets[i])
6951 return (ERROR);
6952 }
6953
6954 if (m->numParsNodeLens > 0)
6955 {
6956 m->parsNodeLens = (CLFlt *) SafeCalloc (m->numParsNodeLens, sizeof(CLFlt));
6957 if (!m->parsNodeLens)
6958 return (ERROR);
6959 }
6960 }
6961
6962 /* finally fill in tip parsimony sets */
6963 for (d=0; d<numCurrentDivisions; d++)
6964 {
6965 m = &modelSettings[d];
6966 mp = &modelParams[d];
6967
6968 if (mp->dataType == CONTINUOUS)
6969 {
6970 /* Note: This is only a placeholder since continuous characters are not implemented yet.
6971 Using additive parsimony would be more efficient than using multiple binary chars as here. */
6972 for (i=0; i<numLocalTaxa; i++)
6973 {
6974 for (c=0, j=m->compMatrixStart; j<m->compMatrixStop; j++, c++)
6975 {
6976 x = compMatrix[pos(i,j,compMatrixRowSize)];
6977
6978 for (k=0; k<m->nParsIntsPerSite; k++)
6979 {
6980 if (x > (unsigned int)(k + 1) * 1000 / (m->nParsIntsPerSite + 1))
6981 m->parsSets[i][c*m->nParsIntsPerSite + k] = 1;
6982 else
6983 m->parsSets[i][c*m->nParsIntsPerSite + k] = 2;
6984 }
6985 }
6986 }
6987 }
6988 else if (m->nCharsPerSite == 1 && m->nParsIntsPerSite == 1)
6989 {
6990 allAmbig = (bitsLongOne<<mp->nStates) - 1UL;
6991 for (i=0; i<numLocalTaxa; i++)
6992 {
6993 for (c=0, j=m->compMatrixStart; j<m->compMatrixStop; j++, c++)
6994 {
6995 x = compMatrix[pos(i,j,compMatrixRowSize)];
6996
6997 if (x == MISSING || x == GAP)
6998 m->parsSets[i][c] = allAmbig;
6999 else
7000 m->parsSets[i][c] = x;
7001 }
7002 }
7003 }
7004 else if (!strcmp(mp->nucModel, "Doublet") && (mp->dataType == DNA || mp->dataType == RNA))
7005 {
7006 allAmbig = 15;
7007 for (i=0; i<numLocalTaxa; i++)
7008 {
7009 for (c=0, j=m->compMatrixStart; j<m->compMatrixStop; j+=m->nCharsPerSite, c++)
7010 {
7011 /* fetch the original values x1 and x2 */
7012 x1 = compMatrix[pos(i,j,compMatrixRowSize)];
7013 if (x1 == MISSING || x1 == GAP)
7014 x1 = allAmbig;
7015 x2 = compMatrix[pos(i,j+1,compMatrixRowSize)];
7016 if (x2 == MISSING || x2 == GAP)
7017 x2 = allAmbig;
7018 /* squeeze them together in the new value x */
7019 x = 0;
7020 for (nuc1=0; nuc1<4; nuc1++)
7021 {
7022 for (nuc2=0; nuc2<4; nuc2++)
7023 {
7024 if (IsBitSet(nuc1,&x1) == YES && IsBitSet(nuc2, &x2) == YES)
7025 x |= (bitsLongOne<<(nuc1*4 + nuc2));
7026 }
7027 }
7028
7029 m->parsSets[i][c] = x;
7030 }
7031 }
7032 }
7033 else if (!strcmp(mp->nucModel, "Codon") && (mp->dataType == DNA || mp->dataType == RNA))
7034 {
7035 allAmbig = 15;
7036 for (i=0; i<numLocalTaxa; i++)
7037 {
7038 for (c=0, j=m->compMatrixStart; j<m->compMatrixStop; j+=m->nCharsPerSite, c++)
7039 {
7040 /* fetch the original values x1, x2, and x3*/
7041 x1 = compMatrix[pos(i,j,compMatrixRowSize)];
7042 if (x1 == MISSING || x1 == GAP)
7043 x1 = allAmbig;
7044 x2 = compMatrix[pos(i,j+1,compMatrixRowSize)];
7045 if (x2 == MISSING || x2 == GAP)
7046 x2 = allAmbig;
7047 x3 = compMatrix[pos(i,j+2,compMatrixRowSize)];
7048 if (x3 == MISSING || x3 == GAP)
7049 x3 = allAmbig;
7050
7051 /* squeeze them together in the new long string pointed to by longPtr */
7052 longPtr = &m->parsSets[i][c*m->nParsIntsPerSite];
7053 allNucCode = codingNucCode = 0;
7054 for (nuc1=0; nuc1<4; nuc1++)
7055 for (nuc2=0; nuc2<4; nuc2++)
7056 for (nuc3=0; nuc3<4; nuc3++)
7057 {
7058 if (mp->codon[allNucCode] != 21)
7059 {
7060 if (IsBitSet(nuc1, &x1) == YES && IsBitSet(nuc2, &x2) == YES && IsBitSet(nuc3, &x3) == YES)
7061 SetBit(codingNucCode, longPtr);
7062 codingNucCode++;
7063 }
7064 allNucCode++;
7065 }
7066 }
7067 }
7068 }
7069 else if (!strcmp(mp->nucModel, "Protein") && (mp->dataType == DNA || mp->dataType == RNA))
7070 {
7071 allAmbig = 15;
7072 for (i=0; i<numLocalTaxa; i++)
7073 {
7074 for (c=0, j=m->compMatrixStart; j<m->compMatrixStop; j+=m->nCharsPerSite, c++)
7075 {
7076 /* fetch the original values x1, x2, and x3*/
7077 x1 = compMatrix[pos(i,j,compMatrixRowSize)];
7078 if (x1 == MISSING || x1 == GAP)
7079 x1 = allAmbig;
7080 x2 = compMatrix[pos(i,j+1,compMatrixRowSize)];
7081 if (x2 == MISSING || x2 == GAP)
7082 x2 = allAmbig;
7083 x3 = compMatrix[pos(i,j+2,compMatrixRowSize)];
7084 if (x3 == MISSING || x3 == GAP)
7085 x3 = allAmbig;
7086
7087 /* squeeze them together in the new long string pointed to by longPtr */
7088 longPtr = &m->parsSets[i][c*m->nParsIntsPerSite]; /* m->nParsIntsPerSite should be 1 */
7089 allNucCode = 0;
7090 for (nuc1=0; nuc1<4; nuc1++)
7091 for (nuc2=0; nuc2<4; nuc2++)
7092 for (nuc3=0; nuc3<4; nuc3++)
7093 {
7094 if (mp->codon[allNucCode] != 21)
7095 {
7096 if (IsBitSet(nuc1, &x1) == YES && IsBitSet(nuc2, &x2) == YES && IsBitSet(nuc3, &x3) == YES)
7097 SetBit(mp->codon[allNucCode]-1, longPtr);
7098 }
7099 allNucCode++;
7100 }
7101 }
7102 }
7103 }
7104 else
7105 {
7106 MrBayesPrint ("%s Unrecognized data format during bitset compression\n", spacer);
7107 return ERROR;
7108 }
7109 }
7110
7111 return (NO_ERROR);
7112 }
7113
7114
7115 /*------------------------------------------------
7116 |
7117 | InitPrintParams: Set up arrays of print para-
7118 | meters and print tree parameters
7119 |
7120 ------------------------------------------------*/
InitPrintParams(void)7121 int InitPrintParams (void)
7122 {
7123 int i, j, k, k1=0;
7124 Param *p;
7125
7126 /* count number of model params to print */
7127 numPrintParams = 0;
7128 for (i=0; i<numParams; i++)
7129 {
7130 p = ¶ms[i];
7131 if (p->printParam == YES &&
7132 p->paramType != P_TOPOLOGY &&
7133 p->paramType != P_BRLENS &&
7134 p->paramType != P_SPECIESTREE &&
7135 p->paramType != P_CPPEVENTS &&
7136 p->paramType != P_TK02BRANCHRATES &&
7137 p->paramType != P_IGRBRANCHRATES &&
7138 p->paramType != P_MIXEDBRCHRATES)
7139 numPrintParams++;
7140 }
7141
7142 /* count number of tree params to print */
7143 numPrintTreeParams = 0;
7144 for (i=0; i<numParams; i++)
7145 {
7146 p = ¶ms[i];
7147 if (p->paramType == P_TOPOLOGY)
7148 {
7149 /* always print parsimony topology (printParam == YES), otherwise */
7150 /* print topology only if brlens never requested (nPrintSubParams == 0)*/
7151 if (p->printParam == YES || p->nPrintSubParams == 0)
7152 numPrintTreeParams++;
7153 }
7154 else if (p->paramType == P_BRLENS)
7155 {
7156 /* print only if brlens (or events) requested for at least one partition */
7157 if (p->printParam == YES || p->nPrintSubParams > 0)
7158 numPrintTreeParams++;
7159 }
7160 else if (p->paramType == P_SPECIESTREE)
7161 {
7162 /* always print if printParam set to YES */
7163 if (p->printParam == YES)
7164 numPrintTreeParams++;
7165 }
7166 }
7167
7168 /* allocate space */
7169 printParam = (Param **) SafeCalloc (numPrintParams + numPrintTreeParams + numTopologies, sizeof(Param *));
7170 topologyPrintIndex = (int *) SafeCalloc (numTopologies + numPrintTreeParams, sizeof(int));
7171 if (!printParam || !topologyPrintIndex)
7172 {
7173 free (printParam);
7174 free (topologyPrintIndex);
7175 MrBayesPrint ("%s Could not allocate printParam vector in InitPrintParams\n", spacer);
7176 return (ERROR);
7177 }
7178 printTreeParam = printParam + numPrintParams;
7179 topologyParam = printTreeParam + numPrintTreeParams;
7180 printTreeTopologyIndex = topologyPrintIndex + numTopologies;
7181 memAllocs[ALLOC_PRINTPARAM] = YES;
7182
7183 /* assign normal print params */
7184 for (i=j=0; i<numParams; i++)
7185 {
7186 p = ¶ms[i];
7187 if (p->printParam == YES &&
7188 p->paramType != P_TOPOLOGY &&
7189 p->paramType != P_BRLENS &&
7190 p->paramType != P_SPECIESTREE &&
7191 p->paramType != P_CPPEVENTS &&
7192 p->paramType != P_TK02BRANCHRATES &&
7193 p->paramType != P_IGRBRANCHRATES &&
7194 p->paramType != P_MIXEDBRCHRATES)
7195 printParam[j++] = p;
7196 }
7197
7198 /* assign tree print params */
7199 for (i=j=k=0; i<numParams; i++)
7200 {
7201 p = ¶ms[i];
7202 if (p->paramType == P_TOPOLOGY)
7203 {
7204 /* always print parsimony topology (printParam == YES), otherwise */
7205 /* print topology only if brlens never requested (nPrintSubParams == 0)*/
7206 if (p->printParam == YES || p->nPrintSubParams == 0)
7207 numPrintTreeParams++;
7208 }
7209 else if (p->paramType == P_BRLENS)
7210 {
7211 /* print only if brlens (or events) requested for at least one partition */
7212 if (p->printParam == YES || p->nPrintSubParams > 0)
7213 printTreeParam[k++] = p;
7214 }
7215 else if (p->paramType == P_SPECIESTREE)
7216 {
7217 if (p->printParam == YES)
7218 printTreeParam[k++] = p;
7219 }
7220 }
7221
7222 /* find topologies, topology file index, and printtree topology index */
7223 for (i=0; i<numPrintTreeParams; i++)
7224 printTreeTopologyIndex[i] = numTopologies;
7225 for (i=j=0; i<numParams; i++)
7226 {
7227 p = ¶ms[i];
7228 if (p->paramType == P_SPECIESTREE)
7229 {
7230 topologyParam[j] = p;
7231 for (k=0; k<numPrintTreeParams; k++)
7232 if (printTreeParam[k] == p)
7233 break;
7234 topologyPrintIndex[j] = k;
7235 printTreeTopologyIndex[k] = j;
7236 j++;
7237 }
7238 else if (p->paramType == P_TOPOLOGY)
7239 {
7240 topologyParam[j] = p;
7241 for (k=0; k<numPrintTreeParams; k++)
7242 if (printTreeParam[k] == p)
7243 break;
7244 if (k<numPrintTreeParams)
7245 {
7246 topologyPrintIndex[j] = k;
7247 printTreeTopologyIndex[k] = j;
7248 }
7249 else
7250 {
7251 for (k=0; k<p->nSubParams; k++)
7252 {
7253 for (k1=0; k1<numPrintTreeParams; k1++)
7254 if (printTreeParam[k1] == p->subParams[k])
7255 break;
7256 if (k1 < numPrintTreeParams)
7257 break;
7258 }
7259 topologyPrintIndex[j] = k1;
7260 printTreeTopologyIndex[k1] = j;
7261 }
7262 j++;
7263 }
7264 }
7265
7266 return (NO_ERROR);
7267 }
7268
7269
IsPFNodeEmpty(PFNODE * p)7270 int IsPFNodeEmpty (PFNODE *p)
7271 {
7272 int i;
7273
7274 for (i=0; i<chainParams.numRuns; i++)
7275 {
7276 if (p->count[i] > 0)
7277 break;
7278 }
7279 if (i == chainParams.numRuns)
7280 return YES;
7281 else
7282 return NO;
7283 }
7284
7285
7286 /* LargestNonemptyPFNode: recursive function to largest nonempty node in a subtree */
LargestNonemptyPFNode(PFNODE * p,int * i,int j)7287 PFNODE *LargestNonemptyPFNode (PFNODE *p, int *i, int j)
7288 {
7289 PFNODE *q;
7290
7291 ++j;
7292 if (p == NULL)
7293 return NULL;
7294
7295 q = LargestNonemptyPFNode (p->left, i, j);
7296
7297 if (q != NULL)
7298 {
7299 return q;
7300 }
7301 else if (IsPFNodeEmpty (p) == NO)
7302 {
7303 *i = j;
7304 return p;
7305 }
7306 else
7307 {
7308 return LargestNonemptyPFNode (p->right, i, j);
7309 }
7310 }
7311
7312
7313 /* ln prior ratio for clock trees */
LogClockTreePriorRatio(Param * param,int chain,MrBFlt * lnPriorRatio)7314 int LogClockTreePriorRatio (Param *param, int chain, MrBFlt *lnPriorRatio)
7315 {
7316 MrBFlt oldLnPrior, newLnPrior, theta, N, growth, clockRate, sF, *sR, *eR, *fR;
7317 char *sS;
7318 Model *mp;
7319 ModelInfo *m;
7320 Tree *newTree, *oldTree;
7321 TreeNode *p, *q=NULL;
7322 int i, j;
7323
7324 (*lnPriorRatio) = 0.0;
7325
7326 mp = &modelParams[param->relParts[0]];
7327 m = &modelSettings[param->relParts[0]];
7328
7329 newTree = GetTree (m->brlens, chain, state[chain]);
7330 oldTree = GetTree (m->brlens, chain, state[chain] ^ 1);
7331
7332 if (m->clockRate != NULL)
7333 clockRate = *GetParamVals(m->clockRate, chain, state[chain]);
7334 else
7335 clockRate = 1.0;
7336
7337 /* calculate prior ratio on brlens of clock tree */
7338 if (!strcmp(mp->clockPr,"Coalescence"))
7339 {
7340 /* coalescence prior */
7341 /* first calculate theta as 4*N*mu, 3*N*mu or 2*N*mu */
7342 N = *(GetParamVals (m->popSize, chain, state[chain]));
7343 if (!strcmp(mp->ploidy, "Diploid"))
7344 theta = 4 * N * clockRate;
7345 else if (!strcmp(mp->ploidy, "Zlinked"))
7346 theta = 3 * N * clockRate;
7347 else
7348 theta = 2 * N * clockRate;
7349 /* deal with growth */
7350 if (!strcmp(mp->growthPr, "Fixed"))
7351 growth = mp->growthFix;
7352 else
7353 growth = *(GetParamVals (m->growthRate, chain, state[chain]));
7354 if (LnCoalescencePriorPr (oldTree, &oldLnPrior, theta, growth) == ERROR)
7355 {
7356 MrBayesPrint ("%s Problem calculating prior for coalescence process\n", spacer);
7357 return (ERROR);
7358 }
7359 if (LnCoalescencePriorPr (newTree, &newLnPrior, theta, growth) == ERROR)
7360 {
7361 MrBayesPrint ("%s Problem calculating prior for coalescence process\n", spacer);
7362 return (ERROR);
7363 }
7364 (*lnPriorRatio) = (newLnPrior - oldLnPrior);
7365 }
7366 else if (!strcmp(mp->clockPr,"Birthdeath"))
7367 {
7368 /* birth-death prior */
7369 sR = GetParamVals (m->speciationRates, chain, state[chain]);
7370 eR = GetParamVals (m->extinctionRates, chain, state[chain]);
7371 sS = mp->sampleStrat;
7372 sF = mp->sampleProb;
7373 if (LnBirthDeathPriorPr (oldTree, clockRate, &oldLnPrior, *sR, *eR, sS, sF) == ERROR)
7374 {
7375 MrBayesPrint ("%s Problem calculating prior for birth-death process\n", spacer);
7376 return (ERROR);
7377 }
7378 if (LnBirthDeathPriorPr (newTree, clockRate, &newLnPrior, *sR, *eR, sS, sF) == ERROR)
7379 {
7380 MrBayesPrint ("%s Problem calculating prior for birth-death process\n", spacer);
7381 return (ERROR);
7382 }
7383 (*lnPriorRatio) = (newLnPrior - oldLnPrior);
7384 }
7385 else if (!strcmp(mp->clockPr,"Fossilization"))
7386 {
7387 /* fossilized birth-death prior */
7388 sR = GetParamVals (m->speciationRates, chain, state[chain]);
7389 eR = GetParamVals (m->extinctionRates, chain, state[chain]);
7390 fR = GetParamVals (m->fossilizationRates, chain, state[chain]);
7391 sF = mp->sampleProb;
7392 sS = mp->sampleStrat;
7393 if (LnFossilizationPriorPr (oldTree, clockRate, &oldLnPrior, sR, eR, fR, sF, sS) == ERROR)
7394 {
7395 MrBayesPrint ("%s Problem calculating prior for fossilized birth-death process\n", spacer);
7396 return (ERROR);
7397 }
7398 if (LnFossilizationPriorPr (newTree, clockRate, &newLnPrior, sR, eR, fR, sF, sS) == ERROR)
7399 {
7400 MrBayesPrint ("%s Problem calculating prior for fossilized birth-death process\n", spacer);
7401 return (ERROR);
7402 }
7403 (*lnPriorRatio) = (newLnPrior - oldLnPrior);
7404 }
7405 else if (!strcmp(mp->clockPr,"Uniform"))
7406 {
7407 oldLnPrior = LnUniformPriorPr(oldTree, clockRate);
7408 newLnPrior = LnUniformPriorPr(newTree, clockRate);
7409 (*lnPriorRatio) = (newLnPrior - oldLnPrior);
7410 }
7411 else if (!strcmp(mp->clockPr,"Speciestreecoalescence"))
7412 {
7413 // Defer this calculation to the BEST code
7414 }
7415
7416 assert (*lnPriorRatio > NEG_INFINITY);
7417
7418 /* take care of calibrations */
7419 if (newTree->isCalibrated == YES)
7420 {
7421 for (i=0; i<newTree->nNodes-1; i++)
7422 {
7423 p = newTree->allDownPass[i];
7424 if (p->isDated == NO)
7425 continue;
7426 for (j=0; j<oldTree->nNodes-1; j++)
7427 {
7428 q = oldTree->allDownPass[j];
7429 if (p->lockID == q->lockID)
7430 break;
7431 }
7432 assert (j != oldTree->nNodes-1);
7433 if (p->isDated == YES && p->calibration->prior != fixed)
7434 {
7435 (*lnPriorRatio) += p->calibration->LnPriorRatio(p->age, q->age, p->calibration->priorParams);
7436 }
7437 }
7438 }
7439
7440 return (NO_ERROR);
7441 }
7442
7443
7444 /*-----------------------------------------------------------------
7445 |
7446 | LogLike: calculate the log likelihood of the new state of the chain
7447 |
7448 -----------------------------------------------------------------*/
LogLike(int chain)7449 MrBFlt LogLike (int chain)
7450 {
7451 int i, d;
7452 ModelInfo *m;
7453 MrBFlt chainLnLike, lnL;
7454
7455 /* initialize chain cond like */
7456 chainLnLike = 0.0;
7457
7458 if (chainParams.runWithData == NO)
7459 return (chainLnLike);
7460
7461 # if defined (DEBUG_RUN_WITHOUT_DATA)
7462 return (chainLnLike);
7463 # endif
7464
7465 # if defined (BEAGLE_V3_ENABLED)
7466 if (modelSettings[0].useBeagleMultiPartitions == YES)
7467 {
7468 /* Launch all divisions that require updating in one Beagle instance, concurrently */
7469 LaunchLogLikeForBeagleMultiPartition(chain, &chainLnLike);
7470 }
7471 else
7472 {
7473 # endif
7474
7475 /* Cycle through divisions and recalculate tis and cond likes as necessary. */
7476 /* Code below does not try to avoid recalculating ti probs for divisions */
7477 /* that could share ti probs with other divisions. */
7478 for (d=0; d<numCurrentDivisions; d++)
7479 {
7480
7481 # if defined (BEST_MPI_ENABLED)
7482 if (isDivisionActive[d] == NO)
7483 continue;
7484 # endif
7485 m = &modelSettings[d];
7486 if (m->upDateCl == YES)
7487 {
7488 /* Work has been delegated to a separate function so we can wrap */
7489 /* a thread around it */
7490 LaunchLogLikeForDivision(chain, d, &(m->lnLike[2 * chain + state[chain]]));
7491 }
7492 if (abortMove == YES)
7493 return MRBFLT_NEG_MAX;
7494 chainLnLike += m->lnLike[2*chain + state[chain]];
7495 }
7496
7497 # if defined (BEAGLE_V3_ENABLED)
7498 }
7499 # endif
7500 #if defined (DEBUG_MB_BEAGLE_MULTIPART)
7501 printf("chainLnLike = %f\n", chainLnLike);
7502 #endif
7503
7504 /* unmark all divisions */
7505 if (chainHasAdgamma == YES)
7506 {
7507 for (d=0; d<numCurrentDivisions; d++)
7508 {
7509 m = &modelSettings[d];
7510 m->mark = NO;
7511 }
7512
7513 /* update HMM likelihoods if appropriate */
7514 for (d=0; d<numCurrentDivisions; d++)
7515 {
7516 # if defined (BEST_MPI_ENABLED)
7517 if (isDivisionActive[d] == NO)
7518 continue;
7519 # endif
7520 m = &modelSettings[d];
7521
7522 if (m->upDateCl == YES && m->correlation != NULL && m->mark != YES)
7523 {
7524 lnL = 0.0;
7525 CalcLikeAdgamma(d, m->correlation, chain, &lnL);
7526
7527 /* store the value for the cases where the HMM is not touched */
7528 m->lnLike[2*chain + state[chain]] = lnL;
7529
7530 /* add it to chainLnLike - it was not added above since the division */
7531 /* lnL was set to zero after the update call to Likelihood_Adgamma */
7532 chainLnLike += lnL;
7533
7534 /* set mark for other divisions in the HMM
7535 (i.e., those with the same correlation parameter AND the same shape parameter) */
7536 for (i=0; i<m->correlation->nRelParts; i++)
7537 {
7538 if (modelSettings[m->correlation->relParts[i]].shape == modelSettings[d].shape)
7539 {
7540 modelSettings[m->correlation->relParts[i]].mark = YES;
7541 }
7542 }
7543 }
7544 }
7545 }
7546
7547 return (chainLnLike);
7548 }
7549
7550
LogOmegaPrior(MrBFlt w1,MrBFlt w2,MrBFlt w3)7551 MrBFlt LogOmegaPrior (MrBFlt w1, MrBFlt w2, MrBFlt w3)
7552 {
7553
7554 /* This function returns the log prior probability of
7555 the ratio on three omegas. Here, we have three
7556 nonsynonymous/synonymous rate ratios, denoted w1, w2,
7557 and w3. They have the property that w1 < w2 < w3.
7558 Remember that w1 = dN1/dS, w2 = dN2/dS, and
7559 w3 = dN3/dS. We assume that dN1, dN2, dN3, and dS
7560 are all independent draws from the same exponential
7561 distribution, and that dN1, dN2, and dN3 are the
7562 order statistics. The w1, w2, and w3, then, are
7563 all scaled to the same dS r.v. */
7564
7565 MrBFlt lnProb;
7566
7567 lnProb = log(36.0) - 4.0 * log(1.0 + w1 + w2 + w3);
7568
7569 return (lnProb);
7570 }
7571
7572
7573 /* ------------------------------------------------------------------------------------------------------------- */
7574 /* Joint distribution of branch lengths t under gamma-Dirichlet prior: */
7575 /* (Zhang et al. 2012, Eq. 4; Rannala et al. 2012, Eq. 36): */
7576 /* ln[f(t|aT,bT,a,c)] = (aT - a*s - a*c*(s-3)) * ln(T) - bT * T + (a-1) * sum[ln(t_j)] + (a*c-1) * sum[ln(t_k)] */
7577 /* + aT * ln(bT) - lnG(aT) - lnB(a,c) */
7578 /* */
7579 /* Joint distribution of branch lengths t under invgamma-Dirichlet prior: */
7580 /* (Zhang et al. 2012, Eq. 6; Rannala et al. 2012, Eq. 39): */
7581 /* ln[f(t|aT,bT,a,c)] = (-aT - a*s - a*c*(s-3)) * ln(T) - bT / T + (a-1) * sum[ln(t_j)] + (a*c-1) * sum[ln(t_k)] */
7582 /* + aT * ln(bT) - lnG(aT) - lnB(a,c) */
7583 /* also see DoCitations() */
7584 /* ------------------------------------------------------------------------------------------------------------- */
7585
7586 /* external (tip): 1, internal: 0 */
7587 #define IsTip(Node) (Node->index < numTaxa || (Node->anc)->index < numTaxa)
7588
LogDirPrior(Tree * t,ModelParams * mp,int PV)7589 MrBFlt LogDirPrior (Tree *t, ModelParams *mp, int PV)
7590 {
7591 /* ln prior prob. under Dirichlet priors and twoExp prior
7592 //chi */
7593
7594 int i, nb[2] = {0,0};
7595 MrBFlt lnprior = 0.0, tb[2] = {0,0}, treeL = 0.0;
7596 MrBFlt aT, bT, a, c;
7597 TreeNode *p;
7598
7599 /* Not safe, should define Marcos. YES or NO should never be defined to 2 or 3 or 4! */
7600 /* PV is 2 or 3: Dirichlet priors */
7601 if (PV == 2 || PV == 3)
7602 {
7603 /* partially for calculating lnPriorRatio, full part is in LogPrior() */
7604 aT = mp->brlensDir[0];
7605 bT = mp->brlensDir[1];
7606 a = mp->brlensDir[2];
7607 c = mp->brlensDir[3];
7608
7609 for (i = 0; i < t->nNodes; i++)
7610 {
7611 p = t->allDownPass[i];
7612 if (p->anc != NULL)
7613 {
7614 treeL += p->length;
7615 nb[IsTip(p)]++;
7616 tb[IsTip(p)] += log(p->length);
7617 }
7618 }
7619 lnprior += (a-1)*tb[1] + (a*c -1)*tb[0];
7620 if (PV == 2)
7621 lnprior += (aT - a*nb[1] - a*c*nb[0]) * log(treeL) - bT*treeL;
7622 else
7623 lnprior += (-aT - a*nb[1] - a*c*nb[0]) * log(treeL) - bT/treeL;
7624 }
7625 /* or 4: twoExp prior */
7626 else if (PV == 4)
7627 {
7628 for (i = 0; i < t->nNodes; i++) {
7629 p = t->allDownPass[i];
7630 if (p->anc != NULL)
7631 {
7632 nb[IsTip(p)]++;
7633 tb[IsTip(p)] += p->length;
7634 }
7635 }
7636 for (i = 0; i < 2; i++)
7637 lnprior += nb[i] * log(mp->brlens2Exp[i]) - tb[i] * (mp->brlens2Exp[i]);
7638 }
7639
7640 return lnprior;
7641 }
7642
7643
LogPrior(int chain)7644 MrBFlt LogPrior (int chain)
7645 {
7646 int i, j, c, n, nStates, *nEvents, sumEvents, *ist, nRates, nParts[6];
7647 const int *rateCat;
7648 MrBFlt *st, *sst, lnPrior, sum, x, clockRate, theta, popSize, growth, *alphaDir, newProp[190],
7649 sF, *sR, *eR, *fR, freq, pInvar, lambda, sigma, nu, igrvar, **rateMultiplier;
7650 char *sS;
7651 CLFlt *nSitesOfPat;
7652 Param *p;
7653 ModelParams *mp;
7654 ModelInfo *m;
7655 Tree *t;
7656 TreeNode *branch, *q;
7657
7658 /* In the stepping-stone method, constants that appear in prior probability density functions must be fully specified.
7659 It is not necessary for Bayesian MCMC analyses because such constants cancel out. */
7660
7661 lnPrior = 0.0;
7662 for (n=0; n<numParams; n++)
7663 {
7664 p = ¶ms[n];
7665 # if defined (MPI_BEST_ENABLED)
7666 /* We skip all parameters that are not handled on this processor. The scheme used here
7667 requires that parameters either be unique to one partition (processor) or that they
7668 are shared across all partitions and that the first processor has all the relevant
7669 information about that parameter. */
7670 if (isDivisionActive[p->relParts[0]] == NO)
7671 continue;
7672 # endif
7673
7674 st = GetParamVals (p, chain, state[chain]);
7675 sst = GetParamSubVals (p, chain, state[chain]);
7676 mp = &modelParams[p->relParts[0]];
7677 m = &modelSettings[p->relParts[0]];
7678
7679 if (p->paramType == P_TRATIO)
7680 {
7681 /* tratio parameter */
7682 if (p->paramId == TRATIO_DIR)
7683 {
7684 alphaDir = mp->tRatioDir;
7685 /* we convert here from the ratio parameterization used in the parameter
7686 struct to the simplex parameterization used for the prior */
7687 newProp[0] = st[0] / (st[0] + 1.0);
7688 newProp[1] = (1.0 - newProp[0]);
7689 x = LnGamma(alphaDir[0]+alphaDir[1]) - LnGamma(alphaDir[0]) - LnGamma(alphaDir[1]);
7690 for (i=0; i<2; i++)
7691 x += (alphaDir[i]-1.0)*log(newProp[i]);
7692 lnPrior += x;
7693 }
7694 }
7695 else if (p->paramType == P_REVMAT)
7696 {
7697 /* revmat parameter */
7698 if (p->paramId == REVMAT_DIR)
7699 {
7700 if (p->nValues == 6)
7701 alphaDir = mp->revMatDir;
7702 else /* if (p->nValues == 190) */
7703 alphaDir = mp->aaRevMatDir;
7704 sum = 0.0;
7705 for (i=0; i<p->nValues; i++)
7706 sum += alphaDir[i];
7707 x = LnGamma(sum);
7708 for (i=0; i<p->nValues; i++)
7709 x -= LnGamma(alphaDir[i]);
7710 for (i=0; i<p->nValues; i++)
7711 x += (alphaDir[i] - 1.0) * log(st[i]);
7712 lnPrior += x;
7713 }
7714 else if (p->paramId == REVMAT_MIX)
7715 {
7716 assert (p->nValues == 6);
7717 alphaDir = &mp->revMatSymDir;
7718 ist = GetParamIntVals(p, chain, state[chain]); /* the growth fxn */
7719 nRates = GetKFromGrowthFxn(ist);
7720 /* get the actual rate proportions of the current groups */
7721 for (i=0; i<nRates; i++)
7722 {
7723 newProp[i] = 0.0;
7724 nParts[i] = 0;
7725 }
7726 for (i=0; i<6; i++)
7727 {
7728 nParts[ist[i]]++;
7729 newProp[ist[i]] += st[i];
7730 }
7731 /* now we calculate probability as usual, with alpha
7732 parameter multiplied by number of parts */
7733 x = LnGamma(6.0 * alphaDir[0]);
7734 for (i=0; i<nRates; i++)
7735 x -= LnGamma(nParts[i] * alphaDir[0]);
7736 for (i=0; i<nRates; i++)
7737 x += (nParts[i] * alphaDir[0] - 1.0) * log(newProp[i]);
7738 /* finally take model probability into account */
7739 x += log (1.0 / 203);
7740 lnPrior += x;
7741 }
7742 else
7743 {
7744 /* fixed or empirical */
7745 }
7746 }
7747 else if (p->paramType == P_OMEGA)
7748 {
7749 /* account for prior on omega proportion if 1 omega category */
7750 if (p->paramId == OMEGA_DIR)
7751 {
7752 alphaDir = mp->omegaDir;
7753 /* convert from ratio parameterization to simplex representation */
7754 newProp[0] = st[0] / (st[0] + 1.0);
7755 newProp[1] = 1.0 - newProp[0];
7756 x = LnGamma(alphaDir[0]+alphaDir[1]) - LnGamma(alphaDir[0]) - LnGamma(alphaDir[1]);
7757 for (i=0; i<2; i++)
7758 x += (alphaDir[i]-1.0)*log(newProp[i]);
7759 lnPrior += x;
7760 }
7761 /* account for stationary state frequencies of M3 and ny98 omega categories */
7762 if (p->paramId == OMEGA_BUD || p->paramId == OMEGA_BED ||
7763 p->paramId == OMEGA_BFD || p->paramId == OMEGA_FUD ||
7764 p->paramId == OMEGA_FED || p->paramId == OMEGA_FFD ||
7765 p->paramId == OMEGA_ED || p->paramId == OMEGA_FD)
7766 {
7767 alphaDir = mp->codonCatDir;
7768 x = 0.0;
7769 for (i=0; i<3; i++)
7770 x += (alphaDir[i]-1.0)*log(sst[i]);
7771 lnPrior += x;
7772 }
7773 /* account for beta prior on omeganeg in NY98 */
7774 if (p->paramId == OMEGA_BUD || p->paramId == OMEGA_BUF ||
7775 p->paramId == OMEGA_BED || p->paramId == OMEGA_BEF ||
7776 p->paramId == OMEGA_BFD || p->paramId == OMEGA_BFF)
7777 {
7778 alphaDir = mp->ny98omega1Beta;
7779 newProp[0] = st[0] / (st[0] + 1.0);
7780 newProp[1] = 1.0 - newProp[0];
7781 x = 0.0;
7782 for (i=0; i<2; i++)
7783 x += (alphaDir[i]-1.0)*log(newProp[i]);
7784 lnPrior += x;
7785 }
7786 /* account for omegapos in NY98 with uniform prior prob */
7787 if (p->paramId == OMEGA_BUD || p->paramId == OMEGA_BUF ||
7788 p->paramId == OMEGA_FUD || p->paramId == OMEGA_FUF)
7789 {
7790 lnPrior += log(1.0) - log(mp->ny98omega3Uni[1] - mp->ny98omega3Uni[0]);
7791 }
7792 /* account for omegapos in NY98 with exponential prior prob */
7793 if (p->paramId == OMEGA_BED || p->paramId == OMEGA_BEF ||
7794 p->paramId == OMEGA_FED || p->paramId == OMEGA_FEF)
7795 {
7796 lnPrior += (log(mp->ny98omega3Exp) - mp->ny98omega3Exp * st[2]);
7797 }
7798 /* account for omegas in M3, which can only be exponential; if fixed, ln prior prob is 0 */
7799 if (p->paramId == OMEGA_EF || p->paramId == OMEGA_ED)
7800 {
7801 lnPrior += LogOmegaPrior (st[0], st[1], st[2]);
7802 }
7803 }
7804 else if (p->paramType == P_PI)
7805 {
7806 /* state frequencies parameter */
7807 if (p->paramId == PI_DIR)
7808 {
7809 nStates = p->nSubValues;
7810 sum = 0.0;
7811 for (i=0; i<nStates; i++)
7812 sum += st[i];
7813 x = LnGamma(sum);
7814 for (i=0; i<nStates; i++)
7815 x -= LnGamma(st[i]);
7816 for (i=0; i<nStates; i++)
7817 x += (st[i] - 1.0)*log(sst[i]);
7818 lnPrior += x;
7819 }
7820 else if (p->paramId == SYMPI_EXP || p->paramId == SYMPI_EXP_MS)
7821 {
7822 lnPrior += - mp->symBetaExp * st[0] + log(mp->symBetaExp);
7823 }
7824 else if (p->paramId == SYMPI_UNI || p->paramId == SYMPI_UNI_MS)
7825 {
7826 lnPrior += log(1.0) - log(mp->symBetaUni[1] - mp->symBetaUni[0]);
7827 }
7828 if (p->paramId == SYMPI_EXP_MS || p->paramId == SYMPI_UNI_MS || p->paramId == SYMPI_FIX_MS)
7829 {
7830 sst = GetParamStdStateFreqs(p, chain, state[chain]);
7831 sst += 2*m->numBetaCats;
7832 for (i=0; i<p->nSympi; i++)
7833 {
7834 nStates = p->sympinStates[i];
7835 x = LnGamma(nStates*st[0]) - nStates*LnGamma(st[0]);
7836 for (j=0; j<nStates; j++)
7837 x += (st[0] - 1.0)*log(sst[j]);
7838 lnPrior += x;
7839 sst += nStates;
7840 }
7841 }
7842 }
7843 else if (p->paramType == P_MIXTURE_RATES)
7844 {
7845 /* site rate mixture; first deal with dirichlet prior */
7846 nStates = p->nSubValues;
7847 sum = 0.0;
7848 for (i=0; i<nStates; i++)
7849 sum += st[i];
7850 x = LnGamma(sum);
7851 for (i=0; i<nStates; i++)
7852 x -= LnGamma(st[i]);
7853 for (i=0; i<nStates; i++)
7854 x += (st[i] - 1.0)*log(sst[i]/nStates);
7855
7856 /* now deal with the order statistic; we multiply the density by the number of orderings */
7857 x += LnFactorial(nStates);
7858
7859 lnPrior += x;
7860 }
7861 else if (p->paramType == P_SHAPE)
7862 {
7863 /* gamma/lnorm shape parameter */
7864 if (p->paramId == SHAPE_UNI)
7865 {
7866 lnPrior += log(1.0) - log(mp->shapeUni[1] - mp->shapeUni[0]);
7867 }
7868 else if (p->paramId == SHAPE_EXP)
7869 {
7870 lnPrior += log(mp->shapeExp) - mp->shapeExp * st[0];
7871 }
7872 for (i=0; i<p->nRelParts; i++)
7873 {
7874 m = &modelSettings[p->relParts[i]];
7875 if (m->gibbsGamma == YES)
7876 {
7877 if (m->pInvar == NULL)
7878 lnPrior += log(1.0/m->numRateCats) * m->numUncompressedChars;
7879 else
7880 {
7881 rateCat = m->tiIndex + chain * m->numChars;
7882 pInvar = *GetParamVals (m->pInvar, chain, state[chain]);
7883 nSitesOfPat = numSitesOfPat + m->compCharStart;
7884 freq = (1.0 - pInvar)/m->numRateCats;
7885 for (c=0; c<m->numChars; c++)
7886 {
7887 if (rateCat[c] < m->numRateCats)
7888 lnPrior += log(freq) * nSitesOfPat[c];
7889 else
7890 lnPrior += log(pInvar) * nSitesOfPat[c];
7891 }
7892 }
7893 }
7894 }
7895 }
7896 else if (p->paramType == P_PINVAR)
7897 {
7898 /* proportion of invariable sites parameter */
7899 lnPrior += log(1.0) - log(mp->pInvarUni[1] - mp->pInvarUni[0]);
7900 }
7901 else if (p->paramType == P_CORREL)
7902 {
7903 /* adGamma model parameter */
7904 lnPrior += log(1.0) - log(mp->corrUni[1] - mp->corrUni[0]);
7905 }
7906 else if (p->paramType == P_SWITCH)
7907 {
7908 /* switching rate parameter of covarion model */
7909 if (p->paramId == SWITCH_UNI)
7910 {
7911 lnPrior += log(1.0) - log(mp->covswitchUni[1] - mp->covswitchUni[0]);
7912 }
7913 else if (p->paramId == SWITCH_EXP)
7914 {
7915 lnPrior += log(mp->covswitchExp) - mp->covswitchExp * st[0];
7916 }
7917 }
7918 else if (p->paramType == P_RATEMULT && p->nValues > 1)
7919 {
7920 nStates = p->nValues;
7921 sum = 0.0;
7922 for (i=0; i<nStates; i++)
7923 sum += sst[i+nStates];
7924 x = LnGamma(sum);
7925 for (i=0; i<nStates; i++)
7926 x -= LnGamma(sst[i+nStates]);
7927 for (i=0; i<nStates; i++)
7928 x += (sst[i+nStates] - 1.0) * log(st[i]);
7929 sum = 0.0; // the constant
7930 for (i=0; i<nStates; i++)
7931 sum += sst[i];
7932 for (i=0; i<nStates; i++) {
7933 if (i < nStates-1)
7934 x += (sst[i+nStates]) * log(sst[i]/sum);
7935 else
7936 x += (sst[i+nStates] - 1.0) * log(sst[i]/sum);
7937 }
7938 lnPrior += x;
7939 }
7940 else if (p->paramType == P_GENETREERATE && p->nValues > 1)
7941 {
7942 nStates = p->nValues;
7943 sum = 0.0;
7944 for (i=0; i<nStates; i++)
7945 sum += sst[i+nStates];
7946 x = LnGamma(sum);
7947 for (i=0; i<nStates; i++)
7948 x -= LnGamma(sst[i+nStates]);
7949 for (i=0; i<nStates; i++)
7950 x += (sst[i+nStates] - 1.0) * log(st[i]);
7951 sum = 0.0; // the constant
7952 for (i=0; i<nStates; i++)
7953 sum += sst[i];
7954 for (i=0; i<nStates; i++) {
7955 if (i < nStates-1)
7956 x += (sst[i+nStates]) * log(sst[i]/sum);
7957 else
7958 x += (sst[i+nStates] - 1.0) * log(sst[i]/sum);
7959 }
7960 lnPrior += x;
7961 }
7962 else if (p->paramType == P_TOPOLOGY)
7963 {
7964 // Note that a topology can have several unlinked branch length subparameters but only
7965 // one set of clock branch lengths. To find all the branch length subparameters of a
7966 // topology, cycle through the p->subParams, which will contain at least one branch length
7967 // parameter.
7968 t = GetTree (p, chain, state[chain]);
7969 if (t->isClock == YES)
7970 continue; /* prior probability taken care of in the brlens parameter */
7971 if (t->nLocks > 0)
7972 {
7973 for (i=0; i<t->nNodes-1; i++)
7974 {
7975 branch = t->allDownPass[i];
7976 if (branch->left == NULL)
7977 branch->x = 1;
7978 else
7979 branch->x = branch->left->x + branch->right->x;
7980 if (branch->isLocked == YES || branch->anc->anc == NULL)
7981 {
7982 for (j = 2*branch->x - 3; j>=1; j-=2)
7983 {
7984 lnPrior -= log ((MrBFlt)j);
7985 }
7986 branch->x = 1;
7987 }
7988 }
7989 }
7990 else
7991 {
7992 for (j = 2*(t->nNodes-t->nIntNodes)-5; j>=1; j-=2)
7993 {
7994 lnPrior -= log ((MrBFlt)j);
7995 }
7996 }
7997 }
7998 else if (p->paramType == P_BRLENS)
7999 {
8000 /* branch lengths */
8001 t = GetTree (p, chain, state[chain]);
8002 if (t->isClock == YES)
8003 {
8004 if (p->paramId == BRLENS_CLOCK_UNI)
8005 {
8006 /* uniformly distributed branch lengths */
8007 clockRate = *(GetParamVals (m->clockRate, chain, state[chain]));
8008 lnPrior += LnUniformPriorPr(t, clockRate);
8009 }
8010 else if (p->paramId == BRLENS_CLOCK_COAL)
8011 {
8012 /* coalescence prior */
8013 popSize = *(GetParamVals (m->popSize, chain, state[chain]));
8014 clockRate = *(GetParamVals (m->clockRate, chain, state[chain]));
8015 if (strcmp(mp->ploidy, "Diploid") == 0)
8016 theta = 4.0 * popSize * clockRate;
8017 else if (strcmp(mp->ploidy, "Zlinked") == 0)
8018 theta = 3.0 * popSize * clockRate;
8019 else
8020 theta = 2.0 * popSize * clockRate;
8021 if (!strcmp(mp->growthPr, "Fixed"))
8022 growth = mp->growthFix;
8023 else
8024 growth = *(GetParamVals (m->growthRate, chain, state[chain]));
8025 if (LnCoalescencePriorPr (t, &x, theta, growth) == ERROR)
8026 {
8027 MrBayesPrint ("%s Problem calculating prior for coalescence process\n", spacer);
8028 }
8029 lnPrior += x;
8030 }
8031 else if (p->paramId == BRLENS_CLOCK_BD)
8032 {
8033 /* birth-death prior */
8034 sR = GetParamVals (m->speciationRates, chain, state[chain]);
8035 eR = GetParamVals (m->extinctionRates, chain, state[chain]);
8036 sS = mp->sampleStrat;
8037 sF = mp->sampleProb;
8038 if (m->clockRate != NULL)
8039 clockRate = *(GetParamVals (m->clockRate, chain, state[chain]));
8040 else
8041 clockRate = 1.0;
8042 if (LnBirthDeathPriorPr (t, clockRate, &x, *sR, *eR, sS, sF) == ERROR)
8043 {
8044 MrBayesPrint ("%s Problem calculating prior for birth-death process\n", spacer);
8045 }
8046 lnPrior += x;
8047 }
8048 else if (p->paramId == BRLENS_CLOCK_FOSSIL)
8049 {
8050 /* fossilized birth-death prior */
8051 sR = GetParamVals (m->speciationRates, chain, state[chain]);
8052 eR = GetParamVals (m->extinctionRates, chain, state[chain]);
8053 fR = GetParamVals (m->fossilizationRates, chain, state[chain]);
8054 sF = mp->sampleProb;
8055 sS = mp->sampleStrat;
8056 if (m->clockRate != NULL)
8057 clockRate = *(GetParamVals (m->clockRate, chain, state[chain]));
8058 else
8059 clockRate = 1.0;
8060 if (LnFossilizationPriorPr (t, clockRate, &x, sR, eR, fR, sF, sS) == ERROR)
8061 {
8062 MrBayesPrint ("%s Problem calculating prior for fossilized birth-death process\n", spacer);
8063 }
8064 lnPrior += x; /* FIXME: x undefined? (from clang static analyzer) */
8065 }
8066 else if (p->paramId == BRLENS_CLOCK_SPCOAL)
8067 {
8068 /* delegate this calculation to the P_SPECIESTREE parameter */
8069 }
8070 if (t->isCalibrated == YES)
8071 {
8072 /* take care of calibrations */
8073 for (i=0; i<t->nNodes-1; i++)
8074 {
8075 q = t->allDownPass[i];
8076 if (q->isDated == YES && q->calibration->prior != fixed)
8077 {
8078 lnPrior += q->calibration->LnPriorProb(q->age, q->calibration->priorParams);
8079 }
8080 }
8081 }
8082 }
8083 else
8084 {
8085 if (p->paramId == BRLENS_UNI)
8086 {
8087 for (i=0; i<t->nNodes; i++)
8088 {
8089 branch = t->allDownPass[i];
8090 if (branch->anc != NULL)
8091 lnPrior += log(1.0) - log(mp->brlensUni[1] - BRLENS_MIN);
8092 }
8093 }
8094 else if (p->paramId == BRLENS_EXP)
8095 {
8096 for (i=0; i<t->nNodes; i++)
8097 {
8098 branch = t->allDownPass[i];
8099 if (branch->anc != NULL)
8100 lnPrior += log(mp->brlensExp) - mp->brlensExp * branch->length;
8101 }
8102 }
8103 /* Dirichlet priors */
8104 else if (p->paramId == BRLENS_GamDir)
8105 {
8106 lnPrior += LogDirPrior(t, mp, 2);
8107 lnPrior += (mp->brlensDir[0]) * log(mp->brlensDir[1]) - LnGamma(mp->brlensDir[0])
8108 + LnGamma (mp->brlensDir[2] * numTaxa + mp->brlensDir[2] * mp->brlensDir[3] * (numTaxa-3))
8109 - numTaxa * LnGamma(mp->brlensDir[2]) - (numTaxa-3) * LnGamma(mp->brlensDir[2] * mp->brlensDir[3]);
8110 }
8111 else if (p->paramId == BRLENS_iGmDir)
8112 {
8113 lnPrior += LogDirPrior(t, mp, 3);
8114 lnPrior += (mp->brlensDir[0]) * log(mp->brlensDir[1]) - LnGamma(mp->brlensDir[0])
8115 + LnGamma (mp->brlensDir[2] * numTaxa + mp->brlensDir[2] * mp->brlensDir[3] * (numTaxa-3))
8116 - numTaxa * LnGamma(mp->brlensDir[2]) - (numTaxa-3) * LnGamma(mp->brlensDir[2] * mp->brlensDir[3]);
8117 }
8118 /* twoExp prior */
8119 else if (p->paramId == BRLENS_twoExp)
8120 lnPrior += LogDirPrior(t, mp, 4);
8121 }
8122 }
8123 else if (p->paramType == P_SPECRATE)
8124 {
8125 /* speciation rate parameter */
8126 if (p->paramId == SPECRATE_UNI)
8127 {
8128 for (i=0; i<p->nValues; i++)
8129 lnPrior += log(1.0) - log(mp->speciationUni[1] - mp->speciationUni[0]);
8130 }
8131 else if (p->paramId == SPECRATE_EXP)
8132 {
8133 for (i=0; i<p->nValues; i++)
8134 lnPrior += log(mp->speciationExp) - mp->speciationExp * st[i];
8135 }
8136 }
8137 else if (p->paramType == P_EXTRATE)
8138 {
8139 /* extinction rate parameter */
8140 if (p->paramId == EXTRATE_BETA)
8141 {
8142 for (i=0; i<p->nValues; i++)
8143 {
8144 alphaDir = mp->extinctionBeta;
8145 newProp[0] = st[i];
8146 newProp[1] = (1.0 - newProp[0]);
8147 lnPrior += LnGamma(alphaDir[0]+alphaDir[1]) - LnGamma(alphaDir[0]) - LnGamma(alphaDir[1]);
8148 lnPrior += (alphaDir[0]-1.0)*log(newProp[0]) + (alphaDir[1]-1.0)*log(newProp[1]);
8149 }
8150 }
8151 }
8152 else if (p->paramType == P_FOSLRATE)
8153 {
8154 /* fossilization rate parameter */
8155 if (p->paramId == FOSLRATE_BETA)
8156 {
8157 for (i=0; i<p->nValues; i++)
8158 {
8159 alphaDir = mp->fossilizationBeta;
8160 newProp[0] = st[i];
8161 newProp[1] = (1.0 - newProp[0]);
8162 // if (newProp[0] > 0.0) /* to avoid psi=0 in [0, x_cut] under diversified sampling */
8163 lnPrior += LnGamma(alphaDir[0]+alphaDir[1]) - LnGamma(alphaDir[0]) - LnGamma(alphaDir[1]);
8164 lnPrior += (alphaDir[0]-1.0)*log(newProp[0]) + (alphaDir[1]-1.0)*log(newProp[1]);
8165 }
8166 }
8167 }
8168 else if (p->paramType == P_POPSIZE)
8169 {
8170 /* neutral coalescence population size parameter; one value, or one value per branch of species tree */
8171 for (i=0; i<p->nValues; i++)
8172 {
8173 lnPrior += p->LnPriorProb(st[i], p->priorParams);
8174 }
8175 }
8176 else if (p->paramType == P_AAMODEL)
8177 {
8178 lnPrior += sst[(int)st[0]];
8179 }
8180 else if (p->paramType == P_BRCORR)
8181 {
8182
8183 }
8184 else if (p->paramType == P_BRSIGMA)
8185 {
8186
8187 }
8188 else if (p->paramType == P_GROWTH)
8189 {
8190 /* population growth parameter */
8191 if (p->paramId == GROWTH_UNI)
8192 {
8193 lnPrior += log(1.0) - log(mp->growthUni[1] - mp->growthUni[0]);
8194 }
8195 else if (p->paramId == GROWTH_EXP)
8196 {
8197 lnPrior += log(mp->growthExp) - mp->growthExp * st[0];
8198 }
8199 }
8200 else if (p->paramType == P_CPPRATE)
8201 {
8202 /* rate (lambda) of comp poisson process of relaxed clock */
8203 if (p->paramId == CPPRATE_EXP)
8204 {
8205 lnPrior += log (mp->cppRateExp) - mp->cppRateExp * st[0];
8206 }
8207 }
8208 else if (p->paramType == P_CPPMULTDEV)
8209 {
8210 /* standard deviation (log) of lognormal distribution of rate multipliers for cpp relaxed clock */
8211 /* only fixed value allowed currently */
8212 }
8213 else if (p->paramType == P_CPPEVENTS)
8214 {
8215 /* events of CPP relaxed clock process */
8216 lambda = *GetParamVals (m->cppRate, chain, state[chain]);
8217 sigma = *GetParamVals (m->cppMultDev, chain, state[chain]);
8218 nEvents = p->nEvents[2*chain+state[chain]];
8219 rateMultiplier = p->rateMult[2*chain+state[chain]];
8220 /* cpp events */
8221 sumEvents = 0;
8222 for (i=0; i<2*numLocalTaxa-2; i++)
8223 sumEvents += nEvents[i];
8224 t = GetTree (p, chain, state[chain]);
8225 lnPrior += - lambda * TreeLength (p, chain) + (sumEvents * log (lambda));
8226 /* rate multipliers */
8227 for (i=0; i<2*numLocalTaxa-2; i++)
8228 {
8229 for (j=0; j<nEvents[i]; j++)
8230 lnPrior += LnProbLogNormal (0.0, sigma, rateMultiplier[i][j]);
8231 }
8232 for (i=0; i<t->nNodes-2; i++)
8233 {
8234 branch = t->allDownPass[i];
8235 assert (fabs(branch->length - (branch->anc->nodeDepth - branch->nodeDepth)) < 0.000001);
8236 }
8237 }
8238 else if (p->paramType == P_TK02VAR)
8239 {
8240 /* variance of rates (nu) in Thorne-Kishino model */
8241 if (p->paramId == TK02VAR_EXP)
8242 {
8243 lnPrior += log (mp->tk02varExp) - mp->tk02varExp * st[0];
8244 }
8245 else if (p->paramId == TK02VAR_UNI)
8246 {
8247 lnPrior += log(1.0) - log (mp->tk02varUni[1] - mp->tk02varUni[0]);
8248 }
8249 }
8250 else if (p->paramType == P_TK02BRANCHRATES || (p->paramType == P_MIXEDBRCHRATES && *GetParamIntVals(p, chain, state[chain]) == RCL_TK02))
8251 {
8252 /* branch rates of Thorne-Kishino model */
8253 t = GetTree (p, chain, state[chain]);
8254 if (p->paramType == P_TK02BRANCHRATES)
8255 nu = *GetParamVals (m->tk02var, chain, state[chain]);
8256 else
8257 nu = *GetParamVals (m->mixedvar, chain, state[chain]);
8258 for (i=0; i<t->nNodes-2; i++)
8259 {
8260 branch = t->allDownPass[i];
8261 if (branch->length > 0.0) // not ancestral fossil
8262 lnPrior += LnProbTK02LogNormal (st[branch->anc->index], nu*branch->length, st[branch->index]);
8263 }
8264 }
8265 else if (p->paramType == P_IGRVAR)
8266 {
8267 /* variance of rates in independent gamma rates model */
8268 if (p->paramId == IGRVAR_EXP)
8269 {
8270 lnPrior += log (mp->igrvarExp) - mp->igrvarExp * st[0];
8271 }
8272 else if (p->paramId == IGRVAR_UNI)
8273 {
8274 lnPrior += log(1.0) - log (mp->igrvarUni[1] - mp->igrvarUni[0]);
8275 }
8276 }
8277 else if (p->paramType == P_IGRBRANCHRATES || (p->paramType == P_MIXEDBRCHRATES && *GetParamIntVals(p, chain, state[chain]) == RCL_IGR))
8278 {
8279 /* branch rates of independent branch rate model */
8280 t = GetTree (p, chain, state[chain]);
8281 if (p->paramType == P_IGRBRANCHRATES)
8282 igrvar = *GetParamVals (m->igrvar, chain, state[chain]);
8283 else
8284 igrvar = *GetParamVals (m->mixedvar, chain, state[chain]);
8285 for (i=0; i<t->nNodes-2; i++)
8286 {
8287 branch = t->allDownPass[i];
8288 if (branch->length > 0.0) // not ancestral fossil
8289 lnPrior += LnProbGamma (branch->length/igrvar, branch->length/igrvar, st[branch->index]);
8290 assert (fabs(sst[branch->index] - branch->length * st[branch->index]) < BRLENS_MIN);
8291 assert (fabs(branch->length - (branch->anc->nodeDepth - branch->nodeDepth)) < BRLENS_MIN);
8292 }
8293 }
8294 else if (p->paramType == P_MIXEDVAR)
8295 {
8296 /* hyper prior of rates in mixed rel clock model */
8297 if (p->paramId == MIXEDVAR_EXP)
8298 {
8299 lnPrior += log (mp->mixedvarExp) - mp->mixedvarExp * st[0];
8300 }
8301 else if (p->paramId == MIXEDVAR_UNI)
8302 {
8303 lnPrior += log(1.0) - log (mp->mixedvarUni[1] - mp->mixedvarUni[0]);
8304 }
8305 }
8306 else if (p->paramType == P_CLOCKRATE)
8307 {
8308 /* base rate of molecular clock */
8309 lnPrior += p->LnPriorProb(st[0], p->priorParams);
8310 }
8311 else if (p->paramType == P_SPECIESTREE)
8312 {
8313 /* calculate prior */
8314 lnPrior += LnSpeciesTreeProb(chain);
8315 }
8316 }
8317 assert (lnPrior == lnPrior);
8318
8319 # if defined (BEST_MPI_ENABLED)
8320 /* Assemble prior probabilities across processors */
8321 myLnPrior = lnPrior;
8322 MPI_AllReduce (&myLnPrior, &lnPrior, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
8323 # endif
8324
8325 return (lnPrior);
8326 }
8327
8328
LnBirthDeathPriorPr(Tree * t,MrBFlt clockRate,MrBFlt * prob,MrBFlt sR,MrBFlt eR,char * sS,MrBFlt sF)8329 int LnBirthDeathPriorPr (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt sR, MrBFlt eR, char *sS, MrBFlt sF)
8330 {
8331 if (!strcmp(sS, "Random"))
8332 {
8333 return LnBirthDeathPriorPrRandom (t, clockRate, prob, sR, eR, sF);
8334 }
8335 else if (!strcmp(sS, "Diversity"))
8336 {
8337 return LnBirthDeathPriorPrDiversity (t, clockRate, prob, sR, eR, sF);
8338 }
8339 else if (!strcmp(sS, "Cluster"))
8340 {
8341 return LnBirthDeathPriorPrCluster (t, clockRate, prob, sR, eR, sF);
8342 }
8343 else
8344 {
8345 MrBayesPrint ("%s ERROR: Sampling strategy for birth-death process not implemented\n", spacer);
8346 return (ERROR);
8347 }
8348 }
8349
8350
8351 /*---------------------------------------------------------------------------------
8352 |
8353 | LnBirthDeathPriorPrRandom
8354 |
8355 | t_4 (age of tips)
8356 | \ \ \ /
8357 | \ \ \ /
8358 | \ \ \ /
8359 | \ \ \ /
8360 | \ \ \/ t_3
8361 | \ \ /
8362 | \ \ /
8363 | \ \ /
8364 | \ \ /
8365 | \ \/ t_2
8366 | \ /
8367 | \ /
8368 | \ /
8369 | \ /
8370 | \/ t_1 (age of most recent common ancestor)
8371 |
8372 |
8373 | This function calculates the probability of a rooted tree under the neutral
8374 | birth death prior with constant birth and death rates, conditioned on t_1,
8375 | the most recent common ancestor. We assume rho-sampling, that is, a constant
8376 | sampling probability rho on extant taxa, which is known.
8377 |
8378 | T: the unlabeled oriented tree, which is equivalent to a set of unordered
8379 | speciation times from a point process
8380 | tau: the labeled unoriented tree
8381 | b: birth (speciation) rate
8382 | d: death (extintion) rate
8383 | f: sampling fraction
8384 | n: number of (extant) taxa in the sampled tree
8385 |
8386 | Eq.3 * Eq.A0 in Hohna et al. 2011 MBE
8387 |
8388 ---------------------------------------------------------------------------------*/
LnBirthDeathPriorPrRandom(Tree * t,MrBFlt clockRate,MrBFlt * prob,MrBFlt sR,MrBFlt eR,MrBFlt sF)8389 int LnBirthDeathPriorPrRandom (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt sR, MrBFlt eR, MrBFlt sF)
8390 {
8391 int i, n, m;
8392 MrBFlt *nt, lambda, mu, tmrca;
8393 TreeNode *p;
8394 Model *mp;
8395
8396 /* transform to standard variables */
8397 lambda = sR / (1.0 - eR);
8398 mu = eR * lambda;
8399
8400 n = t->nIntNodes+1;
8401 m = (int)floor(n/sF+0.5); /* equal to round(n/sF) plus it is compatible with MS Visual Studio */
8402
8403 if (AreDoublesEqual(lambda, mu, ETA) == YES)
8404 {
8405 MrBayesPrint ("\n ERROR: Critical branching process for random sampling not implemented\n");
8406 return (ERROR);
8407 }
8408
8409 /* allocate space for the speciation times */
8410 nt = (MrBFlt *)SafeMalloc((size_t)(t->nIntNodes) * sizeof(MrBFlt));
8411 if (!nt)
8412 {
8413 MrBayesPrint ("\n ERROR: Problem allocating nt\n");
8414 return (ERROR);
8415 }
8416
8417 /* get the node times and put them into a vector */
8418 for (i=0; i<t->nIntNodes; i++)
8419 {
8420 p = t->intDownPass[i];
8421 nt[i] = p->nodeDepth / clockRate;
8422 }
8423 /* time of most recent common ancestor */
8424 tmrca = nt[t->nIntNodes-1]; // t->root->left->nodeDepth / clockRate
8425
8426 /* calculate probability of tree using standard variables */
8427 (*prob) = log(m-1) - log(n-1); // constant
8428 (*prob) += (m-2.0) * (LnP0(tmrca, lambda, mu) + log(lambda) - log(mu));
8429 (*prob) += 2.0 * (LnP1(tmrca, lambda, mu) - log(1.0 - exp(LnP0(tmrca, lambda, mu))));
8430 (*prob) += (n-2.0) * (log(lambda*sF + (lambda-lambda*sF-mu)*exp((mu-lambda)*tmrca)) - log(sF*(1-exp((mu-lambda)*tmrca))));
8431 for (i=0; i<t->nIntNodes-1; i++)
8432 (*prob) += LnP1Subsample(nt[i], lambda, mu, sF);
8433
8434 /* conversion to labeled tree from oriented tree */
8435 // (*prob) += (n-1.0)*log(2.0) - LnFactorial(n); // constant
8436
8437 /* condition on tmrca, calibrations are dealt with separately */
8438 mp = &modelParams[t->relParts[0]];
8439 if (t->root->left->isDated == NO)
8440 (*prob) += mp->treeAgePr.LnPriorProb(tmrca, mp->treeAgePr.priorParams);
8441
8442 /* free memory */
8443 free (nt);
8444
8445 return (NO_ERROR);
8446 }
8447
8448
8449 /*---------------------------------------------------------------------------------
8450 |
8451 | LnBirthDeathPriorPrDiversity
8452 |
8453 | Eq.A1 in Hohna et al. 2011 MBE
8454 |
8455 ---------------------------------------------------------------------------------*/
LnBirthDeathPriorPrDiversity(Tree * t,MrBFlt clockRate,MrBFlt * prob,MrBFlt sR,MrBFlt eR,MrBFlt sF)8456 int LnBirthDeathPriorPrDiversity (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt sR, MrBFlt eR, MrBFlt sF)
8457 {
8458 int i, n, m;
8459 MrBFlt *nt, lambda, mu, nt_min, tmrca, ln_p0_t1;
8460 TreeNode *p;
8461 Model *mp;
8462
8463 /* transform to standard variables */
8464 lambda = sR / (1.0 - eR);
8465 mu = eR * lambda;
8466
8467 n = t->nIntNodes+1;
8468 m = (int)floor(n/sF+0.5); /* equal to round(n/sF) plus it is compatible with MS Visual Studio */
8469
8470 if (AreDoublesEqual(lambda, mu, ETA) == YES)
8471 {
8472 MrBayesPrint ("\n ERROR: Critical branching process for diversity sampling not implemented\n");
8473 return (ERROR);
8474 }
8475
8476 /* allocate space for the speciation times */
8477 nt = (MrBFlt *)SafeMalloc((size_t)(t->nIntNodes) * sizeof(MrBFlt));
8478 if (!nt)
8479 {
8480 MrBayesPrint ("\n ERROR: Problem allocating nt\n");
8481 return (ERROR);
8482 }
8483
8484 /* get the node times and put them into a vector */
8485 for (i=0; i<t->nIntNodes; i++)
8486 {
8487 p = t->intDownPass[i];
8488 nt[i] = p->nodeDepth / clockRate;
8489 }
8490 /* time of most recent common ancestor */
8491 tmrca = nt[t->nIntNodes-1]; // t->root->left->nodeDepth / clockRate
8492
8493 /* find the youngest interal node */
8494 nt_min = nt[0];
8495 for (i=0; i<t->nIntNodes-1; i++)
8496 {
8497 if (nt_min > nt[i])
8498 nt_min = nt[i];
8499 }
8500
8501 ln_p0_t1 = LnP0(tmrca, lambda, mu);
8502 (*prob) = 0; // LnFactorial(m-1) - LnFactorial(n-1) - LnFactorial(m-n); // constant
8503 (*prob) += (m-2.0) * (ln_p0_t1 + log(lambda)) + (n-m)*log(mu);
8504 (*prob) += 2.0 * (LnP1(tmrca, lambda, mu) - log(1.0 - exp(ln_p0_t1)));
8505 (*prob) += (m-n) * (LnP0(nt_min, lambda, mu) - ln_p0_t1);
8506 for (i=0; i<t->nIntNodes-1; i++)
8507 (*prob) += LnP1(nt[i], lambda, mu) - ln_p0_t1;
8508
8509 /* conversion to labeled tree from oriented tree */
8510 // (*prob) += (n-1.0)*log(2.0) - LnFactorial(n); // constant
8511
8512 /* condition on tmrca, calibrations are dealt with separately */
8513 mp = &modelParams[t->relParts[0]];
8514 if (t->root->left->isDated == NO)
8515 (*prob) += mp->treeAgePr.LnPriorProb(tmrca, mp->treeAgePr.priorParams);
8516
8517 /* free memory */
8518 free (nt);
8519
8520 return (NO_ERROR);
8521 }
8522
8523
8524 /*---------------------------------------------------------------------------------
8525 |
8526 | LnBirthDeathPriorPrCluster
8527 |
8528 | Eq.A2 in Hohna et al. 2011 MBE
8529 |
8530 ---------------------------------------------------------------------------------*/
LnBirthDeathPriorPrCluster(Tree * t,MrBFlt clockRate,MrBFlt * prob,MrBFlt sR,MrBFlt eR,MrBFlt sF)8531 int LnBirthDeathPriorPrCluster (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt sR, MrBFlt eR, MrBFlt sF)
8532 {
8533 int i, n, m;
8534 MrBFlt *nt, lambda, mu, nt_2, tmrca, ln_p0_t1;
8535 TreeNode *p;
8536 Model *mp;
8537
8538 /* transform to standard variables */
8539 lambda = sR / (1.0 - eR);
8540 mu = eR * lambda;
8541
8542 n = t->nIntNodes+1;
8543 m = (int)floor(n/sF+0.5); /* equal to round(n/sF) plus it is compatible with MS Visual Studio */
8544
8545 if (AreDoublesEqual(lambda, mu, ETA) == YES)
8546 {
8547 MrBayesPrint ("\n ERROR: Critical branching process for cluster sampling not implemented\n");
8548 return (ERROR);
8549 }
8550
8551 /* allocate space for the speciation times */
8552 nt = (MrBFlt *)SafeMalloc((size_t)(t->nIntNodes) * sizeof(MrBFlt));
8553 if (!nt)
8554 {
8555 MrBayesPrint ("\n ERROR: Problem allocating nt\n");
8556 return (ERROR);
8557 }
8558
8559 /* get the node times and put them into a vector */
8560 for (i=0; i<t->nIntNodes; i++)
8561 {
8562 p = t->intDownPass[i];
8563 nt[i] = p->nodeDepth / clockRate;
8564 }
8565 /* time of most recent common ancestor */
8566 tmrca = nt[t->nIntNodes-1]; // t->root->left->nodeDepth / clockRate
8567
8568 /* find the second oldest interal node */
8569 nt_2 = nt[0];
8570 for (i=0; i<t->nIntNodes-1; i++)
8571 {
8572 if (nt_2 < nt[i])
8573 nt_2 = nt[i];
8574 }
8575
8576 ln_p0_t1 = LnP0(tmrca, lambda, mu);
8577 (*prob) = 0; // LnFactorial(m-1) - LnFactorial(n-1) - LnFactorial(m-n); // constant
8578 (*prob) += (m-2.0) * (ln_p0_t1 + log(lambda)) + (n-m)*log(mu);
8579 (*prob) += 2.0 * (LnP1(tmrca, lambda, mu) - log(1.0 - exp(ln_p0_t1)));
8580 (*prob) += (m-n) * log(1.0 - exp(LnP0(nt_2, lambda, mu))/exp(ln_p0_t1));
8581 for (i=0; i<t->nIntNodes-1; i++)
8582 (*prob) += LnP1(nt[i], lambda, mu) - ln_p0_t1;
8583
8584 /* conversion to labeled tree from oriented tree */
8585 // (*prob) += (n-1.0)*log(2.0) - LnFactorial(n); // constant
8586
8587 /* condition on tmrca, calibrations are dealt with separately */
8588 mp = &modelParams[t->relParts[0]];
8589 if (t->root->left->isDated == NO)
8590 (*prob) += mp->treeAgePr.LnPriorProb(tmrca, mp->treeAgePr.priorParams);
8591
8592 /* free memory */
8593 free (nt);
8594
8595 return (NO_ERROR);
8596 }
8597
8598
8599 /*
8600 |
8601 | The probability of having zero lineages remaining after time t in the
8602 | birth-death process.
8603 |
8604 | param: t - speciation time
8605 | param: b - birth rate
8606 | param: d - death rate
8607 | return: log probability of zero remaining lineages
8608 |
8609 */
LnP0(MrBFlt t,MrBFlt b,MrBFlt d)8610 MrBFlt LnP0 (MrBFlt t, MrBFlt b, MrBFlt d)
8611 {
8612 MrBFlt p0t;
8613
8614 p0t = d*(1.0-exp((d-b)*t)) / (b -d*exp((d-b)*t));
8615
8616 return (log(p0t));
8617 }
8618
8619 /* The probability that a lineage at time t has 0 sampled descendants at time 0 */
LnP0Subsample(MrBFlt t,MrBFlt b,MrBFlt d,MrBFlt f)8620 MrBFlt LnP0Subsample (MrBFlt t, MrBFlt b, MrBFlt d, MrBFlt f)
8621 {
8622 MrBFlt p0t;
8623
8624 p0t = (f*d + (b*(1.0-f) -d)*exp((d-b)*t)) / (f*b + (b*(1.0-f) -d)*exp((d-b)*t));
8625
8626 return (log(p0t));
8627 }
8628
8629 /*
8630 |
8631 | The probability of having one lineage remaining after time t
8632 | in the birth-death process.
8633 |
8634 | param: t - speciation time
8635 | param: b - birth rate
8636 | param: d - death rate
8637 | return: log probability of one remaining lineage
8638 |
8639 */
LnP1(MrBFlt t,MrBFlt b,MrBFlt d)8640 MrBFlt LnP1 (MrBFlt t, MrBFlt b, MrBFlt d)
8641 {
8642 MrBFlt p1t;
8643
8644 p1t = 2.0*log(b-d) - (b-d)*t;
8645
8646 p1t -= 2.0*log(b -d*exp((d-b)*t));
8647
8648 return p1t;
8649 }
8650
8651 /* The probability that a lineage at time t has 1 sampled descendants at time 0 */
LnP1Subsample(MrBFlt t,MrBFlt b,MrBFlt d,MrBFlt f)8652 MrBFlt LnP1Subsample (MrBFlt t, MrBFlt b, MrBFlt d, MrBFlt f)
8653 {
8654 MrBFlt p1t;
8655
8656 p1t = (b-d) / (f*b + (b*(1.0-f)-d)*exp((d-b)*t));
8657
8658 return (2.0*log(p1t) + log(f) - (b-d)*t);
8659 }
8660
8661
8662 /* probability that an individual alive at time t before today has
8663 no sampled extinct or extant descendants
8664 */
LnP0_fossil(MrBFlt t,MrBFlt lambda,MrBFlt mu,MrBFlt psi,MrBFlt c1,MrBFlt c2)8665 MrBFlt LnP0_fossil (MrBFlt t, MrBFlt lambda, MrBFlt mu, MrBFlt psi, MrBFlt c1, MrBFlt c2)
8666 {
8667 MrBFlt other;
8668
8669 // c1 = sqrt(pow(lambda-mu-psi, 2) + 4*lambda*psi);
8670 // c2 = (-lambda + mu + 2*lambda*rho + psi) / c1;
8671 other = (exp(-c1 *t) * (1 - c2) - (1 + c2)) / (exp(-c1 *t) * (1 - c2) + (1 + c2));
8672
8673 return log(lambda + mu + psi + c1 * other) - log(2.0*lambda);
8674 }
8675
8676 /* probability that an individual alive at time t before today has
8677 precisely one sampled extant descendant and no sampled extinct descendant
8678 */
LnP1_fossil(MrBFlt t,MrBFlt rho,MrBFlt c1,MrBFlt c2)8679 MrBFlt LnP1_fossil (MrBFlt t, MrBFlt rho, MrBFlt c1, MrBFlt c2)
8680 {
8681 MrBFlt other;
8682
8683 // c1 = sqrt(pow(lambda-mu-psi, 2) + 4*lambda*psi);
8684 // c2 = (-lambda + mu + 2*lambda*rho + psi) / c1;
8685 other = 2.0 * (1- c2*c2) * exp(-c1 *t) + pow(1-c2, 2) * exp(-2 *c1 *t) + pow(1+c2, 2);
8686
8687 return log(4.0) + log(rho) - c1 *t - log(other);
8688 }
8689
8690
8691 /* return which time interval t is in */
Slice_i(MrBFlt t,MrBFlt * t_f,int sl)8692 int Slice_i (MrBFlt t, MrBFlt *t_f, int sl)
8693 {
8694 int i = 0;
8695 assert (t > 0.0 && sl >= 0);
8696
8697 /* we need some tolerance here, for t[i] < t <= t[i-1] to return i */
8698 while (t < t_f[i] + BRLENS_MIN/5)
8699 {
8700 i++;
8701 if (i > sl)
8702 return sl;
8703 }
8704 return i;
8705 }
8706
8707 /* probability density of an individual at time t giving rise to an edge
8708 between time t and t_i with q_i(t_i) = 1
8709 */
LnQi_fossil(MrBFlt t,MrBFlt * t_f,int sl,MrBFlt * c1,MrBFlt * c2)8710 MrBFlt LnQi_fossil (MrBFlt t, MrBFlt *t_f, int sl, MrBFlt *c1, MrBFlt *c2)
8711 {
8712 MrBFlt lnq;
8713 int i = Slice_i (t, t_f, sl);
8714
8715 lnq = log(4.0) +c1[i] *(t_f[i] -t);
8716 lnq -= 2.0 * log(1 +c2[i] +(1 -c2[i]) *exp(c1[i] *(t_f[i] -t)));
8717
8718 return lnq;
8719 }
8720
8721 /* an individual at time t has no sampled descendants when the process is stopped
8722 (i.e., at time t_s), with t_i < t <= t_{i-1} (i = 1,..,s)
8723 */
LnPi_fossil(MrBFlt t,MrBFlt * t_f,int sl,MrBFlt * c1,MrBFlt * c2,MrBFlt * lambda,MrBFlt * mu,MrBFlt * psi)8724 MrBFlt LnPi_fossil (MrBFlt t, MrBFlt *t_f, int sl, MrBFlt *c1, MrBFlt *c2, MrBFlt *lambda, MrBFlt *mu, MrBFlt *psi)
8725 {
8726 MrBFlt other;
8727 int i = Slice_i (t, t_f, sl);
8728
8729 other = lambda[i] +mu[i] +psi[i] -c1[i] * (1 +c2[i] -(1 -c2[i]) *exp(c1[i] *(t_f[i] -t)))
8730 / (1 +c2[i] +(1 -c2[i]) *exp(c1[i] *(t_f[i] -t)));
8731 return log(other) - log(2 *lambda[i]);
8732 }
8733
8734
LnFossilizationPriorPr(Tree * t,MrBFlt clockRate,MrBFlt * prob,MrBFlt * sR,MrBFlt * eR,MrBFlt * fR,MrBFlt sF,char * sS)8735 int LnFossilizationPriorPr (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt *sR, MrBFlt *eR, MrBFlt *fR, MrBFlt sF, char *sS)
8736 {
8737 /* fossilization priors
8738 //chi */
8739
8740 if (!strcmp(sS, "FossilTip"))
8741 return LnFossilizedBDPriorFossilTip (t, clockRate, prob, sR, eR, fR, sF);
8742 else if (!strcmp(sS, "Random"))
8743 return LnFossilizedBDPriorRandom (t, clockRate, prob, sR, eR, fR, sF);
8744 else if (!strcmp(sS, "Diversity"))
8745 return LnFossilizedBDPriorDiversity (t, clockRate, prob, sR, eR, fR, sF);
8746 else
8747 {
8748 MrBayesPrint ("%s Sampling strategy %s for fossilized birth-death process not implemented\n", spacer, sS);
8749 return (ERROR);
8750 }
8751 }
8752
8753
8754 /*---------------------------------------------------------------------------------
8755 |
8756 | time ___ 0
8757 | \ / |
8758 | \ / |___ y2
8759 | \ \ / |
8760 | \ \ / |
8761 | \ \/ x3 ___|
8762 | \ / |___ y1
8763 | \ \ / |
8764 | \ \ / |
8765 | \ \ / |
8766 | \ \/ x2 ___|
8767 | \ / |
8768 | \ / |
8769 | \ / |
8770 | \ / |
8771 | \/ x1 ___|
8772 |
8773 |
8774 | T: oriented tree
8775 | b: birth (speciation) rate
8776 | d: death (extintion) rate
8777 | p: extant sampling prob
8778 | q: fossil sampling rate
8779 | n: number of extant taxa
8780 | m: number of fossil tips
8781 |
8782 |
8783 | [p1(x1)]^2 n+m-1 m q
8784 | f(T|tmrca) = ---------------- * prod b*p1(xi) * prod ----- .
8785 | [1 - p0(x1)]^2 i=2 i=1 p1(yi)
8786 |
8787 | f(tmrca) ~ uniform, gamma, etc (see treeAge).
8788 |
8789 ---------------------------------------------------------------------------------*/
LnFossilizedBDPriorFossilTip(Tree * t,MrBFlt clockRate,MrBFlt * prob,MrBFlt * sR,MrBFlt * eR,MrBFlt * fR,MrBFlt sF)8790 int LnFossilizedBDPriorFossilTip (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt *sR, MrBFlt *eR, MrBFlt *fR, MrBFlt sF)
8791 {
8792 /* special case: upon sampling the lineage is dead and won't produce descendants. Each extinct sample is a tip */
8793
8794 int i, n, m;
8795 MrBFlt x, lambda, mu, rho, psi, tmrca, c1, c2;
8796 TreeNode *p;
8797 Model *mp;
8798
8799 /* sR = lambda-mu-psi, eR = (mu+psi)/lambda, fR = psi/(mu+psi) */
8800 lambda = sR[0] / (1.0 - eR[0]);
8801 mu = lambda * eR[0] * (1.0 - fR[0]);
8802 psi = lambda * eR[0] * fR[0];
8803 rho = sF;
8804
8805 tmrca = t->root->left->nodeDepth / clockRate;
8806 c1 = sqrt(sR[0]*sR[0] + 4 *lambda *psi);
8807 c2 = (2 *lambda *rho - sR[0]) / c1;
8808
8809 /* calculate prior prob of the fbd tree */
8810 (*prob) = 0.0;
8811
8812 for (n = m = i = 0; i < t->nNodes -1; i++)
8813 {
8814 p = t->allDownPass[i];
8815 x = p->nodeDepth / clockRate;
8816
8817 if (p->left != NULL && p->right != NULL) // internal
8818 {
8819 if (p != t->root->left)
8820 (*prob) += log(lambda) + LnP1_fossil(x, rho, c1, c2);
8821 }
8822 else if (p->left == NULL && p->right == NULL) // tip
8823 {
8824 if (p->nodeDepth > 0.0)
8825 {
8826 (*prob) += log(psi) - LnP1_fossil(x, rho, c1, c2);
8827 m++;
8828 }
8829 else
8830 n++;
8831 }
8832 }
8833
8834 (*prob) += 2.0 * LnP1_fossil(tmrca, rho, c1, c2);
8835
8836 /* condition on sampling at least one individual in both subtrees leading to the root */
8837 (*prob) -= 2.0 * log(1 - exp(LnP0_fossil(tmrca, lambda, mu, psi, c1, c2)));
8838
8839 /* condition on tmrca, calibrations are dealt with separately */
8840 mp = &modelParams[t->relParts[0]];
8841 if (t->root->left->isDated == NO)
8842 (*prob) += mp->treeAgePr.LnPriorProb(tmrca, mp->treeAgePr.priorParams);
8843
8844 /* conversion to labeled tree from oriented tree, constant */
8845 // (*prob) += (n + m - 1) * log(2.0) - LnFactorial(n) - LnFactorial(m);
8846
8847 return (NO_ERROR);
8848 }
8849
8850
CompareDoubleDescending(const void * x,const void * y)8851 int CompareDoubleDescending (const void *x, const void *y)
8852 {
8853 if (*((double *)(x)) > *((double *)(y)))
8854 return -1;
8855 else if (*((double *)(x)) < *((double *)(y)))
8856 return 1;
8857 else
8858 return 0;
8859 }
GetTotalRateShifts(Model * mp,MrBFlt * shiftTimes)8860 int GetTotalRateShifts (Model *mp, MrBFlt *shiftTimes)
8861 {
8862 int i, j, sLen, sTotal;
8863
8864 /* get the total number of rate shifts */
8865 sTotal = mp->fossilSamplingNum;
8866 for (i = 0; i < mp->fossilSamplingNum; i++)
8867 shiftTimes[i] = mp->fossilSamplingTime[i];
8868 sLen = sTotal;
8869 for (i = 0; i < mp->birthRateShiftNum; i++)
8870 {
8871 for (j = 0; j < sLen; j++)
8872 if (fabs(mp->birthRateShiftTime[i] - shiftTimes[j]) <= BRLENS_MIN/2)
8873 break;
8874 if (fabs(mp->birthRateShiftTime[i] - shiftTimes[j]) > BRLENS_MIN/2)
8875 shiftTimes[sTotal++] = mp->birthRateShiftTime[i];
8876 }
8877 sLen = sTotal;
8878 for (i = 0; i < mp->deathRateShiftNum; i++)
8879 {
8880 for (j = 0; j < sLen; j++)
8881 if (fabs(mp->deathRateShiftTime[i] - shiftTimes[j]) <= BRLENS_MIN/2)
8882 break;
8883 if (fabs(mp->deathRateShiftTime[i] - shiftTimes[j]) > BRLENS_MIN/2)
8884 shiftTimes[sTotal++] = mp->deathRateShiftTime[i];
8885 }
8886
8887 /* sort shiftTimes[] in ascending order */
8888 qsort(shiftTimes, sTotal, sizeof(double), CompareDoubleDescending);
8889
8890 return sTotal;
8891 }
8892
8893
8894 /*---------------------------------------------------------------------------------
8895 |
8896 | LnFossilizedBDPriorRandom
8897 |
8898 | Gavryushkina, A., D. Welch, T. Stadler, and A. Drummond. 2014.
8899 | Bayesian inference of sampled ancestor trees for epidemiology and fossil calibration. PLoS Comp. Biol.
8900 | Zhang C., T. Stadler, S. Klopfstein, T. A. Heath, and F. Ronquist. 2015.
8901 | Total-Evidence Dating under the Fossilized Birth-Death Process. Syst. Biol.
8902 |
8903 |
8904 | 0 _____________ t3, rho3
8905 | \ / |
8906 | \ / |___ y2
8907 | \ \ / _________|_________ t2, rho2
8908 | \ \ / |
8909 | \ \/ x3 ___|
8910 | \ / |___ y1
8911 | \ \ / |
8912 | _\ \ / _________|_________ t1, rho1
8913 | \ \ / |
8914 | \ \/ x2 ___|
8915 | \ / |
8916 | \ / |
8917 | \ / |
8918 | \ / |
8919 | \/ x1 ___|_________ t_mrca
8920 |
8921 |
8922 | sl = 2, t1 > t2 > t3 = 0
8923 | E = 2, K = 1, M = 2
8924 |
8925 ---------------------------------------------------------------------------------*/
LnFossilizedBDPriorRandom(Tree * t,MrBFlt clockRate,MrBFlt * prob,MrBFlt * sR,MrBFlt * eR,MrBFlt * fR,MrBFlt sF)8926 int LnFossilizedBDPriorRandom (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt *sR, MrBFlt *eR, MrBFlt *fR, MrBFlt sF)
8927 {
8928 /* Fossils are sampled with piecewise constant rates in the past.
8929 Extant taxa are sampled uniformly at random at present. */
8930
8931 int i, j, i1, i2, i3, sl, K, M, E;
8932 MrBFlt x, tmrca, *t_f, *lambda, *mu, *psi, *rho, *netDiver, *turnOver, *sampProp, *c1, *c2, *p_t;
8933 TreeNode *p;
8934 Model *mp;
8935
8936 mp = &modelParams[t->relParts[0]];
8937
8938 /* time of most recent common ancestor */
8939 tmrca = t->root->left->nodeDepth / clockRate;
8940
8941 /* alloc memory for time of each shift, t_f[sl] = 0 */
8942 t_f = (MrBFlt *)SafeMalloc((size_t)(mp->fossilSamplingNum + mp->birthRateShiftNum + mp->deathRateShiftNum +1) * sizeof(MrBFlt));
8943 if (!t_f)
8944 {
8945 MrBayesPrint ("%s ERROR: Problem allocating t_f in LnFossilizedBDPriorRandom\n", spacer);
8946 return (ERROR);
8947 }
8948 /* get the total rate shifts, sl >= 0 */
8949 sl = GetTotalRateShifts(mp, t_f);
8950
8951 t_f[sl] = 0.0;
8952 assert (t_f[0] < tmrca);
8953
8954 /* alloc memory for the other parameters */
8955 lambda = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8956 mu = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8957 psi = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8958 rho = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8959 netDiver = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8960 turnOver = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8961 sampProp = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8962 c1 = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8963 c2 = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8964 p_t = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
8965 if (!lambda || !mu || !psi || !rho || !netDiver || !turnOver || !sampProp || !c1 || !c2 || !p_t)
8966 {
8967 MrBayesPrint ("%s ERROR: Problem allocating memory in LnFossilizedBDPriorRandom\n", spacer);
8968 free(lambda); free(mu); free(psi); free(rho); free(netDiver); free(turnOver); free(sampProp); free(c1); free(c2); free(p_t);
8969 return (ERROR);
8970 }
8971
8972 /* initialization */
8973 i1 = i2 = i3 = 0;
8974 for (i = 0; i <= sl; i++)
8975 {
8976 if (mp->birthRateShiftNum > 0 && mp->birthRateShiftTime[i1] > t_f[i] +BRLENS_MIN/2)
8977 i1++;
8978 if (mp->deathRateShiftNum > 0 && mp->deathRateShiftTime[i2] > t_f[i] +BRLENS_MIN/2)
8979 i2++;
8980 if (mp->fossilSamplingNum > 0 && mp->fossilSamplingTime[i3] > t_f[i] +BRLENS_MIN/2)
8981 i3++;
8982 netDiver[i] = sR[i1];
8983 turnOver[i] = eR[i2];
8984 sampProp[i] = fR[i3];
8985 }
8986 for (i = 0; i <= sl; i++)
8987 {
8988 /* netDiver = lambda-mu, turnOver = mu/lambda, sampProp = psi/(mu+psi) */
8989 lambda[i] = netDiver[i] / (1.0 - turnOver[i]);
8990 mu[i] = lambda[i] * turnOver[i];
8991 psi[i] = mu[i] * sampProp[i] / (1.0 - sampProp[i]);
8992 rho[i] = 1E-20; // to avoid prob being -inf
8993 }
8994 rho[sl] = sF; // 0 < sF <= 1
8995
8996 for (i = sl; i >= 0; i--)
8997 {
8998 c1[i] = sqrt(pow(lambda[i]-mu[i]-psi[i], 2) + 4*lambda[i]*psi[i]);
8999 if (i == sl)
9000 c2[i] = ((1 - 2* (1-rho[i])) *lambda[i] +mu[i] +psi[i]) /c1[i];
9001 else
9002 c2[i] = ((1 - 2* (1-rho[i]) *p_t[i+1]) *lambda[i] +mu[i] +psi[i]) /c1[i];
9003 if (i > 0)
9004 p_t[i] = (lambda[i] +mu[i] +psi[i] -c1[i] * (1 +c2[i] -(1 -c2[i]) *exp(c1[i] *(t_f[i] -t_f[i-1])))
9005 / (1 +c2[i] +(1 -c2[i]) *exp(c1[i] *(t_f[i] -t_f[i-1])))) *0.5/lambda[i];
9006 else
9007 p_t[i] = (lambda[i] +mu[i] +psi[i] -c1[i] * (1 +c2[i] -(1 -c2[i]) *exp(c1[i] *(t_f[i] -tmrca)))
9008 / (1 +c2[i] +(1 -c2[i]) *exp(c1[i] *(t_f[i] -tmrca)))) *0.5/lambda[i];
9009
9010 # ifdef DEBUG_FBDPR
9011 printf ("%d: lambda=%lf mu=%lf psi=%lf d=%lf r=%lf s=%lf t=%lf rho=%lf\n",
9012 i+1, lambda[i], mu[i], psi[i], netDiver[i], turnOver[i], sampProp[i], t_f[i], rho[i]);
9013 printf (" A=%lf B=%lf p%d(t%d)=%lf\n", c1[i], c2[i], i+1, i, p_t[i]);
9014 # endif
9015 }
9016
9017 /* calculate prior prob of the fbd tree */
9018 (*prob) = 0.0;
9019
9020 for (K = M = E = 0, i = 0; i < t->nNodes -1; i++)
9021 {
9022 p = t->allDownPass[i];
9023 x = p->nodeDepth / clockRate;
9024
9025 if (p->left != NULL && p->right != NULL) // internal
9026 {
9027 if (p->left->length > 0.0 && p->right->length > 0.0)
9028 {
9029 if (p != t->root->left)
9030 (*prob) += log(lambda[Slice_i(x, t_f, sl)]) + LnQi_fossil(x, t_f, sl, c1,c2);
9031 }
9032 else
9033 {
9034 for (j = 0; j < sl; j++)
9035 if (AreDoublesEqual(p->nodeDepth, t_f[j]*clockRate, BRLENS_MIN/5) == YES) break;
9036 if (j == sl) /* fossil ancestor between t[j-1] and t[j] */
9037 {
9038 (*prob) += log(psi[Slice_i(x, t_f, sl)]);
9039 }
9040 else /* fossil ancestor at silice time t[j] */
9041 {
9042 (*prob) += log(rho[j]) - log(1 - rho[j]);
9043 }
9044 K++; /* number of fossil ancestors */
9045 }
9046 }
9047 else if (p->left == NULL && p->length > 0.0) // tip
9048 {
9049 if (p->nodeDepth > 0.0)
9050 {
9051 for (j = 0; j < sl; j++)
9052 if (AreDoublesEqual(p->nodeDepth, t_f[j]*clockRate, BRLENS_MIN/5) == YES) break;
9053 if (j == sl) /* fossil tip between t[j-1] and t[j] */
9054 {
9055 (*prob) += LnPi_fossil(x, t_f, sl, c1,c2, lambda,mu,psi) - LnQi_fossil(x, t_f, sl, c1,c2);
9056 (*prob) += log(psi[Slice_i(x, t_f, sl)]);
9057 }
9058 else /* fossil tip at silice time t[j] */
9059 {
9060 (*prob) += log(p_t[j+1]) + log(rho[j]);
9061 }
9062 M++; /* number of fossil tips */
9063 }
9064 else
9065 {
9066 (*prob) += log(rho[sl]); // 0 < rho[sl] <= 1
9067 E++; /* number of extant taxa */
9068 }
9069 }
9070
9071 for (j = 0; j < sl; j++) /* degree-two vertices at silice time t_j */
9072 {
9073 if (p->length > 0.0 && (p->nodeDepth +BRLENS_MIN/10 < t_f[j]*clockRate) && (t_f[j]*clockRate < p->anc->nodeDepth +BRLENS_MIN/10))
9074 {
9075 (*prob) += LnQi_fossil(t_f[j], t_f, sl, c1,c2) + log(1 - rho[j]);
9076 }
9077 }
9078 }
9079
9080 /* condition on sampling a fossil OR a extant taxa */
9081 (*prob) += 2.0 * (LnQi_fossil(tmrca, t_f, sl, c1,c2) - log(1- p_t[0]));
9082
9083 /* conversion to labeled tree from oriented tree */
9084 (*prob) += (M + E - 1) * log(2.0); // - LnFactorial(E + M + K); // # permutation is constant
9085
9086 /* condition on tmrca, calibrations are dealt with separately */
9087 if (t->root->left->isDated == NO)
9088 (*prob) += mp->treeAgePr.LnPriorProb(tmrca, mp->treeAgePr.priorParams);
9089
9090 # ifdef DEBUG_FBDPR
9091 printf ("K=%d M=%d E=%d\n", K, M, E);
9092 printf ("prob=%lf\n", *prob);
9093 # endif
9094
9095 /* free memory */
9096 free(t_f);
9097 free(lambda); free(mu); free(psi); free(rho); free(netDiver); free(turnOver); free(sampProp); free(c1); free(c2); free(p_t);
9098
9099 return (NO_ERROR);
9100 }
9101
9102
9103 /*---------------------------------------------------------------------------------
9104 |
9105 | LnFossilizedBDPriorDiversity
9106 |
9107 | Zhang C., T. Stadler, S. Klopfstein, T. A. Heath, and F. Ronquist. 2015.
9108 | Total-Evidence Dating under the Fossilized Birth-Death Process. Syst. Biol.
9109 |
9110 ---------------------------------------------------------------------------------*/
LnFossilizedBDPriorDiversity(Tree * t,MrBFlt clockRate,MrBFlt * prob,MrBFlt * sR,MrBFlt * eR,MrBFlt * fR,MrBFlt sF)9111 int LnFossilizedBDPriorDiversity (Tree *t, MrBFlt clockRate, MrBFlt *prob, MrBFlt *sR, MrBFlt *eR, MrBFlt *fR, MrBFlt sF)
9112 {
9113 /* Fossils are sampled with piecewise constant rates in the past.
9114 Extant taxa are sampled with prop sF to maximize diversity. */
9115
9116 int i, j, i1, i2, i3, sl, K, M, E;
9117 MrBFlt x, tmrca, *t_f, x_cut, M_x, *lambda, *mu, *psi, *rho, *netDiver, *turnOver, *sampProp, *c1, *c2, *p_t;
9118 TreeNode *p;
9119 Model *mp;
9120
9121 mp = &modelParams[t->relParts[0]];
9122
9123 /* time of most recent common ancestor */
9124 tmrca = t->root->left->nodeDepth / clockRate;
9125
9126 /* alloc memory for time of each shift, t_f[sl] = 0, t_f[sl-1] = x_cut */
9127 t_f = (MrBFlt *)SafeMalloc((size_t)(mp->fossilSamplingNum + mp->birthRateShiftNum + mp->deathRateShiftNum +2) * sizeof(MrBFlt));
9128 if (!t_f)
9129 {
9130 MrBayesPrint ("%s ERROR: Problem allocating t_f in LnFossilizedBDPriorDiversity\n", spacer);
9131 return (ERROR);
9132 }
9133 /* get the total rate shifts, plus 1 to shift psi to 0 */
9134 sl = GetTotalRateShifts(mp, t_f) + 1;
9135
9136 /* get time of youngest fossil and internal node */
9137 x_cut = tmrca;
9138 for (i = 0; i < t->nNodes -1; i++)
9139 {
9140 p = t->allDownPass[i];
9141 if (p->left == NULL && p->nodeDepth > 0.0) //fossil
9142 if (x_cut > p->nodeDepth / clockRate)
9143 x_cut = p->nodeDepth / clockRate;
9144 if (p->left != NULL && p->right != NULL &&
9145 p->left->length > 0.0 && p->right->length > 0.0) // internal
9146 if (x_cut > p->nodeDepth / clockRate)
9147 x_cut = p->nodeDepth / clockRate;
9148 }
9149
9150 /* lower the cutoff time if not compatible */
9151 if (sl > 1 && x_cut > t_f[sl-2])
9152 x_cut = t_f[sl-2];
9153
9154 t_f[sl] = 0.0;
9155 t_f[sl-1] = x_cut * 0.95; // x_cut
9156 assert (t_f[0] < tmrca);
9157
9158 /* alloc memory for the other parameters */
9159 lambda = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9160 mu = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9161 psi = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9162 rho = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9163 netDiver = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9164 turnOver = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9165 sampProp = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9166 c1 = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9167 c2 = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9168 p_t = (MrBFlt *)SafeMalloc((size_t)(sl+1) * sizeof(MrBFlt));
9169 if (!lambda || !mu || !psi || !rho || !netDiver || !turnOver || !sampProp || !c1 || !c2 || !p_t)
9170 {
9171 MrBayesPrint ("%s ERROR: Problem allocating memory in LnFossilizedBDPriorDiversity\n", spacer);
9172 free(lambda); free(mu); free(psi); free(rho); free(netDiver); free(turnOver); free(sampProp); free(c1); free(c2); free(p_t);
9173 return (ERROR);
9174 }
9175
9176 /* initialization */
9177 i1 = i2 = i3 = 0;
9178 for (i = 0; i < sl; i++)
9179 {
9180 if (mp->birthRateShiftNum > 0 && mp->birthRateShiftTime[i1] > t_f[i] +BRLENS_MIN/2)
9181 i1++;
9182 if (mp->deathRateShiftNum > 0 && mp->deathRateShiftTime[i2] > t_f[i] +BRLENS_MIN/2)
9183 i2++;
9184 if (mp->fossilSamplingNum > 0 && mp->fossilSamplingTime[i3] > t_f[i] +BRLENS_MIN/2)
9185 i3++;
9186 netDiver[i] = sR[i1];
9187 turnOver[i] = eR[i2];
9188 sampProp[i] = fR[i3];
9189 }
9190 for (i = 0; i < sl; i++)
9191 {
9192 /* netDiver = lambda-mu, turnOver = mu/lambda, sampProp = psi/(mu+psi) */
9193 lambda[i] = netDiver[i] / (1.0 - turnOver[i]);
9194 mu[i] = lambda[i] * turnOver[i];
9195 psi[i] = mu[i] * sampProp[i] / (1.0 - sampProp[i]);
9196 rho[i] = 1E-20; // to avoid prob being -inf
9197 }
9198 lambda[sl] = lambda[sl-1]; netDiver[sl] = netDiver[sl-1];
9199 mu[sl] = mu[sl-1]; turnOver[sl] = turnOver[sl-1];
9200 psi[sl] = 0.0; // psi = 0 in [0, x_cut]
9201 rho[sl] = 1.0; // not sF
9202
9203 for (i = sl; i >= 0; i--)
9204 {
9205 c1[i] = sqrt(pow(lambda[i]-mu[i]-psi[i], 2) + 4*lambda[i]*psi[i]);
9206 if (i == sl)
9207 c2[i] = ((1 - 2* (1-rho[i])) *lambda[i] +mu[i] +psi[i]) /c1[i];
9208 else
9209 c2[i] = ((1 - 2* (1-rho[i]) *p_t[i+1]) *lambda[i] +mu[i] +psi[i]) /c1[i];
9210 if (i > 0)
9211 p_t[i] = (lambda[i] +mu[i] +psi[i] -c1[i] * (1 +c2[i] -(1 -c2[i]) *exp(c1[i] *(t_f[i] -t_f[i-1])))
9212 / (1 +c2[i] +(1 -c2[i]) *exp(c1[i] *(t_f[i] -t_f[i-1])))) *0.5/lambda[i];
9213 else
9214 p_t[i] = (lambda[i] +mu[i] +psi[i] -c1[i] * (1 +c2[i] -(1 -c2[i]) *exp(c1[i] *(t_f[i] -tmrca)))
9215 / (1 +c2[i] +(1 -c2[i]) *exp(c1[i] *(t_f[i] -tmrca)))) *0.5/lambda[i];
9216
9217 # ifdef DEBUG_FBDPR
9218 printf ("%d: lambda=%lf mu=%lf psi=%lf d=%lf r=%lf s=%lf t=%lf rho=%lf\n",
9219 i+1, lambda[i], mu[i], psi[i], netDiver[i], turnOver[i], sampProp[i], t_f[i], rho[i]);
9220 printf (" A=%lf B=%lf p%d(t%d)=%lf\n", c1[i], c2[i], i+1, i, p_t[i]);
9221 # endif
9222 }
9223
9224 /* first calculate prob of the fbd tree assuming complete sampling */
9225 (*prob) = 0.0;
9226
9227 for (K = M = E = 0, i = 0; i < t->nNodes -1; i++)
9228 {
9229 p = t->allDownPass[i];
9230 x = p->nodeDepth / clockRate;
9231
9232 if (p->left != NULL && p->right != NULL) // internal
9233 {
9234 if (p->left->length > 0.0 && p->right->length > 0.0)
9235 {
9236 if (p != t->root->left)
9237 (*prob) += log(lambda[Slice_i(x, t_f, sl)]) + LnQi_fossil(x, t_f, sl, c1,c2);
9238 }
9239 else
9240 {
9241 for (j = 0; j < sl; j++)
9242 if (AreDoublesEqual(p->nodeDepth, t_f[j]*clockRate, BRLENS_MIN/5) == YES) break;
9243 if (j == sl) /* fossil ancestor between t[j-1] and t[j] */
9244 {
9245 (*prob) += log(psi[Slice_i(x, t_f, sl)]);
9246 }
9247 else /* fossil ancestor at silice time t[j] */
9248 {
9249 (*prob) += log(rho[j]) - log(1 - rho[j]);
9250 }
9251 K++; /* number of fossil ancestors */
9252 }
9253 }
9254 else if (p->left == NULL && p->length > 0.0) // tip
9255 {
9256 if (p->nodeDepth > 0.0)
9257 {
9258 for (j = 0; j < sl; j++)
9259 if (AreDoublesEqual(p->nodeDepth, t_f[j]*clockRate, BRLENS_MIN/5) == YES) break;
9260 if (j == sl) /* fossil tip between t[j-1] and t[j] */
9261 {
9262 (*prob) += LnPi_fossil(x, t_f, sl, c1,c2, lambda,mu,psi) - LnQi_fossil(x, t_f, sl, c1,c2);
9263 (*prob) += log(psi[Slice_i(x, t_f, sl)]);
9264 }
9265 else /* fossil tip at silice time t[j] */
9266 {
9267 (*prob) += log(p_t[j+1]) + log(rho[j]);
9268 }
9269 M++; /* number of fossil tips */
9270 }
9271 else
9272 {
9273 // (*prob) += log(rho[sl]); // rho[sl] = 1
9274 E++; /* number of extant taxa */
9275 }
9276 }
9277
9278 for (j = 0; j < sl; j++) /* degree-two vertices at silice time t_j */
9279 {
9280 if (p->length > 0.0 && (p->nodeDepth +BRLENS_MIN/10 < t_f[j]*clockRate) && (t_f[j]*clockRate < p->anc->nodeDepth +BRLENS_MIN/10))
9281 {
9282 (*prob) += LnQi_fossil(t_f[j], t_f, sl, c1,c2) + log(1 - rho[j]);
9283 }
9284 }
9285 }
9286
9287 /* condition on sampling a fossil OR a extant taxa */
9288 (*prob) += 2.0 * (LnQi_fossil(tmrca, t_f, sl, c1,c2) - log(1- p_t[0]));
9289
9290 /* number of extant taxa not sampled */
9291 M_x = (int)floor(E/sF + 0.5) - E; /* equal to round(E/sF) plus it is compatible with MS Visual Studio */
9292
9293 /* then calculate the prob of the fbd tree assuming diversified sampling of extant */
9294 (*prob) += M_x * (log(lambda[sl] * (1.0 - exp((mu[sl]-lambda[sl])*t_f[sl-1]))) - log(lambda[sl] - mu[sl] * exp((mu[sl]-lambda[sl])*t_f[sl-1])));
9295
9296 /* conversion to labeled tree from oriented tree */
9297 (*prob) += (M + E - 1) * log(2.0); // - LnFactorial(E + M + K); // # permutation is constant
9298
9299 /* condition on tmrca, calibrations are dealt with separately */
9300 if (t->root->left->isDated == NO)
9301 (*prob) += mp->treeAgePr.LnPriorProb(tmrca, mp->treeAgePr.priorParams);
9302
9303 # ifdef DEBUG_FBDPR
9304 printf ("K=%d M=%d E=%d\n", K, M, E);
9305 printf ("prob=%lf\n", *prob);
9306 # endif
9307
9308 /* free memory */
9309 free(t_f);
9310 free(lambda); free(mu); free(psi); free(rho); free(netDiver); free(turnOver); free(sampProp); free(c1); free(c2); free(p_t);
9311
9312 return (NO_ERROR);
9313 }
9314
9315
9316 /*---------------------------------------------------------------------------------
9317 |
9318 | LnCoalescencePriorPr
9319 |
9320 | This function calculates the probability of a tree under the neutral
9321 | coalescence prior with a (potentially) exponentially growing population.
9322 | We assume a rooted tree that satisfies the molecular clock constraint. The
9323 | Tree is labelled as follows:
9324 |
9325 | t_4 ___
9326 | \ \ \ / \
9327 | \ \ \ / |
9328 | I_4 \ \ \ / | g_4
9329 | \ \ \ / |
9330 | \ \ \/ t_3 ___ /
9331 | \ \ / \
9332 | \ \ / |
9333 | I_3 \ \ / | g_3
9334 | \ \ / |
9335 | \ \/ t_2 ___ /
9336 | \ / \
9337 | \ / |
9338 | I_2 \ / | g_2
9339 | \ / |
9340 | \/ t_1 ___ /
9341 |
9342 | Each interval on the tree is specified by successive coalescence events.
9343 | These intervals are denoted I_2, I_3, I_4, ..., with the subscript denoting
9344 | how many lineages exist in that interval. The time of each coalescence event
9345 | is designated t_1, t_2, t_3, ..., where the subscript denotes the number
9346 | of lineages that exist after the coalescence (t_3, for instance, would be
9347 | the time of the coalescence that went from four lineages to three lineages).
9348 | The duration of the i-th interval is designated g_i.
9349 |
9350 | The probability of the coalescence tree is:
9351 |
9352 | prob = (k_C_2 / (N(t_k + t_k))) * exp(-integral_(from x=t_k, to g_k + t_k) (k_C_2 / N(x)) dx)
9353 |
9354 | where N(x) = N(0) * exp(-r*x). For the constant population size case,
9355 | N(0) = N_e. r is the population growth parameter for the exponentially
9356 | growing population. Here, theta = N(0) * mu when organisms are haploid and
9357 | theta = 2 * N(0) * mu when the organism is diploid.
9358 |
9359 | Below, ct holds the n - 1 coalescence times (t_i, above) sorted from the
9360 | smallest to the largest. Remember that t_4 < t_3 < t_2, etc.
9361 |
9362 | 2010-03-23
9363 | The original function (described above) was incorrect in that it used theta = 2 * N * mu
9364 | in one part of the equation and theta = 4 * N * mu in another. It is now corrected to
9365 | consistently use theta = 4 * N * mu, which is the standard for diploid populations
9366 | and theta = 2 * N * mu for haploid populations. The calculations are the same for the
9367 | diploid and haploid cases, only the interpretation is different. See, e.g., Felsenstein
9368 | (2004; Inferring Phylogenies). -- Fredrik.
9369 |
9370 ---------------------------------------------------------------------------------*/
LnCoalescencePriorPr(Tree * t,MrBFlt * prob,MrBFlt theta,MrBFlt growth)9371 int LnCoalescencePriorPr (Tree *t, MrBFlt *prob, MrBFlt theta, MrBFlt growth)
9372 {
9373 int i, j, k, nNodes;
9374 MrBFlt *ct, tempD, lastCoalescenceTime, coalescenceTime, intervalLength;
9375 TreeNode *p;
9376
9377 /* allocate space for the coalescence times */
9378 ct = (MrBFlt *)SafeMalloc((size_t)(t->nIntNodes) * sizeof(MrBFlt));
9379 if (!ct)
9380 {
9381 MrBayesPrint ("\n ERROR: Problem allocating ct\n");
9382 return (ERROR);
9383 }
9384
9385 /* get the coalescence times and put them into a vector */
9386 for (i=j=0; i<t->nIntNodes; i++)
9387 {
9388 p = t->intDownPass[i];
9389 if (p->anc != NULL)
9390 ct[j++] = p->nodeDepth; // Don't divide clockRate here, as mu is already in theta
9391 }
9392 nNodes = j;
9393
9394 /* sort the coalescence times */
9395 SortMrBFlt (ct, 0, nNodes-1);
9396
9397 /*for (i=0, k=numLocalTaxa; i<nNodes; i++)
9398 {
9399 printf ("%4d -- %2d %lf\n", i, k, ct[i]);
9400 k--;
9401 }*/
9402
9403 /* calculate probability of the tree */
9404 if (AreDoublesEqual (growth, 0.0, 0.000001) == YES)
9405 {
9406 /* use this if there is no population growth */
9407 tempD = lastCoalescenceTime = 0.0;
9408 for (i=0, k=numLocalTaxa; i<nNodes; i++)
9409 {
9410 coalescenceTime = ct[i];
9411 intervalLength = coalescenceTime - lastCoalescenceTime;
9412 lastCoalescenceTime = ct[i];
9413 tempD += - (k * (k-1) * intervalLength) / (theta);
9414 k--;
9415 }
9416 (*prob) = (numLocalTaxa - 1) * log(2.0 / theta) + tempD;
9417 }
9418 else
9419 {
9420 /* use this if the population is growing exponentially */
9421 tempD = lastCoalescenceTime = 0.0;
9422 for (i=0, k=numLocalTaxa; i<nNodes; i++)
9423 {
9424 coalescenceTime = ct[i];
9425 intervalLength = coalescenceTime - lastCoalescenceTime; /* FIXME: Not used (from clang static analyzer) */
9426 tempD += growth * coalescenceTime + (((k * (k-1)) / (theta * growth)) * (exp(growth * lastCoalescenceTime) - exp(growth * coalescenceTime)));
9427 lastCoalescenceTime = ct[i];
9428 k--;
9429 }
9430 (*prob) = (numLocalTaxa - 1) * log(2.0 / theta) + tempD;
9431 }
9432
9433 /* printf ("coal pr = %lf theta = %lf, nNodes = %d, nt = %d tempD = %lf\n", *prob, theta, nNodes, numLocalTaxa, tempD); */
9434
9435 /* free memory */
9436 free (ct);
9437
9438 return (NO_ERROR);
9439 }
9440
9441
9442 /*---------------------------------------------------------------------------------
9443 |
9444 | LnUniformPriorPr
9445 |
9446 | This function calculates the probability of a calibrated clock tree under the
9447 | uniform prior probability distribution on node depths. The tree is labeled as
9448 | follows:
9449 | interval between tip dates
9450 | t_0 t_1 time ____ 0 (interval between t_0 and t_1, duration 0)
9451 | \ / |
9452 | \ t_2 / |___ 1 (interval between t_1 and t_2)
9453 | \ \ / |
9454 | \ \ / |
9455 | \ \/ 0.33 ___| 2 (interval between t_2 and t_3)
9456 | \ t_3 / |___
9457 | \ \ / |
9458 | \ \ / |
9459 | \ \ / |
9460 | \ t_4 \/ 0.67 ___|
9461 | \ \ / | 3 (interval between t_3 and root)
9462 | \ \/ |
9463 | \ / | Note that t_4 is irrelevant for intervals
9464 | \ / | because we need not have a single coalescent
9465 | \/ 1.00 ___|___ event beneath t_4 except for the root.
9466 |
9467 | The probability depends on the number of nodes falling in each interval between
9468 | tip dates, the time interval on which each node depth can vary (if it is interior
9469 | node number i, it can vary on the interval (t_{i+1},1)). Finally, the probability
9470 | is multiplied by the number of coalescent histories compatible with the node order.
9471 |
9472 ---------------------------------------------------------------------------------*/
LnUniformPriorPr(Tree * t,MrBFlt clockRate)9473 MrBFlt LnUniformPriorPr (Tree *t, MrBFlt clockRate)
9474 {
9475 int i, j, k, *nLineages=NULL, nDatedTips, nLineagesIn, nLineagesOut, nTips;
9476 MrBFlt lnProb, treeAge, *nodeDepths=NULL;
9477 TreeNode *p, *root;
9478 Model *mp;
9479
9480 lnProb = 0.0;
9481 mp = &modelParams[t->relParts[0]];
9482 treeAge = t->root->left->nodeDepth / clockRate;
9483 assert (t->root->left->isDated == NO || AreDoublesEqual(treeAge, t->root->left->age, 0.000001) == YES);
9484
9485 /* Calculate number of tips for convenience */
9486 nTips = t->nNodes - t->nIntNodes - 1;
9487
9488 /* First take tree age into account f(t_0) */
9489 if (t->root->left->isDated == YES)
9490 {
9491 lnProb += 0.0; /* calibrations are dealt with separately in calling function */
9492 }
9493 else
9494 {
9495 lnProb += mp->treeAgePr.LnPriorProb(treeAge, mp->treeAgePr.priorParams);
9496 }
9497
9498 /* If tree is not calibrated or only root is calibrated, it is easy */
9499 for (i=j=0; i<t->nNodes-2; i++)
9500 {
9501 if (t->allDownPass[i]->isDated == YES)
9502 j++;
9503 }
9504 if (j == 0)
9505 {
9506 /* Calculate simple probability f(tau|t_0) */
9507 lnProb += (nTips - 1.0)*log(2.0) - LnFactorial(nTips) - log(nTips-1.0) - (nTips - 2.0)*log(treeAge);
9508 assert (lnProb > NEG_INFINITY);
9509 return lnProb;
9510 }
9511
9512 /* We have a tree with interior or tip calibrations */
9513
9514 /* Color subtrees by assigning an index 1,...,k to the x variable of each node,
9515 where k is the total number of separate subtrees. A subtree is characterized
9516 by having all interior nodes being unconstrained and all tips being either
9517 terminals or constrained interior nodes. The root of a subtree may be the
9518 root of the tree, or an interior node that is constrained and dated, or an
9519 interior node that is constrained but not dated. */
9520 i = 0;
9521 ColorClusters (t->root->left, &i);
9522
9523 /* Get the probability for each subtree */
9524 for (i=0; i<t->nIntNodes; i++)
9525 {
9526 p = t->intDownPass[i];
9527
9528 /* Skip unless this is the root of a subtree */
9529 if (p->anc->anc != NULL && p->isDated != YES)
9530 continue;
9531
9532 /* Save the root of the subtree */
9533 root = p;
9534
9535 /* Create an array containing the sorted times */
9536
9537 /* Allocate space for node depths */
9538 nDatedTips = NumDatedTips (root);
9539 nodeDepths = (MrBFlt *) SafeRealloc ((void *)nodeDepths, (nDatedTips+1)*sizeof(MrBFlt));
9540
9541 /* Get the dated node depths and sort them. The call to GetDatedNodeDepths also
9542 returns the root node depth into nodeDepths, which is convenient. For now, this
9543 only works for dated tips, not for constrained but undated interior nodes. */
9544 GetDatedNodeDepths (root, nodeDepths);
9545 SortMrBFlt (nodeDepths, 0, nDatedTips); /* use index of left and right in call */
9546
9547 /* Get probability due to the uniform node depths; we do not use first and last tip depth
9548 for obvious reasons (see figure above) */
9549 for (j=1; j<nDatedTips-1; j++)
9550 lnProb -= log ((root->nodeDepth - nodeDepths[j]) / clockRate);
9551
9552 /* Get probability due to sorting of interior node depths */
9553
9554 /* First get the potential number of lineages leaving each interval j at time nodeDepths[j+1] */
9555 nLineages = (int *) SafeRealloc ((void *)nLineages, nDatedTips*sizeof(int));
9556 for (j=0; j<nDatedTips; j++)
9557 nLineages[j] = j+1;
9558
9559 /* Subtract interior nodes so that we get the real number of lineages leaving each interval.
9560 The number of lineages entering each interval is always 1 + the number of lineages
9561 leaving the previous interval. We utilize the fact here that the last node depth is the
9562 root node depth and is held in nodeDepths[nDatedTips] */
9563 for (j=0; j<t->nIntNodes; j++)
9564 {
9565 p = t->intDownPass[j];
9566 if (p->x != root->x || p == root || p->isDated == YES)
9567 continue;
9568 for (k=0; k<nDatedTips; k++)
9569 if (p->nodeDepth < nodeDepths[k+1])
9570 nLineages[k]--;
9571 }
9572
9573 /* Now get the density effect of the sorting constraints */
9574 for (j=1; j<nDatedTips-1; j++)
9575 {
9576 nLineagesIn = nLineages[j-1] + 1;
9577 if (j==nDatedTips-2)
9578 nLineagesOut = 2; /* skip the last segment and jump directly to root */
9579 else
9580 nLineagesOut = nLineages[j];
9581 /* only calculate if ln different from 0 */
9582 if (nLineagesIn > 1 && nLineagesIn - nLineagesOut >= 1)
9583 {
9584 lnProb += LnFactorial (nLineagesIn-1) - LnFactorial(nLineagesOut-1);
9585 }
9586 }
9587
9588 /* Finally get the effect of the number of possible coalescent histories */
9589 for (j=1; j<nDatedTips; j++)
9590 {
9591 nLineagesIn = nLineages[j-1] + 1;
9592 nLineagesOut = nLineages[j];
9593 if (nLineagesIn != nLineagesOut)
9594 {
9595 lnProb += log(2.0) * (nLineagesIn - nLineagesOut);
9596 lnProb += LnFactorial (nLineagesOut) + LnFactorial (nLineagesOut-1);
9597 lnProb -= LnFactorial (nLineagesIn) + LnFactorial (nLineagesIn-1);
9598 }
9599 }
9600
9601 /* Last but not least, change color of root so that it can be a tip in the next
9602 subtree (if we wanted to use the colors). */
9603 root->x = root->anc->x;
9604
9605 }
9606
9607 free (nodeDepths);
9608 free (nLineages);
9609
9610 assert (lnProb > NEG_INFINITY);
9611
9612 return lnProb;
9613 }
9614
9615
9616 /*------------------------------------------------------------------------
9617 |
9618 | NewtonRaphsonBrlen: Find one maximum likelihood branch length using
9619 | the Newton-Raphson method. This function assumes that the tree is
9620 | a non-clock tree. For clock trees, you have to optimize the node
9621 | depths instead.
9622 |
9623 ------------------------------------------------------------------------*/
NewtonRaphsonBrlen(Tree * t,TreeNode * p,int chain)9624 int NewtonRaphsonBrlen (Tree *t, TreeNode *p, int chain)
9625 {
9626 int c, i, j, s, k, n, d, division, nIterations, maxNumIterations,
9627 index, *rateCat, r;
9628 MrBFlt vOld, *pi, *cijk, *eigenValues, *catRate, baseRate, length, theRate, tolerance,
9629 expLambdaV[64], pInvar=0.0, likeI=0.0, sum=0.0, sum1, sum2, beta, w, v, x, y, expBetaV,
9630 expWV, kappa, bigPi, *ptr;
9631 CLFlt *nSitesOfPat, *clP, *clA, *tiP, *tiP1, *tiP2, like, like1, like2, CLsum, CLsum1, CLsum2,
9632 freq, sumLike1, sumLike2, *lnScaler=NULL, *clInvar=NULL;
9633 ModelInfo *m;
9634
9635 /* TODO: Standard model (also check RES for ascertainment bias) */
9636 tolerance = 0.001;
9637 maxNumIterations = 5;
9638
9639 nIterations = 0;
9640 do {
9641 /* reset f'(v) and f''(v) sums */
9642 sumLike1 = sumLike2 = 0.0;
9643
9644 /* cycle over character partitions */
9645 for (d=0; d<t->nRelParts; d++)
9646 {
9647 division = t->relParts[d];
9648
9649 /* get pointer to model */
9650 m = &modelSettings[division];
9651
9652 /* get number of model states */
9653 n = m->numModelStates;
9654
9655 /* find conditional likelihoods */
9656 clP = m->condLikes[m->condLikeIndex[chain][p->index ]];
9657 clA = m->condLikes[m->condLikeIndex[chain][p->anc->index]];
9658
9659 /* get state frequencies */
9660 pi = GetParamSubVals (m->stateFreq, chain, state[chain]);
9661
9662 /* get base rate */
9663 theRate = 1.0;
9664 baseRate = GetRate (division, chain);
9665
9666 /* get category rates */
9667 if (m->shape == NULL)
9668 catRate = &theRate;
9669 else
9670 catRate = GetParamSubVals (m->shape, chain, state[chain]);
9671
9672 /* find category frequencies and some additional stuff for the invar model */
9673 if (m->pInvar == NULL)
9674 freq = (CLFlt) (1.0 / m->numRateCats);
9675 else
9676 {
9677 /* invariable sites model */
9678 pInvar = * GetParamVals(m->pInvar, chain, state[chain]);
9679 freq = (CLFlt) ((1.0 - pInvar) / m->numRateCats);
9680 baseRate /= (1.0 - pInvar);
9681 clInvar = m->invCondLikes;
9682 lnScaler = m->scalers[m->siteScalerIndex[chain]];
9683 }
9684
9685 /* find the branch lengths times any correction factor to make them
9686 in terms of expected number of substitutions per character */
9687 length = p->length;
9688 if (m->dataType == DNA || m->dataType == RNA)
9689 {
9690 if (m->nucModelId == NUCMODEL_DOUBLET)
9691 length *= 2.0;
9692 else if (m->nucModelId == NUCMODEL_CODON)
9693 length *= 3.0;
9694 }
9695
9696 /* find nSitesOfPat */
9697 nSitesOfPat = numSitesOfPat + ((chainId[chain] % chainParams.numChains)*numCompressedChars) + m->compCharStart;
9698
9699 /* get tiProbs for current length; use the scratch location */
9700 FlipTiProbsSpace(m, chain, p->index);
9701 m->TiProbs (p, division, chain);
9702 tiP = m->tiProbs[m->tiProbsIndex[chain][p->index]];
9703 FlipTiProbsSpace(m, chain, p->index);
9704
9705 /* allocate space for first and second derivatives */
9706 tiP1 = (CLFlt *) SafeCalloc (2*m->tiProbLength, sizeof (CLFlt));
9707 if (!tiP1)
9708 return (ERROR);
9709 tiP2 = tiP1 + m->tiProbLength;
9710
9711 /* calculate first and second derivatives of P(v): P'(v) and P''(v) */
9712 index = 0;
9713 if (m->TiProbs == &TiProbs_Fels || m->TiProbs == &TiProbs_Res)
9714 {
9715 /* calculate beta */
9716 sum = 0.0;
9717 for (i=0; i<m->numModelStates; i++)
9718 for (j=i+1; j<m->numModelStates; j++)
9719 sum += pi[i]*pi[j];
9720 beta = 0.5 / sum;
9721
9722 /* calculate derivatives */
9723 for (k=0; k<m->numRateCats; k++)
9724 {
9725 v = length * catRate[k] * baseRate;
9726 expBetaV = exp (- beta * v);
9727 for (i=0; i<m->numModelStates; i++)
9728 {
9729 for (j=0; j<m->numModelStates; j++)
9730 {
9731 if (i == j)
9732 {
9733 x = - beta * (1.0 - pi[j]) * expBetaV;
9734 tiP1[index] = (CLFlt) x;
9735 tiP2[index] = (CLFlt) (- beta * x);
9736 }
9737 else
9738 {
9739 x = beta * pi[j] * expBetaV;
9740 tiP1[index] = (CLFlt) x;
9741 tiP2[index] = (CLFlt) (- beta * x);
9742 }
9743 index++;
9744 }
9745 }
9746 }
9747 }
9748 else if (m->TiProbs == &TiProbs_Hky)
9749 {
9750 /* get kappa */
9751 kappa = *GetParamVals (m->tRatio, chain, state[chain]);
9752
9753 /* calculate beta */
9754 sum = 0.0;
9755 for (i=0; i<m->numModelStates; i++)
9756 {
9757 for (j=i+1; j<m->numModelStates; j++)
9758 {
9759 if (j - i == 2)
9760 sum += kappa * pi[i] * pi[j];
9761 else
9762 sum += pi[i] * pi[j];
9763 }
9764 }
9765 beta = 0.5 / sum;
9766
9767 /* calculate derivatives */
9768 for (k=0; k<m->numRateCats; k++)
9769 {
9770 v = length * catRate[k] * baseRate;
9771 expBetaV = exp (- beta * v);
9772 for (i=0; i<m->numModelStates; i++)
9773 {
9774 for (j=0; j<m->numModelStates; j++)
9775 {
9776 s = (j + 2) % 4;
9777 bigPi = pi[j] + pi[s];
9778 w = (1.0 + bigPi * (kappa - 1.0)) * beta;
9779 expWV = exp (- w * v);
9780 if (i == j)
9781 {
9782 x = - beta * ((pi[j]/bigPi) - pi[j]) * expBetaV;
9783 y = - w * (pi[s]/bigPi) * expWV;
9784 tiP1[index] = (CLFlt) (x + y);
9785 tiP2[index] = (CLFlt) (- beta * x - w * y);
9786 }
9787 else if (abs(i-j) == 2)
9788 {
9789 x = - beta * pi[j] * ((pi[j]/bigPi) - pi[j]) * expBetaV;
9790 y = w * (pi[j]/bigPi) * expWV;
9791 tiP1[index] = (CLFlt) (x + y);
9792 tiP2[index] = (CLFlt) (- beta * x - w * y);
9793 }
9794 else
9795 {
9796 x = beta * pi[j] * expBetaV;
9797 tiP1[index] = (CLFlt) x;
9798 tiP2[index] = (CLFlt) (- beta * x);
9799 }
9800 index++;
9801 }
9802 }
9803 }
9804 }
9805 else if (m->TiProbs == &TiProbs_Gen || m->TiProbs == &TiProbs_GenCov)
9806 {
9807 /* get eigenvalues and cijk pointers */
9808 eigenValues = m->cijks[m->cijkIndex[chain]];
9809 cijk = eigenValues + (2 * n);
9810
9811 /* calculate P'(v) and P''(v) */
9812 for (k=0; k<m->numRateCats; k++)
9813 {
9814 v = length * catRate[k];
9815 for (s=0; s<n; s++)
9816 expLambdaV[s] = exp(eigenValues[s] * v);
9817 ptr = cijk;
9818 for (i=0; i<n; i++)
9819 {
9820 for (j=0; j<n; j++)
9821 {
9822 sum1 = sum2 = 0.0;
9823 for (s=0; s<n; s++)
9824 {
9825 if (fabs(eigenValues[s]) > 0.000001)
9826 {
9827 x = eigenValues[s] * (*ptr++) * expLambdaV[s];
9828 sum1 += x;
9829 sum2 += eigenValues[s] * x;
9830 }
9831 else
9832 ptr += n;
9833 }
9834 tiP1[index] = (CLFlt) ((sum1 < 0.0) ? 0.0 : sum);
9835 tiP2[index] = (CLFlt) ((sum2 < 0.0) ? 0.0 : sum);
9836 index++;
9837 }
9838 }
9839 }
9840 }
9841 else if (m->TiProbs == &TiProbs_GenCov)
9842 {
9843 /* get eigenvalues and cijk pointers */
9844 eigenValues = m->cijks[m->cijkIndex[chain]];
9845 cijk = eigenValues + (2 * n);
9846
9847 /* calculate P'(v) and P''(v) */
9848 for (k=0; k<m->numTiCats; k++)
9849 {
9850 if (m->numRateCats > 1)
9851 v = length * catRate[k];
9852 else
9853 v = length;
9854 for (s=0; s<n; s++)
9855 expLambdaV[s] = exp(eigenValues[s] * v);
9856 for (i=0; i<n; i++)
9857 {
9858 for (j=0; j<n; j++)
9859 {
9860 sum1 = sum2 = 0.0;
9861 for (s=0; s<n; s++)
9862 {
9863 if (fabs(eigenValues[s]) > 0.000001)
9864 {
9865 x = eigenValues[s] * (*cijk++) * expLambdaV[s];
9866 sum1 += x;
9867 sum2 += eigenValues[s] * x;
9868 }
9869 }
9870 tiP1[index] = (CLFlt) ((sum1 < 0.0) ? 0.0 : sum);
9871 tiP2[index] = (CLFlt) ((sum2 < 0.0) ? 0.0 : sum);
9872 index++;
9873 }
9874 }
9875 /* get new eigenvalues and cijks */
9876 eigenValues += m->cijkLength / m->nCijkParts;
9877 cijk = eigenValues + 2 * n;
9878 }
9879 }
9880 /* calculate f(v), f'(v) and f''(v) for this partition */
9881 /* note that the scalers are irrelevant because they disappear when
9882 we take the derivative of the log likelihood */
9883 if (m->gibbsGamma == YES)
9884 {
9885 /* find rate category index */
9886 rateCat = m->tiIndex + chain*m->numChars;
9887 for (c=0; c<m->numChars; c++)
9888 {
9889 like = like1 = like2 = 0.0;
9890 r = rateCat[c];
9891 if (r < m->numRateCats)
9892 {
9893 index = r*m->numModelStates*m->numModelStates;
9894 for (j=0; j<n; j++)
9895 {
9896 CLsum = CLsum1 = CLsum2 = 0.0;
9897 for (i=0; i<n; i++, index++)
9898 {
9899 CLsum += clP[i] * tiP[index];
9900 CLsum1 += clP[i] * tiP1[index];
9901 CLsum2 += clP[i] * tiP2[index];
9902 }
9903 like += CLsum * (CLFlt) pi[j] * clA[j];
9904 like1 += CLsum1 * (CLFlt) pi[j] * clA[j];
9905 like2 += CLsum2 * (CLFlt) pi[j] * clA[j];
9906 }
9907 like *= freq;
9908 sumLike1 += nSitesOfPat[c] * (like1 / like);
9909 sumLike2 += nSitesOfPat[c] * (like2 * like - like1 * like1) / (like * like);
9910 }
9911 clP += n;
9912 }
9913 }
9914 else
9915 {
9916 for (c=0; c<m->numChars; c++)
9917 {
9918 like = like1 = like2 = 0.0;
9919 index = 0;
9920 for (k=0; k<m->numTiCats; k++)
9921 {
9922 for (j=0; j<n; j++)
9923 {
9924 CLsum = CLsum1 = CLsum2 = 0.0;
9925 for (i=0; i<n; i++, index++)
9926 {
9927 CLsum += clP[i] * tiP[index];
9928 CLsum1 += clP[i] * tiP1[index];
9929 CLsum2 += clP[i] * tiP2[index];
9930 }
9931 like += CLsum * (CLFlt) pi[j] * clA[j];
9932 like1 += CLsum1 * (CLFlt) pi[j] * clA[j];
9933 like2 += CLsum2 * (CLFlt) pi[j] * clA[j];
9934 }
9935 clP += n;
9936 }
9937 like *= freq;
9938 if (m->pInvar != NULL)
9939 {
9940 /* get right like; like1 and like2 not affected */;
9941 for (i=0; i<n; i++)
9942 likeI += (*clInvar++) * pi[i];
9943 likeI *= pInvar;
9944 if (lnScaler[c] < -200.0)
9945 {
9946 /* we are not going to be able to exponentiate the scaling factor */
9947 if (likeI > 1E-70)
9948 {
9949 /* forget about like; it is going to be insignificant compared to likeI */
9950 like = (CLFlt) likeI;
9951 }
9952 else
9953 {
9954 /* treat likeI as if 0.0, that is, ignore it completely */
9955 like = like + (CLFlt)(0.0);
9956 }
9957 }
9958 else /* take both likeI and like into account */
9959 like = like + (CLFlt) (likeI * exp (-lnScaler[c]));
9960 }
9961 sumLike1 += nSitesOfPat[c] * (like1 / like);
9962 sumLike2 += nSitesOfPat[c] * (like2 * like - like1 * like1) / (like * like);
9963 }
9964 }
9965 free (tiP1);
9966 }
9967 vOld = p->length;
9968 p->length -= sumLike2 / sumLike1;
9969 nIterations++;
9970 } while (fabs(p->length - vOld) > tolerance && nIterations < maxNumIterations);
9971
9972 return (NO_ERROR);
9973 }
9974
9975
NodeToNodeDistances(Tree * t,TreeNode * fromNode)9976 void NodeToNodeDistances (Tree *t, TreeNode *fromNode)
9977 {
9978 int i;
9979 TreeNode *p;
9980
9981 /* set all distances to 0.0 and also set marks on all nodes to NO */
9982 for (i=0; i<t->nNodes; i++)
9983 {
9984 p = t->allDownPass[i];
9985 p->x = NO;
9986 p->d = 0.0;
9987 }
9988
9989 /* find distances, and mark path, below "fromNode" */
9990 for (i=0; i<t->nNodes; i++)
9991 {
9992 p = t->allDownPass[i];
9993 if (p == fromNode)
9994 {
9995 p->x = YES;
9996 }
9997 if (p->left != NULL && p->right != NULL && p->anc != NULL)
9998 {
9999 if (p->left->x == YES)
10000 {
10001 p->x = YES;
10002 p->d = p->left->d + p->left->length;
10003 }
10004 else if (p->right->x == YES)
10005 {
10006 p->x = YES;
10007 p->d = p->right->d + p->right->length;
10008 }
10009 }
10010 }
10011
10012 /* find all other distances */
10013 for (i=t->nNodes-1; i>=0; i--)
10014 {
10015 p = t->allDownPass[i];
10016 if (p->anc == NULL)
10017 {
10018 if (p == fromNode)
10019 p->d = 0.0;
10020 else
10021 p->d = p->left->d + p->left->length;
10022 }
10023 else
10024 {
10025 if (p->x == NO)
10026 {
10027 p->d = p->anc->d + p->length;
10028 }
10029 }
10030 }
10031 }
10032
10033
NumCppEvents(Param * p,int chain)10034 int NumCppEvents (Param *p, int chain)
10035 {
10036 int i, *nEvents, sumEvents;
10037
10038 nEvents = p->nEvents[2*chain+state[chain]];
10039
10040 sumEvents = 0;
10041 for (i=0; i<2*numLocalTaxa-2; i++)
10042 sumEvents += nEvents[i];
10043
10044 return (sumEvents);
10045 }
10046
10047
10048 /*----------------------------------------------------------------------
10049 |
10050 | OpenNewMBPrintFile: Open a file the first time for printing
10051 |
10052 ------------------------------------------------------------------------*/
OpenNewMBPrintFile(char * fileName)10053 FILE *OpenNewMBPrintFile (char *fileName)
10054 {
10055 int overWrite;
10056 FILE *fp;
10057
10058 /* Open file, use noWarn to determine if the user should be prompted
10059 to have the file over-written or appended. */
10060 if (noWarn == YES)
10061 {
10062 /* overwrite or append file, if already present */
10063 if ((fp = TestOpenTextFileR(fileName)) != NULL)
10064 {
10065 SafeFclose (&fp);
10066 if (autoOverwrite == NO)
10067 {
10068 MrBayesPrint ("%s Appending to file \"%s\"\n", spacer, fileName);
10069 return (OpenTextFileA(fileName));
10070 }
10071 else
10072 MrBayesPrint ("%s Overwriting file \"%s\"\n", spacer, fileName);
10073 }
10074 return (OpenTextFileW(fileName));
10075 }
10076 else
10077 {
10078 /* prompt user if file is already present */
10079 if ((fp = TestOpenTextFileR(fileName)) != NULL)
10080 {
10081 SafeFclose (&fp);
10082 MrBayesPrint ("\n");
10083 MrBayesPrint ("%s File \"%s\" already exists\n", spacer, fileName);
10084 overWrite = WantTo ("Overwrite information in this file");
10085
10086 if (overWrite == YES)
10087 {
10088 MrBayesPrint ("%s Overwriting file \"%s\"\n", spacer, fileName);
10089 return (OpenTextFileW(fileName));
10090 }
10091 else
10092 {
10093 MrBayesPrint ("%s Appending to file \"%s\"\n", spacer, fileName);
10094 return (OpenTextFileA(fileName));
10095 }
10096 }
10097
10098 else
10099 {
10100 /* file is not present */
10101 return (OpenTextFileW(fileName));
10102 }
10103 }
10104 }
10105
10106
PickProposal(RandLong * seed,int chainIndex)10107 int PickProposal (RandLong *seed, int chainIndex)
10108 {
10109 MrBFlt ran;
10110 int i;
10111
10112 ran = RandomNumber(seed);
10113
10114 for (i=0; usedMoves[i]->cumProposalProb[chainIndex] <= ran; i++);
10115
10116 return i;
10117 }
10118
10119
10120 /* Calculate positive selection probabilities */
PosSelProbs(TreeNode * p,int division,int chain)10121 int PosSelProbs (TreeNode *p, int division, int chain)
10122 {
10123 int c, j, k, nStates;
10124 MrBFlt catLike, *like, *bs, *omegaCatFreq, *omega,
10125 posProb, *ps, sum;
10126 CLFlt **clP;
10127 ModelInfo *m;
10128
10129 /* find model partition */
10130 m = &modelSettings[division];
10131
10132 /* allocate space for conditional likelihood pointer array and site likelihood array */
10133 clP = (CLFlt **) calloc (m->numOmegaCats, sizeof(CLFlt *));
10134 like = (MrBFlt *) calloc (m->numOmegaCats, sizeof(MrBFlt));
10135 if (!clP || !like)
10136 {
10137 MrBayesPrint ("%s ERROR: Out of memory in PosSelProbs\n", spacer);
10138 free (clP);
10139 free (like);
10140 return (ERROR);
10141 }
10142
10143 /* number of states */
10144 nStates = m->numModelStates;
10145
10146 /* find conditional likelihoods */
10147 clP[0] = m->condLikes[m->condLikeIndex[chain][p->index]];
10148 for (k=1; k<m->numOmegaCats; k++)
10149 clP[k] = clP[0] + k*m->numModelStates*m->numChars;
10150
10151 /* find base frequencies */
10152 bs = GetParamSubVals (m->stateFreq, chain, state[chain]);
10153
10154 /* find category frequencies */
10155 omegaCatFreq = GetParamSubVals (m->omega, chain, state[chain]);
10156
10157 /* get category omegas */
10158 omega = GetParamVals (m->omega, chain, state[chain]);
10159
10160 /* find posSelProbs */
10161 ps = posSelProbs + m->compCharStart;
10162 for (c=0; c<m->numChars; c++)
10163 {
10164 sum = 0.0;
10165 for (k=0; k<m->numOmegaCats; k++)
10166 {
10167 like[k] = 0.0;
10168 catLike = 0.0;
10169 for (j=0; j<nStates; j++)
10170 catLike += clP[k][j] * bs[j];
10171 like[k] = catLike * omegaCatFreq[k];
10172 sum += like[k];
10173 clP[k] += nStates;
10174 }
10175 posProb = 0.0;
10176 for (k=0; k<m->numOmegaCats; k++)
10177 {
10178 if (omega[k] > 1.0)
10179 posProb += like[k] / sum;
10180 }
10181 ps[c] = posProb;
10182 }
10183
10184 free (clP);
10185 free (like);
10186
10187 return NO_ERROR;
10188 }
10189
10190
10191 #if defined (SSE_ENABLED)
10192 /* Calculate positive selection probabilities (SSE version) */
PosSelProbs_SSE(TreeNode * p,int division,int chain)10193 int PosSelProbs_SSE (TreeNode *p, int division, int chain)
10194 {
10195 int i, c1, c2, j, k, nStates;
10196 CLFlt **catLike, *siteLike;
10197 MrBFlt *bs, *omegaCatFreq, *omega,
10198 posProb, *ps;
10199 __m128 m1, m2, *clPtr, **clP, mSiteLike, *mCatLike;
10200 ModelInfo *m;
10201
10202 /* find model partition */
10203 m = &modelSettings[division];
10204
10205 /* number of states */
10206 nStates = m->numModelStates;
10207
10208 /* find base frequencies */
10209 bs = GetParamSubVals (m->stateFreq, chain, state[chain]);
10210
10211 /* find category frequencies */
10212 omegaCatFreq = GetParamSubVals (m->omega, chain, state[chain]);
10213
10214 /* get category omegas */
10215 omega = GetParamVals (m->omega, chain, state[chain]);
10216 /* allocate space for category likelihood arrays */
10217 catLike = (CLFlt **) calloc (m->numOmegaCats, sizeof(CLFlt *));
10218 mCatLike = (__m128 *) calloc (m->numOmegaCats, sizeof(__m128));
10219 if (!catLike || !mCatLike)
10220 {
10221 MrBayesPrint ("%s ERROR: Out of memory in PosSelProbs_SSE\n", spacer);
10222 free (catLike);
10223 free (mCatLike);
10224 return (ERROR);
10225 }
10226
10227 /* find conditional likelihood pointers */
10228 clPtr = (__m128 *) m->condLikes[m->condLikeIndex[chain][p->index]];
10229 clP = m->clP_SSE;
10230 for (k=0; k<m->numOmegaCats; k++)
10231 {
10232 clP[k] = clPtr;
10233 clPtr += m->numVecChars * nStates;
10234 catLike[k] = (CLFlt *) (&(mCatLike[k]));
10235 }
10236 siteLike = (CLFlt *) (&mSiteLike);
10237
10238 /* find posSelProbs */
10239 ps = posSelProbs + m->compCharStart;
10240 for (c1=c2=0; c1<m->numVecChars; c1++)
10241 {
10242 mSiteLike = _mm_setzero_ps ();
10243 for (k=0; k<m->numOmegaCats; k++)
10244 {
10245 mCatLike[k] = _mm_setzero_ps();
10246 m1 = _mm_setzero_ps ();
10247 for (j=0; j<nStates; j++)
10248 {
10249 m2 = _mm_mul_ps (clP[k][j], _mm_set1_ps ((CLFlt)bs[j]));
10250 m1 = _mm_add_ps (m1, m2);
10251 }
10252 mCatLike[k] = _mm_mul_ps (m1, _mm_set1_ps ((CLFlt)omegaCatFreq[k]));
10253 mSiteLike = _mm_add_ps (mSiteLike, mCatLike[k]);
10254 clP[k] += nStates;
10255 }
10256
10257 for (i=0; i<m->numFloatsPerVec && c2 < m->numChars; ++i, ++c2)
10258 {
10259 posProb = 0.0;
10260 for (k=0; k<m->numOmegaCats; k++)
10261 {
10262 if (omega[k] > 1.0)
10263 {
10264 posProb += catLike[k][i] / siteLike[i];
10265 }
10266 }
10267 ps[c2] = posProb;
10268 }
10269 }
10270
10271 free (catLike);
10272 free (mCatLike);
10273
10274 return NO_ERROR;
10275 }
10276 #endif
10277
10278
10279 /* Calculate omega values for each site */
SiteOmegas(TreeNode * p,int division,int chain)10280 int SiteOmegas (TreeNode *p, int division, int chain)
10281 {
10282 int c, j, k, nStates;
10283 MrBFlt catLike, *like, *bs, *omegaCatFreq, *omega,
10284 siteOmega, *ps, sum;
10285 CLFlt **clP;
10286 ModelInfo *m;
10287
10288 /* find model partition */
10289 m = &modelSettings[division];
10290
10291 /* allocate space for conditional likelihood pointer array and site likelihood array */
10292 clP = (CLFlt **) calloc (m->numOmegaCats, sizeof(CLFlt *));
10293 like = (MrBFlt *) calloc (m->numOmegaCats, sizeof(MrBFlt));
10294 if (!clP || !like)
10295 {
10296 MrBayesPrint ("%s ERROR: Out of memory in SiteOmegas\n", spacer);
10297 free (clP);
10298 free (like);
10299 return (ERROR);
10300 }
10301
10302 /* number of states */
10303 nStates = m->numModelStates;
10304
10305 /* find conditional likelihoods */
10306 clP[0] = m->condLikes[m->condLikeIndex[chain][p->index]];
10307 for (k=1; k<m->numOmegaCats; k++)
10308 clP[k] = clP[0] + k*m->numModelStates*m->numChars;
10309
10310 /* find base frequencies */
10311 bs = GetParamSubVals (m->stateFreq, chain, state[chain]);
10312
10313 /* find category frequencies */
10314 omegaCatFreq = GetParamSubVals (m->omega, chain, state[chain]);
10315
10316 /* get category omegas */
10317 omega = GetParamVals (m->omega, chain, state[chain]);
10318
10319 /* find site omegas (using posSelProbs space) */
10320 ps = posSelProbs + m->compCharStart;
10321 for (c=0; c<m->numChars; c++)
10322 {
10323 sum = 0.0;
10324 for (k=0; k<m->numOmegaCats; k++)
10325 {
10326 like[k] = 0.0;
10327 catLike = 0.0;
10328 for (j=0; j<nStates; j++)
10329 catLike += clP[k][j] * bs[j];
10330 like[k] = catLike * omegaCatFreq[k];
10331 sum += like[k];
10332 clP[k] += nStates;
10333 }
10334 siteOmega = 0.0;
10335 for (k=0; k<m->numOmegaCats; k++)
10336 {
10337 siteOmega += (like[k]/sum) * omega[k];
10338 }
10339 ps[c] = siteOmega;
10340 }
10341
10342 free (clP);
10343 free (like);
10344
10345 return NO_ERROR;
10346 }
10347
10348
10349 #if defined (SSE_ENABLED)
10350 /* Calculate omega values for each site (SSE version) */
SiteOmegas_SSE(TreeNode * p,int division,int chain)10351 int SiteOmegas_SSE (TreeNode *p, int division, int chain)
10352 {
10353 int i, c1, c2, j, k, nStates;
10354 CLFlt **catLike, *siteLike;
10355 MrBFlt *bs, *omegaCatFreq, *omega,
10356 siteOmega, *ps;
10357 __m128 m1, m2, *clPtr, **clP, mSiteLike, *mCatLike;
10358 ModelInfo *m;
10359
10360 /* find model partition */
10361 m = &modelSettings[division];
10362
10363 /* number of states */
10364 nStates = m->numModelStates;
10365
10366 /* find base frequencies */
10367 bs = GetParamSubVals (m->stateFreq, chain, state[chain]);
10368
10369 /* find category frequencies */
10370 omegaCatFreq = GetParamSubVals (m->omega, chain, state[chain]);
10371
10372 /* get category omegas */
10373 omega = GetParamVals (m->omega, chain, state[chain]);
10374
10375 /* allocate space for category likelihood arrays */
10376 catLike = (CLFlt **) calloc (m->numOmegaCats, sizeof(CLFlt *));
10377 mCatLike = (__m128 *) calloc (m->numOmegaCats, sizeof(__m128));
10378 if (!catLike || !mCatLike)
10379 {
10380 MrBayesPrint ("%s ERROR: Out of memory in SiteOmegas_SSE\n", spacer);
10381 free (catLike);
10382 free (mCatLike);
10383 return (ERROR);
10384 }
10385
10386 /* find conditional likelihood pointers */
10387 clPtr = (__m128 *) m->condLikes[m->condLikeIndex[chain][p->index]];
10388 clP = m->clP_SSE;
10389 for (k=0; k<m->numOmegaCats; k++)
10390 {
10391 clP[k] = clPtr;
10392 clPtr += m->numVecChars * nStates;
10393 catLike[k] = (CLFlt *) (&(mCatLike[k]));
10394 }
10395 siteLike = (CLFlt *) (&mSiteLike);
10396
10397 /* find site omegas (using posSelProbs space) */
10398 ps = posSelProbs + m->compCharStart;
10399 for (c1=c2=0; c1<m->numVecChars; c1++)
10400 {
10401 mSiteLike = _mm_setzero_ps ();
10402 for (k=0; k<m->numOmegaCats; k++)
10403 {
10404 mCatLike[k] = _mm_setzero_ps();
10405 m1 = _mm_setzero_ps ();
10406 for (j=0; j<nStates; j++)
10407 {
10408 m2 = _mm_mul_ps (clP[k][j], _mm_set1_ps ((CLFlt)bs[j]));
10409 m1 = _mm_add_ps (m1, m2);
10410 }
10411 mCatLike[k] = _mm_mul_ps (m1, _mm_set1_ps ((CLFlt)omegaCatFreq[k]));
10412 mSiteLike = _mm_add_ps (mSiteLike, mCatLike[k]);
10413 clP[k] += nStates;
10414 }
10415
10416 for (i=0; i<m->numFloatsPerVec && c2 < m->numChars; ++i, ++c2)
10417 {
10418 siteOmega = 0.0;
10419 for (k=0; k<m->numOmegaCats; k++)
10420 {
10421 siteOmega += (catLike[k][i] / siteLike[i]) * omega[k];
10422 }
10423 ps[c2] = siteOmega;
10424 }
10425 }
10426
10427 free (catLike);
10428 free (mCatLike);
10429
10430 return NO_ERROR;
10431 }
10432 #endif
10433
10434
10435 /*----------------------------------------------------------------------
10436 |
10437 | PreparePrintFiles: Prepare .t, .p, and .mcmc files for printing
10438 |
10439 ------------------------------------------------------------------------*/
PreparePrintFiles(void)10440 int PreparePrintFiles (void)
10441 {
10442 int i, n, previousResults, oldAutoOverwrite, oldNoWarn;
10443 char localFileName[100], fileName[220], bkupName[220];
10444 FILE *tempFile;
10445
10446 #if defined (MPI_ENABLED)
10447 if (proc_id != 0)
10448 return (NO_ERROR);
10449 #endif
10450
10451 oldNoWarn = noWarn;
10452 oldAutoOverwrite = autoOverwrite;
10453
10454 /* Allocate space for file pointers */
10455 if (memAllocs[ALLOC_FILEPOINTERS] == YES)
10456 {
10457 MrBayesPrint ("%s File pointers already allocated in PreparePrintFiles\n", spacer);
10458 return ERROR;
10459 }
10460 fpMcmc = NULL;
10461 fpSS = NULL;
10462 fpParm = NULL;
10463 fpTree = NULL;
10464 fpParm = (FILE **) SafeCalloc (chainParams.numRuns, sizeof (FILE *));
10465 if (fpParm == NULL)
10466 {
10467 MrBayesPrint ("%s Could not allocate fpParm in PreparePrintFiles\n", spacer);
10468 return ERROR;
10469 }
10470 memAllocs[ALLOC_FILEPOINTERS] = YES;
10471 fpTree = (FILE ***) SafeCalloc (chainParams.numRuns, sizeof (FILE **));
10472 if (fpTree == NULL)
10473 {
10474 MrBayesPrint ("%s Could not allocate fpTree in PreparePrintFiles\n", spacer);
10475 return ERROR;
10476 }
10477 fpTree[0] = (FILE **) SafeCalloc (numTrees*chainParams.numRuns, sizeof (FILE *));
10478 if (fpTree[0] == NULL)
10479 {
10480 MrBayesPrint ("%s Could not allocate fpTree[0] in PreparePrintFiles\n", spacer);
10481 return ERROR;
10482 }
10483 for (i=1; i<chainParams.numRuns; i++)
10484 fpTree[i] = fpTree[0] + i*numTrees;
10485
10486 /* Get root of local file name */
10487 strcpy (localFileName, chainParams.chainFileName);
10488
10489 /* Determine whether to overwrite files */
10490 if (noWarn == NO)
10491 {
10492 previousResults = NO;
10493 if (chainParams.mcmcDiagn == YES)
10494 {
10495 sprintf (fileName, "%s.mcmc", localFileName);
10496 if ((tempFile = TestOpenTextFileR(fileName)) != NULL)
10497 {
10498 fclose(tempFile);
10499 previousResults = YES;
10500 }
10501 }
10502 for (n=0; n<chainParams.numRuns; n++)
10503 {
10504 if (chainParams.numRuns == 1)
10505 sprintf (fileName, "%s.p", localFileName);
10506 else
10507 sprintf (fileName, "%s.run%d.p", localFileName, n+1);
10508 if ((tempFile = TestOpenTextFileR(fileName)) != NULL)
10509 {
10510 fclose(tempFile);
10511 previousResults = YES;
10512 }
10513
10514 for (i=0; i<numTrees; i++)
10515 {
10516 if (numTrees == 1 && chainParams.numRuns == 1)
10517 sprintf (fileName, "%s.t", localFileName);
10518 else if (numTrees > 1 && chainParams.numRuns == 1)
10519 sprintf (fileName, "%s.tree%d.t", localFileName, i+1);
10520 else if (numTrees == 1 && chainParams.numRuns > 1)
10521 sprintf (fileName, "%s.run%d.t", localFileName, n+1);
10522 else
10523 sprintf (fileName, "%s.tree%d.run%d.t", localFileName, i+1, n+1);
10524 if ((tempFile = TestOpenTextFileR(fileName)) != NULL)
10525 {
10526 fclose(tempFile);
10527 previousResults = YES;
10528 }
10529 }
10530 }
10531 if (previousResults == YES)
10532 {
10533 MrBayesPrint("\n");
10534 MrBayesPrint("%s There are results from a previous run saved using the same filename(s).\n", spacer);
10535 if (WantTo("Do you want to overwrite these results") == YES)
10536 {
10537 MrBayesPrint("\n");
10538 noWarn = YES;
10539 autoOverwrite = YES;
10540 }
10541 else
10542 {
10543 MrBayesPrint("\n");
10544 MrBayesPrint("%s Please specify a different file name before running the mcmc analysis.\n", spacer);
10545 MrBayesPrint("%s You can do that using 'mcmc filename=<name>'. You can also move or\n", spacer);
10546 MrBayesPrint("%s rename the old result files.\n", spacer);
10547 return ERROR;
10548 }
10549 }
10550 }
10551
10552 /* Prepare the .mcmc file */
10553 if (chainParams.mcmcDiagn == YES)
10554 {
10555 sprintf (fileName, "%s.mcmc", chainParams.chainFileName);
10556 if ((fpMcmc = OpenNewMBPrintFile (fileName)) == NULL)
10557 {
10558 noWarn = oldNoWarn;
10559 autoOverwrite = oldAutoOverwrite;
10560 return (ERROR);
10561 }
10562 }
10563
10564 /* Prepare the .p and .t files */
10565 for (n=0; n<chainParams.numRuns; n++)
10566 {
10567 if (chainParams.numRuns == 1)
10568 sprintf (fileName, "%s.p", localFileName);
10569 else
10570 sprintf (fileName, "%s.run%d.p", localFileName, n+1);
10571 if ((fpParm[n] = OpenNewMBPrintFile (fileName)) == NULL)
10572 {
10573 noWarn = oldNoWarn;
10574 autoOverwrite = oldAutoOverwrite;
10575 return (ERROR);
10576 }
10577
10578 for (i=0; i<numTrees; i++)
10579 {
10580 if (numTrees == 1 && chainParams.numRuns == 1)
10581 sprintf (fileName, "%s.t", localFileName);
10582 else if (numTrees > 1 && chainParams.numRuns == 1)
10583 sprintf (fileName, "%s.tree%d.t", localFileName, i+1);
10584 else if (numTrees == 1 && chainParams.numRuns > 1)
10585 sprintf (fileName, "%s.run%d.t", localFileName, n+1);
10586 else
10587 sprintf (fileName, "%s.tree%d.run%d.t", localFileName, i+1, n+1);
10588 if ((fpTree[n][i] = OpenNewMBPrintFile (fileName)) == NULL)
10589 {
10590 noWarn = oldNoWarn;
10591 autoOverwrite = oldAutoOverwrite;
10592 return (ERROR);
10593 }
10594 }
10595 }
10596
10597 /* Prepare the .ss file */
10598 if (chainParams.isSS == YES)
10599 {
10600 sprintf (fileName, "%s.ss", chainParams.chainFileName);
10601 if ((fpSS = OpenNewMBPrintFile (fileName)) == NULL)
10602 {
10603 noWarn = oldNoWarn;
10604 autoOverwrite = oldAutoOverwrite;
10605 return (ERROR);
10606 }
10607 }
10608
10609 /* Remove previous chekpoint file if present */
10610 sprintf (fileName, "%s%s.ckp", workingDir, chainParams.chainFileName);
10611 strcpy (bkupName, fileName);
10612 strcat (bkupName, "~");
10613 remove (bkupName);
10614 rename (fileName, bkupName);
10615
10616 # if defined (PRINT_DUMP)
10617 fpDump = (FILE **) SafeCalloc (chainParams.numRuns, sizeof (FILE *));
10618
10619 for (n=0; n<chainParams.numRuns; n++)
10620 {
10621 if (chainParams.numRuns == 1)
10622 sprintf (fileName, "%s.dump", localFileName);
10623 else
10624 sprintf (fileName, "%s.run%d.dump", localFileName, n+1);
10625
10626 if ((fpDump[n] = OpenNewMBPrintFile (fileName)) == NULL)
10627 return (ERROR);
10628 }
10629 # endif
10630
10631 return (NO_ERROR);
10632 }
10633
10634
10635 /*----------------------------------------------------------------
10636 |
10637 | PrintAncStates_Bin: print ancestral states after final pass
10638 | Binary model with or without rate variation
10639 |
10640 -----------------------------------------------------------------*/
PrintAncStates_Bin(TreeNode * p,int division,int chain)10641 int PrintAncStates_Bin (TreeNode *p, int division, int chain)
10642 {
10643 int c, i, k;
10644 MrBFlt *bs, freq;
10645 CLFlt *clFP, *cL, sum, **clP;
10646 char *tempStr;
10647 int tempStrSize = TEMPSTRSIZE;
10648 ModelInfo *m;
10649
10650 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
10651 if (!tempStr)
10652 {
10653 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
10654 return (ERROR);
10655 }
10656
10657 /* find model settings for this division */
10658 m = &modelSettings[division];
10659
10660 /* find state frequencies */
10661 bs = GetParamSubVals (m->stateFreq, chain, state[chain]);
10662
10663 /* find frequencies of rate categories */
10664 freq = 1.0 / m->numRateCats;
10665
10666 /* find the conditional likelihoods from the final pass */
10667 clFP = m->condLikes[m->condLikeScratchIndex[p->index]];
10668
10669 clP = m->clP;
10670 for (k=0; k<m->numRateCats; k++)
10671 {
10672 clP[k] = clFP;
10673 clFP += m->numChars * m->numModelStates;
10674 }
10675
10676 /* find the preallocated working space */
10677 cL = m->ancStateCondLikes;
10678
10679 /* cycle over the compressed characters */
10680 for (c=0; c<m->numChars; c++)
10681 {
10682 cL[0] = cL[1] = 0.0;
10683 for (k=0; k<m->numRateCats; k++)
10684 {
10685 cL[0] += clP[k][0];
10686 cL[1] += clP[k][1];
10687 clP[k]+=2;
10688 }
10689 cL[0] *= (CLFlt) (bs[0] * freq);
10690 cL[1] *= (CLFlt) (bs[1] * freq);
10691 sum = cL[0] + cL[1];
10692 assert (cL[0]==cL[0]);
10693 assert (cL[1]==cL[1]);
10694 assert (sum<9999999999999999999999999999999.0);
10695 cL[0] /= sum;
10696 cL[1] /= sum;
10697 assert (cL[0]==cL[0]);
10698 assert (cL[1]==cL[1]);
10699 cL += 2;
10700 }
10701
10702 /* print the resulting conditional likelihoods cycling over uncompressed chars */
10703 for (c=0; c<numChar; c++)
10704 {
10705 if (charInfo[c].isExcluded == YES || partitionId[c][partitionNum] != division+1)
10706 continue;
10707 i = compCharPos[c] - m->compCharStart;
10708 cL = m->ancStateCondLikes + (i*2);
10709 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(cL[0]));
10710 if (AddToPrintString (tempStr) == ERROR) return (ERROR);
10711 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(cL[1]));
10712 if (AddToPrintString (tempStr) == ERROR) return (ERROR);
10713 }
10714
10715 free (tempStr);
10716 return NO_ERROR;
10717 }
10718
10719
10720 /*----------------------------------------------------------------
10721 |
10722 | PrintAncStates_Gen: print ancestral states after final pass
10723 | General model with or without rate variation
10724 |
10725 -----------------------------------------------------------------*/
PrintAncStates_Gen(TreeNode * p,int division,int chain)10726 int PrintAncStates_Gen (TreeNode *p, int division, int chain)
10727 {
10728 int c, i, k, nStates, hasPInvar, nGammaCats;
10729 const int *rateCat;
10730 MrBFlt *bsVals;
10731 CLFlt *cL, sum, pInvar=0.0, freq, f, bs[64];
10732 const CLFlt *clFP, *clInvar=NULL, *lnScaler, **clP;
10733 char *tempStr, *printedChar;
10734 int tempStrSize = TEMPSTRSIZE;
10735 ModelInfo *m;
10736
10737 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
10738 if (!tempStr)
10739 {
10740 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
10741 return (ERROR);
10742 }
10743
10744 if (!strcmp(modelParams[division].nucModel,"Codon") || !strcmp(modelParams[division].nucModel,"Protein") || !strcmp(modelParams[division].nucModel,"Doublet"))
10745 {
10746 assert (modelParams[division].dataType == DNA || modelParams[division].dataType == RNA);
10747 /* Note that we can have matrix with Protein datatype which is not and should not be covered here */
10748 printedChar = (char *) SafeMalloc (numChar*sizeof(char));
10749 }
10750 else
10751 {
10752 printedChar = NULL;
10753 }
10754
10755
10756 /* find model settings for this division */
10757 m = &modelSettings[division];
10758
10759 /* find number of states */
10760 nStates = m->numModelStates;
10761
10762 /* find state frequencies */
10763 bsVals = GetParamSubVals (m->stateFreq, chain, state[chain]);
10764 for (i=0; i<nStates; i++)
10765 bs[i] = (CLFlt) bsVals[i];
10766
10767 /* find invar cond likes */
10768 if (m->pInvar == NULL)
10769 hasPInvar = NO;
10770 else
10771 {
10772 hasPInvar = YES;
10773 clInvar = m->invCondLikes;
10774 pInvar = (CLFlt) *GetParamVals (m->pInvar, chain, state[chain]);
10775 }
10776
10777 /* find number of rate categories */
10778 nGammaCats = m->numRateCats;
10779
10780 /* find frequencies of rate categories (only relevant if gibbsGamma == NO) */
10781 freq = ((CLFlt)1.0 - pInvar) / nGammaCats;
10782
10783 /* find site scaler */
10784 lnScaler = m->scalers[m->siteScalerIndex[chain]];
10785
10786 /* find rate category index */
10787 rateCat = m->tiIndex + chain*m->numChars;
10788
10789 /* find the conditional likelihoods from the final pass */
10790 clFP = m->condLikes[m->condLikeScratchIndex[p->index]];
10791
10792 /* find the preallocated working space */
10793 cL = m->ancStateCondLikes;
10794
10795 /* cycle over the compressed characters */
10796 if (m->gibbsGamma == YES)
10797 {
10798 for (c=0; c<m->numChars; c++)
10799 {
10800 sum = 0.0;
10801 if (rateCat[c] < nGammaCats)
10802 {
10803 for (i=0; i<nStates; i++)
10804 {
10805 cL[i] = *(clFP++) * bs[i];
10806 sum += cL[i];
10807 }
10808 clInvar += nStates;
10809 }
10810 else
10811 {
10812 for (i=0; i<nStates; i++)
10813 {
10814 /* FIXME: clInvar == NULL if hasPInvar != YES (from clang static analyzer) */
10815 cL[i] = *(clInvar++) * bs[i];
10816 sum += cL[i];
10817 }
10818 clFP += nStates;
10819 }
10820 for (i=0; i<nStates; i++)
10821 cL[i] /= sum;
10822 cL += nStates;
10823 }
10824 }
10825 else
10826 {
10827 /* find conditional likelihood pointers */
10828 clP = (const CLFlt**)m->clP;
10829 for (k=0; k<m->numRateCats; k++)
10830 {
10831 clP[k] = clFP;
10832 clFP += m->numChars * m->numModelStates;
10833 }
10834 for (c=0; c<m->numChars; c++)
10835 {
10836 for (i=0; i<nStates; i++)
10837 cL[i] = 0.0;
10838 for (k=0; k<nGammaCats; k++)
10839 {
10840 for (i=0; i<nStates; i++)
10841 cL[i] += *(clP[k]++);
10842 }
10843 for (i=0; i<nStates; i++)
10844 cL[i] *= bs[i];
10845
10846 if (hasPInvar == YES && pInvar > 0)
10847 {
10848 sum = 0.0;
10849 for (i=0; i<nStates; i++)
10850 sum += clInvar[i];
10851 if (sum > 0.0)
10852 {
10853 if (lnScaler[c] < -100.0)
10854 {
10855 /* only invar cond likes are relevant */
10856 for (i=0; i<nStates; i++)
10857 cL[i] = clInvar[i] * bs[i] * pInvar;
10858 }
10859 else
10860 {
10861 f = (CLFlt) exp(lnScaler[c]) * freq;
10862 for (i=0; i<nStates; i++)
10863 cL[i] = clInvar[i] * bs[i] * pInvar + cL[i] * f;
10864 }
10865 }
10866 clInvar += nStates;
10867 }
10868
10869 sum = 0.0;
10870 for (i=0; i<nStates; i++)
10871 sum += cL[i];
10872 assert (sum > 0.0);
10873 for (i=0; i<nStates; i++)
10874 cL[i] /= sum;
10875 cL += nStates;
10876 }
10877 }
10878
10879 /* print the resulting conditional likelihoods cycling over uncompressed chars */
10880 if (printedChar)
10881 for (c=0; c<numChar; c++)
10882 printedChar[c] = NO;
10883
10884 for (c=0; c<numChar; c++)
10885 {
10886 if (charInfo[c].isExcluded == YES || partitionId[c][partitionNum] != division+1 || (printedChar &&
10887 printedChar[c] == YES))
10888 continue;
10889 i = compCharPos[c] - m->compCharStart;
10890 cL = m->ancStateCondLikes + (i*nStates);
10891 if (printedChar)
10892 {
10893 for (i=c+1; i<numChar; i++)
10894 if (charInfo[c].charId == charInfo[i].charId)
10895 printedChar[i] = YES;
10896 }
10897 for (i=0; i<nStates; i++)
10898 {
10899 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(cL[i]));
10900 if (AddToPrintString (tempStr) == ERROR) return (ERROR);
10901 }
10902 }
10903 free (tempStr);
10904 free (printedChar);
10905 return NO_ERROR;
10906 }
10907
10908
10909 /*----------------------------------------------------------------
10910 |
10911 | PrintAncStates_NUC4: print ancestral states after final pass
10912 | 4-state nucleotide model with or without rate variation
10913 |
10914 -----------------------------------------------------------------*/
PrintAncStates_NUC4(TreeNode * p,int division,int chain)10915 int PrintAncStates_NUC4 (TreeNode *p, int division, int chain)
10916 {
10917 int c, i, k, *rateCat, hasPInvar, nGammaCats;
10918 MrBFlt *bsVals;
10919 CLFlt *cL, sum, pInvar=0.0, bs[4], freq, f;
10920 const CLFlt *clFP, *clInvar=NULL, *lnScaler,**clP;
10921 char *tempStr;
10922 int tempStrSize = TEMPSTRSIZE;
10923 ModelInfo *m;
10924
10925 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
10926 if (!tempStr)
10927 {
10928 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
10929 return (ERROR);
10930 }
10931
10932 /* find model settings for this division */
10933 m = &modelSettings[division];
10934
10935 /* find state frequencies */
10936 bsVals = GetParamSubVals (m->stateFreq, chain, state[chain]);
10937 bs[A] = (CLFlt) bsVals[A];
10938 bs[C] = (CLFlt) bsVals[C];
10939 bs[G] = (CLFlt) bsVals[G];
10940 bs[T] = (CLFlt) bsVals[T];
10941
10942 /* find invar cond likes */
10943 if (m->pInvar == NULL)
10944 hasPInvar = NO;
10945 else
10946 {
10947 hasPInvar = YES;
10948 clInvar = m->invCondLikes;
10949 pInvar = (CLFlt) *GetParamVals (m->pInvar, chain, state[chain]);
10950 }
10951
10952 /* find number of rate categories */
10953 nGammaCats = m->numRateCats;
10954
10955 /* find frequencies of rate categories (only relevant if gibbsGamma == NO) */
10956 if (hasPInvar == NO)
10957 freq = (CLFlt) 1.0 / nGammaCats;
10958 else
10959 freq = ((CLFlt)1.0 - pInvar) / nGammaCats;
10960
10961 /* find site scaler */
10962 lnScaler = m->scalers[m->siteScalerIndex[chain]];
10963
10964 /* find rate category index */
10965 rateCat = m->tiIndex + chain*m->numChars;
10966
10967 /* find the conditional likelihoods from the final pass */
10968 clFP = m->condLikes[m->condLikeScratchIndex[p->index]];
10969
10970 /* find the preallocated working space */
10971 cL = m->ancStateCondLikes;
10972
10973 /* cycle over the compressed characters */
10974 if (m->gibbsGamma == YES)
10975 {
10976 for (c=0; c<m->numChars; c++)
10977 {
10978 if (rateCat[c] < nGammaCats)
10979 {
10980 cL[A] = clFP[A] * (CLFlt) bs[A];
10981 cL[C] = clFP[C] * (CLFlt) bs[C];
10982 cL[G] = clFP[G] * (CLFlt) bs[G];
10983 cL[T] = clFP[T] * (CLFlt) bs[T];
10984 }
10985 else
10986 {
10987 cL[A] = clInvar[A] * (CLFlt) bs[A];
10988 cL[C] = clInvar[C] * (CLFlt) bs[C];
10989 cL[G] = clInvar[G] * (CLFlt) bs[G];
10990 cL[T] = clInvar[T] * (CLFlt) bs[T];
10991 }
10992 sum = cL[A] + cL[C] + cL[G] + cL[T];
10993 cL[A] /= sum;
10994 cL[C] /= sum;
10995 cL[G] /= sum;
10996 cL[T] /= sum;
10997 clInvar += 4;
10998 clFP += 4;
10999 cL += 4;
11000 }
11001 }
11002 else
11003 {
11004 /* find conditional likelihood pointers */
11005 clP = (const CLFlt**)m->clP;
11006 for (k=0; k<m->numRateCats; k++)
11007 {
11008 clP[k] = clFP;
11009 clFP += m->numChars * m->numModelStates;
11010 }
11011
11012 for (c=0; c<m->numChars; c++)
11013 {
11014 cL[A] = cL[C] = cL[G] = cL[T] = 0.0;
11015 for (k=0; k<nGammaCats; k++)
11016 {
11017 cL[A] += clP[k][A];
11018 cL[C] += clP[k][C];
11019 cL[G] += clP[k][G];
11020 cL[T] += clP[k][T];
11021 clP[k] += 4;
11022 }
11023 cL[A] *= bs[A];
11024 cL[C] *= bs[C];
11025 cL[G] *= bs[G];
11026 cL[T] *= bs[T];
11027 if (hasPInvar == YES)
11028 {
11029 sum = clInvar[A] + clInvar[C] + clInvar[G] + clInvar[T];
11030 if (sum > 0.0)
11031 {
11032 if (lnScaler[c] < -100.0)
11033 {
11034 /* only invar cond likes are relevant */
11035 cL[A] = clInvar[A] * bs[A] * pInvar;
11036 cL[C] = clInvar[C] * bs[C] * pInvar;
11037 cL[G] = clInvar[G] * bs[G] * pInvar;
11038 cL[T] = clInvar[T] * bs[T] * pInvar;
11039 }
11040 else
11041 {
11042 f = (CLFlt)exp(lnScaler[c]) * freq;
11043 cL[A] = clInvar[A] * bs[A] * pInvar + cL[A] * f;
11044 cL[C] = clInvar[C] * bs[C] * pInvar + cL[C] * f;
11045 cL[G] = clInvar[G] * bs[G] * pInvar + cL[G] * f;
11046 cL[T] = clInvar[T] * bs[T] * pInvar + cL[T] * f;
11047 }
11048 }
11049 clInvar += 4;
11050 }
11051 sum = cL[A] + cL[C] + cL[G] + cL[T];
11052 cL[A] /= sum;
11053 cL[C] /= sum;
11054 cL[G] /= sum;
11055 cL[T] /= sum;
11056 cL += 4;
11057 }
11058 }
11059
11060 /* print the resulting conditional likelihoods cycling over uncompressed chars */
11061 for (c=0; c<numChar; c++)
11062 {
11063 if (charInfo[c].isExcluded == YES || partitionId[c][partitionNum] != division+1)
11064 continue;
11065 i = compCharPos[c] - m->compCharStart;
11066 cL = m->ancStateCondLikes + (i*4);
11067 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(cL[A]));
11068 if (AddToPrintString (tempStr) == ERROR) return ERROR;
11069 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(cL[C]));
11070 if (AddToPrintString (tempStr) == ERROR) return ERROR;
11071 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(cL[G]));
11072 if (AddToPrintString (tempStr) == ERROR) return ERROR;
11073 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(cL[T]));
11074 if (AddToPrintString (tempStr) == ERROR) return ERROR;
11075 }
11076 free (tempStr);
11077 return NO_ERROR;
11078 }
11079
11080
11081 /*----------------------------------------------------------------
11082 |
11083 | PrintAncStates_Std: print ancestral states after final pass
11084 | Standard model with or without rate variation
11085 |
11086 -----------------------------------------------------------------*/
PrintAncStates_Std(TreeNode * p,int division,int chain)11087 int PrintAncStates_Std (TreeNode *p, int division, int chain)
11088 {
11089 int c, i, j, k, s, nStates, numReps;
11090 MrBFlt *bsBase, *bs, freq;
11091 CLFlt *clFP, *cL, sum,** clP;
11092 char *tempStr;
11093 int tempStrSize = TEMPSTRSIZE;
11094 ModelInfo *m;
11095
11096 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
11097 if (!tempStr)
11098 {
11099 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
11100 return (ERROR);
11101 }
11102
11103 /* find model settings for this division */
11104 m = &modelSettings[division];
11105
11106 /* find state frequencies, base index */
11107 bsBase = GetParamStdStateFreqs (m->stateFreq, chain, state[chain]);
11108
11109 /* find the conditional likelihoods from the final pass */
11110 clFP = m->condLikes[m->condLikeScratchIndex[p->index]];
11111
11112 numReps=0;
11113 for (c=0; c<m->numChars; c++)
11114 {
11115 if (m->nStates[c] == 2)
11116 numReps += m->numBetaCats * 2;
11117 else
11118 numReps += m->nStates[c];
11119 }
11120
11121 /* find conditional likelihood pointers */
11122 clP = m->clP;
11123 for (k=0; k<m->numRateCats; k++)
11124 {
11125 clP[k] = clFP;
11126 clFP += numReps;
11127 }
11128
11129 /* find the preallocated working space */
11130 cL = m->ancStateCondLikes;
11131
11132 /* cycle over the compressed characters */
11133 for (c=0; c<m->numChars; c++)
11134 {
11135 nStates = m->nStates[c];
11136 bs = bsBase + m->bsIndex[c];
11137
11138 for (s=0; s<nStates; s++)
11139 cL[s] = 0.0;
11140
11141 if (nStates == 2)
11142 {
11143 freq = 1.0 / (m->numBetaCats * m->numRateCats);
11144 for (i=0; i<m->numBetaCats; i++)
11145 {
11146 for (k=0; k<m->numRateCats; k++)
11147 {
11148 for (s=0; s<nStates; s++)
11149 cL[s] += clP[k][s] * (CLFlt)(bs[s] * freq);
11150 clP[k] += nStates;
11151 }
11152 bs += nStates;
11153 }
11154 }
11155 else
11156 {
11157 freq = 1.0 / (m->numRateCats);
11158 for (k=0; k<m->numRateCats; k++)
11159 {
11160 for (s=0; s<nStates; s++)
11161 cL[s] += clP[k][s] * (CLFlt)(bs[s] * freq);
11162 clP[k] += nStates;
11163 }
11164 }
11165
11166 sum = 0.0;
11167 for (s=0; s<nStates; s++)
11168 sum += cL[s];
11169
11170 assert (sum != 0.0);
11171
11172 for (s=0; s<nStates; s++)
11173 cL[s] /= sum;
11174
11175 cL += nStates;
11176 }
11177
11178 /* print the resulting conditional likelihoods cycling over uncompressed chars */
11179 for (c=0; c<numChar; c++)
11180 {
11181 if (charInfo[c].isExcluded == YES || partitionId[c][partitionNum] != division+1)
11182 continue;
11183
11184 k = compCharPos[c] - m->compCharStart;
11185 for (i=j=0; i<k; i++)
11186 j += m->nStates[i];
11187 cL = m->ancStateCondLikes + j;
11188
11189 for (i=0; i<m->nStates[k]; i++)
11190 {
11191 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(cL[i]));
11192 if (AddToPrintString (tempStr) == ERROR) return (ERROR);
11193 }
11194 }
11195 free (tempStr);
11196 return NO_ERROR;
11197 }
11198
11199
11200 /*-----------------------------------------------------------------------
11201 |
11202 | PrintCheckPoint: Print checkpoint to file
11203 |
11204 ------------------------------------------------------------------------*/
PrintCheckPoint(int gen)11205 int PrintCheckPoint (int gen)
11206 {
11207 int i, j, k, k1, nErrors=0, run, chn, nValues, tempStrSize = TEMPSTRSIZE,
11208 hasEvents, *intValue, id, oldPrecision;
11209 char bkupFileName[220], oldBkupFileName[220], ckpFileName[220], *tempString=NULL;
11210 MrBFlt *value, clockRate;
11211 Param *p = NULL, *subParm = NULL;
11212 Tree *t;
11213 FILE *fp = NULL;
11214 MCMCMove *mv;
11215 /*ModelInfo *m = NULL;*/
11216
11217 #if defined (MPI_ENABLED)
11218 int sumErrors=0,ierror;
11219 MrBFlt r, sum;
11220 #endif
11221
11222 /* use high precision for checkpointing */
11223 oldPrecision = precision;
11224 precision = 15;
11225
11226 /* allocate tempString */
11227 if ((tempString = (char *) SafeCalloc (tempStrSize, sizeof(char))) == NULL)
11228 nErrors++;
11229
11230 #if defined (MPI_ENABLED)
11231 if (proc_id == 0)
11232 {
11233 #endif
11234 if (nErrors == 0)
11235 {
11236 /* figure out check-point file names */
11237 sprintf(ckpFileName, "%s.ckp", chainParams.chainFileName);
11238 strcpy (bkupFileName, ckpFileName);
11239 strcat (bkupFileName, "~");
11240 strcpy (oldBkupFileName, bkupFileName);
11241 strcat (oldBkupFileName, "~");
11242
11243 /* shift check-point files */
11244 remove (oldBkupFileName);
11245 rename (bkupFileName, oldBkupFileName);
11246 rename (ckpFileName, bkupFileName);
11247
11248 /* create new ckp file */
11249 if ((fp = OpenTextFileW (ckpFileName)) == NULL)
11250 {
11251 MrBayesPrint ("%s Problem opening checkpoint file\n", spacer);
11252 nErrors++;
11253 }
11254 }
11255
11256 #if defined (MPI_ENABLED)
11257 } /* end of if (proc_id == 0)*/
11258 #endif
11259
11260 ERROR_TEST2("",free(tempString),return(ERROR));
11261
11262 /* write file header */
11263 MrBayesPrintf (fp, "#NEXUS\n[ID: %s]\n[generation: %d]\n", stamp, gen);
11264
11265 if (chainParams.isSS == YES)
11266 {
11267 /* dump to .ckp file current step contribution */
11268 MrBayesPrintf (fp, "[SsAcumulators:");
11269 # if defined (MPI_ENABLED)
11270 for (j=0; j<chainParams.numRuns ; j++)
11271 {
11272 if (stepAcumulatorSS[j]==0)
11273 r=0;
11274 else
11275 r = log (stepAcumulatorSS[j]) + stepScalerSS[j];
11276 ierror = MPI_Reduce (&r,&sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
11277 if (ierror != MPI_SUCCESS)
11278 {
11279 MrBayesPrint ("%s Problem with MPI_Reduce\n", spacer);
11280 return ERROR;
11281 }
11282 if (proc_id == 0)
11283 {
11284 MrBayesPrintf (fp, " %.4f", sum);
11285 }
11286 }
11287 # else
11288 for (j=0; j<chainParams.numRuns ; j++)
11289 {
11290 MrBayesPrintf (fp, " %.4f", log (stepAcumulatorSS[j]) + stepScalerSS[j]);
11291 }
11292 # endif
11293 MrBayesPrintf (fp, "]\n");
11294 }
11295
11296 #if defined (MPI_ENABLED)
11297 if (proc_id == 0)
11298 {
11299 #endif
11300 MrBayesPrintf (fp, "\nbegin trees;\n\ttranslate\n");
11301
11302 /* remove old bkup file ('~~') */
11303 remove (oldBkupFileName);
11304
11305 /* write translate block */
11306 for (i=0; i<numLocalTaxa; i++)
11307 {
11308 if (i == numLocalTaxa - 1)
11309 MrBayesPrintf (fp, " %2d %s;\n", i+1, localTaxonNames[i]);
11310 else
11311 MrBayesPrintf (fp, " %2d %s,\n", i+1, localTaxonNames[i]);
11312 }
11313
11314 #if defined (MPI_ENABLED)
11315 }
11316 #endif
11317
11318 /* allocate space for print string */
11319 printStringSize = tempStrSize;
11320 printString = (char *) SafeCalloc ((size_t)printStringSize, sizeof(char));
11321 if (!printString)
11322 nErrors++;
11323 else
11324 strcpy(printString,"");
11325
11326 ERROR_TEST2("Memory allocation error",free(tempString),return(ERROR));
11327 /*
11328 # if defined (MPI_ENABLED)
11329 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
11330 if (sumErrors > 0)
11331 {
11332 MrBayesPrint ("%s Memory allocation error on at least one processor\n", spacer);
11333 free (tempString);
11334 return ERROR;
11335 }
11336 # else
11337 if (nErrors > 0)
11338 {
11339 free (tempString);
11340 return ERROR;
11341 }
11342 # endif
11343 */
11344 /* print trees (but not species trees) */
11345 for (i=0; i<numParams; i++)
11346 {
11347 p = ¶ms[i];
11348 if (p->paramType != P_BRLENS && p->paramType != P_TOPOLOGY)
11349 continue;
11350 if (p->paramType == P_TOPOLOGY && p->subParams[0] != p)
11351 continue;
11352 hasEvents = NO;
11353 for (j=0; j<numLocalChains; j++)
11354 {
11355 t = GetTree (p, j, state[j]);
11356 /* write the tree preamble */
11357 if (nErrors == 0 && SafeSprintf (&tempString, &tempStrSize, "\ttree %s", t->name) == ERROR)
11358 nErrors++;
11359 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11360 nErrors++;
11361 if (p->paramType == P_BRLENS && p->nSubParams > 0)
11362 {
11363 for (k=0; k<p->nSubParams; k++)
11364 {
11365 subParm = p->subParams[k];
11366 if (subParm->paramType == P_CPPEVENTS)
11367 {
11368 hasEvents = YES;
11369 if (SafeSprintf (&tempString, &tempStrSize, " [&E %s]", subParm->name) == ERROR) nErrors++;
11370 if (nErrors == 0 && AddToPrintString (tempString) == ERROR) nErrors++;
11371 }
11372 if (nErrors == 0 && (subParm->paramType == P_CPPEVENTS || subParm->paramType == P_TK02BRANCHRATES ||
11373 subParm->paramType == P_IGRBRANCHRATES || subParm->paramType == P_MIXEDBRCHRATES))
11374 {
11375 if (subParm->paramType == P_MIXEDBRCHRATES)
11376 {
11377 id = *GetParamIntVals(subParm, j, state[j]);
11378 if (SafeSprintf (&tempString, &tempStrSize, " [&B %s %d]", subParm->name, id) == ERROR) nErrors++;
11379 }
11380 else
11381 if (SafeSprintf (&tempString, &tempStrSize, " [&B %s]", subParm->name) == ERROR) nErrors++;
11382 if (nErrors == 0 && AddToPrintString (tempString) == ERROR) nErrors++;
11383 }
11384 }
11385 }
11386
11387 if (t->isRooted == YES && t->isClock == NO)
11388 SafeSprintf (&tempString, &tempStrSize, " = ");
11389 else if (t->isRooted == YES && t->isClock == YES)
11390 {
11391 clockRate = *GetParamVals(modelSettings[p->relParts[0]].clockRate, j, state[j]);
11392 SafeSprintf (&tempString, &tempStrSize, " = [&R] [&clockrate = %s] ", MbPrintNum(clockRate));
11393 }
11394 else /* if (t->isRooted == NO) */
11395 SafeSprintf (&tempString, &tempStrSize, " = ");
11396 if (nErrors == 0 && AddToPrintString (tempString) == ERROR) nErrors++;
11397 /* write the tree in (extended) Newick format */
11398 if (nErrors == 0)
11399 {
11400 if (p->paramType == P_TOPOLOGY)
11401 WriteNoEvtTreeToPrintString (t->root->left, j, p, NO, t->isRooted);
11402 else if (hasEvents == NO)
11403 WriteNoEvtTreeToPrintString (t->root->left, j, p, YES, t->isRooted);
11404 else
11405 WriteEventTreeToPrintString (t->root->left, j, p, YES);
11406 if (AddToPrintString (";\n") == ERROR)
11407 nErrors++;
11408 }
11409 }
11410 MrBayesPrintf (fp, "%s", printString);
11411 #if defined (MPI_ENABLED)
11412 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
11413 if (sumErrors > 0)
11414 {
11415 MrBayesPrint ("%s Print error on at least one processor\n", spacer);
11416 goto errorExit;
11417 }
11418 if (PrintMPISlaves(fp) == ERROR)
11419 goto errorExit;
11420 #else
11421 if (nErrors > 0)
11422 goto errorExit;
11423 #endif
11424 strcpy (printString, "");
11425 strcpy (tempString, "");
11426 }
11427 MrBayesPrintf (fp, "end;\n\n");
11428
11429 /* print species trees */
11430 if (strcmp(modelParams[0].topologyPr,"Speciestree") == 0)
11431 {
11432 /* get the first species tree */
11433 for (i=0; i<numParams; i++)
11434 {
11435 p = ¶ms[i];
11436 if (p->paramType == P_SPECIESTREE)
11437 break;
11438 }
11439 t = GetTree(p, 0, state[0]);
11440
11441 #if defined (MPI_ENABLED)
11442 if (proc_id == 0)
11443 {
11444 #endif
11445 /* write the block header and translate block */
11446 MrBayesPrintf (fp, "\nbegin trees;\n");
11447 PrintTranslateBlock (fp, t);
11448 #if defined (MPI_ENABLED)
11449 }
11450 #endif
11451
11452 for (j=0; j<numLocalChains; j++)
11453 {
11454 t = GetTree (p, j, state[j]);
11455
11456 /* write the tree preamble */
11457 if (nErrors == 0 && SafeSprintf (&tempString, &tempStrSize, "\ttree %s", t->name) == ERROR)
11458 nErrors++;
11459 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11460 nErrors++;
11461
11462 subParm = modelSettings[p->relParts[0]].popSize;
11463 if (subParm->nValues > 1)
11464 {
11465 if (SafeSprintf (&tempString, &tempStrSize, " [&N %s]", subParm->name) == ERROR) nErrors++;
11466 if (nErrors == 0 && AddToPrintString (tempString) == ERROR) nErrors++;
11467 }
11468
11469 clockRate = *GetParamVals(modelSettings[p->relParts[0]].clockRate, j, state[j]);
11470 SafeSprintf (&tempString, &tempStrSize, " = [&R] [&clockrate = %s] ", MbPrintNum(clockRate));
11471 if (nErrors == 0 && AddToPrintString (tempString) == ERROR) nErrors++;
11472
11473 /* write the tree in (extended) Newick format */
11474 if (nErrors == 0)
11475 {
11476 WriteNoEvtTreeToPrintString (t->root->left, j, p, YES, t->isRooted);
11477 if (AddToPrintString (";\n") == ERROR)
11478 nErrors++;
11479 }
11480 }
11481 MrBayesPrintf (fp, "%s", printString);
11482 #if defined (MPI_ENABLED)
11483 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
11484 if (sumErrors > 0)
11485 {
11486 MrBayesPrint ("%s Print error on at least one processor\n", spacer);
11487 goto errorExit;
11488 }
11489 if (PrintMPISlaves(fp) == ERROR)
11490 goto errorExit;
11491 #else
11492 if (nErrors > 0)
11493 goto errorExit;
11494 #endif
11495 strcpy (printString, "");
11496 strcpy (tempString, "");
11497 MrBayesPrintf (fp, "end;\n\n");
11498 }
11499
11500 /* start startvals block */
11501 MrBayesPrintf (fp,"begin mrbayes;\n");
11502 MrBayesPrintf (fp, "\tstartvals\n");
11503
11504 /* first print topology values */
11505 for (i=0; i<numParams; i++)
11506 {
11507 p = ¶ms[i];
11508 if (p->paramType == P_TOPOLOGY)
11509 {
11510 for (j=0; j<numLocalChains; j++)
11511 {
11512 t = GetTree (p, j, state[j]);
11513 run = (chainId[j] / chainParams.numChains) + 1;
11514 chn = (chainId[j] % chainParams.numChains) + 1;
11515 if (nErrors == 0 && SafeSprintf (&tempString, &tempStrSize, "\t\t%s(%d,%d)=%s\n", p->name, run, chn, t->name) == ERROR)
11516 nErrors++;
11517 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11518 nErrors++;
11519 }
11520 MrBayesPrintf (fp, "%s", printString);
11521 # if defined (MPI_ENABLED)
11522 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
11523 if (sumErrors > 0)
11524 {
11525 MrBayesPrint ("%s Print error on at least one processor\n", spacer);
11526 goto errorExit;
11527 }
11528 if (PrintMPISlaves(fp) == ERROR)
11529 goto errorExit;
11530 # else
11531 if (nErrors > 0)
11532 goto errorExit;
11533 # endif
11534 strcpy (printString, "");
11535 strcpy (tempString, "");
11536 }
11537 }
11538
11539 /* now print branch lengths and relaxed clock parameters */
11540 for (i=0; i<numParams; i++)
11541 {
11542 p = ¶ms[i];
11543 if (p->paramType == P_BRLENS)
11544 {
11545 for (j=0; j<numLocalChains; j++)
11546 {
11547 t = GetTree (p, j, state[j]);
11548 run = (chainId[j] / chainParams.numChains) + 1;
11549 chn = (chainId[j] % chainParams.numChains) + 1;
11550 if (nErrors == 0 && SafeSprintf (&tempString, &tempStrSize, "\t\t%s(%d,%d)=%s\n", p->name, run, chn, t->name) == ERROR)
11551 nErrors++;
11552 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11553 nErrors++;
11554 for (k=0; k<p->nSubParams; k++)
11555 {
11556 if (nErrors == 0 && SafeSprintf (&tempString, &tempStrSize, "\t\t%s(%d,%d)=%s\n", p->subParams[k]->name, run, chn, t->name) == ERROR)
11557 nErrors++;
11558 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11559 nErrors++;
11560 }
11561 }
11562 MrBayesPrintf (fp, "%s", printString);
11563 # if defined (MPI_ENABLED)
11564 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
11565 if (sumErrors > 0)
11566 {
11567 MrBayesPrint ("%s Print error on at least one processor\n", spacer);
11568 goto errorExit;
11569 }
11570 if (PrintMPISlaves(fp) == ERROR)
11571 goto errorExit;
11572 # else
11573 if (nErrors > 0)
11574 goto errorExit;
11575 # endif
11576 strcpy (printString, "");
11577 strcpy (tempString, "");
11578 }
11579 }
11580
11581 /* now print species tree and population size parameters */
11582 for (i=0; i<numParams; i++)
11583 {
11584 p = ¶ms[i];
11585 if (p->paramType == P_SPECIESTREE)
11586 {
11587 for (j=0; j<numLocalChains; j++)
11588 {
11589 t = GetTree (p, j, state[j]);
11590 run = (chainId[j] / chainParams.numChains) + 1;
11591 chn = (chainId[j] % chainParams.numChains) + 1;
11592 if (nErrors == 0 && SafeSprintf (&tempString, &tempStrSize, "\t\t%s(%d,%d)=%s\n", p->name, run, chn, t->name) == ERROR)
11593 nErrors++;
11594 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11595 nErrors++;
11596 if (modelSettings[p->relParts[0]].popSize->nValues > 1)
11597 if (nErrors == 0 && SafeSprintf (&tempString, &tempStrSize, "\t\t%s(%d,%d)=%s\n", modelSettings[p->relParts[0]].popSize->name, run, chn, t->name) == ERROR)
11598 nErrors++;
11599 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11600 nErrors++;
11601 }
11602 MrBayesPrintf (fp, "%s", printString);
11603 # if defined (MPI_ENABLED)
11604 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
11605 if (sumErrors > 0)
11606 {
11607 MrBayesPrint ("%s Print error on at least one processor\n", spacer);
11608 goto errorExit;
11609 }
11610 if (PrintMPISlaves(fp) == ERROR)
11611 goto errorExit;
11612 # else
11613 if (nErrors > 0)
11614 goto errorExit;
11615 # endif
11616 strcpy (printString, "");
11617 strcpy (tempString, "");
11618 }
11619 }
11620
11621 /* now print param values */
11622 for (i=0; i<numPrintParams; i++)
11623 {
11624 p = printParam[i];
11625 for (j=0; j<numLocalChains; j++)
11626 {
11627 run = (chainId[j] / chainParams.numChains) + 1;
11628 chn = (chainId[j] % chainParams.numChains) + 1;
11629 if (p->paramType == P_PI && modelSettings[p->relParts[0]].dataType != STANDARD)
11630 {
11631 value = GetParamSubVals (p, j, state[j]);
11632 nValues = p->nSubValues;
11633 }
11634 else
11635 {
11636 value = GetParamVals (p, j, state[j]);
11637 nValues = p->nValues;
11638 }
11639 if (nErrors == 0 && SafeSprintf (&tempString, &tempStrSize, "\t\t%s(%d,%d)=(%.15le", p->name, run, chn, value[0]) == ERROR)
11640 nErrors++;
11641 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11642 nErrors++;
11643 for (k=1; k<nValues; k++)
11644 {
11645 if (nErrors==0 && SafeSprintf (&tempString, &tempStrSize, ",%.15le", value[k]) == ERROR)
11646 nErrors++;
11647 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11648 nErrors++;
11649 }
11650 /* print int values if present */
11651 if (p->nIntValues > 0)
11652 {
11653 intValue = GetParamIntVals (p, j, state[j]);
11654 nValues = p->nIntValues;
11655 for (k=0; k<nValues; k++)
11656 {
11657 if (nErrors==0 && SafeSprintf (&tempString, &tempStrSize, ",%d", intValue[k]) == ERROR)
11658 nErrors++;
11659 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11660 nErrors++;
11661 }
11662 }
11663 /* print extra params for symdir multistate */
11664 if (p->nSympi > 0)
11665 {
11666 value = GetParamStdStateFreqs (p, j, state[j]);
11667 if (p->hasBinaryStd == YES)
11668 value += 2 * modelSettings[p->relParts[0]].numBetaCats;
11669 for (k=0; k<p->nSympi; k++)
11670 {
11671 for (k1=0; k1<p->sympinStates[k]; k1++)
11672 {
11673 if (nErrors==0 && SafeSprintf (&tempString, &tempStrSize, ",%.15le", *value++) == ERROR)
11674 nErrors++;
11675 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11676 nErrors++;
11677 }
11678 }
11679 }
11680 /* print extra params for omega */
11681 if (p->paramType == P_OMEGA)
11682 {
11683 value = GetParamSubVals (p, j, state[j]);
11684 nValues = p->nSubValues/2;
11685 for (k=0; k<nValues; k++)
11686 {
11687 if (nErrors==0 && SafeSprintf (&tempString, &tempStrSize, ",%.15le", value[k]) == ERROR)
11688 nErrors++;
11689 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11690 nErrors++;
11691 }
11692 }
11693 if (nErrors == 0 && AddToPrintString (")\n") == ERROR)
11694 nErrors++;
11695 }
11696 MrBayesPrintf (fp, "%s", printString);
11697 #if defined (MPI_ENABLED)
11698 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
11699 if (sumErrors > 0)
11700 {
11701 MrBayesPrint ("%s Print error on at least one processor\n", spacer);
11702 goto errorExit;
11703 }
11704 if (PrintMPISlaves(fp) == ERROR)
11705 goto errorExit;
11706 #else
11707 if (nErrors > 0)
11708 goto errorExit;
11709 #endif
11710 strcpy (printString, "");
11711 strcpy (tempString, "");
11712 }
11713 /* end startvals statement */
11714 MrBayesPrintf (fp, "\t;\n");
11715
11716 /* print tuning parameters of moves */
11717 MrBayesPrintf (fp, "\tpropset\n");
11718 for (i=0; i<numUsedMoves; i++)
11719 {
11720 mv = usedMoves[i];
11721 if (mv->moveType->Autotune == NULL)
11722 continue; /* tuning parameter(s) do not change */
11723 for (j=0; j<numLocalChains; j++)
11724 {
11725 run = (chainId[j] / chainParams.numChains) + 1;
11726 chn = (chainId[j] % chainParams.numChains) + 1;
11727 /* format is: <move_name>$<tuning_param_name>(<run>,<chain>)=<number> */
11728 if (nErrors == 0 && SafeSprintf (&tempString, &tempStrSize, "\t\t%s$%s(%d,%d)=%.15le\n",
11729 mv->name, mv->moveType->shortTuningName[0], run, chn, mv->tuningParam[chainId[j]][0]) == ERROR)
11730 nErrors++;
11731 if (nErrors == 0 && AddToPrintString (tempString) == ERROR)
11732 nErrors++;
11733 }
11734 MrBayesPrintf (fp, "%s", printString);
11735 #if defined (MPI_ENABLED)
11736 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
11737 if (sumErrors > 0)
11738 {
11739 MrBayesPrint ("%s Print error on at least one processor\n", spacer);
11740 goto errorExit;
11741 }
11742 if (PrintMPISlaves(fp) == ERROR)
11743 goto errorExit;
11744 #else
11745 if (nErrors > 0)
11746 goto errorExit;
11747 #endif
11748 strcpy (printString, "");
11749 strcpy (tempString, "");
11750 }
11751
11752 /* end propset statement */
11753 MrBayesPrintf (fp, "\t;\n");
11754
11755 /* end mrbayes block */
11756 MrBayesPrintf (fp, "end;\n\n");
11757
11758 /* change precision back */
11759 precision = oldPrecision;
11760
11761 SafeFclose (&fp);
11762 free (tempString);
11763 free (printString);
11764 printStringSize = 0;
11765 return (NO_ERROR);
11766
11767 errorExit:
11768 precision = oldPrecision;
11769 free (tempString);
11770 free (printString);
11771 printString = NULL;
11772 printStringSize = 0;
11773 SafeFclose (&fp);
11774 return (ERROR);
11775 }
11776
11777
11778 /*----------------------------------------------------------------------
11779 |
11780 | PrintMCMCDiagnosticsToFile: Print acceptance ratios, swapping
11781 | frequencies, and convergence diagnostics to file.
11782 |
11783 ------------------------------------------------------------------------*/
PrintMCMCDiagnosticsToFile(int curGen)11784 int PrintMCMCDiagnosticsToFile (int curGen)
11785 {
11786 int i, j, n;
11787 MCMCMove *theMove;
11788 char *diagnstat;
11789
11790 /* Simply print header if curGen == 0 */
11791 if (curGen == 0)
11792 {
11793 // MrBayesPrintf (fpMcmc, "[LEGEND:]\n");
11794 MrBayesPrintf (fpMcmc, "[ID: %s]\n", stamp);
11795 MrBayesPrintf (fpMcmc, "[ Gen -- Generation]\n");
11796 if (chainParams.allChains == YES)
11797 MrBayesPrintf (fpMcmc, "[ <name1>(<name2>)$acc_run2_chn3 -- Acceptance rate of move <name1> changing parameter <name2> in run 2, chain 3]\n");
11798 else /* if (chainParams.allChains == NO) */
11799 MrBayesPrintf (fpMcmc, "[ <name1>(<name2>)$acc_run2 -- Acceptance rate of move <name1> changing parameter 1 in run 2]\n");
11800 if (chainParams.numChains > 1 && chainParams.numRuns > 1)
11801 MrBayesPrintf (fpMcmc, "[ Swap(1<>2)$acc_run3 -- Acceptance rate of swaps between chains 1 and 2 in run 3]\n");
11802 else if (chainParams.numChains > 1 && chainParams.numRuns == 1)
11803 MrBayesPrintf (fpMcmc, "[ Swap(1<>2)$acc -- Acceptance rate of swaps between chains 1 and 2]\n");
11804 if (chainParams.diagnStat == AVGSTDDEV)
11805 diagnstat = "Average";
11806 else
11807 diagnstat = "Maximum";
11808 if (chainParams.numRuns > 1 && numTopologies == 1 && chainParams.allComps == NO)
11809 MrBayesPrintf (fpMcmc, "[ StdDev(s) -- %s standard deviation of split frequencies]\n", diagnstat);
11810 else if (chainParams.numRuns > 1 && numTopologies == 1 && chainParams.allComps == YES)
11811 MrBayesPrintf (fpMcmc, "[ StdDev(s)(run1-run2) -- %s standard deviation of split frequencies between runs 1 and 2]\n", diagnstat);
11812 else if (chainParams.numRuns > 1 && numTopologies > 1 && chainParams.allComps == NO)
11813 MrBayesPrintf (fpMcmc, "[ StdDev(s.tree1) -- %s standard deviation of split frequencies for topology 1]\n", diagnstat);
11814 else if (chainParams.numRuns > 1 && numTopologies > 1 && chainParams.allComps == YES)
11815 MrBayesPrintf (fpMcmc, "[ StdDev(s.tree1)(run1-run2) -- %s standard deviation of split frequencies between runs 1 and 2 for topology 1]\n", diagnstat);
11816
11817 MrBayesPrintf (fpMcmc, "Gen");
11818 for (n=0; n<chainParams.numRuns; n++)
11819 {
11820 if (chainParams.allChains == YES)
11821 {
11822 for (i=0; i<chainParams.numChains; i++)
11823 {
11824 for (j=0; j<numUsedMoves; j++)
11825 {
11826 theMove = usedMoves[j];
11827 MrBayesPrintf (fpMcmc, "\t%s(%s)$acc_run%d_chn%d", theMove->moveType->shortName,
11828 theMove->parm->name, n+1, i+1);
11829 if (theMove->moveType->Autotune != NULL && chainParams.autotune == YES)
11830 MrBayesPrintf (fpMcmc, "\t%s(%s)$%s_run%d_chn%d", theMove->moveType->shortName,
11831 theMove->parm->name, theMove->moveType->shortTuningName[0], n+1, i+1);
11832 }
11833 }
11834 }
11835 else
11836 {
11837 for (i=0; i<numUsedMoves; i++)
11838 {
11839 theMove = usedMoves[i];
11840 if (chainParams.numRuns == 1)
11841 {
11842 MrBayesPrintf (fpMcmc, "\t%s(%s)$acc", theMove->moveType->shortName, theMove->parm->name);
11843 if (theMove->moveType->Autotune != NULL && chainParams.autotune == YES)
11844 MrBayesPrintf (fpMcmc, "\t%s(%s)$%s", theMove->moveType->shortName, theMove->parm->name, theMove->moveType->shortTuningName[0]);
11845 }
11846 else
11847 {
11848 MrBayesPrintf (fpMcmc, "\t%s(%s)$acc_run%d", theMove->moveType->shortName, theMove->parm->name, n+1);
11849 if (theMove->moveType->Autotune != NULL && chainParams.autotune == YES)
11850 MrBayesPrintf (fpMcmc, "\t%s(%s)$%s_run%d", theMove->moveType->shortName, theMove->parm->name, theMove->moveType->shortTuningName[0], n+1);
11851 }
11852 }
11853 }
11854 if (chainParams.numChains > 1)
11855 {
11856 for (i=0; i<chainParams.numChains; i++)
11857 {
11858 for (j=i+1; j<chainParams.numChains; j++)
11859 {
11860 if (chainParams.numRuns == 1)
11861 MrBayesPrintf (fpMcmc, "\tSwap(%d<>%d)$acc", i+1, j+1);
11862 else
11863 MrBayesPrintf (fpMcmc, "\tSwap(%d<>%d)$acc(%d)", i+1, j+1, n+1);
11864 }
11865 }
11866 }
11867 }
11868
11869 if (chainParams.numRuns > 1)
11870 {
11871 for (n=0; n<numTopologies; n++)
11872 {
11873 if (numTopologies == 1)
11874 {
11875 if (chainParams.diagnStat == AVGSTDDEV)
11876 MrBayesPrintf (fpMcmc, "\tAvgStdDev(s)");
11877 else
11878 MrBayesPrintf (fpMcmc, "\tMaxStdDev(s)");
11879 }
11880 else
11881 {
11882 if (chainParams.diagnStat == AVGSTDDEV)
11883 MrBayesPrintf (fpMcmc, "\tAvgStdDev(s.tree%d)", n+1);
11884 else
11885 MrBayesPrintf (fpMcmc, "\tMaxStdDev(s.tree%d)", n+1);
11886 }
11887
11888 if (chainParams.allComps == YES)
11889 {
11890 for (i=0; i<chainParams.numRuns; i++)
11891 {
11892 for (j=i+1; j<chainParams.numRuns; j++)
11893 {
11894 if (numTopologies == 1)
11895 {
11896 if (chainParams.diagnStat == AVGSTDDEV)
11897 MrBayesPrintf (fpMcmc, "\tAvgStdDev(s)(run%d_run%d)", i+1,j+1);
11898 else
11899 MrBayesPrintf (fpMcmc, "\tMaxStdDev(s)(run%d_run%d)", i+1, j+1);
11900 }
11901 else
11902 {
11903 if (chainParams.diagnStat == AVGSTDDEV)
11904 MrBayesPrintf (fpMcmc, "\tAvgStdDev(s.tree%d)(run%d_run%d)", n+1, i+1, j+1);
11905 else
11906 MrBayesPrintf (fpMcmc, "\tMaxStdDev(s.tree%d)(run%d_run%d)", n+1, i+1, j+1);
11907 }
11908 }
11909 }
11910 }
11911 }
11912 }
11913 MrBayesPrintf (fpMcmc, "\n");
11914 fflush (fpMcmc);
11915 return (NO_ERROR);
11916 }
11917
11918 #if defined (MPI_ENABLED)
11919 /* Reassemble info if MPI version */
11920 if (ReassembleMoveInfo() == ERROR)
11921 return (ERROR);
11922 if (chainParams.numChains > 1 && ReassembleSwapInfo() == ERROR)
11923 return (ERROR);
11924 if (proc_id != 0)
11925 return (NO_ERROR);
11926 #endif
11927
11928 MrBayesPrintf (fpMcmc, "%d", curGen);
11929
11930 for (n=0; n<chainParams.numRuns; n++)
11931 {
11932 if (chainParams.allChains == YES)
11933 {
11934 for (j=n*chainParams.numChains; j<(n+1)*chainParams.numChains; j++)
11935 {
11936 for (i=0; i<numUsedMoves; i++)
11937 {
11938 theMove = usedMoves[i];
11939 if (theMove->nBatches[j] < 1)
11940 MrBayesPrintf (fpMcmc, "\tNA");
11941 else
11942 MrBayesPrintf (fpMcmc, "\t%.6f", theMove->lastAcceptanceRate[j]);
11943 if (theMove->moveType->Autotune != NULL && chainParams.autotune == YES)
11944 MrBayesPrintf (fpMcmc, "\t%.6e", theMove->tuningParam[j][0]);
11945 }
11946 }
11947 }
11948 else
11949 {
11950 j = n*chainParams.numChains;
11951 for (i=0; i<numUsedMoves; i++)
11952 {
11953 theMove = usedMoves[i];
11954 if (theMove->nBatches[j] < 1)
11955 MrBayesPrintf (fpMcmc, "\tNA");
11956 else
11957 MrBayesPrintf (fpMcmc, "\t%.6f", theMove->lastAcceptanceRate[j]);
11958 if (theMove->moveType->Autotune != NULL && chainParams.autotune == YES)
11959 MrBayesPrintf (fpMcmc, "\t%.6e", theMove->tuningParam[j][0]);
11960 }
11961 }
11962 if (chainParams.numChains > 1)
11963 {
11964 for (i=0; i<chainParams.numChains; i++)
11965 {
11966 for (j=i+1; j<chainParams.numChains; j++)
11967 {
11968 MrBayesPrintf (fpMcmc, "\t%.6f", (MrBFlt) swapInfo[n][i][j] / (MrBFlt) swapInfo[n][j][i]);
11969 }
11970 }
11971 }
11972 }
11973
11974 if (chainParams.numRuns > 1)
11975 {
11976 for (n=0; n<numTopologies; n++)
11977 {
11978 if (chainParams.relativeBurnin == NO && curGen < chainParams.chainBurnIn * chainParams.sampleFreq)
11979 MrBayesPrintf (fpMcmc, "\tNA");
11980 else
11981 {
11982 if (chainParams.diagnStat == AVGSTDDEV)
11983 MrBayesPrintf (fpMcmc, "\t%.6f", chainParams.stat[n].avgStdDev);
11984 else
11985 MrBayesPrintf (fpMcmc, "\t%.6f", chainParams.stat[n].max);
11986 }
11987 if (chainParams.allComps == YES)
11988 {
11989 for (i=0; i<chainParams.numRuns; i++)
11990 {
11991 for (j=i+1; j<chainParams.numRuns; j++)
11992 {
11993 if (chainParams.relativeBurnin == NO && curGen < chainParams.chainBurnIn * chainParams.sampleFreq)
11994 MrBayesPrintf (fpMcmc, "\tNA");
11995 else if (chainParams.diagnStat == AVGSTDDEV)
11996 MrBayesPrintf (fpMcmc, "\t%.6f", chainParams.stat[n].pair[i][j] / chainParams.stat[n].pair[j][i]);
11997 else /*if (chainParams.diagnStat == MAXSTDDEV) */
11998 MrBayesPrintf (fpMcmc, "\t%.6f", chainParams.stat[n].pair[i][j]);
11999 }
12000 }
12001 }
12002 }
12003 }
12004
12005 MrBayesPrintf (fpMcmc, "\n");
12006 fflush (fpMcmc);
12007
12008 #if defined MPI_ENABLED
12009 /* Redistribute the move info in the parallel version, so that
12010 swapping occurs correctly; only necessary on processor 0 */
12011 RedistributeMoveInfo();
12012 #endif
12013
12014 return (NO_ERROR);
12015 }
12016
12017
12018 /*-----------------------------------------------------------------------
12019 |
12020 | PrintMPISlaves: Print strings from MPI slave nodes
12021 |
12022 ------------------------------------------------------------------------*/
12023 #if defined (MPI_ENABLED)
PrintMPISlaves(FILE * fp)12024 int PrintMPISlaves (FILE *fp)
12025 {
12026 char *s=NULL;
12027 int i, len, ierror, nErrors, sumErrors, tag;
12028 MPI_Status status;
12029
12030 nErrors = sumErrors = tag = 0;
12031 if (proc_id==0)
12032 {
12033 s = (char *) SafeCalloc (100, sizeof(char));
12034 // if (s!=NULL)
12035 // lenS = 100;
12036 // else
12037 // lenS = 0;
12038 }
12039
12040 for (i=1; i<num_procs; i++)
12041 {
12042 /* communicate length */
12043 if (proc_id == 0)
12044 {
12045 /* receive size */
12046 ierror = MPI_Recv (&len, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
12047 if (ierror != MPI_SUCCESS)
12048 {
12049 MrBayesPrint ("%s Problem receiving string length from proc_id = %d\n", spacer, i);
12050 nErrors++;
12051 }
12052 }
12053 else if (proc_id == i)
12054 {
12055 /* send size */
12056 len = (int)strlen(printString);
12057 ierror = MPI_Send (&len, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
12058 if (ierror != MPI_SUCCESS)
12059 {
12060 MrBayesPrint ("%s Problem sending string length from proc_id = %d\n", spacer, i);
12061 nErrors++;
12062 }
12063 }
12064 /* reallocate string s on processor 0 if necessary */
12065 if (nErrors == 0 && proc_id == 0 && len+5 > strlen(s))
12066 {
12067 if ((s = (char *) SafeRealloc ((void *)s, ((size_t)len+5)*sizeof(char))) == NULL)
12068 {
12069 MrBayesPrint ("%s Problem reallocating %d chars to string 's' on proc 0 in PrintMPISlaves()\n", spacer, len+5);
12070 nErrors++;
12071 }
12072 }
12073 /* communicate and print string */
12074 if (nErrors == 0)
12075 {
12076 if (proc_id == 0)
12077 {
12078 /* receive string */
12079 ierror = MPI_Recv (s, len+1, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
12080 if (ierror != MPI_SUCCESS)
12081 {
12082 MrBayesPrint ("%s Problem receiving printString from proc_id = %d\n", spacer, i);
12083 nErrors++;
12084 }
12085 /* print string */
12086 if (nErrors == 0)
12087 MrBayesPrintf (fp, "%s", s);
12088 }
12089 else if (proc_id == i)
12090 {
12091 /* send string */
12092 ierror = MPI_Send (printString, len+1, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
12093 if (ierror != MPI_SUCCESS)
12094 {
12095 MrBayesPrint ("%s Problem sending printString from proc_id = %d\n", spacer, i);
12096 nErrors++;
12097 }
12098 }
12099 }
12100 if (nErrors > 0)
12101 break;
12102 }
12103
12104 if (proc_id == 0)
12105 {
12106 free (s);
12107 s = NULL;
12108 }
12109
12110 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
12111 if (sumErrors > 0)
12112 {
12113 MrBayesPrint ("%s Problem with the printing in PrintMPISlaves().\n", spacer);
12114 return (ERROR);
12115 }
12116
12117 return (NO_ERROR);
12118 }
12119 #endif
12120
12121
12122 /*----------------------------------------------------------------------
12123 |
12124 | PrintParamValues: print parameter values and subvalues for param
12125 |
12126 ----------------------------------------------------------------------*/
PrintParamValues(Param * p,int chain,char * s)12127 void PrintParamValues (Param *p, int chain, char *s)
12128 {
12129 int j;
12130 MrBFlt *value0, *value1;
12131
12132 if (p == NULL)
12133 MrBayesPrint ("%s %s = NULL\n", spacer, s);
12134 else
12135 {
12136 if (p->nValues > 0)
12137 {
12138 value0 = GetParamVals (p, chain, 0);
12139 value1 = GetParamVals (p, chain, 1);
12140 for (j=0; j<p->nValues; j++)
12141 MrBayesPrint ("%s hyper [%s] = (%lf %lf)\n", spacer, s, value0[j], value1[j]);
12142 }
12143 if (p->nSubValues > 0)
12144 {
12145 value0 = GetParamSubVals (p, chain, 0);
12146 value1 = GetParamSubVals (p, chain, 1);
12147 for (j=0; j<p->nSubValues; j++)
12148 MrBayesPrint ("%s %s = (%lf %lf)\n", spacer, s, value0[j], value1[j]);
12149 }
12150 }
12151 MrBayesPrint ("\n\n");
12152
12153 return;
12154 }
12155
12156
12157 /*----------------------------------------------------------------------
12158 |
12159 | PrintParsMatrix: Print parsimony (bitset) matrix
12160 | using hexadecimal representation
12161 |
12162 |
12163 ------------------------------------------------------------------------*/
PrintParsMatrix(void)12164 int PrintParsMatrix (void)
12165 {
12166 int i, j=0, k, c, d, printWidth, nextColumn, nChars, inputChar;
12167 BitsLong x, y, bitsLongOne;
12168 char ch;
12169 ModelInfo *m;
12170
12171 bitsLongOne = 1;
12172
12173 printWidth = 79;
12174
12175 for (d=0; d<numCurrentDivisions; d++)
12176 {
12177 MrBayesPrint ("\nParsimony (bitset) matrix for division %d\n\n", d+1);
12178
12179 m = &modelSettings[d];
12180
12181 nChars = 1 + (int) (log((bitsLongOne << m->numStates) - 1) / log(16));
12182
12183 for (c=0; c<m->numChars; c++)
12184 {
12185 MrBayesPrint ("Parsimony sets for character %d -- \n", (c / m->nParsIntsPerSite));
12186 for (i=0; i<numTaxa; i++)
12187 {
12188 MrBayesPrint ("%-10.10s ", taxaNames[i]);
12189 j = c*m->nParsIntsPerSite;
12190 for (nextColumn=13; nextColumn < printWidth; nextColumn+=nChars + 1)
12191 {
12192 if (j >= m->numChars*m->nParsIntsPerSite)
12193 break;
12194 x = m->parsSets[i][j];
12195 for (k=8 - nChars; k<8; k++)
12196 {
12197 y = (x >> (4* (7 - k))) & 15;
12198 // if (y > 16) printf ("y is too big %ld\n",y);
12199 if (y < 10)
12200 ch = (char) y + '0';
12201 else
12202 ch = (char) y - 10 + 'A';
12203 MrBayesPrint("%c", ch);
12204 }
12205 MrBayesPrint(" ");
12206 j++;
12207 }
12208 MrBayesPrint ("\n");
12209 }
12210 MrBayesPrint("\n");
12211 printf ("Do you want to stop (y/n)?\n");
12212 inputChar = getchar();
12213 if (inputChar == 'y' || inputChar == 'Y')
12214 return NO_ERROR;
12215 else
12216 MrBayesPrint ("\n");
12217 }
12218 } /* next division */
12219
12220 return NO_ERROR;
12221 }
12222
12223
12224 /*------------------------------------------------------------------
12225 |
12226 | PrintSiteRates_Gen: general n-state models with rate variation
12227 |
12228 -------------------------------------------------------------------*/
PrintSiteRates_Gen(TreeNode * p,int division,int chain)12229 int PrintSiteRates_Gen (TreeNode *p, int division, int chain)
12230 {
12231 int c, j, k, nStates, hasPInvar;
12232 MrBFlt freq, siteLike, invLike, catLike, pInvar=0.0, *bs,
12233 *catRate, baseRate;
12234 MrBFlt s01, s10, probOn, probOff, *swr, covBF[40];
12235 CLFlt *lnScaler, *clP, *siteRates, *clInvar=NULL;
12236 char *tempStr;
12237 int tempStrSize = TEMPSTRSIZE;
12238 ModelInfo *m;
12239
12240 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
12241 if (!tempStr)
12242 {
12243 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
12244 return (ERROR);
12245 }
12246
12247 /* find model settings and nStates, pInvar, invar cond likes */
12248 m = &modelSettings[division];
12249 nStates = m->numModelStates;
12250 if (m->pInvar == NULL)
12251 {
12252 hasPInvar = NO;
12253 }
12254 else
12255 {
12256 hasPInvar = YES;
12257 pInvar = *(GetParamVals (m->pInvar, chain, state[chain]));
12258 clInvar = m->invCondLikes;
12259 }
12260
12261 /* find conditional likelihood pointer */
12262 clP = m->condLikes[m->condLikeIndex[chain][p->index]];
12263
12264 /* use scratch space for root node for temporary calculations */
12265 siteRates = m->condLikes[m->condLikeScratchIndex[p->index]];
12266
12267 /* find site scaler */
12268 lnScaler = m->scalers[m->siteScalerIndex[chain]];
12269
12270 /* find base frequencies */
12271 bs = GetParamSubVals (m->stateFreq, chain, state[chain]);
12272
12273 /* if covarion model, adjust base frequencies */
12274 if (m->switchRates != NULL)
12275 {
12276 /* find the stationary frequencies */
12277 swr = GetParamVals(m->switchRates, chain, state[chain]);
12278 s01 = swr[0];
12279 s10 = swr[1];
12280 probOn = s01 / (s01 + s10);
12281 probOff = 1.0 - probOn;
12282
12283 /* now adjust the base frequencies; on-state stored first in cond likes */
12284 for (j=0; j<nStates/2; j++)
12285 {
12286 covBF[j] = bs[j] * probOn;
12287 covBF[j+nStates/2] = bs[j] * probOff;
12288 }
12289
12290 /* finally set bs pointer to adjusted values */
12291 bs = covBF;
12292 }
12293
12294 /* find category frequencies */
12295 if (hasPInvar == NO)
12296 freq = 1.0 / m->numRateCats;
12297 else
12298 freq = (1.0 - pInvar) / m->numRateCats;
12299
12300 /* get rate multipliers (for gamma & partition specific rates) */
12301 baseRate = GetRate (division, chain);
12302
12303 /* compensate for invariable sites */
12304 if (hasPInvar == YES)
12305 baseRate /= (1.0 - pInvar);
12306
12307 /* get category rates */
12308 catRate = GetParamSubVals (m->shape, chain, state[chain]);
12309
12310 /* loop over characters */
12311 if (hasPInvar == NO)
12312 {
12313 /* no invariable category */
12314 for (c=0; c<m->numChars; c++)
12315 {
12316 siteLike = 0.0;
12317 siteRates[c] = 0.0;
12318 for (k=0; k<m->numRateCats; k++)
12319 {
12320 catLike = 0.0;
12321 for (j=0; j<nStates; j++)
12322 catLike += (*(clP++)) * bs[j];
12323 siteRates[c] += (CLFlt) (catLike * catRate[k]);
12324 siteLike += catLike;
12325 }
12326 siteRates[c] *= (CLFlt) (baseRate / siteLike); /* category frequencies and site scaler cancel out */
12327 }
12328 }
12329 else
12330 {
12331 /* has invariable category */
12332 for (c=0; c<m->numChars; c++)
12333 {
12334 siteLike = invLike = 0.0;
12335 siteRates[c] = 0.0;
12336 for (k=0; k<m->numRateCats; k++)
12337 {
12338 catLike = 0.0;
12339 for (j=0; j<nStates; j++)
12340 catLike += (*(clP++)) * bs[j];
12341 siteRates[c] += (CLFlt) (catLike * catRate[k]);
12342 siteLike += catLike;
12343 }
12344 siteLike *= freq;
12345 siteRates[c] *= (CLFlt) freq;
12346 for (j=0; j<nStates; j++)
12347 invLike += (*(clInvar++)) * bs[j];
12348 siteLike += (invLike / exp (lnScaler[c]) * pInvar);
12349 /* we do not need to add the invariable category into siteRates before rescaling because the rate is 0.0 */
12350 siteRates[c] *= (CLFlt) (baseRate / siteLike); /* site scaler cancels out; category frequencies dealt with above */
12351 }
12352 }
12353
12354 /* print the resulting site rates cycling over uncompressed chars */
12355 for (c=0; c<numChar; c++)
12356 {
12357 if (charInfo[c].isExcluded == YES || partitionId[c][partitionNum] != division+1)
12358 continue;
12359 j = compCharPos[c] - m->compCharStart;
12360 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(siteRates[j]));
12361 if (AddToPrintString (tempStr) == ERROR) return (ERROR);
12362 }
12363
12364 free (tempStr);
12365 return NO_ERROR;
12366 }
12367
12368
12369 /*------------------------------------------------------------------
12370 |
12371 | PrintSiteRates_Std: standard model with rate variation
12372 |
12373 -------------------------------------------------------------------*/
PrintSiteRates_Std(TreeNode * p,int division,int chain)12374 int PrintSiteRates_Std (TreeNode *p, int division, int chain)
12375 {
12376 int c, j, k, nStates;
12377 MrBFlt siteLike, catLike, *bs, *catRate, baseRate;
12378 CLFlt *clP, *siteRates;
12379 char *tempStr;
12380 int tempStrSize = TEMPSTRSIZE;
12381 ModelInfo *m;
12382
12383 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
12384 if (!tempStr)
12385 {
12386 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
12387 return (ERROR);
12388 }
12389
12390 /* find model settings */
12391 m = &modelSettings[division];
12392
12393 /* find conditional likelihood pointer */
12394 clP = m->condLikes[m->condLikeIndex[chain][p->index]];
12395
12396 /* use scratch space for root node for temporary calculations */
12397 siteRates = m->condLikes[m->condLikeScratchIndex[p->index]];
12398
12399 /* find base frequencies */
12400 bs = GetParamStdStateFreqs (m->stateFreq, chain, state[chain]);
12401
12402 /* get rate multiplier */
12403 baseRate = GetRate (division, chain);
12404
12405 /* get category rates */
12406 catRate = GetParamSubVals (m->shape, chain, state[chain]);
12407
12408 /* loop over characters */
12409 for (c=0; c<m->numChars; c++)
12410 {
12411 siteLike = 0.0;
12412 siteRates[c] = 0.0;
12413 nStates = m->nStates[c];
12414 for (k=0; k<m->numRateCats; k++)
12415 {
12416 catLike = 0.0;
12417 for (j=0; j<nStates; j++)
12418 catLike += (*(clP++)) * bs[j];
12419 siteRates[c] += (CLFlt) (catLike * catRate[k]);
12420 siteLike += catLike;
12421 }
12422 siteRates[c] *= (CLFlt)(baseRate / siteLike); /* category frequencies and site scaler cancel out */
12423 }
12424
12425 /* print the resulting site rates cycling over uncompressed chars */
12426 for (c=0; c<numChar; c++)
12427 {
12428 if (charInfo[c].isExcluded == YES || partitionId[c][partitionNum] != division+1)
12429 continue;
12430 j = compCharPos[c] - m->compCharStart;
12431 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(siteRates[j]));
12432 if (AddToPrintString (tempStr) == ERROR) return (ERROR);
12433 }
12434
12435 free (tempStr);
12436 return NO_ERROR;
12437 }
12438
12439
PrintStates(int curGen,int coldId)12440 int PrintStates (int curGen, int coldId)
12441 {
12442 int d, i, j, k, k1, compressedCharPosition, *printedChar=NULL, origAlignmentChars[3];
12443 char *partString=NULL, stateString[4];
12444 MrBFlt *st, *sst, sum;
12445 Param *p;
12446 ModelInfo *m;
12447 Tree *tree;
12448 TreeNode *node;
12449 ModelParams *mp;
12450 char *tempStr;
12451 int tempStrSize;
12452
12453 /* allocate the print string */
12454 printStringSize = tempStrSize = TEMPSTRSIZE;
12455 printString = (char *)SafeMalloc((size_t)printStringSize * sizeof(char));
12456 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
12457
12458 if (!printString)
12459 {
12460 MrBayesPrint ("%s Problem allocating printString (%d)\n", spacer, printStringSize * sizeof(char));
12461 goto errorExit;
12462 }
12463 if (!tempStr)
12464 {
12465 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, (size_t)(tempStrSize) * sizeof(char));
12466 goto errorExit;
12467 }
12468
12469 *printString = '\0';
12470 *tempStr = '\0';
12471
12472 /* Allocate memory, temporarily, in case we infer positive selection probs, site omegas, or site rates */
12473 if (inferPosSel == YES || inferSiteOmegas == YES)
12474 {
12475 if (memAllocs[ALLOC_POSSELPROBS] == YES)
12476 {
12477 MrBayesPrint ("%s posSelProbs not free in PrintStates\n", spacer);
12478 goto errorExit;
12479 }
12480 posSelProbs = (MrBFlt *)SafeMalloc((size_t)numCompressedChars * sizeof(MrBFlt));
12481 if (!posSelProbs)
12482 {
12483 MrBayesPrint ("%s Problem allocating posSelProbs (%d)\n", spacer, numCompressedChars * sizeof(MrBFlt));
12484 goto errorExit;
12485 }
12486 for (i=0; i<numCompressedChars; i++)
12487 posSelProbs[i] = -10.0;
12488 memAllocs[ALLOC_POSSELPROBS] = YES;
12489 }
12490 if (inferPosSel == YES || inferSiteOmegas == YES || inferSiteRates == YES || inferAncStates == YES)
12491 {
12492 printedChar = (int *)SafeMalloc((size_t)numChar * sizeof(int));
12493 if (!printedChar)
12494 {
12495 MrBayesPrint ("%s Problem allocating printedChar (%d)\n", spacer, numChar * sizeof(int));
12496 goto errorExit;
12497 }
12498 for (i=0; i<numChar; i++)
12499 printedChar[i] = NO;
12500 }
12501
12502 /* Set up the header to the file. */
12503 if (curGen == 0)
12504 {
12505 SafeSprintf (&tempStr, &tempStrSize, "[ID: %s]\n", stamp);
12506 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12507 SafeSprintf (&tempStr, &tempStrSize, "Gen");
12508 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12509 SafeSprintf (&tempStr, &tempStrSize, "\tLnL");
12510 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12511 SafeSprintf (&tempStr, &tempStrSize, "\tLnPr");
12512 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12513
12514 for (i=0; i<numParams; i++)
12515 {
12516 p = ¶ms[i];
12517 if (p->paramType == P_BRLENS)
12518 {
12519 /* print tree lengths or heights for all trees */
12520 tree = GetTree (p, coldId, state[coldId]);
12521 if (tree->isRooted == YES)
12522 {
12523 if (FillRelPartsString(p, &partString) == YES)
12524 SafeSprintf (&tempStr, &tempStrSize, "\tTH%s\tTL%s", partString, partString);
12525 else
12526 SafeSprintf (&tempStr, &tempStrSize, "\tTH\tTL");
12527 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12528 }
12529 else
12530 {
12531 if (FillRelPartsString(p, &partString) == YES)
12532 SafeSprintf (&tempStr, &tempStrSize, "\tTL%s", partString);
12533 else
12534 SafeSprintf (&tempStr, &tempStrSize, "\tTL");
12535 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12536 }
12537 /* print # cpp events, or relaxed clock model indicator */
12538 for (j=0; j<p->nSubParams; j++)
12539 {
12540 if (p->subParams[j]->paramType == P_CPPEVENTS)
12541 {
12542 if (FillRelPartsString(p->subParams[j], &partString) == YES)
12543 SafeSprintf (&tempStr, &tempStrSize, "\tn_CPP%s", partString);
12544 else
12545 SafeSprintf (&tempStr, &tempStrSize, "\tn_CPP");
12546 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12547 }
12548 else if (p->subParams[j]->paramType == P_MIXEDBRCHRATES)
12549 {
12550 if (FillRelPartsString(p->subParams[j], &partString) == YES)
12551 SafeSprintf (&tempStr, &tempStrSize, "\tm_RCl%s", partString);
12552 else
12553 SafeSprintf (&tempStr, &tempStrSize, "\tm_RCl");
12554 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12555 }
12556 }
12557 }
12558 /* print proportion of ancestral fossils */
12559 else if (p->paramType == P_FOSLRATE)
12560 {
12561 if (FillRelPartsString(p, &partString) == YES)
12562 {
12563 SafeSprintf (&tempStr, &tempStrSize, "\tprop_ancfossil%s", partString);
12564 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12565 }
12566 else
12567 {
12568 SafeSprintf (&tempStr, &tempStrSize, "\tprop_ancfossil");
12569 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12570 }
12571 }
12572 }
12573
12574 /* print ordinary parameters header */
12575 for (i=0; i<numPrintParams; i++)
12576 {
12577 p = printParam[i];
12578 SafeSprintf (&tempStr, &tempStrSize, "\t%s", p->paramHeader);
12579 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12580 }
12581
12582 /* print substitution model parameters header */
12583 if (inferSiteRates == YES)
12584 {
12585 for (i=0; i<numChar; i++)
12586 printedChar[i] = NO;
12587
12588 for (i=0; i<numChar; i++)
12589 {
12590 if (charInfo[i].isExcluded == YES)
12591 continue;
12592 if (printedChar[i] == YES)
12593 continue;
12594 d = partitionId[i][partitionNum] - 1;
12595 m = &modelSettings[d];
12596
12597 if (m->printSiteRates == YES)
12598 {
12599 if (m->nCharsPerSite == 1)
12600 {
12601 SafeSprintf (&tempStr, &tempStrSize, "\tr(%d)", i+1);
12602 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12603 }
12604 else
12605 {
12606 origAlignmentChars[0] = i;
12607 k = 1;
12608 for (j=i+1; j<numChar; j++)
12609 {
12610 if (compCharPos[i] == compCharPos[j])
12611 {
12612 if (k > m->nCharsPerSite)
12613 return (ERROR);
12614 origAlignmentChars[k++] = j;
12615 printedChar[j] = YES;
12616 }
12617 }
12618 if (k != m->nCharsPerSite)
12619 return (ERROR);
12620 SafeSprintf (&tempStr, &tempStrSize, "\tr(%d,", origAlignmentChars[0]);
12621 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12622 for (j=1; j<k-1; j++)
12623 {
12624 SafeSprintf (&tempStr, &tempStrSize, "%d,", origAlignmentChars[j]);
12625 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12626 }
12627 SafeSprintf (&tempStr, &tempStrSize, "%d)", origAlignmentChars[k-1]);
12628 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12629 }
12630 }
12631 }
12632 }
12633
12634 if (inferPosSel == YES)
12635 {
12636 for (i=0; i<numChar; i++)
12637 printedChar[i] = NO;
12638 for (d=0; d<numCurrentDivisions; d++)
12639 {
12640 m = &modelSettings[d];
12641 tree = GetTree(m->brlens, coldId, state[coldId]);
12642 if (m->printPosSel == YES)
12643 {
12644 if (m->PosSelProbs (tree->root->left, d, coldId) == ERROR)
12645 goto errorExit;
12646 }
12647 }
12648 /* for (i=0; i<numChar; i++)
12649 printf ("%4d -- %3d %3d\n", i, compCharPos[i], compColPos[i]); */
12650 for (i=0; i<numChar; i++)
12651 {
12652 compressedCharPosition = compCharPos[i];
12653 if (!(posSelProbs[compressedCharPosition] < 0.0) && printedChar[i] == NO && charInfo[i].isExcluded == NO)
12654 {
12655 for (j=k=0; j<numChar; j++)
12656 {
12657 if (charInfo[j].charId == charInfo[i].charId)
12658 {
12659 origAlignmentChars[k++] = j;
12660 printedChar[j] = YES;
12661 }
12662 }
12663 SafeSprintf (&tempStr, &tempStrSize, "\tpr+(%d,%d,%d)", origAlignmentChars[0]+1, origAlignmentChars[1]+1, origAlignmentChars[2]+1);
12664 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12665 }
12666 }
12667 for (i=0; i<numChar; i++)
12668 printedChar[i] = NO;
12669 }
12670
12671 if (inferSiteOmegas == YES)
12672 {
12673 for (i=0; i<numChar; i++)
12674 printedChar[i] = NO;
12675 for (d=0; d<numCurrentDivisions; d++)
12676 {
12677 m = &modelSettings[d];
12678 tree = GetTree(m->brlens, coldId, state[coldId]);
12679 if (m->printSiteOmegas == YES)
12680 {
12681 if (m->SiteOmegas (tree->root->left, d, coldId) == ERROR)
12682 goto errorExit;
12683 }
12684 }
12685 /* for (i=0; i<numChar; i++)
12686 printf ("%4d -- %3d %3d\n", i, compCharPos[i], compColPos[i]); */
12687 for (i=0; i<numChar; i++)
12688 {
12689 compressedCharPosition = compCharPos[i];
12690 if (posSelProbs[compressedCharPosition] >= 0.0 && printedChar[i] == NO && charInfo[i].isExcluded == NO)
12691 {
12692 for (j=k=0; j<numChar; j++)
12693 {
12694 if (charInfo[j].charId == charInfo[i].charId)
12695 {
12696 origAlignmentChars[k++] = j;
12697 printedChar[j] = YES;
12698 }
12699 }
12700 SafeSprintf (&tempStr, &tempStrSize, "\tomega(%d,%d,%d)", origAlignmentChars[0]+1, origAlignmentChars[1]+1, origAlignmentChars[2]+1);
12701 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12702 }
12703 }
12704 for (i=0; i<numChar; i++)
12705 printedChar[i] = NO;
12706 }
12707
12708 if (inferAncStates == YES)
12709 {
12710 for (j=0; j<numChar; j++)
12711 printedChar[j] = NO;
12712 for (d=0; d<numCurrentDivisions; d++)
12713 {
12714 m = &modelSettings[d];
12715 mp = &modelParams[d];
12716 if (m->printAncStates != YES)
12717 continue;
12718 for (i=0; i<numDefinedConstraints; i++)
12719 {
12720 if (mp->activeConstraints[i] == NO || definedConstraintsType[i] != HARD)
12721 continue;
12722 for (j=0; j<numChar; j++)
12723 {
12724 if (partitionId[j][partitionNum] - 1 != d || charInfo[j].isExcluded == YES || printedChar[j] == YES)
12725 continue;
12726 if (mp->dataType == STANDARD)
12727 {
12728 for (k=0; k<m->nStates[compCharPos[j] - m->compCharStart]; k++)
12729 {
12730 SafeSprintf (&tempStr, &tempStrSize, "\tp(%c){%d@%s}", m->StateCode(k), j+1, constraintNames[i]);
12731 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12732 }
12733 }
12734 else if ((mp->dataType == DNA || mp->dataType == RNA) && !strcmp(mp->nucModel,"Codon"))
12735 {
12736 origAlignmentChars[0] = j+1;
12737 k1 = 1;
12738 for (k=j+1; k<numChar; k++)
12739 {
12740 if (charInfo[k].charId == charInfo[j].charId)
12741 {
12742 origAlignmentChars[k1++] = k+1;
12743 printedChar[k] = YES;
12744 }
12745 }
12746 for (k=0; k<m->numStates; k++)
12747 {
12748 State_CODON(stateString, k, d);
12749 SafeSprintf (&tempStr, &tempStrSize, "\tp(%s){%d,%d,%d@%s}",
12750 stateString,
12751 origAlignmentChars[0],
12752 origAlignmentChars[1],
12753 origAlignmentChars[2],
12754 constraintNames[i]);
12755 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12756 }
12757 }
12758 else if ((mp->dataType == DNA || mp->dataType == RNA) && !strcmp(mp->nucModel,"Doublet"))
12759 {
12760 origAlignmentChars[0] = j+1;
12761 k1 = 1;
12762 for (k=j+1; k<numChar; k++)
12763 {
12764 if (charInfo[k].charId == charInfo[j].charId)
12765 {
12766 origAlignmentChars[k1++] = k+1;
12767 printedChar[k] = YES;
12768 }
12769 }
12770 for (k=0; k<m->numStates; k++)
12771 {
12772 State_DOUBLET(stateString, k);
12773 SafeSprintf (&tempStr, &tempStrSize, "\tp(%s){%d,%d@%s}",
12774 stateString,
12775 origAlignmentChars[0],
12776 origAlignmentChars[1],
12777 constraintNames[i]);
12778 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12779 }
12780 }
12781 else if ((mp->dataType == DNA || mp->dataType == RNA) && !strcmp(mp->nucModel,"Protein"))
12782 {
12783 origAlignmentChars[0] = j+1;
12784 k1 = 1;
12785 for (k=j+1; k<numChar; k++)
12786 {
12787 if (charInfo[k].charId == charInfo[j].charId)
12788 {
12789 origAlignmentChars[k1++] = k+1;
12790 printedChar[k] = YES;
12791 }
12792 }
12793 for (k=0; k<m->numStates; k++)
12794 {
12795 SafeSprintf (&tempStr, &tempStrSize, "\tp(%c){%d,%d,%d@%s}",
12796 m->StateCode(k),
12797 origAlignmentChars[0],
12798 origAlignmentChars[1],
12799 origAlignmentChars[2],
12800 constraintNames[i]);
12801 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12802 }
12803 }
12804 else
12805 {
12806 for (k=0; k<m->numStates; k++)
12807 {
12808 SafeSprintf (&tempStr, &tempStrSize, "\tp(%c){%d@%s}", m->StateCode(k), j+1, constraintNames[i]);
12809 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12810 }
12811 }
12812 }
12813 }
12814 }
12815 }
12816
12817 SafeSprintf (&tempStr, &tempStrSize, "\n");
12818 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12819 }
12820
12821 /* now print parameter values */
12822 SafeSprintf (&tempStr, &tempStrSize, "%d", curGen);
12823 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12824 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(curLnL[coldId]));
12825 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12826 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(curLnPr[coldId]));
12827 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12828
12829 for (i=0; i<numParams; i++)
12830 {
12831 p = ¶ms[i];
12832 if (p->paramType == P_BRLENS)
12833 {
12834 /* print tree lengths or heights for all trees */
12835 tree = GetTree (p, coldId, state[coldId]);
12836 if (tree->isRooted == NO)
12837 {
12838 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(TreeLength(p, coldId)));
12839 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12840 }
12841 else
12842 {
12843 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(tree->root->left->nodeDepth));
12844 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12845 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(TreeLength(p, coldId)));
12846 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12847 }
12848 /* print # cpp events, or relaxed clock model indicator */
12849 for (j=0; j<p->nSubParams; j++)
12850 {
12851 if (p->subParams[j]->paramType == P_CPPEVENTS)
12852 {
12853 SafeSprintf (&tempStr, &tempStrSize, "\t%d", NumCppEvents(p->subParams[j],coldId));
12854 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12855 }
12856 else if (p->subParams[j]->paramType == P_MIXEDBRCHRATES)
12857 {
12858 SafeSprintf (&tempStr, &tempStrSize, "\t%d", *GetParamIntVals(p->subParams[j],coldId,state[coldId]));
12859 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12860 }
12861 }
12862 }
12863 else if (p->paramType == P_FOSLRATE)
12864 {
12865 /* print proportion of ancestral fossils */
12866 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(PropAncFossil(p, coldId)));
12867 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12868 }
12869 }
12870
12871 /* print ordinary parameters */
12872 for (i=0; i<numPrintParams; i++)
12873 {
12874 p = printParam[i];
12875
12876 /* get model params and settings */
12877 mp = &modelParams[p->relParts[0]];
12878 m = &modelSettings[p->relParts[0]];
12879
12880 st = GetParamVals (p, coldId, state[coldId]);
12881 sst = GetParamSubVals (p, coldId, state[coldId]);
12882
12883 if (p->paramId == SYMPI_EXP_MS || p->paramId == SYMPI_UNI_MS || p->paramId == SYMPI_FIX_MS)
12884 {
12885 /* We print symmetric dirichlet alpha value if not fixed and then multistate character state frequencies */
12886 if (p->paramId != SYMPI_FIX_MS)
12887 {
12888 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(st[0]));
12889 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12890 }
12891 sst = GetParamStdStateFreqs (p, coldId, state[coldId]);
12892 if (p->hasBinaryStd == YES)
12893 sst += 2 * m->numBetaCats;
12894 for (j=0; j<p->nSympi; j++)
12895 {
12896 for (k=0; k<p->sympinStates[j]; k++)
12897 {
12898 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(*sst++));
12899 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12900 }
12901 }
12902 }
12903 else if (p->paramType == P_PI || p->paramType == P_MIXTURE_RATES)
12904 {
12905 /* We print the subvalues if we are dealing with state frequencies (state frequencies are held in subvalues) OR
12906 if we are dealing with rates of a siterate mixture (rates held in subvalues). */
12907 for (j=0; j<p->nSubValues; j++)
12908 {
12909 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(sst[j]));
12910 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12911 }
12912 }
12913 else if (p->paramType == P_TRATIO && !strcmp(mp->tratioFormat,"Dirichlet"))
12914 {
12915 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(st[0] / (1.0 + st[0])));
12916 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12917 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(1.0 / (1.0 + st[0])));
12918 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12919 }
12920 else if (p->paramType == P_REVMAT)
12921 {
12922 if (!strcmp(mp->revmatFormat,"Ratio"))
12923 {
12924 sum = st[p->nValues-1];
12925 for (j=0; j<p->nValues; j++)
12926 {
12927 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(st[j] / sum));
12928 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12929 }
12930 }
12931 else
12932 {
12933 /* we already have rate proportions */
12934 for (j=0; j<p->nValues; j++)
12935 {
12936 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(st[j]));
12937 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12938 }
12939 }
12940 if (p->paramId == REVMAT_MIX)
12941 {
12942 /* add model index and k for nst=mixed */
12943 SafeSprintf (&tempStr, &tempStrSize, "\t%d", FromGrowthFxnToIndex(GetParamIntVals(p, coldId, state[coldId])));
12944 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12945 SafeSprintf (&tempStr, &tempStrSize, "\t%d", GetKFromGrowthFxn(GetParamIntVals(p, coldId, state[coldId])));
12946 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12947 }
12948 }
12949 else if (p->paramType == P_RATEMULT)
12950 {
12951 if (!strcmp(mp->ratemultFormat,"Ratio"))
12952 {
12953 for (j=0; j<p->nValues; j++)
12954 {
12955 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(st[j] / st[0]));
12956 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12957 }
12958 }
12959 else if (!strcmp(mp->ratemultFormat, "Dirichlet"))
12960 {
12961 sum = 0.0;
12962 for (j=0; j<p->nValues; j++)
12963 sum += st[j];
12964 for (j=0; j<p->nValues; j++)
12965 {
12966 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(st[j] / sum));
12967 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12968 }
12969 }
12970 else
12971 {
12972 for (j=0; j<p->nValues; j++)
12973 {
12974 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(st[j]));
12975 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12976 }
12977 }
12978 }
12979 else if (p->paramType == P_AAMODEL)
12980 {
12981 for (j=0; j<p->nValues; j++)
12982 {
12983 SafeSprintf (&tempStr, &tempStrSize, "\t%d", (int)st[j]);
12984 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12985 }
12986 }
12987 else
12988 {
12989 /* run of the mill parameter */
12990 for (j=0; j<p->nValues; j++)
12991 {
12992 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(st[j]));
12993 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
12994 }
12995 }
12996
12997 if (p->paramType == P_OMEGA && p->paramId != OMEGA_DIR && p->paramId != OMEGA_FIX && p->paramId != OMEGA_FFF && p->paramId != OMEGA_FF && p->paramId != OMEGA_10FFF)
12998 {
12999 /* OK, we also need to print subvalues for the category frequencies in a NY98-like model. */
13000 if (!strcmp(mp->omegaVar, "M10"))
13001 {
13002 for (j=0; j<4; j++)
13003 {
13004 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(sst[mp->numM10BetaCats + mp->numM10GammaCats + 4 + j]));
13005 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
13006 }
13007 for (j=0; j<2; j++)
13008 {
13009 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(sst[mp->numM10BetaCats + mp->numM10GammaCats + j]));
13010 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
13011 }
13012 }
13013 else
13014 {
13015 for (j=0; j<3; j++)
13016 {
13017 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(sst[j]));
13018 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
13019 }
13020 }
13021 }
13022 }
13023
13024 /* if user wants site rates, we print those here */
13025 if (inferSiteRates == YES)
13026 {
13027 for (d=0; d<numCurrentDivisions; d++)
13028 {
13029 m = &modelSettings[d];
13030 if (m->printSiteRates == YES)
13031 {
13032 tree = GetTree (m->brlens, coldId, state[coldId]);
13033 node = tree->root->left;
13034 m->PrintSiteRates (node, d, coldId);
13035 }
13036 }
13037 }
13038
13039 /* If the user wants to infer sites that are under positive selection, then we need to print out the posterior
13040 probability that each site is a positively selected one here. */
13041 if (inferPosSel == YES)
13042 {
13043 /* loop over the divisions, calculating the probability of being in the positively
13044 selected class for each relevant partition */
13045 for (d=0; d<numCurrentDivisions; d++)
13046 {
13047 m = &modelSettings[d];
13048 tree = GetTree(m->brlens, coldId, state[coldId]);
13049 if (m->printPosSel == YES)
13050 {
13051 if (m->PosSelProbs (tree->root->left, d, coldId) == ERROR)
13052 {
13053 goto errorExit;
13054 }
13055 }
13056 }
13057
13058 /* print the probabilities for the appropriate sites in the original alignment */
13059 for (i=0; i<numChar; i++)
13060 printedChar[i] = NO;
13061 for (i=0; i<numChar; i++)
13062 {
13063 compressedCharPosition = compCharPos[i];
13064 if (!(posSelProbs[compressedCharPosition] < 0.0) && printedChar[i] == NO && charInfo[i].isExcluded == NO)
13065 {
13066 for (j=k=0; j<numChar; j++)
13067 {
13068 if (charInfo[j].charId == charInfo[i].charId)
13069 {
13070 origAlignmentChars[k++] = j;
13071 printedChar[j] = YES;
13072 }
13073 }
13074 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(posSelProbs[compressedCharPosition]));
13075 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
13076 /* printf ("%4d -> (%3d,%3d,%3d) %1.25le\n", i, origAlignmentChars[0]+1, origAlignmentChars[1]+1,
13077 origAlignmentChars[2]+1, posSelProbs[compressedCharPosition]); */
13078 }
13079 }
13080 }
13081
13082 /* If the user wants omega values for each site, we print those here. */
13083 if (inferSiteOmegas == YES)
13084 {
13085 /* loop over the divisions, calculating the omega value for each site */
13086 for (d=0; d<numCurrentDivisions; d++)
13087 {
13088 m = &modelSettings[d];
13089 tree = GetTree(m->brlens, coldId, state[coldId]);
13090 if (m->printSiteOmegas == YES)
13091 {
13092 if (m->SiteOmegas (tree->root->left, d, coldId) == ERROR)
13093 {
13094 goto errorExit;
13095 }
13096 }
13097 }
13098
13099 /* print the site omegas for the appropriate sites in the original alignment */
13100 /* note that we use posSelProbs to pass values between SiteOmegas and this function */
13101 for (i=0; i<numChar; i++)
13102 printedChar[i] = NO;
13103 for (i=0; i<numChar; i++)
13104 {
13105 compressedCharPosition = compCharPos[i];
13106 if (posSelProbs[compressedCharPosition] >= 0.0 && printedChar[i] == NO && charInfo[i].isExcluded == NO)
13107 {
13108 for (j=k=0; j<numChar; j++)
13109 {
13110 if (charInfo[j].charId == charInfo[i].charId)
13111 {
13112 origAlignmentChars[k++] = j;
13113 printedChar[j] = YES;
13114 }
13115 }
13116 SafeSprintf (&tempStr, &tempStrSize, "\t%s", MbPrintNum(posSelProbs[compressedCharPosition]));
13117 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
13118 /* printf ("%4d -> (%3d,%3d,%3d) %1.25le\n", i, origAlignmentChars[0]+1, origAlignmentChars[1]+1,
13119 origAlignmentChars[2]+1, posSelProbs[compressedCharPosition]); */
13120 }
13121 }
13122 }
13123
13124 /* free memory for positive selection probs or site omegas */
13125 if (inferPosSel == YES || inferSiteOmegas == YES)
13126 {
13127 if (memAllocs[ALLOC_POSSELPROBS] == YES)
13128 free (posSelProbs);
13129 memAllocs[ALLOC_POSSELPROBS] = NO;
13130 free (printedChar);
13131 }
13132
13133 /* if user wants ancestral states for constrained nodes, we obtain and print those here */
13134 if (inferAncStates == YES)
13135 {
13136 for (d=0; d<numCurrentDivisions; d++)
13137 {
13138 m = &modelSettings[d];
13139 if (m->printAncStates == YES)
13140 {
13141 mp = &modelParams[d];
13142 tree = GetTree (m->brlens, coldId, state[coldId]);
13143 for (i=j=tree->nIntNodes - 1; i>=0; i--)
13144 {
13145 node = tree->intDownPass[i];
13146 m->CondLikeUp (node, d, coldId);
13147 }
13148 for (k=0; k<numDefinedConstraints; k++)
13149 {
13150 if (mp->activeConstraints[k] == NO || definedConstraintsType[k] != HARD)
13151 continue;
13152 for (i=tree->nIntNodes-1; i>=0; i--)
13153 {
13154 node = tree->intDownPass[i];
13155 if (node->isLocked == YES && k == node->lockID)
13156 m->PrintAncStates (node, d, coldId);
13157 }
13158 }
13159 }
13160 }
13161 }
13162
13163 SafeSprintf (&tempStr, &tempStrSize, "\n");
13164 if (AddToPrintString (tempStr) == ERROR) goto errorExit;
13165
13166 free (tempStr);
13167 SAFEFREE (partString);
13168
13169 return (NO_ERROR);
13170
13171 errorExit:
13172 if (printedChar)
13173 free (printedChar);
13174 if (memAllocs[ALLOC_POSSELPROBS] == YES)
13175 free (posSelProbs);
13176 memAllocs[ALLOC_POSSELPROBS] = NO;
13177 free (tempStr);
13178 SAFEFREE (partString);
13179 return (ERROR);
13180 }
13181
13182
13183 /*----------------------------------------------------------------------
13184 |
13185 | PrintStatesToFiles: Print trees and model parameters to files. We
13186 | only come into this function if it is the first cycle of the chain
13187 | or if we hit a cycle number evenly divisible by the sample frequency,
13188 | or this is the last cycle of the chain.
13189 |
13190 ------------------------------------------------------------------------*/
PrintStatesToFiles(int curGen)13191 int PrintStatesToFiles (int curGen)
13192 {
13193 int i, j, chn, coldId, runId;
13194 MrBFlt clockRate;
13195 Tree *tree=NULL;
13196 Param *param;
13197 # if defined (MPI_ENABLED)
13198 int id, x, doesThisProcHaveId, procWithChain, ierror, tag, nErrors, sumErrors;
13199 MPI_Status status;
13200 # endif
13201
13202 # if !defined (MPI_ENABLED)
13203
13204 /* print parameter values and trees (single-processor version) */
13205 for (chn=0; chn<numLocalChains; chn++)
13206 {
13207 if ((chainId[chn] % chainParams.numChains) == 0)
13208 {
13209 coldId = chn;
13210 runId = chainId[chn] / chainParams.numChains;
13211
13212 /* print parameter values */
13213 if (PrintStates (curGen, coldId) == ERROR)
13214 return (ERROR);
13215 fprintf (fpParm[runId], "%s", printString);
13216 fflush (fpParm[runId]);
13217 free(printString);
13218
13219 /* print trees */
13220 for (i=0; i<numPrintTreeParams; i++)
13221 {
13222 param = printTreeParam[i];
13223 tree = GetTree(param, coldId, state[coldId]);
13224 if (param->paramType == P_TOPOLOGY)
13225 {
13226 if (tree->isClock == YES)
13227 clockRate = *GetParamVals(modelSettings[tree->relParts[0]].clockRate, coldId, state[coldId]);
13228 else
13229 clockRate = 0.0;
13230 if (PrintTree (curGen, param, coldId, NO, clockRate) == ERROR)
13231 return (ERROR);
13232 }
13233 else
13234 {
13235 if (tree->isClock == YES)
13236 clockRate = *GetParamVals(modelSettings[tree->relParts[0]].clockRate, coldId, state[coldId]);
13237 else
13238 clockRate = 0.0;
13239 if (PrintTree (curGen, param, coldId, YES, clockRate) == ERROR)
13240 return (ERROR);
13241 }
13242
13243 fprintf (fpTree[runId][i], "%s", printString);
13244 fflush (fpTree[runId][i]);
13245 free(printString);
13246
13247 j = printTreeTopologyIndex[i];
13248 if (j<numTopologies)
13249 {
13250 if (chainParams.mcmcDiagn == YES && chainParams.numRuns > 1)
13251 {
13252 if (chainParams.relativeBurnin == YES || curGen >= chainParams.chainBurnIn * chainParams.sampleFreq)
13253 {
13254 if (AddTreeToPartitionCounters (tree, j, runId) == ERROR)
13255 return ERROR;
13256 if (chainParams.relativeBurnin == YES && chainParams.saveTrees == YES && (noWarn == NO || curGen <= chainParams.stopTreeGen))
13257 {
13258 ResetTopologyFromTree (chainParams.dtree, tree);
13259 if (AddToTreeList (&chainParams.treeList[numTopologies*runId+j], chainParams.dtree) == ERROR)
13260 return (ERROR);
13261 }
13262 }
13263 }
13264 }
13265 }
13266 }
13267 }
13268 # else
13269 /* print parameter values and trees (parallel version) */
13270
13271 /* Wait for all of the processors to get to this point before starting the printing. */
13272 ierror = MPI_Barrier (MPI_COMM_WORLD);
13273 if (ierror != MPI_SUCCESS)
13274 {
13275 MrBayesPrint ("%s Problem at chain barrier.\n", spacer);
13276 return ERROR;
13277 }
13278 tag = nErrors = 0;
13279
13280 /* Loop over runs. */
13281 for (runId=0; runId<chainParams.numRuns; runId++)
13282 {
13283 /* Get the ID of the chain we want to print. Remember, the ID's should be numbered
13284 0, 1, 2, ..., numChains X numRuns. Chains numbered 0, numChains, 2 X numChains, ...
13285 are cold. */
13286 id = runId * chainParams.numChains;
13287
13288 /* Does this processor have the chain? */
13289 doesThisProcHaveId = NO;
13290 coldId = 0;
13291 for (chn=0; chn<numLocalChains; chn++)
13292 {
13293 if (chainId[chn] == id)
13294 {
13295 doesThisProcHaveId = YES;
13296 coldId = chn;
13297 break;
13298 }
13299 }
13300
13301 /* Tell all the processors which has the chain we want to print. We do this using the MPI_AllReduce
13302 function. If the processor does not have the chain, then it initializes x = 0. If it does
13303 have the chain, then x = proc_id. When the value of x is summed over all the processors, the sum
13304 should be the proc_id of the processor with the chain. Possible values are 0, 1, 2, num_procs-1.
13305 Note that every processor knows procWithChain because we are using MPI_Allreduce, instead of MPI_Reduce. */
13306 x = 0;
13307 if (doesThisProcHaveId == YES)
13308 x = proc_id;
13309 ierror = MPI_Allreduce (&x, &procWithChain, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
13310 if (ierror != MPI_SUCCESS)
13311 {
13312 MrBayesPrint ("%s Problem finding processor with chain to print.\n", spacer);
13313 return (ERROR);
13314 }
13315
13316 /* ****************************************************************************************************/
13317 /* print parameter values *****************************************************************************/
13318
13319 /* Fill printString with the contents to be printed on proc_id = 0. Note
13320 that printString is allocated in the function. */
13321 if (doesThisProcHaveId == YES)
13322 {
13323 if (PrintStates (curGen, coldId) == ERROR)
13324 nErrors++;
13325 }
13326 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
13327 if (sumErrors > 0)
13328 {
13329 MrBayesPrint ("%s Problem with PrintStates.\n", spacer);
13330 return ERROR;
13331 }
13332
13333 /* First communication: Send/receive the length of the printString. */
13334 if (proc_id == 0 || proc_id == procWithChain)
13335 {
13336 if (procWithChain != 0)
13337 {
13338 if (proc_id == procWithChain)
13339 {
13340 /* Find out how large the string is, and send the information to proc_id = 0. */
13341 ierror = MPI_Send (&printStringSize, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
13342 if (ierror != MPI_SUCCESS)
13343 nErrors++;
13344 }
13345 else
13346 {
13347 /* Receive the length of the string from proc_id = procWithChain, and then allocate
13348 printString to be that length. */
13349 ierror = MPI_Recv (&printStringSize, 1, MPI_LONG, procWithChain, tag, MPI_COMM_WORLD, &status);
13350 if (ierror != MPI_SUCCESS)
13351 {
13352 MrBayesPrint ("%s Problem receiving printStringSize from proc_id = %d\n", spacer, procWithChain);
13353 nErrors++;
13354 }
13355 printString = (char *)SafeMalloc((size_t)printStringSize * sizeof(char));
13356 if (!printString)
13357 {
13358 MrBayesPrint ("%s Problem allocating printString (%d)\n", spacer, printStringSize * sizeof(char));
13359 nErrors++;
13360 }
13361 strcpy (printString, "");
13362 }
13363 }
13364 }
13365 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
13366 if (sumErrors > 0)
13367 {
13368 MrBayesPrint ("%s Problem with first communication (states).\n", spacer);
13369 return ERROR;
13370 }
13371
13372 /* Second communication: Send/receive the printString. */
13373 if (proc_id == 0 || proc_id == procWithChain)
13374 {
13375 if (procWithChain != 0)
13376 {
13377 if (proc_id == procWithChain)
13378 {
13379 /* Send the printString to proc_id = 0. After we send the string to proc_id = 0, we can
13380 free it. */
13381 ierror = MPI_Send (&printString[0], (int)printStringSize, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
13382 if (ierror != MPI_SUCCESS)
13383 nErrors++;
13384 free(printString);
13385 }
13386 else
13387 {
13388 /* Receive the printString from proc_id = procWithChain. */
13389 ierror = MPI_Recv (&printString[0], (int)printStringSize, MPI_CHAR, procWithChain, tag, MPI_COMM_WORLD, &status);
13390 if (ierror != MPI_SUCCESS)
13391 {
13392 MrBayesPrint ("%s Problem receiving printString from proc_id = %d\n", spacer, procWithChain);
13393 nErrors++;
13394 }
13395 }
13396 }
13397 }
13398 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
13399 if (sumErrors > 0)
13400 {
13401 MrBayesPrint ("%s Problem with second communication (states).\n", spacer);
13402 return ERROR;
13403 }
13404
13405 /* Print the string with the parameter information if we are proc_id = 0. */
13406 if (proc_id == 0)
13407 {
13408 fprintf (fpParm[runId], "%s", printString);
13409 fflush (fpParm[runId]);
13410 free(printString);
13411 }
13412
13413 /* ****************************************************************************************************/
13414 /* print trees ****************************************************************************************/
13415
13416 for (i=0; i<numPrintTreeParams; i++)
13417 {
13418 /* Print trees to file. */
13419
13420 /* Fill printString with the contents to be printed on proc_id = 0. Note
13421 that printString is allocated in the function. */
13422 if (doesThisProcHaveId == YES)
13423 {
13424 param = printTreeParam[i];
13425 tree = GetTree(param, coldId, state[coldId]);
13426 if (param->paramType == P_TOPOLOGY)
13427 {
13428 if (tree->isClock == YES)
13429 clockRate = *GetParamVals(modelSettings[tree->relParts[0]].clockRate, coldId, state[coldId]);
13430 else
13431 clockRate = 0.0;
13432 if (PrintTree (curGen, param, coldId, NO, clockRate) == ERROR)
13433 nErrors++;
13434 }
13435 else
13436 {
13437 if (tree->isClock == YES)
13438 clockRate = *GetParamVals(modelSettings[tree->relParts[0]].clockRate, coldId, state[coldId]);
13439 else
13440 clockRate = 0.0;
13441 if (PrintTree (curGen, param, coldId, YES, clockRate) == ERROR)
13442 nErrors++;
13443 }
13444 }
13445 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
13446 if (sumErrors > 0)
13447 {
13448 MrBayesPrint ("%s Problem with printing trees.\n", spacer);
13449 return ERROR;
13450 }
13451
13452 /* First communication: Send/receive the length of the printString. */
13453 if (proc_id == 0 || proc_id == procWithChain)
13454 {
13455 if (procWithChain != 0)
13456 {
13457 if (proc_id == procWithChain)
13458 {
13459 /* Find out how large the string is, and send the information to proc_id = 0. */
13460 ierror = MPI_Send (&printStringSize, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
13461 if (ierror != MPI_SUCCESS)
13462 nErrors++;
13463 }
13464 else
13465 {
13466 /* Receive the length of the string from proc_id = procWithChain, and then allocate
13467 printString to be that length. */
13468 ierror = MPI_Recv (&printStringSize, 1, MPI_LONG, procWithChain, tag, MPI_COMM_WORLD, &status);
13469 if (ierror != MPI_SUCCESS)
13470 {
13471 MrBayesPrint ("%s Problem receiving printStringSize from proc_id = %d\n", spacer, procWithChain);
13472 nErrors++;
13473 }
13474 printString = (char *)SafeMalloc((size_t)printStringSize * sizeof(char));
13475 if (!printString)
13476 {
13477 MrBayesPrint ("%s Problem allocating printString (%d)\n", spacer, printStringSize * sizeof(char));
13478 nErrors++;
13479 }
13480 strcpy (printString, "");
13481 }
13482 }
13483 }
13484 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
13485 if (sumErrors > 0)
13486 {
13487 MrBayesPrint ("%s Problem with first communication (states).\n", spacer);
13488 return ERROR;
13489 }
13490
13491 /* Second communication: Send/receive the printString. */
13492 if (proc_id == 0 || proc_id == procWithChain)
13493 {
13494 if (procWithChain != 0)
13495 {
13496 if (proc_id == procWithChain)
13497 {
13498 /* Send the printString to proc_id = 0. After we send the string to proc_id = 0, we can
13499 free it. */
13500 ierror = MPI_Send (&printString[0], (int)printStringSize, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
13501 if (ierror != MPI_SUCCESS)
13502 nErrors++;
13503 free(printString);
13504 }
13505 else
13506 {
13507 /* Receive the printString from proc_id = procWithChain. */
13508 ierror = MPI_Recv (&printString[0], (int)printStringSize, MPI_CHAR, procWithChain, tag, MPI_COMM_WORLD, &status);
13509 if (ierror != MPI_SUCCESS)
13510 {
13511 MrBayesPrint ("%s Problem receiving printString from proc_id = %d\n", spacer, procWithChain);
13512 nErrors++;
13513 }
13514 }
13515 }
13516 }
13517 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
13518 if (sumErrors > 0)
13519 {
13520 MrBayesPrint ("%s Problem with second communication (states).\n", spacer);
13521 return ERROR;
13522 }
13523
13524 /* Print the string with the parameter information if we are proc_id = 0. */
13525 if (proc_id == 0)
13526 {
13527 fprintf (fpTree[runId][i], "%s", printString);
13528 fflush (fpTree[runId][i]);
13529 j = printTreeTopologyIndex[i];
13530 if (j < numTopologies)
13531 {
13532 if (chainParams.numRuns > 1 && chainParams.mcmcDiagn == YES)
13533 {
13534 if (chainParams.relativeBurnin == YES || curGen >= chainParams.chainBurnIn * chainParams.sampleFreq)
13535 {
13536 char *s = NULL;
13537 StripComments (printString);
13538 /* if it is the first tree, we strip out the translate block first (twice) */
13539 if (curGen==0) {
13540 if (strtok (printString, ";")==NULL) /* get translate lock */
13541 return (ERROR);
13542 if (strtok (NULL, ";")==NULL)
13543 return (ERROR);
13544 if (strtok (NULL, "\n\t\r ")==NULL) /* get 'tree' */
13545 return (ERROR);
13546 }
13547 else {
13548 if (strtok (printString, "\n\t\r ")==NULL) /* get 'tree' */
13549 return (ERROR);
13550 }
13551 if (strtok (NULL, " =")==NULL) /* get 'rep.xxxx' */
13552 return (ERROR);
13553 if ((s = strtok (NULL, " =;"))==NULL) /* get Newick string */
13554 return (ERROR);
13555 ResetTopology (chainParams.dtree, s);
13556 if (AddTreeToPartitionCounters (chainParams.dtree, j, runId) == ERROR)
13557 return ERROR;
13558 if (chainParams.relativeBurnin == YES && chainParams.saveTrees == YES && (noWarn == NO || curGen <= chainParams.stopTreeGen))
13559 {
13560 if (AddToTreeList (&chainParams.treeList[runId*numTopologies+j], chainParams.dtree) == ERROR)
13561 return (ERROR);
13562 }
13563 }
13564 }
13565 }
13566 free(printString);
13567 }
13568 }
13569
13570 /* Have all of the chains wait here, until the string has been successfully printed on proc_id = 0. */
13571 ierror = MPI_Barrier (MPI_COMM_WORLD);
13572 if (ierror != MPI_SUCCESS)
13573 {
13574 MrBayesPrint ("%s Problem at chain barrier.\n", spacer);
13575 return ERROR;
13576 }
13577 }
13578 # endif
13579
13580 return (NO_ERROR);
13581 }
13582
13583
PrintSwapInfo(void)13584 int PrintSwapInfo (void)
13585 {
13586 int i, j, n, maxNumExchanges, len, maxLen, reweightingChars=0;
13587 char *tempStr;
13588 int tempStrSize;
13589
13590 if (chainParams.numChains == 1)
13591 return NO_ERROR;
13592
13593 # if defined (MPI_ENABLED)
13594 if (ReassembleSwapInfo() == ERROR)
13595 return ERROR;
13596 if (proc_id != 0)
13597 return NO_ERROR;
13598 # endif
13599
13600 tempStrSize = TEMPSTRSIZE;
13601 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
13602 if (!tempStr)
13603 {
13604 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
13605 return (ERROR);
13606 }
13607
13608 for (n=0; n<chainParams.numRuns; n++)
13609 {
13610 maxNumExchanges = 0;
13611 for (i=0; i<chainParams.numChains; i++)
13612 for (j=0; j<chainParams.numChains; j++)
13613 if (i > j && swapInfo[n][i][j] > maxNumExchanges)
13614 maxNumExchanges = swapInfo[n][i][j];
13615 SafeSprintf (&tempStr, &tempStrSize, "%d", maxNumExchanges);
13616 maxLen = (int) strlen(tempStr);
13617 if (maxLen < 4)
13618 maxLen = 4;
13619
13620 reweightingChars = NO;
13621 if ((chainParams.weightScheme[0] + chainParams.weightScheme[1]) > 0.00001)
13622 reweightingChars = YES;
13623
13624 if (chainParams.numRuns == 1)
13625 MrBayesPrint ("\n%s Chain swap information:\n\n", spacer);
13626 else
13627 MrBayesPrint ("\n%s Chain swap information for run %d:\n\n", spacer, n+1);
13628
13629 MrBayesPrint ("%s ", spacer);
13630 for (j=0; j<chainParams.numChains; j++)
13631 {
13632 SafeSprintf (&tempStr, &tempStrSize, "%d", j+1);
13633 len = (int) strlen(tempStr);
13634 MrBayesPrint ("%*c %d ", maxLen-len, ' ', j+1);
13635 }
13636 MrBayesPrint ("\n");
13637
13638 MrBayesPrint ("%s --", spacer);
13639 for (j=0; j<chainParams.numChains; j++)
13640 {
13641 MrBayesPrint ("--");
13642 for (i=0; i<maxLen; i++)
13643 MrBayesPrint ("-");
13644 }
13645 MrBayesPrint ("\n");
13646
13647 for (i=0; i<chainParams.numChains; i++)
13648 {
13649 MrBayesPrint ("%s %4d | ", spacer, i+1);
13650 for (j=0; j<chainParams.numChains; j++)
13651 {
13652 if (i < j)
13653 {
13654 if (swapInfo[n][j][i] <= 0)
13655 {
13656 MrBayesPrint ("%*c%s ", maxLen-3, ' ', " NA ");
13657 }
13658 else
13659 {
13660 SafeSprintf (&tempStr, &tempStrSize, "%1.2lf", (MrBFlt)swapInfo[n][i][j]/swapInfo[n][j][i]);
13661 len = (int) strlen(tempStr);
13662 MrBayesPrint ("%*c%1.2lf ", maxLen-len+1, ' ', (MrBFlt)swapInfo[n][i][j]/swapInfo[n][j][i]);
13663 }
13664 }
13665 else if (i == j)
13666 {
13667 MrBayesPrint ("%*c ", maxLen+1, ' ');
13668 }
13669 else
13670 {
13671 SafeSprintf (&tempStr, &tempStrSize, "%d", swapInfo[n][i][j]);
13672 len = (int) strlen(tempStr);
13673 MrBayesPrint ("%*c%d ", maxLen-len+1, ' ', swapInfo[n][i][j]);
13674 }
13675 }
13676 MrBayesPrint ("\n");
13677 }
13678 }
13679
13680 MrBayesPrint ("\n%s Upper diagonal: Proportion of successful state exchanges between chains\n", spacer);
13681 MrBayesPrint ("%s Lower diagonal: Number of attempted state exchanges between chains\n", spacer);
13682
13683 MrBayesPrint ("\n%s Chain information:\n\n", spacer);
13684 MrBayesPrint ("%s ID -- Heat ", spacer);
13685 if (reweightingChars == YES)
13686 MrBayesPrint ("%% Dn %% Up\n");
13687 else
13688 MrBayesPrint ("\n");
13689
13690 MrBayesPrint ("%s -----------", spacer);
13691 if (reweightingChars == YES)
13692 MrBayesPrint ("----------\n");
13693 else
13694 MrBayesPrint ("\n");
13695 for (i=0; i<chainParams.numChains; i++)
13696 {
13697 MrBayesPrint ("%s %4d -- %1.2lf ", spacer, i+1, Temperature (i)/*1.0 / (1.0 + chainParams.chainTemp * i)*/);
13698 if (reweightingChars == YES)
13699 {
13700 if (i == 0)
13701 {
13702 MrBayesPrint (" 0%% 0%% (cold chain)\n");
13703 }
13704 else
13705 {
13706 SafeSprintf (&tempStr, &tempStrSize, "%d", (int)chainParams.weightScheme[0]);
13707 len = (int) strlen(tempStr);
13708 MrBayesPrint ("%*c%d%% ", 3-len, ' ', (int)chainParams.weightScheme[0]);
13709 SafeSprintf (&tempStr, &tempStrSize, "%d", (int)chainParams.weightScheme[1]);
13710 len = (int) strlen(tempStr);
13711 MrBayesPrint ("%*c%d%% \n", 3-len, ' ', (int)chainParams.weightScheme[1]);
13712 }
13713 }
13714 else
13715 {
13716 if (i == 0)
13717 MrBayesPrint (" (cold chain)\n");
13718 else
13719 MrBayesPrint ("\n");
13720 }
13721 }
13722 if (chainParams.userDefinedTemps == NO)
13723 {
13724 MrBayesPrint ("\n%s Heat = 1 / (1 + T * (ID - 1))\n", spacer);
13725 MrBayesPrint ("%s (where T = %1.2lf is the temperature and ID is the chain number)\n", spacer, chainParams.chainTemp);
13726 }
13727 if (reweightingChars == YES)
13728 MrBayesPrint ("%s Reweighting increment = %1.2lf\n", spacer, chainParams.weightScheme[2]);
13729 MrBayesPrint ("\n");
13730
13731 free (tempStr);
13732 return (NO_ERROR);
13733 }
13734
13735
13736 /*----------------------------------------------------------------------
13737 |
13738 | PrintTermState: Print terminal state index matrix
13739 |
13740 ------------------------------------------------------------------------*/
PrintTermState(void)13741 int PrintTermState (void)
13742 {
13743 int i, j=0, c, d, printWidth, nextColumn, nDigits, nReps;
13744 ModelInfo *m;
13745 ModelParams *mp;
13746
13747 printWidth = 79;
13748
13749 for (d=0; d<numCurrentDivisions; d++)
13750 {
13751 MrBayesPrint ("\nTerminal state index matrix for division %d\n\n", d+1);
13752
13753 m = &modelSettings[d];
13754 mp = &modelParams[d];
13755
13756 if (!strcmp(mp->covarionModel, "Yes"))
13757 nReps = 2;
13758 else
13759 nReps = 1;
13760
13761 nDigits = 1 + (int)(log10(mp->nStates * mp->nStates * nReps));
13762
13763 for (c=m->compCharStart; c<m->compCharStop; c=j)
13764 {
13765 for (i=0; i<numTaxa; i++)
13766 {
13767 MrBayesPrint ("%-10.10s ", taxaNames[i]);
13768 j = c;
13769 for (nextColumn=13; nextColumn < printWidth; nextColumn+=nDigits + 1)
13770 {
13771 if (j >= m->compCharStop)
13772 break;
13773 MrBayesPrint ("%*d ",nDigits, m->termState[i][j-c]);
13774 j++;
13775 }
13776 MrBayesPrint ("\n");
13777 }
13778 MrBayesPrint("\n");
13779 }
13780 } /* next division */
13781
13782 return NO_ERROR;
13783 }
13784
13785
13786 /*--------------------------------------------------
13787 |
13788 | PrintTiProbs: This function is for debugging of
13789 | tiProbs; it will print a square matrix of
13790 | tiProbs, check row sums, and check for time
13791 | reversibility
13792 |
13793 ---------------------------------------------------*/
PrintTiProbs(CLFlt * tP,MrBFlt * bs,int nStates)13794 void PrintTiProbs (CLFlt *tP, MrBFlt *bs, int nStates)
13795 {
13796 int i, j;
13797 CLFlt *tiP, sum;
13798
13799 tiP = tP;
13800
13801 printf ("\nTransition matrix\n");
13802 for (i=0; i<nStates; i++)
13803 {
13804 printf ("\t%d", i);
13805 }
13806 printf ("\tsum\n");
13807
13808 for (i=0; i<nStates; i++)
13809 {
13810 printf ("%d\t", i);
13811 sum = 0.0;
13812 for (j=0; j<nStates; j++)
13813 {
13814 printf ("\t%.6f",tP[j]);
13815 sum += tP[j];
13816 }
13817 printf ("\t%.6f\n",sum);
13818 tP += nStates;
13819 }
13820
13821 printf ("\nStationary state frequencies\n");
13822 for (i=0; i<nStates; i++)
13823 printf ("%d -- %f\n",i,bs[i]);
13824
13825 printf ("\nTime reversibility\n");
13826
13827 printf ("State 1\tState 2\tforward\tbackward\tabs diff\n");
13828 for (i=0; i<nStates; i++)
13829 {
13830 for (j=i+1; j<nStates; j++)
13831 {
13832 printf ("%d\t%d\t%.6f\t%.6f\t%.6f\n", i, j, tiP[i*nStates+j]*bs[i],
13833 tiP[j*nStates+i]*bs[j], fabs(tiP[i*nStates+j]*bs[i] - tiP[j*nStates+i]*bs[j]));
13834 }
13835 }
13836
13837 getchar();
13838 return;
13839 }
13840
13841
PrintTopConvInfo(void)13842 int PrintTopConvInfo (void)
13843 {
13844 int i, j, n, len, maxLen;
13845 char *tempStr;
13846 int tempStrSize;
13847 MrBFlt maxNumPartitions;
13848 STATS *stat;
13849
13850 if (chainParams.numRuns == 1)
13851 return NO_ERROR;
13852
13853 # if defined (MPI_ENABLED)
13854 if (proc_id != 0)
13855 return (NO_ERROR);
13856 # endif
13857
13858 tempStrSize = TEMPSTRSIZE;
13859 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
13860 if (!tempStr)
13861 {
13862 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
13863 return (ERROR);
13864 }
13865
13866 for (n=0; n<numTopologies; n++)
13867 {
13868 stat = &(chainParams.stat[n]);
13869 maxNumPartitions = 0.0;
13870 for (i=0; i<chainParams.numRuns; i++)
13871 for (j=0; j<chainParams.numRuns; j++)
13872 if (i > j && stat->pair[i][j] > maxNumPartitions)
13873 maxNumPartitions = stat->pair[i][j];
13874 SafeSprintf (&tempStr, &tempStrSize, "%d", (int) maxNumPartitions);
13875 maxLen = (int) strlen(tempStr);
13876 if (maxLen < 5)
13877 maxLen = 5;
13878
13879 if (numTopologies == 1)
13880 {
13881 if (chainParams.diagnStat == AVGSTDDEV)
13882 MrBayesPrint ("%s Pairwise average standard deviation of split frequencies (upper triangle)\n", spacer);
13883 else
13884 MrBayesPrint ("%s Pairwise maximum standard deviation of split frequencies (upper triangle)\n", spacer);
13885 MrBayesPrint ("%s and number of qualifying splits for each comparison (lower triangle):\n\n", spacer);
13886 }
13887 else
13888 {
13889 if (chainParams.diagnStat == AVGSTDDEV)
13890 MrBayesPrint ("%s Pairwise average standard deviation of split frequencies in topology %d (upper triangle)\n", spacer, n);
13891 else
13892 MrBayesPrint ("%s Pairwise maximum standard deviation of split frequencies in topology %d (upper triangle)\n", spacer, n);
13893 MrBayesPrint ("%s and number of qualifying splits for each comparison (lower triangle):\n\n", spacer);
13894 }
13895
13896 MrBayesPrint ("%s ", spacer);
13897 for (j=0; j<chainParams.numRuns; j++)
13898 {
13899 SafeSprintf (&tempStr, &tempStrSize, "%d", j+1);
13900 len = (int) strlen(tempStr);
13901 MrBayesPrint ("%*c %d ", maxLen-len, ' ', j+1);
13902 }
13903 MrBayesPrint ("\n");
13904
13905 MrBayesPrint ("%s --", spacer);
13906 for (j=0; j<chainParams.numRuns; j++)
13907 {
13908 MrBayesPrint ("--");
13909 for (i=0; i<maxLen; i++)
13910 MrBayesPrint ("-");
13911 }
13912 MrBayesPrint ("\n");
13913
13914 for (i=0; i<chainParams.numRuns; i++)
13915 {
13916 MrBayesPrint ("%s %4d | ", spacer, i+1);
13917 for (j=0; j<chainParams.numRuns; j++)
13918 {
13919 if (i < j)
13920 {
13921 if (chainParams.diagnStat == AVGSTDDEV)
13922 SafeSprintf (&tempStr, &tempStrSize, "%1.3lf", (MrBFlt) (stat->pair[i][j]) / (MrBFlt) (stat->pair[j][i]));
13923 else /* if (chainParams.diagnStat == MAXSTDDEV) */
13924 SafeSprintf (&tempStr, &tempStrSize, "%1.3lf", stat->pair[i][j]);
13925 len = (int) strlen(tempStr);
13926 MrBayesPrint ("%*c%s ", maxLen-len+1, ' ', tempStr);
13927 }
13928 else if (i == j)
13929 {
13930 MrBayesPrint ("%*c ", maxLen+1, ' ');
13931 }
13932 else
13933 {
13934 SafeSprintf (&tempStr, &tempStrSize, "%d", (int) stat->pair[i][j]);
13935 len = (int) strlen(tempStr);
13936 MrBayesPrint ("%*c%s ", maxLen-len+1, ' ', tempStr);
13937 }
13938 }
13939 MrBayesPrint ("\n");
13940 }
13941
13942 MrBayesPrint ("\n");
13943 }
13944
13945 free (tempStr);
13946 return (NO_ERROR);
13947 }
13948
13949
PrintToScreen(int curGen,int startGen,time_t endingT,time_t startingT)13950 void PrintToScreen (int curGen, int startGen, time_t endingT, time_t startingT)
13951 {
13952 int i, chn, nHours, nMins, nSecs;
13953 MrBFlt timePerGen;
13954
13955 # if defined (MPI_ENABLED)
13956 int numLocalColdChains, numFirstAndLastCold;
13957
13958 if (curGen == 0)
13959 {
13960 if (chainParams.isSS == NO && chainParams.mcmcDiagn == YES && chainParams.numRuns > 1)
13961 {
13962 MrBayesPrint ("\n");
13963 if (chainParams.relativeBurnin == YES)
13964 MrBayesPrint ("%s Using a relative burnin of %.1f %% for diagnostics\n", spacer, 100.0*chainParams.burninFraction);
13965 else
13966 MrBayesPrint ("%s Using an absolute burnin of %d samples for diagnostics\n", spacer, chainParams.chainBurnIn);
13967 }
13968 MrBayesPrint ("\n");
13969 MrBayesPrint ("%s Chain results (%d generations requested):\n\n", spacer, chainParams.numGen);
13970 }
13971 MrBayesPrint ("%s %4d -- ", spacer, curGen);
13972 numLocalColdChains = numFirstAndLastCold = 0;
13973 for (chn=0; chn<numLocalChains; chn++)
13974 {
13975 if ((chainId[chn] % chainParams.numChains) == 0)
13976 {
13977 numLocalColdChains++;
13978 if (chn == 0 || chn == numLocalChains - 1)
13979 numFirstAndLastCold++;
13980 }
13981 }
13982
13983 i = 1;
13984 for (chn=0; chn<numLocalChains; chn++)
13985 {
13986 if (i > chainParams.printMax)
13987 {
13988 if (i == chainParams.printMax +1)
13989 {
13990 i++;
13991 if (numLocalColdChains > 0 && numLocalColdChains > numFirstAndLastCold)
13992 MrBayesPrint ("[...%d more local chains...] ", numLocalChains - chainParams.printMax);
13993 else
13994 MrBayesPrint ("(...%d more local chains...) ", numLocalChains - chainParams.printMax);
13995 continue;
13996 }
13997 else
13998 continue;
13999 }
14000 if ((chainId[chn] % chainParams.numChains) == 0)
14001 {
14002 i++;
14003 if (chainParams.printAll == YES)
14004 MrBayesPrint ("[%1.3lf] ", curLnL[chn]);
14005 else
14006 MrBayesPrint ("[%1.3lf] .. ", curLnL[chn]);
14007 }
14008 else if (chainParams.printAll == YES)
14009 {
14010 i++;
14011 MrBayesPrint ("(%1.3lf) ", curLnL[chn]);
14012 }
14013 if (chn < numLocalChains - 1 && (chainId[chn] / chainParams.numChains != chainId[chn+1] / chainParams.numChains))
14014 MrBayesPrint ("* ");
14015 }
14016
14017 if (numLocalColdChains == chainParams.numRuns)
14018 MrBayesPrint ("(...%d remote chains...) ", (chainParams.numChains*chainParams.numRuns) - numLocalChains);
14019 else
14020 MrBayesPrint ("[...%d remote chains...] ", (chainParams.numChains*chainParams.numRuns) - numLocalChains);
14021
14022 if (curGen > 0)
14023 {
14024 timePerGen = (MrBFlt) ((MrBFlt)(endingT-startingT)/(MrBFlt)(curGen-startGen));
14025 nSecs = (int)((chainParams.numGen - curGen) * timePerGen);
14026 nHours = nSecs / 3600;
14027 nSecs = nSecs % 3600;
14028 nMins = nSecs / 60;
14029 nSecs = nSecs % 60;
14030 MrBayesPrint ("-- %d:%0.2d:%0.2d", nHours, nMins, nSecs);
14031 }
14032 MrBayesPrint ("\n");
14033 fflush (stdout);
14034
14035 # else
14036
14037 if (curGen == 0)
14038 {
14039 if (chainParams.isSS == NO && chainParams.mcmcDiagn == YES && chainParams.numRuns > 1)
14040 {
14041 MrBayesPrint ("\n");
14042 if (chainParams.relativeBurnin == YES)
14043 MrBayesPrint ("%s Using a relative burnin of %.1f %% for diagnostics\n", spacer, 100.0*chainParams.burninFraction);
14044 else
14045 MrBayesPrint ("%s Using an absolute burnin of %d samples for diagnostics\n", spacer, chainParams.chainBurnIn);
14046 }
14047 MrBayesPrint ("\n");
14048 MrBayesPrint ("%s Chain results (%d generations requested):\n\n", spacer, chainParams.numGen);
14049 }
14050 MrBayesPrint ("%s %5d -- ", spacer, curGen);
14051 if (numLocalChains == 1)
14052 MrBayesPrint ("%1.3lf ", curLnL[0]);
14053 else
14054 {
14055 i = 0;
14056 for (chn=0; chn<numLocalChains; chn++)
14057 {
14058 if (i >= chainParams.printMax)
14059 {
14060 if (i == chainParams.printMax)
14061 MrBayesPrint (".. ");
14062 i++;
14063 continue;
14064 }
14065 if (chainParams.numChains == 1)
14066 {
14067 MrBayesPrint ("%1.3lf ", curLnL[chn]);
14068 i++;
14069 }
14070 else if (chainId[chn] % chainParams.numChains == 0)
14071 {
14072 if (chainParams.printAll == YES)
14073 MrBayesPrint ("[%1.3lf] ", curLnL[chn]);
14074 else
14075 MrBayesPrint ("[%1.3lf][%d] .. ", curLnL[chn], chn % chainParams.numChains + 1);
14076 i++;
14077 }
14078 else if (chainParams.printAll == YES)
14079 {
14080 MrBayesPrint ("(%1.3lf) ", curLnL[chn]);
14081 i++;
14082 }
14083 if (chn < numLocalChains - 1 && (chainId[chn] / chainParams.numChains != chainId[chn+1] / chainParams.numChains)
14084 && i < chainParams.printMax - 1)
14085 MrBayesPrint ("* ");
14086 }
14087 }
14088
14089 if (curGen > 0)
14090 {
14091 timePerGen = (MrBFlt) ((MrBFlt)(endingT-startingT)/(MrBFlt)(curGen-startGen));
14092 nSecs = (int)((chainParams.numGen - curGen) * timePerGen);
14093 nHours = nSecs / 3600;
14094 nSecs = nSecs % 3600;
14095 nMins = nSecs / 60;
14096 nSecs = nSecs % 60;
14097 MrBayesPrint ("-- %d:%0.2d:%0.2d", nHours, nMins, nSecs);
14098 }
14099 MrBayesPrint ("\n");
14100
14101 fflush (stdout);
14102
14103 # endif
14104
14105 }
14106
14107
PrintTree(int curGen,Param * treeParam,int chain,int showBrlens,MrBFlt clockRate)14108 int PrintTree (int curGen, Param *treeParam, int chain, int showBrlens, MrBFlt clockRate)
14109 {
14110 int i, tempStrSize;
14111 char *tempStr;
14112 Tree *tree;
14113 TreeNode *p=NULL, *q;
14114 Param *subParm;
14115
14116 /* allocate the print string */
14117 printStringSize = 200;
14118 printString = (char *)SafeMalloc((size_t)printStringSize * sizeof(char));
14119 if (!printString)
14120 {
14121 MrBayesPrint ("%s Problem allocating printString (%d)\n", spacer, printStringSize * sizeof(char));
14122 return (ERROR);
14123 }
14124 *printString = '\0';
14125
14126 tempStrSize = TEMPSTRSIZE;
14127 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
14128 if (!tempStr)
14129 {
14130 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
14131 return (ERROR);
14132 }
14133
14134 /* get tree */
14135 tree = GetTree(treeParam, chain, state[chain]);
14136
14137 /* order the taxa */
14138 if (chainParams.orderTaxa == YES)
14139 {
14140 for (i=0; i<tree->nNodes-1; i++)
14141 {
14142 p = tree->allDownPass[i];
14143 if (p->left == NULL)
14144 {
14145 if (p->index == localOutGroup)
14146 p->x = -1;
14147 else
14148 p->x = p->index;
14149 }
14150 else if (p->left->x < p->right->x)
14151 p->x = p->left->x;
14152 else
14153 p->x = p->right->x;
14154 }
14155 for (i=0; i<tree->nIntNodes; i++)
14156 {
14157 if (p->left->x > p->right->x)
14158 {
14159 q = p->left;
14160 p->left = p->right;
14161 p->right = q;
14162 }
14163 }
14164 }
14165
14166 /* print the translate block information and the top of the file */
14167 if (curGen == 0)
14168 {
14169 /* print #NEXUS and translation block information */
14170 SafeSprintf (&tempStr, &tempStrSize, "#NEXUS\n");
14171 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14172 SafeSprintf (&tempStr, &tempStrSize, "[ID: %s]\n", stamp);
14173 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14174 SafeSprintf (&tempStr, &tempStrSize, "[Param: tree");
14175 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14176 if (numCurrentDivisions == 1)
14177 {
14178 SafeSprintf (&tempStr, &tempStrSize, "]\n");
14179 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14180 }
14181 else if (numCurrentDivisions == tree->nRelParts)
14182 {
14183 SafeSprintf (&tempStr, &tempStrSize, "{all}]\n");
14184 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14185 }
14186 else
14187 {
14188 SafeSprintf (&tempStr, &tempStrSize, "{%d", tree->relParts[0]+1);
14189 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14190 for (i=1; i<tree->nRelParts; i++)
14191 {
14192 SafeSprintf (&tempStr, &tempStrSize, ",%d", tree->relParts[i]+1);
14193 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14194 }
14195 SafeSprintf (&tempStr, &tempStrSize, "}]\n");
14196 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14197 }
14198 SafeSprintf (&tempStr, &tempStrSize, "begin trees;\n");
14199 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14200 SafeSprintf (&tempStr, &tempStrSize, " translate\n");
14201 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14202 if (treeParam->paramType == P_SPECIESTREE)
14203 {
14204 for (i=0; i<numSpecies; i++)
14205 {
14206 if (i != numSpecies - 1)
14207 SafeSprintf (&tempStr, &tempStrSize, " %2d %s,\n", i+1, speciesNameSets[speciespartitionNum].names[i]);
14208 else
14209 SafeSprintf (&tempStr, &tempStrSize, " %2d %s;\n", i+1, speciesNameSets[speciespartitionNum].names[i]);
14210 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14211 }
14212 }
14213 else
14214 {
14215 for (i=0; i<numLocalTaxa; i++)
14216 {
14217 if (i != numLocalTaxa - 1)
14218 SafeSprintf (&tempStr, &tempStrSize, " %2d %s,\n", i+1, localTaxonNames[i]);
14219 else
14220 SafeSprintf (&tempStr, &tempStrSize, " %2d %s;\n", i+1, localTaxonNames[i]);
14221 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14222 }
14223 }
14224 }
14225
14226 /* write the tree preamble */
14227 if (SafeSprintf (&tempStr, &tempStrSize, " tree gen.%d", curGen) == ERROR) return (ERROR);
14228 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14229 if (treeParam->paramType == P_BRLENS && treeParam->nSubParams > 0)
14230 {
14231 for (i=0; i<treeParam->nSubParams; i++)
14232 {
14233 subParm = treeParam->subParams[i];
14234 if (subParm->paramType == P_CPPEVENTS)
14235 {
14236 if (SafeSprintf (&tempStr, &tempStrSize, " [&E %s]", subParm->name) == ERROR) return (ERROR);
14237 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14238 }
14239 // if (subParm->paramType == P_MIXEDBRCHRATES)
14240 // {
14241 // id = *GetParamIntVals(subParm, chain, state[chain]);
14242 // if (SafeSprintf (&tempStr, &tempStrSize, " [&B %s %d]", subParm->name, id) == ERROR) return (ERROR);
14243 // }
14244 else
14245 if (SafeSprintf (&tempStr, &tempStrSize, " [&B %s]", subParm->name) == ERROR) return (ERROR);
14246 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14247 }
14248 }
14249 subParm = modelSettings[treeParam->relParts[0]].popSize;
14250 if (treeParam->paramType == P_SPECIESTREE && subParm->nValues > 1)
14251 {
14252 if (SafeSprintf (&tempStr, &tempStrSize, " [&N %s]", subParm->name) == ERROR) return (ERROR);
14253 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14254 }
14255
14256 /* write the tree in (extended) Newick format */
14257 if (tree->isRooted == YES && tree->isCalibrated == NO)
14258 SafeSprintf (&tempStr, &tempStrSize, " = [&R] ");
14259 else if (tree->isRooted == YES && tree->isCalibrated == YES)
14260 SafeSprintf (&tempStr, &tempStrSize, " = [&R] [&clockrate=%s] ", MbPrintNum(clockRate));
14261 else /* if (tree->isRooted == NO) */
14262 SafeSprintf (&tempStr, &tempStrSize, " = [&U] ");
14263 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14264 WriteNoEvtTreeToPrintString (tree->root->left, chain, treeParam, showBrlens, tree->isRooted);
14265 SafeSprintf (&tempStr, &tempStrSize, ";\n");
14266 if (AddToPrintString (tempStr) == ERROR) return(ERROR);
14267
14268 free (tempStr);
14269 return (NO_ERROR);
14270 }
14271
14272
14273 #if defined (MPI_ENABLED)
ReassembleMoveInfo(void)14274 int ReassembleMoveInfo (void)
14275 {
14276 int i, n, ierror;
14277 double x[7], sum[7];
14278 MCMCMove *mv;
14279
14280 for (n=0; n<numGlobalChains; n++)
14281 {
14282 for (i=0; i<numUsedMoves; i++)
14283 {
14284 mv = usedMoves[i];
14285
14286 /* collect counts */
14287 x[0] = mv->nAccepted[n];
14288 x[1] = mv->nTried[n];
14289 x[2] = mv->nBatches[n];
14290 x[3] = mv->nTotAccepted[n];
14291 x[4] = mv->nTotTried[n];
14292 x[5] = mv->lastAcceptanceRate[n];
14293 if (mv->moveType->Autotune != NULL)
14294 x[6]=mv->tuningParam[n][0];
14295
14296 ierror = MPI_Allreduce (&x, &sum, 7, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
14297 if (ierror != MPI_SUCCESS)
14298 return (ERROR);
14299
14300 if (proc_id == 0)
14301 {
14302 mv->nAccepted[n] = (int)sum[0];
14303 mv->nTried[n] = (int)sum[1];
14304 mv->nBatches[n] = (int)sum[2];
14305 mv->nTotAccepted[n] = (int)sum[3];
14306 mv->nTotTried[n] = (int)sum[4];
14307 mv->lastAcceptanceRate[n] = (MrBFlt)sum[5];
14308 if (mv->moveType->Autotune != NULL)
14309 mv->tuningParam[n][0]=(MrBFlt)sum[6];
14310 }
14311 }
14312 }
14313
14314 return (NO_ERROR);
14315 }
14316
14317
ReassembleParamVals(int * curId)14318 int ReassembleParamVals (int *curId)
14319 {
14320 int i, j, k, orderLen, nBrlens, lower, upper, numChainsForProc, proc, ierror, *y, *order, *id, *nEvents;
14321 MrBFlt *x, *brlens, **position, **rateMult;
14322 MPI_Status status;
14323 MPI_Request request;
14324 Tree *tree;
14325 Param *p;
14326
14327 extern MrBFlt *paramValues;
14328 extern int paramValsRowSize;
14329 extern int intValsRowSize;
14330
14331 for (i=0; i<numLocalChains; i++)
14332 curId[i] = chainId[i];
14333
14334 numChainsForProc = numGlobalChains / num_procs;
14335 if (numGlobalChains % num_procs > 0)
14336 lower = upper = numChainsForProc+1;
14337 else
14338 lower = upper = numChainsForProc;
14339
14340 for (proc=1; proc<num_procs; proc++, lower=upper)
14341 {
14342 if (proc < numGlobalChains % num_procs)
14343 upper += numChainsForProc+1;
14344 else
14345 upper += numChainsForProc;
14346
14347 /* chain ids */
14348 if (proc_id == 0)
14349 {
14350 id = curId + lower;
14351 ierror = MPI_Irecv (id, upper-lower, MPI_INT, proc, 0, MPI_COMM_WORLD, &request);
14352 if (ierror != MPI_SUCCESS)
14353 {
14354 return (ERROR);
14355 }
14356 ierror = MPI_Waitall (1, &request, &status);
14357 if (ierror != MPI_SUCCESS)
14358 {
14359 return (ERROR);
14360 }
14361 }
14362 else if (proc_id == proc)
14363 {
14364 id = curId;
14365 ierror = MPI_Isend (id, upper-lower, MPI_INT, 0, 0, MPI_COMM_WORLD, &request);
14366 if (ierror != MPI_SUCCESS)
14367 {
14368 return (ERROR);
14369 }
14370 ierror = MPI_Waitall (1, &request, &status);
14371 if (ierror != MPI_SUCCESS)
14372 {
14373 return (ERROR);
14374 }
14375 }
14376
14377 /* chain states */
14378 if (proc_id == 0)
14379 {
14380 ierror = MPI_Irecv (state+lower, upper-lower, MPI_CHAR, proc, 0, MPI_COMM_WORLD, &request);
14381 if (ierror != MPI_SUCCESS)
14382 {
14383 return (ERROR);
14384 }
14385 ierror = MPI_Waitall (1, &request, &status);
14386 if (ierror != MPI_SUCCESS)
14387 {
14388 return (ERROR);
14389 }
14390 }
14391 else if (proc_id == proc)
14392 {
14393 ierror = MPI_Isend (state, upper-lower, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &request);
14394 if (ierror != MPI_SUCCESS)
14395 {
14396 return (ERROR);
14397 }
14398 ierror = MPI_Waitall (1, &request, &status);
14399 if (ierror != MPI_SUCCESS)
14400 {
14401 return (ERROR);
14402 }
14403 }
14404
14405 /* normal parameter values */
14406 if (proc_id == 0)
14407 {
14408 x = paramValues + 2*paramValsRowSize*lower;
14409 ierror = MPI_Irecv (x, paramValsRowSize*2*(upper-lower), MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, &request);
14410 if (ierror != MPI_SUCCESS)
14411 {
14412 return (ERROR);
14413 }
14414 ierror = MPI_Waitall (1, &request, &status);
14415 if (ierror != MPI_SUCCESS)
14416 {
14417 return (ERROR);
14418 }
14419 if (intValsRowSize > 0)
14420 {
14421 y = intValues + 2*intValsRowSize*lower;
14422 ierror = MPI_Irecv (y, intValsRowSize*2*(upper-lower), MPI_INT, proc, 0, MPI_COMM_WORLD, &request);
14423 if (ierror != MPI_SUCCESS)
14424 {
14425 return (ERROR);
14426 }
14427 ierror = MPI_Waitall (1, &request, &status);
14428 if (ierror != MPI_SUCCESS)
14429 {
14430 return (ERROR);
14431 }
14432 }
14433 }
14434 else if (proc_id == proc)
14435 {
14436 x = paramValues;
14437 ierror = MPI_Isend (x, paramValsRowSize*2*(upper-lower), MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request);
14438 if (ierror != MPI_SUCCESS)
14439 {
14440 return (ERROR);
14441 }
14442 ierror = MPI_Waitall (1, &request, &status);
14443 if (ierror != MPI_SUCCESS)
14444 {
14445 return (ERROR);
14446 }
14447 if (intValsRowSize > 0)
14448 {
14449 y = intValues;
14450 ierror = MPI_Isend (y, intValsRowSize*2*(upper-lower), MPI_INT, 0, 0, MPI_COMM_WORLD, &request);
14451 if (ierror != MPI_SUCCESS)
14452 {
14453 return (ERROR);
14454 }
14455 ierror = MPI_Waitall (1, &request, &status);
14456 if (ierror != MPI_SUCCESS)
14457 {
14458 return (ERROR);
14459 }
14460 }
14461 }
14462
14463 /* std state frequencies */
14464 if (stdStateFreqsRowSize > 0)
14465 {
14466 if (proc_id == 0)
14467 {
14468 x = stdStateFreqs + 2*stdStateFreqsRowSize*lower;
14469 ierror = MPI_Irecv (x, stdStateFreqsRowSize*2*(upper-lower), MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, &request);
14470 if (ierror != MPI_SUCCESS)
14471 {
14472 return (ERROR);
14473 }
14474 ierror = MPI_Waitall (1, &request, &status);
14475 if (ierror != MPI_SUCCESS)
14476 {
14477 return (ERROR);
14478 }
14479 }
14480 else if (proc_id == proc)
14481 {
14482 x = stdStateFreqs;
14483 ierror = MPI_Isend (x, stdStateFreqsRowSize*2*(upper-lower), MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request);
14484 if (ierror != MPI_SUCCESS)
14485 {
14486 return (ERROR);
14487 }
14488 ierror = MPI_Waitall (1, &request, &status);
14489 if (ierror != MPI_SUCCESS)
14490 {
14491 return (ERROR);
14492 }
14493 }
14494 }
14495
14496 /* mcmc trees */
14497 brlens = (MrBFlt *) SafeCalloc (2*numLocalTaxa, sizeof(MrBFlt));
14498 order = (int *) SafeCalloc (2*numLocalTaxa, sizeof(int));
14499 for (i=lower; i<upper; i++)
14500 {
14501 for (j=0; j<numTrees; j++)
14502 {
14503 tree = GetTreeFromIndex(j,0,0);
14504 orderLen = 2*tree->nIntNodes - 1;
14505 nBrlens = tree->nNodes - 1;
14506 if (proc_id == 0)
14507 {
14508 tree = GetTreeFromIndex(j,i,state[i]);
14509 ierror = MPI_Irecv (order, orderLen, MPI_INT, proc, 0, MPI_COMM_WORLD, &request);
14510 if (ierror != MPI_SUCCESS)
14511 {
14512 return (ERROR);
14513 }
14514 ierror = MPI_Waitall (1, &request, &status);
14515 if (ierror != MPI_SUCCESS)
14516 {
14517 return (ERROR);
14518 }
14519 ierror = MPI_Irecv (brlens, nBrlens, MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, &request);
14520 if (ierror != MPI_SUCCESS)
14521 {
14522 return (ERROR);
14523 }
14524 ierror = MPI_Waitall (1, &request, &status);
14525 if (ierror != MPI_SUCCESS)
14526 {
14527 return (ERROR);
14528 }
14529 if (tree->isRooted == YES)
14530 RetrieveRTreeWithIndices(tree, order, brlens);
14531 else
14532 {
14533 RetrieveUTree(tree, order, brlens);
14534 if (localOutGroup!=0)
14535 MoveCalculationRoot(tree,localOutGroup);
14536 }
14537 /* since we only transferred some info, there are additional things we need to
14538 consider, like constraints and calibrations; tree names are OK on proc 0 */
14539 InitializeTreeCalibrations(tree);
14540 CheckSetConstraints(tree);
14541 SetDatedNodeAges(modelSettings[tree->relParts[0]].brlens, i, state[i]);
14542 }
14543 else if (proc_id == proc)
14544 {
14545 tree = GetTreeFromIndex(j,i-lower,state[i-lower]);
14546 if (tree->isRooted == YES)
14547 StoreRTreeWithIndices(tree, order, brlens);
14548 else
14549 StoreUTree(tree, order, brlens);
14550 ierror = MPI_Isend (order, orderLen, MPI_INT, 0, 0, MPI_COMM_WORLD, &request);
14551 if (ierror != MPI_SUCCESS)
14552 {
14553 return (ERROR);
14554 }
14555 ierror = MPI_Waitall (1, &request, &status);
14556 if (ierror != MPI_SUCCESS)
14557 {
14558 return (ERROR);
14559 }
14560 ierror = MPI_Isend (brlens, nBrlens, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request);
14561 if (ierror != MPI_SUCCESS)
14562 {
14563 return (ERROR);
14564 }
14565 ierror = MPI_Waitall (1, &request, &status);
14566 if (ierror != MPI_SUCCESS)
14567 {
14568 return (ERROR);
14569 }
14570 }
14571 }
14572 }
14573 free (brlens);
14574 free (order);
14575
14576 /* CPP event parameters */
14577 for (i=lower; i<upper; i++)
14578 {
14579 for (j=0; j<numParams; j++)
14580 {
14581 p = ¶ms[j];
14582 if (p->paramType == P_CPPEVENTS)
14583 {
14584 if (proc_id == proc)
14585 {
14586 /* get pointers */
14587 nEvents = p->nEvents[2*(i-lower)+state[i-lower]];
14588 position = p->position[2*(i-lower)+state[i-lower]];
14589 rateMult = p->rateMult[2*(i-lower)+state[i-lower]];
14590
14591 /* send number of events */
14592 ierror = MPI_Isend (nEvents, 2*numLocalTaxa, MPI_INT, 0, 0, MPI_COMM_WORLD, &request);
14593 if (ierror != MPI_SUCCESS)
14594 return (ERROR);
14595 ierror = MPI_Waitall (1, &request, &status);
14596 if (ierror != MPI_SUCCESS)
14597 return (ERROR);
14598
14599 /* send events and clear pointers */
14600 for (k=0; k<2*numLocalTaxa; k++)
14601 {
14602 if (nEvents[k] > 0)
14603 {
14604 ierror = MPI_Isend (position[k], nEvents[k], MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request);
14605 if (ierror != MPI_SUCCESS)
14606 return (ERROR);
14607 ierror = MPI_Waitall (1, &request, &status);
14608 if (ierror != MPI_SUCCESS)
14609 return (ERROR);
14610
14611 ierror = MPI_Isend (rateMult[k], nEvents[k], MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request);
14612 if (ierror != MPI_SUCCESS)
14613 return (ERROR);
14614 ierror = MPI_Waitall (1, &request, &status);
14615 if (ierror != MPI_SUCCESS)
14616 return (ERROR);
14617
14618 free(position[k]);
14619 free(rateMult[k]);
14620 position[k] = NULL;
14621 rateMult[k] = NULL;
14622 nEvents[k] = 0;
14623 }
14624 }
14625 }
14626 else if (proc_id == 0)
14627 {
14628 /* find pointers */
14629 nEvents = p->nEvents[2*i];
14630 position = p->position[2*i];
14631 rateMult = p->rateMult[2*i];
14632
14633 /* clear previous events */
14634 for (k=0; k<2*numLocalTaxa; k++)
14635 {
14636 if (nEvents[k] > 0)
14637 {
14638 free (position[k]);
14639 free (rateMult[k]);
14640 position[k] = NULL;
14641 rateMult[k] = NULL;
14642 nEvents[k] = 0;
14643 }
14644 }
14645
14646 /* receive events */
14647 ierror = MPI_Irecv (nEvents, 2*numLocalTaxa, MPI_INT, proc, 0, MPI_COMM_WORLD, &request);
14648 if (ierror != MPI_SUCCESS)
14649 return (ERROR);
14650 ierror = MPI_Waitall (1, &request, &status);
14651 if (ierror != MPI_SUCCESS)
14652 return (ERROR);
14653
14654 for (k=0; k<2*numLocalTaxa; k++)
14655 {
14656 if (nEvents[k] > 0)
14657 {
14658 position[k] = (MrBFlt *) SafeCalloc (nEvents[k], sizeof(MrBFlt));
14659 rateMult[k] = (MrBFlt *) SafeCalloc (nEvents[k], sizeof(MrBFlt));
14660
14661 ierror = MPI_Irecv (position[k], nEvents[k], MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, &request);
14662 if (ierror != MPI_SUCCESS)
14663 return (ERROR);
14664 ierror = MPI_Waitall (1, &request, &status);
14665 if (ierror != MPI_SUCCESS)
14666 return (ERROR);
14667
14668 ierror = MPI_Irecv (rateMult[k], nEvents[k], MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, &request);
14669 if (ierror != MPI_SUCCESS)
14670 return (ERROR);
14671 ierror = MPI_Waitall (1, &request, &status);
14672 if (ierror != MPI_SUCCESS)
14673 return (ERROR);
14674 }
14675 }
14676 }
14677 }
14678 }
14679 }
14680 }
14681
14682 return (NO_ERROR);
14683 }
14684
14685
ReassembleSwapInfo(void)14686 int ReassembleSwapInfo (void)
14687 {
14688 int i, j, n, x, sum, ierror;
14689
14690 for (n=0; n<chainParams.numRuns; n++)
14691 {
14692 for (i=0; i<chainParams.numChains; i++)
14693 {
14694 for (j=0; j<chainParams.numChains; j++)
14695 {
14696 if (i != j)
14697 {
14698 if (proc_id == 0)
14699 x = 0;
14700 else
14701 x = swapInfo[n][i][j];
14702 ierror = MPI_Allreduce (&x, &sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
14703 if (ierror != MPI_SUCCESS)
14704 return (ERROR);
14705 if (proc_id == 0)
14706 swapInfo[n][i][j] += sum;
14707 else
14708 swapInfo[n][i][j] = 0;
14709 }
14710 }
14711 }
14712 }
14713
14714 return (NO_ERROR);
14715 }
14716
14717
ReassembleTuningParams(void)14718 int ReassembleTuningParams (void)
14719 {
14720 int i, j, k, lower, ierror;
14721 MrBFlt *x, *sum;
14722
14723 x = (MrBFlt *) SafeCalloc (2*numUsedMoves, sizeof(MrBFlt));
14724 sum = x + numUsedMoves;
14725
14726 lower = numGlobalChains / num_procs;
14727 if (numGlobalChains % num_procs != 0)
14728 lower++;
14729
14730 for (i=lower; i<numGlobalChains; i++)
14731 {
14732 for (j=0; j<numLocalChains; j++)
14733 {
14734 if (chainId[j] == i)
14735 break;
14736 }
14737
14738 for (k=0; k<numUsedMoves; k++)
14739 {
14740 if (j != numLocalChains && usedMoves[k]->moveType->numTuningParams > 0) /* we have the tuning parameter of interest */
14741 x[k] = usedMoves[k]->tuningParam[i][0];
14742 else
14743 x[k] = 0.0;
14744 }
14745
14746 ierror = MPI_Allreduce (x, sum, numUsedMoves, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
14747 if (ierror != MPI_SUCCESS)
14748 {
14749 free (x);
14750 return (ERROR);
14751 }
14752
14753 if (proc_id == 0)
14754 {
14755 for (k=0; k<numUsedMoves; k++)
14756 {
14757 if (usedMoves[k]->moveType->numTuningParams > 0)
14758 usedMoves[k]->tuningParam[i][0] = sum[k];
14759 }
14760 }
14761 }
14762
14763 free (x);
14764 return (NO_ERROR);
14765 }
14766
14767
RedistributeMoveInfo(void)14768 void RedistributeMoveInfo (void)
14769 {
14770 int i, j, k;
14771 MCMCMove *mv;
14772
14773 /* Leave if not processor 0, because then we already have the necessary info since
14774 it was not deleted in ReassembleMoveInfo */
14775 if (proc_id != 0)
14776 return;
14777
14778 /* If we are processor 0, simply delete the unnecessary information */
14779 for (i=0; i<numGlobalChains; i++)
14780 {
14781 for (j=0; j<numLocalChains; j++)
14782 if (chainId[j] == i)
14783 break;
14784
14785 if (j == numLocalChains)
14786 {
14787 /* we do not have this chain, so delete the move info */
14788 for (k=0; k<numUsedMoves; k++)
14789 {
14790 mv = usedMoves[k];
14791
14792 /* reset counts */
14793 mv->nAccepted[i] = 0;
14794 mv->nTried[i] = 0;
14795 mv->nBatches[i] = 0;
14796 mv->nTotAccepted[i] = 0;
14797 mv->nTotTried[i] = 0;
14798 mv->lastAcceptanceRate[i] = 0;
14799 if (mv->moveType->Autotune != NULL)
14800 mv->tuningParam[i][0]=0.0;
14801 }
14802 }
14803 }
14804 }
14805
14806
RedistributeParamVals(void)14807 int RedistributeParamVals (void)
14808 {
14809 int i, j, k, orderLen, nBrlens, lower, upper, numChainsForProc, proc, ierror, *y, *order, *nEvents;
14810 MrBFlt *x, *brlens, **position, **rateMult;
14811 MPI_Status status;
14812 MPI_Request request;
14813 Tree *tree;
14814 Param *p;
14815
14816 extern MrBFlt *paramValues;
14817 extern int paramValsRowSize;
14818 extern int intValsRowSize;
14819
14820 numChainsForProc = numGlobalChains / num_procs;
14821 if (numGlobalChains % num_procs > 0)
14822 lower = upper = numChainsForProc+1;
14823 else
14824 lower = upper = numChainsForProc;
14825
14826 for (proc=1; proc<num_procs; proc++, lower=upper)
14827 {
14828 if (proc < numGlobalChains % num_procs)
14829 upper += numChainsForProc+1;
14830 else
14831 upper += numChainsForProc;
14832
14833 /* normal parameter values */
14834 if (proc_id == 0)
14835 {
14836 x = paramValues + 2*paramValsRowSize*lower;
14837 ierror = MPI_Isend (x, paramValsRowSize*2*(upper-lower), MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, &request);
14838 if (ierror != MPI_SUCCESS)
14839 {
14840 return (ERROR);
14841 }
14842 ierror = MPI_Waitall (1, &request, &status);
14843 if (ierror != MPI_SUCCESS)
14844 {
14845 return (ERROR);
14846 }
14847 if (intValsRowSize > 0)
14848 {
14849 y = intValues + 2*intValsRowSize*lower;
14850 ierror = MPI_Isend (y, intValsRowSize*2*(upper-lower), MPI_INT, proc, 0, MPI_COMM_WORLD, &request);
14851 if (ierror != MPI_SUCCESS)
14852 {
14853 return (ERROR);
14854 }
14855 ierror = MPI_Waitall (1, &request, &status);
14856 if (ierror != MPI_SUCCESS)
14857 {
14858 return (ERROR);
14859 }
14860 }
14861 }
14862 else if (proc_id == proc)
14863 {
14864 x = paramValues;
14865 ierror = MPI_Irecv (x, paramValsRowSize*2*(upper-lower), MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request);
14866 if (ierror != MPI_SUCCESS)
14867 {
14868 return (ERROR);
14869 }
14870 ierror = MPI_Waitall (1, &request, &status);
14871 if (ierror != MPI_SUCCESS)
14872 {
14873 return (ERROR);
14874 }
14875 if (intValsRowSize > 0)
14876 {
14877 y = intValues;
14878 ierror = MPI_Irecv (y, intValsRowSize*2*(upper-lower), MPI_INT, 0, 0, MPI_COMM_WORLD, &request);
14879 if (ierror != MPI_SUCCESS)
14880 {
14881 return (ERROR);
14882 }
14883 ierror = MPI_Waitall (1, &request, &status);
14884 if (ierror != MPI_SUCCESS)
14885 {
14886 return (ERROR);
14887 }
14888 }
14889 }
14890
14891 /* mcmc trees */
14892 brlens = (MrBFlt *) SafeCalloc (2*numLocalTaxa, sizeof(MrBFlt));
14893 order = (int *) SafeCalloc (2*numLocalTaxa, sizeof(int));
14894 for (i=lower; i<upper; i++)
14895 {
14896 for (j=0; j<numTrees; j++)
14897 {
14898 tree = GetTreeFromIndex(j,0,0);
14899 orderLen = 2*tree->nIntNodes - 1;
14900 nBrlens = tree->nNodes - 1;
14901 if (proc_id == 0)
14902 {
14903 tree = GetTreeFromIndex(j,i,0);
14904 if (tree->isRooted == YES)
14905 StoreRTreeWithIndices(tree, order, brlens);
14906 else
14907 StoreUTree(tree, order, brlens);
14908 ierror = MPI_Isend (order, orderLen, MPI_INT, proc, 0, MPI_COMM_WORLD, &request);
14909 if (ierror != MPI_SUCCESS)
14910 {
14911 return (ERROR);
14912 }
14913 ierror = MPI_Waitall (1, &request, &status);
14914 if (ierror != MPI_SUCCESS)
14915 {
14916 return (ERROR);
14917 }
14918 ierror = MPI_Isend (brlens, nBrlens, MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, &request);
14919 if (ierror != MPI_SUCCESS)
14920 {
14921 return (ERROR);
14922 }
14923 ierror = MPI_Waitall (1, &request, &status);
14924 if (ierror != MPI_SUCCESS)
14925 {
14926 return (ERROR);
14927 }
14928 }
14929 else if (proc_id == proc)
14930 {
14931 tree = GetTreeFromIndex(j,i-lower,0);
14932 ierror = MPI_Irecv (order, orderLen, MPI_INT, 0, 0, MPI_COMM_WORLD, &request);
14933 if (ierror != MPI_SUCCESS)
14934 {
14935 return (ERROR);
14936 }
14937 ierror = MPI_Waitall (1, &request, &status);
14938 if (ierror != MPI_SUCCESS)
14939 {
14940 return (ERROR);
14941 }
14942 ierror = MPI_Irecv (brlens, nBrlens, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request);
14943 if (ierror != MPI_SUCCESS)
14944 {
14945 return (ERROR);
14946 }
14947 ierror = MPI_Waitall (1, &request, &status);
14948 if (ierror != MPI_SUCCESS)
14949 {
14950 return (ERROR);
14951 }
14952 if (tree->isRooted == YES)
14953 RetrieveRTreeWithIndices(tree, order, brlens);
14954 else
14955 {
14956 RetrieveUTree(tree, order, brlens);
14957 if (localOutGroup != 0)
14958 MoveCalculationRoot(tree,localOutGroup);
14959 }
14960 /* since we only transferred some info, there are additional things we need to
14961 consider, like names, constraints and calibrations */
14962 InitializeTreeCalibrations(tree);
14963 CheckSetConstraints(tree);
14964 SetDatedNodeAges(modelSettings[tree->relParts[0]].brlens, i-lower, 0);
14965 strcpy(tree->name, GetTreeFromIndex(j, i, 0)->name);
14966 tree = GetTreeFromIndex(j,i-lower,1);
14967 strcpy(tree->name, GetTreeFromIndex(j, i, 0)->name);
14968 }
14969 }
14970 }
14971 free (brlens);
14972 free (order);
14973
14974 /* CPP relaxed clock parameters */
14975 for (i=lower; i<upper; i++)
14976 {
14977 for (j=0; j<numParams; j++)
14978 {
14979 p = ¶ms[j];
14980 if (p->paramType == P_CPPEVENTS)
14981 {
14982 if (proc_id == 0)
14983 {
14984 /* get pointers */
14985 nEvents = p->nEvents[2*i];
14986 position = p->position[2*i];
14987 rateMult = p->rateMult[2*i];
14988
14989 /* send number of events */
14990 ierror = MPI_Isend (nEvents, 2*numLocalTaxa, MPI_INT, proc, 0, MPI_COMM_WORLD, &request);
14991 if (ierror != MPI_SUCCESS)
14992 return (ERROR);
14993 ierror = MPI_Waitall (1, &request, &status);
14994 if (ierror != MPI_SUCCESS)
14995 return (ERROR);
14996
14997 /* send events and clear pointers */
14998 for (k=0; k<2*numLocalTaxa; k++)
14999 {
15000 if (nEvents[k] > 0)
15001 {
15002 ierror = MPI_Isend (position[k], nEvents[k], MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, &request);
15003 if (ierror != MPI_SUCCESS)
15004 return (ERROR);
15005 ierror = MPI_Waitall (1, &request, &status);
15006 if (ierror != MPI_SUCCESS)
15007 return (ERROR);
15008
15009 ierror = MPI_Isend (rateMult[k], nEvents[k], MPI_DOUBLE, proc, 0, MPI_COMM_WORLD, &request);
15010 if (ierror != MPI_SUCCESS)
15011 return (ERROR);
15012 ierror = MPI_Waitall (1, &request, &status);
15013 if (ierror != MPI_SUCCESS)
15014 return (ERROR);
15015
15016 free(position[k]);
15017 free(rateMult[k]);
15018 position[k] = NULL;
15019 rateMult[k] = NULL;
15020 nEvents[k] = 0;
15021 }
15022 }
15023 }
15024 else if (proc_id == proc)
15025 {
15026 /* find pointers */
15027 nEvents = p->nEvents[2*(i-lower)];
15028 position = p->position[2*(i-lower)];
15029 rateMult = p->rateMult[2*(i-lower)];
15030
15031 /* clear previous events */
15032 for (k=0; k<2*numLocalTaxa; k++)
15033 {
15034 if (nEvents[k] > 0)
15035 {
15036 free (position[k]);
15037 free (rateMult[k]);
15038 position[k] = NULL;
15039 rateMult[k] = NULL;
15040 nEvents[k] = 0;
15041 }
15042 }
15043
15044 /* receive events */
15045 ierror = MPI_Irecv (nEvents, 2*numLocalTaxa, MPI_INT, 0, 0, MPI_COMM_WORLD, &request);
15046 if (ierror != MPI_SUCCESS)
15047 return (ERROR);
15048 ierror = MPI_Waitall (1, &request, &status);
15049 if (ierror != MPI_SUCCESS)
15050 return (ERROR);
15051 for (k=0; k<2*numLocalTaxa; k++)
15052 {
15053 if (nEvents[k] > 0)
15054 {
15055 position[k] = (MrBFlt *) SafeCalloc (nEvents[k], sizeof(MrBFlt));
15056 rateMult[k] = (MrBFlt *) SafeCalloc (nEvents[k], sizeof(MrBFlt));
15057
15058 ierror = MPI_Irecv (position[k], nEvents[k], MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request);
15059 if (ierror != MPI_SUCCESS)
15060 return (ERROR);
15061 ierror = MPI_Waitall (1, &request, &status);
15062 if (ierror != MPI_SUCCESS)
15063 return (ERROR);
15064
15065 ierror = MPI_Irecv (rateMult[k], nEvents[k], MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request);
15066 if (ierror != MPI_SUCCESS)
15067 return (ERROR);
15068 ierror = MPI_Waitall (1, &request, &status);
15069 if (ierror != MPI_SUCCESS)
15070 return (ERROR);
15071 }
15072 }
15073 }
15074 }
15075 }
15076 }
15077
15078 /* update evolutionary branch lengths or rates (because node indices have changed) */
15079 if (proc_id == proc)
15080 {
15081 for (i=0; i<upper-lower; i++)
15082 {
15083 for (j=0; j<numParams; j++)
15084 {
15085 p = ¶ms[j];
15086 if (p->paramType == P_CPPEVENTS)
15087 {
15088 tree = GetTree(p, i, 0);
15089 UpdateCppEvolLengths (p, tree->root->left, i);
15090 }
15091 else if (p->paramType == P_TK02BRANCHRATES || (p->paramType == P_MIXEDBRCHRATES && *GetParamIntVals(p, i, 0) == RCL_TK02))
15092 {
15093 tree = GetTree (p, i, 0);
15094 UpdateTK02EvolLengths (p, tree, i);
15095 }
15096 else if (p->paramType == P_IGRBRANCHRATES || (p->paramType == P_MIXEDBRCHRATES && *GetParamIntVals(p, i, 0) == RCL_IGR))
15097 {
15098 tree = GetTree(p, i, 0);
15099 UpdateIgrBrachLengths (p, tree, i);
15100 }
15101 }
15102 }
15103 }
15104 }
15105
15106 return (NO_ERROR);
15107 }
15108
15109
RedistributeTuningParams(void)15110 int RedistributeTuningParams (void)
15111 {
15112 int i, j, k, lower, ierror;
15113 MrBFlt *x, *sum;
15114
15115 x = (MrBFlt *) SafeCalloc (2*numUsedMoves, sizeof(MrBFlt));
15116 sum = x + numUsedMoves;
15117
15118 lower = numGlobalChains / num_procs;
15119 if (numGlobalChains % num_procs != 0)
15120 lower++;
15121
15122 if (proc_id != 0)
15123 {
15124 for (i=0; i<numGlobalChains; i++)
15125 {
15126 for (k=0; k<numUsedMoves; k++)
15127 {
15128 if (usedMoves[k]->moveType->numTuningParams > 0)
15129 usedMoves[k]->tuningParam[i][0] = 0.0;
15130 }
15131 }
15132 }
15133
15134 for (i=lower; i<numGlobalChains; i++)
15135 {
15136 for (j=0; j<numLocalChains; j++)
15137 {
15138 if (chainId[j] == i)
15139 break;
15140 }
15141
15142 for (k=0; k<numUsedMoves; k++)
15143 {
15144 if (proc_id == 0 && usedMoves[k]->moveType->numTuningParams > 0) /* we have the tuning parameter of interest */
15145 {
15146 x[k] = usedMoves[k]->tuningParam[i][0];
15147 usedMoves[k]->tuningParam[i][0]=0.0;
15148 }
15149 else
15150 x[k] = 0.0;
15151 }
15152
15153 ierror = MPI_Allreduce (x, sum, numUsedMoves, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
15154 if (ierror != MPI_SUCCESS)
15155 {
15156 free (x);
15157 return (ERROR);
15158 }
15159
15160 if (j != numLocalChains) /* we have the chain of interest */
15161 {
15162 for (k=0; k<numUsedMoves; k++)
15163 {
15164 if (usedMoves[k]->moveType->numTuningParams > 0)
15165 usedMoves[k]->tuningParam[i][0] = sum[k];
15166 }
15167 }
15168 }
15169
15170 free (x);
15171 return (NO_ERROR);
15172 }
15173
15174 #endif
15175
15176
15177 /* RemovePartition: Remove a partition from the tree keeping track of partition frequencies */
RemovePartition(PFNODE * r,BitsLong * p,int runId)15178 int RemovePartition (PFNODE *r, BitsLong *p, int runId)
15179 {
15180 int i, comp;
15181
15182 if (r == NULL)
15183 {
15184 return (ERROR);
15185 }
15186 else
15187 {
15188 for (i=0; i<nLongsNeeded; i++)
15189 {
15190 if (r->partition[i] != p[i])
15191 break;
15192 }
15193
15194 if (i == nLongsNeeded)
15195 comp = 0;
15196 else if (r->partition[i] < p[i])
15197 comp = -1;
15198 else
15199 comp = 1;
15200
15201 if (comp == 0) /* match */
15202 {
15203 if (r->count[runId] == 0)
15204 return ERROR;
15205 else
15206 r->count[runId]--;
15207 }
15208 else if (comp < 0) /* greater than -> into left subtree */
15209 {
15210 if ((RemovePartition (r->left, p, runId)) == ERROR)
15211 return ERROR;
15212 }
15213 else
15214 {
15215 /* less than -> into right subtree */
15216 if ((RemovePartition (r->right, p, runId)) == ERROR)
15217 return ERROR;
15218 }
15219 }
15220
15221 return (NO_ERROR);
15222 }
15223
15224
15225 /* RemoveTreeFromPartitionCounters: Break a tree into partitions and remove those from counters */
RemoveTreeFromPartitionCounters(Tree * tree,int treeId,int runId)15226 int RemoveTreeFromPartitionCounters (Tree *tree, int treeId, int runId)
15227 {
15228 int i, j, nTaxa;
15229 TreeNode *p;
15230
15231 if (tree->isRooted == YES)
15232 nTaxa = tree->nNodes - tree->nIntNodes - 1;
15233 else
15234 nTaxa = tree->nNodes - tree->nIntNodes;
15235
15236 for (i=0; i<nTaxa; i++)
15237 {
15238 ClearBits(partition[i], nLongsNeeded);
15239 SetBit(i, partition[i]);
15240 }
15241
15242 for (i=0; i<tree->nIntNodes-1; i++)
15243 {
15244 p = tree->intDownPass[i];
15245 assert (p->index >= tree->nNodes - tree->nIntNodes - (tree->isRooted == YES ? 1 : 0));
15246 for (j=0; j<nLongsNeeded; j++)
15247 {
15248 partition[p->index][j] = partition[p->left->index][j] | partition[p->right->index][j];
15249 }
15250
15251 if ((RemovePartition (partFreqTreeRoot[treeId], partition[p->index], runId)) == ERROR)
15252 {
15253 MrBayesPrint ("%s Could not remove partition %d in RemoveTreeFromPartitionCounters\n", spacer, p->index);
15254 ShowParts(stdout,partition[p->index],numLocalTaxa);
15255 return ERROR;
15256 }
15257 }
15258
15259 return NO_ERROR;
15260 }
15261
15262
15263 /* RemoveTreeSamples: Remove tree samples from partition counters */
RemoveTreeSamples(int from,int to)15264 int RemoveTreeSamples (int from, int to)
15265 {
15266 int i, j, k, longestLine, line;
15267 char c, *s, *lineBuf=0;
15268 FILE *fp;
15269 Tree *t;
15270 TreeList *treeList;
15271 char *tempStr;
15272 int tempStrSize = TEMPSTRSIZE;
15273
15274 # if defined (MPI_ENABLED)
15275 if (proc_id != 0)
15276 return (NO_ERROR);
15277 # endif
15278
15279 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
15280 if (!tempStr)
15281 {
15282 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
15283 return (ERROR);
15284 }
15285
15286 if (chainParams.saveTrees == YES)
15287 {
15288 for (i=0; i<numTopologies; i++)
15289 {
15290 t = chainParams.dtree;
15291 if (topologyParam[i]->tree[0]->isRooted == YES)
15292 t->isRooted = YES;
15293 else
15294 t->isRooted = NO;
15295
15296 for (j=0; j<chainParams.numRuns; j++)
15297 {
15298 treeList = &chainParams.treeList[j*numTopologies + i];
15299
15300 for (k=from; k<=to; k++)
15301 {
15302 GetFromTreeList (treeList, t);
15303 if (RemoveTreeFromPartitionCounters (t, i, j) == ERROR)
15304 {
15305 SafeFclose (&fp);
15306 free (tempStr);
15307 free (lineBuf);
15308 return (ERROR);
15309 }
15310 }
15311 }
15312 }
15313 }
15314 else
15315 {
15316 for (i=0; i<numTopologies; i++)
15317 {
15318 t = chainParams.dtree;
15319
15320 for (j=0; j<chainParams.numRuns; j++)
15321 {
15322 if (numPrintTreeParams == 1)
15323 {
15324 if (chainParams.numRuns == 1)
15325 SafeSprintf (&tempStr, &tempStrSize, "%s.t", chainParams.chainFileName);
15326 else
15327 SafeSprintf (&tempStr, &tempStrSize, "%s.run%d.t", chainParams.chainFileName, j+1);
15328 }
15329 else
15330 {
15331 if (chainParams.numRuns == 1)
15332 SafeSprintf (&tempStr, &tempStrSize, "%s.tree%d.t", chainParams.chainFileName, topologyPrintIndex[i]+1);
15333 else
15334 SafeSprintf (&tempStr, &tempStrSize, "%s.tree%d.run%d.t", chainParams.chainFileName, topologyPrintIndex[i]+1, j+1);
15335 }
15336
15337 if ((fp = OpenTextFileR (tempStr)) == NULL)
15338 {
15339 free (tempStr);
15340 return (ERROR);
15341 }
15342
15343 if (from == 1)
15344 {
15345 longestLine = LongestLine(fp);
15346 lineBuf = (char *) SafeCalloc (longestLine+2,sizeof(char));
15347 fseek (fp, LastBlock(fp, lineBuf, longestLine), SEEK_SET);
15348 fseek (fp, FirstTree(fp, lineBuf, longestLine), SEEK_SET);
15349 fgetpos (fp, &chainParams.tFilePos[j*numTopologies+i]);
15350 free(lineBuf);
15351 lineBuf = NULL;
15352 }
15353 fsetpos (fp, &chainParams.tFilePos[j*numTopologies+i]);
15354
15355 longestLine = 0;
15356 for (k=0; k<=to-from; k++)
15357 {
15358 line = 0;
15359 do {
15360 line++;
15361 } while ((c = fgetc(fp)) != '\r' && c != '\n');
15362
15363 if (line > longestLine)
15364 longestLine = line;
15365
15366 while ((c = fgetc(fp)) == '\r' || c == '\n')
15367 ;
15368 }
15369
15370 lineBuf = (char *) SafeCalloc (longestLine + 10, sizeof (char));
15371 if (!lineBuf)
15372 {
15373 SafeFclose (&fp);
15374 free (tempStr);
15375 return (ERROR);
15376 }
15377 fsetpos (fp, &chainParams.tFilePos[j*numTopologies+i]);
15378 /* The fsetpos and fgetpos pair are affected by writing to the file,
15379 at least on Windows systems. The effect is to put the subsequent
15380 fsetpos back a few positions. The following code will deal with this
15381 problem without affecting systems where this does not happen. */
15382 do { c = fgetc(fp);
15383 } while (c != 't');
15384
15385 for (k=from; k<=to; k++)
15386 {
15387 if (fgets (lineBuf, longestLine + 5, fp) == NULL)
15388 {
15389 free (tempStr);
15390 free (lineBuf);
15391 return ERROR;
15392 }
15393
15394 s = strtok (lineBuf, " ");
15395 for (s = strtok (NULL, ";"); *s != '('; s++)
15396 ;
15397
15398 StripComments (s);
15399 if (ResetTopology (t, s) == ERROR)
15400 {
15401 SafeFclose (&fp);
15402 free (tempStr);
15403 free (lineBuf);
15404 return (ERROR);
15405 }
15406
15407 if (RemoveTreeFromPartitionCounters (t, i, j) == ERROR)
15408 {
15409 SafeFclose (&fp);
15410 free (tempStr);
15411 free (lineBuf);
15412 return (ERROR);
15413 }
15414 }
15415 fgetpos (fp, &chainParams.tFilePos[j*numTopologies+i]);
15416 free (lineBuf);
15417 SafeFclose (&fp);
15418 }
15419 }
15420 }
15421
15422 /* remove unnecessary nodes from the tree holding partition counters */
15423 for (i=0; i<numTopologies; i++)
15424 {
15425 partFreqTreeRoot[i] = CompactTree (partFreqTreeRoot[i]);
15426 }
15427
15428 free (tempStr);
15429 return (NO_ERROR);
15430 }
15431
15432
ReopenMBPrintFiles(void)15433 int ReopenMBPrintFiles (void)
15434 {
15435 int i, n;
15436 char fileName[120], localFileName[100];
15437
15438 /* Take care of the mpi procs that do not have a file */
15439 # if defined (MPI_ENABLED)
15440 if (proc_id != 0)
15441 return (NO_ERROR);
15442 # endif
15443
15444 /* Get root of local file name */
15445 strcpy (localFileName, chainParams.chainFileName);
15446
15447 /* Reopen the .p and .t files */
15448 for (n=0; n<chainParams.numRuns; n++)
15449 {
15450 if (chainParams.numRuns == 1)
15451 sprintf (fileName, "%s.p", localFileName);
15452 else
15453 sprintf (fileName, "%s.run%d.p", localFileName, n+1);
15454
15455 if ((fpParm[n] = OpenTextFileA (fileName)) == NULL)
15456 return (ERROR);
15457
15458 for (i=0; i<numTrees; i++)
15459 {
15460 if (numTrees == 1 && chainParams.numRuns == 1)
15461 sprintf (fileName, "%s.t", localFileName);
15462 else if (numTrees > 1 && chainParams.numRuns == 1)
15463 sprintf (fileName, "%s.tree%d.t", localFileName, i+1);
15464 else if (numTrees == 1 && chainParams.numRuns > 1)
15465 sprintf (fileName, "%s.run%d.t", localFileName, n+1);
15466 else
15467 sprintf (fileName, "%s.tree%d.run%d.t", localFileName, i+1, n+1);
15468
15469 if ((fpTree[n][i] = OpenTextFileA (fileName)) == NULL)
15470 return (ERROR);
15471 }
15472 }
15473
15474 /* Reopen the .mcmc file */
15475 if (chainParams.mcmcDiagn == YES)
15476 {
15477 sprintf (fileName, "%s.mcmc", localFileName);
15478
15479 if ((fpMcmc = OpenTextFileA (fileName)) == NULL)
15480 return (ERROR);
15481 }
15482
15483 # if defined (PRINT_DUMP)
15484 for (n=0; n<chainParams.numRuns; n++)
15485 {
15486 if (chainParams.numRuns == 1)
15487 sprintf (fileName, "%s.dump", localFileName);
15488 else
15489 sprintf (fileName, "%s.run%d.dump", localFileName, n+1);
15490
15491 if ((fpDump[n] = OpenTextFileA (fileName)) == NULL)
15492 return (ERROR);
15493 }
15494 # endif
15495
15496 return (NO_ERROR);
15497 }
15498
15499
ConfirmAbortRun(void)15500 int ConfirmAbortRun(void)
15501 {
15502 char c, line[100];
15503 int ret=0, i;
15504
15505 /* reset requestAbortRun */
15506 requestAbortRun = NO;
15507
15508 MrBayesPrint(" Do you really want to stop the run (y/n)?");
15509 if (fgets (line,98,stdin) == NULL)
15510 {
15511 printf ("Error in function: %s at line: %d in file: %s", __func__, __LINE__, __FILE__);
15512 }
15513 for (i=0; (c=line[i])!='\0' && !isgraph(c); i++)
15514 ;
15515 if (c == 'y' || c == 'Y')
15516 ret=1;
15517 else
15518 {
15519 MrBayesPrint(" Mcmc run continued ...\n\n");
15520 ret=0;
15521 }
15522 return ret;
15523 }
15524
15525
15526 /*-------------------------------------------------------------------
15527 |
15528 | ResetChainIds: Make sure parameter values are swapped back
15529 | at the end of a Metropolis-coupled MCMC run
15530 |
15531 --------------------------------------------------------------------*/
ResetChainIds(void)15532 void ResetChainIds (void)
15533 {
15534 int j, k, k1, tempId, *curId, toChn, fromChn, *to, *from, *swap;
15535 Param *p;
15536 MrBFlt *fromVals, *toVals, *swapVals, **fromPosition, **toPosition,
15537 **swapPosition, **fromRateMult, **toRateMult, **swapRateMult;
15538 Tree *toTree, *fromTree, *swapTree;
15539
15540 curId = (int *) SafeCalloc (numGlobalChains, sizeof(int));
15541
15542 #if defined (MPI_ENABLED)
15543 ReassembleParamVals(curId);
15544 ReassembleTuningParams();
15545 SetChainIds();
15546 if (proc_id != 0)
15547 {
15548 /* reset state */
15549 for (j=0; j<numLocalChains; j++)
15550 state[j] = 0;
15551 free (curId);
15552 return;
15553 }
15554 #else
15555 for (j=0; j<numGlobalChains; j++)
15556 curId[j] = chainId[j];
15557 SetChainIds();
15558 #endif
15559
15560 for (toChn=0; toChn<numGlobalChains; toChn++)
15561 {
15562 if (curId[toChn] == toChn)
15563 {
15564 if (state[toChn] != 0)
15565 {
15566 CopyParams (toChn);
15567 CopyTrees (toChn);
15568 state[toChn] ^= 1;
15569 }
15570 continue;
15571 }
15572
15573 /* we need to swap all values */
15574 /* first find the chain to swap with */
15575 for (j=toChn+1; j<numGlobalChains; j++)
15576 if (curId[j] == toChn)
15577 break;
15578 fromChn = j;
15579
15580 /* normal params */
15581 CopyParams (toChn);
15582 CopyTrees (toChn);
15583 CopyParams (fromChn);
15584 CopyTrees (fromChn);
15585
15586 for (j=0; j<numParams; j++)
15587 {
15588 p = ¶ms[j];
15589 toVals = GetParamVals (p, toChn, 0);
15590 swapVals = GetParamVals (p, toChn, 1);
15591 fromVals = GetParamVals (p, fromChn, state[fromChn]);
15592 for (k=0; k<p->nValues; k++)
15593 {
15594 toVals[k] = fromVals[k];
15595 fromVals[k] = swapVals[k];
15596 }
15597 toVals = GetParamSubVals (p, toChn, 0);
15598 swapVals = GetParamSubVals (p, toChn, 1);
15599 fromVals = GetParamSubVals (p, fromChn, state[fromChn]);
15600 for (k=0; k<p->nSubValues; k++)
15601 {
15602 toVals[k] = fromVals[k];
15603 fromVals[k] = swapVals[k];
15604 }
15605 if (p->nStdStateFreqs > 0)
15606 {
15607 toVals = GetParamStdStateFreqs (p, toChn, 0);
15608 swapVals = GetParamStdStateFreqs (p, toChn, 1);
15609 fromVals = GetParamStdStateFreqs (p, fromChn, state[fromChn]);
15610 for (k=0; k<p->nStdStateFreqs; k++)
15611 {
15612 toVals[k] = fromVals[k];
15613 fromVals[k] = swapVals[k];
15614 }
15615 }
15616 }
15617
15618 /* mcmc trees */
15619 for (j=0; j<numTrees; j++)
15620 {
15621 toTree = GetTreeFromIndex(j, toChn, 0);
15622 swapTree = GetTreeFromIndex(j, toChn, 1);
15623 fromTree = GetTreeFromIndex(j, fromChn, state[fromChn]);
15624 CopyToTreeFromTree (toTree, fromTree);
15625 CopyToTreeFromTree (fromTree, swapTree);
15626 CopyToTreeFromTree (swapTree, toTree);
15627 swapTree = GetTreeFromIndex(j, fromChn, state[fromChn] ^ 1);
15628 CopyToTreeFromTree (swapTree, fromTree);
15629 }
15630 /* CPP relaxed clock params */
15631 for (j=0; j<numParams; j++)
15632 {
15633 p = ¶ms[j];
15634 if (p->paramType == P_CPPEVENTS)
15635 {
15636 to = p->nEvents[2*toChn];
15637 swap = p->nEvents[2*toChn+1];
15638 from = p->nEvents[2*fromChn+state[fromChn]];
15639 toPosition = p->position[2*toChn];
15640 swapPosition = p->position[2*toChn+1];
15641 fromPosition = p->position[2*fromChn+state[fromChn]];
15642 toRateMult = p->rateMult[2*toChn];
15643 swapRateMult = p->rateMult[2*toChn+1];
15644 fromRateMult = p->rateMult[2*fromChn+state[fromChn]];
15645 for (k=0; k<2*numLocalTaxa; k++)
15646 {
15647 if (from[k] > 0)
15648 {
15649 toPosition[k] = (MrBFlt *) SafeRealloc ((void *)toPosition[k], from[k]*sizeof (MrBFlt));
15650 toRateMult[k] = (MrBFlt *) SafeRealloc ((void *)toRateMult[k], from[k]*sizeof (MrBFlt));
15651 for (k1=0; k1<from[k]; k1++)
15652 {
15653 toPosition[k][k1] = fromPosition[k][k1];
15654 toRateMult[k][k1] = fromRateMult[k][k1];
15655 }
15656 }
15657 else if (to[k] > 0)
15658 {
15659 free (toPosition[k]);
15660 toPosition[k] = NULL;
15661 free (toRateMult[k]);
15662 toRateMult[k] = NULL;
15663 }
15664 to[k] = from[k];
15665 if (swap[k] > 0)
15666 {
15667 fromPosition[k] = (MrBFlt *) SafeRealloc ((void *)fromPosition[k], swap[k]*sizeof (MrBFlt));
15668 fromRateMult[k] = (MrBFlt *) SafeRealloc ((void *)fromRateMult[k], swap[k]*sizeof (MrBFlt));
15669 for (k1=0; k1<swap[k]; k1++)
15670 {
15671 fromPosition[k][k1] = swapPosition[k][k1];
15672 fromRateMult[k][k1] = swapRateMult[k][k1];
15673 }
15674 }
15675 else if (from[k] > 0)
15676 {
15677 free (fromPosition[k]);
15678 fromPosition[k] = NULL;
15679 free (fromRateMult[k]);
15680 fromRateMult[k] = NULL;
15681 }
15682 from[k] = swap[k];
15683 }
15684 }
15685 }
15686 /* reset state of chain */
15687 state[toChn] = 0;
15688
15689 /* make sure that the id is correct for the from chain */
15690 tempId = curId[toChn];
15691 curId[fromChn] = curId[toChn];
15692 curId[toChn] = tempId;
15693 }
15694
15695 free (curId);
15696 }
15697
15698
15699 /* ResetFlips: Reset flipped cond likes etc after rejection */
ResetFlips(int chain)15700 void ResetFlips (int chain)
15701 {
15702 int d, i;
15703 ModelInfo *m;
15704 TreeNode *p;
15705 Tree *tree;
15706 #if defined (BEAGLE_ENABLED)
15707 int *isScalerNode=NULL;
15708 #endif
15709
15710 for (d=0; d<numCurrentDivisions; d++)
15711 {
15712 m = &modelSettings[d];
15713 #if defined (BEAGLE_ENABLED)
15714 if (m->useBeagle == YES)
15715 isScalerNode = m->isScalerNode[chain];
15716 #endif
15717 if (m->upDateCl != YES)
15718 continue;
15719
15720 #if defined (BEAGLE_ENABLED)
15721 if (m->useBeagle == NO ||
15722 beagleScalingScheme == MB_BEAGLE_SCALE_ALWAYS ||
15723 m->rescaleBeagleAll == YES)
15724 {
15725 FlipSiteScalerSpace (m, chain);
15726 if (m->useBeagle == YES && m->rescaleBeagleAll == YES)
15727 m->rescaleFreq[chain] = m->rescaleFreqOld;
15728 }
15729 #else
15730 FlipSiteScalerSpace (m, chain);
15731 #endif
15732
15733
15734 if (m->upDateCijk == YES && m->nCijkParts > 0)
15735 FlipCijkSpace (m, chain);
15736
15737 /* cycle over tree */
15738 tree = GetTree (m->brlens, chain, state[chain]);
15739 for (i=0; i<tree->nNodes; i++)
15740 {
15741 p = tree->allDownPass[i];
15742 if (p->upDateTi == YES)
15743 FlipTiProbsSpace (m, chain, p->index);
15744 if (p->right != NULL) /* do not flip terminals in case these flags are inappropriately set by moves */
15745 {
15746 if (p->upDateCl == YES)
15747 {
15748 FlipCondLikeSpace (m, chain, p->index);
15749 #if defined (BEAGLE_ENABLED)
15750 if (m->useBeagle == NO ||
15751 beagleScalingScheme == MB_BEAGLE_SCALE_ALWAYS ||
15752 (m->rescaleBeagleAll == YES && isScalerNode[p->index] == YES))
15753 FlipNodeScalerSpace (m, chain, p->index);
15754 #else
15755 FlipNodeScalerSpace (m, chain, p->index);
15756 #endif
15757 }
15758 #if defined (BEAGLE_ENABLED)
15759 else if (m->rescaleBeagleAll == YES)
15760 {
15761 FlipCondLikeSpace (m, chain, p->index);
15762 if (isScalerNode[p->index] == YES)
15763 FlipNodeScalerSpace (m, chain, p->index);
15764 }
15765 #endif
15766 }
15767 }
15768
15769 /* division flag and tree node flags are reset when trees are copied */
15770 }
15771 }
15772
15773
15774 /*-------------------------------------------------------------------
15775 |
15776 | ResetScalersPartition: reset scaler nodes of the given tree by appropriately setting isScalerNode array.
15777 | @param isScalerNode is an array which gets set with information about scaler node.
15778 | For each internal node isScalerNode[node->index] is set to YES if it has to be scaler node.
15779 | Note: Only internal nodes can become scaler nodes thus isScalerNode is set only for elemnts in interval [numLocalTaxa, numLocalTaxa+t->nIntNodes]
15780 |
15781 | @param rescaleFreq effectively represent gaps between rescaling, higher number means more sparse choice of rescaling nodes
15782 |
15783 --------------------------------------------------------------------*/
ResetScalersPartition(int * isScalerNode,Tree * t,unsigned rescaleFreq)15784 int ResetScalersPartition (int *isScalerNode, Tree* t, unsigned rescaleFreq)
15785 {
15786 int n;
15787 TreeNode *p;
15788
15789 /* set the node depth value of terminal nodes to zero; reset scalerNode */
15790 for (n=0; n<t->nNodes; n++)
15791 {
15792 p = t->allDownPass[n];
15793 if (p->left == NULL)
15794 p->x = 0;
15795 }
15796
15797 /* loop over interior nodes */
15798 for (n=0; n<t->nIntNodes; n++)
15799 {
15800 p = t->intDownPass[n];
15801 assert (((p->index - numLocalTaxa) >= 0) && ((p->index - numLocalTaxa) < t->nIntNodes));
15802 p->x = p->left->x + p->right->x + 1;
15803
15804 if (p->x > 2 * (int)rescaleFreq)
15805 {
15806 assert (p->left->left != NULL && p->right->left != NULL);
15807 isScalerNode[p->left->index] = YES;
15808 p->left->x = 0;
15809 isScalerNode[p->right->index] = YES;
15810 p->right->x = 0;
15811 p->x = 1;
15812 }
15813 else if (p->x > (int)rescaleFreq)
15814 {
15815 if (p->left->x > p->right->x)
15816 {
15817 assert (p->left->left != NULL);
15818 isScalerNode[p->left->index] = YES;
15819 p->left->x = 0;
15820 }
15821 else
15822 {
15823 assert (p->right->left != NULL);
15824 isScalerNode[p->right->index] = YES;
15825 p->right->x = 0;
15826 }
15827 p->x = p->left->x + p->right->x + 1;
15828 }
15829 else
15830 isScalerNode[p->index] = NO;
15831 }
15832
15833 return NO_ERROR;
15834 }
15835
15836
15837 /*----------------------------------------------------------------------
15838 |
15839 | ResetSiteScalers: Set log site scalers to 0.0.
15840 |
15841 ------------------------------------------------------------------------*/
ResetSiteScalers(ModelInfo * m,int chain)15842 void ResetSiteScalers (ModelInfo *m, int chain)
15843 {
15844 int c;
15845 CLFlt *lnScaler;
15846
15847 #if defined (BEAGLE_ENABLED)
15848 if (m->useBeagle == YES)
15849 {
15850 if (m->useBeagleMultiPartitions == NO)
15851 beagleResetScaleFactors(m->beagleInstance, m->siteScalerIndex[chain]);
15852 else
15853 # if defined (BEAGLE_V3_ENABLED)
15854 beagleResetScaleFactorsByPartition(m->beagleInstance, m->siteScalerIndex[chain], m->divisionIndex);
15855 # endif /* BEAGLE_V3_ENABLED */
15856 /* TODO: check if nCijkParts scale factors should also be reset here */
15857 return;
15858 }
15859 #endif
15860 lnScaler = m->scalers[m->siteScalerIndex[chain]];
15861 for (c=0; c<m->numChars; c++)
15862 lnScaler[c] = 0.0;
15863 }
15864
15865
15866 /*----------------------------------------------------------------------
15867 |
15868 | ReusePreviousResults: Save old .p, .t, .ss and .mcmc files with ~ extension,
15869 | then prepare new print files with the relevant old values added in
15870 | The number of samples is returned in numSamples
15871 |
15872 ------------------------------------------------------------------------*/
ReusePreviousResults(int * numSamples,int steps)15873 int ReusePreviousResults (int *numSamples, int steps)
15874 {
15875 int i, n;
15876 char localFileName[100], fileName[220], bkupName[220];
15877
15878 (*numSamples) = 0;
15879
15880 # if defined (MPI_ENABLED)
15881 if (proc_id != 0)
15882 return (NO_ERROR);
15883 # endif
15884
15885 /* Allocate space for file pointers */
15886 if (memAllocs[ALLOC_FILEPOINTERS] == YES)
15887 {
15888 MrBayesPrint ("%s File pointers already allocated in ReusePreviousResults\n", spacer);
15889 return ERROR;
15890 }
15891 fpMcmc = NULL;
15892 fpSS = NULL;
15893 fpParm = NULL;
15894 fpTree = NULL;
15895 fpParm = (FILE **) SafeCalloc (chainParams.numRuns, sizeof (FILE *));
15896 if (fpParm == NULL)
15897 {
15898 MrBayesPrint ("%s Could not allocate fpParm in ReusePreviousResults\n", spacer);
15899 return ERROR;
15900 }
15901 memAllocs[ALLOC_FILEPOINTERS] = YES;
15902 fpTree = (FILE ***) SafeCalloc (chainParams.numRuns, sizeof (FILE **));
15903 if (fpTree == NULL)
15904 {
15905 MrBayesPrint ("%s Could not allocate fpTree in ReusePreviousResults\n", spacer);
15906 return ERROR;
15907 }
15908 fpTree[0] = (FILE **) SafeCalloc (numTrees*chainParams.numRuns, sizeof (FILE *));
15909 if (fpTree[0] == NULL)
15910 {
15911 MrBayesPrint ("%s Could not allocate fpTree[0] in ReusePreviousResults\n", spacer);
15912 return ERROR;
15913 }
15914 for (i=1; i<chainParams.numRuns; i++)
15915 fpTree[i] = fpTree[0] + i*numTrees;
15916
15917 /* Get root of local file name */
15918 strcpy (localFileName, chainParams.chainFileName);
15919
15920 /* Store old and prepare new .p and .t files */
15921 for (n=0; n<chainParams.numRuns; n++)
15922 {
15923 if (chainParams.numRuns == 1)
15924 sprintf (fileName, "%s%s.p", workingDir, localFileName);
15925 else
15926 sprintf (fileName, "%s%s.run%d.p", workingDir, localFileName, n+1);
15927 strcpy(bkupName,fileName);
15928 strcat(bkupName,"~");
15929 remove(bkupName);
15930 if (rename(fileName,bkupName) != 0)
15931 {
15932 MrBayesPrint ("%s Could not rename file %s\n", spacer, fileName);
15933 return ERROR;
15934 }
15935
15936 if ((fpParm[n] = OpenNewMBPrintFile (fileName+strlen(workingDir))) == NULL)
15937 return (ERROR);
15938 else if (CopyResults(fpParm[n],bkupName+strlen(workingDir),numPreviousGen) == ERROR)
15939 return (ERROR);
15940
15941 for (i=0; i<numTrees; i++)
15942 {
15943 if (numTrees == 1 && chainParams.numRuns == 1)
15944 sprintf (fileName, "%s%s.t", workingDir, localFileName);
15945 else if (numTrees > 1 && chainParams.numRuns == 1)
15946 sprintf (fileName, "%s%s.tree%d.t", workingDir, localFileName, i+1);
15947 else if (numTrees == 1 && chainParams.numRuns > 1)
15948 sprintf (fileName, "%s%s.run%d.t", workingDir, localFileName, n+1);
15949 else
15950 sprintf (fileName, "%s%s.tree%d.run%d.t", workingDir, localFileName, i+1, n+1);
15951 strcpy(bkupName,fileName);
15952 strcat(bkupName,"~");
15953 remove(bkupName);
15954 if (rename(fileName,bkupName) != 0)
15955 {
15956 MrBayesPrint ("%s Could not rename file %s\n", spacer, fileName);
15957 return ERROR;
15958 }
15959 if ((fpTree[n][i] = OpenNewMBPrintFile (fileName+strlen(workingDir))) == NULL)
15960 return (ERROR);
15961 else if (CopyTreeResults(fpTree[n][i],bkupName+strlen(workingDir),numPreviousGen,numSamples) == ERROR)
15962 return (ERROR);
15963 }
15964 }
15965
15966 /* Store old and prepare new .ss file */
15967 if (chainParams.isSS == YES)
15968 {
15969 sprintf (fileName, "%s%s.ss", workingDir, chainParams.chainFileName);
15970 strcpy(bkupName,fileName);
15971 strcat(bkupName,"~");
15972 remove(bkupName);
15973 if (rename(fileName,bkupName) != 0)
15974 {
15975 MrBayesPrint ("%s Could not rename file %s\n", spacer, fileName);
15976 return ERROR;
15977 }
15978 if ((fpSS = OpenNewMBPrintFile (fileName+strlen(workingDir))) == NULL)
15979 return (ERROR);
15980 else if (CopyProcessSsFile(fpSS,bkupName+strlen(workingDir),steps,marginalLnLSS,splitfreqSS)==ERROR)
15981 return (ERROR);
15982 }
15983
15984 /* Store old and prepare new .mcmc file */
15985 if (chainParams.mcmcDiagn == YES)
15986 {
15987 sprintf (fileName, "%s%s.mcmc", workingDir, chainParams.chainFileName);
15988 strcpy(bkupName,fileName);
15989 strcat(bkupName,"~");
15990 remove(bkupName);
15991 if (rename(fileName,bkupName) != 0)
15992 {
15993 MrBayesPrint ("%s Could not rename file %s\n", spacer, fileName);
15994 return ERROR;
15995 }
15996 if ((fpMcmc = OpenNewMBPrintFile (fileName+strlen(workingDir))) == NULL)
15997 return (ERROR);
15998 else if (CopyResults(fpMcmc,bkupName+strlen(workingDir),numPreviousGen)==ERROR)
15999 return (ERROR);
16000 }
16001
16002 # if defined (PRINT_DUMP)
16003 fpDump = (FILE **) SafeCalloc (chainParams.numRuns, sizeof (FILE *));
16004
16005 for (n=0; n<chainParams.numRuns; n++)
16006 {
16007 if (chainParams.numRuns == 1)
16008 sprintf (fileName, "%s.dump", localFileName);
16009 else
16010 sprintf (fileName, "%s.run%d.dump", localFileName, n+1);
16011
16012 if ((fpDump[n] = OpenTextFileA (fileName)) == NULL)
16013 return (ERROR);
16014 }
16015 # endif
16016
16017 return (NO_ERROR);
16018 }
16019
16020
RunChain(RandLong * seed)16021 int RunChain (RandLong *seed)
16022 {
16023 int i, j, n, chn, swapA=0, swapB=0, whichMove, acceptMove;
16024 int lastDiagnostics; // the sample no. when last diagnostic was performed
16025 int removeFrom, removeTo=0;
16026 int stopChain, nErrors;
16027 MrBFlt r=0.0, lnLikelihoodRatio, lnPriorRatio, lnProposalRatio, lnLike=0.0, lnPrior=0.0, f=0.0, CPUTime;
16028 MCMCMove *theMove, *mv;
16029 time_t startingT, endingT, stoppingT1, stoppingT2;
16030 clock_t previousCPUTime, currentCPUTime;
16031 # if defined (DEBUG_TIME)
16032 struct timespec tw1, tw2;
16033 # endif
16034 /* Stepping-stone sampling variables */
16035 int run, samplesCountSS=0, stepIndexSS=0, numGenInStepSS=0, numGenOld, lastStepEndSS=0, numGenInStepBurninSS=0;
16036 MrBFlt stepLengthSS=0, meanSS, varSS, *tempX;
16037 char ckpFileName[220], bkupFileName[220];
16038
16039 # if defined (BEAGLE_ENABLED)
16040 # ifdef DEBUG_BEAGLE
16041 int beagleScalingSchemeOld;
16042 # endif
16043 # endif
16044
16045 # if defined (MPI_ENABLED)
16046 int ierror, sumErrors;
16047 MrBFlt best, sum=0.0;
16048 MPI_Status status;
16049 # endif
16050 # if defined (DEBUG_RUNCHAIN)
16051 ModelInfo *m;
16052 # endif
16053
16054 /* set nErrors to 0 */
16055 nErrors = 0;
16056 if (numLocalTaxa < 4)
16057 {
16058 for (i=0; i<numTrees; i++)
16059 if (GetTreeFromIndex(i, 0, 0)->isRooted == NO)
16060 break;
16061 if (i < numTrees && numLocalTaxa < 4)
16062 {
16063 MrBayesPrint ("%s There must be at least four taxa in the analysis\n", spacer);
16064 return (ERROR);
16065 }
16066 else if (i == numTrees && numLocalTaxa < 3)
16067 {
16068 MrBayesPrint ("%s There must be at least three taxa in the analysis\n", spacer);
16069 return (ERROR);
16070 }
16071 }
16072
16073 /* allocate some memory for the chains */
16074 if (memAllocs[ALLOC_CURLNL] == YES)
16075 {
16076 MrBayesPrint ("%s curLnL is already allocated\n", spacer);
16077 nErrors++;
16078 }
16079 else if ((curLnL = (MrBFlt *)SafeMalloc((size_t)numLocalChains * sizeof(MrBFlt))) == NULL)
16080 {
16081 MrBayesPrint ("%s Problem allocating curLnL (%d)\n", spacer, numLocalChains * sizeof(MrBFlt));
16082 nErrors++;
16083 }
16084 else if ((maxLnL0 = (MrBFlt *) SafeCalloc ((size_t)(chainParams.numRuns) * (size_t)(chainParams.numChains), sizeof(MrBFlt))) == NULL)
16085 {
16086 MrBayesPrint ("%s Problem allocating maxLnL0\n", spacer, numLocalChains * sizeof(MrBFlt));
16087 free (curLnL);
16088 nErrors++;
16089 }
16090 else
16091 memAllocs[ALLOC_CURLNL] = YES;
16092 # if defined (MPI_ENABLED)
16093 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16094 if (sumErrors > 0)
16095 {
16096 MrBayesPrint ("%s Memory allocation error on at least one processor\n", spacer);
16097 return ERROR;
16098 }
16099 # else
16100 if (nErrors > 0)
16101 return ERROR;
16102 # endif
16103
16104 if (memAllocs[ALLOC_CURLNPR] == YES)
16105 {
16106 MrBayesPrint ("%s curLnPr is already allocated\n", spacer);
16107 nErrors++;
16108 }
16109 else if ((curLnPr = (MrBFlt *)SafeMalloc((size_t)numLocalChains * sizeof(MrBFlt))) == NULL)
16110 {
16111 MrBayesPrint ("%s Problem allocating curLnPr (%d)\n", spacer, numLocalChains * sizeof(MrBFlt));
16112 nErrors++;
16113 }
16114 else
16115 memAllocs[ALLOC_CURLNPR] = YES;
16116 # if defined (MPI_ENABLED)
16117 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16118 if (sumErrors > 0)
16119 {
16120 MrBayesPrint ("%s Memory allocation error on at least one processor\n", spacer);
16121 return ERROR;
16122 }
16123 # else
16124 if (nErrors > 0)
16125 return ERROR;
16126 # endif
16127
16128 if (memAllocs[ALLOC_CHAINID] == YES)
16129 {
16130 MrBayesPrint ("%s chainId is already allocated\n", spacer);
16131 nErrors++;
16132 }
16133 else if ((chainId = (int *)SafeMalloc((size_t)numLocalChains * sizeof(int))) == NULL)
16134 {
16135 MrBayesPrint ("%s Problem allocating chainId (%d)\n", spacer, numLocalChains * sizeof(int));
16136 nErrors++;
16137 }
16138 else
16139 memAllocs[ALLOC_CHAINID] = YES;
16140 # if defined (MPI_ENABLED)
16141 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16142 if (sumErrors > 0)
16143 {
16144 MrBayesPrint ("%s Memory allocation error on at least one processor\n", spacer);
16145 return ERROR;
16146 }
16147 # else
16148 if (nErrors > 0)
16149 return ERROR;
16150 # endif
16151
16152 if (memAllocs[ALLOC_SWAPINFO] == YES)
16153 {
16154 MrBayesPrint ("%s swapInfo is already allocated\n", spacer);
16155 nErrors++;
16156 }
16157 else if ((swapInfo = (int ***) SafeCalloc (chainParams.numRuns, sizeof (int **))) == NULL)
16158 {
16159 MrBayesPrint ("%s Problem allocating swapInfo\n", spacer);
16160 nErrors++;
16161 }
16162 else
16163 {
16164 for (n=0; n<chainParams.numRuns; n++)
16165 {
16166 swapInfo[n] = AllocateSquareIntegerMatrix (chainParams.numChains);
16167 if (!swapInfo[n])
16168 {
16169 MrBayesPrint ("%s Problem allocating swapInfo[%d]\n", spacer, n);
16170 for (i=0; i<n; i++)
16171 free (swapInfo[i]);
16172 free (swapInfo);
16173 nErrors++;
16174 break;
16175 }
16176 }
16177 memAllocs[ALLOC_SWAPINFO] = YES;
16178 }
16179 # if defined (MPI_ENABLED)
16180 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16181 if (sumErrors > 0)
16182 {
16183 MrBayesPrint ("%s Memory allocation error on at least one processor\n", spacer);
16184 return ERROR;
16185 }
16186 # else
16187 if (nErrors > 0)
16188 return ERROR;
16189 # endif
16190
16191 for (n=0; n<chainParams.numRuns; n++)
16192 for (i=0; i<chainParams.numChains; i++)
16193 for (j=0; j<chainParams.numChains; j++)
16194 swapInfo[n][i][j] = 0;
16195
16196 /* set up counters for topological convergence diagnostics */
16197 /* allocate tree used for some topological convergence diagnostics */
16198 if (chainParams.mcmcDiagn == YES && chainParams.numRuns > 1)
16199 {
16200 if (SetUpPartitionCounters () == ERROR)
16201 nErrors++;
16202 # if defined (MPI_ENABLED)
16203 if (proc_id == 0)
16204 {
16205 # endif
16206 if (chainParams.relativeBurnin == YES)
16207 {
16208 /* we have to remove trees later on */
16209 if (chainParams.saveTrees == YES)
16210 {
16211 if (chainParams.treeList)
16212 nErrors++;
16213 else
16214 {
16215 chainParams.treeList = (TreeList *) SafeCalloc (chainParams.numRuns*numTopologies, sizeof (TreeList));
16216 if (!chainParams.treeList)
16217 nErrors++;
16218 }
16219 if (nErrors == 0)
16220 memAllocs[ALLOC_TREELIST] = YES;
16221 if (noWarn == YES)
16222 chainParams.stopTreeGen = (int) (chainParams.numGen * chainParams.burninFraction);
16223 else
16224 chainParams.stopTreeGen = chainParams.numGen;
16225 }
16226 else /* if (chainParams.saveTrees == NO) */
16227 {
16228 chainParams.tFilePos = (fpos_t*) SafeRealloc ((void *)chainParams.tFilePos, chainParams.numRuns*numTopologies*sizeof (fpos_t));
16229 if (!chainParams.tFilePos)
16230 nErrors++;
16231 else
16232 memAllocs[ALLOC_TFILEPOS] = YES;
16233 }
16234 }
16235 # if defined (MPI_ENABLED)
16236 }
16237 # endif
16238 # if defined (MPI_ENABLED)
16239 if (proc_id == 0)
16240 {
16241 if ((chainParams.stat = (STATS *) SafeCalloc (numTopologies, sizeof (STATS))) == NULL)
16242 nErrors++;
16243 else
16244 {
16245 memAllocs[ALLOC_STATS] = YES;
16246 for (i=0; i<numTopologies; i++)
16247 chainParams.stat[i].pair = NULL;
16248 }
16249
16250 if ((chainParams.dtree = AllocateTree (numLocalTaxa)) == NULL)
16251 {
16252 nErrors++;
16253 }
16254 else
16255 memAllocs[ALLOC_DIAGNTREE] = YES;
16256
16257 if (chainParams.allComps == YES)
16258 {
16259 for (i=0; i<numTopologies; i++)
16260 {
16261 if ((chainParams.stat[i].pair = AllocateSquareDoubleMatrix (chainParams.numRuns)) == NULL)
16262 {
16263 nErrors++;
16264 break;
16265 }
16266 }
16267 }
16268 }
16269 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16270 if (sumErrors > 0)
16271 {
16272 MrBayesPrint ("%s Memory allocation error on at least one processor\n", spacer);
16273 return ERROR;
16274 }
16275 # else
16276 if ((chainParams.stat = (STATS *) SafeCalloc (numTopologies, sizeof (STATS))) == NULL)
16277 return ERROR;
16278 else
16279 {
16280 memAllocs[ALLOC_STATS] = YES;
16281 for (i=0; i<numTopologies; i++)
16282 chainParams.stat[i].pair = NULL;
16283 }
16284
16285 if ((chainParams.dtree = AllocateTree (numLocalTaxa)) == NULL)
16286 {
16287 MrBayesPrint ("%s Could not allocate chainParams.dtree in RunChain\n", spacer);
16288 return ERROR;
16289 }
16290 else
16291 memAllocs[ALLOC_DIAGNTREE] = YES;
16292
16293 if (chainParams.allComps == YES)
16294 {
16295 for (i=0; i<numTopologies; i++)
16296 {
16297 if ((chainParams.stat[i].pair = AllocateSquareDoubleMatrix (chainParams.numRuns)) == NULL)
16298 {
16299 MrBayesPrint ("%s Could not allocate chainParams.stat.pair in RunChain\n", spacer);
16300 return ERROR;
16301 }
16302 }
16303 }
16304 # endif
16305 }
16306
16307 /* get chain IDs */
16308 SetChainIds ();
16309
16310 /* distribute parameter starting values and tuning parameters for MPI version */
16311 # if defined (MPI_ENABLED)
16312 RedistributeParamVals();
16313 RedistributeTuningParams();
16314 # endif
16315
16316 # if defined (TIMING_ANALIZ)
16317 CPUCondLikeDown = 0;
16318 CPUScalers = 0;
16319 CPUScalersRemove = 0;
16320 CPUCondLikeRoot = 0;
16321 CPULilklihood = 0;
16322 # endif
16323
16324 /* initialize likelihoods and prior */
16325 /* touch everything and calculate initial cond likes */
16326 TouchAllPartitions ();
16327 for (chn=0; chn<numLocalChains; chn++)
16328 {
16329 if (chn % chainParams.numChains == 0)
16330 {
16331 if (chainParams.numRuns == 1)
16332 MrBayesPrint ("\n%s Initial log likelihoods and log prior probs:\n", spacer);
16333 else
16334 MrBayesPrint ("\n%s Initial log likelihoods and log prior probs for run %d:\n", spacer, chn / chainParams.numChains + 1);
16335 }
16336 TouchAllTrees (chn);
16337 TouchAllCijks (chn);
16338 curLnL[chn] = LogLike(chn);
16339 curLnPr[chn] = LogPrior(chn);
16340 for (i=0; i<numCurrentDivisions; i++)
16341 {
16342 if (modelSettings[i].gibbsGamma == YES)
16343 curLnL[chn] += GibbsSampleGamma (chn, i, seed);
16344 }
16345 MrBayesPrint ("%s Chain %d -- %.6lf -- %.6lf\n", spacer, (chn % chainParams.numChains) + 1, curLnL[chn], curLnPr[chn]);
16346 }
16347 MrBayesPrint("\n");
16348
16349 # if defined (MPI_ENABLED)
16350 if (num_procs > 2)
16351 MrBayesPrint ("%s There are %d more chains on other processor(s)\n\n", spacer, numGlobalChains - numLocalChains);
16352 else if (num_procs==2)
16353 MrBayesPrint ("%s There are %d more chains on the other processor\n\n", spacer, numGlobalChains - numLocalChains);
16354 # endif
16355
16356 /* All steps are assumed to have the same length. */
16357 if (chainParams.isSS == YES)
16358 {
16359 numGenInStepSS = (chainParams.numGen - chainParams.burninSS*chainParams.sampleFreq)/ chainParams.numStepsSS;
16360 numGenInStepSS = chainParams.sampleFreq*(numGenInStepSS/chainParams.sampleFreq); /*make muliple of chainParams.sampleFreq*/
16361 numGenOld = chainParams.numGen;
16362 chainParams.numGen = (chainParams.burninSS * chainParams.sampleFreq + chainParams.numStepsSS*numGenInStepSS) ;
16363 if (stepRelativeBurninSS==YES)
16364 numGenInStepBurninSS = ((int)(numGenInStepSS*chainParams.burninFraction / chainParams.sampleFreq))*chainParams.sampleFreq;
16365 else
16366 numGenInStepBurninSS = chainParams.chainBurnIn * chainParams.sampleFreq;
16367 MrBayesPrint ("\n");
16368 MrBayesPrint ("%s Starting stepping-stone sampling to estimate marginal likelihood. \n", spacer);
16369 MrBayesPrint ("%s %d steps will be used with %d generations (%d samples) within each step. \n", spacer, chainParams.numStepsSS, numGenInStepSS, numGenInStepSS/chainParams.sampleFreq);
16370 MrBayesPrint ("%s Total of %d generations (%d samples) will be collected while first \n", spacer, chainParams.numGen, chainParams.numGen/chainParams.sampleFreq);
16371 MrBayesPrint ("%s %d generations (%d samples) will be discarded as initial burnin. \n", spacer, chainParams.burninSS*chainParams.sampleFreq, chainParams.burninSS);
16372 MrBayesPrint ("%s Additionally at the beginning of each step %d generations (%d samples) \n", spacer, numGenInStepBurninSS, numGenInStepBurninSS/chainParams.sampleFreq);
16373 MrBayesPrint ("%s will be discarded as burnin. \n", spacer);
16374 if (chainParams.startFromPriorSS==YES)
16375 MrBayesPrint ("%s Sampling from prior to posterior, i.e. first step samples from prior. \n", spacer);
16376 else
16377 {
16378 MrBayesPrint ("%s Sampling from posterior to prior, i.e. first step samples from close to \n", spacer);
16379 MrBayesPrint ("%s posterior. \n", spacer);
16380 }
16381 if (numGenOld != chainParams.numGen)
16382 {
16383 MrBayesPrint ("%s NOTE: Number of generation of each step is reduced to the closest multi-\n", spacer);
16384 MrBayesPrint ("%s ple of sampling frequency. That is why, in total it will be taken %d \n", spacer, chainParams.numGen);
16385 MrBayesPrint ("%s generations instead of requested %d. \n", spacer, numGenOld);
16386 }
16387 MrBayesPrint ("\n");
16388 if ((numGenInStepSS-numGenInStepBurninSS)/chainParams.sampleFreq < 1)
16389 {
16390 MrBayesPrint ("%s There is less then one sample in each step of stepping-stone sampling. \n", spacer);
16391 MrBayesPrint ("%s Please adjust burnin, nuber of generations, sampling frequency or \n", spacer);
16392 MrBayesPrint ("%s numnber of step in order to allow at least one sample per step. \n", spacer);
16393 return ERROR; /*All MPI run will return here since all of them have the same values*/
16394 }
16395 if (numPreviousGen==0 || numPreviousGen < chainParams.burninSS * chainParams.sampleFreq)
16396 {
16397 lastStepEndSS = chainParams.burninSS * chainParams.sampleFreq;
16398 stepIndexSS = chainParams.numStepsSS-1;
16399 if (numPreviousGen != 0)
16400 removeTo=(numPreviousGen/chainParams.sampleFreq)+1;
16401 if (chainParams.startFromPriorSS==YES)
16402 {
16403 // powerSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)(chainParams.numStepsSS-1-stepIndexSS)/(MrBFlt)chainParams.numStepsSS);
16404 powerSS = 0.0;
16405 stepLengthSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)(chainParams.numStepsSS-stepIndexSS)/(MrBFlt)chainParams.numStepsSS)-powerSS;
16406 }
16407 else
16408 {
16409 powerSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)stepIndexSS/(MrBFlt)chainParams.numStepsSS);
16410 stepLengthSS = 1.0-powerSS;
16411 }
16412 samplesCountSS=0;
16413 }
16414 else
16415 {
16416 stepIndexSS = (numPreviousGen-chainParams.burninSS * chainParams.sampleFreq)/numGenInStepSS; /* for now it holds number of steps we fully complited*/
16417 lastStepEndSS = chainParams.burninSS * chainParams.sampleFreq + stepIndexSS*numGenInStepSS;
16418 removeTo = chainParams.burninSS + (stepIndexSS*numGenInStepSS+numGenInStepBurninSS)/chainParams.sampleFreq + 1;
16419 if (numPreviousGen < (removeTo-1)*chainParams.sampleFreq)
16420 removeTo=numPreviousGen/chainParams.sampleFreq+1;
16421 stepIndexSS = chainParams.numStepsSS-1-stepIndexSS;
16422 if (chainParams.startFromPriorSS==YES)
16423 {
16424 powerSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)(chainParams.numStepsSS-1-stepIndexSS)/(MrBFlt)chainParams.numStepsSS);
16425 stepLengthSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)(chainParams.numStepsSS-stepIndexSS)/(MrBFlt)chainParams.numStepsSS)-powerSS;
16426 }
16427 else
16428 {
16429 powerSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)stepIndexSS/(MrBFlt)chainParams.numStepsSS);
16430 stepLengthSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)(stepIndexSS+1)/(MrBFlt)chainParams.numStepsSS)-powerSS;
16431 }
16432 # ifdef SAMPLE_ALL_SS
16433 samplesCountSS = (numPreviousGen-lastStepEndSS-numGenInStepBurninSS);
16434 # else
16435 samplesCountSS = (numPreviousGen-lastStepEndSS-numGenInStepBurninSS)/chainParams.sampleFreq;
16436 # endif
16437 if (samplesCountSS < 0)
16438 samplesCountSS=0;
16439
16440 MrBayesPrint("%s Continue sampling step %d out of %d steps...\n",spacer, chainParams.numStepsSS-stepIndexSS, chainParams.numStepsSS);
16441 /*marginalLnLSS will be red from file and destributed to other MPI_proc later. stepScalerSS, stepAcumulatorSS are lready red and if (samplesCountSS!=0) they will be redestributed. */
16442 }
16443
16444 if (samplesCountSS == 0) /* in appended case it also can happen */
16445 {
16446 for (run=0; run<chainParams.numRuns; run++)
16447 {
16448 marginalLnLSS[run] = 0.0;
16449 stepScalerSS[run] = 0.0;
16450 stepAcumulatorSS[run] = 0.0;
16451 }
16452
16453 for (chn=0; chn<numLocalChains; chn++)
16454 {
16455 if (chainId[chn] % chainParams.numChains == 0)
16456 {
16457 run = chainId[chn] / chainParams.numChains;
16458 stepScalerSS[run] = curLnL[chn]*stepLengthSS;
16459 }
16460 }
16461 }
16462 }
16463
16464 /* Append to previous analysis if this is requested, otherwise just open new print files */
16465 if (chainParams.append == YES)
16466 {
16467 # if defined (MPI_ENABLED)
16468 if (proc_id == 0) {
16469 # endif
16470 /* We get the number of samples in i */
16471 if (ReusePreviousResults(&i, chainParams.numStepsSS-stepIndexSS-1) == ERROR || i < 2)
16472 nErrors++;
16473 else if (chainParams.numRuns > 1 && chainParams.mcmcDiagn == YES) /* we potentially need to add tree samples for conv diagn */
16474 {
16475 /* Add tree samples to partition counters */
16476 if (chainParams.relativeBurnin == YES)
16477 {
16478 if (numPreviousGen/(i-1) != chainParams.sampleFreq)
16479 {
16480 MrBayesPrint ("%s 1. Use the same sampling frequency as in the previous run to use relative burnin.\n", spacer);
16481 MrBayesPrint ("%s 2. Check (and modify) the number in [generation: number] at line 3 of the .ckp file\n", spacer);
16482 MrBayesPrint ("%s to match the previous number of generations in all the .p and .t files. This may\n", spacer);
16483 MrBayesPrint ("%s happen if checkfreq was smaller than samplefreq.\n", spacer);
16484 MrBayesPrint ("%s 3. Rarely, delete the last sample/line in the .p and .t files to achieve 2. above.\n", spacer);
16485 MrBayesPrint ("%s This may happen if ngen was not divisible by samplefreq.\n", spacer);
16486 nErrors++;
16487 }
16488 if (chainParams.isSS == NO)
16489 {
16490 if (noWarn == YES)
16491 {
16492 /* We definitely know the final number of generations */
16493 j = (chainParams.numGen/chainParams.sampleFreq)+1;
16494 j = (int) (j*chainParams.burninFraction);
16495 }
16496 else /* User may extend chain so save all trees if saving trees */
16497 j = i;
16498 if (j < i)
16499 {
16500 if (AddTreeSamples(1,j,chainParams.saveTrees) == ERROR) nErrors++;
16501 if (AddTreeSamples(j+1,i,NO) == ERROR) nErrors++;
16502 /* Since we never need to remove trees from partition counter after total burnin we put NO in the last argument */
16503 }
16504 else
16505 {
16506 if (AddTreeSamples(1,i,chainParams.saveTrees) == ERROR) nErrors++;
16507 }
16508 }
16509 else
16510 {
16511 if (SetFilePositions(removeTo) == ERROR) nErrors++;
16512 if (AddTreeSamples(removeTo+1,i,chainParams.saveTrees) == ERROR) nErrors++;
16513 }
16514 }
16515 else if (chainParams.chainBurnIn < i)
16516 {
16517 if (AddTreeSamples(chainParams.chainBurnIn,i-1,chainParams.saveTrees) == ERROR) nErrors++;
16518 }
16519 }
16520 if (nErrors == 0)
16521 {
16522 if (chainParams.isSS == NO && chainParams.mcmcDiagn == YES && chainParams.numRuns > 1)
16523 {
16524 MrBayesPrint ("\n");
16525 if (chainParams.relativeBurnin == YES)
16526 MrBayesPrint ("%s Using a relative burnin of %.1f %% for diagnostics\n", spacer, 100.0*chainParams.burninFraction);
16527 else
16528 MrBayesPrint ("%s Using an absolute burnin of %d samples for diagnostics\n", spacer, chainParams.chainBurnIn);
16529 }
16530 MrBayesPrint ("\n");
16531 MrBayesPrint ("%s Chain results (continued from previous run; %d generations requested):\n\n", spacer, chainParams.numGen);
16532 }
16533 # if defined (MPI_ENABLED)
16534 }
16535 # endif
16536
16537 if (chainParams.autotune == YES)
16538 {
16539 for (i=0; i<numLocalChains; i++)
16540 {
16541 for (j=0; j<numUsedMoves; j++)
16542 {
16543 if (j==0)
16544 f = usedMoves[j]->cumProposalProb[chainId[i]];
16545 else
16546 f = usedMoves[j]->cumProposalProb[chainId[i]] - usedMoves[j-1]->cumProposalProb[chainId[i]];
16547 if (usedMoves[j]->targetRate[chainId[i]] > 0.0)
16548 {
16549 /* autotuned move; we assume it was perfectly tuned and start tuning from there at appropriate rate */
16550 usedMoves[j]->nBatches[chainId[i]] = (int) (f*numPreviousGen/chainParams.tuneFreq);
16551 usedMoves[j]->lastAcceptanceRate[chainId[i]] = usedMoves[j]->targetRate[chainId[i]];
16552 }
16553 else
16554 {
16555 /* not autotuned move; no previous batches will result in acceptance rate calculated from new samples */
16556 usedMoves[j]->nBatches[chainId[i]] = 0;
16557 usedMoves[j]->lastAcceptanceRate[chainId[i]] = 0.0;
16558 }
16559 }
16560 }
16561 }
16562 # if defined (MPI_ENABLED)
16563 if (chainParams.isSS == YES)
16564 {
16565 MPI_Bcast (marginalLnLSS, chainParams.numRuns, MPI_DOUBLE, 0, MPI_COMM_WORLD);
16566
16567 if (samplesCountSS != 0)
16568 {
16569 MPI_Bcast (stepScalerSS, chainParams.numRuns, MPI_DOUBLE, 0, MPI_COMM_WORLD);
16570 MPI_Bcast (stepAcumulatorSS, chainParams.numRuns, MPI_DOUBLE, 0, MPI_COMM_WORLD);
16571 }
16572 /*Set to zero all runs that we are not responsable for*/
16573 for (run=0; run<chainParams.numRuns; run++)
16574 {
16575 for (chn=0; chn<numLocalChains; chn++)
16576 {
16577 if (chainId[chn] % chainParams.numChains == 0 && run == chainId[chn] / chainParams.numChains)
16578 break;
16579 }
16580 if (chn<numLocalChains)
16581 continue;
16582
16583 marginalLnLSS[run] = 0.0;
16584 stepScalerSS[run] = 0.0;
16585 stepAcumulatorSS[run] = 0.0;
16586 }
16587 }
16588
16589 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16590 if (sumErrors > 0)
16591 {
16592 MrBayesPrint ("%s Error appending to previous run\n", spacer);
16593 return ERROR;
16594 }
16595 # else
16596 if (nErrors == 1)
16597 {
16598 MrBayesPrint ("%s Error appending to previous run\n", spacer);
16599 return ERROR;
16600 }
16601 # endif
16602 }
16603 else
16604 {
16605 if (PreparePrintFiles() == ERROR)
16606 nErrors++;
16607 }
16608
16609 # if defined (MPI_ENABLED)
16610 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16611 if (sumErrors > 0)
16612 {
16613 MrBayesPrint ("%s Error preparing print files on at least one processor\n", spacer);
16614 CloseMBPrintFiles();
16615 return ERROR;
16616 }
16617 # else
16618 if (nErrors > 0)
16619 {
16620 MrBayesPrint ("%s Error preparing print files\n", spacer);
16621 CloseMBPrintFiles();
16622 return ERROR;
16623 }
16624 # endif
16625
16626 stopChain = NO;
16627
16628 for (i=0; i<chainParams.numRuns; i++)
16629 maxLnL0[i] = -100000000.0;
16630
16631 startingT=time(0);
16632 # if defined (DEBUG_TIME)
16633 clock_gettime(CLOCK_MONOTONIC, &tw1);
16634 # endif
16635 CPUTime = 0.0;
16636 previousCPUTime = clock();
16637
16638 /* print headers and starting states */
16639 if (numPreviousGen==0)
16640 {
16641 /* make sure we print headers */
16642 PrintToScreen(0, 0, time(0), startingT);
16643 if (PrintStatesToFiles (0) == ERROR)
16644 {
16645 MrBayesPrint("%s Error in printing headers to files\n");
16646 # if defined (MPI_ENABLED)
16647 nErrors++;
16648 # else
16649 return ERROR;
16650 # endif
16651 }
16652 # if defined (MPI_ENABLED)
16653 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16654 if (sumErrors > 0)
16655 {
16656 MrBayesPrint ("%s Aborting run.\n");
16657 return ERROR;
16658 }
16659 # endif
16660
16661 if (chainParams.mcmcDiagn == YES)
16662 {
16663 if (PrintMCMCDiagnosticsToFile (0) == ERROR)
16664 {
16665 MrBayesPrint ("%s Problem printing mcmc diagnostics headers to file\n", spacer);
16666 # if defined (MPI_ENABLED)
16667 nErrors++;
16668 # else
16669 return (ERROR);
16670 # endif
16671 }
16672 # if defined (MPI_ENABLED)
16673 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16674 if (sumErrors > 0)
16675 {
16676 MrBayesPrint ("%s Aborting run.\n");
16677 return ERROR;
16678 }
16679 # endif
16680
16681 if (chainParams.isSS == YES && chainParams.burninSS == 0 && chainParams.numRuns > 1)
16682 {
16683 /* Remove first sample (generation 0) from diagnostics */
16684 removeTo=1;
16685 if (RemoveTreeSamples (1,1) == ERROR)
16686 {
16687 MrBayesPrint("%s Problem removing tree samples\n");
16688 # if defined (MPI_ENABLED)
16689 nErrors++;
16690 # else
16691 return ERROR;
16692 # endif
16693 }
16694 # if defined (MPI_ENABLED)
16695 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
16696 if (sumErrors > 0)
16697 {
16698 MrBayesPrint ("%s Aborting run.\n");
16699 return ERROR;
16700 }
16701 # endif
16702 }
16703 }
16704 if (chainParams.isSS == YES)
16705 {
16706 if (chainParams.burninSS == 0)
16707 MrBayesPrint("%s Sampling step 1 out of %d steps...\n\n",spacer, chainParams.numStepsSS);
16708
16709 /*Printing SS header*/
16710 MrBayesPrintf (fpSS, "[LEGEND: The file contains statistics on the Steppingstone Sampling.]\n");
16711 MrBayesPrintf (fpSS, "[ID: %s]\n", stamp);
16712 MrBayesPrintf (fpSS, "[ Step -- Index of the step ]\n");
16713 MrBayesPrintf (fpSS, "[ Power -- At each step we sample from the distribution with density (Likelihood^Power)*Prior ]\n");
16714 MrBayesPrintf (fpSS, "[ runX -- Contribution to the marginal log likelihood of run X, i.e. marginal log likelihood for run X is the sum across all steps in column runX. ]\n");
16715 if (chainParams.diagnStat == AVGSTDDEV)
16716 MrBayesPrintf (fpSS, "[ aSplitX -- Average standard deviation of split frequencies of tree X. -2.0 is printed if no diagnostics was requested. -1.0 is printed if there were no splits with frequency above minimum.]\n");
16717 else
16718 MrBayesPrintf (fpSS, "[ mSplitX -- Maximal standard deviation of split frequencies of tree X. -2.0 is printed if no diagnostics was requested. -1.0 is printed if there were no splits with frequency above minimum.]\n");
16719 MrBayesPrintf (fpSS, "Step\tPower");
16720 for (j=0; j<chainParams.numRuns ; j++)
16721 MrBayesPrintf (fpSS, "\trun%d", j+1);
16722 if (chainParams.diagnStat == AVGSTDDEV)
16723 {
16724 for (j=0; j<numTopologies; j++)
16725 MrBayesPrintf (fpSS, "\taSplit%d", j);
16726 }
16727 else
16728 {
16729 for (j=0; j<numTopologies; j++)
16730 MrBayesPrintf (fpSS, "\tmSplit%d", j);
16731 }
16732 MrBayesPrintf (fpSS, "\n");
16733 }
16734 }
16735
16736 for (n=numPreviousGen+1; n<=chainParams.numGen; n++) /* begin run chain */
16737 {
16738 currentCPUTime = clock();
16739 if (currentCPUTime - previousCPUTime > 10 * CLOCKS_PER_SEC)
16740 {
16741 CPUTime += (currentCPUTime - previousCPUTime) / (MrBFlt) (CLOCKS_PER_SEC);
16742 previousCPUTime = currentCPUTime;
16743 }
16744
16745 /*! requestAbortRun is set by the signal handler when it receives a CTRL-C (serial version only) */
16746 if (requestAbortRun == YES && ConfirmAbortRun() == 1)
16747 return ABORT;
16748
16749 // RandLong oldSeed = *seed; /* record the old seed for debugging */
16750 for (chn=0; chn<numLocalChains; chn++)
16751 {
16752 /* Do Gibbs resampling of rate categories for current state if time to do so */
16753 for (i=0; i<numCurrentDivisions; i++)
16754 {
16755 if (modelSettings[i].gibbsGamma == YES && n % modelSettings[i].gibbsFreq == 0)
16756 curLnL[chn] += GibbsSampleGamma (chn, i, seed);
16757 }
16758
16759 /* First copy everything from current state of chain to new state. */
16760 /* The global variable state[chain] gives state. */
16761
16762 /* copy all touched trees and reset update flags */
16763 CopyTrees (chn);
16764
16765 /* copy all model parameters */
16766 CopyParams (chn);
16767
16768 /* shift the state of the chain to the new state */
16769 /* all calculations will be done on this state */
16770 state[chn] ^= 1; /* XORing with 1 switches between 0 and 1 */
16771
16772 /* decide which move to make */
16773 whichMove = PickProposal(seed, chainId[chn]);
16774 theMove = usedMoves[whichMove];
16775 # if defined SHOW_MOVE
16776 printf ("Making move '%s'\n", theMove->name);
16777 # endif
16778
16779 # if defined (BEST_MPI_ENABLED)
16780 bestCycleGen = n % (numNonTreeMoves + numTreeMoves + numBestMoves);
16781 if (bestCycleGen < numNonTreeMoves)
16782 PickNonTreeProposal(seed, chainId[chn]);
16783 else if (bestCycleGen < numNonTreeMoves + numTreeMoves)
16784 PickTreeProposal(seed, chainId[chn]);
16785 else
16786 PickBestProposal(chainId[chn]);
16787 # endif
16788
16789 /* set prior and proposal ratios */
16790 lnProposalRatio = 0.0;
16791 lnPriorRatio = 0.0;
16792
16793 /* reset abort move flag */
16794 abortMove = NO;
16795
16796 /* Touch the relevant partitions */
16797 /* as a service to the move functions. */
16798 for (i=0; i<theMove->parm->nRelParts; i++)
16799 modelSettings[theMove->parm->relParts[i]].upDateCl = YES;
16800
16801 # ifndef NDEBUG
16802 if (IsTreeConsistent(theMove->parm, chn, state[chn]) != YES)
16803 {
16804 printf ("IsTreeConsistent failed before a move!\n");
16805 return ERROR;
16806 }
16807 # endif
16808 # if defined (DEBUG_CONSTRAINTS)
16809 if (theMove->parm->paramType == P_TOPOLOGY && DoesTreeSatisfyConstraints(GetTree (theMove->parm, chn, state[chn]))!=YES)
16810 {
16811 printf ("DEBUG ERROR: DoesTreeSatisfyConstraints failed before a move\n");
16812 return ERROR;
16813 }
16814 # endif
16815 /* make move */
16816 if ((theMove->moveFxn)(theMove->parm, chn, seed, &lnPriorRatio, &lnProposalRatio, theMove->tuningParam[chainId[chn]]) == ERROR)
16817 {
16818 printf ("%s Error in move %s\n", spacer, theMove->name);
16819 # if defined (MPI_ENABLED)
16820 nErrors++;
16821 # else
16822 return ERROR;
16823 # endif
16824 }
16825
16826 if (theMove->parm->paramType == P_TOPOLOGY && DoesTreeSatisfyConstraints(GetTree (theMove->parm, chn, state[chn])) != YES)
16827 {
16828 # if defined (DEBUG_CONSTRAINTS)
16829 if (DoesTreeSatisfyConstraints(GetTree (theMove->parm, chn, state[chn]))==ABORT)
16830 {
16831 printf ("DEBUG ERROR: DoesTreeSatisfyConstraints failed after move '%s'\n", theMove->name);
16832 }
16833 # endif
16834 abortMove = YES;
16835 }
16836
16837 /* abortMove is set to YES if the calculation fails because the likelihood is too small */
16838 if (abortMove == NO)
16839 lnLike = LogLike(chn);
16840
16841 /* calculate acceptance probability */
16842 if (abortMove == NO)
16843 {
16844 lnLikelihoodRatio = lnLike - curLnL[chn];
16845 lnPrior = curLnPr[chn] + lnPriorRatio;
16846
16847 # ifndef NDEBUG
16848 /* We check various aspects of calculations in debug version of code */
16849 if (IsTreeConsistent(theMove->parm, chn, state[chn]) != YES)
16850 {
16851 printf ("DEBUG ERROR: IsTreeConsistent failed after move '%s'\n", theMove->name);
16852 return ERROR;
16853 }
16854 if (lnPriorRatio != lnPriorRatio)
16855 {
16856 printf ("DEBUG ERROR: Log prior ratio nan after move '%s'\n", theMove->name);
16857 // printf ("Seed: %ld\n", oldSeed); state[chn] ^= 1; PrintCheckPoint (n);
16858 return ERROR;
16859 }
16860 if (fabs((lnPrior-LogPrior(chn))/lnPrior) > 0.0001)
16861 {
16862 printf ("DEBUG ERROR: Log prior incorrect after move '%s' :%e :%e\n", theMove->name,lnPrior,LogPrior(chn));
16863 // printf ("Seed: %ld\n", oldSeed); state[chn] ^= 1; PrintCheckPoint (n);
16864 return ERROR;
16865 }
16866 if (lnProposalRatio != lnProposalRatio)
16867 {
16868 printf ("DEBUG ERROR: Log proposal ratio nan after move '%s'\n", theMove->name);
16869 // printf ("Seed: %ld\n", oldSeed); state[chn] ^= 1; PrintCheckPoint (n);
16870 return ERROR;
16871 }
16872 if (lnLike != lnLike)
16873 {
16874 printf ("DEBUG ERROR: Log likelihood nan after move '%s'\n", theMove->name);
16875 // printf ("Seed: %ld\n", oldSeed); state[chn] ^= 1; PrintCheckPoint (n);
16876 return ERROR;
16877 }
16878 # if defined (DEBUG_LNLIKELIHOOD) /* slow */
16879 ResetFlips(chn); /* needed to return flags so they point to old state */
16880 TouchEverything(chn);
16881 if (fabs((lnLike-LogLike(chn))/lnLike) > 0.0001)
16882 {
16883 printf ("DEBUG ERROR: Log likelihood incorrect after move '%s'\n", theMove->name);
16884 return ERROR;
16885 }
16886 # endif
16887 if (theMove->parm->paramType == P_TOPOLOGY && GetTree (theMove->parm, chn, state[chn])->isClock == YES &&
16888 IsClockSatisfied (GetTree (theMove->parm, chn, state[chn]),0.001) == NO)
16889 {
16890 printf ("%s Branch lengths of the tree do not satisfy the requirements of a clock tree.\n", spacer);
16891 ShowNodes(GetTree (theMove->parm, chn, state[chn])->root,0,YES);
16892 return (ERROR);
16893 }
16894 # endif
16895
16896 /* heat */
16897 lnLikelihoodRatio *= Temperature (chainId[chn]);
16898 lnPriorRatio *= Temperature (chainId[chn]);
16899
16900 if (chainParams.isSS == YES)
16901 lnLikelihoodRatio *= powerSS;
16902
16903 /* calculate the acceptance probability */
16904 if (lnLikelihoodRatio + lnPriorRatio + lnProposalRatio < -100.0)
16905 r = 0.0;
16906 else if (lnLikelihoodRatio + lnPriorRatio + lnProposalRatio > 0.0)
16907 r = 1.0;
16908 else
16909 r = exp(lnLikelihoodRatio + lnPriorRatio + lnProposalRatio);
16910 }
16911
16912 /* decide to accept or reject the move */
16913 acceptMove = NO;
16914 i = chainId[chn];
16915 theMove->nTried[i]++;
16916 theMove->nTotTried[i]++;
16917 if (abortMove == NO && RandomNumber(seed) < r)
16918 {
16919 acceptMove = YES;
16920 theMove->nAccepted[i]++;
16921 theMove->nTotAccepted[i]++;
16922 }
16923
16924 /* update the chain */
16925 if (acceptMove == NO)
16926 {
16927 /* the new state did not work out so shift chain back */
16928 if (abortMove == NO)
16929 ResetFlips(chn);
16930 state[chn] ^= 1;
16931 # if defined (BEAGLE_ENABLED)
16932 if (recalcScalers == YES)
16933 {
16934 recalculateScalers(chn);
16935 recalcScalers = NO;
16936 }
16937 # endif
16938 }
16939 else
16940 {
16941 /* if the move is accepted then let the chain stay in the new state */
16942 /* store the likelihood and prior of the chain */
16943 curLnL[chn] = lnLike;
16944 curLnPr[chn] = lnPrior;
16945 }
16946
16947 /* check if time to autotune */
16948 if (theMove->nTried[i] >= chainParams.tuneFreq)
16949 {
16950 theMove->lastAcceptanceRate[i] = (MrBFlt) theMove->nAccepted[i] / (MrBFlt) theMove->nTried[i];
16951 theMove->nTried[i] = 0;
16952 theMove->nAccepted[i] = 0;
16953 theMove->nBatches[i]++; /* we only autotune at most 10000 times */
16954 if (chainParams.autotune == YES && theMove->moveType->Autotune != NULL && theMove->nBatches[i] < MAXTUNINGPARAM)
16955 {
16956 theMove->moveType->Autotune(theMove->lastAcceptanceRate[i],
16957 theMove->targetRate[i],
16958 theMove->nBatches[i],
16959 &theMove->tuningParam[i][0],
16960 theMove->moveType->minimum[0],
16961 theMove->moveType->maximum[0]);
16962 }
16963 }
16964
16965 /* ShowValuesForChain (chn); */
16966
16967 if (curLnL[chn] > maxLnL0[chainId[chn]])
16968 maxLnL0[chainId[chn]] = curLnL[chn];
16969
16970 }
16971
16972 /* attempt swap(s) Non-blocking for MPI if no swap with external process. */
16973 if (chainParams.numChains > 1 && n % chainParams.swapFreq == 0)
16974 {
16975 for (i = 0; i<chainParams.numRuns; i++)
16976 {
16977 for (j = 0; j<chainParams.numSwaps; j++) {
16978 GetSwappers (&swapA, &swapB, i);
16979 if (AttemptSwap (swapA, swapB, seed) == ERROR)
16980 {
16981 MrBayesPrint ("%s Unsuccessful swap of states\n", spacer);
16982 # if defined (MPI_ENABLED)
16983 nErrors++;
16984 # else
16985 return ERROR;
16986 # endif
16987 }
16988 }
16989 }
16990 }
16991
16992 /* print information to screen. Non-blocking for MPI */
16993 if (n % chainParams.printFreq == 0)
16994 {
16995 PrintToScreen(n, numPreviousGen, time(0), startingT);
16996 # if defined (TIMING_ANALIZ)
16997 MrBayesPrint ("%s Time elapsed:%f CondlikeDownTime:%f CondLikeRoot:%f Likelihood:%f ScalersTime:%f ScalersRemove:%f\n", spacer,
16998 CPUTime, CPUCondLikeDown/(MrBFlt)CLOCKS_PER_SEC, CPUCondLikeRoot/(MrBFlt)CLOCKS_PER_SEC, CPULilklihood/(MrBFlt)CLOCKS_PER_SEC,
16999 CPUScalers/(MrBFlt)CLOCKS_PER_SEC, CPUScalersRemove/(MrBFlt)CLOCKS_PER_SEC);
17000 # endif
17001 }
17002
17003 /* print information to files */
17004 /* this will also add tree samples to topological convergence diagnostic counters */
17005 if (n == chainParams.numGen || n % chainParams.sampleFreq == 0)
17006 {
17007 # if defined (MPI_ENABLED)
17008 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
17009 if (sumErrors > 0)
17010 {
17011 MrBayesPrint ("%s Aborting run.\n");
17012 return ERROR;
17013 }
17014 # endif
17015 if (PrintStatesToFiles (n) == ERROR)
17016 {
17017 MrBayesPrint("%s Error in printing states to files\n");
17018 # if defined (MPI_ENABLED)
17019 nErrors++;
17020 # else
17021 return ERROR;
17022 # endif
17023 }
17024 # if defined (MPI_ENABLED)
17025 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
17026 if (sumErrors > 0)
17027 {
17028 MrBayesPrint ("%s Aborting run.\n");
17029 return ERROR;
17030 }
17031 # endif
17032 }
17033
17034 /* print mcmc diagnostics. Blocking for MPI */
17035 if (chainParams.mcmcDiagn == YES && (n % chainParams.diagnFreq == 0
17036 || n == chainParams.numGen
17037 || (chainParams.isSS == YES && (n-lastStepEndSS) % numGenInStepSS == 0)))
17038 {
17039 if (chainParams.numRuns > 1 &&
17040 ((n > 0 && chainParams.relativeBurnin == YES && (chainParams.isSS == NO || (n > chainParams.burninSS * chainParams.sampleFreq && (n-lastStepEndSS) > numGenInStepBurninSS)))
17041 || (n >= chainParams.chainBurnIn * chainParams.sampleFreq && chainParams.relativeBurnin == NO)))
17042 {
17043 /* we need some space for coming output */
17044 MrBayesPrint ("\n");
17045 /* remove tree samples if using burninpercentage */
17046 /* the following function returns immediately in MPI if proc_id != 0 */
17047 if (chainParams.relativeBurnin == YES && chainParams.isSS == NO)
17048 {
17049 removeFrom = removeTo;
17050 removeTo = (int)(chainParams.burninFraction * (n/chainParams.sampleFreq+1)); /* (n/chainParams.sampleFreq+1) is the current number of samples */
17051 if (removeFrom < removeTo)
17052 {
17053 if (RemoveTreeSamples (removeFrom+1, removeTo) == ERROR)
17054 {
17055 MrBayesPrint("%s Problem removing tree samples\n");
17056 # if defined (MPI_ENABLED)
17057 nErrors++;
17058 # else
17059 return ERROR;
17060 # endif
17061 }
17062 }
17063 # if defined (MPI_ENABLED)
17064 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
17065 if (sumErrors > 0)
17066 {
17067 MrBayesPrint ("%s Aborting run.\n");
17068 return ERROR;
17069 }
17070 # endif
17071 }
17072
17073 lastDiagnostics = (n/chainParams.sampleFreq)+1; /* +1 because we always have start tree sampled*/
17074 if (chainParams.relativeBurnin == YES)
17075 {
17076 i = lastDiagnostics - removeTo;
17077 }
17078 else
17079 i = lastDiagnostics - chainParams.chainBurnIn;
17080 # if defined (MPI_ENABLED)
17081 if (proc_id == 0)
17082 {
17083 # endif
17084 /* calculate statistics */
17085 CalcTopoConvDiagn (i);
17086 /* output statistics */
17087 if (numTopologies == 1)
17088 {
17089 f = -1.0;
17090 if (chainParams.stat[0].numPartitions == 0)
17091 {
17092 MrBayesPrint ("%s Average standard deviation of split frequencies: NA (no splits above min. frequency)\n", spacer);
17093 }
17094 else if (chainParams.diagnStat == AVGSTDDEV)
17095 {
17096 f = chainParams.stat[0].avgStdDev;
17097 MrBayesPrint ("%s Average standard deviation of split frequencies: %.6f\n", spacer, f);
17098 }
17099 else
17100 {
17101 f = chainParams.stat[0].max;
17102 MrBayesPrint ("%s Max standard deviation of split frequencies: %.6f\n", spacer, f);
17103 }
17104 if (chainParams.isSS == YES)
17105 splitfreqSS[chainParams.numStepsSS-stepIndexSS-1] = f;
17106 if (chainParams.stat[0].numPartitions > 0 && f <= chainParams.stopVal)
17107 stopChain = YES;
17108 if (n < chainParams.numGen - chainParams.printFreq && (chainParams.stopRule == NO || stopChain == NO))
17109 MrBayesPrint ("\n");
17110 }
17111 else
17112 {
17113 stopChain = YES;
17114 for (i=0; i<numTopologies; i++)
17115 {
17116 f=-1.0;
17117 if (chainParams.stat[i].numPartitions == 0)
17118 {
17119 if (strcmp(modelParams[0].topologyPr,"Speciestree") == 0)
17120 {
17121 if (i == numTopologies-1)
17122 MrBayesPrint ("%s Average standard deviation of split frequencies for species tree (tree %d): NA (no splits above min. frequency)\n", spacer, i+1);
17123 else
17124 MrBayesPrint ("%s Average standard deviation of split frequencies for gene tree %d: NA (no splits above min. frequency)\n", spacer, i+1);
17125 }
17126 else
17127 MrBayesPrint ("%s Average standard deviation of split frequencies for topology %d: NA (no splits above min. frequency)\n", spacer, i+1);
17128 }
17129 else if (chainParams.diagnStat == AVGSTDDEV)
17130 {
17131 f = chainParams.stat[i].avgStdDev;
17132 if (strcmp(modelParams[0].topologyPr,"Speciestree") == 0)
17133 {
17134 if (i == numTopologies-1)
17135 MrBayesPrint ("%s Average standard deviation of split frequencies for species tree (tree %d): %.6f\n", spacer, i+1, f);
17136 else
17137 MrBayesPrint ("%s Average standard deviation of split frequencies for gene tree %d: %.6f\n", spacer, i+1, f);
17138 }
17139 else
17140 MrBayesPrint ("%s Average standard deviation of split frequencies for topology %d: %.6f\n", spacer, i+1, f);
17141 }
17142 else
17143 {
17144 f = chainParams.stat[i].max;
17145 if (strcmp(modelParams[0].topologyPr,"Speciestree") == 0)
17146 {
17147 if (i == numTopologies-1)
17148 MrBayesPrint ("%s Max standard deviation of split frequencies for species tree: %.6f\n", spacer, f);
17149 else
17150 MrBayesPrint ("%s Max standard deviation of split frequencies for gene tree %d: %.6f\n", spacer, i+1, f);
17151 }
17152 else
17153 MrBayesPrint ("%s Max standard deviation of split frequencies for topology %d: %.6f\n", spacer, i+1, f);
17154 }
17155 if (chainParams.isSS == YES)
17156 splitfreqSS[i*chainParams.numStepsSS+chainParams.numStepsSS-stepIndexSS-1] = f;
17157 if (chainParams.stat[i].numPartitions == 0 || f > chainParams.stopVal)
17158 stopChain = NO;
17159 }
17160 if (n < chainParams.numGen - chainParams.printFreq && (chainParams.stopRule == NO || stopChain == NO))
17161 MrBayesPrint ("\n");
17162 }
17163 if (chainParams.allComps == YES)
17164 PrintTopConvInfo ();
17165 # if defined (MPI_ENABLED)
17166 }
17167 ierror = MPI_Bcast (&stopChain, 1, MPI_INT, 0, MPI_COMM_WORLD);
17168 if (ierror != MPI_SUCCESS)
17169 {
17170 MrBayesPrint ("%s Problem broadcasting stop value\n", spacer);
17171 nErrors++;
17172 }
17173 # endif
17174 }
17175
17176 /* part of the following function needs to be performed by all MPI processors. Blocking for MPI. */
17177 if (PrintMCMCDiagnosticsToFile (n) == ERROR)
17178 {
17179 MrBayesPrint ("%s Problem printing mcmc diagnostics to file\n", spacer);
17180 # if defined (MPI_ENABLED)
17181 nErrors++;
17182 # else
17183 return (ERROR);
17184 # endif
17185 }
17186 # if defined (MPI_ENABLED)
17187 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
17188 if (sumErrors > 0)
17189 {
17190 MrBayesPrint ("%s Aborting run.\n");
17191 return ERROR;
17192 }
17193 # endif
17194 }
17195
17196 /* check if time to break because stopVal reached */
17197 if (chainParams.isSS == NO && (chainParams.stopRule == YES && stopChain == YES))
17198 {
17199 MrBayesPrint ("\n%s Analysis stopped because convergence diagnostic hit stop value.\n", spacer);
17200 break;
17201 }
17202
17203 /* user may want to extend chain */
17204 if (chainParams.isSS == NO && (n == chainParams.numGen && autoClose == NO))
17205 {
17206 stoppingT1 = time(0);
17207 currentCPUTime = clock();
17208 CPUTime += (currentCPUTime - previousCPUTime) / (MrBFlt) CLOCKS_PER_SEC;
17209 chainParams.numGen += ExtendChainQuery ();
17210 stoppingT2 = time(0);
17211 startingT += (stoppingT2-stoppingT1);
17212 previousCPUTime = clock();
17213 /* timers should not be increased during the wait for a reply */
17214 }
17215
17216 /* Do stepping sampling staf if needed */
17217 if (chainParams.isSS == YES && n >= chainParams.burninSS*chainParams.sampleFreq)
17218 {
17219 # ifndef SAMPLE_ALL_SS
17220 if ((n-lastStepEndSS) % chainParams.sampleFreq == 0)
17221 # endif
17222 {
17223 if (n > chainParams.burninSS*chainParams.sampleFreq && (n-lastStepEndSS > numGenInStepBurninSS))
17224 { /* do sampling */
17225 for (chn=0; chn<numLocalChains; chn++)
17226 {
17227 if (chainId[chn] % chainParams.numChains == 0)
17228 {
17229 run = chainId[chn] / chainParams.numChains;
17230 if (curLnL[chn]*stepLengthSS > stepScalerSS[run] + 200.0)
17231 {
17232 // adjust scaler;
17233 stepAcumulatorSS[run] /= exp (curLnL[chn]*stepLengthSS - 100.0 - stepScalerSS[run]);
17234 stepScalerSS[run]= curLnL[chn]*stepLengthSS - 100.0;
17235 }
17236 stepAcumulatorSS[run] += exp (curLnL[chn]*stepLengthSS - stepScalerSS[run]);
17237 }
17238 }
17239 samplesCountSS++;
17240 }
17241 }
17242
17243 if ((n-lastStepEndSS) == numGenInStepBurninSS)
17244 {
17245 /* Remove all previouse samples from diagnostics */
17246 if (chainParams.mcmcDiagn == YES && chainParams.numRuns > 1)
17247 {
17248 removeFrom = removeTo;
17249 removeTo = (int)(n/chainParams.sampleFreq); /* (n/chainParams.sampleFreq+1) is the current number of samples including 0 one*/
17250 removeTo++;
17251 if (removeFrom < removeTo)
17252 {
17253 if (RemoveTreeSamples (removeFrom+1, removeTo) == ERROR)
17254 {
17255 nErrors++;
17256 }
17257 ERROR_TEST2("Problem removing tree samples",return(ERROR),);
17258 }
17259 }
17260 }
17261
17262 if ((n-lastStepEndSS) % numGenInStepSS == 0) /* prepare sample of next step */
17263 {
17264 assert (n-lastStepEndSS <= numGenInStepSS);
17265 lastStepEndSS=n;
17266
17267 if (n > chainParams.burninSS*chainParams.sampleFreq)
17268 {
17269 /* dump to file current step contribution */
17270 MrBayesPrintf (fpSS, "%3d\t%.4f", chainParams.numStepsSS-stepIndexSS, powerSS);
17271 # if defined (MPI_ENABLED)
17272 for (j=0; j<chainParams.numRuns ; j++)
17273 {
17274 if (stepAcumulatorSS[j]==0)
17275 r=0;
17276 else
17277 r = log (stepAcumulatorSS[j]/samplesCountSS) + stepScalerSS[j];
17278 ierror = MPI_Reduce (&r,&sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
17279 if (ierror != MPI_SUCCESS)
17280 {
17281 MrBayesPrint ("%s Problem with MPI_Reduce\n", spacer);
17282 return ERROR;
17283 }
17284 if (proc_id == 0)
17285 {
17286 MrBayesPrintf (fpSS, "\t%.6f", sum);
17287 }
17288 }
17289 # else
17290 for (j=0; j<chainParams.numRuns ; j++)
17291 {
17292 MrBayesPrintf (fpSS, "\t%.6f", log (stepAcumulatorSS[j]/samplesCountSS) + stepScalerSS[j]);
17293 }
17294 # endif
17295 if (chainParams.mcmcDiagn == YES && chainParams.numRuns > 1)
17296 {
17297 for (i=0; i<numTopologies; i++)
17298 {
17299 MrBayesPrintf (fpSS, "\t%.6f",splitfreqSS[i*chainParams.numStepsSS+chainParams.numStepsSS-stepIndexSS-1]);
17300 }
17301 }
17302 else{
17303 for (i=0; i<numTopologies; i++)
17304 {
17305 MrBayesPrintf (fpSS, "\t-2.0");
17306 }
17307
17308 }
17309
17310 MrBayesPrintf (fpSS, "\n");
17311 fflush (fpSS);
17312
17313 stepIndexSS--;
17314
17315 if (chainParams.startFromPriorSS==YES)
17316 {
17317 powerSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)(chainParams.numStepsSS-1-stepIndexSS)/(MrBFlt)chainParams.numStepsSS);
17318 stepLengthSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)(chainParams.numStepsSS-stepIndexSS)/(MrBFlt)chainParams.numStepsSS)-powerSS;
17319 }
17320 else
17321 {
17322 stepLengthSS = powerSS;
17323 powerSS = BetaQuantile (chainParams.alphaSS, 1.0, (MrBFlt)stepIndexSS/(MrBFlt)chainParams.numStepsSS);
17324 stepLengthSS -= powerSS;
17325 }
17326 if (n != chainParams.numGen)
17327 MrBayesPrint("%s Sampling step %d out of %d steps...\n\n",spacer, chainParams.numStepsSS-stepIndexSS, chainParams.numStepsSS);
17328 for (chn=0; chn<numLocalChains; chn++)
17329 {
17330 if (chainId[chn] % chainParams.numChains == 0)
17331 {
17332 run = chainId[chn] / chainParams.numChains;
17333 if (samplesCountSS != 0)
17334 marginalLnLSS[run] += log (stepAcumulatorSS[run]/samplesCountSS) + stepScalerSS[run];
17335 stepScalerSS[run] = curLnL[chn]*stepLengthSS;
17336 stepAcumulatorSS[run]=0.0;
17337 }
17338 }
17339 samplesCountSS=0;
17340 }
17341 else
17342 {
17343 MrBayesPrint("\n%s Sampling step 1 out of %d steps...\n\n",spacer, chainParams.numStepsSS);
17344 }
17345
17346 if (chainParams.backupCheckSS !=0 && (chainParams.numStepsSS-stepIndexSS-1)% chainParams.backupCheckSS == 0)
17347 {
17348 /* print check-point file. Blocking for MPI */
17349 ERROR_TEST2("Error before printing checkpoint",return(ERROR),);
17350 if (PrintCheckPoint (n) == ERROR)
17351 {
17352 nErrors++;
17353 }
17354 ERROR_TEST2("Error in printing checkpoint",return(ERROR),);
17355
17356 # if defined (MPI_ENABLED)
17357 if (proc_id == 0)
17358 {
17359 # endif
17360 /* figure out check-point file names */
17361 sprintf (ckpFileName, "%s%s.ckp", workingDir, chainParams.chainFileName);
17362 sprintf (bkupFileName,"%s.ss%d", ckpFileName,chainParams.numStepsSS-stepIndexSS);
17363 if (rename (ckpFileName, bkupFileName)!=0)
17364 {
17365 MrBayesPrint ("%s Could not rename file %s to %s\n", spacer, ckpFileName, bkupFileName);
17366 return ERROR;
17367 }
17368 strcpy (bkupFileName, ckpFileName);
17369 strcat (bkupFileName, "~");
17370 rename (bkupFileName,ckpFileName);
17371 # if defined (MPI_ENABLED)
17372 } /* end of if (proc_id == 0)*/
17373 # endif
17374 }
17375 }
17376 }
17377
17378 /* print check-point file. Blocking for MPI */
17379 if (chainParams.checkPoint == YES && (n % chainParams.checkFreq == 0))
17380 {
17381 ERROR_TEST2("Error before printing checkpoint",return(ERROR),);
17382 if (PrintCheckPoint (n) == ERROR)
17383 {
17384 nErrors++;
17385 }
17386 ERROR_TEST2("Error in printing checkpoint",return(ERROR),);
17387 }
17388
17389 } /* end run chain */
17390 endingT = time(0);
17391 # if defined (DEBUG_TIME)
17392 clock_gettime(CLOCK_MONOTONIC, &tw2);
17393 # endif
17394 currentCPUTime = clock();
17395 CPUTime += (currentCPUTime - previousCPUTime) / (MrBFlt) CLOCKS_PER_SEC;
17396
17397 CloseMBPrintFiles (); /* redundant because files closed in FreeChainMemory but kept here as a safeguard in case of future changes */
17398
17399 # if defined (BEAGLE_ENABLED)
17400 # ifdef DEBUG_BEAGLE
17401 beagleScalingSchemeOld = beagleScalingScheme;
17402 beagleScalingScheme = MB_BEAGLE_SCALE_ALWAYS;
17403 for (chn=0; chn<numLocalChains; chn++)
17404 {
17405 if (chn % chainParams.numChains == 0)
17406 {
17407 if (chainParams.numRuns == 1)
17408 MrBayesPrint ("%s Final log likelihoods and log prior probs (stored and calculated):\n", spacer);
17409 else
17410 MrBayesPrint ("%s Final log likelihoods and log prior probs for run %d (stored and calculated):\n", spacer, chn / chainParams.numChains + 1);
17411 }
17412 TouchEverything (chn);
17413 for (i=0; i<numCurrentDivisions; i++)
17414 {
17415 if (modelSettings[i].gibbsGamma == YES)
17416 curLnL[chn] += GibbsSampleGamma (chn, i, seed);
17417 }
17418 MrBayesPrint ("%s Chain %d -- %.6lf -- %.6lf\n", spacer, (chn % chainParams.numChains) + 1, curLnL[chn], curLnPr[chn]);
17419 MrBayesPrint ("%s Chain %d -- %.6lf -- %.6lf\n", spacer, (chn % chainParams.numChains) + 1, LogLike(chn), LogPrior(chn));
17420 }
17421 beagleScalingScheme = beagleScalingSchemeOld;
17422 # endif
17423 # endif
17424
17425 /* Make sure current state is reset and values copied back to state 0.
17426 Note that this can be tricky for Metropolis-coupled chains because
17427 the chain ids may necessitate some swapping of values among chains. */
17428 ResetChainIds ();
17429
17430 MrBayesPrint ("\n");
17431 if (difftime (endingT, startingT) > 3600.0)
17432 MrBayesPrint ("%s Analysis completed in %d hours %d mins %d seconds\n", spacer,
17433 (int) (difftime(endingT, startingT)/3600),
17434 ((int) (difftime(endingT, startingT))%3600)/60,
17435 (int) (difftime(endingT, startingT))%60);
17436 else if (difftime (endingT, startingT) > 60.0)
17437 MrBayesPrint ("%s Analysis completed in %d mins %d seconds\n", spacer,
17438 (int) (difftime(endingT, startingT)/60),
17439 (int) (difftime(endingT, startingT))%60);
17440 else if (difftime (endingT, startingT) > 2.0)
17441 MrBayesPrint ("%s Analysis completed in %.0f seconds\n", spacer,
17442 difftime(endingT, startingT));
17443 else if (difftime (endingT, startingT) >= 1.0)
17444 MrBayesPrint ("%s Analysis completed in 1 second\n", spacer);
17445 else
17446 MrBayesPrint ("%s Analysis completed in less than 1 second\n", spacer);
17447
17448 # if defined (DEBUG_TIME)
17449 MrBayesPrint ("%s Analysis used %1.2f seconds of total time\n", spacer, (MrBFlt) (1000.0*tw2.tv_sec + 1e-6*tw2.tv_nsec - (1000.0*tw1.tv_sec + 1e-6*tw1.tv_nsec))/1000);
17450 # endif
17451
17452 # if defined (MPI_ENABLED)
17453 MrBayesPrint ("%s Analysis used %1.2f seconds of CPU time on processor 0\n", spacer, (MrBFlt) CPUTime);
17454 # else
17455 MrBayesPrint ("%s Analysis used %1.2f seconds of CPU time\n", spacer, (MrBFlt) CPUTime);
17456 # endif
17457
17458 # if defined (MPI_ENABLED)
17459 /* find the best likelihoods across all of the processors */
17460 ierror = MPI_Barrier (MPI_COMM_WORLD);
17461 if (ierror != MPI_SUCCESS)
17462 {
17463 MrBayesPrint ("%s Problem at chain barrier\n", spacer);
17464 return (ERROR);
17465 }
17466 for (j=0; j<numGlobalChains; j++)
17467 {
17468 best = maxLnL0[j];
17469 for (i=1; i<num_procs; i++)
17470 {
17471 if (proc_id == 0)
17472 {
17473 ierror = MPI_Recv (&maxLnL0[j], 1, MPI_DOUBLE, i, i, MPI_COMM_WORLD, &status);
17474 if (ierror != MPI_SUCCESS)
17475 {
17476 MrBayesPrint ("%s Problem with MPI_Recv", spacer);
17477 return ERROR;
17478 }
17479 if (maxLnL0[j] > best)
17480 best = maxLnL0[j];
17481 }
17482 else if (proc_id == i)
17483 {
17484 ierror = MPI_Send (&maxLnL0[j], 1, MPI_DOUBLE, 0, i, MPI_COMM_WORLD);
17485 if (ierror != MPI_SUCCESS)
17486 {
17487 MrBayesPrint ("%s Problem with MPI_Send\n", spacer);
17488 return ERROR;
17489 }
17490 }
17491 }
17492 maxLnL0[j] = best;
17493 }
17494
17495 /* Collecting marginal log likelihoods if SS is used */
17496 if (chainParams.isSS == YES)
17497 {
17498 for (j=0; j<chainParams.numRuns ; j++)
17499 {
17500 ierror = MPI_Reduce (&marginalLnLSS[j],&r, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
17501 if (ierror != MPI_SUCCESS)
17502 {
17503 MrBayesPrint ("%s Problem with MPI_Send\n", spacer);
17504 return ERROR;
17505 }
17506 if (proc_id == 0)
17507 {
17508 marginalLnLSS[j]=r;
17509 }
17510 }
17511 }
17512 # endif
17513
17514 if (chainParams.numRuns == 1)
17515 {
17516 if (chainParams.numChains == 1)
17517 MrBayesPrint ("%s Log likelihood of best state was %1.2lf\n", spacer, maxLnL0[0]);
17518 else
17519 MrBayesPrint ("%s Log likelihood of best state for \"cold\" chain was %1.2lf\n", spacer, maxLnL0[0]);
17520 }
17521 else
17522 {
17523 for (j=0; j<chainParams.numRuns*chainParams.numChains; j++)
17524 {
17525 if (j % chainParams.numChains == 0)
17526 {
17527 if (chainParams.numChains == 1)
17528 MrBayesPrint ("%s Likelihood of best state for run %d was %1.2lf\n", spacer, j/chainParams.numChains+1, maxLnL0[j/chainParams.numChains]);
17529 else
17530 MrBayesPrint ("%s Likelihood of best state for \"cold\" chain of run %d was %1.2lf\n", spacer, j/chainParams.numChains+1, maxLnL0[j/chainParams.numChains]);
17531 }
17532 }
17533 }
17534
17535 if (chainParams.isSS == YES)
17536 {
17537 MrBayesPrint ("\n");
17538 MrBayesPrint ("%s Marginal likelihood (in natural log units) estimated using stepping-stone sampling based on\n", spacer);
17539 MrBayesPrint ("%s %d steps with %d generations (%d samples) within each step. \n\n", spacer, chainParams.numStepsSS, numGenInStepSS, numGenInStepSS/chainParams.sampleFreq);
17540 MrBayesPrint ("%s Run Marginal likelihood (ln)\n",spacer);
17541 MrBayesPrint ("%s ------------------------------\n",spacer);
17542 for (j=0; j<chainParams.numRuns; j++)
17543 {
17544 MrBayesPrint ("%s %3d %9.2f \n", spacer, j+1, marginalLnLSS[j]);
17545 }
17546 MrBayesPrint ("%s ------------------------------\n",spacer);
17547 if (chainParams.numRuns > 1)
17548 {
17549 MeanVarianceLog(marginalLnLSS,chainParams.numRuns,&meanSS,&varSS,NULL);
17550 MrBayesPrint ("%s Mean: %9.2f\n\n",spacer,meanSS);
17551 //MrBayesPrint ("%s Mean: %9.2lf Scaled variance: %.2f of Marginal log likelihood estimates among runs.\n",spacer,meanSS,varSS-2*meanSS);
17552 //MrBayesPrint ("%s Note: Scaled variance is given in log units and calculated as \"variance/mean^2\"\n",spacer);
17553 }
17554 MrBayesPrint ("%s More statistics on stepping-stone sampling is dumped to %s.ss file.\n", spacer, chainParams.chainFileName);
17555
17556 if (chainParams.mcmcDiagn == YES)
17557 {
17558 if ((tempX = (MrBFlt *) SafeCalloc (chainParams.numStepsSS, sizeof(MrBFlt))) == NULL)
17559 {
17560 nErrors++;
17561 }
17562 ERROR_TEST2("Problem allocating memory", return(ERROR),);
17563
17564 for (i=0; i<chainParams.numStepsSS; i++)
17565 {
17566 tempX[i]=i+1;
17567 }
17568 MrBayesPrint ("\n");
17569
17570 if (numTopologies > 1)
17571 {
17572 if (chainParams.diagnStat == AVGSTDDEV)
17573 MrBayesPrint (" Plots of average standard deviation of split frequencies across steps for different topology.");
17574 else
17575 MrBayesPrint (" Plots of max standard deviation of split frequencies across steps for different topology.");
17576 }
17577 else
17578 {
17579 if (chainParams.diagnStat == AVGSTDDEV)
17580 MrBayesPrint (" Plot of average standard deviation of split frequencies across steps.");
17581 else
17582 MrBayesPrint (" Plot of max standard deviation of split frequencies across steps.");
17583 }
17584 MrBayesPrint ("\n");
17585 MrBayesPrint (" Points at -1.0 (y-axis) indicate that there were no splits\n");
17586 MrBayesPrint (" above minimum frequency for corresponding step.");
17587 if (numTopologies > 1)
17588 {
17589 for (i=0; i<numTopologies; i++)
17590 {
17591 MrBayesPrint ("%s Topology %d.\n", spacer, i+1);
17592 PrintPlot (tempX, splitfreqSS+i*chainParams.numStepsSS, chainParams.numStepsSS);
17593 }
17594 }
17595 else
17596 PrintPlot (tempX, splitfreqSS, chainParams.numStepsSS);
17597
17598 free(tempX);
17599 }
17600 }
17601
17602 # if defined (MPI_ENABLED)
17603 /* we need to collect the information on the number of accepted moves if
17604 this is a parallel version */
17605 if (ReassembleMoveInfo() == ERROR)
17606 nErrors++;
17607 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
17608 if (sumErrors > 0)
17609 {
17610 MrBayesPrint ("%s ReassembleMoveInfo failed\n", spacer);
17611 return ERROR;
17612 }
17613 # endif
17614
17615 /* print acceptance rates for the moves */
17616 for (j=0; j<chainParams.numChains*chainParams.numRuns; j++)
17617 {
17618 if (chainParams.numChains == 1)
17619 {
17620 if (chainParams.numRuns == 1)
17621 MrBayesPrint ("%s Acceptance rates for the moves:\n", spacer);
17622 else
17623 MrBayesPrint ("%s Acceptance rates for the moves in run %d:\n", spacer, j/chainParams.numChains+1);
17624 }
17625 else if (j % chainParams.numChains == 0)
17626 {
17627 if (chainParams.numRuns == 1)
17628 MrBayesPrint ("\n%s Acceptance rates for the moves in the \"cold\" chain:\n", spacer);
17629 else
17630 MrBayesPrint ("\n%s Acceptance rates for the moves in the \"cold\" chain of run %d:\n", spacer, j/chainParams.numChains+1);
17631 }
17632 else if (chainParams.allChains == YES)
17633 {
17634 if (chainParams.numRuns == 1)
17635 MrBayesPrint ("\n%s Acceptance rates for the moves in chain %d (heated):\n\n", spacer, j+1);
17636 else
17637 MrBayesPrint ("\n%s Acceptance rates for the moves in chain %d of run %d (heated):\n\n", spacer, j%chainParams.numChains+1, j/chainParams.numChains+1);
17638 }
17639
17640 if (j % chainParams.numChains == 0 || chainParams.allChains == YES)
17641 {
17642 MrBayesPrint ("%s With prob. (last %d) chain accepted proposals by move\n", spacer, chainParams.tuneFreq);
17643
17644 for (i=0; i<numUsedMoves; i++)
17645 {
17646 mv = usedMoves[i];
17647 if (mv->nBatches[j] < 1)
17648 MrBayesPrint ("%s NA NA %s\n",
17649 spacer, mv->name);
17650 else
17651 MrBayesPrint ("%s %6.1f %% (%3.0f %%) %s\n", spacer,
17652 100.0*mv->nTotAccepted[j]/(MrBFlt)(mv->nTotTried[j]),
17653 100.0*mv->lastAcceptanceRate[j],
17654 mv->name);
17655 }
17656 }
17657 }
17658
17659 # if defined MPI_ENABLED
17660 /* Redistribute move info in case it is needed in a follow-up run */
17661 RedistributeMoveInfo();
17662 # endif
17663
17664 /* output information on the success of the chain state swap proposals */
17665 if (PrintSwapInfo () == ERROR)
17666 nErrors++;
17667 # if defined (MPI_ENABLED)
17668 MPI_Allreduce (&nErrors, &sumErrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
17669 if (sumErrors > 0)
17670 {
17671 MrBayesPrint ("%s PrintSwapInfo failed\n", spacer);
17672 return ERROR;
17673 }
17674 # else
17675 if (nErrors > 1)
17676 {
17677 MrBayesPrint ("%s PrintSwapInfo failed\n", spacer);
17678 return ERROR;
17679 }
17680 # endif
17681
17682 # if defined (MPI_ENABLED)
17683 if (proc_id != 0)
17684 return (NO_ERROR);
17685 # endif
17686
17687 if (chainParams.isSS == NO && (chainParams.numRuns > 1 && chainParams.mcmcDiagn == YES))
17688 {
17689 f = 0.0;
17690 for (i=0; i<numTrees; i++)
17691 {
17692 if (chainParams.stat[i].avgStdDev > f)
17693 f = chainParams.stat[i].avgStdDev;
17694 }
17695 if (f > 0.10)
17696 {
17697 MrBayesPrint ("\n");
17698 MrBayesPrint ("%s ************************* WARNING!! ************************************ \n", spacer);
17699 MrBayesPrint ("%s MrBayes suspects that your runs have not converged because the tree \n", spacer);
17700 MrBayesPrint ("%s samples are very different (average standard deviation of split frequen- \n", spacer);
17701 MrBayesPrint ("%s cies larger than 0.10 (%1.2lf)). MrBayes suggests that you run the ana- \n", spacer, f);
17702 MrBayesPrint ("%s lysis longer or try to improve the MCMC sampling efficiency by fine- \n", spacer);
17703 MrBayesPrint ("%s tuning MCMC proposal or heating parameters. \n", spacer);
17704 }
17705 }
17706
17707 return (NO_ERROR);
17708 }
17709
17710
SafeSprintf(char ** target,int * targetLen,char * fmt,...)17711 int SafeSprintf (char **target, int *targetLen, char *fmt, ...)
17712 {
17713 va_list argp;
17714 int retval;
17715
17716 while (1) {
17717 /* try to print in the available space */
17718 va_start(argp, fmt);
17719 # ifdef VISUAL
17720 retval = _vsnprintf (*target, *targetLen, fmt, argp);
17721 # else
17722 /* With SGI IRIX this function returns 0. Allen Smith suggested using
17723 _xpg5_vnsprintf, but this function needs a recent version of IRIX.
17724 For now, I just allocate a large buffer for SGI, so there is no need for
17725 memory reallocation on this kind of machines */
17726 /* It appears to me that the reason for the bug is that the old version of
17727 this function relies on nonstandard return values for vsnprintf. With
17728 the current version of the code, SGI should do fine without special
17729 treatment. FR */
17730 retval = vsnprintf (*target, *targetLen, fmt, argp);
17731 # endif
17732 va_end(argp);
17733
17734 /* if it worked, retval will be in interval [0,*targetLen-1], else -1 or true length */
17735 if (retval > -1 && retval < *targetLen)
17736 return NO_ERROR;
17737
17738 /* readjust length */
17739 if (retval > -1) /* some C compilers will return true length in retval */
17740 *targetLen = retval + 1; /* exactly what is needed */
17741 else /* some C compilers will return -1 on buffer overwrite */
17742 *targetLen += TARGETLENDELTA;
17743
17744 /* reallocate target */
17745 *target = SafeRealloc ((void *)*target, *targetLen);
17746 if (*target == NULL)
17747 return ERROR;
17748 }
17749 }
17750
17751
SetChainIds(void)17752 void SetChainIds (void)
17753 {
17754 /* Fill <chainId[]> with the global chain number.
17755 Ex. For proc_0, chain[0] = 0;
17756 chain[1] = 1;
17757 chain[2] = 2; (numchains = 3)
17758 For proc_1, chain[0] = 3;
17759 chain[1] = 4; (numchains = 2)
17760 etc...
17761 */
17762
17763 # if defined (MPI_ENABLED)
17764
17765 int i, proc, numChainsForProc, numGlobalChains;
17766 int id;
17767 int remainder;
17768
17769 /* calculate global number of chains */
17770 numGlobalChains = chainParams.numChains * chainParams.numRuns;
17771
17772 /* there are <remainder> chains left over after
17773 load balancing the chains */
17774 remainder = numGlobalChains % num_procs;
17775
17776 /* get the number of chains handled by this proc */
17777 numChainsForProc = (int) (numGlobalChains / num_procs);
17778
17779 /* we must distribute the remaining chains (causing
17780 the chain load between procs to become unbalanced) */
17781 if (proc_id < remainder)
17782 numChainsForProc++;
17783
17784 /* NOTE: at this point, procs can have different number of numChainsForProc
17785 (one more for chains < remainder, one less for procs larger than or equal
17786 to the remainder) */
17787
17788 id = 0;
17789 for (proc=0; proc<num_procs; proc++)
17790 {
17791 /* assign or increment chain id */
17792 if (proc == proc_id)
17793 {
17794 for (i=0; i<numChainsForProc; i++)
17795 chainId[i] = id++;
17796 }
17797 else
17798 {
17799 /* procs below the remainder have 1 more chain
17800 than procs above */
17801 if (proc < remainder)
17802 {
17803 for (i=0; i<(numGlobalChains / num_procs) + 1; i++)
17804 id++;
17805 }
17806 /* procs above the remainder have one less chain
17807 than procs below */
17808 else
17809 {
17810 for (i=0; i<(numGlobalChains / num_procs); i++)
17811 id++;
17812 }
17813 }
17814 }
17815 # else
17816
17817 int chn;
17818
17819 for (chn=0; chn<numLocalChains; chn++)
17820 chainId[chn] = chn;
17821
17822 # endif
17823
17824 }
17825
17826
17827 /* It sets chainParams.tFilePos[] to point immidiatly after sampled tree in position "samplePos" for all .t files. */
SetFilePositions(int samplePos)17828 int SetFilePositions (int samplePos)
17829 {
17830 int i, j, k, longestLine;
17831 BitsLong lastBlock;
17832 char *lineBuf;
17833 FILE *fp;
17834 char *tempStr;
17835 int tempStrSize = TEMPSTRSIZE;
17836
17837 # if defined (MPI_ENABLED)
17838 if (proc_id != 0)
17839 return (NO_ERROR);
17840 # endif
17841
17842 tempStr = (char *) SafeMalloc((size_t)tempStrSize * sizeof(char));
17843 if (!tempStr)
17844 {
17845 MrBayesPrint ("%s Problem allocating tempString (%d)\n", spacer, tempStrSize * sizeof(char));
17846 return (ERROR);
17847 }
17848
17849 for (i=0; i<numTopologies; i++)
17850 {
17851 for (j=0; j<chainParams.numRuns; j++)
17852 {
17853 if (numPrintTreeParams == 1)
17854 {
17855 if (chainParams.numRuns == 1)
17856 SafeSprintf (&tempStr, &tempStrSize, "%s.t", chainParams.chainFileName);
17857 else
17858 SafeSprintf (&tempStr, &tempStrSize, "%s.run%d.t", chainParams.chainFileName, j+1);
17859 }
17860 else
17861 {
17862 if (chainParams.numRuns == 1)
17863 SafeSprintf (&tempStr, &tempStrSize, "%s.tree%d.t", chainParams.chainFileName, i+1);
17864 else
17865 SafeSprintf (&tempStr, &tempStrSize, "%s.tree%d.run%d.t", chainParams.chainFileName, i+1, j+1);
17866 }
17867
17868 if ((fp = OpenBinaryFileR (tempStr)) == NULL)
17869 {
17870 MrBayesPrint ("%s Problem opening file %s.\n", spacer, tempStr);
17871 free (tempStr);
17872 return (ERROR);
17873 }
17874
17875 longestLine = LongestLine (fp);
17876 SafeFclose (&fp);
17877
17878 if ((fp = OpenTextFileR (tempStr)) == NULL)
17879 {
17880 free (tempStr);
17881 return (ERROR);
17882 }
17883
17884 lineBuf = (char *) SafeCalloc (longestLine + 10, sizeof (char));
17885 if (!lineBuf)
17886 {
17887 SafeFclose (&fp);
17888 free (tempStr);
17889 return (ERROR);
17890 }
17891
17892 lastBlock = LastBlock (fp, lineBuf, longestLine);
17893 fseek (fp, lastBlock, SEEK_SET);
17894 fseek (fp, FirstTree(fp, lineBuf, longestLine), SEEK_SET);
17895
17896 for (k=0; k<samplePos; k++)
17897 {
17898 if (fgets (lineBuf, longestLine + 5, fp) == NULL)
17899 {
17900 MrBayesPrint ("%s Not enough records in file %s.\n", spacer, tempStr);
17901 SafeFclose (&fp);
17902 free (tempStr);
17903 free (lineBuf);
17904 return ERROR;
17905 }
17906 }
17907 fgetpos (fp, &chainParams.tFilePos[j*numTopologies+i]);
17908
17909 SafeFclose (&fp);
17910 free (lineBuf);
17911 } /* next run */
17912 } /* next tree */
17913 free (tempStr);
17914 return (NO_ERROR);
17915 }
17916
17917
17918 /* SetFileNames: Set file names */
SetFileNames(void)17919 void SetFileNames (void)
17920 {
17921 strcpy (sumtParams.sumtFileName, chainParams.chainFileName);
17922 strcpy (sumtParams.sumtOutfile, chainParams.chainFileName);
17923 strcpy (sumpParams.sumpFileName, chainParams.chainFileName);
17924 strcpy (sumpParams.sumpOutfile, chainParams.chainFileName);
17925 if (chainParams.numRuns == 1)
17926 {
17927 sprintf (comptreeParams.comptFileName1, "%s.t", chainParams.chainFileName);
17928 sprintf (comptreeParams.comptFileName2, "%s.t", chainParams.chainFileName);
17929 sprintf (plotParams.plotFileName, "%s.p", chainParams.chainFileName);
17930 MrBayesPrint ("%s Setting chain output file names to \"%s.<p/t>\"\n", spacer, chainParams.chainFileName);
17931 }
17932 else /* if (chainParams.numRuns > 1) */
17933 {
17934 sprintf (comptreeParams.comptFileName1, "%s.run1.t", chainParams.chainFileName);
17935 sprintf (comptreeParams.comptFileName2, "%s.run2.t", chainParams.chainFileName);
17936 sprintf (plotParams.plotFileName, "%s.run1.p", chainParams.chainFileName);
17937 MrBayesPrint ("%s Setting chain output file names to \"%s.run<i>.<p/t>\"\n", spacer, chainParams.chainFileName);
17938 }
17939 }
17940
17941
17942 /*----------------------------------------------------------------------------
17943 |
17944 | SetLikeFunctions: This function will set up the pointers from each
17945 | data division to the relevant likelihood functions. It will also
17946 | deal with the settings for SIMD code.
17947 |
17948 -----------------------------------------------------------------------------*/
SetLikeFunctions(void)17949 int SetLikeFunctions (void)
17950 {
17951 int i;
17952
17953 ModelInfo *m;
17954
17955 /* couple divisions with likelihood functions */
17956 for (i=0; i<numCurrentDivisions; i++)
17957 {
17958 m = &modelSettings[i];
17959 m->useVec = VEC_NONE;
17960
17961 if (m->dataType == DNA || m->dataType == RNA)
17962 {
17963 if (m->parsModelId == YES)
17964 {
17965 m->Likelihood = &Likelihood_Pars;
17966 }
17967 else
17968 {
17969 if (m->nucModelId == NUCMODEL_4BY4)
17970 {
17971 if (m->numModelStates > 4)
17972 {
17973 /* covariotide model */
17974 /* TODO: allow autocorrelated rates */
17975 if (m->gibbsGamma == YES)
17976 {
17977 m->CondLikeDown = &CondLikeDown_Gen_GibbsGamma;
17978 m->CondLikeRoot = &CondLikeRoot_Gen_GibbsGamma;
17979 m->CondLikeScaler = &CondLikeScaler_Gen_GibbsGamma;
17980 m->Likelihood = &Likelihood_Gen_GibbsGamma;
17981 }
17982 else
17983 {
17984 m->CondLikeDown = &CondLikeDown_Gen;
17985 m->CondLikeRoot = &CondLikeRoot_Gen;
17986 m->CondLikeScaler = &CondLikeScaler_Gen;
17987 m->Likelihood = &Likelihood_Gen;
17988 }
17989 if (m->correlation != NULL)
17990 m->Likelihood = &Likelihood_Adgamma;
17991 else
17992 m->Likelihood = &Likelihood_Gen;
17993 if (m->nCijkParts == 1)
17994 m->TiProbs = &TiProbs_Gen;
17995 else if (m->nCijkParts > 1)
17996 m->TiProbs = &TiProbs_GenCov;
17997 m->PrintAncStates = &PrintAncStates_NUC4;
17998 m->PrintSiteRates = &PrintSiteRates_Gen;
17999 }
18000 else
18001 {
18002 # if defined (SSE_ENABLED)
18003 if (m->printAncStates == YES || m->printSiteRates == YES)
18004 {
18005 MrBayesPrint ("%s Non-SSE version of conditional likelihood calculator will be used for division %d\n", spacer, i+1);
18006 MrBayesPrint ("%s due to request of reporting 'ancestral states' or 'site rates'.\n", spacer);
18007 }
18008
18009 m->CondLikeUp = &CondLikeUp_NUC4;
18010 m->PrintAncStates = &PrintAncStates_NUC4;
18011 m->PrintSiteRates = &PrintSiteRates_Gen;
18012
18013 if (m->gibbsGamma == YES)
18014 {
18015 m->CondLikeDown = &CondLikeDown_NUC4_GibbsGamma;
18016 m->CondLikeRoot = &CondLikeRoot_NUC4_GibbsGamma;
18017 m->CondLikeScaler = &CondLikeScaler_NUC4_GibbsGamma;
18018 }
18019 else if (m->correlation != NULL || m->printAncStates == YES || m->printSiteRates == YES)
18020 {
18021 m->CondLikeDown = &CondLikeDown_NUC4;
18022 m->CondLikeRoot = &CondLikeRoot_NUC4;
18023 m->CondLikeScaler = &CondLikeScaler_NUC4;
18024 }
18025 else
18026 {
18027 m->useVec = VEC_SSE;
18028 m->numFloatsPerVec = 4;
18029 m->CondLikeDown = &CondLikeDown_NUC4_SSE;
18030 m->CondLikeRoot = &CondLikeRoot_NUC4_SSE;
18031 m->CondLikeScaler = &CondLikeScaler_NUC4_SSE;
18032 #if defined (AVX_ENABLED) // override SSE settings
18033 m->useVec = VEC_AVX;
18034 m->numFloatsPerVec = 8;
18035 m->CondLikeDown = &CondLikeDown_NUC4_AVX;
18036 m->CondLikeRoot = &CondLikeRoot_NUC4_AVX;
18037 m->CondLikeScaler = &CondLikeScaler_NUC4_AVX;
18038 #if defined (FMA_ENABLED) // override AVX settings (CondLikeScaler cannot be improved over AVX)
18039 m->useVec = VEC_FMA;
18040 m->CondLikeDown = &CondLikeDown_NUC4_FMA;
18041 m->CondLikeRoot = &CondLikeRoot_NUC4_FMA;
18042 #endif
18043 #endif
18044 /* Should be sse versions if we want to handle m->printAncStates == YES || inferSiteRates == YES.
18045 For now just set to NULL for early error detection if functions anyway got called by mistake */
18046 m->CondLikeUp = NULL;
18047 m->PrintAncStates = NULL;
18048 m->PrintSiteRates = NULL;
18049 }
18050
18051 if (m->correlation != NULL)
18052 m->Likelihood = &Likelihood_Adgamma;
18053 else if (m->gibbsGamma == YES)
18054 m->Likelihood = &Likelihood_NUC4_GibbsGamma;
18055 else if (m->printAncStates == YES || inferSiteRates == YES)
18056 m->Likelihood = &Likelihood_NUC4;
18057 else
18058 {
18059 m->Likelihood = &Likelihood_NUC4_SSE;
18060 #if defined (AVX_ENABLED)
18061 m->Likelihood = &Likelihood_NUC4_AVX;
18062 #if defined (FMA_ENABLED)
18063 m->Likelihood = &Likelihood_NUC4_FMA;
18064 #endif
18065 #endif
18066 }
18067 # else
18068 if (m->gibbsGamma == YES)
18069 {
18070 m->CondLikeDown = &CondLikeDown_NUC4_GibbsGamma;
18071 m->CondLikeRoot = &CondLikeRoot_NUC4_GibbsGamma;
18072 m->CondLikeScaler = &CondLikeScaler_NUC4_GibbsGamma;
18073 }
18074 else
18075 {
18076 m->CondLikeDown = &CondLikeDown_NUC4;
18077 m->CondLikeRoot = &CondLikeRoot_NUC4;
18078 m->CondLikeScaler = &CondLikeScaler_NUC4;
18079 }
18080
18081 if (m->correlation != NULL)
18082 m->Likelihood = &Likelihood_Adgamma;
18083 else if (m->gibbsGamma == YES)
18084 m->Likelihood = &Likelihood_NUC4_GibbsGamma;
18085 else
18086 m->Likelihood = &Likelihood_NUC4;
18087
18088 m->CondLikeUp = &CondLikeUp_NUC4;
18089 m->PrintAncStates = &PrintAncStates_NUC4;
18090 m->PrintSiteRates = &PrintSiteRates_Gen;
18091 # endif
18092 if (m->nst == 1)
18093 m->TiProbs = &TiProbs_Fels;
18094 else if (m->nst == 2)
18095 m->TiProbs = &TiProbs_Hky;
18096 else
18097 m->TiProbs = &TiProbs_Gen;
18098 m->StateCode = &StateCode_NUC4;
18099 }
18100 }
18101 else if (m->nucModelId == NUCMODEL_DOUBLET)
18102 {
18103 if (m->gibbsGamma == YES)
18104 {
18105 m->CondLikeDown = &CondLikeDown_Gen_GibbsGamma;
18106 m->CondLikeRoot = &CondLikeRoot_Gen_GibbsGamma;
18107 m->CondLikeScaler = &CondLikeScaler_Gen_GibbsGamma;
18108 m->Likelihood = &Likelihood_Gen_GibbsGamma;
18109 }
18110 else
18111 {
18112 m->CondLikeDown = &CondLikeDown_Gen;
18113 m->CondLikeRoot = &CondLikeRoot_Gen;
18114 m->CondLikeScaler = &CondLikeScaler_Gen;
18115 m->Likelihood = &Likelihood_Gen;
18116 }
18117 if (m->nst == 1)
18118 m->TiProbs = &TiProbs_Gen;
18119 else if (m->nst == 2)
18120 m->TiProbs = &TiProbs_Gen;
18121 else
18122 m->TiProbs = &TiProbs_Gen;
18123 m->CondLikeUp = &CondLikeUp_Gen;
18124 m->PrintAncStates = &PrintAncStates_Gen;
18125 m->PrintSiteRates = &PrintSiteRates_Gen;
18126 }
18127 else if (m->nucModelId == NUCMODEL_CODON)
18128 {
18129 /* codon models */
18130 if (m->numOmegaCats == 1)
18131 {
18132 if (m->gibbsGamma == YES)
18133 {
18134 m->CondLikeDown = &CondLikeDown_Gen_GibbsGamma;
18135 m->CondLikeRoot = &CondLikeRoot_Gen_GibbsGamma;
18136 m->CondLikeScaler = &CondLikeScaler_Gen_GibbsGamma;
18137 m->Likelihood = &Likelihood_Gen_GibbsGamma;
18138 }
18139 else
18140 {
18141 m->CondLikeDown = &CondLikeDown_Gen;
18142 m->CondLikeRoot = &CondLikeRoot_Gen;
18143 m->CondLikeScaler = &CondLikeScaler_Gen;
18144 m->Likelihood = &Likelihood_Gen;
18145 # if defined (SSE_ENABLED)
18146 if (m->printAncStates == YES || m->printSiteRates == YES)
18147 {
18148 MrBayesPrint ("%s Non-SSE version of conditional likelihood calculator will be used for division %d\n", spacer, i+1);
18149 MrBayesPrint ("%s due to request of reporting 'ancestral states' or 'site rates'.\n", spacer);
18150 }
18151 else
18152 {
18153 m->useVec = VEC_SSE;
18154 m->numFloatsPerVec = 4;
18155 m->CondLikeDown = &CondLikeDown_Gen_SSE;
18156 m->CondLikeRoot = &CondLikeRoot_Gen_SSE;
18157 m->CondLikeScaler = &CondLikeScaler_Gen_SSE;
18158 m->Likelihood = &Likelihood_Gen_SSE;
18159 }
18160 # endif
18161 }
18162 }
18163 else
18164 {
18165 m->CondLikeDown = &CondLikeDown_NY98;
18166 m->CondLikeRoot = &CondLikeRoot_NY98;
18167 m->CondLikeScaler = &CondLikeScaler_NY98;
18168 m->Likelihood = &Likelihood_NY98;
18169 m->PosSelProbs = &PosSelProbs;
18170 m->SiteOmegas = &SiteOmegas;
18171 # if defined (SSE_ENABLED)
18172 if (m->printAncStates == YES || m->printSiteRates == YES)
18173 {
18174 MrBayesPrint ("%s Non-SSE version of conditional likelihood calculator will be used for division %d\n", spacer, i+1);
18175 MrBayesPrint ("%s due to request of reporting 'ancestral states' or 'site rates'.\n", spacer);
18176 }
18177 else
18178 {
18179 m->useVec = VEC_SSE;
18180 m->numFloatsPerVec = 4;
18181 m->CondLikeDown = &CondLikeDown_NY98_SSE;
18182 m->CondLikeRoot = &CondLikeRoot_NY98_SSE;
18183 m->CondLikeScaler = &CondLikeScaler_NY98_SSE;
18184 m->Likelihood = &Likelihood_NY98_SSE;
18185 m->PosSelProbs = &PosSelProbs_SSE;
18186 m->SiteOmegas = &SiteOmegas_SSE;
18187 }
18188 # endif
18189 }
18190 m->TiProbs = &TiProbs_Gen;
18191 if (m->nCijkParts > 1)
18192 m->TiProbs = &TiProbs_GenCov;
18193 m->CondLikeUp = &CondLikeUp_Gen;
18194 m->PrintAncStates = &PrintAncStates_Gen;
18195 m->PrintSiteRates = &PrintSiteRates_Gen;
18196 }
18197 else /* if (m->nucModelId == NUCMODEL_AA) */
18198 {
18199 if (m->gibbsGamma == YES)
18200 {
18201 m->CondLikeDown = &CondLikeDown_Gen_GibbsGamma;
18202 m->CondLikeRoot = &CondLikeRoot_Gen_GibbsGamma;
18203 m->CondLikeScaler = &CondLikeScaler_Gen_GibbsGamma;
18204 m->Likelihood = &Likelihood_Gen_GibbsGamma;
18205 }
18206 else
18207 {
18208 m->CondLikeDown = &CondLikeDown_Gen;
18209 m->CondLikeRoot = &CondLikeRoot_Gen;
18210 m->CondLikeScaler = &CondLikeScaler_Gen;
18211 m->Likelihood = &Likelihood_Gen;
18212 # if defined (SSE_ENABLED)
18213 if (m->printAncStates == YES || m->printSiteRates == YES)
18214 {
18215 MrBayesPrint ("%s Non-SSE version of conditional likelihood calculator will be used for division %d\n", spacer, i+1);
18216 MrBayesPrint ("%s due to request of reporting 'ancestral states' or 'site rates'.\n", spacer);
18217 }
18218 else
18219 {
18220 m->useVec = VEC_SSE;
18221 m->numFloatsPerVec = 4;
18222 m->CondLikeDown = &CondLikeDown_Gen_SSE;
18223 m->CondLikeRoot = &CondLikeRoot_Gen_SSE;
18224 m->CondLikeScaler = &CondLikeScaler_Gen_SSE;
18225 m->Likelihood = &Likelihood_Gen_SSE;
18226 }
18227 # endif
18228 }
18229 if (m->nCijkParts > 1)
18230 m->TiProbs = &TiProbs_GenCov;
18231 else
18232 m->TiProbs = &TiProbs_Gen;
18233 m->CondLikeUp = &CondLikeUp_Gen;
18234 m->StateCode = &StateCode_AA;
18235 m->PrintAncStates = &PrintAncStates_Gen;
18236 m->PrintSiteRates = &PrintSiteRates_Gen;
18237 }
18238 }
18239 }
18240 else if (m->dataType == PROTEIN)
18241 {
18242 if (m->parsModelId == YES)
18243 {
18244 m->Likelihood = &Likelihood_Pars;
18245 }
18246 else
18247 {
18248 /* TODO: allow autocorrelated rates for covarion model */
18249 if (m->gibbsGamma == YES)
18250 {
18251 m->CondLikeDown = &CondLikeDown_Gen_GibbsGamma;
18252 m->CondLikeRoot = &CondLikeRoot_Gen_GibbsGamma;
18253 m->CondLikeScaler = &CondLikeScaler_Gen_GibbsGamma;
18254 m->Likelihood = &Likelihood_Gen_GibbsGamma;
18255 }
18256 else
18257 {
18258 m->CondLikeDown = &CondLikeDown_Gen;
18259 m->CondLikeRoot = &CondLikeRoot_Gen;
18260 m->CondLikeScaler = &CondLikeScaler_Gen;
18261 m->Likelihood = &Likelihood_Gen;
18262 # if defined (SSE_ENABLED)
18263 if (m->printAncStates == YES || m->printSiteRates == YES)
18264 {
18265 MrBayesPrint ("%s Non-SSE version of conditional likelihood calculator will be used for division %d\n", spacer, i+1);
18266 MrBayesPrint ("%s due to request of reporting 'ancestral states' or 'site rates'.\n", spacer);
18267 }
18268 else
18269 {
18270 m->useVec = VEC_SSE;
18271 m->numFloatsPerVec = 4;
18272 m->CondLikeDown = &CondLikeDown_Gen_SSE;
18273 m->CondLikeRoot = &CondLikeRoot_Gen_SSE;
18274 m->CondLikeScaler = &CondLikeScaler_Gen_SSE;
18275 m->Likelihood = &Likelihood_Gen_SSE;
18276 }
18277 # endif
18278 }
18279 if (m->correlation != NULL)
18280 {
18281 if (m->gibbsGamma == YES)
18282 {
18283 MrBayesPrint ("%s Adgamma model cannot be used with Gibbs sampling of rate categories\n", spacer);
18284 return (ERROR);
18285 }
18286 else
18287 m->Likelihood = &Likelihood_Adgamma;
18288 }
18289 if (m->numModelStates > 20 && m->nCijkParts > 1)
18290 m->TiProbs = &TiProbs_GenCov;
18291 else
18292 m->TiProbs = &TiProbs_Gen;
18293 m->CondLikeUp = &CondLikeUp_Gen;
18294 m->StateCode = &StateCode_AA;
18295 m->PrintAncStates = &PrintAncStates_Gen;
18296 m->PrintSiteRates = &PrintSiteRates_Gen;
18297 }
18298 }
18299 else if (m->dataType == RESTRICTION)
18300 {
18301 if (m->parsModelId == YES)
18302 {
18303 m->Likelihood = &Likelihood_Pars;
18304 }
18305 else
18306 {
18307 m->CondLikeDown = &CondLikeDown_Bin;
18308 m->CondLikeRoot = &CondLikeRoot_Bin;
18309 m->CondLikeScaler = &CondLikeScaler_Gen;
18310 m->Likelihood = &Likelihood_Res;
18311 # if defined (SSE_ENABLED)
18312 if (m->printAncStates == YES || m->printSiteRates == YES)
18313 {
18314 MrBayesPrint ("%s Non-SSE version of conditional likelihood calculator will be used for division %d\n", spacer, i+1);
18315 MrBayesPrint ("%s due to request of reporting 'ancestral states' or 'site rates'.\n", spacer);
18316 }
18317 else
18318 {
18319 m->useVec = VEC_SSE;
18320 m->numFloatsPerVec = 4;
18321 m->CondLikeDown = &CondLikeDown_Bin_SSE;
18322 m->CondLikeRoot = &CondLikeRoot_Bin_SSE;
18323 m->CondLikeScaler = &CondLikeScaler_Gen_SSE;
18324 m->Likelihood = &Likelihood_Res_SSE;
18325 }
18326 # endif
18327 m->TiProbs = &TiProbs_Res;
18328 m->CondLikeUp = &CondLikeUp_Bin;
18329 m->StateCode = &StateCode_Std;
18330 m->PrintAncStates = &PrintAncStates_Bin;
18331 m->PrintSiteRates = &PrintSiteRates_Gen;
18332 }
18333 }
18334 else if (m->dataType == STANDARD)
18335 {
18336 if (m->parsModelId == YES)
18337 {
18338 if (m->numModelStates == 2)
18339 {
18340 m->Likelihood = &Likelihood_Pars; /* this is much faster if number of states do not vary */
18341 m->numStates = 2; /* this is needed for the parsimony calculator */
18342 }
18343 else
18344 m->Likelihood = &Likelihood_ParsStd;
18345 }
18346 else
18347 {
18348 m->CondLikeDown = &CondLikeDown_Std;
18349 m->CondLikeRoot = &CondLikeRoot_Std;
18350 m->CondLikeScaler = &CondLikeScaler_Std;
18351 m->Likelihood = &Likelihood_Std;
18352 m->TiProbs = &TiProbs_Std;
18353 m->CondLikeUp = &CondLikeUp_Std;
18354 m->StateCode = &StateCode_Std;
18355 m->PrintAncStates = &PrintAncStates_Std;
18356 m->PrintSiteRates = &PrintSiteRates_Std;
18357 }
18358 }
18359 else if (m->dataType == CONTINUOUS)
18360 {
18361
18362 }
18363 else
18364 {
18365 MrBayesPrint ("%s ERROR: Data should be one of these types!\n", spacer);
18366 return ERROR;
18367 }
18368
18369 }
18370
18371 return NO_ERROR;
18372 }
18373
18374
18375 /* Determine number of chains and data splits to be handled by MPI processors or threads */
SetLocalChainsAndDataSplits(void)18376 int SetLocalChainsAndDataSplits(void)
18377 {
18378 # if defined (MPI_ENABLED)
18379
18380 /* tell user how many chains each processor has been assigned */
18381 if (num_procs <= numGlobalChains)
18382 {
18383 if (numGlobalChains % num_procs != 0)
18384 {
18385 MrBayesPrint ("%s The total number of chains (%d) must be evenly divisible by\n", spacer, numGlobalChains);
18386 MrBayesPrint ("%s the number of MPI processors (%d).\n", spacer, num_procs);
18387 /*
18388 MrBayesPrint ("%s The total number of chains (%d) must be evenly divisible by\n", spacer, numGlobalChains);
18389 MrBayesPrint ("%s the number of MPI processors (%d), or the number of MPI\n", spacer, num_procs);
18390 MrBayesPrint ("%s processors should be a multiple of the number of chains.\n", spacer, num_procs);
18391 */
18392 MrBayesPrint ("%s Please change your MPI settings.\n", spacer, num_procs);
18393 return ERROR;
18394 }
18395 numLocalChains = numGlobalChains / num_procs;
18396 MrBayesPrint ("%s Number of chains per MPI processor = %d\n", spacer, numLocalChains);
18397 }
18398 else
18399 {
18400 MrBayesPrint ("%s There must be at least as many chains as MPI processors\n", spacer);
18401 return (ERROR);
18402 }
18403 /*
18404 {
18405 if (num_procs % numGlobalChains != 0)
18406 {
18407 MrBayesPrint ("%s The number of MPI processors (%d) must be a multiple of the\n", spacer, num_procs);
18408 MrBayesPrint ("%s total number of chains (%d), or the total number of chains\n", spacer, numGlobalChains);
18409 MrBayesPrint ("%s should be evenly divisible by the number of MPI processsors.\n", spacer);
18410 MrBayesPrint ("%s Please change your MPI settings.\n", spacer);
18411 return ERROR;
18412 }
18413 numLocalChains = 1;
18414 // numMPIDataSplits = num_procs / numGlobalChains;
18415 // MrBayesPrint ("%s Number of MPI data splits per chain = %d\n", spacer, numMPIDataSplits);
18416 }
18417 */
18418 # else
18419
18420 numLocalChains = numGlobalChains;
18421
18422 # endif
18423
18424 return (NO_ERROR);
18425 }
18426
18427
18428 /*----------------------------------------------------------------------------
18429 |
18430 | ShowMoveSummary: Show summary of moves that will be used in MCMC sampling
18431 |
18432 -----------------------------------------------------------------------------*/
ShowMoveSummary(void)18433 int ShowMoveSummary (void)
18434 {
18435 int i, run, chain, areRunsSame, areChainsSame, chainIndex;
18436 MCMCMove *mv;
18437 MrBFlt prob;
18438
18439 /* first find out if the probabilities are different in different runs */
18440 areRunsSame = YES;
18441 for (run=1; run<chainParams.numRuns; run++)
18442 {
18443 for (chain=0; chain<chainParams.numChains; chain++)
18444 {
18445 chainIndex = run*chainParams.numChains + chain;
18446 for (i=0; i<numUsedMoves; i++)
18447 {
18448 mv = usedMoves[i];
18449 if (AreDoublesEqual (mv->relProposalProb[chainIndex], mv->relProposalProb[chain], 0.000001) == NO)
18450 {
18451 areRunsSame = NO;
18452 break;
18453 }
18454 }
18455 if (areRunsSame == NO)
18456 break;
18457 }
18458 if (areRunsSame == NO)
18459 break;
18460 }
18461
18462 /* now print values */
18463 for (run=0; run<chainParams.numRuns; run++)
18464 {
18465 if (areRunsSame == YES && run >= 1)
18466 break;
18467
18468 /* find out if chains are different within this run */
18469 areChainsSame = YES;
18470 for (chain=1; chain<chainParams.numChains; chain++)
18471 {
18472 chainIndex = run*chainParams.numChains + chain;
18473 for (i=0; i<numUsedMoves; i++)
18474 {
18475 mv = usedMoves[i];
18476 if (AreDoublesEqual (mv->relProposalProb[chainIndex], mv->relProposalProb[chainIndex-chain], 0.000001) == NO)
18477 {
18478 areChainsSame = NO;
18479 break;
18480 }
18481 }
18482 if (areChainsSame == NO)
18483 break;
18484 }
18485
18486 for (chain=0; chain<chainParams.numChains; chain++)
18487 {
18488 if (areChainsSame == YES && chain >= 1)
18489 break;
18490
18491 /* now we can print the values */
18492 MrBayesPrint("\n");
18493 if (areRunsSame == YES && areChainsSame == YES)
18494 MrBayesPrint ("%s The MCMC sampler will use the following moves:\n", spacer);
18495 else if (areRunsSame == NO && areChainsSame == YES)
18496 MrBayesPrint ("%s The MCMC sampler will use the following moves for run %d:\n", spacer, run+1);
18497 else if (areRunsSame == YES && areChainsSame == NO)
18498 MrBayesPrint ("%s The MCMC sampler will use the following moves for chain %d:\n", spacer, chain+1);
18499 else if (areRunsSame == NO && areChainsSame == NO)
18500 MrBayesPrint ("%s The MCMC sampler will use the following moves for run %d, chain %d:\n", spacer, run+1, chain+1);
18501
18502 chainIndex = run*chainParams.numChains + chain;
18503 MrBayesPrint ("%s With prob. Chain will use move\n", spacer);
18504 for (i=0; i<numUsedMoves; i++)
18505 {
18506 mv = usedMoves[i];
18507 prob = mv->cumProposalProb[chainIndex];
18508 if (i > 0)
18509 prob -= usedMoves[i-1]->cumProposalProb[chainIndex];
18510 if (AreDoublesEqual(prob,0.0,0.000001) == YES)
18511 continue;
18512 MrBayesPrint ("%s %6.2f %% %s\n", spacer, 100*prob, mv->name);
18513 }
18514 MrBayesPrint ("\n");
18515 } /* next chain */
18516 } /* next run */
18517
18518 return (NO_ERROR);
18519 }
18520
18521
18522 /*----------------------------------------------------------------------
18523 |
18524 | SetUpPartitionCounters: Set up partitions and the root of the
18525 | partition frequency tree
18526 |
18527 |----------------------------------------------------------------------*/
SetUpPartitionCounters(void)18528 int SetUpPartitionCounters (void)
18529 {
18530 int i;
18531
18532 # if defined (MPI_ENABLED)
18533 /* we only keep partition counters on proc 0 in the MPI version */
18534 if (proc_id != 0)
18535 return (NO_ERROR);
18536 # endif
18537 nLongsNeeded = 1 + (numLocalTaxa - 1) / nBitsInALong;
18538
18539 if (memAllocs[ALLOC_PFCOUNTERS] == YES)
18540 {
18541 MrBayesPrint ("%s ERROR: pfcounters not free in SetUpPartitionCounters\n", spacer);
18542 return ERROR;
18543 }
18544 partition = (BitsLong **) SafeCalloc (2*numLocalTaxa, sizeof (BitsLong *));
18545 if (partition == NULL)
18546 {
18547 MrBayesPrint ("%s Failed to allocate partition in SetUpPartitionCounters\n", spacer);
18548 return ERROR;
18549 }
18550 partition[0] = (BitsLong *) SafeCalloc (2*numLocalTaxa * nLongsNeeded, sizeof(BitsLong));
18551 if (partition[0] == NULL)
18552 {
18553 free (partition);
18554 MrBayesPrint ("%s Failed to allocate partition[0] in SetUpPartitionCounters\n", spacer);
18555 return ERROR;
18556 }
18557 partFreqTreeRoot = (PFNODE **) SafeCalloc (numTopologies, sizeof (PFNODE *));
18558 if (partFreqTreeRoot == NULL)
18559 {
18560 free (partition[0]);
18561 free (partition);
18562 MrBayesPrint ("%s Failed to allocate partFreqTreeRoot in SetUpPartitionCounters\n", spacer);
18563 return ERROR;
18564 }
18565 memAllocs[ALLOC_PFCOUNTERS] = YES;
18566
18567 for (i=1; i<2*numLocalTaxa; i++)
18568 {
18569 partition[i] = partition[0] + i*nLongsNeeded;
18570 }
18571
18572 for (i=0; i<numLocalTaxa; i++)
18573 SetBit (i, partition[i]);
18574
18575 for (i=0; i<numTopologies; i++)
18576 partFreqTreeRoot[i] = NULL;
18577
18578 return NO_ERROR;
18579 }
18580
18581
18582 /*----------------------------------------------------------------------
18583 |
18584 | SetupTermState: create matrix holding unambiguous states for
18585 | terminals (used for local compression on terminal branches)
18586 |
18587 -----------------------------------------------------------------------*/
SetUpTermState(void)18588 int SetUpTermState (void)
18589 {
18590 int i, k, n, c, d, x=0, *termStatePtr;
18591 BitsLong *p;
18592 ModelInfo *m;
18593 ModelParams *mp;
18594 int numComprChars = 0;
18595
18596 /* allocate space for termState and isPartAmbig */
18597 if (memAllocs[ALLOC_TERMSTATE] == YES || memAllocs[ALLOC_ISPARTAMBIG] == YES)
18598 {
18599 MrBayesPrint ("%s termState or isPartAmbig is not free in SetupTermState\n", spacer);
18600 return ERROR;
18601 }
18602
18603 # if defined SSE_ENABLED
18604 for (d=0; d<numCurrentDivisions; d++)
18605 {
18606 m = &modelSettings[d];
18607 if ( m->useVec != VEC_NONE )
18608 {
18609 m->numVecChars = ((m->numChars - 1) / m->numFloatsPerVec) + 1;
18610 numComprChars += m->numVecChars * m->numFloatsPerVec;
18611 }
18612 else
18613 numComprChars += m->numChars;
18614 }
18615 # else
18616 numComprChars = numCompressedChars;
18617 # endif
18618
18619 termState = (int *) SafeCalloc (numLocalTaxa * numComprChars, sizeof(int));
18620 if (termState)
18621 memAllocs[ALLOC_TERMSTATE] = YES;
18622 else
18623 {
18624 MrBayesPrint ("%s Problem allocating termState\n", spacer);
18625 return (ERROR);
18626 }
18627 isPartAmbig = (int *) SafeCalloc (numLocalTaxa*numCurrentDivisions, sizeof(int));
18628 if (isPartAmbig)
18629 memAllocs[ALLOC_ISPARTAMBIG] = YES;
18630 else
18631 {
18632 MrBayesPrint ("%s Problem allocating isPartAmbig\n", spacer);
18633 return (ERROR);
18634 }
18635
18636 /*initialize isPartAmbig */
18637 for (i=0; i<numLocalTaxa*numCurrentDivisions; i++)
18638 isPartAmbig[i] = NO;
18639
18640 /* loop over divisions */
18641 termStatePtr = termState;
18642 for (d=0; d<numCurrentDivisions; d++)
18643 {
18644 m = &modelSettings[d];
18645 mp = &modelParams[d];
18646
18647 /* don't do anything for continuous data */
18648 if (mp->dataType == CONTINUOUS)
18649 continue;
18650
18651 m->termState = (int **) SafeCalloc (numLocalTaxa, sizeof(int *));
18652 if (!m->termState)
18653 {
18654 MrBayesPrint("%s Problems allocating termState pointers for division %d\n", spacer, d+1);
18655 return ERROR;
18656 }
18657
18658 # if defined SSE_ENABLED
18659 if (m->dataType != STANDARD && m->gibbsGamma == NO)
18660 numComprChars = m->numVecChars * m->numFloatsPerVec;
18661 else
18662 numComprChars = m->numChars;
18663
18664 # else
18665 numComprChars = m->numChars;
18666 # endif
18667
18668 for (i=0; i<numLocalTaxa; i++)
18669 {
18670 m->termState[i] = termStatePtr;
18671 termStatePtr += numComprChars;
18672 }
18673
18674 m->isPartAmbig = isPartAmbig + numLocalTaxa * d;
18675
18676 for (i=0; i<numLocalTaxa; i++)
18677 {
18678 p = m->parsSets[i];
18679 for (c=0; c<m->numChars; c++)
18680 {
18681 for (k=n=0; k<m->numStates; k++)
18682 {
18683 if (IsBitSet(k, p))
18684 {
18685 x = k;
18686 n++;
18687 }
18688 }
18689 /* find appropriate index */
18690 if (n == 1)
18691 m->termState[i][c] = x * m->numModelStates;
18692 else if (n == m->numStates)
18693 m->termState[i][c] = m->numStates * m->numModelStates;
18694 else
18695 m->isPartAmbig[i] = YES;
18696
18697 p += m->nParsIntsPerSite;
18698 }
18699 for (; c<numComprChars; c++)
18700 {
18701 /* Setting to fully ambig state all padding chars */
18702 m->termState[i][c] = m->numStates * m->numModelStates;
18703 }
18704 }
18705 }
18706
18707 /* print the termState matrix */
18708 # if defined (DEBUG_SETUPTERMSTATE)
18709 PrintTermState();
18710 getchar();
18711 # endif
18712
18713 return NO_ERROR;
18714 }
18715
18716
18717 /*----------------------------------------------------------------------------
18718 |
18719 | SetUsedMoves: This function will extract an array of pointers to the moves
18720 | that will actually be used in the MCMC sampling. It also makes sure
18721 | that the parsimonyBased flag is set in the relevant model partitions
18722 | if there are moves used that are based on parsimony scores
18723 |
18724 -----------------------------------------------------------------------------*/
SetUsedMoves(void)18725 int SetUsedMoves (void)
18726 {
18727 int i, j, moveIndex, numGlobalChains;
18728 MrBFlt prob, sum, cumSum;
18729
18730 /* first count moves */
18731 numUsedMoves = 0;
18732 numGlobalChains = chainParams.numChains * chainParams.numRuns;
18733 for (i=0; i<numApplicableMoves; i++)
18734 {
18735 prob = 0.0;
18736 for (j=0; j<numGlobalChains; j++)
18737 {
18738 if (moves[i]->relProposalProb[j] > prob)
18739 prob = moves[i]->relProposalProb[j];
18740 }
18741 if (prob > 0.000001)
18742 numUsedMoves++;
18743 }
18744
18745 /* allocate space */
18746 if (memAllocs[ALLOC_USEDMOVES] == YES)
18747 {
18748 MrBayesPrint ("%s Memory problem: usedMoves not free in SetUsedMoves\n", spacer);
18749 return (ERROR);
18750 }
18751 usedMoves = (MCMCMove **) SafeMalloc (numUsedMoves * sizeof (MCMCMove *));
18752 if (!usedMoves)
18753 {
18754 MrBayesPrint ("%s Problem allocating usedMoves\n", spacer);
18755 return (ERROR);
18756 }
18757 memAllocs[ALLOC_USEDMOVES] = YES;
18758
18759 /* set move pointers */
18760 moveIndex = 0;
18761 for (i=0; i<numApplicableMoves; i++)
18762 {
18763 prob = 0.0;
18764 for (j=0; j<numGlobalChains; j++)
18765 {
18766 if (moves[i]->relProposalProb[j] > prob)
18767 prob = moves[i]->relProposalProb[j];
18768 }
18769 if (prob > 0.000001)
18770 usedMoves[moveIndex++] = moves[i];
18771 }
18772
18773 if (moveIndex != numUsedMoves)
18774 {
18775 MrBayesPrint ("%s Problem finding the used moves\n", spacer);
18776 return (ERROR);
18777 }
18778
18779 /* set parsimony flag if applicable */
18780 for (i=0; i<numCurrentDivisions; i++)
18781 modelSettings[i].parsimonyBasedMove = NO;
18782 for (i=0; i<numUsedMoves; i++)
18783 {
18784 if (usedMoves[i]->moveType->parsimonyBased == YES)
18785 {
18786 for (j=0; j<usedMoves[i]->parm->nRelParts; j++)
18787 modelSettings[usedMoves[i]->parm->relParts[j]].parsimonyBasedMove = YES;
18788 }
18789 }
18790
18791 /* set cumulative proposal probabilities */
18792 for (j=0; j<numGlobalChains; j++)
18793 {
18794 sum = 0.0;
18795 for (i=0; i<numUsedMoves; i++)
18796 {
18797 sum += usedMoves[i]->relProposalProb[j];
18798 }
18799 cumSum = 0.0;
18800 for (i=0; i<numUsedMoves; i++)
18801 {
18802 cumSum += usedMoves[i]->relProposalProb[j];
18803 usedMoves[i]->cumProposalProb[j] = cumSum / sum;
18804 }
18805 }
18806
18807 /* reset acceptance probability values */
18808 for (i=0; i<numUsedMoves; i++)
18809 {
18810 for (j=0; j<numGlobalChains; j++)
18811 {
18812 usedMoves[i]->nAccepted[j] = 0;
18813 usedMoves[i]->nTried[j] = 0;
18814 usedMoves[i]->nTotAccepted[j] = 0;
18815 usedMoves[i]->nTotTried[j] = 0;
18816 }
18817 }
18818
18819 return (NO_ERROR);
18820 }
18821
18822
ShowValuesForChain(int chn)18823 void ShowValuesForChain (int chn)
18824 {
18825 int i;
18826 char s[100];
18827
18828 MrBayesPrint ("%s Chain = %d\n", spacer, chn);
18829 MrBayesPrint ("%s numParams = %d\n", spacer, numParams);
18830 MrBayesPrint ("%s numTrees = %d\n", spacer, numTrees);
18831 MrBayesPrint ("%s current state: %d\n", spacer, state[chn]);
18832
18833 strcat (spacer, " ");
18834
18835 /* tRatio */
18836 for (i=0; i<numCurrentDivisions; i++)
18837 {
18838 sprintf (s, "tRatio[%d]", i);
18839 PrintParamValues (modelSettings[i].tRatio, chn, s);
18840 }
18841
18842 /* revMat */
18843 for (i=0; i<numCurrentDivisions; i++)
18844 {
18845 sprintf (s, "revMat[%d]", i);
18846 PrintParamValues (modelSettings[i].revMat, chn, s);
18847 }
18848
18849 /* stateFreq */
18850 for (i=0; i<numCurrentDivisions; i++)
18851 {
18852 sprintf (s, "stateFreq[%d]", i);
18853 PrintParamValues (modelSettings[i].stateFreq, chn, s);
18854 }
18855
18856 /* omega */
18857 for (i=0; i<numCurrentDivisions; i++)
18858 {
18859 sprintf (s, "omega[%d]", i);
18860 PrintParamValues (modelSettings[i].omega, chn, s);
18861 }
18862
18863 /* shape */
18864 for (i=0; i<numCurrentDivisions; i++)
18865 {
18866 sprintf (s, "shape[%d]", i);
18867 PrintParamValues (modelSettings[i].shape, chn, s);
18868 }
18869
18870 /* pInvar */
18871 for (i=0; i<numCurrentDivisions; i++)
18872 {
18873 sprintf (s, "pInvar[%d]", i);
18874 PrintParamValues (modelSettings[i].pInvar, chn, s);
18875 }
18876
18877 /* correlation */
18878 for (i=0; i<numCurrentDivisions; i++)
18879 {
18880 sprintf (s, "correlation[%d]", i);
18881 PrintParamValues (modelSettings[i].correlation, chn, s);
18882 }
18883
18884 /* switchRates */
18885 for (i=0; i<numCurrentDivisions; i++)
18886 {
18887 sprintf (s, "switchRates[%d]", i);
18888 PrintParamValues (modelSettings[i].switchRates, chn, s);
18889 }
18890
18891 /* rateMult */
18892 for (i=0; i<numCurrentDivisions; i++)
18893 {
18894 sprintf (s, "rateMult[%d]", i);
18895 PrintParamValues (modelSettings[i].rateMult, chn, s);
18896 }
18897
18898 /* speciationRates */
18899 for (i=0; i<numCurrentDivisions; i++)
18900 {
18901 sprintf (s, "speciationRates[%d]", i);
18902 PrintParamValues (modelSettings[i].speciationRates, chn, s);
18903 }
18904
18905 /* extinctionRates */
18906 for (i=0; i<numCurrentDivisions; i++)
18907 {
18908 sprintf (s, "extinctionRates[%d]", i);
18909 PrintParamValues (modelSettings[i].extinctionRates, chn, s);
18910 }
18911
18912 /* fossilizationRates */
18913 for (i=0; i<numCurrentDivisions; i++)
18914 {
18915 sprintf (s, "fossilizationRates[%d]", i);
18916 PrintParamValues (modelSettings[i].fossilizationRates, chn, s);
18917 }
18918
18919 /* popSize */
18920 for (i=0; i<numCurrentDivisions; i++)
18921 {
18922 sprintf (s, "popSize[%d]", i);
18923 PrintParamValues (modelSettings[i].popSize, chn, s);
18924 }
18925
18926 /* topology */
18927 for (i=0; i<numCurrentDivisions; i++)
18928 {
18929 MrBayesPrint ("%s topology[%d] state 0\n", spacer, i);
18930 ShowTree(GetTree (modelSettings[i].topology, chn, 0));
18931 MrBayesPrint ("%s topology[%d] state 1\n", spacer, i);
18932 ShowTree(GetTree (modelSettings[i].topology, chn, 1));
18933 }
18934
18935 /* brlens */
18936 for (i=0; i<numCurrentDivisions; i++)
18937 {
18938 MrBayesPrint ("%s tree[%d] state 0\n", spacer, i);
18939 ShowTree(GetTree (modelSettings[i].topology, chn, 0));
18940 MrBayesPrint ("%s tree[%d] state 1\n", spacer, i);
18941 ShowTree(GetTree (modelSettings[i].topology, chn, 1));
18942 }
18943
18944 spacer[strlen(spacer) - 3] = '\0';
18945
18946 # if 0
18947 for (i=0; i<sizeOfParamValues; i++)
18948 MrBayesPrint ("%4d -- %lf\n", i, paramValues[i]);
18949 # endif
18950 }
18951
18952
18953 /* SmallestNonemptyPFNode: recursive function to smallest nonempty node in a subtree */
SmallestNonemptyPFNode(PFNODE * p,int * i,int j)18954 PFNODE *SmallestNonemptyPFNode (PFNODE *p, int *i, int j)
18955 {
18956 PFNODE *q;
18957
18958 ++j;
18959 if (p == NULL)
18960 return NULL;
18961
18962 q = SmallestNonemptyPFNode (p->right, i, j);
18963
18964 if (q != NULL)
18965 {
18966 return q;
18967 }
18968 else if (IsPFNodeEmpty (p) == NO)
18969 {
18970 *i = j;
18971 return p;
18972 }
18973 else
18974 {
18975 return SmallestNonemptyPFNode (p->left, i, j);
18976 }
18977 }
18978
18979
18980 /* Talloc: Allocate space for a new node in the tree keeping track of partition frequencies */
Talloc(void)18981 PFNODE *Talloc (void)
18982 {
18983 PFNODE *temp;
18984
18985 temp = (PFNODE *) SafeMalloc (sizeof(PFNODE));
18986 if (temp == NULL)
18987 return NULL;
18988
18989 temp->partition = (BitsLong *) SafeCalloc (nLongsNeeded, sizeof (BitsLong));
18990 if (temp->partition == NULL)
18991 {
18992 free (temp);
18993 return NULL;
18994 }
18995
18996 temp->count = (int *) SafeCalloc (chainParams.numRuns, sizeof (int));
18997 if (temp->count == NULL)
18998 {
18999 free (temp->partition);
19000 free (temp);
19001 return NULL;
19002 }
19003
19004 return temp;
19005 }
19006
19007
Temperature(int id)19008 MrBFlt Temperature (int id)
19009 {
19010 /* let id be number of chain in run */
19011 id %= chainParams.numChains;
19012
19013 if (chainParams.userDefinedTemps == YES)
19014 {
19015 return (chainParams.userTemps[id]);
19016 }
19017 else
19018 {
19019 return (1.0 / (1.0 + chainParams.chainTemp * id));
19020 }
19021 }
19022
19023
19024 /* Tfree: Free space for partition frequency counter tree */
Tfree(PFNODE * r)19025 void Tfree (PFNODE *r)
19026 {
19027 if (r != NULL)
19028 {
19029 if (r->left != NULL)
19030 Tfree (r->left);
19031 if (r->right != NULL)
19032 Tfree (r->right);
19033
19034 free (r->partition);
19035 free (r->count);
19036 free (r);
19037 }
19038 }
19039
19040
TouchAllCijks(int chain)19041 void TouchAllCijks (int chain)
19042 {
19043 int i;
19044
19045 for (i=0; i<numCurrentDivisions; i++)
19046 {
19047 if (modelSettings[i].nCijkParts >= 1)
19048 modelSettings[i].upDateCijk = YES;
19049 }
19050
19051 return;
19052 }
19053
19054
TouchAllPartitions(void)19055 void TouchAllPartitions (void)
19056 {
19057 int i;
19058
19059 for (i=0; i<numCurrentDivisions; i++)
19060 {
19061 modelSettings[i].upDateCl = YES;
19062 }
19063
19064 return;
19065 }
19066
19067
TouchAllTrees(int chain)19068 void TouchAllTrees (int chain)
19069 {
19070 int i, j;
19071 Tree *t;
19072 TreeNode *p;
19073
19074 for (i=0; i<numTrees; i++)
19075 {
19076 t = GetTreeFromIndex (i, chain, state[chain]);
19077 for (j=0; j<t->nNodes; j++)
19078 {
19079 p = t->allDownPass[j];
19080 p->upDateCl = YES;
19081 p->upDateTi = YES;
19082 }
19083 }
19084
19085 for (i=0; i<numCurrentDivisions; i++)
19086 modelSettings[i].upDateAll = YES;
19087
19088 return;
19089 }
19090
19091
19092 /* Touch all update flags so we recalculate likelihood from scratch */
TouchEverything(int chain)19093 void TouchEverything (int chain)
19094 {
19095 int i, j;
19096 Tree *t;
19097 TreeNode *p;
19098
19099 for (i=0; i<numCurrentDivisions; i++)
19100 {
19101 if (modelSettings[i].nCijkParts >= 1)
19102 modelSettings[i].upDateCijk = YES;
19103 modelSettings[i].upDateCl = YES;
19104 modelSettings[i].upDateAll = YES;
19105 }
19106
19107 for (i=0; i<numTrees; i++)
19108 {
19109 t = GetTreeFromIndex (i, chain, state[chain]);
19110 for (j=0; j<t->nNodes; j++)
19111 {
19112 p = t->allDownPass[j];
19113 p->upDateCl = YES;
19114 p->upDateTi = YES;
19115 }
19116 }
19117
19118 return;
19119 }
19120
19121
19122 /*------------------------------------------------------------------
19123 |
19124 | TreeLength: Calculates the tree length as the sum of the lengths
19125 | of all the branches. The tree length is the expected number
19126 | of character state transformations per character over the
19127 | entire phylogenetic tree.
19128 |
19129 -------------------------------------------------------------------*/
TreeLength(Param * param,int chain)19130 MrBFlt TreeLength (Param *param, int chain)
19131 {
19132 int i, j;
19133 MrBFlt tl;
19134 Tree *t;
19135 TreeNode *p;
19136
19137 if (param->paramId == BRLENS_PARSIMONY)
19138 {
19139 tl = 0.0;
19140 for (j=0; j<param->nRelParts; j++)
19141 tl += modelSettings[param->relParts[j]].parsTreeLength[2*chain+state[chain]];
19142 }
19143 else
19144 {
19145 /* get tree */
19146 t = GetTree (param, chain, state[chain]);
19147
19148 /* loop over all branches of the tree */
19149 tl = 0.0;
19150 for (i=0; i<t->nNodes; i++)
19151 {
19152 p = t->allDownPass[i];
19153 if (p->anc != NULL)
19154 {
19155 if (p->anc->anc == NULL && t->isRooted == NO)
19156 {
19157 tl += p->length;
19158 }
19159 else
19160 {
19161 tl += p->length;
19162 }
19163 }
19164 }
19165 }
19166
19167 return (tl);
19168 }
19169
19170
19171 /* proportion of ancestral fossils in a FBD tree */
PropAncFossil(Param * param,int chain)19172 MrBFlt PropAncFossil (Param *param, int chain)
19173 {
19174 int i, m, k;
19175 Tree *t;
19176 TreeNode *p;
19177
19178 t = GetTree (param, chain, state[chain]);
19179
19180 if (t->isRooted == NO)
19181 return 0.0;
19182
19183 /* count # of tip and ancestral fossils */
19184 m = k = 0;
19185 for (i = 0; i < t->nNodes -2; i++)
19186 {
19187 p = t->allDownPass[i];
19188 if (p->left == NULL && p->right == NULL && p->nodeDepth > 0.0)
19189 {
19190 if (p->length > 0.0)
19191 m++;
19192 else
19193 k++;
19194 }
19195 }
19196
19197 if (k == 0)
19198 return 0.0;
19199 else
19200 return (MrBFlt)k / (m+k);
19201 }
19202
19203