Repository navigation
Expand file tree
/
Copy pathchainx-block-graph.cpp
More file actions
547 lines (508 loc) · 20.4 KB
/
Copy pathchainx-block-graph.cpp
File metadata and controls
547 lines (508 loc) · 20.4 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <syncstream>
#include <filesystem>
#include <vector>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <thread>
#include "concurrentqueue.h" // https://github.com/cameron314/concurrentqueue
#include "chainx-block-graph.hpp"
#include "command-line-parsing/cmdline.h" // gengetopt-generated parser
#include "efg.hpp"
#include "chaining.hpp"
//#define CHAINX_DEBUG
using namespace chainx_block_graph;
using std::string;
using std::move, std::back_inserter;
std::mutex mutp;
moodycamel::ConcurrentQueue<std::string*> outputqueue { 50, 64, 64 }; // TODO: parameterize this!
moodycamel::ConcurrentQueue<string> anchorsqueue { 50, 64, 64 }; // TODO: parameterize this!
moodycamel::ConcurrentQueue<std::pair<string,vector<string>>*> taskqueue { 50, 64, 64 }; // TODO: parameterize this!
void writer_worker(std::atomic<bool> &input_done, std::atomic<bool>& done, Params ¶ms)
{
string *ptr;
while (true) {
while (outputqueue.try_dequeue(ptr)) {
params.outputfs << *ptr << "\n";
delete(ptr);
}
// queue is empty?
if (!done or !input_done)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
else
break;
}
}
void reader_worker(const Elasticfoundergraph &efg, std::atomic<bool> &input_done, Params ¶ms)
{
std::pair<string,vector<string>> *ptr;
std::unordered_set<string> anchor_ids;
string current_id = "", line;
vector<string> gafhits; // string description of anchors
string qname;
while (std::getline(params.anchorsfs, line)) {
if (line == "")
continue;
//istringstream linestream(line);
//const GAFHit a(linestream, efg, qname);
qname = read_gaf_query_id(line);
if (qname == current_id) {
//gafhits.push_back(a);
gafhits.push_back(line);
} else {
if (current_id != "" and anchor_ids.contains(qname)) {
std::cerr << "Fatal error for anchors of query " << qname << ": are the anchors sorted? Sort them, or try flag --unsorted_input" << std::endl;
exit(1);
}
if (current_id != "") {
//std::pair<string,vector<GAFHit>> *ptr = new std::pair(current_id,vector<GAFHit>(std::move(gafhits)));
std::pair<string,vector<string>> *ptr = new std::pair(current_id,vector<string>(std::move(gafhits)));
while (!taskqueue.try_enqueue(ptr))
std::this_thread::sleep_for(std::chrono::milliseconds(10));
gafhits.clear(); // TODO assert?
////gafhits.push_back(dummy_start(a, efg));
//istringstream liness(line);
//GAFHit a(liness, efg, qname);
//gafhits.push_back(dummy_start(a, efg).to_string(efg, qname));
}
if (current_id == "") {
//istringstream liness(line);
//GAFHit a(liness, efg, qname);
//gafhits.push_back(dummy_start(a, efg).to_string(efg, qname));
}
anchor_ids.insert(qname);
current_id = qname;
//gafhits.push_back(a);
gafhits.push_back(line);
}
}
if (gafhits.size() > 0) {
//taskqueue.enqueue(new std::pair(current_id,vector<GAFHit>(std::move(gafhits))));
taskqueue.enqueue(new std::pair(current_id,vector<string>(std::move(gafhits))));
}
input_done = true;
}
void chain_worker(const Elasticfoundergraph &graph, unordered_map<string,vector<GAFHit>> &anchors, std::atomic<bool> &input_done, Params ¶ms, vector<Stats> &stats, int statsindex)
{
vector<GAFHit> matches;
string query;
bool warning = false;
if (params.unsorted_anchors) {
while (anchorsqueue.try_dequeue(query)) {
stats[statsindex].reads += 1;
/*{
std::scoped_lock lck {mutp};
if (p == anchors.end())
return;
query = p.first;
anchorlist = p.second;
++p;
}*/
vector<GAFHit> &anchorlist = anchors.at(query);
stats[statsindex].seeds += anchorlist.size() - 1;
//std::cerr << "Chaining for query " << anchorlist[0].get_query_id() << "..." << std::endl;
//anchorlist.push_back(dummy_start(anchorlist.at(0), graph)); // dummy start is already in the list
anchorlist.push_back(dummy_end(anchorlist.at(0), graph));
if (!is_sorted(anchorlist)) {
if (!warning) {
std::cerr << "Anchors do not seem to be sorted by starting position in the query! Sorting...\n";
warning = true;
}
std::sort(anchorlist.begin(), anchorlist.end(), [](GAFHit a1, GAFHit a2)
{
return (a1.get_query_start() < a2.get_query_start());
});
}
#ifdef CHAINX_DEBUG
{
std::osyncstream osscerr(cerr);
std::cerr << "DEBUG Sorted anchors for query " << query << " are" << std::endl;
for (auto &a : anchorlist)
cerr << "DEBUG " << a.to_string(graph, query) << std::endl;
std::cerr << std::endl;
}
#endif
vector<GAFHit> solution;
int initial_guess;
if (params.initialguesscov == 0) {
initial_guess = params.initialguess;
} else {
initial_guess = anchorlist.at(0).get_query_length() - (compute_coverage_greedy(anchorlist) * params.initialguesscov);
}
if (params.global) {
if (params.alternativealignments == 0) {
solution = chain_global_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats[statsindex]);
} else {
vector<GAFHit> chain;
for (int i = params.alternativealignments; i > 0; i--) {
chain = chain_global_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats[statsindex], true);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
chain.clear();
}
chain = chain_global_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats[statsindex]);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
//chain.clear();
}
} else if (params.semiglobal) {
if (params.alternativealignments == 0) {
solution = chain_semiglobal_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats[statsindex]);
} else {
vector<GAFHit> chain;
for (int i = params.alternativealignments; i > 0; i--) {
chain = chain_semiglobal_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats[statsindex], true);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
chain.clear();
}
chain = chain_semiglobal_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats[statsindex]);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
//chain.clear();
}
}
//std::cerr << "...done." << std::endl;
bool reverse = false;
if (query.find("rev_") != std::string::npos) {
for (auto &a : solution)
a.reverse();
reverse = true;
}
if (params.nosplit) {
std::osyncstream ossoutput(params.outputfs);
for (auto &a : solution) {
string *ptr = new std::string(a.to_string(graph, (reverse) ? query.substr(4) : query));
while (!outputqueue.try_enqueue(ptr))
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
} else {
std::osyncstream ossoutput(params.outputfs);
for (auto &a : solution) {
if (params.splitgraphaligner) {
for (auto &b : a.split_single_graphaligner(graph)) {
string *ptr = new std::string(b.to_string(graph, (reverse) ? query.substr(4) : query));
while(!outputqueue.try_enqueue(ptr))
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
} else {
for (auto &b : a.split(graph)) {
string *ptr = new std::string(b.to_string(graph, (reverse) ? query.substr(4) : query));
while (!outputqueue.try_enqueue(ptr))
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
}
}
}
} else {
//std::pair<string,vector<GAFHit>>* task;
std::pair<string,vector<string>>* task;
while (true) {
while (taskqueue.try_dequeue(task)) {
stats[statsindex].reads += 1;
vector<GAFHit> anchors;
//task->second.push_back(dummy_start(task->second.at(0), graph)); // BUG?
//task->second.push_back(dummy_end(task->second.at(0), graph));
string query = string(task->first);
istringstream dummyss(task->second.at(0));
anchors.push_back(dummy_start(GAFHit(dummyss, graph, query), graph));
for (string &line : task->second) {
istringstream ss(line);
anchors.push_back(GAFHit(ss, graph, query));
}
dummyss = istringstream(task->second.at(0));
anchors.push_back(dummy_end(GAFHit(dummyss, graph, query), graph));
stats[statsindex].seeds += anchors.size() - 2;
if (!is_sorted(anchors)) {
std::cerr << "Anchors do not seem to be sorted for read " << query << "...\nSorting...\n";
std::sort(anchors.begin(), anchors.end(), [](GAFHit a1, GAFHit a2)
{
return (a1.get_query_start() < a2.get_query_start());
});
}
/*//std::sort(task->second.begin(), task->second.end(), [](GAFHit a1, GAFHit a2)
std::sort(anchors.begin(), anchors.end(), [](GAFHit a1, GAFHit a2)
{
return (a1.get_query_start() < a2.get_query_start());
});*/
vector<GAFHit> solution;
int initial_guess;
if (params.initialguesscov == 0) {
initial_guess = params.initialguess;
} else {
initial_guess = anchors.at(0).get_query_length() - (compute_coverage_greedy(anchors) * params.initialguesscov);
}
if (params.global) {
if (params.alternativealignments == 0) {
solution = chain_global_eds(anchors, graph, initial_guess, params.rampupfactor, stats[statsindex]);
} else {
vector<GAFHit> chain;
for (int i = params.alternativealignments; i > 0; i--) {
chain = chain_global_eds(anchors, graph, initial_guess, params.rampupfactor, stats[statsindex], true);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
chain.clear();
}
chain = chain_global_eds(anchors, graph, initial_guess, params.rampupfactor, stats[statsindex]);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
//chain.clear();
}
} else if (params.semiglobal) {
if (params.alternativealignments == 0) {
solution = chain_semiglobal_eds(anchors, graph, initial_guess, params.rampupfactor, stats[statsindex]);
} else {
vector<GAFHit> chain;
for (int i = params.alternativealignments; i > 0; i--) {
chain = chain_semiglobal_eds(anchors, graph, initial_guess, params.rampupfactor, stats[statsindex], true);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
chain.clear();
}
chain = chain_semiglobal_eds(anchors, graph, initial_guess, params.rampupfactor, stats[statsindex]);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
//chain.clear();
}
}
//std::cerr << "...done." << std::endl;
bool reverse = false;
if (query.find("rev_") != std::string::npos) {
for (auto &a : solution)
a.reverse();
reverse = true;
}
if (params.nosplit) {
std::osyncstream ossoutput(params.outputfs);
for (auto &a : solution) {
string *ptr = new std::string(a.to_string(graph, (reverse) ? query.substr(4) : query));
while (!outputqueue.try_enqueue(ptr))
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
} else {
std::osyncstream ossoutput(params.outputfs);
for (auto &a : solution) {
if (params.splitgraphaligner) {
for (auto &b : a.split_single_graphaligner(graph)) {
string *ptr = new std::string(b.to_string(graph, (reverse) ? query.substr(4) : query));
while(!outputqueue.try_enqueue(ptr))
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
} else {
for (auto &b : a.split(graph)) {
string *ptr = new std::string(b.to_string(graph, (reverse) ? query.substr(4) : query));
while (!outputqueue.try_enqueue(ptr))
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
}
}
delete(task);
}
if (input_done)
break;
else
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
}
int main(int argc, char* argv[])
{
gengetopt_args_info argsinfo;
if (cmdline_parser(argc, argv, &argsinfo) != 0) exit(1);
if (argsinfo.inputs_num == 0)
{cmdline_parser_print_help(); exit(1);};
if (argsinfo.inputs_num == 1)
{std::cerr << argv[0] << ": missing anchors in input and output file" << std::endl; exit(1);};
if (argsinfo.inputs_num == 2)
{std::cerr << argv[0] << ": missing output file" << std::endl; exit(1);};
if (argsinfo.inputs_num > 3)
{std::cerr << argv[0] << ": too many arguments" << std::endl; exit(1);};
Params params;
params.threads = argsinfo.threads_arg;
params.global = argsinfo.global_flag;
params.semiglobal = argsinfo.semi_global_flag;
params.nosplit = argsinfo.no_split_output_matches_flag;
params.unsorted_anchors = argsinfo.unsorted_input_flag;
params.splitgraphaligner = argsinfo.split_output_matches_graphaligner_flag;
params.alternativealignments = argsinfo.alternative_chains_arg;
params.initialguess = argsinfo.initial_guess_arg;
params.initialguesscov = argsinfo.initial_guess_coverage_arg;
params.rampupfactor = argsinfo.ramp_up_factor_arg;
Stats stats;
if (!params.global and !params.semiglobal)
{std::cerr << argv[0] << ": select mode (global or semiglobal)" << std::endl; exit(1);};
if (params.global and params.semiglobal)
{std::cerr << argv[0] << ": select only one mode (global or semiglobal)" << std::endl; exit(1);};
// TODO check ramp-up factor
// open files
std::filesystem::path graphpath {argsinfo.inputs[0]};
params.graphfs = std::ifstream {graphpath};
if (!params.graphfs) {std::cerr << "Error opening graph file " << graphpath << "." << std::endl; exit(1);};
std::filesystem::path anchorspath {argsinfo.inputs[1]};
params.anchorsfs = std::ifstream {anchorspath};
if (!params.anchorsfs) {std::cerr << "Error opening anchors file " << anchorspath << "." << std::endl; exit(1);};
// check and open output file
std::filesystem::path outputpath {argsinfo.inputs[2]};
if (std::filesystem::exists(outputpath)) {
if (argsinfo.overwrite_flag) {
params.outputfs = std::ofstream(outputpath, std::ios::out | std::ios::trunc);
} else {
std::cerr << "Error: output file already exists." << std::endl;
exit(1);
}
} else {
params.outputfs = std::ofstream(outputpath);
}
if (!params.outputfs) {std::cerr << "Error opening output file " << outputpath << "." << std::endl; exit(1);};
std::cerr << "Reading the graph..." << std::flush;
Elasticfoundergraph graph(params.graphfs);
std::cerr << " done." << std::endl;
std::cerr << "Indexing the graph..." << std::flush;
graph.init_eds_support();
std::cerr << " done." << std::endl;
std::unordered_map<string,vector<GAFHit>> anchors;
if (params.unsorted_anchors) {
std::cerr << "Reading the anchors..." << std::flush;
anchors = read_gaf_chaining(params.anchorsfs, graph, anchorsqueue);
{
long unsigned int n = 0;
for (auto &v : anchors) {
n += v.second.size() - 1;
}
std::cerr << " done (read " << n << " anchors)." << std::endl;
}
#ifdef CHAINX_DEBUG
std::cerr << std::endl;
for (auto &bucket : anchors) {
cerr << "DEBUG Set of anchors:" << std::endl;
for (auto &a : bucket.second)
cerr << "DEBUG " << a.to_string(graph, bucket.first) << std::endl;
}
std::cerr << std::endl;
#endif
}
if (params.threads == -1) {
if (!params.unsorted_anchors) {
std::cerr << "Mode not implemented yet! Try --unsorted_input flag." << std::endl;
exit(1);
}
for (auto &bucket : anchors) {
const string &query = bucket.first;
vector<GAFHit> &anchorlist = bucket.second;
//std::cerr << "Chaining for query " << query << "..." << std::endl;
anchorlist.push_back(dummy_end(anchorlist[0], graph));
std::sort(anchorlist.begin(), anchorlist.end(), [](GAFHit a1, GAFHit a2)
{
return (a1.get_query_start() < a2.get_query_start());
});
#ifdef CHAINX_DEBUG
std::cerr << "DEBUG Sorted anchors for query " << query << " are" << std::endl;
for (auto &a : anchorlist)
cerr << "DEBUG " << a.to_string(graph, query) << std::endl;
std::cerr << std::endl;
#endif
vector<GAFHit> solution;
int initial_guess;
if (params.initialguesscov == 0) {
initial_guess = params.initialguess;
} else {
initial_guess = anchorlist.at(0).get_query_length() - (compute_coverage_greedy(anchorlist) * params.initialguesscov);
}
if (params.global) {
if (params.alternativealignments == 0) {
solution = chain_global_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats);
} else {
vector<GAFHit> chain;
for (int i = params.alternativealignments; i > 0; i--) {
chain = chain_global_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats, true);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
chain.clear();
}
chain = chain_global_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
//chain.clear();
}
} else if (params.semiglobal) {
if (params.alternativealignments == 0) {
solution = chain_semiglobal_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats);
} else {
vector<GAFHit> chain;
for (int i = params.alternativealignments; i > 0; i--) {
chain = chain_semiglobal_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats, true);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
chain.clear();
}
chain = chain_semiglobal_eds(anchorlist, graph, initial_guess, params.rampupfactor, stats);
solution.reserve(solution.size() + chain.size());
move(chain.begin(), chain.end(), back_inserter(solution));
//chain.clear();
}
}
//std::cerr << "...done." << std::endl;
bool reverse = false;
if (query.find("rev_") != std::string::npos) {
for (auto &a : solution)
a.reverse();
reverse = true;
}
if (params.nosplit) {
for (auto &a : solution)
params.outputfs << a.to_string(graph, (reverse) ? query.substr(4) : query) << std::endl;
} else {
for (auto &a : solution) {
if (params.splitgraphaligner) {
for (auto &b : a.split_single_graphaligner(graph))
params.outputfs << b.to_string(graph, (reverse) ? query.substr(4) : query) << std::endl;
} else {
for (auto &b : a.split(graph))
params.outputfs << b.to_string(graph, (reverse) ? query.substr(4) : query) << std::endl;
}
}
}
}
} else {
std::atomic<bool> workers_done = false;
std::atomic<bool> input_done = params.unsorted_anchors ? true : false;
std::thread inputworker;
if (!params.unsorted_anchors) {
inputworker = std::thread(reader_worker, std::ref(graph), std::ref(input_done), std::ref(params));
}
std::thread outputworker(writer_worker, std::ref(input_done), std::ref(workers_done), std::ref(params));
vector<std::thread> workers;
vector<Stats> workerstats(params.threads);
for (int i = 0; i < params.threads; i++) {
workers.push_back(std::thread(chain_worker, std::ref(graph), std::ref(anchors), std::ref(input_done), std::ref(params), std::ref(workerstats), i));
}
for (int i = 0; i < workers.size(); i++)
workers[i].join();
workers_done = true;
for (const Stats &s : workerstats) {
stats = mergestats(stats, s);
}
if (!params.unsorted_anchors)
inputworker.join();
outputworker.join();
// sanity check?
outputworker = std::thread(writer_worker, std::ref(input_done), std::ref(workers_done), std::ref(params));
outputworker.join();
}
std::cerr << "chained " << stats.seeds << " seeds for " << stats.reads << " reads\n";
std::cerr << "min number of revisions is " << stats.miniterations << "\n";
std::cerr << "max number of revisions is " << stats.maxiterations << "\n";
std::cerr << "average number of revisions is " << (double)stats.totaliterations / stats.reads << "\n";
std::cerr << "min chaining cost is " << stats.mincost << "\n";
std::cerr << "max chaining cost is " << stats.maxcost << "\n";
std::cerr << "average chaining cost is " << (double)stats.totalcost / stats.reads << "\n";
std::cerr << "min relative chaining cost is " << stats.minrelativecost << "\n";
std::cerr << "max relative chaining cost is " << stats.maxrelativecost << "\n";
std::cerr << "average relative chaining cost is " << (double)stats.totalrelativecost / stats.reads << "\n";
}