-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReadFileReader.h
1012 lines (896 loc) · 30.9 KB
/
ReadFileReader.h
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
//
// Kmernator/src/ReadFileReader.h
//
// Author: Rob Egan
//
/*****************
Kmernator Copyright (c) 2012, The Regents of the University of California,
through Lawrence Berkeley National Laboratory (subject to receipt of any
required approvals from the U.S. Dept. of Energy). All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
(1) Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
(2) Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
(3) Neither the name of the University of California, Lawrence Berkeley
National Laboratory, U.S. Dept. of Energy nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You are under no obligation whatsoever to provide any bug fixes, patches, or
upgrades to the features, functionality or performance of the source code
("Enhancements") to anyone; however, if you choose to make your Enhancements
available either publicly, or directly to Lawrence Berkeley National
Laboratory, without imposing a separate written license agreement for such
Enhancements, then you hereby grant the following license: a non-exclusive,
royalty-free perpetual license to install, use, modify, prepare derivative
works, incorporate into other computer software, distribute, and sublicense
such enhancements or derivative works thereof, in binary and source code form.
*****************/
// added support for new Illumina 1.8 fasta/q file header & comment format
// @EAS139:136:FC706VJ:2:2104:15343:197393 1:Y:18:ATCACG
// 1: member of a pair
// Y: read fails filter
// control bits (0 is no control, even is something else)
// ATCACG barcode
// virtually translate to
// @EAS139:136:FC706VJ:2:2104:15343:197393/1 Y:18:ATCACG
// (and set the read discard flag if filtered 'Y')
#ifndef _READ_FILE_READER_H
#define _READ_FILE_READER_H
#include <cstring>
#include <cstdlib>
#include <boost/shared_ptr.hpp>
#include <boost/unordered_map.hpp>
#include "config.h"
#include "Log.h"
#include "Utils.h"
using namespace std;
class ReadFileReader {
public:
typedef Kmernator::RecordPtr RecordPtr;
typedef Kmernator::MmapSource MmapSource;
typedef Kmernator::MmapIStream MmapIStream;
typedef Kmernator::FilteredIStream FilteredIStream;
class SequenceStreamParser;
typedef boost::shared_ptr< SequenceStreamParser > SequenceStreamParserPtr;
private:
SequenceStreamParserPtr _parser;
string _path;
ifstream _ifs;
ifstream _qs;
istringstream _iss;
istream *_is;
int _streamType; // 0 - file , 1 - string, 2 - mmap, 3 - generic input stream
public:
ReadFileReader(): _parser(), _streamType(1) {}
ReadFileReader(string fastaFilePath, bool autoFindQual) :
_parser(), _path(fastaFilePath), _streamType(0) {
openFastaFile(fastaFilePath);
if (autoFindQual) {
std::string qualFilePath;
openQualFile(qualFilePath);
} else {
_qs.close();
}
setParser(_ifs, _qs);
}
ReadFileReader(string fastaFilePath, string qualFilePath, bool autoFindQual = GeneralOptions::getOptions().getIgnoreQual()) :
_parser(), _path(fastaFilePath), _streamType(0) {
openFastaFile(fastaFilePath);
if (!qualFilePath.empty() || autoFindQual) {
openQualFile(qualFilePath);
} else {
qualFilePath.clear();
_qs.close();
}
setParser(_ifs, _qs);
}
ReadFileReader(string &fasta) :
_iss(fasta), _streamType(1) {
LOG_DEBUG(2, "ReadFileReader(" << fasta.length() << "): Constructor on a string-based fasta file");
_parser = SequenceStreamParserPtr(new FastaStreamParser(_iss));
}
ReadFileReader(MmapSource &mmap) : _streamType(2) {
setReader(mmap);
}
ReadFileReader(MmapSource &mmap1, MmapSource &mmap2) : _streamType(2) {
setReader(mmap1,mmap2);
}
ReadFileReader(istream &inputStream) : _streamType(3) {
_is = &inputStream;
char c = _is->peek();
if (_is->eof())
return;
setParser(*_is, c);
}
~ReadFileReader() {
switch (_streamType) {
case(0) : _ifs.close(); _qs.close(); break;
case(1) : break;
case(2) : break;
case(3) : break;
}
}
void openFastaFile(string fastaFilePath) {
_ifs.open(_path.c_str());
if (_ifs.fail())
LOG_THROW("ReadFileReader::openFastaFile(): Could not open : " << _path);
}
void openQualFile(string qualFilePath) {
if (!qualFilePath.empty()) {
_qs.open(qualFilePath.c_str());
if (_qs.fail())
LOG_THROW("ReadFileReader::openQualFile(): Could not open : " << qualFilePath);
} else {
_qs.open((_path + ".qual").c_str());
LOG_DEBUG(3, "ReadFileReader() opened " << _path << ".qual " << _qs.good());
}
LOG_DEBUG(2, "ReadFileReader(" << _path << ", " << qualFilePath << ")");
}
std::string getFilePath() {
return _path;
}
void setReader(MmapSource &mmap) {
assert(mmap.is_open());
_streamType = 2;
LOG_DEBUG(3, "setReader(mmap):" << (void*)mmap.data());
setParser(mmap, *mmap.data());
}
void setReader(MmapSource &mmap1, MmapSource &mmap2) {
assert(mmap1.is_open());
assert(mmap2.is_open());
_streamType = 2;
LOG_DEBUG(3, "setReader(mmap, mmap):" << (void*)mmap1.data() << " " << (void*)mmap2.data());
setParser(mmap1,mmap2);
}
void setParser(istream &fs1) {
assert(_streamType == 0);
if (fs1.fail() || !fs1.good())
LOG_THROW("ReadFileRader::setParser(): istream fail() or !good()");
char c = fs1.peek();
if (fs1.eof())
return;
setParser(fs1, c);
}
template<typename U> void setParser(U &data, char marker) {
LOG_DEBUG(3, "setParser(U)");
if (marker == '@')
_parser = SequenceStreamParserPtr(new FastqStreamParser(data));
else if (marker == '>')
_parser = SequenceStreamParserPtr(new FastaStreamParser(data));
else
LOG_THROW("ReadFileReader::setParser(): Unknown file format: marker == " << marker);
}
SequenceStreamParserPtr getParser() {
return _parser;
}
void setParser(istream &fs1, istream &fs2) {
assert(_streamType == 0);
if (fs2.fail() || fs2.eof() || !fs2.good() ) {
setParser(fs1);
} else {
LOG_DEBUG(3, "setParser(istream,istream) FastaQualStreamParser");
_parser = SequenceStreamParserPtr(new FastaQualStreamParser(fs1, fs2));
}
}
void setParser(MmapSource &mmap1, MmapSource &mmap2) {
assert(_streamType == 2);
_parser = SequenceStreamParserPtr(new FastaQualStreamParser(mmap1, mmap2));
LOG_DEBUG(3, "setParser(mmap,mmap) FastaQualStreamParser");
}
bool isMmaped() {
return _parser->isMmaped();
}
RecordPtr getStreamRecordPtr() const {
return _parser->getStreamRecordPtr();
}
RecordPtr getStreamQualRecordPtr() const {
return _parser->getStreamQualRecordPtr();
}
MmapSource getMmap() const {
return _parser->getMmap();
}
MmapSource getQualMmap() const {
return _parser->getQualMmap();
}
const string &getLastName() const {
return _parser->getName();
}
const string &getLastComment() const {
return _parser->getComment();
}
const string &getLastBases() const {
return _parser->getBases();
}
const string &getLastQuals() const {
return _parser->getQuals();
}
bool getLastMultiline() const {
return _parser->isMultiline();
}
bool nextRead(RecordPtr &recordStart) {
LOG_DEBUG(5, "nextRead(recordStart = " << (void*) recordStart << ")");
try {
assert(recordStart == _parser->getStreamRecordPtr());
RecordPtr endPtr = _parser->readRecord();
while (_parser->isDiscardFiltered() && endPtr != NULL && (!_parser->getName().empty()) && (!_parser->isGood()) ) {
LOG_DEBUG_OPTIONAL(2, true, "nextRead(): Skipping failed-filter read: " << _parser->getName());
endPtr = _parser->readRecord();
}
if (endPtr == NULL || _parser->getName().empty()) {
_parser->resetBuffers();
recordStart = NULL;
return false;
} else {
recordStart = endPtr;
return true;
}
} catch (runtime_error &e) {
stringstream error;
error << e.what() << " in file '" << _path << "' at line "
<< _parser->lineNumber() << " position:" << _parser->tellg() << " " \
<< _parser->getName() << _parser->getBases() << _parser->getQuals() \
<< " '" << _parser->getLineBuffer() << _parser->nextLine() << "'";
LOG_THROW("ReadFileReader::nextRead(RecordPtr): " << error.str());
}
}
bool nextRead(RecordPtr &recordStart, string &name, string &bases, string &quals, string &comment, bool &isMultiline) {
bool passed = nextRead(recordStart);
if (passed) {
name = getLastName();
comment = getLastComment();
bases = getLastBases();
quals = getLastQuals();
isMultiline = getLastMultiline();
}
return passed;
}
bool nextRead(string &name, string &bases, string &quals, string &comment) {
try {
_parser->readRecord();
name = _parser->getName();
while (_parser->isDiscardFiltered() && (!name.empty()) && (!_parser->isGood())) {
LOG_DEBUG_OPTIONAL(2, true, "nextRead(,,): Skipping failed-filter read: " << _parser->getName());
_parser->readRecord();
name = _parser->getName();
}
if (name.empty())
return false;
comment = _parser->getComment();
bases = _parser->getBases();
std::transform(bases.begin(), bases.end(), bases.begin(), ::toupper);
quals = _parser->getQuals();
if (quals.length() != bases.length())
if (!(quals.length() == 1 && quals[0] == Read::REF_QUAL))
LOG_THROW("ReadFileReader::nextRead(name,base,qual): Number of bases and quals not equal: " << bases << " " << quals);
LOG_DEBUG(5, "nextRead(name, bases, quals) " << name);
return true;
}
catch (runtime_error &e) {
stringstream error;
error << e.what() << " in file '" << _path << "' at line "
<< _parser->lineNumber() << " position:" << _parser->tellg() << " " \
<< _parser->getName() << _parser->getBases() << _parser->getQuals() \
<< " '" << _parser->getLineBuffer() << _parser->nextLine() << "'";
LOG_THROW("ReadFileReader::nextRead(name,base,qual): " << error.str());
}
}
unsigned long getFileSize() {
if (_parser->isMmaped()) {
return _parser->getMmapFileSize();
} else {
long size = 0;
switch(_streamType) {
case(0) : size = FileUtils::getFileSize(_ifs); break;
case(1) : size = FileUtils::getFileSize(_iss); break;
case(2) : size = 0; break;
case(3) : size = 0; break;
}
return size;
}
}
unsigned long getBlockSize(unsigned int numThreads) {
unsigned long fileSize = getFileSize();
unsigned long blockSize = fileSize / numThreads;
LOG_DEBUG(3, "ReadFileReader(" << _path << ")::getBlockSize(" << numThreads <<"): " << blockSize << " out of " << fileSize);
return blockSize;
}
void setPos(unsigned long pos) {
_parser->setPos(pos);
}
inline unsigned long getPos() {
return _parser->getPos();
}
inline unsigned long getLastPos() {
return _parser->getLastPos();
}
void setLastPos(unsigned long pos) {
_parser->setLastPos(pos);
}
bool seekToNextRecord(unsigned long minimumPos) {
return seekToNextRecord(minimumPos, true);
}
bool seekToNextRecord(unsigned long minimumPos, bool byPair) {
return _parser->seekToNextRecord(minimumPos, byPair);
}
int getType() const {
return _parser->getType();
}
void seekToPartition(int rank, int size) {
unsigned long lastPos = getFileSize();
unsigned long firstPos = 0;
LOG_DEBUG(2, "ReadFileReader(" << _path <<")::seekToPartition(" << rank << ", " << size << ")");
setLastPos(lastPos);
if (size > 1) {
unsigned long blockSize = getBlockSize(size);
if (rank + 1 != size ) {
seekToNextRecord( blockSize * (rank+1) );
lastPos = getPos();
}
if (seekToNextRecord( blockSize * rank )) {
firstPos = getPos();
} else {
firstPos = lastPos;
}
}
setLastPos(lastPos);
LOG_DEBUG(2, "ReadFileReader(" << _path <<")::seekToPartition(" << rank << ", " << size << ") " << firstPos << ", reading until " << lastPos);
}
bool eof() const {
return _parser->endOfStream();
}
public:
class SequenceStreamParser {
private:
istream * _stream;
unsigned long _line;
mutable unsigned long _pos;
unsigned long _lastPos;
bool _discardFiltered;
protected:
char _marker;
ReadFileReader::MmapSource _mmap;
ReadFileReader::RecordPtr _lastPtr;
bool _freeStream;
mutable std::vector<string> _nameBuffer;
mutable std::vector<string> _commentBuffer;
mutable std::vector<string> _basesBuffer;
mutable std::vector<string> _qualsBuffer;
mutable std::vector<string> _lineBuffer;
mutable std::vector<bool> _isMultiline;
mutable std::vector<bool> _isGood;
public:
// returns a termination pointer (the start of next record or lastByte+1 at end)
virtual RecordPtr readRecord(RecordPtr recordPtr) const = 0;
virtual RecordPtr readRecord() = 0;
RecordPtr readRecord(RecordPtr recordPtr, string &name, string &bases, string &quals, string &comment) const {
RecordPtr end = readRecord(recordPtr);
if (end != NULL) {
name = getName();
comment = getComment();
bases = getBases();
quals = getQuals();
} else {
LOG_DEBUG(5, "SequenceStreamParser()::readRecord(" << recordPtr <<",,,) found the end");
resetBuffers();
return NULL;
}
return end;
}
static inline string &nextLine(string &buffer, RecordPtr &recordPtr) {
return SequenceRecordParser::nextLine(buffer, recordPtr);
}
inline string &nextLine(string &buffer) {
_line++;
buffer.clear();
if (isPastPartition())
return buffer;
getline(*_stream, buffer);
_pos += buffer.length() + 1;
LOG_DEBUG(6, "SequenceStreamParser::nextLine(" << buffer << ") position at: " << _pos);
return buffer;
}
inline string &nextLine() {
int threadNum = omp_get_thread_num();
nextLine(_lineBuffer[threadNum]);
return _lineBuffer[threadNum];
}
SequenceStreamParser(istream &stream, char marker) :
_stream(&stream), _line(0), _pos(0), _lastPos(-1), _discardFiltered(true), _marker(marker), _mmap(), _lastPtr(NULL), _freeStream(false) {
if (!_stream->good() || _stream->fail())
LOG_THROW("SequenceStreamParser(" << _stream << ", " << marker << ") has an invalid stream!");
LOG_DEBUG(4, "SequenceStreamParser(istream, " << marker << ")");
setBuffers();
}
SequenceStreamParser(ReadFileReader::MmapSource &mmap, char marker) :
_stream(NULL), _line(0), _pos(0), _lastPos(-1), _discardFiltered(true), _marker(marker), _mmap( mmap ), _lastPtr(NULL), _freeStream(false) {
if (!_mmap.is_open() || !_mmap.size()>0 || _mmap.data() == NULL)
LOG_THROW("SequenceStreamParser(" << _mmap << ", " << marker << ") has an invalid memory map!");
_stream = new MmapIStream(_mmap);
_lastPtr = _mmap.data() + _mmap.size();
_freeStream = true;
LOG_DEBUG(4, "SequenceStreamParser(MmapSource, " << marker << ")");
setBuffers();
}
SequenceStreamParser(ReadFileReader::FilteredIStream &stream, char marker) :
_line(0), _pos(0), _lastPos(-1), _discardFiltered(true), _marker(marker), _mmap(), _lastPtr(NULL), _freeStream(false) {
_stream = (istream *) &stream;
if (!_stream->good() || _stream->fail())
LOG_THROW("SequenceStreamParser(FilteredIStream " << _stream << ", " << marker << ") has an invalid stream!");
LOG_DEBUG(4, "SequenceStreamParser(FilteredIStream, " << marker << ")");
setBuffers();
}
void setBuffers() {
int numThreads = std::max(omp_get_max_threads(), omp_get_num_threads());
_nameBuffer.assign(numThreads, string());
_commentBuffer.assign(numThreads, string());
_basesBuffer.assign(numThreads, string());
_qualsBuffer.assign(numThreads, string());
_lineBuffer.assign(numThreads, string());
_isMultiline.assign(numThreads, false);
_isGood.assign(numThreads, false);
}
void resetBuffers() const {
int threadNum = omp_get_thread_num();
_nameBuffer[threadNum].clear();
_commentBuffer[threadNum].clear();
_basesBuffer[threadNum].clear();
_qualsBuffer[threadNum].clear();
_lineBuffer[threadNum].clear();
_isMultiline[threadNum] = false;
_isGood[threadNum] = false;
}
virtual ~SequenceStreamParser() {
// TODO fix this -- open a new mmap that can be closed!
// Do not free/close istream as that closes the mmap too!!!
//if (_freeStream)
// delete _stream;
}
void setLastPos(unsigned long lastPos) {
_lastPos = lastPos;
}
unsigned long lineNumber() {
return _line;
}
unsigned long tellg() {
return _pos = _stream->tellg();
}
void reset() {
_stream->clear(_stream->rdstate() & ( ~ios_base::eofbit ) );
_stream->clear(_stream->rdstate() & ( ~ios_base::failbit ) );
}
void seekg(unsigned long pos) {
_pos = pos;
reset(); // reset a potential eof...
_stream->seekg(_pos);
}
bool endOfStream() {
return _stream->eof();
}
int peek() {
return _stream->peek();
}
string &getLineBuffer() const {
int threadNum = omp_get_thread_num();
return _lineBuffer[threadNum];
}
string &getName() const {
int threadNum = omp_get_thread_num();
return _nameBuffer[threadNum];
}
string &getComment() const {
int threadNum = omp_get_thread_num();
return _commentBuffer[threadNum];
}
string &getBases() const {
int threadNum = omp_get_thread_num();
return _basesBuffer[threadNum];
}
string &getQuals() const {
int threadNum = omp_get_thread_num();
return _qualsBuffer[threadNum];
}
bool isMultiline() const {
int threadNum = omp_get_thread_num();
return _isMultiline[threadNum];
}
bool isGood() const {
int threadNum = omp_get_thread_num();
return _isGood[threadNum];
}
void setIsGood(bool isGood) const {
int threadNum = omp_get_thread_num();
_isGood[threadNum] = isGood;
}
inline bool &isDiscardFiltered() {
return _discardFiltered;
}
virtual string &readName() {
std::string &name = getName();
std::string &comment = getComment();
nextLine( name );
comment.clear();
int count = 0;
while (name.length() == 0 || name[0] != _marker) // skip empty lines at end of stream
{
LOG_DEBUG(5, "SequenceStreamParser::readName() found nothing on the last line: " << name << " looking for '" << _marker << "' at " << getPos());
if (isPastPartition() || endOfStream()) {
name.clear();
setIsGood(false);
return name;
}
nextLine( name );
if (++count > 100000)
break;
}
LOG_DEBUG(5, "SequenceStreamParser::readName() found " << name << " at " << getPos());
if (name.length() == 0) {
setIsGood(false);
return name;
}
if (name.length() != 0 && name[0] != _marker)
LOG_THROW("Missing name marker '" << _marker << "'" << " in '" << name << "' at " << getPos() << " after " << count << " attempts to find the marker");
// remove marker and any extra comments or fields
bool isGood = SequenceRecordParser::trimName( name, comment );
setIsGood(isGood);
return name;
}
virtual int getType() const = 0;
unsigned long getPos() const {
return _pos;
}
void setPos(unsigned long newPos) const {
_pos = newPos;
}
unsigned long getLastPos() const {
return _lastPos;
}
bool isPastPartition() const {
return _pos >= _lastPos;
}
bool isMmaped() const {
return _mmap.is_open();
}
unsigned long getMmapFileSize() const {
return _mmap.size();
}
RecordPtr getStreamRecordPtr() const {
if (_mmap.is_open()) {
return _mmap.data() + getPos();
} else {
return NULL;
}
}
MmapSource getMmap() const {
return _mmap;
}
virtual MmapSource getQualMmap() const = 0;
RecordPtr getLastRecordPtr() const {
return _lastPtr;
}
virtual RecordPtr getStreamQualRecordPtr() const = 0;
virtual RecordPtr getLastQualRecordPtr() const = 0;
virtual bool seekToNextRecord(unsigned long minimumPos, bool byPair) {
int threadNum = omp_get_thread_num();
LOG_DEBUG(2, "seekToNextRecord(" << minimumPos << ", " << byPair << ")");
// get to the first line after the pos
if (minimumPos > 0) {
seekg(minimumPos - 1);
if (endOfStream())
return false;
if (peek() == '\n') {
seekg(minimumPos);
} else
nextLine();
} else {
seekg(0);
assert(!endOfStream());
return true;
}
LOG_DEBUG(2, "seeked to " << tellg() );
if (endOfStream()) {
LOG_DEBUG(3, "At endofstream already1");
return false;
}
while ( (!isPastPartition()) && (!endOfStream()) && _stream->peek() != _marker) {
std::string line = nextLine();
LOG_DEBUG(4, "reading line (" << tellg() << "): " << line);
}
if (isPastPartition()) {
LOG_WARN(1, "seekToNextRecord(" << minimumPos << "): is past partition: " << tellg());
return false;
}
if (_marker == '@' && (!endOfStream())) {
// since '@' is a valid quality character in a FASTQ (sometimes)
// verify that the next line is not also starting with '@', as that would be the true start of the record
unsigned long tmpPos = tellg();
string current = _lineBuffer[threadNum];
string tmp = nextLine();
if (endOfStream() || _stream->peek() != _marker) {
seekg(tmpPos);
_lineBuffer[threadNum] = current;
LOG_DEBUG(3, "Correctly picked record break point to read fastq in parallel: "
<< current << "\n" << tmp << " " << tellg());
} else {
LOG_DEBUG(3, "Needed to skip an extra line to read fastq in parallel: "
<< current << "\n" << tmp << " " << tellg());
}
}
if (endOfStream()) {
LOG_DEBUG(3, "At endofstream already2");
return false;
}
if (byPair) {
// now verify that we have not split a sequential pair
unsigned long here1 = tellg();
string name1;
readRecord();
if ( (name1 = getName()).empty() || endOfStream()) {
LOG_DEBUG(3, "Found endofstream 0 records in leaving at eof to preserve pair");
return false;
} else {
LOG_DEBUG(3, "Checking pairing against " << name1);
}
string comment1 = getComment();
unsigned long here2 = tellg();
string name2;
readRecord();
if ( (name2 = getName()).empty() || endOfStream()) {
LOG_DEBUG(3, "Found endofstream 1 record in leaving at eof to preserve pair " << name1);
return false;
}
string comment2 = getComment();
string name3;
readRecord();
if ( (name3 = getName()).empty() || endOfStream()) {
seekg(here1);
LOG_DEBUG(3, "Found endofstream two records in, rewinding to initial boundary " << here1 << " : " << name1 << " & " << name2);
return true;
}
string comment3 = getComment();
if (SequenceRecordParser::isPair(name1, name2, comment1, comment2)) {
seekg(here1);
LOG_DEBUG(3, "Found natural pair at boundary, rewinding to " << here1);
} else if (SequenceRecordParser::isPair(name2, name3, comment2, comment3)) {
seekg(here2);
LOG_DEBUG(3, "Found split pair at boundary, incrementing one record to " << here2);
} else {
LOG_DEBUG(3, "Found no pairs at boundary, rewinding to " << here1);
seekg(here1);
}
return true;
} else {
return true;
}
}
};
class FastqStreamParser: public SequenceStreamParser {
public:
FastqStreamParser(istream &s) :
SequenceStreamParser(s, '@') {
}
FastqStreamParser(ReadFileReader::MmapSource &mmap) :
SequenceStreamParser(mmap, '@') {
}
RecordPtr readRecord() {
int threadNum = omp_get_thread_num();
if (readName().empty()) {
resetBuffers();
LOG_DEBUG(5, "FastqStreamParser::readRecord() found the end");
return NULL;
} else {
readBases();
readQuals();
LOG_DEBUG(5, "FastqStreamParser::readRecord() found " << _nameBuffer[threadNum]);
}
return getStreamRecordPtr();
}
RecordPtr readRecord(RecordPtr _recordPtr) const {
int threadNum = omp_get_thread_num();
RecordPtr recordPtr = _recordPtr;
if (isPastPartition()) {
resetBuffers();
return NULL;
}
if (*(recordPtr++) != _marker) {
// skip the first character
LOG_THROW("Detected invalid line marker: at " << getPos() << ": " << *recordPtr);
}
std::string &name = _nameBuffer[threadNum];
std::string &comment = _commentBuffer[threadNum];
nextLine(name, recordPtr);
bool isGood = SequenceRecordParser::trimName( name, comment ); // name
setIsGood(isGood);
nextLine(_basesBuffer[threadNum], recordPtr); // fasta
nextLine(_lineBuffer[threadNum], recordPtr); // qual name
nextLine(_qualsBuffer[threadNum], recordPtr); // quals
setPos( getPos() + (recordPtr - _recordPtr));
return recordPtr;
}
string &readBases() {
int threadNum = omp_get_thread_num();
nextLine(_basesBuffer[threadNum]);
if (_basesBuffer[threadNum].empty() || _basesBuffer[threadNum].length()
== _basesBuffer[threadNum].max_size())
LOG_THROW("FastqStreamParser::readBases(): Missing or too many bases");
string &qualName = nextLine();
if (qualName.empty() || qualName[0] != '+')
LOG_THROW("FastqStreamParser::readBases(): Missing '+' in fastq, got: " << _basesBuffer[threadNum] << " " << qualName);
_isMultiline[threadNum] = false;
return _basesBuffer[threadNum];
}
string &readQuals() {
int threadNum = omp_get_thread_num();
nextLine(_qualsBuffer[threadNum]);
if (Options::getOptions().getIgnoreQual()) {
_qualsBuffer[threadNum].assign(_qualsBuffer[threadNum].length(), Read::REF_QUAL);
}
return _qualsBuffer[threadNum];
}
int getType() const {
return 0;
}
RecordPtr getStreamQualRecordPtr() const { return NULL; }
RecordPtr getLastQualRecordPtr() const { return NULL; }
MmapSource getQualMmap() const { return MmapSource(); }
};
class FastaStreamParser: public SequenceStreamParser {
static const char fasta_marker = '>';
public:
FastaStreamParser(istream &s) :
SequenceStreamParser(s, fasta_marker) {
}
FastaStreamParser(ReadFileReader::MmapSource &mmap) :
SequenceStreamParser(mmap, fasta_marker) {
}
RecordPtr readRecord() {
int threadNum = omp_get_thread_num();
if (readName().empty()) {
resetBuffers();
LOG_DEBUG(5, "FastaStreamParser::readRecord() found the end at " << tellg());
return NULL;
} else {
readBases();
LOG_DEBUG(5, "FastaStreamParser::readRecord() found " << _nameBuffer[threadNum]);
}
return getStreamRecordPtr();
}
RecordPtr readRecord(RecordPtr _recordPtr) const {
int threadNum = omp_get_thread_num();
if (isPastPartition()) {
resetBuffers();
return NULL;
}
RecordPtr recordPtr = _recordPtr;
if (*recordPtr != _marker) {
LOG_THROW("FastaStreamParser::readRecord(): Could not FastaStreamParser::readRecord()");
}
std::string &name = _nameBuffer[threadNum];
std::string &comment = _commentBuffer[threadNum];
nextLine(name, recordPtr); // name
bool isGood = SequenceRecordParser::trimName(name, comment);
setIsGood(isGood);
_basesBuffer[threadNum].clear();
_isMultiline[threadNum] = false;
long count = 0;
while (recordPtr < _lastPtr && *recordPtr != fasta_marker) {
_basesBuffer[threadNum] += nextLine(_lineBuffer[threadNum], recordPtr);
if (++count > 1)
_isMultiline[threadNum] = true;
}
_qualsBuffer[threadNum].assign(_basesBuffer[threadNum].length(), Read::REF_QUAL);
setPos(getPos() + (recordPtr - _recordPtr));
return recordPtr;
}
string &readBases() {
int threadNum = omp_get_thread_num();
string &bases = getBasesOrQuals();
_qualsBuffer[threadNum].assign(bases.length(), Read::REF_QUAL);
return bases;
}
virtual string &getBasesOrQuals() {
int threadNum = omp_get_thread_num();
_basesBuffer[threadNum].clear();
_isMultiline[threadNum] = false;
long count = 0;
while (!endOfStream()) {
nextLine();
if (_lineBuffer[threadNum].empty())
break;
_basesBuffer[threadNum] += _lineBuffer[threadNum];
if (++count > 1)
_isMultiline[threadNum] = true;
if (_basesBuffer[threadNum].size() == _basesBuffer[threadNum].max_size())
LOG_THROW("FastaStreamParser::getBasesOrQuals(): Sequence/Qual too large to read");
if (peek() == fasta_marker)
break;
}
return _basesBuffer[threadNum];
}
int getType() const {
return 1;
}
RecordPtr getStreamQualRecordPtr() const { return NULL; }
RecordPtr getLastQualRecordPtr() const { return NULL; }
MmapSource getQualMmap() const { return MmapSource(); }
};
class FastaQualStreamParser: public FastaStreamParser {
FastaStreamParser _qualParser; // odd, but it works
boost::unordered_map<RecordPtr, RecordPtr> translate;
public:
FastaQualStreamParser(istream &fastaStream, istream &qualStream) :
FastaStreamParser(fastaStream), _qualParser(qualStream) {
}
FastaQualStreamParser(ReadFileReader::MmapSource &mmap, ReadFileReader::MmapSource &qualMmap) :
FastaStreamParser(mmap), _qualParser(qualMmap) {
}
RecordPtr readRecord() {
int threadNum = omp_get_thread_num();
RecordPtr fastaPtr = getStreamRecordPtr();
RecordPtr qualPtr = _qualParser.getStreamRecordPtr();
translate[fastaPtr] = qualPtr;
if (readName().empty()) {
resetBuffers();
LOG_DEBUG(5, "FastaQualStreamParser::readRecord() found the end");
return NULL;
} else {
readBases();
readQuals();
_isMultiline[threadNum] = _isMultiline[threadNum] || _qualParser.isMultiline();
LOG_DEBUG(5, "FastaQualStreamParser::readRecord() found " << _nameBuffer[threadNum]);
}
return getStreamRecordPtr();
}
RecordPtr readRecord(RecordPtr _recordPtr) const {
int threadNum = omp_get_thread_num();
if (isPastPartition()) {
resetBuffers();
return NULL;
}
RecordPtr recordPtr = _recordPtr;
RecordPtr qualPtr = translate.find(recordPtr)->second;
FastaStreamParser::readRecord(recordPtr);
_qualParser.readRecord( qualPtr );
_qualsBuffer[threadNum] = _qualParser.getQuals();
_isMultiline[threadNum] = _isMultiline[threadNum] || _qualParser.isMultiline();
setPos(getPos() + (recordPtr - _recordPtr));
return recordPtr;
}
string &readName() {
string &qualName = _qualParser.readName();
string &name = FastaStreamParser::readName();
if (name != qualName)
LOG_THROW("FastaQualStreamParser::readName(): fasta and quals have different names");
return name;
}
string &readBases() {
return getBasesOrQuals();
}
string &readQuals() {
int threadNum = omp_get_thread_num();
// odd, but it works
string &qualInts = _qualParser.getBasesOrQuals();
_qualsBuffer[threadNum] = SequenceRecordParser::convertQualIntsToChars(qualInts, Read::FASTQ_START_CHAR);
return _qualsBuffer[threadNum];
}
int getType() const {