forked from mothur/mothur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chimerarealigner.cpp
348 lines (285 loc) · 9.91 KB
/
chimerarealigner.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
/*
* chimerarealigner.cpp
* Mothur
*
* Created by westcott on 2/12/10.
* Copyright 2010 Schloss Lab. All rights reserved.
*
*/
#include "chimerarealigner.h"
#include "needlemanoverlap.hpp"
#include "nast.hpp"
//***************************************************************************************************************
ChimeraReAligner::ChimeraReAligner() { m = MothurOut::getInstance(); }
//***************************************************************************************************************
ChimeraReAligner::~ChimeraReAligner() {}
//***************************************************************************************************************
void ChimeraReAligner::reAlign(Sequence* query, vector<string> parents) {
try {
// if (parents.size() != 0) {
// vector<Sequence*> queryParts;
// vector<Sequence*> parentParts; //queryParts[0] relates to parentParts[0]
//
// string qAligned = query->getAligned();
// string newQuery = "";
//
// //sort parents by region start
// sort(parents.begin(), parents.end(), compareRegionStart);
//
// //make sure you don't cutoff beginning of query
// if (parents[0].nastRegionStart > 0) { newQuery += qAligned.substr(0, parents[0].nastRegionStart); }
// int longest = 0;
//
// //take query and break apart into pieces using breakpoints given by results of parents
// for (int i = 0; i < parents.size(); i++) {
// int length = parents[i].nastRegionEnd - parents[i].nastRegionStart+1;
// string q = qAligned.substr(parents[i].nastRegionStart, length);
//
// Sequence* queryFrag = new Sequence(query->getName(), q);
// queryParts.push_back(queryFrag);
//
// string p = parents[i].parentAligned;
// p = p.substr(parents[i].nastRegionStart, length);
// Sequence* parent = new Sequence(parents[i].parent, p);
// parentParts.push_back(parent);
//
// if (queryFrag->getUnaligned().length() > longest) { longest = queryFrag->getUnaligned().length(); }
// if (parent->getUnaligned().length() > longest) { longest = parent->getUnaligned().length(); }
// }
//
// //align each peice to correct parent from results
// for (int i = 0; i < queryParts.size(); i++) {
// if ((queryParts[i]->getUnaligned() == "") || (parentParts[i]->getUnaligned() == "")) {;}
// else {
// Alignment* alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, longest+1); //default gapopen, match, mismatch, longestbase
//
// Nast nast(alignment, queryParts[i], parentParts[i]);
// delete alignment;
// }
// }
//
// //recombine pieces to form new query sequence
// for (int i = 0; i < queryParts.size(); i++) {
// //sometimes the parent regions do not meet, for example region 1 may end at 1000 and region 2 starts at 1100.
// //we don't want to loose length so in this case we will leave query alone
// if (i != 0) {
// int space = parents[i].nastRegionStart - parents[i-1].nastRegionEnd - 1;
// if (space > 0) { //they don't meet and we need to add query piece
// string q = qAligned.substr(parents[i-1].nastRegionEnd+1, space);
// newQuery += q;
// }
// }
//
// newQuery += queryParts[i]->getAligned();
// }
//
// //make sure you don't cutoff end of query
// if (parents[parents.size()-1].nastRegionEnd < (qAligned.length()-1)) { newQuery += qAligned.substr(parents[parents.size()-1].nastRegionEnd+1); }
//
// query->setAligned(newQuery);
//
// //free memory
// for (int i = 0; i < queryParts.size(); i++) { delete queryParts[i]; }
// for (int i = 0; i < parentParts.size(); i++) { delete parentParts[i]; }
//
// } //else leave query alone, you have bigger problems...
if(parents.size() != 0){
alignmentLength = query->getAlignLength(); //x
int queryUnalignedLength = query->getNumBases(); //y
buildTemplateProfile(parents);
createAlignMatrix(queryUnalignedLength, alignmentLength);
fillAlignMatrix(query->getUnaligned());
query->setAligned(getNewAlignment(query->getUnaligned()));
}
}
catch(exception& e) {
m->errorOut(e, "ChimeraReAligner", "reAlign");
exit(1);
}
}
/***************************************************************************************************************/
void ChimeraReAligner::buildTemplateProfile(vector<string> parents) {
try{
int numParents = parents.size();
profile.resize(alignmentLength);
for(int i=0;i<numParents;i++){
string seq = parents[i];
for(int j=0;j<alignmentLength;j++){
if(seq[j] == 'A') { profile[j].A++; }
else if(seq[j] == 'T') { profile[j].T++; }
else if(seq[j] == 'G') { profile[j].G++; }
else if(seq[j] == 'C') { profile[j].C++; }
else if(seq[j] == '-') { profile[j].Gap++; }
else if(seq[j] == '.') { profile[j].Gap++; }
// else { profile[j].A++; }
}
}
for(int i=0;i<alignmentLength;i++){
profile[i].Chars = profile[i].A + profile[i].T + profile[i].G + profile[i].C;
}
}
catch(exception& e) {
m->errorOut(e, "ChimeraReAligner", "buildTemplateProfile");
exit(1);
}
}
/***************************************************************************************************************/
void ChimeraReAligner::createAlignMatrix(int queryUnalignedLength, int alignmentLength){
try{
alignMatrix.resize(alignmentLength+1);
for(int i=0;i<=alignmentLength;i++){
alignMatrix[i].resize(queryUnalignedLength+1);
}
for(int i=1;i<=alignmentLength;i++) { alignMatrix[i][0].direction = 'l'; }
for(int j=1;j<=queryUnalignedLength;j++){ alignMatrix[0][j].direction = 'u'; }
}
catch(exception& e) {
m->errorOut(e, "ChimeraReAligner", "createAlignMatrix");
exit(1);
}
}
/***************************************************************************************************************/
void ChimeraReAligner::fillAlignMatrix(string query){
try{
int GAP = -4;
int nrows = alignMatrix.size()-1;
int ncols = alignMatrix[0].size()-1;
for(int i=1;i<=nrows;i++){
bases p = profile[i-1];
int numChars = p.Chars;
for(int j=1;j<=ncols;j++){
char q = query[j-1];
// score it for if there was a match
int maxScore = calcMatchScore(p, q) + alignMatrix[i-1][j-1].score;
int maxDirection = 'd';
// score it for if there was a gap in the query
int score = alignMatrix[i-1][j].score + (numChars * GAP);
if (score > maxScore) {
maxScore = score;
maxDirection = 'l';
}
alignMatrix[i][j].score = maxScore;
alignMatrix[i][j].direction = maxDirection;
}
}
}
catch(exception& e) {
m->errorOut(e, "ChimeraReAligner", "fillAlignMatrix");
exit(1);
}
}
/***************************************************************************************************************/
int ChimeraReAligner::calcMatchScore(bases p, char q){
try{
int MATCH = 5;
int MISMATCH = -4;
int score = 0;
if(q == 'G') { score = (MATCH * p.G + MISMATCH * (p.A + p.T + p.C + p.Gap)); }
else if(q == 'A') { score = (MATCH * p.A + MISMATCH * (p.G + p.T + p.C + p.Gap)); }
else if(q == 'T') { score = (MATCH * p.T + MISMATCH * (p.G + p.A + p.C + p.Gap)); }
else if(q == 'C') { score = (MATCH * p.C + MISMATCH * (p.G + p.A + p.T + p.Gap)); }
else { score = (MATCH * p.A + MISMATCH * (p.G + p.T + p.C + p.Gap)); }
return score;
}
catch(exception& e) {
m->errorOut(e, "ChimeraReAligner", "calcMatchScore");
exit(1);
}
}
/***************************************************************************************************************/
string ChimeraReAligner::getNewAlignment(string query){
try{
string queryAlignment(alignmentLength, '.');
string referenceAlignment(alignmentLength, '.');
int maxScore = -99999999;
int nrows = alignMatrix.size()-1;
int ncols = alignMatrix[0].size()-1;
int bestCol = -1;
int bestRow = -1;
for(int i=1;i<=nrows;i++){
int score = alignMatrix[i][ncols].score;
if (score > maxScore) {
maxScore = score;
bestRow = i;
bestCol = ncols;
}
}
for(int j=1;j<=ncols;j++){
int score = alignMatrix[nrows][j].score;
if (score > maxScore) {
maxScore = score;
bestRow = nrows;
bestCol = j;
}
}
int currentRow = bestRow;
int currentCol = bestCol;
int alignmentPosition = 0;
if(currentRow < alignmentLength){
for(int i=alignmentLength;i>currentRow;i--){
alignmentPosition++;
}
}
AlignCell c = alignMatrix[currentRow][currentCol];
while(c.direction != 'x'){
char q;
if(c.direction == 'd'){
q = query[currentCol-1];
currentCol--;
currentRow--;
}
else if (c.direction == 'u') {
break;
}
else if(c.direction == 'l'){
char gapChar;
if(currentCol == 0) { gapChar = '.'; }
else { gapChar = '-'; }
q = gapChar;
currentRow--;
}
else{
cout << "you shouldn't be here..." << endl;
}
queryAlignment[alignmentPosition] = q;
alignmentPosition++;
c = alignMatrix[currentRow][currentCol];
}
// need to reverse the string
string flipSeq = "";
for(int i=alignmentLength-1;i>=0;i--){
flipSeq += queryAlignment[i];
}
return flipSeq;
}
catch(exception& e) {
m->errorOut(e, "ChimeraReAligner", "getNewAlignment");
exit(1);
}
}
/***************************************************************************************************************/
// Sequence* ChimeraReAligner::getSequence(string name) {
// try{
// Sequence* temp;
//
// //look through templateSeqs til you find it
// int spot = -1;
// for (int i = 0; i < templateSeqs.size(); i++) {
// if (name == templateSeqs[i]->getName()) {
// spot = i;
// break;
// }
// }
//
// if(spot == -1) { m->mothurOut("Error: Could not find sequence."); m->mothurOutEndLine(); return NULL; }
//
// temp = new Sequence(templateSeqs[spot]->getName(), templateSeqs[spot]->getAligned());
//
// return temp;
// }
// catch(exception& e) {
// m->errorOut(e, "ChimeraReAligner", "getSequence");
// exit(1);
// }
//}
//***************************************************************************************************************/