From 79daf360c40700ee26045f753ec0ceedaf044643 Mon Sep 17 00:00:00 2001
From: nkindlon <nek3d@virginia.edu>
Date: Sat, 28 Sep 2013 21:02:30 -0400
Subject: [PATCH] Fixed unmapped mate bug

---
 src/utils/BinTree/BinTree.cpp                   |  2 +-
 src/utils/FileRecordTools/FileRecordMgr.cpp     | 12 +++++++-----
 src/utils/FileRecordTools/Records/BamRecord.cpp |  1 +
 src/utils/FileRecordTools/Records/Record.cpp    |  5 +++--
 src/utils/FileRecordTools/Records/Record.h      |  2 ++
 5 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/utils/BinTree/BinTree.cpp b/src/utils/BinTree/BinTree.cpp
index 36e5b36e..12da7a1e 100644
--- a/src/utils/BinTree/BinTree.cpp
+++ b/src/utils/BinTree/BinTree.cpp
@@ -111,6 +111,7 @@ void BinTree::getHits(Record *record, RecordKeyList &hitSet)
 	if (_showBinMetrics) {
 		return; //don't care about query entries just yet.
 	}
+
     const QuickString &chr = record->getChrName();
 	mainMapType::iterator mainIter = _mainMap.find(chr);
 	if (mainIter == _mainMap.end()) {
@@ -173,7 +174,6 @@ bool BinTree::addRecordToTree(const Record *record)
 	uint32_t startPos = (uint32_t)(record->getStartPos());
 	uint32_t endPos = (uint32_t)(record->getEndPos());
 
-
 	//is this chr currently in the main map?
 	allBinsType bins = NULL;
 	mainMapType::iterator mainIter = _mainMap.find(chr);
diff --git a/src/utils/FileRecordTools/FileRecordMgr.cpp b/src/utils/FileRecordTools/FileRecordMgr.cpp
index 2931286e..08998aaa 100644
--- a/src/utils/FileRecordTools/FileRecordMgr.cpp
+++ b/src/utils/FileRecordTools/FileRecordMgr.cpp
@@ -124,7 +124,12 @@ Record *FileRecordMgr::allocateAndGetNextRecord()
 		_recordMgr->deleteRecord(record);
 		return NULL;
 	}
-
+	// In the rare case of Bam records where both the read and it's mate failed to map,
+	// Ignore the record. Delete and return null
+	if (record->isUnmapped() && record->isMateUnmapped()) {
+		_recordMgr->deleteRecord(record);
+		return NULL;
+	}
 	if (!record->coordsValid()) {
 		cerr << "Error: Invalid record in file " << _filename << ". Record is " << endl << *record << endl;
 		exit(1);
@@ -160,7 +165,7 @@ void FileRecordMgr::testInputSortOrder(Record *record)
 
 	// Special: For BAM records that aren't mapped, we actually don't want
 	// to test the sort order. Another ugly hack sponsored by the letters B, A, and M.
-	if (record->getType() == FileRecordTypeChecker::BAM_RECORD_TYPE && (static_cast<const BamRecord *>(record))->isUnmapped()) {
+	if (record->isUnmapped()) {
 		return;
 	}
 
@@ -269,9 +274,6 @@ bool FileRecordMgr::allocateAndGetNextMergedRecord(RecordKeyList & recList, WANT
 			startRecord = NULL;
 		}
 	}
-	if (startRecord->getStartPos() == 107071272) {
-		printf("Break point.\n");
-	}
 
 	// OK!! We have a start record!
 
diff --git a/src/utils/FileRecordTools/Records/BamRecord.cpp b/src/utils/FileRecordTools/Records/BamRecord.cpp
index 4b628613..39096456 100644
--- a/src/utils/FileRecordTools/Records/BamRecord.cpp
+++ b/src/utils/FileRecordTools/Records/BamRecord.cpp
@@ -44,6 +44,7 @@ bool BamRecord::initFromFile(BamFileReader *bamFileReader)
 
 	_bamAlignment = bamFileReader->getAlignment();
 	_isUnmapped = !_bamAlignment.IsMapped();
+	_isMateUnmapped = !_bamAlignment.IsMateMapped();
 	return true;
 }
 
diff --git a/src/utils/FileRecordTools/Records/Record.cpp b/src/utils/FileRecordTools/Records/Record.cpp
index f4203153..e5bc1e6f 100644
--- a/src/utils/FileRecordTools/Records/Record.cpp
+++ b/src/utils/FileRecordTools/Records/Record.cpp
@@ -8,7 +8,8 @@ Record::Record()
   _endPos(-1),
   _strand(UNKNOWN),
   _zeroLength(false),
-  _isUnmapped(false)
+  _isUnmapped(false),
+  _isMateUnmapped(false)
 {
 }
 
@@ -38,7 +39,7 @@ void Record::clear() {
 	_endPosStr.clear();
 	_zeroLength = false;
 	_isUnmapped = false;
-
+	_isMateUnmapped = false;
 }
 
 void Record::setStrand(char val)
diff --git a/src/utils/FileRecordTools/Records/Record.h b/src/utils/FileRecordTools/Records/Record.h
index 47cf3b34..9c5c3ff1 100644
--- a/src/utils/FileRecordTools/Records/Record.h
+++ b/src/utils/FileRecordTools/Records/Record.h
@@ -91,6 +91,7 @@ public:
 	// because we want to short circuit the intersects method if either record is an unmapped
 	// Bam record.
 	bool isUnmapped() const { return _isUnmapped; }
+	bool isMateUnmapped() const { return _isMateUnmapped; }
 
 
 	virtual bool operator < (const Record &other) const;
@@ -132,6 +133,7 @@ protected:
 	strandType _strand;
 	bool _zeroLength;
 	bool _isUnmapped;
+	bool _isMateUnmapped;
 };
 
 
-- 
GitLab