forked from grenaud/schmutzi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjointFreqDeaminatedDouble.cpp
287 lines (192 loc) · 6.91 KB
/
jointFreqDeaminatedDouble.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
#include <iostream>
#include <vector>
#include <set>
#include <ctype.h>
#include <stdlib.h>
#include "api/BamMultiReader.h"
#include "api/BamReader.h"
#include "api/BamWriter.h"
#include "api/BamAux.h"
#include "api/SamSequenceDictionary.h"
#include "utils.h"
#include "ReconsReferenceBAM.h"
using namespace std;
using namespace BamTools;
const int offset=33;
bool isTransition(char reference,char read){
if( (reference == 'A' && read == 'G') ||
(reference == 'G' && read == 'A') ||
(reference == 'C' && read == 'T') ||
(reference == 'T' && read == 'C') ){
return true;
}
return false;
}
bool isTransversions(char reference,char read){
if( (reference == 'A' && read == 'C') ||
(reference == 'A' && read == 'T') ||
(reference == 'G' && read == 'C') ||
(reference == 'G' && read == 'T') ||
(reference == 'C' && read == 'A') ||
(reference == 'C' && read == 'G') ||
(reference == 'T' && read == 'A') ||
(reference == 'T' && read == 'G') ){
return true;
}
return false;
}
int main (int argc, char *argv[]) {
int minBaseQuality = 0;
string usage=string(""+string(argv[0])+" [in BAM file]"+
"\nThis program computes the joint freq of deamination\n"+
// "\nreads and the puts the rest into another bam file.\n"+
// "\nTip: if you do not need one of them, use /dev/null as your output\n"+
"arguments:\n"+
"\t"+"--bq [base qual] : Minimum base quality to flag a deaminated site (Default: "+stringify(minBaseQuality)+")\n"+
"\n");
if(argc == 1 ||
argc < 2 ||
(argc == 2 && (string(argv[0]) == "-h" || string(argv[0]) == "--help") )
){
cerr << "Usage "<<usage<<endl;
return 1;
}
for(int i=1;i<(argc-2);i++){
if(string(argv[i]) == "--bq"){
minBaseQuality=destringify<int>(argv[i+1]);
i++;
continue;
}
}
string bamfiletopen = string( argv[ argc-1 ] );
BamReader reader;
if ( !reader.Open(bamfiletopen) ) {
cerr << "Could not open input BAM file"<< bamfiletopen << endl;
return 1;
}
vector<RefData> testRefData=reader.GetReferenceData();
const SamHeader header = reader.GetHeader();
const RefVector references = reader.GetReferenceData();
// BamWriter writerDeam;
// if ( !writerDeam.Open(deambam, header, references) ) {
// cerr << "Could not open output BAM file" << endl;
// return 1;
// }
// BamWriter writerNoDeam;
// if ( !writerNoDeam.Open(nondeambam, header, references) ) {
// cerr << "Could not open output BAM file" << endl;
// return 1;
// }
//iterating over the alignments for these regions
BamAlignment al;
int i;
unsigned int d5pgiven3p =0;
unsigned int nod5pgiven3p=0;
unsigned int d5not3p =0;
unsigned int nod5not3p =0;
unsigned int d5 =0;
unsigned int nod5 =0;
unsigned int d3pgiven5p =0;
unsigned int nod3pgiven5p=0;
unsigned int d3not5p =0;
unsigned int nod3not5p =0;
unsigned int d3 =0;
unsigned int nod3 =0;
while ( reader.GetNextAlignment(al) ) {
//skip unmapped
if(!al.IsMapped())
continue;
if(al.IsPaired() ){
continue;
return 1;
}
string reconstructedReference = reconstructRef(&al);
char refeBase;
char readBase;
bool isDeaminated5p=false;
bool isDeaminated3p=false;
bool count5p=false;
bool count3p=false;
if(al.Qualities.size() != reconstructedReference.size()){
cerr<<"Quality line is not the same size as the reconstructed reference"<<endl;
return 1;
}
if(al.IsReverseStrand()){
//continue;
//first base next to 3'
i = 0 ;
refeBase=toupper(reconstructedReference[i]);
readBase=toupper( al.QueryBases[i]);
if( readBase == 'T' && refeBase == 'C' && int(al.Qualities[i]-offset) >= minBaseQuality){ isDeaminated3p=true; count3p=true;}
if( readBase == 'C' && refeBase == 'M' && int(al.Qualities[i]-offset) >= minBaseQuality){ isDeaminated3p=false;count3p=true; }
//last base next to 5'
i = (al.QueryBases.length()-1) ;
refeBase=toupper(reconstructedReference[i]);
readBase=toupper( al.QueryBases[i]);
if( readBase == 'A' && refeBase == 'G' && int(al.Qualities[i]-offset) >= minBaseQuality){ isDeaminated5p=true; count5p=true; }
if( readBase == 'G' && refeBase == 'M' && int(al.Qualities[i]-offset) >= minBaseQuality){ isDeaminated5p=false; count5p=true; }
}else{
//first base next to 5'
i = 0;
refeBase=toupper(reconstructedReference[i]);
readBase=toupper( al.QueryBases[i]);
if( readBase == 'T' && refeBase == 'C' && int(al.Qualities[i]-offset) >= minBaseQuality){ isDeaminated5p=true; count5p=true; }
if( readBase == 'C' && refeBase == 'M' && int(al.Qualities[i]-offset) >= minBaseQuality){ isDeaminated5p=false; count5p=true; }
//last base next to 3'
i = (al.QueryBases.length()-1);
refeBase=toupper(reconstructedReference[i]);
readBase=toupper( al.QueryBases[i]);
if( readBase == 'A' && refeBase == 'G' && int(al.Qualities[i]-offset) >= minBaseQuality){ isDeaminated3p=true; count3p=true; }
if( readBase == 'G' && refeBase == 'M' && int(al.Qualities[i]-offset) >= minBaseQuality){ isDeaminated3p=false; count3p=true; }
}
// if(!(count5p && count3p))
// continue;
if( count5p ){
if( isDeaminated5p)
d5++;
if( !isDeaminated5p)
nod5++;
}
if( count3p ){
if( isDeaminated3p)
d3++;
if( !isDeaminated3p)
nod3++;
}
if( count3p && count5p ){
if( isDeaminated3p) {
if( isDeaminated5p)
d5pgiven3p++;
if( !isDeaminated5p)
nod5pgiven3p++;
}else{
if( isDeaminated5p)
d5not3p++;
if( !isDeaminated5p)
nod5not3p++;
}
// }
// if(count5p){
if( isDeaminated5p) {
if( isDeaminated3p)
d3pgiven5p++;
if( !isDeaminated3p)
nod3pgiven5p++;
}else{
if( isDeaminated3p)
d3not5p++;
if( !isDeaminated3p)
nod3not5p++;
}
}
// if( isDeaminated5p && isDeaminated3p ) b53d++;
// if( isDeaminated5p && isDeaminated3p ) o5d++;
// if( !isDeaminated5p && isDeaminated3p ) o3d++;
// if( !isDeaminated5p && !isDeaminated3p ) nod++;
}//end for each read
// cout<<b53d<<"\t"<<o5d<<"\t"<<o3d<<"\t"<<nod<<endl;
cout<<d5pgiven3p<<"\t"<<nod5pgiven3p<<"\t"<<d5not3p<<"\t"<<nod5not3p<<"\t"<<d5<<"\t"<<nod5<<"\t"<<double(d5pgiven3p)/double( d5pgiven3p+nod5pgiven3p)<<"\t"<<double(d5not3p)/double( d5not3p+nod5not3p)<<"\t"<<double(d5)/double( d5+nod5)<<endl;
cout<<d3pgiven5p<<"\t"<<nod3pgiven5p<<"\t"<<d3not5p<<"\t"<<nod3not5p<<"\t"<<d3<<"\t"<<nod3<<"\t"<<double(d3pgiven5p)/double( d3pgiven5p+nod3pgiven5p)<<"\t"<<double(d3not5p)/double( d3not5p+nod3not5p)<<"\t"<<double(d3)/double( d3+nod3)<<endl;
reader.Close();
return 0;
}