-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1195 lines (1091 loc) · 47.7 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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <omp.h>
#include <iostream>
#include <vector>
#include <ctime>
#include <seqan/seq_io.h>
#include <seqan/index.h>
#include <seqan/seeds.h>
#include <seqan/arg_parse.h>
#include <seqan/bam_io.h>
#include <seqan/find.h>
#include <seqan/basic.h>
#include <seqan/stream.h>
using namespace seqan;
/* Struct. Defines Parsing Options and CommandLine Arguments.
*
* Properties:
* std::string inRef - path to ReferenceFile. Can be a Fasta or Index File. If Index File please set Option -i
* std::string inReads - path to ReadsFile.
* std::string outSam - path to Output File
* bool loadIndex - boolean indicating if inRef is Fasta or Index File
* bool levenshtein - boolean indicating if distance is set to edit distance
* bool fmInd - boolean indicating if index mode is set to FM-Index
* bool buildIndexOnly - boolean indicating if program should only build index file
* bool matchExtend - boolean indicating if dropout threshold is set
* int seedLen - declare seed Length to split Reads
* int dropout - dropout threshold. Seed Extension will stop when score falls under threshold
* int threads - number of threads to use for processing reads
* int approxSearch - alowed mistakes for searching seed in genome
* Score<int, Simple> extendScheme - alignment scheme for seed extension in seed matching region
* Score<int, Simple> alignScheme - alignment scheme for Alignment between Read and ReferenceGenome
*
* Functions:
* ReadMapperOptions - Constructor of Struct
*
* Other header files required: <seqan/arg_parse.h> <seqan/basic.h>
* Subfunctions: none
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
struct ReadMapperOptions {
std::string inRef;
std::string inReads;
std::string outSam;
bool loadIndex;
bool levenshtein;
bool fmInd;
bool buildIndexOnly;
bool matchExtend;
int seedLen;
int dropout;
int threads;
int approxSearch;
Score<int, Simple> alignScheme;
Score<int, Simple> extendScheme;
ReadMapperOptions() :
inRef(""), inReads(""), outSam(""), loadIndex(false), fmInd(false), seedLen(10),
levenshtein(false), alignScheme(1, -1, 0, -1), dropout(3), approxSearch(0),extendScheme(2, -1, -1, -2), threads(1) { }
};
/* Struct to set Distance Object in runtime */
struct Levenshtein {
Levenshtein() { }
};
/* Struct to set Distance Object in runtime */
struct Hamming {
Hamming() { }
};
/* Struct to set seedExtesnionMode Object in runtime */
struct MatchExt {
MatchExt() { }
};
/* Struct to set seedExtesnionMode Object in runtime */
struct XDrop {
XDrop() { }
};
/* Struct to set IndexMode Object in runtime */
struct FMind {
FMind() { }
};
/* Struct to set IndexMode Object in runtime */
struct SAind {
SAind() { }
};
/* Function to call the extendSeed-Function with to runtime specialized options
*
* Syntax:
* extendWrapper(Seed<Simple> & seed1 , Dna5String &refGen,
* ReadMapperOptions &rmOptions, XDrop )
*
* Inputs:
* Seed<Simple> & seed1 - seed to extend
* Dna5String &refGen - reference Gen to extend seed over
* TText &read - read to extend Seed over
* ReadMapperOptions &rmOptions - options containing AlignmentScheme and dropOut-threshold
* XDrop - object telling the compiler wich version of extendSeed to call
*
* Outputs:
*
* Example:
* extendWrapper(seed1, refGen, rmOptions, XDrop())
*
* Other header files required: none
* Subfunctions: none
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
template<typename TText>
inline static void extendWrapper(Seed<Simple> &seed1, Dna5String &refGen, TText &read,
ReadMapperOptions &rmOptions, XDrop /**/) {
extendSeed(seed1, refGen, read, EXTEND_BOTH, rmOptions.extendScheme, 3, GappedXDrop());
}
/* Function to call the extendSeed-Function with to runtime specialized options
*
* Syntax:
* extendWrapper(Seed<Simple> & seed1 , Dna5String &refGen,
* ReadMapperOptions &rmOptions, MatchExt )
*
* Inputs:
* Seed<Simple> & seed1 - seed to extend
* Dna5String &refGen - reference Gen to extend seed over
* TText &read - read to extend Seed over
* ReadMapperOptions &rmOptions - options containing AlignmentScheme and dropOut-threshold
* MatchExt - object telling the compiler which version of extendSeed to call
*
* Outputs:
*
* Example:
* extendWrapper(seed1, refGen, rmOptions, MatchExt())
*
* Other header files required: none
* Subfunctions: none
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
template<typename TText>
inline static void extendWrapper(Seed<Simple> &seed1, Dna5String &refGen, TText &read,
ReadMapperOptions &rmOptions, MatchExt /**/) {
extendSeed(seed1, refGen, read, EXTEND_BOTH, MatchExtend());
}
/* Function to call the searchSeed-Function with to runtime specialized options
*
* Syntax:
* searchWrapper(TText &seed, StringSet<TText> &pat, Index<TText, TIndex> &index,
* TText &read, unsigned int &endPos,
* ReadMapperOptions &rmOptions,
* std::vector<long> &bestSeed, TExtendMode, Levenshtein) {
*
* Inputs:
* TText & seed - seed to extend
* StringSet<TText> &pat - pattern to search for
* Index<TText, TIndex> &index - index to search over
* TText &read - read in wich seed is located
* unsigned int &endPos - endPosition of seed in read
* ReadMapperOptions &rmOptions - options containing AlignmentScheme and dropOut-threshold etc.
* std::vector<long> &bestSeed - vector containing information about best seed found so far
* TExtendMode - object telling the compiler which version of extendSeed to call
* Levenshtein - object telling the compiler which version of searchWrapper to call
*
* Outputs:
* std::vector<long> - Vector containing:
* First is beginning Position of best hit of seed in ReferenceDNAString(i.e. Index)
* Second is ending Position of best hit of seed in ReferenceDNAString(i.e. Index)
* Third is score of best hit of seed
*
* Example:
* searchWrapper(seed, pat, index, read, 10, rmOptions, bestSeed, XDrop(), Levenshtein())
*
* Other header files required: none
* Subfunctions: extSeed
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
template<typename TText, typename TIndex, typename TExtendMode>
inline static std::vector<long> searchWrapper(TText &seed, StringSet<TText> &pat, Index<TText, TIndex> &index,
TText &read, unsigned int &endPos,
ReadMapperOptions &rmOptions,
std::vector<long> &bestSeed, TExtendMode /**/, Levenshtein /**/) {
typedef typename Iterator<StringSet<Dna5String> const, Rooted>::Type TPatternsIt;
typedef typename Iterator<Index<TText, TIndex>, TopDown<> >::Type TIndexIt;
// lambda-function. Is called each time find-function finds pattern in text
auto delegate = [&seed, &index, &read, &endPos, &bestSeed, &rmOptions](TIndexIt const &it,
TPatternsIt const &patternsIt,
unsigned /*score*/) {
auto pattern_len = length(*patternsIt);
// loop through occurences of pattern in text
for (unsigned i = 0; i < length(getOccurrences(it)); ++i){
//for (auto &occ: getOccurrences(it)) {
// calculate end positions
long begin = getSeqOffset(getOccurrences(it)[i]);
long end = begin + pattern_len;
long score = extSeed(seed, index, read, endPos, begin, end, rmOptions, TExtendMode());
if (score > bestSeed.back()) {
bestSeed.at(0) = begin;
bestSeed.at(1) = end;
bestSeed.at(2) = score;
}
}
};
find(index, pat, 1, delegate, Backtracking<EditDistance>());
return bestSeed;
}
/* Function to call the searchSeed-Function with to runtime specialized options
*
* Syntax:
* searchWrapper(TText &seed, StringSet<TText> &pat, Index<TText, TIndex> &index,
* TText &read, unsigned int &endPos,
* ReadMapperOptions &rmOptions,
* std::vector<long> &bestSeed, TExtendMode, Hamming) {
*
* Inputs:
* TText & seed - seed to extend
* StringSet<TText> &pat - pattern to search for
* Index<TText, TIndex> &index - index to search over
* TText &read - read in wich seed is located
* unsigned int &endPos - endPosition of seed in read
* ReadMapperOptions &rmOptions - options containing AlignmentScheme and dropOut-threshold etc.
* std::vector<long> &bestSeed - vector containing information about best seed found so far
* TExtendMode - object telling the compiler which version of extendSeed to call
* Hamming - object telling the compiler which version of searchWrapper to call
*
* Outputs:
* std::vector<long> - Vector containing:
* First is beginning Position of best hit of seed in ReferenceDNAString(i.e. Index)
* Second is ending Position of best hit of seed in ReferenceDNAString(i.e. Index)
* Third is score of best hit of seed
*
* Example:
* searchWrapper(seed, pat, index, read, 10, rmOptions, bestSeed, XDrop(), Hamming())
*
* Other header files required: none
* Subfunctions: extSeed
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
template<typename TText, typename TIndex, typename TExtendMode>
inline static std::vector<long> searchWrapper(TText &seed, StringSet<TText> &pat, Index<TText, TIndex> &index,
TText &read, unsigned int &endPos,
ReadMapperOptions &rmOptions,
std::vector<long> &bestSeed, TExtendMode /**/, Hamming /**/) {
typedef typename Iterator<StringSet<Dna5String> const, Rooted>::Type TPatternsIt;
typedef typename Iterator<Index<TText, TIndex>, TopDown<> >::Type TIndexIt;
// lambda-function. Is called each time find-function finds pattern in text
auto delegate = [&seed, &index, &read, &endPos, &bestSeed, &rmOptions](TIndexIt const &it,
TPatternsIt const &patternsIt,
unsigned /*score*/) {
auto pattern_len = length(*patternsIt);
// loop through occurences of pattern in text
for (unsigned i = 0; i < length(getOccurrences(it)); ++i){
//for (auto &occ: getOccurrences(it)) {
// calculate end positionsgetOccurrences(it)[i]
long begin = getSeqOffset(getOccurrences(it)[i]);
long end = begin + pattern_len;
long score = extSeed(seed, index, read, endPos, begin, end, rmOptions, TExtendMode());
if (score > bestSeed.back()) {
bestSeed.at(0) = begin;
bestSeed.at(1) = end;
bestSeed.at(2) = score;
}
}
};
find(index, pat, rmOptions.approxSearch, delegate, Backtracking<HammingDistance>());;
return bestSeed;
}
/* Function to parse Arguments from Command Line
*
* Syntax:
* ArgumentParser::ParseResult parseCommandLine(ReadMapperOptions &rmOptions, int argc, char const **argv)
*
* Inputs:
* ReadMapperOptions &rmOptions - optionObject from ReadMapperOption Class.
* See Struct Documentation for more information
* int argc - number of Arguments
* char const **argv - arguments from Command Line
*
* Outputs:
* ArgumentParser::ParseResult - result of parsing Arguments. Can be PARSE_OK or PARSE_ERROR.
*
* Example:
* parseCommandLine(rmOptions, 2 , argv )
*
* Other header files required: <seqan/arg_parse.h>, <seqan/basic.h>
* Subfunctions: none
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
inline ArgumentParser::ParseResult parseCommandLine(ReadMapperOptions &rmOptions, int argc, char const **argv) {
//------------ Set Up Parser and Arguments
ArgumentParser parser("readMapper");
// Set short description, version, and date.
setShortDescription(parser, "Read Mapper");
setVersion(parser, "1.0");
setDate(parser, "Mai 2016");
// Define usage line and long description.
addUsageLine(parser,
"[\\fIInputReference\\fP] [ \"\\fIOPTIONS\\fP\" ]");
addDescription(parser, "This program allows to map Reads to a Reference Genome.");
// 1 arguments required
addArgument(parser, ArgParseArgument(ArgParseArgument::STRING, "InPathRef"));
//------------ Define Options -- Section Modification Options
addSection(parser, "Modification Options");
addOption(parser, ArgParseOption("i", "index", "Option to load ReferenceGenome out of IndexFile instead out of FASTA. Default: FASTA"));
addOption(parser, ArgParseOption("b", "buildIndex", "Option to just build the Index File"));
addOption(parser, ArgParseOption("r", "readFile", "set Path to File containing Reads. FASTA or FASTQ",
ArgParseArgument::STRING, "STRING"));
addOption(parser, ArgParseOption("o", "outSam", "set Path to output File. Default: readFile path",
ArgParseArgument::STRING, "STRING"));
addOption(parser, ArgParseOption("t", "threads", "set Number of threads to use. Default: 1",
ArgParseArgument::INTEGER, "INT"));
addOption(parser, ArgParseOption("s", "seedLength", "Number of Characters of seed. Default: 10",
ArgParseArgument::INTEGER, "INT"));
addOption(parser, ArgParseOption("S", "suffixArray", "set the Index Modus to SuffixArray. Default: SuffixArray"));
addOption(parser, ArgParseOption("F", "fmIndex", "set the Index Modus to FM-Index. Default: SuffixArray"));
addOption(parser, ArgParseOption("l", "levenshtein", "set the extension Distance to Levenshtein-Distance. Default: Hamming distance"));
addOption(parser, ArgParseOption("d", "dropout", "set the dropout threshold when to stop extend seeds Default: exact search",
ArgParseArgument::INTEGER, "INT"));
addOption(parser, ArgParseOption("a", "approximateSearch", "set the allowed mistakes to search seed with. Default: 0",
ArgParseArgument::INTEGER, "INT"));
addOption(parser, ArgParseOption("ha", "hamming", "setting the extension Distance to Hamming-Distance. Default: Hamming distance"));
addOption(parser, ArgParseOption("ma", "match", "MatchCost. Default: 1",
ArgParseArgument::INTEGER, "INT"));
addOption(parser, ArgParseOption("mm", "mismatch", "MisMatchCost. Default: -1 ",
ArgParseArgument::INTEGER, "INT"));
addOption(parser, ArgParseOption("go", "gapOpen", "GapOpenCost. Default: -1",
ArgParseArgument::INTEGER, "INT"));
addOption(parser, ArgParseOption("ge", "gapExtend", "GapExtendCost. Default: 0",
ArgParseArgument::INTEGER, "INT"));
//------------ Add Examples Section.
addTextSection(parser, "Examples");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutIndex\\fP \\fB-i\\fP ",
"Load ReferenceGenome out of IndexFile instead out of FASTA-File.");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-r\\fP \\fB../pathToReadsFile\\fP \\fB-s\\fP \\fI10\\fP ",
"Set seedLength to 10(default)");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-b\\fP \\fB-S\\fP ",
"Set Index Modus to SuffixArray. Build Suffix Array only-Modus. No Reads are processed.");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-b\\fP \\fB-F\\fP ",
"Set Index Modus to FM-Index. Build FM-Index only-Modus. No Reads are processed.");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-r\\fP \\fB../pathToReadsFile\\fP \\fB-l\\fP \\fB-t\\fP \\fB4\\fP",
"Set the search mode to Levenshtein-Distance. Use 4 threads for searching");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-r\\fP \\fB../pathToReadsFile\\fP \\fB-l\\fP \\fB-d\\fP \\fI3\\fP",
"Set the droput threshold to 3. Found seed will be extended till score falls below threshold.");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-r\\fP \\fB../pathToReadsFile\\fP \\fB-ha\\fP ",
"Set the search mode to Hamming-Distance(default)");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-r\\fP \\fB../pathToReadsFile\\fP \\fB-l\\fP \\fB-ma\\fP \\fI2\\fP ",
"Set MatchCost to 2");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-r\\fP \\fB../pathToReadsFile\\fP \\fB-l\\fP \\fB-mm\\fP \\fI-1\\fP ",
"Set MisMatchCost to -1");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-r\\fP \\fB../pathToReadsFile\\fP \\fB-l\\fP \\fB-go\\fP \\fI-2\\fP ",
"Set GapOpenCost to -2");
addListItem(parser,
"\\fBreadMapper\\fP \\fB../pathToInPutFile\\fP \\fB-r\\fP \\fB../pathToReadsFile\\fP \\fB-l\\fP \\fB-ge\\fP \\fI-1\\fP ",
"Set GapExtendCost to -1");
//------------ Parse command line & read necessary Arguments and optional Values
ArgumentParser::ParseResult res = parse(parser, argc, argv);
// Only extract options if the program will continue after parseCommandLine()
if (res != ArgumentParser::PARSE_OK)
return res;
// read Values
getArgumentValue(rmOptions.inRef, parser, 0);
getOptionValue(rmOptions.inReads, parser, "readFile");
getOptionValue(rmOptions.outSam, parser, "outSam");
rmOptions.loadIndex = isSet(parser, "index");
getOptionValue(rmOptions.seedLen, parser, "seedLength");
rmOptions.fmInd = isSet(parser, "fmIndex");
rmOptions.levenshtein = isSet(parser, "levenshtein");
getOptionValue(rmOptions.dropout, parser, "dropout");
getOptionValue(rmOptions.threads, parser, "threads");
rmOptions.buildIndexOnly = isSet(parser, "buildIndex");
rmOptions.matchExtend = !isSet(parser,"dropout");
getOptionValue(rmOptions.approxSearch, parser, "approximateSearch");
//------------ read & set ScoringScheme if specified
int ma, mm, go, ge;
getOptionValue(ma, parser, "match");
getOptionValue(mm, parser, "mismatch");
getOptionValue(go, parser, "gapOpen");
getOptionValue(ge, parser, "gapExtend");
if (isSet(parser, "match")) {
setScoreMatch(rmOptions.alignScheme, ma);
}
if (isSet(parser, "mismatch")) {
setScoreMismatch(rmOptions.alignScheme, mm);
}
if (isSet(parser, "gapOpen")) {
setScoreGapOpen(rmOptions.alignScheme, go);
}
if (isSet(parser, "gapExtend")) {
setScoreGapExtend(rmOptions.alignScheme, ge);
}
//------------ check dissent options
if (!isSet(parser, "buildIndex")) {
if (!isSet(parser, "readFile")) {
std::cerr << "ERROR: Please specify Read-File with option r!\n";
return seqan::ArgumentParser::PARSE_ERROR;
}
}
if (isSet(parser, "suffixArray") && isSet(parser, "fmIndex")) {
std::cerr << "ERROR: You cannot specify both suffixArray- and fmIndex-Modus!\n";
return seqan::ArgumentParser::PARSE_ERROR;
}
if (isSet(parser, "levenshtein") && isSet(parser, "hamming")) {
std::cerr << "ERROR: You cannot specify both levenshtein- and hamming-Distance!\n";
return seqan::ArgumentParser::PARSE_ERROR;
}
if (isSet(parser, "approximateSearch")) {
if (rmOptions.approxSearch < 0){
std::cout << "ERROR: Cannot search for seed with less then 0 errors accepted!\n";
return seqan::ArgumentParser::PARSE_ERROR;
}
if (rmOptions.approxSearch > 3) {
std::cout << "WARNING: Searching for seed with more than 3 errors accepted might take a long time!\n";
}
}
//------------ display warinings and return
if (isSet(parser, "buildIndex")) {
if (isSet(parser, "readFile") || isSet(parser, "levenshtein") || isSet(parser, "hamming")
|| isSet(parser, "gapExtend") || isSet(parser, "gapOpen") || isSet(parser, "mismatch")
|| isSet(parser, "match") || isSet(parser, "threads") || isSet(parser, "dropout")
|| isSet(parser, "seedLength") || isSet(parser, "index") || isSet(parser, "outSam")) {
std::cout <<
"WARNING: Options specified although program just creates Index-File! Input will be ignored!\n";
}
}
if (!isSet(parser, "buildIndex")) {
if (!isSet(parser, "outSam")) {
std::cout << "WARNING: Out-File not specified! Sam-File will be written in same directory as Input-File!\n";
rmOptions.outSam = rmOptions.inReads;
rmOptions.outSam += ".out";
}
}
return ArgumentParser::PARSE_OK;
};
/* Function to read a FastaFile in & building/saving Index-Structure of content in file.
* Returns true if file could be read and index could be built/wrote to disk, otherwise false
*
* Syntax:
* int erg = buildIndex(CharString fileIn, CharString fileOut, ReadMapperOptions rmOptions);
*
* Inputs:
* CharString fileIn - complete Path to fastA file
* CharString fileOut - complete Path to output index file
* ReadMapperOptions &rmOptions - read mapper program option object
*
* Outputs:
* int - true if Output-file written, false otherwise
*
* Example:
* erg = buildIndex(fileIn, fileOut);
*
* Other header files required: <seqan/index.h>, <seqan/basic.h>
* Subfunctions: none
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
static inline int buildIndex(std::string &fileIn, std::string &fileOut, ReadMapperOptions &rmOptions) {
//------------ Read in FastaReferenceFile & print Info
SeqFileIn sFileIn;
std::cout << "Reading Reference-File.." << std::endl;
if (!open(sFileIn, toCString(fileIn))) {
std::cerr << "Could not read Reference-File!" << std::endl;
return 0;
}
std::cout << "File read!" << std::endl;
// read Reference-Sequence
Dna5String genome;
CharString head;
try {
readRecord(head, genome, sFileIn);
} catch (IOError io) {
std::cerr << "ERROR: " << io.what() << std::endl;
} catch (ParseError p) {
std::cerr << "ERROR: " << p.what() << std::endl;
}
//------------ build Index & save to disk & print info
std::cout << "Building Index.." << std::endl;
// build Index
if (!rmOptions.fmInd) {
Index<Dna5String, IndexSa<> > index(genome);
// require Index for being able to save to disk
indexRequire(index, FibreSA());
std::cout << "Index built!" << std::endl;
// save Index to File!
std::cout << "Saving Index to disk.." << std::endl;
if (!save(index, toCString(fileOut))) {
std::cerr << "ERROR: not able to write Index-File to disk. Check for available space & authorization" <<
std::endl;
return 0;
}
std::cout << "Index saved!" << std::endl;
} else {
Index<Dna5String, FMIndex<> > index(genome);
// require Index for being able to save to disk
indexRequire(index, FibreSALF());
std::cout << "Index built!" << std::endl;
// save Index to File!
std::cout << "Saving Index to disk.." << std::endl;
if (!save(index, toCString(fileOut))) {
std::cerr << "ERROR: not able to write Index-File to disk. Check for available space & authorization" <<
std::endl;
return 0;
}
std::cout << "Index saved!" << std::endl;
}
return 1;
};
/* Function load a index-file from disk
*
* Syntax:
* Index<TText, IndexSa<> > loadIndex(ReadMapperOptions &rmOptions, SAind )
*
* Inputs:
* ReadMapperOptions rmOptions - read mapper program option object
* SAind - object of SAind struct
*
* Outputs:
* Index<TText, IndexSa<> > - SA-index
*
* Example:
* Index<TText, IndexSa<> > index = loadIndex(rmOptions, SAind());
*
* Other header files required:
* Subfunctions: none
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
template<typename TText>
static inline Index<TText, IndexSa<> > loadIndex(ReadMapperOptions &rmOptions, SAind /**/){
Index<TText, IndexSa<> > saIndex;
open(saIndex, toCString(rmOptions.inRef));
std::cout << "Index loaded!" << std::endl;
return saIndex;
};
/* Function load a index-file from disk
*
* Syntax:
* Index<TText, FMIndex<> > loadIndex(ReadMapperOptions &rmOptions, FMind )
*
* Inputs:
* ReadMapperOptions rmOptions - read mapper program option object
* FMind - object of FMind struct
*
* Outputs:
* Index<TText, FMIndex<> > - FM-index
*
* Example:
* Index<TText, FMIndex<> > index = loadIndex(rmOptions, FMind());
*
* Other header files required:
* Subfunctions: none
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
template<typename TText>
static inline Index<TText, FMIndex<> > loadIndex(ReadMapperOptions &rmOptions, FMind /**/){
Index<TText, FMIndex<> > saIndex;
open(saIndex, toCString(rmOptions.inRef));
std::cout << "Index loaded!" << std::endl;
return saIndex;
};
/* Function to get the CIGAR-Format out of an Alignment-Object.
* First Gaps and Last Gaps will be ignored. The Function takes
* only one Input arguments, the Alignment Object over two DNA-Strings.
* Output will be String< CigarElement<> > with the CIGAR-Format.
*
* Syntax:
* String< CigarElement<> > cig = getCigar(Align<Dna5String, ArrayGaps> align);
*
* Inputs:
* Align<Dna5String, ArrayGaps> align - alignment object of seqan-Library with 2 Sequences of Dna5String-Type
*
* Outputs:
* String< CigarElement<> > - string of CigarElement-objects representing CIGAR-format of Alignment
*
* Example:
* String< CigarElement<> > cig = getCigar(align)
*
* Other header files required: <seqan/seq_io.h>, <seqan/basic.h>
* Subfunctions: none
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
static inline String<CigarElement<> > getCigar(Align<Dna5String, ArrayGaps> &align) {
typedef Align<Dna5String, ArrayGaps> TAlign;
String<CigarElement<> > cigar;
// Use references to the rows of align.
typedef Row<TAlign>::Type TRow;
TRow &row1 = row(align, 0);
TRow &row2 = row(align, 1);
// Initialize the row iterators.
typedef Iterator<TRow>::Type TRowIterator;
TRowIterator itRow1 = begin(row1);
TRowIterator itEndRow1 = end(row1);
TRowIterator itRow2 = begin(row2);
TRowIterator itEndRow2 = end(row2);
unsigned int numChar = 0;
unsigned long numGaps;
// don't count beginning Gaps!
if (isGap(itRow1) || isGap(itRow2)) {
numGaps = countGaps(itRow1);
numGaps += countGaps(itRow2);
itRow1 += numGaps;
itRow2 += numGaps;
}
// don't count ending Gaps! -->
do {
--itEndRow1;
--itEndRow2;
} while (isGap(itEndRow1) || isGap(itEndRow2));
// count up last Character from while above which was not a gap
++itEndRow1;
++itEndRow2;
// get CIGAR String
while (itRow1 != itEndRow1) {
// Count insertions.
if (isGap(itRow1)) {
numGaps = countGaps(itRow1);
CigarElement<> c('I', (unsigned int) numGaps);
cigar += c;
itRow1 += numGaps;
itRow2 += numGaps;
continue;
}
// Count deletions.
if (isGap(itRow2)) {
numGaps = countGaps(itRow2);
CigarElement<> c('D', (unsigned int) numGaps);
cigar += c;
itRow1 += numGaps;
itRow2 += numGaps;
continue;
}
// Count matches.
while (*itRow1 == *itRow2 && itRow1 != itEndRow1) {
++numChar;
++itRow1;
++itRow2;
}
if (numChar != 0) {
CigarElement<> c('M', numChar);
cigar += c;
numChar = 0;
continue;
}
// Count mismatches.
while (*itRow1 != *itRow2 && itRow1 != itEndRow1) {
++numChar;
++itRow1;
++itRow2;
}
if (numChar != 0) {
CigarElement<> c('S', numChar);
cigar += c;
numChar = 0;
}
}
return cigar;
};
/* Function extends a given seed in both directions. Function returns a score of extension
*
* Syntax:
* unsigned long extSeed(TText &seed, Index<TText, TIndex> &index, TText &read,
* unsigned int &endPos,
* unsigned long startPosition, unsigned long endPosition,
* ReadMapperOptions &rmOptions, TExtendMode)
*
* Inputs:
* TText seed - TText Object of a seed, normally shorter than Reference, i.e. DNA5String
* Index<TText, TIndex> &index - index-object with Template Parameters to search seed in
* TText &read - read over which seed is defined of same Type of seed
* unsigned int &endPos - ending position of seed in read
* unsigned long startPosition - start Position of seed in ReferenceGenome
* unsigned long endPosition - end Position of seed in ReferenceGenome
* ReadMapperOptions &rmOptions - readMapperOptions object containing program parameters
* TExtendMode - template object to set seedExtension Option to runtime
*
* Outputs:
* long - Score of Extension
*
* Example:
* Dna5String seed = "ACGT";
* Dna5String index = "CCCCCCCAACGTTGGGGGGG"
* // normally this is a index-structure, but to show whats going on this is perfect
* Dna5String read = "AAAAAAAAACGTTTTTTT";
* unsigned int endPos = 12;
* unsigned long startPosition = 9;
* unsigned long endPosition = 13;
* unsigned long score = extSeed(seed, index, read, endPos, startPosition, endPosition, rmOptions, XDrop());
* // score would be 6
*
* Other header files required: <seqan/index.h>, <seqan/seeds.h>, <seqan/find.h>
*
* Subfunctions: none
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
template<typename TText, typename TIndex, typename TExtendMode>
inline static long extSeed(TText &seed, Index<TText, TIndex> &index, TText &read,
unsigned int &endPos,
unsigned long startPosition, unsigned long endPosition,
ReadMapperOptions &rmOptions, TExtendMode /**/) {
// necessary counters and numbers
Dna5String refGen;
unsigned long seedLength = length(seed);
// define seed
Seed<Simple> seed1(seedLength, endPos - seedLength, 2 * seedLength, endPos);
// referenceGenome in Match Region with seedLength characters additional to each side if possible
unsigned long beginRefGen = (startPosition >= seedLength) ? startPosition - seedLength : 0;
unsigned long endRefGen = (endPosition + seedLength < length(index)) ? endPosition + seedLength : length(index);
// get underlying Text of Index at position specified above
refGen = infix(indexText(index), beginRefGen, endRefGen);
// call of wrapper Function
extendWrapper(seed1, refGen, read, rmOptions, TExtendMode());
// return score
return (long) (endPositionH(seed1) - beginPositionH(seed1));
};
/* Function to process a set of given reads. This Function cuts each read in variable number of seeds (seed-length
* specified in rmOptions) and searches best position of each seed in reference genome. It returns a Vector containing
* the BamAligmentRecord of each Read with best hit to ReferenceGenome.
*
* Syntax:
* std::vector<BamAlignmentRecord> processReads(StringSet<Dna5String> &reads, ReadMapperOptions &rmOptions,
* Index<TText, TIndex > &index, TExtendMode, TDistance)
*
* Inputs:
* Dna5String &reads - Dna5String-Type of a seed which can be found in read and ReferenceGenome
* ReadMapperOptions &rmOptions - options for this Subroutine
* Index<TText, TIndex > &index - index-object as ReferenceGenome
* TExtendMode - template object to set seedExtension Option to runtime
* TDistance - template object to set distance Option to runtime
*
* Outputs:
* std::vector< std::vector<BamAlignmentRecord> > - vector containing subvectors containing all
* BamAlignmentRecords of all Reads. There are as many subvectors as threads are used.
*
* Example:
* bamRecords = processReads(reads, rmOptions, saIndex, XDrop(), Hamming())
* bamRecords = processReads(reads, rmOptions, saIndex, MatchExt(), Levenshtein())
*
* Other header files required: <seqan/index.h>, <seqan/seeds.h>, <seqan/find.h>
*
* Subfunctions: infix, findSeed, extSeed, getCigar
*
* See also:
* Author: Jan Philipp Albrecht
* Work address:
* email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
* Website: https://github.com/jpalbrecht/SWPraktikum
*/
template<typename TText, typename TIndex, typename TExtendMode, typename TDistance>
inline static std::vector<std::vector<BamAlignmentRecord> > processReads(StringSet<Dna5String> &reads,
ReadMapperOptions &rmOptions,
Index<TText, TIndex> &index, TExtendMode /**/,
TDistance /**/) {
// ------------ define objects, counters, etc.
Dna5String seed;
unsigned pos = 0;
unsigned long beginRefGen;
unsigned long endRefGen;
unsigned long lengthRead;
int score;
String<CigarElement<> > cig;
std::vector<std::vector<unsigned long> > positions;
std::vector<unsigned long> matched;
std::vector<std::vector<BamAlignmentRecord> > bamRecords;
// ------------ loop over Reads
// parallelisation
for (unsigned i = 1; i <= rmOptions.threads; ++i) {
std::vector<BamAlignmentRecord> bamRecord;
bamRecords.push_back(bamRecord);
}
omp_set_num_threads(rmOptions.threads);
typedef Iterator<StringSet<Dna5String> >::Type TIterator;
#pragma omp parallel for shared(bamRecords) private(seed) private(pos) private(lengthRead) private(beginRefGen) private(endRefGen) private(score) private(cig) private(positions) private(matched)
for (unsigned indRead = 0; indRead < length(reads); ++indRead) {
#ifdef verbose
std::cout << "processing Element: " << position(indRead, reads) << ".." << std::endl;
#endif
// defining bestSeed Vector for each read
std::vector<long> bestSeed(3, -1);
pos = 0;
// ------------ cut in small seeds
// Loop through seeds
typedef Iterator<Dna5String>::Type TIterator;
for (TIterator seedIt = begin(reads[indRead]); seedIt != end(reads[indRead]);) {
// if rest of read is bigger than 10 letters
if ((seedIt + rmOptions.seedLen) <= end(reads[indRead])) {
seed = infix(*seedIt, seedIt, seedIt + rmOptions.seedLen);
seedIt += rmOptions.seedLen;
pos += rmOptions.seedLen;
#ifdef verbose
std::cout << "Searching for Seed: " << seed << std::endl;
#endif
// Discard this part ?... could just be one letter... not good to search in Genome!
} else { // if rest is not bigger than 10 letters
break;
}
// ------------ find & extend & get best seed
// Pattern
StringSet<Dna5String> pat;
appendValue(pat, seed);
// search
bestSeed = searchWrapper(seed, pat, index, reads[indRead], pos, rmOptions, bestSeed, TExtendMode(),
TDistance());
} // next seed of read
// catch nothing found
if (bestSeed.at(0) != -1) {
// ------------ perform semi-global Alignment for each Read at position of best seed
lengthRead = length(reads[indRead]);
// referenceGenome in Match Region
beginRefGen = (bestSeed.at(0) >= lengthRead) ? bestSeed.at(0) - lengthRead : 0;
endRefGen = (bestSeed.at(1) + lengthRead < length(index)) ? bestSeed.at(1) + lengthRead : length(index);
typedef Align<TText, ArrayGaps> TAlign;
TAlign align;
resize(rows(align), 2);
assignSource(row(align, 0), infix(indexText(index), beginRefGen, endRefGen));
assignSource(row(align, 1), reads[indRead]);
// do alignment & get score
score = globalAlignment(align, rmOptions.alignScheme);
//score = globalAlignment(align, MyersHirschberg());
#ifdef verbose
std::cout << align << std::endl;
std::cout << "Score: " << score << std::endl;
std::cout << "" << std::endl;
#endif
// ------------ determine CIGAR Format
cig = getCigar(align);
// ------------ Create AlignmentRecord for BAM-File & write in File
BamAlignmentRecord bamAlignmentRecord;
// set recordFlags for read
char name[10];
sprintf(name, "%s%ld", "Read_", (long) indRead);
bamAlignmentRecord.qName = name; // Record Name
bamAlignmentRecord.flag = (uint32_t) indRead; // Record Number
bamAlignmentRecord.beginPos = (int32_t) bestSeed.at(0); // Position in RefGen
bamAlignmentRecord.cigar = cig; // Alignment CIGAR
bamAlignmentRecord.seq = reads[indRead]; // Read sequence
bamRecords.at((unsigned long)omp_get_thread_num()).push_back(bamAlignmentRecord);
}
}// next read
return bamRecords;
}
inline static void processMap(ReadMapperOptions &rmOptions, SAind /**/){
//------------ Read Index & Reads
std::cout << "Loading Index.." << std::endl;
// load Index from disk
Index<Dna5String, IndexSa<> > saIndex;
open(saIndex, toCString(rmOptions.inRef));
std::cout << "Index loaded!" << std::endl;
//------------ read Reads-Records
std::cout << "Reading Reads.." << std::endl;
SeqFileIn rFileIn;
if (!open(rFileIn, toCString(rmOptions.inReads))) {
std::cerr << "ERROR: Could not read Reads-File!" << std::endl;
}
StringSet<Dna5String> reads;
StringSet<CharString> heads;
try {
// try to read all records
readRecords(heads, reads, rFileIn);
} catch (IOError io) {
std::cerr << "ERROR: " << io.what() << std::endl;
} catch (ParseError p) {
std::cerr << "ERROR: " << p.what() << std::endl;
}
std::cout << "Reads read!" << std::endl;
//------------ Processing reads
std::cout << "Processing.." << std::endl;
std::vector<std::vector<BamAlignmentRecord> > bamRecords;
if (!rmOptions.levenshtein) {
if(!rmOptions.matchExtend){