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