1 // ***************************************************************************
2 // BamStandardIndex.h (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 24 June 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides index operations for the standardized BAM index format (".bai")
8 // ***************************************************************************
10 #ifndef BAM_STANDARD_INDEX_FORMAT_H
11 #define BAM_STANDARD_INDEX_FORMAT_H
17 // This file is not part of the BamTools API. It exists purely as an
18 // implementation detail. This header file may change from version to
19 // version without notice, or even be removed.
23 #include <api/BamAux.h>
24 #include <api/BamIndex.h>
33 // -----------------------------------------------------------------------------
34 // BamStandardIndex data structures
36 // defines start and end of a contiguous run of alignments
37 struct BaiAlignmentChunk {
44 BaiAlignmentChunk(const uint64_t& start = 0,
45 const uint64_t& stop = 0)
51 // comparison operator (for sorting)
53 bool operator<(const BaiAlignmentChunk& lhs, const BaiAlignmentChunk& rhs) {
54 return lhs.Start < rhs.Start;
57 // convenience typedef for a list of all alignment 'chunks' in a BAI bin
58 typedef std::vector<BaiAlignmentChunk> BaiAlignmentChunkVector;
60 // convenience typedef for a map of all BAI bins in a reference (ID => chunks)
61 typedef std::map<uint32_t, BaiAlignmentChunkVector> BaiBinMap;
63 // convenience typedef for a list of all 'linear offsets' in a reference
64 typedef std::vector<uint64_t> BaiLinearOffsetVector;
66 // contains all fields necessary for building, loading, & writing
67 // full BAI index data for a single reference
68 struct BaiReferenceEntry {
73 BaiLinearOffsetVector LinearOffsets;
76 BaiReferenceEntry(const int32_t& id = -1)
81 // provides (persistent) summary of BaiReferenceEntry's index data
82 struct BaiReferenceSummary {
87 uint64_t FirstBinFilePosition;
88 uint64_t FirstLinearOffsetFilePosition;
91 BaiReferenceSummary(void)
94 , FirstBinFilePosition(0)
95 , FirstLinearOffsetFilePosition(0)
99 // convenience typedef for describing a full BAI index file summary
100 typedef std::vector<BaiReferenceSummary> BaiFileSummary;
102 // end BamStandardIndex data structures
103 // -----------------------------------------------------------------------------
105 class BamStandardIndex : public BamIndex {
109 BamStandardIndex(Internal::BamReaderPrivate* reader);
110 ~BamStandardIndex(void);
112 // BamIndex implementation
114 // builds index from associated BAM file & writes out to index file
116 // returns whether reference has alignments or no
117 bool HasAlignments(const int& referenceID) const;
118 // attempts to use index data to jump to @region, returns success/fail
119 // a "successful" jump indicates no error, but not whether this region has data
120 // * thus, the method sets a flag to indicate whether there are alignments
121 // available after the jump position
122 bool Jump(const BamTools::BamRegion& region, bool* hasAlignmentsInRegion);
123 // loads existing data from file into memory
124 bool Load(const std::string& filename);
125 // change the index caching behavior
126 void SetCacheMode(const BamIndex::IndexCacheMode& mode);
128 // returns format's file extension
129 static const std::string Extension(void);
133 bool CheckMagicNumber(void);
134 void CloseFile(void);
135 bool IsFileOpen(void) const;
136 bool OpenFile(const std::string& filename, const char* mode);
137 bool Seek(const int64_t& position, const int& origin);
138 int64_t Tell(void) const;
140 // internal BAI index building methods
142 void ClearReferenceEntry(BaiReferenceEntry& refEntry);
143 void SaveAlignmentChunkToBin(BaiBinMap& binMap,
144 const uint32_t& currentBin,
145 const uint64_t& currentOffset,
146 const uint64_t& lastOffset);
147 void SaveLinearOffsetEntry(BaiLinearOffsetVector& offsets,
148 const int& alignmentStartPosition,
149 const int& alignmentStopPosition,
150 const uint64_t& lastOffset);
152 // internal random-access methods
154 bool AdjustRegion(const BamRegion& region, uint32_t& begin, uint32_t& end);
155 void CalculateCandidateBins(const uint32_t& begin,
157 std::set<uint16_t>& candidateBins);
158 bool CalculateCandidateOffsets(const BaiReferenceSummary& refSummary,
159 const uint64_t& minOffset,
160 std::set<uint16_t>& candidateBins,
161 std::vector<int64_t>& offsets);
162 uint64_t CalculateMinOffset(const BaiReferenceSummary& refSummary, const uint32_t& begin);
163 bool GetOffset(const BamRegion& region, int64_t& offset, bool* hasAlignmentsInRegion);
164 uint64_t LookupLinearOffset(const BaiReferenceSummary& refSummary, const int& index);
166 // internal BAI summary (create/load) methods
168 void ReserveForSummary(const int& numReferences);
169 void SaveBinsSummary(const int& refId, const int& numBins);
170 void SaveLinearOffsetsSummary(const int& refId, const int& numLinearOffsets);
171 bool SkipBins(const int& numBins);
172 bool SkipLinearOffsets(const int& numLinearOffsets);
173 bool SummarizeBins(BaiReferenceSummary& refSummary);
174 bool SummarizeIndexFile(void);
175 bool SummarizeLinearOffsets(BaiReferenceSummary& refSummary);
176 bool SummarizeReference(BaiReferenceSummary& refSummary);
178 // internal BAI full index input methods
180 bool ReadBinID(uint32_t& binId);
181 bool ReadBinIntoBuffer(uint32_t& binId, int32_t& numAlignmentChunks);
182 bool ReadIntoBuffer(const unsigned int& bytesRequested);
183 bool ReadLinearOffset(uint64_t& linearOffset);
184 bool ReadNumAlignmentChunks(int& numAlignmentChunks);
185 bool ReadNumBins(int& numBins);
186 bool ReadNumLinearOffsets(int& numLinearOffsets);
187 bool ReadNumReferences(int& numReferences);
189 // internal BAI full index output methods
191 void MergeAlignmentChunks(BaiAlignmentChunkVector& chunks);
192 void SortLinearOffsets(BaiLinearOffsetVector& linearOffsets);
193 bool WriteAlignmentChunk(const BaiAlignmentChunk& chunk);
194 bool WriteAlignmentChunks(BaiAlignmentChunkVector& chunks);
195 bool WriteBin(const uint32_t& binId, BaiAlignmentChunkVector& chunks);
196 bool WriteBins(const int& refId, BaiBinMap& bins);
197 bool WriteHeader(void);
198 bool WriteLinearOffsets(const int& refId, BaiLinearOffsetVector& linearOffsets);
199 bool WriteReferenceEntry(BaiReferenceEntry& refEntry);
205 BamIndex::IndexCacheMode m_cacheMode;
206 BaiFileSummary m_indexFileSummary;
210 unsigned int m_bufferLength;
214 // checks if the buffer is large enough to accomodate the requested size
215 static void CheckBufferSize(char*& buffer,
216 unsigned int& bufferLength,
217 const unsigned int& requestedBytes);
218 // checks if the buffer is large enough to accomodate the requested size
219 static void CheckBufferSize(unsigned char*& buffer,
220 unsigned int& bufferLength,
221 const unsigned int& requestedBytes);
224 static const int MAX_BIN;
225 static const int BAM_LIDX_SHIFT;
226 static const std::string BAI_EXTENSION;
227 static const char* const BAI_MAGIC;
228 static const int SIZEOF_ALIGNMENTCHUNK;
229 static const int SIZEOF_BINCORE;
230 static const int SIZEOF_LINEAROFFSET;
233 } // namespace Internal
234 } // namespace BamTools
236 #endif // BAM_STANDARD_INDEX_FORMAT_H