--- /dev/null
+#include "muscle.h"\r
+#include "tree.h"\r
+#include "textfile.h"\r
+\r
+unsigned Tree::GetAnyNonLeafNode() const\r
+ {\r
+ for (unsigned uNodeIndex = 0; uNodeIndex < m_uNodeCount; ++uNodeIndex)\r
+ if (!IsLeaf(uNodeIndex))\r
+ return uNodeIndex;\r
+ return NULL_NEIGHBOR;\r
+ }\r
+\r
+void Tree::ToFile(TextFile &File) const\r
+ {\r
+ if (IsRooted())\r
+ {\r
+ ToFileNodeRooted(File, m_uRootNodeIndex);\r
+ File.PutString(";\n");\r
+ return;\r
+ }\r
+\r
+// Unrooted.\r
+ unsigned uNodeIndex = GetAnyNonLeafNode();\r
+\r
+ File.PutString("(\n");\r
+ ToFileNodeUnrooted(File, m_uNeighbor1[uNodeIndex], uNodeIndex);\r
+ File.PutString(",\n");\r
+ ToFileNodeUnrooted(File, m_uNeighbor2[uNodeIndex], uNodeIndex);\r
+ File.PutString(",\n");\r
+ ToFileNodeUnrooted(File, m_uNeighbor3[uNodeIndex], uNodeIndex);\r
+ File.PutString(");\n");\r
+ }\r
+\r
+void Tree::ToFileNodeUnrooted(TextFile &File, unsigned uNodeIndex, unsigned uParent) const\r
+ {\r
+ assert(!IsRooted());\r
+\r
+ bool bGroup = !IsLeaf(uNodeIndex);\r
+ if (bGroup)\r
+ File.PutString("(\n");\r
+\r
+ if (IsLeaf(uNodeIndex))\r
+ File.PutString(GetName(uNodeIndex));\r
+ else\r
+ {\r
+ ToFileNodeUnrooted(File, GetFirstNeighbor(uNodeIndex, uParent), uNodeIndex);\r
+ File.PutString(",\n");\r
+ ToFileNodeUnrooted(File, GetSecondNeighbor(uNodeIndex, uParent), uNodeIndex);\r
+ }\r
+\r
+ if (bGroup)\r
+ File.PutString(")");\r
+\r
+ if (HasEdgeLength(uNodeIndex, uParent))\r
+ File.PutFormat(":%g", GetEdgeLength(uNodeIndex, uParent));\r
+ File.PutString("\n");\r
+ }\r
+\r
+void Tree::ToFileNodeRooted(TextFile &File, unsigned uNodeIndex) const\r
+ {\r
+ assert(IsRooted());\r
+\r
+ bool bGroup = !IsLeaf(uNodeIndex) || IsRoot(uNodeIndex);\r
+ if (bGroup)\r
+ File.PutString("(\n");\r
+\r
+ if (IsLeaf(uNodeIndex))\r
+ File.PutString(GetName(uNodeIndex));\r
+ else\r
+ {\r
+ ToFileNodeRooted(File, GetLeft(uNodeIndex));\r
+ File.PutString(",\n");\r
+ ToFileNodeRooted(File, GetRight(uNodeIndex));\r
+ }\r
+\r
+ if (bGroup)\r
+ File.PutString(")");\r
+\r
+ if (!IsRoot(uNodeIndex))\r
+ {\r
+ unsigned uParent = GetParent(uNodeIndex);\r
+ if (HasEdgeLength(uNodeIndex, uParent))\r
+ File.PutFormat(":%g", GetEdgeLength(uNodeIndex, uParent));\r
+ }\r
+ File.PutString("\n");\r
+ }\r