forked from BenLangmead/bowtie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pat.h
1373 lines (1203 loc) · 34.7 KB
/
pat.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
#ifndef PAT_H_
#define PAT_H_
#include <cassert>
#include <cmath>
#include <zlib.h>
#include <sys/stat.h>
#include <stdexcept>
#include <vector>
#include <string>
#include <cstring>
#include <ctype.h>
#include <fstream>
#include <seqan/sequence.h>
#include "alphabet.h"
#include "assert_helpers.h"
#include "tokenize.h"
#include "random_source.h"
#include "threading.h"
#include "filebuf.h"
#include "qual.h"
#include "hit_set.h"
#include "search_globals.h"
#ifdef _WIN32
#define getc_unlocked _fgetc_nolock
#endif
/**
* Classes and routines for reading reads from various input sources.
*/
using namespace std;
using namespace seqan;
/**
* C++ version char* style "itoa":
*/
template<typename T>
char* itoa10(const T& value, char* result) {
// Check that base is valid
char* out = result;
T quotient = value;
if(std::numeric_limits<T>::is_signed) {
if(quotient <= 0) quotient = -quotient;
}
// Now write each digit from most to least significant
do {
*out = "0123456789"[quotient % 10];
++out;
quotient /= 10;
} while (quotient > 0);
// Only apply negative sign for base 10
if(std::numeric_limits<T>::is_signed) {
// Avoid compiler warning in cases where T is unsigned
if (value <= 0 && value != 0) *out++ = '-';
}
reverse( result, out );
*out = 0; // terminator
return out;
}
typedef uint64_t TReadId;
/**
* Parameters affecting how reads and read in.
* Note: Bowtie 2 uses this but Bowtie doesn't yet.
*/
struct PatternParams {
PatternParams() { }
PatternParams(
int format_,
bool fileParallel_,
uint32_t seed_,
size_t max_buf_,
bool solexa64_,
bool phred64_,
bool intQuals_,
int sampleLen_,
int sampleFreq_,
size_t skip_,
int nthreads_,
bool fixName_) :
format(format_),
fileParallel(fileParallel_),
seed(seed_),
max_buf(max_buf_),
solexa64(solexa64_),
phred64(phred64_),
intQuals(intQuals_),
sampleLen(sampleLen_),
sampleFreq(sampleFreq_),
skip(skip_),
nthreads(nthreads_),
fixName(fixName_) { }
int format; // file format
bool fileParallel; // true -> wrap files with separate PatternComposers
uint32_t seed; // pseudo-random seed
size_t max_buf; // number of reads to buffer in one read
bool solexa64; // true -> qualities are on solexa64 scale
bool phred64; // true -> qualities are on phred64 scale
bool intQuals; // true -> qualities are space-separated numbers
int sampleLen; // length of sampled reads for FastaContinuous...
int sampleFreq; // frequency of sampled reads for FastaContinuous...
size_t skip; // skip the first 'skip' patterns
int nthreads; // number of threads for locking
bool fixName; //
};
/**
* A buffer for keeping all relevant information about a single read.
* Each search thread has one.
*/
struct Read {
Read() { reset(); }
~Read() {
clearAll(); reset();
// Prevent seqan from trying to free buffers
_setBegin(patFw, NULL);
_setBegin(patRc, NULL);
_setBegin(qual, NULL);
_setBegin(patFwRev, NULL);
_setBegin(patRcRev, NULL);
_setBegin(qualRev, NULL);
_setBegin(name, NULL);
}
#define RESET_BUF(str, buf, typ) _setBegin(str, (typ*)buf); _setLength(str, 0); _setCapacity(str, BUF_SIZE);
#define RESET_BUF_LEN(str, buf, len, typ) _setBegin(str, (typ*)buf); _setLength(str, len); _setCapacity(str, BUF_SIZE);
/// Point all Strings to the beginning of their respective buffers
/// and set all lengths to 0
void reset() {
patid = 0;
readOrigBufLen = 0;
qualOrigBufLen = 0;
trimmed5 = trimmed3 = 0;
color = false;
primer = '?';
trimc = '?';
seed = 0;
parsed = false;
RESET_BUF(patFw, patBufFw, Dna5);
RESET_BUF(patRc, patBufRc, Dna5);
RESET_BUF(qual, qualBuf, char);
RESET_BUF(patFwRev, patBufFwRev, Dna5);
RESET_BUF(patRcRev, patBufRcRev, Dna5);
RESET_BUF(qualRev, qualBufRev, char);
RESET_BUF(name, nameBuf, char);
}
void clearAll() {
seqan::clear(patFw);
seqan::clear(patRc);
seqan::clear(qual);
seqan::clear(patFwRev);
seqan::clear(patRcRev);
seqan::clear(qualRev);
seqan::clear(name);
parsed = false;
trimmed5 = trimmed3 = 0;
readOrigBufLen = 0;
qualOrigBufLen = 0;
color = false;
primer = '?';
trimc = '?';
seed = 0;
}
/// Return true iff the read (pair) is empty
bool empty() const {
return seqan::empty(patFw);
}
/// Return length of the read in the buffer
uint32_t length() const {
return (uint32_t)seqan::length(patFw);
}
/**
* Construct reverse complement of the pattern. If read is in
* colorspace, reverse color string.
*/
void constructRevComps() {
uint32_t len = length();
RESET_BUF_LEN(patRc, patBufRc, len, Dna5);
if(color) {
for(uint32_t i = 0; i < len; i++) {
// Reverse the sequence
patBufRc[i] = patBufFw[len-i-1];
}
} else {
for(uint32_t i = 0; i < len; i++) {
// Reverse-complement the sequence
patBufRc[i] = (patBufFw[len-i-1] == 4) ? 4 : (patBufFw[len-i-1] ^ 3);
}
}
}
/**
* Given patFw, patRc, and qual, construct the *Rev versions in
* place. Assumes constructRevComps() was called previously.
*/
void constructReverses() {
uint32_t len = length();
RESET_BUF_LEN(patFwRev, patBufFwRev, len, Dna5);
RESET_BUF_LEN(patRcRev, patBufRcRev, len, Dna5);
RESET_BUF_LEN(qualRev, qualBufRev, len, char);
for(uint32_t i = 0; i < len; i++) {
patFwRev[i] = patFw[len-i-1];
patRcRev[i] = patRc[len-i-1];
qualRev[i] = qual[len-i-1];
}
}
/**
* Append a "/1" or "/2" string onto the end of the name buf if
* it's not already there.
*/
void fixMateName(int i) {
assert(i == 1 || i == 2);
size_t namelen = seqan::length(name);
bool append = false;
if(namelen < 2) {
// Name is too short to possibly have /1 or /2 on the end
append = true;
} else {
if(i == 1) {
// append = true iff mate name does not already end in /1
append =
nameBuf[namelen-2] != '/' ||
nameBuf[namelen-1] != '1';
} else {
// append = true iff mate name does not already end in /2
append =
nameBuf[namelen-2] != '/' ||
nameBuf[namelen-1] != '2';
}
}
if(append) {
assert_leq(namelen, BUF_SIZE-2);
_setLength(name, namelen + 2);
nameBuf[namelen] = '/';
nameBuf[namelen+1] = "012"[i];
}
}
/**
* Dump basic information about this read to the given ostream.
*/
void dump(std::ostream& os) const {
os << name << ' ';
if(color) {
for(size_t i = 0; i < seqan::length(patFw); i++) {
os << "0123."[(int)patFw[i]];
}
} else {
os << patFw;
}
os << qual << " ";
}
static const int BUF_SIZE = 1024;
String<Dna5> patFw; // forward-strand sequence
uint8_t patBufFw[BUF_SIZE]; // forward-strand sequence buffer
String<Dna5> patRc; // reverse-complement sequence
uint8_t patBufRc[BUF_SIZE]; // reverse-complement sequence buffer
String<char> qual; // quality values
char qualBuf[BUF_SIZE]; // quality value buffer
String<Dna5> patFwRev; // forward-strand sequence reversed
uint8_t patBufFwRev[BUF_SIZE]; // forward-strand sequence buffer reversed
String<Dna5> patRcRev; // reverse-complement sequence reversed
uint8_t patBufRcRev[BUF_SIZE]; // reverse-complement sequence buffer reversed
String<char> qualRev; // quality values reversed
char qualBufRev[BUF_SIZE]; // quality value buffer reversed
// For remembering the exact input text used to define a read
char readOrigBuf[FileBuf::LASTN_BUF_SZ];
size_t readOrigBufLen;
// For when qualities are in a separate file
char qualOrigBuf[FileBuf::LASTN_BUF_SZ];
size_t qualOrigBufLen;
String<char> name; // read name
char nameBuf[BUF_SIZE]; // read name buffer
bool parsed; // whether read has been fully parsed
uint32_t patid; // unique 0-based id based on order in read file(s)
int mate; // 0 = single-end, 1 = mate1, 2 = mate2
uint32_t seed; // random seed
bool color; // whether read is in color space
char primer; // primer base, for csfasta files
char trimc; // trimmed color, for csfasta files
int trimmed5; // amount actually trimmed off 5' end
int trimmed3; // amount actually trimmed off 3' end
HitSet hitset; // holds previously-found hits; for chaining
};
/**
* All per-thread storage for input read data.
*/
struct PerThreadReadBuf {
PerThreadReadBuf(size_t max_buf) :
max_buf_(max_buf),
bufa_(max_buf),
bufb_(max_buf),
rdid_()
{
bufa_.resize(max_buf);
bufb_.resize(max_buf);
reset();
}
Read& read_a() { return bufa_[cur_buf_]; }
Read& read_b() { return bufb_[cur_buf_]; }
const Read& read_a() const { return bufa_[cur_buf_]; }
const Read& read_b() const { return bufb_[cur_buf_]; }
/**
* Return read id for read/pair currently in the buffer.
*/
TReadId rdid() const {
assert_neq(rdid_, std::numeric_limits<TReadId>::max());
return rdid_ + cur_buf_;
}
/**
* Reset state as though no reads have been read.
*/
void reset() {
cur_buf_ = bufa_.size();
for(size_t i = 0; i < max_buf_; i++) {
bufa_[i].reset();
bufb_[i].reset();
}
rdid_ = std::numeric_limits<TReadId>::max();
}
/**
* Advance cursor to next element
*/
void next() {
assert_lt(cur_buf_, bufa_.size());
cur_buf_++;
}
/**
* Return true when there's nothing left to dish out.
*/
bool exhausted() {
assert_leq(cur_buf_, bufa_.size());
return cur_buf_ >= bufa_.size()-1;
}
/**
* Just after a new batch has been loaded, use init to
* set the cuf_buf_ and rdid_ fields appropriately.
*/
void init() {
cur_buf_ = 0;
}
/**
* Set the read id of the first read in the buffer.
*/
void setReadId(TReadId rdid) {
rdid_ = rdid;
assert_neq(rdid_, std::numeric_limits<TReadId>::max());
}
const size_t max_buf_; // max # reads to read into buffer at once
vector<Read> bufa_; // Read buffer for mate as
vector<Read> bufb_; // Read buffer for mate bs
size_t cur_buf_; // Read buffer currently active
TReadId rdid_; // index of read at offset 0 of bufa_/bufb_
};
/**
* Encapsulates a synchronized source of patterns; usually a file.
* Handles dumping patterns to a logfile (useful for debugging). Also
* optionally reverses reads and quality strings before returning them,
* though that is usually more efficiently done by the concrete
* subclass. Concrete subclasses should delimit critical sections with
* ThreadSafe objects.
*/
class PatternSource {
public:
PatternSource(
const char *dumpfile = NULL) :
readCnt_(0),
dumpfile_(dumpfile),
mutex()
{
// Open dumpfile, if specified
if(dumpfile_ != NULL) {
out_.open(dumpfile_, ios_base::out);
if(!out_.good()) {
cerr << "Could not open pattern dump file \"" << dumpfile_ << "\" for writing" << endl;
throw 1;
}
}
}
virtual ~PatternSource() { }
/**
* Implementation to be provided by concrete subclasses. An
* implementation for this member is only relevant for formats
* where individual input sources look like single-end-read
* sources, e.g., formats where paired-end reads are specified in
* parallel read files.
*/
virtual std::pair<bool, int> nextBatch(
PerThreadReadBuf& pt,
bool batch_a,
bool lock = true) = 0;
/**
* Finishes parsing a given read. Happens outside the critical section.
*/
virtual bool parse(Read& ra, Read& rb, TReadId rdid) const = 0;
/// Reset state to start over again with the first read
virtual void reset() { readCnt_ = 0; }
/**
* Return the number of reads attempted.
*/
TReadId readCount() const { return readCnt_; }
protected:
/**
* Dump the contents of the ReadBuf to the dump file.
*/
void dumpBuf(const Read& r) {
assert(dumpfile_ != NULL);
dump(out_, r.patFw,
empty(r.qual) ? String<char>("(empty)") : r.qual,
empty(r.name) ? String<char>("(empty)") : r.name);
dump(out_, r.patRc,
empty(r.qualRev) ? String<char>("(empty)") : r.qualRev,
empty(r.name) ? String<char>("(empty)") : r.name);
}
/**
* Default format for dumping a read to an output stream. Concrete
* subclasses might want to do something fancier.
*/
virtual void dump(ostream& out,
const String<Dna5>& seq,
const String<char>& qual,
const String<char>& name)
{
out << name << ": " << seq << " " << qual << endl;
}
/// The number of reads read by this PatternSource
volatile uint64_t readCnt_;
const char *dumpfile_; /// dump patterns to this file before returning them
ofstream out_; /// output stream for dumpfile
/// Lock enforcing mutual exclusion for (a) file I/O, (b) writing fields
/// of this or another other shared object.
MUTEX_T mutex;
};
/**
* Encapsualtes a source of patterns where each raw pattern is trimmed
* by some user-defined amount on the 3' and 5' ends. Doesn't
* implement the actual trimming - that's up to the concrete
* descendants.
*/
class TrimmingPatternSource : public PatternSource {
public:
TrimmingPatternSource(const char *dumpfile = NULL,
int trim3 = 0,
int trim5 = 0) :
PatternSource(dumpfile),
trim3_(trim3), trim5_(trim5) { }
protected:
int trim3_;
int trim5_;
};
extern void wrongQualityFormat(const String<char>& read_name);
extern void tooFewQualities(const String<char>& read_name);
extern void tooManyQualities(const String<char>& read_name);
extern void tooManySeqChars(const String<char>& read_name);
/**
* Encapsulates a source of patterns which is an in-memory vector.
*/
class VectorPatternSource : public TrimmingPatternSource {
public:
VectorPatternSource(
const vector<string>& v,
bool color,
const char *dumpfile = NULL,
int trim3 = 0,
int trim5 = 0);
virtual ~VectorPatternSource() { }
/**
* Read next batch. However, batch concept is not very applicable for this
* PatternSource where all the info has already been parsed into the fields
* in the contsructor. This essentially modifies the pt as though we read
* in some number of patterns.
*/
virtual pair<bool, int> nextBatch(
PerThreadReadBuf& pt,
bool batch_a,
bool lock = true);
/**
* Reset so that next call to nextBatch* gets the first batch.
*/
virtual void reset() {
TrimmingPatternSource::reset();
cur_ = 0;
paired_ = false;
}
/**
* Finishes parsing outside the critical section
*/
virtual bool parse(Read& ra, Read& rb, TReadId rdid) const;
private:
pair<bool, int> nextBatchImpl(
PerThreadReadBuf& pt,
bool batch_a);
bool color_; // colorspace?
size_t cur_; // index for first read of next batch
bool paired_; // whether reads are paired
std::vector<std::string> tokbuf_; // buffer for storing parsed tokens
std::vector<std::string> bufs_; // per-read buffers
char nametmp_[20]; // temp buffer for constructing name
};
/**
* Parent class for PatternSources that read from a file.
* Uses unlocked C I/O, on the assumption that all reading
* from the file will take place in an otherwise-protected
* critical section.
*/
class CFilePatternSource : public TrimmingPatternSource {
public:
CFilePatternSource(
const vector<string>& infiles,
const vector<string>* qinfiles,
const char *dumpfile = NULL,
int trim3 = 0,
int trim5 = 0) :
TrimmingPatternSource(dumpfile, trim3, trim5),
infiles_(infiles),
filecur_(0),
fp_(NULL),
qfp_(NULL),
is_open_(false),
first_(true)
{
qinfiles_.clear();
if(qinfiles != NULL) qinfiles_ = *qinfiles;
assert_gt(infiles.size(), 0);
errs_.resize(infiles_.size(), false);
if(qinfiles_.size() > 0 &&
qinfiles_.size() != infiles_.size())
{
cerr << "Error: Different numbers of input FASTA/quality files ("
<< infiles_.size() << "/" << qinfiles_.size() << ")" << endl;
throw 1;
}
open(); // open first file in the list
filecur_++;
}
virtual ~CFilePatternSource() {
if(is_open_) {
if (compressed_) {
gzclose(zfp_);
zfp_ = NULL;
}
else if (fp_ != stdin) {
fclose(fp_);
fp_ = NULL;
}
if(qfp_ != NULL && qfp_ != stdin) {
fclose(qfp_);
qfp_ = NULL;
}
assert(zfp_ == NULL);
assert(fp_ == NULL || fp_ == stdin);
assert(qfp_ == NULL || qfp_ == stdin);
}
}
/**
* Fill Read with the sequence, quality and name for the next
* read in the list of read files. This function gets called by
* all the search threads, so we must handle synchronization.
*
* Returns pair<bool, int> where bool indicates whether we're
* completely done, and int indicates how many reads were read.
*/
virtual pair<bool, int> nextBatch(
PerThreadReadBuf& pt,
bool batch_a,
bool lock);
/**
* Reset so that next call to nextBatch* gets the first batch.
* Should only be called by the master thread.
*/
virtual void reset() {
TrimmingPatternSource::reset();
filecur_ = 0,
open();
filecur_++;
}
protected:
/**
* Light-parse a batch of unpaired reads from current file into the given
* buffer. Called from CFilePatternSource.nextBatch().
*/
virtual std::pair<bool, int> nextBatchFromFile(
PerThreadReadBuf& pt,
bool batch_a) = 0;
/**
* Reset state to handle a fresh file
*/
virtual void resetForNextFile() { }
/**
* Open the next file in the list of input files.
*/
void open();
int getc_wrapper() {
return compressed_ ? gzgetc(zfp_) : getc_unlocked(fp_);
}
int ungetc_wrapper(int c) {
return compressed_ ? gzungetc(c, zfp_) : ungetc(c, fp_);
}
bool is_gzipped_file(const std::string& filename) {
struct stat s;
if (stat(filename.c_str(), &s) != 0) {
perror("stat");
}
else {
if (S_ISFIFO(s.st_mode))
return true;
}
size_t pos = filename.find_last_of(".");
std::string ext = (pos == std::string::npos) ? "" : filename.substr(pos + 1);
if (ext == "" || ext == "gz" || ext == "Z") {
return true;
}
return false;
}
vector<string> infiles_; /// filenames for read files
vector<string> qinfiles_; /// filenames for quality files
vector<bool> errs_; /// whether we've already printed an error for each file
size_t filecur_; /// index into infiles_ of next file to read
FILE *fp_; /// read file currently being read from
FILE *qfp_; /// quality file currently being read from
gzFile zfp_;
bool is_open_; /// whether fp_ is currently open
bool first_;
char buf_[64*1024]; /// file buffer for sequences
char qbuf_[64*1024]; /// file buffer for qualities
bool compressed_;
private:
pair<bool, int> nextBatchImpl(
PerThreadReadBuf& pt,
bool batch_a);
};
/**
* Synchronized concrete pattern source for a list of FASTA or CSFASTA
* (if color = true) files.
*/
class FastaPatternSource : public CFilePatternSource {
public:
FastaPatternSource(
const vector<string>& infiles,
const vector<string>* qinfiles,
bool color,
const char *dumpfile = NULL,
int trim3 = 0,
int trim5 = 0,
bool solexa64 = false,
bool phred64 = false,
bool intQuals = false) :
CFilePatternSource(
infiles,
qinfiles,
dumpfile,
trim3,
trim5),
first_(true),
color_(color),
solexa64_(solexa64),
phred64_(phred64),
intQuals_(intQuals) { }
/**
* Reset so that next call to nextBatch* gets the first batch.
* Should only be called by the master thread.
*/
virtual void reset() {
first_ = true;
CFilePatternSource::reset();
}
/**
* Finalize FASTA parsing outside critical section.
*/
virtual bool parse(Read& ra, Read& rb, TReadId rdid) const;
protected:
/**
* Light-parse a FASTA batch into the given buffer.
*/
virtual std::pair<bool, int> nextBatchFromFile(
PerThreadReadBuf& pt,
bool batch_a);
/**
* Reset state to handle a fresh file
*/
virtual void resetForNextFile() {
first_ = true;
}
virtual void dump(ostream& out,
const String<Dna5>& seq,
const String<char>& qual,
const String<char>& name)
{
out << ">" << name << endl << seq << endl;
}
private:
bool first_;
bool color_;
bool solexa64_;
bool phred64_;
bool intQuals_;
};
/**
* Tokenize a line of space-separated integer quality values.
*/
static inline bool tokenizeQualLine(FileBuf& filebuf, char *buf, size_t buflen, vector<string>& toks) {
size_t rd = filebuf.gets(buf, buflen);
if(rd == 0) return false;
assert(NULL == strrchr(buf, '\n'));
tokenize(string(buf), " ", toks);
return true;
}
/**
* Synchronized concrete pattern source for a list of files with tab-
* delimited name, seq, qual fields (or, for paired-end reads,
* basename, seq1, qual1, seq2, qual2).
*/
class TabbedPatternSource : public CFilePatternSource {
public:
TabbedPatternSource(
const vector<string>& infiles,
bool secondName, // whether it's --12/--tab5 or --tab6
bool color,
const char *dumpfile = NULL,
int trim3 = 0,
int trim5 = 0,
bool solQuals = false,
bool phred64Quals = false,
bool intQuals = false) :
CFilePatternSource(
infiles,
NULL,
dumpfile,
trim3,
trim5),
color_(color),
solQuals_(solQuals),
phred64Quals_(phred64Quals),
intQuals_(intQuals) { }
/**
* Finalize tabbed parsing outside critical section.
*/
virtual bool parse(Read& ra, Read& rb, TReadId rdid) const;
protected:
/**
* Light-parse a batch of tabbed-format reads into given buffer.
*/
virtual std::pair<bool, int> nextBatchFromFile(
PerThreadReadBuf& pt,
bool batch_a);
/**
* Dump a FASTQ-style record for the read.
*/
virtual void dump(ostream& out,
const String<Dna5>& seq,
const String<char>& qual,
const String<char>& name)
{
out << "@" << name << endl << seq << endl
<< "+" << endl << qual << endl;
}
protected:
bool color_; // colorspace reads?
bool solQuals_; // base qualities are log odds
bool phred64Quals_; // base qualities are on -64 scale
bool intQuals_; // base qualities are space-separated strings
bool secondName_; // true if --tab6, false if --tab5
};
/**
* Synchronized concrete pattern source for a list of FASTA files where
* reads need to be extracted from long continuous sequences.
*/
class FastaContinuousPatternSource : public CFilePatternSource {
public:
FastaContinuousPatternSource(
const vector<string>& infiles,
size_t length,
size_t freq,
const char *dumpfile = NULL) :
CFilePatternSource(
infiles,
NULL,
dumpfile,
0,
0),
length_(length),
freq_(freq),
eat_(length_-1),
beginning_(true),
bufCur_(0),
subReadCnt_(0llu)
{
assert_gt(freq_, 0);
resetForNextFile();
assert_lt(length_, (size_t)Read::BUF_SIZE);
}
virtual void reset() {
CFilePatternSource::reset();
resetForNextFile();
}
/**
* Finalize FASTA parsing outside critical section.
*/
virtual bool parse(Read& ra, Read& rb, TReadId rdid) const;
protected:
/**
* Light-parse a batch into the given buffer.
*/
virtual std::pair<bool, int> nextBatchFromFile(
PerThreadReadBuf& pt,
bool batch_a);
/**
* Reset state to be read for the next file.
*/
virtual void resetForNextFile() {
eat_ = length_-1;
//name_prefix_buf_.clear();
beginning_ = true;
bufCur_ = 0;
subReadCnt_ = readCnt_;
}
private:
const size_t length_; /// length of reads to generate
const size_t freq_; /// frequency to sample reads
size_t eat_; /// number of characters we need to skip before
/// we have flushed all of the ambiguous or
/// non-existent characters out of our read
/// window
bool beginning_; /// skipping over the first read length?
char buf_[1024]; /// read buffer
char name_prefix_buf_[1024]; /// FASTA sequence name buffer
char name_int_buf_[20]; /// for composing offsets for names
size_t bufCur_; /// buffer cursor; points to where we should
/// insert the next character
uint64_t subReadCnt_;/// number to subtract from readCnt_ to get
/// the pat id to output (so it resets to 0 for
/// each new sequence)
};
/**
* Read a FASTQ-format file.
* See: http://maq.sourceforge.net/fastq.shtml
*/
class FastqPatternSource : public CFilePatternSource {
public:
FastqPatternSource(
const vector<string>& infiles,
bool color,
const char *dumpfile = NULL,
int trim3 = 0,
int trim5 = 0,
bool solexa_quals = false,
bool phred64Quals = false,
bool integer_quals = false,
bool interleaved = false,
uint32_t skip = 0) :
CFilePatternSource(
infiles,
NULL,
dumpfile,
trim3,
trim5),
first_(true),
solQuals_(solexa_quals),
phred64Quals_(phred64Quals),
intQuals_(integer_quals),
interleaved_(interleaved),
color_(color) { }
virtual void reset() {
first_ = true;
CFilePatternSource::reset();
}
/**
* Finalize FASTQ parsing outside critical section.
*/
virtual bool parse(Read& ra, Read& rb, TReadId rdid) const;
protected:
/**
* "Light" parser. This is inside the critical section, so the key is to do
* just enough parsing so that another function downstream (finalize()) can do
* the rest of the parsing. Really this function's only job is to stick every
* for lines worth of the input file into a buffer (r.readOrigBuf). finalize()
* then parses the contents of r.readOrigBuf later.
*/
virtual pair<bool, int> nextBatchFromFile(
PerThreadReadBuf& pt,
bool batch_a);
virtual void resetForNextFile() {
first_ = true;
}
virtual void dump(ostream& out,
const String<Dna5>& seq,
const String<char>& qual,
const String<char>& name)
{
out << "@" << name << endl << seq << endl << "+" << endl << qual << endl;
}
private:
bool first_;
bool solQuals_;
bool phred64Quals_;
bool intQuals_;