Debian Codegraph @

src/align.h

raw symbols SCIP
1 #ifndef ALIGN_H
2 #define ALIGN_H
3
4 #include <boost/multi_array.hpp>
5
6 #include <iostream>
7
8 namespace torali
9 {
10
11 template<typename TScoreValue>
12 struct DnaScore {
13 typedef TScoreValue TValue;
14
15 TScoreValue match;
16 TScoreValue mismatch;
17 TScoreValue go;
18 TScoreValue ge;
19 TScoreValue inf;
20
21 DnaScore() : match(5), mismatch(-4), go(-10), ge(-1), inf(1000000) {}
22
23 DnaScore(TScoreValue m, TScoreValue mm, TScoreValue gapopen, TScoreValue gapextension) : match(m), mismatch(mm), go(gapopen), ge(gapextension), inf(1000000) {}
24
25 };
26
27
28
29 // Configure the DP matrix
30 template<bool THorizontal = false, bool TVertical = false>
31 class AlignConfig;
32
33 template<>
34 class AlignConfig<false, false> {};
35
36 template<>
37 class AlignConfig<false, true> {};
38
39 template<>
40 class AlignConfig<true, false> {};
41
42 template<>
43 class AlignConfig<true, true> {};
44
45 template<bool THorizontal, typename TPos1, typename TPos2, typename TCost>
46 inline TCost
47 _verticalGap(AlignConfig<THorizontal, false> const&, TPos1 const, TPos2 const, TCost const cost)
48 {
49 return cost;
50 }
51
52 template<bool THorizontal, typename TPos1, typename TPos2, typename TCost>
53 inline TCost
54 _verticalGap(AlignConfig<THorizontal, true> const&, TPos1 const i, TPos2 const iend, TCost const cost)
55 {
56 if ((i == (TPos1) 0) || (i == (TPos1) iend)) return 0;
57 else return cost;
58 }
59
60 template<bool TVertical, typename TPos1, typename TPos2, typename TCost>
61 inline TCost
62 _horizontalGap(AlignConfig<false, TVertical> const&, TPos1 const, TPos2 const, TCost const cost)
63 {
64 return cost;
65 }
66
67 template<bool TVertical, typename TPos1, typename TPos2, typename TCost>
68 inline TCost
69 _horizontalGap(AlignConfig<true, TVertical> const&, TPos1 const i, TPos2 const iend, TCost const cost)
70 {
71 if ((i == (TPos1) 0) || (i == (TPos1) iend)) return 0;
72 else return cost;
73 }
74
75 template<typename TChar, typename TDimension>
76 inline std::size_t
77 _size(boost::multi_array<TChar, 2> const& a, TDimension const i) {
78 return a.shape()[i];
79 }
80
81 template<typename TDimension>
82 inline std::size_t
83 _size(std::string const& s, TDimension const i) {
84 if (i) return s.size();
85 return 1;
86 }
87
88
89 template<typename TProfile, typename TAIndex, typename TScore>
90 inline int
91 _score(std::string const& s1, std::string const& s2, TProfile const&, TProfile const&, TAIndex row, TAIndex col, TScore const& sc)
92 {
93 return (s1[row] == s2[col] ? sc.match : sc.mismatch );
94 }
95
96 template<typename TChar, typename TProfile, typename TAIndex, typename TScore>
97 inline int
98 _score(boost::multi_array<TChar, 2> const& a1, boost::multi_array<TChar, 2> const& a2, TProfile const& p1, TProfile const& p2, TAIndex row, TAIndex col, TScore const& sc)
99 {
100 if ((a1.shape()[0] == 1) && (a2.shape()[0] == 1)) {
101 if (a1[0][row] == a2[0][col]) return sc.match;
102 else return sc.mismatch;
103 } else {
104 typedef typename TProfile::index TPIndex;
105 float score = 0;
106 for(TPIndex k1 = 0; k1<5; ++k1)
107 for(TPIndex k2 = 0; k2<5; ++k2)
108 score += p1[k1][row] * p2[k2][col] * ( (k1 == k2) ? sc.match : sc.mismatch );
109 return ((int) score);
110 }
111 }
112
113
114 template<typename TProfile>
115 inline void
116 _createProfile(std::string const& s, TProfile& p)
117 {
118 typedef typename TProfile::index TPIndex;
119 p.resize(boost::extents[6][s.size()]); // 'A', 'C', 'G', 'T', 'N', '-'
120 for (std::size_t j = 0; j < s.size(); ++j) {
121 for(TPIndex k = 0; k < 6; ++k) p[k][j] = 0;
122 if ((s[j] == 'A') || (s[j] == 'a')) p[0][j] += 1;
123 else if ((s[j] == 'C') || (s[j] == 'c')) p[1][j] += 1;
124 else if ((s[j] == 'G') || (s[j] == 'g')) p[2][j] += 1;
125 else if ((s[j] == 'T') || (s[j] == 't')) p[3][j] += 1;
126 else if ((s[j] == 'N') || (s[j] == 'n')) p[4][j] += 1;
127 else if (s[j] == '-') p[5][j] += 1;
128 }
129 }
130
131 template<typename TProfile>
132 inline void
133 _createProfile(boost::multi_array<char, 2> const& a, TProfile& p)
134 {
135 typedef typename boost::multi_array<char, 2>::index TAIndex;
136 typedef typename TProfile::index TPIndex;
137 p.resize(boost::extents[6][a.shape()[1]]); // 'A', 'C', 'G', 'T', 'N', '-'
138
139 // Ignore leading and trailing gaps
140 std::vector<int32_t> firstAlignedNuc(a.shape()[0], -1);
141 std::vector<int32_t> lastAlignedNuc(a.shape()[0], a.shape()[1]);
142 for(TAIndex i = 0; i < (TAIndex) a.shape()[0]; ++i) {
143 for (TAIndex j = 0; j < (TAIndex) a.shape()[1]; ++j) {
144 if (firstAlignedNuc[i] == -1) {
145 if (a[i][j] != '-') firstAlignedNuc[i] = j;
146 }
147 if (firstAlignedNuc[i] != -1) {
148 if (a[i][j] != '-') lastAlignedNuc[i] = j;
149 }
150 }
151 }
152
153 // Compute alignment profile
154 for (TAIndex j = 0; j < (TAIndex) a.shape()[1]; ++j) {
155 for(TPIndex k = 0; k < 6; ++k) p[k][j] = 0;
156 int sum = 0;
157 for(TAIndex i = 0; i < (TAIndex) a.shape()[0]; ++i) {
158 if ((firstAlignedNuc[i] <= j) && (j <= lastAlignedNuc[i])) {
159 ++sum;
160 if ((a[i][j] == 'A') || (a[i][j] == 'a')) p[0][j] += 1;
161 else if ((a[i][j] == 'C') || (a[i][j] == 'c')) p[1][j] += 1;
162 else if ((a[i][j] == 'G') || (a[i][j] == 'g')) p[2][j] += 1;
163 else if ((a[i][j] == 'T') || (a[i][j] == 't')) p[3][j] += 1;
164 else if ((a[i][j] == 'N') || (a[i][j] == 'n')) p[4][j] += 1;
165 else if (a[i][j] == '-') p[5][j] += 1;
166 else --sum;
167 }
168 }
169 for(TPIndex k = 0; k<6; ++k) p[k][j] /= sum;
170 }
171 }
172
173 template<typename TTrace, typename TAlign>
174 inline void
175 _createLocalAlignment(TTrace const& trace, std::string const& s1, std::string const& s2, TAlign& align, int32_t const maxRow, int32_t const maxCol)
176 {
177 align.resize(boost::extents[2][trace.size()]);
178 std::size_t row = maxRow;
179 std::size_t col = maxCol;
180 std::size_t ai = 0;
181 for(typename TTrace::const_reverse_iterator itT = trace.rbegin(); itT != trace.rend(); ++itT, ++ai) {
182 if (*itT == 's') {
183 align[0][ai] = s1[row++];
184 align[1][ai] = s2[col++];
185 } else if (*itT =='h') {
186 align[0][ai] = '-';
187 align[1][ai] = s2[col++];
188 } else {
189 align[0][ai] = s1[row++];
190 align[1][ai] = '-';
191 }
192 }
193 }
194
195 template<typename TTrace, typename TAlign>
196 inline void
197 _createAlignment(TTrace const& trace, std::string const& s1, std::string const& s2, TAlign& align)
198 {
199 _createLocalAlignment(trace, s1, s2, align, 0, 0);
200 }
201
202 template<typename TTrace, typename TChar, typename TAlign>
203 inline void
204 _createAlignment(TTrace const& trace, boost::multi_array<TChar, 2> const& a1, boost::multi_array<TChar, 2> const& a2, TAlign& align)
205 {
206 typedef typename TAlign::index TAIndex;
207 TAIndex numN = a1.shape()[0];
208 TAIndex numM = a2.shape()[0];
209 align.resize(boost::extents[numN + numM][trace.size()]);
210 TAIndex row = 0;
211 TAIndex col = 0;
212 TAIndex ai = 0;
213 for(typename TTrace::const_reverse_iterator itT = trace.rbegin(); itT != trace.rend(); ++itT, ++ai) {
214 if (*itT == 's') {
215 for(TAIndex i = 0; i<numN; ++i) align[i][ai] = a1[i][row];
216 for(TAIndex i = 0; i<numM; ++i) align[numN + i][ai] = a2[i][col];
217 ++row;
218 ++col;
219 } else if (*itT =='h') {
220 for(TAIndex i = 0; i<numN; ++i) align[i][ai] = '-';
221 for(TAIndex i = 0; i<numM; ++i) align[numN + i][ai] = a2[i][col];
222 ++col;
223 } else {
224 for(TAIndex i = 0; i<numN; ++i) align[i][ai] = a1[i][row];
225 for(TAIndex i = 0; i<numM; ++i) align[numN + i][ai] = '-';
226 ++row;
227 }
228 }
229 }
230
231 template<typename TConfig>
232 inline void
233 _alignmentScore(TConfig& c, std::string const& scoring) {
234 typedef boost::tokenizer< boost::char_separator<char> > Tokenizer;
235 boost::char_separator<char> sep(",\t ");
236 Tokenizer tokens(scoring, sep);
237 Tokenizer::iterator tokIter = tokens.begin();
238 int32_t match = boost::lexical_cast<int32_t>(*tokIter++);
239 int32_t mismatch = boost::lexical_cast<int32_t>(*tokIter++);
240 int32_t go = boost::lexical_cast<int32_t>(*tokIter++);
241 int32_t ge = boost::lexical_cast<int32_t>(*tokIter++);
242 c.aliscore = DnaScore<int>(match, mismatch, go, ge);
243 //std::cerr << c.aliscore.match << ',' << c.aliscore.mismatch << ',' << c.aliscore.go << ',' << c.aliscore.ge << std::endl;
244 return;
245 }
246
247 }
248
249 #endif