/
median_filter.cpp
76 lines (65 loc) · 2.14 KB
/
median_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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017, Mayo Foundation for Medical Education and Research.
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
const int QUAL_THRESHOLD= 30;
double median(vector<int>& qual_vector){
int size= qual_vector.size();
sort(qual_vector.begin(), qual_vector.end());
return size % 2 ? qual_vector[size/2] : 0.5*(qual_vector[size/2-1] + qual_vector[size/2]);
}
int to_ascii_33(char c){
return int(c) - 33;
}
void ascii_to_int(vector<int>& qual_vector, const string& quals){
qual_vector.clear();
qual_vector.resize(quals.length(),0);
transform(quals.begin(),quals.end(),qual_vector.begin(), to_ascii_33);
assert(qual_vector.size() == quals.length());
}
int main(int argc,char **argv)
{
string name, seq, name2, qual;
int good_reads= 0;
int bad_reads= 0;
ifstream in2(argv[1], ios::in);
vector<int> qual_vector;
while(!in2.eof())
{
if(!getline(in2,name,'\n')) break;
if(!getline(in2,seq,'\n')) break;
if(!getline(in2,name2,'\n')) break;
if(!getline(in2,qual,'\n')) break;
ascii_to_int(qual_vector, qual);
double qual_median= median(qual_vector);
if(qual_median >= QUAL_THRESHOLD){
cout << name << "\n" << seq << "\n" << name2 << "\n" << qual << '\n';
good_reads += 1;
}
else {
bad_reads+= 1;
}
}
in2.close();
//Summary
cerr << "Number of good reads: " << good_reads << endl;
cerr << "Number of bad reads: " << bad_reads << endl;
return 0;
}