-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup_sequences_using_sequence.cpp
142 lines (106 loc) · 3.63 KB
/
cleanup_sequences_using_sequence.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
// (C) 2016 Marcelo Soares Souza <[email protected]>
// This program is licensed under a LGPLv3 License.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iterator>
#include <tr1/unordered_set>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <magic.h>
#include "fast-cpp-csv-parser/csv.h"
#include "cleanup_sequences_using_sequence.h"
// const int THRESHOLD = 131072;
const int THRESHOLD = 16777216;
int main(int argc,char **argv) {
if (argc < 3)
{
cout << "Usage: " << argv[0] << " [CSV FILE] [FASTQ/GZ]" << "\n";
return EXIT_FAILURE;
}
int count_clean = 0, count_filter = 0;
string sequence = "";
string csv_file = argv[1];
string fastq_file = argv[2];
string out_clean_file = fastq_file + ".cleaned.result";
string out_filter_file = fastq_file + ".filtered.result";
FastQ f;
unordered_set<string> to_remove = loadCSV(csv_file);
struct magic_set *magic = magic_open(MAGIC_MIME|MAGIC_CHECK);
magic_load(magic, NULL);
// ifstream in_fastq(fastq_file, ios_base::in | ios_base::binary);
ifstream in_fastq(fastq_file, ios_base::in);
boost::iostreams::filtering_streambuf<boost::iostreams::input> in_buf;
if (std::strcmp("application/gzip; charset=binary", magic_file(magic, argv[2])) == 0)
in_buf.push(boost::iostreams::gzip_decompressor());
in_buf.push(in_fastq);
istream in_data(&in_buf);
ofstream out_clean(out_clean_file);
ofstream out_filter(out_filter_file);
cout << "\nUsing " << fastq_file << " and " << csv_file << "\n\nProcessing..." << "\n";
std::string buffer_filter;
buffer_filter.reserve(THRESHOLD);
std::string buffer_clean;
buffer_clean.reserve(THRESHOLD);
while(!in_data.eof())
{
if (!getline(in_data, f.name,'\n')) break;
if (!getline(in_data, f.sequence,'\n')) break;
if (!getline(in_data, f.info,'\n')) break;
if (!getline(in_data, f.quality,'\n')) break;
f.remove = false;
for (auto remove : to_remove) {
if (f.sequence.find(remove) != std::string::npos)
{
// cout << "Found: " << remove << " in " << f.name << '\n';
f.remove = true;
}
}
if (f.remove == false) {
if (buffer_clean.length() + 1 >= THRESHOLD) {
out_clean << buffer_clean;
buffer_clean.resize(0);
}
buffer_clean.append("@" + f.name + "\n");
buffer_clean.append(f.sequence + "\n");
buffer_clean.append(f.info + "\n");
buffer_clean.append(f.quality + "\n");
count_clean++;
}
else
{
if (buffer_filter.length() + 1 >= THRESHOLD) {
out_filter << buffer_filter;
buffer_filter.resize(0);
}
buffer_filter.append("@" + f.name + "\n");
buffer_filter.append(f.sequence + "\n");
buffer_filter.append(f.info + "\n");
buffer_filter.append(f.quality + "\n");
count_filter++;
}
}
out_clean << buffer_clean;
out_filter << buffer_filter;
cout << "\nCheck the results in " << out_clean_file << " (" << count_clean << ")\n";
cout << "\nFiltered results in " << out_filter_file << " (" << count_filter << ")\n";
out_clean.close();
out_filter.close();
in_fastq.close();
return EXIT_SUCCESS;
}
unordered_set<string> loadCSV(string csv_file) {
string seq;
unordered_set<string> to_remove;
CSVReader<1, trim_chars<' '>, no_quote_escape<'\t'>, single_line_comment<'#'>, empty_line_comment > csv (csv_file);
while(csv.read_row(seq))
{
to_remove.insert(seq);
cout << "\nMarked to Remove: " << seq << "\n";
}
return to_remove;
}