Loading...
Searching...
No Matches
Sort.h
Go to the documentation of this file.
// ***************************************************************************
// 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 <algorithm>
#include <cassert>
#include <functional>
#include <string>
#include <vector>
#include "api/BamAlignment.h"
#include "api/BamMultiReader.h"
#include "api/BamReader.h"
#include "api/api_global.h"
namespace BamTools {
namespace Algorithms {
struct API_EXPORT Sort
{
enum Order
{
AscendingOrder = 0,
DescendingOrder
};
template <typename ElemType>
static 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
}
struct ByName
{
// ctor
ByName(const Sort::Order& order = Sort::AscendingOrder)
: m_order(order)
{}
// comparison function
bool operator()(const BamTools::BamAlignment& lhs, const BamTools::BamAlignment& rhs) const
{
return sort_helper(m_order, lhs.Name, rhs.Name);
}
// used by BamMultiReader internals
static bool UsesCharData()
{
return true;
}
// data members
private:
const Sort::Order m_order;
};
struct ByPosition
{
// ctor
ByPosition(const Sort::Order& order = Sort::AscendingOrder)
: m_order(order)
{}
// comparison function
bool operator()(const BamTools::BamAlignment& lhs, const BamTools::BamAlignment& rhs) const
{
// 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 bool UsesCharData()
{
return false;
}
// data members
private:
const Sort::Order m_order;
};
template <typename T>
struct ByTag
{
// 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) const
{
// 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 bool UsesCharData()
{
return true;
}
// data members
private:
const std::string m_tag;
const Sort::Order m_order;
};
struct Unsorted
{
// comparison function
bool operator()(const BamTools::BamAlignment&, const BamTools::BamAlignment&) const
{
return false; // returning false tends to retain insertion order
}
// used by BamMultiReader internals
static bool UsesCharData()
{
return false;
}
};
template <typename Compare>
static void SortAlignments(std::vector<BamAlignment>& data, const Compare& comp = Compare())
{
std::sort(data.begin(), data.end(), comp);
}
template <typename Compare>
static std::vector<BamAlignment> SortAlignments(const std::vector<BamAlignment>& input,
const Compare& comp = Compare())
{
std::vector<BamAlignment> output(input);
SortAlignments(output, comp);
return output;
}
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;
}
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
BamAlignment.h
BamMultiReader.h
BamReader.h
api_global.h
BamTools::BamAlignment
The main BAM alignment data structure.
Definition BamAlignment.h:34
BamTools::BamAlignment::GetTag
bool GetTag(const std::string &tag, T &destination) const
Definition BamAlignment.h:442
BamTools::BamAlignment::Position
int32_t Position
position (0-based) where alignment starts
Definition BamAlignment.h:133
BamTools::BamAlignment::RefID
int32_t RefID
ID number for reference sequence.
Definition BamAlignment.h:132
BamTools::BamAlignment::Name
std::string Name
read name
Definition BamAlignment.h:125
BamTools::BamMultiReader
Convenience class for reading multiple BAM files.
Definition BamMultiReader.h:27
BamTools::BamMultiReader::GetNextAlignmentCore
bool GetNextAlignmentCore(BamAlignment &alignment)
Retrieves next available alignment.
Definition BamMultiReader.cpp:197
BamTools::BamMultiReader::SetRegion
bool SetRegion(const BamRegion &region)
Sets a target region of interest.
Definition BamMultiReader.cpp:413
BamTools::BamMultiReader::HasOpenReaders
bool HasOpenReaders() const
Returns true if there are any open BAM files.
Definition BamMultiReader.cpp:245
BamTools::BamReader
Provides read access to BAM files.
Definition BamReader.h:26
BamTools::BamReader::SetRegion
bool SetRegion(const BamRegion &region)
Sets a target region of interest.
Definition BamReader.cpp:373
BamTools::BamReader::IsOpen
bool IsOpen() const
Returns true if a BAM file is open for reading.
Definition BamReader.cpp:234
BamTools::BamReader::GetNextAlignmentCore
bool GetNextAlignmentCore(BamAlignment &alignment)
Retrieves next available alignment, without populating the alignment's string data fields.
Definition BamReader.cpp:189
BamTools::Algorithms
Provides convenient classes & methods for working with BAM data.
Definition Sort.h:25
BamTools
Contains all BamTools classes & methods.
Definition Sort.h:24
BamTools::Algorithms::Sort::ByName::ByName
ByName(const Sort::Order &order=Sort::AscendingOrder)
Definition Sort.h:83
BamTools::Algorithms::Sort::ByName::operator()
bool operator()(const BamTools::BamAlignment &lhs, const BamTools::BamAlignment &rhs) const
Definition Sort.h:88
BamTools::Algorithms::Sort::ByPosition::operator()
bool operator()(const BamTools::BamAlignment &lhs, const BamTools::BamAlignment &rhs) const
Definition Sort.h:129
BamTools::Algorithms::Sort::ByPosition::ByPosition
ByPosition(const Sort::Order &order=Sort::AscendingOrder)
Definition Sort.h:124
BamTools::Algorithms::Sort::ByTag::ByTag
ByTag(const std::string &tag, const Sort::Order &order=Sort::AscendingOrder)
Definition Sort.h:181
BamTools::Algorithms::Sort::ByTag::operator()
bool operator()(const BamTools::BamAlignment &lhs, const BamTools::BamAlignment &rhs) const
Definition Sort.h:187
BamTools::Algorithms::Sort::Unsorted
Placeholder function object.
Definition Sort.h:228
BamTools::Algorithms::Sort::Unsorted::operator()
bool operator()(const BamTools::BamAlignment &, const BamTools::BamAlignment &) const
Definition Sort.h:231
BamTools::Algorithms::Sort
Provides classes & methods related to sorting BamAlignments.
Definition Sort.h:31
BamTools::Algorithms::Sort::GetSortedRegion
static std::vector< BamAlignment > GetSortedRegion(BamReader &reader, const BamRegion &region, const Compare &comp=Compare())
Definition Sort.h:307
BamTools::Algorithms::Sort::GetSortedRegion
static std::vector< BamAlignment > GetSortedRegion(BamMultiReader &reader, const BamRegion &region, const Compare &comp=Compare())
Definition Sort.h:351
BamTools::Algorithms::Sort::SortAlignments
static void SortAlignments(std::vector< BamAlignment > &data, const Compare &comp=Compare())
Definition Sort.h:257
BamTools::Algorithms::Sort::Order
Order
Provides explicit values for specifying desired sort ordering.
Definition Sort.h:35
BamTools::Algorithms::Sort::DescendingOrder
@ DescendingOrder
Definition Sort.h:37
BamTools::Algorithms::Sort::AscendingOrder
@ AscendingOrder
Definition Sort.h:36
BamTools::Algorithms::Sort::SortAlignments
static std::vector< BamAlignment > SortAlignments(const std::vector< BamAlignment > &input, const Compare &comp=Compare())
Definition Sort.h:278
BamTools::BamRegion
Represents a sequential genomic region.
Definition BamAux.h:90