1 // ==========================================================================
2 //                 SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2018, 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: Rene Rahn <rene.rahn@fu-berlin.de>
33 // ==========================================================================
34 // Implementation of a global alignment between two sequences given a
35 // set of seeds in monotonic non-decreasing order. This implementation is
36 // based on the algorithm described in the "LAGAN and Multi-LAGAN: Efficient
37 // Tools for Large-Scale Multiple Alignment of Genomic Data", Brudno et al.,
38 // Genome Res. 2003, 13: 721-731, doi:10.1101/gr.926603
39 // ==========================================================================
40 
41 #ifndef INCLUDE_SEQAN_SEEDS_BANDED_CHAIN_ALIGNMENT_IMPL_H_
42 #define INCLUDE_SEQAN_SEEDS_BANDED_CHAIN_ALIGNMENT_IMPL_H_
43 
44 namespace seqan
45 {
46 
47 // ============================================================================
48 // Forwards
49 // ============================================================================
50 
51 // ============================================================================
52 // Tags, Classes, Enums
53 // ============================================================================
54 
55 // ----------------------------------------------------------------------------
56 // Struct BandedChainTracking()
57 // ----------------------------------------------------------------------------
58 
59 // Used to distinguish between a cell that has to be tracked for the
60 // initialization of the next grid and the cell that has to be tracked to find
61 // the optimal score for the current traceback.
62 
63 struct BandedChainTracking
64 {
65     enum Options
66     {
67         OPTION_INIT = 0,
68         OPTION_IS_LAST_COLUMN = 1,
69         OPTION_IS_LAST_ROW = 2,
70         OPTION_STORE_INIT_COLUMN = 4,
71         OPTION_STORE_INIT_ROW = 8
72     };
73 };
74 
75 // ============================================================================
76 // Metafunctions
77 // ============================================================================
78 
79 // ============================================================================
80 // Functions
81 // ============================================================================
82 
83 // ----------------------------------------------------------------------------
84 // Function _checkScoreOverflow()
85 // ----------------------------------------------------------------------------
86 
87 template <typename TDist,
88           typename TScoreValue, typename TScoreSpec>
_checkScoreOverflow(TDist const distance,Score<TScoreValue,TScoreSpec> const & score)89 inline bool _checkScoreOverflow(TDist const distance,
90                                 Score<TScoreValue, TScoreSpec> const & score)
91 {
92     using TBits = decltype(BitsPerValue<TScoreValue>::VALUE);
93     TScoreValue mxScore = _max(scoreMatch(score), std::abs(scoreMismatch(score)));
94 
95     TBits bits = bitScanReverse(mxScore) + bitScanReverse(distance);
96     return bits <= BitsPerValue<TScoreValue>::VALUE - 1;
97 }
98 
99 // ----------------------------------------------------------------------------
100 // Function _isLastSeed()
101 // ----------------------------------------------------------------------------
102 
103 template <typename TSeedSet>
104 inline bool
_isLastSeed(typename Iterator<TSeedSet const,Standard>::Type iter,TSeedSet const & seedSet)105 _isLastSeed(typename Iterator<TSeedSet const, Standard>::Type iter,
106             TSeedSet const & seedSet)
107 {
108     typedef typename Iterator<TSeedSet const>::Type TSeedIterator;
109     TSeedIterator itEnd = end(seedSet, Standard());
110     return iter == --itEnd;
111 }
112 
113 // ----------------------------------------------------------------------------
114 // Function _checkColinearity()
115 // ----------------------------------------------------------------------------
116 
117 template <typename TIter>
118 inline bool
_checkColinearity(TIter currIter)119 _checkColinearity(TIter currIter)
120 {
121     TIter nextIter = currIter;
122     ++nextIter;
123 
124     return endPositionH(value(currIter)) <= beginPositionH(value(nextIter)) &&
125            endPositionV(value(currIter)) <= beginPositionV(value(nextIter));
126 }
127 
128 // ----------------------------------------------------------------------------
129 // Function _isCrossingBeginHorizontal()
130 // ----------------------------------------------------------------------------
131 
132 // Checks whether a given seed crosses the first row of the global grid.
133 template <typename TPosition, typename TSize>
_isCrossingBeginHorizontal(TPosition const & horizontalSeedBeginPos,TSize const & bandExtension)134 inline bool _isCrossingBeginHorizontal(TPosition const & horizontalSeedBeginPos, TSize const & bandExtension)
135 {
136     typedef typename MakeSigned<TSize>::Type TSignedSize;
137     if ((TSignedSize) horizontalSeedBeginPos - (TSignedSize) bandExtension <= 0)
138         return true;
139     return false;
140 }
141 
142 // ----------------------------------------------------------------------------
143 // Function _isCrossingBeginVertical()
144 // ----------------------------------------------------------------------------
145 
146 // Checks whether a given seed crosses the first column of the global grid.
147 template <typename TPosition, typename TSize>
_isCrossingBeginVertical(TPosition const & verticalSeedBeginPos,TSize const & bandExtension)148 inline bool _isCrossingBeginVertical(TPosition const & verticalSeedBeginPos, TSize const & bandExtension)
149 {
150     typedef typename MakeSigned<TSize>::Type TSignedSize;
151 
152     if ((TSignedSize) verticalSeedBeginPos - (TSignedSize) bandExtension <= 0)
153         return true;
154     return false;
155 }
156 
157 // ----------------------------------------------------------------------------
158 // Function _isCrossingEndHorizontal()
159 // ----------------------------------------------------------------------------
160 
161 // Checks whether a given seed crosses the last row of the global grid.
162 template <typename TPosition, typename TSize, typename TSequenceH>
163 inline bool
_isCrossingEndHorizontal(TPosition const & horizontalSeedEndPos,TSize const & bandSize,TSequenceH const & seqH)164 _isCrossingEndHorizontal(TPosition const & horizontalSeedEndPos,
165                          TSize const & bandSize,
166                          TSequenceH const & seqH)
167 {
168     typedef typename MakeSigned<TSize>::Type TSignedSize;
169 
170     if ((TSignedSize) horizontalSeedEndPos + (TSignedSize) bandSize >= (TSignedSize) length(seqH))
171         return true;
172     return false;
173 }
174 
175 // ----------------------------------------------------------------------------
176 // Function _isCrossingEndVertical()
177 // ----------------------------------------------------------------------------
178 
179 // Checks whether a given seed crosses the last column of the global grid.
180 template <typename TPosition, typename TSize, typename TSequenceV>
181 inline bool
_isCrossingEndVertical(TPosition const & verticalSeedEndPos,TSize const & bandSize,TSequenceV const & seqV)182 _isCrossingEndVertical(TPosition const & verticalSeedEndPos,
183                        TSize const & bandSize,
184                        TSequenceV const & seqV)
185 {
186     typedef typename MakeSigned<TSize>::Type TSignedSize;
187 
188     if ((TSignedSize) verticalSeedEndPos + (TSignedSize) bandSize >= (TSignedSize) length(seqV))
189         return true;
190     return false;
191 }
192 
193 // ----------------------------------------------------------------------------
194 // Function _horizontalBandShiftBeginPoint()
195 // ----------------------------------------------------------------------------
196 
197 // Returns the shift in horizontal direction for skew bands.
198 template <typename TSeed>
_horizontalBandShiftBeginPoint(TSeed const & seed)199 inline typename Size<TSeed>::Type _horizontalBandShiftBeginPoint(TSeed const & seed)
200 {
201     typedef typename Size<TSeed>::Type TSize;
202     TSize bandSize = (upperDiagonal(seed) - (beginPositionH(seed) - beginPositionV(seed)));
203     SEQAN_ASSERT_GEQ(bandSize, TSize(0));  // must be greater or equal than zero
204     return bandSize;
205 }
206 
207 // ----------------------------------------------------------------------------
208 // Function _verticalBandShiftBeginPoint()
209 // ----------------------------------------------------------------------------
210 
211 // Returns the shift in vertical direction for skew bands.
212 template <typename TSeed>
_verticalBandShiftBeginPoint(TSeed const & seed)213 inline typename Size<TSeed>::Type _verticalBandShiftBeginPoint(TSeed const & seed)
214 {
215     typedef typename Size<TSeed>::Type TSize;
216     TSize bandSize((beginPositionH(seed) - beginPositionV(seed)) - lowerDiagonal(seed));
217     SEQAN_ASSERT_GEQ(bandSize, TSize(0));  // must be greater or equal than zero
218     return bandSize;
219 }
220 
221 // ----------------------------------------------------------------------------
222 // Function _horizontalBandShiftEndPoint()
223 // ----------------------------------------------------------------------------
224 
225     // Returns the shift in horizontal direction for skew bands.
226 template <typename TSeed>
_horizontalBandShiftEndPoint(TSeed const & seed)227 inline typename Size<TSeed>::Type _horizontalBandShiftEndPoint(TSeed const & seed)
228 {
229     typedef typename Size<TSeed>::Type TSize;
230     TSize bandSize = endPositionH(seed) - endPositionV(seed) - lowerDiagonal(seed);
231     SEQAN_ASSERT_GEQ(bandSize, TSize(0));  // must be greater or equal than zero
232     return bandSize;
233 }
234 
235 // ----------------------------------------------------------------------------
236 // Function _verticalBandShiftEndPoint()
237 // ----------------------------------------------------------------------------
238 
239     // Returns the shift in vertical direction for skew bands.
240 template <typename TSeed>
_verticalBandShiftEndPoint(TSeed const & seed)241 inline typename Size<TSeed>::Type _verticalBandShiftEndPoint(TSeed const & seed)
242 {
243     typedef typename Size<TSeed>::Type TSize;
244     TSize bandSize = upperDiagonal(seed) - endPositionH(seed) + endPositionV(seed);
245     SEQAN_ASSERT_GEQ(bandSize, TSize(0));  // must be greater or equal than zero
246     return bandSize;
247 }
248 
249 
250 // ----------------------------------------------------------------------------
251 // Function _verticalCellInitialization()
252 // ----------------------------------------------------------------------------
253 
254 // Initiliazes vertical cell with corresponding value from dp scout state.
255 template <typename TDPCell, typename TTraceNavigator>
256 inline TDPCell &
_verticalCellInitialization(DPScout_<TDPCell,BandedChainAlignmentScout> const & dpScout,TTraceNavigator const & navigator)257 _verticalCellInitialization(DPScout_<TDPCell, BandedChainAlignmentScout> const & dpScout,
258                             TTraceNavigator const & navigator)
259 {
260     return dpScout._dpScoutStatePtr->_verticalInitCurrentMatrix[coordinate(navigator, +DPMatrixDimension_::VERTICAL) -
261                                                                  (navigator._laneLeap - 1)];
262 }
263 
264 // ----------------------------------------------------------------------------
265 // Function _horizontalCellInitialization()
266 // ----------------------------------------------------------------------------
267 
268 // Initializes horizontal cell with corresponding value from dp scout state.
269 template <typename TDPCell, typename TTraceNavigator>
270 inline TDPCell const &
_horizontalCellInitialization(DPScout_<TDPCell,BandedChainAlignmentScout> const & dpScout,TTraceNavigator const & navigator)271 _horizontalCellInitialization(DPScout_<TDPCell, BandedChainAlignmentScout> const & dpScout,
272                               TTraceNavigator const & navigator)
273 {
274     return dpScout._dpScoutStatePtr->_horizontalInitCurrentMatrix[coordinate(navigator, +DPMatrixDimension_::HORIZONTAL)];
275 }
276 
277 // ----------------------------------------------------------------------------
278 // Function _determineTrackingOptions()
279 // ----------------------------------------------------------------------------
280 
281 // Determines the various tracking options for the current dp matrix.
282 template <typename TScout, typename TNavigator, typename TColumnDescriptor, typename TCellDescriptor, typename TFreeEndGaps,
283           typename TMatrixSpec, typename TGapCosts, typename TTracebackSpec, typename TExecPolicy>
_determineTrackingOptions(unsigned & res,TScout const & scout,TNavigator const & traceMatrixNavigator,TColumnDescriptor const &,TCellDescriptor const &,DPProfile_<BandedChainAlignment_<TFreeEndGaps,TMatrixSpec>,TGapCosts,TTracebackSpec,TExecPolicy> const &)284 inline void _determineTrackingOptions(unsigned & res,
285                                       TScout const & scout,
286                                       TNavigator const & traceMatrixNavigator,
287                                       TColumnDescriptor const & /*columnDescriptor*/,
288                                       TCellDescriptor const & /*cellDescriptor*/,
289                                       DPProfile_<BandedChainAlignment_<TFreeEndGaps, TMatrixSpec>, TGapCosts, TTracebackSpec, TExecPolicy> const &)
290 {
291     if (coordinate(traceMatrixNavigator, +DPMatrixDimension_::HORIZONTAL) >=
292         scout._dpScoutStatePtr->_horizontalNextGridOrigin)
293     {
294         // Matches an initialization row.
295         SEQAN_IF_CONSTEXPR (IsSameType<typename TColumnDescriptor::TLocation, PartialColumnBottom>::VALUE)
296         {
297             // Matches the cells that have to be stored for initializing the next matrix horizontally.
298             if (coordinate(traceMatrixNavigator, +DPMatrixDimension_::VERTICAL) + traceMatrixNavigator._laneLeap ==
299                 scout._dpScoutStatePtr->_verticalNextGridOrigin)
300                 res |= BandedChainTracking::OPTION_STORE_INIT_ROW; //BANDED_CHAIN_TRACKING_OPTION_STORE_INIT_ROW;
301         }  // Matches an initialization row.
302         else
303         {
304             // Matches the cells that have to be stored for initializing the next matrix horizontally.
305             if (coordinate(traceMatrixNavigator, +DPMatrixDimension_::VERTICAL) ==
306                 scout._dpScoutStatePtr->_verticalNextGridOrigin)
307                 res |= BandedChainTracking::OPTION_STORE_INIT_ROW;
308         }
309 
310         // Matches an initialization column.
311         if (coordinate(traceMatrixNavigator, +DPMatrixDimension_::HORIZONTAL) ==
312                     scout._dpScoutStatePtr->_horizontalNextGridOrigin)
313             // Matches the cells that have to be stored for initializing the next matrix vertically.
314             if (coordinate(traceMatrixNavigator, +DPMatrixDimension_::VERTICAL) >=
315                 scout._dpScoutStatePtr->_verticalNextGridOrigin)
316                 res |= BandedChainTracking::OPTION_STORE_INIT_COLUMN;
317 
318         // We are in the column that has to be tracked, if we are in the last cell.
319         SEQAN_IF_CONSTEXPR (IsSameType<TCellDescriptor, LastCell>::VALUE)
320         {
321             SEQAN_IF_CONSTEXPR (IsSameType<TMatrixSpec, BandedChainFinalDPMatrix>::VALUE)
322             {
323                 if (IsFreeEndGap_<TFreeEndGaps, DPLastRow>::VALUE)
324                     res |= BandedChainTracking::OPTION_IS_LAST_ROW;
325             }
326             else
327             {
328                 res |= BandedChainTracking::OPTION_IS_LAST_ROW;
329             }
330         }
331 
332         // We track the maximal score if we are in the final column
333         // For the full column we need to check if we are beyond the initialization row.
334         SEQAN_IF_CONSTEXPR (IsSameType<typename TColumnDescriptor::TLocation, FullColumn>::VALUE)
335         {
336             SEQAN_IF_CONSTEXPR (IsSameType<typename TColumnDescriptor::TColumnProperty, DPFinalColumn>::VALUE)
337             {
338                 SEQAN_IF_CONSTEXPR (IsSameType<TCellDescriptor, LastCell>::VALUE)
339                 {
340                     res |= BandedChainTracking::OPTION_IS_LAST_COLUMN | BandedChainTracking::OPTION_IS_LAST_ROW;
341                 }
342                 else if (coordinate(traceMatrixNavigator, +DPMatrixDimension_::VERTICAL) >=
343                          scout._dpScoutStatePtr->_verticalNextGridOrigin)
344                 {
345                     SEQAN_IF_CONSTEXPR (IsSameType<TMatrixSpec, BandedChainFinalDPMatrix>::VALUE)
346                     {
347                         if (IsFreeEndGap_<TFreeEndGaps, DPLastColumn>::VALUE)
348                             res |= BandedChainTracking::OPTION_IS_LAST_COLUMN;
349                     }
350                     else
351                     {
352                         res |= BandedChainTracking::OPTION_IS_LAST_COLUMN;
353                     }
354                 }
355             }
356         }
357         else  // Banded version we track all scores - initialization row ends in first cell of final column
358         {
359             SEQAN_IF_CONSTEXPR (IsSameType<typename TColumnDescriptor::TColumnProperty, DPFinalColumn>::VALUE)
360             {
361                 SEQAN_IF_CONSTEXPR (IsSameType<TCellDescriptor, LastCell>::VALUE)
362                 {
363                     res |= BandedChainTracking::OPTION_IS_LAST_COLUMN | BandedChainTracking::OPTION_IS_LAST_ROW;
364                 }
365                 else
366                 {
367                     SEQAN_IF_CONSTEXPR (IsSameType<TMatrixSpec, BandedChainFinalDPMatrix>::VALUE)
368                     {
369                         if (IsFreeEndGap_<TFreeEndGaps, DPLastColumn>::VALUE)
370                             res |= BandedChainTracking::OPTION_IS_LAST_COLUMN;
371                     }
372                     else
373                     {
374                         res |= BandedChainTracking::OPTION_IS_LAST_COLUMN;
375                     }
376                 }
377             }
378         }
379     }
380 }
381 
382 // ----------------------------------------------------------------------------
383 // Function _applyBandedChainTracking()
384 // ----------------------------------------------------------------------------
385 
386 template <typename TDPScout, typename TTraceMatrixNavigator, typename TDPCell, typename TColumnDescriptor,
387           typename TCellDescriptor, typename TDPProfile>
388 inline void
_applyBandedChainTracking(TDPScout & scout,TTraceMatrixNavigator & traceMatrixNavigator,TDPCell const & activeCell,TColumnDescriptor const &,TCellDescriptor const &,TDPProfile const &)389 _applyBandedChainTracking(TDPScout & scout,
390                           TTraceMatrixNavigator & traceMatrixNavigator,
391                           TDPCell const & activeCell,
392                           TColumnDescriptor const &,
393                           TCellDescriptor const &,
394                           TDPProfile const &)
395 {
396     unsigned result = BandedChainTracking::OPTION_INIT;
397     _determineTrackingOptions(result, scout, traceMatrixNavigator, TColumnDescriptor(), TCellDescriptor(), TDPProfile());
398     _scoutBestScore(scout, activeCell, traceMatrixNavigator,
399                     (result & BandedChainTracking::OPTION_IS_LAST_COLUMN),
400                     (result & BandedChainTracking::OPTION_IS_LAST_ROW),
401                     (result & BandedChainTracking::OPTION_STORE_INIT_COLUMN),
402                     (result & BandedChainTracking::OPTION_STORE_INIT_ROW));
403 }
404 
405 // ----------------------------------------------------------------------------
406 // Function _computeCell()                               [BandedChainAlignment]
407 // ----------------------------------------------------------------------------
408 
409 // Overload of _computeCell function to add functionality specific to the bande chain alignment.
410 template <typename TDPScout,
411           typename TTraceMatrixNavigator,
412           typename TDPCell,
413           typename TSeqHValue,
414           typename TSeqVValue,
415           typename TScoringScheme,
416           typename TColumnDescriptor,
417           typename TCellDescriptor,
418           typename TFreeEndGaps, typename TDPMatrixLocation, typename TGapCosts, typename TTracebackConfig, typename TExecPolicy>
419 inline void
_computeCell(TDPScout & scout,TTraceMatrixNavigator & traceMatrixNavigator,TDPCell & current,TDPCell & diagonal,TDPCell const & horizontal,TDPCell & vertical,TSeqHValue const & seqHVal,TSeqVValue const & seqVVal,TScoringScheme const & scoringScheme,TColumnDescriptor const &,TCellDescriptor const &,DPProfile_<BandedChainAlignment_<TFreeEndGaps,TDPMatrixLocation>,TGapCosts,TracebackOn<TTracebackConfig>,TExecPolicy> const &)420 _computeCell(TDPScout & scout,
421              TTraceMatrixNavigator & traceMatrixNavigator,
422              TDPCell & current,
423              TDPCell & diagonal,
424              TDPCell const & horizontal,
425              TDPCell & vertical,
426              TSeqHValue const & seqHVal,
427              TSeqVValue const & seqVVal,
428              TScoringScheme const & scoringScheme,
429              TColumnDescriptor const &,
430              TCellDescriptor const &,
431              DPProfile_<BandedChainAlignment_<TFreeEndGaps, TDPMatrixLocation>, TGapCosts, TracebackOn<TTracebackConfig>, TExecPolicy> const &)
432 {
433     typedef DPProfile_<BandedChainAlignment_<TFreeEndGaps, TDPMatrixLocation>,
434                        TGapCosts,
435                        TracebackOn<TTracebackConfig>,
436                        TExecPolicy> TDPProfile;
437     typedef DPMetaColumn_<TDPProfile, TColumnDescriptor> TMetaColumnProfile;
438 
439     assignValue(
440         traceMatrixNavigator,
441         _computeScore(current, diagonal, horizontal, vertical, seqHVal, seqVVal,
442                       scoringScheme, typename RecursionDirection_<TMetaColumnProfile, TCellDescriptor>::Type(),
443                       TDPProfile()));
444 
445     if (TrackingEnabled_<TMetaColumnProfile, TCellDescriptor>::VALUE)
446     {
447         _setVerticalScoreOfCell(current, _verticalScoreOfCell(vertical));
448         _applyBandedChainTracking(scout, traceMatrixNavigator, current, TColumnDescriptor(), TCellDescriptor(),
449                                   TDPProfile());
450     }
451 }
452 
453 // ----------------------------------------------------------------------------
454 // Function _computeCell()              [BandedChainAlignment, DPInitialColumn]
455 // ----------------------------------------------------------------------------
456 
457 template <typename TDPScout,
458           typename TDPTraceMatrixNavigator,
459           typename TDPCell,
460           typename TSeqHValue,
461           typename TSeqVValue,
462           typename TScoringScheme,
463           typename TColumnType,
464           typename TCellDescriptor,
465           typename TSpec, typename TDPMatrixLocation, typename TGapCosts, typename TTracebackConfig, typename TExecPolicy>
466 inline void
_computeCell(TDPScout & scout,TDPTraceMatrixNavigator & traceMatrixNavigator,TDPCell & current,TDPCell & diagonal,TDPCell const & horizontal,TDPCell & vertical,TSeqHValue const &,TSeqVValue const &,TScoringScheme const &,MetaColumnDescriptor<DPInitialColumn,TColumnType> const &,TCellDescriptor const &,DPProfile_<BandedChainAlignment_<TSpec,TDPMatrixLocation>,TGapCosts,TracebackOn<TTracebackConfig>,TExecPolicy> const &)467 _computeCell(TDPScout & scout,
468              TDPTraceMatrixNavigator & traceMatrixNavigator,
469              TDPCell & current,
470              TDPCell & diagonal,
471              TDPCell const & horizontal,
472              TDPCell & vertical,
473              TSeqHValue const &,
474              TSeqVValue const &,
475              TScoringScheme const & /*scoringScheme*/,
476              MetaColumnDescriptor<DPInitialColumn, TColumnType> const & /*metaColumnDescriptor*/,
477              TCellDescriptor const & /*cellDescriptor*/,
478              DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>, TGapCosts, TracebackOn<TTracebackConfig>, TExecPolicy> const & /*dpProfile*/)
479 {
480     typedef DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>,
481                        TGapCosts,
482                        TracebackOn<TTracebackConfig>,
483                        TExecPolicy> TDPProfile;
484     typedef MetaColumnDescriptor<DPInitialColumn, TColumnType> TColumnDescriptor;
485     typedef DPMetaColumn_<TDPProfile, TColumnDescriptor> TMetaColumnProfile;
486 
487     _scoreOfCell(diagonal) = _scoreOfCell(horizontal);
488     current = _verticalCellInitialization(scout, traceMatrixNavigator);
489     assignValue(traceMatrixNavigator, TraceBitMap_<>::NONE);
490     _scoreOfCell(vertical) = _scoreOfCell(current);
491     _setVerticalScoreOfCell(vertical, _verticalScoreOfCell(current));
492     if (TrackingEnabled_<TMetaColumnProfile, TCellDescriptor>::VALUE)
493         _applyBandedChainTracking(scout, traceMatrixNavigator, current, TColumnDescriptor(), TCellDescriptor(),
494                                   TDPProfile());
495 }
496 
497 // ----------------------------------------------------------------------------
498 // Function _computeHorizontalInitCell()
499 // ----------------------------------------------------------------------------
500 
501 template <typename TDPScout, typename TDPTraceMatrixNavigator, typename TDPCell,
502           typename TColumnDescriptor, typename TCellDescriptor, typename TDPProfile>
503 inline void
_computeHorizontalInitCell(TDPScout & scout,TDPTraceMatrixNavigator & traceMatrixNavigator,TDPCell & activeCell,TColumnDescriptor const &,TCellDescriptor const &,TDPProfile const &)504 _computeHorizontalInitCell(TDPScout & scout,
505                            TDPTraceMatrixNavigator & traceMatrixNavigator,
506                            TDPCell & activeCell,
507                            TColumnDescriptor const &,
508                            TCellDescriptor const &,
509                            TDPProfile const &)
510 {
511     typedef DPMetaColumn_<TDPProfile, TColumnDescriptor> TMetaColumnProfile;
512 
513     activeCell = _horizontalCellInitialization(scout, traceMatrixNavigator);
514     assignValue(traceMatrixNavigator, TraceBitMap_<>::NONE);
515     if (TrackingEnabled_<TMetaColumnProfile, TCellDescriptor>::VALUE)
516         _applyBandedChainTracking(scout, traceMatrixNavigator, activeCell, TColumnDescriptor(), TCellDescriptor(), TDPProfile());
517 }
518 
519 // ----------------------------------------------------------------------------
520 // Function _computeCell()     [BandedChainAlignment, PartialColumn, FirstCell]
521 // ----------------------------------------------------------------------------
522 
523 // For DPInnerColumn.
524 template <typename TDPScout, typename TDPTraceMatrixNavigator,
525           typename TDPCell,
526           typename TSeqHValue,
527           typename TSeqVValue,
528           typename TScoringScheme,
529           typename TSpec, typename TDPMatrixLocation, typename TGapCosts, typename TTracebackConfig, typename TExecPolicy>
530 inline void
_computeCell(TDPScout & scout,TDPTraceMatrixNavigator & traceMatrixNavigator,TDPCell & current,TDPCell & diagonal,TDPCell const & horizontal,TDPCell & vertical,TSeqHValue const &,TSeqVValue const &,TScoringScheme const &,MetaColumnDescriptor<DPInnerColumn,PartialColumnTop> const &,FirstCell const &,DPProfile_<BandedChainAlignment_<TSpec,TDPMatrixLocation>,TGapCosts,TracebackOn<TTracebackConfig>,TExecPolicy> const &)531 _computeCell(TDPScout & scout,
532              TDPTraceMatrixNavigator & traceMatrixNavigator,
533              TDPCell & current,
534              TDPCell & diagonal,
535              TDPCell const & horizontal,
536              TDPCell & vertical,
537              TSeqHValue const &,
538              TSeqVValue const &,
539              TScoringScheme const &,
540              MetaColumnDescriptor<DPInnerColumn, PartialColumnTop> const &,
541              FirstCell const &,
542              DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>, TGapCosts, TracebackOn<TTracebackConfig>, TExecPolicy> const &)
543 {
544     typedef DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>,
545                        TGapCosts,
546                        TracebackOn<TTracebackConfig>,
547                        TExecPolicy> TDPProfile;
548     _scoreOfCell(diagonal) = _scoreOfCell(horizontal);
549     _computeHorizontalInitCell(scout, traceMatrixNavigator, current,
550                                MetaColumnDescriptor<DPInnerColumn, PartialColumnTop>(), FirstCell(), TDPProfile());
551     _scoreOfCell(vertical) = _scoreOfCell(current);
552     _setVerticalScoreOfCell(vertical, _verticalScoreOfCell(current));
553 }
554 
555 // For DPFinalColumn.
556 template <typename TDPScout,
557           typename TDPTraceMatrixNavigator,
558           typename TDPCell,
559           typename TSeqHValue,
560           typename TSeqVValue,
561           typename TScoringScheme,
562           typename TSpec, typename TDPMatrixLocation, typename TGapCosts, typename TTracebackConfig, typename TExecPolicy>
563 inline void
_computeCell(TDPScout & scout,TDPTraceMatrixNavigator & traceMatrixNavigator,TDPCell & current,TDPCell & diagonal,TDPCell const & horizontal,TDPCell & vertical,TSeqHValue const &,TSeqVValue const &,TScoringScheme const &,MetaColumnDescriptor<DPFinalColumn,PartialColumnTop> const &,FirstCell const &,DPProfile_<BandedChainAlignment_<TSpec,TDPMatrixLocation>,TGapCosts,TracebackOn<TTracebackConfig>,TExecPolicy> const &)564 _computeCell(TDPScout & scout,
565              TDPTraceMatrixNavigator & traceMatrixNavigator,
566              TDPCell & current,
567              TDPCell & diagonal,
568              TDPCell const & horizontal,
569              TDPCell & vertical,
570              TSeqHValue const &,
571              TSeqVValue const &,
572              TScoringScheme const &,
573              MetaColumnDescriptor<DPFinalColumn, PartialColumnTop> const &,
574              FirstCell const &,
575              DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>, TGapCosts, TracebackOn<TTracebackConfig>, TExecPolicy> const &)
576 {
577     typedef DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>,
578                        TGapCosts,
579                        TracebackOn<TTracebackConfig>,
580                        TExecPolicy> TDPProfile;
581     _scoreOfCell(diagonal) = _scoreOfCell(horizontal);
582     _computeHorizontalInitCell(scout, traceMatrixNavigator, current,
583                                MetaColumnDescriptor<DPFinalColumn, PartialColumnTop>(), FirstCell(), TDPProfile());
584     _scoreOfCell(vertical) = _scoreOfCell(current);
585     _setVerticalScoreOfCell(vertical, _verticalScoreOfCell(current));
586 }
587 
588 // ----------------------------------------------------------------------------
589 // Function _computeCell()          [BandedChainAlignment, FullColumn, FirstCell]
590 // ----------------------------------------------------------------------------
591 
592 // For DPInnerColumn.
593 template <typename TDPScout,
594           typename TDPTraceMatrixNavigator,
595           typename TDPCell,
596           typename TSeqHValue,
597           typename TSeqVValue,
598           typename TScoringScheme,
599           typename TSpec, typename TDPMatrixLocation, typename TGapCosts, typename TTracebackConfig, typename TExecPolicy>
600 inline void
_computeCell(TDPScout & scout,TDPTraceMatrixNavigator & traceMatrixNavigator,TDPCell & current,TDPCell & diagonal,TDPCell const & horizontal,TDPCell & vertical,TSeqHValue const &,TSeqVValue const &,TScoringScheme const &,MetaColumnDescriptor<DPInnerColumn,FullColumn> const &,FirstCell const &,DPProfile_<BandedChainAlignment_<TSpec,TDPMatrixLocation>,TGapCosts,TracebackOn<TTracebackConfig>,TExecPolicy> const &)601 _computeCell(TDPScout & scout,
602              TDPTraceMatrixNavigator & traceMatrixNavigator,
603              TDPCell & current,
604              TDPCell & diagonal,
605              TDPCell const & horizontal,
606              TDPCell & vertical,
607              TSeqHValue const &,
608              TSeqVValue const &,
609              TScoringScheme const &,
610              MetaColumnDescriptor<DPInnerColumn, FullColumn> const &,
611              FirstCell const &,
612              DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>, TGapCosts, TracebackOn<TTracebackConfig>, TExecPolicy> const &)
613 {
614     typedef DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>,
615                        TGapCosts,
616                        TracebackOn<TTracebackConfig>,
617                        TExecPolicy> TDPProfile;
618     _scoreOfCell(diagonal) = _scoreOfCell(horizontal);
619     _computeHorizontalInitCell(scout, traceMatrixNavigator, current,
620                                MetaColumnDescriptor<DPInnerColumn, FullColumn>(), FirstCell(), TDPProfile());
621     _scoreOfCell(vertical) = _scoreOfCell(current);
622     _setVerticalScoreOfCell(vertical, _verticalScoreOfCell(current));
623 }
624 
625 // For DPFinalColumn.
626 template <typename TDPScout,
627           typename TDPTraceMatrixNavigator,
628           typename TDPCell,
629           typename TSeqHValue,
630           typename TSeqVValue,
631           typename TScoringScheme,
632           typename TSpec, typename TDPMatrixLocation, typename TGapCosts, typename TTracebackConfig, typename TExecPolicy>
633 inline void
_computeCell(TDPScout & scout,TDPTraceMatrixNavigator & traceMatrixNavigator,TDPCell & current,TDPCell & diagonal,TDPCell const & horizontal,TDPCell & vertical,TSeqHValue const &,TSeqVValue const &,TScoringScheme const &,MetaColumnDescriptor<DPFinalColumn,FullColumn> const &,FirstCell const &,DPProfile_<BandedChainAlignment_<TSpec,TDPMatrixLocation>,TGapCosts,TracebackOn<TTracebackConfig>,TExecPolicy> const &)634 _computeCell(TDPScout & scout,
635              TDPTraceMatrixNavigator & traceMatrixNavigator,
636              TDPCell & current,
637              TDPCell & diagonal,
638              TDPCell const & horizontal,
639              TDPCell & vertical,
640              TSeqHValue const &,
641              TSeqVValue const &,
642              TScoringScheme const &,
643              MetaColumnDescriptor<DPFinalColumn, FullColumn> const &,
644              FirstCell const &,
645              DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>, TGapCosts, TracebackOn<TTracebackConfig>, TExecPolicy> const &)
646 {
647     typedef DPProfile_<BandedChainAlignment_<TSpec, TDPMatrixLocation>,
648                       TGapCosts,
649                       TracebackOn<TTracebackConfig>,
650                       TExecPolicy> TDPProfile;
651     _scoreOfCell(diagonal) = _scoreOfCell(horizontal);
652     _computeHorizontalInitCell(scout, traceMatrixNavigator, current,
653                                MetaColumnDescriptor<DPFinalColumn, FullColumn>(), FirstCell(), TDPProfile());
654     vertical = current;
655 }
656 
657 // ----------------------------------------------------------------------------
658 // Function findFirstAnchor()
659 // ----------------------------------------------------------------------------
660 
661 // Finds the last seed that overlaps with the first row or column of the matrix.
662 // This way we guarantee that special conditions need to be considered only for one seed.
663 template <typename TSeedSet>
664 inline typename Iterator<TSeedSet const, Standard>::Type
_findFirstAnchor(TSeedSet const & seedSet,int bandExtension)665 _findFirstAnchor(TSeedSet const & seedSet, int bandExtension)
666 {
667     typedef typename Iterator<TSeedSet const, Standard>::Type TIterator;
668     typedef typename Value<TSeedSet const>::Type TSeed;
669 
670     SEQAN_ASSERT_GT_MSG(length(seedSet), 0u, "SeedSet is empty!");
671 
672     TIterator it = begin(seedSet, Standard());
673     TIterator itEnd = end(seedSet, Standard());
674     --itEnd;
675 
676     while (it != itEnd)
677     {
678         TSeed seed = value(++it);
679         if (_isCrossingBeginHorizontal(beginPositionH(seed), bandExtension))
680             continue;
681         else if (_isCrossingBeginVertical(beginPositionV(seed), bandExtension))
682             continue;
683         else
684         { // found seed which is not crossing the begin.
685             SEQAN_ASSERT(it != static_cast<TIterator>(begin(seedSet, Standard())));
686             return --it;
687         }
688     }
689     return it;
690 }
691 
692 // ----------------------------------------------------------------------------
693 // Function findLastAnchor()
694 // ----------------------------------------------------------------------------
695 
696 // Finds the last seed that is not overlapping with the last row and column.
697 // This way we guarantee that special conditions need to be considered only for one seed.
698 template <typename TSeedSet, typename TSequenceH, typename TSequenceV>
699 inline typename Iterator<TSeedSet const, Standard>::Type
_findLastAnchor(typename Iterator<TSeedSet const,Standard>::Type & iterBegin,TSeedSet const & seedSet,TSequenceH const & seqH,TSequenceV const & seqV,int bandExtension)700 _findLastAnchor(typename Iterator<TSeedSet const, Standard>::Type & iterBegin,
701                 TSeedSet const & seedSet, TSequenceH const & seqH, TSequenceV const & seqV, int bandExtension)
702 {
703     typedef typename Iterator<TSeedSet const, Standard>::Type TIterator;
704     typedef typename Value<TSeedSet>::Type TSeed;
705 
706     SEQAN_ASSERT_GT_MSG(length(seedSet), 0u, "SeedSet is empty!");
707 
708     TIterator it = end(seedSet, Standard());
709     --it;
710 
711     while (it != iterBegin)
712     {
713         TSeed seed = value(--it);
714         if (_isCrossingEndHorizontal(endPositionH(seed), bandExtension, seqH))
715             continue;
716         else if (_isCrossingEndVertical(endPositionV(seed), bandExtension, seqV))
717             continue;
718         else  // Found seed which is not crossing the end.
719             return it;
720     }
721     return it;
722 }
723 
724 // ----------------------------------------------------------------------------
725 // Function _printTraceSegments()
726 // ----------------------------------------------------------------------------
727 
728 template <typename T>
_printTraceSegments(T const & globalTraceSet)729 void _printTraceSegments(T const & globalTraceSet)
730 {
731     for (unsigned i = 0; i < length(globalTraceSet); ++i)
732     {
733         std::cerr << "Tracback No " << i + 1 << std::endl;
734         for (unsigned j = length(globalTraceSet[i]); j > 0; --j)
735         {
736             std::cerr << globalTraceSet[i][j-1] << "  ";
737         }
738         std::cerr << std::endl;
739     }
740 }
741 
742 // ----------------------------------------------------------------------------
743 // Function _initiaizeBeginningOfBandedChain()
744 // ----------------------------------------------------------------------------
745 
746 // Handles the initialization of cells for the very first dp matrix.
747 template <typename TScoutState, typename TSizeH, typename TSizeV, typename TScoreScheme, typename TFreeEndGaps,
748           typename TDPMatrixLocation, typename TGapCosts, typename TTraceback, typename TExecPolicy>
749 inline void
_initiaizeBeginningOfBandedChain(TScoutState & scoutState,TSizeH sizeH,TSizeV sizeV,TScoreScheme const & scoreScheme,DPProfile_<BandedChainAlignment_<TFreeEndGaps,TDPMatrixLocation>,TGapCosts,TTraceback,TExecPolicy> const &)750 _initiaizeBeginningOfBandedChain(TScoutState & scoutState,
751                                  TSizeH sizeH,
752                                  TSizeV sizeV,
753                                  TScoreScheme const & scoreScheme,
754                                  DPProfile_<BandedChainAlignment_<TFreeEndGaps, TDPMatrixLocation>, TGapCosts, TTraceback, TExecPolicy> const &)
755 {
756     typedef typename TScoutState::TInitCell TInitCell;
757     typedef typename Value<TInitCell, 3>::Type TDPCell;
758     typedef DPProfile_<BandedChainAlignment_<TFreeEndGaps, TDPMatrixLocation>, TGapCosts, TTraceback, TExecPolicy> TDPProfile;
759 
760     TDPCell dpInitCellHorizontal;
761     TDPCell dummy;
762     _computeScore(dpInitCellHorizontal, dummy, dummy, dummy,
763                   Nothing(), Nothing(), Nothing(),
764                   RecursionDirectionZero(), TDPProfile());
765     scoutState._nextInitializationCells.insert(TInitCell(0,0, dpInitCellHorizontal));
766 
767     for (TSizeH activeColumn = 1; activeColumn < sizeH; ++activeColumn)
768     {
769         TDPCell prevCell = dpInitCellHorizontal;
770         if (IsFreeEndGap_<TFreeEndGaps, DPFirstRow>::VALUE)
771             _computeScore(dpInitCellHorizontal, dummy, dummy, dummy,
772                   Nothing(), Nothing(), scoreScheme,
773                   RecursionDirectionZero(), TDPProfile());
774         else
775             _computeScore(dpInitCellHorizontal, dummy, prevCell, dummy,
776                   Nothing(), Nothing(), scoreScheme,
777                   RecursionDirectionHorizontal(), TDPProfile());
778         scoutState._nextInitializationCells.insert(TInitCell(activeColumn, 0, dpInitCellHorizontal));
779     }
780 
781     TDPCell dpInitCellVertical;
782     _computeScore(dpInitCellVertical, dummy, dummy, dummy,
783                   Nothing(), Nothing(), Nothing(),
784                   RecursionDirectionZero(), TDPProfile());
785     for(TSizeV activeRow = 1; activeRow < sizeV; ++activeRow)
786     {
787 //        TDPCell prevCell = dpInitCellVertical;
788         if (IsFreeEndGap_<TFreeEndGaps, DPFirstColumn>::VALUE)
789             _computeScore(dpInitCellVertical, dummy, dummy, dummy,
790                           Nothing(), Nothing(), scoreScheme,
791                           RecursionDirectionZero(), TDPProfile());
792         else
793             _computeScore(dummy, dummy, dummy, dpInitCellVertical,
794                           Nothing(), Nothing(), scoreScheme,
795                           RecursionDirectionVertical(), TDPProfile());
796         scoutState._nextInitializationCells.insert(TInitCell(0, activeRow, dpInitCellVertical));
797     }
798 
799 }
800 
801 // ----------------------------------------------------------------------------
802 // Function _initializeBandedChain()
803 // ----------------------------------------------------------------------------
804 
805 // Initializes the banded chain alignment. It computes the first band or the
806 // first gap-area followed by the first anchor.
807 template <typename TTraceSet, typename TScoutState, typename TSeed, typename TSeqH, typename TSeqV,
808           typename TScoreScheme, typename TFreeEndGaps, typename TDPMatrixLocation, typename TGaps,
809           typename TTracebackConfig, typename TExecPolicy>
810 inline typename Value<TScoreScheme>::Type
_initializeBandedChain(TTraceSet & globalTraceSet,TScoutState & scoutState,TSeed const & seed,unsigned bandExtension,TSeqH const & seqH,TSeqV const & seqV,TScoreScheme const & scoreSchemeAnchor,TScoreScheme const & scoreSchemeGap,DPProfile_<BandedChainAlignment_<TFreeEndGaps,TDPMatrixLocation>,TGaps,TracebackOn<TTracebackConfig>,TExecPolicy> const & dpProfile)811 _initializeBandedChain(TTraceSet & globalTraceSet,
812                        TScoutState & scoutState,
813                        TSeed const & seed,
814                        unsigned bandExtension,
815                        TSeqH const & seqH,
816                        TSeqV const & seqV,
817                        TScoreScheme const & scoreSchemeAnchor,
818                        TScoreScheme const & scoreSchemeGap,
819                        DPProfile_<BandedChainAlignment_<TFreeEndGaps, TDPMatrixLocation>, TGaps, TracebackOn<TTracebackConfig>, TExecPolicy> const & dpProfile)
820 {
821     typedef typename Infix<TSeqH const>::Type TInfixH;
822     typedef typename Infix<TSeqV const>::Type TInfixV;
823     typedef typename Position<TSeed const>::Type TSeedPosition;
824     typedef typename MakeSigned<TSeedPosition>::Type TSignedPosition;
825     typedef typename Size<TSeed const>::Type TSeedSize;
826     typedef typename MakeSigned<TSeedSize>::Type TSignedSize;
827     //typedef typename Value<TTraceSet>::Type TTraceArray;
828     typedef typename Value<TScoreScheme>::Type TScore;
829     typedef Pair<TSeedPosition, TSeedPosition> TGridPoint;
830 
831     // TODO(rmaerker): Change _horizontalBandShift to _horizontalBandShiftBeginPoint
832     // TODO(rmaerker): Change _verticalBandShift to _verticalBandShiftBeginPoint
833     TSeedSize horizontalBandShift = _horizontalBandShiftBeginPoint(seed);
834     TSeedSize verticalBandShift = _verticalBandShiftBeginPoint(seed);
835 
836     TSeedSize horizontalNextGridOrigin = _max(0, static_cast<TSignedPosition>(beginPositionH(seed)) + 1 -
837                                               static_cast<TSignedPosition>(bandExtension));
838     TSeedSize verticalNextGridOrigin = _max(0, static_cast<TSignedPosition>(beginPositionV(seed)) + 1 -
839                                             static_cast<TSignedPosition>(bandExtension));
840 
841     DPBandConfig<BandOn> band;
842     // Determine coordinates of lower right corner of gap-area.
843     setUpperDiagonal(band, static_cast<TSignedPosition>(_min(static_cast<TSignedSize>(length(seqH)),
844         static_cast<TSignedPosition>(horizontalNextGridOrigin +  (bandExtension << 1) + horizontalBandShift +
845         _max(0,static_cast<TSignedPosition>(bandExtension) - static_cast<TSignedPosition>(beginPositionV(seed)) -1)) +
846         _min(0, static_cast<TSignedPosition>(beginPositionH(seed)) + 1 - static_cast<TSignedPosition>(bandExtension)))));
847 
848     setLowerDiagonal(band, -static_cast<TSignedPosition>(_min(static_cast<TSignedSize>(length(seqV)),
849         static_cast<TSignedPosition>(verticalNextGridOrigin + (bandExtension << 1) + verticalBandShift +
850         _max(0,static_cast<TSignedPosition>(bandExtension) - static_cast<TSignedPosition>(beginPositionH(seed)) -1)) +
851         _min(0, static_cast<TSignedPosition>(beginPositionV(seed)) + 1 - static_cast<TSignedPosition>(bandExtension)))));
852 
853     TScore score = 0;
854     TInfixH infixH;
855     TInfixV infixV;
856 
857     // The first anchor does not cross the first row or column. Hence we have to fill the gap first.
858     if (horizontalNextGridOrigin != 0u || verticalNextGridOrigin != 0u)
859     {
860         // INITIALIZATION
861         _initiaizeBeginningOfBandedChain(scoutState, upperDiagonal(band) + 1, 1 - lowerDiagonal(band), scoreSchemeGap,
862                                          dpProfile);
863         // Define infixes covering the area which is to be computed.
864         infixH = infix(seqH, 0, upperDiagonal(band));
865         infixV = infix(seqV, 0, -lowerDiagonal(band));
866 
867         // Prepare the scout state for the next part of the algorithm.
868         _reinitScoutState(scoutState, horizontalNextGridOrigin, verticalNextGridOrigin, 1 + upperDiagonal(band),
869                           1 - lowerDiagonal(band), 1 + upperDiagonal(band) - horizontalNextGridOrigin,
870                           1 - lowerDiagonal(band) - verticalNextGridOrigin);
871 
872         // Call the basic alignment function.
873         score = _computeAlignment(globalTraceSet, scoutState, infixH, infixV, scoreSchemeGap, DPBandConfig<BandOff>(),
874                                   DPProfile_<BandedChainAlignment_<TFreeEndGaps, BandedChainInitialDPMatrix>, TGaps,
875                                              TracebackOn<TTracebackConfig>, TExecPolicy>());
876     }
877     else
878     {
879         _initiaizeBeginningOfBandedChain(scoutState, upperDiagonal(band), -lowerDiagonal(band), scoreSchemeAnchor,
880                                          dpProfile);
881     }
882 
883     TGridPoint gridBegin;
884     // Determine coordinates of upper left corner of anchor.
885     gridBegin.i1 = horizontalNextGridOrigin;  // This is either 0 if not used before or the origin of the current matrix.
886     gridBegin.i2 = verticalNextGridOrigin;  // This is either 0 if not used before or the origin of the current matrix.
887 
888     // Check if the end point is at most as long as the sequences.
889     TGridPoint gridEnd;
890     gridEnd.i1 = _min(length(seqH), endPositionH(seed) + bandExtension);
891     gridEnd.i2 = _min(length(seqV), endPositionV(seed) + bandExtension);
892 
893     SEQAN_ASSERT(_checkScoreOverflow(gridEnd.i1 - gridBegin.i1 + gridEnd.i2 - gridBegin.i2, scoreSchemeGap));
894 
895     // Define area covering the infixes.
896     infixH = infix(seqH, gridBegin.i1, gridEnd.i1);
897     infixV = infix(seqV, gridBegin.i2, gridEnd.i2);
898 
899     horizontalBandShift = _horizontalBandShiftEndPoint(seed);
900     verticalBandShift = _verticalBandShiftEndPoint(seed);
901 
902     horizontalNextGridOrigin = _max(0, static_cast<TSignedPosition>(endPositionH(seed)) -
903         static_cast<TSignedPosition>(bandExtension) - static_cast<TSignedPosition>(horizontalBandShift) -
904         _max(0, static_cast<TSignedPosition>(endPositionV(seed) + bandExtension) -
905         static_cast<TSignedSize>(length(seqV))) - static_cast<TSignedPosition>(gridBegin.i1));
906     verticalNextGridOrigin = _max(0, static_cast<TSignedPosition>(endPositionV(seed)) -
907         static_cast<TSignedPosition>(bandExtension) - static_cast<TSignedPosition>(verticalBandShift) -
908         _max(0, static_cast<TSignedPosition>(endPositionH(seed) + bandExtension) -
909         static_cast<TSignedSize>(length(seqH))) - static_cast<TSignedPosition>(gridBegin.i2));
910 
911     setUpperDiagonal(band, upperDiagonal(band) - static_cast<TSignedPosition>(gridBegin.i1));
912     setLowerDiagonal(band, lowerDiagonal(band) + static_cast<TSignedPosition>(gridBegin.i2));
913 
914     // Calibrate the next vertical origin to the correct position within the band, only if it is a small band.
915     if (static_cast<TSignedPosition>(length(infixV)) + lowerDiagonal(band) > upperDiagonal(band))
916         verticalNextGridOrigin -= (length(infixV) + lowerDiagonal(band)) - upperDiagonal(band);
917 
918     _reinitScoutState(scoutState, horizontalNextGridOrigin, verticalNextGridOrigin, upperDiagonal(band) + 1,
919                   1 - lowerDiagonal(band), length(infixH) - horizontalNextGridOrigin + 1,
920                   length(infixV) - verticalNextGridOrigin + 1);
921 
922     // Prepare the scout state for the next part of the algorithm.
923     TTraceSet localTraceSet;
924     if (gridBegin.i1 == 0u && gridBegin.i2 == 0u)
925     {
926         if (gridEnd.i1 == length(seqH) && gridEnd.i2 == length(seqV))   // Standard global alignment problem.
927         {
928             resize(localTraceSet, 1);
929             DPScoutState_<Default> noScout;
930             score = _computeAlignment(localTraceSet[0], noScout, infixH, infixV, scoreSchemeGap, band,
931                       DPProfile_<GlobalAlignment_<TFreeEndGaps>, TGaps, TracebackOn<TTracebackConfig>, TExecPolicy>());
932         }
933         else
934             score = _computeAlignment(localTraceSet, scoutState, infixH, infixV, scoreSchemeGap, band,
935                       DPProfile_<BandedChainAlignment_<TFreeEndGaps, BandedChainInitialDPMatrix>, TGaps, TracebackOn<TTracebackConfig>, TExecPolicy>());
936     }
937     else
938     {
939         if (gridEnd.i1 == length(seqH) && gridEnd.i2 == length(seqV))  // The anchor crosses the end of the matrix.
940             score = _computeAlignment(localTraceSet, scoutState, infixH, infixV, scoreSchemeGap, band,
941                               DPProfile_<BandedChainAlignment_<TFreeEndGaps, BandedChainFinalDPMatrix>, TGaps, TracebackOn<TTracebackConfig>, TExecPolicy>());
942         else
943             score = _computeAlignment(localTraceSet, scoutState, infixH, infixV, scoreSchemeGap, band, dpProfile);
944     }
945 
946     if (gridBegin.i1 != 0u || gridBegin.i2 != 0u)
947     {
948         _adaptLocalTracesToGlobalGrid(localTraceSet, gridBegin);
949         if (!empty(localTraceSet))
950             _glueTracebacks(globalTraceSet, localTraceSet);
951     }
952     else
953         globalTraceSet = localTraceSet;
954 
955     scoutState._horizontalNextGridOrigin += gridBegin.i1;
956     scoutState._verticalNextGridOrigin += gridBegin.i2;
957 
958     if (static_cast<TSignedPosition>(length(infixV)) + lowerDiagonal(band) > upperDiagonal(band))
959         scoutState._verticalNextGridOrigin += (static_cast<TSignedPosition>(length(infixV)) + lowerDiagonal(band)) -
960                                                upperDiagonal(band);
961     return score;
962 }
963 
964 // ----------------------------------------------------------------------------
965 // Function _computeGapArea()
966 // ----------------------------------------------------------------------------
967 
968 template <typename TTraceSet, typename TDPScoutState, typename TSeed, typename TSeqH, typename TSeqV,
969           typename TScoreScheme, typename TFreeEndGaps, typename TDPMatrixLocation, typename TGaps, typename TTracebackSpec,
970           typename TExecPolicy>
971 inline typename Value<TScoreScheme>::Type
_computeGapArea(TTraceSet & globalTraceSet,TDPScoutState & scoutState,TSeed const & currentSeed,unsigned bandExtension,TSeqH const & seqH,TSeqV const & seqV,TScoreScheme const & scoreScheme,DPProfile_<BandedChainAlignment_<TFreeEndGaps,TDPMatrixLocation>,TGaps,TracebackOn<TTracebackSpec>,TExecPolicy> const & dpProfile)972 _computeGapArea(TTraceSet & globalTraceSet,
973                 TDPScoutState & scoutState,
974                 TSeed const & currentSeed,
975                 unsigned bandExtension,
976                 TSeqH const & seqH,
977                 TSeqV const & seqV,
978                 TScoreScheme const & scoreScheme,
979                 DPProfile_<BandedChainAlignment_<TFreeEndGaps, TDPMatrixLocation>, TGaps, TracebackOn<TTracebackSpec>, TExecPolicy> const & dpProfile)
980 {
981     typedef typename Infix<TSeqH const>::Type TInfixH;
982     typedef typename Infix<TSeqV const>::Type TInfixV;
983     typedef typename Position<TSeqH>::Type TPosH;
984     typedef typename Position<TSeqV>::Type TPosV;
985     typedef typename Position<TSeed const>::Type TPosition;
986     typedef DPBandConfig<BandOff> TBand;
987     typedef typename Value<TScoreScheme>::Type TScoreValue;
988     typedef Pair<TPosition, TPosition> TGridPoint;
989 
990     TGridPoint gridBegin(scoutState._horizontalNextGridOrigin, scoutState._verticalNextGridOrigin);
991 
992     // TODO(rmaerker): Change _horizontalBandShift to _horizontalBandShiftBeginPoint
993     // TODO(rmaerker): Change _verticalBandShift to _verticalBandShiftBeginPoint
994     TGridPoint gridEnd(beginPositionH(currentSeed) + 1 + bandExtension + _horizontalBandShiftBeginPoint(currentSeed),
995                        beginPositionV(currentSeed) + 1 + bandExtension + _verticalBandShiftBeginPoint(currentSeed));
996 
997     SEQAN_ASSERT(_checkScoreOverflow(gridEnd.i1 - gridBegin.i1 + gridEnd.i2 - gridBegin.i2, scoreScheme));
998 
999     // Define infix area for alignment.
1000     TInfixH infixH = infix(seqH, gridBegin.i1, gridEnd.i1);
1001     TInfixV infixV = infix(seqV, gridBegin.i2, gridEnd.i2);
1002 
1003     // Relative begin positions of next grid.
1004     TPosH horizontalNextGridOrigin = beginPositionH(currentSeed) + 1 - bandExtension - gridBegin.i1;
1005     TPosV verticalNextGridOrigin = beginPositionV(currentSeed) + 1 - bandExtension - gridBegin.i2;
1006 
1007     // Prepare the scout state for the next part of the algorithm.
1008     _reinitScoutState(scoutState, horizontalNextGridOrigin, verticalNextGridOrigin, gridEnd.i1 - gridBegin.i1 + 1,
1009                       gridEnd.i2 - gridBegin.i2 + 1, gridEnd.i1 - gridBegin.i1 + 1 - horizontalNextGridOrigin,
1010                       gridEnd.i2 - gridBegin.i2 + 1 - verticalNextGridOrigin);
1011 
1012     // Compute the alignment.
1013     TTraceSet localTraceSet;
1014     TScoreValue score = _computeAlignment(localTraceSet, scoutState, infixH, infixV, scoreScheme, TBand(), dpProfile);
1015 
1016     // Adapt the local traces to match the positions of the  global grid.
1017     _adaptLocalTracesToGlobalGrid(localTraceSet, gridBegin);
1018     if (!empty(localTraceSet))
1019         _glueTracebacks(globalTraceSet, localTraceSet);
1020 
1021     scoutState._horizontalNextGridOrigin += gridBegin.i1;
1022     scoutState._verticalNextGridOrigin += gridBegin.i2;
1023     return score;
1024 }
1025 
1026 // ----------------------------------------------------------------------------
1027 // Function _computeAnchorArea()
1028 // ----------------------------------------------------------------------------
1029 
1030 template <typename TTraceSet, typename TDPScoutState, typename TSeed, typename TSeqH, typename TSeqV,
1031           typename TScoreScheme, typename TFreeEndGaps, typename TDPMatrixLocation, typename TGaps, typename TTracebackSpec,
1032           typename TExecPolicy>
1033 inline typename Value<TScoreScheme>::Type
_computeAnchorArea(TTraceSet & globalTraceSet,TDPScoutState & scoutState,TSeed const & currentSeed,unsigned bandExtension,TSeqH const & seqH,TSeqV const & seqV,TScoreScheme const & scoreScheme,DPProfile_<BandedChainAlignment_<TFreeEndGaps,TDPMatrixLocation>,TGaps,TracebackOn<TTracebackSpec>,TExecPolicy> const & dpProfile)1034 _computeAnchorArea(TTraceSet & globalTraceSet,
1035                    TDPScoutState & scoutState,
1036                    TSeed const & currentSeed,
1037                    unsigned bandExtension,
1038                    TSeqH const & seqH,
1039                    TSeqV const & seqV,
1040                    TScoreScheme const & scoreScheme,
1041                    DPProfile_<BandedChainAlignment_<TFreeEndGaps, TDPMatrixLocation>, TGaps, TracebackOn<TTracebackSpec>, TExecPolicy> const & dpProfile)
1042 {
1043     typedef typename Infix<TSeqH const>::Type TInfixH;
1044     typedef typename Infix<TSeqV const>::Type TInfixV;
1045     typedef typename Size<TSeed const>::Type TSize;
1046     typedef typename MakeSigned<TSize>::Type TSignedSize;
1047     typedef typename Position<TSeed const>::Type TPosition;
1048     typedef typename MakeSigned<TPosition>::Type TSignedPosition;
1049     //typedef DPBandConfig<BandOn> TBand;
1050     typedef typename Value<TScoreScheme>::Type TScore;
1051     typedef Pair<TPosition, TPosition> TGridPoint;
1052 
1053     TGridPoint gridBegin(scoutState._horizontalNextGridOrigin, scoutState._verticalNextGridOrigin);
1054     TGridPoint gridEnd(endPositionH(currentSeed) + bandExtension,
1055                        endPositionV(currentSeed) + bandExtension);
1056 
1057     // Define area covering the infixes of the current sub-matrix.
1058     TInfixH infixH = infix(seqH, gridBegin.i1, gridEnd.i1);
1059     TInfixV infixV = infix(seqV, gridBegin.i2, gridEnd.i2);
1060 
1061     TPosition horizontalBandShift = _horizontalBandShiftBeginPoint(currentSeed);
1062     TPosition verticalBandShift = _verticalBandShiftBeginPoint(currentSeed);
1063 
1064     // Computing the start point of the next grid that overlaps with the current anchor at the end.
1065     TPosition horizontalNextGridOrigin = endPositionH(currentSeed) - bandExtension - _horizontalBandShiftEndPoint(currentSeed) - gridBegin.i1;
1066     TPosition verticalNextGridOrigin = endPositionV(currentSeed) - bandExtension - _verticalBandShiftEndPoint(currentSeed) - gridBegin.i2;
1067 
1068     // Computing the start position of the band according to the start point.
1069     // TODO(rmaerker): Rename the function _verticalBandShift to _verticalBandShiftBeginPoint()
1070     // TODO(rmaerker): Rename the function _horiontalBandShift to _horizontalBandShiftBeginPoint()
1071     DPBandConfig<BandOn> band(-static_cast<TSignedPosition>((bandExtension << 1)) -static_cast<TSignedPosition>(verticalBandShift),
1072                          static_cast<TSignedPosition>((bandExtension << 1) + horizontalBandShift));
1073 
1074     TPosition relativeVerticalNextGridOrigin = verticalNextGridOrigin;
1075     // Calibrate the next vertical origin to the correct position within the band.
1076     if (static_cast<TSignedSize>(length(infixV)) + lowerDiagonal(band) > upperDiagonal(band))
1077         relativeVerticalNextGridOrigin -= (length(infixV) + lowerDiagonal(band)) - upperDiagonal(band);
1078 
1079     _reinitScoutState(scoutState, horizontalNextGridOrigin, relativeVerticalNextGridOrigin, upperDiagonal(band) + 1,
1080                       1 - lowerDiagonal(band), length(infixH) - horizontalNextGridOrigin + 1,
1081                       length(infixV) - verticalNextGridOrigin + 1);
1082 
1083     // Compute the alignment.
1084     TTraceSet localTraceSet;
1085     TScore score = _computeAlignment(localTraceSet, scoutState, infixH, infixV, scoreScheme, band, dpProfile);
1086 
1087     // Adapt the local traces to match the positions of the  global grid.
1088     _adaptLocalTracesToGlobalGrid(localTraceSet, gridBegin);
1089     if (!empty(localTraceSet))
1090         _glueTracebacks(globalTraceSet, localTraceSet);
1091 
1092     scoutState._horizontalNextGridOrigin += gridBegin.i1;
1093     scoutState._verticalNextGridOrigin += gridBegin.i2;
1094     if (static_cast<TSignedSize>(length(infixV)) + lowerDiagonal(band) > upperDiagonal(band))
1095         scoutState._verticalNextGridOrigin += (length(infixV) + lowerDiagonal(band)) - upperDiagonal(band);
1096     return score;
1097 }
1098 
1099 // ----------------------------------------------------------------------------
1100 // Function _finishBandedChain()
1101 // ----------------------------------------------------------------------------
1102 
1103 template <typename TTraceSet, typename TDPScoutState, typename TSeed, typename TSeqH, typename TSeqV,
1104           typename TScoreScheme, typename TFreeEndGaps, typename TDPMatrixLocation, typename TGaps, typename TTracebackConfig,
1105           typename TExecPolicy>
1106 inline typename Value<TScoreScheme>::Type
_finishBandedChain(TTraceSet & globalTraceSet,TDPScoutState & dpScoutState,TSeed const & currentSeed,unsigned bandExtension,TSeqH const & seqH,TSeqV const & seqV,TScoreScheme const & scoreSchemeAnchor,TScoreScheme const & scoreSchemeGap,DPProfile_<BandedChainAlignment_<TFreeEndGaps,TDPMatrixLocation>,TGaps,TracebackOn<TTracebackConfig>,TExecPolicy> const & dpProfile)1107 _finishBandedChain(TTraceSet & globalTraceSet,
1108                    TDPScoutState & dpScoutState,
1109                    TSeed const & currentSeed,
1110                    unsigned bandExtension,
1111                    TSeqH const & seqH,
1112                    TSeqV const & seqV,
1113                    TScoreScheme const & scoreSchemeAnchor,
1114                    TScoreScheme const & scoreSchemeGap,
1115                    DPProfile_<BandedChainAlignment_<TFreeEndGaps, TDPMatrixLocation>, TGaps, TracebackOn<TTracebackConfig>, TExecPolicy> const & dpProfile)
1116 {
1117     //typedef typename Position<TSeqH const>::Type TPosH;
1118     //typedef typename Position<TSeqV const>::Type TPosV;
1119     typedef typename Position<TSeed const>::Type TPosition;
1120     typedef typename MakeSigned<TPosition>::Type TSignedPosition;
1121     typedef typename Size<TSeed const>::Type TSize;
1122     typedef typename MakeSigned<TSize>::Type TSignedSize;
1123     typedef typename Infix<TSeqH const>::Type TInfixH;
1124     typedef typename Infix<TSeqV const>::Type TInfixV;
1125     typedef typename Value<TScoreScheme>::Type TScoreValue;
1126     typedef Pair<TPosition, TPosition> TGridPoint;
1127 
1128     // Part A: Compute last connecting rectangle between last two anchors
1129     TGridPoint gridBegin(dpScoutState._horizontalNextGridOrigin, dpScoutState._verticalNextGridOrigin);
1130 
1131     // TODO(rmaerker): Change _horizontalBandShift to _horizontalBandShiftBeginPoint
1132     // TODO(rmaerker): Change _verticalBandShift to _verticalBandShiftBeginPoint
1133     TPosition horizontalBandShift = _horizontalBandShiftBeginPoint(currentSeed);
1134     TPosition verticalBandShift = _verticalBandShiftBeginPoint(currentSeed);
1135 
1136     TGridPoint gridEnd(_min(length(seqH), beginPositionH(currentSeed) + 1 + bandExtension + horizontalBandShift),
1137                        _min(length(seqV), beginPositionV(currentSeed) + 1 + bandExtension + verticalBandShift));
1138 
1139     // Define infix area for alignment.
1140     TInfixH infixH = infix(seqH, gridBegin.i1, gridEnd.i1);
1141     TInfixV infixV = infix(seqV, gridBegin.i2, gridEnd.i2);
1142 
1143     // Relative begin positions of next grid.
1144     TPosition horizontalNextGridOrigin = _max(0, static_cast<TSignedPosition>(beginPositionH(currentSeed)) + 1 -
1145         static_cast<TSignedPosition>(bandExtension) - static_cast<TSignedPosition>(gridBegin.i1));
1146     TPosition verticalNextGridOrigin = _max(0, static_cast<TSignedPosition>(beginPositionV(currentSeed)) + 1 -
1147         static_cast<TSignedPosition>(bandExtension) - static_cast<TSignedPosition>(gridBegin.i2));
1148 
1149     // Prepare the scout state for the next part of the algorithm.
1150     _reinitScoutState(dpScoutState, horizontalNextGridOrigin, verticalNextGridOrigin, length(infixH) + 1,
1151                       length(infixV) + 1, length(infixH) - horizontalNextGridOrigin + 1,
1152                       length(infixV) - verticalNextGridOrigin + 1);
1153 
1154     // Compute the alignment.
1155     TTraceSet localTraceSet;
1156     _computeAlignment(localTraceSet, dpScoutState, infixH, infixV, scoreSchemeGap, DPBandConfig<BandOff>(), dpProfile);
1157 
1158     // Adapt the local traces to match the positions of the  global grid.
1159     _adaptLocalTracesToGlobalGrid(localTraceSet, gridBegin);
1160     if (!empty(localTraceSet))
1161         _glueTracebacks(globalTraceSet, localTraceSet);
1162 
1163     // Part B: Compute the last anchor
1164 
1165     // Get the absolute positions of the beginning point of the band.
1166     gridBegin.i1 += horizontalNextGridOrigin;
1167     gridBegin.i2 += verticalNextGridOrigin;
1168 
1169     // Get the absolute positions of the end point of the band.
1170     gridEnd.i1 = _min(length(seqH), endPositionH(currentSeed) + bandExtension);
1171     gridEnd.i2 = _min(length(seqV), endPositionV(currentSeed) + bandExtension);
1172 
1173     infixH = infix(seqH, gridBegin.i1, gridEnd.i1);
1174     infixV = infix(seqV, gridBegin.i2, gridEnd.i2);
1175 
1176     // The last anchor is crossing the end of the global matrix in both directions.
1177     if(gridEnd.i1 == length(seqH) && gridEnd.i2 == length(seqV))
1178     {
1179         DPBandConfig<BandOn> band(-static_cast<TSignedPosition>(gridEnd.i2 - gridBegin.i2), static_cast<TSignedPosition>(gridEnd.i1 - gridBegin.i1));
1180         _reinitScoutState(dpScoutState, 0, 0, upperDiagonal(band)+ 1, 1 - lowerDiagonal(band),
1181                           upperDiagonal(band)+ 1, 1 - lowerDiagonal(band));
1182             // TODO(rmaerker): Should we not set the nextGridOrigin to 0 as it is the case for the last rectangle? We want to compute the last full path.
1183             // Compute the last anchor which crosses the end of the global grid.
1184         clear(localTraceSet);
1185         TScoreValue score = _computeAlignment(localTraceSet, dpScoutState, infixH, infixV, scoreSchemeAnchor, band,
1186                                   DPProfile_<BandedChainAlignment_<TFreeEndGaps, BandedChainFinalDPMatrix>, TGaps, TracebackOn<TTracebackConfig>, TExecPolicy>());
1187 
1188         _adaptLocalTracesToGlobalGrid(localTraceSet, gridBegin);
1189         if (!empty(localTraceSet))
1190             _glueTracebacks(globalTraceSet, localTraceSet);
1191         return score;
1192     }
1193 
1194     DPBandConfig<BandOn> band(-static_cast<TSignedPosition>((bandExtension << 1)) -static_cast<TSignedPosition>(verticalBandShift),
1195                          static_cast<TSignedPosition>((bandExtension << 1) + horizontalBandShift));
1196 
1197     // Compute the last anchor and the rectangle to fill the gap between the end of the global matrix and the last anchor.
1198     horizontalNextGridOrigin = _max(0,
1199         static_cast<TSignedPosition>(endPositionH(currentSeed)) - static_cast<TSignedPosition>(bandExtension) -
1200         static_cast<TSignedPosition>(_horizontalBandShiftEndPoint(currentSeed)) - static_cast<TSignedPosition>(gridBegin.i1) -
1201         _max(0, static_cast<TSignedPosition>(endPositionV(currentSeed) + bandExtension) - static_cast<TSignedSize>(length(seqV))));
1202 
1203     verticalNextGridOrigin = _max(0,
1204         static_cast<TSignedPosition>(endPositionV(currentSeed)) - static_cast<TSignedPosition>(bandExtension) -
1205         static_cast<TSignedPosition>(_verticalBandShiftEndPoint(currentSeed)) - static_cast<TSignedPosition>(gridBegin.i2) -
1206         _max(0, static_cast<TSignedPosition>(endPositionH(currentSeed) + bandExtension) - static_cast<TSignedSize>(length(seqH))));
1207 
1208     // Calibrate the next vertical origin to the correct position within the band.
1209     // TODO(rmaerker): Does this also apply to the case, when the anchor is crossing the end of the matrix?
1210     if (static_cast<TSignedSize>(length(infixV)) + lowerDiagonal(band) > upperDiagonal(band))
1211         verticalNextGridOrigin -= (length(infixV) + lowerDiagonal(band)) - upperDiagonal(band);
1212 
1213    _reinitScoutState(dpScoutState, horizontalNextGridOrigin, verticalNextGridOrigin, upperDiagonal(band) + 1,
1214                   1 - lowerDiagonal(band), length(infixH) - horizontalNextGridOrigin + 1,
1215                   length(infixV) - verticalNextGridOrigin + 1);
1216 
1217     clear(localTraceSet);
1218     // Compute the last anchor.
1219     TScoreValue score = _computeAlignment(localTraceSet, dpScoutState, infixH, infixV, scoreSchemeAnchor, band, dpProfile);
1220     _adaptLocalTracesToGlobalGrid(localTraceSet, gridBegin);
1221     if (!empty(localTraceSet))
1222         _glueTracebacks(globalTraceSet, localTraceSet);
1223 
1224     // Close the gap between last anchor and the end of the global grid.
1225     gridBegin.i1 += horizontalNextGridOrigin;
1226     if (static_cast<TSignedSize>(length(infixV)) + lowerDiagonal(band) > upperDiagonal(band))
1227         verticalNextGridOrigin += (length(infixV) + lowerDiagonal(band)) - upperDiagonal(band);
1228     gridBegin.i2 += verticalNextGridOrigin;
1229 
1230     _reinitScoutState(dpScoutState, 0, 0, length(seqH) - gridBegin.i1 + 1, length(seqV) - gridBegin.i2 + 1,
1231                       length(seqH) - gridBegin.i1 + 1, length(seqV) - gridBegin.i2 + 1);
1232 
1233     // Compute the alignment.
1234     clear(localTraceSet);
1235     score = _computeAlignment(localTraceSet, dpScoutState, suffix(seqH, gridBegin.i1), suffix(seqV,gridBegin.i2),
1236                               scoreSchemeGap, DPBandConfig<BandOff>(), DPProfile_<BandedChainAlignment_<TFreeEndGaps,
1237                               BandedChainFinalDPMatrix>, TGaps, TracebackOn<TTracebackConfig>, TExecPolicy>());
1238 
1239     _adaptLocalTracesToGlobalGrid(localTraceSet, gridBegin);
1240     if (!empty(localTraceSet))
1241         _glueTracebacks(globalTraceSet, localTraceSet);
1242     return score;
1243 }
1244 
1245 // ----------------------------------------------------------------------------
1246 // Function _computeAlignment()                                    [DP-Wrapper]
1247 // ----------------------------------------------------------------------------
1248 
1249 template <typename TGapScheme, typename TTraceTarget, typename TScoutState, typename TSequenceH, typename TSequenceV,
1250           typename TScoreScheme, typename TBandSwitch, typename TAlignmentAlgorithm, typename TTraceFlag,
1251           typename TExecPolicy>
1252 inline typename Value<TScoreScheme>::Type
_computeAlignment(TTraceTarget & traceSegments,TScoutState & scoutState,TSequenceH const & seqH,TSequenceV const & seqV,TScoreScheme const & scoreScheme,DPBandConfig<TBandSwitch> const & band,DPProfile_<TAlignmentAlgorithm,TGapScheme,TTraceFlag,TExecPolicy> const & dpProfile)1253 _computeAlignment(TTraceTarget & traceSegments,
1254                   TScoutState & scoutState,
1255                   TSequenceH const & seqH,
1256                   TSequenceV const & seqV,
1257                   TScoreScheme const & scoreScheme,
1258                   DPBandConfig<TBandSwitch> const & band,
1259                   DPProfile_<TAlignmentAlgorithm, TGapScheme, TTraceFlag, TExecPolicy> const & dpProfile)
1260 {
1261     typedef typename Value<TScoreScheme>::Type TScoreValue;
1262     typedef DPContext<DPCell_<TScoreValue, TGapScheme>, typename TraceBitMap_<TScoreValue>::Type> TDPContext;
1263     TDPContext dpContext;
1264     return _computeAlignment(dpContext, traceSegments, scoutState, seqH, seqV, scoreScheme, band, dpProfile);
1265 }
1266 
1267 // ----------------------------------------------------------------------------
1268 // Function _computeAlignment()                          [BandedChainAlignment]
1269 // ----------------------------------------------------------------------------
1270 
1271 // Given a monotonic non-decreasing seed chain the following algorithms computes a
1272 // global alignment around this rough global map, while connecting two anchors  by filling
1273 // the gap between them using a standard dp algorithm.
1274 template <typename TTraceSet, typename TSeedSet, typename TSequenceH, typename TSequenceV, typename TScoreValue,
1275           typename TScoreSpecAnchor, typename TScoreSpecGap, typename TFreeEndGaps, typename TDPMatrixLocation,
1276           typename TGapSpec, typename TTracebackConfig, typename TExecPolicy>
1277 inline TScoreValue
_computeAlignment(TTraceSet & globalTraceSet,TSeedSet const & seedSet,TSequenceH const & seqH,TSequenceV const & seqV,Score<TScoreValue,TScoreSpecAnchor> const & scoreSchemeAnchor,Score<TScoreValue,TScoreSpecGap> const & scoreSchemeGap,unsigned bandExtension,DPProfile_<BandedChainAlignment_<TFreeEndGaps,TDPMatrixLocation>,TGapSpec,TracebackOn<TTracebackConfig>,TExecPolicy> const & profile)1278 _computeAlignment(TTraceSet & globalTraceSet,
1279                   TSeedSet const & seedSet,
1280                   TSequenceH const & seqH,
1281                   TSequenceV const & seqV,
1282                   Score<TScoreValue, TScoreSpecAnchor> const & scoreSchemeAnchor,
1283                   Score<TScoreValue, TScoreSpecGap> const & scoreSchemeGap,
1284                   unsigned bandExtension,
1285                   DPProfile_<BandedChainAlignment_<TFreeEndGaps, TDPMatrixLocation>, TGapSpec,
1286                              TracebackOn<TTracebackConfig>, TExecPolicy> const & profile)
1287 {
1288     typedef typename Position<TSequenceH>::Type TPosH;
1289     typedef typename Position<TSequenceV>::Type TPosV;
1290     typedef typename Iterator<TSeedSet const, Standard>::Type TSeedSetIterator;
1291 
1292     typedef DPCell_<TScoreValue, TGapSpec> TDPCell;
1293     typedef DPScoutState_<BandedChainAlignmentScoutState<TDPCell> > TScoutState;
1294 
1295     // handle cases of empty sequences
1296     SEQAN_ASSERT_MSG(!empty(seqH), "Cannot compute alignment on empty sequence (horizontal sequence)");
1297     SEQAN_ASSERT_MSG(!empty(seqV), "Cannot compute alignment on empty sequence (vertical sequence)");
1298 
1299     // TODO(rmaerker): At the moment the band must have at least a minimalBandwidth of size three. Maybe we change that.
1300     SEQAN_ASSERT_GEQ(bandExtension, 1u);
1301 
1302     // Handle case of empty seed set.
1303     if (length(seedSet) < 1)
1304         return std::numeric_limits<TScoreValue>::min();
1305 
1306     // Find the first anchor that is not covered by the region between the beginning of the matrix and the next anchor
1307     // considering the minbandwidh parameter.
1308     TSeedSetIterator it = _findFirstAnchor(seedSet, bandExtension);
1309     // Find the last anchor that is not covered by the region between the previous anchor and the end of the matrix.
1310     TSeedSetIterator itEnd = _findLastAnchor(it, seedSet, seqH, seqV, bandExtension);
1311 
1312     SEQAN_ASSERT(itEnd != static_cast<TSeedSetIterator>(end(seedSet, Standard())));
1313     // The scout state stores the current state of the dp scout and is used to store the
1314     // initialization values for the next intersecting grid.
1315     TScoutState scoutState;
1316 
1317     // INITIALIZATION
1318     // Initialize the banded chain alignment to compute the first gap.
1319     TScoreValue score = _initializeBandedChain(globalTraceSet, scoutState, value(it), bandExtension, seqH, seqV,
1320                                                scoreSchemeAnchor, scoreSchemeGap, profile);
1321 
1322     // We need to close the band already if there is no other seed to consider.
1323     if (length(seedSet) == 1 || (it == itEnd && _isLastSeed(itEnd, seedSet)))
1324     {
1325         // There is nothing to close.
1326         if (endPositionH(value(it)) + bandExtension < length(seqH) ||
1327             endPositionV(value(it)) + bandExtension < length(seqV))
1328         {
1329             TPosH gridBeginH = scoutState._horizontalNextGridOrigin;
1330             TPosV gridBeginV = scoutState._verticalNextGridOrigin;
1331             _reinitScoutState(scoutState, 0, 0, length(seqH) + 1 - gridBeginH, length(seqV) + 1 - gridBeginV,
1332                               length(seqH) + 1 - gridBeginH, length(seqV) + 1 - gridBeginV);
1333 
1334             // compute the alignment
1335             TTraceSet localTraceSet;
1336             score = _computeAlignment(localTraceSet, scoutState, suffix(seqH, gridBeginH), suffix(seqV, gridBeginV),
1337                               scoreSchemeGap, DPBandConfig<BandOff>(), DPProfile_<BandedChainAlignment_<TFreeEndGaps,
1338                               BandedChainFinalDPMatrix>, TGapSpec, TracebackOn<TTracebackConfig>, TExecPolicy>());
1339             _adaptLocalTracesToGlobalGrid(localTraceSet, Pair<TPosH, TPosV>(gridBeginH, gridBeginV));
1340             _glueTracebacks(globalTraceSet, localTraceSet);
1341         }
1342         return score;
1343     }
1344 
1345     // MAIN: Process all inner seeds.
1346     while (it != itEnd)
1347     {
1348         SEQAN_ASSERT(_checkColinearity(it));
1349         // Process the next gap area that connects to anchors.
1350         _computeGapArea(globalTraceSet, scoutState, value(++it), bandExtension, seqH, seqV, scoreSchemeGap,
1351                         profile);
1352         // Process the folowing anchor.
1353         _computeAnchorArea(globalTraceSet, scoutState, value(it), bandExtension, seqH, seqV, scoreSchemeAnchor, profile);
1354     }
1355     SEQAN_ASSERT(_checkColinearity(it));
1356     // Finish the banded chain alignment while computing the closing gap.
1357     score = _finishBandedChain(globalTraceSet, scoutState, value(++it), bandExtension, seqH, seqV, scoreSchemeAnchor,
1358                                scoreSchemeGap, profile);
1359     return score;
1360 }
1361 
1362 }  // namespace seqan
1363 
1364 #endif  // #ifndef INCLUDE_SEQAN_SEEDS_BANDED_CHAIN_ALIGNMENT_IMPL_H_
1365