예제 #1
0
파일: me.c 프로젝트: rforge/phyloc
void me_o(double *X, int *N, char **labels, char **treeStr, int *nni)
{
  double **D, **A;
  set *species, *slooper;
  node *addNode;
  tree *T;
  char *str;
  int n, nniCount;

  n = *N;
  T = NULL;
  nniCount = 0;
  species = (set *) malloc(sizeof(set));
  species->firstNode = NULL;
  species->secondNode = NULL;
  str = (char *)R_alloc(MAX_INPUT_SIZE, sizeof(char));
  /* added by EP */
  if (strlen(str))
    strncpy(str, "", strlen(str));
  /* end */

  D = loadMatrix (X, labels, n, species);
  A = initDoubleMatrix(2 * n - 2);

  for(slooper = species; NULL != slooper; slooper = slooper->secondNode)
  {
    addNode = copyNode(slooper->firstNode);
    T = GMEaddSpecies(T,addNode,D,A);
  }
  makeOLSAveragesTable(T,D,A);
  // Compute NNI
  if (*nni == 1)
    NNI(T,A,&nniCount,D,n);
  assignOLSWeights(T,A);

  NewickPrintTreeStr(T,str);

  if (strlen (str) < MAX_INPUT_SIZE -1)
    {
      *treeStr = (char *)R_alloc(MAX_INPUT_SIZE, sizeof(char));
      /* added by EP */
      if (strlen(*treeStr))
	strncpy(*treeStr, "", strlen(*treeStr));
      /* end */
      strncpy (*treeStr, str, strlen (str));
    }

 /*  free (str); */
  freeMatrix(D,n);
  freeMatrix(A,2*n - 2);
  freeSet(species);
  freeTree(T);
  T = NULL;

  return;
}
예제 #2
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);*/
}
예제 #3
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);
}
예제 #4
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;
}