| 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 |
#include <iostream> |
| 29 |
#include <fstream> |
| 30 |
#include <ctime> |
| 31 |
|
| 32 |
#include "global-types-and-parameters.h" |
| 33 |
#include "CBaitRecord.h" |
| 34 |
#include "faststring2.h" |
| 35 |
#include "CBlastParser.h" |
| 36 |
#include "print_container.h" |
| 37 |
#include "Ctriple.h" |
| 38 |
#include "climits" |
| 39 |
|
| 40 |
using namespace std; |
| 41 |
|
| 42 |
bool skip_blast = false; |
| 43 |
unsigned start_time = 0; |
| 44 |
unsigned blast_time = 0; |
| 45 |
|
| 46 |
|
| 47 |
// Verbosity rules: |
| 48 |
// verbosity 0 -> only error messages that lead to exit() |
| 49 |
// verbosity 1 -> only warnings |
| 50 |
// verbosity 2 -> information on baits, time used |
| 51 |
// verbosity 3 -> progress |
| 52 |
// verbosity 4 -> more progress |
| 53 |
// verbosity |
| 54 |
|
| 55 |
void welcome(std::ostream &os) |
| 56 |
{ |
| 57 |
os << endl << endl; |
| 58 |
os << "Welcome to " << PROGNAME << ", version " << VERSION << ".\n\n"; |
| 59 |
} |
| 60 |
|
| 61 |
void good_bye(std::ostream &os) |
| 62 |
{ |
| 63 |
os << "Your BaitFilter analysis finished successfully.\n"; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
void get_hours_minutes_seconds(unsigned secs, unsigned &h, unsigned &m, unsigned &s) |
| 68 |
{ |
| 69 |
h = secs/3600; |
| 70 |
secs %= 3600; |
| 71 |
m = secs/60; |
| 72 |
s = secs%60; |
| 73 |
} |
| 74 |
|
| 75 |
void print_time(unsigned secs, ostream &os) |
| 76 |
{ |
| 77 |
char prev = os.fill('0'); |
| 78 |
|
| 79 |
unsigned h, m, s; |
| 80 |
get_hours_minutes_seconds(secs, h, m, s); |
| 81 |
|
| 82 |
os << h << ":"; |
| 83 |
os.width(2); |
| 84 |
os << m << ":"; |
| 85 |
os.width(2); |
| 86 |
os << s; |
| 87 |
|
| 88 |
os.fill(prev); |
| 89 |
} |
| 90 |
|
| 91 |
void print_stats(const CBaitLoci_collection &blc, const char * header= NULL) |
| 92 |
{ |
| 93 |
if (global_verbosity >= 2) |
| 94 |
{ |
| 95 |
CBaitRegionSelectorSets brss; |
| 96 |
if (header) |
| 97 |
cout << header << "\n"; |
| 98 |
blc.fill_locus_selector(brss); |
| 99 |
brss.print_num_genes_num_features(cout, " ", blc.has_feature_information()); |
| 100 |
|
| 101 |
unsigned NumSeqs, NumBaits; |
| 102 |
blc.get_seqs_baits_count(NumSeqs, NumBaits); |
| 103 |
// cout << " Number of bait regions: " << blc.get_number_of_loci() << "\n"; |
| 104 |
cout << " Total number of baits: " << NumBaits << "\n"; |
| 105 |
cout << " Total number of bait windows fully covered by sequences " << NumSeqs << "\n"; |
| 106 |
cout << " Proportion of baits saved (1-#baits/#sequences): " << (1.0 -(double)NumBaits/NumSeqs)*100 << "%\n"; |
| 107 |
|
| 108 |
cout.precision(6); |
| 109 |
cout << " Mean maximum distance of baits to MSAs: " << blc.get_max_dist_mean() << "\n"; |
| 110 |
cout << " Overall maximum distance of baits to MSA: " << blc.get_overall_max_dist() << "\n"; |
| 111 |
cout.precision(2); |
| 112 |
cout << " Mean CG content of baits: " << blc.get_CG_mean() << "\n"; |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
void extract_loci_information(const faststring &header, faststring &alignment_name, faststring &gene_name, unsigned &feature_num, unsigned &start) |
| 119 |
{ |
| 120 |
faststring rest1; |
| 121 |
faststring rest2; |
| 122 |
faststring tmp; |
| 123 |
|
| 124 |
header.divide_at('|', alignment_name, rest1); |
| 125 |
|
| 126 |
rest1.divide_at('|', gene_name, rest2); |
| 127 |
|
| 128 |
rest2.divide_at('|', tmp, rest1); |
| 129 |
|
| 130 |
if (!tmp.isAnUnsigned()) |
| 131 |
{ |
| 132 |
cerr << "Error: Header does not obey formatting conventions: " << header << "\n"; |
| 133 |
cerr << "The third field (separator \"|\") should be an unsigned integer number, but found \"" << tmp << "\" instead.\n"; |
| 134 |
exit(-3); |
| 135 |
} |
| 136 |
feature_num = tmp.ToUnsigned(); |
| 137 |
|
| 138 |
rest1.divide_at('|', tmp, rest2); |
| 139 |
|
| 140 |
if (!tmp.isAnUnsigned()) |
| 141 |
{ |
| 142 |
cerr << "Error: Header does not follow formatting conventions: " << header << "\n"; |
| 143 |
cerr << "The fourth field (separator \"|\") should be an unsigned integer number, but found \"" << tmp << "\" instead.\n"; |
| 144 |
exit(-3); |
| 145 |
} |
| 146 |
start = tmp.ToUnsigned(); |
| 147 |
|
| 148 |
// rest2.divide_at('|', tmp, rest1); |
| 149 |
|
| 150 |
// if (!tmp.isAnUnsigned()) |
| 151 |
// { |
| 152 |
// cerr << "Error: Header does not follow formatting conventions: " << header << "\n"; |
| 153 |
// cerr << "The fifth field (separator \"|\") should be an unsigned integer number, but found \"" << tmp << "\" instead.\n"; |
| 154 |
// exit(-3); |
| 155 |
// } |
| 156 |
|
| 157 |
// if (!rest1.isAnUnsigned()) |
| 158 |
// { |
| 159 |
// cerr << "Error: Header does not follow formatting conventions: " << header << "\n"; |
| 160 |
// cerr << "The sixth field (separator \"|\") should be an unsigned integer number, but found \"" << rest1 << "\" instead.\n"; |
| 161 |
// exit(-3); |
| 162 |
// } |
| 163 |
|
| 164 |
// tiling_index = tmp.ToUnsigned(); |
| 165 |
// counter = rest1.ToUnsigned(); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
int main(int argc, char ** argv) |
| 171 |
{ |
| 172 |
|
| 173 |
start_time = time(NULL); |
| 174 |
|
| 175 |
cout.setf(ios::fixed); |
| 176 |
cout.precision(2); |
| 177 |
|
| 178 |
welcome(cout); |
| 179 |
read_and_init_parameters(argc, argv, cerr); |
| 180 |
|
| 181 |
#ifdef SKIPBLAST |
| 182 |
skip_blast = true; |
| 183 |
global_blast_result_file = "blast_result.txt"; |
| 184 |
#endif |
| 185 |
|
| 186 |
if (!global_blast_result_file.empty()) |
| 187 |
{ |
| 188 |
skip_blast = true; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
if (global_verbosity >= 2) |
| 193 |
{ |
| 194 |
cerr << '\n'; |
| 195 |
print_parameters(cerr, ""); |
| 196 |
print_calling_command_line(cerr, argc, argv); |
| 197 |
cerr << '\n'; |
| 198 |
} |
| 199 |
|
| 200 |
// Parameter tests: |
| 201 |
if (0) |
| 202 |
{ |
| 203 |
faststring cmd = global_blast_exe + " -version"; |
| 204 |
cout << "Blast exe test: " << cmd << "\n"; |
| 205 |
int err = system(cmd.c_str()); |
| 206 |
|
| 207 |
cout << err << "\n"; |
| 208 |
|
| 209 |
exit(0); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
ifstream is; |
| 214 |
ofstream os; |
| 215 |
|
| 216 |
is.open(global_bait_filename.c_str()); |
| 217 |
|
| 218 |
if (is.fail()) |
| 219 |
{ |
| 220 |
cerr << "Error: Input file: " << global_bait_filename << " could not be opened. I guess it does not exist.\n"; |
| 221 |
exit(-1); |
| 222 |
} |
| 223 |
|
| 224 |
if (global_mode != 'S') |
| 225 |
{ |
| 226 |
os.open(global_output_filename.c_str()); |
| 227 |
if (os.fail()) |
| 228 |
{ |
| 229 |
cerr << "\n"; |
| 230 |
cerr << "Error: Failed to open or create output file. " << global_output_filename << "\n"; |
| 231 |
cerr << "Exiting.\n"; |
| 232 |
exit(-1); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if (global_verbosity >= 3) |
| 237 |
{ |
| 238 |
cout << "PROGRESS: Parsing bait file: ... " << flush; |
| 239 |
} |
| 240 |
CBaitLoci_collection blc(is); |
| 241 |
is.close(); |
| 242 |
|
| 243 |
if (global_verbosity >= 3) |
| 244 |
{ |
| 245 |
cout << "Done. Time used since start in seconds: " << (time(NULL)-start_time) << "\n"; |
| 246 |
} |
| 247 |
|
| 248 |
// Note: Bait loci are sorted in the constructor. Usually, they should be sorted in the bait file, |
| 249 |
// if the user has not edited the file. |
| 250 |
|
| 251 |
// DEBUG code: |
| 252 |
// blc.print_stats(cout); |
| 253 |
// blc.print_all(cout, 's'); |
| 254 |
// blc.print_all(cout, 'f'); |
| 255 |
// exit(0); |
| 256 |
|
| 257 |
|
| 258 |
if (global_mode == 'S') |
| 259 |
{ |
| 260 |
faststring msg; |
| 261 |
|
| 262 |
msg = "Characteristics of the set of bait regions in input file: "; |
| 263 |
|
| 264 |
if (global_verbosity < 2) // Can be the case if (global_mode == 'S') |
| 265 |
global_verbosity = 2; // Minimum value required by print_stats() to print something. Could be handled with a parameter! |
| 266 |
print_stats(blc, msg.c_str()); |
| 267 |
exit(0); |
| 268 |
} |
| 269 |
else if (global_verbosity >= 2) |
| 270 |
{ |
| 271 |
faststring msg; |
| 272 |
msg = "Characteristics of the set of bait regions in input file (before filtering): "; |
| 273 |
print_stats(blc, msg.c_str()); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
if (global_mode == 'a') |
| 279 |
{ |
| 280 |
if (global_verbosity >= 3) |
| 281 |
{ |
| 282 |
cout << "\n\n"; |
| 283 |
cout << "PROGRESS: Determining: Best bait region in each alignment/gene with criterion minimum number of baits\n"; |
| 284 |
} |
| 285 |
CBaitLoci_collection blc_best_baitRegions_in_genes_crit_numBaits(blc, 'g', 'b'); |
| 286 |
blc_best_baitRegions_in_genes_crit_numBaits.print_all(os, 's'); |
| 287 |
if (global_verbosity >= 2) |
| 288 |
{ |
| 289 |
cout << "\n"; |
| 290 |
cout << "Characteristics of the set of bait regions after determining the best bait region per alignment/gene under the criterion to minimize the number of baits we have\n"; |
| 291 |
print_stats(blc_best_baitRegions_in_genes_crit_numBaits); |
| 292 |
cout << "\n"; |
| 293 |
} |
| 294 |
} |
| 295 |
else if (global_mode == 'A') |
| 296 |
{ |
| 297 |
if (global_verbosity >= 3) |
| 298 |
{ |
| 299 |
cerr << "\n\n"; |
| 300 |
cerr << "PROGRESS: Determining: Best bait region in each gene with criterion maximum number of sequences.\n"; |
| 301 |
} |
| 302 |
CBaitLoci_collection blc_best_baitRegions_in_genes_crit_numSeqs(blc, 'g', 's'); |
| 303 |
blc_best_baitRegions_in_genes_crit_numSeqs.print_all(os, 's'); |
| 304 |
if (global_verbosity >= 2) |
| 305 |
{ |
| 306 |
cout << "\n"; |
| 307 |
cout << "Characteristics of the set of bait regions after determining the best bait region per alignment/gene under the criterion to maximize the number of sequences we have\n"; |
| 308 |
print_stats(blc_best_baitRegions_in_genes_crit_numSeqs); |
| 309 |
cout << "\n"; |
| 310 |
} |
| 311 |
} |
| 312 |
else if (global_mode == 'f') |
| 313 |
{ |
| 314 |
if (global_verbosity >= 3) |
| 315 |
{ |
| 316 |
cerr << "\n\n"; |
| 317 |
cerr << "PROGRESS: Determining: Best bait region in each feature with criterion minimum number of baits\n"; |
| 318 |
} |
| 319 |
CBaitLoci_collection blc_best_baitRegions_in_features_crit_numBaits(blc, 'f', 'b'); |
| 320 |
blc_best_baitRegions_in_features_crit_numBaits.print_all(os, 's'); |
| 321 |
if (global_verbosity >= 2) |
| 322 |
{ |
| 323 |
cout << "\n"; |
| 324 |
cout << "After determining the best bait region per feature under the criterion to minimize the number of baits we have\n"; |
| 325 |
print_stats(blc_best_baitRegions_in_features_crit_numBaits); |
| 326 |
cout << "\n"; |
| 327 |
} |
| 328 |
} |
| 329 |
else if (global_mode == 'F') |
| 330 |
{ |
| 331 |
if (global_verbosity >= 3) |
| 332 |
{ |
| 333 |
cerr << "\n\n"; |
| 334 |
cerr << "PROGRESS: Determining: Best bait region in each feature with criterion maximum number of sequences.\n"; |
| 335 |
} |
| 336 |
CBaitLoci_collection blc_best_baitRegions_in_features_crit_numSeqs(blc, 'f', 's'); |
| 337 |
blc_best_baitRegions_in_features_crit_numSeqs.print_all(os, 's'); |
| 338 |
if (global_verbosity >= 2) |
| 339 |
{ |
| 340 |
cout << "\n"; |
| 341 |
cout << "Characteristics of the set of bait regions after determining the best bait region per feature under the criterion to maximize the number of sequences we have\n"; |
| 342 |
print_stats(blc_best_baitRegions_in_features_crit_numSeqs); |
| 343 |
cout << "\n"; |
| 344 |
} |
| 345 |
} |
| 346 |
else if (global_mode == 't' || global_mode == 'T') |
| 347 |
{ |
| 348 |
unsigned sum_seqs, sum_baits; |
| 349 |
|
| 350 |
unsigned start_count = 0; |
| 351 |
unsigned best_index = 1; |
| 352 |
unsigned best_num = 0; // This initialisation is only done to silence the compiler warning for using an uninitialised variable. |
| 353 |
|
| 354 |
// This is essential output: Restrict it by verbosity?? |
| 355 |
if (global_verbosity >= 2) |
| 356 |
{ |
| 357 |
cout << "Running thinning mode:\n"; |
| 358 |
cout << "\nThe columns are: starting offset, sum of sequences the result is based on, number of required baits.\n"; |
| 359 |
} |
| 360 |
// Run thinning mode: |
| 361 |
while (start_count < global_thinning_step_width) |
| 362 |
{ |
| 363 |
blc.count_seqs_baits_for_start_and_offset(start_count, global_thinning_step_width, sum_seqs, sum_baits); |
| 364 |
++start_count; |
| 365 |
|
| 366 |
if (start_count == 1) // In this block we will surely initialize best_num: |
| 367 |
{ |
| 368 |
if (global_mode == 't') |
| 369 |
best_num = sum_baits; |
| 370 |
else // global_mode == 'T' |
| 371 |
best_num = sum_seqs; |
| 372 |
} |
| 373 |
|
| 374 |
if (global_mode == 't') |
| 375 |
{ |
| 376 |
if (best_num > sum_baits) |
| 377 |
{ |
| 378 |
best_index = start_count; |
| 379 |
best_num = sum_baits; |
| 380 |
} |
| 381 |
} |
| 382 |
else // global_mode == 'T' |
| 383 |
{ |
| 384 |
if (best_num < sum_seqs) |
| 385 |
{ |
| 386 |
best_index = start_count; |
| 387 |
best_num = sum_seqs; |
| 388 |
} |
| 389 |
} |
| 390 |
if (global_verbosity >= 2) |
| 391 |
{ |
| 392 |
cout << start_count << " " << sum_seqs << " " << sum_baits << "\n"; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
cout << "Best starting offset: " << best_index << "\n"; |
| 397 |
cout << "A bait file that has been thinned out is written to the output file: " << global_output_filename << "\n"; |
| 398 |
cout << "This file contains each " << global_thinning_step_width << "th bait region starting with bait-region " << best_index << " of the input bait file.\n"; |
| 399 |
cout << "Numbers correspond to the line numbers in the input file +1, since all bait files have a headline.\n"; |
| 400 |
|
| 401 |
blc.print_with_start_stepwidth(os, 's', best_index, global_thinning_step_width); |
| 402 |
} |
| 403 |
else if (global_mode == 'h' || global_mode == 'H') |
| 404 |
{ |
| 405 |
unsigned sum_seqs, sum_baits; |
| 406 |
|
| 407 |
unsigned start_count; |
| 408 |
unsigned best_index = UINT_MAX; |
| 409 |
unsigned best_num = 0; // This initialisation is only done to silence the compiler warning for using an uninitialised variable. |
| 410 |
unsigned best_penalty; |
| 411 |
unsigned penalty_diff; |
| 412 |
|
| 413 |
std::vector<CBaitLocus*>::iterator it_bait_vector, it_next_alignment_start, it_this_alignment_start; |
| 414 |
|
| 415 |
// Loop over all alignments or features stored in this collection. |
| 416 |
// Bait loci of all alignments or features are stored this one file. |
| 417 |
// We will handle one alignment/feature a time. |
| 418 |
// The current alignment or feature ends at it_tmp. |
| 419 |
// The file ends at it_end |
| 420 |
|
| 421 |
unsigned alignment_counter = 0; |
| 422 |
bool finished = false; |
| 423 |
|
| 424 |
// For each alignment |
| 425 |
while (!finished) // The variable finished is being set inside blc.count_seqs_baits_for_start_and_offset_multi_gene_feature_aware |
| 426 |
{ |
| 427 |
++ alignment_counter; // First value in loop is 1. |
| 428 |
|
| 429 |
// For alignment_counter==1 the variables it_this_alignment_start and it_next_alignment_start are not initialised, but this is intended. |
| 430 |
// They will be initialised with the first call to blc.count_seqs_baits_for_start_and_offset_multi_gene_feature_aware. |
| 431 |
// If alignment_counter > 1, we start the next iteration. |
| 432 |
it_this_alignment_start = it_next_alignment_start; |
| 433 |
|
| 434 |
if (global_verbosity >= 2) |
| 435 |
{ |
| 436 |
faststring original_file_name; |
| 437 |
faststring gene_name; |
| 438 |
|
| 439 |
blc.get_original_file_name_gene_name(alignment_counter, it_this_alignment_start, original_file_name, gene_name); |
| 440 |
|
| 441 |
cout << "\n"; |
| 442 |
cout << "Running thinning mode for next alignment-file/feature: " << original_file_name << "/" << gene_name << "\n"; |
| 443 |
// cout << "\nThe columns are: starting offset, sum of sequences the result is based on, number of required baits.\n"; |
| 444 |
} |
| 445 |
// Run thinning mode: |
| 446 |
|
| 447 |
start_count = 0; |
| 448 |
penalty_diff = 0; |
| 449 |
best_penalty = UINT_MAX; |
| 450 |
|
| 451 |
unsigned best_sum_seqs, best_sum_baits; |
| 452 |
|
| 453 |
// Test all starting points in this alignment |
| 454 |
while (start_count < global_thinning_step_width) |
| 455 |
{ |
| 456 |
// In the following call, it_this_alignment_start is only changed if alignment_counter==1. |
| 457 |
// In this case it is changed to the "beginning of the data set" and therefore to the beginning of the first alignment. |
| 458 |
// Otherwise it remains unchanged. |
| 459 |
// it_next_alignment_start is set to the start of the next alignment. |
| 460 |
blc.count_seqs_baits_for_start_and_offset_multi_gene_feature_aware(alignment_counter, |
| 461 |
it_this_alignment_start, |
| 462 |
it_next_alignment_start, |
| 463 |
finished, |
| 464 |
start_count, |
| 465 |
global_thinning_step_width, |
| 466 |
sum_seqs, sum_baits, penalty_diff); |
| 467 |
|
| 468 |
if (start_count == 0) // In this block we will initialise best_num: |
| 469 |
{ |
| 470 |
if (global_mode == 'h') |
| 471 |
{ |
| 472 |
best_num = sum_baits; |
| 473 |
} |
| 474 |
else // global_mode == 'H' |
| 475 |
{ |
| 476 |
best_num = sum_seqs; |
| 477 |
} |
| 478 |
best_penalty = penalty_diff; |
| 479 |
best_index = 0; |
| 480 |
best_sum_seqs = sum_seqs; |
| 481 |
best_sum_baits = sum_baits; |
| 482 |
} |
| 483 |
else |
| 484 |
{ |
| 485 |
if (global_mode == 'h') |
| 486 |
{ |
| 487 |
// Penalty is the primary criterion to choose the best starting position |
| 488 |
if (best_penalty > penalty_diff || (best_penalty == penalty_diff && best_num > sum_baits) ) |
| 489 |
{ |
| 490 |
best_num = sum_baits; |
| 491 |
best_index = start_count; |
| 492 |
best_penalty = penalty_diff; |
| 493 |
|
| 494 |
best_sum_seqs = sum_seqs; |
| 495 |
best_sum_baits = sum_baits; |
| 496 |
} |
| 497 |
} |
| 498 |
else // global_mode == 'H' |
| 499 |
{ |
| 500 |
// Penalty is the primary criterion to choose the best starting position |
| 501 |
if (best_penalty > penalty_diff || (best_penalty == penalty_diff && best_num < sum_seqs) ) |
| 502 |
{ |
| 503 |
best_num = sum_seqs; |
| 504 |
best_index = start_count; |
| 505 |
best_penalty = penalty_diff; |
| 506 |
|
| 507 |
best_sum_seqs = sum_seqs; |
| 508 |
best_sum_baits = sum_baits; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
if (global_verbosity >= 5) |
| 513 |
{ |
| 514 |
cout << ": " << start_count << " " << sum_seqs << " " << sum_baits << " " << penalty_diff << "\n"; |
| 515 |
if (global_verbosity >= 7) |
| 516 |
{ |
| 517 |
cout << "best index, penalty, score: " << best_index << " " << best_penalty << " " << best_num << "\n"; |
| 518 |
} |
| 519 |
} |
| 520 |
++start_count; |
| 521 |
} // END while (start_count < global_thinning_step_width) // Test all starting points in this alignment |
| 522 |
|
| 523 |
cout << "Best starting offset (add 1 to get the index): " << best_index << "\n"; |
| 524 |
cout << "Sequences & baits for best starting offset: " << best_sum_seqs << " " << best_sum_baits << "\n"; |
| 525 |
cout << "Thinned bait loci have been appended to the output file: " << global_output_filename << "\n"; |
| 526 |
// cout << "This file contains each " << global_thinning_step_width << "th bait region starting with bait-region " << best_index << " of the input bait file." << "\n"; |
| 527 |
// cout << "Numbers correspond to the line numbers in the input file +1, since all bait files have a headline." << "\n"; |
| 528 |
|
| 529 |
// Write result for this alignment file/feature, before we go to the next a/f or finish. |
| 530 |
blc.print_with_start_stepwidth_multi_gene_feature_aware(os, 's', best_index, global_thinning_step_width, it_this_alignment_start); |
| 531 |
|
| 532 |
} // END while (!finished) |
| 533 |
} // END else if (global_mode == 'h' || global_mode == 'H') |
| 534 |
else if (global_mode == 'b' || global_mode == 'B' || global_mode == 'x' || global_mode == 'C') |
| 535 |
{ |
| 536 |
faststring blastfilename = "blast_result.txt"; |
| 537 |
|
| 538 |
if (skip_blast) |
| 539 |
{ |
| 540 |
blastfilename = global_blast_result_file; |
| 541 |
} |
| 542 |
|
| 543 |
// Conduct the blast search. |
| 544 |
if (!skip_blast) |
| 545 |
{ |
| 546 |
// Write baits to fasta file: |
| 547 |
ofstream os_tmp("tmp_baits.txt"); |
| 548 |
blc.print_all(os_tmp, 'f'); |
| 549 |
os_tmp.close(); |
| 550 |
|
| 551 |
// Conduct a blast of the baits sequences against a reference genome: |
| 552 |
|
| 553 |
// In the blast call we use an E-value threshold of: |
| 554 |
// global_blast_max_second_hit_evalue*2 |
| 555 |
// This will report all relevant hits. |
| 556 |
|
| 557 |
faststring e_value_command; |
| 558 |
|
| 559 |
if (!global_blast_evalue_commandline.empty()) |
| 560 |
{ |
| 561 |
double tmp = global_blast_evalue_commandline.ToDouble(); |
| 562 |
if (tmp <= 10 && tmp > 0) |
| 563 |
{ |
| 564 |
e_value_command = global_blast_evalue_commandline; |
| 565 |
} |
| 566 |
else |
| 567 |
{ |
| 568 |
if (global_blast_min_hit_coverage_of_bait > 0) // For the coverage filter to work, the E-value should not be too small. |
| 569 |
{ |
| 570 |
double temp = global_blast_max_second_hit_evalue*2; |
| 571 |
if (temp < 0.001) |
| 572 |
temp = 0.001; |
| 573 |
|
| 574 |
e_value_command = faststring(temp, 15, true); |
| 575 |
} |
| 576 |
else |
| 577 |
{ |
| 578 |
e_value_command = faststring(global_blast_max_second_hit_evalue*2, 15, true); |
| 579 |
} |
| 580 |
cerr << "Warning: The Evalue that can be specified with the blast-evalue-cutoff option has to be greater than 0 and smaller or equal to 10." |
| 581 |
<< " The evalue that has been specified is not in this range." |
| 582 |
<< " Therefore we are falling back to the default value: " << e_value_command << "\n"; |
| 583 |
} |
| 584 |
} |
| 585 |
else |
| 586 |
{ |
| 587 |
if (global_blast_min_hit_coverage_of_bait > 0) // For the coverage filter to work, the E-value should not be too small. |
| 588 |
{ |
| 589 |
double temp = global_blast_max_second_hit_evalue*2; |
| 590 |
if (temp < 0.001) |
| 591 |
temp = 0.001; |
| 592 |
|
| 593 |
e_value_command = faststring(temp, 15, true); |
| 594 |
} |
| 595 |
else |
| 596 |
{ |
| 597 |
e_value_command = faststring(global_blast_max_second_hit_evalue*2, 15, true); |
| 598 |
} |
| 599 |
} |
| 600 |
faststring blastcommand = global_blast_exe + " " |
| 601 |
+ global_blast_extra_commandline |
| 602 |
+ " -outfmt 6 -task blastn -query tmp_baits.txt -db " + global_blast_db |
| 603 |
+ " -out " + blastfilename + " -evalue " + e_value_command; |
| 604 |
|
| 605 |
if (global_verbosity >= 2) |
| 606 |
{ |
| 607 |
cout << "Running blast with command:\n"; |
| 608 |
cout << blastcommand << "\n"; |
| 609 |
} |
| 610 |
|
| 611 |
blast_time = time(NULL); |
| 612 |
|
| 613 |
// cerr << "Please press return to continue.\n"; |
| 614 |
// cin.get(); |
| 615 |
|
| 616 |
int blast_command_error; |
| 617 |
blast_command_error = system(blastcommand.c_str()); |
| 618 |
|
| 619 |
if (blast_command_error != 0) |
| 620 |
{ |
| 621 |
cerr << "ERROR: The blast command returned with non-zero error status. This indicates that blast encountered an error. The status blast returned via the system command was: " << blast_command_error << "\n"; |
| 622 |
cerr << "I suggest to run blast command manually:\n"; |
| 623 |
cerr << blastcommand << "\n"; |
| 624 |
cerr << "Exiting.\n"; |
| 625 |
exit(-1); |
| 626 |
} |
| 627 |
|
| 628 |
blast_time = time(NULL)-blast_time; |
| 629 |
} // END if (!skip_blast) |
| 630 |
|
| 631 |
if (global_verbosity >= 50) |
| 632 |
cout << "PROGRESS: Reading the blast result file: " << blastfilename << " ..." << flush; |
| 633 |
CBlast_parser bp(blastfilename.c_str()); |
| 634 |
if (global_verbosity >= 50) |
| 635 |
cout << " Done.\n"; |
| 636 |
|
| 637 |
// How to use the CBalstParser to filter hits: |
| 638 |
// Mode 0: |
| 639 |
// Filter gene if first hit is below global_blast_max_first_hit_evalue and if second hit is below best_e_value_this_query*factor_max_increase |
| 640 |
// Mode 1: |
| 641 |
// Filter gene if first hit is below global_blast_max_first_hit_evalue and if second hit is below global_blast_max_second_hit_evalue |
| 642 |
|
| 643 |
// double_hits contain only one of the 2 good hits that have been found. |
| 644 |
// In case more than 2 good hits are found, it contains all but the first good hit, which is more than what we need. |
| 645 |
// But this redundancy is removed when we add the bait information to the locus_selector data structure. |
| 646 |
// |
| 647 |
CBlast_parser double_hits; |
| 648 |
bp.detect_2_good_hits_per_query(global_blast_max_first_hit_evalue, global_blast_max_second_hit_evalue, double_hits, 1); |
| 649 |
|
| 650 |
set<faststring> set_of_names; |
| 651 |
double_hits.set_of_query_names(set_of_names); |
| 652 |
|
| 653 |
// The locus_selector contains information about |
| 654 |
CBaitRegionSelectorSets locus_selector; |
| 655 |
|
| 656 |
set<faststring>::iterator it, it_end; |
| 657 |
it = set_of_names.begin(); |
| 658 |
it_end = set_of_names.end(); |
| 659 |
|
| 660 |
// cout << "Start Time: " << time(NULL) << "\n"; |
| 661 |
|
| 662 |
// For all baits |
| 663 |
while (it != it_end) |
| 664 |
{ |
| 665 |
faststring alignment_name; |
| 666 |
faststring gene_name; |
| 667 |
unsigned feature_num; |
| 668 |
unsigned start; |
| 669 |
// unsigned tiling_index; // Just dummy, will not be used. |
| 670 |
// unsigned counter; // Just dummy, will not be used. |
| 671 |
|
| 672 |
// Extract locus information from bait sequence name: |
| 673 |
extract_loci_information(*it, alignment_name, gene_name, feature_num, start); |
| 674 |
|
| 675 |
if (0) |
| 676 |
{ |
| 677 |
if (!locus_selector.exists(alignment_name) ) |
| 678 |
cout << "New alignmentname: " << alignment_name << "\n"; |
| 679 |
else |
| 680 |
cout << "Existing alignmentname: " << alignment_name << "\n"; |
| 681 |
|
| 682 |
if (!locus_selector.exists(alignment_name, feature_num) ) |
| 683 |
cout << "New alignmentname feature_num: " << alignment_name << " " << feature_num << "\n"; |
| 684 |
else |
| 685 |
cout << "Existing alignmentname feature_num: " << alignment_name << " " << feature_num << "\n"; |
| 686 |
|
| 687 |
if (!locus_selector.exists(alignment_name, feature_num, start) ) |
| 688 |
cout << "New alignmentname feature_num start: " << alignment_name << " " << feature_num << " " << start << "\n"; |
| 689 |
else |
| 690 |
cout << "Existing alignmentname feature_num start: " << alignment_name << " " << feature_num << " " << start << "\n"; |
| 691 |
} |
| 692 |
|
| 693 |
locus_selector.add(alignment_name, feature_num, start); |
| 694 |
++it; |
| 695 |
} // END For all baits: while (it != it_end) |
| 696 |
|
| 697 |
if (0) |
| 698 |
{ |
| 699 |
cout << "\n"; |
| 700 |
cout << "Print locus selectors:\n"; |
| 701 |
locus_selector.print_all(cout); |
| 702 |
cout << "\n"; |
| 703 |
} |
| 704 |
|
| 705 |
// cout << "End Time: " << time(NULL) << "\n"; |
| 706 |
|
| 707 |
// Filter bait loci according to double_hits: |
| 708 |
|
| 709 |
if (global_verbosity >= 100) |
| 710 |
{ |
| 711 |
cout << "START: List of double hits:\n"; |
| 712 |
double_hits.print_all(cout); |
| 713 |
cout << "END: List of double hits:\n\n"; |
| 714 |
} |
| 715 |
|
| 716 |
// The following code was moved to the top. |
| 717 |
// if (global_verbosity >= 2) |
| 718 |
// { |
| 719 |
// // cout << "Number of bait regions before searching/filtering for baits with multiple blast hits: " << blc.get_number_of_loci() << "\n"; |
| 720 |
// cout << "Characteristics of input file (before filtering): "; |
| 721 |
// print_stats(blc); |
| 722 |
// cout << "\n"; |
| 723 |
// } |
| 724 |
|
| 725 |
// Allowed modes in this constructor: |
| 726 |
// Filter modes: b, B, x: |
| 727 |
// No filter: C. Means that filtered_blc obtains a copy of blc. |
| 728 |
|
| 729 |
CBaitLoci_collection filtered_blc(blc, locus_selector, global_mode); |
| 730 |
|
| 731 |
// Print stats with multiple hits results: (Do not print stats in coverage filter only mode. This case is handled below.) |
| 732 |
if (global_verbosity >= 2 && global_mode != 'C') |
| 733 |
{ |
| 734 |
// cout << "Number of bait regions after searching/filtering for baits with multiple blast hits: " << filtered_blc.get_number_of_loci() << "\n"; |
| 735 |
cout << "\n"; |
| 736 |
cout << "Characteristics of set of bait regions after filtering for baits with multiple blast hits to reference genome:\n"; |
| 737 |
print_stats(filtered_blc); |
| 738 |
cout << "\n"; |
| 739 |
} |
| 740 |
|
| 741 |
if (global_blast_min_hit_coverage_of_bait > 0) |
| 742 |
{ |
| 743 |
CBaitLoci_collection coverage_filter(filtered_blc, bp, global_blast_min_hit_coverage_of_bait); |
| 744 |
// if (global_verbosity >= 2) |
| 745 |
// { |
| 746 |
// cout << "Number of bait regions after hit coverage filtering: " << coverage_filter.get_number_of_loci() << "\n"; |
| 747 |
// } |
| 748 |
coverage_filter.print_all(os, 's'); |
| 749 |
|
| 750 |
if (global_verbosity >= 2) |
| 751 |
{ |
| 752 |
if (global_blast_min_hit_coverage_of_bait > 0) |
| 753 |
{ |
| 754 |
cout << "\n"; |
| 755 |
cout << "Characteristics of set of bait regions after filtering for bait regions without sufficient hit coverage values in blast against a reference genome:\n"; |
| 756 |
print_stats(coverage_filter); |
| 757 |
cout << "\n"; |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
else |
| 762 |
{ |
| 763 |
// Report what we have found: |
| 764 |
filtered_blc.print_all(os, 's'); |
| 765 |
} |
| 766 |
} // END if (global_mode == 'b' || global_mode == 'B' || global_mode == 'x' || global_mode == 'C') |
| 767 |
else if (global_mode == 'c') |
| 768 |
{ |
| 769 |
if (global_conversion_mode == 1) |
| 770 |
{ |
| 771 |
// Write baits to file in Agilent-Germany format |
| 772 |
blc.print_all(os, 'A', global_ProbeID_prefix.c_str()); |
| 773 |
|
| 774 |
if (global_verbosity >= 2) |
| 775 |
{ |
| 776 |
cout << "Bait statistics for the converted bait file:\n"; |
| 777 |
print_stats(blc); |
| 778 |
cout << "\n"; |
| 779 |
} |
| 780 |
} |
| 781 |
else |
| 782 |
{ |
| 783 |
cerr << "INTERNAL ERROR: Unknown conversion mode : " << global_conversion_mode << "\n"; |
| 784 |
cerr << "Please report this problem to the developer.\n"; |
| 785 |
exit(-13); |
| 786 |
} |
| 787 |
} // END if (global_mode == 'c') |
| 788 |
else |
| 789 |
{ |
| 790 |
cerr << "INTERNAL ERROR: Unknown internal mode: " << global_mode << "\n"; |
| 791 |
cerr << "Please report this problem to the developer.\n"; |
| 792 |
exit(-13); |
| 793 |
} |
| 794 |
|
| 795 |
unsigned total_seconds = time(NULL)-start_time; |
| 796 |
if (global_verbosity >= 2) |
| 797 |
{ |
| 798 |
cout << "Total time used: "; |
| 799 |
print_time(total_seconds, cout); |
| 800 |
cout << "\n"; |
| 801 |
} |
| 802 |
|
| 803 |
if (global_mode == 'b' || global_mode == 'B' || global_mode == 'x' || global_mode == 'C') |
| 804 |
{ |
| 805 |
if (global_verbosity >= 2) |
| 806 |
{ |
| 807 |
cout << "Time used excluding the Blast search: "; |
| 808 |
print_time(total_seconds-blast_time, cout); |
| 809 |
cout << "\n"; |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
os.close(); |
| 814 |
good_bye(cout); |
| 815 |
} |