| 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 CSEQUENCE_MOL_H |
| 29 |
#define CSEQUENCE_MOL_H |
| 30 |
|
| 31 |
// Changes: |
| 32 |
// 22.08.2009: According to the fasta format specifications, the sequence name is only the string between the ">" and |
| 33 |
// the first space. The rest of the line contains a sequence description or annotation information. |
| 34 |
// This is now reflected in the CSequence_Mol class. The variables full_name, name, and description |
| 35 |
// contain this information. |
| 36 |
// The sequence name variables are now of type faststring (not string as before). |
| 37 |
// 07.01.2011: Migrated to fastring2.h and CFile2_1.h |
| 38 |
// 30.04.2011: Sliding window CG-AT analysis |
| 39 |
|
| 40 |
// TODO: Data type handling is/was not optimal. |
| 41 |
// In function set_taxon_and_sequence the auto_detect_type always superset the |
| 42 |
// value the user specified when creating the CSequence_Mol object. |
| 43 |
// This caused unwanted changes of the data type. |
| 44 |
|
| 45 |
|
| 46 |
#include <vector> |
| 47 |
#include <iostream> |
| 48 |
#include "faststring2.h" |
| 49 |
//#include "CFile/CFile3b.h" |
| 50 |
#include "CFile/CFile2_1.h" |
| 51 |
#include <cstdio> |
| 52 |
#include "basic-DNA-RNA-AA-routines.h" |
| 53 |
#include "fast-realloc-vector.h" |
| 54 |
#include <cstring> |
| 55 |
#include <cstdio> |
| 56 |
|
| 57 |
class CSequence_Mol |
| 58 |
{ |
| 59 |
public: |
| 60 |
enum DataTypesEnum { |
| 61 |
standard = 1, // characters with arbitrarily-assigned, |
| 62 |
// discrete states, such as discrete |
| 63 |
// morphological data |
| 64 |
|
| 65 |
dna, /* DNA sequences (states A, C, G, T) */ |
| 66 |
rna, /* RNA sequences (states A, C, G, U) */ |
| 67 |
// nucleotide, /* nucleotide sequences */ |
| 68 |
protein, /* amino acid sequences */ |
| 69 |
molecular, /* AA, dna, or rna */ |
| 70 |
unknown, |
| 71 |
auto_detect_type, |
| 72 |
mixed |
| 73 |
}; |
| 74 |
|
| 75 |
enum processing_flag { |
| 76 |
convert_toupper = 1, |
| 77 |
convert_ambig2N = 2, |
| 78 |
autodetecttype = 4, |
| 79 |
removegaps = 8, |
| 80 |
removestars = 16, |
| 81 |
compute_numbers_of_residues_flag = 32, |
| 82 |
gaps_to_ambig = 64 |
| 83 |
}; |
| 84 |
|
| 85 |
private: |
| 86 |
faststring full_name; // with description after first space |
| 87 |
faststring short_name; // without description after first space |
| 88 |
faststring description; // description only |
| 89 |
|
| 90 |
faststring data; |
| 91 |
DataTypesEnum type; |
| 92 |
char general_ambig; |
| 93 |
|
| 94 |
bool numbers_of_residues_computed; |
| 95 |
|
| 96 |
// General rule: If we convert symbols, we count the new symbols we have in memory, |
| 97 |
// not the symbols in the file. This is more consistent. |
| 98 |
// |
| 99 |
unsigned number_of_AAs; // Does not include Xs and ? |
| 100 |
unsigned number_of_DNARNA_bases; // Includes Us but not ambigs |
| 101 |
unsigned number_of_DNARNA_amgibs; // Includes Ns and '?' |
| 102 |
unsigned number_of_Us; |
| 103 |
unsigned number_of_gaps; |
| 104 |
unsigned number_of_stars; |
| 105 |
unsigned number_of_questionmarks; |
| 106 |
|
| 107 |
unsigned number_of_Xs; |
| 108 |
unsigned number_of_Ns; |
| 109 |
unsigned number_raw_original_length; //no spaces, but gaps and stars |
| 110 |
unsigned number_CG; // CG content in case of DNA/RNA |
| 111 |
|
| 112 |
|
| 113 |
fastvector<unsigned> gap_and_star_koords; |
| 114 |
fastvector<unsigned> gap_koords; |
| 115 |
fastvector<unsigned> star_koords; |
| 116 |
|
| 117 |
bool bitcode_recoded; |
| 118 |
|
| 119 |
private: |
| 120 |
void find_shortname_and_description() |
| 121 |
{ |
| 122 |
full_name.removeSpacesBack(); |
| 123 |
full_name.removeSpacesFront(); |
| 124 |
|
| 125 |
faststring::size_t pos_description = full_name.find(' '); |
| 126 |
faststring::size_t full_name_len = full_name.size(); |
| 127 |
|
| 128 |
if (pos_description == faststring::npos) // no space -> no sequence description |
| 129 |
{ |
| 130 |
pos_description = full_name.size(); |
| 131 |
short_name.assign(full_name.begin(), full_name.begin() + pos_description); |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
short_name.assign(full_name.begin(), full_name.begin() + pos_description); |
| 136 |
// Skip all spaces |
| 137 |
while (pos_description < full_name_len && full_name.get_unckecked(pos_description) == ' ') |
| 138 |
{ |
| 139 |
// std::cerr << "-------------DEBUG: ++pos_description" << std::endl; |
| 140 |
++pos_description; |
| 141 |
} |
| 142 |
|
| 143 |
// std::cerr << "-------------DEBUG: " << full_name.end() - (full_name.begin() + pos_description) << std::endl; |
| 144 |
// if (full_name.end() - (full_name.begin() + pos_description) > 0) // Should not occur any more!!!! |
| 145 |
// { |
| 146 |
description.assign(full_name.begin() + pos_description, full_name.end() ); |
| 147 |
// } |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void toupper_this_sequence() |
| 152 |
{ |
| 153 |
data.toupper(); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
// TODO: Keep case of ambig. |
| 159 |
void change_ambigs() |
| 160 |
{ |
| 161 |
if (type == dna || type == rna) |
| 162 |
{ |
| 163 |
char * it = data.begin(); |
| 164 |
char * it_end = data.end(); |
| 165 |
|
| 166 |
number_of_questionmarks = 0; |
| 167 |
number_of_Ns = 0; |
| 168 |
|
| 169 |
while (it != it_end) |
| 170 |
{ |
| 171 |
char c = *it; |
| 172 |
|
| 173 |
if ( c == 'N' || c == 'n' || c == '?' ) |
| 174 |
{ |
| 175 |
// Convert all ambiguity codes to N's |
| 176 |
*it = general_ambig; |
| 177 |
if (general_ambig == '?') |
| 178 |
++number_of_questionmarks; |
| 179 |
else |
| 180 |
++number_of_Ns; |
| 181 |
// ++number_of_DNARNA_amgibs; |
| 182 |
} |
| 183 |
++it; |
| 184 |
} |
| 185 |
} |
| 186 |
else if (type == protein) |
| 187 |
{ |
| 188 |
char * it = data.begin(); |
| 189 |
char * it_end = data.end(); |
| 190 |
|
| 191 |
number_of_questionmarks = 0; |
| 192 |
number_of_Xs = 0; |
| 193 |
|
| 194 |
while (it != it_end) |
| 195 |
{ |
| 196 |
char c = *it; |
| 197 |
|
| 198 |
if ( c == 'X' || c == 'x' || c == '?' ) |
| 199 |
{ |
| 200 |
// Convert all ambiguity codes to N's |
| 201 |
++number_of_DNARNA_amgibs; |
| 202 |
*it = general_ambig; |
| 203 |
if (general_ambig == '?' ) |
| 204 |
++number_of_questionmarks; |
| 205 |
else |
| 206 |
++number_of_Xs; |
| 207 |
} |
| 208 |
++it; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// TODO: Adjust the numbers |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
void remove_gaps_stars_this_sequence(bool rg, bool rs) |
| 217 |
{ |
| 218 |
// char *it = data.begin(); |
| 219 |
// char *it_end = data.end(); |
| 220 |
|
| 221 |
|
| 222 |
// TODO |
| 223 |
|
| 224 |
if (rg) |
| 225 |
{} |
| 226 |
|
| 227 |
if (rs) |
| 228 |
{} |
| 229 |
|
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
void gaps_to_ambig_this_sequence() |
| 234 |
{ |
| 235 |
char *it = data.begin(); |
| 236 |
char *it_end = data.end(); |
| 237 |
unsigned count=0; |
| 238 |
|
| 239 |
while (it != it_end) |
| 240 |
{ |
| 241 |
if (*it =='-') |
| 242 |
{ |
| 243 |
*it = general_ambig; |
| 244 |
++count; |
| 245 |
} |
| 246 |
++it; |
| 247 |
} |
| 248 |
|
| 249 |
// If residues are computed, we have to change the numbers counts for gaps and ambigs: |
| 250 |
if (numbers_of_residues_computed) |
| 251 |
{ |
| 252 |
number_of_gaps -= count; // Should indeed be 0 now. |
| 253 |
|
| 254 |
if (general_ambig == 'N' || general_ambig == 'n') |
| 255 |
{ |
| 256 |
number_of_Ns += count; |
| 257 |
} |
| 258 |
else if (general_ambig == 'X' || general_ambig == 'x') |
| 259 |
{ |
| 260 |
number_of_Xs += count; |
| 261 |
} |
| 262 |
else if (general_ambig == '?') |
| 263 |
{ |
| 264 |
number_of_questionmarks += count; |
| 265 |
} |
| 266 |
if (type == dna || type == rna) |
| 267 |
{ |
| 268 |
++number_of_DNARNA_amgibs += count; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
void addCharToData(char c) |
| 275 |
{ |
| 276 |
data.push_back(c); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
// Reads line of data and removes only white spaces |
| 281 |
void getRawLine(CFile& infile) |
| 282 |
{ |
| 283 |
register char c; |
| 284 |
|
| 285 |
c = infile.getchar(); |
| 286 |
while (!infile.fail() && c != '\n') |
| 287 |
{ |
| 288 |
if ( isspace(c) ) // We remove white spaces. |
| 289 |
{ |
| 290 |
c = infile.getchar(); |
| 291 |
continue; |
| 292 |
} |
| 293 |
addCharToData(c); |
| 294 |
c = infile.getchar(); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
void getRawLine(CFile& infile, unsigned &num) |
| 299 |
{ |
| 300 |
register char c; |
| 301 |
unsigned local_num = 0; |
| 302 |
|
| 303 |
c = infile.getchar(); |
| 304 |
while (!infile.fail() && c != '\n') |
| 305 |
{ |
| 306 |
if ( isspace(c) ) // We remove white spaces. |
| 307 |
{ |
| 308 |
c = infile.getchar(); |
| 309 |
continue; |
| 310 |
} |
| 311 |
addCharToData(c); |
| 312 |
c = infile.getchar(); |
| 313 |
++local_num; |
| 314 |
} |
| 315 |
num = local_num; |
| 316 |
} |
| 317 |
|
| 318 |
// Reads line of data and removes only white spaces |
| 319 |
void getRawLine_toupper(CFile& infile) |
| 320 |
{ |
| 321 |
register char c; |
| 322 |
|
| 323 |
c = infile.getchar(); |
| 324 |
while (!infile.fail() && c != '\n') |
| 325 |
{ |
| 326 |
if ( isspace(c) ) // We remove white spaces. |
| 327 |
{ |
| 328 |
c = infile.getchar(); |
| 329 |
continue; |
| 330 |
} |
| 331 |
addCharToData(toupper(c)); |
| 332 |
c = infile.getchar(); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
// Called e.g. from Phobos |
| 338 |
void getLineOfDNA_toupper_ambig2N_removegaps (CFile& infile, unsigned &pos_in_seq) |
| 339 |
{ |
| 340 |
register char c; |
| 341 |
unsigned pos = pos_in_seq; |
| 342 |
register char local_general_ambig = toupper(general_ambig); |
| 343 |
|
| 344 |
// ++pos; |
| 345 |
c = infile.getchar(); |
| 346 |
while (!infile.fail() && c != '\n') |
| 347 |
{ |
| 348 |
// if the char is a space or something we do not count, we subtract this further below |
| 349 |
++number_raw_original_length; |
| 350 |
|
| 351 |
c = toupper(c); |
| 352 |
|
| 353 |
if ( c == 'A' || c == 'T' || c == 'G' || c == 'C') |
| 354 |
{ |
| 355 |
addCharToData(c); |
| 356 |
++number_of_DNARNA_bases; |
| 357 |
} |
| 358 |
else if ( c == 'U') |
| 359 |
{ |
| 360 |
addCharToData('U'); |
| 361 |
++number_of_DNARNA_bases; |
| 362 |
++number_of_Us; |
| 363 |
} |
| 364 |
else if ( c == 'R' || c == 'Y' || c == 'K' || c == 'M' || |
| 365 |
c == 'S' || c == 'W' || c == 'B' || c == 'D' || |
| 366 |
c == 'H' || c == 'V' ) |
| 367 |
{ |
| 368 |
// Convert all ambiguity codes to N's |
| 369 |
addCharToData(local_general_ambig); |
| 370 |
++number_of_DNARNA_amgibs; // includes N and '?' |
| 371 |
if (local_general_ambig == 'N') |
| 372 |
++number_of_Ns; |
| 373 |
else |
| 374 |
++number_of_questionmarks; |
| 375 |
} |
| 376 |
else if (c == '?' || c == 'N') |
| 377 |
{ |
| 378 |
addCharToData(local_general_ambig); |
| 379 |
if (local_general_ambig == 'N') |
| 380 |
++number_of_Ns; |
| 381 |
else |
| 382 |
++number_of_questionmarks; |
| 383 |
++number_of_DNARNA_amgibs; |
| 384 |
} |
| 385 |
else if ( isspace(c) ) |
| 386 |
{ |
| 387 |
--number_raw_original_length; // we do not count spaces |
| 388 |
} // Spaces are removed |
| 389 |
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed, |
| 390 |
// but their positions are stored in order to able to reconstruct the original coordinates. |
| 391 |
{ // I don't really know why we tolerate '*' ? They occur in protein sequences as stop codons. |
| 392 |
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence, |
| 393 |
// we need to keep track of the positions we cut out. |
| 394 |
if (c=='-') |
| 395 |
{ |
| 396 |
gap_koords.push_back(pos); |
| 397 |
++number_of_gaps; |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
star_koords.push_back(pos); |
| 402 |
++number_of_stars; |
| 403 |
} |
| 404 |
// c = infile.getchar(); |
| 405 |
// ++pos; |
| 406 |
} |
| 407 |
else |
| 408 |
{ |
| 409 |
// we do not count this -just to be consistent before we bail out |
| 410 |
--number_raw_original_length; |
| 411 |
infile.clear(CFile::__fail_reason1 | CFile::__fail_flag); |
| 412 |
break; |
| 413 |
} |
| 414 |
c = infile.getchar(); |
| 415 |
++pos; |
| 416 |
} |
| 417 |
pos_in_seq = pos; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
void getLineOfDNA_generic (CFile& infile, unsigned &pos_in_seq, |
| 422 |
processing_flag pflag) |
| 423 |
{ |
| 424 |
register char c; |
| 425 |
register char c_upper; |
| 426 |
register char local_general_ambig = general_ambig; |
| 427 |
unsigned pos = pos_in_seq; |
| 428 |
// register signed char delta; |
| 429 |
register bool convert_toupper_bool = (pflag & convert_toupper); // handled |
| 430 |
register bool convert_ambig2N_bool = (pflag & convert_ambig2N); // handled |
| 431 |
register bool removegaps_bool = (pflag & removegaps); // handled |
| 432 |
register bool removestars_bool = (pflag & removestars); // handled |
| 433 |
register bool gaps_to_ambig_bool = (pflag & gaps_to_ambig); // handled |
| 434 |
|
| 435 |
// In the routine calling this function it should be checked, that the general_ambig |
| 436 |
// character is upper or lower case, as specified. |
| 437 |
|
| 438 |
|
| 439 |
if (convert_toupper_bool) |
| 440 |
local_general_ambig = toupper(general_ambig); |
| 441 |
|
| 442 |
// ++pos; |
| 443 |
c = infile.getchar(); |
| 444 |
c_upper = toupper(c); |
| 445 |
while (!infile.fail() && c != '\n') |
| 446 |
{ |
| 447 |
// if the char is a space or something we do not count, we subtract this further below |
| 448 |
++number_raw_original_length; |
| 449 |
|
| 450 |
if (c >= 'a' && c <= 'z' && convert_toupper_bool) |
| 451 |
c = c_upper; |
| 452 |
|
| 453 |
if ( c_upper == 'A' || c_upper == 'T' || c_upper == 'G' || c_upper == 'C') |
| 454 |
{ |
| 455 |
addCharToData(c); |
| 456 |
++number_of_DNARNA_bases; |
| 457 |
} |
| 458 |
else if ( c_upper == 'U') |
| 459 |
{ |
| 460 |
addCharToData(c); |
| 461 |
++number_of_DNARNA_bases; |
| 462 |
++number_of_Us; |
| 463 |
} |
| 464 |
else if (c_upper == 'R' || c_upper == 'Y' || c_upper == 'K' || c_upper == 'M' || |
| 465 |
c_upper == 'S' || c_upper == 'W' || c_upper == 'B' || c_upper == 'D' || |
| 466 |
c_upper == 'H' || c_upper == 'V' ) |
| 467 |
{ |
| 468 |
if (convert_ambig2N_bool) |
| 469 |
{ |
| 470 |
// Convert all ambiguity codes to N's, but we preserve the case. 'w' -> 'n' and 'W' -> 'N' etc. |
| 471 |
if (c <='Z') // i.e. lower case. |
| 472 |
addCharToData( tolower(local_general_ambig) ); |
| 473 |
else |
| 474 |
addCharToData( toupper(local_general_ambig) ); |
| 475 |
|
| 476 |
++number_of_DNARNA_amgibs; // includes N and '?' |
| 477 |
if (local_general_ambig == '?' ) |
| 478 |
++number_of_questionmarks; |
| 479 |
else |
| 480 |
++number_of_Ns; |
| 481 |
} |
| 482 |
else |
| 483 |
{ |
| 484 |
addCharToData(c); |
| 485 |
} |
| 486 |
} |
| 487 |
else if (c == '?' || c_upper == 'N') // c is either: 'N', 'n', '?' |
| 488 |
{ |
| 489 |
++number_of_DNARNA_amgibs; |
| 490 |
if (local_general_ambig == '?') |
| 491 |
{ |
| 492 |
addCharToData(local_general_ambig); |
| 493 |
++number_of_questionmarks; |
| 494 |
} |
| 495 |
else // c is '?' or 'N' or 'n' and general_ambig is 'N' or 'n' --- add 'N' or 'n' |
| 496 |
{ |
| 497 |
++number_of_Ns; |
| 498 |
if (c == '?') // convert to general ambig with proper case: |
| 499 |
{ |
| 500 |
addCharToData(local_general_ambig); // Append general_ambig with appropriate case. |
| 501 |
} |
| 502 |
else // c is 'N' or 'n', general_ambig is 'N' or 'n' |
| 503 |
{ |
| 504 |
addCharToData(c); // Append N or n and preserve case !!! to be consistent with the flags. |
| 505 |
} |
| 506 |
// Note: In case of: convert to upper, we have already converted c to upper. |
| 507 |
} |
| 508 |
} |
| 509 |
else if ( isspace(c) ) |
| 510 |
{ |
| 511 |
--number_raw_original_length; // we do not count spaces |
| 512 |
} // Spaces are removed |
| 513 |
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed, |
| 514 |
// but their positions are stored in order to able to reconstruct the original coordinates. |
| 515 |
{ // I don't really know why we tolerate '*' ? They occur in protein sequences as stop codons. |
| 516 |
|
| 517 |
if (c=='-') |
| 518 |
{ |
| 519 |
if ( gaps_to_ambig_bool) |
| 520 |
{ |
| 521 |
// std::cerr << "Gaps to ambig " << std::endl; |
| 522 |
addCharToData(local_general_ambig); |
| 523 |
if (general_ambig == '?' ) |
| 524 |
++number_of_questionmarks; |
| 525 |
else |
| 526 |
++number_of_Ns; |
| 527 |
++number_of_DNARNA_amgibs; |
| 528 |
} |
| 529 |
else if ( removegaps_bool ) |
| 530 |
{ |
| 531 |
// std::cerr << "Remove gaps " << std::endl; |
| 532 |
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence, |
| 533 |
// we need to keep track of the positions we cut out. |
| 534 |
gap_koords.push_back(pos); |
| 535 |
++number_of_gaps; |
| 536 |
} |
| 537 |
else |
| 538 |
{ |
| 539 |
addCharToData('-'); |
| 540 |
++number_of_gaps; |
| 541 |
} |
| 542 |
} |
| 543 |
else if (removestars_bool) |
| 544 |
{ |
| 545 |
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence, |
| 546 |
// we need to keep track of the positions we cut out. |
| 547 |
star_koords.push_back(pos); |
| 548 |
++number_of_stars; |
| 549 |
} |
| 550 |
// c = infile.getchar(); |
| 551 |
// ++pos; |
| 552 |
} |
| 553 |
else |
| 554 |
{ |
| 555 |
// we do not count this -just to be consistent before we bail out |
| 556 |
--number_raw_original_length; |
| 557 |
infile.clear(CFile::__fail_reason1 | CFile::__fail_flag); |
| 558 |
break; |
| 559 |
} |
| 560 |
c = infile.getchar(); |
| 561 |
c_upper = toupper(c); |
| 562 |
++pos; |
| 563 |
} |
| 564 |
pos_in_seq = pos; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
void getLineOfProtein_generic (CFile& infile, unsigned &pos_in_seq, |
| 569 |
processing_flag pflag) |
| 570 |
{ |
| 571 |
register char c; |
| 572 |
register char c_upper; |
| 573 |
register char local_general_ambig = general_ambig; |
| 574 |
|
| 575 |
|
| 576 |
unsigned pos = pos_in_seq; |
| 577 |
// register signed char delta; |
| 578 |
register bool convert_toupper_bool = (pflag & convert_toupper); // handled |
| 579 |
// register bool convert_ambig2N_bool = (pflag & convert_ambig2N); // handled |
| 580 |
register bool removegaps_bool = (pflag & removegaps); // handled |
| 581 |
// register bool removestars_bool = (pflag & removestars); // handled |
| 582 |
register bool gaps_to_ambig_bool = (pflag & gaps_to_ambig);// handled |
| 583 |
|
| 584 |
// In the routine calling this function it should be checked, that the general_ambig |
| 585 |
// character is upper or lower case, as specified. |
| 586 |
|
| 587 |
if (convert_toupper_bool) |
| 588 |
local_general_ambig = toupper(general_ambig); |
| 589 |
|
| 590 |
// ++pos; |
| 591 |
c = infile.getchar(); |
| 592 |
c_upper = toupper(c); |
| 593 |
while (!infile.fail() && c != '\n') |
| 594 |
{ |
| 595 |
// if the char is a space or something we do not count, we subtract this further below |
| 596 |
++number_raw_original_length; |
| 597 |
|
| 598 |
if (c >= 'a' && c <= 'z' && convert_toupper_bool) |
| 599 |
c = c_upper; |
| 600 |
|
| 601 |
if (c == '?' || c_upper == 'X' ) |
| 602 |
{ |
| 603 |
if (local_general_ambig == '?') |
| 604 |
{ |
| 605 |
addCharToData(local_general_ambig); |
| 606 |
++number_of_questionmarks; |
| 607 |
} |
| 608 |
else // c is '?' or 'X' or 'x' and general_ambig is 'X' or 'x' |
| 609 |
{ |
| 610 |
++number_of_Xs; |
| 611 |
if (c == '?') |
| 612 |
{ |
| 613 |
addCharToData(local_general_ambig); |
| 614 |
} |
| 615 |
else // c is 'X' 'x', general_ambig is 'X' 'x' |
| 616 |
addCharToData(c); // Append X or x and preserve case !!! to be consistent with the flags. |
| 617 |
// Note: In case of convert to upper, we have already converted c to upper. |
| 618 |
} |
| 619 |
} |
| 620 |
else // The following code assumes we do not have X or ?. Otherwise Xs are duplicated in the data. |
| 621 |
if ( is_aa_or_aa_ambig_extended(c) ) // includes other ambigs (!'X', !'x' ,!'?') |
| 622 |
{ |
| 623 |
addCharToData(c); |
| 624 |
++number_of_AAs; // Counts ambigs but not X or ? |
| 625 |
} |
| 626 |
else if ( isspace(c) ) |
| 627 |
{ |
| 628 |
--number_raw_original_length; // we do not count spaces |
| 629 |
} // Spaces are removed |
| 630 |
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed, |
| 631 |
// but their positions are stored in order to able to reconstruct the original coordinates. |
| 632 |
{ // I don't really know why we tolerate '*' ? They occur in protein sequences as stop codons. |
| 633 |
|
| 634 |
if (c=='-') |
| 635 |
{ |
| 636 |
if ( gaps_to_ambig_bool) |
| 637 |
{ |
| 638 |
addCharToData(local_general_ambig); |
| 639 |
if (local_general_ambig == '?' ) |
| 640 |
++number_of_questionmarks; |
| 641 |
else |
| 642 |
++number_of_Xs; |
| 643 |
} |
| 644 |
else if ( removegaps_bool ) |
| 645 |
{ |
| 646 |
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence, |
| 647 |
// we need to keep track of the positions we cut out. |
| 648 |
gap_koords.push_back(pos); |
| 649 |
++number_of_gaps; |
| 650 |
} |
| 651 |
else |
| 652 |
{ |
| 653 |
addCharToData('-'); |
| 654 |
++number_of_gaps; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
/* else if (removestars_bool) */ |
| 659 |
/* { */ |
| 660 |
/* gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence, */ |
| 661 |
/* // we need to keep track of the positions we cut out. */ |
| 662 |
/* star_koords.push_back(pos); */ |
| 663 |
/* ++number_of_stars; */ |
| 664 |
/* } */ |
| 665 |
// c = infile.getchar(); |
| 666 |
// ++pos; |
| 667 |
} |
| 668 |
else // Unknown symbol |
| 669 |
{ |
| 670 |
// we do not count this -just to be consistent before we bail out |
| 671 |
--number_raw_original_length; |
| 672 |
infile.clear(CFile::__fail_reason2 | CFile::__fail_flag); |
| 673 |
break; |
| 674 |
} |
| 675 |
c = infile.getchar(); |
| 676 |
c_upper = toupper(c); |
| 677 |
++pos; |
| 678 |
} |
| 679 |
pos_in_seq = pos; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
void getLineOfDNA_toupper_removegaps (CFile& infile, unsigned &pos_in_seq) |
| 684 |
{ |
| 685 |
register char c; |
| 686 |
unsigned pos = pos_in_seq; |
| 687 |
register char local_general_ambig = toupper(general_ambig); |
| 688 |
|
| 689 |
// ++pos; |
| 690 |
c = infile.getchar(); |
| 691 |
while (!infile.fail() && c != '\n') |
| 692 |
{ |
| 693 |
// if the char is a space or something we do not count, we subtract this further below |
| 694 |
++number_raw_original_length; |
| 695 |
c = toupper(c); |
| 696 |
|
| 697 |
if ( c == 'A' || c == 'T' || c == 'G' || c == 'C') |
| 698 |
{ |
| 699 |
addCharToData(c); |
| 700 |
++number_of_DNARNA_bases; |
| 701 |
} |
| 702 |
else if ( c == 'N' || c == '?') |
| 703 |
{ |
| 704 |
addCharToData(local_general_ambig); |
| 705 |
if (general_ambig == 'N') |
| 706 |
++number_of_Ns; |
| 707 |
else |
| 708 |
++number_of_questionmarks; |
| 709 |
++number_of_DNARNA_amgibs; // includes N and '?' |
| 710 |
} |
| 711 |
else if ( c == 'U') |
| 712 |
{ |
| 713 |
addCharToData('U'); |
| 714 |
++number_of_DNARNA_bases; |
| 715 |
++number_of_Us; |
| 716 |
} |
| 717 |
else if ( c == 'R' || c == 'Y' || c == 'K' || c == 'M' || |
| 718 |
c == 'S' || c == 'W' || c == 'B' || c == 'D' || |
| 719 |
c == 'H' || c == 'V' ) |
| 720 |
{ |
| 721 |
addCharToData(c); |
| 722 |
++number_of_DNARNA_amgibs; |
| 723 |
} |
| 724 |
else if ( isspace(c) ) // Spaces are removed |
| 725 |
{ |
| 726 |
--number_raw_original_length; // we do not count spaces |
| 727 |
} |
| 728 |
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed, but their positions are stored in order to reconstruct the original coordinates. |
| 729 |
{ // I don't really know why we tolerate '*' ? |
| 730 |
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence, |
| 731 |
// we need to keep track of the positions we cut out. |
| 732 |
if (c=='-') |
| 733 |
{ |
| 734 |
gap_koords.push_back(pos); |
| 735 |
++number_of_gaps; |
| 736 |
} |
| 737 |
else |
| 738 |
{ |
| 739 |
star_koords.push_back(pos); |
| 740 |
++number_of_stars; |
| 741 |
} |
| 742 |
// c = infile.getchar(); |
| 743 |
// ++pos; |
| 744 |
} |
| 745 |
else |
| 746 |
{ |
| 747 |
// Something went wrong -> the last reading event from the file failed |
| 748 |
// -> we set the fail flag, this informs the user that something went wrong |
| 749 |
// We do not count this - just to be consistent before we bail out |
| 750 |
--number_raw_original_length; |
| 751 |
infile.clear(CFile::__fail_reason1 | CFile::__fail_flag); |
| 752 |
break; |
| 753 |
} |
| 754 |
c = infile.getchar(); |
| 755 |
++pos; |
| 756 |
} |
| 757 |
pos_in_seq = pos; |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
void getLineOfProtein_toupper_removegaps (CFile& infile, unsigned &pos_in_seq) |
| 763 |
{ |
| 764 |
register char c; |
| 765 |
unsigned pos = pos_in_seq; |
| 766 |
|
| 767 |
// ++pos; |
| 768 |
c = infile.getchar(); |
| 769 |
char local_general_ambig = toupper(general_ambig); |
| 770 |
|
| 771 |
while (!infile.fail() && c != '\n') |
| 772 |
{ |
| 773 |
// if the char is a space or something we do not count, we subtract this further below |
| 774 |
++number_raw_original_length; |
| 775 |
c = toupper(c); |
| 776 |
|
| 777 |
if (c == 'X' || c=='?') |
| 778 |
{ |
| 779 |
addCharToData(local_general_ambig); |
| 780 |
if (local_general_ambig == 'X' ) |
| 781 |
++number_of_Xs; |
| 782 |
else |
| 783 |
++number_of_questionmarks; |
| 784 |
} |
| 785 |
else if (is_aa_or_aa_ambig_extended(c) ) |
| 786 |
{ |
| 787 |
addCharToData(c); |
| 788 |
++number_of_AAs; |
| 789 |
} |
| 790 |
else if ( isspace(c) )// Spaces are removed |
| 791 |
{ |
| 792 |
--number_raw_original_length; // we do not count spaces |
| 793 |
} |
| 794 |
else if ( c=='-' || c=='*') // What do we do with gaps and *s - Here they are removed, but their positions are stored in order to reconstruct the original coordinates. |
| 795 |
{ // I don't really know why we tolerate '*' ? |
| 796 |
gap_and_star_koords.push_back(pos); // If we want to reconstruct the true position in the sequence, |
| 797 |
// we need to keep track of the positions we cut out. |
| 798 |
if (c=='-') |
| 799 |
{ |
| 800 |
gap_koords.push_back(pos); |
| 801 |
++number_of_gaps; |
| 802 |
} |
| 803 |
else |
| 804 |
{ |
| 805 |
star_koords.push_back(pos); |
| 806 |
++number_of_stars; |
| 807 |
} |
| 808 |
} |
| 809 |
else |
| 810 |
{ |
| 811 |
// we do not count this -just to be consistent before we bail out |
| 812 |
--number_raw_original_length; |
| 813 |
|
| 814 |
infile.clear(CFile::__fail_reason2 | CFile::__fail_flag); |
| 815 |
break; |
| 816 |
} |
| 817 |
c = infile.getchar(); |
| 818 |
++pos; |
| 819 |
} |
| 820 |
pos_in_seq = pos; |
| 821 |
} |
| 822 |
|
| 823 |
void getLineOfDNA_toupper_ambig2N_gaps2ambig (CFile& infile) |
| 824 |
{ |
| 825 |
register char c; |
| 826 |
|
| 827 |
c = infile.getchar(); |
| 828 |
while (!infile.fail() && c != '\n') |
| 829 |
{ |
| 830 |
// if the char is a space or something we do not count, we subtract this further below |
| 831 |
++number_raw_original_length; |
| 832 |
|
| 833 |
c = toupper(c); |
| 834 |
|
| 835 |
if ( c == 'A' || c == 'T' || c == 'G' || c == 'C') |
| 836 |
{ |
| 837 |
addCharToData(c); |
| 838 |
++number_of_DNARNA_bases; |
| 839 |
} |
| 840 |
else if ( c == 'N' || c == '?') |
| 841 |
{ |
| 842 |
addCharToData(general_ambig); |
| 843 |
if (general_ambig == 'N') |
| 844 |
++number_of_Ns; |
| 845 |
else |
| 846 |
++number_of_questionmarks; |
| 847 |
++number_of_DNARNA_amgibs; |
| 848 |
} |
| 849 |
else if ( c == 'U') |
| 850 |
{ |
| 851 |
addCharToData('U'); |
| 852 |
++number_of_DNARNA_bases; |
| 853 |
++number_of_Us; |
| 854 |
} |
| 855 |
else if ( c == 'R' || c == 'Y' || c == 'K' || c == 'M' || |
| 856 |
c == 'S' || c == 'W' || c == 'B' || c == 'D' || |
| 857 |
c == 'H' || c == 'V' ) |
| 858 |
{ |
| 859 |
// Convert all ambiguity codes to N's |
| 860 |
addCharToData(general_ambig); |
| 861 |
if (general_ambig == 'N') |
| 862 |
++number_of_Ns; |
| 863 |
else |
| 864 |
++number_of_questionmarks; |
| 865 |
++number_of_DNARNA_amgibs; |
| 866 |
} |
| 867 |
else if (c=='-') |
| 868 |
{ |
| 869 |
addCharToData(general_ambig); |
| 870 |
if (general_ambig == 'N') |
| 871 |
++number_of_Ns; |
| 872 |
else |
| 873 |
++number_of_questionmarks; |
| 874 |
++number_of_DNARNA_amgibs; |
| 875 |
} |
| 876 |
else if ( isspace(c) ) // We remove white spaces. |
| 877 |
{ // I don't really know why we tolerate '*' ? |
| 878 |
--number_raw_original_length; // we do not count spaces |
| 879 |
} |
| 880 |
else |
| 881 |
{ |
| 882 |
// we do not count this -just to be consistent before we bail out |
| 883 |
--number_raw_original_length; |
| 884 |
|
| 885 |
infile.clear(CFile::__fail_reason1 | CFile::__fail_flag); |
| 886 |
return; |
| 887 |
} |
| 888 |
c = infile.getchar(); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
public: |
| 897 |
CSequence_Mol(DataTypesEnum dt, char p_general_ambig = '\0'): |
| 898 |
type(dt), general_ambig(p_general_ambig), |
| 899 |
numbers_of_residues_computed(false), bitcode_recoded(false) |
| 900 |
{ |
| 901 |
data.reserve(10000); |
| 902 |
if (p_general_ambig == '\0') // Use the typical ambig character |
| 903 |
{ |
| 904 |
if (dt == dna) general_ambig = 'N'; |
| 905 |
else if (dt == protein) general_ambig = 'X'; |
| 906 |
else general_ambig = '?'; |
| 907 |
} |
| 908 |
reset_numbers_of_residues(); |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
CSequence_Mol(DataTypesEnum dt, faststring name, faststring::size_t len, char p_general_ambig = '\0'): |
| 913 |
full_name(name),type(dt), general_ambig(p_general_ambig), |
| 914 |
numbers_of_residues_computed(false), bitcode_recoded(false) |
| 915 |
{ |
| 916 |
if (len < faststring::npos) |
| 917 |
++len; |
| 918 |
data.reserve(len); |
| 919 |
|
| 920 |
find_shortname_and_description(); |
| 921 |
|
| 922 |
if (p_general_ambig == '\0') // Use the typical ambig character |
| 923 |
{ |
| 924 |
if (dt == dna) general_ambig = 'N'; |
| 925 |
else if (dt == protein) general_ambig = 'X'; |
| 926 |
else general_ambig = '?'; |
| 927 |
} |
| 928 |
reset_numbers_of_residues(); |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
//************************************************************************** |
| 933 |
// pos1 must be the first column starting with 0. |
| 934 |
// pos2 must be the index after the last column. |
| 935 |
// pos2-pos1 must be the number of bases that are copied to this sequence. |
| 936 |
//************************************************************************** |
| 937 |
CSequence_Mol(const CSequence_Mol& s, faststring::size_t pos1=0, faststring::size_t pos2 = faststring::npos): |
| 938 |
full_name(s.full_name), |
| 939 |
short_name(s.short_name), |
| 940 |
description(s.description), |
| 941 |
type(s.type), |
| 942 |
general_ambig(s.general_ambig), |
| 943 |
numbers_of_residues_computed(false), |
| 944 |
bitcode_recoded(s.bitcode_recoded) |
| 945 |
{ |
| 946 |
if (pos1 == 0 && pos2 >= s.length() ) |
| 947 |
data = s.data; |
| 948 |
else |
| 949 |
{ |
| 950 |
if (pos2 > s.length() ) |
| 951 |
pos2 = s.length(); |
| 952 |
if (pos1 >= pos2) |
| 953 |
{ |
| 954 |
return; |
| 955 |
} |
| 956 |
data.assign(s.data, pos1, pos2-pos1); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
// Should only be applied to dna sequences |
| 963 |
void convert_ambig2N_this_sequence() |
| 964 |
{ |
| 965 |
if ( !(type == dna || type == rna) ) |
| 966 |
return; |
| 967 |
|
| 968 |
// This is a DNA sequence: |
| 969 |
|
| 970 |
char * it = data.begin(); |
| 971 |
char * it_end = data.end(); |
| 972 |
|
| 973 |
while (it != it_end) |
| 974 |
{ |
| 975 |
char c = *it; |
| 976 |
char c_upper = toupper(c); |
| 977 |
|
| 978 |
if ( c_upper == 'R' || c_upper == 'Y' || c_upper == 'K' || c_upper == 'M' || |
| 979 |
c_upper == 'S' || c_upper == 'W' || c_upper == 'B' || c_upper == 'D' || |
| 980 |
c_upper == 'H' || c_upper == 'V' ) |
| 981 |
{ |
| 982 |
// Convert all ambiguity codes to N's |
| 983 |
*it = general_ambig; |
| 984 |
if (general_ambig == 'N' || general_ambig == 'n') |
| 985 |
++number_of_Ns; |
| 986 |
else |
| 987 |
++number_of_questionmarks; |
| 988 |
++number_of_DNARNA_amgibs; |
| 989 |
} |
| 990 |
++it; |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
bool has_only_gaps() |
| 995 |
{ |
| 996 |
char * it = data.begin(); |
| 997 |
char * it_end = data.end(); |
| 998 |
|
| 999 |
while (it != it_end) |
| 1000 |
{ |
| 1001 |
if (*it != '-') |
| 1002 |
return false; |
| 1003 |
++it; |
| 1004 |
} |
| 1005 |
return true; |
| 1006 |
} |
| 1007 |
|
| 1008 |
bool has_only_ambigs() |
| 1009 |
{ |
| 1010 |
char * it = data.begin(); |
| 1011 |
char * it_end = data.end(); |
| 1012 |
|
| 1013 |
if (type == dna || type == rna) |
| 1014 |
{ |
| 1015 |
while (it != it_end) |
| 1016 |
{ |
| 1017 |
if (!is_DNA_ambig(*it)) |
| 1018 |
return false; |
| 1019 |
++it; |
| 1020 |
} |
| 1021 |
return true; |
| 1022 |
} |
| 1023 |
else if (type == protein) |
| 1024 |
{ |
| 1025 |
while (it != it_end) |
| 1026 |
{ |
| 1027 |
if (!is_aa_ambig(*it)) |
| 1028 |
return false; |
| 1029 |
++it; |
| 1030 |
} |
| 1031 |
return true; |
| 1032 |
} |
| 1033 |
else // This result is undefined, since the data type is not specified. |
| 1034 |
{ |
| 1035 |
return false; |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
bool has_only_ambigs_or_gap() |
| 1040 |
{ |
| 1041 |
// std::cout << "Call to: has_only_ambigs_or_gap" << std::endl; |
| 1042 |
|
| 1043 |
char * it = data.begin(); |
| 1044 |
char * it_end = data.end(); |
| 1045 |
|
| 1046 |
if (type == dna || type == rna) |
| 1047 |
{ |
| 1048 |
// std::cout << "Datatype is dna or rna" << std::endl; |
| 1049 |
|
| 1050 |
while (it != it_end) |
| 1051 |
{ |
| 1052 |
// std::cout << "Checking " << *it << std::endl; |
| 1053 |
if ( !(is_DNA_ambig(*it) || *it == '-') ) |
| 1054 |
{ |
| 1055 |
// std::cout << "Returning false." << std::endl; |
| 1056 |
return false; |
| 1057 |
} |
| 1058 |
++it; |
| 1059 |
} |
| 1060 |
// std::cout << "Returning true." << std::endl; |
| 1061 |
return true; |
| 1062 |
} |
| 1063 |
else if (type == protein) |
| 1064 |
{ |
| 1065 |
while (it != it_end) |
| 1066 |
{ |
| 1067 |
if ( !(is_aa_ambig(*it) || *it == '-') ) |
| 1068 |
return false; |
| 1069 |
++it; |
| 1070 |
} |
| 1071 |
return true; |
| 1072 |
} |
| 1073 |
else // This result is undefined, since the data type is not specified. |
| 1074 |
{ |
| 1075 |
// std::cout << "Returning default false for unspecified type." << std::endl; |
| 1076 |
return false; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
|
| 1081 |
faststring::size_t length() const |
| 1082 |
{ |
| 1083 |
return data.size(); |
| 1084 |
} |
| 1085 |
|
| 1086 |
DataTypesEnum get_datatype() |
| 1087 |
{ |
| 1088 |
return type; |
| 1089 |
} |
| 1090 |
|
| 1091 |
faststring type_as_string() |
| 1092 |
{ |
| 1093 |
if (type == dna) |
| 1094 |
return faststring("DNA"); |
| 1095 |
else if (type == rna) |
| 1096 |
return faststring("RNA"); |
| 1097 |
else if (type == protein) |
| 1098 |
return faststring("Protein"); |
| 1099 |
return "Unknown"; |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
void set_taxon_and_sequence(const faststring& taxonName, |
| 1104 |
const faststring& seqData, DataTypesEnum datatype=auto_detect_type) |
| 1105 |
{ |
| 1106 |
/* unsigned pos; */ |
| 1107 |
|
| 1108 |
if (numbers_of_residues_computed) |
| 1109 |
{ |
| 1110 |
reset_numbers_of_residues(); |
| 1111 |
} |
| 1112 |
|
| 1113 |
full_name = taxonName; |
| 1114 |
|
| 1115 |
find_shortname_and_description(); |
| 1116 |
/* if ( (pos = taxonName.find(' '))!=faststring::npos) */ |
| 1117 |
/* { */ |
| 1118 |
/* short_name = taxonName.substr(0,pos+1); */ |
| 1119 |
/* description = taxonName.substr(pos+1); */ |
| 1120 |
/* } */ |
| 1121 |
/* else */ |
| 1122 |
/* { */ |
| 1123 |
/* short_name = taxonName; */ |
| 1124 |
/* description.clear(); */ |
| 1125 |
/* } */ |
| 1126 |
data = seqData; |
| 1127 |
|
| 1128 |
// |
| 1129 |
// toupper_this_sequence(); |
| 1130 |
|
| 1131 |
// Should not be necessary: |
| 1132 |
// compute_numbers_of_residues(); |
| 1133 |
if (datatype == auto_detect_type || datatype == unknown) |
| 1134 |
{ |
| 1135 |
auto_detect_datatype(); |
| 1136 |
// std::cerr << "Data type: " << type_as_string() << std::endl; |
| 1137 |
// std::cerr << "General ambig: " << general_ambig << std::endl; |
| 1138 |
if (auto_detect_ambig_detect_conflict() ) |
| 1139 |
{ |
| 1140 |
std::cerr << "Conflict in ambiguity code used in imported sequence." << std::endl; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
// std::cerr << "General ambig: " << general_ambig << std::endl; |
| 1144 |
|
| 1145 |
change_ambigs(); |
| 1146 |
// remove_gaps_stars_this_sequence(true, true); |
| 1147 |
|
| 1148 |
// general_ambig = ambig; |
| 1149 |
// type = t; |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
void set_taxon_and_random_DNA_sequence(const faststring& taxonName, |
| 1154 |
unsigned len, |
| 1155 |
double piA, double piC, double piG) |
| 1156 |
{ |
| 1157 |
size_t pos; |
| 1158 |
|
| 1159 |
type = dna; |
| 1160 |
general_ambig = '?'; |
| 1161 |
|
| 1162 |
data.clear(); |
| 1163 |
data.reserve(len+1); |
| 1164 |
|
| 1165 |
reset_numbers_of_residues(); |
| 1166 |
|
| 1167 |
full_name = taxonName; |
| 1168 |
if ( (pos = taxonName.find(' '))!=faststring::npos) |
| 1169 |
{ |
| 1170 |
short_name = taxonName.substr(0,pos+1); |
| 1171 |
description = taxonName.substr(pos+1); |
| 1172 |
} |
| 1173 |
else |
| 1174 |
{ |
| 1175 |
short_name = taxonName; |
| 1176 |
description.clear(); |
| 1177 |
} |
| 1178 |
|
| 1179 |
// Compute random sequence: |
| 1180 |
unsigned i; |
| 1181 |
double z; |
| 1182 |
|
| 1183 |
double bA = piA; |
| 1184 |
double bC = bA+piC; |
| 1185 |
double bG = bC+piG; |
| 1186 |
|
| 1187 |
for (i=0; i<len; ++i) |
| 1188 |
{ |
| 1189 |
z = rand()/(RAND_MAX+1.0); |
| 1190 |
if ( z < bA ) |
| 1191 |
data.push_back('A'); |
| 1192 |
else if ( z < bC ) |
| 1193 |
data.push_back('C'); |
| 1194 |
else if ( z < bG ) |
| 1195 |
data.push_back('G'); |
| 1196 |
else |
| 1197 |
data.push_back('T'); |
| 1198 |
} |
| 1199 |
|
| 1200 |
compute_numbers_of_residues(); |
| 1201 |
|
| 1202 |
// std::cerr << "Data type: " << type_as_string() << std::endl; |
| 1203 |
// std::cerr << "General ambig: " << general_ambig << std::endl; |
| 1204 |
// std::cerr << "General ambig: " << general_ambig << std::endl; |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
char& operator[](unsigned pos) |
| 1209 |
{ |
| 1210 |
return data[pos]; |
| 1211 |
} |
| 1212 |
|
| 1213 |
char get_pos(unsigned pos) |
| 1214 |
{ |
| 1215 |
return data[pos]; |
| 1216 |
} |
| 1217 |
|
| 1218 |
/* void set_pos(unsigned pos, char c) */ |
| 1219 |
/* { */ |
| 1220 |
/* data[pos] = c; */ |
| 1221 |
/* } */ |
| 1222 |
|
| 1223 |
bool is_bitcoderecoded() |
| 1224 |
{ |
| 1225 |
return bitcode_recoded; |
| 1226 |
} |
| 1227 |
|
| 1228 |
unsigned full_original_length() const |
| 1229 |
{ |
| 1230 |
// Remember that some numbers include others: |
| 1231 |
// number_of_DNARNA_amgibs includes 'Ns' and '?', so they are not added again: |
| 1232 |
// number_of_DNARNA_bases includes 'Us' |
| 1233 |
if (type == dna || type == rna ) |
| 1234 |
{ |
| 1235 |
return number_of_DNARNA_bases + number_of_DNARNA_amgibs + number_of_gaps + number_of_stars; |
| 1236 |
} |
| 1237 |
else if (type == protein) |
| 1238 |
{ |
| 1239 |
return number_of_AAs + number_of_gaps + number_of_stars |
| 1240 |
+ number_of_Xs + number_of_questionmarks; |
| 1241 |
} |
| 1242 |
else |
| 1243 |
{ |
| 1244 |
return number_raw_original_length; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
unsigned length_DNARNA_bases() const |
| 1249 |
{ |
| 1250 |
return number_of_DNARNA_bases; |
| 1251 |
} |
| 1252 |
|
| 1253 |
const char* getName() // const |
| 1254 |
{ |
| 1255 |
return short_name.c_str(); |
| 1256 |
} |
| 1257 |
|
| 1258 |
const faststring& getName_faststring() const |
| 1259 |
{ |
| 1260 |
return short_name; |
| 1261 |
} |
| 1262 |
|
| 1263 |
const faststring& getSeq_faststring() const |
| 1264 |
{ |
| 1265 |
return data; |
| 1266 |
} |
| 1267 |
|
| 1268 |
faststring getPhylipName(int mlen) |
| 1269 |
{ |
| 1270 |
faststring str(short_name); |
| 1271 |
|
| 1272 |
str.resize(mlen, ' '); |
| 1273 |
return str; |
| 1274 |
} |
| 1275 |
|
| 1276 |
const char* getFullName() // const |
| 1277 |
{ |
| 1278 |
return full_name.c_str(); |
| 1279 |
} |
| 1280 |
|
| 1281 |
const faststring getFullName_faststring() const |
| 1282 |
{ |
| 1283 |
return full_name; |
| 1284 |
} |
| 1285 |
|
| 1286 |
const char* getDescription() // const |
| 1287 |
{ |
| 1288 |
return description.c_str(); |
| 1289 |
} |
| 1290 |
|
| 1291 |
const char* getSeqStr() |
| 1292 |
{ |
| 1293 |
return data.c_str(); |
| 1294 |
} |
| 1295 |
|
| 1296 |
faststring getPartialSeq(size_t pos, size_t n) |
| 1297 |
{ |
| 1298 |
return data.substr(pos, n); |
| 1299 |
} |
| 1300 |
|
| 1301 |
const char* getSeqBegin() |
| 1302 |
{ |
| 1303 |
return data.begin(); |
| 1304 |
} |
| 1305 |
|
| 1306 |
const char* getSeqEnd() |
| 1307 |
{ |
| 1308 |
return data.end(); |
| 1309 |
} |
| 1310 |
|
| 1311 |
const char* getSeqRBegin() |
| 1312 |
{ |
| 1313 |
return data.rbegin(); |
| 1314 |
} |
| 1315 |
|
| 1316 |
const char*getSeqREnd() |
| 1317 |
{ |
| 1318 |
return data.rend(); |
| 1319 |
} |
| 1320 |
|
| 1321 |
void reset_numbers_of_residues() |
| 1322 |
{ |
| 1323 |
numbers_of_residues_computed = false; |
| 1324 |
|
| 1325 |
number_of_AAs = 0; |
| 1326 |
number_of_DNARNA_bases = 0; |
| 1327 |
number_of_DNARNA_amgibs = 0; |
| 1328 |
number_of_Us = 0; |
| 1329 |
number_of_gaps = 0; |
| 1330 |
number_of_stars = 0; |
| 1331 |
number_of_questionmarks = 0; |
| 1332 |
number_of_Xs = 0; |
| 1333 |
number_of_Ns = 0; |
| 1334 |
|
| 1335 |
number_CG = 0; |
| 1336 |
|
| 1337 |
number_of_questionmarks = 0; |
| 1338 |
number_raw_original_length = 0; |
| 1339 |
} |
| 1340 |
|
| 1341 |
void compute_numbers_of_residues() |
| 1342 |
{ |
| 1343 |
const char *it=data.begin(), *it_end=data.end(); |
| 1344 |
|
| 1345 |
if (numbers_of_residues_computed) |
| 1346 |
{ |
| 1347 |
std::cerr << "Request for computing number of residues - even though they have already been determined" << std::endl; |
| 1348 |
return; |
| 1349 |
} |
| 1350 |
|
| 1351 |
reset_numbers_of_residues(); |
| 1352 |
|
| 1353 |
for (;it<it_end; ++it) |
| 1354 |
{ |
| 1355 |
char c = *it; |
| 1356 |
|
| 1357 |
if (is_DNA_or_RNA_base(c)) |
| 1358 |
{ |
| 1359 |
if (c == 'c' || c == 'C' || c == 'G' || c == 'g' ) |
| 1360 |
++number_CG; |
| 1361 |
++number_of_DNARNA_bases; |
| 1362 |
} |
| 1363 |
|
| 1364 |
if (is_DNA_ambig(c)) |
| 1365 |
++number_of_DNARNA_amgibs; |
| 1366 |
|
| 1367 |
if (is_aa_or_aa_ambig_extended(c) && c!= 'X' && c != '?') |
| 1368 |
++number_of_AAs; |
| 1369 |
|
| 1370 |
if (c == 'N') |
| 1371 |
{ |
| 1372 |
++number_of_Ns; |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (c == '-') |
| 1376 |
{ |
| 1377 |
++number_of_gaps; |
| 1378 |
} |
| 1379 |
|
| 1380 |
if (c == '*') |
| 1381 |
{ |
| 1382 |
++number_of_stars; |
| 1383 |
} |
| 1384 |
|
| 1385 |
if (c == 'X') |
| 1386 |
{ |
| 1387 |
++number_of_Xs; |
| 1388 |
} |
| 1389 |
|
| 1390 |
if (c == '?') |
| 1391 |
{ |
| 1392 |
++number_of_questionmarks; |
| 1393 |
} |
| 1394 |
|
| 1395 |
if (c == 'U' || c == 'u') |
| 1396 |
{ |
| 1397 |
++number_of_Us; |
| 1398 |
} |
| 1399 |
|
| 1400 |
numbers_of_residues_computed = true; |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
void auto_detect_datatype() |
| 1405 |
{ |
| 1406 |
if (!numbers_of_residues_computed) |
| 1407 |
compute_numbers_of_residues(); |
| 1408 |
|
| 1409 |
unsigned aa_stuff = number_of_AAs + number_of_Xs + number_of_stars; |
| 1410 |
unsigned DNARNA_stuff = number_of_DNARNA_bases + number_of_DNARNA_amgibs; |
| 1411 |
|
| 1412 |
// reine DNA: 100 > 0.25 * 100 - false - DNA |
| 1413 |
|
| 1414 |
if (aa_stuff < 0.1*data.length() && DNARNA_stuff < 0.1*data.length() ) |
| 1415 |
type = unknown; |
| 1416 |
else if (aa_stuff > 0.25*DNARNA_stuff) |
| 1417 |
{ |
| 1418 |
if (number_of_Us > 0) |
| 1419 |
type = rna; |
| 1420 |
else |
| 1421 |
type = dna; |
| 1422 |
} |
| 1423 |
else |
| 1424 |
{ |
| 1425 |
type = protein; |
| 1426 |
} |
| 1427 |
} |
| 1428 |
|
| 1429 |
bool auto_detect_ambig_detect_conflict() // Return true in case of ambig conflict, else false |
| 1430 |
{ |
| 1431 |
if (!numbers_of_residues_computed) |
| 1432 |
compute_numbers_of_residues(); |
| 1433 |
|
| 1434 |
if (type == protein) |
| 1435 |
general_ambig = 'X'; |
| 1436 |
else general_ambig = 'N'; |
| 1437 |
|
| 1438 |
if (number_of_questionmarks > 0) |
| 1439 |
general_ambig = '?'; |
| 1440 |
|
| 1441 |
// Determine conlict: |
| 1442 |
return ( number_of_questionmarks > 0 && |
| 1443 |
(number_of_Xs > 0 || ((type==dna || type==rna) && number_of_Ns > 0))); |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
void assignSeq(faststring &newseq, processing_flag flag=(processing_flag)0) |
| 1448 |
{ |
| 1449 |
data.assign(newseq); |
| 1450 |
numbers_of_residues_computed = false; |
| 1451 |
|
| 1452 |
if (flag & compute_numbers_of_residues_flag) |
| 1453 |
compute_numbers_of_residues(); |
| 1454 |
else |
| 1455 |
{ |
| 1456 |
reset_numbers_of_residues(); |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
void maskSeq(const char *start, const char *end, char c) |
| 1461 |
{ |
| 1462 |
char *pos = const_cast<char*>(start); |
| 1463 |
while (pos != end) |
| 1464 |
{ |
| 1465 |
*pos = c; |
| 1466 |
++pos; |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
void maskSeq_tolower(const char *start, const char *end) |
| 1471 |
{ |
| 1472 |
char *pos = const_cast<char*>(start); |
| 1473 |
while (pos != end) |
| 1474 |
{ |
| 1475 |
*pos = tolower(*pos); |
| 1476 |
++pos; |
| 1477 |
} |
| 1478 |
} |
| 1479 |
|
| 1480 |
faststring::size_t find_pos_first_symbol_not_in_alphabet(const char * alphabet=NULL) |
| 1481 |
{ |
| 1482 |
if (alphabet == NULL) |
| 1483 |
{ |
| 1484 |
if (type == dna) |
| 1485 |
{ |
| 1486 |
alphabet = DNARNA_symbols; |
| 1487 |
} |
| 1488 |
else if (type == protein) |
| 1489 |
{ |
| 1490 |
alphabet = aa_symbols; |
| 1491 |
} |
| 1492 |
else |
| 1493 |
{ |
| 1494 |
return faststring::npos; |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 1498 |
return data.find_first_not_of(alphabet); |
| 1499 |
} |
| 1500 |
|
| 1501 |
// Fills str with the sequence data, that contains gaps and stars, that had been removed |
| 1502 |
// temporarily. |
| 1503 |
void getSequence_fill_in_gaps_and_stars(faststring &str) |
| 1504 |
{ |
| 1505 |
if (bitcode_recoded) |
| 1506 |
backrecode_bitcode_sequence(); |
| 1507 |
|
| 1508 |
unsigned upos = 0; |
| 1509 |
unsigned pos_in_gap_and_star_koords=0; |
| 1510 |
unsigned pos_in_gap_koords=0; |
| 1511 |
unsigned pos_in_star_koords=0; |
| 1512 |
|
| 1513 |
const char *pos = data.begin(); |
| 1514 |
// const char *end = data.end(); |
| 1515 |
|
| 1516 |
unsigned N = gap_and_star_koords.size(); |
| 1517 |
unsigned Ngaps = gap_koords.size(); |
| 1518 |
unsigned Nstars = star_koords.size(); |
| 1519 |
|
| 1520 |
// unsigned left_this_line = char_per_line; |
| 1521 |
|
| 1522 |
str.clear(); |
| 1523 |
|
| 1524 |
if (N == 0 && Ngaps == 0 && Nstars == 0) |
| 1525 |
{ |
| 1526 |
str = data; |
| 1527 |
return; |
| 1528 |
} |
| 1529 |
|
| 1530 |
|
| 1531 |
unsigned N_all_char = data.size()+N; |
| 1532 |
str.reserve(N_all_char); |
| 1533 |
|
| 1534 |
while (upos < N_all_char) |
| 1535 |
{ |
| 1536 |
if (pos_in_gap_and_star_koords == N) |
| 1537 |
break; |
| 1538 |
|
| 1539 |
if ( gap_and_star_koords.get_unckecked(pos_in_gap_and_star_koords) == upos ) |
| 1540 |
{ |
| 1541 |
if ( pos_in_gap_koords < Ngaps && gap_koords.get_unckecked(pos_in_gap_koords) == upos ) // this is a gap |
| 1542 |
{ |
| 1543 |
str.push_back('-'); |
| 1544 |
++pos_in_gap_koords; |
| 1545 |
++pos_in_gap_and_star_koords; |
| 1546 |
} |
| 1547 |
else |
| 1548 |
if ( pos_in_star_koords < Nstars && star_koords.get_unckecked(pos_in_star_koords) == upos ) // this is a gap |
| 1549 |
{ |
| 1550 |
str.push_back('*'); |
| 1551 |
++pos_in_star_koords; |
| 1552 |
++pos_in_gap_and_star_koords; |
| 1553 |
} |
| 1554 |
} |
| 1555 |
else |
| 1556 |
{ |
| 1557 |
str.push_back(*pos); |
| 1558 |
++pos; |
| 1559 |
} |
| 1560 |
++upos; |
| 1561 |
// --left_this_line; |
| 1562 |
|
| 1563 |
/* if (left_this_line == 0) */ |
| 1564 |
/* { */ |
| 1565 |
/* str.push_back('\n'); */ |
| 1566 |
/* left_this_line = char_per_line; */ |
| 1567 |
/* } */ |
| 1568 |
} |
| 1569 |
|
| 1570 |
// copy rest without gaps and stars |
| 1571 |
while (upos < N_all_char) |
| 1572 |
{ |
| 1573 |
str.push_back(*pos); |
| 1574 |
++pos; |
| 1575 |
++upos; |
| 1576 |
// --left_this_line; |
| 1577 |
|
| 1578 |
/* if (left_this_line == 0) */ |
| 1579 |
/* { */ |
| 1580 |
/* str.push_back('\n'); */ |
| 1581 |
|
| 1582 |
/* left_this_line = char_per_line; */ |
| 1583 |
/* } */ |
| 1584 |
} |
| 1585 |
/* if (left_this_line != char_per_line) // We have already written a partial line */ |
| 1586 |
/* str.push_back('\n'); */ |
| 1587 |
} |
| 1588 |
|
| 1589 |
// Replace part of the sequence. pos1 and pos2 are in the destination sequence. |
| 1590 |
// - erases region from pos1 to pos2 |
| 1591 |
// pos1 is the first column pos2-pos1 is the number of bases that are removed, |
| 1592 |
// i.e. pos2 is the column after the last column that is to be removed. |
| 1593 |
// - insert s with full length. |
| 1594 |
void replace_part_of_sequence(CSequence_Mol &s, unsigned pos1, unsigned pos2) |
| 1595 |
{ |
| 1596 |
if (type != s.type) |
| 1597 |
{ |
| 1598 |
std::cerr << "Critical warning: Replacing part of sequence with different data type." << std::endl; |
| 1599 |
} |
| 1600 |
data.replace(pos1, pos2-pos1, s.data); |
| 1601 |
} |
| 1602 |
|
| 1603 |
void append_sequence(CSequence_Mol &s) |
| 1604 |
{ |
| 1605 |
if (type != s.type) |
| 1606 |
{ |
| 1607 |
std::cerr << "Critical warning: Appending sequence with different data type." << std::endl; |
| 1608 |
} |
| 1609 |
data.append(s.data); |
| 1610 |
} |
| 1611 |
|
| 1612 |
void append_residue_unchecked(char c) |
| 1613 |
{ |
| 1614 |
data.push_back(c); |
| 1615 |
} |
| 1616 |
|
| 1617 |
void writeSequence_fill_in_gaps_and_stars(FILE *of, unsigned char_per_line=50) |
| 1618 |
{ |
| 1619 |
if (bitcode_recoded) |
| 1620 |
backrecode_bitcode_sequence(); |
| 1621 |
|
| 1622 |
unsigned upos = 0; |
| 1623 |
unsigned pos_in_gap_and_star_koords=0; |
| 1624 |
unsigned pos_in_gap_koords=0; |
| 1625 |
unsigned pos_in_star_koords=0; |
| 1626 |
|
| 1627 |
const char *pos = data.begin(); |
| 1628 |
// const char *end = data.end(); |
| 1629 |
|
| 1630 |
unsigned N = gap_and_star_koords.size(); |
| 1631 |
unsigned Ngaps = gap_koords.size(); |
| 1632 |
unsigned Nstars = star_koords.size(); |
| 1633 |
|
| 1634 |
unsigned left_this_line = char_per_line; |
| 1635 |
|
| 1636 |
unsigned N_all_char = data.size()+N; |
| 1637 |
|
| 1638 |
fprintf(of, ">%s\n", full_name.c_str()); |
| 1639 |
|
| 1640 |
while (upos < N_all_char) |
| 1641 |
{ |
| 1642 |
if (pos_in_gap_and_star_koords == N) |
| 1643 |
break; |
| 1644 |
|
| 1645 |
if ( gap_and_star_koords.get_unckecked(pos_in_gap_and_star_koords) == upos ) |
| 1646 |
{ |
| 1647 |
if ( pos_in_gap_koords < Ngaps && gap_koords.get_unckecked(pos_in_gap_koords) == upos ) // this is a gap |
| 1648 |
{ |
| 1649 |
putc('-', of); |
| 1650 |
++pos_in_gap_koords; |
| 1651 |
++pos_in_gap_and_star_koords; |
| 1652 |
} |
| 1653 |
else |
| 1654 |
if ( pos_in_star_koords < Nstars && star_koords.get_unckecked(pos_in_star_koords) == upos ) // this is a gap |
| 1655 |
{ |
| 1656 |
putc('*', of); |
| 1657 |
++pos_in_star_koords; |
| 1658 |
++pos_in_gap_and_star_koords; |
| 1659 |
} |
| 1660 |
} |
| 1661 |
else |
| 1662 |
{ |
| 1663 |
putc(*pos, of); |
| 1664 |
++pos; |
| 1665 |
} |
| 1666 |
++upos; |
| 1667 |
--left_this_line; |
| 1668 |
|
| 1669 |
if (left_this_line == 0) |
| 1670 |
{ |
| 1671 |
putc('\n', of); |
| 1672 |
|
| 1673 |
left_this_line = char_per_line; |
| 1674 |
} |
| 1675 |
} |
| 1676 |
|
| 1677 |
while (upos < N_all_char) |
| 1678 |
{ |
| 1679 |
putc(*pos, of); |
| 1680 |
++pos; |
| 1681 |
++upos; |
| 1682 |
--left_this_line; |
| 1683 |
|
| 1684 |
if (left_this_line == 0) |
| 1685 |
{ |
| 1686 |
putc('\n', of); |
| 1687 |
|
| 1688 |
left_this_line = char_per_line; |
| 1689 |
} |
| 1690 |
} |
| 1691 |
if (left_this_line != char_per_line) // We have already written a partial line |
| 1692 |
putc('\n', of); |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
void writeSequence(FILE *of, unsigned char_per_line=50) //const |
| 1697 |
{ |
| 1698 |
writeSequence_fasta(of, 0, faststring::npos, full_name.c_str(), char_per_line); |
| 1699 |
} |
| 1700 |
|
| 1701 |
void writeSequence_fasta(FILE *of, unsigned char_per_line=50) //const |
| 1702 |
{ |
| 1703 |
writeSequence_fasta(of, 0, faststring::npos, full_name.c_str(), char_per_line); |
| 1704 |
} |
| 1705 |
|
| 1706 |
void writeSequence(FILE *of, |
| 1707 |
faststring::size_t pos_beg, |
| 1708 |
faststring::size_t pos_end, |
| 1709 |
const char *s_name, |
| 1710 |
unsigned char_per_line=50) const |
| 1711 |
{ |
| 1712 |
writeSequence_fasta(of, pos_beg, pos_end, s_name, char_per_line); |
| 1713 |
} |
| 1714 |
|
| 1715 |
|
| 1716 |
void writeSequence_fasta(FILE *of, |
| 1717 |
faststring::size_t pos_beg, // 0 based |
| 1718 |
faststring::size_t pos_end, // 0 based, stop index |
| 1719 |
const char *s_name, |
| 1720 |
faststring::size_t char_per_line=50) const |
| 1721 |
{ |
| 1722 |
const char *beg = data.begin() + pos_beg; |
| 1723 |
const char *end = data.begin() + pos_end; |
| 1724 |
const char *tmp_end; |
| 1725 |
|
| 1726 |
/* if (bitcode_recoded) */ |
| 1727 |
/* backrecode_bitcode_sequence(); */ |
| 1728 |
|
| 1729 |
if (pos_end == faststring::npos || end > data.end() ) |
| 1730 |
end = data.end(); |
| 1731 |
|
| 1732 |
if (char_per_line == faststring::npos) |
| 1733 |
char_per_line = data.length(); |
| 1734 |
|
| 1735 |
fprintf(of, ">%s\n", s_name); |
| 1736 |
|
| 1737 |
tmp_end = beg + char_per_line; |
| 1738 |
if (tmp_end > end) |
| 1739 |
tmp_end = end; |
| 1740 |
|
| 1741 |
while (beg < end) |
| 1742 |
{ |
| 1743 |
fprintf(of, "%.*s\n", int(tmp_end - beg), beg); |
| 1744 |
|
| 1745 |
beg = tmp_end; |
| 1746 |
tmp_end = beg + char_per_line; |
| 1747 |
if (tmp_end > end) |
| 1748 |
tmp_end = end; |
| 1749 |
} |
| 1750 |
} |
| 1751 |
|
| 1752 |
// Some programs do not accept too many Ns in a row. |
| 1753 |
// This function export sequences so that N-regions are collapsed |
| 1754 |
// to a maximum number of successive Ns. |
| 1755 |
// USE WITH CAUTION: This function changes your sequence data!!! |
| 1756 |
// The output is not identical to your original sequence data. |
| 1757 |
|
| 1758 |
|
| 1759 |
void writeSequence_fasta_collaps_NX_regions(FILE *of, |
| 1760 |
faststring::size_t char_per_line=50, |
| 1761 |
faststring::size_t maximum_number_of_successive_NXs=100) |
| 1762 |
{ |
| 1763 |
writeSequence_fasta_collaps_NX_regions(of, |
| 1764 |
0, |
| 1765 |
faststring::npos, |
| 1766 |
full_name.c_str(), |
| 1767 |
char_per_line, |
| 1768 |
maximum_number_of_successive_NXs); |
| 1769 |
} |
| 1770 |
|
| 1771 |
void writeSequence_fasta_collaps_NX_regions(FILE *of, |
| 1772 |
faststring::size_t pos_beg, // 0 based |
| 1773 |
faststring::size_t pos_end, // 0 based , stop index |
| 1774 |
const char *s_name, |
| 1775 |
faststring::size_t char_per_line=50, |
| 1776 |
faststring::size_t maximum_number_of_successive_NXs=100) const |
| 1777 |
{ |
| 1778 |
const char *beg = data.begin() + pos_beg; |
| 1779 |
const char *end = data.begin() + pos_end; |
| 1780 |
// const char *tmp_end; |
| 1781 |
|
| 1782 |
/* if (bitcode_recoded) */ |
| 1783 |
/* backrecode_bitcode_sequence(); */ |
| 1784 |
|
| 1785 |
if (pos_end == faststring::npos || end > data.end() ) |
| 1786 |
end = data.end(); |
| 1787 |
|
| 1788 |
fprintf(of, ">%s\n", s_name); |
| 1789 |
|
| 1790 |
faststring::size_t chars_in_this_line=0; |
| 1791 |
faststring::size_t tmp_succNs=0; |
| 1792 |
|
| 1793 |
while (beg != end) |
| 1794 |
{ |
| 1795 |
char c = *beg; |
| 1796 |
char c_toupper = toupper(c); |
| 1797 |
|
| 1798 |
if (c_toupper == 'N' || c_toupper == 'X') |
| 1799 |
{ |
| 1800 |
++tmp_succNs; |
| 1801 |
if (tmp_succNs <= maximum_number_of_successive_NXs) // print the N or X |
| 1802 |
{ |
| 1803 |
putc(c, of); |
| 1804 |
++chars_in_this_line; |
| 1805 |
|
| 1806 |
if (chars_in_this_line == char_per_line) |
| 1807 |
{ |
| 1808 |
putc('\n', of); |
| 1809 |
chars_in_this_line = 0; |
| 1810 |
} |
| 1811 |
} |
| 1812 |
// unwritten else: do not print the N or X |
| 1813 |
} |
| 1814 |
else |
| 1815 |
{ |
| 1816 |
putc(c, of); |
| 1817 |
tmp_succNs = 0; |
| 1818 |
++chars_in_this_line; |
| 1819 |
|
| 1820 |
if (chars_in_this_line == char_per_line) |
| 1821 |
{ |
| 1822 |
putc('\n', of); |
| 1823 |
chars_in_this_line = 0; |
| 1824 |
} |
| 1825 |
} |
| 1826 |
++beg; |
| 1827 |
} |
| 1828 |
if (chars_in_this_line != 0) |
| 1829 |
putc('\n', of); |
| 1830 |
} |
| 1831 |
|
| 1832 |
void writeSequence_partial_fasta(FILE *of, |
| 1833 |
faststring::size_t pos_beg, // 0 based |
| 1834 |
faststring::size_t pos_end, // 0 based , stop index |
| 1835 |
const char *s_name, |
| 1836 |
faststring::size_t char_per_line=50, |
| 1837 |
bool degap=true) const |
| 1838 |
{ |
| 1839 |
const char *beg = data.begin() + pos_beg; |
| 1840 |
const char *end = data.begin() + pos_end; |
| 1841 |
|
| 1842 |
/* if (bitcode_recoded) */ |
| 1843 |
/* backrecode_bitcode_sequence(); */ |
| 1844 |
|
| 1845 |
if (pos_end == faststring::npos || end > data.end() ) |
| 1846 |
end = data.end(); |
| 1847 |
|
| 1848 |
fprintf(of, ">%s\n", s_name); |
| 1849 |
|
| 1850 |
faststring::size_t chars_in_this_line=0; |
| 1851 |
|
| 1852 |
if (degap) |
| 1853 |
{ |
| 1854 |
while (beg != end) |
| 1855 |
{ |
| 1856 |
char c = *beg; |
| 1857 |
|
| 1858 |
if (c != '-') |
| 1859 |
{ |
| 1860 |
putc(c, of); |
| 1861 |
++chars_in_this_line; |
| 1862 |
|
| 1863 |
if (chars_in_this_line == char_per_line) |
| 1864 |
{ |
| 1865 |
putc('\n', of); |
| 1866 |
chars_in_this_line = 0; |
| 1867 |
} |
| 1868 |
} |
| 1869 |
// unwritten else: do not print - |
| 1870 |
++beg; |
| 1871 |
} |
| 1872 |
} |
| 1873 |
else |
| 1874 |
{ |
| 1875 |
while (beg != end) |
| 1876 |
{ |
| 1877 |
putc(*beg, of); |
| 1878 |
++chars_in_this_line; |
| 1879 |
|
| 1880 |
if (chars_in_this_line == char_per_line) |
| 1881 |
{ |
| 1882 |
putc('\n', of); |
| 1883 |
chars_in_this_line = 0; |
| 1884 |
} |
| 1885 |
++beg; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
} |
| 1889 |
|
| 1890 |
void writeSequence_fasta_filter_sites(FILE *of, |
| 1891 |
std::vector<unsigned> &site_filter, |
| 1892 |
int offset, // 0 if site_filter is 0 based, 1 if site_filter is 1 based |
| 1893 |
const char *s_name, |
| 1894 |
faststring::size_t char_per_line=50) const |
| 1895 |
{ |
| 1896 |
const char *beg = data.begin(); |
| 1897 |
const char *end = data.end(); |
| 1898 |
|
| 1899 |
/* if (bitcode_recoded) */ |
| 1900 |
/* backrecode_bitcode_sequence(); */ |
| 1901 |
|
| 1902 |
fprintf(of, ">%s\n", s_name); |
| 1903 |
|
| 1904 |
faststring::size_t chars_in_this_line=0; |
| 1905 |
int i=offset; |
| 1906 |
|
| 1907 |
{ |
| 1908 |
while (beg != end) |
| 1909 |
{ |
| 1910 |
if (site_filter[i] != 0) |
| 1911 |
{ |
| 1912 |
putc(*beg, of); |
| 1913 |
++chars_in_this_line; |
| 1914 |
|
| 1915 |
if (chars_in_this_line == char_per_line) |
| 1916 |
{ |
| 1917 |
putc('\n', of); |
| 1918 |
chars_in_this_line = 0; |
| 1919 |
} |
| 1920 |
} |
| 1921 |
// Move on in sequence and site_filter vector |
| 1922 |
++beg; |
| 1923 |
++i; |
| 1924 |
} |
| 1925 |
} |
| 1926 |
} |
| 1927 |
|
| 1928 |
void writeSequence_phylip(FILE *of, |
| 1929 |
faststring::size_t pos_beg, // 0 based |
| 1930 |
faststring::size_t pos_end, // 0 based, stop index. |
| 1931 |
const char *s_name) const |
| 1932 |
{ |
| 1933 |
/* if (bitcode_recoded) */ |
| 1934 |
/* backrecode_bitcode_sequence(); */ |
| 1935 |
|
| 1936 |
const char *beg = data.begin() + pos_beg; |
| 1937 |
// const char *end = data.begin() + pos_end; |
| 1938 |
|
| 1939 |
/* if (pos_end == faststring::npos || end > data.end() ) */ |
| 1940 |
/* end = data.end(); */ |
| 1941 |
|
| 1942 |
unsigned name_len = strlen(s_name); |
| 1943 |
if (name_len < 11) |
| 1944 |
fprintf(of, "%-10.10s", s_name); |
| 1945 |
/* else */ |
| 1946 |
/* { */ |
| 1947 |
/* char str[11]; */ |
| 1948 |
/* std::strncpy(str, s_name, 10); */ |
| 1949 |
/* str[10] = '\0'; */ |
| 1950 |
/* fprintf(of, "%s", str); */ |
| 1951 |
/* } */ |
| 1952 |
fprintf(of, "%.*s\n", pos_end - pos_beg, beg); |
| 1953 |
} |
| 1954 |
|
| 1955 |
// Expects 0 bases coordinates. The range starts at pos_beg and pos_end |
| 1956 |
// points to the base after the last position in the range. |
| 1957 |
// Clearly pos_end > pos_beg. In the empty range, pos_end = pos_beg+1. |
| 1958 |
|
| 1959 |
void writeSequence_revComp(FILE *of, |
| 1960 |
faststring::size_t pos_beg, // 0 based index |
| 1961 |
faststring::size_t pos_end, // 0 based index after the last pos. |
| 1962 |
const char *s_name, |
| 1963 |
faststring::size_t char_per_line=50) const |
| 1964 |
{ |
| 1965 |
/* if (bitcode_recoded) */ |
| 1966 |
/* backrecode_bitcode_sequence(); */ |
| 1967 |
|
| 1968 |
const char *beg = data.begin() + pos_beg; |
| 1969 |
const char *end = data.begin() + pos_end; |
| 1970 |
|
| 1971 |
faststring::size_t interleaved = char_per_line; |
| 1972 |
|
| 1973 |
fprintf(of, ">%s\n", s_name); |
| 1974 |
|
| 1975 |
|
| 1976 |
// Walking backwards, we shift the range: |
| 1977 |
--beg; // 0 based indices. beg is the rend index. Move beg to pos before "first pos". This is the stop index. |
| 1978 |
--end; // 0 based index. Move end to index at which we start to move. |
| 1979 |
|
| 1980 |
// Example: Print first 3 bases. |
| 1981 |
// Then: pos_beg=0, pos_end=3, 0 based, pos_end is index behind last pos in range. |
| 1982 |
// Thus: beg="-1+seq_beg" and end="2+seq_beg", so print 2, 1, 0, and stop at -1 |
| 1983 |
|
| 1984 |
for (; beg < end; --end) |
| 1985 |
{ |
| 1986 |
if (interleaved == 0) |
| 1987 |
{ |
| 1988 |
fputc('\n', of); |
| 1989 |
interleaved = char_per_line; |
| 1990 |
} |
| 1991 |
fputc( complementBase(*end), of ); // From CDnaString.h |
| 1992 |
--interleaved; |
| 1993 |
} |
| 1994 |
fputc('\n', of); |
| 1995 |
} |
| 1996 |
|
| 1997 |
unsigned baseComposition(std::vector<unsigned> &v) const |
| 1998 |
{ |
| 1999 |
if ( v.size() < 256 ) |
| 2000 |
{ |
| 2001 |
std::cerr << "Internal error. -24" << std::endl; |
| 2002 |
exit(0); |
| 2003 |
} |
| 2004 |
|
| 2005 |
char *pos = data.begin(); |
| 2006 |
char *end = data.end(); |
| 2007 |
|
| 2008 |
faststring::size_t len = end - pos; |
| 2009 |
|
| 2010 |
while (pos != end) |
| 2011 |
{ |
| 2012 |
++v[*pos]; |
| 2013 |
++pos; |
| 2014 |
} |
| 2015 |
return len; |
| 2016 |
} |
| 2017 |
|
| 2018 |
void sliding_window_CG_AT_content(std::vector<double> &v, unsigned window_size) |
| 2019 |
{ |
| 2020 |
char *end = data.end(); |
| 2021 |
|
| 2022 |
const char *range_beg = data.begin(); |
| 2023 |
const char *range_end; |
| 2024 |
|
| 2025 |
double CG_prop; |
| 2026 |
unsigned CG_num; |
| 2027 |
unsigned AT_num; |
| 2028 |
|
| 2029 |
while (1) |
| 2030 |
{ |
| 2031 |
range_end = range_beg + window_size; |
| 2032 |
|
| 2033 |
if (range_end > end) |
| 2034 |
range_end = end; |
| 2035 |
|
| 2036 |
if (range_end == range_beg) |
| 2037 |
break; |
| 2038 |
|
| 2039 |
CG_AT_content_in_region(range_beg, range_end, AT_num, CG_num); |
| 2040 |
|
| 2041 |
// std::cerr << AT_num << " " << CG_num << std::endl; |
| 2042 |
|
| 2043 |
if (CG_num == 0 && AT_num == 0) // e.g. if all chars are ambigs |
| 2044 |
CG_prop = -1; |
| 2045 |
else |
| 2046 |
CG_prop = (double)CG_num/(CG_num+AT_num); |
| 2047 |
|
| 2048 |
v.push_back(CG_prop); |
| 2049 |
|
| 2050 |
range_beg = range_end; |
| 2051 |
} |
| 2052 |
} |
| 2053 |
|
| 2054 |
|
| 2055 |
unsigned long long AsciiComposition_CpG_CpNpG(unsigned long long v[], unsigned long long &CpG, unsigned long long &CpNpG, unsigned long long &Nislands) const |
| 2056 |
{ |
| 2057 |
char *pos = data.begin(); |
| 2058 |
char *end = data.end(); |
| 2059 |
char last = 'N'; // An island is not counted if located at the beginning or end of the sequence. |
| 2060 |
|
| 2061 |
unsigned long long len = end - pos; |
| 2062 |
|
| 2063 |
|
| 2064 |
end = end - 2; |
| 2065 |
while (pos < end) |
| 2066 |
{ |
| 2067 |
char c1 = *pos; |
| 2068 |
|
| 2069 |
if ( c1 == 'C' || c1 == 'c' ) |
| 2070 |
{ |
| 2071 |
char c2 = *(pos+1); |
| 2072 |
char c3 = *(pos+2); |
| 2073 |
if ( c2 == 'G' || c2 == 'g') ++CpG; |
| 2074 |
if ( c3 == 'G' || c3 == 'g') ++CpNpG; |
| 2075 |
} |
| 2076 |
if (c1 == 'N' && last != 'N') |
| 2077 |
++Nislands; |
| 2078 |
|
| 2079 |
++v[(int)c1]; |
| 2080 |
++pos; |
| 2081 |
last = c1; |
| 2082 |
} |
| 2083 |
|
| 2084 |
// Now deal with the second last position: |
| 2085 |
if (len > 1 ) |
| 2086 |
{ |
| 2087 |
char c1 = *pos; |
| 2088 |
|
| 2089 |
if ( c1 == 'C' || c1 == 'c' ) |
| 2090 |
{ |
| 2091 |
char c2 = *(pos+1); |
| 2092 |
if ( c2 == 'G' || c2 == 'g' ) ++CpG; |
| 2093 |
} |
| 2094 |
if (c1 == 'N' && last != 'N') |
| 2095 |
++Nislands; |
| 2096 |
++v[(int)c1]; |
| 2097 |
++pos; |
| 2098 |
last = c1; |
| 2099 |
} |
| 2100 |
|
| 2101 |
// Now deal with the last position: |
| 2102 |
if (len > 0) |
| 2103 |
{ |
| 2104 |
++v[(int)*pos]; |
| 2105 |
if (*pos == 'N') // We don't count the island if at the end of the sequence |
| 2106 |
{ |
| 2107 |
if (last == 'N') // it has already been counted so undo this |
| 2108 |
{ |
| 2109 |
--Nislands; |
| 2110 |
} |
| 2111 |
// else |
| 2112 |
// we don't count it |
| 2113 |
} |
| 2114 |
} |
| 2115 |
return len; |
| 2116 |
} |
| 2117 |
|
| 2118 |
void debug_print_gap_coords(std::ostream &os) |
| 2119 |
{ |
| 2120 |
unsigned *git = gap_and_star_koords.begin(); |
| 2121 |
|
| 2122 |
os << "Gap positions:" << std::endl; |
| 2123 |
for (; git < gap_and_star_koords.end(); ++git) |
| 2124 |
os << *git << std::endl; |
| 2125 |
} |
| 2126 |
|
| 2127 |
|
| 2128 |
faststring::size_t getSeqCoord(const char *seq_pos) |
| 2129 |
{ |
| 2130 |
return seq_pos - data.begin() + 1; |
| 2131 |
} |
| 2132 |
|
| 2133 |
char get_ambiguity_character() |
| 2134 |
{ |
| 2135 |
return general_ambig; |
| 2136 |
} |
| 2137 |
|
| 2138 |
faststring::size_t getSeqCoord_gap_correted(const char *seq_pos, faststring::size_t &seq_gap_offset) |
| 2139 |
{ |
| 2140 |
faststring::size_t corrected_pos = seq_pos - data.begin()+1+seq_gap_offset; |
| 2141 |
|
| 2142 |
faststring::size_t i = seq_gap_offset; |
| 2143 |
faststring::size_t N = gap_and_star_koords.size(); |
| 2144 |
|
| 2145 |
// std::cerr << "Entering getSeqCoord_gap_correted i: " << i << " N: " << N << " corr coord: " << corrected_pos << std::endl; |
| 2146 |
|
| 2147 |
while (i < N && gap_and_star_koords.get_unckecked(i) < corrected_pos) |
| 2148 |
{ |
| 2149 |
++i; |
| 2150 |
++corrected_pos; |
| 2151 |
} |
| 2152 |
|
| 2153 |
seq_gap_offset = i; |
| 2154 |
|
| 2155 |
// std::cerr << "Exiting getSeqCoord_gap_correted i: " << i << " : " << N << " corr coord: " << corrected_pos << std::endl; |
| 2156 |
|
| 2157 |
return corrected_pos; |
| 2158 |
} |
| 2159 |
|
| 2160 |
|
| 2161 |
void writeSequence(std::ostream &os, faststring::size_t char_per_line) |
| 2162 |
{ |
| 2163 |
char *pos = data.begin(); |
| 2164 |
char *end = data.end(); |
| 2165 |
char *tmp_end; |
| 2166 |
|
| 2167 |
if (bitcode_recoded) |
| 2168 |
backrecode_bitcode_sequence(); |
| 2169 |
|
| 2170 |
os << ">" << full_name.c_str() << std::endl; |
| 2171 |
// os << ">" << full_name.c_str() << std::endl; |
| 2172 |
|
| 2173 |
tmp_end = pos + char_per_line; |
| 2174 |
if (tmp_end > end) |
| 2175 |
tmp_end = end; |
| 2176 |
|
| 2177 |
while (pos != end) |
| 2178 |
{ |
| 2179 |
while (pos != tmp_end) |
| 2180 |
{ |
| 2181 |
os << *pos; |
| 2182 |
++pos; |
| 2183 |
} |
| 2184 |
os << std::endl; |
| 2185 |
tmp_end = pos + char_per_line; |
| 2186 |
if (tmp_end > end) |
| 2187 |
tmp_end = end; |
| 2188 |
} |
| 2189 |
} |
| 2190 |
|
| 2191 |
/* void set_to_using_phylip_sequential_sequence() */ |
| 2192 |
/* { */ |
| 2193 |
/* } */ |
| 2194 |
|
| 2195 |
|
| 2196 |
void readRawFastaSequence(CFile& infile, faststring::size_t &num) |
| 2197 |
{ |
| 2198 |
char c = '\0'; |
| 2199 |
bool first_line = true; |
| 2200 |
|
| 2201 |
full_name = ""; |
| 2202 |
data.clear(); |
| 2203 |
|
| 2204 |
reset_numbers_of_residues(); |
| 2205 |
|
| 2206 |
c = infile.getchar(); |
| 2207 |
while (!infile.fail() && isspace(c)) // Ignore spaces |
| 2208 |
c = infile.getchar(); |
| 2209 |
while (!infile.fail() && c == '#') // Ignore lines starting with # |
| 2210 |
{ |
| 2211 |
infile.ignore('\n'); |
| 2212 |
c = infile.getchar(); |
| 2213 |
while (!infile.fail() && isspace(c) ) // Ignore spaces |
| 2214 |
c = infile.getchar(); |
| 2215 |
} |
| 2216 |
|
| 2217 |
if (c != '>') // Should be new sequence |
| 2218 |
{ |
| 2219 |
infile.clear(CFile::__fail_flag); |
| 2220 |
return; |
| 2221 |
} |
| 2222 |
infile.getline(full_name); |
| 2223 |
if (infile.fail()) |
| 2224 |
{ |
| 2225 |
return; |
| 2226 |
} |
| 2227 |
find_shortname_and_description(); |
| 2228 |
|
| 2229 |
c = infile.peekchar(); |
| 2230 |
while ( !infile.fail() && c != '>' ) |
| 2231 |
{ |
| 2232 |
// cerr << data.capacity() << endl; |
| 2233 |
if (first_line) |
| 2234 |
getRawLine(infile, num); |
| 2235 |
else |
| 2236 |
getRawLine(infile); |
| 2237 |
first_line = false; |
| 2238 |
c = infile.peekchar(); |
| 2239 |
} |
| 2240 |
} |
| 2241 |
|
| 2242 |
|
| 2243 |
void readRawFastaSequence(CFile& infile) |
| 2244 |
{ |
| 2245 |
char c = '\0'; |
| 2246 |
|
| 2247 |
full_name = ""; |
| 2248 |
data.clear(); |
| 2249 |
|
| 2250 |
reset_numbers_of_residues(); |
| 2251 |
|
| 2252 |
c = infile.getchar(); |
| 2253 |
while (!infile.fail() && isspace(c)) // Ignore spaces |
| 2254 |
c = infile.getchar(); |
| 2255 |
while (!infile.fail() && c == '#') // Ignore lines starting with # |
| 2256 |
{ |
| 2257 |
infile.ignore('\n'); |
| 2258 |
c = infile.getchar(); |
| 2259 |
while (!infile.fail() && isspace(c) ) // Ignore spaces |
| 2260 |
c = infile.getchar(); |
| 2261 |
} |
| 2262 |
|
| 2263 |
if (c != '>') // Should be new sequence |
| 2264 |
{ |
| 2265 |
infile.clear(CFile::__fail_flag); |
| 2266 |
return; |
| 2267 |
} |
| 2268 |
infile.getline(full_name); |
| 2269 |
if (infile.fail()) |
| 2270 |
{ |
| 2271 |
return; |
| 2272 |
} |
| 2273 |
find_shortname_and_description(); |
| 2274 |
|
| 2275 |
c = infile.peekchar(); |
| 2276 |
while ( !infile.fail() && c != '>' ) |
| 2277 |
{ |
| 2278 |
// cerr << data.capacity() << endl; |
| 2279 |
getRawLine(infile); |
| 2280 |
c = infile.peekchar(); |
| 2281 |
} |
| 2282 |
} |
| 2283 |
|
| 2284 |
void readSeqName_ignore_Sequence_data(CFile& infile) |
| 2285 |
{ |
| 2286 |
char c = '\0'; |
| 2287 |
|
| 2288 |
full_name = ""; |
| 2289 |
data.clear(); |
| 2290 |
|
| 2291 |
reset_numbers_of_residues(); |
| 2292 |
|
| 2293 |
c = infile.getchar(); |
| 2294 |
while (!infile.fail() && isspace(c)) // Ignore spaces |
| 2295 |
c = infile.getchar(); |
| 2296 |
while (!infile.fail() && c == '#') // Ignore lines starting with # |
| 2297 |
{ |
| 2298 |
infile.ignore('\n'); |
| 2299 |
c = infile.getchar(); |
| 2300 |
while (!infile.fail() && isspace(c) ) // Ignore spaces |
| 2301 |
c = infile.getchar(); |
| 2302 |
} |
| 2303 |
|
| 2304 |
if (c != '>') // Should be new sequence |
| 2305 |
{ |
| 2306 |
infile.clear(CFile::__fail_flag); |
| 2307 |
return; |
| 2308 |
} |
| 2309 |
infile.getline(full_name); |
| 2310 |
if (infile.fail()) |
| 2311 |
{ |
| 2312 |
return; |
| 2313 |
} |
| 2314 |
find_shortname_and_description(); |
| 2315 |
|
| 2316 |
// Ignore the sequence data of this sequence: |
| 2317 |
c = infile.peekchar(); |
| 2318 |
while ( !infile.fail() && c != '>' ) |
| 2319 |
{ |
| 2320 |
// std::cerr << data.capacity() << std::endl; |
| 2321 |
infile.ignore('\n'); |
| 2322 |
c = infile.peekchar(); |
| 2323 |
} |
| 2324 |
} |
| 2325 |
|
| 2326 |
|
| 2327 |
void readRawFastaSequence_toupper(CFile& infile) |
| 2328 |
{ |
| 2329 |
char c = '\0'; |
| 2330 |
|
| 2331 |
full_name = ""; |
| 2332 |
data.clear(); |
| 2333 |
|
| 2334 |
reset_numbers_of_residues(); |
| 2335 |
|
| 2336 |
c = infile.getchar(); |
| 2337 |
while (!infile.fail() && isspace(c)) |
| 2338 |
c = infile.getchar(); |
| 2339 |
while (!infile.fail() && c == '#') |
| 2340 |
{ |
| 2341 |
infile.ignore('\n'); |
| 2342 |
c = infile.getchar(); |
| 2343 |
while (!infile.fail() && isspace(c) ) |
| 2344 |
c = infile.getchar(); |
| 2345 |
} |
| 2346 |
|
| 2347 |
if (c != '>') |
| 2348 |
{ |
| 2349 |
infile.clear(CFile::__fail_flag); |
| 2350 |
return; |
| 2351 |
} |
| 2352 |
infile.getline(full_name); |
| 2353 |
if (infile.fail()) |
| 2354 |
{ |
| 2355 |
return; |
| 2356 |
} |
| 2357 |
find_shortname_and_description(); |
| 2358 |
|
| 2359 |
c = infile.peekchar(); |
| 2360 |
while ( !infile.fail() && c != '>' ) |
| 2361 |
{ |
| 2362 |
// std::cerr << data.capacity() << std::endl; |
| 2363 |
getRawLine_toupper(infile); |
| 2364 |
c = infile.peekchar(); |
| 2365 |
} |
| 2366 |
} |
| 2367 |
|
| 2368 |
// it1 is the first position to fill. This cooridnate must be 0 based. |
| 2369 |
// it2 is the position behind the last position to fill. It must be 0 based. |
| 2370 |
|
| 2371 |
void fill_range_with(faststring::size_t pos1, faststring::size_t pos2, char c) |
| 2372 |
{ |
| 2373 |
numbers_of_residues_computed = false; |
| 2374 |
|
| 2375 |
char *it1 = data.begin() + pos1; |
| 2376 |
char *it2 = data.begin() + pos2; |
| 2377 |
char *it_end = data.end(); |
| 2378 |
|
| 2379 |
if (it2 > it_end) |
| 2380 |
it2 = it_end; |
| 2381 |
|
| 2382 |
while (it1 < it2) |
| 2383 |
{ |
| 2384 |
*it1 = c; |
| 2385 |
++it1; |
| 2386 |
} |
| 2387 |
} |
| 2388 |
|
| 2389 |
|
| 2390 |
// Called from Phobos: |
| 2391 |
// TODO: I think we should not remove stars any more. |
| 2392 |
// Stars should only be tolerated in protein sequences. |
| 2393 |
void readFastaSequence_toupper_ambig2N_removegaps(CFile& infile) |
| 2394 |
{ |
| 2395 |
char c = '\0'; |
| 2396 |
faststring::size_t pos = 0; |
| 2397 |
|
| 2398 |
full_name = ""; |
| 2399 |
data.clear(); |
| 2400 |
gap_and_star_koords.clear(); |
| 2401 |
gap_koords.clear(); |
| 2402 |
star_koords.clear(); |
| 2403 |
|
| 2404 |
reset_numbers_of_residues(); |
| 2405 |
|
| 2406 |
c = infile.getchar(); |
| 2407 |
while (!infile.fail() && isspace(c)) |
| 2408 |
c = infile.getchar(); |
| 2409 |
while (!infile.fail() && c == '#') |
| 2410 |
{ |
| 2411 |
infile.ignore('\n'); |
| 2412 |
c = infile.getchar(); |
| 2413 |
while (!infile.fail() && isspace(c) ) |
| 2414 |
c = infile.getchar(); |
| 2415 |
} |
| 2416 |
|
| 2417 |
if (c != '>') |
| 2418 |
{ |
| 2419 |
infile.clear(CFile::__fail_flag); |
| 2420 |
return; |
| 2421 |
} |
| 2422 |
infile.getline(full_name); |
| 2423 |
if (infile.fail()) |
| 2424 |
{ |
| 2425 |
return; |
| 2426 |
} |
| 2427 |
find_shortname_and_description(); |
| 2428 |
|
| 2429 |
c = infile.peekchar(); |
| 2430 |
while ( !infile.fail() && c != '>' ) |
| 2431 |
{ |
| 2432 |
// std::cerr << data.capacity() << std::endl; |
| 2433 |
if (type == dna) |
| 2434 |
getLineOfDNA_toupper_ambig2N_removegaps(infile, pos); |
| 2435 |
else |
| 2436 |
std::cout << "Not implemented. Use the readFastaSequence_toupper_removegaps(CFile& infile) function instead." << std::endl; |
| 2437 |
c = infile.peekchar(); |
| 2438 |
} |
| 2439 |
} |
| 2440 |
|
| 2441 |
|
| 2442 |
void readFastaSequence_toupper_removegaps(CFile& infile) |
| 2443 |
{ |
| 2444 |
char c = '\0'; |
| 2445 |
faststring::size_t pos = 0; |
| 2446 |
|
| 2447 |
full_name = ""; |
| 2448 |
data.clear(); |
| 2449 |
gap_and_star_koords.clear(); |
| 2450 |
gap_koords.clear(); |
| 2451 |
star_koords.clear(); |
| 2452 |
|
| 2453 |
reset_numbers_of_residues(); |
| 2454 |
|
| 2455 |
c = infile.getchar(); |
| 2456 |
while (!infile.fail() && isspace(c)) |
| 2457 |
c = infile.getchar(); |
| 2458 |
while (!infile.fail() && c == '#') |
| 2459 |
{ |
| 2460 |
infile.ignore('\n'); |
| 2461 |
c = infile.getchar(); |
| 2462 |
while (!infile.fail() && isspace(c) ) |
| 2463 |
c = infile.getchar(); |
| 2464 |
} |
| 2465 |
|
| 2466 |
if (c != '>') |
| 2467 |
{ |
| 2468 |
infile.clear(CFile::__fail_flag); |
| 2469 |
return; |
| 2470 |
} |
| 2471 |
infile.getline(full_name); |
| 2472 |
if (infile.fail()) |
| 2473 |
{ |
| 2474 |
return; |
| 2475 |
} |
| 2476 |
find_shortname_and_description(); |
| 2477 |
|
| 2478 |
c = infile.peekchar(); |
| 2479 |
while ( !infile.fail() && c != '>' ) |
| 2480 |
{ |
| 2481 |
// std::cerr << data.capacity() << std::endl; |
| 2482 |
if (type == dna) |
| 2483 |
getLineOfDNA_toupper_removegaps(infile, pos); |
| 2484 |
else |
| 2485 |
getLineOfProtein_toupper_removegaps(infile, pos); |
| 2486 |
c = infile.peekchar(); |
| 2487 |
} |
| 2488 |
} |
| 2489 |
|
| 2490 |
// Backward compatible function: |
| 2491 |
// Called e.g. from Phobos |
| 2492 |
void readFastaSequence_toupper_ambig2N_gaps2N(CFile& infile) |
| 2493 |
{ |
| 2494 |
readFastaSequence_toupper_ambig2N_gaps2ambig(infile); |
| 2495 |
} |
| 2496 |
|
| 2497 |
|
| 2498 |
// Has recently been renamed: gaps2N -> gaps2ambig |
| 2499 |
// since we want to prepare for reading proteins. |
| 2500 |
void readFastaSequence_toupper_ambig2N_gaps2ambig(CFile& infile) |
| 2501 |
{ |
| 2502 |
char c = '\0'; |
| 2503 |
|
| 2504 |
full_name = ""; |
| 2505 |
data.clear(); |
| 2506 |
|
| 2507 |
reset_numbers_of_residues(); |
| 2508 |
|
| 2509 |
c = infile.getchar(); |
| 2510 |
while (!infile.fail() && isspace(c)) |
| 2511 |
c = infile.getchar(); |
| 2512 |
while (!infile.fail() && c == '#') |
| 2513 |
{ |
| 2514 |
infile.ignore('\n'); |
| 2515 |
c = infile.getchar(); |
| 2516 |
while (!infile.fail() && isspace(c) ) |
| 2517 |
c = infile.getchar(); |
| 2518 |
} |
| 2519 |
|
| 2520 |
if (c != '>') |
| 2521 |
{ |
| 2522 |
infile.clear(CFile::__fail_flag); |
| 2523 |
return; |
| 2524 |
} |
| 2525 |
infile.getline(full_name); |
| 2526 |
if (infile.fail()) |
| 2527 |
{ |
| 2528 |
return; |
| 2529 |
} |
| 2530 |
find_shortname_and_description(); |
| 2531 |
|
| 2532 |
c = infile.peekchar(); |
| 2533 |
while ( !infile.fail() && c != '>' ) |
| 2534 |
{ |
| 2535 |
// std::cerr << data.capacity() << std::endl; |
| 2536 |
if (type == dna) |
| 2537 |
getLineOfDNA_toupper_ambig2N_gaps2ambig(infile); |
| 2538 |
else |
| 2539 |
std::cout << "Not implemented." << std::endl; |
| 2540 |
c = infile.peekchar(); |
| 2541 |
} |
| 2542 |
} |
| 2543 |
|
| 2544 |
void recode_sequence_to_bitcode() |
| 2545 |
{ |
| 2546 |
if (type == dna) |
| 2547 |
{ |
| 2548 |
char *it = data.begin(); |
| 2549 |
char *it_end = data.end(); |
| 2550 |
|
| 2551 |
while (it != it_end) |
| 2552 |
{ |
| 2553 |
*it = recode_DNA(*it); |
| 2554 |
++it; |
| 2555 |
} |
| 2556 |
bitcode_recoded = true; |
| 2557 |
} |
| 2558 |
else if (type == protein) |
| 2559 |
{} |
| 2560 |
} |
| 2561 |
|
| 2562 |
void backrecode_bitcode_sequence() |
| 2563 |
{ |
| 2564 |
if (type == dna) |
| 2565 |
{ |
| 2566 |
char *it = data.begin(); |
| 2567 |
char *it_end = data.end(); |
| 2568 |
|
| 2569 |
while (it != it_end) |
| 2570 |
{ |
| 2571 |
*it = backrecode_DNA(*it); |
| 2572 |
++it; |
| 2573 |
} |
| 2574 |
bitcode_recoded = false; |
| 2575 |
} |
| 2576 |
else if (type == protein) |
| 2577 |
{} |
| 2578 |
} |
| 2579 |
|
| 2580 |
// Crashed in this function on linux - do not know why yet 64 bit problem?? |
| 2581 |
void check_allowed_symbols_in_sequence(const char *symbols) |
| 2582 |
{ |
| 2583 |
size_t pos=0; |
| 2584 |
|
| 2585 |
pos = data.find_first_not_of(symbols, pos); |
| 2586 |
while (pos != faststring::npos) |
| 2587 |
{ |
| 2588 |
std::cerr << "Found illegal symbol in sequence: " << short_name |
| 2589 |
<< " Position " << pos << ". Found char " << data[pos] << std::endl; |
| 2590 |
|
| 2591 |
++pos; |
| 2592 |
pos = data.find_first_not_of(symbols, pos); |
| 2593 |
} |
| 2594 |
|
| 2595 |
} |
| 2596 |
|
| 2597 |
|
| 2598 |
void export_fasta() |
| 2599 |
{ |
| 2600 |
|
| 2601 |
|
| 2602 |
} |
| 2603 |
|
| 2604 |
void export_phylip() |
| 2605 |
{ |
| 2606 |
|
| 2607 |
|
| 2608 |
} |
| 2609 |
|
| 2610 |
void export_nexus() |
| 2611 |
{ |
| 2612 |
|
| 2613 |
|
| 2614 |
} |
| 2615 |
|
| 2616 |
|
| 2617 |
void readFastaSequence_generic(CFile& infile, processing_flag pflag) |
| 2618 |
{ |
| 2619 |
char c = '\0'; |
| 2620 |
faststring::size_t pos = 0; |
| 2621 |
|
| 2622 |
// TODO: Prepare to remove gaps and/or stars: |
| 2623 |
|
| 2624 |
// Increase the readability: |
| 2625 |
register bool convert_toupper_bool = (pflag & convert_toupper); // |
| 2626 |
register bool convert_ambig2N_bool = (pflag & convert_ambig2N); // |
| 2627 |
register bool removegaps_bool = (pflag & removegaps); // |
| 2628 |
register bool removestars_bool = (pflag & removestars); // |
| 2629 |
bool gaps_to_ambig_bool = (pflag & gaps_to_ambig); // |
| 2630 |
|
| 2631 |
full_name = ""; |
| 2632 |
data.clear(); |
| 2633 |
|
| 2634 |
reset_numbers_of_residues(); |
| 2635 |
|
| 2636 |
c = infile.getchar(); |
| 2637 |
while (!infile.fail() && isspace(c)) |
| 2638 |
c = infile.getchar(); |
| 2639 |
while (!infile.fail() && c == '#') |
| 2640 |
{ |
| 2641 |
infile.ignore('\n'); |
| 2642 |
c = infile.getchar(); |
| 2643 |
while (!infile.fail() && isspace(c) ) |
| 2644 |
c = infile.getchar(); |
| 2645 |
} |
| 2646 |
|
| 2647 |
if (c != '>') |
| 2648 |
{ |
| 2649 |
infile.clear(CFile::__fail_flag); |
| 2650 |
return; |
| 2651 |
} |
| 2652 |
infile.getline(full_name); |
| 2653 |
if (infile.fail()) |
| 2654 |
{ |
| 2655 |
return; |
| 2656 |
} |
| 2657 |
find_shortname_and_description(); |
| 2658 |
|
| 2659 |
c = infile.peekchar(); |
| 2660 |
while ( !infile.fail() && c != '>' ) |
| 2661 |
{ |
| 2662 |
// std::cerr << data.capacity() << std::endl; |
| 2663 |
if (type == dna) |
| 2664 |
getLineOfDNA_generic(infile, pos, pflag); |
| 2665 |
else if (type == protein) |
| 2666 |
getLineOfProtein_generic(infile, pos, pflag); |
| 2667 |
else if (type == molecular) |
| 2668 |
{ |
| 2669 |
getRawLine(infile); |
| 2670 |
if (convert_toupper_bool) |
| 2671 |
toupper_this_sequence(); |
| 2672 |
|
| 2673 |
if (convert_ambig2N_bool) |
| 2674 |
convert_ambig2N_this_sequence(); |
| 2675 |
if (gaps_to_ambig_bool) |
| 2676 |
gaps_to_ambig_this_sequence(); |
| 2677 |
if (removegaps_bool || removestars_bool) |
| 2678 |
remove_gaps_stars_this_sequence(removegaps_bool, removestars_bool); |
| 2679 |
} |
| 2680 |
else if (type == auto_detect_type) |
| 2681 |
{ |
| 2682 |
getRawLine(infile); |
| 2683 |
if (convert_toupper_bool) |
| 2684 |
toupper_this_sequence(); |
| 2685 |
auto_detect_datatype(); |
| 2686 |
|
| 2687 |
if (convert_ambig2N_bool) |
| 2688 |
convert_ambig2N_this_sequence(); |
| 2689 |
if (gaps_to_ambig_bool) |
| 2690 |
gaps_to_ambig_this_sequence(); |
| 2691 |
if (removegaps_bool || removestars_bool) |
| 2692 |
remove_gaps_stars_this_sequence(removegaps_bool, removestars_bool); |
| 2693 |
} |
| 2694 |
c = infile.peekchar(); |
| 2695 |
} |
| 2696 |
} |
| 2697 |
|
| 2698 |
unsigned get_number_of_Ns() |
| 2699 |
{ |
| 2700 |
if (!numbers_of_residues_computed) |
| 2701 |
compute_numbers_of_residues(); |
| 2702 |
return number_of_Ns; |
| 2703 |
} |
| 2704 |
|
| 2705 |
unsigned get_number_of_gaps() |
| 2706 |
{ |
| 2707 |
if (!numbers_of_residues_computed) |
| 2708 |
compute_numbers_of_residues(); |
| 2709 |
return number_of_gaps; |
| 2710 |
} |
| 2711 |
|
| 2712 |
unsigned get_number_raw_original_length() |
| 2713 |
{ |
| 2714 |
if (!numbers_of_residues_computed) |
| 2715 |
compute_numbers_of_residues(); |
| 2716 |
return number_raw_original_length; |
| 2717 |
} |
| 2718 |
|
| 2719 |
unsigned get_number_of_DNARNA_bases() |
| 2720 |
{ |
| 2721 |
if (!numbers_of_residues_computed) |
| 2722 |
compute_numbers_of_residues(); |
| 2723 |
return number_of_DNARNA_bases; |
| 2724 |
} |
| 2725 |
|
| 2726 |
unsigned get_number_of_AAs() |
| 2727 |
{ |
| 2728 |
if (!numbers_of_residues_computed) |
| 2729 |
compute_numbers_of_residues(); |
| 2730 |
return number_of_AAs; |
| 2731 |
} |
| 2732 |
|
| 2733 |
unsigned get_number_of_DNARNA_ambigs() |
| 2734 |
{ |
| 2735 |
return number_of_DNARNA_amgibs; |
| 2736 |
} |
| 2737 |
|
| 2738 |
double get_CG_content() |
| 2739 |
{ |
| 2740 |
// std::cerr << "number_CG " << number_CG << std::endl; |
| 2741 |
// std::cerr << "number_of_DNARNA_bases " << number_of_DNARNA_bases << std::endl; |
| 2742 |
// // std::cerr << "number_of_DNARNA_amgibs " << number_of_DNARNA_amgibs << std::endl; |
| 2743 |
|
| 2744 |
return ((double) number_CG)/(number_of_DNARNA_bases); |
| 2745 |
} |
| 2746 |
|
| 2747 |
|
| 2748 |
bool range_contains_gaps_or_Ns(faststring::size_t pos1, faststring::size_t pos2) |
| 2749 |
{ |
| 2750 |
faststring::size_t len = length(); |
| 2751 |
|
| 2752 |
if (pos1 >= pos2 || pos1 > len) // Empty range |
| 2753 |
return false; |
| 2754 |
if (pos2 > len) |
| 2755 |
pos2 = len; |
| 2756 |
|
| 2757 |
const char *p1, *p2; |
| 2758 |
p1 = getSeqBegin() + pos1; |
| 2759 |
p2 = getSeqBegin() + pos2; |
| 2760 |
|
| 2761 |
char c='\0'; |
| 2762 |
|
| 2763 |
while (p1 != p2) |
| 2764 |
{ |
| 2765 |
c = *p1; |
| 2766 |
if (c == 'N' || c == 'n' || c == '-') |
| 2767 |
break; |
| 2768 |
++p1; |
| 2769 |
} |
| 2770 |
if (c == 'N' || c == 'n' || c == '-') |
| 2771 |
return true; |
| 2772 |
else |
| 2773 |
return false; |
| 2774 |
} |
| 2775 |
|
| 2776 |
// Requirement: Amino acids have to encoded as upper case characters. |
| 2777 |
// Returns -1 in case the sequence is not proper blosum code. |
| 2778 |
// Returns 0 or positive value indicating the number of Js that have been replaced. |
| 2779 |
int check_protein_sequence_for_blosum_compatibility(unsigned &refpos) |
| 2780 |
{ |
| 2781 |
unsigned pos; |
| 2782 |
|
| 2783 |
int count = data.replace_char_count('J','X'); |
| 2784 |
bool OK = is_aa_blosum_code_or_gap_sequence(data.begin(), data.end(), &pos); |
| 2785 |
|
| 2786 |
if (!OK) |
| 2787 |
{ |
| 2788 |
refpos = pos; |
| 2789 |
return -1; |
| 2790 |
} |
| 2791 |
|
| 2792 |
return count; |
| 2793 |
} |
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
// void readFastaSequence(CFile& infile) is deprecated: |
| 2798 |
// Use one of the other read functions below. |
| 2799 |
// This function used to by of type |
| 2800 |
// toupper_ambig2N_removegaps |
| 2801 |
|
| 2802 |
/* void readFastaSequence(CFile& infile) */ |
| 2803 |
/* { */ |
| 2804 |
/* #warning "The readFastaSequence function is deprecated." */ |
| 2805 |
|
| 2806 |
/* char c = '\0'; */ |
| 2807 |
|
| 2808 |
/* full_name = ""; */ |
| 2809 |
/* data.clear(); */ |
| 2810 |
|
| 2811 |
/* c = infile.getchar(); */ |
| 2812 |
/* while (!infile.fail() && isspace(c)) */ |
| 2813 |
/* c = infile.getchar(); */ |
| 2814 |
/* while (!infile.fail() && c == '#') */ |
| 2815 |
/* { */ |
| 2816 |
/* infile.ignore('\n'); */ |
| 2817 |
/* c = infile.getchar(); */ |
| 2818 |
/* while (!infile.fail() && isspace(c) ) */ |
| 2819 |
/* c = infile.getchar(); */ |
| 2820 |
/* } */ |
| 2821 |
|
| 2822 |
/* if (c != '>') */ |
| 2823 |
/* { */ |
| 2824 |
/* infile.clear(CFile::__fail_flag); */ |
| 2825 |
/* return; */ |
| 2826 |
/* } */ |
| 2827 |
/* infile.getline(full_name); */ |
| 2828 |
/* if (infile.fail()) */ |
| 2829 |
/* { */ |
| 2830 |
/* return; */ |
| 2831 |
/* } */ |
| 2832 |
/* find_shortname_and_description(); */ |
| 2833 |
|
| 2834 |
/* c = infile.peekchar(); */ |
| 2835 |
/* while ( !infile.fail() && c != '>' ) */ |
| 2836 |
/* { */ |
| 2837 |
/* // cerr << data.capacity() << endl; */ |
| 2838 |
/* if (type == dna) */ |
| 2839 |
/* getLineOfDNA_toupper_ambig2N_removegaps(infile); */ |
| 2840 |
/* else */ |
| 2841 |
/* std::cout << "Not implemented." << std::endl; */ |
| 2842 |
/* c = infile.peekchar(); */ |
| 2843 |
/* } */ |
| 2844 |
/* } */ |
| 2845 |
|
| 2846 |
}; |
| 2847 |
|
| 2848 |
// Functions provided to read sequences: |
| 2849 |
// void readRawFastaSequence(CFile& infile) // No character checks |
| 2850 |
// void readRawFastaSequence_toupper(CFile& infile) // No character checks |
| 2851 |
// void readFastaSequence_toupper_ambig2N_removegaps(CFile& infile) // |
| 2852 |
// void readFastaSequence_toupper_ambig2N_gaps2ambig(CFile& infile) // |
| 2853 |
// void readFastaSequence_toupper_removegaps(CFile& infile) // In Arbeit |
| 2854 |
// |
| 2855 |
#endif |