3 //functions from me_balanced.c
4 tree *BMEaddSpecies(tree *T,node *v, double **D, double **A);
5 void assignBMEWeights(tree *T, double **A);
6 void makeBMEAveragesTable(tree *T, double **D, double **A);
7 //functions from me_ols.c
8 tree *GMEaddSpecies(tree *T,node *v, double **D, double **A);
9 void assignOLSWeights(tree *T, double **A);
10 void makeOLSAveragesTable(tree *T, double **D, double **A);
11 //functions from bNNI.c
12 void bNNI(tree *T, double **avgDistArray, int *count, double **D, int numSpecies);
13 //functions from NNI.c
14 void NNI(tree *T, double **avgDistArray, int *count, double **D, int numSpecies);
17 void me_b(double *X, int *N, char **labels, char **treeStr, int *nni)
20 set *species, *slooper;
29 species = (set *) malloc(sizeof(set));
30 species->firstNode = NULL;
31 species->secondNode = NULL;
32 str = (char *)R_alloc(MAX_INPUT_SIZE, sizeof(char));
35 strncpy(str, "", strlen(str));
37 D = loadMatrix (X, labels, n, species);
38 A = initDoubleMatrix(2 * n - 2);
40 for(slooper = species; NULL != slooper; slooper = slooper->secondNode)
42 addNode = copyNode(slooper->firstNode);
43 T = BMEaddSpecies(T,addNode,D,A);
47 bNNI(T,A,&nniCount,D,n);
48 assignBMEWeights(T,A);
50 NewickPrintTreeStr(T,str);
52 if (strlen (str) < MAX_INPUT_SIZE -1)
54 *treeStr = (char *)R_alloc(MAX_INPUT_SIZE, sizeof(char));
57 strncpy(*treeStr, "", strlen(*treeStr));
59 strncpy (*treeStr, str, strlen(str));
64 freeMatrix(A,2*n - 2);
72 void me_o(double *X, int *N, char **labels, char **treeStr, int *nni)
75 set *species, *slooper;
84 species = (set *) malloc(sizeof(set));
85 species->firstNode = NULL;
86 species->secondNode = NULL;
87 str = (char *)R_alloc(MAX_INPUT_SIZE, sizeof(char));
90 strncpy(str, "", strlen(str));
93 D = loadMatrix (X, labels, n, species);
94 A = initDoubleMatrix(2 * n - 2);
96 for(slooper = species; NULL != slooper; slooper = slooper->secondNode)
98 addNode = copyNode(slooper->firstNode);
99 T = GMEaddSpecies(T,addNode,D,A);
101 makeOLSAveragesTable(T,D,A);
104 NNI(T,A,&nniCount,D,n);
105 assignOLSWeights(T,A);
107 NewickPrintTreeStr(T,str);
109 if (strlen (str) < MAX_INPUT_SIZE -1)
111 *treeStr = (char *)R_alloc(MAX_INPUT_SIZE, sizeof(char));
113 if (strlen(*treeStr))
114 strncpy(*treeStr, "", strlen(*treeStr));
116 strncpy (*treeStr, str, strlen (str));
121 freeMatrix(A,2*n - 2);
129 /*************************************************************************
133 *************************************************************************/
135 double **initDoubleMatrix(int d)
139 A = (double **) malloc(d*sizeof(double *));
142 A[i] = (double *) malloc(d*sizeof(double));
149 double **loadMatrix (double *X, char **labels, int n, set *S)
151 char nextString[MAX_LABEL_LENGTH];
156 table = (double **) calloc(n,sizeof(double *));
158 table[i] = (double *) calloc(n,sizeof(double));
162 strncpy (nextString, labels[i], MAX_LABEL_LENGTH);
163 // ReplaceForbiddenChars (nextString, '_');
164 v = makeNewNode(nextString,-1);
167 for (j=i; j<n; j++) {
170 table[j][i] = X[XINDEX(a,b)];
171 table[i][j] = X[XINDEX(a,b)];
179 /*************************************************************************
183 *************************************************************************/
185 set *addToSet(node *v, set *X)
189 X = (set *) malloc(sizeof(set));
191 X->secondNode = NULL;
193 else if (NULL == X->firstNode)
196 X->secondNode = addToSet(v,X->secondNode);
200 node *makeNewNode(char *label, int i)
202 return(makeNode(label,NULL,i));
205 node *makeNode(char *label, edge *parentEdge, int index)
207 node *newNode; /*points to new node added to the graph*/
208 newNode = (node *) malloc(sizeof(node));
209 strncpy(newNode->label,label,NODE_LABEL_LENGTH);
210 newNode->index = index;
211 newNode->index2 = -1;
212 newNode->parentEdge = parentEdge;
213 newNode->leftEdge = NULL;
214 newNode->middleEdge = NULL;
215 newNode->rightEdge = NULL;
216 /*all fields have been initialized*/
220 /*copyNode returns a copy of v which has all of the fields identical to those
221 of v, except the node pointer fields*/
222 node *copyNode(node *v)
225 w = makeNode(v->label,NULL,v->index);
226 w->index2 = v->index2;
230 edge *siblingEdge(edge *e)
232 if(e == e->tail->leftEdge)
233 return(e->tail->rightEdge);
235 return(e->tail->leftEdge);
238 edge *makeEdge(char *label, node *tail, node *head, double weight)
241 newEdge = (edge *) malloc(sizeof(edge));
242 strncpy(newEdge->label,label,EDGE_LABEL_LENGTH);
243 newEdge->tail = tail;
244 newEdge->head = head;
245 newEdge->distance = weight;
246 newEdge->totalweight = 0.0;
253 T = (tree *) malloc(sizeof(tree));
260 void updateSizes(edge *e, int direction)
266 f = e->head->leftEdge;
269 f = e->head->rightEdge;
278 f = e->tail->parentEdge;
286 /*detrifurcate takes the (possibly trifurcated) input tree
287 and reroots the tree to a leaf*/
288 /*assumes tree is only trifurcated at root*/
289 tree *detrifurcate(tree *T)
296 if (NULL != v->parentEdge)
298 Rprintf ("Error: root %s is poorly rooted.\n",v->label);
301 for(e = v->middleEdge, v->middleEdge = NULL; NULL != e; e = f )
310 w->parentEdge = NULL;
316 void compareSets(tree *T, set *S)
321 e = depthFirstTraverse(T,NULL);
325 for(X = S; NULL != X; X = X->secondNode)
328 if (0 == strcmp(v->label,w->label))
330 v->index2 = w->index2;
335 e = depthFirstTraverse(T,e);
338 for(X = S; NULL != X; X = X->secondNode)
341 if (0 == strcmp(v->label,w->label))
343 v->index2 = w->index2;
350 Rprintf("Error leaf %s in tree not in distance matrix.\n",v->label);
353 e = depthFirstTraverse(T,NULL);
357 if ((leaf(v)) && (-1 == v->index2))
359 Rprintf("Error leaf %s in tree not in distance matrix.\n",v->label);
362 e = depthFirstTraverse(T,e);
364 for(X = S; NULL != X; X = X->secondNode)
365 if (X->firstNode->index2 > -1)
367 Rprintf("Error node %s in matrix but not a leaf in tree.\n",X->firstNode->label);
373 void partitionSizes(tree *T)
376 e = depthFirstTraverse(T,NULL);
382 e->bottomsize = e->head->leftEdge->bottomsize
383 + e->head->rightEdge->bottomsize;
384 e->topsize = (T->size + 2)/2 - e->bottomsize;
385 e = depthFirstTraverse(T,e);
389 /*************************************************************************
393 *************************************************************************/
395 edge *depthFirstTraverse(tree *T, edge *e)
396 /*depthFirstTraverse returns the edge f which is least in T according
397 to the depth-first order, but which is later than e in the search
398 pattern. If e is null, f is the least edge of T*/
403 f = T->root->leftEdge;
405 f = findBottomLeft(f);
406 return(f); /*this is the first edge of this search pattern*/
408 else /*e is non-null*/
410 if (e->tail->leftEdge == e)
411 /*if e is a left-oriented edge, we skip the entire
412 tree cut below e, and find least edge*/
414 else /*if e is a right-oriented edge, we have already looked at its
415 sibling and everything below e, so we move up*/
416 f = e->tail->parentEdge;
421 edge *findBottomLeft(edge *e)
422 /*findBottomLeft searches by gottom down in the tree and to the left.*/
426 while (NULL != f->head->leftEdge)
427 f = f->head->leftEdge;
431 edge *moveRight(edge *e)
434 f = e->tail->rightEdge; /*this step moves from a left-oriented edge
435 to a right-oriented edge*/
437 f = findBottomLeft(f);
441 edge *topFirstTraverse(tree *T, edge *e)
442 /*topFirstTraverse starts from the top of T, and from there moves stepwise
443 down, left before right*/
444 /*assumes tree has been detrifurcated*/
448 return(T->root->leftEdge); /*first Edge searched*/
449 else if (!(leaf(e->head)))
450 return(e->head->leftEdge); /*down and to the left is preferred*/
451 else /*e->head is a leaf*/
458 edge *moveUpRight(edge *e)
462 while ((NULL != f) && ( f->tail->leftEdge != f))
463 f = f->tail->parentEdge;
464 /*go up the tree until f is a leftEdge*/
466 return(f); /*triggered at end of search*/
468 return(f->tail->rightEdge);
469 /*and then go right*/
472 /*************************************************************************
476 *************************************************************************/
478 void freeMatrix(double **D, int size)
489 freeSet(S->secondNode);
493 void freeTree(tree *T)
497 if (NULL != v->leftEdge)
498 freeSubTree(v->leftEdge);
503 void freeSubTree(edge *e)