1 /* newick.c 2012-02-09 */
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 error("error reading subtree");
78 if('(' == treeString[i]) /*if treeString[i] is a left parenthesis,
79 we scan down the string until we find its partner.
80 the relative number of '('s vs. ')'s is counted
81 by the variable parcount*/
87 while (('(' != treeString[i]) && (')' != treeString[i]))
88 i++; /*skip over characters which are not parentheses*/
89 if('(' == treeString[i])
91 else if (')' == treeString[i])
95 right = i; /*at this point, the subtree string goes from
96 treeString[left] to treeString[right - 1]*/
97 thisNode = decodeNewickSubtree(treeString + left,T,uCount); /*note that this
100 i = right; /*having created the node for the subtree, we move
101 to assigning the label for the new node.
102 treeString[right] will be the start of this label */
103 } /* end if ('(' == treeString[i]) condition */
106 //thisNode = makeNode(label,NULL,nodeCount++);
107 thisNode = makeNode("",NULL,nodeCount++);
113 left = i; /*recall "left" is the left marker for the substring, "right" the right*/
114 if (':' == treeString[i]) /*this means an internal node?*/
116 //sprintf(thisNode->label,"I%d",(*uCount)++);
117 //snprintf(thisNode->label,MAX_LABEL_LENGTH,"I%d",(*uCount)++);
123 while((':' != treeString[i]) && (',' != treeString[i]) && (')' != treeString[i]))
127 for(i = left; i < right;i++)
128 if(!(whiteSpace(treeString[i])))
129 thisNode->label[j++] = treeString[i];
130 thisNode->label[j] = '\0';
132 if(':' == treeString[right])
144 ((',' != treeString[i]) && (')' != treeString[i]))
148 for(i = left; i < right; i++)
149 stringWeight[j++] = treeString[i];
150 stringWeight[j] = '\0';
151 thisWeight = atof(stringWeight);
155 //thisEdge = makeEdge(label,centerNode,thisNode,thisWeight);
156 thisEdge = makeEdge("",centerNode,thisNode,thisWeight);
157 thisNode->parentEdge = thisEdge;
158 if (NULL == centerNode->leftEdge)
159 centerNode->leftEdge = thisEdge;
160 else if (NULL == centerNode->rightEdge)
161 centerNode->rightEdge = thisEdge;
162 else if (NULL == centerNode->middleEdge)
163 centerNode->middleEdge = thisEdge;
166 error("node %s has too many (>3) children.", centerNode->label);
168 //sprintf(thisEdge->label,"E%d",edgeCount++);
169 //snprintf(thisEdge->label,MAX_LABEL_LENGTH,"E%d",edgeCount++);
172 if (',' == treeString[right])
182 tree *readNewickString (char *str, int numLeaves)
191 char rootLabel[MAX_LABEL_LENGTH];
192 nodeCount = edgeCount = 0;
198 error("generated tree does not start with '('");
200 inputLength = strlen (str)+1;
201 for(i = 0; i < inputLength; i++)
205 else if (')' == str[i])
209 else if (0 == parCount)
213 sprintf(rootLabel,"URoot");
217 if (!(whiteSpace (str[i++])))
218 rootLabel[j++] = str[i-1]; /*be careful here */
223 else if (parCount < 0)
225 error("generated tree has too many right parentheses");
228 centerNode = decodeNewickSubtree (str, T, &uCount);
229 snprintf (centerNode->label, MAX_LABEL_LENGTH, "%s", rootLabel); /* added "%s" following Jos Kafer's suggestion (2010-11-23) */
230 T->root = centerNode;