-
Notifications
You must be signed in to change notification settings - Fork 10
/
DumpBF.cpp
225 lines (191 loc) · 5 KB
/
DumpBF.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
#include <iostream>
#include <cstdio>
#include <string>
#include <getopt.h>
#include <sys/stat.h>
#include "Common.hpp"
#include "DumpBF.hpp"
#include "HashTables.hpp"
#include "Kmer.hpp"
#include "KmerIntPair.hpp"
struct DumpBF_ProgramOptions {
size_t k;
bool verbose;
bool quake;
string input;
string output;
DumpBF_ProgramOptions() : k(0), verbose(false), quake(false) {}
};
void DumpBF_PrintUsage() {
cerr << "BFCounter " << BFC_VERSION << endl << endl;
cerr << "Writes k-mer occurrences into a tab-separated text file." << endl << endl;
cerr << "Usage: BFCounter dump [options] ...";
cerr << endl << endl <<
"-k, --kmer-size=INT Size of k-mers, at most " << (int) (Kmer::MAX_K-1)<< endl <<
"-i, --input=STRING Filename for input file from count" << endl <<
"-o, --output=STRING Filename for output" << endl <<
" --quake Write q-mers and quality scores to file for use with Quake (default=FALSE)" << endl <<
" --verbose Print lots of messages during run" << endl << endl
;
}
void DumpBF_ParseOptions(int argc, char **argv, DumpBF_ProgramOptions &opt) {
int verbose_flag = 0;
int quake_flag = 0;
const char* opt_string = "k:i:o:";
static struct option long_options[] =
{
{"verbose", no_argument, &verbose_flag, 1},
{"quake" , no_argument, &quake_flag, 1},
{"kmer-size", required_argument, 0, 'k'},
{"input", required_argument, 0, 'i'},
{"output", required_argument, 0, 'o'},
{0,0,0,0}
};
int option_index = 0;
int c;
while (true) {
c = getopt_long(argc,argv,opt_string, long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
case 0: break;
case 'k':
opt.k = atoi(optarg);
break;
case 'o':
opt.output = optarg;
break;
case 'i':
opt.input = optarg;
break;
default: break;
}
}
if (verbose_flag) {
opt.verbose = true;
}
if (quake_flag) {
opt.quake = true;
}
}
bool DumpBF_CheckOptions(const DumpBF_ProgramOptions &opt) {
bool ret = true;
if (opt.k <= 0 || opt.k >= MAX_KMER_SIZE) {
cerr << "Error, invalid value for kmer-size: " << opt.k << endl;
cerr << "Values must be between 1 and " << (MAX_KMER_SIZE-1) << endl;
ret = false;
}
struct stat stFileInfo;
int intStat;
// check inputfile
intStat = stat(opt.input.c_str(), &stFileInfo);
if (intStat != 0) {
cerr << "Error: input file not found, " << opt.input << endl;
ret = false;
}
// TODO: check if we can write to output.
return ret;
}
void DumpBF_Quake(const DumpBF_ProgramOptions &opt) {
hmapq_t kmap;
FILE* f = fopen(opt.input.c_str(), "rb");
if (f == NULL) {
cerr << "Could not open file " << opt.input << endl;
exit(1);
} else {
kmap.read_metadata(f);
kmap.read_nopointer_data(f);
fclose(f);
f = NULL;
}
Kmer km_del;
km_del.set_deleted();
kmap.set_deleted_key(km_del);
FILE *of = fopen(opt.output.c_str(), "w");
if (of == NULL) {
cerr << "Could not open file for writing, " << opt.output << endl;
exit(1);
} else {
char buf[1024];
float qval;
Kmer km;
hmapq_t::iterator it, it_end;
it_end = kmap.end();
for (it = kmap.begin(); it != kmap.end(); ++it) {
km = it->first;
qval = it->second;
km.toString(&buf[0]);
fprintf(of, "%s\t%.6f\n",buf,qval);
}
fclose(of);
}
}
void DumpBF_Normal(const DumpBF_ProgramOptions &opt) {
// load hashtable from file
hmap_t kmap;
hmapL_t kmap_Large;
FILE* f = fopen(opt.input.c_str(), "rb");
if (f == NULL) {
cerr << "Could not open file " << opt.input << endl;
exit(1);
} else {
kmap.read_metadata(f);
kmap.read_nopointer_data(f);
kmap_Large.read_metadata(f);
kmap_Large.read_nopointer_data(f);
fclose(f);
f = NULL;
}
// set deleted key
Kmer km_del;
km_del.set_deleted();
kmap.set_deleted_key(km_del);
// open output file
FILE* of = fopen(opt.output.c_str(), "w");
if (of == NULL) {
cerr << "Could not open file for writing, " << opt.output << endl;
exit(1);
} else {
// buffer
char buf[1024];
size_t cov;
Kmer km;
hmap_t::iterator it,it_end;
hmapL_t::const_iterator l_it,l_it_end;
it_end = kmap.end();
for (it = kmap.begin(); it != kmap.end(); ++it) {
km = it->GetKey();
cov = it->GetVal();
if (cov == 255) {
l_it = kmap_Large.find(km);
if (l_it != kmap_Large.end()) {
cov = l_it->second;
}
}
km.toString(&buf[0]);
fprintf(of, "%s\t%zu\n",buf, cov);
}
fclose(of);
}
}
void DumpBF(int argc, char **argv) {
DumpBF_ProgramOptions opt;
DumpBF_ParseOptions(argc, argv, opt);
if (argc < 2) {
DumpBF_PrintUsage();
exit(1);
}
if (!DumpBF_CheckOptions(opt)) {
cerr << endl << endl;
DumpBF_PrintUsage();
exit(1);
}
Kmer::set_k(opt.k);
//size_t k = Kmer::k;
if (opt.quake) {
DumpBF_Quake(opt);
} else {
DumpBF_Normal(opt);
}
}