]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BamToolsIndex_p.h
Removed index cache mode
[bamtools.git] / src / api / internal / BamToolsIndex_p.h
1 // ***************************************************************************
2 // BamToolsIndex.h (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides index operations for the BamTools index format (".bti")
8 // ***************************************************************************
9
10 #ifndef BAMTOOLS_INDEX_FORMAT_H
11 #define BAMTOOLS_INDEX_FORMAT_H
12
13 //  -------------
14 //  W A R N I N G
15 //  -------------
16 //
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.
20 //
21 // We mean it.
22
23 #include "api/BamAux.h"
24 #include "api/BamIndex.h"
25 #include <map>
26 #include <string>
27 #include <vector>
28
29 namespace BamTools {
30 namespace Internal {
31
32 // contains data for each 'block' in a BTI index
33 struct BtiBlock {
34
35     // data members
36     int32_t MaxEndPosition;
37     int64_t StartOffset;
38     int32_t StartPosition;
39
40     // ctor
41     BtiBlock(const int32_t& maxEndPosition = 0,
42              const int64_t& startOffset    = 0,
43              const int32_t& startPosition  = 0)
44         : MaxEndPosition(maxEndPosition)
45         , StartOffset(startOffset)
46         , StartPosition(startPosition)
47     { }
48 };
49
50 // convenience typedef for describing a a list of BTI blocks on a reference
51 typedef std::vector<BtiBlock> BtiBlockVector;
52
53 // contains all fields necessary for building, loading, & writing
54 // full BTI index data for a single reference
55 struct BtiReferenceEntry {
56
57     // data members
58     int32_t ID;
59     BtiBlockVector Blocks;
60
61     // ctor
62     BtiReferenceEntry(const int& id = -1)
63         : ID(id)
64     { }
65 };
66
67 // provides (persistent) summary of BtiReferenceEntry's index data
68 struct BtiReferenceSummary {
69
70     // data members
71     int NumBlocks;
72     uint64_t FirstBlockFilePosition;
73
74     // ctor
75     BtiReferenceSummary(void)
76         : NumBlocks(0)
77         , FirstBlockFilePosition(0)
78     { }
79 };
80
81 // convenience typedef for describing a full BTI index file summary
82 typedef std::vector<BtiReferenceSummary> BtiFileSummary;
83
84 class BamToolsIndex : public BamIndex {
85
86     // keep a list of any supported versions here
87     // (might be useful later to handle any 'legacy' versions if the format changes)
88     // listed for example like: BTI_1_0 = 1, BTI_1_1 = 2, BTI_1_2 = 3, BTI_2_0 = 4, and so on
89     //
90     // so a change introduced in BTI_1_2 may be handled from then on by:
91     //
92     // if ( indexVersion >= BTI_1_2 )
93     //   do something new
94     // else
95     //   do the old thing
96     enum Version { BTI_1_0 = 1
97                  , BTI_1_1
98                  , BTI_1_2
99                  , BTI_2_0
100                  };
101
102     // ctor & dtor
103     public:
104         BamToolsIndex(Internal::BamReaderPrivate* reader);
105         ~BamToolsIndex(void);
106
107     // BamIndex implementation
108     public:
109         // builds index from associated BAM file & writes out to index file
110         bool Create(void);
111         // returns whether reference has alignments or no
112         bool HasAlignments(const int& referenceID) const;
113         // attempts to use index data to jump to @region, returns success/fail
114         // a "successful" jump indicates no error, but not whether this region has data
115         //   * thus, the method sets a flag to indicate whether there are alignments
116         //     available after the jump position
117         bool Jump(const BamTools::BamRegion& region, bool* hasAlignmentsInRegion);
118         // loads existing data from file into memory
119         bool Load(const std::string& filename);
120     public:
121         // returns format's file extension
122         static const std::string Extension(void);
123
124     // internal methods
125     private:
126
127         // index file ops
128         void CheckMagicNumber(void);
129         void CheckVersion(void);
130         void CloseFile(void);
131         bool IsFileOpen(void) const;
132         void OpenFile(const std::string& filename, const char* mode);
133         void Seek(const int64_t& position, const int& origin);
134         int64_t Tell(void) const;
135
136         // index-creation methods
137         void ClearReferenceEntry(BtiReferenceEntry& refEntry);
138         void WriteBlock(const BtiBlock& block);
139         void WriteBlocks(const BtiBlockVector& blocks);
140         void WriteHeader(void);
141         void WriteReferenceEntry(const BtiReferenceEntry& refEntry);
142
143         // random-access methods
144         void GetOffset(const BamRegion& region, int64_t& offset, bool* hasAlignmentsInRegion);
145         void ReadBlock(BtiBlock& block);
146         void ReadBlocks(const BtiReferenceSummary& refSummary, BtiBlockVector& blocks);
147         void ReadReferenceEntry(BtiReferenceEntry& refEntry);
148
149         // BTI summary data methods
150         void InitializeFileSummary(const int& numReferences);
151         void LoadFileSummary(void);
152         void LoadHeader(void);
153         void LoadNumBlocks(int& numBlocks);
154         void LoadNumReferences(int& numReferences);
155         void LoadReferenceSummary(BtiReferenceSummary& refSummary);
156         void SkipBlocks(const int& numBlocks);
157
158     // data members
159     private:
160         bool  m_isBigEndian;
161         BtiFileSummary m_indexFileSummary;
162         uint32_t m_blockSize;
163         int32_t m_inputVersion; // Version is serialized as int
164         Version m_outputVersion;
165
166         struct RaiiWrapper {
167             FILE* IndexStream;
168             RaiiWrapper(void);
169             ~RaiiWrapper(void);
170         };
171         RaiiWrapper Resources;
172
173     // static constants
174     private:
175         static const uint32_t DEFAULT_BLOCK_LENGTH;
176         static const std::string BTI_EXTENSION;
177         static const char* const BTI_MAGIC;
178         static const int SIZEOF_BLOCK;
179 };
180
181 } // namespace Internal
182 } // namespace BamTools
183
184 #endif // BAMTOOLS_INDEX_FORMAT_H