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::IsOpen(void) const { return d->mBGZF.IsOpen; }
\r
141 bool BamReader::Jump(int refID, int position) {
\r
142 d->Region.LeftRefID = refID;
\r
143 d->Region.LeftPosition = position;
\r
144 d->IsLeftBoundSpecified = true;
\r
145 d->IsRightBoundSpecified = false;
\r
146 return d->Jump(refID, position);
\r
148 bool BamReader::Open(const string& filename, const string& indexFilename) { return d->Open(filename, indexFilename); }
\r
149 bool BamReader::Rewind(void) { return d->Rewind(); }
\r
150 bool BamReader::SetRegion(const BamRegion& region) { return d->SetRegion(region); }
\r
151 bool BamReader::SetRegion(const int& leftRefID, const int& leftBound, const int& rightRefID, const int& rightBound) {
\r
152 return d->SetRegion( BamRegion(leftRefID, leftBound, rightRefID, rightBound) );
\r
155 // access alignment data
\r
156 bool BamReader::GetNextAlignment(BamAlignment& bAlignment) { return d->GetNextAlignment(bAlignment); }
\r
157 bool BamReader::GetNextAlignmentCore(BamAlignment& bAlignment) { return d->GetNextAlignmentCore(bAlignment); }
\r
159 // access auxiliary data
\r
160 const string BamReader::GetHeaderText(void) const { return d->HeaderText; }
\r
161 int BamReader::GetReferenceCount(void) const { return d->References.size(); }
\r
162 const RefVector& BamReader::GetReferenceData(void) const { return d->References; }
\r
163 int BamReader::GetReferenceID(const string& refName) const { return d->GetReferenceID(refName); }
\r
164 const std::string BamReader::GetFilename(void) const { return d->Filename; }
\r
166 // index operations
\r
167 bool BamReader::CreateIndex(bool useDefaultIndex) { return d->CreateIndex(useDefaultIndex); }
\r
169 // -----------------------------------------------------
\r
170 // BamReaderPrivate implementation
\r
171 // -----------------------------------------------------
\r
174 BamReader::BamReaderPrivate::BamReaderPrivate(BamReader* parent)
\r
176 , IsIndexLoaded(false)
\r
177 , AlignmentsBeginOffset(0)
\r
178 , IsLeftBoundSpecified(false)
\r
179 , IsRightBoundSpecified(false)
\r
180 , IsRegionSpecified(false)
\r
184 , DNA_LOOKUP("=ACMGRSVTWYHKDBN")
\r
185 , CIGAR_LOOKUP("MIDNSHP")
\r
187 IsBigEndian = SystemIsBigEndian();
\r
191 BamReader::BamReaderPrivate::~BamReaderPrivate(void) {
\r
195 bool BamReader::BamReaderPrivate::BuildCharData(BamAlignment& bAlignment) {
\r
197 // calculate character lengths/offsets
\r
198 const unsigned int dataLength = bAlignment.SupportData.BlockLength - BAM_CORE_SIZE;
\r
199 // const unsigned int cigarDataOffset = bAlignment.SupportData.QueryNameLength;
\r
200 const unsigned int seqDataOffset = bAlignment.SupportData.QueryNameLength + (bAlignment.SupportData.NumCigarOperations * 4);
\r
201 const unsigned int qualDataOffset = seqDataOffset + (bAlignment.SupportData.QuerySequenceLength+1)/2;
\r
202 const unsigned int tagDataOffset = qualDataOffset + bAlignment.SupportData.QuerySequenceLength;
\r
203 const unsigned int tagDataLength = dataLength - tagDataOffset;
\r
205 // set up char buffers
\r
206 const char* allCharData = bAlignment.SupportData.AllCharData.data();
\r
207 // uint32_t* cigarData = (uint32_t*)(allCharData + cigarDataOffset);
\r
208 const char* seqData = ((const char*)allCharData) + seqDataOffset;
\r
209 const char* qualData = ((const char*)allCharData) + qualDataOffset;
\r
210 char* tagData = ((char*)allCharData) + tagDataOffset;
\r
212 // store alignment name (relies on null char in name as terminator)
\r
213 bAlignment.Name.assign((const char*)(allCharData));
\r
215 // // save CigarOps
\r
217 // bAlignment.CigarData.clear();
\r
218 // bAlignment.CigarData.reserve(bAlignment.SupportData.NumCigarOperations);
\r
219 // for (unsigned int i = 0; i < bAlignment.SupportData.NumCigarOperations; ++i) {
\r
221 // // swap if necessary
\r
222 // if ( IsBigEndian ) { SwapEndian_32(cigarData[i]); }
\r
224 // // build CigarOp structure
\r
225 // op.Length = (cigarData[i] >> BAM_CIGAR_SHIFT);
\r
226 // op.Type = CIGAR_LOOKUP[ (cigarData[i] & BAM_CIGAR_MASK) ];
\r
229 // bAlignment.CigarData.push_back(op);
\r
232 // save query sequence
\r
233 bAlignment.QueryBases.clear();
\r
234 bAlignment.QueryBases.reserve(bAlignment.SupportData.QuerySequenceLength);
\r
235 for (unsigned int i = 0; i < bAlignment.SupportData.QuerySequenceLength; ++i) {
\r
236 char singleBase = DNA_LOOKUP[ ( ( seqData[(i/2)] >> (4*(1-(i%2)))) & 0xf ) ];
\r
237 bAlignment.QueryBases.append(1, singleBase);
\r
240 // save qualities, converting from numeric QV to 'FASTQ-style' ASCII character
\r
241 bAlignment.Qualities.clear();
\r
242 bAlignment.Qualities.reserve(bAlignment.SupportData.QuerySequenceLength);
\r
243 for (unsigned int i = 0; i < bAlignment.SupportData.QuerySequenceLength; ++i) {
\r
244 char singleQuality = (char)(qualData[i]+33);
\r
245 bAlignment.Qualities.append(1, singleQuality);
\r
248 // if QueryBases is empty (and this is a allowed case)
\r
249 if ( bAlignment.QueryBases.empty() )
\r
250 bAlignment.AlignedBases = bAlignment.QueryBases;
\r
252 // if QueryBases contains data, then build AlignedBases using CIGAR data
\r
255 // resize AlignedBases
\r
256 bAlignment.AlignedBases.clear();
\r
257 bAlignment.AlignedBases.reserve(bAlignment.SupportData.QuerySequenceLength);
\r
259 // iterate over CigarOps
\r
261 vector<CigarOp>::const_iterator cigarIter = bAlignment.CigarData.begin();
\r
262 vector<CigarOp>::const_iterator cigarEnd = bAlignment.CigarData.end();
\r
263 for ( ; cigarIter != cigarEnd; ++cigarIter ) {
\r
265 const CigarOp& op = (*cigarIter);
\r
270 bAlignment.AlignedBases.append(bAlignment.QueryBases.substr(k, op.Length)); // for 'M', 'I' - write bases
\r
274 k += op.Length; // for 'S' - soft clip, skip over query bases
\r
278 bAlignment.AlignedBases.append(op.Length, '-'); // for 'D' - write gap character
\r
282 bAlignment.AlignedBases.append( op.Length, '*' ); // for 'P' - write padding character
\r
286 bAlignment.AlignedBases.append( op.Length, 'N' ); // for 'N' - write N's, skip bases in original query sequence
\r
290 break; // for 'H' - hard clip, do nothing to AlignedBases, move to next op
\r
293 printf("ERROR: Invalid Cigar op type\n"); // shouldn't get here
\r
299 // -----------------------
\r
300 // Added: 3-25-2010 DB
\r
301 // Fixed: endian-correctness for tag data
\r
302 // -----------------------
\r
303 if ( IsBigEndian ) {
\r
305 while ( (unsigned int)i < tagDataLength ) {
\r
307 i += 2; // skip tag type (e.g. "RG", "NM", etc)
\r
308 uint8_t type = toupper(tagData[i]); // lower & upper case letters have same meaning
\r
309 ++i; // skip value type
\r
319 SwapEndian_16p(&tagData[i]);
\r
320 i += sizeof(uint16_t);
\r
325 SwapEndian_32p(&tagData[i]);
\r
326 i += sizeof(uint32_t);
\r
330 SwapEndian_64p(&tagData[i]);
\r
331 i += sizeof(uint64_t);
\r
336 while (tagData[i]) { ++i; }
\r
337 ++i; // increment one more for null terminator
\r
341 printf("ERROR: Invalid tag value type\n"); // shouldn't get here
\r
348 bAlignment.TagData.clear();
\r
349 bAlignment.TagData.resize(tagDataLength);
\r
350 memcpy((char*)bAlignment.TagData.data(), tagData, tagDataLength);
\r
352 // clear the core-only flag
\r
353 bAlignment.SupportData.HasCoreOnly = false;
\r
359 // clear index data structure
\r
360 void BamReader::BamReaderPrivate::ClearIndex(void) {
\r
365 // closes the BAM file
\r
366 void BamReader::BamReaderPrivate::Close(void) {
\r
368 // close BGZF file stream
\r
371 // clear out index data
\r
374 // clear out header data
\r
375 HeaderText.clear();
\r
377 // clear out region flags
\r
378 IsLeftBoundSpecified = false;
\r
379 IsRightBoundSpecified = false;
\r
380 IsRegionSpecified = false;
\r
383 // create BAM index from BAM file (keep structure in memory) and write to default index output file
\r
384 bool BamReader::BamReaderPrivate::CreateIndex(bool useDefaultIndex) {
\r
386 // clear out prior index data
\r
389 // create default index
\r
390 if ( useDefaultIndex )
\r
391 NewIndex = new BamDefaultIndex(&mBGZF, Parent, IsBigEndian);
\r
392 // create BamTools 'custom' index
\r
394 NewIndex = new BamToolsIndex(&mBGZF, Parent, IsBigEndian);
\r
397 ok &= NewIndex->Build();
\r
398 ok &= NewIndex->Write(Filename);
\r
400 // return success/fail
\r
404 // get next alignment (from specified region, if given)
\r
405 bool BamReader::BamReaderPrivate::GetNextAlignment(BamAlignment& bAlignment) {
\r
407 // if valid alignment found, attempt to parse char data, and return success/failure
\r
408 if ( GetNextAlignmentCore(bAlignment) )
\r
409 return BuildCharData(bAlignment);
\r
411 // no valid alignment found
\r
416 // retrieves next available alignment core data (returns success/fail)
\r
417 // ** DOES NOT parse any character data (read name, bases, qualities, tag data)
\r
418 // these can be accessed, if necessary, from the supportData
\r
419 // useful for operations requiring ONLY positional or other alignment-related information
\r
420 bool BamReader::BamReaderPrivate::GetNextAlignmentCore(BamAlignment& bAlignment) {
\r
422 // if valid alignment available
\r
423 if ( LoadNextAlignment(bAlignment) ) {
\r
425 // set core-only flag
\r
426 bAlignment.SupportData.HasCoreOnly = true;
\r
428 // if region not specified, return success
\r
429 if ( !IsLeftBoundSpecified ) return true;
\r
431 // determine region state (before, within, after)
\r
432 BamReader::BamReaderPrivate::RegionState state = IsOverlap(bAlignment);
\r
434 // if alignment lies after region, return false
\r
435 if ( state == AFTER_REGION )
\r
438 while ( state != WITHIN_REGION ) {
\r
439 // if no valid alignment available (likely EOF) return failure
\r
440 if ( !LoadNextAlignment(bAlignment) ) return false;
\r
441 // if alignment lies after region, return false (no available read within region)
\r
442 state = IsOverlap(bAlignment);
\r
443 if ( state == AFTER_REGION) return false;
\r
447 // return success (alignment found that overlaps region)
\r
451 // no valid alignment
\r
456 // returns RefID for given RefName (returns References.size() if not found)
\r
457 int BamReader::BamReaderPrivate::GetReferenceID(const string& refName) const {
\r
459 // retrieve names from reference data
\r
460 vector<string> refNames;
\r
461 RefVector::const_iterator refIter = References.begin();
\r
462 RefVector::const_iterator refEnd = References.end();
\r
463 for ( ; refIter != refEnd; ++refIter) {
\r
464 refNames.push_back( (*refIter).RefName );
\r
467 // return 'index-of' refName ( if not found, returns refNames.size() )
\r
468 return distance(refNames.begin(), find(refNames.begin(), refNames.end(), refName));
\r
471 // returns region state - whether alignment ends before, overlaps, or starts after currently specified region
\r
472 // this *internal* method should ONLY called when (at least) IsLeftBoundSpecified == true
\r
473 BamReader::BamReaderPrivate::RegionState BamReader::BamReaderPrivate::IsOverlap(BamAlignment& bAlignment) {
\r
475 // --------------------------------------------------
\r
476 // check alignment start against right bound cutoff
\r
478 // if full region of interest was given
\r
479 if ( IsRightBoundSpecified ) {
\r
481 // read starts on right bound reference, but AFTER right bound position
\r
482 if ( bAlignment.RefID == Region.RightRefID && bAlignment.Position > Region.RightPosition )
\r
483 return AFTER_REGION;
\r
485 // if read starts on reference AFTER right bound, return false
\r
486 if ( bAlignment.RefID > Region.RightRefID )
\r
487 return AFTER_REGION;
\r
490 // --------------------------------------------------------
\r
491 // no right bound given OR read starts before right bound
\r
492 // so, check if it overlaps left bound
\r
494 // if read starts on left bound reference AND after left boundary, return success
\r
495 if ( bAlignment.RefID == Region.LeftRefID && bAlignment.Position >= Region.LeftPosition)
\r
496 return WITHIN_REGION;
\r
498 // if read is on any reference sequence before left bound, return false
\r
499 if ( bAlignment.RefID < Region.LeftRefID )
\r
500 return BEFORE_REGION;
\r
502 // --------------------------------------------------------
\r
503 // read is on left bound reference, but starts before left bound position
\r
505 // if it overlaps, return WITHIN_REGION
\r
506 if ( bAlignment.GetEndPosition() >= Region.LeftPosition )
\r
507 return WITHIN_REGION;
\r
508 // else begins before left bound position
\r
510 return BEFORE_REGION;
\r
513 // jumps to specified region(refID, leftBound) in BAM file, returns success/fail
\r
514 bool BamReader::BamReaderPrivate::Jump(int refID, int position) {
\r
516 // -----------------------------------------------------------------------
\r
517 // check for existing index
\r
518 if ( NewIndex == 0 ) return false;
\r
519 // see if reference has alignments
\r
520 if ( !NewIndex->HasAlignments(refID) ) return false;
\r
521 // make sure position is valid
\r
522 if ( position > References.at(refID).RefLength ) return false;
\r
524 // determine possible offsets
\r
525 vector<int64_t> offsets;
\r
526 if ( !NewIndex->GetOffsets(Region, IsRightBoundSpecified, offsets) ) {
\r
527 printf("ERROR: Could not jump: unable to calculate offset for specified region.\n");
\r
531 // iterate through offsets
\r
532 BamAlignment bAlignment;
\r
533 bool result = true;
\r
534 for ( vector<int64_t>::const_iterator o = offsets.begin(); o != offsets.end(); ++o) {
\r
536 // attempt seek & load first available alignment
\r
537 result &= mBGZF.Seek(*o);
\r
538 LoadNextAlignment(bAlignment);
\r
540 // if this alignment corresponds to desired position
\r
541 // return success of seeking back to 'current offset'
\r
542 if ( (bAlignment.RefID == refID && bAlignment.Position + bAlignment.Length > position) || (bAlignment.RefID > refID) ) {
\r
543 if ( o != offsets.begin() ) --o;
\r
544 return mBGZF.Seek(*o);
\r
551 // load BAM header data
\r
552 void BamReader::BamReaderPrivate::LoadHeaderData(void) {
\r
554 // check to see if proper BAM header
\r
556 if (mBGZF.Read(buffer, 4) != 4) {
\r
557 printf("Could not read header type\n");
\r
561 if (strncmp(buffer, "BAM\001", 4)) {
\r
562 printf("wrong header type!\n");
\r
566 // get BAM header text length
\r
567 mBGZF.Read(buffer, 4);
\r
568 unsigned int headerTextLength = BgzfData::UnpackUnsignedInt(buffer);
\r
569 if ( IsBigEndian ) { SwapEndian_32(headerTextLength); }
\r
571 // get BAM header text
\r
572 char* headerText = (char*)calloc(headerTextLength + 1, 1);
\r
573 mBGZF.Read(headerText, headerTextLength);
\r
574 HeaderText = (string)((const char*)headerText);
\r
576 // clean up calloc-ed temp variable
\r
580 // load existing index data from BAM index file (".bai"), return success/fail
\r
581 bool BamReader::BamReaderPrivate::LoadIndex(void) {
\r
583 // clear out any existing index data
\r
586 // skip if index file empty
\r
587 if ( IndexFilename.empty() )
\r
590 // check supplied filename for index type
\r
591 size_t defaultExtensionFound = IndexFilename.find(".bai");
\r
592 size_t customExtensionFound = IndexFilename.find(".bti");
\r
594 // if SAM/BAM default (".bai")
\r
595 if ( defaultExtensionFound != string::npos )
\r
596 NewIndex = new BamDefaultIndex(&mBGZF, Parent, IsBigEndian);
\r
598 // if BamTools custom index (".bti")
\r
599 else if ( customExtensionFound != string::npos )
\r
600 NewIndex = new BamToolsIndex(&mBGZF, Parent, IsBigEndian);
\r
604 printf("ERROR: Unknown index file extension.\n");
\r
608 // return success of loading index data
\r
609 return NewIndex->Load(IndexFilename);
\r
612 // populates BamAlignment with alignment data under file pointer, returns success/fail
\r
613 bool BamReader::BamReaderPrivate::LoadNextAlignment(BamAlignment& bAlignment) {
\r
615 // read in the 'block length' value, make sure it's not zero
\r
617 mBGZF.Read(buffer, 4);
\r
618 bAlignment.SupportData.BlockLength = BgzfData::UnpackUnsignedInt(buffer);
\r
619 if ( IsBigEndian ) { SwapEndian_32(bAlignment.SupportData.BlockLength); }
\r
620 if ( bAlignment.SupportData.BlockLength == 0 ) { return false; }
\r
622 // read in core alignment data, make sure the right size of data was read
\r
623 char x[BAM_CORE_SIZE];
\r
624 if ( mBGZF.Read(x, BAM_CORE_SIZE) != BAM_CORE_SIZE ) { return false; }
\r
626 if ( IsBigEndian ) {
\r
627 for ( int i = 0; i < BAM_CORE_SIZE; i+=sizeof(uint32_t) ) {
\r
628 SwapEndian_32p(&x[i]);
\r
632 // set BamAlignment 'core' and 'support' data
\r
633 bAlignment.RefID = BgzfData::UnpackSignedInt(&x[0]);
\r
634 bAlignment.Position = BgzfData::UnpackSignedInt(&x[4]);
\r
636 unsigned int tempValue = BgzfData::UnpackUnsignedInt(&x[8]);
\r
637 bAlignment.Bin = tempValue >> 16;
\r
638 bAlignment.MapQuality = tempValue >> 8 & 0xff;
\r
639 bAlignment.SupportData.QueryNameLength = tempValue & 0xff;
\r
641 tempValue = BgzfData::UnpackUnsignedInt(&x[12]);
\r
642 bAlignment.AlignmentFlag = tempValue >> 16;
\r
643 bAlignment.SupportData.NumCigarOperations = tempValue & 0xffff;
\r
645 bAlignment.SupportData.QuerySequenceLength = BgzfData::UnpackUnsignedInt(&x[16]);
\r
646 bAlignment.MateRefID = BgzfData::UnpackSignedInt(&x[20]);
\r
647 bAlignment.MatePosition = BgzfData::UnpackSignedInt(&x[24]);
\r
648 bAlignment.InsertSize = BgzfData::UnpackSignedInt(&x[28]);
\r
650 // set BamAlignment length
\r
651 bAlignment.Length = bAlignment.SupportData.QuerySequenceLength;
\r
653 // read in character data - make sure proper data size was read
\r
654 bool readCharDataOK = false;
\r
655 const unsigned int dataLength = bAlignment.SupportData.BlockLength - BAM_CORE_SIZE;
\r
656 char* allCharData = (char*)calloc(sizeof(char), dataLength);
\r
658 if ( mBGZF.Read(allCharData, dataLength) == (signed int)dataLength) {
\r
660 // store 'allCharData' in supportData structure
\r
661 bAlignment.SupportData.AllCharData.assign((const char*)allCharData, dataLength);
\r
663 // set success flag
\r
664 readCharDataOK = true;
\r
667 // need to calculate this here so that BamAlignment::GetEndPosition() performs correctly,
\r
668 // even when BamReader::GetNextAlignmentCore() is called
\r
669 const unsigned int cigarDataOffset = bAlignment.SupportData.QueryNameLength;
\r
670 uint32_t* cigarData = (uint32_t*)(allCharData + cigarDataOffset);
\r
672 bAlignment.CigarData.clear();
\r
673 bAlignment.CigarData.reserve(bAlignment.SupportData.NumCigarOperations);
\r
674 for (unsigned int i = 0; i < bAlignment.SupportData.NumCigarOperations; ++i) {
\r
676 // swap if necessary
\r
677 if ( IsBigEndian ) SwapEndian_32(cigarData[i]);
\r
679 // build CigarOp structure
\r
680 op.Length = (cigarData[i] >> BAM_CIGAR_SHIFT);
\r
681 op.Type = CIGAR_LOOKUP[ (cigarData[i] & BAM_CIGAR_MASK) ];
\r
684 bAlignment.CigarData.push_back(op);
\r
689 return readCharDataOK;
\r
692 // loads reference data from BAM file
\r
693 void BamReader::BamReaderPrivate::LoadReferenceData(void) {
\r
695 // get number of reference sequences
\r
697 mBGZF.Read(buffer, 4);
\r
698 unsigned int numberRefSeqs = BgzfData::UnpackUnsignedInt(buffer);
\r
699 if ( IsBigEndian ) { SwapEndian_32(numberRefSeqs); }
\r
700 if (numberRefSeqs == 0) { return; }
\r
701 References.reserve((int)numberRefSeqs);
\r
703 // iterate over all references in header
\r
704 for (unsigned int i = 0; i != numberRefSeqs; ++i) {
\r
706 // get length of reference name
\r
707 mBGZF.Read(buffer, 4);
\r
708 unsigned int refNameLength = BgzfData::UnpackUnsignedInt(buffer);
\r
709 if ( IsBigEndian ) { SwapEndian_32(refNameLength); }
\r
710 char* refName = (char*)calloc(refNameLength, 1);
\r
712 // get reference name and reference sequence length
\r
713 mBGZF.Read(refName, refNameLength);
\r
714 mBGZF.Read(buffer, 4);
\r
715 int refLength = BgzfData::UnpackSignedInt(buffer);
\r
716 if ( IsBigEndian ) { SwapEndian_32(refLength); }
\r
718 // store data for reference
\r
719 RefData aReference;
\r
720 aReference.RefName = (string)((const char*)refName);
\r
721 aReference.RefLength = refLength;
\r
722 References.push_back(aReference);
\r
724 // clean up calloc-ed temp variable
\r
729 // opens BAM file (and index)
\r
730 bool BamReader::BamReaderPrivate::Open(const string& filename, const string& indexFilename) {
\r
732 Filename = filename;
\r
733 IndexFilename = indexFilename;
\r
735 // open the BGZF file for reading, return false on failure
\r
736 if ( !mBGZF.Open(filename, "rb") )
\r
739 // retrieve header text & reference data
\r
741 LoadReferenceData();
\r
743 // store file offset of first alignment
\r
744 AlignmentsBeginOffset = mBGZF.Tell();
\r
746 // open index file & load index data (if exists)
\r
747 if ( !IndexFilename.empty() )
\r
754 // returns BAM file pointer to beginning of alignment data
\r
755 bool BamReader::BamReaderPrivate::Rewind(void) {
\r
757 // rewind to first alignment
\r
758 if ( !mBGZF.Seek(AlignmentsBeginOffset) ) return false;
\r
760 // retrieve first alignment data
\r
762 if ( !LoadNextAlignment(al) ) return false;
\r
764 // reset default region info using first alignment in file
\r
765 Region.LeftRefID = al.RefID;
\r
766 Region.LeftPosition = al.Position;
\r
767 Region.RightRefID = -1;
\r
768 Region.RightPosition = -1;
\r
769 IsLeftBoundSpecified = false;
\r
770 IsRightBoundSpecified = false;
\r
772 // rewind back to before first alignment
\r
773 // return success/fail of seek
\r
774 return mBGZF.Seek(AlignmentsBeginOffset);
\r
777 // sets a region of interest (with left & right bound reference/position)
\r
778 // attempts a Jump() to left bound as well
\r
779 // returns success/failure of Jump()
\r
780 bool BamReader::BamReaderPrivate::SetRegion(const BamRegion& region) {
\r
782 // save region of interest
\r
786 if ( region.LeftRefID >= 0 && region.LeftPosition >= 0 )
\r
787 IsLeftBoundSpecified = true;
\r
788 if ( region.RightRefID >= 0 && region.RightPosition >= 0 )
\r
789 IsRightBoundSpecified = true;
\r
791 // attempt jump to beginning of region, return success/fail of Jump()
\r
792 return Jump( Region.LeftRefID, Region.LeftPosition );
\r