]> git.donarmstrong.com Git - bamtools.git/blob - src/api/BamMultiReader.h
Merge branch 'master' of git://github.com/pezmaster31/bamtools
[bamtools.git] / src / api / BamMultiReader.h
1 // ***************************************************************************\r
2 // BamMultiReader.h (c) 2010 Erik Garrison\r
3 // Marth Lab, Department of Biology, Boston College\r
4 // All rights reserved.\r
5 // ---------------------------------------------------------------------------\r
6 // Last modified: 3 September 2010 (DB)\r
7 // ---------------------------------------------------------------------------\r
8 // Functionality for simultaneously reading multiple BAM files\r
9 // ***************************************************************************\r
10 \r
11 #ifndef BAMMULTIREADER_H\r
12 #define BAMMULTIREADER_H\r
13 \r
14 // C++ includes\r
15 #include <string>\r
16 #include <map>\r
17 #include <utility> // for pair\r
18 #include <sstream>\r
19 \r
20 using namespace std;\r
21 \r
22 // BamTools includes\r
23 #include "BamAux.h"\r
24 #include "BamReader.h"\r
25 \r
26 namespace BamTools {\r
27 \r
28 // index mapping reference/position pairings to bamreaders and their alignments\r
29 typedef multimap<pair<int, int>, pair<BamReader*, BamAlignment*> > AlignmentIndex;\r
30 \r
31 \r
32 class BamMultiReader {\r
33 \r
34     // constructor / destructor\r
35     public:\r
36         BamMultiReader(void);\r
37         ~BamMultiReader(void);\r
38 \r
39     // public interface\r
40     public:\r
41 \r
42         // positioning\r
43         int CurrentRefID;\r
44         int CurrentLeft;\r
45 \r
46         // region under analysis, specified using SetRegion\r
47         BamRegion Region;\r
48 \r
49         // ----------------------\r
50         // BAM file operations\r
51         // ----------------------\r
52 \r
53         // close BAM files\r
54         void Close(void);\r
55 \r
56         // opens BAM files (and optional BAM index files, if provided)\r
57         // @openIndexes - triggers index opening, useful for suppressing\r
58         // error messages during merging of files in which we may not have\r
59         // indexes.\r
60         // @coreMode - setup our first alignments using GetNextAlignmentCore();\r
61         // also useful for merging\r
62         // @preferStandardIndex - look for standard BAM index ".bai" first.  If false, \r
63         // will look for BamTools index ".bti".  \r
64         bool Open(const vector<string> filenames, bool openIndexes = true, bool coreMode = false, bool preferStandardIndex = true);\r
65 \r
66         // returns whether underlying BAM readers ALL have an index loaded\r
67         // this is useful to indicate whether Jump() or SetRegion() are possible\r
68         bool IsIndexLoaded(void) const;\r
69         \r
70         // performs random-access jump to reference, position\r
71         bool Jump(int refID, int position = 0);\r
72 \r
73         // sets the target region\r
74         bool SetRegion(const BamRegion& region);\r
75         bool SetRegion(const int&, const int&, const int&, const int&); // convenience function to above\r
76 \r
77         // returns file pointers to beginning of alignments\r
78         bool Rewind(void);\r
79 \r
80         // ----------------------\r
81         // access alignment data\r
82         // ----------------------\r
83         // updates the reference id marker to match the lower limit of our readers\r
84         void UpdateReferenceID(void);\r
85 \r
86         // retrieves next available alignment (returns success/fail) from all files\r
87         bool GetNextAlignment(BamAlignment&);\r
88         // retrieves next available alignment (returns success/fail) from all files\r
89         // and populates the support data with information about the alignment\r
90         // *** BUT DOES NOT PARSE CHARACTER DATA FROM THE ALIGNMENT\r
91         bool GetNextAlignmentCore(BamAlignment&);\r
92         // ... should this be private?\r
93         bool HasOpenReaders(void);\r
94 \r
95         // ----------------------\r
96         // access auxiliary data\r
97         // ----------------------\r
98 \r
99         // returns unified SAM header text for all files\r
100         const string GetHeaderText(void) const;\r
101         // returns number of reference sequences\r
102         const int GetReferenceCount(void) const;\r
103         // returns vector of reference objects\r
104         const BamTools::RefVector GetReferenceData(void) const;\r
105         // returns reference id (used for BamMultiReader::Jump()) for the given reference name\r
106         const int GetReferenceID(const std::string& refName) const;\r
107         // validates that we have a congruent set of BAM files that are aligned against the same reference sequences\r
108         void ValidateReaders() const;\r
109 \r
110         // ----------------------\r
111         // BAM index operations\r
112         // ----------------------\r
113 \r
114         // creates index for BAM files which lack them, saves to files (default = bamFilename + ".bai")\r
115         bool CreateIndexes(bool useDefaultIndex = true);\r
116 \r
117         //const int GetReferenceID(const string& refName) const;\r
118 \r
119         // utility\r
120         void PrintFilenames(void);\r
121         void DumpAlignmentIndex(void);\r
122         void UpdateAlignments(void); // updates our alignment cache\r
123 \r
124     // private implementation\r
125     private:\r
126 \r
127         // the set of readers and alignments which we operate on, maintained throughout the life of this class\r
128         vector<pair<BamReader*, BamAlignment*> > readers;\r
129 \r
130         // readers and alignments sorted by reference id and position, to keep track of the lowest (next) alignment\r
131         // when a reader reaches EOF, its entry is removed from this index\r
132         AlignmentIndex alignments;\r
133 \r
134         vector<string> fileNames;\r
135 };\r
136 \r
137 } // namespace BamTools\r
138 \r
139 #endif // BAMMULTIREADER_H\r