-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool_temp.cpp
227 lines (186 loc) · 5.84 KB
/
tool_temp.cpp
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
// ******************************************************
// vcfCTools (c) 2011 Alistair Ward
// Marth Lab, Department of Biology, Boston College
// All rights reserved.
// ------------------------------------------------------
// Last modified: 1 March 2011
// ------------------------------------------------------
// Filter the vcf file on user specific criteria. vcf
// records that fail the filters have the filter field
// populated with a semi-colon seperated list of failed
// filters.
// ******************************************************
#include <cmath>
#include "tool_temp.h"
using namespace std;
using namespace vcfCTools;
// intersectTool imlementation.
tempTool::tempTool(void)
: AbstractTool()
{}
// Destructor.
tempTool::~tempTool(void) {}
// Help
int tempTool::Help(void) {
cout << "Template help" << endl;
cout << "Usage: ./vcfCTools temp [options]." << endl;
cout << endl;
cout << "Options:" << endl;
cout << " -h, --help" << endl;
cout << " display intersect help." << endl;
cout << " -i, --in" << endl;
cout << " input vcf files (two, or one if intersecting with bed file)." << endl;
cout << " -o, --out" << endl;
cout << " output vcf file." << endl;
cout << " -q, --quality" << endl;
cout << " filter on variant quality." << endl;
cout << " -r, --remove-genotypes" << endl;
cout << " do not include genotypes in the output vcf file." << endl;
cout << endl;
return 0;
}
// Parse the command line and get all required and optional arguments.
int tempTool::parseCommandLine(int argc, char* argv[]) {
commandLine = argv[0];
for (int i = 2; i < argc; i++) {
commandLine += " ";
commandLine += argv[i];
}
int argument; // Counter for getopt.
// Define the long options.
while (true) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"in", required_argument, 0, 'i'},
{"out", required_argument, 0, 'o'},
{"mark-as-pass", no_argument, 0, 'm'},
{"quality", required_argument, 0, 'q'},
{"remove-genotypes", required_argument, 0, 'r'},
{0, 0, 0, 0}
};
int option_index = 0;
argument = getopt_long(argc, argv, "hi:o:q:mr", long_options, &option_index);
if (argument == -1) {break;}
switch (argument) {
// Input vcf file - required input.
case 'i':
vcfFile = optarg;
break;
// Output vcf file.
case 'o':
outputFile = optarg;
break;
// Filter on SNP quality.
case 'q':
filterQuality = true;
char* value;
value = optarg;
filterQualityValue = atof(value);
break;
// Mark all records as passed filters.
case 'm':
markPass = true;
break;
// Remove genotypes from the output file.
case 'r':
removeGenotypes = true;
break;
// Help.
case 'h':
return Help();
//
case '?':
cout << "Unknown option: " << argv[optind - 1] << endl;
exit(1);
// default
default:
abort ();
}
}
// Remaining arguments are unknown, so terminate with an error.
if (optind < argc - 1) {
cerr << "Unknown options." << endl;
exit(1);
}
// Check that a vcf file was specified.
if (vcfFile == "") {
cerr << "A vcf file must be specified (--in, -i)." << endl;
exit(1);
}
return 0;
}
// Run the tool.
int tempTool::Run(int argc, char* argv[]) {
markPass = false;
filterQuality = false;
removeGenotypes = false;
int getOptions = tempTool::parseCommandLine(argc, argv);
output = openOutputFile(outputFile);
vcf v; // Define vcf object.
// Open the vcf file.
v.openVcf(vcfFile);
// Read in the header information.
v.parseHeader();
string taskDescription = "##vcfCTools=filter";
if (markPass) {taskDescription += "marked all records as PASS";}
writeHeader(output, v, removeGenotypes, taskDescription);
v.processInfo = true;
//Parse the vcf file and check if any of the filters are failed. If
// so, build up a string of failed filters.
while (v.getRecord()) {
string filterString = "";
// Mark the record as "PASS" if --mark-as-pass was applied.
if (markPass) {filterString = "PASS";}
// Check for quality filtering.
if (filterQuality && !markPass) {
if (v.quality < filterQualityValue) {
ostringstream s;
s << filterQualityValue;
filterString = (filterString != "") ? filterString + ";" + "Q" + s.str() : "Q" + s.str();
}
}
filterString = (filterString == "") ? v.filters : filterString;
v.filters = filterString;
//string tag = "SA";
//information sInfo = v.getInfo(tag);
//unsigned int ABA = atoi(sInfo.values[0].c_str());
//tag = "ABR";
//sInfo = v.getInfo(tag);
//unsigned int ABR = atoi(sInfo.values[0].c_str());
//double AB = double(ABR) / ( double(ABR) + double(ABA) );
//long double phred;
//if (ABR == 0 && ABA == 0) {
// AB = 1.0;
// phred = 1000;
//}
//else {
// double successes = double(ABA);
// double trials = double(ABA) + double(ABR);
// double prob = 0.5;
// long double ABP = log(0.5) + (-2 * pow(trials * prob - successes, 2) / trials);
// phred = -10 * M_LOG10E * ABP;
//}
//ostringstream build;
size_t found = v.info.find(";SR=");
if (found != string::npos) {
size_t end = v.info.find_first_of(";",found + 1);
//build << AB;
string newValue = "";
v.info.replace(found, end - found - 1, newValue);
//build.str("");
}
found = v.info.find(";SA=");
if (found != string::npos) {
size_t end = v.info.find_first_of(";",found + 1);
//build << phred;
string newValue = "";
v.info.replace(found, end - found - 1, newValue);
//build.str("");
}
string record = v.buildRecord(removeGenotypes);
*output << record << endl;
}
// Close the vcf files.
v.closeVcf();
return 0;
}