NCBI C++ ToolKit
sparse_iterator.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: sparse_iterator.cpp 25706 2012-04-25 00:21:04Z voronov $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 
38 
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// CSparseIterator
42 
44 {
45  if( ! (bool)*this) {
46  m_Segment.Init(-1, -1, -1, -1, IAlnSegment::fInvalid); // invalid
47  } else {
48  int dir_flag = m_It_1->IsDirect() ? 0 : IAlnSegment::fReversed;
49 
50  if(m_It_1 == m_It_2) { // aligned segment
51  if(m_Clip && (m_It_1 == m_Clip->m_First_It || m_It_1 == m_Clip->m_Last_It_1)) {
52  // we need to clip the current segment
53  TAlignRange r = *m_It_1;
55  r.IntersectWith(clip);
56 
57  m_Segment.Init(r.GetFirstFrom(), r.GetFirstTo(),
58  r.GetSecondFrom(), r.GetSecondTo(), IAlnSegment::fAligned | dir_flag);
59  } else {
60  const TAlignRange& r = *m_It_1;
61  m_Segment.Init(r.GetFirstFrom(), r.GetFirstTo(),
62  r.GetSecondFrom(), r.GetSecondTo(), IAlnSegment::fAligned | dir_flag);
63  }
64  } else { // gap
65  TSignedSeqPos from = m_It_2->GetSecondToOpen();
66  TSignedSeqPos to = m_It_1->GetSecondFrom() - 1;
67 
68  if(m_Clip && (m_It_1 == m_Clip->m_First_It || m_It_1 == m_Clip->m_Last_It_1)) {
69  TSignedRange r(m_It_2->GetFirstToOpen(), m_It_1->GetFirstFrom() - 1);
71  r.IntersectWith(clip);
72 
73  m_Segment.Init(r.GetFrom(), r.GetTo(), from, to, IAlnSegment::fGap);
74  } else {
75  m_Segment.Init(m_It_2->GetFirstToOpen(), m_It_1->GetFirstFrom() - 1,
76  from, to, IAlnSegment::fGap);
77  }
78  }
79  }
80 }
81 
82 // assuming clipping range
84 {
85  _ASSERT(m_Coll);
86 
87  bool first_gap = false, last_gap = false;
88  if(m_Clip) {
89  // adjsut starting position
90  TSignedSeqPos from = m_Clip->m_From;
91 
92  pair<TAlignColl::const_iterator, bool> res = m_Coll->find_2(from);
93 
94  // set start position
95  if(res.second) {
96  _ASSERT(res.first->FirstContains(from)); // pos inside a segment
97  m_Clip->m_First_It = res.first;
98  } else { // pos is in the gap
99  m_Clip->m_First_It = res.first;
100  if(res.first != m_Coll->begin()) {
101  first_gap = true;
102  }
103  }
104 
105  // set end condition
106  TSignedSeqPos to = m_Clip->m_ToOpen - 1;
107  res = m_Coll->find_2(to);
108 
109  if(res.second) {
110  _ASSERT(res.first->FirstContains(to)); // pos inside a segment
111  m_Clip->m_Last_It_1 = m_Clip->m_Last_It_2 = res.first;
112  } else {
113  // pos is in the gap
114  if(res.first == m_Coll->end()) {
115  m_Clip->m_Last_It_1 = m_Clip->m_Last_It_2 = res.first - 1;
116  } else {
117  m_Clip->m_Last_It_1 = res.first;
118  m_Clip->m_Last_It_2 = res.first - 1;
119  last_gap = true;
120  }
121  }
122  // initialize iterators
124  } else { // no clip
125  m_It_1 = m_It_2 = m_Coll->begin();
126  }
127 
128  // adjsutiterators
129  switch(m_Flag) {
130  case eAllSegments:
131  if(first_gap) {
132  --m_It_2;
133  }
134  break;
135  case eSkipGaps:
136  if(last_gap) {
137  --m_Clip->m_Last_It_1;
138  }
139  break;
140  case eInsertsOnly:
141  if(first_gap) {
142  --m_It_2;
143  } else {
144  ++m_It_1;
145  }
146  if( ! x_IsInsert()) {
147  ++(*this);
148  }
149  break;
150  case eSkipInserts:
151  if(first_gap && x_IsInsert()) {
152  ++(*this);
153  }
154  }
155 
156  x_InitSegment();
157 }
158 
159 
161 : m_Coll(NULL),
162  m_Clip(NULL)
163 {
164  x_InitSegment();
165 }
166 
167 
169 : m_Coll(&coll),
170  m_Flag(flag),
171  m_Clip(NULL)
172 {
173  x_InitIterator();
174 }
175 
176 
179 : m_Coll(&coll),
180  m_Flag(flag),
181  m_Clip(NULL)
182 {
183  if(m_Coll) {
184  m_Clip = new SClip;
185  m_Clip->m_From = range.GetFrom();
186  m_Clip->m_ToOpen = range.GetToOpen();
187  }
188 
189  x_InitIterator();
190 }
191 
192 
194 : m_Coll(orig.m_Coll),
195  m_Flag(orig.m_Flag),
196  m_It_1(orig.m_It_1),
197  m_It_2(orig.m_It_2)
198 {
199  if(orig.m_Clip) {
200  m_Clip = new SClip(*orig.m_Clip);
201  }
202  x_InitSegment();
203 }
204 
205 
207 {
208  delete m_Clip;
209 }
210 
211 
213 {
214  return new CSparseIterator(*this);
215 }
216 
217 
218 CSparseIterator::operator bool() const
219 {
220  if(m_Coll && m_It_1 >= m_Coll->begin()) {
221  if(m_Clip) {
222  return (m_It_1 <= m_Clip->m_Last_It_1) && (m_It_2 <= m_Clip->m_Last_It_2);
223  }
224  }
225  return false;
226 }
227 
228 
230 {
231  _ASSERT(m_Coll);
232 
233  switch(m_Flag) {
234  case eAllSegments:
235  if(m_It_1 == m_It_2) { // aligned segment - move to gap
236  ++m_It_1;
237  } else { // gap - move to the next segment
238  ++m_It_2;
239  }
240  break;
241  case eSkipGaps:
242  ++m_It_1;
243  ++m_It_2;
244  break;
245  case eInsertsOnly:
246  do {
247  ++m_It_1;
248  ++m_It_2;
249  } while((bool)*this && m_It_1->GetFirstFrom() != m_It_2->GetFirstToOpen());
250  break;
251  case eSkipInserts:
252  if(m_It_1 == m_It_2) { // aligned segment - move to gap
253  ++m_It_1;
254  if(m_It_1->GetFirstFrom() == m_It_2->GetFirstTo()) { // insert - skip
255  ++m_It_2;
256  }
257  } else { // gap - move to the next segment
258  ++m_It_2;
259  }
260  break;
261  default:
262  _ASSERT(false); // not implemented
263  }
264 
265  x_InitSegment();
266  return *this;
267 }
268 
269 
271 {
272  if(typeid(*this) == typeid(it)) {
273  const CSparseIterator* sparse_it =
274  dynamic_cast<const CSparseIterator*>(&it);
275  return x_Equals(*sparse_it);
276  }
277  return false;
278 }
279 
280 
282 {
283  if(typeid(*this) == typeid(it)) {
284  const CSparseIterator* sparse_it =
285  dynamic_cast<const CSparseIterator*>(&it);
286  return ! x_Equals(*sparse_it);
287  }
288  return true;
289 }
290 
291 
293 {
294  _ASSERT(*this);
295  return m_Segment;
296 }
297 
298 
300 {
301  _ASSERT(*this);
302  return &m_Segment;
303 }
304 
305 
const_iterator end() const
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_iterator begin() const
CAlignRange Represents an element of pairwise alignment of two sequences.
Definition: align_range.hpp:63
CSparseIterator - IAlnSegmentIterator implementation for CAlnMap::CAlnChunkVec.
void x_InitSegment()
CSparseIterator.
TAlignColl::const_iterator m_It_1
bool x_IsInsert() const
SClip * m_Clip
iterating mode
virtual const value_type * operator->() const
virtual bool operator!=(const IAlnSegmentIterator &it) const
TAlignColl::const_iterator m_It_2
virtual IAlnSegmentIterator * Clone() const
Create a copy of the iterator.
virtual const value_type & operator*() const
virtual IAlnSegmentIterator & operator++()
postfix operators are not defined to avoid performance overhead
const TAlignColl * m_Coll
bool x_Equals(const CSparseIterator &it) const
CSparseSegment m_Segment
if m_It1 != m_It2 then the iterator points to a gap between m_It1 and m_It2
virtual bool operator==(const IAlnSegmentIterator &it) const
Compare iterators.
virtual ~CSparseIterator()
void Init(TSignedSeqPos aln_from, TSignedSeqPos aln_to, TSignedSeqPos from, TSignedSeqPos to, TSegTypeFlags type)
Definition: sparse_ci.hpp:51
Alignment segment iterator interface.
EFlags
Iterator options.
@ eSkipInserts
Iterate segments where at least some rows are aligned (including gap segments)
@ eInsertsOnly
Iterate only ranges not participating in the alignment (unaligned segments)
@ eAllSegments
Iterate all segments.
@ eSkipGaps
Skip gap segments (show only aligned ranges)
Alignment segment interface.
@ fAligned
Aligned segment.
@ fInvalid
The iterator is in bad state.
@ fReversed
The selected row is reversed (relative to the anchor).
@ fGap
Both anchor row and the selected row are not included in the segment (some other row is present and t...
#define bool
Definition: bool.h:34
int TSignedSeqPos
Type for signed sequence position.
Definition: ncbimisc.hpp:887
#define NULL
Definition: ncbistd.hpp:225
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
where boath are integers</td > n< td ></td > n</tr > n< tr > n< td > tse</td > n< td > optional</td > n< td > String</td > n< td class=\"description\"> TSE option controls what blob is orig
range(_Ty, _Ty) -> range< _Ty >
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
USING_SCOPE(ncbi::objects)
const_iterator m_Last_It_1
const_iterator m_Last_It_2
#define _ASSERT
Modified on Fri Sep 20 14:57:13 2024 by modify_doxy.py rev. 669887