diff --git a/src/utils/Contexts/Context.cpp b/src/utils/Contexts/Context.cpp index 22bbf126583b7a737ef8b4ab4a24e881160518d5..439c1fef8931460ea24540b42cc10fe3d38757ce 100644 --- a/src/utils/Contexts/Context.cpp +++ b/src/utils/Contexts/Context.cpp @@ -20,6 +20,7 @@ Context::Context() _showHelp(false), _obeySplits(false), _uncompressedBam(false), + _useBufferedOutput(true), _anyHit(false), _noHit(false), _writeA(false), @@ -288,6 +289,10 @@ bool Context::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { setSortedInput(true); markUsed(i - skipFirstArgs); } + else if(strcmp(argv[i], "-nobuf") == 0) { + setUseBufferedOutput(false); + markUsed(i - skipFirstArgs); + } else if(strcmp(argv[i], "-header") == 0) { setPrintHeader(true); markUsed(i - skipFirstArgs); diff --git a/src/utils/Contexts/Context.h b/src/utils/Contexts/Context.h index 7f26fc0c550e1c5babe20bf23c96578e6f4904d2..19160e9556936d3e6907c147d7653be35b3069d8 100644 --- a/src/utils/Contexts/Context.h +++ b/src/utils/Contexts/Context.h @@ -105,6 +105,9 @@ public: bool getUncompressedBam() const { return _uncompressedBam; } void setUncompressedBam(bool val) { _uncompressedBam = val; } + + bool getUseBufferedOutput() const { return _useBufferedOutput; } + void setUseBufferedOutput(bool val) { _useBufferedOutput = val; } // // INTERSECT METOHDS // @@ -241,6 +244,7 @@ protected: bool _showHelp; bool _obeySplits; bool _uncompressedBam; + bool _useBufferedOutput; bool _anyHit; bool _noHit; diff --git a/src/utils/FileRecordTools/FileReaders/BufferedStreamMgr.h b/src/utils/FileRecordTools/FileReaders/BufferedStreamMgr.h index 963b5ce33bf5167840f1ea1ca62eb1b75011e60c..2a77854abe69561a330e39434f7cbaa7c9a1d008 100644 --- a/src/utils/FileRecordTools/FileReaders/BufferedStreamMgr.h +++ b/src/utils/FileRecordTools/FileReaders/BufferedStreamMgr.h @@ -43,7 +43,7 @@ private: bool _streamFinished; QuickString _currScanBuffer; //The minus ones in these constants are for leaving room for a null terminator after reading into buffers. - static const int MAIN_BUF_READ_SIZE = 2047; // 2 Kb minus 1 + static const int MAIN_BUF_READ_SIZE = 1023; // 2 Kb minus 1 static const int GZIP_LINE_BUF_SIZE = 8191; // 8K bool readFileChunk(); bool getTypeData(); diff --git a/src/utils/FileRecordTools/RecordOutputMgr.cpp b/src/utils/FileRecordTools/RecordOutputMgr.cpp index c405a435693f0cc628c652da42c40de8b3b6e224..b9b1a0a3bb37d6b94d9a62ae72a231a3a12d8f38 100644 --- a/src/utils/FileRecordTools/RecordOutputMgr.cpp +++ b/src/utils/FileRecordTools/RecordOutputMgr.cpp @@ -82,6 +82,7 @@ bool RecordOutputMgr::printKeyAndTerminate(RecordKeyList &keyList) { } else if (bamCode == NOT_BAM) { keyList.getKey()->print(_outBuf); return false; + } //otherwise, it was BAM_AS_BED, and the key was printed. return false; @@ -157,6 +158,7 @@ void RecordOutputMgr::printRecord(RecordKeyList &keyList, RecordKeyList *blockLi tab(); _outBuf.append('0'); newline(); + if (needsFlush()) flush(); } else if (_context->getLeftJoin()) { if (printKeyAndTerminate(keyList)) { @@ -166,6 +168,7 @@ void RecordOutputMgr::printRecord(RecordKeyList &keyList, RecordKeyList *blockLi tab(); null(false, true); newline(); + if (needsFlush()) flush(); _currBlockList = NULL; return; } @@ -202,6 +205,7 @@ void RecordOutputMgr::checkForHeader() { } } _context->setPrintHeader(false); + if (needsFlush()) flush(); } void RecordOutputMgr::reportOverlapDetail(const Record *keyRecord, const Record *hitRecord) @@ -269,22 +273,26 @@ void RecordOutputMgr::reportOverlapDetail(const Record *keyRecord, const Record if (!_context->getWriteA() && !_context->getWriteB() && !_context->getWriteOverlap() && !_context->getLeftJoin()) { printKey(keyRecord, *startStr, *endStr); newline(); + if (needsFlush()) flush(); } else if ((_context->getWriteA() && _context->getWriteB()) || _context->getLeftJoin()) { printKey(keyRecord); tab(); hitRecord->print(_outBuf); newline(); + if (needsFlush()) flush(); } else if (_context->getWriteA()) { printKey(keyRecord); newline(); + if (needsFlush()) flush(); } else if (_context->getWriteB()) { printKey(keyRecord, *startStr, *endStr); tab(); hitRecord->print(_outBuf); newline(); + if (needsFlush()) flush(); } else if (_context->getWriteOverlap()) { int printOverlapBases = max(0, minEnd-maxStart); @@ -294,6 +302,7 @@ void RecordOutputMgr::reportOverlapDetail(const Record *keyRecord, const Record tab(); int2str(printOverlapBases, _outBuf, true); newline(); + if (needsFlush()) flush(); } } @@ -305,6 +314,7 @@ void RecordOutputMgr::reportOverlapSummary(RecordKeyList &keyList) return; } newline(); + if (needsFlush()) flush(); } else if (_context->getWriteCount()) { if (printKeyAndTerminate(keyList)) { return; @@ -312,11 +322,13 @@ void RecordOutputMgr::reportOverlapSummary(RecordKeyList &keyList) tab(); int2str(numOverlapsFound, _outBuf, true); newline(); + if (needsFlush()) flush(); } else if (_context->getNoHit() && numOverlapsFound == 0) { if (printKeyAndTerminate(keyList)) { return; } newline(); + if (needsFlush()) flush(); } } diff --git a/src/utils/FileRecordTools/RecordOutputMgr.h b/src/utils/FileRecordTools/RecordOutputMgr.h index 1bb8bbfcadb4f84e98a9a2039a63f3e33d618bd1..de6261162c8f8bdbd19aafadbc535b9776f7b1bd 100644 --- a/src/utils/FileRecordTools/RecordOutputMgr.h +++ b/src/utils/FileRecordTools/RecordOutputMgr.h @@ -10,10 +10,10 @@ using namespace std; +#include "Context.h" #include "RecordKeyList.h" #include "api/BamWriter.h" -class Context; class BlockMgr; class RecordOutputMgr { @@ -53,7 +53,13 @@ private: void reportOverlapSummary(RecordKeyList &keyList); static const unsigned int MAX_OUTBUF_SIZE = 16384; //16 K - bool needsFlush() const { return _outBuf.size() >= MAX_OUTBUF_SIZE *.9; } + + // If we are using buffered output, only flush the output buffer if it's least + // 90% full. If we're not using buffered output, flush if it's not empty + bool needsFlush() const { + return ((_context->getUseBufferedOutput() &&_outBuf.size() >= MAX_OUTBUF_SIZE *.9) || + (!_context->getUseBufferedOutput() && !_outBuf.empty())); + } void flush(); };