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