-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanual_db.cpp
286 lines (207 loc) · 6.72 KB
/
manual_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
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <getopt.h>
#include "maestro.h"
#include "kwage.h"
using namespace std;
int main(int argc, char *argv[])
{
try{
// Command line arguments
// -d <input database file to read accessions from>
// -s <status file to update>
// --meta <metadata file to read>
string database_filename;
string status_filename;
string metadata_filename;
const char* options = "d:s:h?";
int config_opt = 0;
int long_index = 0;
struct option long_opts[] = {
{"meta", true, &config_opt, 1},
{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
metadata_filename = optarg;
break;
}
cerr << "Unknown flag!" << endl;
break;
case 'd':
database_filename = optarg;
break;
case 's':
status_filename = 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-d <input database file to read accessions from>" << endl;
cerr << "\t-s <status file to update>" << endl;
cerr << "\t--meta <metadata file to read>" << endl;
return EXIT_SUCCESS;
}
if( database_filename.empty() ){
cerr << "Please specify a database file to read" << endl;
return EXIT_SUCCESS;
}
if( status_filename.empty() ){
cerr << "Please specify a status file to update" << endl;
return EXIT_SUCCESS;
}
if( metadata_filename.empty() ){
cerr << "Please specify a metadata file to read" << endl;
return EXIT_SUCCESS;
}
// Make sure we can open the database file for reading
ifstream fdb(database_filename.c_str(), ios::binary);
if(!fdb){
cerr << "Unable to open the database file " << database_filename << " for reading accessions" << endl;
return EXIT_FAILURE;
}
// Read the binary metadata file and extract:
// - The list of SRA accessions
// - The starting location of the each SRA accession metadata record in the metadata file
// Note that the resulting list of (accession, location) will be sorted by accession for
// fast lookup by accession
vector< pair<SraAccession, size_t /*location*/> > accession_loc;
if( parse_accession_loc(accession_loc, metadata_filename, true /*verbose*/) == false ){
cerr << "Error reading metadata information from " << metadata_filename << endl;
return EXIT_FAILURE;
}
const size_t num_sra = accession_loc.size();
if(num_sra == 0){
cerr << "Did not read any SRA accessions from the input metadata file" << endl;
return EXIT_FAILURE;
}
unsigned char *status = new unsigned char [num_sra];
if(!status){
throw __FILE__ ":main: Unable to allocate status buffer";
}
memset(status, STATUS_INIT, num_sra);
size_t database_index = 1; // <-- Start counting database files from 1
// Read the status file
if( restore_status(status_filename, status, num_sra, database_index, false /*create missing*/) == false){
if(status != NULL){
delete [] status;
status = NULL;
}
}
// Read the list of accessions from the database file
DBFileHeader header;
binary_read(fdb, header);
if(!fdb){
cerr << "Unable to read database header" << endl;
return EXIT_FAILURE;
}
cerr << "Header information for " << database_filename << endl;
cerr << "\tmagic = " << header.magic << endl;
cerr << "\tversion = " << header.version << endl;
cerr << "\tcrc32 = " << std::hex << header.crc32 << std::dec << endl;
cerr << "\tkmer_len = " << header.kmer_len << endl;
cerr << "\tnum_hash = " << header.num_hash << endl;
const size_t filter_len = header.filter_len();
cerr << "\tfilter_len = " << filter_len << endl;
cerr << "\tlog_2_filter_len = " << header.log_2_filter_len << endl;
cerr << "\tnum_filter = " << header.num_filter << endl;
switch(header.hash_func){
case MURMUR_HASH_32:
cerr << "\thash_func = Murmur32" << endl;
break;
case UNKNOWN_HASH:
cerr << "\thash_func = Unknown" << endl;
break;
default:
cerr << "\thash_func = Invalid" << endl;
break;
};
// Jump to the start of annotation information (after the array of FilerInfo
// locations, which we do not need since we'll be reading the FileInfo structures
// sequentially)
fdb.seekg( header.info_start + header.num_filter*sizeof(unsigned long int) );
size_t num_success = 0;
size_t num_fail = 0;
cerr << "Updating: ";
string info_buffer;
for(unsigned int i = 0;i < header.num_filter;++i){
FilterInfo info;
binary_read(fdb, info);
if(!fdb){
cerr << "Error reading FilterInfo for filter " << i << endl;
throw __FILE__ ":main: Error reading FilterInfo from database";
}
if(info.run_accession == INVALID_ACCESSION){
cerr << "Warning: FilterInfo " << i << " has an invalid run accession!" << endl;
++num_fail;
continue;
}
for(string::const_iterator j = info_buffer.begin();j != info_buffer.end();++j){
cerr << '\b';
}
for(string::const_iterator j = info_buffer.begin();j != info_buffer.end();++j){
cerr << ' ';
}
for(string::const_iterator j = info_buffer.begin();j != info_buffer.end();++j){
cerr << '\b';
}
info_buffer = accession_to_str(info.run_accession);
cerr << info_buffer;
try{
const size_t index = get_accession_index(info.run_accession, accession_loc);
status[index] = STATUS_DATABASE_SUCCESS;
++num_success;
}
catch(...){
cerr << "Unable to find a valid status file index for SRA accession "
<< accession_to_str(info.run_accession) << endl;
++num_fail;
break;
}
}
cerr << endl;
fdb.close();
cerr << "Successfully updated the status of " << num_success << " out of "
<< header.num_filter << " accessions" << endl;
if(num_success != header.num_filter){
cerr << "Failed to update all accession -- the status file will not be modified" << endl;
}
else{
if( !write_status(status_filename, status, num_sra, database_index) ){
cerr << "** Warning! Unable to commit status information to disk!" << endl;
}
else{
cerr << "Successfully updated status file with " << num_success
<< " accessions from " << database_filename << endl;
}
}
if(status != NULL){
delete [] status;
status = NULL;
}
}
catch(const char *error){
cerr << "Caught the error: " << error << endl;
return EXIT_FAILURE;
}
catch(...){
cerr << "Caught an unhandled error" << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}