Debian Codegraph @

scoring-matrices/CScoreMatrix.h

raw
1 /* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits
2 * Copyright 2013-2016 by Christoph Mayer
3 *
4 * This source file is part of the BaitFisher-package.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 * For any enquiries send an Email to Christoph Mayer
21 * [email protected]
22 *
23 * When publishing work that is based on the results please cite:
24 * Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
25 *
26 */
27 #ifndef CSCOREMATRIX_H
28
29 #define CSCOREMATRIX_H
30
31
32 #include <iostream>
33 #include <fstream>
34 #include "../faststring2.h"
35 #include <climits>
36 #include <sstream>
37
38 class CScoreMatrix
39 {
40 private:
41 faststring alphabet;
42 unsigned char alphabet_to_index[256];
43
44 unsigned char num_sym;
45
46 int gap_open;
47 int gap_ext;
48
49
50 short **score_mat;
51
52 char type;
53
54
55 public:
56 /* int alphabet_index(char base) */
57 /* { */
58 /* return 0; */
59 /* } */
60
61 CScoreMatrix (const char *filename)
62 {
63 faststring line;
64 std::ifstream ifs;
65
66 ifs.open(filename);
67
68 if (!ifs.fail())
69 {
70 read_from_file(ifs);
71 ifs.close();
72 }
73 else
74 {
75 std::cerr << "INFORMATION: Could not find the file with alignment parameters: " << filename << std::endl;
76 std::cerr << "The following default values will be assumed as alignment parameters:" << std::endl;
77
78 std::stringstream stringst;
79 stringst << ";D standard DNA scoring matrix\n 1 45 80 5 6 80 4\n -0 -3\n * @ 0 1 2 \n ACGTRYMWSKDHVBN\n 0 1 2 3 0 1 0 0 1 2 0 0 0 1 0\n 1\n -2 1\n -2 -2 1\n -2 -2 -2 1\n 1 -2 1 -2 1\n -2 1 -2 1 -2 1 \n 1 1 -2 -2 -2 -2 1\n 1 -2 -2 1 -2 -2 -2 1\n -2 1 1 -2 -2 -2 -2 -2 1\n -2 -2 1 1 -2 -2 -2 -2 -2 1\n 1 -2 1 1 1 -2 -2 1 -2 1 1\n 1 1 -2 1 -2 1 1 1 -2 -2 -2 1\n 1 1 1 -2 1 -2 1 -2 1 -2 -2 -2 1\n -2 1 1 1 -2 1 -2 -2 1 1 -2 -2 -2 1\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n";
80 // stringst << ";D standard DNA scoring matrix\n 1 45 80 5 6 80 4\n -12 -4\n * @ 0 1 2 \n ACGTRYMWSKDHVBN\n 0 1 2 3 0 1 0 0 1 2 0 0 0 1 0\n 4\n -3 4\n -3 -3 4\n -3 -3 -3 4\n 1 -1 1 -1 1\n -1 1 -1 1 -3 1\n 1 1 -2 -2 0 0 1\n 1 -2 -2 1 0 0 0 1\n -2 1 1 -2 0 0 0 0 1\n -2 -2 1 1 0 0 0 0 0 1\n 1 -2 1 1 1 0 0 1 0 1 1\n 1 1 -2 1 0 1 1 1 0 0 0 1\n 1 1 1 -2 1 0 1 0 1 0 0 0 1\n -2 1 1 1 0 1 0 0 1 1 0 0 0 1\n 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1\n";
81 read_from_file(stringst);
82
83 std::ios_base::fmtflags f = std::cerr.flags();
84 std::cerr.flags(std::ios::right);
85 std::cerr << "Gap openening penalty: "; std::cerr.width(3); std::cerr << gap_open << std::endl;
86 std::cerr << "Gap extension penalty: "; std::cerr.width(3); std::cerr << gap_ext << std::endl;
87 std::cerr << "Match score: "; std::cerr.width(3); std::cerr << score_compare_symbols('A', 'A') << std::endl;
88 std::cerr << "Mismatch score: "; std::cerr.width(3); std::cerr << score_compare_symbols('A', 'C') << std::endl;
89 std::cerr << "Score against N: "; std::cerr.width(3); std::cerr << score_compare_symbols('A', 'N') << std::endl;
90 std::cerr << "Scores against other ambigs: match or mismatch score" << std::endl;
91 std::cerr.setf(f);
92 }
93 }
94
95 ~CScoreMatrix()
96 {
97 delete [] score_mat;
98 }
99
100
101 int get_Gap_open_score()
102 {
103 return gap_open;
104 }
105
106 int get_Gap_ext_score()
107 {
108 return gap_ext;
109 }
110
111 double get_mean_matchscore_of_first_N_symbols(unsigned N)
112 {
113 unsigned i;
114 double sum=0;
115 for (i=0; i<N; ++i)
116 {
117 sum += score_mat[i][i];
118 }
119 return sum/N;
120 }
121
122 char get_Type()
123 {
124 return type;
125 }
126
127 int get_NumbrOfSymbols()
128 {
129 return num_sym;
130 }
131
132 // Expects an existing file that is openend and reading pointer
133 // pointing to the beginning of the file.
134 void read_from_file(std::istream &is)
135 {
136 faststring line;
137
138 // First line
139 getline(is, line);
140 if (line[0] != ';')
141 {
142 // Error - must be a ';'
143 }
144 if (toupper(line[1]) == 'P') // protein
145 {
146 type = 'P';
147 }
148 else if (toupper(line[1]) == 'D') // DNA
149 {
150 type = 'D';
151 }
152 else
153 {
154 // Error, must be either 'D' or 'P'
155
156 }
157
158 // Second line
159 getline(is, line); // Unused data
160
161 // Third line
162 is >> gap_open >> gap_ext;
163 getline(is, line); // Read newline of this line.
164
165 // Fourth line
166 getline(is, line); // Unused data
167
168 int i;
169
170 for (i=0; i<256; ++i)
171 alphabet_to_index[i] = UCHAR_MAX;
172
173
174 // Fifth line
175 unsigned char c = is.get();
176 num_sym = 0;
177 while (c!= '\n')
178 {
179 if (!isspace(c))
180 {
181 alphabet.push_back(c);
182 alphabet_to_index[c] = num_sym;
183 ++num_sym;
184 }
185 c = is.get();
186 }
187
188 // Sixth line:
189 getline(is, line); // Unused data - Hashkeys
190
191 // Now we read the matrix:
192 // Indices:
193 // First index: row
194 // Second index: col
195
196 score_mat = new short* [num_sym];
197
198 for (i=0; i< num_sym; ++i)
199 {
200 score_mat[i] = new short[num_sym];
201 }
202
203 int num_entries = num_sym*(num_sym+1)/2;
204
205 i = 0;
206 int ind1 = 0;
207 int ind2 = 0;
208
209 short entry;
210
211 while (i < num_entries)
212 {
213 is >> entry;
214
215 score_mat[ind2][ind1] = entry;
216 score_mat[ind1][ind2] = entry;
217
218 if (ind1 == ind2)
219 {
220 ++ind2;
221 ind1=0;
222 }
223 else
224 {
225 ++ind1;
226 }
227 ++i;
228 }
229
230 if (!is)
231 {
232 std::cerr << "Error while reading the score matrix containing the alignment parameters. The file/content seems to be damaged. Please revert to a working version of this file/content." << std::endl;
233 exit(10);
234 }
235 }
236
237 void debug_out(std::ostream &os)
238 {
239 os << "Data stored in scoring matrix:" << std::endl;
240 os << "Type: " << type << std::endl;
241 os << "number of symbols: " << (int) num_sym << std::endl;
242 os << "Alphabet: " << std::endl;
243
244 int i = 0;
245 while (i<num_sym)
246 {
247 os << i << " " << alphabet.get_unckecked(i) << " " << (int)alphabet_to_index[(unsigned char)alphabet.get_unckecked(i)] << std::endl;
248 ++i;
249 }
250 os << std::endl;
251 os << "gap open " << gap_open << " gap_ext " << gap_ext << std::endl;
252
253 int j;
254
255 for (i=0; i< num_sym; ++i) // rows
256 {
257 for(j=0; j<=i; ++j) // cols
258 {
259 os << score_mat[i][j] << " ";
260 }
261 os << std::endl;
262 }
263 }
264
265 void convert_sym_to_index(unsigned char *beg, unsigned char *end)
266 {
267 while (beg != end)
268 {
269 *beg = alphabet_to_index[*beg];
270 ++beg;
271 }
272 }
273
274 void convert_sym_to_index(unsigned char *beg)
275 {
276 while (*beg)
277 {
278 *beg = alphabet_to_index[*beg];
279 ++beg;
280 }
281 *beg = UCHAR_MAX; // UCHAR_MAX indicates the end of the string.
282 }
283
284 void convert_index_to_sym(unsigned char *beg, unsigned char*end)
285 {
286 while (beg != end)
287 {
288 *beg = alphabet.get_unckecked(*beg);
289 ++beg;
290 }
291 }
292
293 void convert_to_index_to_sym(unsigned char *beg)
294 {
295 while (*beg != UCHAR_MAX)
296 {
297 *beg = alphabet.get_unckecked(*beg);
298 ++beg;
299 }
300 *beg = 0; // Convert UCHAR_MAX to 0 to indicate the end of the string.
301 }
302
303
304
305 int score_compare_indizes(unsigned char i, unsigned char j)
306 {
307 return score_mat[j][i]; // From i to j, col index (i) to row index (j)
308 }
309
310 int score_compare_symbols(unsigned char a, unsigned char b)
311 {
312 return score_mat[alphabet_to_index[b]][alphabet_to_index[a]];
313 }
314
315
316
317 };
318
319
320 #endif