-
-double GetSubTreeLength (tree *T, edge *e)
-{
- double ret = 0;
-
- if ( (NULL != e) && (! leaf(e->head) )) {
- ret += GetSubTreeLength (T, e->head->leftEdge);
- ret += GetSubTreeLength (T, e->head->rightEdge);
- }
- ret += e->distance;
- return ret;
-}
-
-void NewickPrintSubtree(tree *T, edge *e, char *str)
-{
- char *tmp;
- if (NULL == e)
- {
- Rprintf("Error with Newick Printing routine.\n");
- exit(0);
- }
- if(!(leaf(e->head)))
- {
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, "(", 1);
- NewickPrintSubtree(T,e->head->leftEdge,str);
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, ",", 1);
- NewickPrintSubtree(T,e->head->rightEdge,str);
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, ")", 1);
- }
- if (strlen (str) < MAX_INPUT_SIZE - strlen (e->head->label) -1)
- strncat (str, e->head->label, strlen (e->head->label));
-
- if (strlen (str) < MAX_INPUT_SIZE - 2)
- strncat (str, ":", 1);
-
- tmp = (char *)R_alloc(INPUT_SIZE, sizeof(char));
- /* added by EP */
- if (strlen(tmp))
- strncpy(tmp, "", strlen(tmp));
- /* end */
- snprintf (tmp, INPUT_SIZE, "%lf", e->distance);
- if (strlen (str) < MAX_INPUT_SIZE - strlen (tmp) -1)
- strncat (str, tmp, strlen (tmp));
-
- /* free (tmp); */
- return;
-}
-
-double GetBinaryTreeLength (tree *T)
-{
- double ret = 0;
- edge *e, *f;
- node *rootchild;
- e = T->root->leftEdge;
- rootchild = e->head;
-
- f = rootchild->leftEdge;
- if (NULL != f)
- ret += GetSubTreeLength (T, f);
- f = rootchild->rightEdge;
- if (NULL != f)
- ret += GetSubTreeLength (T, f);
- ret += e->distance;
- return ret;
-}
-
-void NewickPrintBinaryTree(tree *T, char *str)
-{
- edge *e, *f;
- node *rootchild;
- char *tmp;
- e = T->root->leftEdge;
- rootchild = e->head;
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, "(", 1);
- f = rootchild->leftEdge;
- if (NULL != f)
- {
- NewickPrintSubtree(T,f,str);
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, ",", 1);
- }
- f = rootchild->rightEdge;
- if (NULL != f)
- {
- NewickPrintSubtree(T,f,str);
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, ",", 1);
- }
- if (strlen (str) < MAX_INPUT_SIZE - strlen (T->root->label) -1)
- strncat (str, T->root->label, strlen (T->root->label));
-
- if (strlen (str) < MAX_INPUT_SIZE - 2)
- strncat (str, ":", 1);
-
- tmp = (char *)R_alloc(INPUT_SIZE, sizeof(char));
- /* added by EP */
- if (strlen(tmp))
- strncpy(tmp, "", strlen(tmp));
- /* end */
- snprintf (tmp, INPUT_SIZE, "%lf", e->distance);
- if (strlen (str) < MAX_INPUT_SIZE - strlen (tmp) -1)
- strncat (str, tmp, strlen (tmp));
-
- if (strlen (str) < MAX_INPUT_SIZE - 2)
- strncat (str, ")", 1);
-
- if (NULL != rootchild->label)
- if (strlen (str) < MAX_INPUT_SIZE - strlen (rootchild->label) -1)
- strncat (str, T->root->label, strlen (rootchild->label));
-
- if (strlen (str) < MAX_INPUT_SIZE - 3)
- strncat (str, ";\n", 2);
-
- /* free (tmp); */
- return;
-}
-
-double GetTrinaryTreeLength (tree *T)
-{
- double ret = 0;
- edge *f;
- f = T->root->leftEdge;
- if (NULL != f)
- ret += GetSubTreeLength (T, f);
- f = T->root->rightEdge;
- if (NULL != f)
- ret += GetSubTreeLength (T, f);
- f = T->root->middleEdge;
- if (NULL != f)
- ret += GetSubTreeLength (T, f);
-
- return ret;
-}
-
-void NewickPrintTrinaryTree(tree *T, char *str)
-{
- edge *f;
- f = T->root->leftEdge;
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, "(", 1);
- if (NULL != f)
- {
- NewickPrintSubtree(T,f,str);
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, ",", 1);
- }
- f = T->root->rightEdge;
- if (NULL != f)
- {
- NewickPrintSubtree(T,f,str);
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, ",", 1);
- }
- f = T->root->middleEdge;
- if (NULL != f)
- {
- NewickPrintSubtree(T,f,str);
- if (strlen (str) < MAX_INPUT_SIZE -2)
- strncat (str, ")", 1);
- }
- if (NULL != T->root->label)
- if (strlen (str) < MAX_INPUT_SIZE - strlen (T->root->label) -1)
- strncat (str, T->root->label, strlen (T->root->label));
- if (strlen (str) < MAX_INPUT_SIZE - 3)
- strncat (str, ";\n", 2);
- return;
-}
-
-void NewickPrintTreeStr(tree *T, char *str)
-{
- if (leaf(T->root))
- NewickPrintBinaryTree(T,str);
- else
- NewickPrintTrinaryTree(T,str);
-}
-
-double GetTreeLength (tree *T)
-{
- double ret = 0;
- if (leaf(T->root))
- ret = GetBinaryTreeLength (T);
- else
- ret = GetTrinaryTreeLength (T);
- return ret;
-}
-/*
-void NewickPrintTree(tree *T, FILE *ofile)
-{
- if (leaf(T->root))
- NewickPrintBinaryTree(T,ofile);
- else
- NewickPrintTrinaryTree(T,ofile);
-}
-*/
-//edge *depthFirstTraverse(tree *T, edge *e);
-