4 * This file contains the functions
5 * for calculating the probability of
6 * gene trees given the species tree
7 * and the prior probability of the
11 * Department of Statistics
12 * The Ohio State University
15 * liuliang@stat.ohio-state.edu
26 const char* const svnRevisionBestC = "$Rev: 1040 $"; /* Revision keyword which is expended/updated by svn on each commit/update */
28 /****************************** Local functions converted by Fredrik from BEST code *****************************/
29 int CompareDepths (const void *x, const void *y);
30 int CompareDoubles (const void *x, const void *y);
31 int CompareNodes (const void *x, const void *y);
32 int CompareNodesByX (const void *x, const void *y);
33 int GetSpeciesTreeFromMinDepths (Tree* speciesTree, double *depthMatrix);
34 int GetDepthMatrix(Tree * speciesTree, double *depthMatrix);
35 int GetMeanDist (Tree *speciesTree, double *depthMatrix, double *mean);
36 int GetMinDepthMatrix (Tree **geneTrees, int numGeneTrees, double *depthMatrix);
37 void LineagesIn (TreeNode* geneTreeNode, TreeNode* speciesTreeNode);
38 double LnPriorProbGeneTree (Tree *geneTree, double mu, Tree *speciesTree, double *popSizePtr);
39 double LnProposalProbSpeciesTree (Tree *speciesTree, double *depthMatrix, double expRate);
40 void MapGeneTreeToSpeciesTree (Tree *geneTree, Tree *speciesTree);
41 int ModifyDepthMatrix (double expRate, double *depthMatrix, RandLong *seed);
43 /* Global BEST variables */
44 BitsLong **speciesPairSets;
47 /* Allocate variables used by best code during mcmc */
48 void AllocateBestChainVariables (void)
50 int i, j, index, numUpperTriang, nLongsNeeded;
52 // Free if by mistake variables are already allocated
53 if (memAllocs[ALLOC_BEST] == YES)
54 FreeBestChainVariables ();
56 // Allocate space for upper triangular pair sets
57 numUpperTriang = (numSpecies * (numSpecies-1)) / 2;
58 nLongsNeeded = ((numSpecies - 1) / nBitsInALong) + 1;
59 speciesPairSets = (BitsLong **) SafeCalloc (numUpperTriang, sizeof(BitsLong *));
60 speciesPairSets[0] = (BitsLong *) SafeCalloc (numUpperTriang*nLongsNeeded, sizeof(BitsLong));
61 for (i=1; i<numUpperTriang; i++)
62 speciesPairSets[i] = speciesPairSets[0] + i*nLongsNeeded;
64 // Set upper triangular pair partitions once and for all
66 for (i=0; i<numSpecies; i++) {
67 for (j=i+1; j<numSpecies; j++) {
68 SetBit(i, speciesPairSets[index]);
69 SetBit(j, speciesPairSets[index]);
74 /* allocate species for depthMatrix */
75 depthMatrix = SafeCalloc (numUpperTriang, sizeof(double));
77 memAllocs[ALLOC_BEST] = YES;
81 /** Compare function (Depth struct) for qsort */
82 int CompareDepths (const void *x, const void *y) {
84 if ((*((Depth *)(x))).depth < (*((Depth *)(y))).depth)
86 else if ((*((Depth *)(x))).depth > (*((Depth *)(y))).depth)
93 /** Compare function (doubles) for qsort */
94 int CompareDoubles (const void *x, const void *y) {
96 if (*((double *)(x)) < *((double *)(y)))
98 else if (*((double *)(x)) > *((double *)(y)))
105 /** Compare function (TreeNode struct) for qsort */
106 int CompareNodes (const void *x, const void *y) {
108 if ((*((TreeNode **)(x)))->nodeDepth < (*((TreeNode**)(y)))->nodeDepth)
110 else if ((*((TreeNode **)(x)))->nodeDepth > (*((TreeNode**)(y)))->nodeDepth)
117 /** Compare function (TreeNode struct; sort by x, then by nodeDepth) for qsort */
118 int CompareNodesByX (const void *x, const void *y) {
120 if ((*((TreeNode **)(x)))->x < (*((TreeNode**)(y)))->x)
122 else if ((*((TreeNode **)(x)))->x > (*((TreeNode**)(y)))->x)
125 if ((*((TreeNode **)(x)))->nodeDepth < (*((TreeNode**)(y)))->nodeDepth)
127 else if ((*((TreeNode **)(x)))->nodeDepth > (*((TreeNode**)(y)))->nodeDepth)
135 /**-----------------------------------------------------------------
137 | FillSpeciesTreeParams: Fill in species trees (start value)
139 ------------------------------------------------------------------*/
140 int FillSpeciesTreeParams (RandLong *seed, int fromChain, int toChain)
142 int i, k, chn, numGeneTrees, freeBestChainVars;
144 Tree *speciesTree, **geneTrees;
146 // Allocate space for global best model variables used in this function, in case they are not allocated
147 if (memAllocs[ALLOC_BEST] == NO)
149 freeBestChainVars = YES;
150 AllocateBestChainVariables();
153 freeBestChainVars = NO;
155 // Use global variable numTopologies to calculate number of gene trees
156 // There is one topology for the species tree, the other ones are gene trees
157 // The number of current divisions is not safe because one gene tree can have
158 // several partitions, for instance if we assign the different genes on the
159 // mitochondrion different substitution models, or if we assign different rates
160 // to the codon site positions in a sequence
161 numGeneTrees = numTopologies - 1;
162 geneTrees = (Tree **) SafeCalloc (numGeneTrees, sizeof(Tree*));
164 // Build species trees for state 0
165 for (chn=fromChain; chn<toChain; chn++)
167 for (k=0; k<numParams; k++)
170 if (p->paramType == P_SPECIESTREE)
172 // Find species tree and gene trees
173 speciesTree = GetTree(p, chn, 0);
174 for (i=0; i<p->nSubParams; i++)
175 geneTrees[i] = GetTree(p->subParams[i], chn, 0);
177 // Get minimum depth matrix for species tree
178 GetMinDepthMatrix (geneTrees, numGeneTrees, depthMatrix);
180 // Get a species tree from min depth matrix
181 GetSpeciesTreeFromMinDepths(speciesTree, depthMatrix);
183 assert (IsSpeciesTreeConsistent(speciesTree, chn) == YES);
186 if (LabelTree (speciesTree, speciesNameSets[speciespartitionNum].names) == ERROR)
188 FreeBestChainVariables();
198 // Free best model variables if appropriate
199 if (freeBestChainVars == YES)
200 FreeBestChainVariables();
203 MrBayesPrint ("%lf", *seed); /* just because I am tired of seeing the unused parameter error msg */
207 /**-----------------------------------------------------------------
209 | FreeBestChainVariables: Free best variables used during an mcmc
212 ------------------------------------------------------------------*/
213 void FreeBestChainVariables(void)
215 if (memAllocs[ALLOC_BEST] == YES) {
216 free (speciesPairSets[0]);
217 free (speciesPairSets);
218 speciesPairSets = NULL;
224 memAllocs[ALLOC_BEST] = NO;
228 /**---------------------------------------------------------------------
232 | This algorithm calculates the upper triangular depth matrix for the
233 | species tree. Time complexity O(n^2).
235 | @param speciesTree The species tree (in)
236 | @param depthMatrix The minimum depth matrix, upper triangular array (out)
237 | @returns Returns ERROR or NO_ERROR
238 ----------------------------------------------------------------------*/
239 int GetDepthMatrix (Tree *speciesTree, double *depthMatrix) {
241 int i, left, right, numUpperTriang, index, nLongsNeeded, freeBitsets;
245 // Make sure we have bitfields allocated and set
247 if (speciesTree->bitsets == NULL)
249 AllocateTreePartitions(speciesTree);
254 ResetTreePartitions(speciesTree); // just in case
258 // Calculate number of values in the upper triangular matrix
259 numUpperTriang = numSpecies * (numSpecies - 1) / 2;
261 // Number of longs needed in a bitfield representing a species set
262 nLongsNeeded = ((numSpecies -1) / nBitsInALong) + 1;
264 // Set all cells to max
265 maxDepth = speciesTree->root->left->nodeDepth; // root depth
266 for (i=0; i<numUpperTriang; i++)
267 depthMatrix[i] = maxDepth;
269 // Loop over interior nodes
270 for (i=0; i<speciesTree->nIntNodes; i++)
272 p = speciesTree->intDownPass[i];
273 for (left = FirstTaxonInPartition(p->left->partition, nLongsNeeded); left < numSpecies; left = NextTaxonInPartition(left, p->left->partition, nLongsNeeded))
275 for (right = FirstTaxonInPartition(p->right->partition, nLongsNeeded); right < numSpecies; right = NextTaxonInPartition(right, p->right->partition, nLongsNeeded))
277 index = UpperTriangIndex(left, right, numSpecies);
278 depthMatrix[index] = p->nodeDepth;
283 // Free partitions if appropriate
284 if (freeBitsets == YES)
285 FreeTreePartitions(speciesTree);
291 /**---------------------------------------------------------------------
295 | This algorithm calculates the mean distance between a distance matrix
296 | and the minimum depths that define a species tree.
298 | @param speciesTree The species tree (in)
299 | @param minDepthMatrix The minimum depth matrix, upper triangular array (in)
300 | @param mean The mean distance (out)
301 | @returns Returns ERROR or NO_ERROR
302 ----------------------------------------------------------------------*/
303 int GetMeanDist (Tree *speciesTree, double *minDepthMatrix, double *mean) {
305 int i, left, right, index, nLongsNeeded, freeBitsets;
306 double dist, minDist=0.0, distSum;
309 // Make sure we have bitfields allocated and set
311 if (speciesTree->bitsets == NULL)
313 AllocateTreePartitions(speciesTree);
318 ResetTreePartitions(speciesTree); // just in case
322 // Number of longs needed in a bitfield representing a species set
323 nLongsNeeded = ((numSpecies -1) / nBitsInALong) + 1;
325 // Loop over interior nodes
327 for (i=0; i<speciesTree->nIntNodes; i++)
329 p = speciesTree->intDownPass[i];
331 while ((left=FirstTaxonInPartition(p->left->partition, nLongsNeeded)) < numSpecies)
333 while ((right=FirstTaxonInPartition(p->right->partition, nLongsNeeded)) < numSpecies)
336 index = UpperTriangIndex(left, right, numSpecies);
337 dist = depthMatrix[index] - p->nodeDepth;
340 else if (dist < minDist)
342 ClearBit(right, p->right->partition);
344 ClearBit(left, p->left->partition);
349 (*mean) = distSum / speciesTree->nIntNodes;
351 // Reset partitions that were destroyed above or free partitions, as appropriate
352 if (freeBitsets == YES)
353 FreeTreePartitions(speciesTree);
355 ResetTreePartitions(speciesTree);
358 MrBayesPrint ("%lf", *minDepthMatrix); /* just because I am tired of seeing the unused parameter error msg */
362 /**---------------------------------------------------------------------
364 | GetMinDepthMatrix: converted from GetMinDists.
366 | This algorithm scans the gene trees and calculates the minimum depth
367 | (height) separating species across gene trees. The complexity of the
368 | original algorithm was O(mn^3), where m is the number of gene trees and
369 | n is the number of taxa in each gene tree. I think this algorithm has
370 | complexity that is better on average, but the difference is small.
372 | I have rewritten the algorithm also to show alternative techniques that
373 | could be used in this and other BEST algorithms.
375 | @param geneTrees The gene trees (in)
376 | @param depthMatrix The minimum depth matrix, upper triangular array (out)
377 | @returns Returns ERROR or NO_ERROR
378 ----------------------------------------------------------------------*/
379 int GetMinDepthMatrix (Tree **geneTrees, int numGeneTrees, double *depthMatrix) {
381 int i, j, w, nLongsNeeded, numUpperTriang, index, trace=0;
384 BitsLong **speciesSets;
386 // Allocate space for species partitions
387 nLongsNeeded = ((numSpecies -1) / nBitsInALong) + 1; // number of longs needed in a bitfield representing a species set
388 speciesSets = (BitsLong **) SafeCalloc ((2*(size_t)numLocalTaxa-1), sizeof(BitsLong *));
389 speciesSets[0] = (BitsLong *) SafeCalloc ((2*(size_t)numLocalTaxa-1)*nLongsNeeded, sizeof(BitsLong));
390 for (i=1; i<2*numLocalTaxa-1; i++)
391 speciesSets[i] = speciesSets[0] + i*nLongsNeeded;
393 // Set tip species partitions once and for all
394 for (i=0; i<numLocalTaxa; i++)
395 SetBit(speciespartitionId[i][speciespartitionNum]-1, speciesSets[i]);
397 // Set initial max depth for upper triangular matrix
398 numUpperTriang = (numSpecies * (numSpecies - 1)) / 2;
399 maxDepth = geneTrees[0]->root->left->nodeDepth;
400 for (i=0; i<numUpperTriang; i++)
401 depthMatrix[i] = maxDepth;
403 // Now we are ready to cycle over gene trees
404 for (w=0; w<numGeneTrees; w++)
407 printf ("\nGene %d\n",w);
408 ShowTree(geneTrees[w]);
411 // Set species sets for interior nodes. O(n)
412 for (i=0; i<geneTrees[w]->nIntNodes; i++) {
413 p = geneTrees[w]->intDownPass[i];
414 for (j=0; j<nLongsNeeded; j++)
415 speciesSets[p->index][j] = speciesSets[p->left->index][j] | speciesSets[p->right->index][j];
418 // Now order the interior nodes in terms of node depth. We rely on the fact that the
419 // ordered sequence is a valid downpass sequence. O(log n).
420 qsort((void *)(geneTrees[w]->intDownPass), (size_t)(geneTrees[w]->nIntNodes), sizeof(TreeNode *), CompareNodes);
422 // Finally find the minimum for each cell in the upper triangular matrix
423 // This is the time critical step with complexity O(n^3) in the simplest
424 // algorithm version. This algorithm should do a little better in most cases.
425 for (i=0; i<numUpperTriang; i++) {
427 // Find shallowest node that has the pair
428 for (j=0; j<geneTrees[w]->nIntNodes; j++) {
429 p = geneTrees[w]->intDownPass[j];
431 // Because nodes are ordered in time, if this test is true then we cannot beat the minimum
432 if (p->nodeDepth > depthMatrix[i])
435 // Check whether the node is a candidate minimum for the species pair
436 // If the test is true, we know from the test above that p->nodeDepth is
437 // either a tie or the new minimum
438 if (IsPartNested(speciesPairSets[i], speciesSets[p->index], nLongsNeeded) == YES) {
439 depthMatrix[i] = p->nodeDepth;
449 printf ("Mindepth matrix\n");
450 for (i=0;i<numSpecies;i++) {
453 for (j=i+1;j<numSpecies;j++) {
454 printf ("%.6f ",depthMatrix[index]);
462 free (speciesSets[0]);
469 /**---------------------------------------------------------------------
471 | GetSpeciesTreeFromMinDepths: converted from GetConstraints, Startsptree,
474 | This is a clustering algorithm based on minimum depths for species pairs.
475 | It reduces an n choose 2 upper triangular min depth matrix to an array
476 | of n-1 node depths, which fit onto a tree.
478 | @param speciesTree The species tree to be filled (out)
479 | @param depthMatrix The min depth matrix, upper triangular array (in)
480 | @returns Returns NO_ERROR if success, ERROR if negative brlens occur
481 ----------------------------------------------------------------------*/
482 int GetSpeciesTreeFromMinDepths (Tree* speciesTree, double *depthMatrix) {
484 int i, j, numUpperTriang, nLongsNeeded, index, nextNodeIndex;
487 PolyNode *p, *q, *r, *u, *qPrev, *rPrev;
489 nLongsNeeded = ((numSpecies - 1) / nBitsInALong) + 1;
490 numUpperTriang = numSpecies*(numSpecies - 1) / 2;
491 minDepth = (Depth *) SafeCalloc (numUpperTriang, sizeof(Depth));
493 // Convert depthMatrix to an array of Depth structs
495 for (i=0; i<numSpecies; i++) {
496 for (j=i+1; j<numSpecies; j++) {
497 minDepth[index].depth = depthMatrix[index];
498 minDepth[index].pairSet = speciesPairSets[index];
503 // Sort the array of distance structs (O(log n^2))
504 qsort((void *)(minDepth), (size_t)(numUpperTriang), sizeof(Depth), CompareDepths);
506 // The algorithm below reduces the upper triangular matrix (n choose 2) to an n-1
507 // array in O(n^2log(n)) time. We build the tree at the same time, since we can
508 // find included pairs in the tree in log(n) time. We use a polytomous tree for this.
510 // Allocate space for polytomous tree and set up partitions
511 polyTree = AllocatePolyTree(numSpecies);
512 AllocatePolyTreePartitions(polyTree);
514 // Build initial tree (a bush)
515 polyTree->isRooted = YES;
516 polyTree->isClock = YES;
517 polyTree->root = &polyTree->nodes[2*numSpecies-2];
518 for (i=0; i<numSpecies; i++) {
519 p = &polyTree->nodes[i];
524 p->sib = &polyTree->nodes[i+1];
527 p->anc = polyTree->root;
530 p->index = 2*numSpecies - 2;
531 p->left = &polyTree->nodes[0];
535 polyTree->nNodes = numSpecies + 1;
536 polyTree->nIntNodes = 1;
537 GetPolyDownPass(polyTree);
538 ResetPolyTreePartitions(polyTree); /* set bitsets (partitions) for initial tree */
540 // Resolve bush using sorted depth structs
541 nextNodeIndex = numSpecies;
542 for (i=0; i<numUpperTriang; i++) {
544 // Find tip corresponding to first taxon in pair
545 p = &polyTree->nodes[FirstTaxonInPartition(minDepth[i].pairSet, nLongsNeeded)];
547 // Descend tree until we find a node within which the pair set is nested
551 while (!IsPartNested(minDepth[i].pairSet, p->partition, nLongsNeeded));
553 if (p->left->sib->sib != NULL) {
554 // This node is still a polytomy
556 // Find left and right descendants of new node
558 for (q=p->left; IsSectionEmpty(q->partition, minDepth[i].pairSet, nLongsNeeded); q=q->sib)
561 for (r=q->sib; IsSectionEmpty(r->partition, minDepth[i].pairSet, nLongsNeeded); r=r->sib)
564 // Introduce the new node
565 u = &polyTree->nodes[nextNodeIndex];
566 u->index = nextNodeIndex;
568 polyTree->nIntNodes++;
576 // former upstream sibling to r should point to r->sib
585 u->depth = minDepth[i].depth; // because minDepth structs are sorted, we know this is the min depth
586 assert (u->depth > 0.0);
588 // Create new taxon set with bitfield operations
589 for (j=0; j<nLongsNeeded; j++)
590 u->partition[j] = q->partition[j] | r->partition[j];
592 // Patch the tree together with the new node added
598 else if (p == polyTree->root && p->depth < 0.0) {
599 // This is the first time we hit the root of the tree && it is resolved
600 p->depth = minDepth[i].depth;
601 assert (p->depth > 0.0);
603 // other cases should not be added to tree
606 // Make sure we have a complete species tree
607 assert (polyTree->nIntNodes == numSpecies - 1);
609 // Set traversal sequences
610 GetPolyDownPass(polyTree);
612 // Set branch lengths from node depths (not done automatically for us)
613 // Make sure all branch lengths are nonnegative (we can have 0.0 brlens, they
614 // should not be problematic in a species tree; they occur when there are
615 // ties in the min depth matrix that have not been modified by the move)
616 for (i=0; i<polyTree->nNodes; i++) {
617 p = polyTree->allDownPass[i];
621 p->length = p->anc->depth - p->depth;
622 if (p->length < 0.0) {
623 FreePolyTree(polyTree);
629 // Copy to species tree from polytomous tree
630 CopyToSpeciesTreeFromPolyTree (speciesTree, polyTree);
632 // Free locally allocated variables
633 FreePolyTree(polyTree);
640 /**---------------------------------------------------------------------------------------
642 | IsSpeciesTreeConsistent: Called when user tries to set a species tree or when
643 | attempting to use a species tree from a check point as starting value.
645 -----------------------------------------------------------------------------------------*/
646 int IsSpeciesTreeConsistent (Tree *speciesTree, int chain)
648 int i, answer, numGeneTrees, numUpperTriang, freeBestVars;
649 double *speciesTreeDepthMatrix;
655 if (memAllocs[ALLOC_BEST] == NO)
657 AllocateBestChainVariables();
661 numGeneTrees = numTrees - 1;
662 geneTrees = (Tree **) SafeCalloc (numGeneTrees, sizeof(Tree *));
663 for (i=0; i<numTrees-1; i++)
664 geneTrees[i] = GetTreeFromIndex(i, chain, state[chain]);
666 numUpperTriang = numSpecies * (numSpecies - 1) / 2;
667 speciesTreeDepthMatrix = (double *) SafeCalloc (numUpperTriang, sizeof(double));
669 GetMinDepthMatrix(geneTrees, numGeneTrees, depthMatrix);
670 GetDepthMatrix(speciesTree, speciesTreeDepthMatrix);
672 for (i=0; i<numUpperTriang; i++)
674 if (depthMatrix[i] < speciesTreeDepthMatrix[i])
678 if (i == numUpperTriang)
684 ShowNodes(speciesTree->root, 0, YES);
686 if (freeBestVars == YES)
687 FreeBestChainVariables();
689 free (speciesTreeDepthMatrix);
696 /**---------------------------------------------------------------------------------------
698 | LineagesIn: Recursive function to get number of gene tree lineages coming into each
699 | branch of the species tree (in ->x of speciestree nodes). We also assign each gene
700 | tree lineage to the corresponding species tree lineage (in ->x of genetree nodes).
701 | Finally, number of coalescent events is recorded (in ->y of speciestree nodes).
702 | Time complexity is O(n).
704 -----------------------------------------------------------------------------------------*/
705 void LineagesIn (TreeNode *geneTreeNode, TreeNode *speciesTreeNode)
709 if (geneTreeNode->nodeDepth < speciesTreeNode->nodeDepth) {
710 // climb up species tree
711 if (speciesTreeNode->left == NULL) {
712 assert (geneTreeNode->left == NULL);
713 speciesTreeNode->x++;
716 nLongsNeeded = (numSpecies - 1) / nBitsInALong + 1;
717 speciesTreeNode->x++;
718 if (IsPartNested(geneTreeNode->partition, speciesTreeNode->left->partition, nLongsNeeded) == YES)
719 LineagesIn (geneTreeNode, speciesTreeNode->left);
720 else if (IsPartNested(geneTreeNode->partition, speciesTreeNode->right->partition, nLongsNeeded) == YES)
721 LineagesIn (geneTreeNode, speciesTreeNode->right);
725 // climb up gene tree
726 if (geneTreeNode->left != NULL)
727 LineagesIn(geneTreeNode->left, speciesTreeNode);
728 if (geneTreeNode->right != NULL)
729 LineagesIn(geneTreeNode->right, speciesTreeNode);
730 if (geneTreeNode->left == NULL) {
731 speciesTreeNode->x++;
732 assert (speciesTreeNode->left == NULL);
735 speciesTreeNode->y++;
737 geneTreeNode->x = speciesTreeNode->index;
742 /**-----------------------------------------------------------------
744 | LnSpeciesTreeProb: Wrapper for LnJointGeneTreeSpeciesTreePr to
745 | free calling functions from retrieving gene and species trees.
747 ------------------------------------------------------------------*/
748 double LnSpeciesTreeProb(int chain)
752 Tree **geneTrees, *speciesTree;
755 m = &modelSettings[0];
757 speciesTree = GetTree(m->speciesTree, chain, state[chain]);
759 numGeneTrees = m->speciesTree->nSubParams;
760 geneTrees = (Tree **) SafeCalloc (numGeneTrees, sizeof(Tree *));
762 for (i=0; i<m->speciesTree->nSubParams; i++)
763 geneTrees[i] = GetTree(m->speciesTree->subParams[i], chain, state[chain]);
765 lnProb = LnJointGeneTreeSpeciesTreePr(geneTrees, numGeneTrees, speciesTree, chain);
773 /**-----------------------------------------------------------------
775 | LnJointGeneTreeSpeciesTreePr: Converted from LnJointGenetreePr,
776 | SPLogLike, SPLogPrior.
778 | In this function we calculate the entire probability of the species
779 | tree, including its probability given its priors, and the probability
780 | of the gene trees given the species tree.
782 ------------------------------------------------------------------*/
783 double LnJointGeneTreeSpeciesTreePr(Tree **geneTrees, int numGeneTrees, Tree *speciesTree, int chain)
785 double lnPrior, lnLike, clockRate, mu, *popSizePtr, sR, eR, sF;
790 // Get model info for species tree
791 m = &modelSettings[speciesTree->relParts[0]];
793 // Get model params for species tree
794 mp = &modelParams[speciesTree->relParts[0]];
797 popSizePtr = GetParamVals(m->popSize, chain, state[chain]);
800 if (speciesTree->isCalibrated == YES)
801 clockRate = *GetParamVals(m->clockRate, chain, state[chain]);
805 // Calculate probability of gene trees given species tree
806 // ShowNodes(speciesTree->root, 0, YES);
809 for (i=0; i<numGeneTrees; i++) {
810 lnLike += LnPriorProbGeneTree(geneTrees[i], mu, speciesTree, popSizePtr);
813 // Calculate probability of species tree given its priors
814 if (strcmp(mp->speciesTreeBrlensPr, "Birthdeath") == 0) {
815 sR = *GetParamVals(m->speciationRates, chain, state[chain]);
816 eR = *GetParamVals(m->extinctionRates, chain, state[chain]);
819 LnBirthDeathPriorPr(speciesTree, clockRate, &lnPrior, sR, eR, mp->sampleStrat, sF);
824 // The population size is taken care of elsewhere
826 return lnLike + lnPrior;
830 /**-----------------------------------------------------------------
832 | LnPriorProbGeneTree: Calculate the prior probability of a gene
835 ------------------------------------------------------------------*/
836 double LnPriorProbGeneTree (Tree *geneTree, double mu, Tree *speciesTree, double *popSizePtr)
838 int i, k, index, nEvents, trace=0;
839 double N, lnProb, ploidyFactor, theta, timeInterval;
840 TreeNode *p, *q=NULL, *r;
844 mp = &modelParams[speciesTree->relParts[0]];
846 // Find ploidy setting
847 if (strcmp(mp->ploidy, "Diploid") == 0)
849 else if (strcmp(mp->ploidy, "Haploid") == 0)
851 else /* if (strcmp(mp->ploidy, "Zlinked") == 0) */
854 // Initialize species tree with theta in d
855 for (i=0; i<speciesTree->nNodes-1; i++) {
856 p = speciesTree->allDownPass[i];
857 if (strcmp(mp->popVarPr, "Equal") != 0)
858 N = popSizePtr[p->index];
861 p->d = ploidyFactor * N * mu;
864 // Map gene tree to species tree
865 MapGeneTreeToSpeciesTree(geneTree, speciesTree);
867 // Sort gene tree interior nodes first by speciestree branch on which they coalesce, then in time order
868 qsort((void *)(geneTree->intDownPass), (size_t)(geneTree->nIntNodes), sizeof(TreeNode *), CompareNodesByX);
870 // Debug output of qsort result
872 printf ("index -- x -- nodeDepth for gene tree\n");
873 for (i=0; i<geneTree->nIntNodes; i++)
874 printf ("%d -- %d -- %e\n", geneTree->intDownPass[i]->index, geneTree->intDownPass[i]->x, geneTree->intDownPass[i]->nodeDepth);
877 // Now calculate probability after making sure species tree nodes appear in index order
878 // (the order does not have to be a correct downpass sequence)
879 for (i=0; i<speciesTree->memNodes; i++)
881 p = &(speciesTree->nodes[i]);
882 speciesTree->allDownPass[p->index] = p;
886 for (i=0; i<speciesTree->nNodes-1; i++) {
888 p = speciesTree->allDownPass[i];
893 // Get number of events
896 // Calculate probability
897 lnProb += nEvents * log (2.0 / theta);
899 for (k=p->x; k > p->x - p->y; k--) {
901 q = geneTree->intDownPass[index];
902 assert (q->x == p->index);
905 timeInterval = q->nodeDepth - p->nodeDepth;
907 r = geneTree->intDownPass[index-1];
908 timeInterval = q->nodeDepth - r->nodeDepth;
911 lnProb -= (k * (k - 1) * timeInterval) / theta;
915 if (p->x - p->y > 1) {
918 timeInterval = p->anc->nodeDepth - p->nodeDepth;
920 timeInterval = p->anc->nodeDepth - q->nodeDepth;
922 assert (p->anc->anc != NULL);
923 assert (timeInterval >= 0.0);
926 lnProb -= (k * (k - 1) * timeInterval) / theta;
930 // Restore downpass sequences (probably not necessary for gene tree, but may be if some
931 // code relies on intDownPass and allDownPass to be in same order)
932 GetDownPass(speciesTree);
933 GetDownPass(geneTree);
936 FreeTreePartitions(speciesTree);
937 FreeTreePartitions(geneTree);
943 /**---------------------------------------------------------------------
945 | LnProposalProbSpeciesTree:
947 | This algorithm calculates the probability of proposing a particular
948 | species tree given a distance matrix modified using a scheme based on
949 | truncated exponential distributions with rate expRate.
951 | @param speciesTree The species tree (in)
952 | @param depthMatrix The minimum depth matrix, upper triangular array (in)
953 | @param expRate Rate of truncated exponential distribution
954 | @returns Returns probability of proposing the species tree
955 ----------------------------------------------------------------------*/
956 double LnProposalProbSpeciesTree (Tree *speciesTree, double *depthMatrix, double expRate)
958 int i, left, right, index, nLongsNeeded, freeBitsets;
959 double dist, normConst=1.0, negLambdaX=0.0, eNegLambdaX, density, prob,
960 sumDensRatio, prodProb, lnProb;
963 // Make sure we have bitfields allocated and set
965 if (speciesTree->bitsets == NULL)
969 AllocateTreePartitions(speciesTree);
971 // Number of longs needed in a bitfield representing a species set
972 nLongsNeeded = ((numSpecies -1) / nBitsInALong) + 1;
974 // Loop over interior nodes
976 for (i=0; i<speciesTree->nIntNodes; i++)
978 p = speciesTree->intDownPass[i];
982 for (left = FirstTaxonInPartition(p->left->partition, nLongsNeeded); left < numSpecies; left = NextTaxonInPartition(left, p->left->partition, nLongsNeeded))
984 for (right = FirstTaxonInPartition(p->right->partition, nLongsNeeded); right < numSpecies; right = NextTaxonInPartition(right, p->right->partition, nLongsNeeded))
987 index = UpperTriangIndex(left, right, numSpecies); assert (index < numSpecies*(numSpecies - 1) / 2);
988 dist = depthMatrix[index] - p->nodeDepth; // distance between depth matrix entry and actual species-tree node
989 normConst = 1.0 - exp(-expRate * depthMatrix[index]); // normalization constant because of truncation of exp distribution
990 negLambdaX = - expRate * dist;
991 eNegLambdaX = exp(negLambdaX);
992 density = expRate * eNegLambdaX / normConst; // density for x == dist, f(dist)
993 prob = (1.0 - eNegLambdaX) / normConst; // cumulative prob for x <= dist, F(dist)
994 sumDensRatio += density / prob; // warning: if dist==0, prob is ZERO!
999 lnProb += log(expRate) + negLambdaX - log(normConst);
1001 lnProb += log(sumDensRatio * prodProb);
1004 // to avoid lnProposalProb is NaN at initial steps
1005 if (lnProb != lnProb) lnProb = 0.0;
1007 // Free partitions if appropriate
1008 if (freeBitsets == YES)
1009 FreeTreePartitions(speciesTree);
1015 /**-----------------------------------------------------------------
1017 | MapGeneTreeToSpeciesTree: Fold gene tree into species tree. We
1018 | are going to use ->x of gene tree to give index of the
1019 | corresponding node in the species tree. ->x in the species
1020 | tree will give the number of lineages into the corresponding
1021 | branch, and ->y will give the number of coalescent events on
1024 ------------------------------------------------------------------*/
1025 void MapGeneTreeToSpeciesTree (Tree *geneTree, Tree *speciesTree)
1027 int i, j, nLongsNeeded, trace=0;
1030 // Initialize species partitions for both gene tree and species tree
1031 // This will set the partitions to reflect the partitions in the tree itself,
1032 // which is OK for the species tree, but we want the gene tree partitions to
1033 // reflect the species partitions and not the gene partitions, so we need to
1035 AllocateTreePartitions(geneTree);
1036 AllocateTreePartitions(speciesTree);
1037 nLongsNeeded = (numSpecies - 1) / nBitsInALong + 1;
1038 for (i=0; i<geneTree->nNodes-1; i++) {
1039 p = geneTree->allDownPass[i];
1040 ClearBits(p->partition, nLongsNeeded);
1041 if (p->left == NULL)
1042 SetBit(speciespartitionId[p->index][speciespartitionNum]-1, p->partition);
1044 for (j=0; j<nLongsNeeded; j++)
1045 p->partition[j] = p->left->partition[j] | p->right->partition[j];
1048 // Species tree partitions already set by call to AllocateTreePartitions
1050 // Reset ->x and ->y of species tree (->x of gene tree does not need to be initialized)
1051 for (i=0; i<speciesTree->nNodes; i++)
1053 p = speciesTree->allDownPass[i];
1058 // Call recursive function to match gene tree and species tree
1059 LineagesIn(geneTree->root->left, speciesTree->root->left);
1062 printf ("index -- x -- y for species tree\n");
1063 for (i=0; i<speciesTree->nNodes-1; i++)
1064 printf ("%-2d -- %d -- %d\n", speciesTree->allDownPass[i]->index, speciesTree->allDownPass[i]->x, speciesTree->allDownPass[i]->y);
1068 printf ("index -- x -- nodeDepth for gene tree\n");
1069 for (i=0; i<geneTree->nIntNodes; i++)
1070 printf ("%-2d -- %d -- %e\n", geneTree->intDownPass[i]->index, geneTree->intDownPass[i]->x, geneTree->intDownPass[i]->nodeDepth);
1074 FreeTreePartitions(speciesTree);
1075 FreeTreePartitions(geneTree);
1079 /**---------------------------------------------------------------------
1081 | ModifyDepthMatrix:
1083 | This algorithm uses a truncated exponential distribution to modify
1086 | @param expRate The rate of the exponential distribution (in)
1087 | @param depthMatrix The minimum depth matrix to be modified, upper triangular array (in/out)
1088 | @param seed Pointer to seed for random number generator (in/ut)
1089 | @returns Returns ERROR or NO_ERROR
1090 ----------------------------------------------------------------------*/
1091 int ModifyDepthMatrix (double expRate, double *depthMatrix, RandLong *seed)
1093 int i, numUpperTriang;
1094 double u, interval, delta;
1096 numUpperTriang = numSpecies * (numSpecies - 1) / 2;
1097 for (i=0; i<numUpperTriang; i++)
1099 interval = depthMatrix[i];
1100 u = RandomNumber(seed);
1101 delta = log (1.0 - u*(1.0 - exp(-expRate*interval))) / (-expRate);
1102 assert (delta <= interval);
1103 depthMatrix[i] -= delta;
1110 /**-----------------------------------------------------------------
1112 | Move_GeneTree1: Propose a new gene tree using ExtSPRClock
1114 | @param param The parameter (gene tree) to change
1115 | @param chain The chain number
1116 | @param seed Pointer to the seed of the random number gen.
1117 | @param lnPriorRatio Pointer to the log prior ratio (out)
1118 | @param lnProposalRatio Pointer to the log proposal (Hastings) ratio (out)
1119 | @param mvp Pointer to tuning parameter(s)
1120 ------------------------------------------------------------------*/
1121 int Move_GeneTree1 (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
1123 int i, numGeneTrees, numUpperTriang;
1124 double newLnProb, oldLnProb, backwardLnProposalProb, forwardLnProposalProb,
1125 *oldMinDepths, *modMinDepths, forwardLambda, backwardLambda, mean;
1126 Tree *newSpeciesTree, *oldSpeciesTree, **geneTrees;
1129 // Calculate number of gene trees
1130 numGeneTrees = numTopologies - 1;
1132 // Get model settings
1133 m = &modelSettings[param->relParts[0]];
1135 // Get species tree (this trick is possible because we always copy tree params)
1136 newSpeciesTree = GetTree (m->speciesTree, chain, state[chain]);
1137 oldSpeciesTree = GetTree (m->speciesTree, chain, state[chain] ^ 1);
1140 geneTrees = (Tree **) SafeCalloc (2*numGeneTrees, sizeof(Tree *));
1141 for (i=0; i<m->speciesTree->nSubParams; i++) {
1142 geneTrees[i] = GetTree(m->speciesTree->subParams[i], chain, state[chain]);
1145 // Allocate space for depth matrix copy
1146 numUpperTriang = numSpecies * (numSpecies - 1) / 2;
1147 oldMinDepths = (double *) SafeCalloc (2*numUpperTriang, sizeof(double));
1148 modMinDepths = oldMinDepths + numUpperTriang;
1150 // Get min depth matrix for old gene trees
1151 GetMinDepthMatrix(geneTrees, numTopologies-1, depthMatrix);
1154 for (i=0; i<numUpperTriang; i++)
1155 oldMinDepths[i] = depthMatrix[i];
1157 // Get forward lambda
1158 GetMeanDist(oldSpeciesTree, depthMatrix, &mean);
1159 // if (mean < 1E-6) mean = 1E-6;
1160 forwardLambda = 1.0 / mean;
1162 // Calculate joint probability of old gene trees and old species tree
1163 oldLnProb = LnJointGeneTreeSpeciesTreePr(geneTrees, numGeneTrees, oldSpeciesTree, chain);
1165 // Modify the picked gene tree using code from a regular MrBayes move
1166 Move_ExtSPRClock(param, chain, seed, lnPriorRatio, lnProposalRatio, mvp);
1168 // Update the min depth matrix
1169 GetMinDepthMatrix(geneTrees, numTopologies-1, depthMatrix);
1171 // Copy the min depth matrix
1172 for (i=0; i<numUpperTriang; i++)
1173 modMinDepths[i] = depthMatrix[i];
1175 // Modify the min depth matrix
1176 ModifyDepthMatrix (forwardLambda, modMinDepths, seed);
1178 // Get a new species tree
1179 if (GetSpeciesTreeFromMinDepths (newSpeciesTree, modMinDepths) == ERROR) {
1182 free (oldMinDepths);
1186 // Calculate joint probability of new gene trees and new species tree
1187 newLnProb = LnJointGeneTreeSpeciesTreePr(geneTrees, numGeneTrees, newSpeciesTree, chain);
1189 // Get backward lambda
1190 GetMeanDist(newSpeciesTree, depthMatrix, &mean);
1191 // if (mean < 1E-6) mean = 1E-6;
1192 backwardLambda = 1.0 / mean;
1194 // Get proposal probability of old species tree
1195 backwardLnProposalProb = LnProposalProbSpeciesTree (oldSpeciesTree, oldMinDepths, backwardLambda);
1197 // Get proposal probability of new species tree
1198 forwardLnProposalProb = LnProposalProbSpeciesTree (newSpeciesTree, depthMatrix, forwardLambda);
1200 // Update prior ratio taking species tree into account
1201 (*lnPriorRatio) += (newLnProb - oldLnProb);
1203 // Update proposal ratio based on this move
1204 (*lnProposalRatio) += (backwardLnProposalProb - forwardLnProposalProb);
1206 // Free allocated memory
1208 free (oldMinDepths);
1214 /**-----------------------------------------------------------------
1216 | Move_GeneTree2: Propose a new gene tree using NNIClock
1218 | @param param The parameter to change
1219 | @param chain The chain number
1220 | @param seed Pointer to the seed of the random number gen.
1221 | @param lnPriorRatio Pointer to the log prior ratio (out)
1222 | @param lnProposalRatio Pointer to the log proposal (Hastings) ratio (out)
1223 | @param mvp Pointer to tuning parameter(s)
1224 ------------------------------------------------------------------*/
1225 int Move_GeneTree2 (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
1227 int i, numGeneTrees, numUpperTriang;
1228 double newLnProb, oldLnProb, backwardLnProposalProb, forwardLnProposalProb,
1229 *oldMinDepths, *modMinDepths, forwardLambda, backwardLambda, mean;
1230 Tree *newSpeciesTree, *oldSpeciesTree, **geneTrees;
1233 // Calculate number of gene trees
1234 numGeneTrees = numTopologies - 1;
1236 // Get model settings
1237 m = &modelSettings[param->relParts[0]];
1239 // Get species tree (this trick is possible because we always copy tree params)
1240 newSpeciesTree = GetTree (m->speciesTree, chain, state[chain]);
1241 oldSpeciesTree = GetTree (m->speciesTree, chain, state[chain] ^ 1);
1244 geneTrees = (Tree **) SafeCalloc (2*numGeneTrees, sizeof(Tree *));
1245 for (i=0; i<m->speciesTree->nSubParams; i++) {
1246 geneTrees[i] = GetTree(m->speciesTree->subParams[i], chain, state[chain]);
1249 // Allocate space for depth matrix copy
1250 numUpperTriang = numSpecies * (numSpecies - 1) / 2;
1251 oldMinDepths = (double *) SafeCalloc (2*numUpperTriang, sizeof(double));
1252 modMinDepths = oldMinDepths + numUpperTriang;
1254 // Get min depth matrix for old gene trees
1255 GetMinDepthMatrix(geneTrees, numTopologies-1, depthMatrix);
1258 for (i=0; i<numUpperTriang; i++)
1259 oldMinDepths[i] = depthMatrix[i];
1261 // Get forward lambda
1262 GetMeanDist(oldSpeciesTree, depthMatrix, &mean);
1263 // if (mean < 1E-6) mean = 1E-6;
1264 forwardLambda = 1.0 / mean;
1266 // Calculate joint probability of old gene trees and old species tree
1267 oldLnProb = LnJointGeneTreeSpeciesTreePr(geneTrees, numGeneTrees, oldSpeciesTree, chain);
1269 // Modify the picked gene tree using code from a regular MrBayes move (no tuning parameter, so passing on mvp is OK)
1270 Move_NNIClock(param, chain, seed, lnPriorRatio, lnProposalRatio, mvp);
1272 // Update the min depth matrix
1273 GetMinDepthMatrix(geneTrees, numTopologies-1, depthMatrix);
1275 // Copy the min depth matrix
1276 for (i=0; i<numUpperTriang; i++)
1277 modMinDepths[i] = depthMatrix[i];
1279 // Modify the min depth matrix
1280 ModifyDepthMatrix (forwardLambda, modMinDepths, seed);
1282 // Get a new species tree
1283 if (GetSpeciesTreeFromMinDepths (newSpeciesTree, modMinDepths) == ERROR) {
1286 free (oldMinDepths);
1290 // Calculate joint probability of new gene trees and new species tree
1291 newLnProb = LnJointGeneTreeSpeciesTreePr(geneTrees, numGeneTrees, newSpeciesTree, chain);
1293 // Get backward lambda
1294 GetMeanDist(newSpeciesTree, depthMatrix, &mean);
1295 // if (mean < 1E-6) mean = 1E-6;
1296 backwardLambda = 1.0 / mean;
1298 // Get proposal probability of old species tree
1299 backwardLnProposalProb = LnProposalProbSpeciesTree (oldSpeciesTree, oldMinDepths, backwardLambda);
1301 // Get proposal probability of new species tree
1302 forwardLnProposalProb = LnProposalProbSpeciesTree (newSpeciesTree, depthMatrix, forwardLambda);
1304 // Update prior ratio taking species tree into account
1305 (*lnPriorRatio) += (newLnProb - oldLnProb);
1307 // Update proposal ratio based on this move
1308 (*lnProposalRatio) += (backwardLnProposalProb - forwardLnProposalProb);
1310 // Free allocated memory
1312 free (oldMinDepths);
1318 /**-----------------------------------------------------------------
1320 | Move_GeneTree3: Propose a new gene tree using ParsSPRClock
1322 | @param param The parameter to change
1323 | @param chain The chain number
1324 | @param seed Pointer to the seed of the random number gen.
1325 | @param lnPriorRatio Pointer to the log prior ratio (out)
1326 | @param lnProposalRatio Pointer to the log proposal (Hastings) ratio (out)
1327 | @param mvp Pointer to tuning parameter(s)
1328 ------------------------------------------------------------------*/
1329 int Move_GeneTree3 (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
1331 int i, numGeneTrees, numUpperTriang;
1332 double newLnProb, oldLnProb, backwardLnProposalProb, forwardLnProposalProb,
1333 *oldMinDepths, *modMinDepths, forwardLambda, backwardLambda, mean;
1334 Tree *newSpeciesTree, *oldSpeciesTree, **geneTrees;
1337 // Calculate number of gene trees
1338 numGeneTrees = numTopologies - 1;
1340 // Get model settings
1341 m = &modelSettings[param->relParts[0]];
1343 // Get species tree (this trick is possible because we always copy tree params)
1344 newSpeciesTree = GetTree (m->speciesTree, chain, state[chain]);
1345 oldSpeciesTree = GetTree (m->speciesTree, chain, state[chain] ^ 1);
1348 geneTrees = (Tree **) SafeCalloc (2*numGeneTrees, sizeof(Tree *));
1349 for (i=0; i<m->speciesTree->nSubParams; i++) {
1350 geneTrees[i] = GetTree(m->speciesTree->subParams[i], chain, state[chain]);
1353 // Allocate space for depth matrix copy
1354 numUpperTriang = numSpecies * (numSpecies - 1) / 2;
1355 oldMinDepths = (double *) SafeCalloc (2*numUpperTriang, sizeof(double));
1356 modMinDepths = oldMinDepths + numUpperTriang;
1358 // Get min depth matrix for old gene trees
1359 GetMinDepthMatrix(geneTrees, numTopologies-1, depthMatrix);
1362 for (i=0; i<numUpperTriang; i++)
1363 oldMinDepths[i] = depthMatrix[i];
1365 // Get forward lambda
1366 GetMeanDist(oldSpeciesTree, depthMatrix, &mean);
1367 // if (mean < 1E-6) mean = 1E-6;
1368 forwardLambda = 1.0 / mean;
1370 // Calculate joint probability of old gene trees and old species tree
1371 oldLnProb = LnJointGeneTreeSpeciesTreePr(geneTrees, numGeneTrees, oldSpeciesTree, chain);
1373 // Modify the picked gene tree using code from a regular MrBayes move
1374 Move_ParsSPRClock(param, chain, seed, lnPriorRatio, lnProposalRatio, mvp);
1376 // Update the min depth matrix
1377 GetMinDepthMatrix(geneTrees, numTopologies-1, depthMatrix);
1379 // Copy the min depth matrix
1380 for (i=0; i<numUpperTriang; i++)
1381 modMinDepths[i] = depthMatrix[i];
1383 // Modify the min depth matrix
1384 ModifyDepthMatrix (forwardLambda, modMinDepths, seed);
1386 // Get a new species tree
1387 if (GetSpeciesTreeFromMinDepths (newSpeciesTree, modMinDepths) == ERROR) {
1390 free (oldMinDepths);
1394 // Calculate joint probability of new gene trees and new species tree
1395 newLnProb = LnJointGeneTreeSpeciesTreePr(geneTrees, numGeneTrees, newSpeciesTree, chain);
1397 // Get backward lambda
1398 GetMeanDist(newSpeciesTree, depthMatrix, &mean);
1399 // if (mean < 1E-6) mean = 1E-6;
1400 backwardLambda = 1.0 / mean;
1402 // Get proposal probability of old species tree
1403 backwardLnProposalProb = LnProposalProbSpeciesTree (oldSpeciesTree, oldMinDepths, backwardLambda);
1405 // Get proposal probability of new species tree
1406 forwardLnProposalProb = LnProposalProbSpeciesTree (newSpeciesTree, depthMatrix, forwardLambda);
1408 // Update prior ratio taking species tree into account
1409 (*lnPriorRatio) += (newLnProb - oldLnProb);
1411 // Update proposal ratio based on this move
1412 (*lnProposalRatio) += (backwardLnProposalProb - forwardLnProposalProb);
1414 // Free allocated memory
1416 free (oldMinDepths);
1422 /*-----------------------------------------------------------------------------------
1424 | Move_NodeSliderGeneTree: Move the position of one (root or nonroot) node in a
1425 | gene tree inside a species tree.
1427 -------------------------------------------------------------------------------------*/
1428 int Move_NodeSliderGeneTree (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
1431 MrBFlt window, minDepth, maxDepth, oldDepth, newDepth,
1432 oldLeftLength=0.0, oldRightLength=0.0, clockRate,
1433 oldPLength=0.0, lambda=0.0, nu=0.0, igrvar=0.0,
1434 *brlens=NULL, *tk02Rate=NULL, *igrRate=NULL, *popSizePtr;
1437 Tree *geneTree, *speciesTree;
1440 window = mvp[0]; /* window size */
1442 m = &modelSettings[param->relParts[0]];
1444 /* get gene tree and species tree */
1445 geneTree = GetTree (param, chain, state[chain]);
1446 speciesTree = GetTree (m->speciesTree, chain, state[chain]);
1448 /* get population size(s) */
1449 popSizePtr = GetParamVals(m->popSize, chain, state[chain]);
1451 /* get clock rate */
1452 if (m->clockRate == NULL)
1455 clockRate = *GetParamVals(m->clockRate, chain, state[chain]);
1457 /* pick a node to be changed */
1458 p = geneTree->intDownPass[(int)(RandomNumber(seed)*geneTree->nIntNodes)];
1460 # if defined (DEBUG_CSLIDER)
1461 printf ("Before node slider (gene tree):\n");
1462 printf ("Picked branch with index %d and depth %f\n", p->index, p->nodeDepth);
1463 if (p->anc->anc == NULL)
1464 printf ("Old clock rate: %f\n", clockRate);
1465 ShowNodes (t->root, 0, t->isRooted);
1469 /* get gene tree prior prob before move */
1470 (*lnPriorRatio) -= LnPriorProbGeneTree(geneTree, clockRate, speciesTree, popSizePtr);
1472 /* store values needed later for prior calculation (relaxed clocks) */
1473 oldPLength = p->length;
1474 if (p->left != NULL)
1476 oldLeftLength = p->left->length;
1477 oldRightLength = p->right->length;
1480 oldLeftLength = oldRightLength = 0.0;
1482 /* find species tree branch to which the gene tree node belongs */
1483 MapGeneTreeToSpeciesTree(geneTree, speciesTree);
1485 for (i=0; i<speciesTree->nNodes-1; i++)
1487 q = speciesTree->allDownPass[i];
1488 if (p->x == q->index)
1491 assert (q != NULL && p->x == q->index);
1493 /* determine lower and upper bound */
1494 minDepth = p->left->nodeDepth + POS_MIN;
1495 if (p->right->nodeDepth + POS_MIN > minDepth)
1496 minDepth = p->right->nodeDepth + POS_MIN;
1497 if (q->nodeDepth + POS_MIN > minDepth)
1498 minDepth = q->nodeDepth + POS_MIN;
1499 if (p->anc->anc == NULL)
1500 maxDepth = TREEHEIGHT_MAX;
1502 maxDepth = p->anc->nodeDepth - POS_MIN;
1504 /* abort if impossible */
1505 if (minDepth >= maxDepth)
1511 if (maxDepth-minDepth < window)
1513 window = maxDepth-minDepth;
1516 /* pick the new node depth */
1517 oldDepth = p->nodeDepth;
1518 newDepth = oldDepth + (RandomNumber(seed) - 0.5) * window;
1520 /* reflect the new node depth */
1521 while (newDepth < minDepth || newDepth > maxDepth)
1523 if (newDepth < minDepth)
1524 newDepth = 2.0 * minDepth - newDepth;
1525 if (newDepth > maxDepth)
1526 newDepth = 2.0 * maxDepth - newDepth;
1528 p->nodeDepth = newDepth;
1530 /* determine new branch lengths around p and set update of transition probabilities */
1531 if (p->left != NULL)
1533 p->left->length = p->nodeDepth - p->left->nodeDepth;
1534 assert (p->left->length >= POS_MIN);
1535 p->left->upDateTi = YES;
1536 p->right->length = p->nodeDepth - p->right->nodeDepth;
1537 assert (p->right->length >= POS_MIN);
1538 p->right->upDateTi = YES;
1540 if (p->anc->anc != NULL)
1542 p->length = p->anc->nodeDepth - p->nodeDepth;
1543 assert (p->length >= POS_MIN);
1547 /* set flags for update of cond likes from p and down to root */
1549 while (q->anc != NULL)
1555 /* calculate proposal ratio */
1556 (*lnProposalRatio) = 0.0;
1558 /* calculate prior ratio */
1559 (*lnPriorRatio) += LnPriorProbGeneTree (geneTree, clockRate, speciesTree, popSizePtr);
1561 /* adjust proposal and prior ratio for relaxed clock models */
1562 for (i=0; i<param->nSubParams; i++)
1564 subParm = param->subParams[i];
1565 if (subParm->paramType == P_CPPEVENTS)
1567 nEvents = subParm->nEvents[2*chain+state[chain]];
1568 lambda = *GetParamVals (modelSettings[subParm->relParts[0]].cppRate, chain, state[chain]);
1570 /* proposal ratio */
1571 if (p->left != NULL)
1573 (*lnProposalRatio) += nEvents[p->left->index ] * log (p->left->length / oldLeftLength);
1574 (*lnProposalRatio) += nEvents[p->right->index] * log (p->right->length / oldRightLength);
1576 if (p->anc->anc != NULL)
1577 (*lnProposalRatio) += nEvents[p->index] * log (p->length / oldPLength);
1580 if (p->anc->anc == NULL) // two branches changed in same direction
1581 (*lnPriorRatio) += lambda * (2.0 * (oldDepth - newDepth));
1582 else if (p->left != NULL) // two branches changed in one direction, one branch in the other direction
1583 (*lnPriorRatio) += lambda * (oldDepth - newDepth);
1584 else /* if (p->left == NULL) */ // one branch changed
1585 (*lnPriorRatio) += lambda * (newDepth - oldDepth);
1587 /* update effective evolutionary lengths */
1588 if (UpdateCppEvolLengths (subParm, p, chain) == ERROR)
1594 else if ( subParm->paramType == P_TK02BRANCHRATES ||
1595 (subParm->paramType == P_MIXEDBRCHRATES && *GetParamIntVals(subParm, chain, state[chain]) == RCL_TK02))
1597 if (subParm->paramType == P_TK02BRANCHRATES)
1598 nu = *GetParamVals (modelSettings[subParm->relParts[0]].tk02var, chain, state[chain]);
1600 nu = *GetParamVals (modelSettings[subParm->relParts[0]].mixedvar, chain, state[chain]);
1601 tk02Rate = GetParamVals (subParm, chain, state[chain]);
1602 brlens = GetParamSubVals (subParm, chain, state[chain]);
1604 /* no proposal ratio effect */
1607 if (p->left != NULL)
1609 (*lnPriorRatio) -= LnProbTK02LogNormal (tk02Rate[p->index], nu*oldLeftLength, tk02Rate[p->left->index]);
1610 (*lnPriorRatio) -= LnProbTK02LogNormal (tk02Rate[p->index], nu*oldRightLength, tk02Rate[p->right->index]);
1611 (*lnPriorRatio) += LnProbTK02LogNormal (tk02Rate[p->index], nu*p->left->length, tk02Rate[p->left->index]);
1612 (*lnPriorRatio) += LnProbTK02LogNormal (tk02Rate[p->index], nu*p->right->length, tk02Rate[p->right->index]);
1614 if (p->anc->anc != NULL)
1616 (*lnPriorRatio) -= LnProbTK02LogNormal (tk02Rate[p->anc->index], nu*oldPLength, tk02Rate[p->index]);
1617 (*lnPriorRatio) += LnProbTK02LogNormal (tk02Rate[p->anc->index], nu*p->length, tk02Rate[p->index]);
1620 /* update effective evolutionary lengths */
1621 if (p->left != NULL)
1623 brlens[p->left->index] = p->left->length * (tk02Rate[p->left->index]+tk02Rate[p->index])/2.0;
1624 brlens[p->right->index] = p->right->length * (tk02Rate[p->right->index]+tk02Rate[p->index])/2.0;
1626 if (p->anc->anc != NULL)
1628 brlens[p->index] = p->length * (tk02Rate[p->index]+tk02Rate[p->anc->index])/2.0;
1631 else if ( subParm->paramType == P_IGRBRANCHRATES ||
1632 (subParm->paramType == P_MIXEDBRCHRATES && *GetParamIntVals(subParm, chain, state[chain]) == RCL_IGR))
1634 if (subParm->paramType == P_IGRBRANCHRATES)
1635 igrvar = *GetParamVals (modelSettings[subParm->relParts[0]].igrvar, chain, state[chain]);
1637 igrvar = *GetParamVals (modelSettings[subParm->relParts[0]].mixedvar, chain, state[chain]);
1638 igrRate = GetParamVals (subParm, chain, state[chain]);
1639 brlens = GetParamSubVals (subParm, chain, state[chain]);
1641 if (p->left != NULL)
1643 (*lnPriorRatio) -= LnProbGamma (oldLeftLength/igrvar, oldLeftLength/igrvar, igrRate[p->left->index ]);
1644 (*lnPriorRatio) -= LnProbGamma (oldRightLength/igrvar, oldRightLength/igrvar, igrRate[p->right->index]);
1645 (*lnPriorRatio) += LnProbGamma (p->left->length/igrvar, p->left->length/igrvar, igrRate[p->left->index ]);
1646 (*lnPriorRatio) += LnProbGamma (p->right->length/igrvar, p->right->length/igrvar, igrRate[p->right->index]);
1648 if (p->anc->anc != NULL)
1650 (*lnPriorRatio) -= LnProbGamma (oldPLength/igrvar, oldPLength/igrvar, igrRate[p->index]);
1651 (*lnPriorRatio) += LnProbGamma (p->length /igrvar, p->length /igrvar, igrRate[p->index]);
1654 if (p->left != NULL)
1656 brlens[p->left->index ] = igrRate[p->left->index ] * p->left->length;
1657 brlens[p->right->index] = igrRate[p->right->index] * p->right->length;
1659 if (p->anc->anc != NULL)
1661 brlens[p->index] = igrRate[p->index] * p->length;
1666 # if defined (DEBUG_CSLIDER)
1667 printf ("After node slider (gene tree):\n");
1668 printf ("Old depth: %f -- New depth: %f -- LnPriorRatio %f -- LnProposalRatio %f\n",
1669 oldDepth, newDepth, (*lnPriorRatio), (*lnProposalRatio));
1670 ShowNodes (t->root, 0, t->isRooted);
1679 /*------------------------------------------------------------------
1681 | Move_SpeciesTree: Propose a new species tree
1683 ------------------------------------------------------------------*/
1684 int Move_SpeciesTree (Param *param, int chain, RandLong *seed, MrBFlt *lnPriorRatio, MrBFlt *lnProposalRatio, MrBFlt *mvp)
1686 int i, numGeneTrees, numUpperTriang;
1687 double newLnProb, oldLnProb, backwardLnProposalProb, forwardLnProposalProb, *modMinDepths,
1688 forwardLambda, backwardLambda, lambdaDiv, mean;
1689 Tree *newSpeciesTree, *oldSpeciesTree, **geneTrees;
1692 /* get tuning parameter (lambda divider) */
1695 /* calculate number of gene trees */
1696 numGeneTrees = param->nSubParams;
1698 /* get model settings */
1699 m = &modelSettings[param->relParts[0]];
1701 /* get new and old species trees */
1702 newSpeciesTree = GetTree (m->speciesTree, chain, state[chain]);
1703 oldSpeciesTree = GetTree (m->speciesTree, chain, state[chain] ^ 1);
1705 /* get gene trees */
1706 geneTrees = (Tree **) SafeCalloc (numGeneTrees, sizeof(Tree*));
1707 for (i=0; i<param->nSubParams; i++)
1708 geneTrees[i] = GetTree(param->subParams[i], chain, state[chain]);
1710 /* get minimum depth matrix */
1711 GetMinDepthMatrix(geneTrees, numGeneTrees, depthMatrix);
1713 /* get forward lambda */
1714 GetMeanDist(oldSpeciesTree, depthMatrix, &mean);
1715 forwardLambda = 1.0 / (mean * lambdaDiv);
1717 /* make a copy for modification */
1718 numUpperTriang = numSpecies * (numSpecies - 1) / 2;
1719 modMinDepths = (double *) SafeCalloc (numUpperTriang, sizeof(double));
1720 for (i=0; i<numUpperTriang; i++)
1721 modMinDepths[i] = depthMatrix[i];
1723 /* modify minimum depth matrix */
1724 ModifyDepthMatrix (forwardLambda, modMinDepths, seed);
1726 /* construct a new species tree from the modified constraints */
1727 if (GetSpeciesTreeFromMinDepths(newSpeciesTree, modMinDepths) == ERROR) {
1729 free (modMinDepths);
1734 /* get lambda for back move */
1735 GetMeanDist(newSpeciesTree, depthMatrix, &mean);
1736 backwardLambda = 1.0 / (mean * lambdaDiv);
1738 /* calculate proposal ratio */
1739 backwardLnProposalProb = LnProposalProbSpeciesTree (oldSpeciesTree, depthMatrix, backwardLambda);
1740 forwardLnProposalProb = LnProposalProbSpeciesTree (newSpeciesTree, depthMatrix, forwardLambda);
1741 (*lnProposalRatio) = backwardLnProposalProb - forwardLnProposalProb;
1743 # if defined (BEST_MPI_ENABLED)
1744 // Broadcast the proposed species tree to all processors if MPI version
1747 # if defined (BEST_MPI_ENABLED)
1748 // Let each processor calculate the ln probability ratio of its current gene tree(s)
1749 // given the new and old species tree in the MPI version
1751 // Assemble the ln probability ratios across the processors and to lnPriorRatio
1753 /* calculate the ln probability ratio of the current gene trees
1754 given the new and old species trees */
1755 newLnProb = LnJointGeneTreeSpeciesTreePr(geneTrees, numGeneTrees, newSpeciesTree, chain);
1756 oldLnProb = LnJointGeneTreeSpeciesTreePr(geneTrees, numGeneTrees, oldSpeciesTree, chain);
1759 /* set (*lnPriorRatio) to ln probability ratio */
1760 (*lnPriorRatio) = (newLnProb - oldLnProb);
1762 /* free allocated space */
1763 free (modMinDepths);
1770 /** Show upper triangular matrix */
1771 void ShowUpperTriangMatrix (double *values, int squareSize)
1776 printf ("Upper triang matrix:\n");
1777 for (i=0; i<squareSize; i++) {
1780 for (j=i+1; j<squareSize; j++) {
1781 printf ("%.6f ", values[index]);