1 #ifndef UTIL___ALIGN_RANGE_COLL__HPP
2 #define UTIL___ALIGN_RANGE_COLL__HPP
56 return "CAlignRangeCollection - operation resulted in invalid state.";
74 template<
class TAlnRange>
84 typedef typename TAlignRangeVector::size_type
size_type;
179 return std::lower_bound(
begin(),
end(),
r.GetFirstFrom(), p);
184 if (
r.GetLength() > 0) {
202 if (
r.GetLength() > 0) {
232 if (it_ins !=
end()) {
249 if (it_ins ==
end()) {
317 pair<const_iterator, bool> res =
find_2(pos);
318 return res.second ? res.first :
m_Ranges.end();
329 bool b_contains = (it !=
end() && it->GetFirstFrom() <= pos);
330 return make_pair(it, b_contains);
336 return std::lower_bound(
begin(),
end(), pos, p);
342 return std::upper_bound(
begin(),
end(), pos, p);
350 return begin()->GetFirstFrom();
353 return TAlignRange::GetEmptyFrom();
360 return rbegin()->GetFirstToOpen();
363 return TAlignRange::GetEmptyToOpen();
370 return rbegin()->GetFirstTo();
373 return TAlignRange::GetEmptyTo();
381 return rbegin()->GetFirstToOpen() - from;
412 pair<const_iterator, bool> res =
find_2(pos);
417 if (res.first !=
end()) {
419 pos = res.first->GetFirstFrom();
423 if (res.first !=
begin()) {
426 pos = res.first->GetFirstTo();
430 return res.second ? res.first->GetSecondPosByFirstPos(pos)
456 else if(dir !=
eNone) {
462 res_pos =
r.GetSecondFrom();
463 dist = res_pos - pos;
466 res_pos = it->GetSecondTo();
467 dist = pos - res_pos;
469 if (dist > 0 && (min_dist == -1 || min_dist > dist)) {
477 return (it_closest ==
end()) ? -1 : it_closest->GetFirstPosBySecondPos(min_pos);
504 vector<bool>* dead =
NULL;
505 size_t range_size =
m_Ranges.size();
507 for (
size_t i = range_size - 1;
i > 0; --
i) {
510 dead =
new vector<bool>(range_size,
false);
511 dead->reserve(range_size);
519 for (
size_t i = 0;
i < range_size;
i++ ) {
528 m_Ranges.resize(range_size - shift);
542 if(
flags == complete) {
578 swap(r_left, r_right);
667 typedef typename TAlignRangeVector::iterator
iterator;
691 rg.SetFirstFrom(rg.GetFirstFrom()*3);
692 rg.SetSecondFrom(rg.GetSecondFrom()*3);
693 rg.SetLength(rg.GetLength()*3);
696 rg.SetFirstFrom(rg.GetFirstFrom()*3);
697 rg.SetSecondFrom(rg.GetSecondFrom()*3);
698 rg.SetLength(rg.GetLength()*3);
703 typename TAlignRangeVector::iterator it = ranges.begin();
704 while (it != ranges.end()) {
705 if (!it->IntersectingWith(
range)) {
706 typename TAlignRangeVector::iterator del = it;
711 if (it->GetFirstFrom() <
range.GetFrom() || it->GetFirstTo() >
range.GetTo()) {
712 it->IntersectWith(
range);
719 typename TAlignRangeVector::iterator it = ranges.begin();
720 while (it != ranges.end()) {
721 if (it->GetSecondTo() <
range.GetFrom() || it->GetSecondFrom() >
range.GetTo()) {
722 typename TAlignRangeVector::iterator del = it;
727 if (it->GetSecondFrom() <
range.GetFrom() || it->GetSecondTo() >
range.GetTo()) {
728 it->IntersectSecondWith(
range);
751 template<
class TAlnRange>
789 return *
this += -
delta;
824 return m_Iter == iter.m_Iter;
828 return !(*
this == iter);
866 return *
this += -
delta;
965 const_iterator
end()
const
995 return k1->GetFirstFrom() < k2->GetFirstFrom();
999 return k1->GetFirstFrom() < pos;
1003 return pos < k2->GetFirstFrom();
1010 return k1->GetSecondFrom() < k2->GetSecondFrom();
1014 return k1->GetSecondFrom() < pos;
1018 return pos < k2->GetSecondFrom();
1035 if (
r.GetLength() > 0) {
1053 const_iterator it_ins =
end();
1054 if (
r.GetLength() > 0) {
1060 auto it_left =
prev(it);
1061 if (it_left->IsAbutting(
r)) {
1068 r.CombineWithAbutting(*it_left);
1078 if (it->IsAbutting(
r)) {
1083 r.CombineWithAbutting(*it);
1104 const_iterator
erase(const_iterator it_del)
1106 const_iterator it_res =
end();
1108 if (it_del != it_res) {
1109 it_res =
next(it_del);
1162 pair<const_iterator, bool> res =
find_2(pos);
1163 return res.second ? res.first :
end();
1173 bool b_contains = (it !=
end() && it->GetFirstFrom() <= pos);
1174 return make_pair(it, b_contains);
1183 return res.second? res.first.first:
end();
1206 return std::lower_bound(
begin(),
end(), pos, p);
1212 return std::upper_bound(
begin(),
end(), pos, p);
1221 return TAlignRange::GetEmptyFrom();
1224 return begin()->GetFirstFrom();
1231 return TAlignRange::GetEmptyToOpen();
1234 return rbegin()->GetFirstToOpen();
1241 return TAlignRange::GetEmptyTo();
1244 return rbegin()->GetFirstTo();
1255 return rbegin()->GetFirstToOpen() - from;
1283 pair<const_iterator, bool> res =
find_2(pos);
1286 if ( !res.second ) {
1288 if ( res.first !=
end() ) {
1290 pos = res.first->GetFirstFrom();
1294 if ( res.first !=
begin() ) {
1297 pos = res.first->GetFirstTo();
1301 return res.second ? res.first->GetSecondPosByFirstPos(pos)
1309 if ( dir ==
eLeft ) {
1313 else if ( dir ==
eRight ) {
1319 const_iterator it_closest =
end();
1322 pair<pair<const_iterator, const_iterator>,
bool> res =
find_2_by_second(pos);
1323 if ( !res.second ) {
1325 if ( res.first.first !=
end() ) {
1331 it_closest = res.first.first;
1332 min_pos =
r.GetSecondFrom();
1333 min_dist =
r.GetSecondFrom() - pos;
1336 if ( res.first.second !=
end() ) {
1342 int dist = pos -
r.GetSecondTo();
1343 if ( min_dist < 0 || dist < min_dist ) {
1344 it_closest = res.first.second;
1345 min_pos =
r.GetSecondTo();
1350 if ( min_dist >= 0 ) {
1355 it_closest = res.first.first;
1358 return min_dist >= 0? it_closest->GetFirstPosBySecondPos(pos): -1;
1361 const_iterator it_closest =
end();
1364 for (const_iterator it =
begin(); it !=
end(); it++) {
1370 else if(dir !=
eNone) {
1376 res_pos =
r.GetSecondFrom();
1377 dist = res_pos - pos;
1380 res_pos = it->GetSecondTo();
1381 dist = pos - res_pos;
1383 if (dist > 0 && (min_dist == -1 || min_dist > dist)) {
1391 return (it_closest ==
end()) ? -1 : it_closest->GetFirstPosBySecondPos(min_pos);
1400 if (
next == where ) {
1432 i = i_next, ++i_next ) {
1433 if (
i->IsAbutting(*i_next) ) {
1435 i_next->CombineWithAbutting(*
i);
1455 i = i_next, ++i_next ) {
1457 if(
flags == complete) {
1494 swap(r_left, r_right);
1518 for (
auto&
i : insertions) {
1529 for (
auto&
i : collection ) {
1612 auto pos = iter->GetFirstFrom();
1614 if ( *it == iter ) {
1619 _ASSERT(0&&
"Cannot find range in m_IndexByFirst");
1623 auto pos = iter->GetSecondFrom();
1625 if ( *it == iter ) {
1630 _ASSERT(0&&
"Cannot find range in m_IndexBySecond");
1680 auto prev_iter =
prev(iter);
1681 _ASSERT((*prev_iter)->GetFirstFrom() <= pos);
1682 if ( (*prev_iter)->GetFirstToOpen() > pos ) {
1700 pair<pair<const_iterator, const_iterator>,
bool>
1703 pair<pair<const_iterator, const_iterator>,
bool> ret;
1707 auto prev_iter =
prev(iter);
1708 _ASSERT((*prev_iter)->GetSecondFrom() <= pos);
1709 if ( (*prev_iter)->GetSecondToOpen() > pos ) {
1711 ret.first.first = *prev_iter;
1712 ret.first.second =
end();
1715 ret.first.second = *prev_iter;
1718 ret.first.second =
end();
1721 ret.first.first = *iter;
1724 ret.first.first =
end();
1743 rg.SetFirstFrom(rg.GetFirstFrom()*3);
1744 rg.SetSecondFrom(rg.GetSecondFrom()*3);
1745 rg.SetLength(rg.GetLength()*3);
1748 rg.SetFirstFrom(rg.GetFirstFrom()*3);
1749 rg.SetSecondFrom(rg.GetSecondFrom()*3);
1750 rg.SetLength(rg.GetLength()*3);
1753 rg.SetFirstFrom(rg.GetFirstFrom()*3);
1754 rg.SetSecondFrom(rg.GetSecondFrom()*3);
1755 rg.SetLength(rg.GetLength()*3);
1759 template<
class TRanges>
1761 typename TRanges::iterator it = ranges.begin();
1762 while (it != ranges.end()) {
1763 if (!it->IntersectingWith(
range)) {
1764 typename TRanges::iterator del = it;
1769 if (it->GetFirstFrom() <
range.GetFrom() || it->GetFirstTo() >
range.GetTo()) {
1770 it->IntersectWith(
range);
1776 template<
class TRanges>
1778 typename TRanges::iterator it = ranges.begin();
1779 while (it != ranges.end()) {
1780 if (it->GetSecondTo() <
range.GetFrom() || it->GetSecondFrom() >
range.GetTo()) {
1781 typename TRanges::iterator del = it;
1786 if (it->GetSecondFrom() <
range.GetFrom() || it->GetSecondTo() >
range.GetTo()) {
1787 it->IntersectSecondWith(
range);
1808 template <
class TColl>
1856 (
r->GetSecondFrom(),
r));
1921 return p.second->GetSecondTo() < pos;
1926 return pos < p.second->GetSecondTo();
1931 return p1.second->GetSecondTo() < p2.second->GetSecondTo();
1944 template<
class TAlnRange>
1949 m_Ranges.assign(src.
begin(), src.
end());
vector< TRangeWithFuzz > TRanges
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
CAlignRangeCollException()
CAlignRangeCollExtender this is an adapter for a collection that perfroms some of the operations fast...
position_type GetSecondTo() const
position_type GetSecondToOpen() const
void Init(const TColl &coll)
CAlignRangeCollExtender(const TColl &coll)
multimap< position_type, const TAlignRange * > TFrom2Range
position_type GetSecondFrom() const
TColl::ESearchDirection TSearchDir
CRange< position_type > GetSecondRange() const
const_iterator FindOnSecond(position_type pos) const
finds a segment at logarithmic time (linear time on first call) returns an iterator to the fisrt segm...
const_iterator end() const
TFrom2Range::const_iterator const_iterator
TColl::position_type position_type
position_type GetSecondLength(void) const
TColl::TAlignRange TAlignRange
const_iterator begin() const
const_iterator operator++(int)
const_iterator & operator--()
pointer operator->() const
const_iterator operator+(int delta)
bidirectional_iterator_tag iterator_category
const_iterator(const TRawIterator &iter)
const TAlignRange * pointer
ptrdiff_t difference_type
const TAlignRange & reference
const_iterator operator-(int delta)
reference operator*() const
const_iterator & operator-=(int delta)
bool operator==(const const_iterator &iter) const
const_iterator & operator+=(int delta)
const_iterator & operator++()
const_iterator operator--(int)
bool operator!=(const const_iterator &iter) const
iterator & operator+=(int delta)
reference operator*() const
iterator operator-(int delta)
iterator(const TRawIterator &i)
iterator operator+(int delta)
pointer operator->() const
iterator & operator-=(int delta)
class CAlignRangeCollectionList<TAlignRange> represent a sorted collection of TAlignRange.
const_iterator find_insertion_point(const TAlignRange &range) const
bool IsSet(int flags) const
static int ValidateRanges(const TAlignRange &r_1, const TAlignRange &r_2)
determine conflicts between two ranges
int GetStateFlags() const
const_iterator x_FindRangeContaining(position_type pos) const
int GetPolicyFlags() const
TIndexBySecond m_IndexBySecond
CAlignRangeCollectionList(const TAlignRangeVectorImpl &v, int flags)
const_iterator begin() const
const_iterator insert(const TAlignRange &r)
ESearchDirection
adding empty ranges is considered valid, they are simply ignored
CAlignRangeCollectionList< TAlignRange > TThisType
void x_Unindex(TIndexKey iter)
const_iterator insert(const_iterator it, const TAlignRange &arg_r)
TAlignRangeVectorImpl m_RangesVector
void x_UnindexBySecond(TIndexKey iter)
const TAlignRange & operator[](size_type pos) const
Use iterator access.
void IntersectFirst(const CRange< position_type > &range)
list< TAlignRange > TAlignRangeList
pair< const_iterator, bool > find_2(position_type pos) const
returns an iterator pointing to a range containing "pos"; if such a range does not exists an iterator...
const_reverse_iterator rbegin() const
position_type GetFirstLength(void) const
TSignedSeqPos GetSecondPosByFirstPos(position_type pos, ESearchDirection dir=eNone) const
void IntersectSecond(const CRange< position_type > &range)
void Normalize()
ensures that segments are sorted, if fAllowAdjust is not set - merges adjacent segments
pair< pair< const_iterator, const_iterator >, bool > find_2_by_second(position_type pos) const
returns an iterator pointing to a range containing "pos"; if such a range does not exists an iterator...
void push_back(const TAlignRange &r)
void x_IntersectFirst(TRanges &ranges, const CRange< position_type > &range)
TAlignRangeList::size_type size_type
bool x_Equals(const TThisType &c) const
iterator x_FindInsertionPlace(const TAlignRange &r)
position_type GetFirstFrom() const
TAlignRangeList::const_reverse_iterator const_reverse_iterator
const TIndexByFirst & GetIndexByFirst() const
CRange< position_type > GetFirstRange() const
@ fMixedDir
contains at least one reversed range
@ fPolicyMask
Keep compatible with SC-8.0.
@ fNotValidated
State flags:
@ fAllowOverlap
allow segments with different orientation
@ fAllowAbutting
allow segments overlapping on the first sequence
@ fInvalid
collection was modified and not validated
@ fIgnoreInsertions
allows segments not separated by gaps
@ fAllowMixedDir
enforce all policies after any modification
@ fKeepNormalized
Policies:
@ fReversed
contains at least one direct range
@ fDefaultPolicy
do not store insertions
@ fUnsorted
one or more policies violated
TSignedSeqPos GetFirstPosBySecondPos(position_type pos, ESearchDirection dir=eNone) const
TAlignRangeList::iterator TRawIterator
CAlignRangeCollectionList(const TAlignRangeList &v, int flags)
const TIndexBySecond & GetIndexBySecond() const
const_iterator find(position_type pos) const
void x_SetFlags(int flags)
TIndexByFirst m_IndexByFirst
combination of EFlags
void CombineAbutting()
merge adjacent segments together, merging changes collection size and invalidates iterators
TAlignRangeList m_RangesList
const TInsertions & GetInsertions() const
Each insertion shows where the 'first' sequence has a gap while the 'second' sequence has the inserti...
void x_ResetFlags(int flags)
vector< TAlignRange > TInsertions
const_reverse_iterator rend() const
TRawIterator x_Insert(TRawIterator where, const TAlignRange &r)
void x_Erase(TRawIterator iter)
multiset< TIndexKey, PIndexBySecondLess > TIndexBySecond
void x_Index(TIndexKey iter)
const_iterator end() const
multiset< TIndexKey, PIndexByFirstLess > TIndexByFirst
vector< TAlignRange > TAlignRangeVector
position_type GetFirstTo() const
vector< TAlignRange > TAlignRangeVectorImpl
Use iterators on TAlignRangeList or TInsertions for insertions.
position_type GetFirstToOpen() const
void x_UnindexByFirst(TIndexKey iter)
const_iterator find_by_second(position_type pos) const
const_iterator erase(const_iterator it_del)
void x_MultiplyCoordsBy3()
void Validate()
analyses segements and updates flags
void AddInsertion(const TAlignRange &r)
TAlignRangeVectorImpl & x_GetAlignRangeVector() const
void x_IntersectSecond(TRanges &ranges, const CRange< position_type > &range)
TAlignRange::position_type position_type
void AddInsertions(const TInsertions &insertions)
void AddInsertions(const TThisType &collection)
CAlignRangeCollectionList(int flags=fDefaultPolicy)
pair< pair< const_iterator, const_iterator >, bool > x_FindRangesBySecondContaining(position_type pos) const
void SortInsertions(void)
class CAlignRangeCollection<TAlignRange> represent a sorted collection of TAlignRange.
position_type GetFirstTo() const
void Normalize()
ensures that segments are sorted, if fAllowAdjust is not set - merges adjacent segments
void x_IntersectSecond(TAlignRangeVector &ranges, const CRange< position_type > &range)
TAlignRangeVector m_Ranges
const_reverse_iterator rend() const
TAlignRangeVector::const_reverse_iterator const_reverse_iterator
TAlignRangeVector::const_iterator const_iterator
vector< TAlignRange > TAlignRangeVector
const_iterator insert(const_iterator where, const TAlignRange &r)
bool IsSet(int flags) const
CAlignRangeCollection(const TAlignRangeVector &v, int flags)
int GetPolicyFlags() const
void AddInsertions(const TThisType &collection)
TSignedSeqPos GetFirstPosBySecondPos(position_type pos, ESearchDirection dir=eNone) const
void push_back(const TAlignRange &r)
void CombineAbutting()
merge adjacent segments together, merging changes collection size and invalidates iterators
CRange< position_type > GetFirstRange() const
position_type GetFirstToOpen() const
const_iterator lower_bound(position_type pos) const
@ fAllowOverlap
allow segments with different orientation
@ fMixedDir
contains at least one reversed range
@ fPolicyMask
Keep compatible with SC-8.0.
@ fIgnoreInsertions
allows segments not separated by gaps
@ fAllowAbutting
allow segments overlapping on the first sequence
@ fAllowMixedDir
enforce all policies after any modification
@ fInvalid
collection was modified and not validated
@ fKeepNormalized
Policies:
@ fUnsorted
one or more policies violated
@ fDefaultPolicy
do not store insertions
@ fNotValidated
State flags:
@ fReversed
contains at least one direct range
void AddInsertions(const TAlignRangeVector &insertions)
position_type GetFirstFrom() const
void Validate()
analyses segements and updates flags
const_iterator insert(const TAlignRange &r)
void x_SetFlags(int flags)
const_iterator upper_bound(position_type pos) const
TSignedSeqPos GetSecondPosByFirstPos(position_type pos, ESearchDirection dir=eNone) const
const_iterator erase(const_iterator it)
bool x_Equals(const TThisType &c) const
void IntersectSecond(const CRange< position_type > &range)
const TAlignRange & operator[](size_type pos) const
position_type GetFirstLength(void) const
const_iterator find(position_type pos) const
void x_ResetFlags(int flags)
ESearchDirection
adding empty ranges is considered valid, they are simply ignored
CAlignRangeCollection(int flags=fDefaultPolicy)
void SortInsertions(void)
TAlignRange::position_type position_type
const_iterator end() const
TAlignRangeVector::iterator iterator
const_reverse_iterator rbegin() const
TAlignRangeVector m_Insertions
void AddInsertion(const TAlignRange &r)
int GetStateFlags() const
TAlignRangeVector::size_type size_type
void x_IntersectFirst(TAlignRangeVector &ranges, const CRange< position_type > &range)
void Assign(const CAlignRangeCollectionList< TAlignRange > &src)
pair< const_iterator, bool > find_2(position_type pos) const
returns an iterator pointing to a range containing "pos"; if such a range does not exists an iterator...
void x_MultiplyCoordsBy3()
CAlignRangeCollection< TAlignRange > TThisType
const TAlignRangeVector & GetInsertions() const
Each insertion shows where the 'first' sequence has a gap while the 'second' sequence has the inserti...
const_iterator begin() const
TAlignRangeVector::reverse_iterator reverse_iterator
const_iterator find_insertion_point(const TAlignRange &r)
static int ValidateRanges(const TAlignRange &r_1, const TAlignRange &r_2)
determine conflicts between two ranges
TAlignRange & operator[](size_type pos)
void IntersectFirst(const CRange< position_type > &range)
CAlignRange Represents an element of pairwise alignment of two sequences.
container_type::const_iterator const_iterator
const_iterator end() const
iterator insert(const value_type &val)
const_iterator begin() const
container_type::value_type value_type
const_iterator begin() const
const_iterator upper_bound(const key_type &key) const
iterator insert(const value_type &val)
const_iterator end() const
const_iterator lower_bound(const key_type &key) const
Include a standard set of the NCBI C++ Toolkit most basic headers.
static DLIST_TYPE *DLIST_NAME() prev(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
pair< TRange, CRef< CSeq_align > > TAlignRange
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
int TSignedSeqPos
Type for signed sequence position.
void swap(NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair1, NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair2)
bool IsAbutting(const TThisType &r) const
position_type GetFirstToOpen(void) const
bool NotEmpty(void) const
TThisType & CombineWithAbutting(const TThisType &r)
position_type GetFirstFrom(void) const
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
range(_Ty, _Ty) -> range< _Ty >
constexpr auto sort(_Init &&init)
Defines NCBI C++ exception handling.
The NCBI C++/STL use hints.
Int4 delta(size_t dimension_, const Int4 *score_)
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
bool operator()(const typename TFrom2Range::value_type &p1, const typename TFrom2Range::value_type &p2)
TAlignRange::position_type position_type
bool operator()(const typename TFrom2Range::value_type &p, position_type pos)
bool operator()(position_type pos, const typename TFrom2Range::value_type &p)
bool operator()(position_type pos, const TIndexKey &k2) const
bool operator()(const TIndexKey &k1, const TIndexKey &k2) const
bool operator()(const TIndexKey &k1, position_type pos) const
bool operator()(position_type pos, const TIndexKey &k2) const
bool operator()(const TIndexKey &k1, const TIndexKey &k2) const
bool operator()(const TIndexKey &k1, position_type pos) const