1 // ==========================================================================
2 //                 SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2015, Knut Reinert, FU Berlin
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 //       notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above copyright
13 //       notice, this list of conditions and the following disclaimer in the
14 //       documentation and/or other materials provided with the distribution.
15 //     * Neither the name of Knut Reinert or the FU Berlin nor the names of
16 //       its contributors may be used to endorse or promote products derived
17 //       from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
23 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //
31 // ==========================================================================
32 //  Author: Andreas Gogol-Doering <andreas.doering@mdc-berlin.de>
33 // ==========================================================================
34 
35 #ifndef SEQAN_HEADER_ALIGN_ITERATOR_BASE_H
36 #define SEQAN_HEADER_ALIGN_ITERATOR_BASE_H
37 
38 namespace SEQAN_NAMESPACE_MAIN
39 {
40 
41 //////////////////////////////////////////////////////////////////////////////
42 // Align Iterator for Gaps alignment
43 //////////////////////////////////////////////////////////////////////////////
44 
45 // TODO(holtgrew): Extend class Iter?
46 /*!
47  * @class AlignColIterator
48  * @extends Iter
49  * @headerfile <seqan/align.h>
50  * @brief Iterator for alignment columns.
51  *
52  * @signature template <typename TAlign, typename TSpec>
53  *            class Iter<TAlign, AlignColIterator<TSpec> >;
54  *
55  * @tparam TAlign Align object to iterate columns of.
56  * @tparam TSpec  Tag for specializing the class further.
57  */
58 
59 template <typename TAlign, typename TSpec>
60 class Iter<TAlign, AlignColIterator<TSpec> >
61 {
62 public:
63     typedef typename Rows<TAlign>::Type TRows;
64     typedef typename Row<TAlign>::Type TRow;
65     typedef typename Iterator<TRow, Standard>::Type TRowIterator;
66     typedef typename Position<TRow>::Type TRowPosition;
67     typedef String<TRowIterator> TIterators;
68 
69     TAlign * data_host;
70     TIterators data_iterators;
71 
72 public:
Iter()73     Iter()
74     {
75 SEQAN_CHECKPOINT
76     }
Iter(TAlign & _align)77     Iter(TAlign & _align):
78         data_host(& _align)
79     {
80 SEQAN_CHECKPOINT
81         typename Position<TRows>::Type _i = length(rows(_align));
82         resize(data_iterators, _i, Exact());
83     }
Iter(TAlign & _align,TRowPosition _pos)84     Iter(TAlign & _align, TRowPosition _pos):
85         data_host(& _align)
86     {
87 SEQAN_CHECKPOINT
88         typename Position<TRows>::Type _i = length(rows(_align));
89         resize(data_iterators, _i, Exact());
90 
91         while (_i > 0)
92         {
93             --_i;
94             data_iterators[_i] = iter(row(_align, _i), _pos);
95         }
96     }
Iter(Iter const & _other)97     Iter(Iter const & _other):
98         data_host(_other.data_host),
99         data_iterators(_other.data_iterators)
100     {
101     }
~Iter()102     ~Iter()
103     {
104 SEQAN_CHECKPOINT
105     }
106 
107     Iter const &
108     operator = (Iter const & _other)
109     {
110 SEQAN_CHECKPOINT
111         data_host = _other.data_host;
112         data_iterators = _other.data_iterators;
113         return *this;
114     }
115 //____________________________________________________________________________
116 };
117 
118 //////////////////////////////////////////////////////////////////////////////
119 
120 // TODO(holtgrew); Document as dox/hosted?
121 
122 template <typename TAlign, typename TSpec>
123 inline TAlign &
host(Iter<TAlign,AlignColIterator<TSpec>> & me)124 host(Iter<TAlign, AlignColIterator<TSpec> > & me)
125 {
126 SEQAN_CHECKPOINT
127     return *me.data_host;
128 }
129 template <typename TAlign, typename TSpec>
130 inline TAlign &
host(Iter<TAlign,AlignColIterator<TSpec>> const & me)131 host(Iter<TAlign, AlignColIterator<TSpec> > const & me)
132 {
133 SEQAN_CHECKPOINT
134     return *me.data_host;
135 }
136 
137 //////////////////////////////////////////////////////////////////////////////
138 
139 template <typename TAlign, typename TSpec>
140 inline void
setHost(Iter<TAlign,AlignColIterator<TSpec>> & me,TAlign & _host)141 setHost(Iter<TAlign, AlignColIterator<TSpec> > & me, TAlign & _host)
142 {
143 SEQAN_CHECKPOINT
144     me.data_host = & _host;
145 }
146 
147 //////////////////////////////////////////////////////////////////////////////
148 
149 template <typename TAlign, typename TSpec>
150 inline typename Cols<TAlign>::Type
container(Iter<TAlign,AlignColIterator<TSpec>> & me)151 container(Iter<TAlign, AlignColIterator<TSpec> > & me)
152 {
153 SEQAN_CHECKPOINT
154     return cols(*me.data_host);
155 }
156 template <typename TAlign, typename TSpec>
157 inline typename Cols<TAlign>::Type
container(Iter<TAlign,AlignColIterator<TSpec>> const & me)158 container(Iter<TAlign, AlignColIterator<TSpec> > const & me)
159 {
160 SEQAN_CHECKPOINT
161     return cols(*me.data_host);
162 }
163 
164 //////////////////////////////////////////////////////////////////////////////
165 
166 template <typename TAlign, typename TSpec>
167 inline void
goNext(Iter<TAlign,AlignColIterator<TSpec>> & me)168 goNext(Iter<TAlign, AlignColIterator<TSpec> > & me)
169 {
170 SEQAN_CHECKPOINT
171     typedef typename Row<TAlign>::Type TRow;
172     typedef typename Iterator<TRow, Standard>::Type TRowIterator;
173     typedef String<TRowIterator> TIterators;
174     typedef typename Iterator<TIterators, Standard>::Type TIteratorsIterator;
175 
176     TIteratorsIterator _it = begin(me.data_iterators);
177     TIteratorsIterator _it_end = end(me.data_iterators);
178 
179     while (_it != _it_end)
180     {
181         goNext(*_it);
182         ++_it;
183     }
184 }
185 //____________________________________________________________________________
186 
187 template <typename TAlign, typename TSpec>
188 inline Iter<TAlign, AlignColIterator<TSpec> > &
189 operator ++(Iter<TAlign, AlignColIterator<TSpec> > & me)
190 {
191 SEQAN_CHECKPOINT
192     goNext(me);
193     return me;
194 }
195 //____________________________________________________________________________
196 
197 template <typename TAlign, typename TSpec>
198 inline Iter<TAlign, AlignColIterator<TSpec> >
199 operator ++(Iter<TAlign, AlignColIterator<TSpec> > & me, int)
200 {
201 SEQAN_CHECKPOINT
202     Iter<TAlign, AlignColIterator<TSpec> > ret = me;
203     goNext(me);
204     return ret;
205 }
206 
207 //////////////////////////////////////////////////////////////////////////////
208 
209 template <typename TAlign, typename TSpec>
210 inline void
goPrevious(Iter<TAlign,AlignColIterator<TSpec>> & me)211 goPrevious(Iter<TAlign, AlignColIterator<TSpec> > & me)
212 {
213 SEQAN_CHECKPOINT
214     typedef typename Row<TAlign>::Type TRow;
215     typedef typename Iterator<TRow, Standard>::Type TRowIterator;
216     typedef String<TRowIterator> TIterators;
217     typedef typename Iterator<TIterators, Standard>::Type TIteratorsIterator;
218 
219     TIteratorsIterator _it = begin(me.data_iterators);
220     TIteratorsIterator _it_end = end(me.data_iterators);
221 
222     while (_it != _it_end)
223     {
224         goPrevious(*_it);
225         ++_it;
226     }
227 }
228 //____________________________________________________________________________
229 
230 template <typename TAlign, typename TSpec>
231 inline Iter<TAlign, AlignColIterator<TSpec> > &
232 operator --(Iter<TAlign, AlignColIterator<TSpec> > & me)
233 {
234 SEQAN_CHECKPOINT
235     goPrevious(me);
236     return me;
237 }
238 //____________________________________________________________________________
239 
240 template <typename TAlign, typename TSpec>
241 inline Iter<TAlign, AlignColIterator<TSpec> >
242 operator --(Iter<TAlign, AlignColIterator<TSpec> > & me, int)
243 {
244 SEQAN_CHECKPOINT
245     Iter<TAlign, AlignColIterator<TSpec> > ret = me;
246     goPrevious(me);
247     return ret;
248 }
249 
250 //////////////////////////////////////////////////////////////////////////////
251 
252 template <typename TAlign1, typename TAlign2, typename TSpec>
253 inline bool
254 operator ==(Iter<TAlign1, AlignColIterator<TSpec> > & _left,
255             Iter<TAlign2, AlignColIterator<TSpec> > & _right)
256 {
257 SEQAN_CHECKPOINT
258     return getValue(_left.data_iterators, 0) == getValue(_right.data_iterators, 0);
259 }
260 template <typename TAlign1, typename TAlign2, typename TSpec>
261 inline bool
262 operator ==(Iter<TAlign1, AlignColIterator<TSpec> > const & _left,
263             Iter<TAlign2, AlignColIterator<TSpec> > & _right)
264 {
265 SEQAN_CHECKPOINT
266     return value(_left.data_iterators, 0) == value(_right.data_iterators, 0);
267 }
268 template <typename TAlign1, typename TAlign2, typename TSpec>
269 inline bool
270 operator ==(Iter<TAlign1, AlignColIterator<TSpec> > & _left,
271             Iter<TAlign2, AlignColIterator<TSpec> > const & _right)
272 {
273 SEQAN_CHECKPOINT
274     return value(_left.data_iterators, 0) == value(_right.data_iterators, 0);
275 }
276 template <typename TAlign1, typename TAlign2, typename TSpec>
277 inline bool
278 operator ==(Iter<TAlign1, AlignColIterator<TSpec> > const & _left,
279             Iter<TAlign2, AlignColIterator<TSpec> > const & _right)
280 {
281 SEQAN_CHECKPOINT
282     return value(_left.data_iterators, 0) == value(_right.data_iterators, 0);
283 }
284 
285 //////////////////////////////////////////////////////////////////////////////
286 
287 template <typename TAlign1, typename TAlign2, typename TSpec>
288 inline bool
289 operator !=(Iter<TAlign1, AlignColIterator<TSpec> > & _left,
290             Iter<TAlign2, AlignColIterator<TSpec> > & _right)
291 {
292 SEQAN_CHECKPOINT
293     return value(_left.data_iterators, 0) != value(_right.data_iterators, 0);
294 }
295 template <typename TAlign1, typename TAlign2, typename TSpec>
296 inline bool
297 operator !=(Iter<TAlign1, AlignColIterator<TSpec> > const & _left,
298             Iter<TAlign2, AlignColIterator<TSpec> > & _right)
299 {
300 SEQAN_CHECKPOINT
301     return value(_left.data_iterators, 0) != value(_right.data_iterators, 0);
302 }
303 template <typename TAlign1, typename TAlign2, typename TSpec>
304 inline bool
305 operator !=(Iter<TAlign1, AlignColIterator<TSpec> > & _left,
306             Iter<TAlign2, AlignColIterator<TSpec> > const & _right)
307 {
308 SEQAN_CHECKPOINT
309     return value(_left.data_iterators, 0) != value(_right.data_iterators, 0);
310 }
311 template <typename TAlign1, typename TAlign2, typename TSpec>
312 inline bool
313 operator !=(Iter<TAlign1, AlignColIterator<TSpec> > const & _left,
314             Iter<TAlign2, AlignColIterator<TSpec> > const & _right)
315 {
316 SEQAN_CHECKPOINT
317     return value(_left.data_iterators, 0) != value(_right.data_iterators, 0);
318 }
319 
320 //////////////////////////////////////////////////////////////////////////////
321 
322 template <typename TAlign, typename TSpec, typename TPosition>
323 inline typename Reference<TAlign>::Type
value(Iter<TAlign,AlignColIterator<TSpec>> & me,TPosition pos_)324 value(Iter<TAlign, AlignColIterator<TSpec> > & me,
325       TPosition pos_)
326 {
327 SEQAN_CHECKPOINT
328     return value(me.data_iterators[pos_]);
329 }
330 template <typename TAlign, typename TSpec, typename TPosition>
331 inline typename Reference<TAlign>::Type
value(Iter<TAlign,AlignColIterator<TSpec>> const & me,TPosition pos_)332 value(Iter<TAlign, AlignColIterator<TSpec> > const & me,
333       TPosition pos_)
334 {
335 SEQAN_CHECKPOINT
336     return value(me.data_iterators[pos_]);
337 }
338 //////////////////////////////////////////////////////////////////////////////
339 
340 template <typename TAlign, typename TSpec, typename TPosition>
341 inline typename GetValue<TAlign>::Type
getValue(Iter<TAlign,AlignColIterator<TSpec>> & me,TPosition pos_)342 getValue(Iter<TAlign, AlignColIterator<TSpec> > & me,
343          TPosition pos_)
344 {
345 SEQAN_CHECKPOINT
346     return getValue(me.data_iterators[pos_]);
347 }
348 template <typename TAlign, typename TSpec, typename TPosition>
349 inline typename GetValue<TAlign>::Type
getValue(Iter<TAlign,AlignColIterator<TSpec>> const & me,TPosition pos_)350 getValue(Iter<TAlign, AlignColIterator<TSpec> > const & me,
351          TPosition pos_)
352 {
353 SEQAN_CHECKPOINT
354     return getValue(me.data_iterators[pos_]);
355 }
356 
357 //////////////////////////////////////////////////////////////////////////////
358 
359 template <typename TAlign, typename TSpec, typename TPosition, typename TValue>
360 inline void
assignValue(Iter<TAlign,AlignColIterator<TSpec>> & me,TPosition pos_,TValue & val)361 assignValue(Iter<TAlign, AlignColIterator<TSpec> > & me,
362             TPosition pos_,
363             TValue & val)
364 {
365 SEQAN_CHECKPOINT
366     return assignValue(me.data_iterators[pos_], val);
367 }
368 template <typename TAlign, typename TSpec, typename TPosition, typename TValue>
369 inline void
assignValue(Iter<TAlign,AlignColIterator<TSpec>> & me,TPosition pos_,TValue const & val)370 assignValue(Iter<TAlign, AlignColIterator<TSpec> > & me,
371             TPosition pos_,
372             TValue const & val)
373 {
374 SEQAN_CHECKPOINT
375     return assignValue(me.data_iterators[pos_], val);
376 }
377 template <typename TAlign, typename TSpec, typename TPosition, typename TValue>
378 inline void
assignValue(Iter<TAlign,AlignColIterator<TSpec>> const & me,TPosition pos_,TValue & val)379 assignValue(Iter<TAlign, AlignColIterator<TSpec> > const & me,
380             TPosition pos_,
381             TValue & val)
382 {
383 SEQAN_CHECKPOINT
384     return assignValue(me.data_iterators[pos_], val);
385 }
386 template <typename TAlign, typename TSpec, typename TPosition, typename TValue>
387 inline void
assignValue(Iter<TAlign,AlignColIterator<TSpec>> const & me,TPosition pos_,TValue const & val)388 assignValue(Iter<TAlign, AlignColIterator<TSpec> > const & me,
389             TPosition pos_,
390             TValue const & val)
391 {
392 SEQAN_CHECKPOINT
393     return assignValue(me.data_iterators[pos_], val);
394 }
395 
396 //////////////////////////////////////////////////////////////////////////////
397 
398 template <typename TAlign, typename TSpec, typename TPosition, typename TValue>
399 inline void
moveValue(Iter<TAlign,AlignColIterator<TSpec>> & me,TPosition pos_,TValue & val)400 moveValue(Iter<TAlign, AlignColIterator<TSpec> > & me,
401           TPosition pos_,
402           TValue & val)
403 {
404 SEQAN_CHECKPOINT
405     return moveValue(me.data_iterators[pos_], val);
406 }
407 template <typename TAlign, typename TSpec, typename TPosition, typename TValue>
408 inline void
moveValue(Iter<TAlign,AlignColIterator<TSpec>> & me,TPosition pos_,TValue const & val)409 moveValue(Iter<TAlign, AlignColIterator<TSpec> > & me,
410           TPosition pos_,
411           TValue const & val)
412 {
413 SEQAN_CHECKPOINT
414     return moveValue(me.data_iterators[pos_], val);
415 }
416 template <typename TAlign, typename TSpec, typename TPosition, typename TValue>
417 inline void
moveValue(Iter<TAlign,AlignColIterator<TSpec>> const & me,TPosition pos_,TValue & val)418 moveValue(Iter<TAlign, AlignColIterator<TSpec> > const & me,
419           TPosition pos_,
420           TValue & val)
421 {
422 SEQAN_CHECKPOINT
423     return moveValue(me.data_iterators[pos_], val);
424 }
425 template <typename TAlign, typename TSpec, typename TPosition, typename TValue>
426 inline void
moveValue(Iter<TAlign,AlignColIterator<TSpec>> const & me,TPosition pos_,TValue const & val)427 moveValue(Iter<TAlign, AlignColIterator<TSpec> > const & me,
428           TPosition pos_,
429           TValue const & val)
430 {
431 SEQAN_CHECKPOINT
432     return moveValue(me.data_iterators[pos_], val);
433 }
434 
435 //////////////////////////////////////////////////////////////////////////////
436 
437 //??? TODO
438 //disabled since GapsIterator has no operator - and +
439 /*
440 template <typename TAlign, typename TSpec, typename TSize>
441 inline Iter<TAlign, AlignColIterator<TSpec> > &
442 operator +=(Iter<TAlign, AlignColIterator<TSpec> > & me,
443             TSize size)
444 {
445 SEQAN_CHECKPOINT
446     typedef typename Row<TAlign>::Type TRow;
447     typedef typename Iterator<TRow>::Type TRowIterator;
448     typedef String<TRowIterator> TIterators;
449     typedef typename Iterator<TIterators>::Type TIteratorsIterator;
450 
451     TIteratorsIterator _it = begin(me.data_iterators);
452     TIteratorsIterator _it_end = end(me.data_iterators);
453 
454     while (_it != _it_end)
455     {
456         *_it += size;
457         ++_it;
458     }
459     return me;
460 }
461 
462 
463 //////////////////////////////////////////////////////////////////////////////
464 
465 template <typename TAlign, typename TSpec, typename TSize>
466 inline Iter<TAlign, AlignColIterator<TSpec> >
467 operator +(Iter<TAlign, AlignColIterator<TSpec> > & me,
468            TSize size)
469 {
470 SEQAN_CHECKPOINT
471     Iter<TAlign, AlignColIterator<TSpec> > ret = me;
472     me += size;
473     return me;
474 }
475 template <typename TAlign, typename TSpec, typename TSize>
476 inline Iter<TAlign, AlignColIterator<TSpec> >
477 operator +(Iter<TAlign, AlignColIterator<TSpec> > const & me,
478            TSize size)
479 {
480 SEQAN_CHECKPOINT
481     Iter<TAlign, AlignColIterator<TSpec> > ret = me;
482     me += size;
483     return me;
484 }
485 
486 //////////////////////////////////////////////////////////////////////////////
487 
488 template <typename TAlign, typename TSpec, typename TSize>
489 inline Iter<TAlign, AlignColIterator<TSpec> > &
490 operator -=(Iter<TAlign, AlignColIterator<TSpec> > & me,
491             TSize size)
492 {
493 SEQAN_CHECKPOINT
494     typedef typename Row<TAlign>::Type TRow;
495     typedef typename Iterator<TRow>::Type TRowIterator;
496     typedef String<TRowIterator> TIterators;
497     typedef typename Iterator<TIterators>::Type TIteratorsIterator;
498 
499     TIteratorsIterator _it = begin(me.data_iterators);
500     TIteratorsIterator _it_end = end(me.data_iterators);
501 
502     while (_it != _it_end)
503     {
504         *_it -= size;
505         ++_it;
506     }
507     return me;
508 }
509 
510 //////////////////////////////////////////////////////////////////////////////
511 
512 template <typename TAlign, typename TSpec, typename TSize>
513 inline Iter<TAlign, AlignColIterator<TSpec> >
514 operator -(Iter<TAlign, AlignColIterator<TSpec> > & me,
515            TSize size)
516 {
517 SEQAN_CHECKPOINT
518     Iter<TAlign, AlignColIterator<TSpec> > ret = me;
519     me -= size;
520     return me;
521 }
522 template <typename TAlign, typename TSpec, typename TSize>
523 inline Iter<TAlign, AlignColIterator<TSpec> >
524 operator -(Iter<TAlign, AlignColIterator<TSpec> > const & me,
525            TSize size)
526 {
527 SEQAN_CHECKPOINT
528     Iter<TAlign, AlignColIterator<TSpec> > ret = me;
529     me -= size;
530     return me;
531 }
532 
533 //____________________________________________________________________________
534 
535 template <typename TAlign, typename TSpec>
536 inline typename Difference<TAlign>::Type
537 operator -(Iter<TAlign, AlignColIterator<TSpec> > const & left,
538            Iter<TAlign, AlignColIterator<TSpec> > const & right)
539 {
540 SEQAN_CHECKPOINT
541     SEQAN_ASSERT_GT(length(left.data_iterators), 0u);
542     SEQAN_ASSERT_GT(length(right.data_iterators), 0u);
543 
544     return (left.data_iterators[0] - right.data_iterators[0]);
545 }
546 
547 //////////////////////////////////////////////////////////////////////////////
548 
549 template <typename TAlign, typename TSpec>
550 inline typename Position<TAlign>::Type
551 position(Iter<TAlign, AlignColIterator<TSpec> > & me)
552 {
553 SEQAN_CHECKPOINT
554     return position(me.data_iterators[0], row(host(me), 0));
555 }
556 template <typename TAlign, typename TSpec>
557 inline typename Position<TAlign>::Type
558 position(Iter<TAlign, AlignColIterator<TSpec> > const & me)
559 {
560 SEQAN_CHECKPOINT
561     return position(me.data_iterators[0], row(host(me), 0));
562 }
563 */
564 //////////////////////////////////////////////////////////////////////////////
565 
566 
567 
568 //////////////////////////////////////////////////////////////////////////////
569 
570 }// namespace SEQAN_NAMESPACE_MAIN
571 
572 #endif //#ifndef SEQAN_HEADER_...
573