1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
|
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
*/
#include <kapp/main.h>
#include <kapp/args.h>
#include <klib/text.h>
#include <klib/log.h>
#include <klib/out.h>
#include <klib/status.h>
#include <klib/rc.h>
#include <klib/printf.h>
#include <kfs/directory.h>
#include <kfs/file.h>
#include <kapp/log-xml.h>
#include <align/writer-refseq.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <limits.h>
#include "Globals.h"
#include "loader-imp.h"
/*: ARGS
Summary:
Load a BAM formatted data file
Usage:
--help display this text and quit
--version display the version string and quit
[global-options] [options] <file...> [ --remap [options] <file>... ]...
//:global-options
Global Options:
* options effecting logging
log-level <level> logging level values: [fatal|sys|int|err|warn|info|0-5] default: info
xml-log <filename> produce an XML-formatted log file
* options effecting performance optimisation
tmpfs <directory> where to store temparary files, default: '/tmp'
cache-size <mbytes> the limit in MB for temparary files
* options effecting error limits
max-err-count <number> the maximum number of errors to ignore
max-warning-dup-flag <count> the limit for number of duplicate flag mismatch warnings
//:options
Options:
output <name> name of the output, required
config <file> reference configuration file (See Configuration)
header <file> file containing the SAM header
remap special option to enable processing sets of remapped files. remap MUST be given between each set, all regular options can be respecified, in fact the output must be unique for each set. This is for when a set of reads are aligned multiple times, for example against different reference builds or with different aligners. This mode ensures that spot ids are the same across the several outputs.
Debugging Options:
only-verify exit after verifying existence of references
max-rec-count <number> exit after processing this many records (per file)
nomatch-log <path> log alignments with no matching bases
Filtering Options:
minimum-match <number> minimum number of matches for an alignment
no-secondary ignore alignments marked as secondary
accept-dups accept spots with inconsistent PCR duplicate flags
accept-nomatch accept alignments with no matching bases
ref-config limit processing to references in the config file, ignoring all others
ref-filter <name> limit processing to the given reference, ignoring all others
min-mapq <number> filter secondary alignments by minimum mapping quality
Rare or Esoteric Options:
input <directory> where to find fasta files
ref-file <file> fasta file with references
unsorted expect unsorted input (requires more memory)
sorted require sorted input
TI look for trace id optional tag
unaligned <file> file without aligned reads
Deprecated Options:
use-OQ use OQ option column for quality values instead of QUAL
no-verify skip verify existence of references from the BAM file
accept-hard-clip allow hard clipping in CIGAR
allow-multi-map allow the same reference to be mapped to multiple names in the input files
edit-aligned-qual <number> convert quality at aligned positions to this value
cs turn on awareness of colorspace
qual-quant quality scores quantization level
keep-mismatch-qual don't quantized quality at mismatched positions
Example:
bam-load -o /tmp/SRZ123456 -k analysis.bam.cfg 123456.bam
*/
/* MARK: Arguments and Usage */
static char const option_input[] = "input";
static char const option_output[] = "output";
static char const option_tmpfs[] = "tmpfs";
static char const option_config[] = "config";
static char const option_min_mapq[] = "min-mapq";
static char const option_qual_compress[] = "qual-quant";
static char const option_cache_size[] = "cache-size";
static char const option_unsorted[] = "unsorted";
static char const option_sorted[] = "sorted";
static char const option_max_err_count[] = "max-err-count";
static char const option_max_rec_count[] = "max-rec-count";
static char const option_no_verify[] = "no-verify";
static char const option_only_verify[] = "only-verify";
static char const option_use_qual[] = "use-QUAL";
static char const option_ref_filter[] = "ref-filter";
static char const option_ref_config[] = "ref-config";
static char const option_edit_aligned_qual[] = "edit-aligned-qual";
static char const option_unaligned[] = "unaligned";
static char const option_accept_dup[] = "accept-dups";
static char const option_accept_nomatch[] = "accept-nomatch";
static char const option_nomatch_log[] = "nomatch-log";
static char const option_keep_mismatch_qual[] = "keep-mismatch-qual";
static char const option_min_match[] = "minimum-match";
static char const option_header[] = "header";
static char const option_no_cs[] = "no-cs";
static char const option_no_secondary[] = "no-secondary";
static char const option_ref_file[] = "ref-file";
static char const option_TI[] = "TI";
static char const option_max_warn_dup_flag[] = "max-warning-dup-flag";
static char const option_accept_hard_clip[] = "accept-hard-clip";
static char const option_allow_multi_map[] = "allow-multi-map";
static char const option_allow_secondary[] = "make-spots-with-secondary";
static char const option_defer_secondary[] = "defer-secondary";
#define OPTION_INPUT option_input
#define OPTION_OUTPUT option_output
#define OPTION_TMPFS option_tmpfs
#define OPTION_CONFIG option_config
#define OPTION_MINMAPQ option_min_mapq
#define OPTION_QCOMP option_qual_compress
#define OPTION_CACHE_SIZE option_cache_size
#define OPTION_MAX_ERR_COUNT option_max_err_count
#define OPTION_MAX_REC_COUNT option_max_rec_count
#define OPTION_UNALIGNED option_unaligned
#define OPTION_ACCEPT_DUP option_accept_dup
#define OPTION_ACCEPT_NOMATCH option_accept_nomatch
#define OPTION_NOMATCH_LOG option_nomatch_log
#define OPTION_MIN_MATCH option_min_match
#define OPTION_HEADER option_header
#define OPTION_NO_CS option_no_cs
#define OPTION_NO_SECONDARY option_no_secondary
#define OPTION_REF_FILE option_ref_file
#define OPTION_TI option_TI
#define OPTION_MAX_WARN_DUP_FLAG option_max_warn_dup_flag
#define OPTION_ACCEPT_HARD_CLIP option_accept_hard_clip
#define OPTION_ALLOW_MULTI_MAP option_allow_multi_map
#define OPTION_ALLOW_SECONDARY option_allow_secondary
#define OPTION_DEFER_SECONDARY option_defer_secondary
#define ALIAS_INPUT "i"
#define ALIAS_OUTPUT "o"
#define ALIAS_TMPFS "t"
#define ALIAS_CONFIG "k"
#define ALIAS_MINMAPQ "q"
#define ALIAS_QCOMP "Q"
#define ALIAS_MAX_ERR_COUNT "E"
#define ALIAS_UNALIGNED "u"
#define ALIAS_ACCEPT_DUP "d"
#define ALIAS_NO_SECONDARY "P"
#define ALIAS_REF_FILE "r"
static
char const * input_usage[] =
{
"Path where to get fasta files from.",
NULL
};
static
char const * output_usage[] =
{
"Path and Name of the output database.",
NULL
};
static
char const * tmpfs_usage[] =
{
"Path to be used for scratch files.",
NULL
};
static
char const * config_usage[] =
{
"Path to configuration file:",
"maps the input BAM file's reference names to the equivalent GenBank accession.",
"It is a tab-delimited text file with unix line endings (\\n - LF) with the following fields in this order:",
"#1 reference name as it occurs in the BAM file's SN field of @SQ header record;",
"#2 INSDC reference ID",
NULL
};
static
char const * min_mapq_usage[] =
{
"Filter secondary alignments by minimum mapping quality.",
NULL
};
static
char const * qcomp_usage[] =
{
"Quality scores quantization level, can be a number (0: none, 1: 2bit, 2: 1bit), or a string like '1:10,10:20,20:30,30:-' (which is equivalent to 1) (nb. the endpoint is exclusive).",
NULL
};
static
char const * unsorted_usage[] =
{
"Tell the loader to expect unsorted input (requires more memory)",
NULL
};
static
char const * sorted_usage[] =
{
"Tell the loader to require sorted input",
NULL
};
static
char const * cache_size_usage[] =
{
"Set the cache size in MB for the temporary indices",
NULL
};
static
char const * mrc_usage[] =
{
"Set the maximum number of records to process from the BAM file",
NULL
};
static
char const * mec_usage[] =
{
"Set the maximum number of errors to ignore from the BAM file",
NULL
};
static
char const * no_verify_usage[] =
{
"Skip verify existence of references from the BAM file",
NULL
};
static
char const * only_verify_usage[] =
{
"Exit after verifying existence of references from the BAM file",
NULL
};
static
char const * use_QUAL_usage[] =
{
"use QUAL column for quality values (default is to use OQ if it is available)",
NULL
};
static
char const * use_ref_filter[] =
{
"Only process alignments to the given reference",
NULL
};
static
char const * use_ref_config[] =
{
"Only process alignments to references in the config file",
NULL
};
static
char const * use_edit_aligned_qual[] =
{
"Convert quality at aligned positions to this value",
NULL
};
static
char const * use_keep_mismatch_qual[] =
{
"Don't quantized quality at mismatched positions",
NULL
};
static
char const * use_unaligned[] =
{
"Specify file without aligned reads",
NULL
};
static
char const * use_accept_dups[] =
{
"Accept spots inconsistent PCR duplicate flags",
NULL
};
static
char const * use_accept_nomatch[] =
{
"Accept alignments with no matching bases",
NULL
};
static
char const * use_nomatch_log[] =
{
"Where to write info for alignments with no matching bases",
NULL
};
static
char const * use_min_match[] =
{
"minimum number of matches for an alignment",
NULL
};
static
char const * use_header[] =
{
"path to a file containing the SAM header to store in the resulting cSRA, recommended in case of multiple input BAMs",
NULL
};
static
char const * use_no_cs[] =
{
"turn off awareness of colorspace",
NULL
};
static
char const * use_no_secondary[] =
{
"ignore alignments marked as secondary",
NULL
};
static
char const * use_ref_file[] =
{
"path to a fasta file with references",
NULL
};
static
char const * use_TI[] =
{
"for trace alignments",
NULL
};
static
char const * use_max_dup_warnings[] =
{
"set limit for number of duplicate flag mismatch warnings",
NULL
};
static
char const * use_accept_hard_clip[] =
{
"accept hard clipping in CIGAR",
NULL
};
static
char const * use_allow_multi_map[] =
{
"allow the same reference to be mapped to multiple names in the input files",
"(default is disallow, old behaviors was to allow it)",
NULL
};
static
char const * use_allow_secondary[] =
{
"use secondary alignments for constructing spots",
NULL
};
static
char const * use_defer_secondary[] =
{
"defer processing of secondary alignments until the end of the file",
NULL
};
OptDef Options[] =
{
/* order here is same as in param array below!!! */
{ OPTION_INPUT, ALIAS_INPUT, NULL, input_usage, 1, true, false },
{ OPTION_OUTPUT, ALIAS_OUTPUT, NULL, output_usage, 1, true, true },
{ OPTION_CONFIG, ALIAS_CONFIG, NULL, config_usage, 1, true, false },
{ OPTION_HEADER, NULL, NULL, use_header, 1, true, false },
{ OPTION_TMPFS, ALIAS_TMPFS, NULL, tmpfs_usage, 1, true, false },
{ OPTION_UNALIGNED, ALIAS_UNALIGNED, NULL, use_unaligned, 256, true, false },
{ OPTION_ACCEPT_DUP, ALIAS_ACCEPT_DUP, NULL, use_accept_dups, 1, false, false },
{ OPTION_ACCEPT_NOMATCH, NULL, NULL, use_accept_nomatch, 1, false, false },
{ OPTION_NOMATCH_LOG, NULL, NULL, use_nomatch_log, 1, true, false },
{ OPTION_QCOMP, ALIAS_QCOMP, NULL, qcomp_usage, 1, true, false },
{ OPTION_MINMAPQ, ALIAS_MINMAPQ, NULL, min_mapq_usage, 1, true, false },
{ OPTION_CACHE_SIZE, NULL, NULL, cache_size_usage, 1, true, false },
{ OPTION_NO_CS, NULL, NULL, use_no_cs, 1, false, false },
{ OPTION_MIN_MATCH, NULL, NULL, use_min_match, 1, true, false },
{ OPTION_NO_SECONDARY, ALIAS_NO_SECONDARY, NULL, use_no_secondary, 1, false, false },
{ option_unsorted, NULL, NULL, unsorted_usage, 1, false, false },
{ option_sorted, NULL, NULL, sorted_usage, 1, false, false },
{ option_no_verify, NULL, NULL, no_verify_usage, 1, false, false },
{ option_only_verify, NULL, NULL, only_verify_usage, 1, false, false },
{ option_use_qual, NULL, NULL, use_QUAL_usage, 1, false, false },
{ option_ref_config, NULL, NULL, use_ref_config, 1, false, false },
{ option_ref_filter, NULL, NULL, use_ref_filter, 1, true, false },
{ option_edit_aligned_qual, NULL, NULL, use_edit_aligned_qual, 1, true, false },
{ option_keep_mismatch_qual, NULL, NULL, use_keep_mismatch_qual, 1, false, false },
{ OPTION_MAX_REC_COUNT, NULL, NULL, mrc_usage, 1, true, false },
{ OPTION_MAX_ERR_COUNT, ALIAS_MAX_ERR_COUNT, NULL, mec_usage, 1, true, false },
{ OPTION_REF_FILE, ALIAS_REF_FILE, NULL, use_ref_file, 0, true, false },
{ OPTION_TI, NULL, NULL, use_TI, 1, false, false },
{ OPTION_MAX_WARN_DUP_FLAG, NULL, NULL, use_max_dup_warnings, 1, true, false },
{ OPTION_ACCEPT_HARD_CLIP, NULL, NULL, use_accept_hard_clip, 1, false, false },
{ OPTION_ALLOW_MULTI_MAP, NULL, NULL, use_allow_multi_map, 1, false, false },
{ OPTION_ALLOW_SECONDARY, NULL, NULL, use_allow_secondary, 1, false, false },
{ OPTION_DEFER_SECONDARY, NULL, NULL, use_defer_secondary, 1, false, false }
};
const char* OptHelpParam[] =
{
/* order here is same as in OptDef array above!!! */
"path", /* input */
"path", /* output */
"path-to-file", /* config */
"path-to-file", /* header */
"path", /* tmpfs */
"path-to-file", /* unaligned */
NULL, /* accept dups */
NULL, /* accept no-match */
"path-to-file", /* no-match log */
"level", /* quality compression */
"phred-score", /* min. mapq */
"mbytes", /* cache size */
NULL, /* no colorspace */
"count", /* min. match count */
NULL, /* no secondary */
NULL, /* unsorted */
NULL, /* sorted */
NULL, /* no verify ref's */
NULL, /* quit after verify ref's */
NULL, /* force QUAL */
NULL, /* ref's from config */
"name", /* only this ref */
"new-value", /* value for aligned qualities */
NULL, /* no quantize mismatch qualities */
"number", /* max. record count to process */
"number", /* max. error count */
"path-to-file", /* reference fasta file */
NULL, /* use XT->TI */
"count", /* max. duplicate warning count */
NULL, /* allow hard clipping */
NULL, /* allow multimapping */
NULL, /* allow secondary */
NULL /* defer secondary */
};
rc_t UsageSummary (char const * progname)
{
return KOutMsg (
"Usage:\n"
"\t%s [options] <bam-file>\n"
"\n"
"Summary:\n"
"\tLoad a BAM formatted data file\n"
"\n"
"Example:\n"
"\t%s -o /tmp/SRZ123456 -k analysis.bam.cfg 123456.bam\n"
"\n"
,progname, progname);
}
char const UsageDefaultName[] = "bam-load";
rc_t CC Usage (const Args * args)
{
rc_t rc;
int i;
const char * progname = UsageDefaultName;
const char * fullpath = UsageDefaultName;
const size_t argsQty = sizeof(Options) / sizeof(Options[0]);
if (args == NULL)
rc = RC (rcApp, rcArgv, rcAccessing, rcSelf, rcNull);
else
rc = ArgsProgram (args, &fullpath, &progname);
if (rc)
progname = fullpath = UsageDefaultName;
UsageSummary (progname);
for(i = 0; i < argsQty; i++ ) {
if( Options[i].required && Options[i].help[0] != NULL ) {
HelpOptionLine(Options[i].aliases, Options[i].name, OptHelpParam[i], Options[i].help);
}
}
OUTMSG(("\nOptions:\n"));
for(i = 0; i < argsQty; i++ ) {
if( !Options[i].required && Options[i].help[0] != NULL ) {
HelpOptionLine(Options[i].aliases, Options[i].name, OptHelpParam[i], Options[i].help);
}
}
XMLLogger_Usage();
OUTMSG(("\n"));
HelpOptionsStandard ();
HelpVersion (fullpath, KAppVersion());
return rc;
}
/* MARK: Definitions and Globals */
#define SCHEMAFILE "/usr/lib/ncbi-vdb/align/align.vschema"
Globals G;
#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
#endif
static void set_pid(void)
{
G.pid = getpid();
}
static rc_t PathWithBasePath(char rslt[], size_t sz, char const path[], char const base[])
{
size_t const plen = strlen(path);
bool const hasBase = base && base[0];
bool const isBareName = strchr(path, '/') == NULL;
if (isBareName && hasBase) {
if (string_printf(rslt, sz, NULL, "%s/%s", base, path) == 0)
return 0;
}
else if (plen < sz) {
strncpy(rslt, path, sz);
return 0;
}
{
rc_t const rc = RC(rcApp, rcArgv, rcAccessing, rcBuffer, rcInsufficient);
(void)LOGERR(klogErr, rc, "The path to the file is too long");
return rc;
}
}
static rc_t OpenFile(KFile const **kf, char const path[], char const base[])
{
char fname[4096];
rc_t rc = PathWithBasePath(fname, sizeof(fname), path, base);
if (rc == 0) {
KDirectory *dir;
rc = KDirectoryNativeDir(&dir);
if (rc == 0) {
rc = KDirectoryOpenFileRead(dir, kf, "%s", fname);
KDirectoryRelease(dir);
}
}
return rc;
}
static rc_t LoadHeader(char const **rslt, char const path[], char const base[])
{
KFile const *kf;
rc_t rc = OpenFile(&kf, path, base);
*rslt = NULL;
if (rc == 0) {
uint64_t fsize;
rc = KFileSize(kf, &fsize);
if (rc == 0) {
char *fdata = malloc(fsize+1);
if (fdata) {
size_t nread;
rc = KFileRead(kf, 0, fdata, fsize, &nread);
if (rc == 0) {
if (nread) {
fdata[nread] = '\0';
*rslt = fdata;
}
else {
free(fdata);
rc = RC(rcApp, rcArgv, rcAccessing, rcFile, rcEmpty);
(void)PLOGERR(klogErr, (klogErr, rc, "File '$(file)' is empty", "file=%s", path));
}
}
else {
(void)PLOGERR(klogErr, (klogErr, rc, "Failed to read file '$(file)'", "file=%s", path));
}
}
else {
rc = RC(rcApp, rcArgv, rcAccessing, rcMemory, rcExhausted);
(void)PLOGERR(klogErr, (klogErr, rc, "Failed to read file '$(file)'", "file=%s", path));
}
}
KFileRelease(kf);
}
else {
(void)PLOGERR(klogErr, (klogErr, rc, "Failed to open file '$(file)'", "file=%s", path));
}
return rc;
}
static rc_t main_help_vers(int argc, char * argv[])
{
Args *args = NULL;
rc_t const rc = ArgsMakeAndHandle (&args, argc, argv, 2, Options,
sizeof Options / sizeof (OptDef), XMLLogger_Args, XMLLogger_ArgsQty);
ArgsWhack(args);
return rc;
}
static rc_t getArgValue(Args *const args, char const *name, int index, char const **result)
{
void const *value;
rc_t const rc = ArgsOptionValue(args, name, index, &value);
if (rc) return rc;
free((void *)*result);
*result = strdup(value);
assert(*result);
return 0;
}
static rc_t main_1(int argc, char *argv[], bool const continuing, unsigned const load)
{
Args *args;
rc_t rc;
unsigned n_aligned = 0;
unsigned n_unalgnd = 0;
char *aligned[256];
char *unalgnd[256];
char *name_buffer = NULL;
unsigned next_name = 0;
unsigned nbsz = 0;
char const *value;
char *dummy;
rc = ArgsMakeAndHandle (&args, argc, argv, 1, Options, sizeof(Options)/sizeof(Options[0]));
while (rc == 0) {
uint32_t pcount;
rc = ArgsOptionCount(args, option_only_verify, &pcount);
if (rc)
break;
G.onlyVerifyReferences |= (pcount > 0);
rc = ArgsOptionCount(args, option_no_verify, &pcount);
if (rc)
break;
G.noVerifyReferences |= (pcount > 0);
rc = ArgsOptionCount(args, option_use_qual, &pcount);
if (rc)
break;
G.useQUAL |= (pcount > 0);
rc = ArgsOptionCount(args, option_ref_config, &pcount);
if (rc)
break;
G.limit2config |= (pcount > 0);
rc = ArgsOptionCount(args, OPTION_REF_FILE, &pcount);
if (rc)
break;
if (pcount && G.refFiles) {
int i;
for (i = 0; G.refFiles[i]; ++i)
free((void *)G.refFiles[i]);
free((void *)G.refFiles);
}
G.refFiles = calloc(pcount + 1, sizeof(*(G.refFiles)));
if (!G.refFiles) {
rc = RC(rcApp, rcArgv, rcAccessing, rcMemory, rcExhausted);
break;
}
while(pcount-- > 0) {
rc = getArgValue(args, OPTION_REF_FILE, pcount, &G.refFiles[pcount]);
if (rc)
break;
}
rc = ArgsOptionCount (args, OPTION_TMPFS, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = getArgValue(args, OPTION_TMPFS, 0, &G.tmpfs);
if (rc)
break;
}
else if (pcount > 1)
{
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
OUTMSG (("Single parameter required\n"));
MiniUsage (args);
break;
}
rc = ArgsOptionCount (args, OPTION_INPUT, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = getArgValue(args, OPTION_INPUT, 0, &G.inpath);
if (rc)
break;
}
else if (pcount > 1)
{
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
OUTMSG (("Single input parameter required\n"));
MiniUsage (args);
break;
}
rc = ArgsOptionCount (args, option_ref_filter, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = getArgValue(args, option_ref_filter, 0, &G.refFilter);
if (rc)
break;
}
else if (pcount > 1)
{
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
OUTMSG (("Single parameter required\n"));
MiniUsage (args);
break;
}
rc = ArgsOptionCount (args, OPTION_CONFIG, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = getArgValue(args, OPTION_CONFIG, 0, &G.refXRefPath);
if (rc)
break;
}
else if (pcount > 1)
{
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
OUTMSG (("Single input parameter required\n"));
MiniUsage (args);
break;
}
rc = ArgsOptionCount (args, OPTION_OUTPUT, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = getArgValue(args, OPTION_OUTPUT, 0, &G.outpath);
if (rc)
break;
if (load == 0) {
G.firstOut = strdup(G.outpath);
}
value = strrchr(G.outpath, '/');
G.outname = value ? (value + 1) : G.outpath;
}
else if (pcount > 1)
{
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
OUTMSG (("Single output parameter required\n"));
MiniUsage (args);
break;
}
else if (!G.onlyVerifyReferences) {
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcInsufficient);
OUTMSG (("Output parameter required\n"));
MiniUsage (args);
break;
}
rc = ArgsOptionCount (args, OPTION_MINMAPQ, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = ArgsOptionValue(args, OPTION_MINMAPQ, 0, (const void **)&value);
if (rc)
break;
G.minMapQual = strtoul(value, &dummy, 0);
}
rc = ArgsOptionCount (args, OPTION_QCOMP, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = getArgValue(args, OPTION_QCOMP, 0, &G.QualQuantizer);
if (rc)
break;
}
rc = ArgsOptionCount (args, option_edit_aligned_qual, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = ArgsOptionValue (args, option_edit_aligned_qual, 0, (const void **)&value);
if (rc)
break;
G.alignedQualValue = strtoul(value, &dummy, 0);
if (G.alignedQualValue == 0) {
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcIncorrect);
OUTMSG (("edit-aligned-qual: bad value\n"));
MiniUsage (args);
break;
}
G.editAlignedQual = true;
}
rc = ArgsOptionCount (args, OPTION_CACHE_SIZE, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = ArgsOptionValue (args, OPTION_CACHE_SIZE, 0, (const void **)&value);
if (rc)
break;
G.cache_size = strtoul(value, &dummy, 0) * 1024UL * 1024UL;
if (G.cache_size == 0) {
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcIncorrect);
OUTMSG (("cache-size: bad value\n"));
MiniUsage (args);
break;
}
}
rc = ArgsOptionCount (args, OPTION_MAX_WARN_DUP_FLAG, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = ArgsOptionValue (args, OPTION_MAX_WARN_DUP_FLAG, 0, (const void **)&value);
if (rc)
break;
G.maxWarnCount_DupConflict = strtoul(value, &dummy, 0);
}
rc = ArgsOptionCount (args, option_unsorted, &pcount);
if (rc)
break;
G.expectUnsorted |= (pcount > 0);
rc = ArgsOptionCount (args, option_sorted, &pcount);
if (rc)
break;
G.requireSorted |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_MAX_REC_COUNT, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = ArgsOptionValue (args, OPTION_MAX_REC_COUNT, 0, (const void **)&value);
if (rc)
break;
G.maxAlignCount = strtoul(value, &dummy, 0);
}
rc = ArgsOptionCount (args, OPTION_MAX_ERR_COUNT, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = ArgsOptionValue (args, OPTION_MAX_ERR_COUNT, 0, (const void **)&value);
if (rc)
break;
G.maxErrCount = strtoul(value, &dummy, 0);
}
rc = ArgsOptionCount (args, OPTION_MIN_MATCH, &pcount);
if (rc)
break;
if (pcount == 1)
{
rc = ArgsOptionValue (args, OPTION_MIN_MATCH, 0, (const void **)&value);
if (rc)
break;
G.minMatchCount = strtoul(value, &dummy, 0);
}
rc = ArgsOptionCount (args, OPTION_ACCEPT_DUP, &pcount);
if (rc)
break;
G.acceptBadDups |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_ACCEPT_NOMATCH, &pcount);
if (rc)
break;
G.acceptNoMatch |= (pcount > 0);
rc = ArgsOptionCount (args, option_keep_mismatch_qual, &pcount);
if (rc)
break;
G.keepMismatchQual |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_NO_CS, &pcount);
if (rc)
break;
G.noColorSpace |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_NO_SECONDARY, &pcount);
if (rc)
break;
G.noSecondary |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_TI, &pcount);
if (rc)
break;
G.hasTI |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_ACCEPT_HARD_CLIP, &pcount);
if (rc)
break;
G.acceptHardClip |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_ALLOW_MULTI_MAP, &pcount);
if (rc)
break;
G.allowMultiMapping |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_ALLOW_SECONDARY, &pcount);
if (rc)
break;
G.assembleWithSecondary |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_DEFER_SECONDARY, &pcount);
if (rc)
break;
G.deferSecondary |= (pcount > 0);
rc = ArgsOptionCount (args, OPTION_NOMATCH_LOG, &pcount);
if (rc)
break;
if (pcount == 1)
{
KDirectory *dir;
rc = ArgsOptionValue (args, OPTION_NOMATCH_LOG, 0, (const void **)&value);
if (rc) break;
rc = KDirectoryNativeDir(&dir);
if (rc) break;
rc = KDirectoryCreateFile(dir, &G.noMatchLog, 0, 0664, kcmInit, "%s", value);
KDirectoryRelease(dir);
if (rc) break;
}
rc = ArgsOptionCount (args, OPTION_HEADER, &pcount);
if (rc)
break;
if (pcount == 1) {
rc = ArgsOptionValue (args, OPTION_HEADER, 0, (const void **)&value);
if (rc) break;
free((void *)G.headerText);
rc = LoadHeader(&G.headerText, value, G.inpath);
if (rc) break;
}
rc = ArgsParamCount (args, &pcount);
if (rc) break;
if (pcount == 0)
{
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcInsufficient);
MiniUsage (args);
break;
}
else if (pcount > sizeof(aligned)/sizeof(aligned[0])) {
rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
(void)PLOGERR(klogErr, (klogErr, rc, "$(count) input files is too many, $(max) is the limit",
"count=%u,max=%u", (unsigned)pcount, (unsigned)(sizeof(aligned)/sizeof(aligned[0]))));
break;
}
else {
unsigned need = G.inpath ? (strlen(G.inpath) + 1) * pcount : 0;
unsigned i;
for (i = 0; i < pcount; ++i) {
rc = ArgsParamValue(args, i, (const void **)&value);
if (rc) break;
need += strlen(value) + 1;
}
nbsz = need;
}
rc = ArgsOptionCount (args, OPTION_UNALIGNED, &pcount);
if (rc)
break;
if (pcount > 0)
{
unsigned need = G.inpath ? (strlen(G.inpath) + 1) * pcount : 0;
unsigned i;
for (i = 0; i < pcount; ++i) {
rc = ArgsOptionValue(args, OPTION_UNALIGNED, i, (const void **)&value);
if (rc) break;
need += strlen(value) + 1;
}
if (rc) break;
nbsz += need;
}
name_buffer = malloc(nbsz);
if (name_buffer == NULL) {
rc = RC(rcApp, rcArgv, rcAccessing, rcMemory, rcExhausted);
break;
}
rc = ArgsOptionCount (args, OPTION_UNALIGNED, &pcount);
if (rc == 0) {
unsigned i;
for (i = 0; i < pcount; ++i) {
rc = ArgsOptionValue(args, OPTION_UNALIGNED, i, (const void **)&value);
if (rc) break;
unalgnd[n_unalgnd++] = name_buffer + next_name;
rc = PathWithBasePath(name_buffer + next_name, nbsz - next_name, value, G.inpath);
if (rc) break;
next_name += strlen(name_buffer + next_name) + 1;
}
if (rc) break;
}
else
break;
rc = ArgsParamCount (args, &pcount);
if (rc == 0) {
unsigned i;
for (i = 0; i < pcount; ++i) {
rc = ArgsParamValue(args, i, (const void **)&value);
if (rc) break;
aligned[n_aligned++] = name_buffer + next_name;
rc = PathWithBasePath(name_buffer + next_name, nbsz - next_name, value, G.inpath);
if (rc) break;
next_name += strlen(name_buffer + next_name) + 1;
}
}
else
break;
rc = run(argv[0], n_aligned, (char const **)aligned, n_unalgnd, (char const **)unalgnd, continuing);
break;
}
free(name_buffer);
if (rc) {
(void)PLOGERR(klogErr, (klogErr, rc, "load failed",
"severity=total,status=failure,accession=%s,errors=%u", G.outname, G.errCount));
} else {
(void)PLOGMSG(klogInfo, (klogInfo, "loaded",
"severity=total,status=success,accession=%s,errors=%u", G.outname, G.errCount));
}
ArgsWhack(args);
return rc;
}
static void cleanupGlobal(void)
{
if (G.refFiles) {
int i;
for (i = 0; G.refFiles[i]; ++i)
free((void *)G.refFiles[i]);
free((void *)G.refFiles);
}
free((void *)G.tmpfs);
free((void *)G.inpath);
free((void *)G.refFilter);
free((void *)G.refXRefPath);
free((void *)G.outpath);
free((void *)G.firstOut);
free((void *)G.headerText);
free((void *)G.QualQuantizer);
free((void *)G.schemaPath);
}
static int find_arg(char const *const *const query, int const first, int const argc, char **const argv)
{
int i;
for (i = first; i < argc; ++i) {
int j;
for (j = 0; query[j] != NULL; ++j) {
if (strcmp(argv[i], query[j]) == 0)
return i;
}
}
return 0;
}
static bool has_arg(char const *const *const query, int const argc, char **const argv)
{
return find_arg(query, 1, argc, argv) > 0;
}
static const char *logger_options[] = { "--xml-log-fd", "--xml-log", "-z" };
static XMLLogger const *make_logger(int *argc, char *argv[])
{
XMLLogger const *rslt = NULL;
char *argf[4];
int i;
argf[0] = argv[0];
argf[1] = NULL;
argf[2] = NULL;
argf[3] = NULL;
for (i = 1; i < *argc; ++i) {
int remove = 0;
if (strcmp(argv[i], logger_options[2]) == 0) {
argf[1] = argv[i];
argf[2] = argv[i + 1];
remove = 2;
}
else {
int j;
for (j = 0; j < 2; ++j) {
if (strstr(argv[i], logger_options[j]) == argv[i]) {
int const n = strlen(logger_options[j]);
if (argv[i][n] == '\0') {
argf[1] = argv[i];
argf[2] = argv[i + 1];
remove = 2;
}
else if (argv[i][n] == '=') {
argv[i][n] = '\0';
argf[1] = argv[i];
argf[2] = argv[i] + n + 1;
remove = 1;
}
break;
}
}
}
if (argf[1] != NULL) {
Args *args = NULL;
ArgsMakeAndHandle(&args, 3, argf, 1, XMLLogger_Args, XMLLogger_ArgsQty);
if (args) {
XMLLogger_Make(&rslt, NULL, args);
ArgsWhack(args);
}
}
if (remove) {
*argc -= remove;
memmove(argv + i, argv + i + remove, (*argc + 1) * sizeof(argv[0]));
break;
}
}
return rslt;
}
rc_t CC KMain(int argc, char *argv[])
{
static const char *help[] = { "--help", "-h", "-?", NULL };
static const char *vers[] = { "--version", "-V", NULL };
bool const has_help = has_arg(help, argc, argv);
bool const has_vers = has_arg(vers, argc, argv);
XMLLogger const *logger = NULL;
int argfirst = 0;
int arglast = 0;
rc_t rc = 0;
unsigned load = 0;
if (has_help) {
argc = 2;
argv[1] = "--help";
return main_help_vers(argc, argv);
}
if (has_vers) {
argc = 2;
argv[1] = "--version";
return main_help_vers(argc, argv);
}
logger = make_logger(&argc, argv);
memset(&G, 0, sizeof(G));
G.mode = mode_Archive;
G.globalMode = mode_Archive;
G.maxSeqLen = TableWriterRefSeq_MAX_SEQ_LEN;
G.schemaPath = strdup(SCHEMAFILE);
G.omit_aligned_reads = true;
G.omit_reference_reads = true;
G.minMapQual = 0; /* accept all */
G.tmpfs = strdup("/tmp");
G.cache_size = ((size_t)16) << 30;
G.maxErrCount = 1000;
G.minMatchCount = 10;
set_pid();
for (arglast = 1; arglast < argc; ++arglast) {
if (strcmp(argv[arglast], "--remap") == 0) {
argv[arglast] = argv[0];
G.globalMode = mode_Remap;
rc = main_1(arglast - argfirst, argv + argfirst, true, load);
if (rc)
break;
G.mode = mode_Remap;
argfirst = arglast;
++load;
}
}
rc = main_1(arglast - argfirst, argv + argfirst, false, load);
XMLLogger_Release(logger);
cleanupGlobal();
return rc;
}
|