-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateBloom.cpp
273 lines (240 loc) · 8.49 KB
/
CreateBloom.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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <getopt.h>
#include <cassert>
#include <cmath>
#include "nthist.hpp"
#include "BloomFilter.hpp"
#include "ntHashIterator.hpp"
#include "Uncompress.h"
#ifdef _OPENMP
# include <omp.h>
#endif
#define PROGRAM "CreateBloom"
static const char VERSION_MESSAGE[] =
PROGRAM " Version 1.0.0 \n"
"Written by Hamid Mohamadi and Hamza Khan.\n"
"Copyright 2017 Canada's Michael Smith Genome Science Centre\n";
static const char USAGE_MESSAGE[] =
"Usage: " PROGRAM " [OPTION]... FILES...\n"
"Creates a Bloom filter (BF) to find exon-exon junctions.\n"
"Acceptable file formats: fastq, fasta, sam, bam, gz, bz, zip.\n"
"\n"
" Options:\n"
"\n"
" -t, --threads=N use N parallel threads [1]\n"
" -k, --kmer=N the length of kmer [50]\n"
" -d, --fpr1=N primary BF fpr [0.01]\n"
" -s, --fpr2=N secondary BF fpr [0.01]\n"
" -r, --ref using FASTA reference as input instead of FASTQ reads. Don't use fpr2 in this case\n"
" --help display this help and exit\n"
" --version output version information and exit\n"
"\n"
"Report bugs to hmohamadi@bcgsc.ca or hkhan@bcgsc.ca \n";
using namespace std;
namespace opt {
unsigned nThrd=1;
unsigned nhash1;
unsigned nhash2;
unsigned kmLen=50;
size_t m1;
size_t m2;
double fpr1=0.01;
double fpr2=0.01;
bool ref=false;
size_t dbfSize=0;
size_t sbfSize=0;
}
static const char shortopts[] = "t:k:d:s:H:h:r:c";
enum { OPT_HELP = 1, OPT_VERSION };
static const struct option longopts[] = {
{ "threads", required_argument, NULL, 't' },
{ "kmer", required_argument, NULL, 'k' },
{ "hash1", required_argument, NULL, 'H' },
{ "hash2", required_argument, NULL, 'h' },
{ "fpr1", required_argument, NULL, 'd' },
{ "fpr2", required_argument, NULL, 's' },
{ "ref", no_argument, NULL, 'r' },
{ "help", no_argument, NULL, OPT_HELP },
{ "version", no_argument, NULL, OPT_VERSION },
{ NULL, 0, NULL, 0 }
};
bool getFAseq(std::ifstream &uFile, std::string &line) {
bool good=false;
std::string hline;
line.clear();
do {
good=static_cast<bool>(getline(uFile, hline));
if(hline[0]=='>'&&!line.empty())
break;
if(hline[0]!='>')
line+=hline;
} while(good);
if(!good&&!line.empty())
good=true;
return good;
}
void loadBFfa(std::ifstream &in, BloomFilter &dbFilter) {
bool good = true;
#pragma omp parallel
for(string seq, hseq; good;) {
#pragma omp critical(in)
good = getFAseq(in,seq);
if(good) {
ntHashIterator itr(seq, opt::nhash1, opt::kmLen);
while (itr != itr.end()) {
dbFilter.insert(*itr);
++itr;
}
}
}
}
void genBFref(const vector<string> &inFiles) {
cerr << "k-mer length: " << opt::kmLen << "\n";
cerr << "Number of distinct k-mers: " << opt::dbfSize << "\n";
cerr << "Number of k-mers with freq>1: " << opt::sbfSize << "\n";
cerr << "BF fpr: " << opt::fpr1 << "\n";
cerr << "BF bits: " << opt::m1 << "\t bits/kmer= " << opt::m1/opt::dbfSize << "\n";
cerr << "BF hashes: " << opt::nhash1 << "\n";
BloomFilter dbFilter(opt::m1, opt::nhash1, opt::kmLen);
for (unsigned file_i = 0; file_i < inFiles.size(); ++file_i) {
std::ifstream in(inFiles[file_i].c_str());
loadBFfa(in, dbFilter);
in.close();
}
dbFilter.storeFilter("Bfilter.bf");
ofstream bfinfo("Bfilter.inf");
bfinfo << opt::m1 << "\n" << opt::nhash1 << "\n" <<opt::kmLen << "\n" << pow(1.0*dbFilter.getPop()/opt::m1,opt::nhash1);
bfinfo.close();
cerr << "BF actual fpr: " << setprecision(4) << fixed << pow(1.0*dbFilter.getPop()/opt::m1,opt::nhash1) << "\n";
cerr << "Popcnt of bf: " << dbFilter.getPop() << "\n";
}
void loadBFfq(std::ifstream &in, BloomFilter &dbFilter, BloomFilter &sbFilter) {
bool good = true;
unsigned maxHash= std::max(opt::nhash1,opt::nhash2);
#pragma omp parallel
for(string seq, hseq; good;) {
#pragma omp critical(in)
{
good =static_cast<bool>(getline(in, hseq));
good = static_cast<bool>(getline(in, seq));
good = static_cast<bool>(getline(in, hseq));
good = static_cast<bool>(getline(in, hseq));
}
if(good) {
ntHashIterator itr(seq, maxHash, opt::kmLen);
while (itr != itr.end()) {
if(!dbFilter.insert_make_change(*itr))
sbFilter.insert(*itr);
++itr;
}
}
}
}
void genBFseq(const vector<string> &inFiles) {
cerr << "k-mer length: " << opt::kmLen << "\n";
cerr << "Number of distinct k-mers: " << opt::dbfSize << "\n";
cerr << "Number of k-mers with freq>1: " << opt::sbfSize << "\n";
cerr << "Primary BF fpr: " << opt::fpr1 << "\n";
cerr << "Secondary BF fpr: " << opt::fpr2 << "\n";
cerr << "Primary BF bits: " << opt::m1 << "\t bits/kmer= " << opt::m1/opt::dbfSize << "\n";
cerr << "Secondary BF bits: " << opt::m2 << "\t bits/kmer= " << opt::m2/opt::sbfSize << "\n";
cerr << "Primary BF hashes: " << opt::nhash1 << "\n";
cerr << "Secondary BF hashes: " << opt::nhash2 << "\n";
BloomFilter dbFilter(opt::m1, opt::nhash1, opt::kmLen);
BloomFilter sbFilter(opt::m2, opt::nhash2, opt::kmLen);
for (unsigned file_i = 0; file_i < inFiles.size(); ++file_i) {
std::ifstream in(inFiles[file_i].c_str());
loadBFfq(in, dbFilter, sbFilter);
in.close();
}
sbFilter.storeFilter("Bfilter.bf");
ofstream bfinfo("Bfilter.inf");
bfinfo << opt::m1 << "\n" << opt::nhash1 << "\n" <<opt::kmLen << "\n" << pow(1.0*dbFilter.getPop()/opt::m1,opt::nhash1) << "\n";
bfinfo << opt::m2 << "\n" << opt::nhash2 << "\n" <<opt::kmLen << "\n" << pow(1.0*sbFilter.getPop()/opt::m2,opt::nhash2);
bfinfo.close();
cerr << "Primary BF actual fpr: " << setprecision(4) << fixed << pow(1.0*dbFilter.getPop()/opt::m1,opt::nhash1) << "\n";
cerr << "Secondary BF actual fpr: " << setprecision(4) << fixed << pow(1.0*sbFilter.getPop()/opt::m2,opt::nhash2) << "\n";
cerr << "Popcnt of pbf and sbf: " << dbFilter.getPop() << "\t" << sbFilter.getPop() << "\n";
}
int main(int argc, char** argv) {
double sTime = omp_get_wtime();
bool die = false;
for (int c; (c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1;) {
std::istringstream arg(optarg != NULL ? optarg : "");
switch (c) {
case '?':
die = true;
break;
case 't':
arg >> opt::nThrd;
break;
case 'k':
arg >> opt::kmLen;
break;
case 'd':
arg >> opt::fpr1;
break;
case 's':
arg >> opt::fpr2;
break;
case 'r':
opt::ref=true;
break;
case OPT_HELP:
std::cerr << USAGE_MESSAGE;
exit(EXIT_SUCCESS);
case OPT_VERSION:
std::cerr << VERSION_MESSAGE;
exit(EXIT_SUCCESS);
}
if (optarg != NULL && !arg.eof()) {
std::cerr << PROGRAM ": invalid option: `-"
<< (char)c << optarg << "'\n";
exit(EXIT_FAILURE);
}
}
if (argc - optind < 1) {
std::cerr << PROGRAM ": missing arguments\n";
die = true;
}
if (die) {
std::cerr << "Try `" << PROGRAM << " --help' for more information.\n";
exit(EXIT_FAILURE);
}
vector<string> inFiles;
for (int i = optind; i < argc; ++i) {
string file(argv[i]);
if(file[0]=='@') {
string inName;
ifstream inList(file.substr(1,file.length()).c_str());
while(getline(inList,inName))
inFiles.push_back(inName);
}
else
inFiles.push_back(file);
}
#ifdef _OPENMP
omp_set_num_threads(opt::nThrd);
#endif
getHist(opt::dbfSize, opt::sbfSize, opt::kmLen, opt::nThrd, inFiles);
opt::m1 = -1*log(opt::fpr1)/log(2)/log(2)*opt::dbfSize;
opt::m2 = -1*log(opt::fpr2)/log(2)/log(2)*opt::sbfSize;
opt::nhash1 = opt::m1/opt::dbfSize*log(2);
opt::nhash2 = opt::m2/opt::sbfSize*log(2);
if(opt::nhash1==0)
opt::nhash1 = 1;
if(opt::nhash2==0)
opt::nhash2 = 1;
if(opt::ref)
genBFref(inFiles);
else
genBFseq(inFiles);
cerr << "time(sec): " <<setprecision(4) << fixed << omp_get_wtime() - sTime << "\n";
return 0;
}