-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdedup.cpp
More file actions
265 lines (235 loc) · 8.88 KB
/
dedup.cpp
File metadata and controls
265 lines (235 loc) · 8.88 KB
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
// dedup.cpp
// removes PCR duplicates from paired-ends fastq files
// Simon Joly, 2025
#include <iostream>
#include <iomanip> // <<<< this is required for std::setprecision
#include <fstream>
#include <string>
#include <unordered_set>
#include <filesystem>
#include <zlib.h>
#include <openssl/sha.h>
#include <sqlite3.h>
#include <getopt.h>
#include "bloom_filter.hpp"
// --------------------------------------------------
// SHA-256 hashing (hex string)
// --------------------------------------------------
std::string sha256(const std::string& data) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((unsigned char*)data.c_str(), data.size(), hash);
std::string hex;
char buf[3];
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(buf, "%02x", hash[i]);
hex.append(buf);
}
return hex;
}
// --------------------------------------------------
// FASTQ record structure
// --------------------------------------------------
struct FastqRecord {
std::string id, seq, plus, qual;
};
// --------------------------------------------------
// Read next FASTQ record
// --------------------------------------------------
bool read_fastq_record(gzFile file, FastqRecord &rec) {
char buf[10000];
if (!gzgets(file, buf, sizeof(buf))) return false;
rec.id = buf;
if (!gzgets(file, buf, sizeof(buf))) return false;
rec.seq = buf;
if (!gzgets(file, buf, sizeof(buf))) return false;
rec.plus = buf;
if (!gzgets(file, buf, sizeof(buf))) return false;
rec.qual = buf;
return true;
}
// --------------------------------------------------
// Write FASTQ record
// --------------------------------------------------
void write_fastq_record(gzFile file, const FastqRecord &rec) {
gzputs(file, rec.id.c_str());
gzputs(file, rec.seq.c_str());
gzputs(file, rec.plus.c_str());
gzputs(file, rec.qual.c_str());
}
// --------------------------------------------------
// Count number of fastq records
// --------------------------------------------------
size_t count_fastq_records(const std::string& filename) {
gzFile f = gzopen(filename.c_str(), "rb");
if (!f) throw std::runtime_error("Cannot open file: " + filename);
char buf[8192];
size_t lines = 0;
while (gzgets(f, buf, sizeof(buf))) ++lines;
gzclose(f);
return lines / 4;
}
// --------------------------------------------------
// Extract barcode from FASTQ header
// --------------------------------------------------
std::string extract_barcode_from_name(const std::string& header) {
size_t space_pos = header.find(' ');
std::string main_part = (space_pos == std::string::npos) ? header : header.substr(0, space_pos);
size_t last_colon = main_part.rfind(':');
if (last_colon == std::string::npos) return "";
return main_part.substr(last_colon + 1);
}
// --------------------------------------------------
// SQLite backend
// --------------------------------------------------
class SQLiteStore {
sqlite3* db;
public:
SQLiteStore(const std::string& filename) {
if (sqlite3_open(filename.c_str(), &db))
throw std::runtime_error("Cannot open SQLite DB");
const char* sql = "CREATE TABLE IF NOT EXISTS hashes (hash TEXT PRIMARY KEY);";
sqlite3_exec(db, sql, 0, 0, 0);
}
~SQLiteStore() { sqlite3_close(db); }
bool is_unique(const std::string& key) {
sqlite3_stmt* stmt;
const char* insert = "INSERT INTO hashes (hash) VALUES (?);";
if (sqlite3_prepare_v2(db, insert, -1, &stmt, 0) != SQLITE_OK)
throw std::runtime_error("SQLite prepare failed");
sqlite3_bind_text(stmt, 1, key.c_str(), -1, SQLITE_STATIC);
bool unique = true;
if (sqlite3_step(stmt) != SQLITE_DONE) unique = false;
sqlite3_finalize(stmt);
return unique;
}
};
// --------------------------------------------------
// Main
// --------------------------------------------------
int main(int argc, char* argv[]) {
std::string read1_file, read2_file, index_file;
bool barcode_in_name = false;
std::string backend = "bloom"; // default
static struct option long_options[] = {
{"read1", required_argument, 0, 'a'},
{"read2", required_argument, 0, 'b'},
{"index", required_argument, 0, 'i'},
{"barcode-in-name", no_argument, 0, 'c'},
{"use-memory", no_argument, 0, 'm'},
{"use-bloom", no_argument, 0, 'l'},
{"use-sqlite", no_argument, 0, 's'},
{0, 0, 0, 0}
};
int opt;
while ((opt = getopt_long(argc, argv, "a:b:i:cmls", long_options, NULL)) != -1) {
switch (opt) {
case 'a': read1_file = optarg; break;
case 'b': read2_file = optarg; break;
case 'i': index_file = optarg; break;
case 'c': barcode_in_name = true; break;
case 'm': backend = "memory"; break;
case 'l': backend = "bloom"; break;
case 's': backend = "sqlite"; break;
default:
std::cerr << "Usage: dedup --read1 R1.fq.gz --read2 R2.fq.gz "
<< "[--index I.fq.gz] [--barcode-in-name] "
<< "[--use-memory | --use-bloom | --use-sqlite]\n";
return 1;
}
}
if (read1_file.empty() || read2_file.empty()) {
std::cerr << "Error: must provide --read1 and --read2\n";
return 1;
}
// Count reads
std::cerr << "Counting reads in " << read1_file << "...\n";
size_t total_reads = count_fastq_records(read1_file);
std::cerr << "Total reads: " << total_reads << "\n";
// Open input and output files
gzFile f1 = gzopen(read1_file.c_str(), "rb");
gzFile f2 = gzopen(read2_file.c_str(), "rb");
if (!f1 || !f2) {
std::cerr << "Error opening input files.\n";
return 1;
}
gzFile f3 = nullptr;
if (!index_file.empty()) {
f3 = gzopen(index_file.c_str(), "rb");
if (!f3) {
std::cerr << "Error opening index file.\n";
return 1;
}
}
// Extract base filenames (no directories)
std::string read1_base_filename = std::filesystem::path(read1_file).filename().string();
std::string read2_base_filename = std::filesystem::path(read2_file).filename().string();
std::string out1_name = "nodup_" + read1_base_filename;
std::string out2_name = "nodup_" + read2_base_filename;
gzFile out1 = gzopen(out1_name.c_str(), "wb");
gzFile out2 = gzopen(out2_name.c_str(), "wb");
// Backend init
SQLiteStore* sqlite_store = nullptr;
bloom_filter* bloom = nullptr;
std::unordered_set<std::string> seen;
if (backend == "sqlite") {
sqlite_store = new SQLiteStore("dedup.sqlite");
} else if (backend == "bloom") {
bloom_parameters params;
params.projected_element_count = static_cast<uint64_t>(total_reads);
params.false_positive_probability = 0.001;
params.compute_optimal_parameters();
bloom = new bloom_filter(params);
}
// Process FASTQ pairs
FastqRecord r1, r2, r3;
size_t processed = 0, dup = 0, written = 0;
while (read_fastq_record(f1, r1) && read_fastq_record(f2, r2)) {
std::string key;
if (barcode_in_name) {
std::string barcode = extract_barcode_from_name(r1.id);
key = sha256(barcode + r1.seq + r2.seq);
} if (!index_file.empty()) {
read_fastq_record(f3, r3);
key = sha256(r3.seq + r1.seq + r2.seq);
}
else {
key = sha256(r1.seq + r2.seq);
}
bool unique = true;
if (backend == "memory") {
if (seen.count(key)) unique = false;
else seen.insert(key);
} else if (backend == "sqlite") {
unique = sqlite_store->is_unique(key);
} else if (backend == "bloom") {
if (bloom->contains(key)) unique = false;
else bloom->insert(key);
}
if (unique) {
write_fastq_record(out1, r1);
write_fastq_record(out2, r2);
written++;
} else {
dup++;
}
processed++;
if (processed % 100000 == 0) {
double pct_processed = (100.0 * processed) / total_reads;
double pct_dup = (100.0 * dup) / processed;
std::cerr << "\rProcessed: " << processed << " / " << total_reads << " ("
<< std::fixed << std::setprecision(1) << pct_processed << "%) | "
<< dup << " (" << std::fixed << std::setprecision(1) << pct_dup << "%) duplicates" << std::flush;
}
}
// Cleanup
gzclose(f1); gzclose(f2);
if (f3) gzclose(f3);
gzclose(out1); gzclose(out2);
delete sqlite_store;
delete bloom;
std::cerr << "\nDone.\n";
std::cerr << "Processed: " << processed << " read pairs\n";
std::cerr << "Written: " << written << " unique read pairs\n";
std::cerr << "Duplicates: " << dup << " (" << (100.0 * dup / processed) << "%)\n";
return 0;
}