| 1 |
/* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits |
| 2 |
* BaitFilter (version 1.0.6) a program for selecting optimal bait regions |
| 3 |
* Copyright 2013-2017 by Christoph Mayer |
| 4 |
* |
| 5 |
* This source file is part of the BaitFisher-package. |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with BaitFisher. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
* |
| 20 |
* |
| 21 |
* For any enquiries send an Email to Christoph Mayer |
| 22 |
* [email protected] |
| 23 |
* |
| 24 |
* When publishing work that is based on the results please cite: |
| 25 |
* Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design |
| 26 |
* |
| 27 |
*/ |
| 28 |
#ifndef DNA_ROUTINES_H |
| 29 |
#define DNA_ROUTINES_H |
| 30 |
|
| 31 |
/* Todo: count DNA bases, guess data type, is AA, etc. */ |
| 32 |
|
| 33 |
// Changes 10/11.11.2016: |
| 34 |
// * Removed a bug: (c == 'O' && c == 'U') should read (c == 'O' || c == 'U'). |
| 35 |
// The impact of this error is low. Basically, we overlook O or U as not allowed amino acids. |
| 36 |
// Since they should not occur in normal amino acid sequences, it does not make much of a difference. |
| 37 |
// * added the blosum symbols, lists and functions |
| 38 |
// * added some upper lists and functions. |
| 39 |
// * added a look-up table for the blosum symbols. This is much more efficient. |
| 40 |
// * removed z from aa_symbols, aa_symbols_ext and _upper, since it as an ambiguity character. |
| 41 |
|
| 42 |
|
| 43 |
#include <cstring> |
| 44 |
#include <cctype> |
| 45 |
|
| 46 |
static const char DNARNA_symbols[] = "aAcCgGtTuU"; |
| 47 |
static const char DNARNA_symbols_upper[] = "ACGTU"; |
| 48 |
static const char aa_symbols[] = "aArRnNdDcCeEqQgGhHiIlLkKmMfFpPsStTwWyYvVxX"; |
| 49 |
static const char aa_symbols_upper[] = "ARNDCEQGHILKMFPSTWYVX"; |
| 50 |
|
| 51 |
// Extended also includes: O and U: |
| 52 |
static const char aa_symbols_ext[] = "aArRnNdDcCeEqQgGhHiIlLkKmMfFpPsStTwWyYvVxXuUoO*"; |
| 53 |
static const char aa_symbols_ext_upper [] = "ARNDCEQGHILKMFPSTWYVXUO*"; |
| 54 |
|
| 55 |
static const char non_aa_in_ABC [] = "BJOUZbjouz"; |
| 56 |
|
| 57 |
// AA ambiguity characters: J is a very rare ambiguity character also included in this set. |
| 58 |
// J is not allowed in many programs. |
| 59 |
static const char aa_ambig [] = "bBzZjJxX"; |
| 60 |
|
| 61 |
// Usually a safe list of allowed characters are those in the blosum matrix: |
| 62 |
// Lists the symbols that are allowed in the blosum matrix: Contains B,Z but not J as ambiguity character. |
| 63 |
// See also the look up tables: is_aa_blosum_code[] and is_aa_blosum_code_or_gap[] |
| 64 |
|
| 65 |
static const char aa_symbols_blosum[] = "aArRnNdDcCeEqQzZgGhHiIlLkKmMfFpPsStTwWyYvVbBxX*"; |
| 66 |
static const char aa_symbols_blosum_upper [] = "ARNDCEQZGHILKMFPSTWYVBX*"; |
| 67 |
|
| 68 |
static const char non_aa_symbols_blosum[] = "jJoOuU"; |
| 69 |
static const char non_aa_symbols_blosum_upper [] = "JOU"; |
| 70 |
|
| 71 |
static const char DNARNA_iupac_symbols [] = "aAcCgGtTuURYSWryswKMBDkmbdHVNhvn"; |
| 72 |
static const char phylip_DNARNA_allowed_symbols [] = "?-nNaAcCgGtTuURYSWryswKMBDkmbdHVhvoOxX"; |
| 73 |
|
| 74 |
static const char phylip_neither_aa_dna_symbol [] = "jJzZ"; |
| 75 |
static const char allowed_non_ABC [] = "-?*"; |
| 76 |
|
| 77 |
// O and U are special amino acids that are usually not in the models. |
| 78 |
// Chars that are not an aa symbol: B, J, O, U, Z |
| 79 |
// X is the amibuity code |
| 80 |
|
| 81 |
// Includes the symbols which occur in the blosum matrices: |
| 82 |
const int is_aa_blosum_code_lookup[] = { |
| 83 |
0,0,0,0,0,0,0,0,0,0, // 0- 9 |
| 84 |
0,0,0,0,0,0,0,0,0,0, // 10-19 |
| 85 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 86 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 87 |
0,0,1,0,0,0,0,0,0,0, // 40-49 45: - is not included, 42: * included |
| 88 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 89 |
0,0,0,0,0,1,1,1,1,1, // 60-69 63: ? is not included |
| 90 |
1,1,1,1,0,1,1,1,1,0, // 70-79 |
| 91 |
1,1,1,1,1,0,1,1,1,1, // 80-89 |
| 92 |
1,0,0,0,0,0,0,1,1,1, // 90-99 |
| 93 |
1,1,1,1,1,1,0,1,1,1, //100-109 |
| 94 |
1,0,1,1,1,1,1,0,1,1, //110-119 |
| 95 |
1,1,1,0,0,0,0,0 //120-127 |
| 96 |
}; |
| 97 |
|
| 98 |
// Includes the symbols which occur in the blosum matrices and the gap character: |
| 99 |
const int is_aa_blosum_code_or_gap_lookup[] = { |
| 100 |
0,0,0,0,0,0,0,0,0,0, // 0- 9 |
| 101 |
0,0,0,0,0,0,0,0,0,0, // 10-19 |
| 102 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 103 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 104 |
0,0,1,0,0,1,0,0,0,0, // 40-49 45: - is included, 42: * included |
| 105 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 106 |
0,0,0,0,0,1,1,1,1,1, // 60-69 63: ? is not included |
| 107 |
1,1,1,1,0,1,1,1,1,0, // 70-79 |
| 108 |
1,1,1,1,1,0,1,1,1,1, // 80-89 |
| 109 |
1,0,0,0,0,0,0,1,1,1, // 90-99 |
| 110 |
1,1,1,1,1,1,0,1,1,1, //100-109 |
| 111 |
1,0,1,1,1,1,1,0,1,1, //110-119 |
| 112 |
1,1,1,0,0,0,0,0 //120-127 |
| 113 |
}; |
| 114 |
|
| 115 |
// Inlcudes all DNA symbols, no ambiguity characters |
| 116 |
const char is_DNA_lookup[128] = { |
| 117 |
0,0,0,0,0,0,0,0,0,0, // 0- 9 |
| 118 |
0,0,0,0,0,0,0,0,0,0, // 10-19 |
| 119 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 120 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 121 |
0,0,0,0,0,0,0,0,0,0, // 40-49 |
| 122 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 123 |
0,0,0,0,0,1,0,1,0,0, // 60-69 63:'?', 65:'A' to 69:'E' |
| 124 |
0,1,0,0,0,0,0,0,0,0, // 70-79 |
| 125 |
0,0,0,0,1,0,0,0,0,0, // 80-89 |
| 126 |
0,0,0,0,0,0,0,1,0,1, // 90-99 |
| 127 |
0,0,0,1,0,0,0,0,0,0, //100-109 |
| 128 |
0,0,0,0,0,0,1,0,0,0, //110-119 |
| 129 |
0,0,0,0,0,0,0,0 //120-127 |
| 130 |
}; |
| 131 |
|
| 132 |
// Inlcudes all DNA symbols and gap '-', no ambiguity characters |
| 133 |
const char is_DNA_or_GAP_lookup[128] = { |
| 134 |
0,0,0,0,0,0,0,0,0,0, // 0- 9 |
| 135 |
0,0,0,0,0,0,0,0,0,0, // 10-19 |
| 136 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 137 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 138 |
0,0,0,0,0,1,0,0,0,0, // 40-49 |
| 139 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 140 |
0,0,0,0,0,1,0,1,0,0, // 60-69 63:'?', 65:'A' to 69:'E' |
| 141 |
0,1,0,0,0,0,0,0,0,0, // 70-79 |
| 142 |
0,0,0,0,1,0,0,0,0,0, // 80-89 |
| 143 |
0,0,0,0,0,0,0,1,0,1, // 90-99 |
| 144 |
0,0,0,1,0,0,0,0,0,0, //100-109 |
| 145 |
0,0,0,0,0,0,1,0,0,0, //110-119 |
| 146 |
0,0,0,0,0,0,0,0 //120-127 |
| 147 |
}; |
| 148 |
|
| 149 |
// Inlcudes all ambiguity characters as well as ? |
| 150 |
const char is_DNA_or_AMBIG_lookup[128] = { |
| 151 |
0,0,0,0,0,0,0,0,0,0, // 0- 9 |
| 152 |
0,0,0,0,0,0,0,0,0,0, // 10-19 |
| 153 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 154 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 155 |
0,0,0,0,0,0,0,0,0,0, // 40-49 |
| 156 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 157 |
0,0,0,1,0,1,1,1,1,0, // 60-69 63:'?', 65:'A' to 69:'E' |
| 158 |
0,1,1,0,0,1,0,1,1,0, // 70-79 |
| 159 |
0,0,1,1,1,0,1,1,0,1, // 80-89 |
| 160 |
0,0,0,0,0,0,0,1,1,1, // 90-99 |
| 161 |
1,0,0,1,1,0,0,0,0,0, //100-109 |
| 162 |
0,0,0,0,1,0,1,0,1,1, //110-119 |
| 163 |
0,1,0,0,0,0,0,0 //120-127 |
| 164 |
}; |
| 165 |
|
| 166 |
// Inlcudes all ambiguity characters as well as ? and - |
| 167 |
const char is_DNA_or_AMBIG_or_GAP_lookup[128] = { |
| 168 |
0,0,0,0,0,0,0,0,0,0, // 0- 9 |
| 169 |
0,0,0,0,0,0,0,0,0,0, // 10-19 |
| 170 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 171 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 172 |
0,0,0,0,0,1,0,0,0,0, // 40-49 45: - is included |
| 173 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 174 |
0,0,0,1,0,1,1,1,1,0, // 60-69 63:'?', 65:'A' to 69:'E' |
| 175 |
0,1,1,0,0,1,0,1,1,0, // 70-79 |
| 176 |
0,0,1,1,1,0,1,1,0,1, // 80-89 |
| 177 |
0,0,0,0,0,0,0,1,1,1, // 90-99 |
| 178 |
1,0,0,1,1,0,0,0,0,0, //100-109 |
| 179 |
0,0,0,0,1,0,1,0,1,1, //110-119 |
| 180 |
0,1,0,0,0,0,0,0 //120-127 |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
/* inline bool is_DNA_base_upper(char c) */ |
| 185 |
/* { */ |
| 186 |
/* if (c == 'A' || c == 'C' || c == 'G' || c == 'T' ) */ |
| 187 |
/* return true; */ |
| 188 |
/* return false; */ |
| 189 |
/* } */ |
| 190 |
|
| 191 |
/* inline bool is_DNA_base_lower(char c) */ |
| 192 |
/* { */ |
| 193 |
/* if (c == 'a' || c == 'c' || c == 'g' || c == 't' ) */ |
| 194 |
/* return true; */ |
| 195 |
/* return false; */ |
| 196 |
/* } */ |
| 197 |
|
| 198 |
// Only defined if c <= 127 |
| 199 |
inline bool is_DNA_base(char c) |
| 200 |
{ |
| 201 |
return is_DNA_lookup[(int)c]; |
| 202 |
} |
| 203 |
|
| 204 |
inline bool is_DNA_base_or_GAP(char c) |
| 205 |
{ |
| 206 |
return is_DNA_or_GAP_lookup[(int)c]; |
| 207 |
} |
| 208 |
|
| 209 |
inline bool is_DNA_or_DNA_ambig(char c) |
| 210 |
{ |
| 211 |
return is_DNA_or_AMBIG_lookup[(int)c]; |
| 212 |
} |
| 213 |
|
| 214 |
inline bool is_DNA_or_DNA_ambig_or_GAP(char c) |
| 215 |
{ |
| 216 |
return is_DNA_or_AMBIG_or_GAP_lookup[(int)c]; |
| 217 |
} |
| 218 |
|
| 219 |
inline bool is_DNA_or_RNA_base(char c) |
| 220 |
{ |
| 221 |
if (c == 'A' || c == 'C' || c == 'G' || c == 'T' || |
| 222 |
c == 'a' || c == 'c' || c == 'g' || c == 't' || c == 'U' || c == 'u') |
| 223 |
return true; |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
inline bool is_allowed_non_ABC(char c) |
| 228 |
{ |
| 229 |
if (c == '-' || c == '?' || c == '*') |
| 230 |
return true; |
| 231 |
else |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
inline bool is_phylip_aa_dna_symbol(char c) // Does not include -? or similar |
| 236 |
{ |
| 237 |
// Only one character is not allowed: |
| 238 |
if (c=='o' || c == 'O') |
| 239 |
return false; |
| 240 |
if (c >='a' && c <= 'z') |
| 241 |
{ |
| 242 |
return true; |
| 243 |
} |
| 244 |
else if (c >='A' && c <= 'Z') |
| 245 |
{ |
| 246 |
return true; |
| 247 |
} |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
inline bool is_gap(char c) |
| 252 |
{ |
| 253 |
return c == '-'; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
// Only defined if c <= 127 |
| 258 |
// Includes only DNA ambig symbols. Does not include DNA symbols. See also is_DNA_or_AMBIG below. |
| 259 |
inline bool is_DNA_ambig(char c) |
| 260 |
{ |
| 261 |
return is_DNA_or_AMBIG_lookup[(int)c] && !is_DNA_lookup[(int)c]; |
| 262 |
/* if (c == 'N' || c == '?' || c == 'n' || */ |
| 263 |
/* c == 'R' || c == 'Y' || c == 'S' || c == 'W' || */ |
| 264 |
/* c == 'r' || c == 'y' || c == 's' || c == 'w' || */ |
| 265 |
/* c == 'K' || c == 'M' || c == 'B' || c == 'D' || */ |
| 266 |
/* c == 'k' || c == 'm' || c == 'b' || c == 'd' || */ |
| 267 |
/* c == 'H' || c == 'V' || */ |
| 268 |
/* c == 'h' || c == 'v') */ |
| 269 |
/* return true; */ |
| 270 |
/* else */ |
| 271 |
/* return false; */ |
| 272 |
} |
| 273 |
|
| 274 |
/* inline bool is_DNA_iupac_ambig_upper(char c) */ |
| 275 |
/* { */ |
| 276 |
/* if (c == 'N' || c == '?' || */ |
| 277 |
/* c == 'R' || c == 'Y' || c == 'S' || c == 'W' || */ |
| 278 |
/* c == 'K' || c == 'M' || c == 'B' || c == 'D' || */ |
| 279 |
/* c == 'H' || c == 'V') */ |
| 280 |
/* return true; */ |
| 281 |
/* else */ |
| 282 |
/* return false; */ |
| 283 |
/* } */ |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
// A list can be found here: https://en.wikipedia.org/wiki/Amino_acid |
| 295 |
// The acceptance of J as an ambiguous amino acid symbol is low. |
| 296 |
// J does not occur in the blosm matrix and is not allowed in some programs. |
| 297 |
inline bool is_aa_ambig(char c) |
| 298 |
{ |
| 299 |
if (c == 'b' || c == 'B' || c == '?' || c == 'x' || c == 'X' || |
| 300 |
c == 'j' || c == 'J' || c == 'z' || c == 'Z') |
| 301 |
return true; |
| 302 |
else |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
inline bool is_aa_blosum_code(unsigned char c) |
| 307 |
{ |
| 308 |
return (is_aa_blosum_code_lookup[c] != 0); |
| 309 |
} |
| 310 |
|
| 311 |
inline bool is_aa_blosum_code_or_gap(unsigned char c) |
| 312 |
{ |
| 313 |
return (is_aa_blosum_code_or_gap_lookup[c] != 0); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
inline int is_aa_upper(char c) |
| 319 |
{ |
| 320 |
if (c >='A' && c <= 'Z') |
| 321 |
{ |
| 322 |
// AA ambigs: |
| 323 |
if (c == 'B' || c == 'X' || c == 'J' || c == 'Z') |
| 324 |
return false; |
| 325 |
|
| 326 |
// The few symbols that represent nothing. |
| 327 |
// Indeed they are listed as the 21st and 22nd amino acid in https://en.wikipedia.org/wiki/Amino_acid |
| 328 |
// But they are usually not handled by programs, so we return false. |
| 329 |
if (c == 'O' || c == 'U') // OU |
| 330 |
return false; |
| 331 |
return true; |
| 332 |
} |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
inline int is_aa_or_aa_ambig(char c) // includes '?' and B, Z, J, X |
| 338 |
{ |
| 339 |
if (c >='a' && c <= 'z') |
| 340 |
{ |
| 341 |
if (c != 'o' && c != 'u') // Only not allowed characters in a..z are: ou |
| 342 |
return true; |
| 343 |
} |
| 344 |
else if (c >='A' && c <= 'Z') |
| 345 |
{ |
| 346 |
if (c != 'O' && c != 'U') // Only not allowed characters in a..z are: OU |
| 347 |
return true; |
| 348 |
} |
| 349 |
|
| 350 |
if (c == '?' || c == '*') |
| 351 |
return true; |
| 352 |
|
| 353 |
return false; |
| 354 |
// return (strchr(aa_symbols, c) != NULL); |
| 355 |
} |
| 356 |
|
| 357 |
inline int is_aa_or_aa_ambig_extended(char c) // O and U are allowed // includes '?' and B, Z, J, X |
| 358 |
{ |
| 359 |
if (c >='a' && c <= 'z') |
| 360 |
{ |
| 361 |
return true; |
| 362 |
} |
| 363 |
else if (c >='A' && c <= 'Z') |
| 364 |
{ |
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
if (c == '?' || c == '*') |
| 369 |
return true; |
| 370 |
|
| 371 |
return false; |
| 372 |
// return (strchr(aa_symbols, c) != NULL); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
inline char complementBase_of_upper(char c) |
| 377 |
{ |
| 378 |
switch (c) { |
| 379 |
case 'A': c = 'T'; break; |
| 380 |
case 'T': c = 'A'; break; |
| 381 |
case 'G': c = 'C'; break; |
| 382 |
case 'C': c = 'G'; break; |
| 383 |
|
| 384 |
case 'R': c = 'Y'; break; |
| 385 |
case 'Y': c = 'R'; break; |
| 386 |
|
| 387 |
case 'M': c = 'K'; break; |
| 388 |
case 'K': c = 'M'; break; |
| 389 |
|
| 390 |
case 'B': c = 'V'; break; |
| 391 |
case 'V': c = 'B'; break; |
| 392 |
|
| 393 |
case 'D': c = 'H'; break; |
| 394 |
case 'H': c = 'D'; break; |
| 395 |
|
| 396 |
case 'N': c = 'N'; break; |
| 397 |
case '?': c = '?'; break; |
| 398 |
case '-': c = '-'; break; |
| 399 |
|
| 400 |
default: |
| 401 |
c = '*'; |
| 402 |
break; |
| 403 |
} |
| 404 |
return c; |
| 405 |
} |
| 406 |
|
| 407 |
inline char complementBase_of_lower(char c) |
| 408 |
{ |
| 409 |
switch (c) { |
| 410 |
case 'a': c = 't'; break; |
| 411 |
case 't': c = 'a'; break; |
| 412 |
case 'g': c = 'c'; break; |
| 413 |
case 'c': c = 'g'; break; |
| 414 |
|
| 415 |
case 'r': c = 'y'; break; |
| 416 |
case 'y': c = 'r'; break; |
| 417 |
|
| 418 |
case 'm': c = 'k'; break; |
| 419 |
case 'k': c = 'm'; break; |
| 420 |
|
| 421 |
case 'b': c = 'v'; break; |
| 422 |
case 'v': c = 'b'; break; |
| 423 |
|
| 424 |
case 'd': c = 'h'; break; |
| 425 |
case 'h': c = 'd'; break; |
| 426 |
|
| 427 |
case 'n': c = 'n'; break; |
| 428 |
case '?': c = '?'; break; |
| 429 |
case '-': c = '-'; break; |
| 430 |
|
| 431 |
default: |
| 432 |
c = '*'; |
| 433 |
break; |
| 434 |
} |
| 435 |
return c; |
| 436 |
} |
| 437 |
|
| 438 |
inline char complementBase(char c) |
| 439 |
{ |
| 440 |
if ( isupper(c) ) |
| 441 |
return complementBase_of_upper(c); |
| 442 |
else |
| 443 |
return complementBase_of_lower(c); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
inline void CG_AT_content_in_region(const char *beg, const char *end, unsigned &AT, unsigned &CG) |
| 448 |
{ |
| 449 |
AT = 0; |
| 450 |
CG = 0; |
| 451 |
|
| 452 |
char c; |
| 453 |
|
| 454 |
while (beg != end) |
| 455 |
{ |
| 456 |
c = *beg; |
| 457 |
if (c == 'C' || c == 'c' || c == 'G' || c == 'g') |
| 458 |
++CG; |
| 459 |
else if (c == 'A' || c == 'a' || c == 'T' || c == 't') |
| 460 |
++AT; |
| 461 |
|
| 462 |
++beg; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
inline void content_in_region_DNA(const char *beg, const char *end, unsigned &AT, unsigned &CG, unsigned &gap, unsigned &ambig, unsigned &unknown) |
| 467 |
{ |
| 468 |
AT = 0; |
| 469 |
CG = 0; |
| 470 |
gap = 0; |
| 471 |
ambig = 0; |
| 472 |
unknown = 0; |
| 473 |
|
| 474 |
char c; |
| 475 |
|
| 476 |
while (beg != end) |
| 477 |
{ |
| 478 |
c = *beg; |
| 479 |
if (c == 'C' || c == 'c' || c == 'G' || c == 'g') |
| 480 |
++CG; |
| 481 |
else if (c == 'A' || c == 'a' || c == 'T' || c == 't') |
| 482 |
++AT; |
| 483 |
else if (c == '-') |
| 484 |
++gap; |
| 485 |
else if (is_DNA_ambig(c) ) |
| 486 |
++ambig; |
| 487 |
else |
| 488 |
++unknown; |
| 489 |
|
| 490 |
++beg; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
inline bool is_aa_blosum_code_or_gap_sequence(const char *p1, const char *p2, unsigned *offending_pos) |
| 495 |
{ |
| 496 |
const char *startp = p1; |
| 497 |
|
| 498 |
while (p1 != p2) |
| 499 |
{ |
| 500 |
if (!is_aa_blosum_code_or_gap(*p1) ) |
| 501 |
{ |
| 502 |
*offending_pos = p1 - startp; |
| 503 |
return false; |
| 504 |
} |
| 505 |
++p1; |
| 506 |
} |
| 507 |
return true; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
// Recoding symbols: |
| 513 |
// A char can hold 8 bits, enough for 8 characters to be "in or out", |
| 514 |
// to be switched on or off. Thus a char can be used to hold |
| 515 |
// all bases including ambiguities as follows: |
| 516 |
// Say A, C, G, T are the bits 1,2,3,4, |
| 517 |
// the ambiguity {A,C} is 1+2 = 3 |
| 518 |
|
| 519 |
// A: 65, 97 -> 1 |
| 520 |
// C: 67, 99 -> 2 |
| 521 |
// G: 71,103 -> 4 |
| 522 |
// T: 84,116 -> 8 |
| 523 |
// R(AG): 82,114 -> 5 |
| 524 |
// Y(CT): 89,121 -> 10 |
| 525 |
// S(CG): 83,115 -> 6 |
| 526 |
// W(TA): 87,119 -> 9 |
| 527 |
// K(TG): 75,107 -> 12 |
| 528 |
// M(CA): 77,109 -> 3 |
| 529 |
// B(CTG): 66,98 -> 14 |
| 530 |
// D(ATG): 68,100 -> 13 |
| 531 |
// H(ATC): 72,104 -> 11 |
| 532 |
// V(ACG): 86,118 -> 7 |
| 533 |
// N(ACGT): 78,110 -> 15 |
| 534 |
// - 45 -> 0 |
| 535 |
// ? 63 -> 15 |
| 536 |
|
| 537 |
const char ascii2recode_DNA[128] = { |
| 538 |
0,0,0,0,0,0,0,0,0,0, // 0- 9 |
| 539 |
0,0,0,0,0,0,0,0,0,0, // 10-19 |
| 540 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 541 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 542 |
0,0,0,0,0,0,0,0,0,0, // 40-49 |
| 543 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 544 |
0,0,0,15,0,1,14,2,13,0, // 60-69 |
| 545 |
0,4,11,0,0,12,0,3,15,0, // 70-79 |
| 546 |
0,0,5,6,8,0,7,9,0,10, // 80-89 |
| 547 |
0,0,0,0,0,0,0,1,14,2, // 90-99 |
| 548 |
13,0,0,4,11,0,0,0,0,0, //100-109 |
| 549 |
0,0,0,0,5,0,8,0,7,9, //110-119 |
| 550 |
0,10,0,0,0,0,0,0 //120-127 |
| 551 |
}; |
| 552 |
|
| 553 |
const char recode2ascii_DNA[128] = { |
| 554 |
45,65,67,77,71,82,83,86,84,87, // 0- 9 |
| 555 |
89,72,75,68,66,78,0,0,0,0, // 10-19 |
| 556 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 557 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 558 |
0,0,0,0,0,0,0,0,0,0, // 40-49 |
| 559 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 560 |
0,0,0,0,0,0,0,0,0,0, // 60-69 |
| 561 |
0,0,0,0,0,0,0,0,0,0, // 70-79 |
| 562 |
0,0,0,0,0,0,0,0,0,0, // 80-89 |
| 563 |
0,0,0,0,0,0,0,0,0,0, // 90-99 |
| 564 |
0,0,0,0,0,0,0,0,0,0, //100-109 |
| 565 |
0,0,0,0,0,0,0,0,0,0, //110-119 |
| 566 |
0,0,0,0,0,0,0,0 //120-127 |
| 567 |
}; |
| 568 |
|
| 569 |
const char recodeISDNAamig[128] = { |
| 570 |
0,0,0,1,0,1,1,1,0,1, // 0- 9 |
| 571 |
1,1,1,1,1,1,0,0,0,0, // 10-19 |
| 572 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 573 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 574 |
0,0,0,0,0,0,0,0,0,0, // 40-49 |
| 575 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 576 |
0,0,0,0,0,0,0,0,0,0, // 60-69 |
| 577 |
0,0,0,0,0,0,0,0,0,0, // 70-79 |
| 578 |
0,0,0,0,0,0,0,0,0,0, // 80-89 |
| 579 |
0,0,0,0,0,0,0,0,0,0, // 90-99 |
| 580 |
0,0,0,0,0,0,0,0,0,0, //100-109 |
| 581 |
0,0,0,0,0,0,0,0,0,0, //110-119 |
| 582 |
0,0,0,0,0,0,0,0 //120-127 |
| 583 |
}; |
| 584 |
|
| 585 |
inline unsigned char recode_DNA(unsigned char c) |
| 586 |
{ |
| 587 |
return ascii2recode_DNA[c]; |
| 588 |
} |
| 589 |
|
| 590 |
inline unsigned char backrecode_DNA(unsigned char c) |
| 591 |
{ |
| 592 |
return recode2ascii_DNA[c]; |
| 593 |
} |
| 594 |
|
| 595 |
inline unsigned char recode_is_DNA_ambig(unsigned char c) |
| 596 |
{ |
| 597 |
return recodeISDNAamig[c]; |
| 598 |
} |
| 599 |
|
| 600 |
inline bool is_base_in_recode(unsigned char recode, unsigned char base) |
| 601 |
{ |
| 602 |
return recode & recode_DNA(base); |
| 603 |
} |
| 604 |
|
| 605 |
inline bool is_valid_recode_DNA(unsigned char c) |
| 606 |
{ |
| 607 |
if (c > 15) |
| 608 |
return false; |
| 609 |
else |
| 610 |
return true; |
| 611 |
} |
| 612 |
|
| 613 |
inline bool is_recode_gap(unsigned char c) |
| 614 |
{ |
| 615 |
return (c == 0); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
// A Ala Alanine 01 65 |
| 621 |
// R Arg Arginine 02 82 |
| 622 |
// N Asn Asparagine 03 78 |
| 623 |
// D Asp Aspartic acid 08 68 |
| 624 |
// C Cys Cysteine 16 67 |
| 625 |
// Q Gln Glutamine 32 81 |
| 626 |
// E Glu Glutamic acid 64 69 |
| 627 |
// G Gly Glycine 128 71 |
| 628 |
// H His Histidine 256 72 |
| 629 |
// I Ile Isoleucine 512 73 |
| 630 |
// L Leu Leucine 1024 76 |
| 631 |
// K Lys Lysine 2048 75 |
| 632 |
// M Met Methionine 4096 77 |
| 633 |
// F Phe Phenylalanine 8192 70 |
| 634 |
// P Pro Proline 16384 80 |
| 635 |
// S Ser Serine 32768 83 |
| 636 |
// T Thr Threonine 65536 84 |
| 637 |
// W Trp Tryptophan 131072 87 |
| 638 |
// Y Tyr Tyrosine 262144 89 |
| 639 |
// V Val Valine 524288 86 |
| 640 |
// - 45 -> 0 |
| 641 |
// ? 63 -> 1048575 |
| 642 |
// X 88 -> 1048575 |
| 643 |
|
| 644 |
const int ascii2recode_aa[128] = { |
| 645 |
0,0,0,0,0,0,0,0,0,0, // 0- 9 |
| 646 |
0,0,0,0,0,0,0,0,0,0, // 10-19 |
| 647 |
0,0,0,0,0,0,0,0,0,0, // 20-29 |
| 648 |
0,0,0,0,0,0,0,0,0,0, // 30-39 |
| 649 |
0,0,0,0,0,64,0,0,0,0, // 40-49 |
| 650 |
0,0,0,0,0,0,0,0,0,0, // 50-59 |
| 651 |
0,0,0,15,0,1,14,2,13,0, // 60-69 |
| 652 |
0,4,11,0,0,12,0,3,15,0, // 70-79 |
| 653 |
0,0,5,6,8,0,7,9,0,10, // 80-89 |
| 654 |
0,0,0,0,0,0,0,1,14,2, // 90-99 |
| 655 |
13,0,0,4,11,0,0,0,0,0, //100-109 |
| 656 |
0,0,0,0,5,0,8,0,7,9, //110-119 |
| 657 |
0,10,0,0,0,0,0,0 //120-127 |
| 658 |
}; |
| 659 |
|
| 660 |
|
| 661 |
#endif |
| 662 |
|