-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvert_bam.cpp
executable file
·283 lines (274 loc) · 9.89 KB
/
convert_bam.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
/* this function select info from a bam file, which is piped from samtools
new on 2018-06-07:
1. remove discortly aligned read pairs (inter-chromosome ones).
*/
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
#include "./gzlib/gzstream.h"
#include "split_string.h"
bool convertBam(int argc, char* argv[])
{
if(argc < 4)
{
cout << "\n DrLink preprocess\t"
<< "Usage: samtools view longranger_wgs.bam | DrLink preprocess - out_prefix_str " << endl;
return false;
}
clock_t tbeg;
tbeg = clock();
bool verbose = true; // to add future option
//
cout << " Info: converting streaming bam info started..." << endl;
// prepare output gzipped file
string ofilename = (string)argv[3] + "_bamInfo_trashme.gz";
ogzstream ofp;
ofp.open(ofilename.c_str());
if ( ! ofp.good()) {
std::cerr << "ERROR: Opening file `" << ofilename << "' failed.\n";
return 1;
}
ofp << "#chr\tpos\tbamCIGAR\tbarcode\tlrMI\tread-order:bamFLAG\treversed-align\tread-id\tpnext" << endl;
// pnext: where its pair is aligned: must be the same-chr-position - only concordant alignments kept.
//
unsigned long numraw = 0;
unsigned long numcov = 0;
unsigned long numNoBX = 0;
unsigned long numNoCG = 0; // no cigar string
unsigned long numNoMI = 0;
unsigned long numNoMA = 0; // number of reads showing multiple alignment: only one kept.
unsigned long numDiscordant = 0; // read pairs aligned not in the same chr-region.
string exampleNoBX("");
string exampleNoCG("");
string exampleNoMI("");
string exampleNoMA("");
string exampleDiscordant("");
bool out = false;
std::string line;
while (std::getline(std::cin, line))
{
if(line.compare("quit")==0 || line.compare("q")==0 || line.compare("exit")==0) break;
if(line.size()==0 || line[0]=='#') continue;
numraw ++;
if(numraw%10000000 == 0)
{
cout << " info: " << numraw << "th aligm..." << endl;
}
//
vector<string> lineinfo = split_string(line, '\t');
// cigar string
if(lineinfo[5].compare("*") == 0)
{
if(exampleNoCG.size()==0)
{
exampleNoCG = line;
cout << " Warning: there are alignments without explicit CIGAR string, skipped., e.g.: "
<< line
<< endl;
cout << " Info: you will get a total number of such alignments in the end of program. "
<< endl;
}
numNoCG ++;
continue;
}
// discordant read pairs
if(lineinfo[6].compare("=") != 0)
{
// case 1: not on the same chr
if(exampleDiscordant.size()==0)
{
exampleDiscordant = line;
cout << " Warning: there are discordant alignments, skipped., e.g.: "
<< line
<< endl;
cout << " Info: you will get a total number of such alignments in the end of program. "
<< endl;
}
numDiscordant ++;
continue;
}
else
{
// case 2: on the same read, but unexpected template length
string tmplen;
if(lineinfo[8].find("-") != std::string::npos)
{
tmplen = lineinfo[8].substr(1);
}
else
{
tmplen = lineinfo[8].substr(0);
}
unsigned long templateLen = strtoul(tmplen.c_str(), NULL, 0);
if(templateLen==0 || templateLen>2000) // caution: fragment size
{
if(exampleDiscordant.size()==0)
{
exampleDiscordant = line;
cout << " Warning: there are discordant alignments, skipped., e.g.: "
<< line
<< endl;
cout << " Info: you will get a total number of such alignments in the end of program. "
<< endl;
}
numDiscordant ++;
continue;
}
}
//
/* BX: Chromium barcode sequence that is error-corrected and confirmed against
a list of known-good barcode sequences. Use this for analysis, e.g., '\tBX:Z:TTTGTGTGTATGACTC-1' */
size_t pos1 = line.find("\tBX:");
if(pos1 == std::string::npos)
{
if(exampleNoBX.size()==0)
{
exampleNoBX = line;
cout << " Warning: there are alignments without BX info, skipped., e.g.: "
<< line
<< endl;
cout << " Info: you will get a total number of such alignments in the end of program. "
<< endl;
}
numNoBX ++;
continue;
}
pos1 += 1; // from 'BX:Z:...'
size_t pos2 = line.find("\t", pos1);
if(pos2 == std::string::npos)
{
pos2 = line.size();
}
string rawbx = line.substr(pos1, pos2-pos1);
//
/* MI: Global molecule identifier for molecule that generated this read,
e.g., '\tMI:i:29' */
size_t pos3 = line.find("\tMI:");
bool foundmi = true;
if(pos3 == std::string::npos)
{
if(exampleNoMI.size()==0)
{
exampleNoMI = line;
cout << " Warning: there are alignments without MI info, but kept, e.g.: "
<< line
<< endl;
cout << " Info: you will get a total number of such alignments in the end of program. "
<< endl;
}
numNoMI ++;
foundmi = false;
}
pos3 += 1; // from 'MI:i:...'
size_t pos4;
string rawmi("-1");
if(foundmi)
{
pos4 = line.find("\t", pos3);
if(pos4 == std::string::npos)
{
pos4 = line.size();
}
rawmi = line.substr(pos3+5, pos4-pos3-5); // without "MI:i:"
}
//
// check if it is read 1 or read 2
int hexflag = strtol(lineinfo[1].c_str(), NULL, 0);
if(hexflag > 255)
{
numNoMA ++;
if(exampleNoMA.size()==0)
{
exampleNoMA = line;
cout << " Warning: there are alignments being secondary/supplementary or not passing filters, skipped, e.g.: "
<< line
<< endl;
cout << " Info: you will get a total number of such alignments in the end of program. "
<< endl;
}
continue;
}
string readflag = "U";
if((hexflag & 0x40) == 0x40 && (hexflag & 0x80) == 0)
{
readflag = "R1";
}
else
if((hexflag & 0x40) == 0 && (hexflag & 0x80) == 0x80)
{
readflag = "R2";
}
else
{
readflag = "U0";
cout << " hexflag=" << hexflag << endl;
cout << " hexflag & 0x40 == 0x40= " << (hexflag & 0x40) << endl;
cout << " hexflag & 0x80 == 0 = " << (hexflag & 0x80) << endl;
}
string reversed = "nrc";
if((hexflag & 0x10) == 0x10) reversed = "rc";
// chr pos CIGAR barcode MI read-ordering:R1/R2 reversed read-id pnext
// caution!!! code below also works for reversed-cases of alignment!
// in bam/sam, it is always pos + CIGAR"M" ==> spanning of a read!!!
ofp << lineinfo[2] << "\t"
<< lineinfo[3] << "\t"
<< lineinfo[5] << "\t"
<< rawbx.substr(5, rawbx.size()-7) << "\t"
<< rawmi << "\t"
<< readflag << ":" << lineinfo[1] << "\t"
<< reversed << "\t"
<< lineinfo[0] << "\t"
<< lineinfo[7] << endl; // with read id and pnext
//
numcov ++;
if(!out) out = true;
line.clear();
}
//
ofp.close();
if(numNoBX > 0)
{
cout << " Warning: there are "<< numNoBX
<< " alignments without BX info, skipped. "
<< endl;
}
if(numNoCG > 0)
{
cout << " Warning: there are "<< numNoCG
<< " alignments without explicit CIAGR info, skipped."
<< endl;
}
if(numNoMI > 0)
{
cout << " Warning: there are "<< numNoMI
<< " alignments without MI info, but kept."
<< endl;
}
if(numNoMA > 0)
{
cout << " Warning: there are "<< numNoMA
<< " alignments being secondary/supplementary alignment"
<< " or not passing filters, skipped."
<< endl;
}
if(numDiscordant > 0)
{
cout << " Warning: there are "<< numDiscordant
<< " alignments being discordant (pairs not on same chr or distance>2kb), skipped."
<< endl;
}
cout << " Info: in total "
<< numraw
<< " alignment lines, of which "
<< numcov
<< " with barcode info converted."
<< endl;
cout << " Info: time on converting streaming bam info: "
<< (float)(clock()-tbeg)/CLOCKS_PER_SEC
<< " seconds.\n"
<< endl;
if(!out) return false;
else return true;
}