-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMSAClusterTree.cpp
executable file
·330 lines (293 loc) · 10.6 KB
/
MSAClusterTree.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/////////////////////////////////////////////////////////////////
// MSAClusterTree.cpp
//
// Routines for UPGMA guide tree
//
/////////////////////////////////////////////////////////////////
#include "MSAClusterTree.h"
MSAClusterTree::MSAClusterTree(MSA* msa, VVF& distMatrix, int numSeqs) :
MSAGuideTree(msa, distMatrix, numSeqs)
{
}
MSAClusterTree::~MSAClusterTree()
{
}
void MSAClusterTree::create()
{
//generate the neighbor-joining tree
this->generateClusterTree();
//calculate sequence weights
this->getSeqsWeights();
//construct the alignment orders
this->createAlignmentOrders();
}
void MSAClusterTree::create(int second_step)
{
//generate the neighbor-joining tree
this->generateClusterTree(second_step);
//calculate sequence weights
this->getSeqsWeights();
//construct the alignment orders
this->createAlignmentOrders();
}
void MSAClusterTree::generateClusterTree()
{
int i;
ValidNode* validNodes, *headValidNodes;
ValidNode* miniPtr, *minjPtr, *ivalid, *jvalid;
int mini, minj;
float* joins;
unsigned int* clusterLeafs;
//initialize the valid nodes link list
validNodes = new ValidNode[leafsNum + 1];
joins = new float[leafsNum + 1];
clusterLeafs = new unsigned int[nodesNum + 1];
if (!validNodes || !joins || !clusterLeafs)
{
cerr << "Out of memory of the reconstruction of cluster tree" << endl;
}
//initialize cluster size
for (i = 0; i < this->leafsNum; i++)
{
clusterLeafs[i] = 1;
}
headValidNodes = &validNodes[0];
headValidNodes->next = &validNodes[1];
headValidNodes->n = -1;
headValidNodes->node = -1;
headValidNodes->prev = NULL;
//build an initial link list
ValidNode* curr = &validNodes[1];
ValidNode* prev = headValidNodes;
ValidNode* next = &validNodes[2];
for (i = 0; i < leafsNum; i++)
{
curr->n = i;
curr->node = i;
curr->prev = prev;
curr->next = next;
prev = curr;
curr = next;
next++;
}
prev->next = NULL;
//to generate the cluster tree
int nodeIdx; //the index of an internal node
int firstNode = leafsNum; //the index of the first internal node
int lastNode = firstNode + leafsNum - 1;//the index of the last internal node
for (nodeIdx = firstNode; nodeIdx < lastNode; nodeIdx++)
{
//find closest pair of clusters
float minDist = 1.1f;
miniPtr = headValidNodes;
minjPtr = headValidNodes;
for (ivalid = headValidNodes->next; ivalid != NULL;
ivalid = ivalid->next)
{
mini = ivalid->n;
for (jvalid = headValidNodes->next;
jvalid != NULL && jvalid->n < mini; jvalid = jvalid->next)
{
minj = jvalid->n;
float dist = (*distMatrix)[mini][minj];
if (dist < 0)
{
cerr
<< "ERROR: It is impossible to have distance value less than zero"
<< endl;
dist = 0;
}
if (dist < minDist)
{
minDist = dist;
miniPtr = ivalid;
minjPtr = jvalid;
}
//printf("dist %g mini %d minj %d\n", dist, ivalid->node, jvalid->node);
}
}
//printf("**** mini %d minj %d minDist %g *****\n", miniPtr->node, minjPtr->node, minDist);
//check the validity of miniPtr and minjPtr;
if (miniPtr == headValidNodes || minjPtr == headValidNodes)
{
cerr << "OOPS: Error occurred while constructing the cluster tree\n"
<< endl;
exit(-1);
}
//computing branch length and join the two nodes
float branchLength = minDist * 0.5f;
this->connectNodes(&nodes[nodeIdx], nodeIdx, &nodes[miniPtr->node],
branchLength, &nodes[minjPtr->node], branchLength);
clusterLeafs[nodeIdx] = clusterLeafs[miniPtr->node]
+ clusterLeafs[minjPtr->node];
//remove the valid node minjPtr from the list
minjPtr->prev->next = minjPtr->next;
if (minjPtr->next != NULL)
{
minjPtr->next->prev = minjPtr->prev;
}
minjPtr->prev = minjPtr->next = NULL;
//compute the distance of each remaining valid node to the new node
for (ivalid = headValidNodes->next; ivalid != NULL;
ivalid = ivalid->next)
{
int idx = ivalid->n;
float idist = (*distMatrix)[miniPtr->n][idx];
float jdist = (*distMatrix)[minjPtr->n][idx];
unsigned int isize = clusterLeafs[miniPtr->node];
unsigned int jsize = clusterLeafs[minjPtr->node];
joins[idx] = (idist * isize + jdist * jsize) / (isize + jsize);
//joins[idx] = (idist + jdist )/ 2;
}
//update the distance to the new node
miniPtr->node = nodeIdx;
mini = miniPtr->n;
for (jvalid = headValidNodes->next; jvalid != NULL;
jvalid = jvalid->next)
{
minj = jvalid->n;
float dist = joins[minj];
(*distMatrix)[mini][minj] = dist;
(*distMatrix)[minj][mini] = dist;
}
}
//add a pseudo root to this unrooted NJ tree
this->root = &nodes[lastNode - 1];
delete[] validNodes;
delete[] joins;
delete[] clusterLeafs;
}
//new add
void MSAClusterTree::generateClusterTree(int second_step)
{
int i;
ValidNode* validNodes, *headValidNodes;
ValidNode* miniPtr, *minjPtr, *ivalid, *jvalid;
int mini, minj;
float* joins;
unsigned int* clusterLeafs;
//initialize the valid nodes link list
validNodes = new ValidNode[leafsNum + 1];
joins = new float[leafsNum + 1];
clusterLeafs = new unsigned int[nodesNum + 1];
if (!validNodes || !joins || !clusterLeafs)
{
cerr << "Out of memory of the reconstruction of cluster tree" << endl;
}
//initialize cluster size
for (i = 0; i < this->leafsNum; i++)
{
clusterLeafs[i] = 1;
}
headValidNodes = &validNodes[0];
headValidNodes->next = &validNodes[1];
headValidNodes->n = -1;
headValidNodes->node = -1;
headValidNodes->prev = NULL;
//build an initial link list
ValidNode* curr = &validNodes[1];
ValidNode* prev = headValidNodes;
ValidNode* next = &validNodes[2];
for (i = 0; i < leafsNum; i++)
{
curr->n = i;
curr->node = i;
curr->prev = prev;
curr->next = next;
prev = curr;
curr = next;
next++;
}
prev->next = NULL;
//to generate the cluster tree
int nodeIdx; //the index of an internal node
int firstNode = leafsNum; //the index of the first internal node
int lastNode = firstNode + leafsNum - 1;//the index of the last internal node
for (nodeIdx = firstNode; nodeIdx < lastNode; nodeIdx++)
{
//find closest pair of clusters
float minDist = 1.1f;
miniPtr = headValidNodes;
minjPtr = headValidNodes;
for (ivalid = headValidNodes->next; ivalid != NULL;
ivalid = ivalid->next)
{
mini = ivalid->n;
for (jvalid = headValidNodes->next;
jvalid != NULL && jvalid->n < mini; jvalid = jvalid->next)
{
minj = jvalid->n;
float dist = (*distMatrix)[mini][minj];
if (dist < 0)
{
cerr
<< "ERROR: It is impossible to have distance value less than zero"
<< endl;
dist = 0;
}
if (dist < minDist)
{
minDist = dist;
miniPtr = ivalid;
minjPtr = jvalid;
}
//printf("dist %g mini %d minj %d\n", dist, ivalid->node, jvalid->node);
}
}
//printf("**** mini %d minj %d minDist %g *****\n", miniPtr->node, minjPtr->node, minDist);
//check the validity of miniPtr and minjPtr;
if (miniPtr == headValidNodes || minjPtr == headValidNodes)
{
cerr << "OOPS: Error occurred while constructing the cluster tree\n"
<< endl;
exit(-1);
}
//computing branch length and join the two nodes
float branchLength = minDist * 0.5f;
this->connectNodes(&nodes[nodeIdx], nodeIdx, &nodes[miniPtr->node],
branchLength, &nodes[minjPtr->node], branchLength);
clusterLeafs[nodeIdx] = clusterLeafs[miniPtr->node]
+ clusterLeafs[minjPtr->node];
//remove the valid node minjPtr from the list
minjPtr->prev->next = minjPtr->next;
if (minjPtr->next != NULL)
{
minjPtr->next->prev = minjPtr->prev;
}
minjPtr->prev = minjPtr->next = NULL;
//compute the distance of each remaining valid node to the new node
for (ivalid = headValidNodes->next; ivalid != NULL;
ivalid = ivalid->next)
{
int idx = ivalid->n;
float idist = (*distMatrix)[miniPtr->n][idx];
float jdist = (*distMatrix)[minjPtr->n][idx];
unsigned int isize = clusterLeafs[miniPtr->node];
unsigned int jsize = clusterLeafs[minjPtr->node];
if(second_step == 1){
//cerr << "Step 2: using WPGMA." << endl;
joins[idx] = (idist + jdist )/ 2;
}
else{
//cerr << "Step 2: using UPGMA." << endl;
joins[idx] = (idist * isize + jdist * jsize) / (isize + jsize);
}
}
//update the distance to the new node
miniPtr->node = nodeIdx;
mini = miniPtr->n;
for (jvalid = headValidNodes->next; jvalid != NULL;
jvalid = jvalid->next)
{
minj = jvalid->n;
float dist = joins[minj];
(*distMatrix)[mini][minj] = dist;
(*distMatrix)[minj][mini] = dist;
}
}
//add a pseudo root to this unrooted NJ tree
this->root = &nodes[lastNode - 1];
delete[] validNodes;
delete[] joins;
delete[] clusterLeafs;
}