-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathfilter.cpp
280 lines (235 loc) · 7.93 KB
/
filter.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
#include "processor.h"
#include "peprocessor.h"
#include "seprocessor.h"
#include "overlapanalysis.h"
#include <cmath>
Filter::Filter(Options* opt){
mOptions = opt;
}
Filter::~Filter(){
}
int Filter::passFilter(Read* r) {
if(r == NULL || r->length()==0) {
return FAIL_LENGTH;
}
int rlen = r->length();
int lowQualNum = 0;
int nBaseNum = 0;
float totalQual = 0;
// need to recalculate lowQualNum and nBaseNum if the corresponding filters are enabled
if(mOptions->qualfilter.enabled || mOptions->lengthFilter.enabled) {
const char* seqstr = r->mSeq->c_str();
const char* qualstr = r->mQuality->c_str();
for(int i=0; i<rlen; i++) {
char base = seqstr[i];
char qual = qualstr[i];
totalQual += pow(10, ((qual - 33)/(-10.0)));
if(qual < mOptions->qualfilter.qualifiedQual)
lowQualNum ++;
if(base == 'N')
nBaseNum++;
}
}
if(mOptions->qualfilter.enabled) {
if(lowQualNum > (mOptions->qualfilter.unqualifiedPercentLimit * rlen / 100.0) )
return FAIL_QUALITY;
else if(mOptions->qualfilter.avgQualReq > 0 && ((-10)*(log10(totalQual / rlen)))<mOptions->qualfilter.avgQualReq)
return FAIL_QUALITY;
else if(nBaseNum > mOptions->qualfilter.nBaseLimit )
return FAIL_N_BASE;
}
if(mOptions->lengthFilter.enabled) {
if(rlen < mOptions->lengthFilter.requiredLength)
return FAIL_LENGTH;
if(mOptions->lengthFilter.maxLength > 0 && rlen > mOptions->lengthFilter.maxLength)
return FAIL_TOO_LONG;
}
if(mOptions->complexityFilter.enabled) {
if(!passLowComplexityFilter(r))
return FAIL_COMPLEXITY;
}
return PASS_FILTER;
}
bool Filter::passLowComplexityFilter(Read* r) {
int diff = 0;
int length = r->length();
if(length <= 1)
return false;
const char* data = r->mSeq->c_str();
for(int i=0; i<length-1; i++) {
if(data[i] != data[i+1])
diff++;
}
if( (double)diff/(double)(length-1) >= mOptions->complexityFilter.threshold )
return true;
else
return false;
}
Read* Filter::trimAndCut(Read* r, int front, int tail, int& frontTrimmed) {
frontTrimmed = 0;
// return the same read for speed if no change needed
if(front == 0 && tail == 0 && !mOptions->qualityCut.enabledFront && !mOptions->qualityCut.enabledTail && !mOptions->qualityCut.enabledRight)
return r;
int rlen = r->length() - front - tail ;
if (rlen < 0)
return NULL;
if(front == 0 && !mOptions->qualityCut.enabledFront && !mOptions->qualityCut.enabledTail && !mOptions->qualityCut.enabledRight){
r->resize(rlen);
return r;
} else if(!mOptions->qualityCut.enabledFront && !mOptions->qualityCut.enabledTail && !mOptions->qualityCut.enabledRight){
r->mSeq->erase(0,front);
r->mSeq->resize(rlen);
r->mQuality->erase(0,front);
r->mQuality->resize(rlen);
frontTrimmed = front;
return r;
}
// need quality cutting
int l = r->length();
const char* qualstr = r->mQuality->c_str();
const char* seq = r->mSeq->c_str();
// quality cutting forward
if(mOptions->qualityCut.enabledFront) {
int w = mOptions->qualityCut.windowSizeFront;
int s = front;
if(l - front - tail - w <= 0)
return NULL;
int totalQual = 0;
// preparing rolling
for(int i=0; i<w-1; i++)
totalQual += qualstr[s+i];
for(s=front; s+w<l-tail; s++) {
totalQual += qualstr[s+w-1];
// rolling
if(s > front) {
totalQual -= qualstr[s-1];
}
// add 33 for phred33 transforming
if((double)totalQual / (double)w >= 33 + mOptions->qualityCut.qualityFront)
break;
}
// the trimming in front is forwarded and rlen is recalculated
if(s >0 )
s = s+w-1;
while(s<l && seq[s] == 'N')
s++;
front = s;
rlen = l - front - tail;
}
// quality cutting in right mode
if(mOptions->qualityCut.enabledRight) {
int w = mOptions->qualityCut.windowSizeRight;
int s = front;
if(l - front - tail - w <= 0)
return NULL;
int totalQual = 0;
// preparing rolling
for(int i=0; i<w-1; i++)
totalQual += qualstr[s+i];
bool foundLowQualWindow = false;
for(s=front; s+w<l-tail; s++) {
totalQual += qualstr[s+w-1];
// rolling
if(s > front) {
totalQual -= qualstr[s-1];
}
// add 33 for phred33 transforming
if((double)totalQual / (double)w < 33 + mOptions->qualityCut.qualityRight) {
foundLowQualWindow = true;
break;
}
}
if(foundLowQualWindow ) {
// keep the good bases in the window
while(s<l-1 && qualstr[s]>=33 + mOptions->qualityCut.qualityRight)
s++;
rlen = s - front;
}
}
// quality cutting backward
if(!mOptions->qualityCut.enabledRight && mOptions->qualityCut.enabledTail) {
int w = mOptions->qualityCut.windowSizeTail;
if(l - front - tail - w <= 0)
return NULL;
int totalQual = 0;
int t = l - tail - 1;
// preparing rolling
for(int i=0; i<w-1; i++)
totalQual += qualstr[t-i];
for(t=l-tail-1; t-w>=front; t--) {
totalQual += qualstr[t-w+1];
// rolling
if(t < l-tail-1) {
totalQual -= qualstr[t+1];
}
// add 33 for phred33 transforming
if((double)totalQual / (double)w >= 33 + mOptions->qualityCut.qualityTail)
break;
}
if(t < l-1)
t = t-w+1;
while(t>=0 && seq[t] == 'N')
t--;
rlen = t - front + 1;
}
if(rlen <= 0 || front >= l-1)
return NULL;
r->mSeq->erase(0, front);
r->mSeq->resize(rlen);
r->mQuality->erase(0, front);
r->mQuality->resize(rlen);
frontTrimmed = front;
return r;
}
bool Filter::filterByIndex(Read* r) {
if(mOptions->indexFilter.enabled) {
if( match(mOptions->indexFilter.blacklist1, r->firstIndex(), mOptions->indexFilter.threshold) )
return true;
}
return false;
}
bool Filter::filterByIndex(Read* r1, Read* r2) {
if(mOptions->indexFilter.enabled) {
if( match(mOptions->indexFilter.blacklist1, r1->firstIndex(), mOptions->indexFilter.threshold) )
return true;
if( match(mOptions->indexFilter.blacklist2, r2->lastIndex(), mOptions->indexFilter.threshold) )
return true;
}
return false;
}
bool Filter::match(vector<string>& list, string target, int threshold) {
for(int i=0; i<list.size(); i++) {
int diff = 0;
int len1 = list[i].length();
int len2 = target.length();
for(int s=0; s<len1 && s<len2; s++) {
if(list[i][s] != target[s]) {
diff++;
if(diff>threshold)
break;
}
}
if(diff <= threshold)
return true;
}
return false;
}
bool Filter::test() {
Read r("@name",
"TTTTAACCCCCCCCCCCCCCCCCCCCCCCCCCCCAATTTT",
"+",
"/////CCCCCCCCCCCC////CCCCCCCCCCCCCC////E");
Options opt;
opt.qualityCut.enabledFront = true;
opt.qualityCut.enabledTail = true;
opt.qualityCut.windowSizeFront = 4;
opt.qualityCut.qualityFront = 20;
opt.qualityCut.windowSizeTail = 4;
opt.qualityCut.qualityTail = 20;
Filter filter(&opt);
int frontTrimmed = 0;
Read* ret = filter.trimAndCut(&r, 0, 1, frontTrimmed);
ret->print();
return *ret->mSeq == "CCCCCCCCCCCCCCCCCCCCCCCCCCCC"
&& *ret->mQuality == "CCCCCCCCCCC////CCCCCCCCCCCCC";
}