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
11 #ifndef BAMMULTIREADER_H
\r
12 #define BAMMULTIREADER_H
\r
18 #include "BamReader.h"
\r
20 namespace BamTools {
\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
25 class BamMultiReader {
\r
27 // constructor / destructor
\r
29 BamMultiReader(void);
\r
30 ~BamMultiReader(void);
\r
39 // region under analysis, specified using SetRegion
\r
42 // ----------------------
\r
43 // BAM file operations
\r
44 // ----------------------
\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
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
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
63 // performs random-access jump to reference, position
\r
64 bool Jump(int refID, int position = 0);
\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
70 // returns file pointers to beginning of alignments
\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
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
88 // ----------------------
\r
89 // access auxiliary data
\r
90 // ----------------------
\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
103 // ----------------------
\r
104 // BAM index operations
\r
105 // ----------------------
\r
107 // creates index for BAM files which lack them, saves to files (default = bamFilename + ".bai")
\r
108 bool CreateIndexes(bool useStandardIndex = true);
\r
110 //const int GetReferenceID(const string& refName) const;
\r
113 void PrintFilenames(void);
\r
114 void DumpAlignmentIndex(void);
\r
115 void UpdateAlignments(void); // updates our alignment cache
\r
117 // private implementation
\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
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
127 std::vector<std::string> fileNames;
\r
130 } // namespace BamTools
\r
132 #endif // BAMMULTIREADER_H
\r