| 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 |
// Uses modified comparison functions to make sort stable. |
| 29 |
|
| 30 |
#include <iostream> |
| 31 |
#include <fstream> |
| 32 |
#include "faststring2.h" |
| 33 |
#include <vector> |
| 34 |
#include <set> |
| 35 |
#include "Ctriple.h" |
| 36 |
#include "print_container.h" |
| 37 |
#include "CBlastParser.h" |
| 38 |
|
| 39 |
// Introducing this define has several advantages: |
| 40 |
// - simple switch to stable_sort and back |
| 41 |
// - simple find all calls to sort in this file. |
| 42 |
#define MYSORT sort |
| 43 |
// Instead of using stable_sort we now use modified comparison functions. So this switch should not be necessary any more. |
| 44 |
// #define MYSORT stable_sort |
| 45 |
|
| 46 |
class CBaitRegionSelectorSets |
| 47 |
{ |
| 48 |
std::set<faststring> set_of_alignment_names; |
| 49 |
std::set<std::pair<faststring, unsigned> > set_of_alignment_names_feature_num; |
| 50 |
std::set<Ctriple<faststring, unsigned, unsigned> > set_of_alignment_name_feature_num_start__ie_locus; |
| 51 |
|
| 52 |
public: |
| 53 |
void add(const faststring &str, unsigned i, unsigned j) |
| 54 |
{ |
| 55 |
set_of_alignment_names.insert(str); |
| 56 |
set_of_alignment_names_feature_num.insert(std::make_pair(str, i)); |
| 57 |
set_of_alignment_name_feature_num_start__ie_locus.insert(Ctriple<faststring, unsigned, unsigned>(str,i, j)); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
bool exists(const faststring &str) const |
| 62 |
{ |
| 63 |
if (set_of_alignment_names.find(str) != set_of_alignment_names.end() ) |
| 64 |
return true; |
| 65 |
else |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
bool exists(const faststring &str, unsigned i) const |
| 70 |
{ |
| 71 |
if (set_of_alignment_names_feature_num.find(std::make_pair(str, i)) != set_of_alignment_names_feature_num.end() ) |
| 72 |
return true; |
| 73 |
else |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
bool exists(const faststring &str, unsigned i, unsigned j) const |
| 78 |
{ |
| 79 |
if (set_of_alignment_name_feature_num_start__ie_locus.find(Ctriple<faststring, unsigned, unsigned>(str,i,j)) != set_of_alignment_name_feature_num_start__ie_locus.end() ) |
| 80 |
return true; |
| 81 |
else |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
void print_all(std::ostream &os) const |
| 86 |
{ |
| 87 |
os << "Selector string (alignment) argument:" << std::endl; |
| 88 |
print_container(os, set_of_alignment_names, "", "\n" , "\n"); |
| 89 |
os << "Selector string (alignment) argument, unsigned (feature number): " << std::endl; |
| 90 |
print_container(os, set_of_alignment_names_feature_num, "", "\n" , "\n"); |
| 91 |
os << "Selector string (alignment) argument, unsigned (feature number), unsigned (start): " << std::endl; |
| 92 |
print_container(os, set_of_alignment_name_feature_num_start__ie_locus, "", "\n" , "\n"); |
| 93 |
} |
| 94 |
|
| 95 |
void print_num_genes_num_features(std::ostream &os, const char *s, bool print_num_featues) const |
| 96 |
{ |
| 97 |
os << s << "The bait regions belong to this number of alignment files: " << set_of_alignment_names.size() << std::endl; |
| 98 |
os << s << "Number of bait regions: " << set_of_alignment_name_feature_num_start__ie_locus.size() << std::endl; |
| 99 |
if (print_num_featues) |
| 100 |
os << s << "Number of features " << set_of_alignment_names_feature_num.size() << std::endl; |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
class CBait |
| 105 |
{ |
| 106 |
faststring bait; |
| 107 |
double CG; |
| 108 |
double max_dist; |
| 109 |
unsigned tiling_index; // 1 based index in tiling array. 1: first tile; 2: second tile, etc. |
| 110 |
|
| 111 |
public: |
| 112 |
|
| 113 |
CBait(faststring &str) |
| 114 |
{ |
| 115 |
faststring pre, post1, post2; |
| 116 |
|
| 117 |
// The error messages below could be improved. It would be a good idea to tell the user the line in the file which causes the error. |
| 118 |
// At the moment the user is being told the bait string or the number that causes the problem. While this should help in most cases to find the problem |
| 119 |
// it would be much more convenient for the user to have the line number in the file. |
| 120 |
|
| 121 |
str.divide_at(' ', pre, post2); |
| 122 |
if (!pre.isAnUnsigned()) |
| 123 |
{ |
| 124 |
std::cerr << "Error when parsing the bait file: Has it been edited manually?" << std::endl; |
| 125 |
std::cerr << "Before each bait string BaitFilter expects a number indicating the index of the tiling design column. This number could not be found for the following bait:" |
| 126 |
<< std::endl; |
| 127 |
std::cerr << str << std::endl; |
| 128 |
std::cerr << "Exiting." << std::endl; |
| 129 |
exit(-4); |
| 130 |
} |
| 131 |
tiling_index = pre.ToUnsigned(); |
| 132 |
post2.divide_at(' ', pre, post1); |
| 133 |
bait = pre; |
| 134 |
post1.divide_at(' ', pre, post2); |
| 135 |
if (!pre.isADouble()) |
| 136 |
{ |
| 137 |
std::cerr << "Error when parsing the bait file: Has it been edited manually?" << std::endl; |
| 138 |
std::cerr << "After each bait string BaitFilter expects two double numbers containing the baits' CG content and its maximum distance to the MSA.\n" |
| 139 |
<< "Expecting a double number for the CG content but found: " << pre |
| 140 |
<< std::endl; |
| 141 |
std::cerr << "Exiting." << std::endl; |
| 142 |
exit(-4); |
| 143 |
} |
| 144 |
if (!post2.isADouble()) |
| 145 |
{ |
| 146 |
std::cerr << "Error when parsing the bait file: Has it been edited manually?" << std::endl; |
| 147 |
std::cerr << "After each bait string BaitFilter expects two double numbers containing the baits' CG content and its maximum distance in the MSA.\n" |
| 148 |
<< "Expecting a double number for the distance value but found: " << post2 |
| 149 |
<< std::endl; |
| 150 |
std::cerr << "Exiting." << std::endl; |
| 151 |
exit(-4); |
| 152 |
} |
| 153 |
|
| 154 |
CG = pre.ToDouble(); |
| 155 |
max_dist = post2.ToDouble(); |
| 156 |
} |
| 157 |
|
| 158 |
faststring& get_bait() |
| 159 |
{ |
| 160 |
return bait; |
| 161 |
} |
| 162 |
|
| 163 |
unsigned get_tiling_index() |
| 164 |
{ |
| 165 |
return tiling_index; |
| 166 |
} |
| 167 |
|
| 168 |
void print(std::ostream &os) |
| 169 |
{ |
| 170 |
os << tiling_index << " " << bait << " " << CG << " " << max_dist; |
| 171 |
} |
| 172 |
|
| 173 |
double get_max_distance() |
| 174 |
{ |
| 175 |
return max_dist; |
| 176 |
} |
| 177 |
|
| 178 |
double get_CG_content() |
| 179 |
{ |
| 180 |
return CG; |
| 181 |
} |
| 182 |
|
| 183 |
}; // END CBait |
| 184 |
|
| 185 |
// Forward declaration of friend functions of the CBaitLocus class. Avoids friend injection problems: |
| 186 |
class CBaitLocus; |
| 187 |
bool num_baits_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b); |
| 188 |
bool num_sequences_in_region_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b); |
| 189 |
bool gene_name__feature_num_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b); |
| 190 |
|
| 191 |
bool num_baits_greaterThanCBaitLocus(CBaitLocus *a, CBaitLocus *b); |
| 192 |
bool num_sequences_in_region_greaterThanCBaitLocus(CBaitLocus *a, CBaitLocus *b); |
| 193 |
bool gene_name__feature_num_greaterThanCBaitLocus(CBaitLocus *a, CBaitLocus *b); |
| 194 |
|
| 195 |
bool gene_name__feature_num_lessThanOrEqualCBaitLocus(CBaitLocus *a, CBaitLocus *b); |
| 196 |
|
| 197 |
// bool original_file_name__feature_num_start_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b); |
| 198 |
|
| 199 |
bool locus_start_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b); |
| 200 |
|
| 201 |
|
| 202 |
// Information about a bait region, i.e. a locus with a tiling design and its baits. |
| 203 |
// Basically this is the information stored in one line of a BaitFisher output file. |
| 204 |
|
| 205 |
class CBaitLocus |
| 206 |
{ |
| 207 |
faststring original_file_name; |
| 208 |
faststring gene_name; |
| 209 |
unsigned feature_num; |
| 210 |
unsigned num_features; |
| 211 |
unsigned start; |
| 212 |
unsigned num_sequences_in_region; |
| 213 |
unsigned num_baits; |
| 214 |
unsigned bait_length; |
| 215 |
unsigned number_of_tiles; |
| 216 |
|
| 217 |
std::vector<CBait*> bait_vec; |
| 218 |
|
| 219 |
public: |
| 220 |
|
| 221 |
CBaitLocus(faststring &str) |
| 222 |
{ |
| 223 |
std::vector<faststring> container; |
| 224 |
container.reserve(7); |
| 225 |
split(container, str, "\t|#"); |
| 226 |
//split(container, str, "\t"); |
| 227 |
|
| 228 |
if (container.size() != 8) |
| 229 |
{ |
| 230 |
std::cerr << std::endl; |
| 231 |
std::cerr << "An error occurred while parsing the bait file. The line of input that causes the problem is:\n"; |
| 232 |
std::cerr << str << std::endl; |
| 233 |
std::cerr << "The line does not contain 8 tab delimited fields as required." << std::endl; |
| 234 |
exit(-1); |
| 235 |
} |
| 236 |
|
| 237 |
container[0].removeSpacesFront(); |
| 238 |
container[0].removeSpacesBack(); |
| 239 |
container[1].removeSpacesFront(); |
| 240 |
container[1].removeSpacesBack(); |
| 241 |
container[2].removeSpacesFront(); |
| 242 |
container[2].removeSpacesBack(); |
| 243 |
container[3].removeSpacesFront(); |
| 244 |
container[3].removeSpacesBack(); |
| 245 |
container[4].removeSpacesFront(); |
| 246 |
container[4].removeSpacesBack(); |
| 247 |
container[5].removeSpacesFront(); |
| 248 |
container[5].removeSpacesBack(); |
| 249 |
container[6].removeSpacesFront(); |
| 250 |
container[6].removeSpacesBack(); |
| 251 |
container[7].removeSpacesFront(); |
| 252 |
container[7].removeSpacesBack(); |
| 253 |
|
| 254 |
original_file_name = container[0]; |
| 255 |
gene_name = container[1]; |
| 256 |
feature_num = container[2].ToUnsigned(); |
| 257 |
num_features = container[3].ToUnsigned(); |
| 258 |
start = container[4].ToUnsigned(); |
| 259 |
num_sequences_in_region = container[5].ToUnsigned(); |
| 260 |
num_baits = container[6].ToUnsigned(); |
| 261 |
|
| 262 |
std::list<faststring> b; |
| 263 |
split(b, container[7], ","); |
| 264 |
|
| 265 |
std::list<faststring>::iterator it, it_end; |
| 266 |
|
| 267 |
CBait *p_tmp; |
| 268 |
|
| 269 |
it = b.begin(); |
| 270 |
it_end = b.end(); |
| 271 |
|
| 272 |
number_of_tiles = 0; |
| 273 |
// For all baits in this bait region: |
| 274 |
while(it != it_end) |
| 275 |
{ |
| 276 |
p_tmp = new CBait(*it); |
| 277 |
bait_vec.push_back(p_tmp); |
| 278 |
if (p_tmp->get_tiling_index() > number_of_tiles) |
| 279 |
number_of_tiles = p_tmp->get_tiling_index(); |
| 280 |
|
| 281 |
/* if (0) */ |
| 282 |
/* { */ |
| 283 |
/* std::cerr << "Adding bait:" << std::endl; */ |
| 284 |
/* bait_vec.back()->print(std::cerr); */ |
| 285 |
/* std::cerr << std::endl; */ |
| 286 |
/* } */ |
| 287 |
|
| 288 |
++it; |
| 289 |
} |
| 290 |
if (bait_vec.size() != 0) |
| 291 |
{ |
| 292 |
bait_length = bait_vec[0]->get_bait().size(); |
| 293 |
} |
| 294 |
else |
| 295 |
{ |
| 296 |
bait_length = 0; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
~CBaitLocus() |
| 301 |
{ |
| 302 |
std::vector<CBait *>::iterator it, it_end; |
| 303 |
it = bait_vec.begin(); |
| 304 |
it_end = bait_vec.end(); |
| 305 |
|
| 306 |
while (it != it_end) |
| 307 |
{ |
| 308 |
delete *it; |
| 309 |
++it; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
bool has_feature_information() |
| 314 |
{ |
| 315 |
return (num_features == 0); |
| 316 |
} |
| 317 |
|
| 318 |
faststring &get_original_file_name() |
| 319 |
{ |
| 320 |
return original_file_name; |
| 321 |
} |
| 322 |
|
| 323 |
const faststring &get_gene_name() const |
| 324 |
{ |
| 325 |
return gene_name; |
| 326 |
} |
| 327 |
|
| 328 |
void get_vector_of_baits(unsigned tiling_index, std::vector<CBait*> &v) |
| 329 |
{ |
| 330 |
std::vector<CBait*>::iterator it, it_end; |
| 331 |
it = bait_vec.begin(); |
| 332 |
it_end = bait_vec.end(); |
| 333 |
|
| 334 |
v.clear(); |
| 335 |
v.reserve(bait_vec.size()); |
| 336 |
|
| 337 |
if (tiling_index == 0) // all |
| 338 |
{ |
| 339 |
while (it != it_end) |
| 340 |
{ |
| 341 |
v.push_back(*it); |
| 342 |
++it; |
| 343 |
} |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
while (it != it_end) |
| 348 |
{ |
| 349 |
if ((**it).get_tiling_index() == tiling_index) |
| 350 |
{ |
| 351 |
v.push_back(*it); |
| 352 |
} |
| 353 |
++it; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
unsigned get_feature_num() const |
| 359 |
{ |
| 360 |
return feature_num; |
| 361 |
} |
| 362 |
|
| 363 |
unsigned get_start() const |
| 364 |
{ |
| 365 |
return start; |
| 366 |
} |
| 367 |
|
| 368 |
unsigned get_number_of_tiles() const |
| 369 |
{ |
| 370 |
return number_of_tiles; |
| 371 |
} |
| 372 |
|
| 373 |
unsigned get_bait_length() const |
| 374 |
{ |
| 375 |
return bait_length; |
| 376 |
} |
| 377 |
|
| 378 |
faststring get_bait_region_name_string() |
| 379 |
{ |
| 380 |
return original_file_name + "|" + gene_name + "|" + faststring(feature_num) + "|" + faststring(start); |
| 381 |
} |
| 382 |
|
| 383 |
// TODO: Change this to FILE * instead of using ostream. |
| 384 |
// Known formats are 'f', 's' for the 2 parameter version of this function. |
| 385 |
void print(std::ostream &os, char format) |
| 386 |
{ |
| 387 |
std::vector<CBait *>::iterator i1, i2, i3; |
| 388 |
// faststring name; |
| 389 |
unsigned count = 1; |
| 390 |
|
| 391 |
i1 = bait_vec.begin(); |
| 392 |
i2 = bait_vec.end(); |
| 393 |
|
| 394 |
if (format == 'f') // fasta |
| 395 |
{ |
| 396 |
// name = ">" + original_file_name + "|" + gene_name + "|" + faststring(feature_num) + "|" + faststring(start); |
| 397 |
|
| 398 |
while (i1 != i2) |
| 399 |
{ |
| 400 |
os << ">" << get_bait_region_name_string() << "|" << (**i1).get_tiling_index(); |
| 401 |
os << "|" << count << std::endl; |
| 402 |
os << (**i1).get_bait() << std::endl; |
| 403 |
++i1; |
| 404 |
++count; |
| 405 |
} |
| 406 |
|
| 407 |
} // END if (format == 'f') // fasta |
| 408 |
else if (format == 's') // standard - same as input |
| 409 |
{ |
| 410 |
i3 = i2; |
| 411 |
--i3; |
| 412 |
os << original_file_name << "\t" |
| 413 |
<< gene_name << "\t" |
| 414 |
<< feature_num << "\t" |
| 415 |
<< num_features << "\t" |
| 416 |
<< start << "\t" |
| 417 |
<< num_sequences_in_region << "\t" |
| 418 |
<< num_baits << "\t"; |
| 419 |
|
| 420 |
// Works for 0, 1, or more elements in vector: |
| 421 |
if (i1 != i2) // Should always be the case, but be cautious |
| 422 |
{ |
| 423 |
while (true) |
| 424 |
{ |
| 425 |
|
| 426 |
(**i1).print(os); |
| 427 |
if (i1 == i3) |
| 428 |
break; |
| 429 |
os << ","; |
| 430 |
++i1; |
| 431 |
++count; |
| 432 |
} |
| 433 |
} |
| 434 |
os << std::endl; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
// TODO: Change this to FILE * instead of using ostream. |
| 439 |
// Known formats are 'A' for the 3 parameter version of this function. |
| 440 |
void print(std::ostream &os, char format, const char *probeIDprefix) |
| 441 |
{ |
| 442 |
std::vector<CBait *>::iterator i1, i2, i3; |
| 443 |
// faststring name; |
| 444 |
|
| 445 |
i1 = bait_vec.begin(); |
| 446 |
i2 = bait_vec.end(); |
| 447 |
|
| 448 |
if (format == 'A') // Agilent-Germany format: |
| 449 |
{ |
| 450 |
unsigned t_index, t_index_old = 0; |
| 451 |
unsigned count = 1; |
| 452 |
|
| 453 |
while (i1 != i2) |
| 454 |
{ |
| 455 |
t_index = (**i1).get_tiling_index(); |
| 456 |
|
| 457 |
if (t_index != t_index_old) |
| 458 |
{ |
| 459 |
t_index_old = t_index; |
| 460 |
count = 1; |
| 461 |
} |
| 462 |
|
| 463 |
os << gene_name << "_" << feature_num << '\t' |
| 464 |
<< probeIDprefix |
| 465 |
<< gene_name << "_" |
| 466 |
<< feature_num << "_" |
| 467 |
<< num_features << "_" |
| 468 |
<< start << "_" |
| 469 |
<< t_index << "_" |
| 470 |
<< count << '\t' |
| 471 |
<< (**i1).get_bait() |
| 472 |
<< "\t1" << std::endl; |
| 473 |
++i1; |
| 474 |
++count; |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
void print_stats(std::ostream &os) |
| 482 |
{ |
| 483 |
os << "Locus: " << gene_name << " " << feature_num << " " << start << " Num baits: " << bait_vec.size() << " bait-length :" << bait_length << std::endl; |
| 484 |
} |
| 485 |
|
| 486 |
unsigned get_num_baits() |
| 487 |
{ |
| 488 |
return num_baits; |
| 489 |
} |
| 490 |
|
| 491 |
unsigned get_num_sequences() |
| 492 |
{ |
| 493 |
return num_sequences_in_region; |
| 494 |
} |
| 495 |
|
| 496 |
// Can be used to compute mean values together with get_num_baits() |
| 497 |
double get_sum_max_distances() |
| 498 |
{ |
| 499 |
int i, N=num_baits; |
| 500 |
double sum=0; |
| 501 |
|
| 502 |
for (i=0; i<N; ++i) |
| 503 |
{ |
| 504 |
sum += bait_vec[i]->get_max_distance(); |
| 505 |
} |
| 506 |
return sum; |
| 507 |
} |
| 508 |
|
| 509 |
double get_overall_max_dist() |
| 510 |
{ |
| 511 |
int i, N = num_baits; |
| 512 |
double overall_max = -1000; // True values must be in the range 0..1 |
| 513 |
double dist; |
| 514 |
|
| 515 |
for (i=0; i<N; ++i) |
| 516 |
{ |
| 517 |
dist = bait_vec[i]->get_max_distance(); |
| 518 |
if (dist > overall_max) |
| 519 |
overall_max = dist; |
| 520 |
} |
| 521 |
return overall_max; |
| 522 |
} |
| 523 |
|
| 524 |
// Can be used to compute mean values together with get_num_baits() |
| 525 |
double get_sum_CG() |
| 526 |
{ |
| 527 |
int i, N=num_baits; |
| 528 |
double sum=0; |
| 529 |
|
| 530 |
for (i=0; i<N; ++i) |
| 531 |
{ |
| 532 |
sum += bait_vec[i]->get_CG_content(); |
| 533 |
} |
| 534 |
return sum; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
friend bool num_baits_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b) |
| 539 |
{ |
| 540 |
// First criterion: |
| 541 |
if (a->num_baits != b->num_baits) |
| 542 |
return a->num_baits < b->num_baits; |
| 543 |
|
| 544 |
// Second criterion: (opposite to first, therefore we use > and not <: |
| 545 |
if (a->num_sequences_in_region != b->num_sequences_in_region) |
| 546 |
return a->num_sequences_in_region > b->num_sequences_in_region; |
| 547 |
|
| 548 |
// Otherwise we sort by starting position. This makes the program output independent of |
| 549 |
// different versions of the sort algorithm. They are guaranteed to differ in different alignments files. |
| 550 |
return a->start < b->start; |
| 551 |
} |
| 552 |
|
| 553 |
// Not used: |
| 554 |
friend bool num_sequences_in_region_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b) |
| 555 |
{ |
| 556 |
// For loci based on an equal number of sequences, we prefer the one with less needed baits. |
| 557 |
// Note: This comparator sorts in the order greater is better, less is worse. |
| 558 |
// Better loci come later in a sorted list. |
| 559 |
|
| 560 |
// First criterion: |
| 561 |
if (a->num_sequences_in_region != b->num_sequences_in_region) |
| 562 |
return a->num_sequences_in_region < b->num_sequences_in_region; |
| 563 |
|
| 564 |
// Second criterion: (opposite to first, therefore we use > and not <: |
| 565 |
if (a->num_baits != b->num_baits) |
| 566 |
return a->num_baits > b->num_baits; |
| 567 |
|
| 568 |
// Otherwise we sort by starting position. This makes the program output independent of |
| 569 |
// different versions of the sort algorithm. They are guaranteed to differ in different alignments files. |
| 570 |
return a->start < b->start; |
| 571 |
} |
| 572 |
|
| 573 |
// First criterion: gene_name |
| 574 |
// Second criterion: feature num |
| 575 |
// Third criterion: locus start coordinate |
| 576 |
friend bool gene_name__feature_num_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b) |
| 577 |
{ |
| 578 |
// First criterion |
| 579 |
if (a->gene_name != b->gene_name) |
| 580 |
return a->gene_name < b->gene_name; |
| 581 |
|
| 582 |
// Second criterion |
| 583 |
if (a->feature_num != b->feature_num) |
| 584 |
return a->feature_num < b->feature_num; |
| 585 |
|
| 586 |
// Third criterion |
| 587 |
return a->start < b->start; |
| 588 |
} |
| 589 |
|
| 590 |
/* // not used: */ |
| 591 |
/* friend bool num_baits_greaterThanCBaitLocus(CBaitLocus *a, CBaitLocus *b) */ |
| 592 |
/* { */ |
| 593 |
/* // First criterion: */ |
| 594 |
/* if (a->num_baits != b->num_baits) */ |
| 595 |
/* return a->num_baits > b->num_baits; */ |
| 596 |
|
| 597 |
/* // Second criterion: (for the sequence coverage, more is better , therefore we use > and not <: */ |
| 598 |
/* if (a->num_sequences_in_region != b->num_sequences_in_region) */ |
| 599 |
/* return a->num_sequences_in_region < b->num_sequences_in_region; */ |
| 600 |
|
| 601 |
/* // Otherwise we sort by starting position. This makes the program output independent of */ |
| 602 |
/* // different versions of the sort algorithm. They are guaranteed to differ in different alignments files. */ |
| 603 |
/* return a->start < b->start; */ |
| 604 |
/* } */ |
| 605 |
|
| 606 |
friend bool num_sequences_in_region_greaterThanCBaitLocus(CBaitLocus *a, CBaitLocus *b) |
| 607 |
{ |
| 608 |
// First criterion: more sequence (coverage) |
| 609 |
if (a->num_sequences_in_region != b->num_sequences_in_region) |
| 610 |
return a->num_sequences_in_region > b->num_sequences_in_region; |
| 611 |
|
| 612 |
// Second criterion: less baits, opposite comparison |
| 613 |
if (a->num_baits != b->num_baits) |
| 614 |
return a->num_baits < b->num_baits; |
| 615 |
|
| 616 |
// Otherwise we sort by starting position. This makes the program output independent of |
| 617 |
// different versions of the sort algorithm. They are guaranteed to differ in different alignments files. |
| 618 |
return a->start < b->start; |
| 619 |
} |
| 620 |
|
| 621 |
friend bool gene_name__feature_num_greaterThanCBaitLocus(CBaitLocus *a, CBaitLocus *b) |
| 622 |
{ |
| 623 |
if (a->gene_name != b->gene_name) |
| 624 |
return a->gene_name > b->gene_name; |
| 625 |
|
| 626 |
if (a->feature_num != b->feature_num) |
| 627 |
return a->feature_num > b->feature_num; |
| 628 |
|
| 629 |
return a->start > b->start; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
friend bool locus_start_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b) |
| 634 |
{ |
| 635 |
// First criterion: |
| 636 |
if (a->gene_name != b->gene_name) |
| 637 |
return a->gene_name < b->gene_name; |
| 638 |
|
| 639 |
// Second criterion: |
| 640 |
return a->start < b->start; |
| 641 |
} |
| 642 |
|
| 643 |
/* friend bool original_file_name__feature_num_start_lessThanCBaitLocus(CBaitLocus *a, CBaitLocus *b) */ |
| 644 |
/* { */ |
| 645 |
/* // First criterion: */ |
| 646 |
/* if (a->original_file_name != b->original_file_name) */ |
| 647 |
/* return a->original_file_name < b->original_file_name; */ |
| 648 |
|
| 649 |
/* // Second criterion: */ |
| 650 |
/* if (a->feature_num != b->feature_num) */ |
| 651 |
/* return a->feature_num < b->feature_num; */ |
| 652 |
|
| 653 |
/* // Third criterion: */ |
| 654 |
/* return a->start < b->start; */ |
| 655 |
/* } */ |
| 656 |
|
| 657 |
}; // END CBaitLocus |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
// Contains usually multiple loci of bait regions. |
| 662 |
// Usually, loci come from multiple alignments, genes and features. |
| 663 |
|
| 664 |
class CBaitLoci_collection |
| 665 |
{ |
| 666 |
std::vector<CBaitLocus*> baitLoci_vec; |
| 667 |
std::map<Ctriple<faststring, unsigned, unsigned>, CBaitLocus*> baitLoci_map; |
| 668 |
bool delete_in_destructor; |
| 669 |
bool has_feature_information_bool; |
| 670 |
|
| 671 |
// unsigned bait_length; // Will be set to 0 in constructor and will be adjusted to its true value in the add method. |
| 672 |
// // The fact that we have one value for a collection of baits implies, that we assume that all baits in all loci have the same length. |
| 673 |
|
| 674 |
// Internal add |
| 675 |
void add_locus(CBaitLocus *p) |
| 676 |
{ |
| 677 |
baitLoci_vec.push_back(p); |
| 678 |
baitLoci_map.insert(std::pair<Ctriple<faststring, unsigned, unsigned>, CBaitLocus*>(Ctriple<faststring, unsigned, unsigned>(p->get_original_file_name(), p->get_feature_num(), p->get_start()), p) ); |
| 679 |
|
| 680 |
if (p->has_feature_information()) |
| 681 |
has_feature_information_bool = true; |
| 682 |
// unsigned new_bait_length = p->get_bait_length(); |
| 683 |
|
| 684 |
/* if (bait_length == 0) */ |
| 685 |
/* bait_length = new_bait_length; */ |
| 686 |
/* else */ |
| 687 |
/* { */ |
| 688 |
/* if (bait_length != new_bait_length) */ |
| 689 |
/* { */ |
| 690 |
/* std::cerr << "ERROR: It is assumed that all baits in all bait loci have identical lenghts." << std::endl; */ |
| 691 |
/* exit(-4); */ |
| 692 |
/* } */ |
| 693 |
/* } */ |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
public: |
| 698 |
CBaitLoci_collection(std::ifstream &is):delete_in_destructor(true), has_feature_information_bool(false) //, bait_length(0) |
| 699 |
{ |
| 700 |
faststring line; |
| 701 |
|
| 702 |
getline(is, line); |
| 703 |
while (!is.eof()) |
| 704 |
{ |
| 705 |
line.removeSpacesBack(); |
| 706 |
line.removeSpacesFront(); |
| 707 |
if (line[0] != '#' && !line.empty() ) |
| 708 |
add_locus(new CBaitLocus(line) ); |
| 709 |
getline(is, line); |
| 710 |
} |
| 711 |
if (!is_partially_ordered() ) |
| 712 |
{ |
| 713 |
std::cerr << "Note: Bait loci in input file are not ordered or genes appear twice." << std::endl; |
| 714 |
std::cerr << "Note: Lets try to order them." << std::endl; |
| 715 |
sort_by_gene_name__feature_num(baitLoci_vec.begin(), baitLoci_vec.end()); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
CBaitLoci_collection(CBaitLoci_collection &source, char genes_or_features, char num_baits_or_sequences):delete_in_destructor(false), has_feature_information_bool(false) //, bait_length(0) |
| 721 |
{ |
| 722 |
/* { */ |
| 723 |
/* std::cout << "++++++++++++++" << std::endl; */ |
| 724 |
/* std::cout << "Full set: DEBUG OUT" << std::endl; */ |
| 725 |
/* source.print_all(std::cout, 's'); */ |
| 726 |
/* std::cout << "++++++++++++++" << std::endl; */ |
| 727 |
/* } */ |
| 728 |
|
| 729 |
|
| 730 |
if (!source.is_partially_ordered() ) |
| 731 |
{ |
| 732 |
// std::cout << "Source loci have to be sorted partially:" << std::endl; |
| 733 |
source.sort_within_genes(gene_name__feature_num_lessThanCBaitLocus); |
| 734 |
if (!source.is_partially_ordered() ) |
| 735 |
if (global_verbosity >= 1) |
| 736 |
{ |
| 737 |
std::cerr << "WARNING: Sorting NOT successful." << std::endl; |
| 738 |
} |
| 739 |
/* else */ |
| 740 |
/* std::cout << "Sorting was successful." << std::endl; */ |
| 741 |
} |
| 742 |
|
| 743 |
/* { */ |
| 744 |
/* std::cout << "**************" << std::endl; */ |
| 745 |
/* std::cout << "Full set: DEBUG OUT" << std::endl; */ |
| 746 |
/* source.print_all(std::cout, 's'); */ |
| 747 |
/* std::cout << "**************" << std::endl; */ |
| 748 |
/* } */ |
| 749 |
|
| 750 |
// Sort such the the best loci is first in each "group" of loci. |
| 751 |
if (genes_or_features == 'g') |
| 752 |
{ |
| 753 |
// std::cout << "Sorting within genes" << std::endl; |
| 754 |
if (num_baits_or_sequences == 'b') |
| 755 |
source.sort_within_genes(num_baits_lessThanCBaitLocus); |
| 756 |
else if (num_baits_or_sequences == 's') |
| 757 |
source.sort_within_genes(num_sequences_in_region_greaterThanCBaitLocus); |
| 758 |
} |
| 759 |
else if (genes_or_features == 'f') |
| 760 |
{ |
| 761 |
// std::cout << "Sorting within features" << std::endl; |
| 762 |
if (num_baits_or_sequences == 'b') |
| 763 |
source.sort_within_featues(num_baits_lessThanCBaitLocus); |
| 764 |
else if (num_baits_or_sequences == 's') |
| 765 |
source.sort_within_featues(num_sequences_in_region_greaterThanCBaitLocus); |
| 766 |
} |
| 767 |
|
| 768 |
/* { */ |
| 769 |
/* std::cout << "##############" << std::endl; */ |
| 770 |
/* std::cout << "Full set: DEBUG OUT" << std::endl; */ |
| 771 |
/* source.print_all(std::cout, 's'); */ |
| 772 |
/* std::cout << "##############" << std::endl; */ |
| 773 |
/* } */ |
| 774 |
|
| 775 |
// Add best to this: |
| 776 |
std::vector<CBaitLocus*>::iterator it, it_end, it_tmp; |
| 777 |
it = source.baitLoci_vec.begin(); |
| 778 |
it_end = source.baitLoci_vec.end(); |
| 779 |
|
| 780 |
if (genes_or_features == 'g') |
| 781 |
{ |
| 782 |
while (it != it_end) |
| 783 |
{ |
| 784 |
add_locus(*it); |
| 785 |
// baitLoci_vec.push_back(*it); |
| 786 |
it = source.find_end_this_gene(it); |
| 787 |
} |
| 788 |
} |
| 789 |
else if (genes_or_features == 'f') |
| 790 |
{ |
| 791 |
while (it != it_end) |
| 792 |
{ |
| 793 |
// std::cout << "Next it:" << std::endl; |
| 794 |
// (**it).print(std::cout, 's'); |
| 795 |
// std::cout << "==" << std::endl; |
| 796 |
add_locus(*it); |
| 797 |
// baitLoci_vec.push_back(*it); |
| 798 |
it = source.find_end_this_feature(it); |
| 799 |
} |
| 800 |
} |
| 801 |
// We restore the original sorting of the source CBaitLoci_collection |
| 802 |
sort_within_genes(gene_name__feature_num_lessThanCBaitLocus); |
| 803 |
} // END CBaitLoci_collection(CBaitLoci_collection &source, char genes_or_features, char num_baits_or_sequences) |
| 804 |
|
| 805 |
|
| 806 |
CBaitLoci_collection(const CBaitLoci_collection &source, const CBaitRegionSelectorSets &lss, char mode):delete_in_destructor(false), has_feature_information_bool(false) // , bait_length(0) |
| 807 |
{ |
| 808 |
unsigned i, N; |
| 809 |
N=source.baitLoci_vec.size(); |
| 810 |
|
| 811 |
if (mode == 'b') // Remove all loci from ALIGNMENT if a double hit occurs in this set |
| 812 |
{ |
| 813 |
for (i=0; i<N; ++i) |
| 814 |
{ |
| 815 |
CBaitLocus *p = source.baitLoci_vec[i]; |
| 816 |
|
| 817 |
if (!lss.exists(p->get_original_file_name()) ) |
| 818 |
{ |
| 819 |
add_locus(p); |
| 820 |
// baitLoci_vec.push_back(p); |
| 821 |
} |
| 822 |
} |
| 823 |
} |
| 824 |
else if (mode == 'B') // Remove all loci from FEATURE if a double hit occurs in this set |
| 825 |
{ |
| 826 |
for (i=0; i<N; ++i) |
| 827 |
{ |
| 828 |
CBaitLocus *p = source.baitLoci_vec[i]; |
| 829 |
|
| 830 |
if (!lss.exists(p->get_original_file_name(), p->get_feature_num()) ) |
| 831 |
{ |
| 832 |
add_locus(p); |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
if (mode == 'x') // Remove Locus if a double hit occurs in it |
| 837 |
{ |
| 838 |
for (i=0; i<N; ++i) |
| 839 |
{ |
| 840 |
CBaitLocus *p = source.baitLoci_vec[i]; |
| 841 |
|
| 842 |
if (!lss.exists(p->get_original_file_name(), p->get_feature_num(), p->get_start()) ) |
| 843 |
{ |
| 844 |
add_locus(p); |
| 845 |
} |
| 846 |
} |
| 847 |
} |
| 848 |
else if (mode == 'C') // In this mode we simply copy the loci to this collector. |
| 849 |
{ |
| 850 |
for (i=0; i<N; ++i) |
| 851 |
{ |
| 852 |
CBaitLocus *p = source.baitLoci_vec[i]; |
| 853 |
add_locus(p); |
| 854 |
} |
| 855 |
} |
| 856 |
else |
| 857 |
{ |
| 858 |
std::cerr << "Error: Unknown mode in constructor: CBaitLoci_collection(const CBaitLoci_collection &source, const CBaitRegionSelectorSets &lss, char mode): " << std::endl; |
| 859 |
std::cerr << "Bad mode character: " << mode << std::endl; |
| 860 |
exit(-1); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
CBaitLoci_collection(const CBaitLoci_collection &source, CBlast_parser &bp, double minimum_coverage):delete_in_destructor(false), has_feature_information_bool(false) //, bait_length(source.bait_length) |
| 866 |
{ |
| 867 |
unsigned tile_index, number_of_tiles; |
| 868 |
faststring recognition_string; |
| 869 |
double best_coverage_this_tiling_stack; |
| 870 |
bool bait_region_has_tiling_stack_with_insufficient_coverage; |
| 871 |
int max_hit_length, bait_length_this_bait_region; |
| 872 |
unsigned count, N; |
| 873 |
|
| 874 |
std::vector<CBaitLocus*>::const_iterator it, it_end; |
| 875 |
|
| 876 |
it = source.baitLoci_vec.begin(); |
| 877 |
it_end = source.baitLoci_vec.end(); |
| 878 |
|
| 879 |
N = source.baitLoci_vec.size(); |
| 880 |
count = 0; |
| 881 |
|
| 882 |
// for each bait region |
| 883 |
while (it != it_end) |
| 884 |
{ |
| 885 |
++count; |
| 886 |
number_of_tiles = (**it).get_number_of_tiles(); |
| 887 |
bait_length_this_bait_region = (**it).get_bait_length();; |
| 888 |
/* std::cout << "Number of tiles for: " << (**it).get_bait_region_name_string() */ |
| 889 |
/* << " " << number_of_tiles << std::endl; */ |
| 890 |
|
| 891 |
if (bait_length_this_bait_region == 0) |
| 892 |
{ |
| 893 |
// We have a problem. Something seems to have gone wrong. This should not happen. |
| 894 |
// We catch this potential error, since it leads to division by 0 and a crash would be expected. |
| 895 |
std::cerr << "The bait length has been determined to be 0 for the bait region: " << std::endl; |
| 896 |
(**it).print(std::cerr, 's'); |
| 897 |
std::cerr << "This bug should be reported. It should not occur. It might be caused by malformatted input files," |
| 898 |
" but the error should be caught earlier than here." << std::endl; |
| 899 |
exit(-5); |
| 900 |
} |
| 901 |
|
| 902 |
if (global_verbosity >= 3) |
| 903 |
{ |
| 904 |
std::cout << "Handling region: " << count << " outof " << N << std::endl; |
| 905 |
} |
| 906 |
|
| 907 |
// Within each bait region determine the maximum hit coverage of all baits |
| 908 |
// For each tiling stack of baits: |
| 909 |
|
| 910 |
// Each tiling stack has to be checked individually. |
| 911 |
// A single tiling stack with insufficient query coverage should result in the exclusion of this bait region. |
| 912 |
bait_region_has_tiling_stack_with_insufficient_coverage = false; |
| 913 |
for (tile_index=1; tile_index <= number_of_tiles; ++tile_index) |
| 914 |
{ |
| 915 |
// Do something with (**it): |
| 916 |
// Determine string to identify blast hits of baits of this bait region and tiling_index against the genome. |
| 917 |
recognition_string = (**it).get_bait_region_name_string() + "|" + tile_index + "|"; |
| 918 |
|
| 919 |
// std::cout << "T1: " << time(NULL) << std::endl; |
| 920 |
max_hit_length = bp.get_max_hit_query_span__matching_beginning2(recognition_string, global_verbosity); |
| 921 |
// std::cout << "T2: " << time(NULL) << std::endl; |
| 922 |
|
| 923 |
best_coverage_this_tiling_stack = (double) max_hit_length/bait_length_this_bait_region; |
| 924 |
|
| 925 |
if (global_verbosity >= 50) |
| 926 |
{ |
| 927 |
std::cout << "Tile: " << tile_index << " " << recognition_string << " " << max_hit_length << "/" << bait_length_this_bait_region << "=" |
| 928 |
<< best_coverage_this_tiling_stack << std::endl; |
| 929 |
} |
| 930 |
|
| 931 |
if (best_coverage_this_tiling_stack < 0) |
| 932 |
{ |
| 933 |
if (global_verbosity >= 50) |
| 934 |
{ |
| 935 |
std::cout << "No hit for recognition string: " << recognition_string << " --> " |
| 936 |
<< 0.00 << std::endl; |
| 937 |
} |
| 938 |
} |
| 939 |
else |
| 940 |
{ |
| 941 |
if (global_verbosity >= 50) |
| 942 |
{ |
| 943 |
std::cout << "Determined best coverage of this tiling design column: " << recognition_string << " --> " |
| 944 |
<< best_coverage_this_tiling_stack << std::endl; |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
if (best_coverage_this_tiling_stack < minimum_coverage) |
| 949 |
bait_region_has_tiling_stack_with_insufficient_coverage = true; |
| 950 |
} |
| 951 |
|
| 952 |
if (!bait_region_has_tiling_stack_with_insufficient_coverage) |
| 953 |
{ |
| 954 |
add_locus(*it); |
| 955 |
} |
| 956 |
++it; |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
~CBaitLoci_collection() |
| 961 |
{ |
| 962 |
if (delete_in_destructor) |
| 963 |
{ |
| 964 |
std::vector<CBaitLocus*>::iterator it, it_end; |
| 965 |
it = baitLoci_vec.begin(); |
| 966 |
it_end = baitLoci_vec.end(); |
| 967 |
|
| 968 |
while (it != it_end) |
| 969 |
{ |
| 970 |
delete *it; |
| 971 |
++it; |
| 972 |
} |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
bool has_feature_information() const |
| 977 |
{ |
| 978 |
return has_feature_information_bool; |
| 979 |
} |
| 980 |
|
| 981 |
void get_map_of_baits_to_locus_info(faststring original_file_name_match, unsigned tiling_index, std::map<faststring, CBaitLocus *> &bait_map) |
| 982 |
{ |
| 983 |
// sort_by_original_file_name__feature_num_start(); |
| 984 |
|
| 985 |
std::vector<CBaitLocus*>::iterator it_Locus, it_Locus_end; |
| 986 |
it_Locus = baitLoci_vec.begin(); |
| 987 |
it_Locus_end = baitLoci_vec.end(); |
| 988 |
|
| 989 |
std::vector<CBait *> tmp_bait_vec; |
| 990 |
|
| 991 |
unsigned i, N; |
| 992 |
|
| 993 |
std::map<faststring, CBaitLocus *>::iterator find_it; |
| 994 |
|
| 995 |
while (it_Locus != it_Locus_end) |
| 996 |
{ |
| 997 |
if ( (**it_Locus).get_original_file_name().find(original_file_name_match) != faststring::npos) |
| 998 |
{ |
| 999 |
(**it_Locus).get_vector_of_baits(tiling_index, tmp_bait_vec); |
| 1000 |
|
| 1001 |
N=tmp_bait_vec.size(); |
| 1002 |
for (i=0; i<N; ++i) |
| 1003 |
{ |
| 1004 |
faststring &thebait = tmp_bait_vec[i]->get_bait(); |
| 1005 |
find_it = bait_map.find(thebait); |
| 1006 |
if (find_it == bait_map.end()) // Insert, if not present yet. |
| 1007 |
bait_map[thebait] = *it_Locus; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
++it_Locus; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
/* void clean_NULL_elements_form_baitLoci_vec() */ |
| 1015 |
/* { */ |
| 1016 |
/* std::vector<CBaitLocus*>::iterator it_end, it1, it2; */ |
| 1017 |
/* unsigned count = 0; */ |
| 1018 |
|
| 1019 |
/* it1 = it2 = baitLoci_vec.begin(); */ |
| 1020 |
/* it_end = baitLoci_vec.end(); */ |
| 1021 |
|
| 1022 |
/* // Advance it2 until we have the first NULL element: */ |
| 1023 |
/* while (*it2 != NULL && it2 != it_end) */ |
| 1024 |
/* { */ |
| 1025 |
/* ++it1; */ |
| 1026 |
/* ++it2; */ |
| 1027 |
/* ++count; */ |
| 1028 |
/* } */ |
| 1029 |
|
| 1030 |
/* // if it2 is not at the end, it will point to a NULL element */ |
| 1031 |
/* while (it2 != it_end) */ |
| 1032 |
/* { */ |
| 1033 |
/* // Find next non-NULL element: */ |
| 1034 |
/* while (*it2 == NULL && it2 != it_end) */ |
| 1035 |
/* { */ |
| 1036 |
/* ++it2; */ |
| 1037 |
/* } */ |
| 1038 |
/* if (it2 == it_end) */ |
| 1039 |
/* break; */ |
| 1040 |
/* //Copy: */ |
| 1041 |
/* *it1 = *it2; */ |
| 1042 |
/* ++count; */ |
| 1043 |
|
| 1044 |
/* ++it1; */ |
| 1045 |
/* ++it2; */ |
| 1046 |
/* } */ |
| 1047 |
|
| 1048 |
/* // Resize vector: */ |
| 1049 |
/* baitLoci_vec.resize(count, 0); */ |
| 1050 |
|
| 1051 |
/* } */ |
| 1052 |
|
| 1053 |
|
| 1054 |
/* void sort_by_num_baits(std::vector<CBaitLocus*>::iterator it, std::vector<CBaitLocus*>::iterator it_end) */ |
| 1055 |
/* { */ |
| 1056 |
/* MYSORT (it, it_end, num_baits_lessThanCBaitLocus); */ |
| 1057 |
/* } */ |
| 1058 |
|
| 1059 |
/* void sort_by_num_sequences(std::vector<CBaitLocus*>::iterator it, std::vector<CBaitLocus*>::iterator it_end) */ |
| 1060 |
/* { */ |
| 1061 |
/* MYSORT (it, it_end, num_sequences_in_region_greaterThanCBaitLocus); */ |
| 1062 |
/* } */ |
| 1063 |
|
| 1064 |
void sort_by_gene_name__feature_num(std::vector<CBaitLocus*>::iterator it, std::vector<CBaitLocus*>::iterator it_end) |
| 1065 |
{ |
| 1066 |
MYSORT (it, it_end, gene_name__feature_num_lessThanCBaitLocus); |
| 1067 |
} |
| 1068 |
|
| 1069 |
unsigned get_number_of_loci() const |
| 1070 |
{ |
| 1071 |
return baitLoci_vec.size(); |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
void print_stats(std::ostream &os) |
| 1076 |
{ |
| 1077 |
unsigned numBaits = 0; |
| 1078 |
|
| 1079 |
os << "Number of loci: " << baitLoci_vec.size() << std::endl; |
| 1080 |
|
| 1081 |
std::vector<CBaitLocus*>::iterator it, it_end; |
| 1082 |
it = baitLoci_vec.begin(); |
| 1083 |
it_end = baitLoci_vec.end(); |
| 1084 |
|
| 1085 |
while (it != it_end) |
| 1086 |
{ |
| 1087 |
(**it).print_stats(os); |
| 1088 |
numBaits += (**it).get_num_baits(); |
| 1089 |
++it; |
| 1090 |
} |
| 1091 |
os << "Total number of baits: " << numBaits << std::endl; |
| 1092 |
// os << "Bait length: " << bait_length << std::endl; |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
////////////////// |
| 1097 |
// We now impelmented this in a constructor to be consistent with other filtering steps. |
| 1098 |
////////////////// |
| 1099 |
/* void filter_hit_coverage(double minimum_coverage, CBlast_parser &bp) */ |
| 1100 |
/* { */ |
| 1101 |
/* unsigned tile_index, number_of_tiles; */ |
| 1102 |
/* faststring recognition_string; */ |
| 1103 |
/* double best_coverage_this_tiling_stack; */ |
| 1104 |
/* bool bait_region_has_tiling_stack_with_insufficient_coverage; */ |
| 1105 |
|
| 1106 |
/* std::vector<CBaitLocus*>::iterator it, it_end; */ |
| 1107 |
|
| 1108 |
/* it = baitLoci_vec.begin(); */ |
| 1109 |
/* it_end = baitLoci_vec.end(); */ |
| 1110 |
|
| 1111 |
/* // for each bait region */ |
| 1112 |
/* while (it != it_end) */ |
| 1113 |
/* { */ |
| 1114 |
/* number_of_tiles = (**it).get_number_of_tiles(); */ |
| 1115 |
/* /\* std::cout << "Number of tiles for: " << (**it).get_bait_region_name_string() *\/ */ |
| 1116 |
/* /\* << " " << number_of_tiles << std::endl; *\/ */ |
| 1117 |
|
| 1118 |
/* // Within each bait region determine the maximum hit coverage of all baits */ |
| 1119 |
/* // For each tiling stack of baits: */ |
| 1120 |
|
| 1121 |
/* // Each tiling stack has to be checked individually. */ |
| 1122 |
/* // A single tiling stack with insufficient query coverage should result in the exclusion of this bait region. */ |
| 1123 |
/* bait_region_has_tiling_stack_with_insufficient_coverage = false; */ |
| 1124 |
/* for (tile_index=1; tile_index <= number_of_tiles; ++tile_index) */ |
| 1125 |
/* { */ |
| 1126 |
/* // Do something with (**it): */ |
| 1127 |
/* // Determine string to identify blast hits of baits of this bait region and tiling_index against the genome. */ |
| 1128 |
/* recognition_string = (**it).get_bait_region_name_string() + "|" + tile_index + "|"; */ |
| 1129 |
|
| 1130 |
/* best_coverage_this_tiling_stack = (double) bp.get_max_hit_query_span__matching_beginning1(recognition_string)/bait_length; */ |
| 1131 |
|
| 1132 |
/* if (best_coverage_this_tiling_stack < 0) */ |
| 1133 |
/* { */ |
| 1134 |
/* std::cout << "No hit for recognition string: " << recognition_string << " --> " */ |
| 1135 |
/* << 0.00 << std::endl; */ |
| 1136 |
/* } */ |
| 1137 |
/* else */ |
| 1138 |
/* { */ |
| 1139 |
/* std::cout << "Determined best coverage of this tiling stack: " << recognition_string << " --> " */ |
| 1140 |
/* << best_coverage_this_tiling_stack << std::endl; */ |
| 1141 |
/* } */ |
| 1142 |
|
| 1143 |
/* if (best_coverage_this_tiling_stack < minimum_coverage) */ |
| 1144 |
/* bait_region_has_tiling_stack_with_insufficient_coverage = true; */ |
| 1145 |
/* } */ |
| 1146 |
|
| 1147 |
/* if (bait_region_has_tiling_stack_with_insufficient_coverage) */ |
| 1148 |
/* { */ |
| 1149 |
/* if (delete_in_destructor) */ |
| 1150 |
/* delete *it; */ |
| 1151 |
/* *it = NULL; */ |
| 1152 |
/* } */ |
| 1153 |
/* } */ |
| 1154 |
|
| 1155 |
/* clean_NULL_elements_form_baitLoci_vec(); */ |
| 1156 |
/* } */ |
| 1157 |
|
| 1158 |
|
| 1159 |
void print_all(std::ostream &os, char format) |
| 1160 |
{ |
| 1161 |
std::vector<CBaitLocus*>::iterator it, it_end; |
| 1162 |
it = baitLoci_vec.begin(); |
| 1163 |
it_end = baitLoci_vec.end(); |
| 1164 |
|
| 1165 |
// TODO: In case we have no alignment cutting we should use an alternative heading in the output file. |
| 1166 |
// This is not urgent, just more elegant. It requires some work to get the information to this point. |
| 1167 |
if (format == 's') |
| 1168 |
{ |
| 1169 |
// if (alignment_cutting) |
| 1170 |
os << "# Original-file-name\tGene-name\tFeature-number\tNumber-of-features-of-specified-type-for-this-gene-in-gff-file\tStart-coordinate-of-bait-region-in-alignment\tNumber-of-sequences-in-region-the-baits-are-based-on\tNumber-of-baits-constructed-for-this-bait-region\tList of baits - for each bait the tiling design column is given, then the bait string, its CG content and its maximum distance to the sequences in the alignment." << std::endl; |
| 1171 |
// else |
| 1172 |
// os << "# Corresponding sequence name\tGene-name\tn.a.\tn.a.\tStart-coordinate-of-bait-region-in-alignment\t Number-of-sequences-in-region-the-baits-are-based-on\tNumber-of-baits-constructed-for-this-bait-region\tList of baits - for each bait the tiling design column is given, then the bait string, its CG content and its maximum distance to the sequences in the alignment." << std::endl; |
| 1173 |
} |
| 1174 |
|
| 1175 |
if ( !(format == 's' || format == 'f' ) ) |
| 1176 |
{ |
| 1177 |
std::cerr << "ERROR: Unknown internal format " << format << " found in call to CBaitLoci_collection::print_all(2 parameters)" << std::endl; |
| 1178 |
exit(-24); |
| 1179 |
} |
| 1180 |
|
| 1181 |
while (it != it_end) |
| 1182 |
{ |
| 1183 |
(**it).print(os, format); |
| 1184 |
++it; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
void print_all(std::ostream &os, char format, const char *probeIDprefix) |
| 1190 |
{ |
| 1191 |
std::vector<CBaitLocus*>::iterator it, it_end; |
| 1192 |
it = baitLoci_vec.begin(); |
| 1193 |
it_end = baitLoci_vec.end(); |
| 1194 |
|
| 1195 |
if (format == 'A') |
| 1196 |
{ |
| 1197 |
os << "TargetID\tProbeID\tSequence\tReplication" << std::endl; |
| 1198 |
} |
| 1199 |
else |
| 1200 |
{ |
| 1201 |
std::cerr << "ERROR: Unknown internal format " << format << " found in call to CBaitLoci_collection::print_all(3 parameters)" << std::endl; |
| 1202 |
exit(-25); |
| 1203 |
} |
| 1204 |
|
| 1205 |
while (it != it_end) |
| 1206 |
{ |
| 1207 |
(**it).print(os, format, probeIDprefix); |
| 1208 |
++it; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
void fill_locus_selector(CBaitRegionSelectorSets &ls) const |
| 1214 |
{ |
| 1215 |
std::vector<CBaitLocus*>::const_iterator it, it_end; |
| 1216 |
it = baitLoci_vec.begin(); |
| 1217 |
it_end = baitLoci_vec.end(); |
| 1218 |
|
| 1219 |
while (it != it_end) |
| 1220 |
{ |
| 1221 |
ls.add( (**it).get_gene_name(), |
| 1222 |
(**it).get_feature_num(), |
| 1223 |
(**it).get_start() ); |
| 1224 |
++it; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
void print_with_start_stepwidth(std::ostream &os, char format, unsigned start, unsigned step) |
| 1230 |
{ |
| 1231 |
// TODO: In case we have no alignment cutting we should use an alternative heading in the output file. |
| 1232 |
// This is not urgent, just more elegant. It requires some work to get the information to this point. |
| 1233 |
if (format == 's') |
| 1234 |
{ |
| 1235 |
// if (alignment_cutting) |
| 1236 |
os << "# Original-file-name\tGene-name\tFeature-number\tNumber-of-features-of-specified-type-for-this-gene-in-gff-file\tStart-coordinate-of-bait-region-in-alignment\tNumber-of-sequences-in-region-the-baits-are-based-on\tNumber-of-baits-constructed-for-this-bait-region\tList of baits - for each bait the tiling design column is given, then the bait string, its CG content and its maximum distance to the sequences in the alignment." << std::endl; |
| 1237 |
// else |
| 1238 |
// os << "# Corresponding sequence name\tGene-name\tn.a.\tn.a.\tStart-coordinate-of-bait-region-in-alignment\t Number-of-sequences-in-region-the-baits-are-based-on\tNumber-of-baits-constructed-for-this-bait-region\tList of baits - for each bait the tiling design column is given, then the bait string, its CG content and its maximum distance to the sequences in the alignment." << std::endl; |
| 1239 |
} |
| 1240 |
|
| 1241 |
unsigned i,N = baitLoci_vec.size(); |
| 1242 |
|
| 1243 |
i = start-1; // start is 1 based, i is 0 based. Therefore we have to subtract 1. |
| 1244 |
while (i < N) |
| 1245 |
{ |
| 1246 |
baitLoci_vec[i]->print(os, format); |
| 1247 |
i += step; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
void print_with_start_stepwidth_multi_gene_feature_aware(std::ostream &os, char format, unsigned start_count, unsigned offset_count, |
| 1252 |
std::vector<CBaitLocus*>::iterator it_start_this_alignment) |
| 1253 |
{ |
| 1254 |
if (format == 's') |
| 1255 |
{ |
| 1256 |
// if (alignment_cutting) |
| 1257 |
os << "# Original-file-name\tGene-name\tFeature-number\tNumber-of-features-of-specified-type-for-this-gene-in-gff-file\tStart-coordinate-of-bait-region-in-alignment\tNumber-of-sequences-in-region-the-baits-are-based-on\tNumber-of-baits-constructed-for-this-bait-region\tList of baits - for each bait the tiling design column is given, then the bait string, its CG content and its maximum distance to the sequences in the alignment." << std::endl; |
| 1258 |
// else |
| 1259 |
// os << "# Corresponding sequence name\tGene-name\tn.a.\tn.a.\tStart-coordinate-of-bait-region-in-alignment\t Number-of-sequences-in-region-the-baits-are-based-on\tNumber-of-baits-constructed-for-this-bait-region\tList of baits - for each bait the tiling design column is given, then the bait string, its CG content and its maximum distance to the sequences in the alignment." << std::endl; |
| 1260 |
} |
| 1261 |
|
| 1262 |
std::vector<CBaitLocus*>::iterator it_tmp, it; |
| 1263 |
|
| 1264 |
it = it_start_this_alignment; |
| 1265 |
|
| 1266 |
if ( (**it).has_feature_information() ) |
| 1267 |
it_tmp = find_end_this_feature(it); |
| 1268 |
else |
| 1269 |
it_tmp = find_end_this_gene(it); |
| 1270 |
|
| 1271 |
// First we determine the starting point for the iterator it "at start_count" |
| 1272 |
unsigned i=0, startdiff, laststart, start; |
| 1273 |
|
| 1274 |
laststart = (**it).get_start(); |
| 1275 |
while (i < start_count) // We won't walk past the end of this feature or alignment |
| 1276 |
{ |
| 1277 |
++it; |
| 1278 |
if (it == it_tmp) |
| 1279 |
break; |
| 1280 |
// If we had to skip possible starting points, we try to correct for this. |
| 1281 |
start = (**it).get_start(); |
| 1282 |
startdiff = start - laststart; // Difference of indices, should be at least 1. |
| 1283 |
i += startdiff; |
| 1284 |
laststart = start; |
| 1285 |
} |
| 1286 |
|
| 1287 |
if (it != it_tmp) // we should never have it == it_tmp, but if we have, we will crash, so we better check. |
| 1288 |
{ |
| 1289 |
// Loop over all loci in steps of offset_count |
| 1290 |
while (true) // We won't walk past the end of this feature or alignment. We break inside the loop if it == it_tmp. |
| 1291 |
{ |
| 1292 |
// This locus will be printed. |
| 1293 |
(*it)->print(os, format); |
| 1294 |
|
| 1295 |
i = 0; // Count the number of loci we skip in next iteration. |
| 1296 |
// Move to next locus we want to print: |
| 1297 |
// We move by one locus position. (The first move.) |
| 1298 |
// We can handle the first move outside the loop below since offset_count must be > 0. |
| 1299 |
++it; |
| 1300 |
if (it == it_tmp) |
| 1301 |
break; |
| 1302 |
|
| 1303 |
// If we had to skip possible starting points, we try to correct for this. |
| 1304 |
start = (**it).get_start(); |
| 1305 |
startdiff = start - laststart; |
| 1306 |
i += startdiff; // Diff is 1 or larger. |
| 1307 |
laststart = start; |
| 1308 |
|
| 1309 |
// Skip offset_count loci. |
| 1310 |
while (it < it_tmp && i < offset_count) |
| 1311 |
{ |
| 1312 |
// Next iteration. |
| 1313 |
++it; |
| 1314 |
if (it == it_tmp) |
| 1315 |
break; |
| 1316 |
start = (**it).get_start(); |
| 1317 |
startdiff = start - laststart; |
| 1318 |
i += startdiff; |
| 1319 |
laststart = start; |
| 1320 |
} |
| 1321 |
|
| 1322 |
// If it == it_tmp, we have an iterator not pointing into this alignment/feature any more. |
| 1323 |
// So we have to break immediately. |
| 1324 |
if (it == it_tmp) |
| 1325 |
break; |
| 1326 |
|
| 1327 |
// In the ideal case i == offset_count. Then we are at the next locus we wanted to count. |
| 1328 |
// If i < offset_count we have not reached the next good locus in this alignment/gene/feature |
| 1329 |
// before we moved past the end. In this case we have it == it_tmp and we leave the |
| 1330 |
// loop with the next while check. |
| 1331 |
// If i > offset_count, then we had to skip more loci than we wanted to skip, since not all |
| 1332 |
// loci are valid starting points. In this case we add a penalty. |
| 1333 |
|
| 1334 |
} // END while true |
| 1335 |
} // END if (it != it_tmp) |
| 1336 |
} // END print_with_start_stepwidth_multi_gene_feature_aware |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
std::vector<CBaitLocus*>::iterator find_end_this_feature(std::vector<CBaitLocus*>::iterator it) |
| 1341 |
{ |
| 1342 |
unsigned the_feature_num = (**it).get_feature_num(); |
| 1343 |
faststring the_gene_name = (**it).get_gene_name(); |
| 1344 |
|
| 1345 |
while (it != baitLoci_vec.end() ) |
| 1346 |
{ |
| 1347 |
if (the_feature_num != (**it).get_feature_num() || the_gene_name != (**it).get_gene_name()) |
| 1348 |
{ |
| 1349 |
break; |
| 1350 |
} |
| 1351 |
++it; |
| 1352 |
} |
| 1353 |
return it; |
| 1354 |
} |
| 1355 |
|
| 1356 |
std::vector<CBaitLocus*>::iterator find_end_this_gene(std::vector<CBaitLocus*>::iterator it) |
| 1357 |
{ |
| 1358 |
faststring the_gene_name = (**it).get_gene_name(); |
| 1359 |
|
| 1360 |
while (it != baitLoci_vec.end() ) |
| 1361 |
{ |
| 1362 |
if (the_gene_name != (**it).get_gene_name()) |
| 1363 |
{ |
| 1364 |
break; |
| 1365 |
} |
| 1366 |
++it; |
| 1367 |
} |
| 1368 |
return it; |
| 1369 |
} |
| 1370 |
|
| 1371 |
void sort_within_featues(bool (*f)(CBaitLocus*,CBaitLocus*)) |
| 1372 |
{ |
| 1373 |
std::vector<CBaitLocus*>::iterator it, it_end, it_tmp; |
| 1374 |
it = baitLoci_vec.begin(); |
| 1375 |
it_end = baitLoci_vec.end(); |
| 1376 |
|
| 1377 |
while (it != it_end) |
| 1378 |
{ |
| 1379 |
// Find end of this gene: |
| 1380 |
it_tmp = find_end_this_feature(it); |
| 1381 |
// std::cout << "Sort range: " << distance(baitLoci_vec.begin(), it) << " " << distance(baitLoci_vec.begin(), it_tmp) << std::endl; |
| 1382 |
MYSORT(it,it_tmp, f); |
| 1383 |
it = it_tmp; |
| 1384 |
} |
| 1385 |
} |
| 1386 |
|
| 1387 |
void sort_within_genes(bool (*f)(CBaitLocus*,CBaitLocus*)) |
| 1388 |
{ |
| 1389 |
std::vector<CBaitLocus*>::iterator it, it_end, it_tmp; |
| 1390 |
it = baitLoci_vec.begin(); |
| 1391 |
it_end = baitLoci_vec.end(); |
| 1392 |
|
| 1393 |
while (it != it_end) |
| 1394 |
{ |
| 1395 |
// Find end of this gene: |
| 1396 |
it_tmp = find_end_this_gene(it); |
| 1397 |
MYSORT(it,it_tmp, f); |
| 1398 |
it = it_tmp; |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
/* void sort_by_original_file_name__feature_num_start() */ |
| 1403 |
/* { */ |
| 1404 |
/* MYSORT(baitLoci_vec.begin(), baitLoci_vec.end(), original_file_name__feature_num_start_lessThanCBaitLocus); */ |
| 1405 |
/* } */ |
| 1406 |
|
| 1407 |
|
| 1408 |
bool is_ordered_within_genes() |
| 1409 |
{ |
| 1410 |
std::vector<CBaitLocus*>::iterator it, it_end, it_tmp; |
| 1411 |
it = baitLoci_vec.begin(); |
| 1412 |
it_end = baitLoci_vec.end(); |
| 1413 |
|
| 1414 |
if (it == it_end) |
| 1415 |
return true; |
| 1416 |
|
| 1417 |
it_tmp = it; |
| 1418 |
++it_tmp; |
| 1419 |
|
| 1420 |
while (it_tmp != it_end) |
| 1421 |
{ |
| 1422 |
if (!gene_name__feature_num_lessThanCBaitLocus(*it, *it_tmp) ) |
| 1423 |
return false; |
| 1424 |
it = it_tmp; |
| 1425 |
++it_tmp; |
| 1426 |
} |
| 1427 |
return true; |
| 1428 |
} |
| 1429 |
|
| 1430 |
|
| 1431 |
// Determines whether feature numbers reappear in successive loci |
| 1432 |
// if a different feature number was found in between. |
| 1433 |
// This ordering is assumed when filtering for the best loci in a feature. |
| 1434 |
bool is_partially_ordered() |
| 1435 |
{ |
| 1436 |
std::set<faststring> set_genes_features; |
| 1437 |
|
| 1438 |
std::vector<CBaitLocus*>::iterator it, it_end; |
| 1439 |
it = baitLoci_vec.begin(); |
| 1440 |
it_end = baitLoci_vec.end(); |
| 1441 |
|
| 1442 |
unsigned the_feature_num; |
| 1443 |
faststring the_gene_name; |
| 1444 |
|
| 1445 |
if (it == it_end) |
| 1446 |
return true; |
| 1447 |
|
| 1448 |
the_feature_num = (**it).get_feature_num(); |
| 1449 |
the_gene_name = (**it).get_gene_name(); |
| 1450 |
|
| 1451 |
faststring gene_feature_name; |
| 1452 |
gene_feature_name = the_gene_name + "_" + the_feature_num; |
| 1453 |
set_genes_features.insert(gene_feature_name); |
| 1454 |
|
| 1455 |
while (it != it_end) |
| 1456 |
{ |
| 1457 |
if (the_feature_num != (**it).get_feature_num() || the_gene_name != (**it).get_gene_name() ) |
| 1458 |
{ |
| 1459 |
// new gene or exon: |
| 1460 |
the_feature_num = (**it).get_feature_num(); |
| 1461 |
the_gene_name = (**it).get_gene_name(); |
| 1462 |
gene_feature_name = the_gene_name + "_" + the_feature_num; |
| 1463 |
|
| 1464 |
if ( set_genes_features.find(gene_feature_name) != set_genes_features.end() ) |
| 1465 |
{ |
| 1466 |
return false; // We have seen this gene/exon combination before. So we return false; |
| 1467 |
} |
| 1468 |
set_genes_features.insert(gene_feature_name); |
| 1469 |
} |
| 1470 |
++it; |
| 1471 |
} |
| 1472 |
return true; |
| 1473 |
} |
| 1474 |
|
| 1475 |
void get_seqs_baits_count(unsigned &NumSeqs, unsigned &NumBaits) const |
| 1476 |
{ |
| 1477 |
std::vector<CBaitLocus*>::const_iterator it, it_end; |
| 1478 |
it = baitLoci_vec.begin(); |
| 1479 |
it_end = baitLoci_vec.end(); |
| 1480 |
|
| 1481 |
unsigned local_NumSeqs = 0; |
| 1482 |
unsigned local_NumBaits = 0; |
| 1483 |
|
| 1484 |
while (it != it_end) |
| 1485 |
{ |
| 1486 |
local_NumSeqs += (**it).get_num_sequences(); |
| 1487 |
local_NumBaits += (**it).get_num_baits(); |
| 1488 |
++it; |
| 1489 |
} |
| 1490 |
|
| 1491 |
NumSeqs = local_NumSeqs; |
| 1492 |
NumBaits = local_NumBaits; |
| 1493 |
} |
| 1494 |
|
| 1495 |
// Used to evaluate a starting index in thinning mode. |
| 1496 |
// Thinning can be done for multiple alignment files even though we originally had one file in mind. |
| 1497 |
// At the moment we do not start at position 1 when coming to the next alignment. |
| 1498 |
|
| 1499 |
void count_seqs_baits_for_start_and_offset(unsigned start_count, unsigned offset_count, |
| 1500 |
unsigned &sum_seq, unsigned &sum_baits) |
| 1501 |
{ |
| 1502 |
unsigned i,N; |
| 1503 |
N = baitLoci_vec.size(); |
| 1504 |
|
| 1505 |
sum_seq = sum_baits = 0; |
| 1506 |
|
| 1507 |
for (i=start_count; i<N; i += offset_count) |
| 1508 |
{ |
| 1509 |
sum_baits += baitLoci_vec[i]->get_num_baits(); |
| 1510 |
sum_seq += baitLoci_vec[i]->get_num_sequences(); |
| 1511 |
} |
| 1512 |
} |
| 1513 |
|
| 1514 |
// Used to evaluate a starting index in thinning mode. |
| 1515 |
// Thinning can now be done for multiple alignment files even though we it was originally designed to work only for single files. |
| 1516 |
// This function is called for the loci of each alignment individually. |
| 1517 |
// This functions obtains the first locus it should consider and walks through all loci until the loci belong to the next alignment. |
| 1518 |
// It passes back the start locus for the next alignment. |
| 1519 |
//// At the moment we do not start at position 1 when coming to the next alignment. |
| 1520 |
|
| 1521 |
void count_seqs_baits_for_start_and_offset_multi_gene_feature_aware(unsigned alignment_counter, |
| 1522 |
std::vector<CBaitLocus*>::iterator &it_start_this_alignment, |
| 1523 |
std::vector<CBaitLocus*>::iterator &it_start_next_alignment, |
| 1524 |
bool &finished, |
| 1525 |
unsigned start_count, unsigned offset_count, |
| 1526 |
unsigned &sum_seq, unsigned &sum_baits, |
| 1527 |
unsigned &penalty_diff) |
| 1528 |
{ |
| 1529 |
unsigned i; |
| 1530 |
unsigned lastLocusStart, thisLocusStart, locusStartDiff; |
| 1531 |
int penalty_diff_local = 0; |
| 1532 |
|
| 1533 |
sum_seq = sum_baits = 0; |
| 1534 |
|
| 1535 |
if (global_verbosity >= 50) |
| 1536 |
{ |
| 1537 |
std::cerr << "Working on alignment number: " << alignment_counter << " start " << start_count << std::endl; |
| 1538 |
} |
| 1539 |
|
| 1540 |
std::vector<CBaitLocus*>::iterator it, it_end_al; |
| 1541 |
|
| 1542 |
if (alignment_counter == 1) // Since the calling function does not have access to the baitLoci_vec, the start is initialized in this function, if alignment_counter == 1. |
| 1543 |
{ |
| 1544 |
it = it_start_this_alignment = baitLoci_vec.begin(); |
| 1545 |
} |
| 1546 |
else |
| 1547 |
{ |
| 1548 |
it = it_start_this_alignment; |
| 1549 |
} |
| 1550 |
|
| 1551 |
if ( (**it).has_feature_information() ) |
| 1552 |
it_end_al = find_end_this_feature(it); |
| 1553 |
else |
| 1554 |
it_end_al = find_end_this_gene(it); |
| 1555 |
|
| 1556 |
|
| 1557 |
// First we determine the start for iterator at start_count |
| 1558 |
i=0; |
| 1559 |
lastLocusStart = (**it).get_start(); // This information is used to keep track of already deleted starting positions of loci. |
| 1560 |
// This variable is initialised with the locus staring coordinate of the first bait region starting position. |
| 1561 |
|
| 1562 |
while (i < start_count) // We won't walk past the end of this feature or alignment |
| 1563 |
{ |
| 1564 |
++it; |
| 1565 |
if (it == it_end_al) |
| 1566 |
break; |
| 1567 |
// If we had to skip possible starting points, we try to correct for this. |
| 1568 |
thisLocusStart = (**it).get_start(); |
| 1569 |
locusStartDiff = thisLocusStart - lastLocusStart; // Difference of indices, should be at least 1. Is 0 for first bait region. |
| 1570 |
i += locusStartDiff; |
| 1571 |
lastLocusStart = thisLocusStart; |
| 1572 |
} |
| 1573 |
|
| 1574 |
// std::cout << "Found start (index:start_count, index-found:i): " << start_count << " " << i << std::endl; |
| 1575 |
|
| 1576 |
// Differences are not penalised if they occur while searching for the starting position. |
| 1577 |
// In the ideal case, i == start_count. i < start_count should not be possible. |
| 1578 |
// However, if we moved passed the ideal start_count, we penalise this. |
| 1579 |
if (i > start_count) |
| 1580 |
{ |
| 1581 |
penalty_diff_local = i-start_count; |
| 1582 |
} |
| 1583 |
|
| 1584 |
// If we moved past the end of the alignment, we found no valid starting point in this alignment. |
| 1585 |
// This will be penalised. This penalty might superseed a previous penalty. |
| 1586 |
if (it == it_end_al) |
| 1587 |
{ |
| 1588 |
// The amount of the penalty could be a matter of debate. A small/moderate penalty is the |
| 1589 |
// start_count itself. It penalises larger start counts more. |
| 1590 |
penalty_diff_local = start_count; |
| 1591 |
} |
| 1592 |
else |
| 1593 |
{ |
| 1594 |
// Loop over all loci in steps of offset_count |
| 1595 |
while (true) // We won't walk past the end of this feature or alignment. We break inside the loop if it == it_end_al. |
| 1596 |
{ |
| 1597 |
// This locus is scored. |
| 1598 |
// std::cout << "Score locus (seq-coord:start,counter:i): " << (**it).get_start() << " " << i << std::endl; |
| 1599 |
sum_baits += (**it).get_num_baits(); |
| 1600 |
sum_seq += (**it).get_num_sequences(); |
| 1601 |
|
| 1602 |
i = 0; // Count the number of loci we skip in next iteration. |
| 1603 |
|
| 1604 |
// Move to next loci we want to add: |
| 1605 |
// We move by one locus position. (The first move.) |
| 1606 |
// We can handle the first move outside the loop below since offset_count must be > 0. |
| 1607 |
++it; |
| 1608 |
if (it == it_end_al) |
| 1609 |
break; |
| 1610 |
|
| 1611 |
// If we had to skip possible starting points, we try to correct for this. |
| 1612 |
thisLocusStart = (**it).get_start(); |
| 1613 |
locusStartDiff = thisLocusStart - lastLocusStart; |
| 1614 |
i += locusStartDiff; // Diff is 1 or larger. |
| 1615 |
lastLocusStart = thisLocusStart; |
| 1616 |
|
| 1617 |
// Skip offset_count loci. |
| 1618 |
while (it < it_end_al && i < offset_count) |
| 1619 |
{ |
| 1620 |
// Next iteration. |
| 1621 |
++it; |
| 1622 |
if (it == it_end_al) |
| 1623 |
break; |
| 1624 |
thisLocusStart = (**it).get_start(); |
| 1625 |
locusStartDiff = thisLocusStart - lastLocusStart; |
| 1626 |
i += locusStartDiff; |
| 1627 |
lastLocusStart = thisLocusStart; |
| 1628 |
} |
| 1629 |
|
| 1630 |
// If it == it_end_al, we have an iterator not pointing this alignment/feature any more. |
| 1631 |
// So we have break immediately. |
| 1632 |
if (it == it_end_al) |
| 1633 |
break; |
| 1634 |
|
| 1635 |
// In the ideal case i == offset_count. Then we are at the next locus we wanted to count. |
| 1636 |
// If i < offset_count we have not reached the next good locus in this alignment/gene/feature |
| 1637 |
// before we moved past the end. In this case we have it == it_end_al and we leave the |
| 1638 |
// loop with the next while check. |
| 1639 |
// If i > offset_count, then we had to skip more loci than we wanted to skip, since not all |
| 1640 |
// loci are valid starting points. In this case we add a penalty. |
| 1641 |
|
| 1642 |
if (i > offset_count) |
| 1643 |
{ |
| 1644 |
penalty_diff_local += (i-offset_count); |
| 1645 |
} |
| 1646 |
} // END while true |
| 1647 |
} // END ELSE if (it == it_end_al) |
| 1648 |
|
| 1649 |
penalty_diff = penalty_diff_local; |
| 1650 |
if (it == baitLoci_vec.end() ) |
| 1651 |
finished = true; |
| 1652 |
else |
| 1653 |
finished = false; |
| 1654 |
|
| 1655 |
it_start_next_alignment = it_end_al; |
| 1656 |
|
| 1657 |
} //END FUNCTION count_seqs_baits_for_start_and_offset_multi_gene_feature_aware |
| 1658 |
|
| 1659 |
|
| 1660 |
void get_original_file_name_gene_name(unsigned alignment_counter, |
| 1661 |
std::vector<CBaitLocus*>::iterator &it_start_this_alignment, |
| 1662 |
faststring &P_original_file_name, |
| 1663 |
faststring &P_gene_name |
| 1664 |
) |
| 1665 |
{ |
| 1666 |
if (alignment_counter == 1) |
| 1667 |
{ |
| 1668 |
it_start_this_alignment = baitLoci_vec.begin(); |
| 1669 |
} |
| 1670 |
|
| 1671 |
P_original_file_name = (**it_start_this_alignment).get_original_file_name(); |
| 1672 |
P_gene_name = (**it_start_this_alignment).get_gene_name(); |
| 1673 |
} |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
double get_max_dist_mean() const |
| 1679 |
{ |
| 1680 |
double sum=0; |
| 1681 |
double num=0; |
| 1682 |
int i, N = baitLoci_vec.size(); |
| 1683 |
|
| 1684 |
for (i=0; i<N; ++i) |
| 1685 |
{ |
| 1686 |
sum += baitLoci_vec[i]->get_sum_max_distances(); |
| 1687 |
num += baitLoci_vec[i]->get_num_baits(); |
| 1688 |
} |
| 1689 |
|
| 1690 |
// std::cerr << "sum max distances: " << sum << std::endl; |
| 1691 |
// std::cerr << "num baits: " << num << std::endl; |
| 1692 |
|
| 1693 |
return sum/num; |
| 1694 |
} |
| 1695 |
|
| 1696 |
double get_overall_max_dist() const |
| 1697 |
{ |
| 1698 |
double overall_max_dist = -1000; // True values must be in the range 0..1. |
| 1699 |
double om_dist; |
| 1700 |
int i, N = baitLoci_vec.size(); |
| 1701 |
|
| 1702 |
for (i=0; i<N; ++i) |
| 1703 |
{ |
| 1704 |
om_dist = baitLoci_vec[i]->get_overall_max_dist(); |
| 1705 |
if (om_dist > overall_max_dist) |
| 1706 |
{ |
| 1707 |
overall_max_dist = om_dist; |
| 1708 |
} |
| 1709 |
} |
| 1710 |
return overall_max_dist; |
| 1711 |
} |
| 1712 |
|
| 1713 |
|
| 1714 |
double get_CG_mean() const |
| 1715 |
{ |
| 1716 |
double sum=0; |
| 1717 |
double num=0; |
| 1718 |
int i, N = baitLoci_vec.size(); |
| 1719 |
|
| 1720 |
for (i=0; i<N; ++i) |
| 1721 |
{ |
| 1722 |
sum += baitLoci_vec[i]->get_sum_CG(); |
| 1723 |
num += baitLoci_vec[i]->get_num_baits(); |
| 1724 |
} |
| 1725 |
return sum/num; |
| 1726 |
} |
| 1727 |
|
| 1728 |
}; // END CBaitLoci_collection |