Beispiel #1
0
edge *depthFirstTraverse(tree *T, edge *e)
     /*depthFirstTraverse returns the edge f which is least in T according
       to the depth-first order, but which is later than e in the search
       pattern.  If e is null, f is the least edge of T*/
{
  edge *f;
  if (NULL == e)
    {
      f = T->root->leftEdge;
      if (NULL != f)
	f = findBottomLeft(f);
      return(f);  /*this is the first edge of this search pattern*/
    }
  else /*e is non-null*/
    {
      if (e->tail->leftEdge == e)
	/*if e is a left-oriented edge, we skip the entire
	  tree cut below e, and find least edge*/
	f = moveRight(e);
      else  /*if e is a right-oriented edge, we have already looked at its
	      sibling and everything below e, so we move up*/
	f = e->tail->parentEdge;
    }
  return(f);
}
Beispiel #2
0
edge *moveRight(edge *e)
{
  edge *f;
  f = e->tail->rightEdge; /*this step moves from a left-oriented edge
			    to a right-oriented edge*/
  if (NULL != f)
    f = findBottomLeft(f);
  return(f);
}
Beispiel #3
0
void calcTBRaverages(tree *T, edge *esplit, double **A, double **dXTop)
{
  edge *ebottom, *par, *sib;
  for (ebottom = findBottomLeft(esplit); ebottom != esplit; ebottom = depthFirstTraverse(T,ebottom))
    {
      par = esplit->tail->parentEdge;
      sib = siblingEdge(esplit);
      calcTBRTopBottomAverage(ebottom->head,A, dXTop, 0.0, esplit, par,esplit,UP);
      calcTBRTopBottomAverage(ebottom->head,A, dXTop, 0.0, esplit, sib,esplit,DOWN);
    }
}
Beispiel #4
0
void TBR(tree *T, double **D, double **A)
{
  int i;
  edge *esplit, *etop, *eBestTop, *eBestBottom, *eBestSplit;
  edge *eout, *block;
  edge *left, *right, *par, *sib;
  double **dXTop; /*dXTop[i][j] is average distance from subtree rooted at i to tree above split edge, if
		    SPR above split edge cuts edge whose head has index j*/
  double bestWeight;
  double ***TBRWeights;
  dXTop = initDoubleMatrix(T->size);
  weighTree(T);
  TBRWeights = (double ***)calloc(T->size,sizeof(double **));
  for(i=0;i<T->size;i++)
    TBRWeights[i] = initDoubleMatrix(T->size);
  do
    {
      zero3DMatrix(TBRWeights,T->size,T->size,T->size);
      bestWeight = 0.0;
      eBestSplit = eBestTop = eBestBottom = NULL;
      for(esplit=depthFirstTraverse(T,NULL);NULL!=esplit;esplit=depthFirstTraverse(T,esplit))
	{
	  par = esplit->tail->parentEdge;
	  if (NULL != par)
	    {
	      sib = siblingEdge(esplit);
	      /*next two lines calculate the possible improvements for any SPR above esplit*/
	      assignTBRDownWeightsUp(par,esplit->head,sib->head,NULL,NULL,0.0,1.0,A,TBRWeights,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);
	      assignTBRDownWeightsSkew(sib,esplit->head,sib->tail,NULL,NULL,0.0,1.0,A,TBRWeights,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);
	      calcTBRaverages(T,esplit,A,dXTop); /*calculates the average distance from any subtree
						   below esplit to the entire subtree above esplit,
						   after any possible SPR above*/
	      /*for etop above esplit, we loop using information from above to calculate values
		for all possible SPRs below esplit*/
	    }

	  right = esplit->head->rightEdge;
	  if (NULL != right)
	    {
	      left = esplit->head->leftEdge;
	      /*test case: etop = null means only do bottom SPR*/
	      assignTBRUpWeights(left,esplit->head,right->head,NULL,NULL,0.0,1.0,A,dXTop,TBRWeights,NULL,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);
	      assignTBRUpWeights(right,esplit->head,left->head,NULL,NULL,0.0,1.0,A,dXTop,TBRWeights,NULL,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);

	      block = esplit;
	      while (NULL != block)
		{
		  if (block != esplit)
		    {
		      etop = block;
		      assignTBRUpWeights(left,esplit->head,right->head,NULL,NULL,0.0,1.0,A,dXTop,TBRWeights,etop,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);
		      assignTBRUpWeights(right,esplit->head,left->head,NULL,NULL,0.0,1.0,A,dXTop,TBRWeights,etop,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);
		    }
		  eout = siblingEdge(block);
		  if (NULL != eout)
		    {
		      etop = findBottomLeft(eout);
		      while (etop->tail != eout->tail)
			{
			  /*for ebottom below esplit*/

			  assignTBRUpWeights(left,esplit->head,right->head,NULL,NULL,0.0,1.0,A,dXTop,TBRWeights,etop,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);
			  assignTBRUpWeights(right,esplit->head,left->head,NULL,NULL,0.0,1.0,A,dXTop,TBRWeights,etop,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);
			  etop = depthFirstTraverse(T,etop);
			}

		      /*etop == eout*/

		      assignTBRUpWeights(left,esplit->head,right->head,NULL,NULL,0.0,1.0,A,dXTop,TBRWeights,etop,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);
		      assignTBRUpWeights(right,esplit->head,left->head,NULL,NULL,0.0,1.0,A,dXTop,TBRWeights,etop,&bestWeight,&eBestSplit,&eBestTop,&eBestBottom);
		    }
		  block = block->tail->parentEdge;
		}
	    } /*if NULL != right*/
	} /*for esplit*/
      /*find bestWeight, best split edge, etc.*/
      if (bestWeight < -EPSILON)
	{
//	  if (verbose)
//	    {
//	      printf("TBR #%d: Splitting edge %s: top edge %s, bottom edge %s\n",*count+1,
//		     eBestSplit->label, eBestTop->label,eBestBottom->label);
//	      printf("Old tree weight is %lf, new tree weight should be %lf\n",T->weight, T->weight + 0.25*bestWeight);
//	    }
	  TBRswitch(T,eBestSplit,eBestTop,eBestBottom);
	  makeBMEAveragesTable(T,D,A);
	  assignBMEWeights(T,A);
	  weighTree(T);
//	  if (verbose)
//	    printf("TBR: new tree weight is %lf\n\n",T->weight);<
//	  (*count)++;
	}
      else
	bestWeight = 1.0;
    } while (bestWeight < -EPSILON);
  for(i=0;i<T->size;i++)
    freeMatrix(TBRWeights[i],T->size);
  freeMatrix(dXTop,T->size);
}
Beispiel #5
0
//void NNI(tree *T, double **avgDistArray, int *count)
void NNI(tree *T, double **avgDistArray, int *count, double **D, int numSpecies)
{
  edge *e, *centerEdge;
  edge **edgeArray;
  int *location;
  int *p,*q;
  int i,j;
  int possibleSwaps;
  double *weights;
  p = initPerm(T->size+1);
  q = initPerm(T->size+1);
  edgeArray = (edge **) malloc((T->size+1)*sizeof(double));
  weights = (double *) malloc((T->size+1)*sizeof(double));
  location = (int *) malloc((T->size+1)*sizeof(int));

  double epsilon = 0.0;
  for (i=0; i<numSpecies; i++)
    for (j=0; j<numSpecies; j++)
      epsilon += D[i][j];
  epsilon = (epsilon / (numSpecies * numSpecies)) * EPSILON;

  for (i=0;i<T->size+1;i++)
    {
      weights[i] = 0.0;
      location[i] = NONE;
    }
  e = findBottomLeft(T->root->leftEdge);
  /* *count = 0; */
  while (NULL != e)
    {
      edgeArray[e->head->index+1] = e;
      location[e->head->index+1] =
	NNIEdgeTest(e,T,avgDistArray,weights + e->head->index + 1);
      e = depthFirstTraverse(T,e);
    }
  possibleSwaps = makeThreshHeap(p,q,weights,T->size+1,0.0);
  permInverse(p,q,T->size+1);
  /*we put the negative values of weights into a heap, indexed by p
    with the minimum value pointed to by p[1]*/
  /*p[i] is index (in edgeArray) of edge with i-th position
    in the heap, q[j] is the position of edge j in the heap */
  while (weights[p[1]] + epsilon < 0)
    {
      centerEdge = edgeArray[p[1]];
      (*count)++;
      T->weight = T->weight + weights[p[1]];
      NNItopSwitch(T,edgeArray[p[1]],location[p[1]],avgDistArray);
      location[p[1]] = NONE;
      weights[p[1]] = 0.0;  /*after the NNI, this edge is in optimal
			      configuration*/
      popHeap(p,q,weights,possibleSwaps--,1);
      /*but we must retest the other four edges*/
      e = centerEdge->head->leftEdge;
      NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
      e = centerEdge->head->rightEdge;
      NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
      e = siblingEdge(centerEdge);
      NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
      e = centerEdge->tail->parentEdge;
      NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
    }
  free(p);
  free(q);
  free(location);
  free(edgeArray);
}
Beispiel #6
0
void NNIupdateAverages(double **A, edge *e, edge *par, edge *skew,
		       edge *swap, edge *fixed, tree *T)
{
  node *v;
  edge *elooper;
  v = e->head;
  /*first, v*/
  A[e->head->index][e->head->index] =
    (swap->bottomsize*
     ((skew->bottomsize*A[skew->head->index][swap->head->index]
       + fixed->bottomsize*A[fixed->head->index][swap->head->index])
      / e->bottomsize) +
     par->topsize*
     ((skew->bottomsize*A[skew->head->index][par->head->index]
       + fixed->bottomsize*A[fixed->head->index][par->head->index])
      / e->bottomsize)
     ) / e->topsize;

  elooper = findBottomLeft(e); /*next, we loop over all the edges
				 which are below e*/
  while (e != elooper)
    {
      A[e->head->index][elooper->head->index] =
	A[elooper->head->index][v->index]
	= (swap->bottomsize*A[elooper->head->index][swap->head->index] +
	   par->topsize*A[elooper->head->index][par->head->index])
	/ e->topsize;
      elooper = depthFirstTraverse(T,elooper);
    }
  elooper = findBottomLeft(swap); /*next we loop over all the edges below and
				    including swap*/
  while (swap != elooper)
  {
    A[e->head->index][elooper->head->index] =
      A[elooper->head->index][e->head->index]
      = (skew->bottomsize * A[elooper->head->index][skew->head->index] +
	 fixed->bottomsize*A[elooper->head->index][fixed->head->index])
      / e->bottomsize;
    elooper = depthFirstTraverse(T,elooper);
  }
  /*now elooper = skew */
  A[e->head->index][elooper->head->index] =
    A[elooper->head->index][e->head->index]
    = (skew->bottomsize * A[elooper->head->index][skew->head->index] +
       fixed->bottomsize* A[elooper->head->index][fixed->head->index])
    / e->bottomsize;

  /*finally, we loop over all the edges in the tree
    on the far side of parEdge*/
  elooper = T->root->leftEdge;
  while ((elooper != swap) && (elooper != e)) /*start a top-first traversal*/
    {
      A[e->head->index][elooper->head->index] =
	A[elooper->head->index][e->head->index]
	= (skew->bottomsize * A[elooper->head->index][skew->head->index]
	   + fixed->bottomsize* A[elooper->head->index][fixed->head->index])
	/ e->bottomsize;
      elooper = topFirstTraverse(T,elooper);
    }

  /*At this point, elooper = par.
    We finish the top-first traversal, excluding the subtree below par*/
  elooper = moveUpRight(par);
  while (NULL != elooper)
    {
      A[e->head->index][elooper->head->index]
	= A[elooper->head->index][e->head->index]
	= (skew->bottomsize * A[elooper->head->index][skew->head->index] +
	   fixed->bottomsize* A[elooper->head->index][fixed->head->index])
	/ e->bottomsize;
      elooper = topFirstTraverse(T,elooper);
    }
}
Beispiel #7
0
void bNNI(meTree *T, double **avgDistArray, int *count)
{
  meEdge *e, *centerEdge;
  meEdge **edgeArray;
  int *p, *location, *q;
  int i;
  int possibleSwaps;
  double *weights;
  p = initPerm(T->size+1);
  q = initPerm(T->size+1);
  edgeArray = (meEdge **) malloc((T->size+1)*sizeof(double));
  weights = (double *) malloc((T->size+1)*sizeof(double));
  location = (int *) malloc((T->size+1)*sizeof(int));
  for (i=0;i<T->size+1;i++)
    {
      weights[i] = 0.0;
      location[i] = NONE;
    }
  if (verbose)
    {
      assignBalWeights(T,avgDistArray);
      weighTree(T);
    }
  e = findBottomLeft(T->root->leftEdge);
  while (NULL != e)
    {
      edgeArray[e->head->index+1] = e;
      location[e->head->index+1] =
	bNNIEdgeTest(e,T,avgDistArray,weights + e->head->index + 1);
      e = depthFirstTraverse(T,e);
    }
  possibleSwaps = makeThreshHeap(p,q,weights,T->size+1,0.0);
  permInverse(p,q,T->size+1);
  /*we put the negative values of weights into a heap, indexed by p
    with the minimum value pointed to by p[1]*/
  /*p[i] is index (in edgeArray) of edge with i-th position
    in the heap, q[j] is the position of edge j in the heap */
  /*NOTE: the loop below should test that weights[p[1]] < 0, but
    if compiled with optimization it is possible that weights[p[1]]
    ends up negative and very small, so that changes to the heap
    become cyclic and the loop becomes infinite. To avoid this
    behavior, stop the loop short of 0.0. This is a workaround 
    until the roundoff sensitivity is removed algorithmically */
  while (weights[p[1]] < -1e-8)
    {
      centerEdge = edgeArray[p[1]];
      (*count)++;
      if (verbose)
	{
	  T->weight = T->weight + weights[p[1]];
	  printf("New tree weight is %lf.\n",T->weight);
	}
      bNNItopSwitch(T,edgeArray[p[1]],location[p[1]],avgDistArray);
      location[p[1]] = NONE;
      weights[p[1]] = 0.0;  /*after the bNNI, this edge is in optimal
			      configuration*/
      popHeap(p,q,weights,possibleSwaps--,1);
      /*but we must retest the other edges of T*/
      /*CHANGE 2/28/2003 expanding retesting to _all_ edges of T*/
      e = depthFirstTraverse(T,NULL);
      while (NULL != e)
	{
	  bNNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
	  e = depthFirstTraverse(T,e);
	}
    }
  free(p);
  free(q);
  free(location);
  free(edgeArray);
  assignBalWeights(T,avgDistArray);
}
Beispiel #8
0
Datei: bNNI.c Projekt: cran/ape
//void bNNI(tree *T, double **avgDistArray, int *count)
void bNNI(tree *T, double **avgDistArray, int *count, double **D, int numSpecies)
{
    edge *e;//, *centerEdge deleted by EP, 2013-09-26, see also below
  edge **edgeArray;
  int *p, *location, *q;
  int i,j;
  int possibleSwaps;
  double *weights;
  p = initPerm(T->size+1);
  q = initPerm(T->size+1);
  edgeArray = (edge **) malloc((T->size+1)*sizeof(double));
  weights = (double *) malloc((T->size+1)*sizeof(double));
  location = (int *) malloc((T->size+1)*sizeof(int));

  double epsilon = 0.0;
  for (i=0; i<numSpecies; i++)
    for (j=0; j<numSpecies; j++)
      epsilon += D[i][j];
  epsilon = (epsilon / (numSpecies * numSpecies)) * EPSILON;

  for (i=0;i<T->size+1;i++)
    {
      weights[i] = 0.0;
      location[i] = NONE;
    }
/*  if (verbose)
    {
      assignBMEWeights(T,avgDistArray);
      weighTree(T);
    }*/
  e = findBottomLeft(T->root->leftEdge);
  while (NULL != e)
    {
      edgeArray[e->head->index+1] = e;
      location[e->head->index+1] =
	bNNIEdgeTest(e,T,avgDistArray,weights + e->head->index + 1);
      e = depthFirstTraverse(T,e);
    }
  possibleSwaps = makeThreshHeap(p,q,weights,T->size+1,0.0);
  permInverse(p,q,T->size+1);
  /*we put the negative values of weights into a heap, indexed by p
    with the minimum value pointed to by p[1]*/
  /*p[i] is index (in edgeArray) of edge with i-th position
    in the heap, q[j] is the position of edge j in the heap */
  while (weights[p[1]] + epsilon < 0)
    {
	/* centerEdge = edgeArray[p[1]]; apparently unused later, deleted by EP, 2013-09-26 */
      (*count)++;
/*      if (verbose)
	{
	  T->weight = T->weight + weights[p[1]];
	  printf("New tree weight is %lf.\n",T->weight);
	}*/
      bNNItopSwitch(T,edgeArray[p[1]],location[p[1]],avgDistArray);
      location[p[1]] = NONE;
      weights[p[1]] = 0.0;  /*after the bNNI, this edge is in optimal
			      configuration*/
      popHeap(p,q,weights,possibleSwaps--,1);
      /*but we must retest the other edges of T*/
      /*CHANGE 2/28/2003 expanding retesting to _all_ edges of T*/
      e = depthFirstTraverse(T,NULL);
      while (NULL != e)
	{
	  bNNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps);
	  e = depthFirstTraverse(T,e);
	}
    }
  free(p);
  free(q);
  free(location);
  free(edgeArray);
  free(weights);
  assignBMEWeights(T,avgDistArray);
}
Beispiel #9
0
void bNNI (tree *T, double **avgDistArray, int *count, FILE *statfile)
{
	edge *e;
	edge **edgeArray;
	int *p, *location, *q;
	int i;
	int possibleSwaps;
	double *weights;

	p = initPerm (T->size+1);
	q = initPerm (T->size+1);
	edgeArray = (edge **) mCalloc ((T->size+1), sizeof (edge *));
	weights = (double *) mCalloc ((T->size+1), sizeof (double));
	location = (int *) mCalloc ((T->size+1), sizeof (int));
	for (i=0; i<T->size+1; i++)
	{
		weights[i] = 0.0;
		location[i] = NONE;
	}

	assignBMEWeights (T, avgDistArray);
	weighTree (T);
	if (!isBoostrap)
	{
		if (statfile)
			fprintf (statfile, "\tBefore NNI:     tree length is %f.\n", T->weight);
		if (verbose > 2)
			Debug ( (char*)"Before NNI: tree length is %f.", T->weight);
		else if (verbose > 1)
			Message ( (char*)". Before NNI: tree length is %f.", T->weight);
	}

	e = findBottomLeft (T->root->leftEdge);
	while (NULL != e)
	{
		edgeArray[e->head->index+1] = e;
		location[e->head->index+1] = bNNIEdgeTest (e, T, avgDistArray,
			weights + e->head->index + 1);
		e = depthFirstTraverse (T,e);
	}

	possibleSwaps = makeThreshHeap (p, q, weights, T->size+1,0.0);
	permInverse (p, q, T->size+1);

	/* We put the negative values of weights into a heap, indexed by p
	 * with the minimum value pointed to by p[1]
	 * p[i] is index (in edgeArray) of edge with i-th position in the
	 * heap, q[j] is the position of edge j in the heap */

	while (weights[p[1]] < -DBL_EPSILON)
	{
		(*count)++;
		T->weight = T->weight + weights[p[1]];
		if (!isBoostrap)
		{
			if (statfile)
				fprintf (statfile, "\tNNI  %5d: new tree length is %f.\n", *count, T->weight);
			if (verbose > 2)
				Debug ( (char*)"NNI %5d: new tree length is %f.", *count, T->weight);
			else if (verbose > 1)
				Message ( (char*)". NNI %5d: new tree length is %f.", *count, T->weight);
		}

		bNNItopSwitch (edgeArray[p[1]], location[p[1]], avgDistArray);
		location[p[1]] = NONE;
		weights[p[1]] = 0.0;	//after the bNNI, this edge is in optimal configuration
		popHeap (p, q, weights, possibleSwaps--, 1);

		/* but we must retest the other edges of T */
		/* CHANGE 2/28/2003 expanding retesting to _all_ edges of T */

		e = depthFirstTraverse (T, NULL);
		while (NULL != e)
		{
			bNNIRetestEdge (p, q, e, T, avgDistArray, weights, location, &possibleSwaps);
			e = depthFirstTraverse (T, e);
		}
	}

	free (p);
	free (q);
	free (location);
	free (edgeArray);
	free (weights);
	assignBMEWeights (T, avgDistArray);

	return;
}