-
Notifications
You must be signed in to change notification settings - Fork 120
/
filebuf.h
718 lines (661 loc) · 15.5 KB
/
filebuf.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
/*
* Copyright 2011, Ben Langmead <[email protected]>
*
* This file is part of Bowtie 2.
*
* Bowtie 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bowtie 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILEBUF_H_
#define FILEBUF_H_
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdexcept>
#include "assert_helpers.h"
/**
* Simple, fast helper for determining if a character is a newline.
*/
static inline bool isnewline(int c) {
return c == '\r' || c == '\n';
}
/**
* Simple, fast helper for determining if a character is a non-newline
* whitespace character.
*/
static inline bool isspace_notnl(int c) {
return isspace(c) && !isnewline(c);
}
/**
* Simple wrapper for a FILE*, istream or ifstream that reads it in chunks
* using fread and keeps those chunks in a buffer. It also services calls to
* get(), peek() and gets() from the buffer, reading in additional chunks when
* necessary.
*
* Helper functions do things like parse strings, numbers, and FASTA records.
*
*
*/
class FileBuf {
public:
FileBuf() {
init();
}
FileBuf(FILE *in) {
init();
_in = in;
assert(_in != NULL);
}
FileBuf(std::ifstream *inf) {
init();
_inf = inf;
assert(_inf != NULL);
}
FileBuf(std::istream *ins) {
init();
_ins = ins;
assert(_ins != NULL);
}
/**
* Return true iff there is a stream ready to read.
*/
bool isOpen() {
return _in != NULL || _inf != NULL || _ins != NULL;
}
/**
* Close the input stream (if that's possible)
*/
void close() {
if(_in != NULL && _in != stdin) {
fclose(_in);
} else if(_inf != NULL) {
_inf->close();
} else {
// can't close _ins
}
}
/**
* Get the next character of input and advance.
*/
int get() {
assert(_in != NULL || _inf != NULL || _ins != NULL);
int c = peek();
if(c != -1) {
_cur++;
if(_lastn_cur < LASTN_BUF_SZ) _lastn_buf[_lastn_cur++] = c;
}
return c;
}
/**
* Return true iff all input is exhausted.
*/
bool eof() {
return (_cur == _buf_sz) && _done;
}
/**
* Initialize the buffer with a new C-style file.
*/
void newFile(FILE *in) {
_in = in;
_inf = NULL;
_ins = NULL;
_cur = BUF_SZ;
_buf_sz = BUF_SZ;
_done = false;
}
/**
* Initialize the buffer with a new ifstream.
*/
void newFile(std::ifstream *__inf) {
_in = NULL;
_inf = __inf;
_ins = NULL;
_cur = BUF_SZ;
_buf_sz = BUF_SZ;
_done = false;
}
/**
* Initialize the buffer with a new istream.
*/
void newFile(std::istream *__ins) {
_in = NULL;
_inf = NULL;
_ins = __ins;
_cur = BUF_SZ;
_buf_sz = BUF_SZ;
_done = false;
}
/**
* Restore state as though we just started reading the input
* stream.
*/
void reset() {
if(_inf != NULL) {
_inf->clear();
_inf->seekg(0, std::ios::beg);
} else if(_ins != NULL) {
_ins->clear();
_ins->seekg(0, std::ios::beg);
} else {
rewind(_in);
}
_cur = BUF_SZ;
_buf_sz = BUF_SZ;
_done = false;
}
/**
* Peek at the next character of the input stream without
* advancing. Typically we can simple read it from the buffer.
* Occasionally we'll need to read in a new buffer's worth of data.
*/
int peek() {
assert(_in != NULL || _inf != NULL || _ins != NULL);
assert_leq(_cur, _buf_sz);
if(_cur == _buf_sz) {
if(_done) {
// We already exhausted the input stream
return -1;
}
// Read a new buffer's worth of data
else {
// Get the next chunk
if(_inf != NULL) {
_inf->read((char*)_buf, BUF_SZ);
_buf_sz = _inf->gcount();
} else if(_ins != NULL) {
_ins->read((char*)_buf, BUF_SZ);
_buf_sz = _ins->gcount();
} else {
assert(_in != NULL);
_buf_sz = fread(_buf, 1, BUF_SZ, _in);
}
_cur = 0;
if(_buf_sz == 0) {
// Exhausted, and we have nothing to return to the
// caller
_done = true;
return -1;
} else if(_buf_sz < BUF_SZ) {
// Exhausted
_done = true;
}
}
}
return (int)_buf[_cur];
}
/**
* Store a string of characters from the input file into 'buf',
* until we see a newline, EOF, or until 'len' characters have been
* read.
*/
size_t gets(char *buf, size_t len) {
size_t stored = 0;
while(true) {
int c = get();
if(c == -1) {
// End-of-file
buf[stored] = '\0';
return stored;
}
if(stored == len-1 || isnewline(c)) {
// End of string
buf[stored] = '\0';
// Skip over all end-of-line characters
int pc = peek();
while(isnewline(pc)) {
get(); // discard
pc = peek();
}
// Next get() will be after all newline characters
return stored;
}
buf[stored++] = (char)c;
}
}
/**
* Store a string of characters from the input file into 'buf',
* until we see a newline, EOF, or until 'len' characters have been
* read.
*/
size_t get(char *buf, size_t len) {
size_t stored = 0;
for(size_t i = 0; i < len; i++) {
int c = get();
if(c == -1) return i;
buf[stored++] = (char)c;
}
return len;
}
static const size_t LASTN_BUF_SZ = 8 * 1024;
/**
* Keep get()ing characters until a non-whitespace character (or
* -1) is reached, and return it.
*/
int getPastWhitespace() {
int c;
while(isspace(c = get()) && c != -1);
return c;
}
/**
* Keep get()ing characters until a we've passed over the next
* string of newline characters (\r's and \n's) or -1 is reached,
* and return it.
*/
int getPastNewline() {
int c = get();
while(!isnewline(c) && c != -1) c = get();
while(isnewline(c)) c = get();
assert_neq(c, '\r');
assert_neq(c, '\n');
return c;
}
/**
* Keep get()ing characters until a we've passed over the next
* string of newline characters (\r's and \n's) or -1 is reached,
* and return it.
*/
int peekPastNewline() {
int c = peek();
while(!isnewline(c) && c != -1) c = get();
while(isnewline(c)) c = get();
assert_neq(c, '\r');
assert_neq(c, '\n');
return c;
}
/**
* Keep peek()ing then get()ing characters until the next return
* from peek() is just after the last newline of the line.
*/
int peekUptoNewline() {
int c = peek();
while(!isnewline(c) && c != -1) {
get(); c = peek();
}
while(isnewline(c)) {
get();
c = peek();
}
assert_neq(c, '\r');
assert_neq(c, '\n');
return c;
}
/**
* Parse a FASTA record. Append name characters to 'name' and and append
* all sequence characters to 'seq'. If gotCaret is true, assuming the
* file cursor has already moved just past the starting '>' character.
*/
template <typename TNameStr, typename TSeqStr>
void parseFastaRecord(
TNameStr& name,
TSeqStr& seq,
bool gotCaret = false)
{
int c;
if(!gotCaret) {
// Skip over caret and non-newline whitespace
c = peek();
while(isspace_notnl(c) || c == '>') { get(); c = peek(); }
} else {
// Skip over non-newline whitespace
c = peek();
while(isspace_notnl(c)) { get(); c = peek(); }
}
size_t namecur = 0, seqcur = 0;
// c is the first character of the fasta name record, or is the first
// newline character if the name record is empty
while(!isnewline(c) && c != -1) {
name[namecur++] = c; get(); c = peek();
}
// sequence consists of all the non-whitespace characters between here
// and the next caret
while(true) {
// skip over whitespace
while(isspace(c)) { get(); c = peek(); }
// if we see caret or EOF, break
if(c == '>' || c == -1) break;
// append and continue
seq[seqcur++] = c;
get(); c = peek();
}
}
/**
* Parse a FASTA record and return its length. If gotCaret is true,
* assuming the file cursor has already moved just past the starting '>'
* character.
*/
void parseFastaRecordLength(
size_t& nameLen,
size_t& seqLen,
bool gotCaret = false)
{
int c;
nameLen = seqLen = 0;
if(!gotCaret) {
// Skip over caret and non-newline whitespace
c = peek();
while(isspace_notnl(c) || c == '>') { get(); c = peek(); }
} else {
// Skip over non-newline whitespace
c = peek();
while(isspace_notnl(c)) { get(); c = peek(); }
}
// c is the first character of the fasta name record, or is the first
// newline character if the name record is empty
while(!isnewline(c) && c != -1) {
nameLen++; get(); c = peek();
}
// sequence consists of all the non-whitespace characters between here
// and the next caret
while(true) {
// skip over whitespace
while(isspace(c)) { get(); c = peek(); }
// if we see caret or EOF, break
if(c == '>' || c == -1) break;
// append and continue
seqLen++;
get(); c = peek();
}
}
/**
* Reset to the beginning of the last-N-chars buffer.
*/
void resetLastN() {
_lastn_cur = 0;
}
/**
* Copy the last several characters in the last-N-chars buffer
* (since the last reset) into the provided buffer.
*/
size_t copyLastN(char *buf) {
memcpy(buf, _lastn_buf, _lastn_cur);
return _lastn_cur;
}
/**
* Get const pointer to the last-N-chars buffer.
*/
const char *lastN() const {
return _lastn_buf;
}
/**
* Get current size of the last-N-chars buffer.
*/
size_t lastNLen() const {
return _lastn_cur;
}
private:
void init() {
_in = NULL;
_inf = NULL;
_ins = NULL;
_cur = _buf_sz = BUF_SZ;
_done = false;
_lastn_cur = 0;
// no need to clear _buf[]
}
static const size_t BUF_SZ = 256 * 1024;
FILE *_in;
std::ifstream *_inf;
std::istream *_ins;
size_t _cur;
size_t _buf_sz;
bool _done;
uint8_t _buf[BUF_SZ]; // (large) input buffer
size_t _lastn_cur;
char _lastn_buf[LASTN_BUF_SZ]; // buffer of the last N chars dispensed
};
/**
* Wrapper for a buffered output stream that writes bitpairs.
*/
class BitpairOutFileBuf {
public:
/**
* Open a new output stream to a file with given name.
*/
BitpairOutFileBuf(const char *in) : bpPtr_(0), cur_(0) {
assert(in != NULL);
out_ = fopen(in, "wb");
if(out_ == NULL) {
std::cerr << "Error: Could not open bitpair-output file " << in << std::endl;
throw 1;
}
memset(buf_, 0, BUF_SZ);
}
/**
* Write a single bitpair into the buf. Flush the buffer if it's
* full.
*/
void write(int bp) {
assert_lt(bp, 4);
assert_geq(bp, 0);
buf_[cur_] |= (bp << bpPtr_);
if(bpPtr_ == 6) {
bpPtr_ = 0;
cur_++;
if(cur_ == BUF_SZ) {
// Flush the buffer
if(!fwrite((const void *)buf_, BUF_SZ, 1, out_)) {
std::cerr << "Error writing to the reference index file (.4.ebwt)" << std::endl;
throw 1;
}
// Reset to beginning of the buffer
cur_ = 0;
}
// Initialize next octet to 0
buf_[cur_] = 0;
} else {
bpPtr_ += 2;
}
}
/**
* Write any remaining bitpairs and then close the input
*/
void close() {
if(cur_ > 0 || bpPtr_ > 0) {
if(bpPtr_ == 0) cur_--;
if(!fwrite((const void *)buf_, cur_ + 1, 1, out_)) {
std::cerr << "Error writing to the reference index file (.4.ebwt)" << std::endl;
throw 1;
}
}
fclose(out_);
}
private:
static const size_t BUF_SZ = 128 * 1024;
FILE *out_;
int bpPtr_;
size_t cur_;
char buf_[BUF_SZ]; // (large) input buffer
};
/**
* Wrapper for a buffered output stream that writes characters and
* other data types. This class is *not* synchronized; the caller is
* responsible for synchronization.
*/
class OutFileBuf {
public:
/**
* Open a new output stream to a file with given name.
*/
OutFileBuf(const std::string& out, bool binary = false) :
name_(out.c_str()), cur_(0), closed_(false)
{
out_ = fopen(out.c_str(), binary ? "wb" : "w");
if(out_ == NULL) {
std::cerr << "Error: Could not open alignment output file " << out.c_str() << std::endl;
throw 1;
}
if(setvbuf(out_, NULL, _IOFBF, 10* 1024* 1024))
std::cerr << "Warning: Could not allocate the proper buffer size for output file stream. " << std::endl;
}
/**
* Open a new output stream to a file with given name.
*/
OutFileBuf(const char *out, bool binary = false) :
name_(out), cur_(0), closed_(false)
{
assert(out != NULL);
out_ = fopen(out, binary ? "wb" : "w");
if(out_ == NULL) {
std::cerr << "Error: Could not open alignment output file " << out << std::endl;
throw 1;
}
}
/**
* Open a new output stream to standard out.
*/
OutFileBuf() : name_("cout"), cur_(0), closed_(false) {
out_ = stdout;
}
/**
* Close buffer when object is destroyed.
*/
~OutFileBuf() { close(); }
/**
* Open a new output stream to a file with given name.
*/
void setFile(const char *out, bool binary = false) {
assert(out != NULL);
out_ = fopen(out, binary ? "wb" : "w");
if(out_ == NULL) {
std::cerr << "Error: Could not open alignment output file " << out << std::endl;
throw 1;
}
reset();
}
/**
* Write a single character into the write buffer and, if
* necessary, flush.
*/
void write(char c) {
assert(!closed_);
if(cur_ == BUF_SZ) flush();
buf_[cur_++] = c;
}
/**
* Write a c++ string to the write buffer and, if necessary, flush.
*/
void writeString(const std::string& s) {
assert(!closed_);
size_t slen = s.length();
if(cur_ + slen > BUF_SZ) {
if(cur_ > 0) flush();
if(slen >= BUF_SZ) {
fwrite(s.c_str(), slen, 1, out_);
} else {
memcpy(&buf_[cur_], s.data(), slen);
assert_eq(0, cur_);
cur_ = slen;
}
} else {
memcpy(&buf_[cur_], s.data(), slen);
cur_ += slen;
}
assert_leq(cur_, BUF_SZ);
}
/**
* Write a c++ string to the write buffer and, if necessary, flush.
*/
template<typename T>
void writeString(const T& s) {
assert(!closed_);
size_t slen = s.length();
if(cur_ + slen > BUF_SZ) {
if(cur_ > 0) flush();
if(slen >= BUF_SZ) {
fwrite(s.toZBuf(), slen, 1, out_);
} else {
memcpy(&buf_[cur_], s.toZBuf(), slen);
assert_eq(0, cur_);
cur_ = slen;
}
} else {
memcpy(&buf_[cur_], s.toZBuf(), slen);
cur_ += slen;
}
assert_leq(cur_, BUF_SZ);
}
/**
* Write a c++ string to the write buffer and, if necessary, flush.
*/
void writeChars(const char * s, size_t len) {
assert(!closed_);
if(cur_ + len > BUF_SZ) {
if(cur_ > 0) flush();
if(len >= BUF_SZ) {
fwrite(s, len, 1, out_);
} else {
memcpy(&buf_[cur_], s, len);
assert_eq(0, cur_);
cur_ = len;
}
} else {
memcpy(&buf_[cur_], s, len);
cur_ += len;
}
assert_leq(cur_, BUF_SZ);
}
/**
* Write a 0-terminated C string to the output stream.
*/
void writeChars(const char * s) {
writeChars(s, strlen(s));
}
/**
* Write any remaining bitpairs and then close the input
*/
void close() {
if(closed_) return;
if(cur_ > 0) flush();
closed_ = true;
if(out_ != stdout) {
fclose(out_);
}
}
/**
* Reset so that the next write is as though it's the first.
*/
void reset() {
cur_ = 0;
closed_ = false;
}
void flush() {
if(!fwrite((const void *)buf_, cur_, 1, out_)) {
std::cerr << "Error while flushing and closing output" << std::endl;
throw 1;
}
cur_ = 0;
}
/**
* Return true iff this stream is closed.
*/
bool closed() const {
return closed_;
}
/**
* Return the filename.
*/
const char *name() {
return name_;
}
private:
static const size_t BUF_SZ = 16 * 1024;
const char *name_;
FILE *out_;
size_t cur_;
char buf_[BUF_SZ]; // (large) input buffer
bool closed_;
};
#endif /*ndef FILEBUF_H_*/