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: Tobias Rausch <rausch@embl.de>
33 // Author: Anne-Katrin Emde <anne-katrin.emde@fu-berlin.de>
34 // ==========================================================================
35
36 #ifndef SEQAN_INCLUDE_SEQAN_GRAPH_ALGORITHM_REFINE_ALIGN_H_
37 #define SEQAN_INCLUDE_SEQAN_GRAPH_ALGORITHM_REFINE_ALIGN_H_
38
39 namespace seqan {
40
41
42 ///////////////////////////////////////////////////////////////////////////////////////////////////////
43 //Functions for Align<TSource,TSpec>
44 //project onto other sequence
45 template<typename TSource,typename TSpec,typename TValue, typename TId1, typename TPos1, typename TId2, typename TPos2,typename TMap>
46 void
_getOtherSequenceAndProject(Align<TSource,TSpec> & segment,TValue seg_num,TMap & seq_map,TId1,TPos1 node_i,TId2 & seq_j_id,TPos2 & node_j)47 _getOtherSequenceAndProject(Align<TSource,TSpec> & segment,
48 TValue seg_num,
49 TMap & seq_map,
50 TId1 ,
51 TPos1 node_i,
52 TId2 & seq_j_id,
53 TPos2 & node_j)
54 {
55
56 if(seg_num == 0)
57 {
58 seq_j_id = seq_map[getObjectId(source(row(segment, 1)))];
59 TPos1 view_clipped_end_pos = clippedEndPosition(row(segment,0)) - clippedBeginPosition(row(segment, 0));
60 if(node_i >= (TPos1)toSourcePosition(row(segment, 0), view_clipped_end_pos))
61 node_j = static_cast<TPos2>(-1);
62 else
63 node_j = toSourcePosition(row(segment, 1), toViewPosition(row(segment, 0), node_i));
64 }
65 else
66 {
67 seq_j_id = seq_map[getObjectId(source(row(segment, 0)))];
68 TPos1 view_clipped_end_pos = clippedEndPosition(row(segment, 1)) - clippedBeginPosition(row(segment, 1));
69 if(node_i >= (TPos1)toSourcePosition(row(segment, 1), view_clipped_end_pos))
70 node_j = static_cast<TPos2>(-1);
71 else
72 node_j = toSourcePosition(row(segment, 0), toViewPosition(row(segment, 1), node_i));
73 }
74 }
75
76
77 //unspektakul�re funktion, die die int ID zur�ckgibt (braucht man damit es f�r alle alignment typen geht)
78 //template<typename TSource,typename TSpec, typename TValue, typename TSeqMap>
79 //int
80 //_getSeqMapId(TSeqMap & seq_map,
81 // Align<TSource,TSpec> & segment,
82 // TValue seq_i)
83 //{
84 // return seq_map[getObjectId(source(row(segment,seq_i)))];
85 //}
86 //
87 //given seq and segment, get the sequenceId (seq_i) and its begin and end
88 //if seq = 0 get first sequence (that takes part in the segment match)
89 //if seq = 1 get second sequence
90 template<typename TAliSource,typename TAliSpec, typename TId, typename TPosition, typename TId2>
91 void
_getSeqBeginAndEnd(Align<TAliSource,TAliSpec> & segment,std::map<const void *,int> & seq_map,TId & seq_i_id,TPosition & begin_i,TPosition & end_i,TId2 seq)92 _getSeqBeginAndEnd(Align<TAliSource,TAliSpec> & segment,
93 std::map<const void * ,int> & seq_map,
94 TId & seq_i_id,
95 TPosition & begin_i,
96 TPosition & end_i,
97 TId2 seq)
98 {
99 seq_i_id = seq_map[getObjectId(source(row(segment,seq)))];
100 begin_i = clippedBeginPosition(row(segment, seq));
101 end_i = toSourcePosition(row(segment, seq), clippedEndPosition(row(segment, seq)) - begin_i);
102 }
103
104
105
106 ////////////////////////////////////////////////////////////////////////////////////////
107 // 50000 _getRefinedMatchScore Functions
108 ////////////////////////////////////////////////////////////////////////////////////////
109 ////////////////////////////////
110
111 //TODO(singer): Remove this forward
112 template <typename T>
113 struct Row;
114
115 //for Align<TAliSource,TAliSpec>
116 //get score for alignment of length len starting at pos_i on first sequence
117 //and pos_j on second sequence
118 template<typename TScoreValue,typename TScoreSpec,typename TStringSet,typename TAliSource,typename TAliSpec,typename TValue>
119 TScoreValue
_getRefinedMatchScore(Score<TScoreValue,TScoreSpec> & score_type,TStringSet &,Align<TAliSource,TAliSpec> & segment,TValue pos_i,TValue pos_j,TValue len,TValue)120 _getRefinedMatchScore(Score<TScoreValue,TScoreSpec> & score_type,
121 TStringSet &,
122 Align<TAliSource,TAliSpec> & segment,
123 TValue pos_i,
124 TValue pos_j,
125 TValue len,
126 TValue)
127 {
128 typedef Align<TAliSource,TAliSpec> TAlign;
129 typedef typename Row<TAlign>::Type TRow;
130 // typedef typename Iterator<TRow,GapsIterator<ArrayGaps> >::Type TIterator;
131 typedef typename Iterator<TRow, Rooted>::Type TIterator;
132 TIterator row0_it, row1_it;
133 row0_it = iter(row(segment,0),toViewPosition(row(segment,0),pos_i));
134 row1_it = iter(row(segment,1),toViewPosition(row(segment,1),pos_j));
135 len = toViewPosition(row(segment,0),pos_i + len) - toViewPosition(row(segment,0),pos_i);
136 TValue i = 0;
137 TScoreValue ret_score = 0;
138 while(i < len)
139 {
140 if(isGap(row1_it)||isGap(row0_it))
141 ret_score += scoreGapExtend(score_type);
142 else
143 ret_score += score(score_type,getValue(row0_it),getValue(row1_it));
144 ++i;
145 ++row0_it;
146 ++row1_it;
147 }
148 return ret_score;
149 }
150
151
152 //get score for alignment starting at pos_i on first sequence
153 //and pos_j on second sequence, if len1!=len2 then the refinement
154 //process was stopped (the cut is not exact)
155 //template<typename TScore,typename TStringSet, typename TAliSource,typename TAliSpec,typename TValue>
156 //typename Value<TScore>::Type
157 //_getRefinedMatchScore(TScore & score_type,
158 // TStringSet &,
159 // Align<TAliSource,TAliSpec> & segment,
160 // TValue pos_i,
161 // TValue pos_j,
162 // TValue len1,
163 // TValue len2)
164 //{
165 // typedef Align<TAliSource,TAliSpec> TAlign;
166 // typedef typename Row<TAlign>::Type TRow;
167 // typedef typename Iterator<TRow>::Type TIterator;
168 // TIterator row0_it, row1_it;
169 // TValue len;
170 // row0_it = iter(row(segment,0),toViewPosition(row(segment,0),pos_i));
171 // row1_it = iter(row(segment,1),toViewPosition(row(segment,1),pos_j));
172 // len1 = toViewPosition(row(segment,0),pos_i + len1) - toViewPosition(row(segment,0),pos_i);
173 // len2 = toViewPosition(row(segment,1),pos_j + len2) - toViewPosition(row(segment,1),pos_j);
174 // len = (len1 < len2) ? len1 : len2;
175 // int i = 0;
176 // typename Value<TScore>::Type ret_score = 0;
177 //
178 // //calculate score for aligned region
179 // while(i < len)
180 // {
181 // ret_score += score(score_type,getValue(row0_it),getValue(row1_it));
182 // ++i;
183 // ++row0_it;
184 // ++row1_it;
185 // }
186 // //fill up with gaps if one sequence is longer than the other
187 // len = (len1 > len2) ? len1 : len2;
188 // ret_score += (len - i) * scoreGapExtend(score_type);
189 //
190 // return ret_score;
191 //}
192
193 } // namespace seqan
194
195 #endif // #ifndef SEQAN_INCLUDE_SEQAN_GRAPH_ALGORITHM_REFINE_ALIGN_H_
196