/usr/include/bamtools/api/algorithms/Sort.h is in libbamtools-dev 2.4.1+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | // ***************************************************************************
// Sort.h (c) 2009 Derek Barnett
// Marth Lab, Department of Biology, Boston College
// All rights reserved.
// ---------------------------------------------------------------------------
// Last modified: 4 April 2012 (DB)
// ---------------------------------------------------------------------------
// Provides sorting functionality.
// ***************************************************************************
#ifndef ALGORITHMS_SORT_H
#define ALGORITHMS_SORT_H
#include "api/api_global.h"
#include "api/BamAlignment.h"
#include "api/BamReader.h"
#include "api/BamMultiReader.h"
#include <cassert>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
namespace BamTools {
namespace Algorithms {
/*! \struct BamTools::Algorithms::Sort
\brief Provides classes & methods related to sorting BamAlignments
*/
struct API_EXPORT Sort {
//! Provides explicit values for specifying desired sort ordering
enum Order { AscendingOrder = 0
, DescendingOrder
};
/*! \fn template<typename ElemType> static inline bool sort_helper(const Sort::Order& order, const ElemType& lhs, const ElemType& rhs)
\internal
Determines necessary STL function object depending on requested Sort::Order
*/
template<typename ElemType>
static inline bool sort_helper(const Sort::Order& order, const ElemType& lhs, const ElemType& rhs) {
switch ( order ) {
case ( Sort::AscendingOrder ) : { std::less<ElemType> comp; return comp(lhs, rhs); }
case ( Sort::DescendingOrder ) : { std::greater<ElemType> comp; return comp(lhs, rhs); }
default : BT_ASSERT_UNREACHABLE;
}
return false; // <-- unreachable
}
//! Base class for our sorting function objects
typedef std::binary_function<BamAlignment, BamAlignment, bool> AlignmentSortBase;
/*! \struct BamTools::Algorithms::Sort::ByName
\brief Function object for comparing alignments by name
Default sort order is Sort::AscendingOrder.
\code
std::vector<BamAlignment> a;
// sort by name, in ascending order (the following two lines are equivalent):
std::sort( a.begin(), a.end(), Sort::ByName() );
std::sort( a.begin(), a.end(), Sort::ByName(Sort::AscendingOrder) );
// OR sort in descending order
std::sort( a.begin(), a.end(), Sort::ByName(Sort::DescendingOrder) );
\endcode
*/
struct ByName : public AlignmentSortBase {
// ctor
ByName(const Sort::Order& order = Sort::AscendingOrder)
: m_order(order)
{ }
// comparison function
bool operator()(const BamTools::BamAlignment& lhs, const BamTools::BamAlignment& rhs) {
return sort_helper(m_order, lhs.Name, rhs.Name);
}
// used by BamMultiReader internals
static inline bool UsesCharData(void) { return true; }
// data members
private:
const Sort::Order m_order;
};
/*! \struct BamTools::Algorithms::Sort::ByPosition
\brief Function object for comparing alignments by position
Default sort order is Sort::AscendingOrder.
\code
std::vector<BamAlignment> a;
// sort by position, in ascending order (the following two lines are equivalent):
std::sort( a.begin(), a.end(), Sort::ByPosition() );
std::sort( a.begin(), a.end(), Sort::ByPosition(Sort::AscendingOrder) );
// OR sort in descending order
std::sort( a.begin(), a.end(), Sort::ByPosition(Sort::DescendingOrder) );
\endcode
*/
struct ByPosition : public AlignmentSortBase {
// ctor
ByPosition(const Sort::Order& order = Sort::AscendingOrder)
: m_order(order)
{ }
// comparison function
bool operator()(const BamTools::BamAlignment& lhs, const BamTools::BamAlignment& rhs) {
// force unmapped aligmnents to end
if ( lhs.RefID == -1 ) return false;
if ( rhs.RefID == -1 ) return true;
// if on same reference, sort on position
if ( lhs.RefID == rhs.RefID )
return sort_helper(m_order, lhs.Position, rhs.Position);
// otherwise sort on reference ID
return sort_helper(m_order, lhs.RefID, rhs.RefID);
}
// used by BamMultiReader internals
static inline bool UsesCharData(void) { return false; }
// data members
private:
const Sort::Order m_order;
};
/*! \struct BamTools::Algorithms::Sort::ByTag
\brief Function object for comparing alignments by tag value
Default sort order is Sort::AscendingOrder.
\code
std::vector<BamAlignment> a;
// sort by edit distance, in ascending order (the following two lines are equivalent):
std::sort( a.begin(), a.end(), Sort::ByTag<int>("NM") );
std::sort( a.begin(), a.end(), Sort::ByTag<int>("NM", Sort::AscendingOrder) );
// OR sort in descending order
std::sort( a.begin(), a.end(), Sort::ByTag<int>("NM", Sort::DescendingOrder) );
\endcode
*/
template<typename T>
struct ByTag : public AlignmentSortBase {
// ctor
ByTag(const std::string& tag,
const Sort::Order& order = Sort::AscendingOrder)
: m_tag(tag)
, m_order(order)
{ }
// comparison function
bool operator()(const BamTools::BamAlignment& lhs, const BamTools::BamAlignment& rhs) {
// force alignments without tag to end
T lhsTagValue;
T rhsTagValue;
if ( !lhs.GetTag(m_tag, lhsTagValue) ) return false;
if ( !rhs.GetTag(m_tag, rhsTagValue) ) return true;
// otherwise compare on tag values
return sort_helper(m_order, lhsTagValue, rhsTagValue);
}
// used by BamMultiReader internals
static inline bool UsesCharData(void) { return true; }
// data members
private:
const std::string m_tag;
const Sort::Order m_order;
};
/*! \struct BamTools::Algorithms::Sort::Unsorted
\brief Placeholder function object
This function object exists purely to allow for dropping a "do not care" ordering
into methods, containers, etc that are designed to work with the other sorting objects.
\code
std::set<BamAlignment, Sort::ByName>; // STL set, ordered on alignment name
std::set<BamAlignment, Sort::Unsorted>; // STL set, unsorted (but probably insertion order)
\endcode
*/
struct Unsorted : public AlignmentSortBase {
// comparison function
inline bool operator()(const BamTools::BamAlignment&, const BamTools::BamAlignment&) {
return false; // returning false tends to retain insertion order
}
// used by BamMultiReader internals
static inline bool UsesCharData(void) { return false; }
};
/*! Sorts a std::vector of alignments (in-place), using the provided compare function.
\code
std::vector<BamAlignemnt> a;
// populate data
// sort our alignment list by edit distance
Sort::SortAlignments(a, Sort::ByTag<int>("NM"));
\endcode
\param[in,out] data vector of alignments to be sorted
\param[in] comp comparison function object
*/
template<typename Compare>
static inline void SortAlignments(std::vector<BamAlignment>& data,
const Compare& comp = Compare())
{
std::sort(data.begin(), data.end(), comp);
}
/*! Returns a sorted copy of the input alignments, using the provided compare function.
\code
std::vector<BamAlignemnt> a;
// populate data
// get a copy of our original data, sorted by edit distance (descending order)
std::vector<BamAligment> sortedData;
sortedData = Sort::SortAlignments(a, Sort::ByTag<int>("NM", Sort::DescendingOrder));
\endcode
\param[in] input vector of alignments to be sorted
\param[in] comp comparison function object
\return sorted copy of the input data
*/
template<typename Compare>
static inline std::vector<BamAlignment> SortAlignments(const std::vector<BamAlignment>& input,
const Compare& comp = Compare())
{
std::vector<BamAlignment> output(input);
SortAlignments(output, comp);
return output;
}
/*! Reads a region of alignments from a position-sorted BAM file,
then sorts by the provided compare function
\code
BamReader reader;
// open BAM file & index file
BamRegion region;
// define a region of interest (i.e. a exon or some other feature)
// get all alignments covering that region, sorted by read group name
std::vector<BamAlignments> a;
a = Sort::GetSortedRegion(reader, region, Sort::ByTag<std::string>("RG"));
\endcode
\param[in] reader BamReader opened on desired BAM file
\param[in] region desired region-of-interest
\param[in] comp comparison function object
\return sorted vector of the region's alignments
*/
template<typename Compare>
static std::vector<BamAlignment> GetSortedRegion(BamReader& reader,
const BamRegion& region,
const Compare& comp = Compare())
{
// return empty container if unable to find region
if ( !reader.IsOpen() ) return std::vector<BamAlignment>();
if ( !reader.SetRegion(region) ) return std::vector<BamAlignment>();
// iterate through region, grabbing alignments
BamAlignment al;
std::vector<BamAlignment> results;
while ( reader.GetNextAlignmentCore(al) )
results.push_back(al);
// sort & return alignments
SortAlignments(results, comp);
return results;
}
/*! Reads a region of alignments from position-sorted BAM files,
then sorts by the provided compare function
\code
BamMultiReader reader;
// open BAM files & index files
BamRegion region;
// define a region of interest (i.e. a exon or some other feature)
// get all alignments covering that region, sorted by read group name
std::vector<BamAlignments> a;
a = Sort::GetSortedRegion(reader, region, Sort::ByTag<std::string>("RG"));
\endcode
\param[in] reader BamMultiReader opened on desired BAM files
\param[in] region desired region-of-interest
\param[in] comp comparison function object
\return sorted vector of the region's alignments
*/
template<typename Compare>
static std::vector<BamAlignment> GetSortedRegion(BamMultiReader& reader,
const BamRegion& region,
const Compare& comp = Compare())
{
// return empty container if unable to find region
if ( !reader.HasOpenReaders() ) return std::vector<BamAlignment>();
if ( !reader.SetRegion(region) ) return std::vector<BamAlignment>();
// iterate through region, grabbing alignments
BamAlignment al;
std::vector<BamAlignment> results;
while ( reader.GetNextAlignmentCore(al) )
results.push_back(al);
// sort & return alignments
SortAlignments(results, comp);
return results;
}
};
} // namespace Algorithms
} // namespace BamTools
#endif // ALGORITHMS_SORT_H
|