BamIndex.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __BAM_INDEX_H__
00019 #define __BAM_INDEX_H__
00020
00021 #include <stdint.h>
00022 #include <vector>
00023 #include <map>
00024 #include <stdlib.h>
00025
00026 #include "InputFile.h"
00027 #include "SamStatus.h"
00028
00029 class Chunk
00030 {
00031 public:
00032 uint64_t chunk_beg;
00033 uint64_t chunk_end;
00034
00035 static const uint64_t MAX_CHUNK_VALUE = 0xFFFFFFFFFFFFFFFFULL;
00036
00037 bool operator< (const Chunk& otherChunk) const
00038 {
00039 return(this->chunk_beg < otherChunk.chunk_beg);
00040 }
00041 };
00042
00043
00044
00045
00046
00047 class SortedChunkList
00048 {
00049 public:
00050
00051 Chunk pop();
00052 bool insert(const Chunk& chunkToInsert);
00053 void clear();
00054 bool empty();
00055 bool mergeOverlapping();
00056
00057 private:
00058 std::map<uint64_t, Chunk> chunkList;
00059 };
00060
00061 class BamIndex
00062 {
00063 public:
00064
00065 BamIndex();
00066 ~BamIndex();
00067
00068
00069 void resetIndex();
00070
00071
00072
00073
00074 SamStatus::Status readIndex(const char* filename);
00075
00076
00077
00078
00079
00080 bool getChunksForRegion(int32_t refID, int32_t start, int32_t end,
00081 SortedChunkList& chunkList);
00082
00083 uint64_t getMaxOffset() const;
00084
00085
00086
00087
00088
00089
00090 bool getReferenceMinMax(int32_t refID,
00091 uint64_t& minOffset,
00092 uint64_t& maxOffset) const;
00093
00094
00095
00096 int32_t getNumRefs() const;
00097
00098
00099
00100
00101 void printIndex(int32_t refID, bool summary = false);
00102
00103
00104 static const int32_t REF_ID_UNMAPPED = -1;
00105
00106
00107 static const int32_t REF_ID_ALL = -2;
00108
00109 private:
00110
00111 const static uint32_t MAX_NUM_BINS = 37450;
00112
00113 const static uint32_t MAX_POSITION = 536870911;
00114
00115
00116
00117 const static uint32_t LINEAR_INDEX_SHIFT = 14;
00118
00119 class Bin
00120 {
00121 public:
00122 Bin(){chunks = NULL; reset();}
00123 ~Bin() {reset();}
00124 void reset()
00125 {
00126 if(chunks != NULL)
00127 {
00128 free(chunks);
00129 chunks = NULL;
00130 }
00131 n_chunk = 0;
00132 bin = NOT_USED_BIN;
00133 }
00134 uint32_t bin;
00135 int32_t n_chunk;
00136 Chunk* chunks;
00137 static const uint32_t NOT_USED_BIN = 0xFFFFFFFF;
00138 };
00139
00140 class Reference
00141 {
00142
00143
00144 public:
00145 static const int32_t UNKNOWN_MAP_INFO = -1;
00146 Reference(){ioffsets = NULL; reset();}
00147 ~Reference(){reset();}
00148 void reset()
00149 {
00150 bins.clear();
00151 if(ioffsets != NULL)
00152 {
00153 free(ioffsets);
00154 ioffsets = NULL;
00155 }
00156 n_bin = 0;
00157 n_intv = 0;
00158 minChunkOffset = -1;
00159 maxChunkOffset = 0;
00160 n_mapped = UNKNOWN_MAP_INFO;
00161 n_unmapped = UNKNOWN_MAP_INFO;
00162 }
00163 int32_t n_bin;
00164 int32_t n_intv;
00165 std::vector<Bin> bins;
00166 uint64_t* ioffsets;
00167 uint64_t minChunkOffset;
00168 uint64_t maxChunkOffset;
00169 int32_t n_mapped;
00170 int32_t n_unmapped;
00171 };
00172
00173
00174
00175 static int getBinsForRegion(uint32_t start, uint32_t end, uint16_t binList[MAX_NUM_BINS]);
00176
00177
00178
00179 uint64_t getMinOffsetFromLinearIndex(int32_t refID, uint32_t position);
00180
00181
00182 int32_t n_ref;
00183
00184 uint64_t maxOverallOffset;
00185
00186
00187 std::vector<Reference> myRefs;
00188 };
00189
00190
00191 #endif