]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BamMultiMerger_p.h
Implemented proper -byname sorting (finally).
[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: 19 November 2010 (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 <string>
29 #include <utility>
30
31 namespace BamTools {
32 namespace Internal {
33
34 typedef std::pair<BamReader*, BamAlignment*> ReaderAlignment;
35
36 // generic MultiMerger interface
37 class IBamMultiMerger {
38
39     public:
40         IBamMultiMerger(void) { }
41         virtual ~IBamMultiMerger(void) { }
42
43     public:
44         virtual void Add(const ReaderAlignment& value) =0;
45         virtual void Clear(void) =0;
46         virtual const ReaderAlignment& First(void) const =0;
47         virtual const int Size(void) const =0;
48         virtual ReaderAlignment TakeFirst(void) =0;
49 };
50
51 // IBamMultiMerger implementation - sorted on BamAlignment: (RefId, Position)
52 class PositionMultiMerger : public IBamMultiMerger {
53
54     public:
55         PositionMultiMerger(void) : IBamMultiMerger() { }
56         ~PositionMultiMerger(void) { }
57
58     public:
59         void Add(const ReaderAlignment& value);
60         void Clear(void);
61         const ReaderAlignment& First(void) const;
62         const int Size(void) const;
63         ReaderAlignment TakeFirst(void);
64
65     private:
66         typedef std::pair<int, int>                     KeyType;
67         typedef std::multimap<KeyType, ReaderAlignment> IndexType;
68         typedef std::pair<KeyType, ReaderAlignment>     KeyValueType;
69         typedef IndexType::iterator                     IndexIterator;
70         typedef IndexType::const_iterator               IndexConstIterator;
71
72         IndexType m_data;
73 };
74
75 // IBamMultiMerger implementation - sorted on BamAlignment: Name
76 class ReadNameMultiMerger : public IBamMultiMerger {
77
78     public:
79         ReadNameMultiMerger(void) : IBamMultiMerger() { }
80         ~ReadNameMultiMerger(void) { }
81
82     public:
83         void Add(const ReaderAlignment& value);
84         void Clear(void);
85         const ReaderAlignment& First(void) const;
86         const int Size(void) const;
87         ReaderAlignment TakeFirst(void);
88
89     private:
90         typedef std::string                             KeyType;
91         typedef std::multimap<KeyType, ReaderAlignment> IndexType;
92         typedef std::pair<KeyType, ReaderAlignment>     KeyValueType;
93         typedef IndexType::iterator                     IndexIterator;
94         typedef IndexType::const_iterator               IndexConstIterator;
95
96         IndexType m_data;
97 };
98
99 // ------------------------------------------
100 // PositionMultiMerger implementation
101
102 inline void PositionMultiMerger::Add(const ReaderAlignment& value) {
103     const KeyType key = std::make_pair<int, int>( value.second->RefID, value.second->Position );
104     m_data.insert( KeyValueType(key, value) );
105 }
106
107 inline void PositionMultiMerger::Clear(void) {
108     m_data.clear();
109 }
110
111 inline const ReaderAlignment& PositionMultiMerger::First(void) const {
112     const KeyValueType& entry = (*m_data.begin());
113     return entry.second;
114 }
115
116 inline const int PositionMultiMerger::Size(void) const {
117     return m_data.size();
118 }
119
120 inline ReaderAlignment PositionMultiMerger::TakeFirst(void) {
121     IndexIterator first = m_data.begin();
122     ReaderAlignment next = (*first).second;
123     m_data.erase(first);
124     return next;
125 }
126
127 // ------------------------------------------
128 // ReadNameMultiMerger implementation
129
130 inline void ReadNameMultiMerger::Add(const ReaderAlignment& value) {
131     const KeyType key = value.second->Name;
132     m_data.insert( KeyValueType(key, value) );
133 }
134
135 inline void ReadNameMultiMerger::Clear(void) {
136     m_data.clear();
137 }
138
139 inline const ReaderAlignment& ReadNameMultiMerger::First(void) const {
140     const KeyValueType& entry = (*m_data.begin());
141     return entry.second;
142 }
143
144 inline const int ReadNameMultiMerger::Size(void) const {
145     return m_data.size();
146 }
147
148 inline ReaderAlignment ReadNameMultiMerger::TakeFirst(void) {
149     IndexIterator first = m_data.begin();
150     ReaderAlignment next = (*first).second;
151     m_data.erase(first);
152     return next;
153 }
154
155 } // namespace Internal
156 } // namespace BamTools
157
158 #endif // BAMMULTIMERGER_P_H