-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool_intersect.cpp
396 lines (332 loc) · 12.7 KB
/
tool_intersect.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
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
// ******************************************************
// vcfCTools (c) 2011 Alistair Ward
// Marth Lab, Department of Biology, Boston College
// All rights reserved.
// ------------------------------------------------------
// Last modified: 18 February 2011
// ------------------------------------------------------
// Calculate the intersection of two vcf files or a vcf
// file and a bed file.
// ******************************************************
#include "tool_intersect.h"
using namespace std;
using namespace vcfCTools;
// intersectTool imlementation.
intersectTool::intersectTool(void)
: AbstractTool()
{
distanceDistribution = false;
allowMismatch = false;
passFilters = false;
findCommon = false;
findUnion = false;
findUnique = false;
sitesOnly = false;
processComplex = false;
processIndels = false;
processMnps = false;
processRearrangements = false;
processSnps = false;
processSvs = false;
whollyWithin = false;
currentReferenceSequence = "";
}
// Destructor.
intersectTool::~intersectTool(void) {}
// Help
int intersectTool::Help(void) {
cout << "Intersect help" << endl;
cout << "Usage: ./vcfCTools intersect [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 << " -b, --bed" << endl;
cout << " input bed file." << endl;
cout << " -o, --output" << endl;
cout << " output vcf file." << endl;
cout << " -d, --distance" << endl;
cout << " calculate the distribution of distances of each variant from the nearest bed interval." << endl;
cout << " -c, --common" << endl;
cout << " output variants present in both files." << endl;
cout << " -m, --mismatch" << endl;
cout << " variants of the same type at the same locus are considered a match." << endl;
cout << " -q, --unique" << endl;
cout << " output variants unique to one of the files." << endl;
cout << " -u, --union" << endl;
cout << " output variants present in either file." << endl;
cout << " -s, --sites-only" << endl;
cout << " only compare files based on sites. Do not evaluate the alleles." << endl;
cout << " -p, --pass-filters" << endl;
cout << " Only variants that pass filters are considered." << endl;
cout << " -w, --wholly-within-interval" << endl;
cout << " For bed-intersections, start and end of ref/variant allele must fall within interval." << endl;
cout << " -1, --snps" << endl;
cout << " analyse SNPs." << endl;
cout << " -2, --mnps" << endl;
cout << " analyse MNPs." << endl;
cout << " -3, --indels" << endl;
cout << " analyse indels." << endl;
cout << " -4, --complex" << endl;
cout << " analyse complex events." << endl;
cout << " -5, --structural-variants" << endl;
cout << " analyse structural variantion events." << endl;
cout << " -6, --rearrangements" << endl;
cout << " analyse complex rearrangement events." << endl;
cout << endl;
cout << "Additional information:" << endl;
cout << " The -c, -u and -q options require either 'a', 'b' or 'q' (not valid for -q) as an argument." << endl;
cout << endl;
cout << " a: Write out records from the first file." << endl;
cout << " b: Write out records from the second file." << endl;
cout << " q: Write out records with the highest variant quality." << endl;
cout << endl;
exit(0);
return 0;
}
// Parse the command line and get all required and optional arguments.
int intersectTool::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.
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"bed", required_argument, 0, 'b'},
{"common", required_argument, 0, 'c'},
{"in", required_argument, 0, 'i'},
{"out", required_argument, 0, 'o'},
{"distance", no_argument, 0, 'd'},
{"mismatch", no_argument, 0, 'm'},
{"pass-filters", no_argument, 0, 'p'},
{"unique", required_argument, 0, 'q'},
{"union", required_argument, 0, 'u'},
{"sites-only", no_argument, 0, 's'},
{"wholly-within-interval", no_argument, 0, 'w'},
{"snps", no_argument, 0, '1'},
{"mnps", no_argument, 0, '2'},
{"indels", no_argument, 0, '3'},
{"complex", no_argument, 0, '4'},
{"structural-variants", no_argument, 0, '5'},
{"rearrangements", no_argument, 0, '6'},
{0, 0, 0, 0}
};
while (true) {
int option_index = 0;
argument = getopt_long(argc, argv, "hb:i:o:dmpc:u:q:sw123456", long_options, &option_index);
if (argument == -1) {break;}
switch (argument) {
// Input bed file.
case 'b':
bedFile = optarg;
break;
// Determine if the distribution of the variant distance from the
// nearest bed interval is required.
case 'd':
distanceDistribution = true;
break;
// Input vcf file - required input.
case 'i':
vcfFiles.push_back(optarg);
break;
// Help.
case 'h':
return Help();
// Output file.
case 'o':
outputFile = optarg;
break;
// Common variants.
case 'c':
findCommon = true;
writeFrom = optarg;
break;
// Find the union.
case 'u':
findUnion = true;
writeFrom = optarg;
break;
// Unique variants.
case 'q':
findUnique = true;
writeFrom = optarg;
break;
// Determine if comparing for exact matches or not.
case 'm':
allowMismatch = true;
break;
// Only compare variants based on the position. Do not
// interrogate the alleles.
case 's':
sitesOnly = true;
break;
// Only consider variants if they pass filters.
case 'p':
passFilters = true;
break;
// An allele must fall wholly within bed interval.
case 'w':
whollyWithin = true;
break;
// Analyse SNPs.
case '1':
processSnps = true;
break;
// Analyse MNPs.
case '2':
processMnps = true;
break;
// Analyse indels.
case '3':
processIndels = true;
break;
// Analyse complex events.
case '4':
processComplex = true;
break;
// Analyse structural variants.
case '5':
processSvs = true;
break;
// Analyse complex rearrangements.
case '6':
processRearrangements = true;
break;
//
case '?':
cerr << "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 either two vcf files or one vcf and one bed file is specified.
if (vcfFiles.size() == 0 || (vcfFiles.size() == 1 and bedFile == "") || (bedFile != "" && vcfFiles.size() != 1) || vcfFiles.size() > 2) {
cerr << "Two vcf files or a vcf and a bed file must be specified (--in, -i, --bed, -b)." << endl;
exit(1);
}
// Check whether finding common, union or unique variants. If more than one of
// these options are selected, terminate the program.
if ( (findCommon + findUnion + findUnique) > 1) {
cerr << "Only one operation (-c [--common], -u [--union] or -q [--unique]) can be performed at once." << endl;
exit(1);
} else if (!findCommon && !findUnion && !findUnique) {
cerr << "One operation (-c [--common], -u [--union] or -q [--unique]) must be selected." << endl;
exit(1);
} else {
if (writeFrom == "a") {cerr << "Writing out records from file: " << vcfFiles[0] << endl;}
else if (writeFrom == "b") {cerr << "Writing out records from file: " << vcfFiles[1] << endl;}
else {
cerr << "The file from which the records are to be written needs to be selected." << endl;
cerr << " a - write records from the first inputted file." << endl;
cerr << " b - write records from the second inputted file." << endl;
exit(1);
}
}
return 0;
}
// Run the tool.
int intersectTool::Run(int argc, char* argv[]) {
int getOptions = intersectTool::parseCommandLine(argc, argv);
// Define an output object and open the output file.
output ofile;
ofile.outputStream = ofile.openOutputFile(outputFile);
intersect ints; // Define an intersection object.
ints.setBooleanFlags(findCommon, findUnion, findUnique, sitesOnly, false, whollyWithin); // Set the flags required for performing intersections.
if (writeFrom == "a") {ints.flags.writeFromFirst = true;}
else if (writeFrom == "b") {ints.flags.writeFromFirst = false;}
// If intersection is between a vcf file and a bed file, create a vcf and a bed object
// and intersect.
if (bedFile != "") {
// Create a vcf object.
vcf v;
v.openVcf(vcfFiles[0]);
// Create a variant object.
variant var;
var.determineVariantsToProcess(processSnps, processMnps, processIndels, processComplex, processSvs, processRearrangements, false, true, false);
bed b; // Create a bed object.
bedStructure bs; // Create a bed structure.
b.openBed(bedFile);
// Define the header object and parse the headers.
vcfHeader header;
header.parseHeader(v.input);
b.parseHeader(bedFile);
// Write the header to the output file.
string taskDescription = "##vcfCtools=intersect " + vcfFiles[0] + ", " + bedFile;
header.writeHeader(ofile.outputStream, false, taskDescription);
// Intersect the files.
ints.intersectVcfBed(header, v, var, b, bs, ofile);
// Check that the input files had the same list of reference sequences.
// If not, it is possible that there were some problems.
//checkReferenceSequences(v.referenceSequenceVector, b.referenceSequenceVector); // tools.cpp
// Close the vcf file and return.
v.closeVcf();
// Output some brief statistics on the targets.
cerr << "Number: " << b.numberTargets << endl;
cerr << "Total target length: " << b.targetLength << endl;
cerr << "Average target length: " << b.targetLength / b.numberTargets << endl;
cerr << endl;
// Print out the distribution of distances to targets.
// cerr << "Histogram describing the distribution of variant distances from targets:" << endl;
// cerr << endl;
//
// map<string, map<int, unsigned int> >::iterator rIter = ints.distanceDist.begin();
// map<int, unsigned int>::iterator dIter;
// for (; rIter != ints.distanceDist.end(); rIter++) {
// cerr << "Reference sequence: " << rIter->first << endl;
// for (dIter = rIter->second.begin(); dIter != rIter->second.end(); dIter++) {
// cerr << dIter->first << " " << dIter->second << endl;
// }
// }
// Close the bed object.
b.closeBed();
// Intersection of two vcf files.
} else {
// Create a vcf object.
vcf v1;
v1.openVcf(vcfFiles[0]);
// Create a variant object.
variant var1;
var1.determineVariantsToProcess(processSnps, processMnps, processIndels, processComplex, processSvs, processRearrangements, false, true, true);
// Create a second vcf object.
vcf v2;
v2.openVcf(vcfFiles[1]);
// Create a second variant object.
variant var2;
var2.determineVariantsToProcess(processSnps, processMnps, processIndels, processComplex, processSvs, processRearrangements, false, true, true);
// Define the header objects and parse the header information.
vcfHeader header1;
header1.parseHeader(v1.input);
vcfHeader header2;
header2.parseHeader(v2.input);
// Check that the header for the two files contain the same samples.
//if (v1.samples != v2.samples) {
// cerr << "vcf files contain different samples (or sample order)." << endl;
// exit(1);
//} else {
string taskDescription = "##vcfCTools=intersect " + vcfFiles[0] + ", " + vcfFiles[1];
if (ints.flags.writeFromFirst) {header1.writeHeader(ofile.outputStream, false, taskDescription);}
else {header2.writeHeader(ofile.outputStream, false, taskDescription);}
//}
// Intersect the two vcf files.
ints.intersectVcf(header1, header2, v1, var1, v2, var2, ofile);
// Check that the input files had the same list of reference sequences.
// If not, it is possible that there were some problems.
ints.checkReferenceSequences(var1, var2);
// Close the vcf files.
v1.closeVcf();
v2.closeVcf();
}
return 0;
}