-
Notifications
You must be signed in to change notification settings - Fork 7
/
vcf_v41.ragel
786 lines (635 loc) · 29.2 KB
/
vcf_v41.ragel
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
#include <cstdio>
#include "vcf_validator.hpp"
%%{
machine vcf;
#############################################
# Actions definition #
#############################################
############## General actions ##############
action token_begin {
ParsePolicy::handle_token_begin(*this);
}
action token_middle {
ParsePolicy::handle_token_char(*this, *p);
}
action token_end {
//std::cout << "m_current_token '" << ParsePolicy::current_token() << "'" << std::endl;
}
action line_break {
++n_lines;
n_columns = 1;
}
action tab_break {
++n_columns;
}
######### Incorrect section actions #########
action fileformat_section_error {
ErrorPolicy::handle_fileformat_section_error(*this);
fhold; fgoto meta_section_skip;
}
action meta_section_error {
ErrorPolicy::handle_meta_section_error(*this);
fhold; fgoto meta_section_skip;
}
action header_section_error {
ErrorPolicy::handle_header_section_error(*this);
fhold; fgoto body_section_skip;
}
action body_section_error {
ErrorPolicy::handle_body_section_error(*this);
fhold; fgoto body_section_skip;
}
########### Correct fields actions ##########
action fileformat_end {
// set_vcf_file_format(ts, p-ts, file);
}
action meta_entry_begin {
// printf("Reading meta entry\n");
// status->current_meta_entry = vcf_meta_entry_new();
}
action meta_entry_end {
// add_vcf_meta_entry(status->current_meta_entry, file);
}
action meta_id_end {
// set_vcf_meta_entry_name(ts, p-ts, status->current_meta_entry);
}
action meta_field_value_end {
/* if (*ts == '<') {
add_vcf_meta_entry_value(ts+1, p-ts-1, status->current_meta_entry);
} else if (*(p-1) == '>') {
add_vcf_meta_entry_value(ts, p-ts-1, status->current_meta_entry);
} else {
add_vcf_meta_entry_value(ts, p-ts, status->current_meta_entry);
}*/
}
action meta_alt_read {
//printf("Meta ALT in line %zu\n", n_lines);
}
action meta_filter_read {
//printf("Meta FILTER in line %zu\n", n_lines);
}
action meta_format_read {
//printf("Meta FORMAT in line %zu\n", n_lines);
}
action meta_info_read {
//printf("Meta INFO in line %zu\n", n_lines);
}
action meta_generic_read {
//printf("Meta in line %zu\n", n_lines);
}
action header_begin {
ts = p;
// printf("Reading header\n");
}
action sample_name_end {
// add_vcf_sample_name(ts, p-ts, file);
}
action record_begin {
// status->current_record = vcf_record_new();
// std::cout << "Reading record" << std::endl;
}
action record_end {
// std::cout << "Finished reading record" << std::endl;
/* // If batch is full, add to the list of batches and create a new, empty one
if (batch_size > 0 && status->current_batch->records->size == batch_size)
{
add_vcf_batch(status->current_batch, file);
LOG_DEBUG_F("Batch %d added - %zu records\t", batches, status->current_batch->records->size);
status->current_batch = vcf_batch_new(batch_size);
if (p+1) {
status->current_batch->text = p+1;
LOG_DEBUG_F("batch text = '%.*s'\n", 50, status->current_batch->text);
}
batches++;
}
// If not a blank line, add status->current record to status->current batch
add_record_to_vcf_batch(status->current_record, status->current_batch);
// If the record is a structural variant, add it to the set in the VCF file
add_structural_variant(status->current_record, file);
status->num_records++;
status->num_samples = 0;
*/
}
action chrom_end {
// set_vcf_record_chromosome(ts, p-ts, status->current_record);
}
action pos_end {
// char *field = strndup(ts, p-ts);
// set_vcf_record_position(atol(field), status->current_record);
// free(field);
}
action id_end {
// set_vcf_record_id(ts, p-ts, status->current_record);
}
action ref_end {
// set_vcf_record_reference(ts, p-ts, status->current_record);
}
action alt_end {
/* if (!strncmp("0", ts, 1)) {
set_vcf_record_alternate(".", 1, status->current_record);
} else {
set_vcf_record_alternate(ts, p-ts, status->current_record);
}*/
}
action alt_snv_end {
// set_vcf_record_type(VARIANT_SNV, status->current_record);
}
action alt_indel_end {
// set_vcf_record_type(VARIANT_INDEL, status->current_record);
}
action alt_sv_end {
// set_vcf_record_type(VARIANT_SV, status->current_record);
}
action qual_end {
/* float quality = -1.0f;
if (strncmp(".", ts, 1) != 0) {
char *field = strndup(ts, p-ts);
quality = atof(field);
free(field);
}
set_vcf_record_quality(quality, status->current_record);*/
}
action filter_end {
// set_vcf_record_filter(ts, p-ts, status->current_record);
}
action info_end {
// set_vcf_record_info(ts, p-ts, status->current_record);
}
action format_end {
// set_vcf_record_format(ts, p-ts, status->current_record);
}
action sample_end {
// add_vcf_record_sample(ts, p-ts, status->current_record);
}
########## Incorrect metadata and header actions ##########
# Fileformat line
action fileformat_error {
ErrorPolicy::handle_fileformat_section_error(*this,
"Fileformat is not a sequence of alphanumeric and/or punctuation characters");
fhold; fgoto meta_section_skip;
}
# ALT metadata
action meta_alt_err {
ErrorPolicy::handle_meta_section_error(*this, "Error in ALT metadata");
fhold; fgoto meta_section_skip;
}
action meta_alt_id_prefix_err {
ErrorPolicy::handle_meta_section_error(*this, "ALT metadata ID does not begin with DEL/INS/DUP/INV/CNV");
fhold; fgoto meta_section_skip;
}
action meta_alt_id_suffix_err {
ErrorPolicy::handle_meta_section_error(*this, "ALT metadata ID suffix (after ':') is not valid");
fhold; fgoto meta_section_skip;
}
# assembly metadata
action meta_assembly_err {
ErrorPolicy::handle_meta_section_error(*this, "Error in assembly metadata");
fhold; fgoto meta_section_skip;
}
# contig metadata
action meta_contig_err {
ErrorPolicy::handle_meta_section_error(*this, "Error in contig metadata");
fhold; fgoto meta_section_skip;
}
# FILTER metadata
action meta_filter_err {
ErrorPolicy::handle_meta_section_error(*this, "Error in FILTER metadata");
fhold; fgoto meta_section_skip;
}
# FORMAT metadata
action meta_format_err {
ErrorPolicy::handle_meta_section_error(*this, "Error in FORMAT metadata");
fhold; fgoto meta_section_skip;
}
action meta_format_number_err {
ErrorPolicy::handle_meta_section_error(*this, "FORMAT metadata Number is not a number, A, R, G or dot");
fhold; fgoto meta_section_skip;
}
action meta_format_type_err {
ErrorPolicy::handle_meta_section_error(*this, "FORMAT metadata Type is not a Integer, Float, Character or String");
fhold; fgoto meta_section_skip;
}
# INFO metadata
action meta_info_err {
ErrorPolicy::handle_meta_section_error(*this, "Error in INFO metadata");
fhold; fgoto meta_section_skip;
}
action meta_info_number_err {
ErrorPolicy::handle_meta_section_error(*this, "INFO metadata Number is not a number, A, R, G or dot");
fhold; fgoto meta_section_skip;
}
action meta_info_type_err {
ErrorPolicy::handle_meta_section_error(*this, "INFO metadata Type is not a Integer, Float, Flag, Character or String");
fhold; fgoto meta_section_skip;
}
action meta_info_source_err {
ErrorPolicy::handle_meta_section_error(*this, "INFO metadata Source string is not valid");
fhold; fgoto meta_section_skip;
}
action meta_info_version_err {
ErrorPolicy::handle_meta_section_error(*this, "INFO metadata Version string is not valid");
fhold; fgoto meta_section_skip;
}
# PEDIGREE metadata
action meta_pedigree_err {
ErrorPolicy::handle_meta_section_error(*this, "Error in PEDIGREE metadata");
fhold; fgoto meta_section_skip;
}
# pedigreeDB metadata
action meta_pedigreedb_err {
ErrorPolicy::handle_meta_section_error(*this, "Error in pedigreeDB metadata");
fhold; fgoto meta_section_skip;
}
# SAMPLE metadata
action meta_sample_err {
ErrorPolicy::handle_meta_section_error(*this, "Error in SAMPLE metadata");
fhold; fgoto meta_section_skip;
}
action meta_sample_genomes_err {
ErrorPolicy::handle_meta_section_error(*this, "SAMPLE metadata Genomes contains a character different from alphanumeric, dot, underscore and dash");
fhold; fgoto meta_section_skip;
}
action meta_sample_mixture_err {
ErrorPolicy::handle_meta_section_error(*this, "SAMPLE metadata Mixture contains a character different from alphanumeric, dot, underscore and dash");
fhold; fgoto meta_section_skip;
}
# Metadata generic errors (do not apply to a specific type)
action meta_id_err {
ErrorPolicy::handle_meta_section_error(*this, "Metadata ID contains a character different from alphanumeric, dot, underscore and dash");
fhold; fgoto meta_section_skip;
}
action meta_desc_err {
ErrorPolicy::handle_meta_section_error(*this, "Metadata description string is not valid");
fhold; fgoto meta_section_skip;
}
action meta_url_err {
ErrorPolicy::handle_meta_section_error(*this, "Metadata URL is not valid");
fhold; fgoto meta_section_skip;
}
# Header errors
action header_prefix_err {
ErrorPolicy::handle_header_section_error(*this, "The header line does not start with the mandatory columns: CHROM, POS, ID, REF, ALT, QUAL, FILTER and INFO");
fhold; fgoto body_section_skip;
}
########## Incorrect records actions ##########
# Chromosome
action chrom_error {
ErrorPolicy::handle_body_section_error(*this, "Chromosome is not a string without colons or whitespaces, optionally wrapped with angle brackets (<>)");
fhold; fgoto body_section_skip;
}
# Position
action pos_error {
ErrorPolicy::handle_body_section_error(*this, "Position is not a positive number");
fhold; fgoto body_section_skip;
}
# ID
action id_error {
ErrorPolicy::handle_body_section_error(*this, "ID is not a single dot or a list of strings without semicolons or whitespaces");
fhold; fgoto body_section_skip;
}
# Reference allele
action ref_error {
ErrorPolicy::handle_body_section_error(*this, "Reference is not a string of bases");
fhold; fgoto body_section_skip;
}
# Alternate alleles
action alt_error {
ErrorPolicy::handle_body_section_error(*this, "Alternate is not a single dot or a comma-separated list of bases");
fhold; fgoto body_section_skip;
}
# Quality
action qual_error {
ErrorPolicy::handle_body_section_error(*this, "Quality is not a single dot or a positive number");
fhold; fgoto body_section_skip;
}
# Filter
action filter_error {
ErrorPolicy::handle_body_section_error(*this, "Filter is not a single dot or a semicolon-separated list of strings");
fhold; fgoto body_section_skip;
}
# Info
action info_error {
ErrorPolicy::handle_body_section_error(*this, "Info is not a single dot or a semicolon-separated list of key-value pairs");
fhold; fgoto body_section_skip;
}
action info_key_error {
ErrorPolicy::handle_body_section_error(*this, "Info key is not a sequence of alphanumeric and/or punctuation characters");
fhold; fgoto body_section_skip;
}
action info_value_error {
ErrorPolicy::handle_body_section_error(*this, "Info field value is not a comma-separated list of valid strings (maybe it contains whitespaces?)");
fhold; fgoto body_section_skip;
}
action info_AA_error {
ErrorPolicy::handle_body_section_error(*this, "Info AA value is not a single dot or a string of bases");
fhold; fgoto body_section_skip;
}
action info_AC_error {
ErrorPolicy::handle_body_section_error(*this, "Info AC value is not a comma-separated list of numbers");
fhold; fgoto body_section_skip;
}
action info_AF_error {
ErrorPolicy::handle_body_section_error(*this, "Info AF value is not a comma-separated list of numbers");
fhold; fgoto body_section_skip;
}
action info_AN_error {
ErrorPolicy::handle_body_section_error(*this, "Info AN value is not an integer number");
fhold; fgoto body_section_skip;
}
action info_BQ_error {
ErrorPolicy::handle_body_section_error(*this, "Info BQ value is not a number");
fhold; fgoto body_section_skip;
}
action info_CIGAR_error {
ErrorPolicy::handle_body_section_error(*this, "Info CIGAR value is not an alphanumeric string");
fhold; fgoto body_section_skip;
}
action info_DB_error {
ErrorPolicy::handle_body_section_error(*this, "Info DB is not a flag (with 1/0/no value)");
fhold; fgoto body_section_skip;
}
action info_DP_error {
ErrorPolicy::handle_body_section_error(*this, "Info DP value is not an integer number");
fhold; fgoto body_section_skip;
}
action info_END_error {
ErrorPolicy::handle_body_section_error(*this, "Info END value is not an integer number");
fhold; fgoto body_section_skip;
}
action info_H2_error {
ErrorPolicy::handle_body_section_error(*this, "Info H2 is not a flag (with 1/0/no value)");
fhold; fgoto body_section_skip;
}
action info_H3_error {
ErrorPolicy::handle_body_section_error(*this, "Info H3 is not a flag (with 1/0/no value)");
fhold; fgoto body_section_skip;
}
action info_MQ_error {
ErrorPolicy::handle_body_section_error(*this, "Info MQ value is not a number");
fhold; fgoto body_section_skip;
}
action info_MQ0_error {
ErrorPolicy::handle_body_section_error(*this, "Info MQ0 value is not an integer number");
fhold; fgoto body_section_skip;
}
action info_NS_error {
ErrorPolicy::handle_body_section_error(*this, "Info NS value is not an integer number");
fhold; fgoto body_section_skip;
}
action info_SB_error {
ErrorPolicy::handle_body_section_error(*this, "Info SB value is not a number");
fhold; fgoto body_section_skip;
}
action info_SOMATIC_error {
ErrorPolicy::handle_body_section_error(*this, "Info SOMATIC is not a flag (with 1/0/no value)");
fhold; fgoto body_section_skip;
}
action info_VALIDATED_error {
ErrorPolicy::handle_body_section_error(*this, "Info VALIDATED is not a flag (with 1/0/no value)");
fhold; fgoto body_section_skip;
}
action info_1000G_error {
ErrorPolicy::handle_body_section_error(*this, "Info 1000G is not a flag (with 1/0/no value)");
fhold; fgoto body_section_skip;
}
# Format
action format_error {
ErrorPolicy::handle_body_section_error(*this, "Format is not a colon-separated list of alphanumeric strings");
fhold; fgoto body_section_skip;
}
action format_gt_error {
ErrorPolicy::handle_body_section_error(*this, "Format first field is not GT");
fhold; fgoto body_section_skip;
}
# Samples
action sample_error {
std::ostringstream message_stream;
message_stream << "Sample #" << (n_columns - 9) << " is not a valid string";
ErrorPolicy::handle_body_section_error(*this, message_stream.str());
fhold; fgoto body_section_skip;
}
action sample_gt_error {
std::ostringstream message_stream;
message_stream << "Sample #" << (n_columns - 9) << " does not start with a valid genotype";
ErrorPolicy::handle_body_section_error(*this, message_stream.str());
fhold; fgoto body_section_skip;
}
#################################################
# Machine definition #
#################################################
NL = "\n" >line_break;
CS = "\t" >tab_break;
# Integer and floating-point numbers
any_number = (digit)+ ("." (digit)+)? ;
int_number = (digit)+ ;
# Bases are case-insensitive
bases = ("A" | "C" | "G" | "T" | "N" | "a" | "c" | "t" | "g" | "n" )+ ;
# Identifiers may contain some symbols, but not only those
# TODO Could it accept more symbols? Comma won't be, for sure
identifier = (alnum | "." | "_" | "-" )+ - ("." | "_" | "-")+ ;
# URL inspired in http://stackoverflow.com/questions/8784903/failed-to-convert-url-parser-regular-expression-to-ragel
scheme = (alpha (any - [:/?#] - "\n")+ ) ;
authority = (alpha (any - [/?#] - "\n")* ) ;
path = (any - [?#] - "\n")* ;
query = (any - [#] - "\n")* ;
fragment = (any - "\n")* ;
url = scheme "://" authority path ("?" query)? ("#" fragment)?;
# File format must be a sequence of alphanumeric and/or punctuation characters like "VCFv4.1"
fileformat_name = (alnum | punct)+ $err(fileformat_error);
fileformat = "##fileformat=" fileformat_name >token_begin %fileformat_end;
# Meta-data
meta_key = (alnum | "_" )+ - ("ALT" | "FILTER" | "FORMAT" | "INFO" | "assembly" | "contig" | "SAMPLE" | "PEDIGREE" | "pedigreeDB");
meta_field_value= (print - "=")+ ;
meta_field_desc = (print - "\"")+ ;
meta_field = ( meta_key "=" meta_field_value ) >token_begin %meta_field_value_end ;
meta_alt = "ID=" (
("DEL" | "INS" | "DUP" | "INV" | "CNV") $err(meta_alt_id_prefix_err)
( ":" meta_field_value)? $err(meta_alt_id_suffix_err)
)
",Description=\"" meta_field_desc "\"" $err(meta_desc_err) ;
meta_assembly = url $err(meta_url_err) ;
meta_contig = "ID=" identifier $err(meta_id_err)
("," identifier "=" meta_field_value)* ;
meta_filter = "ID=" identifier $err(meta_id_err)
",Description=\"" meta_field_desc "\"" $err(meta_desc_err) ;
meta_format = "ID=" identifier $err(meta_id_err)
",Number=" ( (digit)+ | "A" | "R" | "G" | "." ) $err(meta_format_number_err)
",Type=" ( "Integer" | "Float" | "Character" | "String" ) $err(meta_format_type_err)
",Description=\"" meta_field_desc "\"" $err(meta_desc_err) ;
meta_info = "ID=" identifier $err(meta_id_err)
",Number=" ( (digit)+ | "A" | "R" | "G" | "." ) $err(meta_info_number_err)
",Type=" ( "Integer" | "Float" | "Flag" | "Character" | "String" ) $err(meta_info_type_err)
",Description=\"" meta_field_desc "\"" $err(meta_desc_err)
(",Source=\"" meta_field_desc "\"")? $err(meta_info_source_err)
(",Version=\"" meta_field_desc "\"")? $err(meta_info_version_err) ;
meta_pedigree = identifier $err(meta_id_err) "=" identifier $err(meta_id_err) ("," identifier $err(meta_id_err) "=" identifier $err(meta_id_err))* ;
meta_pedigreeDB = url $err(meta_url_err) ;
meta_sample = "ID=" identifier $err(meta_id_err)
(",Genomes=" identifier (";" identifier)* ) $err(meta_sample_genomes_err)
(",Mixture=" identifier (";" identifier)* ) $err(meta_sample_mixture_err)
(",Description=\"" meta_field_desc (";" meta_field_desc)* "\"" ) $err(meta_desc_err) ;
meta_entry = (
("##" meta_key >token_begin %meta_id_end "=<" meta_field ("," meta_field)* ">") %(meta_generic_read) |
("##ALT=<" meta_alt ">" ) %(meta_alt_read) $err(meta_alt_err) |
("##FILTER=<" meta_filter ">" ) %(meta_filter_read) $err(meta_filter_err) |
("##FORMAT=<" meta_format ">" ) %(meta_format_read) $err(meta_format_err) |
("##INFO=<" meta_info ">" ) %(meta_info_read) $err(meta_info_err) |
("##assembly=" meta_assembly ) $err(meta_assembly_err) |
("##contig=<" meta_contig ">" ) $err(meta_contig_err) |
("##SAMPLE=<" meta_sample ">" ) $err(meta_sample_err) |
("##PEDIGREE=<" meta_pedigree ">" ) $err(meta_pedigree_err) |
("##pedigreeDB=<" meta_pedigreeDB ">" ) $err(meta_pedigreedb_err) |
("##" meta_field >token_begin %meta_field_value_end) |
("##" meta_field_value >token_begin %meta_field_value_end)
)
>meta_entry_begin %meta_entry_end;
# Header between meta and records
sample_name = (print - ("\n" | "\t"))+ ;
header = "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO" $err(header_prefix_err)
("\tFORMAT" ("\t" sample_name >token_begin %sample_name_end)+ )?;
# Records
## A chromosome must be a string with no white-spaces or colons, and may be surronded by < > symbols (for contigs)
record_chr_basic= (alnum | punct - (":" | "<" | ">"))+ ;
record_chr_contg= "<" (alnum | punct - (":" | ">"))+ ">" ;
record_chrom = record_chr_basic | record_chr_contg;
record_pos = int_number ;
## ID must be a (list of) string with no white-spaces or semi-colons
record_id_value = (print - (space | ";"))+ ;
record_id = record_id_value (";" record_id_value)* | "." ;
record_ref = bases ;
## A basic ALT can be something like AGT (biallelic), A,AT (multiallelic) or dot (no variant, must be the only allele)
## Zero is just for compatibility with PlinkSeq transformation
record_alt_snv = (bases ("," bases)* )+ | "." |"0";
## Indel alternates can be represented by standardized prefixes or an asterisk
record_alt_indel= "<DEL>" | "<INS>" | "<DUP>" | "<INV>" | "<CNV>" | "<DUP:TANDEM>" | "<DEL:ME:" (alnum)+ ">" | "<INS:ME:" (alnum)+ ">" | "*";
## Structural variants follow forms like:
## ]1:1234]ATG or ]<contig_1>:1234]ATG : paired breakends
## .AGT, AGT.: single breakends
record_alt_sv = "]" record_chrom ":" record_pos "]" (bases)+ |
"[" record_chrom ":" record_pos "[" (bases)+ |
(bases)+ "]" record_chrom ":" record_pos "]" |
(bases)+ "[" record_chrom ":" record_pos "[" |
"." bases | bases ".";
## Other alternates can be any identifier surrounded by < > symbols
record_alt_other= ("<" identifier ">") - ("<DEL>" | "<INS>" | "<DUP>" | "<INV>" | "<CNV>" | "<DUP:TANDEM>" | "<DEL:ME:" (alnum)+ ">" | "<INS:ME:" (alnum)+ ">");
## Main alternate allele rule
record_alt = record_alt_snv %alt_snv_end |
record_alt_indel %alt_indel_end |
record_alt_sv %alt_sv_end |
record_alt_other ;
record_qual = any_number | "." ;
filter_value = (alnum | punct - ";")+ - (punct)+ ;
record_filter = (filter_value (";" filter_value)*) | "." ;
info_key = (alnum | (punct - (";" | "=")))+ - ( "AA" | "AC" | "AF" | "AN" | "BQ" | "CIGAR" | "DB" | "DP" | "END" | "H2" | "H3" | "MQ" | "MQ0" | "NS" | "SB" | "SOMATIC" | "VALIDATED" | "1000G" | (punct)+ ) ;
info_value = (print - (space | ";"))+ ;
info_value_list = info_value ("," info_value)* ;
info_entry = (
"AA=" (bases | ".") $err(info_AA_error) |
"AC=" (int_number ("," int_number)*) $err(info_AC_error) |
"AF=" (any_number ("," any_number)*) $err(info_AF_error) |
"AN=" int_number $err(info_AN_error) |
"BQ=" any_number $err(info_BQ_error) |
"CIGAR=" (alnum)+ $err(info_CIGAR_error) |
"DB" ("=" ("1" | "0"))? $err(info_DB_error) |
"DP=" int_number $err(info_DP_error) |
"END=" int_number $err(info_END_error) |
"H2" ("=" ("1" | "0"))? $err(info_H2_error) |
"H3" ("=" ("1" | "0"))? $err(info_H3_error) |
"MQ=" any_number $err(info_MQ_error) |
"MQ0=" int_number $err(info_MQ0_error) |
"NS=" int_number $err(info_NS_error) |
"SB=" any_number $err(info_SB_error) |
"SOMATIC" ("=" ("1" | "0"))? $err(info_SOMATIC_error) |
"VALIDATED" ("=" ("1" | "0"))? $err(info_VALIDATED_error) |
"1000G" ("=" ("1" | "0"))? $err(info_1000G_error) |
info_key $err(info_key_error) |
info_key $err(info_key_error) "=" info_value_list $err(info_value_error)
);
record_info = info_entry (";" info_entry)* | "." ;
## TODO Could this be extended because a lot of files use _ as a separator?
format_value = (alnum)+ ;
record_format = "GT" $err(format_gt_error) (":" format_value)* ;
## In a sample, the genotype is mandatory and must be the first field
sample_allele = (digit | ".") ;
sample_gt = sample_allele (("/" | "|") sample_allele)* ;
record_sample = sample_gt $err(sample_gt_error) (":" (alnum | punct)+)* ;
record = (record_chrom >token_begin @token_middle %token_end $err(chrom_error)
CS record_pos >token_begin @token_middle %token_end $err(pos_error)
CS record_id >token_begin @token_middle %token_end $err(id_error)
CS record_ref >token_begin @token_middle %token_end $err(ref_error)
CS record_alt >token_begin @token_middle %token_end $err(alt_error)
CS record_qual >token_begin @token_middle %token_end $err(qual_error)
CS record_filter >token_begin @token_middle %token_end $err(filter_error)
CS record_info >token_begin @token_middle %token_end $err(info_error)
(CS record_format >token_begin @token_middle %token_end $err(format_error)
(CS record_sample >token_begin @token_middle %token_end $err(sample_error))+ )?
) >record_begin %record_end;
fileformat_section = (fileformat NL) $err(fileformat_section_error);
meta_section = (meta_entry NL)* $err(meta_section_error);
header_section = (header NL) $err(header_section_error);
body_section = (record NL)* $err(body_section_error);
# Machine start (fileformat, then optional meta, header, then optional records)
main := fileformat_section
meta_section
header_section
body_section ;
# Error recovery machines that skip until the next line and restart the
# the most appropriate section state.
meta_section_skip := [^\n]* NL @{ fgoto meta_section; };
body_section_skip := [^\n]* NL @{ fgoto body_section; };
}%%
namespace
{
%%{
write data;
}%%
}
namespace opencb
{
namespace vcf
{
template <typename Configuration>
Parser<Configuration>::Parser()
{
%%{
write init;
}%%
}
template <typename Configuration>
void Parser<Configuration>::parse(std::vector<char> const & text)
{
char const * p = &text[0];
char const * pe = &text[0] + text.size();
char const * eof = nullptr;
parse_buffer(p, pe, eof);
}
template <typename Configuration>
void Parser<Configuration>::parse(std::string const & text)
{
char const * p = text.data();
char const * pe = text.data() + text.size();
char const * eof = nullptr;
parse_buffer(p, pe, eof);
}
template <typename Configuration>
void Parser<Configuration>::end()
{
char const * empty = "";
parse_buffer(empty, empty, empty);
}
template <typename Configuration>
bool Parser<Configuration>::is_valid() const
{
return cs >= %%{ write first_final; }%%;
}
template <typename Configuration>
void Parser<Configuration>::parse_buffer(char const * p, char const * pe, char const * eof)
{
char const * ts = nullptr;
char const * te = nullptr;
%%{
write exec;
}%%
}
}
}