1 #include "Variant.h"
2 #include "split.h"
3 #include "Fasta.h"
4 #include "gpatInfo.hpp"
5 #include <getopt.h>
6
7 using namespace std;
8 using namespace vcflib;
9
printSummary(char ** argv)10 void printSummary(char** argv) {
11 cerr << "usage: " << argv[0] << " [options] <vcf file>" << endl
12 << endl
13 << "options:" << endl
14 << " -f, --fasta-reference FASTA reference file to use to obtain primer sequences" << endl
15 << " -x, --exclude-failures If a record fails, don't print it. Otherwise do." << endl
16 << " -k, --keep-failures Print if the record fails, otherwise not." << endl
17 << " -h, --help Print this message." << endl
18 << " -v, --version Print version." << endl
19 << endl
20 << "Verifies that the VCF REF field matches the reference as described." << endl
21 << endl;
22 exit(0);
23 }
24
25
main(int argc,char ** argv)26 int main(int argc, char** argv) {
27
28 int c;
29 string fastaRef;
30 bool keepFailures = false;
31 bool excludeFailures = false;
32
33 if (argc == 1)
34 printSummary(argv);
35
36 while (true) {
37 static struct option long_options[] =
38 {
39 /* These options set a flag. */
40 //{"verbose", no_argument, &verbose_flag, 1},
41 {"help", no_argument, 0, 'h'},
42 {"fasta-reference", required_argument, 0, 'f'},
43 {"exclude-failures", no_argument, 0, 'x'},
44 {"keep-failures", no_argument, 0, 'k'},
45 {"version", no_argument, 0, 'v'},
46 //{"length", no_argument, &printLength, true},
47 {0, 0, 0, 0}
48 };
49 /* getopt_long stores the option index here. */
50 int option_index = 0;
51
52 c = getopt_long (argc, argv, "hvxkf:",
53 long_options, &option_index);
54
55 /* Detect the end of the options. */
56 if (c == -1)
57 break;
58
59 switch (c)
60 {
61 case 0:
62 /* If this option set a flag, do nothing else now. */
63 if (long_options[option_index].flag != 0)
64 break;
65 printf ("option %s", long_options[option_index].name);
66 if (optarg)
67 printf (" with arg %s", optarg);
68 printf ("\n");
69 break;
70
71 case 'f':
72 {
73 fastaRef = optarg;
74 break;
75 }
76 case 'v':
77 {
78 printBasicVersion();
79 exit(0);
80 }
81 case 'x':
82 {
83 excludeFailures = true;
84 break;
85 }
86 case 'k':
87 {
88 keepFailures = true;
89 break;
90 }
91 case 'h':
92 {
93 printSummary(argv);
94 exit(0);
95 break;
96 }
97 case '?':
98 {
99 /* getopt_long already printed an error message. */
100 printSummary(argv);
101 exit(1);
102 break;
103 }
104 default:
105 abort ();
106 }
107 }
108
109 if (fastaRef.empty()) {
110 cerr << "a FASTA reference sequence must be specified" << endl;
111 exit(1);
112 }
113
114 FastaReference ref;
115 ref.open(fastaRef);
116
117 VariantCallFile variantFile;
118 string inputFilename;
119 if (optind == argc - 1) {
120 inputFilename = argv[optind];
121 variantFile.open(inputFilename);
122 } else {
123 variantFile.open(std::cin);
124 }
125
126 if (!variantFile.is_open()) {
127 return 1;
128 }
129
130 if (keepFailures || excludeFailures) {
131 cout << variantFile.header << endl;
132 }
133
134 Variant var(variantFile);
135 while (variantFile.getNextVariant(var)) {
136 int refstart = var.position - 1; // convert to 0-based
137 string matchedRef = ref.getSubSequence(var.sequenceName, refstart, var.ref.size());
138 if (var.ref != matchedRef) {
139 if (keepFailures) {
140 cout << var << endl;
141 } else if (!excludeFailures) {
142 cout << "mismatched reference " << var.ref << " should be " << matchedRef << " at "
143 << var.sequenceName << ":" << var.position << endl;
144 }
145 } else if (excludeFailures) {
146 cout << var << endl;
147 }
148 }
149
150 return 0;
151
152 }
153
154