| 1 | #include "CompactedDBG.hpp" |
| 2 | #include "ColoredCDBG.hpp" |
| 3 | |
| 4 | using namespace std; |
| 5 | |
| 6 | void PrintVersion() { |
| 7 | |
| 8 | cout << BFG_VERSION << endl; |
| 9 | } |
| 10 | |
| 11 | void PrintUsage() { |
| 12 | |
| 13 | cout << "Bifrost " << BFG_VERSION << endl << endl; |
| 14 | |
| 15 | cout << "Highly parallel construction, indexing and querying of colored and compacted de Bruijn graphs" << endl << endl; |
| 16 | |
| 17 | cout << "Usage: Bifrost [COMMAND] [PARAMETERS]" << endl << endl; |
| 18 | |
| 19 | cout << "[COMMAND]:" << endl << endl; |
| 20 | |
| 21 | cout << " build Build a compacted de Bruijn graph, with or without colors" << endl; |
| 22 | cout << " update Update a compacted (colored) de Bruijn graph with new sequences" << endl; |
| 23 | cout << " query Query a compacted (colored) de Bruijn graph" << endl << endl; |
| 24 | |
| 25 | cout << "[PARAMETERS]: build" << endl << endl; |
| 26 | |
| 27 | cout << " > Mandatory with required argument:" << endl << endl; |
| 28 | |
| 29 | cout << " -s, --input-seq-file Input sequence file in fasta/fastq(.gz) format" << endl; |
| 30 | cout << " Multiple files can be provided as a list in a text file (one file per line)" << endl; |
| 31 | cout << " K-mers with exactly 1 occurrence in the input sequence files will be discarded" << endl; |
| 32 | cout << " -r, --input-ref-file Input reference file in fasta/fastq(.gz) or gfa(.gz) format" << endl; |
| 33 | cout << " Multiple files can be provided as a list in a text file (one file per line)" << endl; |
| 34 | cout << " All k-mers of the input reference files are used" << endl; |
| 35 | cout << " -o, --output-file Prefix for output file(s)" << endl << endl; |
| 36 | |
| 37 | cout << " > Optional with required argument:" << endl << endl; |
| 38 | |
| 39 | cout << " -t, --threads Number of threads (default is 1)" << endl; |
| 40 | cout << " -k, --kmer-length Length of k-mers (default is 31)" << endl; |
| 41 | cout << " -m, --min-length Length of minimizers (default is automatically chosen)" << endl; |
| 42 | cout << " -B, --bloom-bits Number of Bloom filter bits per k-mer (default is 14)" << endl; |
| 43 | cout << " -l, --load-mbbf Input Blocked Bloom Filter file, skips filtering step (default is no input)" << endl; |
| 44 | cout << " -w, --write-mbbf Output Blocked Bloom Filter file (default is no output)" << endl; |
| 45 | |
| 46 | cout << " > Optional with no argument:" << endl << endl; |
| 47 | |
| 48 | cout << " -c, --colors Color the compacted de Bruijn graph (default is no coloring)" << endl; |
| 49 | //cout << " -y, --keep-mercy Keep low coverage k-mers connecting tips" << endl; |
| 50 | cout << " -i, --clip-tips Clip tips shorter than k k-mers in length" << endl; |
| 51 | cout << " -d, --del-isolated Delete isolated contigs shorter than k k-mers in length" << endl; |
| 52 | cout << " -f, --fasta-out Output file is in fasta format (only sequences) instead of gfa (unless graph is colored)" << endl; |
| 53 | cout << " -b, --bfg-out Output file is in bfg/bfi format (Bifrost graph and index) instead of gfa (unless graph is colored)" << endl; |
| 54 | cout << " -n, --no-compress-out Output files must be uncompressed" << endl << endl; |
| 55 | cout << " -N, --no-index-out No index file is created" << endl << endl; |
| 56 | cout << " -v, --verbose Print information messages during execution" << endl << endl; |
| 57 | |
| 58 | cout << "[PARAMETERS]: update" << endl << endl; |
| 59 | |
| 60 | cout << " > Mandatory with required argument:" << endl << endl; |
| 61 | |
| 62 | cout << " -g, --input-graph-file Input graph file to update in gfa(.gz) or bfg format" << endl; |
| 63 | cout << " -s, --input-seq-file Input sequence file in fasta/fastq(.gz) format" << endl; |
| 64 | cout << " Multiple files can be provided as a list in a text file (one file per line)" << endl; |
| 65 | cout << " K-mers with exactly 1 occurrence in the input sequence files will be discarded" << endl; |
| 66 | cout << " -r, --input-ref-file Input reference file in fasta/fastq(.gz) or gfa(.gz) format" << endl; |
| 67 | cout << " Multiple files can be provided as a list in a text file (one file per line)" << endl; |
| 68 | cout << " All k-mers of the input reference files are used" << endl; |
| 69 | cout << " -o, --output-file Prefix for output file(s)" << endl << endl; |
| 70 | |
| 71 | cout << " > Optional with required argument:" << endl << endl; |
| 72 | |
| 73 | cout << " -I, --input-index-file Input index file associated with graph to update in bfi format" << endl; |
| 74 | cout << " -C, --input-color-file Input color file associated with graph to update in color.bfg format" << endl; |
| 75 | cout << " -t, --threads Number of threads (default is 1)" << endl; |
| 76 | cout << " -k, --kmer-length Length of k-mers (default is read from input graph file if built with Bifrost or 31)" << endl; |
| 77 | cout << " -m, --min-length Length of minimizers (default is read from input graph file if built with Bifrost or automatically chosen)" << endl << endl; |
| 78 | |
| 79 | cout << " > Optional with no argument:" << endl << endl; |
| 80 | |
| 81 | cout << " -i, --clip-tips Clip tips shorter than k k-mers in length" << endl; |
| 82 | cout << " -d, --del-isolated Delete isolated contigs shorter than k k-mers in length" << endl; |
| 83 | cout << " -f, --fasta-out Output file is in fasta format (only sequences) instead of gfa (unless colors are output)" << endl; |
| 84 | cout << " -b, --bfg-out Output file is in bfg/bfi format (Bifrost graph and index) instead of gfa (unless graph is colored)" << endl; |
| 85 | cout << " -n, --no-compress-out Output files must be uncompressed" << endl << endl; |
| 86 | cout << " -N, --no-index-out No index file is created" << endl << endl; |
| 87 | cout << " -v, --verbose Print information messages during execution" << endl << endl; |
| 88 | |
| 89 | cout << "[PARAMETERS]: query" << endl << endl; |
| 90 | |
| 91 | cout << " > Mandatory with required argument:" << endl << endl; |
| 92 | |
| 93 | cout << " -g, --input-graph-file Input graph file to query in gfa(.gz) or bfg" << endl; |
| 94 | cout << " -q, --input-query-file Input query file in fasta/fastq(.gz)" << endl; |
| 95 | cout << " Multiple files can be provided as a list in a text file (one file per line)" << endl; |
| 96 | cout << " -o, --output-file Prefix for output file" << endl; |
| 97 | cout << " -e, --ratio-kmers Ratio of k-mers from queries that must occur in the graph (default is 0.8)" << endl << endl; |
| 98 | |
| 99 | cout << " > Optional with required argument:" << endl << endl; |
| 100 | |
| 101 | cout << " -I, --input-index-file Input index file associated with graph to query in bfi format" << endl; |
| 102 | cout << " -C, --input-color-file Input color file associated with the graph to query in color.bfg format" << endl; |
| 103 | cout << " Presence/absence of queries will be output for each color" << endl; |
| 104 | cout << " -t, --threads Number of threads (default is 1)" << endl; |
| 105 | cout << " -k, --kmer-length Length of k-mers (default is read from input graph file if built with Bifrost or 31)" << endl; |
| 106 | cout << " -m, --min-length Length of minimizers (default is read from input graph file if built with Bifrost or or automatically chosen)" << endl << endl; |
| 107 | |
| 108 | cout << " > Optional with no argument:" << endl << endl; |
| 109 | |
| 110 | cout << " -a, --approximate Graph is searched with exact and inexact k-mers (1 substitution or indel) from queries" << endl; |
| 111 | cout << " -v, --verbose Print information messages during execution" << endl << endl; |
| 112 | } |
| 113 | |
| 114 | int parse_ProgramOptions(int argc, char **argv, CCDBG_Build_opt& opt) { |
| 115 | |
| 116 | int option_index = 0, c; |
| 117 | |
| 118 | const char* opt_string = "s:r:q:g:I:C:o:t:k:m:e:B:l:w:aidvcyfbnN"; |
| 119 | |
| 120 | static struct option long_options[] = { |
| 121 | |
| 122 | {"input-seq-file", required_argument, 0, 's'}, |
| 123 | {"input-ref-file", required_argument, 0, 'r'}, |
| 124 | {"input-query-file", required_argument, 0, 'q'}, |
| 125 | {"input-graph-file", required_argument, 0, 'g'}, |
| 126 | {"input-index-file", required_argument, 0, 'I'}, |
| 127 | {"input-color-file", required_argument, 0, 'C'}, |
| 128 | {"output-file", required_argument, 0, 'o'}, |
| 129 | {"threads", required_argument, 0, 't'}, |
| 130 | {"kmer-length", required_argument, 0, 'k'}, |
| 131 | {"min-length", required_argument, 0, 'm'}, |
| 132 | {"ratio-kmers", required_argument, 0, 'e'}, |
| 133 | {"bloom-bits", required_argument, 0, 'B'}, |
| 134 | {"load-mbbf", required_argument, 0, 'l'}, |
| 135 | {"write-mbbf", required_argument, 0, 'w'}, |
| 136 | {"approximate", no_argument, 0, 'a'}, |
| 137 | {"clip-tips", no_argument, 0, 'i'}, |
| 138 | {"del-isolated", no_argument, 0, 'd'}, |
| 139 | {"verbose", no_argument, 0, 'v'}, |
| 140 | {"colors", no_argument, 0, 'c'}, |
| 141 | //{"keep-mercy", no_argument, 0, 'y'}, |
| 142 | {"fasta-out", no_argument, 0, 'f'}, |
| 143 | {"bfg-out", no_argument, 0, 'b'}, |
| 144 | {"no-compress-out", no_argument, 0, 'n'}, |
| 145 | {"no-index-out", no_argument, 0, 'N'}, |
| 146 | {0, 0, 0, 0 } |
| 147 | }; |
| 148 | |
| 149 | if (strcmp(argv[1], "--version") == 0) return 1; // Print version |
| 150 | else if (strcmp(argv[1], "--help") == 0) return 2; // print help |
| 151 | |
| 152 | if (strcmp(argv[1], "build") == 0) opt.build = true; |
| 153 | else if (strcmp(argv[1], "update") == 0) opt.update = true; |
| 154 | else if (strcmp(argv[1], "query") == 0) opt.query = true; |
| 155 | |
| 156 | if (opt.build || opt.update || opt.query){ |
| 157 | |
| 158 | while ((c = getopt_long(argc, argv, opt_string, long_options, &option_index)) != -1) { |
| 159 | |
| 160 | switch (c) { |
| 161 | |
| 162 | case 's': |
| 163 | opt.filename_seq_in.push_back(optarg); |
| 164 | break; |
| 165 | case 'r': |
| 166 | opt.filename_ref_in.push_back(optarg); |
| 167 | break; |
| 168 | case 'q': |
| 169 | opt.filename_query_in.push_back(optarg); |
| 170 | break; |
| 171 | case 'g': |
| 172 | opt.filename_graph_in = optarg; |
| 173 | break; |
| 174 | case 'I': |
| 175 | opt.filename_index_in = optarg; |
| 176 | break; |
| 177 | case 'C': |
| 178 | opt.filename_colors_in = optarg; |
| 179 | break; |
| 180 | case 'o': |
| 181 | opt.prefixFilenameOut = optarg; |
| 182 | break; |
| 183 | case 't': |
| 184 | opt.nb_threads = atoi(optarg); |
| 185 | break; |
| 186 | case 'k': |
| 187 | opt.k = atoi(optarg); |
| 188 | break; |
| 189 | case 'm': |
| 190 | opt.g = atoi(optarg); |
| 191 | break; |
| 192 | case 'e': |
| 193 | opt.ratio_kmers = atof(optarg); |
| 194 | break; |
| 195 | case 'B': |
| 196 | opt.nb_bits_kmers_bf = atoi(optarg); |
| 197 | break; |
| 198 | case 'w': |
| 199 | opt.outFilenameBBF = optarg; |
| 200 | break; |
| 201 | case 'l': |
| 202 | opt.inFilenameBBF = optarg; |
| 203 | break; |
| 204 | case 'a': |
| 205 | opt.inexact_search = true; |
| 206 | break; |
| 207 | case 'i': |
| 208 | opt.clipTips = true; |
| 209 | break; |
| 210 | case 'd': |
| 211 | opt.deleteIsolated = true; |
| 212 | break; |
| 213 | case 'v': |
| 214 | opt.verbose = true; |
| 215 | break; |
| 216 | case 'c': |
| 217 | opt.outputColors = true; |
| 218 | break; |
| 219 | /*case 'y': |
| 220 | opt.useMercyKmers = true; |
| 221 | break;*/ |
| 222 | case 'f': |
| 223 | opt.outputGFA = false; |
| 224 | opt.outputFASTA = true; |
| 225 | break; |
| 226 | case 'b': |
| 227 | opt.outputGFA = false; |
| 228 | opt.outputBFG = true; |
| 229 | break; |
| 230 | case 'n': |
| 231 | opt.compressOutput = false; |
| 232 | break; |
| 233 | case 'N': |
| 234 | opt.writeIndexFile = false; |
| 235 | break; |
| 236 | default: break; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | bool check_ProgramOptions(CCDBG_Build_opt& opt) { |
| 245 | |
| 246 | bool ret = true; |
| 247 | |
| 248 | size_t max_threads = std::thread::hardware_concurrency(); |
| 249 | |
| 250 | auto check_files = [&](vector<string>& v_files) { |
| 251 | |
| 252 | vector<string> files_tmp; |
| 253 | |
| 254 | char* buffer = new char[4096](); |
| 255 | |
| 256 | for (const auto& file : v_files) { |
| 257 | |
| 258 | if (!check_file_exists(file)) { |
| 259 | |
| 260 | cerr << "Error: File " << file << " not found." << endl; |
| 261 | ret = false; |
| 262 | } |
| 263 | else { |
| 264 | |
| 265 | const int format = FileParser::getFileFormat(file.c_str()); |
| 266 | |
| 267 | if (format >= 0) files_tmp.push_back(file); // File is FASTA/FASTQ/GFA/GRAPH.BFG |
| 268 | else { |
| 269 | |
| 270 | FILE* fp = fopen(file.c_str(), "r"); |
| 271 | |
| 272 | if (fp != NULL){ |
| 273 | |
| 274 | fclose(fp); |
| 275 | |
| 276 | ifstream ifs_file_txt(file); |
| 277 | istream i_file_txt(ifs_file_txt.rdbuf()); |
| 278 | |
| 279 | size_t i = 0; |
| 280 | |
| 281 | while (i_file_txt.getline(buffer, 4096).good()){ |
| 282 | |
| 283 | fp = fopen(buffer, "r"); |
| 284 | |
| 285 | if (fp == NULL) { |
| 286 | |
| 287 | cerr << "Error: Could not open file at line " << i << " in file " << file << " for reading." << endl; |
| 288 | ret = false; |
| 289 | break; |
| 290 | } |
| 291 | else { |
| 292 | |
| 293 | fclose(fp); |
| 294 | files_tmp.push_back(string(buffer)); |
| 295 | } |
| 296 | |
| 297 | ++i; |
| 298 | } |
| 299 | |
| 300 | if (i_file_txt.fail() && (i == 0)) { |
| 301 | |
| 302 | cerr << "Error: File " << file << " is neither FASTA, FASTQ nor GFA." << endl; |
| 303 | cerr << "If it is a list of files, it is either empty or has a line with >4096 characters." << endl; |
| 304 | ret = false; |
| 305 | } |
| 306 | |
| 307 | ifs_file_txt.close(); |
| 308 | } |
| 309 | else { |
| 310 | |
| 311 | cerr << "Error: Could not open file " << file << " for reading." << endl; |
| 312 | ret = false; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | v_files = move(files_tmp); |
| 319 | |
| 320 | delete[] buffer; |
| 321 | }; |
| 322 | |
| 323 | // Check general parameters |
| 324 | |
| 325 | if (!opt.build && !opt.update && !opt.query){ |
| 326 | |
| 327 | cerr << "Error: No command selected (can be 'build' or 'update' or 'query')." << endl; |
| 328 | ret = false; |
| 329 | } |
| 330 | |
| 331 | if (opt.nb_threads <= 0){ |
| 332 | |
| 333 | cerr << "Error: Number of threads cannot be less than or equal to 0." << endl; |
| 334 | ret = false; |
| 335 | } |
| 336 | |
| 337 | if (opt.nb_threads > max_threads){ |
| 338 | |
| 339 | cerr << "Error: Number of threads cannot be greater than or equal to " << max_threads << "." << endl; |
| 340 | ret = false; |
| 341 | } |
| 342 | |
| 343 | if (opt.k <= 2){ |
| 344 | |
| 345 | cerr << "Error: Length k of k-mers cannot be less than 3." << endl; |
| 346 | ret = false; |
| 347 | } |
| 348 | |
| 349 | if (opt.k >= MAX_KMER_SIZE){ |
| 350 | |
| 351 | cerr << "Error: Length k of k-mers cannot exceed " << (MAX_KMER_SIZE - 1) << "." << endl; |
| 352 | cerr << "To enable a larger k, recompile Bifrost with the appropriate MAX_KMER_SIZE variable." << endl; |
| 353 | ret = false; |
| 354 | } |
| 355 | |
| 356 | if (opt.g == 0){ |
| 357 | |
| 358 | cerr << "Error: Length m of minimizers cannot be equal to 0." << endl; |
| 359 | ret = false; |
| 360 | } |
| 361 | |
| 362 | if ((opt.g >= 0) && (opt.g > opt.k - 2)) { |
| 363 | |
| 364 | cerr << "Error: Length m of minimizers cannot exceed k - 2 (" << (opt.k - 2) << ")." << endl; |
| 365 | ret = false; |
| 366 | } |
| 367 | |
| 368 | if (opt.query){ // Check param. command build |
| 369 | |
| 370 | if (opt.prefixFilenameOut.length() == 0) { |
| 371 | |
| 372 | cerr << "Error: No output filename prefix given." << endl; |
| 373 | ret = false; |
| 374 | } |
| 375 | else { |
| 376 | |
| 377 | const string out = opt.prefixFilenameOut + ".tsv"; |
| 378 | |
| 379 | FILE* fp = fopen(out.c_str(), "w"); |
| 380 | |
| 381 | if (fp == NULL) { |
| 382 | |
| 383 | cerr << "Error: Could not open file for writing output of query in TSV format: " << out << "." << endl; |
| 384 | ret = false; |
| 385 | } |
| 386 | else { |
| 387 | |
| 388 | fclose(fp); |
| 389 | if (remove(out.c_str()) != 0) cerr << "Error: Could not remove temporary file " << out << "." << endl; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (opt.filename_query_in.size() == 0) { |
| 394 | |
| 395 | cerr << "Error: Missing input query files." << endl; |
| 396 | ret = false; |
| 397 | } |
| 398 | else check_files(opt.filename_query_in); |
| 399 | |
| 400 | if ((opt.ratio_kmers < 0.0) || (opt.ratio_kmers > 1.0)) { |
| 401 | |
| 402 | cerr << "Error: Ratio of k-mers from queries that must occur in the graph cannot be less than 0.0 or more than 1.0 (" << opt.ratio_kmers << ")." << endl; |
| 403 | ret = false; |
| 404 | } |
| 405 | |
| 406 | if (opt.g > opt.k - 2){ |
| 407 | |
| 408 | cerr << "Error: Length m of minimizers cannot exceed k - 2 (" << (opt.k - 2) << ")." << endl; |
| 409 | ret = false; |
| 410 | } |
| 411 | } |
| 412 | else { |
| 413 | |
| 414 | if (opt.prefixFilenameOut.length() == 0) { |
| 415 | |
| 416 | cerr << "Error: No output filename prefix given." << endl; |
| 417 | ret = false; |
| 418 | } |
| 419 | else { |
| 420 | |
| 421 | const string out = opt.prefixFilenameOut + (opt.outputGFA ? ".gfa" : ".fasta"); |
| 422 | |
| 423 | FILE* fp = fopen(out.c_str(), "w"); |
| 424 | |
| 425 | if (fp == NULL) { |
| 426 | |
| 427 | cerr << "Error: Could not open file for writing output graph in GFA format: " << out << "." << endl; |
| 428 | ret = false; |
| 429 | } |
| 430 | else { |
| 431 | |
| 432 | fclose(fp); |
| 433 | if (remove(out.c_str()) != 0) cerr << "Error: Could not remove temporary file " << out << "." << endl; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if ((opt.filename_seq_in.size() + opt.filename_ref_in.size()) == 0) { |
| 438 | |
| 439 | cerr << "Error: Missing input files." << endl; |
| 440 | ret = false; |
| 441 | } |
| 442 | else { |
| 443 | |
| 444 | check_files(opt.filename_seq_in); |
| 445 | check_files(opt.filename_ref_in); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | if (opt.build){ // Check param. command build |
| 450 | |
| 451 | if (opt.outFilenameBBF.length() != 0){ |
| 452 | |
| 453 | FILE* fp = fopen(opt.outFilenameBBF.c_str(), "wb"); |
| 454 | |
| 455 | if (fp == NULL) { |
| 456 | |
| 457 | cerr << "Error: Could not open Blocked Bloom filter file " << opt.outFilenameBBF << " for writing." << endl; |
| 458 | ret = false; |
| 459 | } |
| 460 | else { |
| 461 | |
| 462 | fclose(fp); |
| 463 | |
| 464 | if (remove(opt.outFilenameBBF.c_str()) != 0){ |
| 465 | |
| 466 | cerr << "Error: Could not remove temporary file " << opt.outFilenameBBF << "." << endl; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (opt.inFilenameBBF.length() != 0){ |
| 472 | |
| 473 | if (check_file_exists(opt.inFilenameBBF)){ |
| 474 | |
| 475 | FILE* fp = fopen(opt.inFilenameBBF.c_str(), "rb"); |
| 476 | |
| 477 | if (fp == NULL) { |
| 478 | |
| 479 | cerr << "Error: Could not read input Blocked Bloom filter file " << opt.inFilenameBBF << "." << endl; |
| 480 | ret = false; |
| 481 | } |
| 482 | else fclose(fp); |
| 483 | } |
| 484 | else { |
| 485 | |
| 486 | cerr << "Error: Input Blocked Bloom filter " << opt.inFilenameBBF << " file does not exist." << endl; |
| 487 | ret = false; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (opt.build || opt.update) { |
| 493 | |
| 494 | if (opt.outputFASTA && opt.outputBFG) { |
| 495 | |
| 496 | cerr << "Error: Two output format selected: FASTA and GFA. Rerun with one format selected." << endl; |
| 497 | ret = false; |
| 498 | } |
| 499 | |
| 500 | if (opt.outputBFG && !opt.writeIndexFile) { |
| 501 | |
| 502 | cerr << "Error: Bifrost index file output (.bfi) is required to output Bifrost graph file (.bfg). Remove the no index output argument." << endl; |
| 503 | ret = false; |
| 504 | } |
| 505 | |
| 506 | } |
| 507 | |
| 508 | if (opt.update || opt.query) { |
| 509 | |
| 510 | if (opt.filename_graph_in.length() == 0){ |
| 511 | |
| 512 | cerr << "Error: No graph file was provided in input." << endl; |
| 513 | ret = false; |
| 514 | } |
| 515 | else if (!check_file_exists(opt.filename_graph_in)){ |
| 516 | |
| 517 | cerr << "Error: The graph file does not exist or is not a valid input file format." << endl; |
| 518 | ret = false; |
| 519 | } |
| 520 | else { |
| 521 | |
| 522 | FILE* fp = fopen(opt.filename_graph_in.c_str(), "r"); |
| 523 | |
| 524 | if (fp == NULL) { |
| 525 | |
| 526 | cerr << "Error: Could not read input graph file " << opt.filename_graph_in << "." << endl; |
| 527 | ret = false; |
| 528 | } |
| 529 | else fclose(fp); |
| 530 | } |
| 531 | |
| 532 | if (opt.filename_colors_in.length() != 0){ |
| 533 | |
| 534 | if (!check_file_exists(opt.filename_colors_in)){ |
| 535 | |
| 536 | cerr << "Error: The input color file does not exist or is not a valid input file format." << endl; |
| 537 | ret = false; |
| 538 | } |
| 539 | else { |
| 540 | |
| 541 | FILE* fp = fopen(opt.filename_colors_in.c_str(), "rb"); |
| 542 | |
| 543 | if (fp == NULL) { |
| 544 | |
| 545 | cerr << "Error: Could not read input color file " << opt.filename_colors_in << "." << endl; |
| 546 | ret = false; |
| 547 | } |
| 548 | else fclose(fp); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (opt.filename_index_in.length() != 0){ |
| 553 | |
| 554 | if (!check_file_exists(opt.filename_index_in)){ |
| 555 | |
| 556 | cerr << "Error: The input index file does not exist or is not a valid input file format." << endl; |
| 557 | ret = false; |
| 558 | } |
| 559 | else { |
| 560 | |
| 561 | FILE* fp = fopen(opt.filename_index_in.c_str(), "rb"); |
| 562 | |
| 563 | if (fp == NULL) { |
| 564 | |
| 565 | cerr << "Error: Could not read index file " << opt.filename_index_in << "." << endl; |
| 566 | ret = false; |
| 567 | } |
| 568 | else fclose(fp); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | int main(int argc, char **argv){ |
| 577 | |
| 578 | if (argc < 2) PrintUsage(); |
| 579 | else { |
| 580 | |
| 581 | CCDBG_Build_opt opt; |
| 582 | |
| 583 | opt.outputColors = false; // We dont know yet if we want colors or not |
| 584 | |
| 585 | const int print = parse_ProgramOptions(argc, argv, opt); // Parse input parameters |
| 586 | |
| 587 | if (print == 1) PrintVersion(); |
| 588 | else if (print == 2) PrintUsage(); |
| 589 | else if (check_ProgramOptions(opt)) { |
| 590 | |
| 591 | bool success = true; // Abort if any operation goes wrong |
| 592 | |
| 593 | if (opt.build){ // Build the graph |
| 594 | |
| 595 | if (opt.outputColors){ |
| 596 | |
| 597 | ColoredCDBG<> ccdbg(opt.k, opt.g); |
| 598 | |
| 599 | success = ccdbg.buildGraph(opt); |
| 600 | |
| 601 | if (success) success = ccdbg.simplify(opt.deleteIsolated, opt.clipTips, opt.verbose); |
| 602 | if (success) success = ccdbg.buildColors(opt); |
| 603 | if (success) success = ccdbg.write(opt.prefixFilenameOut, opt.nb_threads, opt.writeIndexFile, opt.compressOutput, opt.verbose); |
| 604 | } |
| 605 | else { |
| 606 | |
| 607 | CompactedDBG<> cdbg(opt.k, opt.g); |
| 608 | |
| 609 | success = cdbg.build(opt); |
| 610 | |
| 611 | if (success) success = cdbg.simplify(opt.deleteIsolated, opt.clipTips, opt.verbose); |
| 612 | if (success) success = cdbg.write(opt.prefixFilenameOut, opt.nb_threads, opt.outputGFA, opt.outputFASTA, opt.outputBFG, opt.writeIndexFile, opt.compressOutput, opt.verbose); |
| 613 | } |
| 614 | } |
| 615 | else if (opt.update){ |
| 616 | |
| 617 | CCDBG_Build_opt lopt = opt; |
| 618 | |
| 619 | if (lopt.filename_colors_in.size() != 0){ // If colors in or out |
| 620 | |
| 621 | ColoredCDBG<> ccdbg1(lopt.k, lopt.g); |
| 622 | |
| 623 | if (lopt.filename_index_in.length() == 0) success = ccdbg1.read(lopt.filename_graph_in, lopt.filename_colors_in, lopt.nb_threads, lopt.verbose); |
| 624 | else success = ccdbg1.read(lopt.filename_graph_in, lopt.filename_index_in, lopt.filename_colors_in, lopt.nb_threads, lopt.verbose); |
| 625 | |
| 626 | if (success) { |
| 627 | |
| 628 | lopt.k = ccdbg1.getK(); |
| 629 | lopt.g = ccdbg1.getG(); |
| 630 | |
| 631 | ColoredCDBG<> ccdbg2(lopt.k, lopt.g); |
| 632 | |
| 633 | if (success) success = ccdbg2.buildGraph(lopt); |
| 634 | if (success) success = ccdbg2.buildColors(lopt); |
| 635 | |
| 636 | if (success) { |
| 637 | |
| 638 | const size_t ccdbg1_len = ccdbg1.length(); |
| 639 | const size_t ccdbg2_len = ccdbg2.length(); |
| 640 | |
| 641 | ColoredCDBG<>& ccdbg_a = (ccdbg1_len > ccdbg2_len) ? ccdbg1 : ccdbg2; |
| 642 | ColoredCDBG<>& ccdbg_b = (ccdbg1_len > ccdbg2_len) ? ccdbg2 : ccdbg1; |
| 643 | |
| 644 | if (success) success = ccdbg_a.merge(move(ccdbg_b), lopt.nb_threads, lopt.verbose); |
| 645 | |
| 646 | if (success) success = ccdbg_a.simplify(lopt.deleteIsolated, lopt.clipTips, lopt.verbose); |
| 647 | if (success) success = ccdbg_a.write(lopt.prefixFilenameOut, lopt.nb_threads, lopt.writeIndexFile, lopt.compressOutput, lopt.verbose); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | else { |
| 652 | |
| 653 | CompactedDBG<> cdbg1(lopt.k, lopt.g); |
| 654 | |
| 655 | if (lopt.filename_index_in.length() == 0) success = cdbg1.read(lopt.filename_graph_in, lopt.nb_threads, lopt.verbose); |
| 656 | else success = cdbg1.read(lopt.filename_graph_in, lopt.filename_index_in, lopt.nb_threads, lopt.verbose); |
| 657 | |
| 658 | if (success) { |
| 659 | |
| 660 | lopt.k = cdbg1.getK(); |
| 661 | lopt.g = cdbg1.getG(); |
| 662 | |
| 663 | CompactedDBG<> cdbg2(lopt.k, lopt.g); |
| 664 | |
| 665 | if (success) success = cdbg2.build(lopt); |
| 666 | if (success) { |
| 667 | |
| 668 | const size_t cdbg1_len = cdbg1.length(); |
| 669 | const size_t cdbg2_len = cdbg2.length(); |
| 670 | |
| 671 | CompactedDBG<>& cdbg_a = (cdbg1_len > cdbg2_len) ? cdbg1 : cdbg2; |
| 672 | CompactedDBG<>& cdbg_b = (cdbg1_len > cdbg2_len) ? cdbg2 : cdbg1; |
| 673 | |
| 674 | if (success) { |
| 675 | |
| 676 | success = cdbg_a.merge(cdbg_b, lopt.nb_threads, lopt.verbose); |
| 677 | cdbg_b.clear(); |
| 678 | } |
| 679 | |
| 680 | if (success) success = cdbg_a.simplify(lopt.deleteIsolated, lopt.clipTips, lopt.verbose); |
| 681 | if (success) success = cdbg_a.write(lopt.prefixFilenameOut, lopt.nb_threads, lopt.outputGFA, |
| 682 | lopt.outputFASTA, lopt.outputBFG, lopt.writeIndexFile, lopt.compressOutput, lopt.verbose); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | else if (opt.query){ |
| 688 | |
| 689 | if (opt.filename_colors_in.size() != 0){ |
| 690 | |
| 691 | ColoredCDBG<> ccdbg(opt.k, opt.g); |
| 692 | |
| 693 | if (opt.filename_index_in.length() == 0) success = ccdbg.read(opt.filename_graph_in, opt.filename_colors_in, opt.nb_threads, opt.verbose); |
| 694 | else success = ccdbg.read(opt.filename_graph_in, opt.filename_index_in, opt.filename_colors_in, opt.nb_threads, opt.verbose); |
| 695 | |
| 696 | if (success) success = ccdbg.search(opt.filename_query_in, opt.prefixFilenameOut, opt.ratio_kmers, opt.inexact_search, opt.nb_threads, opt.verbose); |
| 697 | } |
| 698 | else { |
| 699 | |
| 700 | CompactedDBG<> cdbg(opt.k, opt.g); |
| 701 | |
| 702 | if (opt.filename_index_in.length() == 0) success = cdbg.read(opt.filename_graph_in, opt.nb_threads, opt.verbose); |
| 703 | else success = cdbg.read(opt.filename_graph_in, opt.filename_index_in, opt.nb_threads, opt.verbose); |
| 704 | |
| 705 | if (success) success = cdbg.search(opt.filename_query_in, opt.prefixFilenameOut, opt.ratio_kmers, opt.inexact_search, opt.nb_threads, opt.verbose); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (!success) { |
| 710 | |
| 711 | cerr << "Operation aborted." << endl; |
| 712 | exit(1); |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | } |