예제 #1
0
파일: SPR.c 프로젝트: Ward9250/ape
void SPR(tree *T, double **D, double **A, int *count)
{
  int i,j,k;
  node *v;
  /*FILE *treefile;*/
  edge *e,*f;
  /* char filename[MAX_LABEL_LENGTH];*/
  double ***swapWeights;
  double swapValue = 0.0;
  swapWeights = (double ***)malloc(2*sizeof(double **));
  makeBMEAveragesTable(T,D,A);
  assignBMEWeights(T,A);
  weighTree(T);
  /*if (verbose)
    printf("Before SPRs: tree length is %lf.\n",T->weight);*/
  for(i=0;i<2;i++)
    swapWeights[i] = initDoubleMatrix(T->size);
  do
    {
      swapValue=0.0;
      zero3DMatrix(swapWeights,2,T->size,T->size);
      i = j = k = 0;
      for(e=depthFirstTraverse(T,NULL);NULL!=e;e=depthFirstTraverse(T,e))
	assignSPRWeights(e->head,A,swapWeights);
      findTableMin(&i,&j,&k,T->size,swapWeights,&swapValue);
      swapValue = swapWeights[i][j][k];
      if (swapValue < -EPSILON)
	{
//	  if (verbose)
//	    printf("New tree weight should be %lf.\n",T->weight + 0.25*swapValue);
	  v = indexedNode(T,j);
	  f = indexedEdge(T,k);
//	  if (verbose)
//	    printf("Swapping tree below %s to split edge %s with head %s and tail %s\n",
//			   v->parentEdge->label,f->label,f->head->label,f->tail->label);
	  SPRTopShift(T,v,f,2-i);
	  makeBMEAveragesTable(T,D,A);
	  assignBMEWeights(T,A);
	  weighTree(T);
	  (*count)++;
	  /*sprintf(filename,"tree%d.new",*count);*/
//	  if (verbose)
//	    printf("After %d SPRs, tree weight is %lf.\n\n",*count,T->weight);
	  /*treefile = fopen(filename,"w");
	  NewickPrintTree(T,treefile);
	  fclose(treefile);*/
	  }
    } while (swapValue < -EPSILON);
  for(i=0;i<2;i++)
    freeMatrix(swapWeights[i],T->size);
  free(swapWeights);
  /*if (verbose)
    readOffTree(T);*/
}
예제 #2
0
파일: TBR.c 프로젝트: Ward9250/ape
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);
}
예제 #3
0
파일: SPR.c 프로젝트: pranjalv123/ASTRID
void SPR (tree *T, double **D, double **A, int *count, FILE *statfile)
{
	int i, j, k;
	node *v;
	edge *e, *f;
	double ***swapWeights;
	double treeWeightBefore;
	double swapValue = 0.0;
	boolean firstSPR = TRUE;

	swapWeights = (double ***) mCalloc (2, sizeof(double **));
	makeBMEAveragesTable (T, D, A);
	assignBMEWeights (T, A);
	weighTree (T);
	treeWeightBefore = T->weight;

	for (i=0; i<2; i++)
		swapWeights[i] = initDoubleMatrix (T->size);

	do
	{
		swapValue = 0.0;
		zero3DMatrix (swapWeights, 2, T->size, T->size);
		i = j = k = 0;

		for (e = depthFirstTraverse (T, NULL); NULL != e; e = depthFirstTraverse (T, e))
			assignSPRWeights (e->head, A, swapWeights);

		findTableMin (&i, &j, &k, T->size, swapWeights, &swapValue);
		swapValue = swapWeights[i][j][k];
		if (swapValue < -FLT_EPSILON)
		{
			if (firstSPR)
			{
				firstSPR = FALSE;
				if (!isBoostrap)
				{

					if (verbose > 2)
						Debug ( (char*)"Before SPR: tree length is %f.", treeWeightBefore);
					else if (verbose > 1)
						Message ( (char*)". Before SPR: tree length is %f.", treeWeightBefore);
				}
			}

			if (verbose > 2 && !isBoostrap)
				Debug ( (char*)"New tree length should be %f.", T->weight + 0.25 * swapValue);

			v = indexedNode (T, j);
			f = indexedEdge (T, k);

			if (verbose > 2 && !isBoostrap)
			{
				if ((NULL == f->head->label) || (strlen (f->head->label) == 0))
				{
					if ((NULL == f->tail->label) || (strlen (f->tail->label) == 0))
						Debug ( (char*)"Swapping tree below '%s' to split edge '%s' with internal head and tail.",
							v->parentEdge->label, f->label);

					else
						Debug ( (char*)"Swapping tree below '%s' to split edge '%s' with internal head and tail '%s'.",
							v->parentEdge->label, f->label, f->tail->label);
				}
				else
				{
					if ((NULL == f->tail->label) || (strlen (f->tail->label) == 0))
						Debug ( (char*)"Swapping tree below '%s' to split edge '%s' with head '%s' and internal tail.",
							v->parentEdge->label, f->label, f->head->label, f->tail->label);

					else
						Debug ( (char*)"Swapping tree below '%s' to split edge '%s' with head '%s' and tail '%s'.",
							v->parentEdge->label, f->label, f->head->label, f->tail->label);
				}
			}

			SPRTopShift (v, f, 2-i);
			makeBMEAveragesTable (T, D, A);
			assignBMEWeights (T, A);
			weighTree (T);
			(*count)++;

			if (!isBoostrap)
			{

				if (verbose > 2)
					Debug ( (char*)"SPR %5d: new tree length is %f.", *count, T->weight);
				else if (verbose > 1)
					Message ( (char*)". SPR %5d: new tree length is %f.", *count, T->weight);
			}

		}
	} while (swapValue < -FLT_EPSILON);

	for (i=0; i<2; i++)
		freeMatrix (swapWeights[i], T->size);

	free (swapWeights);

	return;
}
예제 #4
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);
}
예제 #5
0
파일: bNNI.c 프로젝트: pranjalv123/ASTRID
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;
}