| 1 | /*! \file */ |
| 2 | |
| 3 | #include <stdint.h> |
| 4 | #include <string.h> |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #include <fstream> |
| 8 | #include <iostream> |
| 9 | #include <sstream> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "call_consensus_pileup.h" |
| 13 | #include "call_variants.h" |
| 14 | #include "get_common_variants.h" |
| 15 | #include "get_masked_amplicons.h" |
| 16 | #include "remove_reads_from_amplicon.h" |
| 17 | #include "suffix_tree.h" |
| 18 | #include "trim_primer_quality.h" |
| 19 | |
| 20 | const std::string VERSION = "1.4.4"; |
| 21 | |
| 22 | struct args_t { |
| 23 | std::string bam; // -i |
| 24 | std::string bed; // -b |
| 25 | std::string text; // -t |
| 26 | std::string seq_id; // -i for consensus |
| 27 | std::string prefix; // -p |
| 28 | std::string ref; // -r |
| 29 | std::string region; // -R |
| 30 | uint8_t min_qual; // -q |
| 31 | uint8_t sliding_window; // -s |
| 32 | double min_threshold; // -t |
| 33 | double min_insert_threshold; // -c |
| 34 | int min_length; // -m |
| 35 | std::string f1; // -1 |
| 36 | std::string f2; // -2 |
| 37 | std::string adp_path; // -a |
| 38 | uint32_t min_depth; // -m |
| 39 | char gap; // -n |
| 40 | bool keep_min_coverage; // -k |
| 41 | std::string primer_pair_file; // -f |
| 42 | int32_t primer_offset; // -x |
| 43 | std::string file_list; // -f |
| 44 | bool write_no_primers_flag; // -e |
| 45 | std::string gff; // -g |
| 46 | bool keep_for_reanalysis; // -k |
| 47 | bool gapped_depth; // -G |
| 48 | } g_args; |
| 49 | |
| 50 | void print_usage() { |
| 51 | std::cout |
| 52 | << "Usage: ivar [command " |
| 53 | "<trim|variants|filtervariants|consensus|getmasked|removereads|" |
| 54 | "version|help>]\n" |
| 55 | "\n" |
| 56 | " Command Description\n" |
| 57 | " trim Trim reads in aligned BAM file\n" |
| 58 | " variants Call variants from aligned BAM file\n" |
| 59 | " filtervariants Filter variants across replicates or samples\n" |
| 60 | " consensus Call consensus from aligned BAM file\n" |
| 61 | " getmasked Detect primer mismatches and get primer " |
| 62 | "indices for the amplicon to be masked\n" |
| 63 | " removereads Remove reads from trimmed BAM file\n" |
| 64 | " version Show version information\n" |
| 65 | "\n" |
| 66 | "To view detailed usage for each command type `ivar <command>` \n"; |
| 67 | } |
| 68 | |
| 69 | void print_trim_usage() { |
| 70 | std::cout |
| 71 | << "Usage: ivar trim -i [<input.bam>] -b <primers.bed> [-p <prefix>] [-m " |
| 72 | "<min-length>] [-q <min-quality>] [-s <sliding-window-width>]\n\n" |
| 73 | "Input Options Description\n" |
| 74 | " -i BAM file, with aligned reads, to " |
| 75 | "trim primers and quality. If not specified will use standard in\n" |
| 76 | " -b BED file with primer sequences and positions. If no " |
| 77 | "BED file is specified, only quality trimming will be done.\n" |
| 78 | " -f [EXPERIMENTAL] Primer pair information file " |
| 79 | "containing left and right primer names for the same amplicon " |
| 80 | "separated by a tab\n" |
| 81 | " If provided, reads that do not fall within atleat " |
| 82 | "one amplicon will be ignored prior to primer trimming.\n" |
| 83 | " -x Primer position offset (Default: 0). Reads that " |
| 84 | "occur at the specified offset positions relative to primer positions " |
| 85 | "will also be trimmed.\n" |
| 86 | " -m Minimum length of read to retain after trimming " |
| 87 | "(Default: 50% average length of the first 1000 reads)\n" |
| 88 | " -q Minimum quality threshold for sliding window to " |
| 89 | "pass (Default: 20)\n" |
| 90 | " -s Width of sliding window (Default: 4)\n" |
| 91 | " -e Include reads with no primers. By default, reads " |
| 92 | "with no primers are excluded\n" |
| 93 | " -k Keep reads to allow for reanalysis: keep reads " |
| 94 | "which would be dropped by\n" |
| 95 | " alignment length filter or primer requirements, but " |
| 96 | "mark them QCFAIL\n\n" |
| 97 | "Output Options Description\n" |
| 98 | " -p Prefix for the output BAM file. If none is specified output will go to std out\n"; |
| 99 | } |
| 100 | |
| 101 | void print_variants_usage() { |
| 102 | std::cout |
| 103 | << "Usage: samtools mpileup -aa -A -d 0 -B -Q 0 --reference " |
| 104 | "[<reference-fasta] <input.bam> | ivar variants -p <prefix> [-q " |
| 105 | "<min-quality>] [-t <min-frequency-threshold>] [-m <minimum depth>] " |
| 106 | "[-r <reference-fasta>] [-g GFF file]\n\n" |
| 107 | "Note : samtools mpileup output must be piped into ivar variants\n\n" |
| 108 | "Input Options Description\n" |
| 109 | " -q Minimum quality score threshold to count base " |
| 110 | "(Default: 20)\n" |
| 111 | " -t Minimum frequency threshold(0 - 1) to call variants " |
| 112 | "(Default: 0.03)\n" |
| 113 | " -m Minimum read depth to call variants (Default: 0)\n" |
| 114 | " -G Count gaps towards depth. By default, gaps are not counted\n" |
| 115 | " -r Reference file used for alignment. This is used to " |
| 116 | "translate the nucleotide sequences and identify intra host single " |
| 117 | "nucleotide variants\n" |
| 118 | " -g A GFF file in the GFF3 format can be supplied to " |
| 119 | "specify coordinates of open reading frames (ORFs). In absence of GFF " |
| 120 | "file, amino acid translation will not be done.\n\n" |
| 121 | "Output Options Description\n" |
| 122 | " -p (Required) Prefix for the output tsv variant " |
| 123 | "file\n\n"; |
| 124 | } |
| 125 | |
| 126 | void print_filtervariants_usage() { |
| 127 | std::cout |
| 128 | << "Usage: ivar filtervariants -p <prefix> replicate-one.tsv " |
| 129 | "replicate-two.tsv ... OR ivar filtervariants -p <prefix> -f <text " |
| 130 | "file with one variant file per line> \n" |
| 131 | "Input: Variant tsv files for each replicate/sample\n\n" |
| 132 | "Input Options Description\n" |
| 133 | " -t Minimum fraction of files required to contain the " |
| 134 | "same variant. Specify value within [0,1]. (Default: 1)\n" |
| 135 | " -f A text file with one variant file per line.\n\n" |
| 136 | "Output Options Description\n" |
| 137 | " -p (Required) Prefix for the output filtered tsv " |
| 138 | "file\n"; |
| 139 | } |
| 140 | |
| 141 | void print_consensus_usage() { |
| 142 | std::cout |
| 143 | << "Usage: samtools mpileup -aa -A -d 0 -Q 0 <input.bam> | ivar " |
| 144 | "consensus -p <prefix> \n\n" |
| 145 | "Note : samtools mpileup output must be piped into `ivar " |
| 146 | "consensus`\n\n" |
| 147 | "Input Options Description\n" |
| 148 | " -q Minimum quality score threshold to count base " |
| 149 | "(Default: 20)\n" |
| 150 | " -t Minimum frequency threshold(0 - 1) to call " |
| 151 | "consensus. (Default: 0)\n" |
| 152 | " Frequently used thresholds | Description\n" |
| 153 | " ---------------------------|------------\n" |
| 154 | " 0 | Majority or most " |
| 155 | "common base\n" |
| 156 | " 0.2 | Bases that make up " |
| 157 | "atleast 20% of the depth at a position\n" |
| 158 | " 0.5 | Strict or bases that " |
| 159 | "make up atleast 50% of the depth at a position\n" |
| 160 | " 0.9 | Strict or bases that " |
| 161 | "make up atleast 90% of the depth at a position\n" |
| 162 | " 1 | Identical or bases " |
| 163 | "that make up 100% of the depth at a position. Will have highest " |
| 164 | "ambiguities\n" |
| 165 | " -c Minimum insertion frequency threshold(0 - 1) to " |
| 166 | "call consensus. (Default: 0.8)\n" |
| 167 | " Frequently used thresholds | Description\n" |
| 168 | " ---------------------------|------------\n" |
| 169 | " 0 | Allow insertion if it " |
| 170 | "appears even once\n" |
| 171 | " 0.2 | Insertions with at " |
| 172 | "least 20% of the depth at a position\n" |
| 173 | " 0.5 | Insertion with at " |
| 174 | "least 50% of the depth at a position\n" |
| 175 | " 0.9 | Insertions with at " |
| 176 | "least 90% of the depth at a position\n" |
| 177 | " 1 | Insertion with 100% of " |
| 178 | "the depth at a position. Will have highest ambiguities\n" |
| 179 | " -m Minimum depth to call consensus(Default: 10)\n" |
| 180 | " -k If '-k' flag is added, regions with depth less than " |
| 181 | "minimum depth will not be added to the consensus sequence. Using " |
| 182 | "'-k' will override any option specified using -n \n" |
| 183 | " -n (N/-) Character to print in regions with less than " |
| 184 | "minimum coverage(Default: N)\n\n" |
| 185 | "Output Options Description\n" |
| 186 | " -p (Required) Prefix for the output fasta file and " |
| 187 | "quality file\n" |
| 188 | " -i (Optional) Name of fasta header. By default, the " |
| 189 | "prefix is used to create the fasta header in the following format, " |
| 190 | "Consensus_<prefix>_threshold_<frequency-threshold>_quality_<minimum-" |
| 191 | "quality>_<min-insert-threshold>\n"; |
| 192 | } |
| 193 | |
| 194 | void print_removereads_usage() { |
| 195 | std::cout |
| 196 | << "Usage: ivar removereads -i <input.trimmed.bam> -p <prefix> -t " |
| 197 | "<text-file-with-primer-indices> -b <primers.bed> \n" |
| 198 | "Note: This step is used only for amplicon-based sequencing.\n\n" |
| 199 | "Input Options Description\n" |
| 200 | " -i (Required) Input BAM file trimmed with ‘ivar " |
| 201 | "trim’. Must be sorted which can be done using `samtools sort`.\n" |
| 202 | " -t (Required) Text file with primer indices separated " |
| 203 | "by spaces. This is the output of `getmasked` command.\n" |
| 204 | " -b (Required) BED file with primer sequences and " |
| 205 | "positions.\n\n" |
| 206 | "Output Options Description\n" |
| 207 | " -p (Required) Prefix for the output filtered BAM " |
| 208 | "file\n"; |
| 209 | } |
| 210 | |
| 211 | void print_getmasked_usage() { |
| 212 | std::cout << "Usage: ivar getmasked -i <input-filtered.tsv> -b <primers.bed> " |
| 213 | "-f <primer_pairs.tsv> -p <prefix>\n" |
| 214 | "Note: This step is used only for amplicon-based sequencing.\n\n" |
| 215 | "Input Options Description\n" |
| 216 | " -i (Required) Input filtered variants tsv " |
| 217 | "generated from `ivar filtervariants`\n" |
| 218 | " -b (Required) BED file with primer sequences and " |
| 219 | "positions\n" |
| 220 | " -f (Required) Primer pair information file " |
| 221 | "containing left and right primer names for the same amplicon " |
| 222 | "separated by a tab\n" |
| 223 | "Output Options Description\n" |
| 224 | " -p (Required) Prefix for the output text file\n"; |
| 225 | } |
| 226 | |
| 227 | void print_trimadapter_usage() { |
| 228 | std::cout << "NOTE: EXPERIMENTAL FEATURE\n" |
| 229 | "Usage: ivar trimadapter [-f1 <input-fastq>] [-f2 " |
| 230 | "<input-fastq-2>] [-p prefix] [-a <adapter-fasta-file>]\n\n" |
| 231 | "Input Options Description\n" |
| 232 | " -1 (Required) Input fastq file\n" |
| 233 | " -2 Input fastq file 2 (for pair ended reads)\n" |
| 234 | " -a (Required) Adapter Fasta File\n\n" |
| 235 | "Output Options Description\n" |
| 236 | " -p (Required) Prefix of output fastq files\n"; |
| 237 | } |
| 238 | |
| 239 | void print_version_info() { |
| 240 | std::cout << "iVar version " << VERSION << std::endl |
| 241 | << "\nPlease raise issues and bug reports at " |
| 242 | "https://github.com/andersen-lab/ivar/\n\n"; |
| 243 | } |
| 244 | |
| 245 | static const char *trim_opt_str = "i:b:f:x:p:m:q:s:ekh?"; |
| 246 | static const char *variants_opt_str = "p:t:q:m:r:g:Gh?"; |
| 247 | static const char *consensus_opt_str = "i:p:q:t:c:m:n:kh?"; |
| 248 | static const char *removereads_opt_str = "i:p:t:b:h?"; |
| 249 | static const char *filtervariants_opt_str = "p:t:f:h?"; |
| 250 | static const char *getmasked_opt_str = "i:b:f:p:h?"; |
| 251 | static const char *trimadapter_opt_str = "1:2:p:a:h?"; |
| 252 | |
| 253 | std::string get_filename_without_extension(std::string f, std::string ext) { |
| 254 | if (ext.length() > f.length()) // If extension longer than filename |
| 255 | return f; |
| 256 | if (f.substr(f.length() - ext.length(), ext.length()).compare(ext) == 0) { |
| 257 | return f.substr(0, f.length() - ext.length()); |
| 258 | } |
| 259 | return f; |
| 260 | } |
| 261 | |
| 262 | /*! |
| 263 | Main Function |
| 264 | |
| 265 | This is where the command line arguments into iVar are parsed. |
| 266 | iVar first parses the first argument and depending on the command it either |
| 267 | returns the version or uses GNU getopt to parse the remaining arguments. |
| 268 | */ |
| 269 | |
| 270 | int main(int argc, char *argv[]) { |
| 271 | if (argc == 1) { |
| 272 | print_usage(); |
| 273 | return -1; |
| 274 | } |
| 275 | std::stringstream cl_cmd; |
| 276 | cl_cmd << "@PG\tID:ivar-" << argv[1] << "\tPN:ivar\tVN:" << VERSION |
| 277 | << "\tCL:" << argv[0] << " "; |
| 278 | for (int i = 1; i < argc; ++i) { |
| 279 | cl_cmd << argv[i]; |
| 280 | if (i != argc - 1) cl_cmd << " "; |
| 281 | } |
| 282 | cl_cmd << "\n\0"; |
| 283 | std::string cmd(argv[1]); |
| 284 | if (cmd.compare("-v") == 0) { |
| 285 | print_version_info(); |
| 286 | return 0; |
| 287 | } |
| 288 | int opt = 0, res = 0; |
| 289 | // Sift arg by 1 for getopt |
| 290 | argv[1] = argv[0]; |
| 291 | argv++; |
| 292 | argc--; |
| 293 | // ivar trim |
| 294 | if (cmd.compare("trim") == 0) { |
| 295 | g_args.min_qual = 20; |
| 296 | g_args.sliding_window = 4; |
| 297 | g_args.min_length = -1; |
| 298 | g_args.write_no_primers_flag = false; |
| 299 | g_args.keep_for_reanalysis = false; |
| 300 | g_args.bed = ""; |
| 301 | g_args.primer_pair_file = ""; |
| 302 | g_args.primer_offset = 0; |
| 303 | opt = getopt(argc, argv, trim_opt_str); |
| 304 | while (opt != -1) { |
| 305 | switch (opt) { |
| 306 | case 'i': |
| 307 | g_args.bam = optarg; |
| 308 | break; |
| 309 | case 'b': |
| 310 | g_args.bed = optarg; |
| 311 | break; |
| 312 | case 'f': |
| 313 | g_args.primer_pair_file = optarg; |
| 314 | break; |
| 315 | case 'x': |
| 316 | g_args.primer_offset = std::stoi(optarg); |
| 317 | break; |
| 318 | case 'p': |
| 319 | g_args.prefix = optarg; |
| 320 | break; |
| 321 | case 'm': |
| 322 | g_args.min_length = std::stoi(optarg); |
| 323 | break; |
| 324 | case 'q': |
| 325 | g_args.min_qual = std::stoi(optarg); |
| 326 | break; |
| 327 | case 's': |
| 328 | g_args.sliding_window = std::stoi(optarg); |
| 329 | break; |
| 330 | case 'e': |
| 331 | g_args.write_no_primers_flag = true; |
| 332 | break; |
| 333 | case 'k': |
| 334 | g_args.keep_for_reanalysis = true; |
| 335 | break; |
| 336 | case 'h': |
| 337 | case '?': |
| 338 | print_trim_usage(); |
| 339 | return -1; |
| 340 | break; |
| 341 | } |
| 342 | opt = getopt(argc, argv, trim_opt_str); |
| 343 | } |
| 344 | if (g_args.bam.empty() && isatty(STDIN_FILENO)) { |
| 345 | std::cout << "Please supply a BAM file using -i or supply the input file " |
| 346 | "through standard input" |
| 347 | << std::endl |
| 348 | << std::endl; |
| 349 | print_trim_usage(); |
| 350 | return -1; |
| 351 | } |
| 352 | g_args.prefix = get_filename_without_extension(g_args.prefix, ".bam"); |
| 353 | res = trim_bam_qual_primer(g_args.bam, g_args.bed, g_args.prefix, |
| 354 | g_args.min_qual, g_args.sliding_window, |
| 355 | cl_cmd.str(), g_args.write_no_primers_flag, |
| 356 | g_args.keep_for_reanalysis, g_args.min_length, |
| 357 | g_args.primer_pair_file, g_args.primer_offset); |
| 358 | } |
| 359 | // ivar variants |
| 360 | else if (cmd.compare("variants") == 0) { |
| 361 | g_args.min_qual = 20; |
| 362 | g_args.min_threshold = 0.03; |
| 363 | g_args.min_depth = 0; |
| 364 | g_args.ref = ""; |
| 365 | g_args.gff = ""; |
| 366 | g_args.gapped_depth = false; |
| 367 | opt = getopt(argc, argv, variants_opt_str); |
| 368 | while (opt != -1) { |
| 369 | switch (opt) { |
| 370 | case 'p': |
| 371 | g_args.prefix = optarg; |
| 372 | break; |
| 373 | case 't': |
| 374 | g_args.min_threshold = atof(optarg); |
| 375 | break; |
| 376 | case 'q': |
| 377 | g_args.min_qual = std::stoi(optarg); |
| 378 | break; |
| 379 | case 'm': |
| 380 | g_args.min_depth = std::stoi(optarg); |
| 381 | break; |
| 382 | case 'r': |
| 383 | g_args.ref = optarg; |
| 384 | break; |
| 385 | case 'g': |
| 386 | g_args.gff = optarg; |
| 387 | break; |
| 388 | case 'G': |
| 389 | g_args.gapped_depth = true; |
| 390 | break; |
| 391 | case 'h': |
| 392 | case '?': |
| 393 | print_variants_usage(); |
| 394 | return 0; |
| 395 | break; |
| 396 | } |
| 397 | opt = getopt(argc, argv, variants_opt_str); |
| 398 | } |
| 399 | if (g_args.prefix.empty()) { |
| 400 | print_variants_usage(); |
| 401 | return -1; |
| 402 | } |
| 403 | if (g_args.gff.empty()) |
| 404 | std::cout << "A GFF file containing the open reading frames (ORFs) has " |
| 405 | "not been provided. Amino acid translation will not be done." |
| 406 | << std::endl; |
| 407 | if (g_args.ref.empty()) |
| 408 | std::cout << "A reference sequence has not been supplied. Amino acid " |
| 409 | "translation will not be done." |
| 410 | << std::endl; |
| 411 | if (!g_args.gff.empty() && |
| 412 | g_args.ref.empty()) { // GFF specified but no reference then exit |
| 413 | std::cout << "Please specify reference (using -r) based on which the GFF " |
| 414 | "file was computed." |
| 415 | << std::endl; |
| 416 | print_variants_usage(); |
| 417 | return -1; |
| 418 | } |
| 419 | g_args.prefix = get_filename_without_extension(g_args.prefix, ".tsv"); |
| 420 | g_args.min_threshold = |
| 421 | (g_args.min_threshold < 0 || g_args.min_threshold > 1) |
| 422 | ? 0.03 |
| 423 | : g_args.min_threshold; |
| 424 | if (isatty(STDIN_FILENO)) { |
| 425 | std::cout << "Please pipe mpileup into `ivar variants` command.\n\n"; |
| 426 | print_variants_usage(); |
| 427 | return -1; |
| 428 | } |
| 429 | res = call_variants_from_plup(std::cin, g_args.prefix, g_args.min_qual, |
| 430 | g_args.min_threshold, g_args.min_depth, |
| 431 | g_args.ref, g_args.gff, g_args.gapped_depth); |
| 432 | } |
| 433 | // ivar consensus |
| 434 | else if (cmd.compare("consensus") == 0) { |
| 435 | opt = getopt(argc, argv, consensus_opt_str); |
| 436 | g_args.seq_id = ""; |
| 437 | g_args.min_threshold = 0; |
| 438 | g_args.min_depth = 10; |
| 439 | g_args.gap = 'N'; |
| 440 | g_args.min_qual = 20; |
| 441 | g_args.keep_min_coverage = true; |
| 442 | g_args.min_insert_threshold = 0.8; |
| 443 | while (opt != -1) { |
| 444 | switch (opt) { |
| 445 | case 't': |
| 446 | g_args.min_threshold = atof(optarg); |
| 447 | break; |
| 448 | case 'c': |
| 449 | g_args.min_insert_threshold = atof(optarg); |
| 450 | break; |
| 451 | case 'i': |
| 452 | g_args.seq_id = optarg; |
| 453 | break; |
| 454 | case 'p': |
| 455 | g_args.prefix = optarg; |
| 456 | break; |
| 457 | case 'm': |
| 458 | g_args.min_depth = std::stoi(optarg); |
| 459 | break; |
| 460 | case 'n': |
| 461 | g_args.gap = optarg[0]; |
| 462 | break; |
| 463 | case 'q': |
| 464 | g_args.min_qual = std::stoi(optarg); |
| 465 | break; |
| 466 | case 'k': |
| 467 | g_args.keep_min_coverage = false; |
| 468 | case 'g': |
| 469 | break; |
| 470 | case 'h': |
| 471 | case '?': |
| 472 | print_consensus_usage(); |
| 473 | return 0; |
| 474 | break; |
| 475 | } |
| 476 | opt = getopt(argc, argv, consensus_opt_str); |
| 477 | } |
| 478 | if (g_args.prefix.empty()) { |
| 479 | print_consensus_usage(); |
| 480 | return -1; |
| 481 | } |
| 482 | if (isatty(STDIN_FILENO)) { |
| 483 | std::cout << "Please pipe mpileup into `ivar consensus` command.\n\n"; |
| 484 | print_consensus_usage(); |
| 485 | return -1; |
| 486 | } |
| 487 | g_args.prefix = get_filename_without_extension(g_args.prefix, ".fa"); |
| 488 | g_args.prefix = get_filename_without_extension(g_args.prefix, ".fasta"); |
| 489 | g_args.gap = (g_args.gap != 'N' && g_args.gap != '-') |
| 490 | ? 'N' |
| 491 | : g_args.gap; // Accept only N or - |
| 492 | std::cout << "Minimum Quality: " << (uint16_t)g_args.min_qual << std::endl; |
| 493 | std::cout << "Threshold: " << g_args.min_threshold << std::endl; |
| 494 | std::cout << "Minimum depth: " << (unsigned)g_args.min_depth << std::endl; |
| 495 | std::cout << "Minimum Insert Threshold: " << g_args.min_insert_threshold |
| 496 | << std::endl; |
| 497 | if (!g_args.keep_min_coverage) |
| 498 | std::cout << "Regions with depth less than minimum depth will not added " |
| 499 | "to consensus" |
| 500 | << std::endl; |
| 501 | else |
| 502 | std::cout << "Regions with depth less than minimum depth covered by: " |
| 503 | << g_args.gap << std::endl; |
| 504 | res = call_consensus_from_plup( |
| 505 | std::cin, g_args.seq_id, g_args.prefix, g_args.min_qual, |
| 506 | g_args.min_threshold, g_args.min_depth, g_args.gap, |
| 507 | g_args.keep_min_coverage, g_args.min_insert_threshold); |
| 508 | } else if (cmd.compare("removereads") == 0) { |
| 509 | opt = getopt(argc, argv, removereads_opt_str); |
| 510 | while (opt != -1) { |
| 511 | switch (opt) { |
| 512 | case 'i': |
| 513 | g_args.bam = optarg; |
| 514 | break; |
| 515 | case 't': |
| 516 | g_args.text = optarg; |
| 517 | break; |
| 518 | case 'b': |
| 519 | g_args.bed = optarg; |
| 520 | break; |
| 521 | case 'p': |
| 522 | g_args.prefix = optarg; |
| 523 | break; |
| 524 | case 'h': |
| 525 | case '?': |
| 526 | print_removereads_usage(); |
| 527 | return 0; |
| 528 | break; |
| 529 | } |
| 530 | opt = getopt(argc, argv, removereads_opt_str); |
| 531 | } |
| 532 | if (g_args.bam.empty() || g_args.prefix.empty() || g_args.bed.empty() || |
| 533 | g_args.text.empty()) { |
| 534 | print_removereads_usage(); |
| 535 | return -1; |
| 536 | } |
| 537 | std::string s; |
| 538 | std::vector<std::string> amp; |
| 539 | std::ifstream fin(g_args.text.c_str()); |
| 540 | while (getline(fin, s, '\t')) { |
| 541 | amp.push_back(s); |
| 542 | } |
| 543 | fin.close(); |
| 544 | g_args.prefix = get_filename_without_extension(g_args.prefix, ".bam"); |
| 545 | res = rmv_reads_from_amplicon(g_args.bam, g_args.region, g_args.prefix, amp, |
| 546 | g_args.bed, cl_cmd.str()); |
| 547 | |
| 548 | } else if (cmd.compare("filtervariants") == 0) { |
| 549 | opt = getopt(argc, argv, filtervariants_opt_str); |
| 550 | g_args.min_threshold = 1; |
| 551 | while (opt != -1) { |
| 552 | switch (opt) { |
| 553 | case 'p': |
| 554 | g_args.prefix = optarg; |
| 555 | break; |
| 556 | case 't': |
| 557 | g_args.min_threshold = atof(optarg); |
| 558 | break; |
| 559 | case 'f': |
| 560 | g_args.file_list = optarg; |
| 561 | break; |
| 562 | case 'h': |
| 563 | case '?': |
| 564 | print_filtervariants_usage(); |
| 565 | return 0; |
| 566 | break; |
| 567 | } |
| 568 | opt = getopt(argc, argv, filtervariants_opt_str); |
| 569 | } |
| 570 | if (g_args.min_threshold < 0 || g_args.min_threshold > 1) { |
| 571 | print_filtervariants_usage(); |
| 572 | return -1; |
| 573 | } |
| 574 | if (optind >= argc && g_args.file_list.empty()) { |
| 575 | print_filtervariants_usage(); |
| 576 | return -1; |
| 577 | } |
| 578 | if (g_args.prefix.empty()) { |
| 579 | print_filtervariants_usage(); |
| 580 | return -1; |
| 581 | } |
| 582 | g_args.prefix = get_filename_without_extension(g_args.prefix, ".tsv"); |
| 583 | // Read files from list |
| 584 | char **files = new char *[100]; |
| 585 | int nfiles = 100, ctr = 0; |
| 586 | std::string line; |
| 587 | if (!g_args.file_list.empty()) { // File list supplied |
| 588 | std::ifstream file_fin = std::ifstream(g_args.file_list); |
| 589 | while (std::getline(file_fin, line)) { |
| 590 | files[ctr] = strdup(line.c_str()); |
| 591 | if (ctr == nfiles - 1) { |
| 592 | nfiles += 100; |
| 593 | *files = (char *)realloc(*files, nfiles * (sizeof(char *))); |
| 594 | } |
| 595 | ctr++; |
| 596 | } |
| 597 | file_fin.close(); |
| 598 | nfiles = (nfiles > ctr) ? ctr : nfiles; |
| 599 | res = common_variants(g_args.prefix, g_args.min_threshold, files, nfiles); |
| 600 | // Free files, nfiles |
| 601 | for (int i = 0; i < nfiles; ++i) { |
| 602 | free(files[i]); |
| 603 | } |
| 604 | delete[] files; |
| 605 | } else { |
| 606 | res = common_variants(g_args.prefix, g_args.min_threshold, argv + optind, |
| 607 | argc - optind); |
| 608 | } |
| 609 | } else if (cmd.compare("getmasked") == 0) { |
| 610 | opt = getopt(argc, argv, getmasked_opt_str); |
| 611 | while (opt != -1) { |
| 612 | switch (opt) { |
| 613 | case 'i': |
| 614 | g_args.bam = optarg; |
| 615 | break; |
| 616 | case 'b': |
| 617 | g_args.bed = optarg; |
| 618 | break; |
| 619 | case 'f': |
| 620 | g_args.primer_pair_file = optarg; |
| 621 | break; |
| 622 | case 'p': |
| 623 | g_args.prefix = optarg; |
| 624 | break; |
| 625 | case 'h': |
| 626 | case '?': |
| 627 | print_getmasked_usage(); |
| 628 | return 0; |
| 629 | break; |
| 630 | } |
| 631 | opt = getopt(argc, argv, getmasked_opt_str); |
| 632 | } |
| 633 | if (g_args.bed.empty() || g_args.bam.empty() || g_args.prefix.empty() || |
| 634 | g_args.primer_pair_file.empty()) { |
| 635 | print_getmasked_usage(); |
| 636 | return -1; |
| 637 | } |
| 638 | g_args.prefix = get_filename_without_extension(g_args.prefix, ".txt"); |
| 639 | res = get_primers_with_mismatches(g_args.bed, g_args.bam, g_args.prefix, |
| 640 | g_args.primer_pair_file); |
| 641 | } else if (cmd.compare("trimadapter") == 0) { |
| 642 | opt = getopt(argc, argv, trimadapter_opt_str); |
| 643 | while (opt != -1) { |
| 644 | switch (opt) { |
| 645 | case '1': |
| 646 | g_args.f1 = optarg; |
| 647 | break; |
| 648 | case '2': |
| 649 | g_args.f2 = optarg; |
| 650 | break; |
| 651 | case 'p': |
| 652 | g_args.prefix = optarg; |
| 653 | break; |
| 654 | case 'a': |
| 655 | g_args.adp_path = optarg; |
| 656 | break; |
| 657 | case 'h': |
| 658 | case '?': |
| 659 | print_trimadapter_usage(); |
| 660 | return 0; |
| 661 | break; |
| 662 | } |
| 663 | opt = getopt(argc, argv, trimadapter_opt_str); |
| 664 | } |
| 665 | if (g_args.f1.empty() || g_args.prefix.empty() || g_args.adp_path.empty()) { |
| 666 | print_trimadapter_usage(); |
| 667 | return -1; |
| 668 | } |
| 669 | res = trim_adapter(g_args.f1, g_args.f2, g_args.adp_path, g_args.prefix); |
| 670 | } else if (cmd.compare("version") == 0) { |
| 671 | print_version_info(); |
| 672 | } else { |
| 673 | std::cout << "Unknown command: \"" << cmd << "\"" << std::endl << std::endl; |
| 674 | print_usage(); |
| 675 | } |
| 676 | return res; |
| 677 | } |