1 // ***************************************************************************
\r
2 // BamReader.cpp (c) 2009 Derek Barnett, Michael Str�mberg
\r
3 // Marth Lab, Department of Biology, Boston College
\r
4 // All rights reserved.
\r
5 // ---------------------------------------------------------------------------
\r
6 // Last modified: 30 August 2010 (DB)
\r
7 // ---------------------------------------------------------------------------
\r
8 // Uses BGZF routines were adapted from the bgzf.c code developed at the Broad
\r
10 // ---------------------------------------------------------------------------
\r
11 // Provides the basic functionality for reading BAM files
\r
12 // ***************************************************************************
\r
15 #include <algorithm>
\r
21 // BamTools includes
\r
23 #include "BamReader.h"
\r
24 #include "BamIndex.h"
\r
25 using namespace BamTools;
\r
26 using namespace std;
\r
28 struct BamReader::BamReaderPrivate {
\r
30 // -------------------------------
\r
31 // structs, enums, typedefs
\r
32 // -------------------------------
\r
33 enum RegionState { BEFORE_REGION = 0
\r
38 // -------------------------------
\r
40 // -------------------------------
\r
42 // general file data
\r
47 RefVector References;
\r
49 int64_t AlignmentsBeginOffset;
\r
51 string IndexFilename;
\r
56 // user-specified region values
\r
58 bool IsLeftBoundSpecified;
\r
59 bool IsRightBoundSpecified;
\r
61 bool IsRegionSpecified;
\r
68 // BAM character constants
\r
69 const char* DNA_LOOKUP;
\r
70 const char* CIGAR_LOOKUP;
\r
72 // -------------------------------
\r
73 // constructor & destructor
\r
74 // -------------------------------
\r
75 BamReaderPrivate(BamReader* parent);
\r
76 ~BamReaderPrivate(void);
\r
78 // -------------------------------
\r
79 // "public" interface
\r
80 // -------------------------------
\r
84 bool Jump(int refID, int position = 0);
\r
85 bool Open(const string& filename, const string& indexFilename = "");
\r
87 bool SetRegion(const BamRegion& region);
\r
89 // access alignment data
\r
90 bool GetNextAlignment(BamAlignment& bAlignment);
\r
91 bool GetNextAlignmentCore(BamAlignment& bAlignment);
\r
93 // access auxiliary data
\r
94 int GetReferenceID(const string& refName) const;
\r
97 bool CreateIndex(bool useDefaultIndex);
\r
99 // -------------------------------
\r
100 // internal methods
\r
101 // -------------------------------
\r
103 // *** reading alignments and auxiliary data *** //
\r
105 // fills out character data for BamAlignment data
\r
106 bool BuildCharData(BamAlignment& bAlignment);
\r
107 // checks to see if alignment overlaps current region
\r
108 RegionState IsOverlap(BamAlignment& bAlignment);
\r
109 // retrieves header text from BAM file
\r
110 void LoadHeaderData(void);
\r
111 // retrieves BAM alignment under file pointer
\r
112 bool LoadNextAlignment(BamAlignment& bAlignment);
\r
113 // builds reference data structure from BAM file
\r
114 void LoadReferenceData(void);
\r
116 // *** index file handling *** //
\r
118 // clear out inernal index data structure
\r
119 void ClearIndex(void);
\r
120 // loads index from BAM index file
\r
121 bool LoadIndex(void);
\r
124 // -----------------------------------------------------
\r
125 // BamReader implementation (wrapper around BRPrivate)
\r
126 // -----------------------------------------------------
\r
128 BamReader::BamReader(void) {
\r
129 d = new BamReaderPrivate(this);
\r
133 BamReader::~BamReader(void) {
\r
139 void BamReader::Close(void) { d->Close(); }
\r
140 bool BamReader::IsIndexLoaded(void) const { return d->IsIndexLoaded; }
\r
141 bool BamReader::IsOpen(void) const { return d->mBGZF.IsOpen; }
\r
142 bool BamReader::Jump(int refID, int position) {
\r
143 d->Region.LeftRefID = refID;
\r
144 d->Region.LeftPosition = position;
\r
145 d->IsLeftBoundSpecified = true;
\r
146 d->IsRightBoundSpecified = false;
\r
147 return d->Jump(refID, position);
\r
149 bool BamReader::Open(const string& filename, const string& indexFilename) { return d->Open(filename, indexFilename); }
\r
150 bool BamReader::Rewind(void) { return d->Rewind(); }
\r
151 bool BamReader::SetRegion(const BamRegion& region) { return d->SetRegion(region); }
\r
152 bool BamReader::SetRegion(const int& leftRefID, const int& leftBound, const int& rightRefID, const int& rightBound) {
\r
153 return d->SetRegion( BamRegion(leftRefID, leftBound, rightRefID, rightBound) );
\r
156 // access alignment data
\r
157 bool BamReader::GetNextAlignment(BamAlignment& bAlignment) { return d->GetNextAlignment(bAlignment); }
\r
158 bool BamReader::GetNextAlignmentCore(BamAlignment& bAlignment) { return d->GetNextAlignmentCore(bAlignment); }
\r
160 // access auxiliary data
\r
161 const string BamReader::GetHeaderText(void) const { return d->HeaderText; }
\r
162 int BamReader::GetReferenceCount(void) const { return d->References.size(); }
\r
163 const RefVector& BamReader::GetReferenceData(void) const { return d->References; }
\r
164 int BamReader::GetReferenceID(const string& refName) const { return d->GetReferenceID(refName); }
\r
165 const std::string BamReader::GetFilename(void) const { return d->Filename; }
\r
167 // index operations
\r
168 bool BamReader::CreateIndex(bool useDefaultIndex) { return d->CreateIndex(useDefaultIndex); }
\r
170 // -----------------------------------------------------
\r
171 // BamReaderPrivate implementation
\r
172 // -----------------------------------------------------
\r
175 BamReader::BamReaderPrivate::BamReaderPrivate(BamReader* parent)
\r
177 , IsIndexLoaded(false)
\r
178 , AlignmentsBeginOffset(0)
\r
179 , IsLeftBoundSpecified(false)
\r
180 , IsRightBoundSpecified(false)
\r
181 , IsRegionSpecified(false)
\r
185 , DNA_LOOKUP("=ACMGRSVTWYHKDBN")
\r
186 , CIGAR_LOOKUP("MIDNSHP")
\r
188 IsBigEndian = SystemIsBigEndian();
\r
192 BamReader::BamReaderPrivate::~BamReaderPrivate(void) {
\r
196 bool BamReader::BamReaderPrivate::BuildCharData(BamAlignment& bAlignment) {
\r
198 // calculate character lengths/offsets
\r
199 const unsigned int dataLength = bAlignment.SupportData.BlockLength - BAM_CORE_SIZE;
\r
200 // const unsigned int cigarDataOffset = bAlignment.SupportData.QueryNameLength;
\r
201 const unsigned int seqDataOffset = bAlignment.SupportData.QueryNameLength + (bAlignment.SupportData.NumCigarOperations * 4);
\r
202 const unsigned int qualDataOffset = seqDataOffset + (bAlignment.SupportData.QuerySequenceLength+1)/2;
\r
203 const unsigned int tagDataOffset = qualDataOffset + bAlignment.SupportData.QuerySequenceLength;
\r
204 const unsigned int tagDataLength = dataLength - tagDataOffset;
\r
206 // set up char buffers
\r
207 const char* allCharData = bAlignment.SupportData.AllCharData.data();
\r
208 // uint32_t* cigarData = (uint32_t*)(allCharData + cigarDataOffset);
\r
209 const char* seqData = ((const char*)allCharData) + seqDataOffset;
\r
210 const char* qualData = ((const char*)allCharData) + qualDataOffset;
\r
211 char* tagData = ((char*)allCharData) + tagDataOffset;
\r
213 // store alignment name (relies on null char in name as terminator)
\r
214 bAlignment.Name.assign((const char*)(allCharData));
\r
216 // // save CigarOps
\r
218 // bAlignment.CigarData.clear();
\r
219 // bAlignment.CigarData.reserve(bAlignment.SupportData.NumCigarOperations);
\r
220 // for (unsigned int i = 0; i < bAlignment.SupportData.NumCigarOperations; ++i) {
\r
222 // // swap if necessary
\r
223 // if ( IsBigEndian ) { SwapEndian_32(cigarData[i]); }
\r
225 // // build CigarOp structure
\r
226 // op.Length = (cigarData[i] >> BAM_CIGAR_SHIFT);
\r
227 // op.Type = CIGAR_LOOKUP[ (cigarData[i] & BAM_CIGAR_MASK) ];
\r
230 // bAlignment.CigarData.push_back(op);
\r
233 // save query sequence
\r
234 bAlignment.QueryBases.clear();
\r
235 bAlignment.QueryBases.reserve(bAlignment.SupportData.QuerySequenceLength);
\r
236 for (unsigned int i = 0; i < bAlignment.SupportData.QuerySequenceLength; ++i) {
\r
237 char singleBase = DNA_LOOKUP[ ( ( seqData[(i/2)] >> (4*(1-(i%2)))) & 0xf ) ];
\r
238 bAlignment.QueryBases.append(1, singleBase);
\r
241 // save qualities, converting from numeric QV to 'FASTQ-style' ASCII character
\r
242 bAlignment.Qualities.clear();
\r
243 bAlignment.Qualities.reserve(bAlignment.SupportData.QuerySequenceLength);
\r
244 for (unsigned int i = 0; i < bAlignment.SupportData.QuerySequenceLength; ++i) {
\r
245 char singleQuality = (char)(qualData[i]+33);
\r
246 bAlignment.Qualities.append(1, singleQuality);
\r
249 // if QueryBases is empty (and this is a allowed case)
\r
250 if ( bAlignment.QueryBases.empty() )
\r
251 bAlignment.AlignedBases = bAlignment.QueryBases;
\r
253 // if QueryBases contains data, then build AlignedBases using CIGAR data
\r
256 // resize AlignedBases
\r
257 bAlignment.AlignedBases.clear();
\r
258 bAlignment.AlignedBases.reserve(bAlignment.SupportData.QuerySequenceLength);
\r
260 // iterate over CigarOps
\r
262 vector<CigarOp>::const_iterator cigarIter = bAlignment.CigarData.begin();
\r
263 vector<CigarOp>::const_iterator cigarEnd = bAlignment.CigarData.end();
\r
264 for ( ; cigarIter != cigarEnd; ++cigarIter ) {
\r
266 const CigarOp& op = (*cigarIter);
\r
271 bAlignment.AlignedBases.append(bAlignment.QueryBases.substr(k, op.Length)); // for 'M', 'I' - write bases
\r
275 k += op.Length; // for 'S' - soft clip, skip over query bases
\r
279 bAlignment.AlignedBases.append(op.Length, '-'); // for 'D' - write gap character
\r
283 bAlignment.AlignedBases.append( op.Length, '*' ); // for 'P' - write padding character
\r
287 bAlignment.AlignedBases.append( op.Length, 'N' ); // for 'N' - write N's, skip bases in original query sequence
\r
291 break; // for 'H' - hard clip, do nothing to AlignedBases, move to next op
\r
294 printf("ERROR: Invalid Cigar op type\n"); // shouldn't get here
\r
300 // -----------------------
\r
301 // Added: 3-25-2010 DB
\r
302 // Fixed: endian-correctness for tag data
\r
303 // -----------------------
\r
304 if ( IsBigEndian ) {
\r
306 while ( (unsigned int)i < tagDataLength ) {
\r
308 i += 2; // skip tag type (e.g. "RG", "NM", etc)
\r
309 uint8_t type = toupper(tagData[i]); // lower & upper case letters have same meaning
\r
310 ++i; // skip value type
\r
320 SwapEndian_16p(&tagData[i]);
\r
321 i += sizeof(uint16_t);
\r
326 SwapEndian_32p(&tagData[i]);
\r
327 i += sizeof(uint32_t);
\r
331 SwapEndian_64p(&tagData[i]);
\r
332 i += sizeof(uint64_t);
\r
337 while (tagData[i]) { ++i; }
\r
338 ++i; // increment one more for null terminator
\r
342 printf("ERROR: Invalid tag value type\n"); // shouldn't get here
\r
349 bAlignment.TagData.clear();
\r
350 bAlignment.TagData.resize(tagDataLength);
\r
351 memcpy((char*)bAlignment.TagData.data(), tagData, tagDataLength);
\r
353 // clear the core-only flag
\r
354 bAlignment.SupportData.HasCoreOnly = false;
\r
360 // clear index data structure
\r
361 void BamReader::BamReaderPrivate::ClearIndex(void) {
\r
364 IsIndexLoaded = false;
\r
367 // closes the BAM file
\r
368 void BamReader::BamReaderPrivate::Close(void) {
\r
370 // close BGZF file stream
\r
373 // clear out index data
\r
376 // clear out header data
\r
377 HeaderText.clear();
\r
379 // clear out region flags
\r
380 IsLeftBoundSpecified = false;
\r
381 IsRightBoundSpecified = false;
\r
382 IsRegionSpecified = false;
\r
385 // create BAM index from BAM file (keep structure in memory) and write to default index output file
\r
386 bool BamReader::BamReaderPrivate::CreateIndex(bool useDefaultIndex) {
\r
388 // clear out prior index data
\r
391 // create default index
\r
392 if ( useDefaultIndex )
\r
393 NewIndex = new BamDefaultIndex(&mBGZF, Parent, IsBigEndian);
\r
394 // create BamTools 'custom' index
\r
396 NewIndex = new BamToolsIndex(&mBGZF, Parent, IsBigEndian);
\r
400 ok &= NewIndex->Build();
\r
401 IsIndexLoaded = ok;
\r
403 // attempt to save index data to file
\r
404 ok &= NewIndex->Write(Filename);
\r
406 // return success/fail of both building & writing index
\r
410 // get next alignment (from specified region, if given)
\r
411 bool BamReader::BamReaderPrivate::GetNextAlignment(BamAlignment& bAlignment) {
\r
413 // if valid alignment found, attempt to parse char data, and return success/failure
\r
414 if ( GetNextAlignmentCore(bAlignment) )
\r
415 return BuildCharData(bAlignment);
\r
417 // no valid alignment found
\r
422 // retrieves next available alignment core data (returns success/fail)
\r
423 // ** DOES NOT parse any character data (read name, bases, qualities, tag data)
\r
424 // these can be accessed, if necessary, from the supportData
\r
425 // useful for operations requiring ONLY positional or other alignment-related information
\r
426 bool BamReader::BamReaderPrivate::GetNextAlignmentCore(BamAlignment& bAlignment) {
\r
428 // if valid alignment available
\r
429 if ( LoadNextAlignment(bAlignment) ) {
\r
431 // set core-only flag
\r
432 bAlignment.SupportData.HasCoreOnly = true;
\r
434 // if region not specified, return success
\r
435 if ( !IsLeftBoundSpecified ) return true;
\r
437 // determine region state (before, within, after)
\r
438 BamReader::BamReaderPrivate::RegionState state = IsOverlap(bAlignment);
\r
440 // if alignment lies after region, return false
\r
441 if ( state == AFTER_REGION )
\r
444 while ( state != WITHIN_REGION ) {
\r
445 // if no valid alignment available (likely EOF) return failure
\r
446 if ( !LoadNextAlignment(bAlignment) ) return false;
\r
447 // if alignment lies after region, return false (no available read within region)
\r
448 state = IsOverlap(bAlignment);
\r
449 if ( state == AFTER_REGION) return false;
\r
453 // return success (alignment found that overlaps region)
\r
457 // no valid alignment
\r
462 // returns RefID for given RefName (returns References.size() if not found)
\r
463 int BamReader::BamReaderPrivate::GetReferenceID(const string& refName) const {
\r
465 // retrieve names from reference data
\r
466 vector<string> refNames;
\r
467 RefVector::const_iterator refIter = References.begin();
\r
468 RefVector::const_iterator refEnd = References.end();
\r
469 for ( ; refIter != refEnd; ++refIter) {
\r
470 refNames.push_back( (*refIter).RefName );
\r
473 // return 'index-of' refName ( if not found, returns refNames.size() )
\r
474 return distance(refNames.begin(), find(refNames.begin(), refNames.end(), refName));
\r
477 // returns region state - whether alignment ends before, overlaps, or starts after currently specified region
\r
478 // this *internal* method should ONLY called when (at least) IsLeftBoundSpecified == true
\r
479 BamReader::BamReaderPrivate::RegionState BamReader::BamReaderPrivate::IsOverlap(BamAlignment& bAlignment) {
\r
481 // --------------------------------------------------
\r
482 // check alignment start against right bound cutoff
\r
484 // if full region of interest was given
\r
485 if ( IsRightBoundSpecified ) {
\r
487 // read starts on right bound reference, but AFTER right bound position
\r
488 if ( bAlignment.RefID == Region.RightRefID && bAlignment.Position > Region.RightPosition )
\r
489 return AFTER_REGION;
\r
491 // if read starts on reference AFTER right bound, return false
\r
492 if ( bAlignment.RefID > Region.RightRefID )
\r
493 return AFTER_REGION;
\r
496 // --------------------------------------------------------
\r
497 // no right bound given OR read starts before right bound
\r
498 // so, check if it overlaps left bound
\r
500 // if read starts on left bound reference AND after left boundary, return success
\r
501 if ( bAlignment.RefID == Region.LeftRefID && bAlignment.Position >= Region.LeftPosition)
\r
502 return WITHIN_REGION;
\r
504 // if read is on any reference sequence before left bound, return false
\r
505 if ( bAlignment.RefID < Region.LeftRefID )
\r
506 return BEFORE_REGION;
\r
508 // --------------------------------------------------------
\r
509 // read is on left bound reference, but starts before left bound position
\r
511 // if it overlaps, return WITHIN_REGION
\r
512 if ( bAlignment.GetEndPosition() >= Region.LeftPosition )
\r
513 return WITHIN_REGION;
\r
514 // else begins before left bound position
\r
516 return BEFORE_REGION;
\r
519 // jumps to specified region(refID, leftBound) in BAM file, returns success/fail
\r
520 bool BamReader::BamReaderPrivate::Jump(int refID, int position) {
\r
522 // -----------------------------------------------------------------------
\r
523 // check for existing index
\r
524 if ( !IsIndexLoaded || NewIndex == 0 ) return false;
\r
525 // see if reference has alignments
\r
526 if ( !NewIndex->HasAlignments(refID) ) return false;
\r
527 // make sure position is valid
\r
528 if ( position > References.at(refID).RefLength ) return false;
\r
530 // determine possible offsets
\r
531 vector<int64_t> offsets;
\r
532 if ( !NewIndex->GetOffsets(Region, IsRightBoundSpecified, offsets) ) {
\r
533 printf("ERROR: Could not jump: unable to calculate offset for specified region.\n");
\r
537 // iterate through offsets
\r
538 BamAlignment bAlignment;
\r
539 bool result = true;
\r
540 for ( vector<int64_t>::const_iterator o = offsets.begin(); o != offsets.end(); ++o) {
\r
542 // attempt seek & load first available alignment
\r
543 result &= mBGZF.Seek(*o);
\r
544 LoadNextAlignment(bAlignment);
\r
546 // if this alignment corresponds to desired position
\r
547 // return success of seeking back to 'current offset'
\r
548 if ( (bAlignment.RefID == refID && bAlignment.Position + bAlignment.Length > position) || (bAlignment.RefID > refID) ) {
\r
549 if ( o != offsets.begin() ) --o;
\r
550 return mBGZF.Seek(*o);
\r
557 // load BAM header data
\r
558 void BamReader::BamReaderPrivate::LoadHeaderData(void) {
\r
560 // check to see if proper BAM header
\r
562 if (mBGZF.Read(buffer, 4) != 4) {
\r
563 printf("Could not read header type\n");
\r
567 if (strncmp(buffer, "BAM\001", 4)) {
\r
568 printf("wrong header type!\n");
\r
572 // get BAM header text length
\r
573 mBGZF.Read(buffer, 4);
\r
574 unsigned int headerTextLength = BgzfData::UnpackUnsignedInt(buffer);
\r
575 if ( IsBigEndian ) { SwapEndian_32(headerTextLength); }
\r
577 // get BAM header text
\r
578 char* headerText = (char*)calloc(headerTextLength + 1, 1);
\r
579 mBGZF.Read(headerText, headerTextLength);
\r
580 HeaderText = (string)((const char*)headerText);
\r
582 // clean up calloc-ed temp variable
\r
586 // load existing index data from BAM index file (".bai"), return success/fail
\r
587 bool BamReader::BamReaderPrivate::LoadIndex(void) {
\r
589 // clear out any existing index data
\r
592 // skip if index file empty
\r
593 if ( IndexFilename.empty() )
\r
596 // check supplied filename for index type
\r
597 size_t defaultExtensionFound = IndexFilename.find(".bai");
\r
598 size_t customExtensionFound = IndexFilename.find(".bti");
\r
600 // if SAM/BAM default (".bai")
\r
601 if ( defaultExtensionFound != string::npos )
\r
602 NewIndex = new BamDefaultIndex(&mBGZF, Parent, IsBigEndian);
\r
604 // if BamTools custom index (".bti")
\r
605 else if ( customExtensionFound != string::npos )
\r
606 NewIndex = new BamToolsIndex(&mBGZF, Parent, IsBigEndian);
\r
610 printf("ERROR: Unknown index file extension.\n");
\r
614 // return success of loading index data from file
\r
615 IsIndexLoaded = NewIndex->Load(IndexFilename);
\r
616 return IsIndexLoaded;
\r
619 // populates BamAlignment with alignment data under file pointer, returns success/fail
\r
620 bool BamReader::BamReaderPrivate::LoadNextAlignment(BamAlignment& bAlignment) {
\r
622 // read in the 'block length' value, make sure it's not zero
\r
624 mBGZF.Read(buffer, 4);
\r
625 bAlignment.SupportData.BlockLength = BgzfData::UnpackUnsignedInt(buffer);
\r
626 if ( IsBigEndian ) { SwapEndian_32(bAlignment.SupportData.BlockLength); }
\r
627 if ( bAlignment.SupportData.BlockLength == 0 ) { return false; }
\r
629 // read in core alignment data, make sure the right size of data was read
\r
630 char x[BAM_CORE_SIZE];
\r
631 if ( mBGZF.Read(x, BAM_CORE_SIZE) != BAM_CORE_SIZE ) { return false; }
\r
633 if ( IsBigEndian ) {
\r
634 for ( int i = 0; i < BAM_CORE_SIZE; i+=sizeof(uint32_t) ) {
\r
635 SwapEndian_32p(&x[i]);
\r
639 // set BamAlignment 'core' and 'support' data
\r
640 bAlignment.RefID = BgzfData::UnpackSignedInt(&x[0]);
\r
641 bAlignment.Position = BgzfData::UnpackSignedInt(&x[4]);
\r
643 unsigned int tempValue = BgzfData::UnpackUnsignedInt(&x[8]);
\r
644 bAlignment.Bin = tempValue >> 16;
\r
645 bAlignment.MapQuality = tempValue >> 8 & 0xff;
\r
646 bAlignment.SupportData.QueryNameLength = tempValue & 0xff;
\r
648 tempValue = BgzfData::UnpackUnsignedInt(&x[12]);
\r
649 bAlignment.AlignmentFlag = tempValue >> 16;
\r
650 bAlignment.SupportData.NumCigarOperations = tempValue & 0xffff;
\r
652 bAlignment.SupportData.QuerySequenceLength = BgzfData::UnpackUnsignedInt(&x[16]);
\r
653 bAlignment.MateRefID = BgzfData::UnpackSignedInt(&x[20]);
\r
654 bAlignment.MatePosition = BgzfData::UnpackSignedInt(&x[24]);
\r
655 bAlignment.InsertSize = BgzfData::UnpackSignedInt(&x[28]);
\r
657 // set BamAlignment length
\r
658 bAlignment.Length = bAlignment.SupportData.QuerySequenceLength;
\r
660 // read in character data - make sure proper data size was read
\r
661 bool readCharDataOK = false;
\r
662 const unsigned int dataLength = bAlignment.SupportData.BlockLength - BAM_CORE_SIZE;
\r
663 char* allCharData = (char*)calloc(sizeof(char), dataLength);
\r
665 if ( mBGZF.Read(allCharData, dataLength) == (signed int)dataLength) {
\r
667 // store 'allCharData' in supportData structure
\r
668 bAlignment.SupportData.AllCharData.assign((const char*)allCharData, dataLength);
\r
670 // set success flag
\r
671 readCharDataOK = true;
\r
674 // need to calculate this here so that BamAlignment::GetEndPosition() performs correctly,
\r
675 // even when BamReader::GetNextAlignmentCore() is called
\r
676 const unsigned int cigarDataOffset = bAlignment.SupportData.QueryNameLength;
\r
677 uint32_t* cigarData = (uint32_t*)(allCharData + cigarDataOffset);
\r
679 bAlignment.CigarData.clear();
\r
680 bAlignment.CigarData.reserve(bAlignment.SupportData.NumCigarOperations);
\r
681 for (unsigned int i = 0; i < bAlignment.SupportData.NumCigarOperations; ++i) {
\r
683 // swap if necessary
\r
684 if ( IsBigEndian ) SwapEndian_32(cigarData[i]);
\r
686 // build CigarOp structure
\r
687 op.Length = (cigarData[i] >> BAM_CIGAR_SHIFT);
\r
688 op.Type = CIGAR_LOOKUP[ (cigarData[i] & BAM_CIGAR_MASK) ];
\r
691 bAlignment.CigarData.push_back(op);
\r
696 return readCharDataOK;
\r
699 // loads reference data from BAM file
\r
700 void BamReader::BamReaderPrivate::LoadReferenceData(void) {
\r
702 // get number of reference sequences
\r
704 mBGZF.Read(buffer, 4);
\r
705 unsigned int numberRefSeqs = BgzfData::UnpackUnsignedInt(buffer);
\r
706 if ( IsBigEndian ) { SwapEndian_32(numberRefSeqs); }
\r
707 if (numberRefSeqs == 0) { return; }
\r
708 References.reserve((int)numberRefSeqs);
\r
710 // iterate over all references in header
\r
711 for (unsigned int i = 0; i != numberRefSeqs; ++i) {
\r
713 // get length of reference name
\r
714 mBGZF.Read(buffer, 4);
\r
715 unsigned int refNameLength = BgzfData::UnpackUnsignedInt(buffer);
\r
716 if ( IsBigEndian ) { SwapEndian_32(refNameLength); }
\r
717 char* refName = (char*)calloc(refNameLength, 1);
\r
719 // get reference name and reference sequence length
\r
720 mBGZF.Read(refName, refNameLength);
\r
721 mBGZF.Read(buffer, 4);
\r
722 int refLength = BgzfData::UnpackSignedInt(buffer);
\r
723 if ( IsBigEndian ) { SwapEndian_32(refLength); }
\r
725 // store data for reference
\r
726 RefData aReference;
\r
727 aReference.RefName = (string)((const char*)refName);
\r
728 aReference.RefLength = refLength;
\r
729 References.push_back(aReference);
\r
731 // clean up calloc-ed temp variable
\r
736 // opens BAM file (and index)
\r
737 bool BamReader::BamReaderPrivate::Open(const string& filename, const string& indexFilename) {
\r
739 Filename = filename;
\r
740 IndexFilename = indexFilename;
\r
742 // open the BGZF file for reading, return false on failure
\r
743 if ( !mBGZF.Open(filename, "rb") )
\r
746 // retrieve header text & reference data
\r
748 LoadReferenceData();
\r
750 // store file offset of first alignment
\r
751 AlignmentsBeginOffset = mBGZF.Tell();
\r
753 // open index file & load index data (if exists)
\r
754 if ( !IndexFilename.empty() )
\r
761 // returns BAM file pointer to beginning of alignment data
\r
762 bool BamReader::BamReaderPrivate::Rewind(void) {
\r
764 // rewind to first alignment
\r
765 if ( !mBGZF.Seek(AlignmentsBeginOffset) ) return false;
\r
767 // retrieve first alignment data
\r
769 if ( !LoadNextAlignment(al) ) return false;
\r
771 // reset default region info using first alignment in file
\r
772 Region.LeftRefID = al.RefID;
\r
773 Region.LeftPosition = al.Position;
\r
774 Region.RightRefID = -1;
\r
775 Region.RightPosition = -1;
\r
776 IsLeftBoundSpecified = false;
\r
777 IsRightBoundSpecified = false;
\r
779 // rewind back to before first alignment
\r
780 // return success/fail of seek
\r
781 return mBGZF.Seek(AlignmentsBeginOffset);
\r
784 // sets a region of interest (with left & right bound reference/position)
\r
785 // attempts a Jump() to left bound as well
\r
786 // returns success/failure of Jump()
\r
787 bool BamReader::BamReaderPrivate::SetRegion(const BamRegion& region) {
\r
789 // save region of interest
\r
793 if ( region.LeftRefID >= 0 && region.LeftPosition >= 0 )
\r
794 IsLeftBoundSpecified = true;
\r
795 if ( region.RightRefID >= 0 && region.RightPosition >= 0 )
\r
796 IsRightBoundSpecified = true;
\r
798 // attempt jump to beginning of region, return success/fail of Jump()
\r
799 return Jump( Region.LeftRefID, Region.LeftPosition );
\r