Skip to content
Snippets Groups Projects
Commit d42edc99 authored by Aaron Quinlan's avatar Aaron Quinlan
Browse files

Merge pull request #64 from nkindlon/master

Fixed merge bugs w/ sort order and chrom change, added unit tests
parents 70345fd5 bbda1f41
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......
......@@ -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("");
......
......@@ -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
......
......@@ -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;
}
}
......
chr1 9 30 2
chr1 10 20 1
chr1 11 25 3
chr1 12 30 5
chr1 100 110 4
chr2 11 20 6
......@@ -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
chr1 10 20 1
chr1 9 30 2
chr1 11 25 3
chr1 100 110 4
chr1 12 30 5
chr2 11 20 6
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment