-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlignment.C
334 lines (278 loc) · 6.42 KB
/
Alignment.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
/***********************************************************************
Alignment.C
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***********************************************************************/
#include "Alignment.H"
#include <iostream>
using namespace BOOM;
int Alignment::MAX_WIDTH=60;
Alignment::Alignment(const Sequence &s1,const Sequence &s2,
const Alphabet &alpha,double score)
: s1(s1), s2(s2), alphabet(alpha), score(score)
{
}
const Sequence &Alignment::getFirstSequence() const
{
return s1;
}
const Sequence &Alignment::getSecondSequence() const
{
return s2;
}
int Alignment::getAlignmentLength() const
{
return matchData.size();
}
MatchType Alignment::operator[](int position) const
{
return matchData[position];
}
Alignment &Alignment::operator+=(MatchType m)
{
matchData.push_back(m);
return *this;
}
void Alignment::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;
int pos1=0, pos2=0;
while(begin<len) {
if(begin+subLen>len) subLen=len-begin;
const String topSubstr=topRow.substring(begin,subLen);
const String middleSubstr=middleRow.substring(begin,subLen);
const String bottomSubstr=bottomRow.substring(begin,subLen);
const int topResidues=topSubstr.length()-topSubstr.count('-');
const int bottomResidues=bottomSubstr.length()-bottomSubstr.count('-');
os<<"Query: "<<topSubstr<<" "<<pos1<<"-"<<pos1+topResidues-1<<endl;
os<<" "<<middleSubstr<<endl;
os<<"Sbjct: "<<bottomSubstr<<" "<<pos2<<"-"<<pos2+bottomResidues-1<<endl;
pos1+=topResidues;
pos2+=bottomResidues;
begin+=subLen;
if(begin<len) os << endl;
}
}
ostream &BOOM::operator<<(ostream &os,BOOM::Alignment const &alignment)
{
alignment.printOn(os);
return os;
}
double Alignment::getScore() const
{
return score;
}
int Alignment::countMatches() const
{
int matches=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) ++matches;
++index1;
++index2; }
break;
case FIRST_UNMATCHED: ++index1; break;
case SECOND_UNMATCHED: ++index2; break;
}
return matches;
}
void Alignment::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 Alignment::countNearMatches(SubstitutionMatrix<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 Alignment::countNearMatches(SubstitutionMatrix<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 Alignment::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 Alignment::render(String &str1,String &str2)
{
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];
str1+=alphabet.lookup(symbol1);
str2+=alphabet.lookup(symbol2);
++index1;
++index2;
}
break;
case FIRST_UNMATCHED:
{
Symbol symbol1=s1[index1];
str1+=alphabet.lookup(symbol1);
str2+='-';
++index1;
}
break;
case SECOND_UNMATCHED:
{
Symbol symbol2=s2[index2];
str1+='-';
str2+=alphabet.lookup(symbol2);
++index2;
}
break;
}
}
char matchTypeToCigarLetter(MatchType m)
{
switch(m)
{
case MATCH: return 'M';
case FIRST_UNMATCHED: return 'D';
case SECOND_UNMATCHED: return 'I';
default: INTERNAL_ERROR;
}
}
String Alignment::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 && i<L) { ++opLen; ++i; }
cigar+=String(opLen); cigar+=String(op);
if(i<L) {
matchType=matchData[i];
op=matchTypeToCigarLetter(matchType);
opLen=1;
++i; }
}
return cigar;
}