-
Notifications
You must be signed in to change notification settings - Fork 2
/
dump_db.cpp
326 lines (244 loc) · 9.19 KB
/
dump_db.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Dump the contents of KWAGE database file in a human readable
// format.
// J. D. Gans
// Bioscience Division, B-11
// Los Alamos National Laboratory
// Mon Mar 2 12:02:07 2020
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <getopt.h>
#include "kwage.h"
#include "bloom.h"
#include "string_conversion.h"
#include "keys.h"
using namespace std;
int main(int argc, char *argv[])
{
try{
// Command line arguments
// -i <input database file> (can be repeated)
// [-o <output file name>] (default is stdout)
// [--bits <maximum number of bit-slices to display>]
// [--bits.all] (display all bit-slices)
// [--bits.none] (don't display any bit-slices) <-- default
deque<string> input_filename;
string output_filename;
size_t num_bit_slice = 0;
const char* options = "o:i:h?";
int config_opt = 0;
int long_index = 0;
struct option long_opts[] = {
{"bits", true, &config_opt, 1},
{"bits.all", false, &config_opt, 2},
{"bits.none", false, &config_opt, 3},
{0,0,0,0} // Terminate options list
};
int opt_code;
opterr = 0;
bool print_usage = (argc == 1);
while( (opt_code = getopt_long( argc, argv, options, long_opts, &long_index) ) != EOF ){
switch( opt_code ){
case 0:
if(config_opt == 1){ // bits
num_bit_slice = str_to_size_t(optarg);
break;
}
if(config_opt == 2){ // bits.all
num_bit_slice = 0xFFFFFFFFFFFFFFFF;
break;
}
if(config_opt == 3){ // bits.none
num_bit_slice = 0;
break;
}
cerr << "Unknown flag!" << endl;
break;
case 'o':
output_filename = optarg;
break;
case 'i':
input_filename.push_back(optarg);
break;
case 'h':
case '?':
print_usage = true;
break;
default:
cerr << '\"' << (char)opt_code << "\" is not a valid option!" << endl;
break;
};
}
if(print_usage){
cerr << "Usage:" << endl;
cerr << "\t-i <input database file> (can be repeated)" << endl;
cerr << "\t[-o <output file name>] (default is stdout)" << endl;
cerr << "\t[--bits <maximum number of bit-slices to display>]" << endl;
cerr << "\t[--bits.all] (display all bit-slices)" << endl;
cerr << "\t[--bits.none] (don't display any bit-slices) <-- default" << endl;
return EXIT_SUCCESS;
}
if( input_filename.empty() ){
cerr << "Please specify one or more filenames" << endl;
return EXIT_SUCCESS;
}
ofstream fout;
if( !output_filename.empty() ){
fout.open( output_filename.c_str() );
if(!fout){
cerr << "Unable to open output filename: " << output_filename << endl;
return EXIT_FAILURE;
}
}
ostream &out = fout.is_open() ? fout : cout;
for(deque<string>::const_iterator f = input_filename.begin();f != input_filename.end();++f){
ifstream fin(f->c_str(), ios::binary);
if(!fin){
cerr << "Unable to open " << *f << " for reading" << endl;
return EXIT_FAILURE;
}
DBFileHeader header;
binary_read(fin, header);
if(!fin){
cerr << "Unable to read database header" << endl;
return EXIT_FAILURE;
}
out << "Header information for " << *f << endl;
out << "\tmagic = " << header.magic << endl;
out << "\tversion = " << header.version << endl;
out << "\tcrc32 = " << std::hex << header.crc32 << std::dec << endl;
out << "\tkmer_len = " << header.kmer_len << endl;
out << "\tnum_hash = " << header.num_hash << endl;
const size_t filter_len = header.filter_len();
out << "\tfilter_len = " << filter_len << endl;
out << "\tlog_2_filter_len = " << header.log_2_filter_len << endl;
out << "\tnum_filter = " << header.num_filter << endl;
switch(header.hash_func){
case MURMUR_HASH_32:
out << "\thash_func = Murmur32" << endl;
break;
case UNKNOWN_HASH:
out << "\thash_func = Unknown" << endl;
break;
default:
out << "\thash_func = Invalid" << endl;
break;
};
switch(header.compression){
case NO_COMPRESSION:
out << "\tcompression = None" << endl;
break;
case RLE_COMPRESSION:
out << "\tcompression = RLE" << endl;
break;
default:
out << "\tcompression = Invalid" << endl;
break;
};
if(header.compression != NO_COMPRESSION){
cerr << "Compressed database files are not currently supported!" << endl;
return EXIT_SUCCESS;
}
const unsigned int bytes_per_bitslice = header.num_filter/BitVector::BITS_PER_BLOCK +
( (header.num_filter%BitVector::BITS_PER_BLOCK) > 0 ? 1 : 0);
cout << "There are " << bytes_per_bitslice << " bytes per slice" << endl;
cout << "Info start @ " << header.info_start << endl;
if(header.info_start == 0){
cerr << "** Info start is 0 -- database is not complete! **" << endl;
return EXIT_SUCCESS;
}
BitVector slice(header.num_filter);
const size_t num_slice = min(num_bit_slice, header.filter_len() );
if(num_slice > 0){
out << "Raw bits for the first " << num_slice << " bitslices" << endl;
// Read the first few slices from the file
for(size_t i = 0;i < num_slice;++i){
slice.read(fin);
if(!fin){
throw __FILE__":search: Error reading slice from file (1)";
}
out << i;
for(size_t j = 0;j < header.num_filter;++j){
out << ' ' << (slice.get_bit(j) ? 1 : 0);
}
out << endl;
}
}
// Read and display any annotation information associated with this file
unsigned long int info_start = header.info_start;
// Read and throw away the location of each filter annotation
//unsigned long int *annotation_loc = new unsigned long int [header.num_filter];
//if(annotation_loc == NULL){
// throw __FILE__ ":main: Unable to allocate annotation_loc";
//}
//fin.seekg(info_start);
fin.seekg( info_start + header.num_filter*sizeof(unsigned long int) );
if(!fin){
throw __FILE__ ":main: Unable to jump to the start of FilterInfo data";
}
//fin.read( (char*)annotation_loc, header.num_filter*sizeof(unsigned long int) );
//delete [] annotation_loc;
for(unsigned int i = 0;i < header.num_filter;++i){
FilterInfo info;
binary_read(fin, info);
out << "Annotation information for Bloom filter " << i << endl;
out << "\trun_accession = " << ( (info.run_accession == INVALID_ACCESSION) ?
"NA" : accession_to_str(info.run_accession) ) << endl;
out << "\texperiment_accession = " << ( (info.experiment_accession == INVALID_ACCESSION) ?
"NA" : accession_to_str(info.experiment_accession) ) << endl;
out << "\texperiment_title = " << (info.experiment_title.empty() ?
"NA" : info.experiment_title) << endl;
out << "\texperiment_design_description = " << (info.experiment_design_description.empty() ?
"NA" : info.experiment_design_description) << endl;
out << "\texperiment_library_name = " << (info.experiment_library_name.empty() ?
"NA" : info.experiment_library_name) << endl;
out << "\texperiment_library_strategy = " << (info.experiment_library_strategy.empty() ?
"NA" : info.experiment_library_strategy) << endl;
out << "\texperiment_library_source = " << (info.experiment_library_source.empty() ?
"NA" : info.experiment_library_source) << endl;
out << "\texperiment_library_selection = " << (info.experiment_library_selection.empty() ?
"NA" : info.experiment_library_selection) << endl;
out << "\texperiment_instrument_model = " << (info.experiment_instrument_model.empty() ?
"NA" : info.experiment_instrument_model) << endl;
out << "\tsample_accession = " << ( (info.sample_accession == INVALID_ACCESSION) ?
"NA" : accession_to_str(info.sample_accession) ) << endl;
out << "\tsample_taxa = " << (info.sample_taxa.empty() ?
"NA" : info.sample_taxa) << endl;
if( !info.sample_attributes.empty() ){
out << "\tsample_attributes:" << endl;
// Enforce a consistent order on the sample attributes (note that the keys() function
// sorts the keys in ascending order)
const vector<string> attrib_key = keys(info.sample_attributes);
for(vector<string>::const_iterator j = attrib_key.begin();j != attrib_key.end();++j){
MAP<std::string, std::string>::const_iterator attrib_iter = info.sample_attributes.find(*j);
if( attrib_iter == info.sample_attributes.end() ){
throw __FILE__ ":main: Unable to lookup sample attribute by key name";
}
out << "\t\t" << attrib_iter->first << " = " << attrib_iter->second << endl;
}
}
out << "\tstudy_accession = " << ( (info.study_accession == INVALID_ACCESSION) ? "NA" :
accession_to_str(info.study_accession) ) << endl;
out << "\tstudy_title = " << (info.study_title.empty() ? "NA" : info.study_title) << endl;
out << "\tstudy_abstract = " << (info.study_abstract.empty() ? "NA" : info.study_abstract) << endl;
// Separate annotation fields with a blank line
out << endl;
}
}
}
catch(const char *error){
cerr << "Caught the error " << error << endl;
return EXIT_FAILURE;
}
catch(const string error){
cerr << "Caught the error " << error << endl;
return EXIT_FAILURE;
}
catch(...){
cerr << "Caught an unhandled error" << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}