]> git.donarmstrong.com Git - bamtools.git/blob - BamIndex.h
Separated indexing ops to new class. Implemented a fixed-partition-size index format...
[bamtools.git] / BamIndex.h
1 #ifndef BAM_INDEX_H
2 #define BAM_INDEX_H
3
4 #include <string>
5 #include <vector>
6 #include "BamAux.h"
7
8 namespace BamTools {
9
10 class BamReader;
11 class BgzfData;
12   
13 // BamIndex base class
14 class BamIndex {
15
16     public:
17         BamIndex(BamTools::BgzfData*  bgzf, 
18                  BamTools::BamReader* reader,
19                  bool isBigEndian);
20         virtual ~BamIndex(void) { }
21
22     public:
23         // creates index data (in-memory) from current reader data
24         virtual bool Build(void) =0;
25         // calculates offset(s) for a given region
26         virtual bool GetOffsets(const BamTools::BamRegion& region, const bool isRightBoundSpecified, std::vector<int64_t>& offsets) =0;
27         // loads existing data from file into memory
28         virtual bool Load(const std::string& filename)  =0;
29         // returns whether reference has alignments or no
30         virtual bool HasAlignments(const int& referenceID); 
31         // writes in-memory index data out to file 
32         // N.B. - (this is the original BAM filename, method will modify it to use applicable extension)
33         virtual bool Write(const std::string& bamFilename) =0;
34         
35     protected:
36         BamTools::BgzfData*  m_BGZF;
37         BamTools::BamReader* m_reader;
38         BamTools::RefVector  m_references;
39         bool m_isBigEndian;
40 };
41
42 // BamDefaultIndex class
43 // 
44 // implements default (per SAM/BAM spec) index file ops
45 class BamDefaultIndex : public BamIndex {
46
47   
48     // ctor & dtor
49     public:
50         BamDefaultIndex(BamTools::BgzfData*  bgzf, 
51                         BamTools::BamReader* reader,
52                         bool isBigEndian);
53         ~BamDefaultIndex(void);
54         
55     // interface (implements BamIndex virtual methods)
56     public:
57         // creates index data (in-memory) from current reader data
58         bool Build(void);
59         // calculates offset(s) for a given region
60         bool GetOffsets(const BamTools::BamRegion& region, const bool isRightBoundSpecified, std::vector<int64_t>& offsets);
61          // loads existing data from file into memory
62         bool Load(const std::string& filename);
63         // writes in-memory index data out to file 
64         // N.B. - (this is the original BAM filename, method will modify it to use applicable extension)
65         bool Write(const std::string& bamFilename);
66       
67     // internal implementation
68     private:
69         struct BamDefaultIndexPrivate;
70         BamDefaultIndexPrivate* d;
71 };
72
73 // BamToolsIndex class
74 //
75 // implements BamTools-specific index file ops
76 class BamToolsIndex : public BamIndex {
77
78     // ctor & dtor
79     public:
80         BamToolsIndex(BamTools::BgzfData*  bgzf, 
81                       BamTools::BamReader* reader,
82                       bool isBigEndian);
83         ~BamToolsIndex(void);
84         
85     // interface (implements BamIndex virtual methods)
86     public:
87         // creates index data (in-memory) from current reader data
88         bool Build(void);
89         // calculates offset(s) for a given region
90         bool GetOffsets(const BamTools::BamRegion& region, const bool isRightBoundSpecified, std::vector<int64_t>& offsets);
91          // loads existing data from file into memory
92         bool Load(const std::string& filename);
93         // writes in-memory index data out to file 
94         // N.B. - (this is the original BAM filename, method will modify it to use applicable extension)
95         bool Write(const std::string& bamFilename);
96     
97     // internal implementation
98     private:
99         struct BamToolsIndexPrivate;
100         BamToolsIndexPrivate* d;
101 };
102
103 } // namespace BamTools
104
105 #endif // BAM_INDEX_H