forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.c
1788 lines (1713 loc) · 64.1 KB
/
filter.c
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* filter.c -- filter expressions.
Copyright (C) 2013-2015 Genome Research Ltd.
Author: Petr Danecek <[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 <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <wordexp.h>
#include <regex.h>
#include <htslib/khash_str2int.h>
#include "filter.h"
#include "bcftools.h"
#include <htslib/hts_defs.h>
#include <htslib/vcfutils.h>
typedef struct _token_t
{
// read-only values, same for all VCF lines
int tok_type; // one of the TOK_* keys below
char *key; // set only for string constants, otherwise NULL
char *tag; // for debugging and printout only, VCF tag name
float threshold; // filtering threshold
int hdr_id, type; // BCF header lookup ID and one of BCF_HT_* types
int idx; // 0-based index to VCF vectors, -1: not a vector, -2: any field ([*])
void (*setter)(filter_t *, bcf1_t *, struct _token_t *);
int (*comparator)(struct _token_t *, struct _token_t *, int op_type, bcf1_t *);
void *hash; // test presence of str value in the hash via comparator
regex_t *regex; // precompiled regex for string comparison
// modified on filter evaluation at each VCF line
float *values; // In case str_value is set, values[0] is one sample's string length
char *str_value; // and values[0]*nsamples gives the total length;
int is_str, is_missing; // is_missing is set only for constants, variables are controled via nvalues
int pass_site; // -1 not applicable, 0 fails, >0 pass
uint8_t *pass_samples; // status of individual samples
int nsamples; // number of samples
int nvalues, mvalues; // number of used values, n=0 for missing values, n=1 for scalars
// for strings, total length of str_value
}
token_t;
struct _filter_t
{
bcf_hdr_t *hdr;
char *str;
int nfilters;
token_t *filters, **flt_stack; // filtering input tokens (in RPN) and evaluation stack
int32_t *tmpi;
int max_unpack, mtmpi, nsamples;
};
#define TOK_VAL 0
#define TOK_LFT 1 // (
#define TOK_RGT 2 // )
#define TOK_LE 3 // less or equal
#define TOK_LT 4 // less than
#define TOK_EQ 5 // equal
#define TOK_BT 6 // bigger than
#define TOK_BE 7 // bigger or equal
#define TOK_NE 8 // not equal
#define TOK_OR 9 // |
#define TOK_AND 10 // &
#define TOK_ADD 11 // +
#define TOK_SUB 12 // -
#define TOK_MULT 13 // *
#define TOK_DIV 14 // /
#define TOK_MAX 15
#define TOK_MIN 16
#define TOK_AVG 17
#define TOK_AND_VEC 18 // && (operator applied in samples)
#define TOK_OR_VEC 19 // || (operator applied in samples)
#define TOK_LIKE 20 // ~ regular expression
#define TOK_NLIKE 21 // !~ regular expression
#define TOK_SUM 22
#define TOK_ABS 23
#define TOK_LEN 24
#define TOK_FUNC 25
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// ( ) [ < = > ] ! | & + - * / M m a A O ~ ^ S . l
static int op_prec[] = {0,1,1,5,5,5,5,5,5,2,3, 6, 6, 7, 7, 8, 8, 8, 3, 2, 5, 5, 8, 8, 8};
#define TOKEN_STRING "x()[<=>]!|&+-*/MmaAO~^f"
static int filters_next_token(char **str, int *len)
{
char *tmp = *str;
while ( *tmp && isspace(*tmp) ) tmp++;
*str = tmp;
*len = 0;
// test for doubles: d.ddde[+-]dd
if ( isdigit(*str[0]) || *str[0]=='.' ) // strtod would eat +/-
{
double HTS_UNUSED v = strtod(*str, &tmp);
if ( *str!=tmp && (!tmp[0] || !isalnum(tmp[0])) )
{
*len = tmp - (*str);
return TOK_VAL;
}
tmp = *str;
}
if ( !strncasecmp(tmp,"MAX(",4) ) { (*str) += 3; return TOK_MAX; }
if ( !strncasecmp(tmp,"MIN(",4) ) { (*str) += 3; return TOK_MIN; }
if ( !strncasecmp(tmp,"AVG(",4) ) { (*str) += 3; return TOK_AVG; }
if ( !strncasecmp(tmp,"SUM(",4) ) { (*str) += 3; return TOK_SUM; }
if ( !strncasecmp(tmp,"ABS(",4) ) { (*str) += 3; return TOK_ABS; }
if ( !strncasecmp(tmp,"STRLEN(",7) ) { (*str) += 6; return TOK_LEN; }
if ( !strncasecmp(tmp,"%MAX(",5) ) { (*str) += 4; return TOK_MAX; } // for backward compatibility
if ( !strncasecmp(tmp,"%MIN(",5) ) { (*str) += 4; return TOK_MIN; } // for backward compatibility
if ( !strncasecmp(tmp,"%AVG(",5) ) { (*str) += 4; return TOK_AVG; } // for backward compatibility
if ( !strncasecmp(tmp,"%SUM(",5) ) { (*str) += 4; return TOK_SUM; } // for backward compatibility
if ( !strncasecmp(tmp,"INFO/",5) ) tmp += 5;
if ( !strncasecmp(tmp,"FORMAT/",7) ) tmp += 7;
if ( !strncasecmp(tmp,"FMT/",4) ) tmp += 4;
if ( tmp[0]=='@' ) // file name
{
while ( *tmp && !isspace(*tmp) && *tmp!='=' && *tmp!='!' ) tmp++;
*len = tmp - (*str);
return TOK_VAL;
}
while ( tmp[0] )
{
if ( tmp[0]=='"' ) break;
if ( tmp[0]=='\'' ) break;
if ( isspace(tmp[0]) ) break;
if ( tmp[0]=='<' ) break;
if ( tmp[0]=='>' ) break;
if ( tmp[0]=='=' ) break;
if ( tmp[0]=='!' ) break;
if ( tmp[0]=='&' ) break;
if ( tmp[0]=='|' ) break;
if ( tmp[0]=='(' ) break;
if ( tmp[0]==')' ) break;
if ( tmp[0]=='+' ) break;
// hacky: so that [*] is not split, the tokenizer does not recognise square brackets []
if ( tmp[0]=='*' && (tmp==*str || tmp[-1]!='[') ) break;
if ( tmp[0]=='-' ) break;
if ( tmp[0]=='/' ) break;
if ( tmp[0]=='~' ) break;
tmp++;
}
if ( tmp > *str )
{
*len = tmp - (*str);
return TOK_VAL;
}
if ( tmp[0]=='"' || tmp[0]=='\'' )
{
int quote = tmp[0];
tmp++;
while ( *tmp && tmp[0]!=quote ) tmp++;
if ( !*tmp ) return -1; // missing quotes
*len = tmp - (*str) + 1;
return TOK_VAL;
}
if ( tmp[0]=='!' )
{
if ( tmp[1]=='=' ) { (*str) += 2; return TOK_NE; }
if ( tmp[1]=='~' ) { (*str) += 2; return TOK_NLIKE; }
}
if ( tmp[0]=='<' )
{
if ( tmp[1]=='=' ) { (*str) += 2; return TOK_LE; }
(*str) += 1; return TOK_LT;
}
if ( tmp[0]=='>' )
{
if ( tmp[1]=='=' ) { (*str) += 2; return TOK_BE; }
(*str) += 1; return TOK_BT;
}
if ( tmp[0]=='=' )
{
if ( tmp[1]=='=' ) { (*str) += 2; return TOK_EQ; }
(*str) += 1; return TOK_EQ;
}
if ( tmp[0]=='(' ) { (*str) += 1; return TOK_LFT; }
if ( tmp[0]==')' ) { (*str) += 1; return TOK_RGT; }
if ( tmp[0]=='&' && tmp[1]=='&' ) { (*str) += 2; return TOK_AND_VEC; }
if ( tmp[0]=='|' && tmp[1]=='|' ) { (*str) += 2; return TOK_OR_VEC; }
if ( tmp[0]=='&' ) { (*str) += 1; return TOK_AND; }
if ( tmp[0]=='|' ) { (*str) += 1; return TOK_OR; }
if ( tmp[0]=='+' ) { (*str) += 1; return TOK_ADD; }
if ( tmp[0]=='-' ) { (*str) += 1; return TOK_SUB; }
if ( tmp[0]=='*' ) { (*str) += 1; return TOK_MULT; }
if ( tmp[0]=='/' ) { (*str) += 1; return TOK_DIV; }
if ( tmp[0]=='~' ) { (*str) += 1; return TOK_LIKE; }
*len = tmp - (*str);
return TOK_VAL;
}
static void filters_set_qual(filter_t *flt, bcf1_t *line, token_t *tok)
{
float *ptr = &line->qual;
if ( bcf_float_is_missing(*ptr) )
tok->nvalues = 0;
else
{
tok->values[0] = line->qual;
tok->nvalues = 1;
}
}
static void filters_set_type(filter_t *flt, bcf1_t *line, token_t *tok)
{
tok->values[0] = bcf_get_variant_types(line);
tok->nvalues = 1;
}
static void filters_set_info(filter_t *flt, bcf1_t *line, token_t *tok)
{
assert( tok->hdr_id >=0 );
int i;
for (i=0; i<line->n_info; i++)
if ( line->d.info[i].key == tok->hdr_id ) break;
if ( i==line->n_info )
tok->nvalues = 0;
else if ( line->d.info[i].type==BCF_BT_CHAR )
{
int n = line->d.info[i].len;
int m = (int)tok->values[0];
hts_expand(char,n+1,m,tok->str_value);
memcpy(tok->str_value,line->d.info[i].vptr,n);
tok->str_value[n] = 0;
tok->values[0] = m;
tok->nvalues = n;
}
else if ( line->d.info[i].type==BCF_BT_FLOAT )
{
if ( bcf_float_is_missing(line->d.info[i].v1.f) ) tok->nvalues = 0;
else
{
tok->values[0] = line->d.info[i].v1.f;
tok->nvalues = 1;
}
tok->str_value = NULL;
}
else
{
if ( line->d.info[i].type==BCF_BT_INT8 && line->d.info[i].v1.i==bcf_int8_missing ) tok->nvalues = 0;
else if ( line->d.info[i].type==BCF_BT_INT16 && line->d.info[i].v1.i==bcf_int16_missing ) tok->nvalues = 0;
else if ( line->d.info[i].type==BCF_BT_INT32 && line->d.info[i].v1.i==bcf_int32_missing ) tok->nvalues = 0;
else
{
tok->values[0] = line->d.info[i].v1.i;
tok->nvalues = 1;
}
tok->str_value = NULL;
}
}
static int filters_cmp_filter(token_t *atok, token_t *btok, int op_type, bcf1_t *line)
{
int i;
if ( op_type==TOK_NE ) // AND logic: none of the filters can match
{
if ( !line->d.n_flt )
{
if ( atok->hdr_id==-1 ) return 0; // missing value
return 1; // no filter present, eval to true
}
for (i=0; i<line->d.n_flt; i++)
if ( atok->hdr_id==line->d.flt[i] ) return 0;
return 1;
}
// TOK_EQ with OR logic: at least one of the filters must match
if ( !line->d.n_flt )
{
if ( atok->hdr_id==-1 ) return 1;
return 0; // no filter present, eval to false
}
for (i=0; i<line->d.n_flt; i++)
if ( atok->hdr_id==line->d.flt[i] ) return 1;
return 0;
}
static int filters_cmp_id(token_t *atok, token_t *btok, int op_type, bcf1_t *line)
{
// multiple IDs not supported yet (easy to add though)
if ( btok->hash )
{
token_t *tmp = atok; atok = btok; btok = tmp;
}
if ( atok->hash )
{
int ret = khash_str2int_has_key(atok->hash, line->d.id);
if ( op_type==TOK_EQ ) return ret;
return ret ? 0 : 1;
}
if ( op_type==TOK_EQ ) return strcmp(btok->str_value,line->d.id) ? 0 : 1;
return strcmp(btok->str_value,line->d.id) ? 1 : 0;
}
/**
* bcf_get_info_value() - get single INFO value, int or float
* @line: BCF line
* @info_id: tag ID, as returned by bcf_hdr_id2int
* @ivec: 0-based index to retrieve, -1 when single value is expected
* @vptr: pointer to memory location of sufficient size to accomodate
* info_id's type
*
* The returned value is -1 if tag is not present, 0 if present but
* values is missing or ivec is out of range, and 1 on success.
*/
static int bcf_get_info_value(bcf1_t *line, int info_id, int ivec, void *value)
{
int j;
for (j=0; j<line->n_info; j++)
if ( line->d.info[j].key == info_id ) break;
if ( j==line->n_info ) return -1;
bcf_info_t *info = &line->d.info[j];
if ( info->len == 1 )
{
if ( info->type==BCF_BT_FLOAT ) *((float*)value) = info->v1.f;
else if ( info->type==BCF_BT_INT8 || info->type==BCF_BT_INT16 || info->type==BCF_BT_INT32 ) *((int*)value) = info->v1.i;
return 1;
}
if ( ivec<0 ) ivec = 0;
#define BRANCH(type_t, is_missing, is_vector_end, out_type_t) { \
type_t *p = (type_t *) info->vptr; \
for (j=0; j<ivec && j<info->len; j++) \
{ \
if ( is_vector_end ) return 0; \
} \
if ( is_missing ) return 0; \
*((out_type_t*)value) = p[j]; \
return 1; \
}
switch (info->type) {
case BCF_BT_INT8: BRANCH(int8_t, p[j]==bcf_int8_missing, p[j]==bcf_int8_vector_end, int); break;
case BCF_BT_INT16: BRANCH(int16_t, p[j]==bcf_int16_missing, p[j]==bcf_int16_vector_end, int); break;
case BCF_BT_INT32: BRANCH(int32_t, p[j]==bcf_int32_missing, p[j]==bcf_int32_vector_end, int); break;
case BCF_BT_FLOAT: BRANCH(float, bcf_float_is_missing(p[j]), bcf_float_is_vector_end(p[j]), float); break;
default: fprintf(stderr,"todo: type %d\n", info->type); exit(1); break;
}
#undef BRANCH
return -1; // this shouldn't happen
}
static void filters_set_pos(filter_t *flt, bcf1_t *line, token_t *tok)
{
tok->values[0] = line->pos+1;
tok->nvalues = 1;
}
static void filters_set_info_int(filter_t *flt, bcf1_t *line, token_t *tok)
{
if ( tok->idx==-2 )
{
int i, n = bcf_get_info_int32(flt->hdr,line,tok->tag,&flt->tmpi,&flt->mtmpi);
tok->nvalues = n;
hts_expand(float,n,tok->mvalues,tok->values);
for (i=0; i<n; i++) tok->values[i] = flt->tmpi[i];
}
else
{
int32_t value;
if ( bcf_get_info_value(line,tok->hdr_id,tok->idx,&value) <= 0 )
tok->nvalues = 0;
else
{
tok->values[0] = value;
tok->nvalues = 1;
}
}
}
static void filters_set_info_float(filter_t *flt, bcf1_t *line, token_t *tok)
{
if ( tok->idx==-2 )
{
tok->nvalues = bcf_get_info_float(flt->hdr,line,tok->tag,&tok->values,&tok->mvalues);
if ( tok->nvalues<0 ) tok->nvalues = 0;
}
else
{
float value;
if ( bcf_get_info_value(line,tok->hdr_id,tok->idx,&value) <= 0 )
tok->nvalues = 0;
else
{
tok->values[0] = value;
tok->nvalues = 1;
}
}
}
static void filters_set_info_string(filter_t *flt, bcf1_t *line, token_t *tok)
{
int m = (int)tok->values[0];
int n = bcf_get_info_string(flt->hdr,line,tok->tag,&tok->str_value,&m);
if ( n<0 ) { tok->nvalues = 0; return; }
tok->values[0] = m; // allocated length
if ( tok->idx>=0 )
{
// get ith field (i=tok->idx)
int i = 0;
char *ss = tok->str_value, *se = tok->str_value + n;
while ( ss<se && i<tok->idx )
{
if ( *ss==',' ) i++;
ss++;
}
if ( ss==se || i!=tok->idx ) { tok->nvalues = 0; return; }
se = ss;
while ( se-tok->str_value<n && *se!=',' ) se++;
if ( ss==tok->str_value ) *se = 0;
else
{
memmove(tok->str_value,ss,se-ss);
tok->str_value[se-ss] = 0;
}
tok->nvalues = se-ss;
}
else if ( tok->idx==-2 ) tok->nvalues = n;
}
static void filters_set_info_flag(filter_t *flt, bcf1_t *line, token_t *tok)
{
int j;
for (j=0; j<line->n_info; j++)
if ( line->d.info[j].key == tok->hdr_id ) break;
tok->values[0] = j==line->n_info ? 0 : 1;
tok->nvalues = 1;
}
static void filters_set_format_int(filter_t *flt, bcf1_t *line, token_t *tok)
{
int i;
if ( (tok->nvalues=bcf_get_format_int32(flt->hdr,line,tok->tag,&flt->tmpi,&flt->mtmpi))<0 )
tok->nvalues = 0;
else
{
int is_missing = 1;
hts_expand(float,tok->nvalues,tok->mvalues,tok->values);
for (i=0; i<tok->nvalues; i++)
{
if ( flt->tmpi[i]==bcf_int32_missing || flt->tmpi[i]==bcf_int32_vector_end )
bcf_float_set_missing(tok->values[i]);
else
{
tok->values[i] = flt->tmpi[i];
is_missing = 0;
}
}
if ( is_missing ) tok->nvalues = 0;
else if ( tok->idx >= 0 )
{
int nsmpl = bcf_hdr_nsamples(flt->hdr);
int nvals = tok->nvalues / nsmpl;
if ( tok->idx >= nvals )
tok->nvalues = 0; // the index is too big
else
{
for (i=0; i<nsmpl; i++)
tok->values[i] = tok->values[i*nvals+tok->idx];
tok->nvalues = nsmpl;
}
}
}
tok->nsamples = tok->nvalues;
}
static void filters_set_format_float(filter_t *flt, bcf1_t *line, token_t *tok)
{
if ( (tok->nvalues=bcf_get_format_float(flt->hdr,line,tok->tag,&tok->values,&tok->mvalues))<=0 )
tok->nvalues = tok->nsamples = 0; // missing values
else if ( tok->idx >= 0 )
{
int i, nsmpl, nvals;
nsmpl = bcf_hdr_nsamples(flt->hdr);
nvals = tok->nvalues / nsmpl;
if ( tok->idx >= nvals )
tok->nsamples = tok->nvalues = 0; // the index is too big
else
{
for (i=0; i<nsmpl; i++)
tok->values[i] = tok->values[i*nvals+tok->idx];
tok->nsamples = tok->nvalues = nsmpl;
}
}
tok->nsamples = tok->nvalues;
}
static void filters_set_format_string(filter_t *flt, bcf1_t *line, token_t *tok)
{
int ndim = tok->nsamples * (int)tok->values[0];
int ret = bcf_get_format_char(flt->hdr,line,tok->tag,&tok->str_value,&ndim);
int nsmpl = bcf_hdr_nsamples(flt->hdr);
ndim /= nsmpl;
tok->values[0] = ndim;
if ( ret<=0 )
{
tok->nvalues = 0;
return;
}
if ( tok->idx < 0 ) // scalar
{
tok->nvalues = tok->nsamples = nsmpl;
return;
}
// vector
int i;
for (i=0; i<nsmpl; i++)
{
char *ss = tok->str_value + i*ndim;
int is = 0, ivec = 0;
while ( ivec<tok->idx && is<ndim && ss[is] )
{
if ( ss[is]==',' ) ivec++;
is++;
}
if ( ivec!=tok->idx || is==ndim || !ss[is] )
{
ss[0] = '.';
ss[1] = 0;
continue;
}
int ie = is;
while ( ie<ndim && ss[ie] && ss[ie]!=',' ) ie++;
if ( is ) memmove(ss,&ss[is],ie-is);
if ( ndim-(ie-is) ) memset(ss+ie-is,0,ndim-(ie-is));
}
if ( !ndim )
{
tok->nvalues = 0;
return;
}
tok->nvalues = ret;
tok->nsamples = nsmpl;
}
static void filters_set_genotype_string(filter_t *flt, bcf1_t *line, token_t *tok)
{
bcf_fmt_t *fmt = bcf_get_fmt(flt->hdr, line, "GT");
if ( !fmt )
{
tok->nvalues = tok->nsamples = 0;
return;
}
int i, blen = 3, nsmpl = bcf_hdr_nsamples(flt->hdr);
kstring_t str;
gt_length_too_big:
str.s = tok->str_value; str.m = tok->values[0] * nsmpl; str.l = 0;
for (i=0; i<nsmpl; i++)
{
int plen = str.l;
#define BRANCH(type_t) { \
type_t *ptr = (type_t*) (fmt->p + i*fmt->size); \
if ( !(ptr[0]>>1) ) kputc('.',&str); \
}
switch (fmt->type) {
case BCF_BT_INT8: BRANCH(int8_t); break;
case BCF_BT_INT16: BRANCH(int16_t); break;
case BCF_BT_INT32: BRANCH(int32_t); break;
default: fprintf(stderr,"FIXME: type %d in bcf_format_gt?\n", fmt->type); abort(); break;
}
#undef BRANCH
if ( plen==str.l )
{
bcf_format_gt(fmt, i, &str);
if ( str.l - plen > blen )
{
// too many alternate alleles or ploidy is too large, the genotype does not fit
// three characters ("0/0" vs "10/10").
tok->str_value = str.s;
blen *= 2;
goto gt_length_too_big;
}
}
plen = str.l - plen;
while ( plen<blen )
{
kputc_(0, &str);
plen++;
}
}
tok->nvalues = str.l;
tok->nsamples = nsmpl;
tok->values[0] = blen;
tok->str_value = str.s;
}
static void filters_set_ref_string(filter_t *flt, bcf1_t *line, token_t *tok)
{
kstring_t str; str.s = tok->str_value; str.m = tok->values[0]; str.l = 0;
kputs(line->d.allele[0], &str);
tok->nvalues = str.l;
tok->values[0] = str.m;
tok->str_value = str.s;
}
static void filters_set_alt_string(filter_t *flt, bcf1_t *line, token_t *tok)
{
kstring_t str; str.s = tok->str_value; str.m = tok->values[0]; str.l = 0;
if ( tok->idx>=0 )
{
if ( line->n_allele >= tok->idx )
kputs(line->d.allele[tok->idx], &str);
else
kputc('.', &str);
}
else if ( line->n_allele>1 )
{
kputs(line->d.allele[1], &str);
int i;
for (i=2; i<line->n_allele; i++)
{
kputc(',', &str);
kputs(line->d.allele[i], &str);
}
}
else if ( line->n_allele==1 )
kputc('.', &str);
tok->nvalues = str.l;
tok->values[0] = str.m;
tok->str_value = str.s;
}
static void filters_set_nalt(filter_t *flt, bcf1_t *line, token_t *tok)
{
tok->nvalues = 1;
tok->values[0] = line->n_allele - 1;
}
static void filters_set_ac(filter_t *flt, bcf1_t *line, token_t *tok)
{
hts_expand(int32_t, line->n_allele, flt->mtmpi, flt->tmpi);
if ( !bcf_calc_ac(flt->hdr, line, flt->tmpi, BCF_UN_INFO|BCF_UN_FMT) )
{
tok->nvalues = 0;
return;
}
int i, an = flt->tmpi[0];
for (i=1; i<line->n_allele; i++) an += flt->tmpi[i];
if ( !an )
{
tok->nvalues = 0;
return;
}
flt->tmpi[0] = an; // for filters_set_[mac|af|maf]
if ( tok->idx>=0 )
{
tok->nvalues = 1;
tok->values[0] = tok->idx+1<line->n_allele ? flt->tmpi[tok->idx+1] : 0;
}
else if ( line->n_allele==1 ) // no ALT
{
tok->nvalues = 1;
tok->values[0] = 0;
}
else
{
hts_expand(float,line->n_allele,tok->mvalues,tok->values);
for (i=1; i<line->n_allele; i++)
tok->values[i-1] = flt->tmpi[i];
tok->nvalues = line->n_allele - 1;
}
}
static void filters_set_an(filter_t *flt, bcf1_t *line, token_t *tok)
{
filters_set_ac(flt,line,tok);
tok->values[0] = tok->nvalues ? flt->tmpi[0] : 0;
tok->nvalues = 1;
}
static void filters_set_mac(filter_t *flt, bcf1_t *line, token_t *tok)
{
filters_set_ac(flt,line,tok);
if ( !tok->nvalues ) return;
int i, an = flt->tmpi[0];
for (i=0; i<tok->nvalues; i++)
if ( tok->values[i] > an*0.5 ) tok->values[i] = an - tok->values[i];
}
static void filters_set_af(filter_t *flt, bcf1_t *line, token_t *tok)
{
filters_set_ac(flt,line,tok);
if ( !tok->nvalues ) return;
int i, an = flt->tmpi[0];
for (i=0; i<tok->nvalues; i++)
tok->values[i] /= (float)an;
}
static void filters_set_maf(filter_t *flt, bcf1_t *line, token_t *tok)
{
filters_set_ac(flt,line,tok);
if ( !tok->nvalues ) return;
int i, an = flt->tmpi[0];
for (i=0; i<tok->nvalues; i++)
{
tok->values[i] /= (float)an;
if ( tok->values[i] > 0.5 ) tok->values[i] = 1 - tok->values[i];
}
}
static void set_max(filter_t *flt, bcf1_t *line, token_t *tok)
{
float val = -HUGE_VAL;
int i;
for (i=0; i<tok->nvalues; i++)
{
if ( !bcf_float_is_missing(tok->values[i]) && val < tok->values[i] ) val = tok->values[i];
}
tok->values[0] = val;
tok->nvalues = 1;
tok->nsamples = 0;
}
static void set_min(filter_t *flt, bcf1_t *line, token_t *tok)
{
float val = HUGE_VAL;
int i;
for (i=0; i<tok->nvalues; i++)
if ( !bcf_float_is_missing(tok->values[i]) && val > tok->values[i] ) val = tok->values[i];
tok->values[0] = val;
tok->nvalues = 1;
tok->nsamples = 0;
}
static void set_avg(filter_t *flt, bcf1_t *line, token_t *tok)
{
float val = 0;
int i, n = 0;
for (i=0; i<tok->nvalues; i++)
if ( !bcf_float_is_missing(tok->values[i]) ) { val += tok->values[i]; n++; }
tok->values[0] = n ? val / n : 0;
tok->nvalues = 1;
tok->nsamples = 0;
}
static void set_sum(filter_t *flt, bcf1_t *line, token_t *tok)
{
float val = 0;
int i, n = 0;
for (i=0; i<tok->nvalues; i++)
if ( !bcf_float_is_missing(tok->values[i]) ) { val += tok->values[i]; n++; }
tok->values[0] = val;
tok->nvalues = 1;
tok->nsamples = 0;
}
static void set_abs(filter_t *flt, bcf1_t *line, token_t *tok)
{
if ( tok->is_str ) error("ABS() can be applied only on numeric values\n");
int i;
for (i=0; i<tok->nvalues; i++)
tok->values[i] = fabs(tok->values[i]);
}
static void set_strlen(filter_t *flt, bcf1_t *line, token_t *tok)
{
tok->is_str = 0;
if ( !tok->nvalues ) return;
if ( tok->idx==-2 )
{
int i = 0;
char *ss = tok->str_value;
while ( *ss )
{
char *se = ss;
while ( *se && *se!=',' ) se++;
if ( !*se ) tok->values[i] = strlen(ss);
else
{
*se = 0;
tok->values[i] = strlen(ss);
*se = ',';
}
ss = *se ? se + 1 : se;
i++;
}
tok->nvalues = i;
}
else
{
tok->values[0] = strlen(tok->str_value);
tok->nvalues = 1;
}
}
#define VECTOR_ARITHMETICS(atok,btok,AOP) \
{ \
int i, has_values = 0; \
if ( !(atok)->nvalues || !(btok)->nvalues ) /* missing values */ \
{ \
(atok)->nvalues = 0; (atok)->nsamples = 0; \
} \
else \
{ \
if ( ((atok)->nsamples && (btok)->nsamples) || (!(atok)->nsamples && !(btok)->nsamples)) \
{ \
for (i=0; i<(atok)->nvalues; i++) \
{ \
if ( bcf_float_is_missing((atok)->values[i]) ) continue; \
if ( bcf_float_is_missing((btok)->values[i]) ) { bcf_float_set_missing((atok)->values[i]); continue; } \
has_values = 1; \
(atok)->values[i] = (atok)->values[i] AOP (btok)->values[i]; \
} \
} \
else if ( (btok)->nsamples ) \
{ \
hts_expand(float,(btok)->nvalues,(atok)->mvalues,(atok)->values); \
for (i=0; i<(btok)->nvalues; i++) \
{ \
if ( bcf_float_is_missing((atok)->values[0]) || bcf_float_is_missing((btok)->values[i]) ) \
{ \
bcf_float_set_missing((atok)->values[i]); \
continue; \
} \
has_values = 1; \
(atok)->values[i] = (atok)->values[0] AOP (btok)->values[i]; \
} \
(atok)->nvalues = (btok)->nvalues; \
(atok)->nsamples = (btok)->nsamples; \
} \
else if ( (atok)->nsamples ) \
{ \
for (i=0; i<(atok)->nvalues; i++) \
{ \
if ( bcf_float_is_missing((atok)->values[i]) || bcf_float_is_missing((btok)->values[0]) ) \
{ \
bcf_float_set_missing((atok)->values[i]); \
continue; \
} \
has_values = 1; \
(atok)->values[i] = (atok)->values[i] AOP (btok)->values[0]; \
} \
} \
} \
if ( !has_values ) { (atok)->nvalues = 0; (atok)->nsamples = 0; } \
}
static int vector_logic_and(token_t *atok, token_t *btok, int and_type)
{
// We are comparing either two scalars (result of INFO tag vs a threshold), two vectors (two FORMAT fields),
// or a vector and a scalar (FORMAT field vs threshold)
int i, pass_site = 0;
if ( !atok->nvalues || !btok->nvalues )
{
atok->nvalues = atok->nsamples = 0;
return 0;
}
if ( !atok->nsamples && !btok->nsamples ) return atok->pass_site && btok->pass_site;
if ( atok->nsamples && btok->nsamples )
{
if ( and_type==TOK_AND )
{
// perform AND within a sample
for (i=0; i<atok->nsamples; i++)
{
atok->pass_samples[i] = atok->pass_samples[i] && btok->pass_samples[i];
if ( !pass_site && atok->pass_samples[i] ) pass_site = 1;
}
}
else
{
// perform AND across samples
int pass_a = 0, pass_b = 0;
for (i=0; i<atok->nsamples; i++)
{
if ( atok->pass_samples[i] ) pass_a = 1;
atok->pass_samples[i] = atok->pass_samples[i] && btok->pass_samples[i];
}
for (i=0; i<btok->nsamples; i++)
{
if ( btok->pass_samples[i] ) { pass_b = 1; break; }
}
pass_site = pass_a && pass_b;
}
return pass_site;
}
if ( btok->nsamples )
{
for (i=0; i<btok->nsamples; i++)
{
atok->pass_samples[i] = atok->pass_site && btok->pass_samples[i];
if ( !pass_site && atok->pass_samples[i] ) pass_site = 1;
}
atok->nsamples = btok->nsamples;
return pass_site;
}
/* atok->nsamples!=0 */
for (i=0; i<atok->nsamples; i++)
{
atok->pass_samples[i] = atok->pass_samples[i] && btok->pass_site;
if ( !pass_site && atok->pass_samples[i] ) pass_site = 1;
}
return pass_site;
}
static int vector_logic_or(token_t *atok, token_t *btok, int or_type)
{
int i, pass_site = 0;
if ( !atok->nvalues && !btok->nvalues ) // missing sites in both
{
atok->nvalues = atok->nsamples = 0;
return 0;
}
if ( !atok->nvalues ) // missing value in a
{
for (i=0; i<btok->nsamples; i++)
atok->pass_samples[i] = btok->pass_samples[i];
atok->nsamples = btok->nsamples;
return btok->pass_site;
}
if ( !btok->nvalues ) // missing value in b
return atok->pass_site;
if ( !atok->nsamples && !btok->nsamples ) return atok->pass_site || btok->pass_site;
if ( !atok->nsamples )
{
if ( or_type==TOK_OR )
{
for (i=0; i<btok->nsamples; i++)
{
atok->pass_samples[i] = btok->pass_samples[i];
if ( atok->pass_site || atok->pass_samples[i] ) pass_site = 1;
}
}
else
{
for (i=0; i<btok->nsamples; i++)
{
atok->pass_samples[i] = atok->pass_site || btok->pass_samples[i];
if ( atok->pass_samples[i] ) pass_site = 1;
}
}
atok->nsamples = btok->nsamples;
return pass_site;
}
if ( !btok->nsamples ) // vector vs site
{
if ( or_type==TOK_OR )
{
for (i=0; i<atok->nsamples; i++)
if ( btok->pass_site || atok->pass_samples[i] ) pass_site = 1;
}
else
{
for (i=0; i<atok->nsamples; i++)
{
atok->pass_samples[i] = atok->pass_samples[i] || btok->pass_site;
if ( atok->pass_samples[i] ) pass_site = 1;
}
}
return pass_site;
}
for (i=0; i<atok->nsamples; i++)
{
atok->pass_samples[i] = atok->pass_samples[i] || btok->pass_samples[i];
if ( !pass_site && atok->pass_samples[i] ) pass_site = 1;
}
return pass_site;
}
#define CMP_MISSING(atok,btok,CMP_OP,ret) \
{ \
if ( (atok)->nsamples || (btok)->nsamples ) error("todo: Querying of missing values in FORMAT\n"); \
token_t *tok = (atok)->is_missing ? (btok) : (atok); \
(ret) = ( tok->nvalues CMP_OP 1 ) ? 0 : 1; \
}
#define CMP_VECTORS(atok,btok,CMP_OP,ret) \
{ \
int i, j, has_values = 0, pass_site = 0; \
if ( !(atok)->nvalues || !(btok)->nvalues ) { (atok)->nvalues = 0; (atok)->nsamples = 0; (ret) = 0; } \
else \
{ \
if ( (atok)->nsamples && (btok)->nsamples ) \
{ \
for (i=0; i<(atok)->nsamples; i++) \
{ \
if ( bcf_float_is_missing((atok)->values[i]) ) { (atok)->pass_samples[i] = 0; continue; } \
if ( bcf_float_is_missing((btok)->values[i]) ) { (atok)->pass_samples[i] = 0; continue; } \
has_values = 1; \
if ( (atok)->values[i] CMP_OP (btok)->values[i] ) { (atok)->pass_samples[i] = 1; pass_site = 1; } \
else (atok)->pass_samples[i] = 0; \
} \
if ( !has_values ) (atok)->nvalues = 0; \
} \