-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlambda.cpp
More file actions
527 lines (468 loc) · 17.5 KB
/
Copy pathlambda.cpp
File metadata and controls
527 lines (468 loc) · 17.5 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
// ==========================================================================
// lambda
// ==========================================================================
// Copyright (c) 2013-2017, Hannes Hauswedell <h2 @ fsfe.org>
// Copyright (c) 2016-2017, Knut Reinert and Freie Universität Berlin
// All rights reserved.
//
// This file is part of Lambda.
//
// Lambda is Free Software: you can redistribute it and/or modify it
// under the terms found in the LICENSE[.md|.rst] file distributed
// together with this file.
//
// Lambda is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// ==========================================================================
// lambda.cpp: Main File for Lambda
// ==========================================================================
#include <iostream>
//TODO TEMPORARY REMOVE
#define amd64
#include <seqan/basic.h>
#include <seqan/sequence.h>
#include <seqan/arg_parse.h>
#include <seqan/seq_io.h>
#include <seqan/reduced_aminoacid.h>
#include <seqan/misc/terminal.h>
#include "output.hpp"
#include "options.hpp"
#include "holders.hpp"
#include "lambda.hpp"
using namespace seqan;
// inline BlastFormatFile
// _fileType(LambdaOptions const & options)
// {
// if (endsWith(options.output, ".m0"))
// return BlastFormatFile::PAIRWISE;
// else if (endsWith(options.output, ".m8"))
// return BlastFormatFile::TABULAR;
// else if (endsWith(options.output, ".m9"))
// return BlastFormatFile::TABULAR_WITH_COMMENTS;
// else
// return BlastFormatFile::UNKNOWN;
// }
// forwards
inline int
argConv0(LambdaOptions & options);
//-
template <typename TOutFormat,
BlastTabularSpec h>
inline int
argConv1(LambdaOptions & options,
TOutFormat const & /**/,
BlastTabularSpecSelector<h> const &);
//-
template <typename TOutFormat,
BlastTabularSpec h,
BlastProgram p>
inline int
argConv2(LambdaOptions & options,
TOutFormat const & /**/,
BlastTabularSpecSelector<h> const &,
BlastProgramSelector<p> const &);
//-
template <typename TOutFormat,
typename TRedAlph,
BlastTabularSpec h,
BlastProgram p>
inline int
argConv3(LambdaOptions & options,
TOutFormat const &,
BlastTabularSpecSelector<h> const &,
BlastProgramSelector<p> const &,
TRedAlph const &);
//-
template <typename TOutFormat,
typename TRedAlph,
typename TScoreExtension,
BlastTabularSpec h,
BlastProgram p>
inline int
argConv4(LambdaOptions & options,
TOutFormat const & /**/,
BlastTabularSpecSelector<h> const &,
BlastProgramSelector<p> const &,
TRedAlph const & /**/,
TScoreExtension const & /**/);
//-
template <typename TIndexSpec,
typename TRedAlph,
typename TScoreExtension,
typename TOutFormat,
BlastProgram p,
BlastTabularSpec h>
inline int
realMain(LambdaOptions & options,
TOutFormat const & /**/,
BlastTabularSpecSelector<h> const &,
BlastProgramSelector<p> const &,
TRedAlph const & /**/,
TScoreExtension const & /**/);
// --------------------------------------------------------------------------
// Function main()
// --------------------------------------------------------------------------
// Program entry point.
int main(int argc, char const ** argv)
{
// Parse the command line.
seqan::ArgumentParser parser;
LambdaOptions options;
seqan::ArgumentParser::ParseResult res = parseCommandLine(options, argc, argv);
// If there was an error parsing or built-in argument parser functionality
// was triggered then we exit the program. The return code is 1 if there
// were errors and 0 if there were none.
if (res != seqan::ArgumentParser::PARSE_OK)
return res == seqan::ArgumentParser::PARSE_ERROR;
if (std::string(CMAKE_BUILD_TYPE) != "Release")
std::cerr << "WARNING: This binary is not built in release mode and will be much slower than it should be!\n";
#ifdef NDEBUG
int ret = 0;
try
{
ret = argConv0(options);
} catch (std::bad_alloc const & e)
{
std::cerr << "ERROR: Lambda ran out of memory :(\n"
" You need to split your file into smaller segments or search against a smaller database.\n";
ret = 1;
} catch (std::exception const & e)
{
std::cerr << e.what() << '\n';
ret = 1;
}
return ret;
#else
// In debug mode we don't catch the exceptions so that we get a backtrace from SeqAn's handler
return argConv0(options);
#endif
}
// CONVERT Run-time options to compile-time Format-Type
inline int
argConv0(LambdaOptions & options)
{
CharString output = options.output;
if (endsWith(output, ".gz"))
output = prefix(output, length(output) - 3);
else if (endsWith(output, ".bz2"))
output = prefix(output, length(output) - 4);
if (endsWith(output, ".m0"))
return argConv1(options, BlastReport(), BlastTabularSpecSelector<BlastTabularSpec::NO_COMMENTS>());
else if (endsWith(output, ".m8"))
return argConv1(options, BlastTabular(), BlastTabularSpecSelector<BlastTabularSpec::NO_COMMENTS>());
else if (endsWith(output, ".m9"))
return argConv1(options, BlastTabular(), BlastTabularSpecSelector<BlastTabularSpec::COMMENTS>());
else if (endsWith(output, ".sam") || endsWith(output, ".bam")) // handled elsewhere
return argConv1(options, BlastTabular(), BlastTabularSpecSelector<BlastTabularSpec::COMMENTS>());
std::cerr << "ERROR: Cannot handle output extension.\n";
return -1;
}
template <typename TOutFormat,
BlastTabularSpec h>
inline int
argConv1(LambdaOptions & options,
TOutFormat const & /**/,
BlastTabularSpecSelector<h> const &)
{
switch(options.blastProgram)
{
#ifndef FASTBUILD
case BlastProgram::BLASTN:
return argConv3(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<BlastProgram::BLASTN>(),
Dna5());
#endif
case BlastProgram::BLASTP:
return argConv2(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<BlastProgram::BLASTP>());
case BlastProgram::BLASTX:
return argConv2(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<BlastProgram::BLASTX>());
#ifndef FASTBUILD
case BlastProgram::TBLASTN:
return argConv2(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<BlastProgram::TBLASTN>());
case BlastProgram::TBLASTX:
return argConv2(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<BlastProgram::TBLASTX>());
#endif
default:
break;
}
std::cerr << "ERROR: Cannot handle program mode (perhaps you are building in FASTMODE?).\n";
return -1;
}
/// Alphabet reduction
template <typename TOutFormat,
BlastTabularSpec h,
BlastProgram p>
inline int
argConv2(LambdaOptions & options,
TOutFormat const & /**/,
BlastTabularSpecSelector<h> const &,
BlastProgramSelector<p> const &)
{
using Th = BlastTabularSpecSelector<h>;
using Tp = BlastProgramSelector<p>;
switch (options.alphReduction)
{
case 0:
return argConv3(options, TOutFormat(), Th(), Tp(), AminoAcid());
case 2:
return argConv3(options, TOutFormat(), Th(), Tp(), ReducedAminoAcid<Murphy10>());
#if 0
case 10:
return argConv2(options, TOutFormat(), ReducedAminoAcid<ClusterReduction<10>>());
case 1:
return argConv2(options, TOutFormat(), AminoAcid10());
case 8:
return argConv2(options, TOutFormat(), ReducedAminoAcid<ClusterReduction<8>>());
case 12:
return argConv2(options, TOutFormat(), ReducedAminoAcid<ClusterReduction<12>>());
#endif
default:
break;
}
std::cerr << "ERROR: Cannot handle the specified alphabet reduction.\n";
return -1;
}
// extension model
template <typename TOutFormat,
typename TRedAlph,
BlastTabularSpec h,
BlastProgram p>
inline int
argConv3(LambdaOptions & options,
TOutFormat const &,
BlastTabularSpecSelector<h> const &,
BlastProgramSelector<p> const &,
TRedAlph const &)
{
if (options.gapOpen == 0)
#ifndef LAMBDA_LINGAPS_OPT
std::cerr << "ATTENTION: You have set the additional gap open cost to 0. If you run LAMBDA "
"in this configuration regularly, you might want to rebuild it with "
"LAMBDA_LINGAPS_OPT=1 to profit from additional optimizations.\n";
#else
return argConv4(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<p>(),
TRedAlph(),
LinearGaps());
else
#endif
return argConv4(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<p>(),
TRedAlph(),
AffineGaps());
}
template <typename TOutFormat,
typename TRedAlph,
typename TScoreExtension,
BlastTabularSpec h,
BlastProgram p>
inline int
argConv4(LambdaOptions & options,
TOutFormat const & /**/,
BlastTabularSpecSelector<h> const &,
BlastProgramSelector<p> const &,
TRedAlph const & /**/,
TScoreExtension const & /**/)
{
if (options.dbIndexType == DbIndexType::SUFFIX_ARRAY)
return realMain<IndexSa<>>(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<p>(),
TRedAlph(),
TScoreExtension());
else if (options.dbIndexType == DbIndexType::BI_FM_INDEX)
return realMain<BidirectionalIndex<TFMIndexInBi<>>>(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<p>(),
TRedAlph(),
TScoreExtension());
else
return realMain<TFMIndex<>>(options,
TOutFormat(),
BlastTabularSpecSelector<h>(),
BlastProgramSelector<p>(),
TRedAlph(),
TScoreExtension());
}
/// REAL MAIN
#ifdef _OPENMP
#define TID omp_get_thread_num()
#else
#define TID 0
#endif
template <typename TIndexSpec,
typename TRedAlph,
typename TScoreExtension,
typename TOutFormat,
BlastProgram p,
BlastTabularSpec h>
inline int
realMain(LambdaOptions & options,
TOutFormat const & /**/,
BlastTabularSpecSelector<h> const &,
BlastProgramSelector<p> const &,
TRedAlph const & /**/,
TScoreExtension const & /**/)
{
using TGlobalHolder = GlobalDataHolder<TRedAlph, TIndexSpec, TOutFormat, p, h>;
using TLocalHolder = LocalDataHolder<TGlobalHolder, TScoreExtension>;
myPrint(options, 1, "LAMBDA - the Local Aligner for Massive Biological DatA"
"\n======================================================"
"\nVersion ", SEQAN_APP_VERSION, "\n\n");
int ret = validateIndexOptions<TRedAlph, p>(options);
if (ret)
return ret;
if (options.verbosity >= 2)
printOptions<TLocalHolder>(options);
ret = checkRAM(options);
if (ret)
return ret;
TGlobalHolder globalHolder;
// context(globalHolder.outfile).scoringScheme._internalScheme = matr;
ret = prepareScoring(globalHolder, options);
if (ret)
return ret;
ret = loadSubjects(globalHolder, options);
if (ret)
return ret;
ret = loadDbIndexFromDisk(globalHolder, options);
if (ret)
return ret;
ret = loadTaxonomy(globalHolder, options);
if (ret)
return ret;
ret = loadQuery(globalHolder, options);
if (ret)
return ret;
// std::cout << "1st Query:\n"
// << front(globalHolder.qrySeqs) << "\n"
// << front(globalHolder.redQrySeqs) << "\n";
//
// std::cout << "last Query:\n"
// << back(globalHolder.qrySeqs) << "\n"
// << back(globalHolder.redQrySeqs) << "\n";
//
// std::cout << "1st Subject:\n"
// << front(globalHolder.subjSeqs) << "\n"
// << front(globalHolder.redSubjSeqs) << "\n";
//
// std::cout << "last Subject:\n"
// << back(globalHolder.subjSeqs) << "\n"
// << back(globalHolder.redSubjSeqs) << "\n";
myWriteHeader(globalHolder, options);
if (options.doubleIndexing)
{
myPrint(options, 1,
"Searching ",
options.queryPart,
" blocks of query with ",
options.threads,
" threads...\n");
if ((options.isTerm) && (options.verbosity >= 1))
{
for (unsigned char i=0; i< options.threads+3; ++i)
std::cout << std::endl;
std::cout << "\033[" << options.threads+2 << "A";
}
} else
{
myPrint(options, 1, "Searching and extending hits on-line...progress:\n"
"0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%\n|");
}
double start = sysTime();
uint64_t lastPercent = 0;
SEQAN_OMP_PRAGMA(parallel)
{
TLocalHolder localHolder(options, globalHolder);
SEQAN_OMP_PRAGMA(for schedule(dynamic))
for (uint64_t t = 0; t < localHolder.nBlocks; ++t)
{
int res = 0;
localHolder.init(t);
// seed
#ifdef LAMBDA_MICRO_STATS
double buf = sysTime();
#endif
if (options.doubleIndexing)
{
res = generateSeeds(localHolder);
if (res)
continue;
res = generateTrieOverSeeds(localHolder);
if (res)
continue;
}
#ifdef LAMBDA_MICRO_STATS
localHolder.stats.timeGenSeeds += sysTime() - buf;
// search
buf = sysTime();
#endif
search(localHolder); //TODO seed refining if iterateMatches gives 0 results
#ifdef LAMBDA_MICRO_STATS
localHolder.stats.timeSearch += sysTime() - buf;
#endif
// // TODO DEBUG
// for (auto const & m : localHolder.matches)
// _printMatch(m);
// sort
if (options.filterPutativeAbundant || options.filterPutativeDuplicates || options.mergePutativeSiblings)
{
#ifdef LAMBDA_MICRO_STATS
buf = sysTime();
#endif
sortMatches(localHolder);
#ifdef LAMBDA_MICRO_STATS
localHolder.stats.timeSort += sysTime() - buf;
#endif
}
// extend
if (length(localHolder.matches) > 0)
res = iterateMatches(localHolder);
if (res)
continue;
if ((!options.doubleIndexing) && (TID == 0) &&
(options.verbosity >= 1))
{
unsigned curPercent = ((t * 50) / localHolder.nBlocks) * 2; // round to even
printProgressBar(lastPercent, curPercent);
}
} // implicit thread sync here
if ((!options.doubleIndexing) && (TID == 0) && (options.verbosity >= 1))
printProgressBar(lastPercent, 100);
SEQAN_OMP_PRAGMA(critical(statsAdd))
{
globalHolder.stats += localHolder.stats;
}
}
if (ret)
return ret;
myWriteFooter(globalHolder, options);
if (!options.doubleIndexing)
{
myPrint(options, 2, "Runtime total: ", sysTime() - start, "s.\n\n");
}
printStats(globalHolder.stats, options);
return 0;
}