diff --git a/src/utils/Contexts/ContextBase.cpp b/src/utils/Contexts/ContextBase.cpp index f05c5b186ab71b1b3c4821650da715c0bb518d15..8ca547eafa42e52dae75f7acbdf24f6b011d9f26 100644 --- a/src/utils/Contexts/ContextBase.cpp +++ b/src/utils/Contexts/ContextBase.cpp @@ -101,6 +101,7 @@ bool ContextBase::determineOutputType() { //Otherwise, if the input is BAM, then the output is BAM if (getFile(0)->getFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) { setOutputFileType(FileRecordTypeChecker::BAM_FILE_TYPE); + return true; } //Okay, it's bed. @@ -264,11 +265,19 @@ int ContextBase::getBamHeaderAndRefIdx() { //already found which BAM file to use for the header return _bamHeaderAndRefIdx; } - if (_files[_queryFileIdx]->getFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) { - _bamHeaderAndRefIdx = _queryFileIdx; - } else { - _bamHeaderAndRefIdx = _databaseFileIdx; + if (hasIntersectMethods()) { + if (_files[_queryFileIdx]->getFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) { + _bamHeaderAndRefIdx = _queryFileIdx; + } else { + _bamHeaderAndRefIdx = _databaseFileIdx; + } + return _bamHeaderAndRefIdx; } + if (_files[0]->getFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) { + _bamHeaderAndRefIdx = 0; + return _bamHeaderAndRefIdx; + } + return _bamHeaderAndRefIdx; } diff --git a/src/utils/Contexts/ContextMerge.cpp b/src/utils/Contexts/ContextMerge.cpp index 57ee37094995253920b2a4480819caaa3af60b31..a9b6fb3010eab1ff1f1591f8435b018c74b82c8d 100644 --- a/src/utils/Contexts/ContextMerge.cpp +++ b/src/utils/Contexts/ContextMerge.cpp @@ -10,8 +10,10 @@ ContextMerge::ContextMerge() { + setSortedInput(true); setUseMergedIntervals(true); setColumnOpsMethods(true); + setExplicitBedOutput(true); //merge has no default columnOps the way map does, so we'll need to clear those. _keyListOps->setColumns(""); diff --git a/src/utils/FileRecordTools/FileRecordMergeMgr.cpp b/src/utils/FileRecordTools/FileRecordMergeMgr.cpp index f4c39f12169fea8e302ad73738794c22b77ef524..c3b5969af232a4dde90f8c50258f416b52c0af7e 100644 --- a/src/utils/FileRecordTools/FileRecordMergeMgr.cpp +++ b/src/utils/FileRecordTools/FileRecordMergeMgr.cpp @@ -95,16 +95,14 @@ Record *FileRecordMergeMgr::getNextRecord(RecordKeyList *recList) //check that we are still on the same chromosome. const QuickString &newChrom = nextRecord->getChrName(); if (newChrom != currChrom) { //hit a different chromosome. - if (_foundChroms.find(newChrom) == _foundChroms.end() || takenFromStorage) { - //haven't seen this chromosome before, sort order is already enforced in the base class method. - if (!mustDelete) { - addToStorage(nextRecord); - } else { - deleteRecord(nextRecord); - } - nextRecord = NULL; - break; + //haven't seen this chromosome before, sort order is already enforced in the base class method. + if (!mustDelete) { + addToStorage(nextRecord); + } else { + deleteRecord(nextRecord); } + nextRecord = NULL; + break; } //check whether it's in range diff --git a/src/utils/RecordOutputMgr/RecordOutputMgr.cpp b/src/utils/RecordOutputMgr/RecordOutputMgr.cpp index 9aaa511bc3d2e10c73c15706bbd42e29808f435a..b7b2f9a456878783864c85b97bcbd3c667c27536 100644 --- a/src/utils/RecordOutputMgr/RecordOutputMgr.cpp +++ b/src/utils/RecordOutputMgr/RecordOutputMgr.cpp @@ -76,18 +76,18 @@ void RecordOutputMgr::init(ContextBase *context) { } bool RecordOutputMgr::printKeyAndTerminate(RecordKeyList &keyList) { + if (_context->getProgram() == ContextBase::MERGE) { + //when printing merged records, we want to force the printing into + //bed3 format, which is surprisingly difficult to do. Had to use the following: + const Bed3Interval *bed3 = static_cast<const Bed3Interval *>(keyList.getKey()); + bed3->Bed3Interval::print(_outBuf); + return false; + } printBamType bamCode = printBamRecord(keyList); if (bamCode == BAM_AS_BAM) { return true; } else if (bamCode == NOT_BAM) { - if (_context->getProgram() == ContextBase::MERGE) { - //when printing merged records, we want to force the printing into - //bed3 format, which is surprisingly difficult to do. Had to use the following: - const Bed3Interval *bed3 = static_cast<const Bed3Interval *>(keyList.getKey()); - bed3->Bed3Interval::print(_outBuf); - } else { - keyList.getKey()->print(_outBuf); - } + keyList.getKey()->print(_outBuf); return false; } //otherwise, it was BAM_AS_BED, and the key was printed. @@ -221,17 +221,6 @@ void RecordOutputMgr::printRecord(RecordKeyList &keyList, RecordKeyList *blockLi printKeyAndTerminate(keyList); _currBamBlockList = NULL; return; - } else if (_context->getProgram() == ContextBase::MERGE) { - if (!printKeyAndTerminate(keyList)) { - if (_context->getDesiredStrand() != FileRecordMergeMgr::ANY_STRAND) { - //add the sign of the record - tab(); - _outBuf.append(keyList.getKey()->getStrand()); - } - if (!_afterVal.empty()) tab(); - } - _currBamBlockList = NULL; - return; } } diff --git a/test/merge/b.bed b/test/merge/b.bed new file mode 100644 index 0000000000000000000000000000000000000000..6f977e5fad7e928e5dd8d1a0ae526915c618cf39 --- /dev/null +++ b/test/merge/b.bed @@ -0,0 +1,7 @@ +chr1 9 30 2 +chr1 10 20 1 +chr1 11 25 3 +chr1 12 30 5 +chr1 100 110 4 +chr2 11 20 6 + diff --git a/test/merge/test-merge.sh b/test/merge/test-merge.sh index b7f5e12f1ae83da37e96ec92b7c36c0017e2269b..051fc12fe2eb25422afb106db8f35908913ac2f9 100644 --- a/test/merge/test-merge.sh +++ b/test/merge/test-merge.sh @@ -239,4 +239,40 @@ check exp obs rm obs exp +########################################################### +# Test that sort order is enforced +########################################################### +echo " merge.t19...\c" +echo \ +"Error: Sorted input specified, but the file unsorted.bed has the following out of order record +chr1 9 30 2" > exp +$BT merge -i unsorted.bed 2>&1 > /dev/null | head -2 >obs +check exp obs +rm obs exp + +########################################################### +# Test that chrom change is handled correctly +########################################################### +echo " merge.t20...\c" +echo \ +"chr1 9 30 +chr1 100 110 +chr2 11 20" > exp +$BT merge -i b.bed > obs +check exp obs +rm exp obs + +########################################################### +# Test that a merged BAM file only gives BED3 output +########################################################### +echo " merge.t21...\c" +echo \ +"chr1 10 20 +chr1 30 100 +chr2 10 20 +chr2 30 40 +chr2 42 100" > exp +$BT merge -i a.full.bed > obs +check exp obs +rm exp obs diff --git a/test/merge/unsorted.bed b/test/merge/unsorted.bed new file mode 100644 index 0000000000000000000000000000000000000000..e3f9500d64095c9640927a4f0427fb3e0c95f484 --- /dev/null +++ b/test/merge/unsorted.bed @@ -0,0 +1,7 @@ +chr1 10 20 1 +chr1 9 30 2 +chr1 11 25 3 +chr1 100 110 4 +chr1 12 30 5 +chr2 11 20 6 +