Next version of JABA
[jabaws.git] / binaries / src / muscle / phyfromclust.cpp
1 #include "muscle.h"\r
2 #include "tree.h"\r
3 #include "clust.h"\r
4 \r
5 void Tree::InitCache(unsigned uCacheCount)\r
6         {\r
7         m_uCacheCount = uCacheCount;\r
8 \r
9         m_uNeighbor1 = new unsigned[m_uCacheCount];\r
10         m_uNeighbor2 = new unsigned[m_uCacheCount];\r
11         m_uNeighbor3 = new unsigned[m_uCacheCount];\r
12 \r
13         m_Ids = new unsigned[m_uCacheCount];\r
14 \r
15         m_dEdgeLength1 = new double[m_uCacheCount];\r
16         m_dEdgeLength2 = new double[m_uCacheCount];\r
17         m_dEdgeLength3 = new double[m_uCacheCount];\r
18         m_dHeight = new double[m_uCacheCount];\r
19 \r
20         m_bHasEdgeLength1 = new bool[m_uCacheCount];\r
21         m_bHasEdgeLength2 = new bool[m_uCacheCount];\r
22         m_bHasEdgeLength3 = new bool[m_uCacheCount];\r
23         m_bHasHeight = new bool[m_uCacheCount];\r
24 \r
25         m_ptrName = new char *[m_uCacheCount];\r
26 \r
27         for (unsigned uNodeIndex = 0; uNodeIndex < m_uNodeCount; ++uNodeIndex)\r
28                 {\r
29                 m_uNeighbor1[uNodeIndex] = NULL_NEIGHBOR;\r
30                 m_uNeighbor2[uNodeIndex] = NULL_NEIGHBOR;\r
31                 m_uNeighbor3[uNodeIndex] = NULL_NEIGHBOR;\r
32                 m_bHasEdgeLength1[uNodeIndex] = false;\r
33                 m_bHasEdgeLength2[uNodeIndex] = false;\r
34                 m_bHasEdgeLength3[uNodeIndex] = false;\r
35                 m_bHasHeight[uNodeIndex] = false;\r
36                 m_dEdgeLength1[uNodeIndex] = dInsane;\r
37                 m_dEdgeLength2[uNodeIndex] = dInsane;\r
38                 m_dEdgeLength3[uNodeIndex] = dInsane;\r
39                 m_dHeight[uNodeIndex] = dInsane;\r
40                 m_ptrName[uNodeIndex] = 0;\r
41                 m_Ids[uNodeIndex] = uInsane;\r
42                 }\r
43         }\r
44 \r
45 void Tree::FromClust(Clust &C)\r
46         {\r
47         Clear();\r
48 \r
49         m_uNodeCount = C.GetNodeCount();\r
50         InitCache(m_uNodeCount);\r
51 \r
52 // Cluster is always rooted. An unrooted cluster\r
53 // is represented by a pseudo-root, which we fix later.\r
54         m_bRooted = true;\r
55         const unsigned uRoot = C.GetRootNodeIndex();\r
56         m_uRootNodeIndex = uRoot;\r
57         m_uNeighbor1[uRoot] = NULL_NEIGHBOR;\r
58         m_bHasEdgeLength1[uRoot] = false;\r
59 \r
60         for (unsigned uNodeIndex = 0; uNodeIndex < m_uNodeCount; ++uNodeIndex)\r
61                 {\r
62                 if (C.IsLeaf(uNodeIndex))\r
63                         {\r
64                         const char *ptrName = C.GetNodeName(uNodeIndex);\r
65                         m_ptrName[uNodeIndex] = strsave(ptrName);\r
66                         m_Ids[uNodeIndex] = C.GetNodeId(uNodeIndex);\r
67                         continue;\r
68                         }\r
69 \r
70                 const unsigned uLeft = C.GetLeftIndex(uNodeIndex);\r
71                 const unsigned uRight = C.GetRightIndex(uNodeIndex);\r
72 \r
73                 const double dLeftLength = C.GetLength(uLeft);\r
74                 const double dRightLength = C.GetLength(uRight);\r
75 \r
76                 m_uNeighbor2[uNodeIndex] = uLeft;\r
77                 m_uNeighbor3[uNodeIndex] = uRight;\r
78 \r
79                 m_dEdgeLength1[uLeft] = dLeftLength;\r
80                 m_dEdgeLength1[uRight] = dRightLength;\r
81 \r
82                 m_uNeighbor1[uLeft] = uNodeIndex;\r
83                 m_uNeighbor1[uRight] = uNodeIndex;\r
84 \r
85                 m_bHasEdgeLength1[uLeft] = true;\r
86                 m_bHasEdgeLength1[uRight] = true;\r
87 \r
88                 m_dEdgeLength2[uNodeIndex] = dLeftLength;\r
89                 m_dEdgeLength3[uNodeIndex] = dRightLength;\r
90 \r
91                 m_bHasEdgeLength2[uNodeIndex] = true;\r
92                 m_bHasEdgeLength3[uNodeIndex] = true;\r
93                 }\r
94         Validate();\r
95         }\r