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