]> git.donarmstrong.com Git - bamtools.git/blob - src/api/BamReader.h
Implemented index cache mode for both BAI & BTI formats
[bamtools.git] / src / api / BamReader.h
1 // ***************************************************************************\r
2 // BamReader.h (c) 2009 Derek Barnett, Michael Str�mberg\r
3 // Marth Lab, Department of Biology, Boston College\r
4 // All rights reserved.\r
5 // ---------------------------------------------------------------------------\r
6 // Last modified: 13 October 2010 (DB)\r
7 // ---------------------------------------------------------------------------\r
8 // Uses BGZF routines were adapted from the bgzf.c code developed at the Broad\r
9 // Institute.\r
10 // ---------------------------------------------------------------------------\r
11 // Provides the basic functionality for reading BAM files\r
12 // ***************************************************************************\r
13 \r
14 #ifndef BAMREADER_H\r
15 #define BAMREADER_H\r
16 \r
17 #include <string>\r
18 #include "BamAlignment.h"\r
19 #include "BamIndex.h"\r
20 \r
21 namespace BamTools {\r
22   \r
23 class BamReader {\r
24 \r
25     // constructor / destructor\r
26     public:\r
27         BamReader(void);\r
28         ~BamReader(void);\r
29 \r
30     // public interface\r
31     public:\r
32 \r
33         // ----------------------\r
34         // BAM file operations\r
35         // ----------------------\r
36 \r
37         // close BAM file\r
38         void Close(void);\r
39         // returns whether reader is open for reading or not\r
40         bool IsOpen(void) const;\r
41         // performs random-access jump using (reference, position) as a left-bound\r
42         bool Jump(int refID, int position = 0);\r
43         // opens BAM file (and optional BAM index file, if provided)\r
44         // @lookForIndex - if no indexFilename provided, look in BAM file's directory for an existing index file\r
45         //   default behavior is to skip index file search if no index filename given\r
46         // @preferStandardIndex - if true, give priority in index file searching to standard BAM index (*.bai)\r
47         //   default behavior is to prefer the BamToolsIndex (*.bti) if both are available\r
48         bool Open(const std::string& filename, \r
49                   const std::string& indexFilename = "", \r
50                   const bool lookForIndex = false, \r
51                   const bool preferStandardIndex = false);\r
52         // returns file pointer to beginning of alignments\r
53         bool Rewind(void);\r
54         // sets a region of interest (with left & right bound reference/position)\r
55         // returns success/failure of seeking to left bound of region\r
56         bool SetRegion(const BamRegion& region);\r
57         bool SetRegion(const int& leftRefID, const int& leftBound, const int& rightRefID, const int& rightBound);\r
58 \r
59         // ----------------------\r
60         // access alignment data\r
61         // ----------------------\r
62 \r
63         // retrieves next available alignment (returns success/fail)\r
64         bool GetNextAlignment(BamAlignment& bAlignment);\r
65         // retrieves next available alignment core data (returns success/fail)\r
66         // ** DOES NOT parse any character data (read name, bases, qualities, tag data) **\r
67         // useful for operations requiring ONLY aligner-related information \r
68         // (refId/position, alignment flags, CIGAR, mapQuality, etc)\r
69         bool GetNextAlignmentCore(BamAlignment& bAlignment);\r
70 \r
71         // ----------------------\r
72         // access auxiliary data\r
73         // ----------------------\r
74 \r
75         // returns SAM header text\r
76         const std::string GetHeaderText(void) const;\r
77         // returns number of reference sequences\r
78         int GetReferenceCount(void) const;\r
79         // returns vector of reference objects\r
80         const BamTools::RefVector& GetReferenceData(void) const;\r
81         // returns reference id (used for BamReader::Jump()) for the given reference name\r
82         int GetReferenceID(const std::string& refName) const;\r
83         // returns the name of the file associated with this BamReader\r
84         const std::string GetFilename(void) const;\r
85 \r
86         // ----------------------\r
87         // BAM index operations\r
88         // ----------------------\r
89 \r
90         // creates index for BAM file, saves to file\r
91         // default behavior is to create the BAM standard index (".bai")\r
92         // set flag to false to create the BamTools-specific index (".bti")\r
93         bool CreateIndex(bool useStandardIndex = true);\r
94         // returns whether index data is available for reading \r
95         // (e.g. if true, BamReader should be able to seek to a region)\r
96         bool HasIndex(void) const;\r
97         // change the index caching behavior\r
98         // default BamReader/Index mode is LimitedIndexCaching\r
99         // @mode - can be either FullIndexCaching, LimitedIndexCaching, \r
100         //   or NoIndexCaching. See BamIndex.h for more details\r
101         void SetIndexCacheMode(const BamIndex::BamIndexCacheMode mode);\r
102         \r
103     // deprecated methods\r
104     public:\r
105         \r
106         // deprecated (but still available): prefer HasIndex() instead\r
107         //\r
108         // Deprecated purely for API semantic clarity - HasIndex() should be clearer \r
109         // than IsIndexLoaded() in light of the new caching modes that may clear the \r
110         // index data from memory, but leave the index file open for later random access \r
111         // seeks.\r
112         //\r
113         // For example, what would (IsIndexLoaded() == true) mean when cacheMode has been \r
114         // explicitly set to NoIndexCaching? This is confusing at best, misleading about \r
115         // current memory behavior at worst.\r
116         //\r
117         // returns whether index data is available \r
118         // (e.g. if true, BamReader should be able to seek to a region)\r
119         bool IsIndexLoaded(void) const;\r
120         \r
121     // private implementation\r
122     private:\r
123         struct BamReaderPrivate;\r
124         BamReaderPrivate* d;\r
125 };\r
126 \r
127 } // namespace BamTools\r
128 \r
129 #endif // BAMREADER_H\r