1 // ***************************************************************************
2 // BamToolsIndex.cpp (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 19 November 2010 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides index operations for the BamTools index format (".bti")
9 // ***************************************************************************
11 #include <api/BamAlignment.h>
12 #include <api/BamReader.h>
13 #include <api/BamToolsIndex.h>
15 using namespace BamTools;
16 using namespace BamTools::Internal;
25 BamToolsIndex::BamToolsIndex(BgzfData* bgzf, BamReader* reader)
26 : BamIndex(bgzf, reader)
28 , m_dataBeginOffset(0)
29 , m_hasFullDataCache(false)
31 , m_outputVersion(BTI_1_2) // latest version - used for writing new index files
33 m_isBigEndian = BamTools::SystemIsBigEndian();
37 BamToolsIndex::~BamToolsIndex(void) {
41 // creates index data (in-memory) from current reader data
42 bool BamToolsIndex::Build(void) {
44 // be sure reader & BGZF file are valid & open for reading
45 if ( m_reader == 0 || m_BGZF == 0 || !m_BGZF->IsOpen )
48 // move file pointer to beginning of alignments
49 if ( !m_reader->Rewind() ) return false;
51 // initialize index data structure with space for all references
52 const int numReferences = (int)m_references.size();
54 m_hasFullDataCache = false;
55 SetReferenceCount(numReferences);
57 // set up counters and markers
58 int32_t currentBlockCount = 0;
59 int64_t currentAlignmentOffset = m_BGZF->Tell();
60 int32_t blockRefId = 0;
61 int32_t blockMaxEndPosition = 0;
62 int64_t blockStartOffset = currentAlignmentOffset;
63 int32_t blockStartPosition = -1;
65 // plow through alignments, storing index entries
67 while ( m_reader->GetNextAlignmentCore(al) ) {
69 // if block contains data (not the first time through) AND alignment is on a new reference
70 if ( currentBlockCount > 0 && al.RefID != blockRefId ) {
72 // store previous data
73 BamToolsIndexEntry entry(blockMaxEndPosition, blockStartOffset, blockStartPosition);
74 SaveOffsetEntry(blockRefId, entry);
76 // intialize new block for current alignment's reference
77 currentBlockCount = 0;
78 blockMaxEndPosition = al.GetEndPosition();
79 blockStartOffset = currentAlignmentOffset;
82 // if beginning of block, save first alignment's refID & position
83 if ( currentBlockCount == 0 ) {
84 blockRefId = al.RefID;
85 blockStartPosition = al.Position;
88 // increment block counter
92 int32_t alignmentEndPosition = al.GetEndPosition();
93 if ( alignmentEndPosition > blockMaxEndPosition )
94 blockMaxEndPosition = alignmentEndPosition;
96 // if block is full, get offset for next block, reset currentBlockCount
97 if ( currentBlockCount == m_blockSize ) {
98 BamToolsIndexEntry entry(blockMaxEndPosition, blockStartOffset, blockStartPosition);
99 SaveOffsetEntry(blockRefId, entry);
100 blockStartOffset = m_BGZF->Tell();
101 currentBlockCount = 0;
104 // not the best name, but for the next iteration, this value will be the offset of the *current* alignment
105 // necessary because we won't know if this next alignment is on a new reference until we actually read it
106 currentAlignmentOffset = m_BGZF->Tell();
109 // store final block with data
110 BamToolsIndexEntry entry(blockMaxEndPosition, blockStartOffset, blockStartPosition);
111 SaveOffsetEntry(blockRefId, entry);
114 m_hasFullDataCache = true;
116 // return success/failure of rewind
117 return m_reader->Rewind();
120 // check index file magic number, return true if OK
121 bool BamToolsIndex::CheckMagicNumber(void) {
123 // see if index is valid BAM index
125 size_t elementsRead = fread(magic, 1, 4, m_indexStream);
126 if ( elementsRead != 4 ) return false;
127 if ( strncmp(magic, "BTI\1", 4) != 0 ) {
128 fprintf(stderr, "Problem with index file - invalid format.\n");
136 // check index file version, return true if OK
137 bool BamToolsIndex::CheckVersion(void) {
139 // read version from file
140 size_t elementsRead = fread(&m_inputVersion, sizeof(m_inputVersion), 1, m_indexStream);
141 if ( elementsRead != 1 ) return false;
142 if ( m_isBigEndian ) SwapEndian_32(m_inputVersion);
144 // if version is negative, or zero
145 if ( m_inputVersion <= 0 ) {
146 fprintf(stderr, "Problem with index file - invalid version.\n");
150 // if version is newer than can be supported by this version of bamtools
151 else if ( m_inputVersion > m_outputVersion ) {
152 fprintf(stderr, "Problem with index file - attempting to use an outdated version of BamTools with a newer index file.\n");
153 fprintf(stderr, "Please update BamTools to a more recent version to support this index file.\n");
157 // ------------------------------------------------------------------
158 // check for deprecated, unsupported versions
159 // (typically whose format did not accomodate a particular bug fix)
161 else if ( (Version)m_inputVersion == BTI_1_0 ) {
162 fprintf(stderr, "\nProblem with index file - this version of the index contains a bug related to accessing data near reference ends.\n");
163 fprintf(stderr, "\nPlease run \'bamtools index -bti -in yourData.bam\' to generate an up-to-date BamToolsIndex.\n\n");
167 else if ( (Version)m_inputVersion == BTI_1_1 ) {
168 fprintf(stderr, "\nProblem with index file - this version of the index contains a bug related to handling empty references.\n");
169 fprintf(stderr, "\nPlease run \'bamtools index -bti -in yourData.bam\' to generate an up-to-date BamToolsIndex.\n\n");
177 // clear all current index offset data in memory
178 void BamToolsIndex::ClearAllData(void) {
179 BamToolsIndexData::const_iterator indexIter = m_indexData.begin();
180 BamToolsIndexData::const_iterator indexEnd = m_indexData.end();
181 for ( ; indexIter != indexEnd; ++indexIter ) {
182 const int& refId = (*indexIter).first;
183 ClearReferenceOffsets(refId);
187 // clear all index offset data for desired reference
188 void BamToolsIndex::ClearReferenceOffsets(const int& refId) {
189 if ( m_indexData.find(refId) == m_indexData.end() ) return;
190 vector<BamToolsIndexEntry>& offsets = m_indexData[refId].Offsets;
192 m_hasFullDataCache = false;
195 // return file position after header metadata
196 const off_t BamToolsIndex::DataBeginOffset(void) const {
197 return m_dataBeginOffset;
200 // calculate BAM file offset for desired region
201 // return true if no error (*NOT* equivalent to "has alignments or valid offset")
202 // check @hasAlignmentsInRegion to determine this status
203 // @region - target region
204 // @offset - resulting seek target
205 // @hasAlignmentsInRegion - sometimes a file just lacks data in region, this flag indicates that status
206 // N.B. - ignores isRightBoundSpecified
207 bool BamToolsIndex::GetOffset(const BamRegion& region, int64_t& offset, bool* hasAlignmentsInRegion) {
209 // return false if leftBound refID is not found in index data
210 BamToolsIndexData::const_iterator indexIter = m_indexData.find(region.LeftRefID);
211 if ( indexIter == m_indexData.end()) return false;
213 // load index data for region if not already cached
214 if ( !IsDataLoaded(region.LeftRefID) ) {
215 bool loadedOk = true;
216 loadedOk &= SkipToReference(region.LeftRefID);
217 loadedOk &= LoadReference(region.LeftRefID);
218 if ( !loadedOk ) return false;
221 // localize index data for this reference (& sanity check that data actually exists)
222 indexIter = m_indexData.find(region.LeftRefID);
223 if ( indexIter == m_indexData.end()) return false;
224 const vector<BamToolsIndexEntry>& referenceOffsets = (*indexIter).second.Offsets;
225 if ( referenceOffsets.empty() ) return false;
227 // -------------------------------------------------------
228 // calculate nearest index to jump to
231 offset = (*referenceOffsets.begin()).StartOffset;
233 // iterate over offsets entries on this reference
234 vector<BamToolsIndexEntry>::const_iterator offsetIter = referenceOffsets.begin();
235 vector<BamToolsIndexEntry>::const_iterator offsetEnd = referenceOffsets.end();
236 for ( ; offsetIter != offsetEnd; ++offsetIter ) {
237 const BamToolsIndexEntry& entry = (*offsetIter);
238 // break if alignment 'entry' overlaps region
239 if ( entry.MaxEndPosition >= region.LeftPosition ) break;
240 offset = (*offsetIter).StartOffset;
243 // set flag based on whether an index entry was found for this region
244 *hasAlignmentsInRegion = ( offsetIter != offsetEnd );
246 // if cache mode set to none, dump the data we just loaded
247 if (m_cacheMode == BamIndex::NoIndexCaching )
248 ClearReferenceOffsets(region.LeftRefID);
254 // returns whether reference has alignments or no
255 bool BamToolsIndex::HasAlignments(const int& refId) const {
257 BamToolsIndexData::const_iterator indexIter = m_indexData.find(refId);
258 if ( indexIter == m_indexData.end()) return false;
259 const BamToolsReferenceEntry& refEntry = (*indexIter).second;
260 return refEntry.HasAlignments;
263 // return true if all index data is cached
264 bool BamToolsIndex::HasFullDataCache(void) const {
265 return m_hasFullDataCache;
268 // returns true if index cache has data for desired reference
269 bool BamToolsIndex::IsDataLoaded(const int& refId) const {
271 BamToolsIndexData::const_iterator indexIter = m_indexData.find(refId);
272 if ( indexIter == m_indexData.end()) return false;
273 const BamToolsReferenceEntry& refEntry = (*indexIter).second;
275 if ( !refEntry.HasAlignments ) return true; // no data period
277 // return whether offsets list contains data
278 return !refEntry.Offsets.empty();
281 // attempts to use index to jump to region; returns success/fail
282 bool BamToolsIndex::Jump(const BamRegion& region, bool* hasAlignmentsInRegion) {
285 *hasAlignmentsInRegion = false;
287 // check valid BamReader state
288 if ( m_reader == 0 || m_BGZF == 0 || !m_reader->IsOpen() ) {
289 fprintf(stderr, "ERROR: Could not jump: invalid BamReader state.\n");
293 // make sure left-bound position is valid
294 if ( region.LeftPosition > m_references.at(region.LeftRefID).RefLength )
297 // calculate nearest offset to jump to
299 if ( !GetOffset(region, offset, hasAlignmentsInRegion) ) {
300 fprintf(stderr, "ERROR: Could not jump - unable to calculate offset for specified region.\n");
304 // return success/failure of seek
305 return m_BGZF->Seek(offset);
308 // clears index data from all references except the first
309 void BamToolsIndex::KeepOnlyFirstReferenceOffsets(void) {
310 BamToolsIndexData::const_iterator indexBegin = m_indexData.begin();
311 KeepOnlyReferenceOffsets( (*indexBegin).first );
314 // clears index data from all references except the one specified
315 void BamToolsIndex::KeepOnlyReferenceOffsets(const int& refId) {
316 BamToolsIndexData::iterator mapIter = m_indexData.begin();
317 BamToolsIndexData::iterator mapEnd = m_indexData.end();
318 for ( ; mapIter != mapEnd; ++mapIter ) {
319 const int entryRefId = (*mapIter).first;
320 if ( entryRefId != refId )
321 ClearReferenceOffsets(entryRefId);
325 // load index data for all references, return true if loaded OK
326 bool BamToolsIndex::LoadAllReferences(bool saveData) {
328 // skip if data already loaded
329 if ( m_hasFullDataCache ) return true;
331 // read in number of references
332 int32_t numReferences;
333 if ( !LoadReferenceCount(numReferences) ) return false;
334 //SetReferenceCount(numReferences);
336 // iterate over reference entries
337 bool loadedOk = true;
338 for ( int i = 0; i < numReferences; ++i )
339 loadedOk &= LoadReference(i, saveData);
342 if ( loadedOk && saveData )
343 m_hasFullDataCache = true;
345 // return success/failure of load
349 // load header data from index file, return true if loaded OK
350 bool BamToolsIndex::LoadHeader(void) {
352 // check magic number
353 if ( !CheckMagicNumber() ) return false;
356 if ( !CheckVersion() ) return false;
358 // read in block size
359 size_t elementsRead = fread(&m_blockSize, sizeof(m_blockSize), 1, m_indexStream);
360 if ( elementsRead != 1 ) return false;
361 if ( m_isBigEndian ) SwapEndian_32(m_blockSize);
363 // store offset of beginning of data
364 m_dataBeginOffset = ftell64(m_indexStream);
366 // return success/failure of load
367 return (elementsRead == 1);
370 // load a single index entry from file, return true if loaded OK
371 // @saveData - save data in memory if true, just read & discard if false
372 bool BamToolsIndex::LoadIndexEntry(const int& refId, bool saveData) {
374 // read in index entry data members
375 size_t elementsRead = 0;
376 BamToolsIndexEntry entry;
377 elementsRead += fread(&entry.MaxEndPosition, sizeof(entry.MaxEndPosition), 1, m_indexStream);
378 elementsRead += fread(&entry.StartOffset, sizeof(entry.StartOffset), 1, m_indexStream);
379 elementsRead += fread(&entry.StartPosition, sizeof(entry.StartPosition), 1, m_indexStream);
380 if ( elementsRead != 3 ) {
381 cerr << "Error reading index entry. Expected 3 elements, read in: " << elementsRead << endl;
385 // swap endian-ness if necessary
386 if ( m_isBigEndian ) {
387 SwapEndian_32(entry.MaxEndPosition);
388 SwapEndian_64(entry.StartOffset);
389 SwapEndian_32(entry.StartPosition);
394 SaveOffsetEntry(refId, entry);
396 // return success/failure of load
400 // load a single reference from file, return true if loaded OK
401 // @saveData - save data in memory if true, just read & discard if false
402 bool BamToolsIndex::LoadFirstReference(bool saveData) {
403 BamToolsIndexData::const_iterator indexBegin = m_indexData.begin();
404 return LoadReference( (*indexBegin).first, saveData );
407 // load a single reference from file, return true if loaded OK
408 // @saveData - save data in memory if true, just read & discard if false
409 bool BamToolsIndex::LoadReference(const int& refId, bool saveData) {
411 // read in number of offsets for this reference
413 size_t elementsRead = fread(&numOffsets, sizeof(numOffsets), 1, m_indexStream);
414 if ( elementsRead != 1 ) return false;
415 if ( m_isBigEndian ) SwapEndian_32(numOffsets);
417 // initialize offsets container for this reference
418 SetOffsetCount(refId, (int)numOffsets);
420 // iterate over offset entries
421 for ( unsigned int j = 0; j < numOffsets; ++j )
422 LoadIndexEntry(refId, saveData);
424 // return success/failure of load
428 // loads number of references, return true if loaded OK
429 bool BamToolsIndex::LoadReferenceCount(int& numReferences) {
431 size_t elementsRead = 0;
433 // read reference count
434 elementsRead += fread(&numReferences, sizeof(numReferences), 1, m_indexStream);
435 if ( m_isBigEndian ) SwapEndian_32(numReferences);
437 // return success/failure of load
438 return ( elementsRead == 1 );
441 // saves an index offset entry in memory
442 void BamToolsIndex::SaveOffsetEntry(const int& refId, const BamToolsIndexEntry& entry) {
443 BamToolsReferenceEntry& refEntry = m_indexData[refId];
444 refEntry.HasAlignments = true;
445 refEntry.Offsets.push_back(entry);
448 // pre-allocates size for offset vector
449 void BamToolsIndex::SetOffsetCount(const int& refId, const int& offsetCount) {
450 BamToolsReferenceEntry& refEntry = m_indexData[refId];
451 refEntry.Offsets.reserve(offsetCount);
452 refEntry.HasAlignments = ( offsetCount > 0);
455 // initializes index data structure to hold @count references
456 void BamToolsIndex::SetReferenceCount(const int& count) {
457 for ( int i = 0; i < count; ++i )
458 m_indexData[i].HasAlignments = false;
461 // position file pointer to first reference begin, return true if skipped OK
462 bool BamToolsIndex::SkipToFirstReference(void) {
463 BamToolsIndexData::const_iterator indexBegin = m_indexData.begin();
464 return SkipToReference( (*indexBegin).first );
467 // position file pointer to desired reference begin, return true if skipped OK
468 bool BamToolsIndex::SkipToReference(const int& refId) {
471 if ( !Rewind() ) return false;
473 // read in number of references
474 int32_t numReferences;
475 size_t elementsRead = fread(&numReferences, sizeof(numReferences), 1, m_indexStream);
476 if ( elementsRead != 1 ) return false;
477 if ( m_isBigEndian ) SwapEndian_32(numReferences);
479 // iterate over reference entries
480 bool skippedOk = true;
481 int currentRefId = 0;
482 while (currentRefId != refId) {
483 skippedOk &= LoadReference(currentRefId, false);
487 // return success/failure of skip
491 // write header to new index file
492 bool BamToolsIndex::WriteHeader(void) {
494 size_t elementsWritten = 0;
496 // write BTI index format 'magic number'
497 elementsWritten += fwrite("BTI\1", 1, 4, m_indexStream);
499 // write BTI index format version
500 int32_t currentVersion = (int32_t)m_outputVersion;
501 if ( m_isBigEndian ) SwapEndian_32(currentVersion);
502 elementsWritten += fwrite(¤tVersion, sizeof(currentVersion), 1, m_indexStream);
505 int32_t blockSize = m_blockSize;
506 if ( m_isBigEndian ) SwapEndian_32(blockSize);
507 elementsWritten += fwrite(&blockSize, sizeof(blockSize), 1, m_indexStream);
509 // store offset of beginning of data
510 m_dataBeginOffset = ftell64(m_indexStream);
512 // return success/failure of write
513 return ( elementsWritten == 6 );
516 // write index data for all references to new index file
517 bool BamToolsIndex::WriteAllReferences(void) {
519 size_t elementsWritten = 0;
521 // write number of references
522 int32_t numReferences = (int32_t)m_indexData.size();
523 if ( m_isBigEndian ) SwapEndian_32(numReferences);
524 elementsWritten += fwrite(&numReferences, sizeof(numReferences), 1, m_indexStream);
526 // iterate through references in index
528 BamToolsIndexData::const_iterator refIter = m_indexData.begin();
529 BamToolsIndexData::const_iterator refEnd = m_indexData.end();
530 for ( ; refIter != refEnd; ++refIter )
531 refOk &= WriteReferenceEntry( (*refIter).second );
533 return ( (elementsWritten == 1) && refOk );
536 // write current reference index data to new index file
537 bool BamToolsIndex::WriteReferenceEntry(const BamToolsReferenceEntry& refEntry) {
539 size_t elementsWritten = 0;
541 // write number of offsets listed for this reference
542 uint32_t numOffsets = refEntry.Offsets.size();
543 if ( m_isBigEndian ) SwapEndian_32(numOffsets);
544 elementsWritten += fwrite(&numOffsets, sizeof(numOffsets), 1, m_indexStream);
546 // iterate over offset entries
547 bool entriesOk = true;
548 vector<BamToolsIndexEntry>::const_iterator offsetIter = refEntry.Offsets.begin();
549 vector<BamToolsIndexEntry>::const_iterator offsetEnd = refEntry.Offsets.end();
550 for ( ; offsetIter != offsetEnd; ++offsetIter )
551 entriesOk &= WriteIndexEntry( (*offsetIter) );
553 return ( (elementsWritten == 1) && entriesOk );
556 // write current index offset entry to new index file
557 bool BamToolsIndex::WriteIndexEntry(const BamToolsIndexEntry& entry) {
560 int32_t maxEndPosition = entry.MaxEndPosition;
561 int64_t startOffset = entry.StartOffset;
562 int32_t startPosition = entry.StartPosition;
564 // swap endian-ness if necessary
565 if ( m_isBigEndian ) {
566 SwapEndian_32(maxEndPosition);
567 SwapEndian_64(startOffset);
568 SwapEndian_32(startPosition);
571 // write the reference index entry
572 size_t elementsWritten = 0;
573 elementsWritten += fwrite(&maxEndPosition, sizeof(maxEndPosition), 1, m_indexStream);
574 elementsWritten += fwrite(&startOffset, sizeof(startOffset), 1, m_indexStream);
575 elementsWritten += fwrite(&startPosition, sizeof(startPosition), 1, m_indexStream);
576 return ( elementsWritten == 3 );