1 /* newick.c 2008-01-14 */
3 /* Copyright 2007-2008 Vincent Lefort */
5 /* This file is part of the R-package `ape'. */
6 /* See the file ../COPYING for licensing issues. */
13 int whiteSpace(char c)
15 if ((' ' == c) || ('\t' == c) || ('\n' == c))
23 if (NULL != v->parentEdge)
25 if (NULL != v->leftEdge)
27 if (NULL != v->rightEdge)
29 if (NULL != v->middleEdge)
36 /*decodeNewickSubtree is used to turn a string of the form
37 "(v1:d1,v2:d2,(subtree) v3:d3....vk:dk) subroot:d," into a subtree
38 rooted at subrooted, with corresponding subtrees and leaves at v1
39 through vk. It is called recursively on subtrees*/
41 node *decodeNewickSubtree(char *treeString, tree *T, int *uCount)
47 // char label[MAX_LABEL_LENGTH];
48 char stringWeight[MAX_LABEL_LENGTH];
54 // snprintf(label,14,"Default_Label\0");
57 state = ReadOpenParenthesis;
58 if('(' == treeString[0])
60 //centerNode = makeNode(label,NULL,nodeCount++);
61 centerNode = makeNode("",NULL,nodeCount++);
65 while(whiteSpace(treeString[i]))
69 case(ReadOpenParenthesis):
70 if('(' != treeString[0])
72 Rprintf("Error reading subtree.\n");
79 if('(' == treeString[i]) /*if treeString[i] is a left parenthesis,
80 we scan down the string until we find its partner.
81 the relative number of '('s vs. ')'s is counted
82 by the variable parcount*/
88 while (('(' != treeString[i]) && (')' != treeString[i]))
89 i++; /*skip over characters which are not parentheses*/
90 if('(' == treeString[i])
92 else if (')' == treeString[i])
96 right = i; /*at this point, the subtree string goes from
97 treeString[left] to treeString[right - 1]*/
98 thisNode = decodeNewickSubtree(treeString + left,T,uCount); /*note that this
101 i = right; /*having created the node for the subtree, we move
102 to assigning the label for the new node.
103 treeString[right] will be the start of this label */
104 } /* end if ('(' == treeString[i]) condition */
107 //thisNode = makeNode(label,NULL,nodeCount++);
108 thisNode = makeNode("",NULL,nodeCount++);
114 left = i; /*recall "left" is the left marker for the substring, "right" the right*/
115 if (':' == treeString[i]) /*this means an internal node?*/
117 //sprintf(thisNode->label,"I%d",(*uCount)++);
118 //snprintf(thisNode->label,MAX_LABEL_LENGTH,"I%d",(*uCount)++);
124 while((':' != treeString[i]) && (',' != treeString[i]) && (')' != treeString[i]))
128 for(i = left; i < right;i++)
129 if(!(whiteSpace(treeString[i])))
130 thisNode->label[j++] = treeString[i];
131 thisNode->label[j] = '\0';
133 if(':' == treeString[right])
145 ((',' != treeString[i]) && (')' != treeString[i]))
149 for(i = left; i < right; i++)
150 stringWeight[j++] = treeString[i];
151 stringWeight[j] = '\0';
152 thisWeight = atof(stringWeight);
156 //thisEdge = makeEdge(label,centerNode,thisNode,thisWeight);
157 thisEdge = makeEdge("",centerNode,thisNode,thisWeight);
158 thisNode->parentEdge = thisEdge;
159 if (NULL == centerNode->leftEdge)
160 centerNode->leftEdge = thisEdge;
161 else if (NULL == centerNode->rightEdge)
162 centerNode->rightEdge = thisEdge;
163 else if (NULL == centerNode->middleEdge)
164 centerNode->middleEdge = thisEdge;
167 Rprintf("Error: node %s has too many (>3) children.\n",centerNode->label);
170 //sprintf(thisEdge->label,"E%d",edgeCount++);
171 //snprintf(thisEdge->label,MAX_LABEL_LENGTH,"E%d",edgeCount++);
174 if (',' == treeString[right])
184 tree *readNewickString (char *str, int numLeaves)
193 char rootLabel[MAX_LABEL_LENGTH];
194 nodeCount = edgeCount = 0;
200 Rprintf("Error reading generated tree - does not start with '('.\n");
203 inputLength = strlen (str)+1;
204 for(i = 0; i < inputLength; i++)
208 else if (')' == str[i])
212 else if (0 == parCount)
216 sprintf(rootLabel,"URoot");
220 if (!(whiteSpace (str[i++])))
221 rootLabel[j++] = str[i-1]; /*be careful here */
226 else if (parCount < 0)
228 Rprintf("Error reading generated tree. Too many right parentheses.\n");
232 centerNode = decodeNewickSubtree (str, T, &uCount);
233 snprintf (centerNode->label, MAX_LABEL_LENGTH, rootLabel);
234 T->root = centerNode;