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: Birte Kehr <birte.kehr@fu-berlin.de>
33 // ==========================================================================
34 // Operations on alignments such as integration
35 // ==========================================================================
36 
37 #ifndef SEQAN_INCLUDE_SEQAN_ALIGN_ALIGNMENT_OPERATIONS_H_
38 #define SEQAN_INCLUDE_SEQAN_ALIGN_ALIGNMENT_OPERATIONS_H_
39 
40 namespace seqan {
41 
42 // ============================================================================
43 // Forwards
44 // ============================================================================
45 
46 // ============================================================================
47 // Tags, Classes, Enums
48 // ============================================================================
49 
50 // ============================================================================
51 // Metafunctions
52 // ============================================================================
53 
54 // ============================================================================
55 // Functions
56 // ============================================================================
57 
58 // ----------------------------------------------------------------------------
59 // Function integrateAlign()
60 // ----------------------------------------------------------------------------
61 
62 /*!
63  * @fn integrateAlign
64  * @headerfile <seqan/align.h>
65  * @brief Integrates an alignment into another by copying the gaps.
66  *
67  * @signature void integrateAlign(align1, align2[, positions]);
68  *
69  * @param[in,out] align1    Target Alignment object into which align2 is to be integrated.
70  * @param[in]     align2    Alignment object that is to be integrated into align1.
71  * @param[in]     positions The integration positions in align1 for all rows (view positions), String of positions.
72  *
73  * @section Examples
74  *
75  * @include demos/align/integrate_align.cpp
76  *
77  * The output is as follows:
78  *
79  * @include demos/align/integrate_align.cpp.stdout
80  */
81 
82 template <typename TSource1, typename TSpec1, typename TSource2, typename TSpec2, typename TPos>
integrateAlign(Align<TSource1,TSpec1> & align,Align<TSource2,TSpec2> const & infixAlign,String<TPos> const & viewPos)83 void integrateAlign(Align<TSource1, TSpec1> & align,
84                     Align<TSource2, TSpec2> const & infixAlign,
85                     String<TPos> const & viewPos)
86 {
87     typedef Align<TSource1, TSpec1> TAlign;
88     typedef Align<TSource2, TSpec2> TInfixAlign;
89     typedef typename Size<TAlign>::Type TSize;
90 
91     typedef typename Row<TAlign>::Type TRow;
92     typedef typename Row<TInfixAlign const>::Type TInfixRow;
93 
94     // Iterators on align and infixAlign.
95     typename Iterator<TRow>::Type it;
96     typedef typename Iterator<TInfixRow, Standard>::Type TInfixRowIt;
97 
98 
99     for (TSize i = 0; i < length(rows(align)); ++i)
100     {
101         TInfixRow const & infixRow = row(infixAlign, i);
102         // This assertion ensures that the number of sequence characters after viewPos[i] is greater than or equal to
103         // the number of source characters in the clipped infix row.
104         SEQAN_ASSERT_GEQ(endPosition(row(align, i)) - toSourcePosition(row(align, i), viewPos[i]),
105                 endPosition(infixRow) - beginPosition(infixRow));
106 
107         // init iterators
108         it = iter(row(align, i), value(viewPos, i));
109 
110         // walk through Gaps containers and copy gaps
111         for (TInfixRowIt infixIt = begin(infixRow, Standard()); !atEnd(infixIt);)
112         {
113             TSize gapSize = countGaps(infixIt);
114             insertGaps(it, gapSize);
115             goFurther(it, gapSize+1);
116             goFurther(infixIt, gapSize+1);
117         }
118     }
119 }
120 
121 template <typename TSource, typename TSpec1, typename TSpec2>
integrateAlign(Align<TSource,TSpec1> & align,Align<typename Infix<TSource>::Type,TSpec2> const & infixAlign)122 void integrateAlign(Align<TSource, TSpec1> & align,
123                     Align<typename Infix<TSource>::Type, TSpec2> const & infixAlign)
124 {
125     typedef typename Size<TSource>::Type TSize;
126     typedef typename Position<typename Row<Align<TSource, TSpec1> >::Type>::Type TPos;
127 
128     String<TPos> viewPos;
129     TPos pos;
130     for (TSize i = 0; i < length(rows(infixAlign)); ++i)
131     {
132         pos = beginPosition(source(row(infixAlign, i)))
133             - beginPosition(source(row(align, i)))
134             + beginPosition(row(infixAlign, i));
135         appendValue(viewPos, toViewPosition(row(align, i), pos));
136     }
137 
138     integrateAlign(align, infixAlign, viewPos);
139 }
140 
141 }  // namespace seqan
142 
143 #endif  // #ifndef SEQAN_INCLUDE_SEQAN_ALIGN_ALIGNMENT_OPERATIONS_H_
144