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
00102 int32_t getNumMappedReads(int32_t refID);
00103
00104
00105
00106
00107
00108 int32_t getNumUnMappedReads(int32_t refID);
00109
00110
00111
00112
00113 void printIndex(int32_t refID, bool summary = false);
00114
00115
00116 static const int32_t UNKNOWN_NUM_READS = -1;
00117
00118
00119 static const int32_t REF_ID_UNMAPPED = -1;
00120
00121
00122 static const int32_t REF_ID_ALL = -2;
00123
00124 private:
00125
00126 const static uint32_t MAX_NUM_BINS = 37450;
00127
00128 const static uint32_t MAX_POSITION = 536870911;
00129
00130
00131
00132 const static uint32_t LINEAR_INDEX_SHIFT = 14;
00133
00134 class Bin
00135 {
00136 public:
00137 Bin(){chunks = NULL; reset();}
00138 ~Bin() {reset();}
00139 void reset()
00140 {
00141 if(chunks != NULL)
00142 {
00143 free(chunks);
00144 chunks = NULL;
00145 }
00146 n_chunk = 0;
00147 bin = NOT_USED_BIN;
00148 }
00149 uint32_t bin;
00150 int32_t n_chunk;
00151 Chunk* chunks;
00152 static const uint32_t NOT_USED_BIN = 0xFFFFFFFF;
00153 };
00154
00155 class Reference
00156 {
00157
00158
00159 public:
00160 static const int32_t UNKNOWN_MAP_INFO = -1;
00161 Reference(){ioffsets = NULL; reset();}
00162 ~Reference(){reset();}
00163 void reset()
00164 {
00165 bins.clear();
00166 if(ioffsets != NULL)
00167 {
00168 free(ioffsets);
00169 ioffsets = NULL;
00170 }
00171 n_bin = 0;
00172 n_intv = 0;
00173 minChunkOffset = -1;
00174 maxChunkOffset = 0;
00175 n_mapped = UNKNOWN_MAP_INFO;
00176 n_unmapped = UNKNOWN_MAP_INFO;
00177 }
00178 int32_t n_bin;
00179 int32_t n_intv;
00180 std::vector<Bin> bins;
00181 uint64_t* ioffsets;
00182 uint64_t minChunkOffset;
00183 uint64_t maxChunkOffset;
00184 int32_t n_mapped;
00185 int32_t n_unmapped;
00186 };
00187
00188
00189
00190 static int getBinsForRegion(uint32_t start, uint32_t end, uint16_t binList[MAX_NUM_BINS]);
00191
00192
00193
00194 uint64_t getMinOffsetFromLinearIndex(int32_t refID, uint32_t position);
00195
00196
00197 int32_t n_ref;
00198
00199 uint64_t maxOverallOffset;
00200
00201 int32_t myUnMappedNumReads;
00202
00203
00204 std::vector<Reference> myRefs;
00205 };
00206
00207
00208 #endif