forked from lucian-ilie/SAGE2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
521 lines (448 loc) · 14.9 KB
/
main.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
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
/*****************************************************************************
* Author : Mike Molnar and Ehsan Haghshenas
* Date : August 27, 2015
* Description :
* Changes :
*
****************************************************************************/
#include "main.h"
int main(int argc, char *argv[])
{
//set default values and initializations
defaultVals();
//parse the command line arguments
parseArgs(argc, argv);
//check if all the required options are set
if(!checkRequired())
{
cout<< "\n";
printUsage();
cout<< "(For more information run ./SAGE2 -h)\n\n";
exit(0);
}
initializeEverything();
ReadLoader *loaderObj=NULL;
HashTable *hashObj=NULL;
EconomyGraph *economyObj=NULL;
OverlapGraph *graphObj=NULL;
CopyCountEstimator *estimateObj=NULL;
MatePair *mateObj=NULL;
ContigExtender *mergeObj=NULL;
ScaffoldMaker *scaffObj=NULL;
//step 1
if(minStep<=1 && maxStep>=1)
{
logStream<< "***********************************************************************************************************\n";
logStream<< " STEP 1\n";
logStream<< " organizing reads\n";
logStream<< "***********************************************************************************************************\n";
logStream.flush();
loaderObj = new ReadLoader(minOverlap);
if(listInput!="")
loaderObj->loadFromList(listInput);
else
loaderObj->readDatasetInBytes(fileInput);
loaderObj->organizeReads();
if(maxStep==1)
{
loaderObj->saveReadsInFile(outputDir+prefixName+".reads");
delete loaderObj;
}
else if(saveAll==true)
{
loaderObj->saveReadsInFile(outputDir+prefixName+".reads");
}
}
if(minStep<=2 && maxStep>=2)
{
logStream<< "***********************************************************************************************************\n";
logStream<< " STEP 2\n";
logStream<< " building hash table\n";
logStream<< "***********************************************************************************************************\n";
logStream.flush();
if(minStep==2)
{
loaderObj = new ReadLoader(minOverlap);
loaderObj->loadReadsFromFile(outputDir+inputPrefix+".reads");
}
hashObj = new HashTable(minOverlap, loaderObj);
hashObj->hashPrefixesAndSuffix();
if(maxStep==2)
{
if(minStep==1 && saveAll==false)
loaderObj->saveReadsInFile(outputDir+prefixName+".reads");
hashObj->saveHashTableInFile(outputDir+prefixName+".hashTable");
delete hashObj;
delete loaderObj;
}
else if(saveAll==true)
{
hashObj->saveHashTableInFile(outputDir+prefixName+".hashTable");
}
}
if(minStep<=3 && maxStep>=3)
{
logStream<< "***********************************************************************************************************\n";
logStream<< " STEP 3\n";
logStream<< " building overlap graph\n";
logStream<< "***********************************************************************************************************\n";
logStream.flush();
if(minStep==3)
{
loaderObj = new ReadLoader(minOverlap);
loaderObj->loadReadsFromFile(outputDir+inputPrefix+".reads");
hashObj = new HashTable(minOverlap, loaderObj);
hashObj->loadHashTableFromFile(outputDir+inputPrefix+".hashTable");
}
economyObj = new EconomyGraph(minOverlap, hashObj);
economyObj->buildInitialOverlapGraph();
economyObj->buildOverlapGraphEconomy();
delete hashObj;
economyObj->sortEconomyGraph();
graphObj = new OverlapGraph(economyObj, loaderObj);
graphObj->convertGraph();
delete economyObj;
if(maxStep==3)
{
if(minStep==1 && saveAll==false)
loaderObj->saveReadsInFile(outputDir+prefixName+".reads");
graphObj->saveOverlapGraphInFile(outputDir+prefixName+".graph3");
delete graphObj;
delete loaderObj;
}
else if(saveAll==true)
{
graphObj->saveOverlapGraphInFile(outputDir+prefixName+".graph3");
}
}
if(minStep<=4 && maxStep>=4)
{
logStream<< "***********************************************************************************************************\n";
logStream<< " STEP 4\n";
logStream<< " simplify overlap graph\n";
logStream<< "***********************************************************************************************************\n";
logStream.flush();
if(minStep==4)
{
loaderObj = new ReadLoader(minOverlap);
loaderObj->loadReadsFromFile(outputDir+inputPrefix+".reads");
graphObj = new OverlapGraph(loaderObj);
graphObj->loadOverlapGraphFromFile(outputDir+inputPrefix+".graph3");
}
int threshold=0, closeValue=10;
contractCompositePaths(graphObj, loaderObj);
removeDeadEnds(graphObj, loaderObj, threshold);
removeBubbles(graphObj, loaderObj, closeValue);
contractCompositePaths(graphObj, loaderObj);
uint64_t removedEdges = 0;
while(1)
{
removedEdges = 0;
removedEdges += removeDeadEnds(graphObj, loaderObj, threshold);
removedEdges += removeBubbles(graphObj, loaderObj, closeValue);
removedEdges += contractCompositePaths(graphObj, loaderObj);
if(removedEdges==0)
break;
if(closeValue<50)
closeValue += 10;
if(threshold<3)
threshold++;
}
if(maxStep==4)
{
if(minStep==1 && saveAll==false)
loaderObj->saveReadsInFile(outputDir+prefixName+".reads");
delete graphObj;
delete loaderObj;
}
}
if(minStep<=5 && maxStep>=5)
{
logStream<< "***********************************************************************************************************\n";
logStream<< " STEP 5\n";
logStream<< " copy count estimation (min cost flow) & single end assembly\n";
logStream<< "***********************************************************************************************************\n";
logStream.flush();
if(minStep==5)
{
loaderObj = new ReadLoader(minOverlap);
loaderObj->loadReadsFromFile(outputDir+inputPrefix+".reads");
graphObj = new OverlapGraph(loaderObj);
graphObj->loadOverlapGraphFromFile(outputDir+inputPrefix+".graph4");
}
graphObj->checkIfAllReadsPresent();
estimateObj = new CopyCountEstimator(graphObj, loaderObj);
genomeSize = estimateObj->genomeSizeEstimation();
estimateObj->computeMinCostFlow(outputDir);
delete estimateObj;
if(maxStep==5)
{
if(minStep==1 && saveAll==false)
loaderObj->saveReadsInFile(outputDir+prefixName+".reads");
graphObj->saveOverlapGraphInFile(outputDir+prefixName+".graph5");
delete graphObj;
delete loaderObj;
}
else if(saveAll==true)
{
graphObj->saveOverlapGraphInFile(outputDir+prefixName+".graph5");
}
}
if(minStep<=6 && maxStep>=6)
{
logStream<< "***********************************************************************************************************\n";
logStream<< " STEP 6\n";
logStream<< " Map mate pairs & merge single end assemblies to get contigs\n";
logStream<< "***********************************************************************************************************\n";
logStream.flush();
if(minStep==6)
{
loaderObj = new ReadLoader(minOverlap);
loaderObj->loadReadsFromFile(outputDir+inputPrefix+".reads");
graphObj = new OverlapGraph(loaderObj);
graphObj->loadOverlapGraphFromFile(outputDir+inputPrefix+".graph5");
}
mateObj = new MatePair(graphObj, loaderObj);
if(listInput!="")
mateObj->mapMatePairsFromList(listInput);
else
mateObj->mapMatePairs(fileInput, "", 1);
mateObj->meanSdEstimation();
mergeObj = new ContigExtender(graphObj, mateObj, loaderObj);
mergeObj->extendContigs(minOverlap);
delete mergeObj;
graphObj->printGraph(outputDir+prefixName+"_contig");
if(maxStep==6)
{
if(minStep==1 && saveAll==false)
loaderObj->saveReadsInFile(outputDir+prefixName+".reads");
graphObj->saveOverlapGraphInFile(outputDir+prefixName+".graph6");
delete mateObj;
delete graphObj;
delete loaderObj;
}
else if(saveAll==true)
{
graphObj->saveOverlapGraphInFile(outputDir+prefixName+".graph6");
}
}
if(minStep<=7 && maxStep>=7)
{
logStream<< "***********************************************************************************************************\n";
logStream<< " STEP 7\n";
logStream<< " merge contigs to get scaffolds\n";
logStream<< "***********************************************************************************************************\n";
logStream.flush();
if(minStep==7)
{
loaderObj = new ReadLoader(minOverlap);
loaderObj->loadReadsFromFile(outputDir+inputPrefix+".reads");
graphObj = new OverlapGraph(loaderObj);
graphObj->loadOverlapGraphFromFile(outputDir+inputPrefix+".graph6");
mateObj = new MatePair(graphObj, loaderObj);
if(listInput!="")
mateObj->mapMatePairsFromList(listInput);
else
mateObj->mapMatePairs(fileInput, "", 1);
mateObj->meanSdEstimation();
}
scaffObj = new ScaffoldMaker(graphObj, mateObj, loaderObj);
scaffObj->makeScaffolds(minOverlap);
delete scaffObj;
graphObj->printGraph(outputDir+prefixName+"_scaffold", true);
delete mateObj;
delete graphObj;
delete loaderObj;
}
logStream<< "\n*************************************************** END ***************************************************\n";
logStream.close();
return 0;
}
void printUsage()
{
cout<< "USAGE:\n";
cout<< "\t./SAGE2 [options] -f <inputFile> -k <minOverlap>\n\n";
}
void printListOfArgs()
{
cout<< "Full list of options:\n";
cout<< "\t-f|--fileInput <string>\n";
cout<< "\t\tInput file in fasta/fastq format (interleaved). Mutually\n\t\texclusive with option -l|--listInput. [Required]\n\n";
cout<< "\t-l|--listInput <string>\n";
cout<< "\t\tA list of input files in fasta/fastq format. Mutually exclusive\n\t\twith option -f|--fileInput. (for more information consult with\n\t\tthe README file). [Required]\n\n";
cout<< "\t-k|--minOverlap <int>\n";
cout<< "\t\tThe minimum length of an overlap to be considered for graph\n\t\tbuilding. [Required]\n\n";
cout<< "\t-o|--outputDir <string>\n";
cout<< "\t\tPath to the output directory. [Default: null]\n\n";
cout<< "\t-p|--prefix <string>\n";
cout<< "\t\tPrefix of all output files in the output directory.\n\t\t[Default: \"untitled\"]\n\n";
cout<< "\t-i|--inputPrefix <string>\n";
cout<< "\t\tPrefix of all input files in the output directory.\n\t\t[Default: value of prefix]\n\n";
cout<< "\t-m|--minStep <int>\n";
cout<< "\t\tThe first step which will be run by the program (for more\n\t\tinformation consult with the README file). [Default: 1]\n\n";
cout<< "\t-M|--maxStep <int>\n";
cout<< "\t\tThe last step which will be run by the program (for more\n\t\tinformation consult with the README file). [Default: 7]\n\n";
cout<< "\t-s|--saveAll\n";
cout<< "\t\tSaves the result of all intermediate steps.\n\n";
cout<< "\t-d|--debug\n";
cout<< "\t\tOutputs and saves more results for debugging.\n\n";
cout<< "\t-h|--help\n";
cout<< "\t\tPrints the usage and full list of options.\n\n";
}
void defaultVals()
{
minStep = 1;
maxStep = 7;
saveAll = false;
debugging = false;
minOverlap = 0;
fileInput = "";
listInput = "";
outputDir = "";
prefixName = "untitled";
inputPrefix = "";
lowThreshold = 1;
highThreshold = 5;
}
void initializeEverything()
{
makeDirectory(outputDir);
logStream.open((outputDir+prefixName+".log").c_str());
logStream.imbue(std::locale(""));
logStream<< std::fixed;
logStream<< setprecision(2);
logStream<< "***********************************************************************************************************\n";
logStream<< "\tEXECUTING PROGRAM: SAGE2 version 1.0\n";
if(listInput!="")
logStream<< "\t INPUT LIST PATH: " << listInput << "\n";
else
logStream<< "\t INPUT FILE PATH: " << fileInput << "\n";
logStream<< "\t OUTPUT DIRECTORY: " << outputDir << "\n";
logStream<< "\t OUTPUT PREFIX: " << prefixName << "\n";
logStream<< "\t MINIMUM OVERLAP: " << minOverlap << "\n";
logStream<< "\t START STEP: " << (int)minStep << "\n";
logStream<< "\t END STEP: " << (int)maxStep << "\n";
logStream<< "\t SAVE ALL FILES: " << (saveAll? "TRUE" : "FALSE") << "\n";
logStream<< "***********************************************************************************************************\n\n";
logStream.flush();
}
void parseArgs(int argc, char *argv[])
{
int c;
int option_index = 0;
bool fFlag=false, lFlag=false;
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"fileInput", required_argument, 0, 'f'},
{"minOverlap", required_argument, 0, 'k'},
{"listInput", required_argument, 0, 'l'},
{"outputDir", required_argument, 0, 'o'},
{"prefix", required_argument, 0, 'p'},
{"inputPrefix", required_argument, 0, 'i'},
{"minStep", required_argument, 0, 'm'},
{"maxStep", required_argument, 0, 'M'},
{"saveAll", no_argument, 0, 's'},
{"debug", no_argument, 0, 'd'},
{0, 0, 0, 0}
};
while((c = getopt_long(argc, argv, "hf:l:k:o:p:i:m:M:sd", long_options, &option_index)) != -1)
{
switch(c)
{
case 0:
cout<< "option --" << long_options[option_index].name;
if(optarg)
cout<< " with argument " << optarg;
cout<< "\n";
break;
case 'h':
cout<< "\n";
printUsage();
printListOfArgs();
exit(0);
break;
case 'f':
fileInput = optarg;
fFlag = true;
break;
case 'l':
listInput = optarg;
lFlag = true;
break;
case 'k':
minOverlap = atoi(optarg);
break;
case 'o':
outputDir = optarg;
if(outputDir!="")
outputDir = trimBack(outputDir, "/") + "/";
break;
case 'p':
prefixName = optarg;
break;
case 'i':
inputPrefix = optarg;
break;
case 'm':
minStep = atoi(optarg);
if(minStep<1)
minStep = 1;
break;
case 'M':
maxStep = atoi(optarg);
if(maxStep>7)
maxStep = 7;
break;
case 's':
saveAll = true;
break;
case 'd':
debugging = true;
break;
case '?':
cout<< "\n";
exit(0);
break;
default:
cout<< "[ERROR] Wrong command line arguments!\n\n";
exit(0);
}
}
if (optind < argc)
{
cout<< "[WARNING] There are some non-option arguments: ";
while (optind < argc)
cout<< argv[optind++] << " ";
cout<< "\n";
}
if(fFlag && lFlag)
{
cout<< "[ERROR] Options -f|--fileInput and -l|--listInput are mutually exclusive!\n\n";
exit(0);
}
}
bool checkRequired()
{
bool allSet = true;
if(fileInput=="" && listInput=="")
{
cout<< "[ERROR] One of the options -f|--fileInput or -l|--listInput is required.\n";
allSet = false;
}
if(minOverlap==0)
{
cout<< "[ERROR] Option -k|--minOverlap is required.\n";
allSet = false;
}
if(maxStep<minStep)
{
cout<< "[ERROR] maxStep should not be smaller than minStep!\n\n";
allSet = false;
}
if(inputPrefix=="")
{
inputPrefix = prefixName;
}
return allSet;
}