File: GlobalAlign.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 (229 lines) | stat: -rw-r--r-- 6,684 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
//////////////////////////////////////////////////////////////////////
// GlobalAlign.cc
//////////////////////////////////////////////////////////////////////

#include <string.h>
#include "Assert.h"
#include "GlobalAlign.h"
#include "Utilities.h"

//////////////////////////////////////////////////////////////////////
// Compute maximum weight trace
//////////////////////////////////////////////////////////////////////

char *GlobalAlign::ComputeMWTrace (const Matrix &m, float *score, int *length){
  const int rows = m.GetNumRows();
  const int cols = m.GetNumCols();
  int i,j;
  int beginx, beginy, endx, endy;
  beginx = beginy = endx = endy = -1;
  // memory allocation
  Matrix *bestp = new Matrix (1, rows, cols);
  ASSERT (bestp, "Out of memory.");
  Matrix &best = *bestp;
  TracebackType *traceback = new TracebackType[rows * cols];
  ASSERT (traceback, "Out of memory.");
  
  // compute best path
  best.Fill (LOG_ZERO_FLOAT);
  best(0,0,0) = 0;
  for (i = 0; i < rows; i++){
    for (j = 0; j < cols; j++){
      traceback[i * cols + j] = NONE;
      if (i > 0 && best(0,i-1,j) > best(0,i,j)){
	best(0,i,j) = best(0,i-1,j);
	traceback[i * cols + j] = UP;
      }
      if (j > 0 && best(0,i,j-1) > best(0,i,j)){
	best(0,i,j) = best(0,i,j-1);
	traceback[i * cols + j] = LEFT;
      }
      if (i > 0 && j > 0 && best(0,i-1,j-1) + m(0,i,j) > best(0,i,j)){
	best(0,i,j) = best(0,i-1,j-1) + m(0,i,j);
	traceback[i * cols + j] = UP_LEFT;
	if(beginx == -1) {beginx = i;beginy = j;}
	endx = i; endy = j;
      }
    }
  }

  if (score){
	  *score = best(0,rows-1,cols-1);
	  *length = min(endy - beginy, endx-beginx) + 1;
  }
  
  delete bestp;
  
  // follow tracebacks
  char *buffer = new char[rows * cols];
  ASSERT (buffer, "Out of memory.");
  int r = rows-1, c = cols-1, len = 0;
  while (traceback[r * cols + c] != NONE){
    switch (traceback[r * cols + c]){
    case UP: r--; buffer[len++] = 'X'; break;
    case LEFT: c--; buffer[len++] = 'Y'; break;
    case UP_LEFT: r--; c--; buffer[len++] = 'B'; break;
    default: ASSERT (false, "Unexpected value found in traceback matrix!");
    }
  }
  
  delete[] traceback;
  
  // reverse alignment path
  char *ret = new char[len+1];
  ASSERT (ret, "Out of memory.");
  for (i = 0; i < len; i++)
    ret[i] = buffer[len - 1 - i];
  ret[len] = '\0';
  
  delete[] buffer;
  
  return ret;
}


//////////////////////////////////////////////////////////////////////
// Insert gaps into aligned sequence
//////////////////////////////////////////////////////////////////////

Sequence *GlobalAlign::InsertGaps (const Sequence &seq, const char *alignmentPath, char ch){
  int len = strlen (alignmentPath);
  const char *data = seq.GetData();
  char *newData = new char[len+2];
  ASSERT (newData, "Out of memory.");

  char *newName = new char[strlen(seq.GetName())+1];
  ASSERT (newName, "Out of memory.");

  newData[0] = '@';
  newData[len+1] = '\0';

  int j = 1;
  for (int i = 0; i < len; i++){
    if (alignmentPath[i] == ch || alignmentPath[i] == 'B')
      newData[i+1] = data[j++];
    else
      newData[i+1] = '-';
  }

  memcpy (newName, seq.GetName(), strlen(seq.GetName())+1);
  
  return new Sequence (newData, newName, len, seq.GetID());
}

//////////////////////////////////////////////////////////////////////
// Build alignment from alignment path
//////////////////////////////////////////////////////////////////////

MultiSequence *GlobalAlign::BuildAlignment (const MultiSequence &group1, 
					    const MultiSequence &group2, 
					    const char *alignmentPath){
  MultiSequence *ret = new MultiSequence();
  ASSERT (ret, "Out of memory.");
  
  for (int i = 0; i < group1.GetNumSequences(); i++)
    ret->AddSequence (InsertGaps (group1.GetSequence(i), alignmentPath, 'X'));
  {for (int i = 0; i < group2.GetNumSequences(); i++)
	  ret->AddSequence (InsertGaps (group2.GetSequence(i), alignmentPath, 'Y'));}
  
  return ret;
}

//////////////////////////////////////////////////////////////////////
// Align two groups of sequences
//////////////////////////////////////////////////////////////////////

MultiSequence *GlobalAlign::AlignGroups (int n,
					 SparseMatrix **posteriors, 
					 const MultiSequence &group1, 
					 const MultiSequence &group2){
  
  int groupLen1 = group1.GetLength();
  int groupLen2 = group2.GetLength();
  Matrix *mp = new Matrix (1, groupLen1+1, groupLen2+1);
  Matrix &m = *mp;
  m.Fill (0);

  for (int s = 0; s < group1.GetNumSequences(); s++){
    for (int t = 0; t < group2.GetNumSequences(); t++){
      int id1 = group1.GetSequence(s).GetID();
      int id2 = group2.GetSequence(t).GetID();
      const SparseMatrix &sm = *posteriors[id1*n+id2];

      int *mapping1 = group1.GetSequence(s).ComputeMapping();
      int *mapping2 = group2.GetSequence(t).ComputeMapping();
      
      for (int i = 0; i < sm.GetNumRows(); i++){
	const SparseMatrix::SparseMatrixEntry *p = sm.GetRowPtr (0, i);
	for (int j = 0; j < sm.GetRowSize(0,i); j++){
	  int gr = mapping1[i];
	  int gc = mapping2[p->column];
	  m(0,gr,gc) += p->value;
	  ++p;
	}
      }
      
      delete[] mapping1;
      delete[] mapping2;
    }
  }
  const char *path = ComputeMWTrace (m);
  delete mp;

  MultiSequence *res = BuildAlignment (group1, group2, path);
  delete[] (char *)path;
  

  return res;
}
				
//////////////////////////////////////////////////////////////////////
// Compute alignment score
//////////////////////////////////////////////////////////////////////

float GlobalAlign::ComputeAlignmentScore (const MultiSequence &seqs, 
					  int n, 
					  SparseMatrix **posteriors){

  int i;
  float score = 0;
  int length = seqs.GetSequence(0).GetLength()+1;
  float *scores = new float[length];
  for(i =0; i < length; i++)
	  scores[i] = 0;
  for (int s = 0; s < n-1; s++){
    for (int t = s+1; t < n; t++){
      int id1 = seqs.GetSequence(s).GetID();
      int id2 = seqs.GetSequence(t).GetID();
      const SparseMatrix &sm = *posteriors[id1*n+id2];
      int *mapping1 = seqs.GetSequence(s).ComputeMapping();
      int *mapping2 = seqs.GetSequence(t).ComputeMapping();
      int len1 = sm.GetNumRows()-1;
      int len2 = sm.GetNumCols()-1;
      
      int pos1 = 1;
      int pos2 = 1;
      while (pos1 <= len1 && pos2 <= len2){
	if (mapping1[pos1] < mapping2[pos2]) pos1++;
	else if (mapping1[pos1] > mapping2[pos2]) pos2++;
	else {
	  score += sm(0,pos1,pos2);
	  scores[mapping1[pos1]] += sm(0,pos1,pos2);
	  pos1++;
	  pos2++;
	}
    }
      
      delete [] mapping1;
      delete [] mapping2;      
    }
  }

  for(i = 1; i < length; i++){
	  fprintf(stderr, "%4.2f ", scores[i]); 
  }
  fprintf(stderr,"\n");
  delete scores;
  
  return score;
}