Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / muscle / phytofile.cpp
1 #include "muscle.h"\r
2 #include "tree.h"\r
3 #include "textfile.h"\r
4 \r
5 unsigned Tree::GetAnyNonLeafNode() const\r
6         {\r
7         for (unsigned uNodeIndex = 0; uNodeIndex < m_uNodeCount; ++uNodeIndex)\r
8                 if (!IsLeaf(uNodeIndex))\r
9                         return uNodeIndex;\r
10         return NULL_NEIGHBOR;\r
11         }\r
12 \r
13 void Tree::ToFile(TextFile &File) const\r
14         {\r
15         if (IsRooted())\r
16                 {\r
17                 ToFileNodeRooted(File, m_uRootNodeIndex);\r
18                 File.PutString(";\n");\r
19                 return;\r
20                 }\r
21 \r
22 // Unrooted.\r
23         unsigned uNodeIndex = GetAnyNonLeafNode();\r
24 \r
25         File.PutString("(\n");\r
26         ToFileNodeUnrooted(File, m_uNeighbor1[uNodeIndex], uNodeIndex);\r
27         File.PutString(",\n");\r
28         ToFileNodeUnrooted(File, m_uNeighbor2[uNodeIndex], uNodeIndex);\r
29         File.PutString(",\n");\r
30         ToFileNodeUnrooted(File, m_uNeighbor3[uNodeIndex], uNodeIndex);\r
31         File.PutString(");\n");\r
32         }\r
33 \r
34 void Tree::ToFileNodeUnrooted(TextFile &File, unsigned uNodeIndex, unsigned uParent) const\r
35         {\r
36         assert(!IsRooted());\r
37 \r
38         bool bGroup = !IsLeaf(uNodeIndex);\r
39         if (bGroup)\r
40                 File.PutString("(\n");\r
41 \r
42         if (IsLeaf(uNodeIndex))\r
43                 File.PutString(GetName(uNodeIndex));\r
44         else\r
45                 {\r
46                 ToFileNodeUnrooted(File, GetFirstNeighbor(uNodeIndex, uParent), uNodeIndex);\r
47                 File.PutString(",\n");\r
48                 ToFileNodeUnrooted(File, GetSecondNeighbor(uNodeIndex, uParent), uNodeIndex);\r
49                 }\r
50 \r
51         if (bGroup)\r
52                 File.PutString(")");\r
53 \r
54         if (HasEdgeLength(uNodeIndex, uParent))\r
55                 File.PutFormat(":%g", GetEdgeLength(uNodeIndex, uParent));\r
56         File.PutString("\n");\r
57         }\r
58 \r
59 void Tree::ToFileNodeRooted(TextFile &File, unsigned uNodeIndex) const\r
60         {\r
61         assert(IsRooted());\r
62 \r
63         bool bGroup = !IsLeaf(uNodeIndex) || IsRoot(uNodeIndex);\r
64         if (bGroup)\r
65                 File.PutString("(\n");\r
66 \r
67         if (IsLeaf(uNodeIndex))\r
68                 File.PutString(GetName(uNodeIndex));\r
69         else\r
70                 {\r
71                 ToFileNodeRooted(File, GetLeft(uNodeIndex));\r
72                 File.PutString(",\n");\r
73                 ToFileNodeRooted(File, GetRight(uNodeIndex));\r
74                 }\r
75 \r
76         if (bGroup)\r
77                 File.PutString(")");\r
78 \r
79         if (!IsRoot(uNodeIndex))\r
80                 {\r
81                 unsigned uParent = GetParent(uNodeIndex);\r
82                 if (HasEdgeLength(uNodeIndex, uParent))\r
83                         File.PutFormat(":%g", GetEdgeLength(uNodeIndex, uParent));\r
84                 }\r
85         File.PutString("\n");\r
86         }\r