1 | /* |
---|
2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
---|
3 | |
---|
4 | This file is part of SINA. |
---|
5 | SINA is free software: you can redistribute it and/or modify it under |
---|
6 | the terms of the GNU General Public License as published by the Free |
---|
7 | Software Foundation, either version 3 of the License, or (at your |
---|
8 | option) any later version. |
---|
9 | |
---|
10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
13 | for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU General Public License |
---|
16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | Additional permission under GNU GPL version 3 section 7 |
---|
19 | |
---|
20 | If you modify SINA, or any covered work, by linking or combining it |
---|
21 | with components of ARB (or a modified version of that software), |
---|
22 | containing parts covered by the terms of the |
---|
23 | ARB-public-library-license, the licensors of SINA grant you additional |
---|
24 | permission to convey the resulting work. Corresponding Source for a |
---|
25 | non-source form of such a combination shall include the source code |
---|
26 | for the parts of ARB used as well as that of the covered work. |
---|
27 | */ |
---|
28 | |
---|
29 | // Copyright 2011 Elmar Pruesse |
---|
30 | |
---|
31 | #ifndef _PSEQ_H_ |
---|
32 | #define _PSEQ_H_ |
---|
33 | |
---|
34 | #include "cseq.h" |
---|
35 | #include "aligned_base.h" |
---|
36 | |
---|
37 | #include <vector> |
---|
38 | #include <sstream> |
---|
39 | |
---|
40 | namespace sina { |
---|
41 | |
---|
42 | class base_profile { |
---|
43 | public: |
---|
44 | enum base_types { |
---|
45 | BASE_A=0, |
---|
46 | BASE_G=1, |
---|
47 | BASE_C=2, |
---|
48 | BASE_TU=3, |
---|
49 | BASE_MAX=4, |
---|
50 | }; |
---|
51 | |
---|
52 | base_profile(); // no default |
---|
53 | |
---|
54 | base_profile(int a, int g, int c, int t, |
---|
55 | int open, int extend) { |
---|
56 | int sum = a + g + c + t + open + extend; |
---|
57 | bases[BASE_A] = (float)a/sum; |
---|
58 | bases[BASE_G] = (float)g/sum; |
---|
59 | bases[BASE_C] = (float)c/sum; |
---|
60 | bases[BASE_TU]= (float)t/sum; |
---|
61 | gapOpen = (float)open/sum; |
---|
62 | gapExtend = (float)extend/sum; |
---|
63 | } |
---|
64 | |
---|
65 | base_profile(const base_iupac& b) |
---|
66 | : gapOpen(0), gapExtend(0) |
---|
67 | { |
---|
68 | if (b.ambig_order() > 0) { |
---|
69 | float val = 1.f / b.ambig_order(); |
---|
70 | for (float & base : bases) { |
---|
71 | base=0.f; |
---|
72 | } |
---|
73 | if (b.has_A()) { |
---|
74 | bases[BASE_A] = val; |
---|
75 | } |
---|
76 | if (b.has_G()) { |
---|
77 | bases[BASE_G] = val; |
---|
78 | } |
---|
79 | if (b.has_C()) { |
---|
80 | bases[BASE_C] = val; |
---|
81 | } |
---|
82 | if (b.has_TU()) { |
---|
83 | bases[BASE_TU] = val; |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | void complement() { |
---|
89 | std::swap(bases[BASE_A],bases[BASE_TU]); |
---|
90 | std::swap(bases[BASE_G],bases[BASE_C]); |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | float getA() const { return bases[BASE_A]; } |
---|
95 | float getG() const { return bases[BASE_G]; } |
---|
96 | float getC() const { return bases[BASE_C]; } |
---|
97 | float getT() const { return bases[BASE_TU]; } |
---|
98 | */ |
---|
99 | |
---|
100 | float comp(const base_profile& rhs, float match, float mismatch, |
---|
101 | float gap, float gap_ext) const { |
---|
102 | float res = 0; |
---|
103 | for (int i=0; i<BASE_MAX; i++) { |
---|
104 | for (int j=0; j<BASE_MAX; j++) { |
---|
105 | if (i==j) { |
---|
106 | res += match * bases[i] * rhs.bases[j]; |
---|
107 | } else { |
---|
108 | res += mismatch * bases[i] * rhs.bases[j]; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | return res + gap * gapOpen + gap_ext * gapExtend; |
---|
113 | } |
---|
114 | |
---|
115 | float comp(const base_iupac& base, float match, float mismatch, |
---|
116 | float gap, float gap_ext) const { |
---|
117 | return comp(base_profile(base), match, mismatch, |
---|
118 | gap, gap_ext); |
---|
119 | } |
---|
120 | |
---|
121 | std::string getString() { |
---|
122 | std::stringstream out; |
---|
123 | const char l[4] = {'A','G','C','T'}; |
---|
124 | for (int i=0; i<BASE_MAX; i++) { |
---|
125 | out << l[i] << bases[i] << ":"; |
---|
126 | } |
---|
127 | |
---|
128 | return out.str(); |
---|
129 | } |
---|
130 | private: |
---|
131 | float bases[4]; |
---|
132 | float gapOpen; |
---|
133 | float gapExtend; |
---|
134 | }; |
---|
135 | |
---|
136 | using aligned_base_profile = aligned<base_profile>; |
---|
137 | |
---|
138 | class pseq { |
---|
139 | public: |
---|
140 | using idx_type = unsigned int; |
---|
141 | class iterator; |
---|
142 | class const_iterator; |
---|
143 | using pn_iterator = iterator; |
---|
144 | using value_type = aligned_base_profile; |
---|
145 | |
---|
146 | pseq(std::vector<const cseq*>::iterator seqs_it, std::vector<const cseq*>::iterator seqs_end); |
---|
147 | |
---|
148 | idx_type size() const { return profile.size(); } |
---|
149 | idx_type getWidth() const { return width; } |
---|
150 | iterator begin(); |
---|
151 | const_iterator begin() const; |
---|
152 | iterator end(); |
---|
153 | const_iterator end() const; |
---|
154 | pn_iterator pn_first_begin(); |
---|
155 | pn_iterator pn_first_end(); |
---|
156 | pn_iterator pn_last_begin(); |
---|
157 | pn_iterator pn_last_end(); |
---|
158 | const aligned_base_profile& getById(idx_type i) const {return profile[i];} |
---|
159 | void sort() {} |
---|
160 | |
---|
161 | void print_graphviz(std::ostream& out, const std::string& name); |
---|
162 | |
---|
163 | private: |
---|
164 | idx_type width; |
---|
165 | std::vector<aligned_base_profile> profile; |
---|
166 | std::vector<idx_type> positions; |
---|
167 | |
---|
168 | friend pseq::idx_type get_node_id(const pseq& p, const pseq::iterator& i); |
---|
169 | }; |
---|
170 | |
---|
171 | class pseq::iterator |
---|
172 | : public std::vector<aligned_base_profile>::iterator |
---|
173 | { |
---|
174 | public: |
---|
175 | iterator(std::vector<aligned_base_profile>::iterator it) |
---|
176 | : std::vector<aligned_base_profile>::iterator(it) {} |
---|
177 | iterator() = default; |
---|
178 | |
---|
179 | using pn_iterator = iterator; |
---|
180 | iterator prev_begin() const { iterator n(*this); return --n; } |
---|
181 | iterator prev_end() const { return (*this); } |
---|
182 | iterator next_begin() const { iterator n(*this); return ++n; } |
---|
183 | iterator next_end() const { iterator n(*this); return n+2; } |
---|
184 | }; |
---|
185 | |
---|
186 | class pseq::const_iterator |
---|
187 | : public std::vector<aligned_base_profile>::const_iterator |
---|
188 | { |
---|
189 | public: |
---|
190 | const_iterator(std::vector<aligned_base_profile>::const_iterator it) |
---|
191 | : std::vector<aligned_base_profile>::const_iterator(it) {} |
---|
192 | const_iterator() = default; |
---|
193 | |
---|
194 | using const_pn_iterator = const_iterator; |
---|
195 | const_iterator prev_begin() const { const_iterator n(*this); return --n; } |
---|
196 | const_iterator prev_end() const { return (*this); } |
---|
197 | const_iterator next_begin() const { const_iterator n(*this); return ++n; } |
---|
198 | const_iterator next_end() const { const_iterator n(*this); return n+2; } |
---|
199 | }; |
---|
200 | |
---|
201 | inline pseq::idx_type |
---|
202 | get_node_id(pseq& p, const pseq::iterator& i) { |
---|
203 | return i - p.begin(); |
---|
204 | } |
---|
205 | |
---|
206 | inline pseq::iterator |
---|
207 | pseq::begin() { |
---|
208 | return {profile.begin()}; |
---|
209 | } |
---|
210 | |
---|
211 | inline pseq::iterator |
---|
212 | pseq::end() { |
---|
213 | return {profile.end()}; |
---|
214 | } |
---|
215 | |
---|
216 | inline pseq::const_iterator |
---|
217 | pseq::begin() const { |
---|
218 | return {profile.begin()}; |
---|
219 | } |
---|
220 | |
---|
221 | inline pseq::const_iterator |
---|
222 | pseq::end() const { |
---|
223 | return {profile.end()}; |
---|
224 | } |
---|
225 | |
---|
226 | inline pseq::pn_iterator |
---|
227 | pseq::pn_first_begin() { |
---|
228 | return begin(); |
---|
229 | } |
---|
230 | inline pseq::pn_iterator |
---|
231 | pseq::pn_first_end() { |
---|
232 | return begin()+1; |
---|
233 | } |
---|
234 | inline pseq::pn_iterator |
---|
235 | pseq::pn_last_begin() { |
---|
236 | return end()-1; |
---|
237 | } |
---|
238 | inline pseq::pn_iterator |
---|
239 | pseq::pn_last_end() { |
---|
240 | return end(); |
---|
241 | } |
---|
242 | |
---|
243 | inline pseq::iterator |
---|
244 | prev_begin(const pseq& p, const pseq::iterator& it) { |
---|
245 | if (p.begin() != it) { |
---|
246 | return it-1; |
---|
247 | } |
---|
248 | return it; |
---|
249 | } |
---|
250 | inline pseq::iterator |
---|
251 | prev_end(const pseq& /*c*/, const pseq::iterator& it) { |
---|
252 | return it; |
---|
253 | } |
---|
254 | inline pseq::iterator |
---|
255 | next_begin(const pseq& /*c*/, const pseq::iterator& it) { |
---|
256 | return it+1; |
---|
257 | } |
---|
258 | inline pseq::iterator |
---|
259 | next_end(const pseq& p, const pseq::iterator& it) { |
---|
260 | if (it+1 == p.end()) { |
---|
261 | return it + 1; |
---|
262 | } |
---|
263 | return it + 2; |
---|
264 | } |
---|
265 | |
---|
266 | std::ostream& operator<<(std::ostream& out, const sina::aligned_base_profile& ab); |
---|
267 | } // namespace sina |
---|
268 | |
---|
269 | |
---|
270 | |
---|
271 | #endif // _PSEQ_H_ |
---|
272 | |
---|
273 | /* |
---|
274 | Local Variables: |
---|
275 | mode:c++ |
---|
276 | c-file-style:"stroustrup" |
---|
277 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) |
---|
278 | indent-tabs-mode:nil |
---|
279 | fill-column:99 |
---|
280 | End: |
---|
281 | */ |
---|
282 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
---|
283 | |
---|