-
Notifications
You must be signed in to change notification settings - Fork 37
/
peek.cpp
433 lines (387 loc) · 14.6 KB
/
peek.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
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
/* The MIT License
Copyright (c) 2013 Adrian Tan <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "peek.h"
namespace
{
KHASH_MAP_INIT_INT(32, char)
class Igor : Program
{
public:
///////////
//options//
///////////
std::string input_vcf_file;
std::string output_vcf_file;
std::vector<GenomeInterval> intervals;
std::string ref_fasta_file;
///////
//i/o//
///////
BCFOrderedReader *odr;
bcf1_t *v;
//////////
//filter//
//////////
std::string fexp;
Filter filter;
bool filter_exists;
/////////
//stats//
/////////
uint32_t no_samples;
uint32_t no_chromosomes;
uint32_t no_observed_variants;
uint32_t no_classified_variants;
uint32_t no_ref;
uint32_t no_snp2;
uint32_t no_snp2_ts;
uint32_t no_snp2_tv;
uint32_t no_snp3;
uint32_t no_snp4;
uint32_t no_mnp2;
uint32_t no_mnp2_ts;
uint32_t no_mnp2_tv;
uint32_t no_mnp_multi;
uint32_t no_indel2;
uint32_t no_ins2;
uint32_t no_del2;
uint32_t no_indel_multi;
uint32_t no_snpindel2;
uint32_t no_snpins2;
uint32_t no_snpdel2;
uint32_t no_snpindel_multi;
uint32_t no_mnpindel2;
uint32_t no_mnpins2;
uint32_t no_mnpdel2;
uint32_t no_mnpindel_multi;
uint32_t no_snp_mnp_indel;
uint32_t no_snp_mnp;
uint32_t no_clumped2;
uint32_t no_clumped_multi;
/////////
//tools//
/////////
VariantManip *vm;
Igor(int argc, char **argv)
{
version = "0.5";
//////////////////////////
//options initialization//
//////////////////////////
try
{
std::string desc = "Summarizes the variants in a VCF file";
TCLAP::CmdLine cmd(desc, ' ', version);
VTOutput my; cmd.setOutput(&my);
TCLAP::ValueArg<std::string> arg_ref_fasta_file("r", "r", "reference sequence fasta file []", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_intervals("i", "i", "intervals []", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_interval_list("I", "I", "file containing list of intervals []", false, "", "file", cmd);
TCLAP::ValueArg<std::string> arg_fexp("f", "f", "filter expression []", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_output_vcf_file("o", "o", "output VCF file [-]", false, "-", "str", cmd);
TCLAP::UnlabeledValueArg<std::string> arg_input_vcf_file("<in.vcf>", "input VCF file", true, "","file", cmd);
cmd.parse(argc, argv);
input_vcf_file = arg_input_vcf_file.getValue();
output_vcf_file = arg_output_vcf_file.getValue();
fexp = arg_fexp.getValue();
parse_intervals(intervals, arg_interval_list.getValue(), arg_intervals.getValue());
ref_fasta_file = arg_ref_fasta_file.getValue();
}
catch (TCLAP::ArgException &e)
{
std::cerr << "error: " << e.error() << " for arg " << e.argId() << "\n";
abort();
}
};
void initialize()
{
//////////////////////
//i/o initialization//
//////////////////////
odr = new BCFOrderedReader(input_vcf_file, intervals);
v = bcf_init1();
/////////////////////////
//filter initialization//
/////////////////////////
filter.parse(fexp.c_str());
filter_exists = fexp=="" ? false : true;
////////////////////////
//stats initialization//
////////////////////////
no_samples = 0;
no_chromosomes = 0;
no_observed_variants = 0;
no_classified_variants = 0;
no_ref = 0;
no_snp2 = 0;
no_snp2_ts = 0;
no_snp2_tv = 0;
no_snp3 = 0;
no_snp4 = 0;
no_mnp2 = 0;
no_mnp2_ts = 0;
no_mnp2_tv = 0;
no_mnp_multi = 0;
no_indel2 = 0;
no_ins2 = 0;
no_del2 = 0;
no_indel_multi = 0;
no_snpindel2 = 0;
no_snpins2 = 0;
no_snpdel2 = 0;
no_snpindel_multi = 0;
no_mnpindel2 = 0;
no_mnpins2 = 0;
no_mnpdel2 = 0;
no_mnpindel_multi = 0;
no_snp_mnp_indel = 0;
no_snp_mnp = 0;
no_clumped2 = 0;
no_clumped_multi = 0;
no_clumped2 = 0;
no_clumped_multi = 0;
////////////////////////
//tools initialization//
////////////////////////
vm = new VariantManip(ref_fasta_file);
}
void peek()
{
no_samples = bcf_hdr_get_n_sample(odr->hdr);
int ret, is_missing;
khiter_t k;
khash_t(32) *h = kh_init(32);
Variant variant;
while (odr->read(v))
{
int32_t vtype = vm->classify_variant(odr->hdr, v, variant);
if (filter_exists)
{
if (!filter.apply(odr->hdr, v, &variant))
{
continue;
}
}
if ((k = kh_get(32, h, bcf_get_rid(v))) == kh_end(h))
{
kh_put(32, h, bcf_get_rid(v), &ret);
kh_value(h, k) = 1; //not really necessary.
++no_chromosomes;
}
if (vtype & VT_CLUMPED)
{
if (bcf_get_n_allele(v)==2)
{
++no_clumped2;
}
else if (bcf_get_n_allele(v)>2)
{
++no_clumped_multi;
}
++no_classified_variants;
}
else if (vtype==VT_SNP)
{
if (bcf_get_n_allele(v)==2)
{
++no_snp2;
no_snp2_ts += variant.alleles[0].ts;
no_snp2_tv += variant.alleles[0].tv;
}
else if (bcf_get_n_allele(v)==3)
{
++no_snp3;
}
else if (bcf_get_n_allele(v)==4)
{
++no_snp4;
}
++no_classified_variants;
}
else if (vtype==VT_MNP)
{
if (bcf_get_n_allele(v)==2)
{
++no_mnp2;
no_mnp2_ts += variant.alleles[0].ts;
no_mnp2_tv += variant.alleles[0].tv;
}
else if (bcf_get_n_allele(v)>2)
{
++no_mnp_multi;
}
++no_classified_variants;
}
else if (vtype==VT_INDEL) //strictly simple indels
{
if (bcf_get_n_allele(v)==2)
{
++no_indel2;
if (variant.alleles[0].ins)
{
++no_ins2;
}
else
{
++no_del2;
}
}
else if (bcf_get_n_allele(v)>2)
{
++no_indel_multi;
}
++no_classified_variants;
}
else if (vtype==(VT_SNP|VT_INDEL))
{
if (bcf_get_n_allele(v)==2)
{
++no_snpindel2;
if (variant.alleles[0].ins)
{
++no_snpins2;
}
else
{
++no_snpdel2;
}
}
else if (bcf_get_n_allele(v)>2)
{
++no_snpindel_multi;
}
++no_classified_variants;
}
else if (vtype==(VT_MNP|VT_INDEL))
{
if (bcf_get_n_allele(v)==2)
{
++no_mnpindel2;
if (variant.alleles[0].ins)
{
++no_mnpins2;
}
else
{
++no_mnpdel2;
}
}
else if (bcf_get_n_allele(v)>2)
{
++no_mnpindel_multi;
}
++no_classified_variants;
}
else if (vtype==(VT_SNP|VT_MNP|VT_INDEL))
{
++no_snp_mnp_indel;
++no_classified_variants;
}
else if (vtype==(VT_SNP|VT_MNP))
{
++no_snp_mnp;
++no_classified_variants;
}
else if (vtype==VT_REF) //MNPs that are not real MNPs
{
++no_ref;
++no_classified_variants;
}
else
{
std::cerr << "UNCLASSIFIED ";
bcf_print_lite(odr->hdr, v);
std::cerr << " - ";
std::cerr << vm->vtype2string(vtype) << "\n";
}
++no_observed_variants;
}
kh_destroy(32, h);
odr->close();
};
void print_options()
{
std::clog << "peek v" << version << "\n\n";
std::clog << "options: input VCF file " << input_vcf_file << "\n";
std::clog << " [o] output VCF file " << output_vcf_file << "\n";
print_str_op(" [f] filter ", fexp);
print_ref_op(" [r] reference FASTA file ", ref_fasta_file);
print_int_op(" [i] intervals ", intervals);
std::clog << "\n";
}
void print_stats()
{
fprintf(stderr, "\n");
fprintf(stderr, "stats: no. of samples : %10d\n", no_samples);
fprintf(stderr, " no. of chromosomes : %10d\n", no_chromosomes);
fprintf(stderr, "\n");
fprintf(stderr, " no. of SNPs : %10d\n", no_snp2+no_snp3+no_snp4);
fprintf(stderr, " biallelic (ts/tv) : %15d (%.2f) [%d/%d]\n", no_snp2, no_snp2_tv ? (float)no_snp2_ts/no_snp2_tv : NAN, no_snp2_ts, no_snp2_tv);
fprintf(stderr, " 3 alleles : %15d\n", no_snp3);
fprintf(stderr, " 4 alleles : %15d\n", no_snp4);
fprintf(stderr, "\n");
fprintf(stderr, " no. of MNPs : %10d\n", no_mnp2+no_mnp_multi);
fprintf(stderr, " biallelic (ts/tv) : %15d (%.2f) [%d/%d]\n", no_mnp2, no_mnp2_tv ? (float)no_mnp2_ts/no_mnp2_tv : NAN, no_mnp2_ts, no_mnp2_tv);
fprintf(stderr, " multiallelic : %15d\n", no_mnp_multi);
fprintf(stderr, "\n");
fprintf(stderr, " no. Indels : %10d\n", no_indel2+no_indel_multi);
fprintf(stderr, " biallelic (ins/del) : %15d (%.2f)\n", no_indel2, no_del2 ? (float)no_ins2/no_del2 : NAN);
fprintf(stderr, " insertions : %15d\n", no_ins2);
fprintf(stderr, " deletions : %15d\n", no_del2);
fprintf(stderr, " multiallelic : %15d\n", no_indel_multi);
fprintf(stderr, "\n");
fprintf(stderr, " no. SNP/Indels : %10d\n", no_snpindel2+no_snpindel_multi);
fprintf(stderr, " biallelic (ins/del) : %15d (%.2f)\n", no_snpindel2, no_snpdel2 ? (float)no_snpins2/no_snpdel2 : NAN);
fprintf(stderr, " insertions : %15d\n", no_snpins2);
fprintf(stderr, " deletions : %15d\n", no_snpdel2);
fprintf(stderr, " multiallelic : %15d\n", no_snpindel_multi);
fprintf(stderr, "\n");
fprintf(stderr, " no. MNP/Indels : %10d\n", no_mnpindel2+no_mnpindel_multi);
fprintf(stderr, " biallelic (ins/del) : %15d (%.2f)\n", no_mnpindel2, no_mnpdel2 ? (float)no_mnpins2/no_mnpdel2 : NAN);
fprintf(stderr, " insertions : %15d\n", no_mnpins2);
fprintf(stderr, " deletions : %15d\n", no_mnpdel2);
fprintf(stderr, " multiallelic : %15d\n", no_mnpindel_multi);
fprintf(stderr, "\n");
fprintf(stderr, " no. SNP/MNP/Indels : %10d\n", no_snp_mnp_indel);
fprintf(stderr, " (multiallelic)\n");
fprintf(stderr, "\n");
fprintf(stderr, " no. SNP/MNP : %10d\n", no_snp_mnp);
fprintf(stderr, " (multiallelic)\n");
fprintf(stderr, "\n");
fprintf(stderr, " no. of clumped variants : %10d\n", no_clumped2+no_clumped_multi);
fprintf(stderr, " biallelic : %15d\n", no_clumped2);
fprintf(stderr, " multiallelic : %15d\n", no_clumped_multi);
fprintf(stderr, "\n");
fprintf(stderr, " no. of reference : %10d\n", no_ref);
fprintf(stderr, "\n");
fprintf(stderr, " no. of observed variants : %10d\n", no_observed_variants);
fprintf(stderr, " no. of unclassified variants : %10d\n", no_observed_variants-no_classified_variants);
fprintf(stderr, "\n");
};
~Igor() {};
private:
};
}
void peek(int argc, char ** argv)
{
Igor igor(argc, argv);
igor.print_options();
igor.initialize();
igor.peek();
igor.print_stats();
};