]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BamMultiMerger_p.h
Added UNSORTED to BamMultiReader::SortOrder types. Unsorted BAMs are 'merged' through...
[bamtools.git] / src / api / internal / BamMultiMerger_p.h
1 // ***************************************************************************
2 // BamMultiMerger_p.h (c) 2010 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 17 January 2011 (DB)
7 // ---------------------------------------------------------------------------
8 // Provides merging functionality for BamMultiReader.  At this point, supports
9 // sorting results by (refId, position) or by read name.
10 // ***************************************************************************
11
12 #ifndef BAMMULTIMERGER_P_H
13 #define BAMMULTIMERGER_P_H
14
15 //  -------------
16 //  W A R N I N G
17 //  -------------
18 //
19 // This file is not part of the BamTools API.  It exists purely as an
20 // implementation detail. This header file may change from version to version
21 // without notice, or even be removed.
22 //
23 // We mean it.
24
25 #include <api/BamAlignment.h>
26 #include <api/BamReader.h>
27 #include <map>
28 #include <queue>
29 #include <string>
30 #include <utility>
31
32 namespace BamTools {
33 namespace Internal {
34
35 typedef std::pair<BamReader*, BamAlignment*> ReaderAlignment;
36
37 // generic MultiMerger interface
38 class IBamMultiMerger {
39
40     public:
41         IBamMultiMerger(void) { }
42         virtual ~IBamMultiMerger(void) { }
43
44     public:
45         virtual void Add(const ReaderAlignment& value) =0;
46         virtual void Clear(void) =0;
47         virtual const ReaderAlignment& First(void) const =0;
48         virtual const int Size(void) const =0;
49         virtual ReaderAlignment TakeFirst(void) =0;
50 };
51
52 // IBamMultiMerger implementation - sorted on BamAlignment: (RefId, Position)
53 class PositionMultiMerger : public IBamMultiMerger {
54
55     public:
56         PositionMultiMerger(void) : IBamMultiMerger() { }
57         ~PositionMultiMerger(void) { }
58
59     public:
60         void Add(const ReaderAlignment& value);
61         void Clear(void);
62         const ReaderAlignment& First(void) const;
63         const int Size(void) const;
64         ReaderAlignment TakeFirst(void);
65
66     private:
67         typedef std::pair<int, int>                     KeyType;
68         typedef std::multimap<KeyType, ReaderAlignment> IndexType;
69         typedef std::pair<KeyType, ReaderAlignment>     KeyValueType;
70         typedef IndexType::iterator                     IndexIterator;
71         typedef IndexType::const_iterator               IndexConstIterator;
72
73         IndexType m_data;
74 };
75
76 // IBamMultiMerger implementation - sorted on BamAlignment: Name
77 class ReadNameMultiMerger : public IBamMultiMerger {
78
79     public:
80         ReadNameMultiMerger(void) : IBamMultiMerger() { }
81         ~ReadNameMultiMerger(void) { }
82
83     public:
84         void Add(const ReaderAlignment& value);
85         void Clear(void);
86         const ReaderAlignment& First(void) const;
87         const int Size(void) const;
88         ReaderAlignment TakeFirst(void);
89
90     private:
91         typedef std::string                             KeyType;
92         typedef std::multimap<KeyType, ReaderAlignment> IndexType;
93         typedef std::pair<KeyType, ReaderAlignment>     KeyValueType;
94         typedef IndexType::iterator                     IndexIterator;
95         typedef IndexType::const_iterator               IndexConstIterator;
96
97         IndexType m_data;
98 };
99
100 // IBamMultiMerger implementation - unsorted BAM file(s)
101 class UnsortedMultiMerger : public IBamMultiMerger {
102
103     public:
104         UnsortedMultiMerger(void) : IBamMultiMerger() { }
105         ~UnsortedMultiMerger(void) { }
106
107     public:
108         void Add(const ReaderAlignment& value);
109         void Clear(void);
110         const ReaderAlignment& First(void) const;
111         const int Size(void) const;
112         ReaderAlignment TakeFirst(void);
113
114     private:
115         typedef std::queue<ReaderAlignment> IndexType;
116         IndexType m_data;
117 };
118
119 // ------------------------------------------
120 // PositionMultiMerger implementation
121
122 inline void PositionMultiMerger::Add(const ReaderAlignment& value) {
123     const KeyType key = std::make_pair<int, int>( value.second->RefID, value.second->Position );
124     m_data.insert( KeyValueType(key, value) );
125 }
126
127 inline void PositionMultiMerger::Clear(void) {
128     m_data.clear();
129 }
130
131 inline const ReaderAlignment& PositionMultiMerger::First(void) const {
132     const KeyValueType& entry = (*m_data.begin());
133     return entry.second;
134 }
135
136 inline const int PositionMultiMerger::Size(void) const {
137     return m_data.size();
138 }
139
140 inline ReaderAlignment PositionMultiMerger::TakeFirst(void) {
141     IndexIterator first = m_data.begin();
142     ReaderAlignment next = (*first).second;
143     m_data.erase(first);
144     return next;
145 }
146
147 // ------------------------------------------
148 // ReadNameMultiMerger implementation
149
150 inline void ReadNameMultiMerger::Add(const ReaderAlignment& value) {
151     const KeyType key = value.second->Name;
152     m_data.insert( KeyValueType(key, value) );
153 }
154
155 inline void ReadNameMultiMerger::Clear(void) {
156     m_data.clear();
157 }
158
159 inline const ReaderAlignment& ReadNameMultiMerger::First(void) const {
160     const KeyValueType& entry = (*m_data.begin());
161     return entry.second;
162 }
163
164 inline const int ReadNameMultiMerger::Size(void) const {
165     return m_data.size();
166 }
167
168 inline ReaderAlignment ReadNameMultiMerger::TakeFirst(void) {
169     IndexIterator first = m_data.begin();
170     ReaderAlignment next = (*first).second;
171     m_data.erase(first);
172     return next;
173 }
174
175 // ------------------------------------------
176 // UnsortedMultiMerger implementation
177
178 inline void UnsortedMultiMerger::Add(const ReaderAlignment& value) {
179     m_data.push(value);
180 }
181
182 inline void UnsortedMultiMerger::Clear(void) {
183     m_data.clear();
184 }
185
186 inline const ReaderAlignment& UnsortedMultiMerger::First(void) const {
187     return m_data.front();
188 }
189
190 inline const int UnsortedMultiMerger::Size(void) const {
191     return m_data.size();
192 }
193
194 inline ReaderAlignment UnsortedMultiMerger::TakeFirst(void) {
195     ReaderAlignment first = m_data.front();
196     m_data.pop();
197     return first;
198 }
199
200 } // namespace Internal
201 } // namespace BamTools
202
203 #endif // BAMMULTIMERGER_P_H