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