1 #include "BamFileReader.h"
2 #include "ParseTools.h"
3 #include <cstdio>
BamFileReader()4 BamFileReader::BamFileReader()
5 :  _bamReader(NULL),
6    _eof(false),
7    _useTags(true)
8 {
9 
10 }
11 
~BamFileReader()12 BamFileReader::~BamFileReader()
13 {
14 }
15 
open()16 bool BamFileReader::open()
17 {
18     _bamHeader = _bamReader->GetHeaderText();
19     _references = _bamReader->GetReferenceData();
20 
21 	return true;
22 }
23 
isOpen() const24 bool BamFileReader::isOpen() const
25 {
26 	return _bamReader->IsOpen();
27 }
28 
close()29 void BamFileReader::close()
30 {
31 //	_bamReader->Close();
32 }
33 
readEntry()34 bool BamFileReader::readEntry()
35 {
36 	if (_useTags) {
37 		if (_bamReader->GetNextAlignment(_bamAlignment)) {
38 			return true;
39 		}
40 	} else {
41 		if (_bamReader->GetNextAlignmentCore(_bamAlignment)) {
42 			return true;
43 		}
44 	}
45 	//hit end of file
46 	_eof = true;
47 	return false;
48 }
49 
getChrName(string & str) const50 void BamFileReader::getChrName(string &str) const
51 {
52 	int refId = _bamAlignment.RefID;
53 	if (refId < 0) {
54 		return;
55 	}
56 	str =  _references[refId].RefName;
57 }
58 
getBamChrId() const59 int BamFileReader::getBamChrId() const
60 {
61 	return _bamAlignment.RefID;
62 }
63 
getStartPos() const64 int BamFileReader::getStartPos() const
65 {
66 	return _bamAlignment.Position;
67 }
68 
getEndPos() const69 int BamFileReader::getEndPos() const
70 {
71 	return _bamAlignment.GetEndPosition(false, false);
72 }
73 
getName(string & str) const74 void BamFileReader::getName(string &str) const
75 {
76 	if (!_useTags) {
77 		str = _bamAlignment.SupportData.AllCharData.c_str();
78 	} else {
79 		str = _bamAlignment.Name;
80 	}
81     if (_bamAlignment.IsFirstMate()) {
82     	str += "/1";
83     }
84     else if (_bamAlignment.IsSecondMate()) {
85     	str += "/2";
86     }
87 }
88 
getScore(string & str) const89 void BamFileReader::getScore(string &str) const
90 {
91 	int2str((int)_bamAlignment.MapQuality, str);
92 }
93 
getStrand() const94 char BamFileReader::getStrand() const
95 {
96 	if (_bamAlignment.IsReverseStrand()) {
97 		return '-';
98 	}
99 	return '+';
100 }
101 
getMateChrName(string & str) const102 void BamFileReader::getMateChrName(string &str) const
103 {
104 	int refId = _bamAlignment.MateRefID;
105 	if (refId < 0) {
106 		return;
107 	}
108 	str =  _references[refId].RefName;
109 }
110 
111