-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlignmentPath.C
338 lines (282 loc) · 6.48 KB
/
AlignmentPath.C
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
/****************************************************************
AlignmentPath.C
****************************************************************/
#include "AlignmentPath.H"
#include "BOOM/Exceptions.H"
#include <iostream>
using namespace std;
using namespace BOOM;
int AlignmentPath::MAX_WIDTH=60;
AlignmentPath::AlignmentPath(const Sequence &s1,const Sequence &s2,Alphabet &alpha,
double score)
: s1(s1), s2(s2), alphabet(alpha), score(score)
{
}
const Sequence &AlignmentPath::getFirstSequence() const
{
return s1;
}
const Sequence &AlignmentPath::getSecondSequence() const
{
return s2;
}
int AlignmentPath::getAlignmentLength() const
{
return matchData.size();
}
int AlignmentPath::getAlignedLength() const
{
// Find first match position
int i, L=matchData.size();
for(i=0 ; i<L ; ++i) if(matchData[i]==MATCH) break;
if(i==L) return 0; // no matches!
int firstMatch=i;
// Find last match position
for(i=L-1 ; i>0 ; --i) if(matchData[i]==MATCH) break;
int lastMatch=i;
// Compute length of aligned region between first and last match
return lastMatch-firstMatch+1;
}
MatchType AlignmentPath::operator[](int position) const
{
return matchData[position];
}
AlignmentPath &AlignmentPath::operator+=(MatchType m)
{
matchData.push_back(m);
return *this;
}
void AlignmentPath::printOn(ostream &os) const
{
String topRow, middleRow, bottomRow;
Vector<MatchType>::const_iterator cur=matchData.begin(),
end=matchData.end();
int index1=0, index2=0;
for(; cur!=end ; ++cur)
switch(*cur)
{
case MATCH:
{
Symbol symbol1=s1[index1], symbol2=s2[index2];
topRow+=alphabet.lookup(symbol1);
bottomRow+=alphabet.lookup(symbol2);
middleRow+=(symbol1==symbol2 ? '|' : '*');
++index1;
++index2;
}
break;
case FIRST_UNMATCHED:
topRow+=alphabet.lookup(s1[index1]);
bottomRow+='-';
middleRow+=' ';
++index1;
break;
case SECOND_UNMATCHED:
topRow+='-';
bottomRow+=alphabet.lookup(s2[index2]);
middleRow+=' ';
++index2;
break;
}
int len=getAlignmentLength(), begin=0, subLen=MAX_WIDTH;
while(begin<len)
{
if(begin+subLen>len) subLen=len-begin;
os << "Query: " << topRow.substring(begin,subLen) << endl;
os << " " << middleRow.substring(begin,subLen) << endl;
os << "Sbjct: " << bottomRow.substring(begin,subLen) << endl;
begin+=subLen;
if(begin<len) os << endl;
}
}
ostream &operator<<(ostream &os,const AlignmentPath &alignment)
{
alignment.printOn(os);
return os;
}
double AlignmentPath::getScore() const
{
return score;
}
void AlignmentPath::countNonNColumns(int &cols,int &matches) const {
cols=matches=0;
Symbol N=alphabet.lookup('N');
Vector<MatchType>::const_iterator cur=matchData.begin(),
end=matchData.end();
int index1=0, index2=0;
for(; cur!=end ; ++cur) {
switch(*cur)
{
case MATCH:
{
Symbol symbol1=s1[index1], symbol2=s2[index2];
if(!(symbol1==N) && !(symbol2==N)) {
++cols;
if(symbol1==symbol2) ++matches;
}
++index1;
++index2;
}
break;
case FIRST_UNMATCHED:
++index1;
break;
case SECOND_UNMATCHED:
++index2;
break;
}
}
}
void AlignmentPath::countMismatches(int &mismatches,int &insertions) const
{
mismatches=insertions=0;
Vector<MatchType>::const_iterator cur=matchData.begin(),
end=matchData.end();
int index1=0, index2=0;
for(; cur!=end ; ++cur)
switch(*cur)
{
case MATCH:
{
Symbol symbol1=s1[index1], symbol2=s2[index2];
if(!(symbol1==symbol2)) ++mismatches;
++index1;
++index2;
}
break;
case FIRST_UNMATCHED:
++insertions;
++index1;
break;
case SECOND_UNMATCHED:
++index2;
++insertions;
break;
}
}
int AlignmentPath::countNearMatches(AlignmentSubstMatrix<float> &M)
{
int count=0;
Vector<MatchType>::const_iterator cur=matchData.begin(),
end=matchData.end();
int index1=0, index2=0;
for(; cur!=end ; ++cur)
switch(*cur)
{
case MATCH:
{
Symbol symbol1=s1[index1], symbol2=s2[index2];
if(M(symbol1,symbol2)>0) ++count;
++index1;
++index2;
}
break;
case FIRST_UNMATCHED:
++index1;
break;
case SECOND_UNMATCHED:
++index2;
break;
}
return count;
}
int AlignmentPath::countNearMatches(AlignmentSubstMatrix<double> &M)
{
int count=0;
Vector<MatchType>::const_iterator cur=matchData.begin(),
end=matchData.end();
int index1=0, index2=0;
for(; cur!=end ; ++cur)
switch(*cur)
{
case MATCH:
{
Symbol symbol1=s1[index1], symbol2=s2[index2];
if(M(symbol1,symbol2)>0) ++count;
++index1;
++index2;
}
break;
case FIRST_UNMATCHED:
++index1;
break;
case SECOND_UNMATCHED:
++index2;
break;
}
return count;
}
void AlignmentPath::getResidualsOnRight(Sequence &seq1,Sequence &seq2)
{
seq1.clear();
seq2.clear();
int len=matchData.size();
int index1=s1.getLength()-1, index2=s2.getLength()-1;
for(int i=len-1 ; i>0 ; --i)
switch(matchData[i])
{
case MATCH:
return;
case FIRST_UNMATCHED:
seq1.prepend(s1[index1]);
--index1;
break;
case SECOND_UNMATCHED:
seq2.prepend(s2[index2]);
--index2;
}
}
void AlignmentPath::getMatchExtent(int &firstBegin,int &firstEnd,int &secondBegin,int &secondEnd) const
{
int L=matchData.size();
int firstPos=0, secondPos=0;
firstBegin=secondBegin=firstEnd=secondEnd=-1;
for(int i=0 ; i<L ; ++i) {
MatchType m=matchData[i];
switch(m) {
case MATCH:
if(firstBegin<0) firstBegin=firstPos;
if(secondBegin<0) secondBegin=secondPos;
firstEnd=firstPos;
secondEnd=secondPos;
++firstPos; ++secondPos;
break;
case FIRST_UNMATCHED:
++firstPos;
break;
case SECOND_UNMATCHED:
++secondPos;
break;
default: INTERNAL_ERROR;
}
}
}
char matchTypeToCigarLetter(MatchType m)
{
switch(m)
{
case MATCH: return 'M';
case FIRST_UNMATCHED: return 'D';
case SECOND_UNMATCHED: return 'I';
default: INTERNAL_ERROR;
}
}
String AlignmentPath::getCigarString() const
{
int L=matchData.size();
if(L==0) return "";
String cigar;
int opLen=1;
MatchType matchType=matchData[0];
char op=matchTypeToCigarLetter(matchType);
for(int i=1 ; i<L ; ) {
while(matchData[i]==matchType) { ++opLen; ++i; }
cigar+=opLen; cigar+=op;
if(i<L) {
matchType=matchData[i];
op=matchTypeToCigarLetter(matchType);
opLen=1; }
}
return cigar;
}