forked from kuangmeng/DLPAlign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSAGuideTree.cpp
374 lines (352 loc) · 11.2 KB
/
MSAGuideTree.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
////////////////////////////////////////////////////////////////
// MSAGuideTree.cpp
//
// Utilities for tree data structure
/////////////////////////////////////////////////////////////////
#include "MSAGuideTree.h"
#include "MSA.h"
MSAGuideTree::MSAGuideTree(MSA* msa, VVF& distances, int numSeqs)
{
int i;
TreeNode* node;
//system configuration
this->msa = msa;
this->distMatrix = &distances;
this->numSeqs = numSeqs;
this->seqsWeights = msa->getSeqsWeights();
//tree structure
this->nodesSize = this->numSeqs * 2 + 1;
this->nodes = new TreeNode[this->nodesSize];
if (!this->nodes)
{
cerr << "TreeNodes memory allocation failed" << endl;
exit(-1);
}
//initialize all the tree nodes
this->leafs = this->nodes;
this->leafsNum = this->numSeqs;
this->nodesNum = 2 * this->leafsNum - 1;
for (i = 0; i < this->nodesSize; i++)
{
node = &nodes[i];
node->left = 0;
node->right = 0;
node->parent = 0;
node->leftIdx = -1;
node->rightIdx = -1;
node->parentIdx = -1;
node->idx = -1;
node->dist = 0;
node->leaf = NODE; //setted to be NODE, by default
node->order = 0;
node->depth = 0;
}
//initialize the leaf nodes
for (i = 0; i < this->leafsNum; i++)
{
node = &this->leafs[i];
node->idx = i;
node->leaf = LEAF;
}
}
MSAGuideTree::~MSAGuideTree()
{
//release tree nodes
delete[] this->nodes;
//release alignment orders
releaseAlignmentOrders();
}
//get the tree nodes
TreeNode* MSAGuideTree::getNodes()
{
return nodes;
}
//get the leaf nodes
TreeNode* MSAGuideTree::getLeafs()
{
return leafs;
}
//get the number of nodes;
int MSAGuideTree::getNodesNum()
{
return nodesNum;
}
//get the number of leaf nodes
int MSAGuideTree::getLeafsNum()
{
return leafsNum;
}
//get the alignment orders
AlignmentOrder* MSAGuideTree::getAlignOrders()
{
return alignOrders;
}
int MSAGuideTree::getAlignOrdersNum()
{
return alignOrdersNum;
}
/****************************************************
create the evolutionary relationship
****************************************************/
void MSAGuideTree::connectNodes(TreeNode* parent, int parentIdx,
TreeNode* leftChild, float leftDist, TreeNode* rightChild,
float rightDist)
{
//save the parents index for each child
leftChild->parent = parent;
leftChild->parentIdx = parentIdx;
rightChild->parent = parent;
rightChild->parentIdx = parentIdx;
//save the branch lengths (i.e. distance) from each child to its parent
leftChild->dist = leftDist;
rightChild->dist = rightDist;
//save the indices of itself and its children for this new tree node
parent->idx = parentIdx;
parent->left = leftChild;
parent->leftIdx = leftChild->idx;
parent->right = rightChild;
parent->rightIdx = rightChild->idx;
}
/*****************************************
compute the alignment order of the phylogentic tree
*****************************************/
void MSAGuideTree::createAlignmentOrders()
{
int i;
AlignmentOrder* order;
//allocate memory space for alignment orders vector
this->alignOrdersNum = 0;//for alignment orders, it starts from 1 instead of 0
this->alignOrdersSize = numSeqs;//the number of internal nodes of the phylogentic tree + 1
this->alignOrders = new AlignmentOrder[this->alignOrdersSize];
if (!this->alignOrders)
{
cerr << "OOPS: Alignment orders memory allocation failed" << endl;
exit(-1);
}
//initialize the alignment orders vector
for (i = 0; i < this->alignOrdersSize; i++)
{
order = &this->alignOrders[i];
order->leftOrder = 0;
order->rightOrder = 0;
order->leftLeafs = 0;
order->leftNum = 0;
order->rightLeafs = 0;
order->rightNum = 0;
}
//starting out constructing the alignment orders
int subLeafsNum;
int nodeDepth = 1;
int subOrder = recursiveCreateAlignmentOrders(this->root, 0, subLeafsNum,
nodeDepth);
//check whether the function works well
if (subLeafsNum != numSeqs || this->alignOrdersNum != subOrder)
{
fprintf(stderr,
"The alignment orders constructed were wrong (subLeafsNum %d, alignOrdersNum %d, subOrder %d)\n",
subLeafsNum, alignOrdersNum, subOrder);
}
}
int MSAGuideTree::recursiveCreateAlignmentOrders(TreeNode* subRoot,
int* subLeafs, int& subLeafsNum, int nodeDepth)
{
int leftNum, rightNum;
int leftOrder, rightOrder;
int* leftLeafs, *rightLeafs;
if (subRoot->leaf == LEAF)
{
subLeafs[0] = subRoot->idx;
subLeafsNum = 1;
return 0; //if it is a leaf, return the index 0
}
leftOrder = rightOrder = 0;
leftNum = rightNum = 0;
leftLeafs = new int[numSeqs];
rightLeafs = new int[numSeqs];
//check the left subtree
if (subRoot->left)
{
//recursively tranverse the left subtree
leftOrder = recursiveCreateAlignmentOrders(subRoot->left, leftLeafs,
leftNum, nodeDepth + 1);
}
//check the right subtree
if (subRoot->right)
{
rightOrder = recursiveCreateAlignmentOrders(subRoot->right, rightLeafs,
rightNum, nodeDepth + 1);
}
//save the leafs in the left and right subtrees of the current subtree
if (this->alignOrdersNum > this->alignOrdersSize)
{
fprintf(stderr, "the alignment order function works bad\n");
\
exit(-1);
}
AlignmentOrder* order = &this->alignOrders[++this->alignOrdersNum];
order->nodeDepth = nodeDepth;
order->leftOrder = leftOrder;
order->rightOrder = rightOrder;
order->leftNum = leftNum;
order->rightNum = rightNum;
order->leftLeafs = new int[order->leftNum];
order->rightLeafs = new int[order->rightNum];
if (!order->leftLeafs || !order->rightLeafs)
{
fprintf(stderr,
"memory allocation failed while recursively constructing alignment orders\n");
exit(-1);
}
memcpy(order->leftLeafs, leftLeafs, order->leftNum * sizeof(int));
memcpy(order->rightLeafs, rightLeafs, order->rightNum * sizeof(int));
delete[] leftLeafs;
delete[] rightLeafs;
//for the root of the tree, subLeafs buffer is set to 0
if (subLeafs)
{
//copy the results to the parent tree node
memcpy(subLeafs, order->leftLeafs, order->leftNum * sizeof(int));
memcpy(subLeafs + order->leftNum, order->rightLeafs,
order->rightNum * sizeof(int));
}
//compute the total number of leafs in this subtree
subLeafsNum = order->leftNum + order->rightNum;
return this->alignOrdersNum;//return the index of itself, starting from 1, instead of 0
}
void MSAGuideTree::releaseAlignmentOrders()
{
if (!this->alignOrders)
{
return;
}
for (int i = 0; i < this->alignOrdersNum; i++)
{
AlignmentOrder* order = &this->alignOrders[i];
if (order->leftLeafs)
{
delete[] order->leftLeafs;
}
if (order->rightLeafs)
{
delete[] order->rightLeafs;
}
}
delete[] alignOrders;
}
/********************************
display the alignment orders
********************************/
void MSAGuideTree::displayAlignmentOrders()
{
int i, j;
AlignmentOrder* order;
fprintf(stderr, "************DISPLAY ALIGNMENT ORDER***************\n");
for (i = 1; i <= this->alignOrdersNum; i++)
{
order = &this->alignOrders[i];
fprintf(stderr, "GROUP (%d depth %d):\n---LEFT ORDER: %d\n", i,
order->nodeDepth, order->leftOrder);
fprintf(stderr, "---LEFT: ");
for (j = 0; j < order->leftNum; j++)
{
fprintf(stderr, "%d ", order->leftLeafs[j]);
}
fprintf(stderr, "\n---RIGHT ORDER: %d\n", order->rightOrder);
fprintf(stderr, "\n---RIGHT: ");
for (j = 0; j < order->rightNum; j++)
{
fprintf(stderr, "%d ", order->rightLeafs[j]);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "*******************************************\n");
}
/*********************************
display the tree
*********************************/
void MSAGuideTree::displayTree()
{
fprintf(stderr, "**************DISPLAY TREE*********************\n");
for (int i = 0; i < nodesNum; i++)
{
TreeNode* node = &nodes[i];
fprintf(stderr,
"%d(%p): left(%p) %d, right(%p) %d, parent(%p) %d, dist %f\n",
(node == &nodes[node->idx]) ? node->idx : -2, node, node->left,
(!node->left || node->left == &nodes[node->leftIdx]) ?
node->leftIdx : -2, node->right,
(!node->right || node->right == &nodes[node->rightIdx]) ?
node->rightIdx : -2, node->parent,
(!node->parent || node->parent == &nodes[node->parentIdx]) ?
node->parentIdx : -2, node->dist);
}
fprintf(stderr, "*******************************************\n");
}
/*********************************
compute the sequence weights
*********************************/
void MSAGuideTree::getSeqsWeights()
{
int i;
TreeNode* curr;
//compute the order of each node, which represents the number of leaf nodes in the substree rooting from it.
for (i = 0; i < leafsNum; i++)
{
//for each leaf nodes
curr = &this->leafs[i];
while (curr != 0)
{
curr->order++;
curr = curr->parent;
}
}
//compute the weight of each sequence, which corresponds to a leaf node
for (i = 0; i < numSeqs; i++)
{
//compute the weight of each sequence
float weights = 0;
curr = &this->leafs[i];
while (curr->parent != 0)
{
weights += curr->dist / curr->order;
curr = curr->parent;
//printf("order:%d weights: %f\n", curr->order, weights);
}
//save the weight of this sequence
seqsWeights[i] = (int) (100 * weights);
//printf("%d\n", seqsWeights[i]);
}
//normalize the weights
int wsum = 0;
for (i = 0; i < numSeqs; i++)
{
wsum += seqsWeights[i];
}
if (wsum == 0)
{
//in this case, every sequence is assumed to have an identical weight
for (i = 0; i < numSeqs; i++)
{
seqsWeights[i] = 1;
}
wsum = numSeqs;
}
//printf("wsum:%d \n", wsum);
for (i = 0; i < numSeqs; i++)
{
seqsWeights[i] = (seqsWeights[i] * INT_MULTIPLY) / wsum;
if (seqsWeights[i] < 1)
{
seqsWeights[i] = 1;
}
//printf("%d \n", seqsWeights[i]);
}
}
void MSAGuideTree::create()
{
//do nothing
}
void MSAGuideTree::create(int variance)
{
//do nothing
}