forked from grenaud/libgab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReconsReferenceBAM.cpp
340 lines (268 loc) · 10.3 KB
/
ReconsReferenceBAM.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
/*
* ReconsReferenceBAM
* Date: Oct-03-2012
* Author : Gabriel Renaud gabriel.reno [at sign here ] gmail.com
*
*/
#include "ReconsReferenceBAM.h"
int numberOfDeletions(const BamAlignment * al){
int toReturn=0;
vector<CigarOp> cigarData=al->CigarData;
for(unsigned int i=0;i<cigarData.size();i++){
if(cigarData[i].Type == 'D')
toReturn+=cigarData[i].Length;
//reconstructedTemp+=string(cigarData[i].Length,cigarData[i].Type);
}
return toReturn;
}
//! This function returns a string representation of the reference
/*!
*
* This function parses the MD field and CIGAR and reconstructs the reference
* useful for detecting mutations
*
\return A string representation of the reference
*/
string reconstructRef(const BamAlignment * al){
pair< string, vector<int> > toReturn = reconstructRefWithPos(al);
return toReturn.first;
// //initialize
// // int editDist=-1;
// string mdFieldString="";
// string reconstructed="";
// string reconstructedTemp="";
// //skip unmapped
// if(!al->IsMapped()){
// cerr<<"The function reconstructRef() cannot be called for unmapped reads"<<endl;
// exit(1);
// }
// //get relevant data
// // if(!al->GetTag("NM",editDist)){
// // cerr<<"Cannot get NM tag from "<<al->Name<<endl;
// // exit(1);
// // }
// if(!al->GetTag("MD",mdFieldString)){
// cerr<<"ReconsReferenceBAM: Cannot get MD tag from "<<al->Name<<endl;
// exit(1);
// }
// vector<CigarOp> cigarData=al->CigarData;
// for(unsigned int i=0;i<cigarData.size();i++){
// reconstructedTemp+=string(cigarData[i].Length,cigarData[i].Type);
// }
// //get a vector representation of the MD field
// vector<mdField> parsedMD=mdString2Vector(mdFieldString);
// vector<int> positionsOnControl;
// int initialPositionControl=al->Position;
// //combine the CIGAR and MD into one single string
// int mdVectorIndex=0;
// for(unsigned int i=0;i<reconstructedTemp.size();i++){
// if(reconstructedTemp[i] == 'M' ){ //only look at matches and indels
// if(mdVectorIndex<int(parsedMD.size())){ //still have mismatches
// if(parsedMD[mdVectorIndex].offset == 0){ //we have reached a mismatch
// if(parsedMD[mdVectorIndex].bp == DUMMYCHAR){ //no char to add, need to backtrack on the CIGAR
// i--;
// }else{
// reconstructed+=parsedMD[mdVectorIndex].bp;
// positionsOnControl.push_back(initialPositionControl++);
// }
// mdVectorIndex++;
// }else{ //wait until we reach a mismatch
// reconstructed+=reconstructedTemp[i];
// parsedMD[mdVectorIndex].offset--;
// positionsOnControl.push_back(initialPositionControl++);
// }
// //skipping all the positions with deletions on the read
// //if(mdVectorIndex<int(parsedMD.size())){ //still have mismatches
// while( (mdVectorIndex<int(parsedMD.size())) &&
// parsedMD[mdVectorIndex].bp == '^'){
// initialPositionControl+=parsedMD[mdVectorIndex].offset;
// mdVectorIndex++;
// }
// //}
// }else{
// reconstructed+=reconstructedTemp[i];
// positionsOnControl.push_back(initialPositionControl++);
// }
// }else{
// if(reconstructedTemp[i] == 'S' || reconstructedTemp[i] == 'I'){ //soft clipped bases and indels
// reconstructed+=reconstructedTemp[i];
// positionsOnControl.push_back(initialPositionControl);
// }
// }
// }
// if(reconstructed.size() != al->QueryBases.size()){
// cerr << "Could not recreate the sequence for read "<<al->Name << endl;
// exit(1);
// }
// if(positionsOnControl.size() != reconstructed.size()){
// cerr << "Could not determine the positions for the read "<<al->Name << endl;
// exit(1);
// }
// return reconstructed;
}
//! This function returns a pair with the string representation of the reference and the vector of the positions on the ref
/*!
*
* This function parses the MD field and CIGAR and reconstructs the reference
* useful for detecting mutations
*
\return A pair with the string representation of the reference and the vector of the positions on the ref
*/
pair< string, vector<int> > reconstructRefWithPos(const BamAlignment * al){
//initialize
// int editDist=-1;
string mdFieldString="";
string reconstructed="";
string reconstructedTemp="";
//skip unmapped
if(!al->IsMapped()){
cerr<<"The function reconstructRef() cannot be called for unmapped reads"<<endl;
exit(1);
}
//get relevant data
// if(!al->GetTag("NM",editDist)){
// cerr<<"Cannot get NM tag from "<<al->Name<<endl;
// exit(1);
// }
if(!al->GetTag("MD",mdFieldString)){
cerr<<"ReconsReferenceBAM: Cannot get MD tag from "<<al->Name<<endl;
exit(1);
}
vector<CigarOp> cigarData=al->CigarData;
for(unsigned int i=0;i<cigarData.size();i++){
reconstructedTemp+=string(cigarData[i].Length,cigarData[i].Type);
}
//get a vector representation of the MD field
vector<mdField> parsedMD=mdString2Vector(mdFieldString);
vector<int> positionsOnControl;
int initialPositionControl=al->Position;
//combine the CIGAR and MD into one single string
int mdVectorIndex=0;
for(unsigned int i=0;i<reconstructedTemp.size();i++){
if(reconstructedTemp[i] == 'M' ){ //only look at matches and indels
if(mdVectorIndex<int(parsedMD.size())){ //still have mismatches
if(parsedMD[mdVectorIndex].offset == 0){ //we have reached a mismatch
if(parsedMD[mdVectorIndex].bp == DUMMYCHAR){ //no char to add, need to backtrack on the CIGAR
i--;
}else{
reconstructed+=parsedMD[mdVectorIndex].bp;
positionsOnControl.push_back(initialPositionControl++);
}
mdVectorIndex++;
}else{ //wait until we reach a mismatch
reconstructed+=reconstructedTemp[i];
parsedMD[mdVectorIndex].offset--;
positionsOnControl.push_back(initialPositionControl++);
}
//skipping all the positions with deletions on the read
//if(mdVectorIndex<int(parsedMD.size())){ //still have mismatches
while( (mdVectorIndex<int(parsedMD.size())) &&
(parsedMD[mdVectorIndex].bp == '^' ) ){
initialPositionControl+=parsedMD[mdVectorIndex].offset;
mdVectorIndex++;
}
}else{
reconstructed+=reconstructedTemp[i];
positionsOnControl.push_back(initialPositionControl++);
}
}else{
if(reconstructedTemp[i] == 'S' || reconstructedTemp[i] == 'I'){ //soft clipped bases and indels
reconstructed+=reconstructedTemp[i];
positionsOnControl.push_back(initialPositionControl);
}
}
}
if(reconstructed.size() != al->QueryBases.size()){
cerr << "Could not recreate the sequence for read "<<al->Name << endl;
exit(1);
}
if(positionsOnControl.size() != reconstructed.size()){
cerr << "Could not determine the positions for the read "<<al->Name << endl;
exit(1);
}
return pair< string, vector<int> >(reconstructed,positionsOnControl);
}
//! This function returns a pair with the string representation of the reference and the vector of the positions on the ref
/*!
*
* This function parses the MD field and CIGAR and reconstructs the reference
* useful for detecting mutations
*
\return A pair with the string representation of the reference and the vector of the positions on the ref
*/
pair< string, vector<int> > reconstructRefWithPosOnRead(const BamAlignment * al){
//initialize
// int editDist=-1;
string mdFieldString="";
string reconstructed="";
string reconstructedTemp="";
//skip unmapped
if(!al->IsMapped()){
cerr<<"The function reconstructRef() cannot be called for unmapped reads"<<endl;
exit(1);
}
//get relevant data
// if(!al->GetTag("NM",editDist)){
// cerr<<"Cannot get NM tag from "<<al->Name<<endl;
// exit(1);
// }
if(!al->GetTag("MD",mdFieldString)){
cerr<<"ReconsReferenceBAM: Cannot get MD tag from "<<al->Name<<endl;
exit(1);
}
vector<CigarOp> cigarData=al->CigarData;
for(unsigned int i=0;i<cigarData.size();i++){
reconstructedTemp+=string(cigarData[i].Length,cigarData[i].Type);
}
//get a vector representation of the MD field
vector<mdField> parsedMD=mdString2Vector(mdFieldString);
vector<int> positionsOnControl;
int initialPositionControl=al->Position;
//combine the CIGAR and MD into one single string
int mdVectorIndex=0;
for(unsigned int i=0;i<reconstructedTemp.size();i++){
if(reconstructedTemp[i] == 'M' ){ //only look at matches and indels
if(mdVectorIndex<int(parsedMD.size())){ //still have mismatches
if(parsedMD[mdVectorIndex].offset == 0){ //we have reached a mismatch
if(parsedMD[mdVectorIndex].bp == DUMMYCHAR){ //no char to add, need to backtrack on the CIGAR
i--;
}else{
reconstructed+=parsedMD[mdVectorIndex].bp;
positionsOnControl.push_back(initialPositionControl++);
}
mdVectorIndex++;
}else{ //wait until we reach a mismatch
reconstructed+=reconstructedTemp[i];
parsedMD[mdVectorIndex].offset--;
positionsOnControl.push_back(initialPositionControl++);
}
//skipping all the positions with deletions on the read
//if(mdVectorIndex<int(parsedMD.size())){ //still have mismatches
while( (mdVectorIndex<int(parsedMD.size())) &&
(parsedMD[mdVectorIndex].bp == '^' ) ){
initialPositionControl+=parsedMD[mdVectorIndex].offset;
for(int d=0;d<parsedMD[mdVectorIndex].offset;d++)
reconstructed+="D";
mdVectorIndex++;
}
}else{
reconstructed+=reconstructedTemp[i];
positionsOnControl.push_back(initialPositionControl++);
}
}else{
if(reconstructedTemp[i] == 'S' || reconstructedTemp[i] == 'I'){ //soft clipped bases and indels
reconstructed+=reconstructedTemp[i];
positionsOnControl.push_back(initialPositionControl);
}
}
}
// if(reconstructed.size() != al->QueryBases.size()){
// cerr << "Could not recreate the sequence for read "<<al->Name << endl;
// exit(1);
// }
// if(positionsOnControl.size() != reconstructed.size()){
// cerr << "Could not determine the positions for the read "<<al->Name << endl;
// exit(1);
// }
return pair< string, vector<int> >(reconstructed,positionsOnControl);
}