BamTools 2.5.3
Loading...
Searching...
No Matches
Sort.h
Go to the documentation of this file.
1// ***************************************************************************
2// Sort.h (c) 2009 Derek Barnett
3// Marth Lab, Department of Biology, Boston College
4// All rights reserved.
5// ---------------------------------------------------------------------------
6// Last modified: 4 April 2012 (DB)
7// ---------------------------------------------------------------------------
8// Provides sorting functionality.
9// ***************************************************************************
10
11#ifndef ALGORITHMS_SORT_H
12#define ALGORITHMS_SORT_H
13
14#include <algorithm>
15#include <cassert>
16#include <functional>
17#include <string>
18#include <vector>
19#include "api/BamAlignment.h"
20#include "api/BamMultiReader.h"
21#include "api/BamReader.h"
22#include "api/api_global.h"
23
24namespace BamTools {
25namespace Algorithms {
26
30struct API_EXPORT Sort
31{
32
34 enum Order
35 {
36 AscendingOrder = 0,
37 DescendingOrder
38 };
39
45 template <typename ElemType>
46 static bool sort_helper(const Sort::Order& order, const ElemType& lhs, const ElemType& rhs)
47 {
48 switch (order) {
49 case (Sort::AscendingOrder): {
50 std::less<ElemType> comp;
51 return comp(lhs, rhs);
52 }
53 case (Sort::DescendingOrder): {
54 std::greater<ElemType> comp;
55 return comp(lhs, rhs);
56 }
57 default:
58 BT_ASSERT_UNREACHABLE;
59 }
60 return false; // <-- unreachable
61 }
62
79 struct ByName
80 {
81
82 // ctor
83 ByName(const Sort::Order& order = Sort::AscendingOrder)
84 : m_order(order)
85 {}
86
87 // comparison function
88 bool operator()(const BamTools::BamAlignment& lhs, const BamTools::BamAlignment& rhs) const
89 {
90 return sort_helper(m_order, lhs.Name, rhs.Name);
91 }
92
93 // used by BamMultiReader internals
94 static bool UsesCharData()
95 {
96 return true;
97 }
98
99 // data members
100 private:
101 const Sort::Order m_order;
102 };
103
121 {
122
123 // ctor
124 ByPosition(const Sort::Order& order = Sort::AscendingOrder)
125 : m_order(order)
126 {}
127
128 // comparison function
130 {
131
132 // force unmapped aligmnents to end
133 if (lhs.RefID == -1) {
134 return false;
135 }
136 if (rhs.RefID == -1) {
137 return true;
138 }
139
140 // if on same reference, sort on position
141 if (lhs.RefID == rhs.RefID) {
142 return sort_helper(m_order, lhs.Position, rhs.Position);
143 }
144
145 // otherwise sort on reference ID
146 return sort_helper(m_order, lhs.RefID, rhs.RefID);
147 }
148
149 // used by BamMultiReader internals
150 static bool UsesCharData()
151 {
152 return false;
153 }
154
155 // data members
156 private:
157 const Sort::Order m_order;
158 };
159
176 template <typename T>
177 struct ByTag
178 {
179
180 // ctor
181 ByTag(const std::string& tag, const Sort::Order& order = Sort::AscendingOrder)
182 : m_tag(tag)
183 , m_order(order)
184 {}
185
186 // comparison function
188 {
189
190 // force alignments without tag to end
191 T lhsTagValue;
192 T rhsTagValue;
193 if (!lhs.GetTag(m_tag, lhsTagValue)) {
194 return false;
195 }
196 if (!rhs.GetTag(m_tag, rhsTagValue)) {
197 return true;
198 }
199
200 // otherwise compare on tag values
201 return sort_helper(m_order, lhsTagValue, rhsTagValue);
202 }
203
204 // used by BamMultiReader internals
205 static bool UsesCharData()
206 {
207 return true;
208 }
209
210 // data members
211 private:
212 const std::string m_tag;
213 const Sort::Order m_order;
214 };
215
227 struct Unsorted
228 {
229
230 // comparison function
232 {
233 return false; // returning false tends to retain insertion order
234 }
235
236 // used by BamMultiReader internals
237 static bool UsesCharData()
238 {
239 return false;
240 }
241 };
242
256 template <typename Compare>
257 static void SortAlignments(std::vector<BamAlignment>& data, const Compare& comp = Compare())
258 {
259 std::sort(data.begin(), data.end(), comp);
260 }
261
277 template <typename Compare>
278 static std::vector<BamAlignment> SortAlignments(const std::vector<BamAlignment>& input,
279 const Compare& comp = Compare())
280 {
281 std::vector<BamAlignment> output(input);
282 SortAlignments(output, comp);
283 return output;
284 }
285
306 template <typename Compare>
307 static std::vector<BamAlignment> GetSortedRegion(BamReader& reader, const BamRegion& region,
308 const Compare& comp = Compare())
309 {
310 // return empty container if unable to find region
311 if (!reader.IsOpen()) {
312 return std::vector<BamAlignment>();
313 }
314 if (!reader.SetRegion(region)) {
315 return std::vector<BamAlignment>();
316 }
317
318 // iterate through region, grabbing alignments
319 BamAlignment al;
320 std::vector<BamAlignment> results;
321 while (reader.GetNextAlignmentCore(al)) {
322 results.push_back(al);
323 }
324
325 // sort & return alignments
326 SortAlignments(results, comp);
327 return results;
328 }
329
350 template <typename Compare>
351 static std::vector<BamAlignment> GetSortedRegion(BamMultiReader& reader,
352 const BamRegion& region,
353 const Compare& comp = Compare())
354 {
355 // return empty container if unable to find region
356 if (!reader.HasOpenReaders()) {
357 return std::vector<BamAlignment>();
358 }
359 if (!reader.SetRegion(region)) {
360 return std::vector<BamAlignment>();
361 }
362
363 // iterate through region, grabbing alignments
364 BamAlignment al;
365 std::vector<BamAlignment> results;
366 while (reader.GetNextAlignmentCore(al)) {
367 results.push_back(al);
368 }
369
370 // sort & return alignments
371 SortAlignments(results, comp);
372 return results;
373 }
374};
375
376} // namespace Algorithms
377} // namespace BamTools
378
379#endif // ALGORITHMS_SORT_H
The main BAM alignment data structure.
Definition BamAlignment.h:34
bool GetTag(const std::string &tag, T &destination) const
Definition BamAlignment.h:442
int32_t RefID
ID number for reference sequence.
Definition BamAlignment.h:132
std::string Name
read name
Definition BamAlignment.h:125
int32_t Position
position (0-based) where alignment starts
Definition BamAlignment.h:133
Convenience class for reading multiple BAM files.
Definition BamMultiReader.h:27
bool GetNextAlignmentCore(BamAlignment &alignment)
Retrieves next available alignment.
Definition BamMultiReader.cpp:197
bool SetRegion(const BamRegion &region)
Sets a target region of interest.
Definition BamMultiReader.cpp:413
bool HasOpenReaders() const
Returns true if there are any open BAM files.
Definition BamMultiReader.cpp:245
Provides read access to BAM files.
Definition BamReader.h:26
bool SetRegion(const BamRegion &region)
Sets a target region of interest.
Definition BamReader.cpp:373
bool IsOpen() const
Returns true if a BAM file is open for reading.
Definition BamReader.cpp:234
bool GetNextAlignmentCore(BamAlignment &alignment)
Retrieves next available alignment, without populating the alignment's string data fields.
Definition BamReader.cpp:189
Contains all BamTools classes & methods.
Definition Sort.h:24
Function object for comparing alignments by name.
Definition Sort.h:80
ByName(const Sort::Order &order=Sort::AscendingOrder)
Definition Sort.h:83
bool operator()(const BamTools::BamAlignment &lhs, const BamTools::BamAlignment &rhs) const
Definition Sort.h:88
Function object for comparing alignments by position.
Definition Sort.h:121
bool operator()(const BamTools::BamAlignment &lhs, const BamTools::BamAlignment &rhs) const
Definition Sort.h:129
ByPosition(const Sort::Order &order=Sort::AscendingOrder)
Definition Sort.h:124
Function object for comparing alignments by tag value.
Definition Sort.h:178
ByTag(const std::string &tag, const Sort::Order &order=Sort::AscendingOrder)
Definition Sort.h:181
bool operator()(const BamTools::BamAlignment &lhs, const BamTools::BamAlignment &rhs) const
Definition Sort.h:187
Placeholder function object.
Definition Sort.h:228
bool operator()(const BamTools::BamAlignment &, const BamTools::BamAlignment &) const
Definition Sort.h:231
Provides classes & methods related to sorting BamAlignments.
Definition Sort.h:31
static std::vector< BamAlignment > GetSortedRegion(BamReader &reader, const BamRegion &region, const Compare &comp=Compare())
Definition Sort.h:307
static std::vector< BamAlignment > GetSortedRegion(BamMultiReader &reader, const BamRegion &region, const Compare &comp=Compare())
Definition Sort.h:351
static void SortAlignments(std::vector< BamAlignment > &data, const Compare &comp=Compare())
Definition Sort.h:257
Order
Provides explicit values for specifying desired sort ordering.
Definition Sort.h:35
static std::vector< BamAlignment > SortAlignments(const std::vector< BamAlignment > &input, const Compare &comp=Compare())
Definition Sort.h:278
Represents a sequential genomic region.
Definition BamAux.h:90