]> git.donarmstrong.com Git - bamtools.git/blob - BamIndex.h
Various cleanups. Added -noheader to SAM conversion in ConvertTool
[bamtools.git] / 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: 13 July 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 // BamIndex base class
26 class BamIndex {
27
28     public:
29         BamIndex(BamTools::BgzfData*  bgzf, 
30                  BamTools::BamReader* reader,
31                  bool isBigEndian);
32         virtual ~BamIndex(void) { }
33
34     public:
35         // creates index data (in-memory) from current reader data
36         virtual bool Build(void) =0;
37         // calculates offset(s) for a given region
38         virtual bool GetOffsets(const BamTools::BamRegion& region, const bool isRightBoundSpecified, std::vector<int64_t>& offsets) =0;
39         // loads existing data from file into memory
40         virtual bool Load(const std::string& filename)  =0;
41         // returns whether reference has alignments or no
42         virtual bool HasAlignments(const int& referenceID); 
43         // writes in-memory index data out to file 
44         // N.B. - (this is the original BAM filename, method will modify it to use applicable extension)
45         virtual bool Write(const std::string& bamFilename) =0;
46         
47     protected:
48         BamTools::BgzfData*  m_BGZF;
49         BamTools::BamReader* m_reader;
50         BamTools::RefVector  m_references;
51         bool m_isBigEndian;
52 };
53
54 // BamDefaultIndex class
55 // 
56 // implements default (per SAM/BAM spec) index file ops
57 class BamDefaultIndex : public BamIndex {
58
59   
60     // ctor & dtor
61     public:
62         BamDefaultIndex(BamTools::BgzfData*  bgzf, 
63                         BamTools::BamReader* reader,
64                         bool isBigEndian);
65         ~BamDefaultIndex(void);
66         
67     // interface (implements BamIndex virtual methods)
68     public:
69         // creates index data (in-memory) from current reader data
70         bool Build(void);
71         // calculates offset(s) for a given region
72         bool GetOffsets(const BamTools::BamRegion& region, const bool isRightBoundSpecified, std::vector<int64_t>& offsets);
73          // loads existing data from file into memory
74         bool Load(const std::string& filename);
75         // writes in-memory index data out to file 
76         // N.B. - (this is the original BAM filename, method will modify it to use applicable extension)
77         bool Write(const std::string& bamFilename);
78       
79     // internal implementation
80     private:
81         struct BamDefaultIndexPrivate;
82         BamDefaultIndexPrivate* d;
83 };
84
85 // BamToolsIndex class
86 //
87 // implements BamTools-specific index file ops
88 class BamToolsIndex : public BamIndex {
89
90     // ctor & dtor
91     public:
92         BamToolsIndex(BamTools::BgzfData*  bgzf, 
93                       BamTools::BamReader* reader,
94                       bool isBigEndian);
95         ~BamToolsIndex(void);
96         
97     // interface (implements BamIndex virtual methods)
98     public:
99         // creates index data (in-memory) from current reader data
100         bool Build(void);
101         // calculates offset(s) for a given region
102         bool GetOffsets(const BamTools::BamRegion& region, const bool isRightBoundSpecified, std::vector<int64_t>& offsets);
103          // loads existing data from file into memory
104         bool Load(const std::string& filename);
105         // writes in-memory index data out to file 
106         // N.B. - (this is the original BAM filename, method will modify it to use applicable extension)
107         bool Write(const std::string& bamFilename);
108     
109     // internal implementation
110     private:
111         struct BamToolsIndexPrivate;
112         BamToolsIndexPrivate* d;
113 };
114
115 } // namespace BamTools
116
117 #endif // BAM_INDEX_H