-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallelKmerCountEstimate.cpp
303 lines (257 loc) · 8.1 KB
/
parallelKmerCountEstimate.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <iostream>
#include <climits>
#include <zlib.h>
#include <stdio.h>
#include "kseq.h"
#include <time.h>
#include "metrohash64.cpp"
#include <stdint.h>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
#include <cassert>
#include <string.h>
#include <cstring>
#include <string>
#include <vector>
#include <fstream>
#include <cmath>
#include <math.h>
#include <sys/time.h>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <stack>
#include <limits.h>
#include <map>
#include <bitset>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <cstring>
#include <iostream>
#include <random>
#include <cinttypes>
#include <thread>
#include <atomic>
#include <queue>
#include "ntHashIterator.hpp"
#include "sparsepp/spp.h"
using spp::sparse_hash_map;
using namespace std;
#define MAX_THREADPOOL 1000
//global variables
bool eof;
unsigned trailing_zeros(uint64_t n) {
return n ? __builtin_ctzll(n) : -1;
}
typedef sparse_hash_map<uint64_t, uint32_t> SMap;
using namespace std;
KSEQ_INIT(int, read)
void help() {
cout << "KmerEst [options] -f <fasta/fastq> -k <k-mer length> -s <sample size> -o <output file>" << endl
<< " -h help" << endl
<< " -f <file> Input sequence file " << endl
<< " -k <k-mer size > kmer size (default 31) " << endl
<< " -s <sample size> sample size (default 25m)" << endl
<< " -c coverage> coverage (default 64)" << endl
<< " -o Prefix of the Output file " << endl
<< " -t Number of threads required " << endl
<< " -q Queue size " << endl;
exit(0);
}
void consume(vector<SMap> &vmap, queue<char *> &Q, mutex &mtx, condition_variable &cond, int k_size, int L,
int &th, uint64_t &no_kmers) {
int count = 0;
uint64_t hash;
uint8_t tz;
while (!eof || !Q.empty()) {
unique_lock<mutex> lck(mtx);
while (Q.empty()) {
cond.wait(lck);
if (eof)
break;
}
while (!Q.empty()) {
ntHashIterator itr(Q.front(), 1, k_size);
while (itr != itr.end()) {
hash = (*itr)[0];
no_kmers++;
tz = trailing_zeros(hash);
if (tz >= th) {
if (vmap[tz].find(hash) != vmap[tz].end())
vmap[tz][hash] += 1;
else { //// insert if not there
vmap[tz].insert(make_pair(hash, 1));
++count; // insert if not there
if (count == L) {
int cnt = vmap[th].size();
count = count - cnt;
SMap().swap(vmap[th]);
++th;
}
}
}
++itr;
}
Q.pop();
}
cond.notify_one();
}
}
int main(int argc, char **argv) {
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
if (argc == 1) {
cout << argv[0] << " -f <seq.fa> -k <kmerLen> -s <minHeap_Size> -c <coverage> -o <out.txt>" << endl;
exit(0);
}
//making all required variable local.
int k = 31;
int s = 25000000;
int thread_pool_size = 4;
int c = 64;
string in_file, out_file;
int q_size = 10;
//Read input
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-h")) { help(); }
else if (!strcmp(argv[i], "-k")) {
k = atoi(argv[i + 1]);
i++;
} else if (!strcmp(argv[i], "-f")) {
in_file = argv[i + 1];
i++;
} else if (!strcmp(argv[i], "-s")) {
s = atoi(argv[i + 1]);
//k = s / thread_count;
i++;
} else if (!strcmp(argv[i], "-c")) {
c = atoi(argv[i + 1]);
i++;
} else if (!strcmp(argv[i], "-o")) {
out_file = argv[i + 1];
i++;
} else if (!strcmp(argv[i], "-t")) {
thread_pool_size = atoi(argv[i + 1]);
i++;
}
else if (!strcmp(argv[i], "-q")) {
q_size = atoi(argv[i + 1]);
i++;
}
}
if (in_file.empty() || out_file.empty()) {
help();
}
kseq_t *seq;
FILE *fp;
fp = fopen(in_file.c_str(), "r");
if (fp == Z_NULL) {
cout << "File: " << in_file << " does not exist" << endl;
exit(1);
}
seq = kseq_init(fileno(fp));
eof = false;
uint64_t total = 0;
int th_list[thread_pool_size];
uint64_t no_kmers_t[thread_pool_size];
uint64_t no_kmers = 0;
bool flag = false;
vector<SMap> consolidated_vmap(64);
queue<char *> Q[thread_pool_size];
thread t_pool[thread_pool_size];
vector<SMap> all_maps[thread_pool_size];
mutex mtx[thread_pool_size];
condition_variable cond[thread_pool_size];
for (int i = 0; i < thread_pool_size; ++i) {
no_kmers_t[i] = 0;
th_list[i] = 0;
all_maps[i] = vector<SMap>(64);
t_pool[i] = thread(&consume, ref(all_maps[i]), ref(Q[i]), ref(mtx[i]), ref(cond[i]), k, s / thread_pool_size,
ref(th_list[i]),
ref(no_kmers_t[i]));
}
//producer logic
int thread_no = 0; // thread index
while (true)
{
unique_lock<mutex> lck(mtx[thread_no]);
while (!Q[thread_no].empty())
cond[thread_no].wait(lck);
for (int push = 0; push < q_size; push++) {
int l = kseq_read(seq);
if (l < 0) {
flag = true;
break;
}
++total;
char *st = strdup(seq->seq.s);
Q[thread_no].push(st);
}
if (!Q[thread_no].empty()) {
cond[thread_no].notify_one();
}
if (flag)
break;
thread_no = (thread_no + 1) % thread_pool_size;
}
eof = true;
for (int i = 0; i < thread_pool_size; ++i) {
cond[i].notify_one();
t_pool[i].join();
}
//consolidation of threads data
int co = 0;
for (auto &item : all_maps) {
no_kmers += no_kmers_t[co];
for (int i = th_list[co]; i < 64; i++) {
SMap::iterator it = item[i].begin();
while (it != item[i].end()) {
if (consolidated_vmap[i].find(it->first) == consolidated_vmap[i].end()) {
consolidated_vmap[i][it->first] = it->second;
} else {
consolidated_vmap[i][it->first] = consolidated_vmap[i][it->first] + it->second;
}
++it;
}
}
co++;
}
int *th = max_element(th_list, th_list + thread_pool_size);
cout << "th: " << *th << endl;
cout << "No. of sequences: " << total << endl;
FILE *fo = fopen(out_file.c_str(), "w");
uint32_t csize = 0;
for (int i = *th; i < 64; i++) csize += consolidated_vmap[i].size();
unsigned long F0 = csize * pow(2, (*th));
cout << "F0: " << F0 << endl;
fprintf(fo, "F1\t%llu\n", (uint64_t) no_kmers);
fprintf(fo, "F0\t%lu\n", F0);
cout << endl;
cout << "total: " << total << endl;
cout << "no_kmer: " << no_kmers << endl;
unsigned long *freq = new unsigned long[c];
for (int i = 1; i <= c; i++) freq[i] = 0;
unsigned long tot = 0;
int xx = 0;
for (int i = *th; i < 64; i++) {
for (auto &p: consolidated_vmap[i]) {
if (p.second <= c) freq[p.second]++;
}
}
cout << "th: " << *th << endl;
for (int i = 1; i <= c; i++) {
unsigned long fff = (freq[i] * pow(2, *th));
fprintf(fo, "f%d\t%lu\n", i, fff);
}
fclose(fo);
std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> dur = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
double elapsed = dur.count();
cout << "\n\n Time taken = " << elapsed << " seconds\n" << endl;
return 0;
}