File: Tree.cc

package info (click to toggle)
proda 1.0-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 584 kB
  • sloc: cpp: 8,549; xml: 163; makefile: 109; sh: 12
file content (261 lines) | stat: -rw-r--r-- 7,248 bytes parent folder | download | duplicates (4)
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
//////////////////////////////////////////////////////////////////////
// Tree.cc
//////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include "Tree.h"
#include "GlobalAlign.h"

//////////////////////////////////////////////////////////////////////
// TreeNode constructor
//////////////////////////////////////////////////////////////////////

Tree::TreeNode::TreeNode (bool isLeaf, int numSequences, MultiSequence *seqs,
			  TreeNode *leftChild, TreeNode *rightChild) : 
  isLeaf(isLeaf), numSequences(numSequences), seqs(seqs),
  leftChild(leftChild), rightChild(rightChild) {}

//////////////////////////////////////////////////////////////////////
// TreeNode destructor
//////////////////////////////////////////////////////////////////////

Tree::TreeNode::~TreeNode (){
  if (seqs) delete seqs;
  if (!isLeaf){
    if (leftChild) delete leftChild;
    if (rightChild) delete rightChild;
  }
}

//////////////////////////////////////////////////////////////////////
// Returns whether this node is a leaf or not
//////////////////////////////////////////////////////////////////////

const bool Tree::TreeNode::GetIsLeaf() const {
  return isLeaf;
}

//////////////////////////////////////////////////////////////////////
// Returns number of sequences in this subtree
//////////////////////////////////////////////////////////////////////

const int Tree::TreeNode::GetNumSequences() const {
  return numSequences;
}

//////////////////////////////////////////////////////////////////////
// Returns the MultiSequence data associated with this node.
//////////////////////////////////////////////////////////////////////

const MultiSequence *Tree::TreeNode::GetSequences() const {
  return seqs;
}

//////////////////////////////////////////////////////////////////////
// Returns the left child TreeNode
//////////////////////////////////////////////////////////////////////

const Tree::TreeNode *Tree::TreeNode::GetLeftChild() const {
  return leftChild;
}

//////////////////////////////////////////////////////////////////////
// Returns the right child TreeNode
//////////////////////////////////////////////////////////////////////

const Tree::TreeNode *Tree::TreeNode::GetRightChild() const {
  return rightChild;
}

//////////////////////////////////////////////////////////////////////
// Print out subtree recursively
//////////////////////////////////////////////////////////////////////

void Tree::TreeNode::Print (FILE *file) const {
  if (isLeaf){
    fprintf (file, "%s", seqs->GetSequence(0).GetName());
  } else {
    fprintf (file, "(");
    leftChild->Print(file);
    if (leftChild->isLeaf || rightChild->isLeaf)
      fprintf (file, " ");
    rightChild->Print(file);
    fprintf (file, ")");
  }
}

//////////////////////////////////////////////////////////////////////
// Perform progressive alignment for subtree
//////////////////////////////////////////////////////////////////////

void Tree::TreeNode::ProgressiveAlignment (int numSequences, SparseMatrix **posteriors){

  if (seqs) return;

  leftChild->ProgressiveAlignment (numSequences, posteriors);
  rightChild->ProgressiveAlignment (numSequences, posteriors);

  seqs = GlobalAlign::AlignGroups (numSequences, posteriors, 
				   *leftChild->seqs, *rightChild->seqs);
  seqs->Sort();
}

void Tree::TreeNode::GetIDs(IVECT &ids)
{
	if(isLeaf){
		ids.push_back(seqs->GetSequence(0).GetID());
		Sequence *ptr = seqs->GetSequencePtr(0);
		ptr->SetID(ids.size()-1);
	}
	else{
		leftChild->GetIDs(ids);
		rightChild->GetIDs(ids);
	}
}

void Tree::TreeNode::UpdateIDs(int *used)
{
		
	if(isLeaf){

		Sequence *ptr = seqs->GetSequencePtr(0);
		ptr->SetID(used[ptr->GetID()]);
	}
	else{
		leftChild->UpdateIDs(used);
		rightChild->UpdateIDs(used);
	}
}

//////////////////////////////////////////////////////////////////////
// Tree constructor
//////////////////////////////////////////////////////////////////////

typedef Tree::TreeNode *TreeNodePtr;

Tree::Tree (Matrix similarity, const MultiSequence &seqs, float threshold){
  // get number of sequences
  
  int n = similarity.GetNumRows();
  ASSERT (n == similarity.GetNumCols(), "Similarity matrix not square.");
  
  int i,j,k;
  // initialize diagonal of distance matrix
  for (i = 0; i < n; i++)
    similarity(0,i,i) = -1;
  
  // build initial set of trees
  
  TreeNode **trees = new TreeNodePtr[n];
  ASSERT (trees, "Out of memory.");
  
  for (i = 0; i < n; i++){
    MultiSequence *tempSeqs = new MultiSequence();
    ASSERT (tempSeqs, "Out of memory.");
    Sequence *seq = new Sequence (seqs.GetSequence (i));
    ASSERT (seq, "Out of memory.");
    tempSeqs->AddSequence (seq);
    trees[i] = new TreeNode (true, 1, tempSeqs, NULL, NULL);
  }
  
  // perform n - 1 merges
  
  for (k = 0; k < n-1; k++){
    
    // find nearest neighbors
    
    int bi = 0, bj = 0;
    for (i = 0; i < n; i++){
      for (j = i+1; j < n; j++) if (i != j){
	if (similarity(0,i,j) > similarity(0,bi,bj)){
	  bi = i;
	  bj = j;
	}
      }
    }
	//stop if similarity drops bellow the threshold
	int numSeq = trees[bi]->GetNumSequences() + trees[bj]->GetNumSequences();
	if((similarity(0,bi,bj) < threshold && numSeq <= 2) ||
	   similarity(0,bi,bj) < threshold - 0.2) break;

    // merge trees
    
    TreeNode *temp = new TreeNode (false, numSeq,
				   NULL, trees[bi], trees[bj]);
    ASSERT (temp, "Out of memory.");
    trees[bi] = temp;
    trees[bj] = NULL;
    
    // update distances
    similarity(0,bi,bj) = similarity(0,bj,bi) = -1;
    for (int m = 0; m < n; m++) if (m != bi && m != bj){
      similarity(0,bi,m) = similarity(0,m,bi) = 
	(similarity(0,bi,m) * temp->GetLeftChild()->GetNumSequences() + 
	 similarity(0,bj,m) * temp->GetRightChild()->GetNumSequences()) /
	temp->GetNumSequences();
      similarity(0,bj,m) = similarity(0,m,bj) = -1;
    }
  }
  
  root = trees[0];
  if( root->GetNumSequences() == 1) {
	  for (i = 1; i < n; i++){
		if (trees[i] != NULL && trees[i]->GetNumSequences() > root->GetNumSequences()){
			root = trees[i];
		}
	  }
  }
  for (i = 0; i < n; i++){
	  if(trees[i] && trees[i] != root)
		  delete trees[i];
  }
  delete[] trees;
}

//////////////////////////////////////////////////////////////////////
// Tree destructor
//////////////////////////////////////////////////////////////////////

Tree::~Tree (){
  delete root;
}

//////////////////////////////////////////////////////////////////////
// Print tree
//////////////////////////////////////////////////////////////////////

void Tree::Print (FILE *file) const {
  ASSERT (root, "Tree not created.");

  root->Print (file);
  fprintf (file, "\n");
}

//////////////////////////////////////////////////////////////////////
// Perform progressive alignment on the tree
//////////////////////////////////////////////////////////////////////

MultiSequence *Tree::ProgressiveAlignment (int numSequences, SparseMatrix **posteriors){
  root->ProgressiveAlignment (numSequences, posteriors);
  return new MultiSequence(*root->GetSequences());
  
}

int Tree::GetNumSequences()
{
	if(root)
		return root->GetNumSequences();
	else return 0;
}


void Tree::GetIDs(IVECT &ids)
{
	root->GetIDs(ids);
}

void Tree::UpdateIDs(int *used)
{
	root->UpdateIDs(used);
}