| 1 |
#include "bcftools.pysam.h" |
| 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 |
#include <ctype.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <strings.h> |
| 30 |
#include <assert.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <math.h> |
| 33 |
#include <sys/types.h> |
| 34 |
#include <stdint.h> |
| 35 |
#include <inttypes.h> |
| 36 |
#ifndef _WIN32 |
| 37 |
#include <pwd.h> |
| 38 |
#endif |
| 39 |
#include <regex.h> |
| 40 |
#include <htslib/khash_str2int.h> |
| 41 |
#include <htslib/hts_defs.h> |
| 42 |
#include <htslib/vcfutils.h> |
| 43 |
#include <htslib/kfunc.h> |
| 44 |
#include <htslib/hts_endian.h> |
| 45 |
#include "config.h" |
| 46 |
#include "filter.h" |
| 47 |
#include "bcftools.h" |
| 48 |
|
| 49 |
#if ENABLE_PERL_FILTERS |
| 50 |
|
| 51 |
# if defined(__clang__) |
| 52 |
# define PERL_GCC_BRACE_GROUPS_FORBIDDEN |
| 53 |
# endif |
| 54 |
|
| 55 |
# define filter_t perl_filter_t |
| 56 |
# include <EXTERN.h> |
| 57 |
# include <perl.h> |
| 58 |
# undef filter_t |
| 59 |
# define my_perl perl |
| 60 |
|
| 61 |
static int filter_ninit = 0; |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
#ifndef __FUNCTION__ |
| 66 |
# define __FUNCTION__ __func__ |
| 67 |
#endif |
| 68 |
|
| 69 |
typedef struct _token_t |
| 70 |
{ |
| 71 |
|
| 72 |
int tok_type; |
| 73 |
int nargs; |
| 74 |
char *key; |
| 75 |
char *tag; |
| 76 |
double threshold; |
| 77 |
int is_constant; |
| 78 |
int hdr_id, hl_type, ht_type; |
| 79 |
int idx; |
| 80 |
|
| 81 |
|
| 82 |
int *idxs; |
| 83 |
int nidxs, nuidxs; |
| 84 |
uint8_t *usmpl; |
| 85 |
int nsamples; |
| 86 |
void (*setter)(filter_t *, bcf1_t *, struct _token_t *); |
| 87 |
int (*func)(filter_t *, bcf1_t *, struct _token_t *rtok, struct _token_t **stack, int nstack); |
| 88 |
void (*comparator)(struct _token_t *, struct _token_t *, struct _token_t *rtok, bcf1_t *); |
| 89 |
void *hash; |
| 90 |
regex_t *regex; |
| 91 |
int iext; |
| 92 |
|
| 93 |
|
| 94 |
double *values; |
| 95 |
kstring_t str_value; |
| 96 |
int is_str, is_missing; |
| 97 |
int pass_site; |
| 98 |
uint8_t *pass_samples; |
| 99 |
int nvalues, mvalues; |
| 100 |
int nval1; |
| 101 |
} |
| 102 |
token_t; |
| 103 |
|
| 104 |
struct _filter_t |
| 105 |
{ |
| 106 |
bcf_hdr_t *hdr; |
| 107 |
char *str; |
| 108 |
int nfilters; |
| 109 |
token_t *filters, **flt_stack; |
| 110 |
int32_t *tmpi; |
| 111 |
float *tmpf; |
| 112 |
kstring_t tmps; |
| 113 |
int max_unpack, mtmpi, mtmpf, nsamples; |
| 114 |
struct { |
| 115 |
bcf1_t *line; |
| 116 |
int32_t *buf, nbuf, mbuf; |
| 117 |
uint64_t *mask; |
| 118 |
} cached_GT; |
| 119 |
#if ENABLE_PERL_FILTERS |
| 120 |
PerlInterpreter *perl; |
| 121 |
#endif |
| 122 |
char **undef_tag, **used_tag; |
| 123 |
int nundef_tag, nused_tag; |
| 124 |
int status, exit_on_error; |
| 125 |
int n_ext; |
| 126 |
int *ext; |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
#define TOK_VAL 0 |
| 131 |
#define TOK_LFT 1 // ( |
| 132 |
#define TOK_RGT 2 // ) |
| 133 |
#define TOK_LE 3 // less or equal |
| 134 |
#define TOK_LT 4 // less than |
| 135 |
#define TOK_EQ 5 // equal |
| 136 |
#define TOK_BT 6 // bigger than |
| 137 |
#define TOK_BE 7 // bigger or equal |
| 138 |
#define TOK_NE 8 // not equal |
| 139 |
#define TOK_OR 9 // | |
| 140 |
#define TOK_AND 10 // & |
| 141 |
#define TOK_ADD 11 // + |
| 142 |
#define TOK_SUB 12 // - |
| 143 |
#define TOK_MULT 13 // * |
| 144 |
#define TOK_DIV 14 // / |
| 145 |
#define TOK_MAX 15 |
| 146 |
#define TOK_MIN 16 |
| 147 |
#define TOK_AVG 17 |
| 148 |
#define TOK_AND_VEC 18 // && (operator applied in samples) |
| 149 |
#define TOK_OR_VEC 19 // || (operator applied in samples) |
| 150 |
#define TOK_LIKE 20 // ~ regular expression |
| 151 |
#define TOK_NLIKE 21 // !~ regular expression |
| 152 |
#define TOK_SUM 22 |
| 153 |
#define TOK_ABS 23 |
| 154 |
#define TOK_LEN 24 |
| 155 |
#define TOK_FUNC 25 |
| 156 |
#define TOK_CNT 26 |
| 157 |
#define TOK_PERLSUB 27 |
| 158 |
#define TOK_BINOM 28 |
| 159 |
#define TOK_PHRED 29 |
| 160 |
#define TOK_MEDIAN 30 |
| 161 |
#define TOK_STDEV 31 |
| 162 |
#define TOK_sMAX 32 |
| 163 |
#define TOK_sMIN 33 |
| 164 |
#define TOK_sAVG 34 |
| 165 |
#define TOK_sMEDIAN 35 |
| 166 |
#define TOK_sSTDEV 36 |
| 167 |
#define TOK_sSUM 37 |
| 168 |
#define TOK_IN 38 // contains, e.g. FILTER~"A" |
| 169 |
#define TOK_NOT_IN 39 // does not contain, e.g. FILTER!~"A" |
| 170 |
#define TOK_MODULO 40 // % |
| 171 |
#define TOK_EXT 41 // external values set before each filter_test_ext() call, can be one of {},{str},{int},{float} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
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, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 0}; |
| 176 |
#define TOKEN_STRING "x()[<=>]!|&+-*/MmaAO~^S.lfcpis" // this is only for debugging, not maintained diligently |
| 177 |
|
| 178 |
static void cmp_vector_strings(token_t *atok, token_t *btok, token_t *rtok); |
| 179 |
inline static void tok_init_samples(token_t *atok, token_t *btok, token_t *rtok); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
static int filters_next_token(char **str, int *len) |
| 184 |
{ |
| 185 |
char *tmp = *str; |
| 186 |
while ( *tmp && isspace(*tmp) ) tmp++; |
| 187 |
*str = tmp; |
| 188 |
*len = 0; |
| 189 |
|
| 190 |
|
| 191 |
if ( isdigit(*str[0]) || *str[0]=='.' ) |
| 192 |
{ |
| 193 |
double HTS_UNUSED v = strtod(*str, &tmp); |
| 194 |
if ( *str!=tmp && (!tmp[0] || !isalnum(tmp[0])) ) |
| 195 |
{ |
| 196 |
*len = tmp - (*str); |
| 197 |
return TOK_VAL; |
| 198 |
} |
| 199 |
tmp = *str; |
| 200 |
} |
| 201 |
|
| 202 |
if ( !strncasecmp(tmp,"SMPL_MAX(",9) ) { (*str) += 8; return TOK_sMAX; } |
| 203 |
if ( !strncasecmp(tmp,"SMPL_MIN(",9) ) { (*str) += 8; return TOK_sMIN; } |
| 204 |
if ( !strncasecmp(tmp,"SMPL_MEAN(",10) ) { (*str) += 9; return TOK_sAVG; } |
| 205 |
if ( !strncasecmp(tmp,"SMPL_MEDIAN(",12) ) { (*str) += 11; return TOK_sMEDIAN; } |
| 206 |
if ( !strncasecmp(tmp,"SMPL_AVG(",9) ) { (*str) += 8; return TOK_sAVG; } |
| 207 |
if ( !strncasecmp(tmp,"SMPL_STDEV(",11) ) { (*str) += 10; return TOK_sSTDEV; } |
| 208 |
if ( !strncasecmp(tmp,"SMPL_SUM(",9) ) { (*str) += 8; return TOK_sSUM; } |
| 209 |
if ( !strncasecmp(tmp,"sMAX(",5) ) { (*str) += 4; return TOK_sMAX; } |
| 210 |
if ( !strncasecmp(tmp,"sMIN(",5) ) { (*str) += 4; return TOK_sMIN; } |
| 211 |
if ( !strncasecmp(tmp,"sMEAN(",6) ) { (*str) += 5; return TOK_sAVG; } |
| 212 |
if ( !strncasecmp(tmp,"sMEDIAN(",8) ) { (*str) += 7; return TOK_sMEDIAN; } |
| 213 |
if ( !strncasecmp(tmp,"sAVG(",5) ) { (*str) += 4; return TOK_sAVG; } |
| 214 |
if ( !strncasecmp(tmp,"sSTDEV(",7) ) { (*str) += 6; return TOK_sSTDEV; } |
| 215 |
if ( !strncasecmp(tmp,"sSUM(",5) ) { (*str) += 4; return TOK_sSUM; } |
| 216 |
if ( !strncasecmp(tmp,"MAX(",4) ) { (*str) += 3; return TOK_MAX; } |
| 217 |
if ( !strncasecmp(tmp,"MIN(",4) ) { (*str) += 3; return TOK_MIN; } |
| 218 |
if ( !strncasecmp(tmp,"MEAN(",5) ) { (*str) += 4; return TOK_AVG; } |
| 219 |
if ( !strncasecmp(tmp,"MEDIAN(",7) ) { (*str) += 6; return TOK_MEDIAN; } |
| 220 |
if ( !strncasecmp(tmp,"AVG(",4) ) { (*str) += 3; return TOK_AVG; } |
| 221 |
if ( !strncasecmp(tmp,"STDEV(",6) ) { (*str) += 5; return TOK_STDEV; } |
| 222 |
if ( !strncasecmp(tmp,"SUM(",4) ) { (*str) += 3; return TOK_SUM; } |
| 223 |
if ( !strncasecmp(tmp,"ABS(",4) ) { (*str) += 3; return TOK_ABS; } |
| 224 |
if ( !strncasecmp(tmp,"COUNT(",4) ) { (*str) += 5; return TOK_CNT; } |
| 225 |
if ( !strncasecmp(tmp,"STRLEN(",7) ) { (*str) += 6; return TOK_LEN; } |
| 226 |
if ( !strncasecmp(tmp,"BINOM(",6) ) { (*str) += 5; return -TOK_BINOM; } |
| 227 |
if ( !strncasecmp(tmp,"PHRED(",6) ) { (*str) += 5; return TOK_PHRED; } |
| 228 |
if ( !strncasecmp(tmp,"%MAX(",5) ) { (*str) += 4; return TOK_MAX; } |
| 229 |
if ( !strncasecmp(tmp,"%MIN(",5) ) { (*str) += 4; return TOK_MIN; } |
| 230 |
if ( !strncasecmp(tmp,"%AVG(",5) ) { (*str) += 4; return TOK_AVG; } |
| 231 |
if ( !strncasecmp(tmp,"%SUM(",5) ) { (*str) += 4; return TOK_SUM; } |
| 232 |
if ( !strncasecmp(tmp,"INFO/",5) ) tmp += 5; |
| 233 |
if ( !strncasecmp(tmp,"FORMAT/",7) ) tmp += 7; |
| 234 |
if ( !strncasecmp(tmp,"FMT/",4) ) tmp += 4; |
| 235 |
if ( !strncasecmp(tmp,"PERL.",5) ) { (*str) += 5; return -TOK_PERLSUB; } |
| 236 |
if ( !strncasecmp(tmp,"N_PASS(",7) ) { *len = 6; (*str) += 6; return -TOK_FUNC; } |
| 237 |
if ( !strncasecmp(tmp,"F_PASS(",7) ) { *len = 6; (*str) += 6; return -TOK_FUNC; } |
| 238 |
if ( !strncasecmp(tmp,"%ILEN",5) ) { *len = 5; return TOK_VAL; } |
| 239 |
if ( !strncasecmp(tmp,"{}",2) ) { *len = 2; return TOK_EXT; } |
| 240 |
if ( !strncasecmp(tmp,"{STR}",5) ) { *len = 5; return TOK_EXT; } |
| 241 |
if ( !strncasecmp(tmp,"{INT}",5) ) { *len = 5; return TOK_EXT; } |
| 242 |
if ( !strncasecmp(tmp,"{FLOAT}",7) ) { *len = 7; return TOK_EXT; } |
| 243 |
|
| 244 |
if ( tmp[0]=='@' ) |
| 245 |
{ |
| 246 |
while ( *tmp && !isspace(*tmp) && *tmp!='=' && *tmp!='!' ) tmp++; |
| 247 |
*len = tmp - (*str); |
| 248 |
return TOK_VAL; |
| 249 |
} |
| 250 |
|
| 251 |
int square_brackets = 0; |
| 252 |
while ( tmp[0] ) |
| 253 |
{ |
| 254 |
if ( !square_brackets ) |
| 255 |
{ |
| 256 |
if ( tmp[0]=='"' ) break; |
| 257 |
if ( tmp[0]=='\'' ) break; |
| 258 |
if ( isspace(tmp[0]) ) break; |
| 259 |
if ( tmp[0]=='<' ) break; |
| 260 |
if ( tmp[0]=='>' ) break; |
| 261 |
if ( tmp[0]=='=' ) break; |
| 262 |
if ( tmp[0]=='!' ) break; |
| 263 |
if ( tmp[0]=='&' ) break; |
| 264 |
if ( tmp[0]=='|' ) break; |
| 265 |
if ( tmp[0]=='(' ) break; |
| 266 |
if ( tmp[0]==')' ) break; |
| 267 |
if ( tmp[0]=='+' ) break; |
| 268 |
if ( tmp[0]=='*' ) break; |
| 269 |
if ( tmp[0]=='-' ) break; |
| 270 |
if ( tmp[0]=='/' ) break; |
| 271 |
if ( tmp[0]=='~' ) break; |
| 272 |
if ( tmp[0]=='%' ) break; |
| 273 |
} |
| 274 |
if ( tmp[0]==']' ) { if (square_brackets) tmp++; break; } |
| 275 |
if ( tmp[0]=='[' ) square_brackets++; |
| 276 |
tmp++; |
| 277 |
} |
| 278 |
if ( tmp > *str ) |
| 279 |
{ |
| 280 |
*len = tmp - (*str); |
| 281 |
return TOK_VAL; |
| 282 |
} |
| 283 |
if ( tmp[0]=='"' || tmp[0]=='\'' ) |
| 284 |
{ |
| 285 |
int quote = tmp[0]; |
| 286 |
tmp++; |
| 287 |
while ( *tmp && tmp[0]!=quote ) tmp++; |
| 288 |
if ( !*tmp ) return -1; |
| 289 |
*len = tmp - (*str) + 1; |
| 290 |
return TOK_VAL; |
| 291 |
} |
| 292 |
if ( tmp[0]=='!' ) |
| 293 |
{ |
| 294 |
if ( tmp[1]=='=' ) { (*str) += 2; return TOK_NE; } |
| 295 |
if ( tmp[1]=='~' ) { (*str) += 2; return TOK_NLIKE; } |
| 296 |
} |
| 297 |
if ( tmp[0]=='<' ) |
| 298 |
{ |
| 299 |
if ( tmp[1]=='=' ) { (*str) += 2; return TOK_LE; } |
| 300 |
(*str) += 1; return TOK_LT; |
| 301 |
} |
| 302 |
if ( tmp[0]=='>' ) |
| 303 |
{ |
| 304 |
if ( tmp[1]=='=' ) { (*str) += 2; return TOK_BE; } |
| 305 |
(*str) += 1; return TOK_BT; |
| 306 |
} |
| 307 |
if ( tmp[0]=='=' ) |
| 308 |
{ |
| 309 |
if ( tmp[1]=='=' ) { (*str) += 2; return TOK_EQ; } |
| 310 |
(*str) += 1; return TOK_EQ; |
| 311 |
} |
| 312 |
if ( tmp[0]=='(' ) { (*str) += 1; return TOK_LFT; } |
| 313 |
if ( tmp[0]==')' ) { (*str) += 1; return TOK_RGT; } |
| 314 |
if ( tmp[0]=='&' && tmp[1]=='&' ) { (*str) += 2; return TOK_AND_VEC; } |
| 315 |
if ( tmp[0]=='|' && tmp[1]=='|' ) { (*str) += 2; return TOK_OR_VEC; } |
| 316 |
if ( tmp[0]=='&' ) { (*str) += 1; return TOK_AND; } |
| 317 |
if ( tmp[0]=='|' ) { (*str) += 1; return TOK_OR; } |
| 318 |
if ( tmp[0]=='+' ) { (*str) += 1; return TOK_ADD; } |
| 319 |
if ( tmp[0]=='-' ) { (*str) += 1; return TOK_SUB; } |
| 320 |
if ( tmp[0]=='*' ) { (*str) += 1; return TOK_MULT; } |
| 321 |
if ( tmp[0]=='/' ) { (*str) += 1; return TOK_DIV; } |
| 322 |
if ( tmp[0]=='~' ) { (*str) += 1; return TOK_LIKE; } |
| 323 |
if ( tmp[0]=='%' ) { (*str) += 1; return TOK_MODULO; } |
| 324 |
|
| 325 |
*len = tmp - (*str); |
| 326 |
return TOK_VAL; |
| 327 |
} |
| 328 |
|
| 329 |
#define FILTER_OK 0 |
| 330 |
#define FILTER_ERR_UNKN_TAGS 1 |
| 331 |
#define FILTER_ERR_OTHER 2 |
| 332 |
|
| 333 |
static void filter_add_undef_tag(filter_t *filter, char *str) |
| 334 |
{ |
| 335 |
int i; |
| 336 |
for (i=0; i<filter->nundef_tag; i++) |
| 337 |
if ( !strcmp(str,filter->undef_tag[i]) ) break; |
| 338 |
if ( i<filter->nundef_tag ) return; |
| 339 |
filter->nundef_tag++; |
| 340 |
filter->undef_tag = (char**)realloc(filter->undef_tag,sizeof(*filter->undef_tag)*filter->nundef_tag); |
| 341 |
if ( !filter->undef_tag ) error("Could not allocate memory\n"); |
| 342 |
filter->undef_tag[filter->nundef_tag-1] = strdup(str); |
| 343 |
if ( !filter->undef_tag[filter->nundef_tag-1] ) error("Could not allocate memory\n"); |
| 344 |
} |
| 345 |
const char **filter_list_undef_tags(filter_t *filter, int *ntags) |
| 346 |
{ |
| 347 |
*ntags = filter->nundef_tag; |
| 348 |
return (const char**)filter->undef_tag; |
| 349 |
} |
| 350 |
static void filter_add_used_tag(filter_t *filter, const char *prefix, char *str) |
| 351 |
{ |
| 352 |
int i; |
| 353 |
kstring_t tmp = {0,0,0}; |
| 354 |
if ( prefix ) kputs(prefix,&tmp); |
| 355 |
kputs(str,&tmp); |
| 356 |
for (i=0; i<filter->nused_tag; i++) |
| 357 |
if ( !strcmp(tmp.s,filter->used_tag[i]) ) break; |
| 358 |
if ( i<filter->nused_tag ) |
| 359 |
{ |
| 360 |
free(tmp.s); |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
filter->nused_tag++; |
| 365 |
filter->used_tag = (char**)realloc(filter->used_tag,sizeof(*filter->used_tag)*filter->nused_tag); |
| 366 |
if ( !filter->used_tag ) error("Could not allocate memory\n"); |
| 367 |
filter->used_tag[filter->nused_tag-1] = tmp.s; |
| 368 |
if ( !filter->used_tag[filter->nused_tag-1] ) error("Could not allocate memory\n"); |
| 369 |
} |
| 370 |
const char **filter_list_used_tags(filter_t *filter, int *ntags) |
| 371 |
{ |
| 372 |
*ntags = filter->nused_tag; |
| 373 |
return (const char**)filter->used_tag; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
char *expand_path(char *path) |
| 385 |
{ |
| 386 |
kstring_t str = {0,0,0}; |
| 387 |
|
| 388 |
if ( path[0] == '~' ) |
| 389 |
{ |
| 390 |
if ( !path[1] || path[1] == '/' ) |
| 391 |
{ |
| 392 |
#ifdef _WIN32 |
| 393 |
kputs(getenv("HOMEDRIVE"), &str); |
| 394 |
kputs(getenv("HOMEPATH"), &str); |
| 395 |
#else |
| 396 |
|
| 397 |
kputs(getenv("HOME"), &str); |
| 398 |
if ( path[1] ) kputs(path+1, &str); |
| 399 |
#endif |
| 400 |
} |
| 401 |
#ifndef _WIN32 |
| 402 |
else |
| 403 |
{ |
| 404 |
|
| 405 |
char *end = path; |
| 406 |
while ( *end && *end!='/' ) end++; |
| 407 |
kputsn(path+1, end-path-1, &str); |
| 408 |
struct passwd *pwentry = getpwnam(str.s); |
| 409 |
str.l = 0; |
| 410 |
|
| 411 |
if ( !pwentry ) kputsn(path, end-path, &str); |
| 412 |
else kputs(pwentry->pw_dir, &str); |
| 413 |
kputs(end, &str); |
| 414 |
} |
| 415 |
#endif |
| 416 |
return ks_release(&str); |
| 417 |
} |
| 418 |
if ( path[0] == '$' ) |
| 419 |
{ |
| 420 |
char *var = getenv(path+1); |
| 421 |
if ( var ) { |
| 422 |
kputs(var, &str); |
| 423 |
return ks_release(&str); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
return strdup(path); |
| 428 |
} |
| 429 |
static int filters_cache_genotypes(filter_t *flt, bcf1_t *line) |
| 430 |
{ |
| 431 |
if ( flt->cached_GT.line==line ) return flt->cached_GT.nbuf > 0 ? 0 : -1; |
| 432 |
flt->cached_GT.line = line; |
| 433 |
flt->cached_GT.nbuf = bcf_get_genotypes(flt->hdr, line, &flt->cached_GT.buf, &flt->cached_GT.mbuf); |
| 434 |
if ( flt->cached_GT.nbuf<=0 ) return -1; |
| 435 |
if ( !flt->cached_GT.mask ) |
| 436 |
{ |
| 437 |
flt->cached_GT.mask = (uint64_t*) malloc(sizeof(*flt->cached_GT.mask)*flt->nsamples); |
| 438 |
if ( !flt->cached_GT.mask ) error("Could not alloc %zu bytes\n",sizeof(*flt->cached_GT.mask)*flt->nsamples); |
| 439 |
} |
| 440 |
int i,j, ngt1 = flt->cached_GT.nbuf / line->n_sample; |
| 441 |
for (i=0; i<line->n_sample; i++) |
| 442 |
{ |
| 443 |
int32_t *ptr = flt->cached_GT.buf + i*ngt1; |
| 444 |
flt->cached_GT.mask[i] = 0; |
| 445 |
for (j=0; j<ngt1; j++) |
| 446 |
{ |
| 447 |
if ( bcf_gt_is_missing(ptr[j]) ) continue; |
| 448 |
if ( ptr[j]==bcf_int32_vector_end ) break; |
| 449 |
int allele = bcf_gt_allele(ptr[j]); |
| 450 |
if ( allele > 63 ) |
| 451 |
{ |
| 452 |
static int warned = 0; |
| 453 |
if ( !warned ) |
| 454 |
{ |
| 455 |
fprintf(bcftools_stderr,"Too many alleles, skipping GT filtering at this site %s:%"PRId64". " |
| 456 |
"(This warning is printed only once.)\n", bcf_seqname(flt->hdr,line),line->pos+1); |
| 457 |
warned = 1; |
| 458 |
} |
| 459 |
flt->cached_GT.nbuf = 0; |
| 460 |
return -1; |
| 461 |
} |
| 462 |
flt->cached_GT.mask[i] |= 1<<allele; |
| 463 |
} |
| 464 |
} |
| 465 |
return 0; |
| 466 |
} |
| 467 |
|
| 468 |
static void filters_set_qual(filter_t *flt, bcf1_t *line, token_t *tok) |
| 469 |
{ |
| 470 |
float *ptr = &line->qual; |
| 471 |
if ( bcf_float_is_missing(*ptr) ) |
| 472 |
bcf_double_set_missing(tok->values[0]); |
| 473 |
else |
| 474 |
tok->values[0] = (double)line->qual; |
| 475 |
tok->nvalues = 1; |
| 476 |
} |
| 477 |
static void filters_set_type(filter_t *flt, bcf1_t *line, token_t *tok) |
| 478 |
{ |
| 479 |
tok->values[0] = bcf_get_variant_types(line); |
| 480 |
if ( !tok->values[0] ) tok->values[0] = 1; |
| 481 |
else tok->values[0] = ((int)tok->values[0]) << 1; |
| 482 |
tok->nvalues = 1; |
| 483 |
} |
| 484 |
static void filters_set_info(filter_t *flt, bcf1_t *line, token_t *tok) |
| 485 |
{ |
| 486 |
assert( tok->hdr_id >=0 ); |
| 487 |
int i; |
| 488 |
for (i=0; i<line->n_info; i++) |
| 489 |
if ( line->d.info[i].key == tok->hdr_id ) break; |
| 490 |
|
| 491 |
if ( i==line->n_info ) |
| 492 |
tok->nvalues = tok->str_value.l = 0; |
| 493 |
else if ( line->d.info[i].type==BCF_BT_CHAR ) |
| 494 |
{ |
| 495 |
int n = line->d.info[i].len; |
| 496 |
if ( n >= tok->str_value.m ) |
| 497 |
{ |
| 498 |
tok->str_value.m = n + 1; |
| 499 |
tok->str_value.s = (char*) realloc(tok->str_value.s, tok->str_value.m); |
| 500 |
if ( !tok->str_value.s ) error("Failed to alloc %d bytes\n", (int)tok->str_value.m); |
| 501 |
} |
| 502 |
memcpy(tok->str_value.s, line->d.info[i].vptr, n); |
| 503 |
tok->str_value.s[n] = 0; |
| 504 |
tok->nvalues = tok->str_value.l = n; |
| 505 |
} |
| 506 |
else if ( line->d.info[i].type==BCF_BT_FLOAT ) |
| 507 |
{ |
| 508 |
if ( bcf_float_is_missing(line->d.info[i].v1.f) ) tok->nvalues = 0; |
| 509 |
else |
| 510 |
{ |
| 511 |
tok->values[0] = line->d.info[i].v1.f; |
| 512 |
tok->nvalues = 1; |
| 513 |
} |
| 514 |
tok->str_value.l = 0; |
| 515 |
} |
| 516 |
else |
| 517 |
{ |
| 518 |
tok->str_value.l = 0; |
| 519 |
if ( line->d.info[i].type==BCF_BT_INT8 && line->d.info[i].v1.i==bcf_int8_missing ) tok->nvalues = 0; |
| 520 |
else if ( line->d.info[i].type==BCF_BT_INT16 && line->d.info[i].v1.i==bcf_int16_missing ) tok->nvalues = 0; |
| 521 |
else if ( line->d.info[i].type==BCF_BT_INT32 && line->d.info[i].v1.i==bcf_int32_missing ) tok->nvalues = 0; |
| 522 |
else |
| 523 |
{ |
| 524 |
tok->values[0] = line->d.info[i].v1.i; |
| 525 |
tok->nvalues = 1; |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
static void filters_cmp_bit_and(token_t *atok, token_t *btok, token_t *rtok, bcf1_t *line) |
| 530 |
{ |
| 531 |
int a = (int)(atok->nvalues?atok->values[0]:atok->threshold); |
| 532 |
int b = (int)(btok->nvalues?btok->values[0]:btok->threshold); |
| 533 |
if ( rtok->tok_type==TOK_LIKE ) |
| 534 |
rtok->pass_site = a&b ? 1 : 0; |
| 535 |
else |
| 536 |
rtok->pass_site = a&b ? 0 : 1; |
| 537 |
} |
| 538 |
static void filters_cmp_filter(token_t *atok, token_t *btok, token_t *rtok, bcf1_t *line) |
| 539 |
{ |
| 540 |
|
| 541 |
int i,j; |
| 542 |
if ( rtok->tok_type==TOK_NOT_IN ) |
| 543 |
{ |
| 544 |
if ( !btok->nvalues ) |
| 545 |
{ |
| 546 |
if ( line->d.n_flt ) rtok->pass_site = 1; |
| 547 |
return; |
| 548 |
} |
| 549 |
if ( !line->d.n_flt ) |
| 550 |
{ |
| 551 |
rtok->pass_site = 1; |
| 552 |
return; |
| 553 |
} |
| 554 |
for (j=0; j<btok->nvalues; j++) |
| 555 |
{ |
| 556 |
for (i=0; i<line->d.n_flt; i++) |
| 557 |
if ( btok->values[j]==line->d.flt[i] ) break; |
| 558 |
if ( i==line->d.n_flt ) break; |
| 559 |
} |
| 560 |
if ( j!=btok->nvalues ) rtok->pass_site = 1; |
| 561 |
return; |
| 562 |
} |
| 563 |
else if ( rtok->tok_type==TOK_IN ) |
| 564 |
{ |
| 565 |
if ( !btok->nvalues ) |
| 566 |
{ |
| 567 |
if ( !line->d.n_flt ) rtok->pass_site = 1; |
| 568 |
return; |
| 569 |
} |
| 570 |
if ( !line->d.n_flt ) return; |
| 571 |
for (j=0; j<btok->nvalues; j++) |
| 572 |
{ |
| 573 |
for (i=0; i<line->d.n_flt; i++) |
| 574 |
if ( btok->values[j]==line->d.flt[i] ) break; |
| 575 |
if ( i==line->d.n_flt ) break; |
| 576 |
} |
| 577 |
if ( j==btok->nvalues ) rtok->pass_site = 1; |
| 578 |
return; |
| 579 |
} |
| 580 |
else if ( rtok->tok_type==TOK_NE ) |
| 581 |
{ |
| 582 |
if ( btok->nvalues != line->d.n_flt ) |
| 583 |
{ |
| 584 |
rtok->pass_site = 1; |
| 585 |
return; |
| 586 |
} |
| 587 |
if ( !btok->nvalues ) return; |
| 588 |
for (j=0; j<btok->nvalues; j++) |
| 589 |
{ |
| 590 |
for (i=0; i<line->d.n_flt; i++) |
| 591 |
if ( btok->values[j]==line->d.flt[i] ) break; |
| 592 |
if ( i==line->d.n_flt ) break; |
| 593 |
} |
| 594 |
if ( j!=btok->nvalues ) rtok->pass_site = 1; |
| 595 |
return; |
| 596 |
} |
| 597 |
else if ( rtok->tok_type==TOK_EQ ) |
| 598 |
{ |
| 599 |
if ( btok->nvalues != line->d.n_flt ) return; |
| 600 |
if ( !btok->nvalues ) |
| 601 |
{ |
| 602 |
rtok->pass_site = 1; |
| 603 |
return; |
| 604 |
} |
| 605 |
for (j=0; j<btok->nvalues; j++) |
| 606 |
{ |
| 607 |
for (i=0; i<line->d.n_flt; i++) |
| 608 |
if ( btok->values[j]==line->d.flt[i] ) break; |
| 609 |
if ( i==line->d.n_flt ) break; |
| 610 |
} |
| 611 |
if ( j==btok->nvalues ) rtok->pass_site = 1; |
| 612 |
return; |
| 613 |
} |
| 614 |
else |
| 615 |
error("Only ==, !=, ~, and !~ operators are supported for FILTER\n"); |
| 616 |
return; |
| 617 |
} |
| 618 |
static void filters_cmp_string_hash(token_t *atok, token_t *btok, token_t *rtok, bcf1_t *line) |
| 619 |
{ |
| 620 |
if ( btok->hash ) |
| 621 |
{ |
| 622 |
token_t *tmp = atok; atok = btok; btok = tmp; |
| 623 |
} |
| 624 |
if ( rtok->tok_type!=TOK_EQ && rtok->tok_type!=TOK_NE ) |
| 625 |
error("Only == and != operators are supported for strings read from a file\n"); |
| 626 |
|
| 627 |
|
| 628 |
if ( !btok->nsamples ) |
| 629 |
{ |
| 630 |
|
| 631 |
if ( btok->idx >= 0 ) |
| 632 |
{ |
| 633 |
int ret = btok->str_value.s ? khash_str2int_has_key(atok->hash, btok->str_value.s) : 0; |
| 634 |
if ( rtok->tok_type==TOK_NE ) ret = ret ? 0 : 1; |
| 635 |
rtok->pass_site = ret; |
| 636 |
return; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
int ret = 0; |
| 641 |
char *ptr = btok->str_value.s; |
| 642 |
while ( *ptr ) |
| 643 |
{ |
| 644 |
char *eptr = ptr + 1; |
| 645 |
while ( *eptr && *eptr!=',' ) eptr++; |
| 646 |
char keep = *eptr; |
| 647 |
*eptr = 0; |
| 648 |
ret |= khash_str2int_has_key(atok->hash, ptr); |
| 649 |
*eptr = keep; |
| 650 |
if ( !keep ) break; |
| 651 |
ptr = eptr + 1; |
| 652 |
} |
| 653 |
if ( rtok->tok_type==TOK_NE ) ret = ret ? 0 : 1; |
| 654 |
rtok->pass_site = ret; |
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
tok_init_samples(atok, btok, rtok); |
| 661 |
rtok->pass_site = 0; |
| 662 |
int i; |
| 663 |
|
| 664 |
|
| 665 |
if ( btok->idx >= 0 ) |
| 666 |
{ |
| 667 |
for (i=0; i<btok->nsamples; i++) |
| 668 |
{ |
| 669 |
if ( !rtok->usmpl[i] ) continue; |
| 670 |
char *str = btok->str_value.s + i*btok->nval1; |
| 671 |
char keep = str[btok->nval1]; |
| 672 |
str[btok->nval1] = 0; |
| 673 |
int ret = khash_str2int_has_key(atok->hash, str); |
| 674 |
str[btok->nval1] = keep; |
| 675 |
if ( rtok->tok_type==TOK_NE ) ret = ret ? 0 : 1; |
| 676 |
rtok->pass_samples[i] = ret; |
| 677 |
rtok->pass_site |= ret; |
| 678 |
} |
| 679 |
return; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
for (i=0; i<btok->nsamples; i++) |
| 684 |
{ |
| 685 |
if ( !rtok->usmpl[i] ) continue; |
| 686 |
char *str = btok->str_value.s + i*btok->nval1; |
| 687 |
char keep = str[btok->nval1]; |
| 688 |
str[btok->nval1] = 0; |
| 689 |
|
| 690 |
|
| 691 |
int ret = 0; |
| 692 |
char *ptr = str; |
| 693 |
while ( *ptr ) |
| 694 |
{ |
| 695 |
char *eptr = ptr + 1; |
| 696 |
while ( *eptr && *eptr!=',' ) eptr++; |
| 697 |
char keep0 = *eptr; |
| 698 |
*eptr = 0; |
| 699 |
ret |= khash_str2int_has_key(atok->hash, ptr); |
| 700 |
*eptr = keep0; |
| 701 |
if ( !keep0 ) break; |
| 702 |
ptr = eptr + 1; |
| 703 |
} |
| 704 |
str[btok->nval1] = keep; |
| 705 |
if ( rtok->tok_type==TOK_NE ) ret = ret ? 0 : 1; |
| 706 |
rtok->pass_samples[i] = ret; |
| 707 |
rtok->pass_site |= ret; |
| 708 |
} |
| 709 |
} |
| 710 |
static void filters_cmp_id(token_t *atok, token_t *btok, token_t *rtok, bcf1_t *line) |
| 711 |
{ |
| 712 |
if ( btok->hash ) |
| 713 |
{ |
| 714 |
token_t *tmp = atok; atok = btok; btok = tmp; |
| 715 |
} |
| 716 |
|
| 717 |
char *id = line->d.id; |
| 718 |
int pass = 0; |
| 719 |
|
| 720 |
while ( id ) |
| 721 |
{ |
| 722 |
char *ep = strchr(id,';'); |
| 723 |
if ( ep ) *ep = 0; |
| 724 |
|
| 725 |
if ( atok->hash ) |
| 726 |
{ |
| 727 |
if ( rtok->tok_type!=TOK_EQ && rtok->tok_type!=TOK_NE ) |
| 728 |
error("Only == and != operators are supported for strings read from a file\n"); |
| 729 |
|
| 730 |
pass = khash_str2int_has_key(atok->hash, id); |
| 731 |
} |
| 732 |
else |
| 733 |
{ |
| 734 |
if ( !btok->str_value.l ) error("Error occurred while evaluating the expression\n"); |
| 735 |
|
| 736 |
if ( rtok->tok_type==TOK_EQ || rtok->tok_type==TOK_NE ) |
| 737 |
pass = strcmp(btok->str_value.s,id) ? 0 : 1; |
| 738 |
else |
| 739 |
{ |
| 740 |
if ( rtok->tok_type!=TOK_LIKE && rtok->tok_type!=TOK_NLIKE ) |
| 741 |
error("Only the following operators are supported for querying ID: ==, !=, ~, !~; the operator type %d is not supported (%p %p)\n", |
| 742 |
rtok->tok_type,atok->regex,btok->regex); |
| 743 |
|
| 744 |
regex_t *regex = atok->regex ? atok->regex : (btok->regex ? btok->regex : NULL); |
| 745 |
if ( !regex ) error("fixme: regex initialization failed\n"); |
| 746 |
pass = regexec(regex,id, 0,NULL,0) ? 0 : 1; |
| 747 |
} |
| 748 |
} |
| 749 |
if ( ep ) |
| 750 |
{ |
| 751 |
*ep = ';'; |
| 752 |
id = ep + 1; |
| 753 |
} |
| 754 |
if ( pass || !ep ) break; |
| 755 |
} |
| 756 |
if ( rtok->tok_type==TOK_NE || rtok->tok_type==TOK_NE) pass = pass ? 0 : 1; |
| 757 |
rtok->pass_site = pass; |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
static int bcf_get_info_value(bcf1_t *line, int info_id, int ivec, void *value) |
| 772 |
{ |
| 773 |
int j; |
| 774 |
for (j=0; j<line->n_info; j++) |
| 775 |
if ( line->d.info[j].key == info_id ) break; |
| 776 |
if ( j==line->n_info ) return -1; |
| 777 |
|
| 778 |
bcf_info_t *info = &line->d.info[j]; |
| 779 |
if ( info->len == 1 ) |
| 780 |
{ |
| 781 |
if ( info->type==BCF_BT_FLOAT ) *((double*)value) = info->v1.f; |
| 782 |
else if ( info->type==BCF_BT_INT8 || info->type==BCF_BT_INT16 || info->type==BCF_BT_INT32 ) *((int64_t*)value) = info->v1.i; |
| 783 |
return 1; |
| 784 |
} |
| 785 |
|
| 786 |
if ( ivec<0 ) ivec = 0; |
| 787 |
if ( ivec>=info->len) return 0; |
| 788 |
|
| 789 |
#define BRANCH(type_t, convert, is_missing, is_vector_end, out_type_t) { \ |
| 790 |
uint8_t *p = info->vptr; \ |
| 791 |
for (j=0; j<ivec; j++) \ |
| 792 |
{ \ |
| 793 |
type_t val = convert(&p[j * sizeof(type_t)]); \ |
| 794 |
if ( is_vector_end ) return 0; \ |
| 795 |
} \ |
| 796 |
type_t val = convert(&p[ivec * sizeof(type_t)]); \ |
| 797 |
if ( is_missing ) return 0; \ |
| 798 |
*((out_type_t*)value) = val; \ |
| 799 |
return 1; \ |
| 800 |
} |
| 801 |
switch (info->type) { |
| 802 |
case BCF_BT_INT8: BRANCH(int8_t, le_to_i8, val==bcf_int8_missing, val==bcf_int8_vector_end, int64_t); break; |
| 803 |
case BCF_BT_INT16: BRANCH(int16_t, le_to_i16, val==bcf_int16_missing, val==bcf_int16_vector_end, int64_t); break; |
| 804 |
case BCF_BT_INT32: BRANCH(int32_t, le_to_i32, val==bcf_int32_missing, val==bcf_int32_vector_end, int64_t); break; |
| 805 |
case BCF_BT_FLOAT: BRANCH(float, le_to_float, bcf_float_is_missing(val), bcf_float_is_vector_end(val), double); break; |
| 806 |
default: fprintf(bcftools_stderr,"todo: type %d\n", info->type); bcftools_exit(1); break; |
| 807 |
} |
| 808 |
#undef BRANCH |
| 809 |
return -1; |
| 810 |
} |
| 811 |
|
| 812 |
static void filters_set_chrom(filter_t *flt, bcf1_t *line, token_t *tok) |
| 813 |
{ |
| 814 |
tok->str_value.l = 0; |
| 815 |
kputs(bcf_seqname(flt->hdr,line), &tok->str_value); |
| 816 |
tok->nvalues = tok->str_value.l; |
| 817 |
tok->is_str = 1; |
| 818 |
} |
| 819 |
|
| 820 |
static void filters_set_pos(filter_t *flt, bcf1_t *line, token_t *tok) |
| 821 |
{ |
| 822 |
tok->values[0] = line->pos+1; |
| 823 |
tok->nvalues = 1; |
| 824 |
} |
| 825 |
|
| 826 |
static void filters_set_info_int(filter_t *flt, bcf1_t *line, token_t *tok) |
| 827 |
{ |
| 828 |
if ( tok->idx==-2 ) |
| 829 |
{ |
| 830 |
tok->nvalues = bcf_get_info_int32(flt->hdr,line,tok->tag,&flt->tmpi,&flt->mtmpi); |
| 831 |
if ( tok->nvalues<=0 ) tok->nvalues = 0; |
| 832 |
else |
| 833 |
{ |
| 834 |
hts_expand(double,tok->nvalues,tok->mvalues,tok->values); |
| 835 |
int i, j = 0, end = tok->idxs[tok->nidxs-1] < 0 ? tok->nvalues - 1 : tok->nidxs - 1; |
| 836 |
if ( end >= tok->nvalues ) end = tok->nvalues - 1; |
| 837 |
for (i=0; i<=end; i++) |
| 838 |
if ( i>=tok->nidxs || tok->idxs[i] ) tok->values[j++] = flt->tmpi[i]; |
| 839 |
tok->nvalues = j; |
| 840 |
} |
| 841 |
} |
| 842 |
else |
| 843 |
{ |
| 844 |
int64_t value = 0; |
| 845 |
if ( bcf_get_info_value(line,tok->hdr_id,tok->idx,&value) <= 0 ) |
| 846 |
tok->nvalues = 0; |
| 847 |
else |
| 848 |
{ |
| 849 |
tok->values[0] = value; |
| 850 |
tok->nvalues = 1; |
| 851 |
} |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
static void filters_set_info_float(filter_t *flt, bcf1_t *line, token_t *tok) |
| 856 |
{ |
| 857 |
if ( tok->idx==-2 ) |
| 858 |
{ |
| 859 |
tok->nvalues = bcf_get_info_float(flt->hdr,line,tok->tag,&flt->tmpf,&flt->mtmpf); |
| 860 |
if ( tok->nvalues<=0 ) tok->nvalues = 0; |
| 861 |
else |
| 862 |
{ |
| 863 |
hts_expand(double,tok->nvalues,tok->mvalues,tok->values); |
| 864 |
int i, j = 0, end = tok->idxs[tok->nidxs-1] < 0 ? tok->nvalues - 1 : tok->nidxs - 1; |
| 865 |
if ( end >= tok->nvalues ) end = tok->nvalues - 1; |
| 866 |
for (i=0; i<=end; i++) |
| 867 |
if ( i>=tok->nidxs || tok->idxs[i] ) |
| 868 |
{ |
| 869 |
if ( bcf_float_is_missing(flt->tmpf[i]) ) bcf_double_set_missing(tok->values[j]); |
| 870 |
else tok->values[j] = flt->tmpf[i]; |
| 871 |
j++; |
| 872 |
} |
| 873 |
tok->nvalues = j; |
| 874 |
} |
| 875 |
} |
| 876 |
else |
| 877 |
{ |
| 878 |
double value; |
| 879 |
if ( bcf_get_info_value(line,tok->hdr_id,tok->idx,&value) <= 0 ) |
| 880 |
tok->nvalues = 0; |
| 881 |
else |
| 882 |
{ |
| 883 |
tok->values[0] = value; |
| 884 |
tok->nvalues = 1; |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
static void filters_set_info_string(filter_t *flt, bcf1_t *line, token_t *tok) |
| 890 |
{ |
| 891 |
int32_t m = tok->str_value.m; |
| 892 |
int n = bcf_get_info_string(flt->hdr,line,tok->tag,&tok->str_value.s,&m); |
| 893 |
tok->str_value.m = m; |
| 894 |
if ( n<0 ) { tok->nvalues = tok->str_value.l = 0; return; } |
| 895 |
|
| 896 |
if ( tok->idx>=0 ) |
| 897 |
{ |
| 898 |
|
| 899 |
int i = 0; |
| 900 |
char *ss = tok->str_value.s, *se = tok->str_value.s + n; |
| 901 |
while ( ss<se && i<tok->idx ) |
| 902 |
{ |
| 903 |
if ( *ss==',' ) i++; |
| 904 |
ss++; |
| 905 |
} |
| 906 |
if ( ss==se || i!=tok->idx ) { tok->nvalues = tok->str_value.l = 0; return; } |
| 907 |
se = ss; |
| 908 |
while ( se - tok->str_value.s < n && *se!=',' ) se++; |
| 909 |
if ( ss==tok->str_value.s ) *se = 0; |
| 910 |
else |
| 911 |
{ |
| 912 |
memmove(tok->str_value.s, ss, se-ss); |
| 913 |
tok->str_value.s[se-ss] = 0; |
| 914 |
} |
| 915 |
tok->str_value.l = se - ss; |
| 916 |
} |
| 917 |
else if ( tok->idx==-2 && tok->idxs[0]==-1 ) |
| 918 |
tok->str_value.l = n; |
| 919 |
else if ( tok->idx==-2 ) |
| 920 |
{ |
| 921 |
flt->tmps.l = 0; |
| 922 |
ks_resize(&flt->tmps, n); |
| 923 |
int i, iend = tok->idxs[tok->nidxs-1] < 0 ? n - 1 : tok->nidxs - 1; |
| 924 |
if ( iend >= n ) iend = n - 1; |
| 925 |
char *beg = tok->str_value.s, *dst = flt->tmps.s; |
| 926 |
for (i=0; i<=iend; i++) |
| 927 |
{ |
| 928 |
char *end = beg; |
| 929 |
while ( *end && *end!=',' ) end++; |
| 930 |
if ( i>=tok->nidxs || tok->idxs[i] ) |
| 931 |
{ |
| 932 |
memcpy(dst, beg, end - beg); |
| 933 |
dst += end - beg; |
| 934 |
dst[0] = ','; |
| 935 |
dst++; |
| 936 |
} |
| 937 |
beg = end+1; |
| 938 |
} |
| 939 |
dst[0] = 0; |
| 940 |
tok->str_value.l = dst - flt->tmps.s; |
| 941 |
|
| 942 |
#define SWAP(type_t, a, b) { type_t t = a; a = b; b = t; } |
| 943 |
SWAP(char *, flt->tmps.s, tok->str_value.s); |
| 944 |
SWAP(size_t, flt->tmps.m, tok->str_value.m); |
| 945 |
} |
| 946 |
tok->nvalues = tok->str_value.l; |
| 947 |
} |
| 948 |
|
| 949 |
static void filters_set_info_flag(filter_t *flt, bcf1_t *line, token_t *tok) |
| 950 |
{ |
| 951 |
int j; |
| 952 |
for (j=0; j<line->n_info; j++) |
| 953 |
if ( line->d.info[j].key == tok->hdr_id ) break; |
| 954 |
tok->values[0] = j==line->n_info ? 0 : 1; |
| 955 |
tok->nvalues = 1; |
| 956 |
} |
| 957 |
|
| 958 |
static void filters_set_format_int(filter_t *flt, bcf1_t *line, token_t *tok) |
| 959 |
{ |
| 960 |
if ( line->n_sample != tok->nsamples ) |
| 961 |
error("Incorrect number of FORMAT fields at %s:%"PRId64" .. %s, %d vs %d\n", bcf_seqname(flt->hdr,line),(int64_t) line->pos+1,tok->tag,line->n_sample,tok->nsamples); |
| 962 |
|
| 963 |
int nvals; |
| 964 |
if ( (nvals=bcf_get_format_int32(flt->hdr,line,tok->tag,&flt->tmpi,&flt->mtmpi))<0 ) |
| 965 |
{ |
| 966 |
tok->nvalues = 0; |
| 967 |
return; |
| 968 |
} |
| 969 |
int i, nsrc1 = nvals / tok->nsamples; |
| 970 |
tok->nval1 = tok->idx >= 0 ? 1 : (tok->nuidxs ? tok->nuidxs : nsrc1); |
| 971 |
tok->nvalues = tok->nval1*tok->nsamples; |
| 972 |
hts_expand(double, tok->nvalues, tok->mvalues, tok->values); |
| 973 |
|
| 974 |
if ( tok->idx >= 0 ) |
| 975 |
{ |
| 976 |
for (i=0; i<tok->nsamples; i++) |
| 977 |
{ |
| 978 |
if ( !tok->usmpl[i] ) continue; |
| 979 |
int32_t *ptr = flt->tmpi + i*nsrc1; |
| 980 |
if ( tok->idx>=nsrc1 || ptr[tok->idx]==bcf_int32_missing ) |
| 981 |
bcf_double_set_missing(tok->values[i]); |
| 982 |
else if ( ptr[tok->idx]==bcf_int32_vector_end ) |
| 983 |
bcf_double_set_vector_end(tok->values[i]); |
| 984 |
else |
| 985 |
tok->values[i] = ptr[tok->idx]; |
| 986 |
} |
| 987 |
} |
| 988 |
else if ( tok->idx==-3 ) |
| 989 |
{ |
| 990 |
if ( filters_cache_genotypes(flt,line)!=0 ) |
| 991 |
{ |
| 992 |
tok->nvalues = 0; |
| 993 |
return; |
| 994 |
} |
| 995 |
for (i=0; i<tok->nsamples; i++) |
| 996 |
{ |
| 997 |
if ( !tok->usmpl[i] ) continue; |
| 998 |
int32_t *src = flt->tmpi + i*nsrc1; |
| 999 |
double *dst = tok->values + i*tok->nval1; |
| 1000 |
int k, j = 0; |
| 1001 |
for (k=0; k<nsrc1; k++) |
| 1002 |
{ |
| 1003 |
if ( !(flt->cached_GT.mask[i] & (1<<k)) ) continue; |
| 1004 |
dst[j++] = src[k]; |
| 1005 |
} |
| 1006 |
if ( !j ) { bcf_double_set_missing(dst[j]); j++; } |
| 1007 |
for (; j<tok->nval1; j++) bcf_double_set_vector_end(dst[j]); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
else |
| 1011 |
{ |
| 1012 |
int kend = tok->idxs[tok->nidxs-1] < 0 ? tok->nval1 : tok->nidxs; |
| 1013 |
for (i=0; i<tok->nsamples; i++) |
| 1014 |
{ |
| 1015 |
if ( !tok->usmpl[i] ) continue; |
| 1016 |
int32_t *src = flt->tmpi + i*nsrc1; |
| 1017 |
double *dst = tok->values + i*tok->nval1; |
| 1018 |
int k, j = 0; |
| 1019 |
for (k=0; k<kend; k++) |
| 1020 |
{ |
| 1021 |
if ( k<tok->nidxs && !tok->idxs[k] ) continue; |
| 1022 |
if ( src[k]==bcf_int32_missing ) |
| 1023 |
bcf_double_set_missing(dst[j]); |
| 1024 |
else if ( src[k]==bcf_int32_vector_end ) |
| 1025 |
bcf_double_set_vector_end(dst[j]); |
| 1026 |
else |
| 1027 |
dst[j] = src[k]; |
| 1028 |
j++; |
| 1029 |
} |
| 1030 |
if ( j==0 ) |
| 1031 |
{ |
| 1032 |
bcf_double_set_missing(dst[j]); |
| 1033 |
j++; |
| 1034 |
} |
| 1035 |
while (j < tok->nval1) |
| 1036 |
{ |
| 1037 |
bcf_double_set_vector_end(dst[j]); |
| 1038 |
j++; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
static void filters_set_format_float(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1044 |
{ |
| 1045 |
if ( line->n_sample != tok->nsamples ) |
| 1046 |
error("Incorrect number of FORMAT fields at %s:%"PRId64" .. %s, %d vs %d\n", bcf_seqname(flt->hdr,line),(int64_t) line->pos+1,tok->tag,line->n_sample,tok->nsamples); |
| 1047 |
|
| 1048 |
int nvals; |
| 1049 |
if ( (nvals=bcf_get_format_float(flt->hdr,line,tok->tag,&flt->tmpf,&flt->mtmpf))<0 ) |
| 1050 |
{ |
| 1051 |
tok->nvalues = 0; |
| 1052 |
return; |
| 1053 |
} |
| 1054 |
int i, nsrc1 = nvals / tok->nsamples; |
| 1055 |
tok->nval1 = tok->idx >= 0 ? 1 : (tok->nuidxs ? tok->nuidxs : nsrc1); |
| 1056 |
tok->nvalues = tok->nval1*tok->nsamples; |
| 1057 |
hts_expand(double, tok->nvalues, tok->mvalues, tok->values); |
| 1058 |
|
| 1059 |
if ( tok->idx >= 0 ) |
| 1060 |
{ |
| 1061 |
for (i=0; i<tok->nsamples; i++) |
| 1062 |
{ |
| 1063 |
if ( !tok->usmpl[i] ) continue; |
| 1064 |
float *ptr = flt->tmpf + i*nsrc1; |
| 1065 |
if ( tok->idx>=nsrc1 || bcf_float_is_missing(ptr[tok->idx]) ) |
| 1066 |
bcf_double_set_missing(tok->values[i]); |
| 1067 |
else if ( bcf_float_is_vector_end(ptr[tok->idx]) ) |
| 1068 |
bcf_double_set_vector_end(tok->values[i]); |
| 1069 |
else |
| 1070 |
tok->values[i] = ptr[tok->idx]; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
else if ( tok->idx==-3 ) |
| 1074 |
{ |
| 1075 |
if ( filters_cache_genotypes(flt,line)!=0 ) |
| 1076 |
{ |
| 1077 |
tok->nvalues = 0; |
| 1078 |
return; |
| 1079 |
} |
| 1080 |
for (i=0; i<tok->nsamples; i++) |
| 1081 |
{ |
| 1082 |
if ( !tok->usmpl[i] ) continue; |
| 1083 |
float *src = flt->tmpf + i*nsrc1; |
| 1084 |
double *dst = tok->values + i*tok->nval1; |
| 1085 |
int k, j = 0; |
| 1086 |
for (k=0; k<nsrc1; k++) |
| 1087 |
{ |
| 1088 |
if ( !(flt->cached_GT.mask[i] & (1<<k)) ) continue; |
| 1089 |
if ( bcf_float_is_missing(src[k]) ) |
| 1090 |
bcf_double_set_missing(dst[j]); |
| 1091 |
else if ( bcf_float_is_vector_end(src[k]) ) |
| 1092 |
bcf_double_set_vector_end(dst[j]); |
| 1093 |
else |
| 1094 |
dst[j] = src[k]; |
| 1095 |
j++; |
| 1096 |
} |
| 1097 |
if ( !j ) { bcf_double_set_missing(dst[j]); j++; } |
| 1098 |
for (; j<tok->nval1; j++) bcf_double_set_vector_end(dst[j]); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
else |
| 1102 |
{ |
| 1103 |
int kend = tok->idxs[tok->nidxs-1] < 0 ? tok->nval1 : tok->nidxs; |
| 1104 |
for (i=0; i<tok->nsamples; i++) |
| 1105 |
{ |
| 1106 |
if ( !tok->usmpl[i] ) continue; |
| 1107 |
float *src = flt->tmpf + i*nsrc1; |
| 1108 |
double *dst = tok->values + i*tok->nval1; |
| 1109 |
int k, j = 0; |
| 1110 |
for (k=0; k<kend; k++) |
| 1111 |
{ |
| 1112 |
if ( k<tok->nidxs && !tok->idxs[k] ) continue; |
| 1113 |
if ( bcf_float_is_missing(src[k]) ) |
| 1114 |
bcf_double_set_missing(dst[j]); |
| 1115 |
else if ( bcf_float_is_vector_end(src[k]) ) |
| 1116 |
bcf_double_set_vector_end(dst[j]); |
| 1117 |
else |
| 1118 |
dst[j] = src[k]; |
| 1119 |
j++; |
| 1120 |
} |
| 1121 |
if ( j==0 ) |
| 1122 |
{ |
| 1123 |
bcf_double_set_missing(dst[j]); |
| 1124 |
j++; |
| 1125 |
} |
| 1126 |
while (j < tok->nval1) |
| 1127 |
{ |
| 1128 |
bcf_double_set_vector_end(dst[j]); |
| 1129 |
j++; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
} |
| 1134 |
static void filters_set_format_string(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1135 |
{ |
| 1136 |
if ( line->n_sample != tok->nsamples ) |
| 1137 |
error("Incorrect number of FORMAT fields at %s:%"PRId64" .. %s, %d vs %d\n", bcf_seqname(flt->hdr,line),(int64_t) line->pos+1,tok->tag,line->n_sample,tok->nsamples); |
| 1138 |
|
| 1139 |
int i, ndim = tok->str_value.m; |
| 1140 |
int nstr = bcf_get_format_char(flt->hdr, line, tok->tag, &tok->str_value.s, &ndim); |
| 1141 |
tok->str_value.m = tok->str_value.l = ndim; |
| 1142 |
kputc(0,&tok->str_value); |
| 1143 |
tok->str_value.l = tok->nvalues = 0; |
| 1144 |
|
| 1145 |
if ( nstr<0 ) return; |
| 1146 |
|
| 1147 |
if ( tok->idx==-3 && filters_cache_genotypes(flt,line)!=0 ) return; |
| 1148 |
|
| 1149 |
tok->nvalues = tok->str_value.l = nstr; |
| 1150 |
tok->nval1 = nstr / tok->nsamples; |
| 1151 |
for (i=0; i<tok->nsamples; i++) |
| 1152 |
{ |
| 1153 |
if ( !tok->usmpl[i] ) continue; |
| 1154 |
char *src = tok->str_value.s + i*tok->nval1, *dst = src; |
| 1155 |
int ibeg = 0; |
| 1156 |
int idx = 0; |
| 1157 |
while ( ibeg < tok->nval1 ) |
| 1158 |
{ |
| 1159 |
int iend = ibeg; |
| 1160 |
while ( iend < tok->nval1 && src[iend] && src[iend]!=',' ) iend++; |
| 1161 |
|
| 1162 |
int keep = 0; |
| 1163 |
if ( tok->idx >= 0 ) |
| 1164 |
{ |
| 1165 |
if ( tok->idx==idx ) keep = 1; |
| 1166 |
} |
| 1167 |
else if ( tok->idx == -3 ) |
| 1168 |
{ |
| 1169 |
if ( flt->cached_GT.mask[i] & (1<<idx) ) keep = 1; |
| 1170 |
} |
| 1171 |
else |
| 1172 |
{ |
| 1173 |
if ( idx < tok->nidxs ) |
| 1174 |
{ |
| 1175 |
if ( tok->idxs[idx] != 0 ) keep = 1; |
| 1176 |
} |
| 1177 |
else if ( tok->idxs[tok->nidxs-1] < 0 ) |
| 1178 |
keep = 1; |
| 1179 |
} |
| 1180 |
if ( keep ) |
| 1181 |
{ |
| 1182 |
if ( ibeg!=0 ) memmove(dst, src+ibeg, iend-ibeg+1); |
| 1183 |
dst += iend - ibeg + 1; |
| 1184 |
if ( tok->idx>=0 ) break; |
| 1185 |
} |
| 1186 |
if ( !src[iend] ) break; |
| 1187 |
ibeg = iend + 1; |
| 1188 |
idx++; |
| 1189 |
} |
| 1190 |
if ( dst==src ) { dst[0] = '.'; dst+=2; } |
| 1191 |
if ( dst - src < tok->nval1 ) memset(dst-1, 0, tok->nval1 - (dst - src)); |
| 1192 |
} |
| 1193 |
} |
| 1194 |
static void _filters_set_genotype(filter_t *flt, bcf1_t *line, token_t *tok, int type) |
| 1195 |
{ |
| 1196 |
bcf_fmt_t *fmt = bcf_get_fmt(flt->hdr, line, "GT"); |
| 1197 |
if ( !fmt ) |
| 1198 |
{ |
| 1199 |
tok->nvalues = tok->str_value.l = 0; |
| 1200 |
return; |
| 1201 |
} |
| 1202 |
|
| 1203 |
int i,j, nsmpl = bcf_hdr_nsamples(flt->hdr), nvals1 = type==2 ? 3 : 4; |
| 1204 |
if ( tok->str_value.m <= nvals1*nsmpl ) |
| 1205 |
{ |
| 1206 |
tok->str_value.m = nvals1*nsmpl + 1; |
| 1207 |
tok->str_value.s = (char*)realloc(tok->str_value.s, tok->str_value.m); |
| 1208 |
} |
| 1209 |
|
| 1210 |
#define BRANCH_INT(type_t, convert, vector_end) \ |
| 1211 |
{ \ |
| 1212 |
for (i=0; i<line->n_sample; i++) \ |
| 1213 |
{ \ |
| 1214 |
uint8_t *ptr = fmt->p + i*fmt->size; \ |
| 1215 |
int is_het = 0, has_ref = 0, missing = 0, jal = 0; \ |
| 1216 |
for (j=0; j<fmt->n; j++) \ |
| 1217 |
{ \ |
| 1218 |
type_t ial = convert(&ptr[j * sizeof(type_t)]); \ |
| 1219 |
if ( ial==vector_end ) break; \ |
| 1220 |
if ( bcf_gt_is_missing(ial) ) { missing=1; break; } \ |
| 1221 |
if ( bcf_gt_allele(ial)==0 ) has_ref = 1; \ |
| 1222 |
if ( j>0 ) \ |
| 1223 |
{ \ |
| 1224 |
if ( bcf_gt_allele(ial)!=bcf_gt_allele(jal) ) is_het = 1; \ |
| 1225 |
} \ |
| 1226 |
jal = ial; \ |
| 1227 |
} \ |
| 1228 |
char *dst = &tok->str_value.s[nvals1*i]; \ |
| 1229 |
if ( type==4 ) \ |
| 1230 |
{ \ |
| 1231 |
if ( !j || missing ) dst[0]='m', dst[1]='i', dst[2]='s', dst[3] = 0; \ |
| 1232 |
else if ( !has_ref ) dst[0]='a', dst[1]='l', dst[2]='t', dst[3] = 0; \ |
| 1233 |
else if ( !is_het ) dst[0]='r', dst[1]='e', dst[2]='f', dst[3] = 0; \ |
| 1234 |
else dst[0]='a', dst[1]='l', dst[2]='t', dst[3] = 0; \ |
| 1235 |
} \ |
| 1236 |
else if ( !j || missing ) dst[0]='.', dst[1]=0; \ |
| 1237 |
else if ( type==3 ) \ |
| 1238 |
{ \ |
| 1239 |
if ( j==1 ) dst[0]='h', dst[1]='a', dst[2]='p', dst[3] = 0; \ |
| 1240 |
else if ( !is_het ) dst[0]='h', dst[1]='o', dst[2]='m', dst[3] = 0; \ |
| 1241 |
else dst[0]='h', dst[1]='e', dst[2]='t', dst[3] = 0; \ |
| 1242 |
} \ |
| 1243 |
else \ |
| 1244 |
{ \ |
| 1245 |
if ( j==1 ) \ |
| 1246 |
{ \ |
| 1247 |
if ( has_ref ) dst[0]='r', dst[1]=0; \ |
| 1248 |
else dst[0]='a', dst[1]=0; \ |
| 1249 |
} \ |
| 1250 |
else if ( !is_het ) \ |
| 1251 |
{ \ |
| 1252 |
if ( has_ref ) dst[0]='r', dst[1]='r', dst[2] = 0; \ |
| 1253 |
else dst[0]='a', dst[1]='a', dst[2] = 0; \ |
| 1254 |
} \ |
| 1255 |
else \ |
| 1256 |
{ \ |
| 1257 |
if ( has_ref ) dst[0]='r', dst[1]='a', dst[2] = 0; \ |
| 1258 |
else dst[0]='a', dst[1]='A', dst[2] = 0; \ |
| 1259 |
} \ |
| 1260 |
} \ |
| 1261 |
} \ |
| 1262 |
} |
| 1263 |
switch (fmt->type) { |
| 1264 |
case BCF_BT_INT8: BRANCH_INT(int8_t, le_to_i8, bcf_int8_vector_end); break; |
| 1265 |
case BCF_BT_INT16: BRANCH_INT(int16_t, le_to_i16, bcf_int16_vector_end); break; |
| 1266 |
case BCF_BT_INT32: BRANCH_INT(int32_t, le_to_i32, bcf_int32_vector_end); break; |
| 1267 |
default: error("The GT type is not lineognised: %d at %s:%"PRId64"\n",fmt->type, bcf_seqname(flt->hdr,line),(int64_t) line->pos+1); break; |
| 1268 |
} |
| 1269 |
#undef BRANCH_INT |
| 1270 |
assert( tok->nsamples == nsmpl ); |
| 1271 |
tok->nvalues = tok->str_value.l = nvals1*nsmpl; |
| 1272 |
tok->str_value.s[tok->str_value.l] = 0; |
| 1273 |
tok->nval1 = nvals1; |
| 1274 |
} |
| 1275 |
static void filters_set_genotype2(filter_t *flt, bcf1_t *line, token_t *tok) { _filters_set_genotype(flt, line, tok, 2); } |
| 1276 |
static void filters_set_genotype3(filter_t *flt, bcf1_t *line, token_t *tok) { _filters_set_genotype(flt, line, tok, 3); } |
| 1277 |
static void filters_set_genotype4(filter_t *flt, bcf1_t *line, token_t *tok) { _filters_set_genotype(flt, line, tok, 4); } |
| 1278 |
|
| 1279 |
static void filters_set_genotype_string(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1280 |
{ |
| 1281 |
bcf_fmt_t *fmt = bcf_get_fmt(flt->hdr, line, "GT"); |
| 1282 |
if ( !fmt ) |
| 1283 |
{ |
| 1284 |
tok->nvalues = 0; |
| 1285 |
return; |
| 1286 |
} |
| 1287 |
int i, blen = 4, nsmpl = line->n_sample; |
| 1288 |
|
| 1289 |
gt_length_too_big: |
| 1290 |
tok->str_value.l = 0; |
| 1291 |
for (i=0; i<nsmpl; i++) |
| 1292 |
{ |
| 1293 |
size_t plen = tok->str_value.l; |
| 1294 |
bcf_format_gt(fmt, i, &tok->str_value); |
| 1295 |
kputc_(0, &tok->str_value); |
| 1296 |
if ( tok->str_value.l - plen > blen ) |
| 1297 |
{ |
| 1298 |
|
| 1299 |
|
| 1300 |
blen *= 2; |
| 1301 |
goto gt_length_too_big; |
| 1302 |
} |
| 1303 |
|
| 1304 |
plen = tok->str_value.l - plen; |
| 1305 |
while ( plen < blen ) |
| 1306 |
{ |
| 1307 |
kputc_(0, &tok->str_value); |
| 1308 |
plen++; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
assert( tok->nsamples == nsmpl ); |
| 1312 |
tok->nvalues = tok->str_value.l; |
| 1313 |
tok->nval1 = blen; |
| 1314 |
} |
| 1315 |
static void filters_set_ilen(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1316 |
{ |
| 1317 |
tok->nvalues = line->n_allele - 1; |
| 1318 |
hts_expand(double,tok->nvalues,tok->mvalues,tok->values); |
| 1319 |
|
| 1320 |
int i, rlen = strlen(line->d.allele[0]); |
| 1321 |
for (i=1; i<line->n_allele; i++) |
| 1322 |
{ |
| 1323 |
if ( line->d.allele[i][0]=='<' ) |
| 1324 |
{ |
| 1325 |
bcf_double_set_missing(tok->values[i-1]); |
| 1326 |
continue; |
| 1327 |
} |
| 1328 |
int alen = strlen(line->d.allele[i]); |
| 1329 |
tok->values[i-1] = alen - rlen; |
| 1330 |
} |
| 1331 |
} |
| 1332 |
static void filters_set_ref_string(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1333 |
{ |
| 1334 |
tok->str_value.l = 0; |
| 1335 |
kputs(line->d.allele[0], &tok->str_value); |
| 1336 |
tok->nvalues = tok->str_value.l; |
| 1337 |
} |
| 1338 |
static void filters_set_alt_string(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1339 |
{ |
| 1340 |
tok->str_value.l = 0; |
| 1341 |
if ( tok->idx>=0 ) |
| 1342 |
{ |
| 1343 |
if ( line->n_allele > tok->idx + 1 ) |
| 1344 |
kputs(line->d.allele[tok->idx + 1], &tok->str_value); |
| 1345 |
else |
| 1346 |
kputc('.', &tok->str_value); |
| 1347 |
} |
| 1348 |
else if ( tok->idx==-2 ) |
| 1349 |
{ |
| 1350 |
int i, end = tok->nuidxs ? tok->nuidxs : line->n_allele - 1; |
| 1351 |
if ( end >= line->n_allele - 1 ) end = line->n_allele - 2; |
| 1352 |
for (i=0; i<=end; i++) |
| 1353 |
if ( i>=tok->nidxs || tok->idxs[i] ) |
| 1354 |
{ |
| 1355 |
if ( tok->str_value.l ) kputc(',', &tok->str_value); |
| 1356 |
kputs(line->d.allele[i+1], &tok->str_value); |
| 1357 |
} |
| 1358 |
} |
| 1359 |
else if ( line->n_allele>1 ) |
| 1360 |
{ |
| 1361 |
kputs(line->d.allele[1], &tok->str_value); |
| 1362 |
int i; |
| 1363 |
for (i=2; i<line->n_allele; i++) |
| 1364 |
{ |
| 1365 |
kputc(',', &tok->str_value); |
| 1366 |
kputs(line->d.allele[i], &tok->str_value); |
| 1367 |
} |
| 1368 |
} |
| 1369 |
else if ( line->n_allele==1 ) |
| 1370 |
kputc('.', &tok->str_value); |
| 1371 |
tok->nvalues = tok->str_value.l; |
| 1372 |
} |
| 1373 |
static void filters_set_nmissing(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1374 |
{ |
| 1375 |
bcf_unpack(line, BCF_UN_FMT); |
| 1376 |
if ( !line->n_sample ) |
| 1377 |
{ |
| 1378 |
tok->nvalues = 1; |
| 1379 |
tok->values[0] = 0; |
| 1380 |
return; |
| 1381 |
} |
| 1382 |
|
| 1383 |
int i,igt = bcf_hdr_id2int(flt->hdr, BCF_DT_ID, "GT"); |
| 1384 |
bcf_fmt_t *fmt = NULL; |
| 1385 |
for (i=0; i<line->n_fmt; i++) |
| 1386 |
if ( line->d.fmt[i].id==igt ) { fmt = &line->d.fmt[i]; break; } |
| 1387 |
if ( !fmt ) |
| 1388 |
{ |
| 1389 |
tok->nvalues = 0; |
| 1390 |
return; |
| 1391 |
} |
| 1392 |
|
| 1393 |
int j,nmissing = 0; |
| 1394 |
#define BRANCH(type_t, convert, is_vector_end) { \ |
| 1395 |
for (i=0; i<line->n_sample; i++) \ |
| 1396 |
{ \ |
| 1397 |
uint8_t *ptr = fmt->p + i*fmt->size; \ |
| 1398 |
for (j=0; j<fmt->n; j++) \ |
| 1399 |
{ \ |
| 1400 |
type_t val = convert(&ptr[j * sizeof(type_t)]); \ |
| 1401 |
if ( val==is_vector_end ) break; \ |
| 1402 |
if ( val==bcf_gt_missing ) { nmissing++; break; } \ |
| 1403 |
} \ |
| 1404 |
} \ |
| 1405 |
} |
| 1406 |
switch (fmt->type) { |
| 1407 |
case BCF_BT_INT8: BRANCH(int8_t, le_to_i8, bcf_int8_vector_end); break; |
| 1408 |
case BCF_BT_INT16: BRANCH(int16_t, le_to_i16, bcf_int16_vector_end); break; |
| 1409 |
case BCF_BT_INT32: BRANCH(int32_t, le_to_i32, bcf_int32_vector_end); break; |
| 1410 |
default: fprintf(bcftools_stderr,"todo: type %d\n", fmt->type); bcftools_exit(1); break; |
| 1411 |
} |
| 1412 |
#undef BRANCH |
| 1413 |
tok->nvalues = 1; |
| 1414 |
tok->values[0] = tok->tag[0]=='N' ? nmissing : (double)nmissing / line->n_sample; |
| 1415 |
} |
| 1416 |
static int func_npass(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1417 |
{ |
| 1418 |
if ( nstack==0 ) error("Error parsing the expression\n"); |
| 1419 |
token_t *tok = stack[nstack - 1]; |
| 1420 |
if ( !tok->nsamples ) error("The function %s works with FORMAT fields\n", rtok->tag); |
| 1421 |
assert(tok->usmpl); |
| 1422 |
|
| 1423 |
int i, npass = 0; |
| 1424 |
for (i=0; i<tok->nsamples; i++) |
| 1425 |
{ |
| 1426 |
if ( !tok->usmpl[i] ) continue; |
| 1427 |
if ( tok->pass_samples[i] ) npass++; |
| 1428 |
} |
| 1429 |
hts_expand(double,1,rtok->mvalues,rtok->values); |
| 1430 |
rtok->nsamples = 0; |
| 1431 |
rtok->nvalues = 1; |
| 1432 |
rtok->values[0] = rtok->tag[0]=='N' ? npass : (line->n_sample ? 1.0*npass/line->n_sample : 0); |
| 1433 |
|
| 1434 |
return 1; |
| 1435 |
} |
| 1436 |
static void filters_set_nalt(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1437 |
{ |
| 1438 |
tok->nvalues = 1; |
| 1439 |
tok->values[0] = line->n_allele - 1; |
| 1440 |
} |
| 1441 |
static void filters_set_ac(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1442 |
{ |
| 1443 |
hts_expand(int32_t, line->n_allele, flt->mtmpi, flt->tmpi); |
| 1444 |
if ( !bcf_calc_ac(flt->hdr, line, flt->tmpi, BCF_UN_INFO|BCF_UN_FMT) ) |
| 1445 |
{ |
| 1446 |
tok->nvalues = 0; |
| 1447 |
return; |
| 1448 |
} |
| 1449 |
int i, an = flt->tmpi[0]; |
| 1450 |
for (i=1; i<line->n_allele; i++) an += flt->tmpi[i]; |
| 1451 |
if ( !an ) |
| 1452 |
{ |
| 1453 |
tok->nvalues = 0; |
| 1454 |
return; |
| 1455 |
} |
| 1456 |
flt->tmpi[0] = an; |
| 1457 |
if ( tok->idx>=0 ) |
| 1458 |
{ |
| 1459 |
tok->nvalues = 1; |
| 1460 |
tok->values[0] = tok->idx+1<line->n_allele ? flt->tmpi[tok->idx+1] : 0; |
| 1461 |
} |
| 1462 |
else if ( line->n_allele==1 ) |
| 1463 |
{ |
| 1464 |
tok->nvalues = 1; |
| 1465 |
tok->values[0] = 0; |
| 1466 |
} |
| 1467 |
else |
| 1468 |
{ |
| 1469 |
hts_expand(double,line->n_allele,tok->mvalues,tok->values); |
| 1470 |
for (i=1; i<line->n_allele; i++) |
| 1471 |
tok->values[i-1] = flt->tmpi[i]; |
| 1472 |
tok->nvalues = line->n_allele - 1; |
| 1473 |
} |
| 1474 |
} |
| 1475 |
static void filters_set_an(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1476 |
{ |
| 1477 |
filters_set_ac(flt,line,tok); |
| 1478 |
tok->values[0] = tok->nvalues ? flt->tmpi[0] : 0; |
| 1479 |
tok->nvalues = 1; |
| 1480 |
} |
| 1481 |
static void filters_set_mac(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1482 |
{ |
| 1483 |
filters_set_ac(flt,line,tok); |
| 1484 |
if ( !tok->nvalues ) return; |
| 1485 |
int i, an = flt->tmpi[0]; |
| 1486 |
for (i=0; i<tok->nvalues; i++) |
| 1487 |
if ( tok->values[i] > an*0.5 ) tok->values[i] = an - tok->values[i]; |
| 1488 |
} |
| 1489 |
static void filters_set_af(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1490 |
{ |
| 1491 |
filters_set_ac(flt,line,tok); |
| 1492 |
if ( !tok->nvalues ) return; |
| 1493 |
int i, an = flt->tmpi[0]; |
| 1494 |
for (i=0; i<tok->nvalues; i++) |
| 1495 |
tok->values[i] /= (double)an; |
| 1496 |
} |
| 1497 |
static void filters_set_maf(filter_t *flt, bcf1_t *line, token_t *tok) |
| 1498 |
{ |
| 1499 |
filters_set_ac(flt,line,tok); |
| 1500 |
if ( !tok->nvalues ) return; |
| 1501 |
int i, an = flt->tmpi[0]; |
| 1502 |
for (i=0; i<tok->nvalues; i++) |
| 1503 |
{ |
| 1504 |
tok->values[i] /= (double)an; |
| 1505 |
if ( tok->values[i] > 0.5 ) tok->values[i] = 1 - tok->values[i]; |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
static int func_max(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1510 |
{ |
| 1511 |
token_t *tok = stack[nstack - 1]; |
| 1512 |
rtok->nvalues = 0; |
| 1513 |
if ( !tok->nvalues ) return 1; |
| 1514 |
double *ptr, val = -HUGE_VAL; |
| 1515 |
int i,j, has_value = 0; |
| 1516 |
if ( tok->nsamples ) |
| 1517 |
{ |
| 1518 |
for (i=0; i<tok->nsamples; i++) |
| 1519 |
{ |
| 1520 |
if ( !tok->usmpl[i] ) continue; |
| 1521 |
ptr = tok->values + i*tok->nval1; |
| 1522 |
for (j=0; j<tok->nval1; j++) |
| 1523 |
{ |
| 1524 |
if ( bcf_double_is_missing_or_vector_end(ptr[j]) ) continue; |
| 1525 |
has_value = 1; |
| 1526 |
if ( val < ptr[j] ) val = ptr[j]; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
} |
| 1530 |
else |
| 1531 |
{ |
| 1532 |
for (i=0; i<tok->nvalues; i++) |
| 1533 |
{ |
| 1534 |
if ( bcf_double_is_missing_or_vector_end(tok->values[i]) ) continue; |
| 1535 |
has_value = 1; |
| 1536 |
if ( val < tok->values[i] ) val = tok->values[i]; |
| 1537 |
} |
| 1538 |
} |
| 1539 |
if ( has_value ) |
| 1540 |
{ |
| 1541 |
rtok->values[0] = val; |
| 1542 |
rtok->nvalues = has_value; |
| 1543 |
} |
| 1544 |
return 1; |
| 1545 |
} |
| 1546 |
static int func_smpl_max(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1547 |
{ |
| 1548 |
token_t *tok = stack[nstack - 1]; |
| 1549 |
if ( !tok->nsamples ) return func_max(flt,line,rtok,stack,nstack); |
| 1550 |
rtok->nsamples = tok->nsamples; |
| 1551 |
rtok->nvalues = tok->nsamples; |
| 1552 |
rtok->nval1 = 1; |
| 1553 |
hts_expand(double,rtok->nvalues,rtok->mvalues,rtok->values); |
| 1554 |
assert(tok->usmpl); |
| 1555 |
if ( !rtok->usmpl ) rtok->usmpl = (uint8_t*) malloc(tok->nsamples); |
| 1556 |
memcpy(rtok->usmpl, tok->usmpl, tok->nsamples); |
| 1557 |
int i, j, has_value; |
| 1558 |
double val, *ptr; |
| 1559 |
for (i=0; i<tok->nsamples; i++) |
| 1560 |
{ |
| 1561 |
if ( !rtok->usmpl[i] ) continue; |
| 1562 |
val = -HUGE_VAL; |
| 1563 |
has_value = 0; |
| 1564 |
ptr = tok->values + i*tok->nval1; |
| 1565 |
for (j=0; j<tok->nval1; j++) |
| 1566 |
{ |
| 1567 |
if ( bcf_double_is_missing_or_vector_end(ptr[j]) ) continue; |
| 1568 |
has_value = 1; |
| 1569 |
if ( val < ptr[j] ) val = ptr[j]; |
| 1570 |
} |
| 1571 |
if ( has_value ) rtok->values[i] = val; |
| 1572 |
else bcf_double_set_missing(rtok->values[i]); |
| 1573 |
} |
| 1574 |
return 1; |
| 1575 |
} |
| 1576 |
static int func_min(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1577 |
{ |
| 1578 |
token_t *tok = stack[nstack - 1]; |
| 1579 |
rtok->nvalues = 0; |
| 1580 |
if ( !tok->nvalues ) return 1; |
| 1581 |
double *ptr, val = HUGE_VAL; |
| 1582 |
int i,j, has_value = 0; |
| 1583 |
if ( tok->nsamples ) |
| 1584 |
{ |
| 1585 |
for (i=0; i<tok->nsamples; i++) |
| 1586 |
{ |
| 1587 |
if ( !tok->usmpl[i] ) continue; |
| 1588 |
ptr = tok->values + i*tok->nval1; |
| 1589 |
for (j=0; j<tok->nval1; j++) |
| 1590 |
{ |
| 1591 |
if ( bcf_double_is_missing_or_vector_end(ptr[j]) ) continue; |
| 1592 |
has_value = 1; |
| 1593 |
if ( val > ptr[j] ) val = ptr[j]; |
| 1594 |
} |
| 1595 |
} |
| 1596 |
} |
| 1597 |
else |
| 1598 |
{ |
| 1599 |
for (i=0; i<tok->nvalues; i++) |
| 1600 |
{ |
| 1601 |
if ( bcf_double_is_missing_or_vector_end(tok->values[i]) ) continue; |
| 1602 |
has_value = 1; |
| 1603 |
if ( val > tok->values[i] ) val = tok->values[i]; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
if ( has_value ) |
| 1607 |
{ |
| 1608 |
rtok->values[0] = val; |
| 1609 |
rtok->nvalues = has_value; |
| 1610 |
} |
| 1611 |
return 1; |
| 1612 |
} |
| 1613 |
static int func_smpl_min(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1614 |
{ |
| 1615 |
token_t *tok = stack[nstack - 1]; |
| 1616 |
if ( !tok->nsamples ) return func_min(flt,line,rtok,stack,nstack); |
| 1617 |
rtok->nsamples = tok->nsamples; |
| 1618 |
rtok->nvalues = tok->nsamples; |
| 1619 |
rtok->nval1 = 1; |
| 1620 |
hts_expand(double,rtok->nvalues,rtok->mvalues,rtok->values); |
| 1621 |
assert(tok->usmpl); |
| 1622 |
if ( !rtok->usmpl ) rtok->usmpl = (uint8_t*) malloc(tok->nsamples); |
| 1623 |
memcpy(rtok->usmpl, tok->usmpl, tok->nsamples); |
| 1624 |
int i, j, has_value; |
| 1625 |
double val, *ptr; |
| 1626 |
for (i=0; i<tok->nsamples; i++) |
| 1627 |
{ |
| 1628 |
if ( !rtok->usmpl[i] ) continue; |
| 1629 |
val = HUGE_VAL; |
| 1630 |
has_value = 0; |
| 1631 |
ptr = tok->values + i*tok->nval1; |
| 1632 |
for (j=0; j<tok->nval1; j++) |
| 1633 |
{ |
| 1634 |
if ( bcf_double_is_missing_or_vector_end(ptr[j]) ) continue; |
| 1635 |
has_value = 1; |
| 1636 |
if ( val > ptr[j] ) val = ptr[j]; |
| 1637 |
} |
| 1638 |
if ( has_value ) rtok->values[i] = val; |
| 1639 |
else bcf_double_set_missing(rtok->values[i]); |
| 1640 |
} |
| 1641 |
return 1; |
| 1642 |
} |
| 1643 |
static int func_avg(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1644 |
{ |
| 1645 |
token_t *tok = stack[nstack - 1]; |
| 1646 |
rtok->nvalues = 0; |
| 1647 |
if ( !tok->nvalues ) return 1; |
| 1648 |
double *ptr, val = 0; |
| 1649 |
int i,j, n = 0; |
| 1650 |
if ( tok->nsamples ) |
| 1651 |
{ |
| 1652 |
for (i=0; i<tok->nsamples; i++) |
| 1653 |
{ |
| 1654 |
if ( !tok->usmpl[i] ) continue; |
| 1655 |
ptr = tok->values + i*tok->nval1; |
| 1656 |
for (j=0; j<tok->nval1; j++) |
| 1657 |
{ |
| 1658 |
if ( bcf_double_is_missing_or_vector_end(ptr[j]) ) continue; |
| 1659 |
val += ptr[j]; |
| 1660 |
n++; |
| 1661 |
} |
| 1662 |
} |
| 1663 |
} |
| 1664 |
else |
| 1665 |
{ |
| 1666 |
for (i=0; i<tok->nvalues; i++) |
| 1667 |
if ( !bcf_double_is_missing_or_vector_end(tok->values[i]) ) { val += tok->values[i]; n++; } |
| 1668 |
} |
| 1669 |
if ( n ) |
| 1670 |
{ |
| 1671 |
rtok->values[0] = val / n; |
| 1672 |
rtok->nvalues = 1; |
| 1673 |
} |
| 1674 |
return 1; |
| 1675 |
} |
| 1676 |
static int func_smpl_avg(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1677 |
{ |
| 1678 |
token_t *tok = stack[nstack - 1]; |
| 1679 |
if ( !tok->nsamples ) return func_avg(flt,line,rtok,stack,nstack); |
| 1680 |
rtok->nsamples = tok->nsamples; |
| 1681 |
rtok->nvalues = tok->nsamples; |
| 1682 |
rtok->nval1 = 1; |
| 1683 |
hts_expand(double,rtok->nvalues,rtok->mvalues,rtok->values); |
| 1684 |
assert(tok->usmpl); |
| 1685 |
if ( !rtok->usmpl ) rtok->usmpl = (uint8_t*) malloc(tok->nsamples); |
| 1686 |
memcpy(rtok->usmpl, tok->usmpl, tok->nsamples); |
| 1687 |
int i, j, n; |
| 1688 |
double val, *ptr; |
| 1689 |
for (i=0; i<tok->nsamples; i++) |
| 1690 |
{ |
| 1691 |
if ( !rtok->usmpl[i] ) continue; |
| 1692 |
val = 0; |
| 1693 |
n = 0; |
| 1694 |
ptr = tok->values + i*tok->nval1; |
| 1695 |
for (j=0; j<tok->nval1; j++) |
| 1696 |
{ |
| 1697 |
if ( !bcf_double_is_missing_or_vector_end(ptr[j]) ) { val += ptr[j]; n++; } |
| 1698 |
} |
| 1699 |
if ( n ) rtok->values[i] = val / n; |
| 1700 |
else bcf_double_set_missing(rtok->values[i]); |
| 1701 |
} |
| 1702 |
return 1; |
| 1703 |
} |
| 1704 |
static int compare_doubles(const void *lhs, const void *rhs) |
| 1705 |
{ |
| 1706 |
double arg1 = *(const double*) lhs; |
| 1707 |
double arg2 = *(const double*) rhs; |
| 1708 |
if (arg1 < arg2) return -1; |
| 1709 |
if (arg1 > arg2) return 1; |
| 1710 |
return 0; |
| 1711 |
} |
| 1712 |
static int func_median(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1713 |
{ |
| 1714 |
token_t *tok = stack[nstack - 1]; |
| 1715 |
rtok->nvalues = 0; |
| 1716 |
if ( !tok->nvalues ) return 1; |
| 1717 |
|
| 1718 |
int i,j,k = 0, n = 0; |
| 1719 |
if ( tok->nsamples ) |
| 1720 |
{ |
| 1721 |
for (i=0; i<tok->nsamples; i++) |
| 1722 |
{ |
| 1723 |
if ( !tok->usmpl[i] ) { k += tok->nval1; continue; } |
| 1724 |
for (j=0; j<tok->nval1; k++,j++) |
| 1725 |
{ |
| 1726 |
if ( bcf_double_is_missing_or_vector_end(tok->values[k]) ) continue; |
| 1727 |
if ( n < k ) tok->values[n] = tok->values[k]; |
| 1728 |
n++; |
| 1729 |
} |
| 1730 |
} |
| 1731 |
} |
| 1732 |
else |
| 1733 |
{ |
| 1734 |
for (i=0; i<tok->nvalues; i++) |
| 1735 |
{ |
| 1736 |
if ( bcf_double_is_missing_or_vector_end(tok->values[i]) ) continue; |
| 1737 |
if ( n < i ) tok->values[n] = tok->values[i]; |
| 1738 |
n++; |
| 1739 |
} |
| 1740 |
} |
| 1741 |
if ( !n ) return 1; |
| 1742 |
if ( n==1 ) rtok->values[0] = tok->values[0]; |
| 1743 |
else |
| 1744 |
{ |
| 1745 |
qsort(tok->values, n, sizeof(double), compare_doubles); |
| 1746 |
rtok->values[0] = n % 2 ? tok->values[n/2] : (tok->values[n/2-1] + tok->values[n/2]) * 0.5; |
| 1747 |
} |
| 1748 |
rtok->nvalues = 1; |
| 1749 |
return 1; |
| 1750 |
} |
| 1751 |
static int func_smpl_median(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1752 |
{ |
| 1753 |
token_t *tok = stack[nstack - 1]; |
| 1754 |
if ( !tok->nsamples ) return func_avg(flt,line,rtok,stack,nstack); |
| 1755 |
rtok->nsamples = tok->nsamples; |
| 1756 |
rtok->nvalues = tok->nsamples; |
| 1757 |
rtok->nval1 = 1; |
| 1758 |
hts_expand(double,rtok->nvalues,rtok->mvalues,rtok->values); |
| 1759 |
assert(tok->usmpl); |
| 1760 |
if ( !rtok->usmpl ) rtok->usmpl = (uint8_t*) malloc(tok->nsamples); |
| 1761 |
memcpy(rtok->usmpl, tok->usmpl, tok->nsamples); |
| 1762 |
int i, j, n; |
| 1763 |
double *ptr; |
| 1764 |
for (i=0; i<tok->nsamples; i++) |
| 1765 |
{ |
| 1766 |
if ( !rtok->usmpl[i] ) continue; |
| 1767 |
n = 0; |
| 1768 |
ptr = tok->values + i*tok->nval1; |
| 1769 |
for (j=0; j<tok->nval1; j++) |
| 1770 |
{ |
| 1771 |
if ( bcf_double_is_missing_or_vector_end(ptr[j]) ) continue; |
| 1772 |
if ( n < j ) ptr[n] = ptr[j]; |
| 1773 |
n++; |
| 1774 |
} |
| 1775 |
if ( n==0 ) |
| 1776 |
bcf_double_set_missing(rtok->values[i]); |
| 1777 |
else if ( n==1 ) |
| 1778 |
rtok->values[i] = ptr[0]; |
| 1779 |
else |
| 1780 |
{ |
| 1781 |
qsort(ptr, n, sizeof(double), compare_doubles); |
| 1782 |
rtok->values[i] = n % 2 ? ptr[n/2] : (ptr[n/2-1] + ptr[n/2]) * 0.5; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
return 1; |
| 1786 |
} |
| 1787 |
static int func_stddev(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1788 |
{ |
| 1789 |
token_t *tok = stack[nstack - 1]; |
| 1790 |
rtok->nvalues = 0; |
| 1791 |
if ( !tok->nvalues ) return 1; |
| 1792 |
|
| 1793 |
int i,j,k = 0, n = 0; |
| 1794 |
if ( tok->nsamples ) |
| 1795 |
{ |
| 1796 |
for (i=0; i<tok->nsamples; i++) |
| 1797 |
{ |
| 1798 |
if ( !tok->usmpl[i] ) { k += tok->nval1; continue; } |
| 1799 |
for (j=0; j<tok->nval1; k++,j++) |
| 1800 |
{ |
| 1801 |
if ( bcf_double_is_missing_or_vector_end(tok->values[k]) ) continue; |
| 1802 |
if ( n < k ) tok->values[n] = tok->values[k]; |
| 1803 |
n++; |
| 1804 |
} |
| 1805 |
} |
| 1806 |
} |
| 1807 |
else |
| 1808 |
{ |
| 1809 |
for (i=0; i<tok->nvalues; i++) |
| 1810 |
{ |
| 1811 |
if ( bcf_double_is_missing_or_vector_end(tok->values[i]) ) continue; |
| 1812 |
if ( n < i ) tok->values[n] = tok->values[i]; |
| 1813 |
n++; |
| 1814 |
} |
| 1815 |
} |
| 1816 |
if ( !n ) return 1; |
| 1817 |
if ( n==1 ) rtok->values[0] = 0; |
| 1818 |
else |
| 1819 |
{ |
| 1820 |
double sdev = 0, avg = 0; |
| 1821 |
for (i=0; i<n; i++) avg += tok->values[i]; |
| 1822 |
avg /= n; |
| 1823 |
for (i=0; i<n; i++) sdev += (tok->values[i] - avg) * (tok->values[i] - avg); |
| 1824 |
rtok->values[0] = sqrt(sdev/n); |
| 1825 |
} |
| 1826 |
rtok->nvalues = 1; |
| 1827 |
return 1; |
| 1828 |
} |
| 1829 |
static int func_smpl_stddev(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1830 |
{ |
| 1831 |
token_t *tok = stack[nstack - 1]; |
| 1832 |
if ( !tok->nsamples ) return func_avg(flt,line,rtok,stack,nstack); |
| 1833 |
rtok->nsamples = tok->nsamples; |
| 1834 |
rtok->nvalues = tok->nsamples; |
| 1835 |
rtok->nval1 = 1; |
| 1836 |
hts_expand(double,rtok->nvalues,rtok->mvalues,rtok->values); |
| 1837 |
assert(tok->usmpl); |
| 1838 |
if ( !rtok->usmpl ) rtok->usmpl = (uint8_t*) malloc(tok->nsamples); |
| 1839 |
memcpy(rtok->usmpl, tok->usmpl, tok->nsamples); |
| 1840 |
int i, j, n; |
| 1841 |
double *ptr; |
| 1842 |
for (i=0; i<tok->nsamples; i++) |
| 1843 |
{ |
| 1844 |
if ( !rtok->usmpl[i] ) continue; |
| 1845 |
n = 0; |
| 1846 |
ptr = tok->values + i*tok->nval1; |
| 1847 |
for (j=0; j<tok->nval1; j++) |
| 1848 |
{ |
| 1849 |
if ( bcf_double_is_missing_or_vector_end(ptr[j]) ) continue; |
| 1850 |
if ( n < j ) ptr[n] = ptr[j]; |
| 1851 |
n++; |
| 1852 |
} |
| 1853 |
if ( n==0 ) |
| 1854 |
bcf_double_set_missing(rtok->values[i]); |
| 1855 |
else if ( n==1 ) |
| 1856 |
rtok->values[i] = 0; |
| 1857 |
else |
| 1858 |
{ |
| 1859 |
double sdev = 0, avg = 0; |
| 1860 |
for (j=0; j<n; j++) avg += ptr[j]; |
| 1861 |
avg /= n; |
| 1862 |
for (j=0; j<n; j++) sdev += (ptr[j] - avg) * (ptr[j] - avg); |
| 1863 |
rtok->values[i] = sqrt(sdev/n); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
return 1; |
| 1867 |
} |
| 1868 |
static int func_sum(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1869 |
{ |
| 1870 |
rtok->nvalues = 0; |
| 1871 |
token_t *tok = stack[nstack - 1]; |
| 1872 |
if ( !tok->nvalues ) return 1; |
| 1873 |
double *ptr, val = 0; |
| 1874 |
int i,j, n = 0; |
| 1875 |
if ( tok->nsamples ) |
| 1876 |
{ |
| 1877 |
for (i=0; i<tok->nsamples; i++) |
| 1878 |
{ |
| 1879 |
if ( !tok->usmpl[i] ) continue; |
| 1880 |
ptr = tok->values + i*tok->nval1; |
| 1881 |
for (j=0; j<tok->nval1; j++) |
| 1882 |
{ |
| 1883 |
if ( bcf_double_is_missing_or_vector_end(ptr[j]) ) continue; |
| 1884 |
val += ptr[j]; |
| 1885 |
n++; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
} |
| 1889 |
else |
| 1890 |
{ |
| 1891 |
for (i=0; i<tok->nvalues; i++) |
| 1892 |
if ( !bcf_double_is_missing_or_vector_end(tok->values[i]) ) { val += tok->values[i]; n++; } |
| 1893 |
} |
| 1894 |
if ( n ) |
| 1895 |
{ |
| 1896 |
rtok->values[0] = val; |
| 1897 |
rtok->nvalues = 1; |
| 1898 |
} |
| 1899 |
return 1; |
| 1900 |
} |
| 1901 |
static int func_smpl_sum(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1902 |
{ |
| 1903 |
token_t *tok = stack[nstack - 1]; |
| 1904 |
if ( !tok->nsamples ) return func_avg(flt,line,rtok,stack,nstack); |
| 1905 |
rtok->nsamples = tok->nsamples; |
| 1906 |
rtok->nvalues = tok->nsamples; |
| 1907 |
rtok->nval1 = 1; |
| 1908 |
hts_expand(double,rtok->nvalues,rtok->mvalues,rtok->values); |
| 1909 |
assert(tok->usmpl); |
| 1910 |
if ( !rtok->usmpl ) rtok->usmpl = (uint8_t*) malloc(tok->nsamples); |
| 1911 |
memcpy(rtok->usmpl, tok->usmpl, tok->nsamples); |
| 1912 |
int i, j, has_value; |
| 1913 |
double val, *ptr; |
| 1914 |
for (i=0; i<tok->nsamples; i++) |
| 1915 |
{ |
| 1916 |
if ( !rtok->usmpl[i] ) continue; |
| 1917 |
val = 0; |
| 1918 |
has_value = 0; |
| 1919 |
ptr = tok->values + i*tok->nval1; |
| 1920 |
for (j=0; j<tok->nval1; j++) |
| 1921 |
{ |
| 1922 |
if ( bcf_double_is_missing_or_vector_end(ptr[j]) ) continue; |
| 1923 |
has_value = 1; |
| 1924 |
val += ptr[j]; |
| 1925 |
} |
| 1926 |
if ( has_value ) rtok->values[i] = val; |
| 1927 |
else bcf_double_set_missing(rtok->values[i]); |
| 1928 |
} |
| 1929 |
return 1; |
| 1930 |
} |
| 1931 |
static int func_abs(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1932 |
{ |
| 1933 |
token_t *tok = stack[nstack - 1]; |
| 1934 |
if ( tok->is_str ) error("ABS() can be applied only on numeric values\n"); |
| 1935 |
rtok->nsamples = tok->nsamples; |
| 1936 |
rtok->nvalues = tok->nvalues; |
| 1937 |
rtok->nval1 = tok->nval1; |
| 1938 |
hts_expand(double,rtok->nvalues,rtok->mvalues,rtok->values); |
| 1939 |
if ( tok->usmpl ) |
| 1940 |
{ |
| 1941 |
if ( !rtok->usmpl ) rtok->usmpl = (uint8_t*) malloc(tok->nsamples); |
| 1942 |
memcpy(rtok->usmpl, tok->usmpl, tok->nsamples); |
| 1943 |
} |
| 1944 |
if ( !tok->nvalues ) return 1; |
| 1945 |
hts_expand(double, rtok->nvalues, rtok->mvalues, rtok->values); |
| 1946 |
int i,j,k = 0; |
| 1947 |
if ( tok->usmpl ) |
| 1948 |
{ |
| 1949 |
for (i=0; i<tok->nsamples; i++) |
| 1950 |
{ |
| 1951 |
if ( !tok->usmpl[i] ) { k+= tok->nval1; continue; } |
| 1952 |
for (j=0; j<tok->nval1; k++,j++) |
| 1953 |
{ |
| 1954 |
if ( bcf_double_is_missing_or_vector_end(tok->values[k]) ) bcf_double_set_missing(rtok->values[k]); |
| 1955 |
else rtok->values[k] = fabs(tok->values[k]); |
| 1956 |
} |
| 1957 |
} |
| 1958 |
} |
| 1959 |
else |
| 1960 |
{ |
| 1961 |
for (i=0; i<tok->nvalues; i++) |
| 1962 |
{ |
| 1963 |
if ( tok->usmpl && !tok->usmpl[i] ) continue; |
| 1964 |
if ( bcf_double_is_missing(tok->values[i]) ) bcf_double_set_missing(rtok->values[i]); |
| 1965 |
else if ( !bcf_double_is_vector_end(tok->values[i]) ) rtok->values[i] = fabs(tok->values[i]); |
| 1966 |
} |
| 1967 |
} |
| 1968 |
return 1; |
| 1969 |
} |
| 1970 |
static int func_count(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 1971 |
{ |
| 1972 |
token_t *tok = stack[nstack - 1]; |
| 1973 |
int i,j, cnt = 0; |
| 1974 |
if ( tok->tag && tok->nsamples ) |
| 1975 |
{ |
| 1976 |
|
| 1977 |
if ( tok->is_str ) error("todo: Type=String for COUNT on FORMAT fields?\n"); |
| 1978 |
for (i=0; i<tok->nsamples; i++) |
| 1979 |
{ |
| 1980 |
if ( !tok->usmpl[i] ) continue; |
| 1981 |
double *ptr = tok->values + i*tok->nval1; |
| 1982 |
for (j=0; j<tok->nval1; j++) |
| 1983 |
if ( !bcf_double_is_missing_or_vector_end(ptr[j]) ) cnt++; |
| 1984 |
} |
| 1985 |
} |
| 1986 |
else if ( tok->nsamples ) |
| 1987 |
{ |
| 1988 |
|
| 1989 |
for (i=0; i<tok->nsamples; i++) |
| 1990 |
if ( tok->pass_samples[i] ) cnt++; |
| 1991 |
} |
| 1992 |
else if ( tok->is_str ) |
| 1993 |
{ |
| 1994 |
if ( tok->str_value.l ) cnt = 1; |
| 1995 |
for (i=0; i<tok->str_value.l; i++) if ( tok->str_value.s[i]==',' ) cnt++; |
| 1996 |
} |
| 1997 |
else |
| 1998 |
cnt = tok->nvalues; |
| 1999 |
|
| 2000 |
rtok->nvalues = 1; |
| 2001 |
rtok->values[0] = cnt; |
| 2002 |
return 1; |
| 2003 |
} |
| 2004 |
static int func_strlen(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 2005 |
{ |
| 2006 |
token_t *tok = stack[nstack - 1]; |
| 2007 |
rtok->nvalues = rtok->str_value.l = 0; |
| 2008 |
if ( !tok->str_value.l ) return 1; |
| 2009 |
|
| 2010 |
if ( tok->idx==-2 ) |
| 2011 |
{ |
| 2012 |
int i = 0; |
| 2013 |
char *ss = tok->str_value.s; |
| 2014 |
while ( *ss ) |
| 2015 |
{ |
| 2016 |
char *se = ss; |
| 2017 |
while ( *se && *se!=',' ) se++; |
| 2018 |
hts_expand(double, i+1, rtok->mvalues, rtok->values); |
| 2019 |
if ( !*se ) rtok->values[i] = strlen(ss); |
| 2020 |
else |
| 2021 |
{ |
| 2022 |
*se = 0; |
| 2023 |
rtok->values[i] = strlen(ss); |
| 2024 |
*se = ','; |
| 2025 |
} |
| 2026 |
ss = *se ? se + 1 : se; |
| 2027 |
i++; |
| 2028 |
} |
| 2029 |
rtok->nvalues = i; |
| 2030 |
} |
| 2031 |
else |
| 2032 |
{ |
| 2033 |
if ( !tok->str_value.s[1] && tok->str_value.s[0]=='.' ) |
| 2034 |
rtok->values[0] = 0; |
| 2035 |
else |
| 2036 |
rtok->values[0] = strlen(tok->str_value.s); |
| 2037 |
rtok->nvalues = 1; |
| 2038 |
} |
| 2039 |
return 1; |
| 2040 |
} |
| 2041 |
static int func_binom(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 2042 |
{ |
| 2043 |
int i, istack = nstack - rtok->nargs; |
| 2044 |
|
| 2045 |
if ( rtok->nargs!=2 && rtok->nargs!=1 ) error("Error: binom() takes one or two arguments\n"); |
| 2046 |
assert( istack>=0 ); |
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
|
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
token_t *tok = stack[istack]; |
| 2067 |
if ( tok->nsamples ) |
| 2068 |
{ |
| 2069 |
|
| 2070 |
rtok->nval1 = 1; |
| 2071 |
rtok->nvalues = tok->nsamples; |
| 2072 |
rtok->nsamples = tok->nsamples; |
| 2073 |
hts_expand(double, rtok->nvalues, rtok->mvalues, rtok->values); |
| 2074 |
assert(tok->usmpl); |
| 2075 |
if ( !rtok->usmpl ) rtok->usmpl = (uint8_t*) malloc(tok->nsamples); |
| 2076 |
memcpy(rtok->usmpl, tok->usmpl, tok->nsamples); |
| 2077 |
|
| 2078 |
if ( istack+1==nstack ) |
| 2079 |
{ |
| 2080 |
|
| 2081 |
int ngt = bcf_get_genotypes(flt->hdr, line, &flt->tmpi, &flt->mtmpi); |
| 2082 |
int max_ploidy = ngt/line->n_sample; |
| 2083 |
if ( ngt <= 0 || max_ploidy < 2 ) |
| 2084 |
{ |
| 2085 |
for (i=0; i<rtok->nsamples; i++) |
| 2086 |
if ( rtok->usmpl[i] ) bcf_double_set_missing(rtok->values[i]); |
| 2087 |
return rtok->nargs; |
| 2088 |
} |
| 2089 |
for (i=0; i<rtok->nsamples; i++) |
| 2090 |
{ |
| 2091 |
if ( !rtok->usmpl[i] ) continue; |
| 2092 |
int32_t *ptr = flt->tmpi + i*max_ploidy; |
| 2093 |
if ( bcf_gt_is_missing(ptr[0]) || bcf_gt_is_missing(ptr[1]) || ptr[1]==bcf_int32_vector_end ) |
| 2094 |
{ |
| 2095 |
bcf_double_set_missing(rtok->values[i]); |
| 2096 |
continue; |
| 2097 |
} |
| 2098 |
int idx1 = bcf_gt_allele(ptr[0]); |
| 2099 |
int idx2 = bcf_gt_allele(ptr[1]); |
| 2100 |
if ( idx1>=line->n_allele ) error("Incorrect allele index at %s:%"PRId64", sample %s\n", bcf_seqname(flt->hdr,line),(int64_t) line->pos+1,flt->hdr->samples[i]); |
| 2101 |
if ( idx2>=line->n_allele ) error("Incorrect allele index at %s:%"PRId64", sample %s\n", bcf_seqname(flt->hdr,line),(int64_t) line->pos+1,flt->hdr->samples[i]); |
| 2102 |
double *vals = tok->values + tok->nval1*i; |
| 2103 |
if ( bcf_double_is_missing_or_vector_end(vals[idx1]) || bcf_double_is_missing_or_vector_end(vals[idx2]) ) |
| 2104 |
{ |
| 2105 |
bcf_double_set_missing(rtok->values[i]); |
| 2106 |
continue; |
| 2107 |
} |
| 2108 |
rtok->values[i] = calc_binom_two_sided(vals[idx1],vals[idx2],0.5); |
| 2109 |
if ( rtok->values[i] < 0 ) |
| 2110 |
{ |
| 2111 |
bcf_double_set_missing(rtok->values[i]); |
| 2112 |
continue; |
| 2113 |
} |
| 2114 |
} |
| 2115 |
} |
| 2116 |
else |
| 2117 |
{ |
| 2118 |
|
| 2119 |
token_t *tok2 = stack[istack+1]; |
| 2120 |
if ( tok->nval1!=1 || tok2->nval1!=1 ) |
| 2121 |
error("Expected one value per binom() argument, found %d and %d at %s:%"PRId64"\n",tok->nval1,tok2->nval1, bcf_seqname(flt->hdr,line),(int64_t) line->pos+1); |
| 2122 |
for (i=0; i<rtok->nsamples; i++) |
| 2123 |
{ |
| 2124 |
if ( !rtok->usmpl[i] ) continue; |
| 2125 |
double *ptr1 = tok->values + tok->nval1*i; |
| 2126 |
double *ptr2 = tok2->values + tok2->nval1*i; |
| 2127 |
if ( bcf_double_is_missing_or_vector_end(ptr1[0]) || bcf_double_is_missing_or_vector_end(ptr2[0]) ) |
| 2128 |
{ |
| 2129 |
bcf_double_set_missing(rtok->values[i]); |
| 2130 |
continue; |
| 2131 |
} |
| 2132 |
rtok->values[i] = calc_binom_two_sided(ptr1[0],ptr2[0],0.5); |
| 2133 |
if ( rtok->values[i] < 0 ) |
| 2134 |
{ |
| 2135 |
bcf_double_set_missing(rtok->values[i]); |
| 2136 |
continue; |
| 2137 |
} |
| 2138 |
} |
| 2139 |
} |
| 2140 |
} |
| 2141 |
else |
| 2142 |
{ |
| 2143 |
|
| 2144 |
rtok->nvalues = 1; |
| 2145 |
hts_expand(double, rtok->nvalues, rtok->mvalues, rtok->values); |
| 2146 |
|
| 2147 |
double *ptr1 = NULL, *ptr2 = NULL; |
| 2148 |
if ( istack+1==nstack ) |
| 2149 |
{ |
| 2150 |
|
| 2151 |
if ( tok->nvalues==2 ) |
| 2152 |
{ |
| 2153 |
ptr1 = &tok->values[0]; |
| 2154 |
ptr2 = &tok->values[1]; |
| 2155 |
} |
| 2156 |
} |
| 2157 |
else |
| 2158 |
{ |
| 2159 |
|
| 2160 |
token_t *tok2 = stack[istack+1]; |
| 2161 |
if ( tok->nvalues==1 && tok2->nvalues==1 ) |
| 2162 |
{ |
| 2163 |
ptr1 = &tok->values[0]; |
| 2164 |
ptr2 = &tok2->values[0]; |
| 2165 |
} |
| 2166 |
} |
| 2167 |
if ( !ptr1 || !ptr2 || bcf_double_is_missing_or_vector_end(ptr1[0]) || bcf_double_is_missing_or_vector_end(ptr2[0]) ) |
| 2168 |
bcf_double_set_missing(rtok->values[0]); |
| 2169 |
else |
| 2170 |
{ |
| 2171 |
rtok->values[0] = calc_binom_two_sided(ptr1[0],ptr2[0],0.5); |
| 2172 |
if ( rtok->values[0] < 0 ) |
| 2173 |
bcf_double_set_missing(rtok->values[0]); |
| 2174 |
} |
| 2175 |
} |
| 2176 |
return rtok->nargs; |
| 2177 |
} |
| 2178 |
static int func_phred(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 2179 |
{ |
| 2180 |
token_t *tok = stack[nstack - 1]; |
| 2181 |
if ( tok->is_str ) error("PHRED() can be applied only on numeric values\n"); |
| 2182 |
|
| 2183 |
rtok->nsamples = tok->nsamples; |
| 2184 |
rtok->nval1 = tok->nval1; |
| 2185 |
memcpy(rtok->pass_samples, tok->pass_samples, rtok->nsamples*sizeof(*rtok->pass_samples)); |
| 2186 |
assert(tok->usmpl); |
| 2187 |
if ( !rtok->usmpl ) |
| 2188 |
{ |
| 2189 |
rtok->usmpl = (uint8_t*) malloc(tok->nsamples*sizeof(*rtok->usmpl)); |
| 2190 |
memcpy(rtok->usmpl, tok->usmpl, tok->nsamples*sizeof(*rtok->usmpl)); |
| 2191 |
} |
| 2192 |
rtok->nvalues = tok->nvalues; |
| 2193 |
if ( !tok->nvalues ) return 1; |
| 2194 |
|
| 2195 |
hts_expand(double, rtok->nvalues, rtok->mvalues, rtok->values); |
| 2196 |
int i,j,k = 0; |
| 2197 |
if ( tok->usmpl ) |
| 2198 |
{ |
| 2199 |
for (i=0; i<tok->nsamples; i++) |
| 2200 |
{ |
| 2201 |
if ( !tok->usmpl[i] ) { k+= tok->nval1; continue; } |
| 2202 |
for (j=0; j<tok->nval1; k++,j++) |
| 2203 |
{ |
| 2204 |
if ( bcf_double_is_missing_or_vector_end(tok->values[k]) ) bcf_double_set_missing(rtok->values[k]); |
| 2205 |
else rtok->values[k] = -4.34294481903*log(tok->values[k]); |
| 2206 |
} |
| 2207 |
} |
| 2208 |
} |
| 2209 |
else |
| 2210 |
{ |
| 2211 |
for (i=0; i<tok->nvalues; i++) |
| 2212 |
{ |
| 2213 |
if ( bcf_double_is_missing_or_vector_end(tok->values[i]) ) bcf_double_set_missing(rtok->values[i]); |
| 2214 |
else rtok->values[i] = -4.34294481903*log(tok->values[i]); |
| 2215 |
} |
| 2216 |
} |
| 2217 |
return 1; |
| 2218 |
} |
| 2219 |
inline static void tok_init_values(token_t *atok, token_t *btok, token_t *rtok) |
| 2220 |
{ |
| 2221 |
token_t *tok; |
| 2222 |
if ( (atok->nsamples || btok->nsamples) && (!atok->nsamples || !btok->nsamples) ) |
| 2223 |
tok = atok->nsamples ? atok : btok; |
| 2224 |
else |
| 2225 |
tok = atok->nvalues > btok->nvalues ? atok : btok; |
| 2226 |
rtok->nvalues = tok->nvalues; |
| 2227 |
rtok->nval1 = tok->nval1; |
| 2228 |
hts_expand(double, rtok->nvalues, rtok->mvalues, rtok->values); |
| 2229 |
} |
| 2230 |
inline static void tok_init_samples(token_t *atok, token_t *btok, token_t *rtok) |
| 2231 |
{ |
| 2232 |
if ( (atok->nsamples || btok->nsamples) && !rtok->nsamples ) |
| 2233 |
{ |
| 2234 |
rtok->nsamples = atok->nsamples ? atok->nsamples : btok->nsamples; |
| 2235 |
rtok->usmpl = (uint8_t*) calloc(rtok->nsamples,1); |
| 2236 |
int i; |
| 2237 |
for (i=0; i<atok->nsamples; i++) rtok->usmpl[i] |= atok->usmpl[i]; |
| 2238 |
for (i=0; i<btok->nsamples; i++) rtok->usmpl[i] |= btok->usmpl[i]; |
| 2239 |
} |
| 2240 |
if (rtok->nsamples) |
| 2241 |
memset(rtok->pass_samples, 0, rtok->nsamples); |
| 2242 |
} |
| 2243 |
|
| 2244 |
#define VECTOR_ARITHMETICS(atok,btok,_rtok,AOP,TYPE) \ |
| 2245 |
{ \ |
| 2246 |
token_t *rtok = _rtok; \ |
| 2247 |
int i, has_values = 0; \ |
| 2248 |
if ( atok->nvalues && btok->nvalues ) \ |
| 2249 |
{ \ |
| 2250 |
tok_init_values(atok, btok, rtok); \ |
| 2251 |
tok_init_samples(atok, btok, rtok); \ |
| 2252 |
if ( !atok->nsamples && !btok->nsamples ) \ |
| 2253 |
{ \ |
| 2254 |
if ( atok->nvalues!=btok->nvalues && atok->nvalues!=1 && btok->nvalues!=1 ) \ |
| 2255 |
error("Cannot run numeric operator in -i/-e filtering on vectors of different lengths: %d vs %d\n",atok->nvalues,btok->nvalues); \ |
| 2256 |
int ir,ia = 0, ib = 0; \ |
| 2257 |
for (ir=0; ir<rtok->nvalues; ir++) \ |
| 2258 |
{ \ |
| 2259 |
if ( atok->nvalues > 1 ) ia = ir; \ |
| 2260 |
if ( btok->nvalues > 1 ) ib = ir; \ |
| 2261 |
if ( bcf_double_is_missing_or_vector_end(atok->values[ia]) || bcf_double_is_missing_or_vector_end(btok->values[ib]) ) \ |
| 2262 |
{ \ |
| 2263 |
bcf_double_set_missing(rtok->values[ir]); \ |
| 2264 |
continue; \ |
| 2265 |
} \ |
| 2266 |
has_values = 1; \ |
| 2267 |
rtok->values[ir] = TYPE atok->values[ia] AOP TYPE btok->values[ib]; \ |
| 2268 |
} \ |
| 2269 |
} \ |
| 2270 |
else if ( atok->nsamples && btok->nsamples ) \ |
| 2271 |
{ \ |
| 2272 |
assert( atok->nsamples==btok->nsamples ); \ |
| 2273 |
if ( atok->nval1!=btok->nval1 && atok->nval1!=1 && btok->nval1!=1 ) \ |
| 2274 |
error("Cannot run numeric operator in -i/-e filtering on vectors of different lengths: %d vs %d\n",atok->nval1,btok->nval1); \ |
| 2275 |
for (i=0; i<rtok->nsamples; i++) \ |
| 2276 |
{ \ |
| 2277 |
double *rval = rtok->values + i*rtok->nval1; \ |
| 2278 |
double *aval = atok->values + i*atok->nval1; \ |
| 2279 |
double *bval = btok->values + i*btok->nval1; \ |
| 2280 |
int ir,ia = 0, ib = 0; \ |
| 2281 |
for (ir=0; ir<rtok->nval1; ir++) \ |
| 2282 |
{ \ |
| 2283 |
if ( atok->nval1 > 1 ) ia = ir; \ |
| 2284 |
if ( btok->nval1 > 1 ) ib = ir; \ |
| 2285 |
if ( bcf_double_is_missing_or_vector_end(aval[ia]) || bcf_double_is_missing_or_vector_end(bval[ib]) ) \ |
| 2286 |
{ \ |
| 2287 |
bcf_double_set_missing(rval[ir]); \ |
| 2288 |
continue; \ |
| 2289 |
} \ |
| 2290 |
has_values = 1; \ |
| 2291 |
rval[ir] = TYPE aval[ia] AOP TYPE bval[ib]; \ |
| 2292 |
} \ |
| 2293 |
} \ |
| 2294 |
} \ |
| 2295 |
else if ( atok->nsamples ) \ |
| 2296 |
{ \ |
| 2297 |
assert( btok->nvalues==1 ); \ |
| 2298 |
if ( !bcf_double_is_missing_or_vector_end(btok->values[0]) ) \ |
| 2299 |
{ \ |
| 2300 |
for (i=0; i<atok->nvalues; i++) \ |
| 2301 |
{ \ |
| 2302 |
if ( bcf_double_is_missing_or_vector_end(atok->values[i]) ) \ |
| 2303 |
{ \ |
| 2304 |
bcf_double_set_missing(rtok->values[i]); \ |
| 2305 |
continue; \ |
| 2306 |
} \ |
| 2307 |
has_values = 1; \ |
| 2308 |
rtok->values[i] = TYPE atok->values[i] AOP TYPE btok->values[0]; \ |
| 2309 |
} \ |
| 2310 |
} \ |
| 2311 |
} \ |
| 2312 |
else \ |
| 2313 |
{ \ |
| 2314 |
assert( atok->nvalues==1 ); \ |
| 2315 |
if ( !bcf_double_is_missing_or_vector_end(atok->values[0]) ) \ |
| 2316 |
{ \ |
| 2317 |
for (i=0; i<btok->nvalues; i++) \ |
| 2318 |
{ \ |
| 2319 |
if ( bcf_double_is_missing_or_vector_end(btok->values[i]) ) \ |
| 2320 |
{ \ |
| 2321 |
bcf_double_set_missing(rtok->values[i]); \ |
| 2322 |
continue; \ |
| 2323 |
} \ |
| 2324 |
has_values = 1; \ |
| 2325 |
rtok->values[i] = TYPE atok->values[0] AOP TYPE btok->values[i]; \ |
| 2326 |
} \ |
| 2327 |
} \ |
| 2328 |
} \ |
| 2329 |
} \ |
| 2330 |
if ( !has_values ) rtok->nvalues = 0; \ |
| 2331 |
} |
| 2332 |
|
| 2333 |
static int vector_logic_or(filter_t *filter, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 2334 |
{ |
| 2335 |
if ( nstack < 2 ) error("Error occurred while processing the filter \"%s\"\n", filter->str); |
| 2336 |
|
| 2337 |
token_t *atok = stack[nstack-2]; |
| 2338 |
token_t *btok = stack[nstack-1]; |
| 2339 |
tok_init_samples(atok, btok, rtok); |
| 2340 |
|
| 2341 |
if ( !atok->pass_site && !btok->pass_site ) return 2; |
| 2342 |
|
| 2343 |
rtok->pass_site = 1; |
| 2344 |
if ( !atok->nsamples && !btok->nsamples ) return 2; |
| 2345 |
|
| 2346 |
int i; |
| 2347 |
if ( rtok->tok_type==TOK_OR_VEC ) |
| 2348 |
{ |
| 2349 |
if ( (!atok->nsamples && !atok->pass_site) || (!btok->nsamples && !btok->pass_site) ) |
| 2350 |
{ |
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
token_t *tok = atok->nsamples ? atok : btok; |
| 2356 |
for (i=0; i<rtok->nsamples; i++) |
| 2357 |
{ |
| 2358 |
if ( !rtok->usmpl[i] ) continue; |
| 2359 |
rtok->pass_samples[i] = tok->pass_samples[i]; |
| 2360 |
} |
| 2361 |
} |
| 2362 |
else |
| 2363 |
{ |
| 2364 |
for (i=0; i<rtok->nsamples; i++) |
| 2365 |
{ |
| 2366 |
if ( !rtok->usmpl[i] ) continue; |
| 2367 |
rtok->pass_samples[i] = 1; |
| 2368 |
} |
| 2369 |
} |
| 2370 |
return 2; |
| 2371 |
} |
| 2372 |
|
| 2373 |
|
| 2374 |
|
| 2375 |
if ( !atok->nsamples || !btok->nsamples ) |
| 2376 |
{ |
| 2377 |
token_t *tok = atok->nsamples ? atok : btok; |
| 2378 |
for (i=0; i<rtok->nsamples; i++) |
| 2379 |
{ |
| 2380 |
if ( !rtok->usmpl[i] ) continue; |
| 2381 |
rtok->pass_samples[i] = tok->pass_samples[i]; |
| 2382 |
} |
| 2383 |
return 2; |
| 2384 |
} |
| 2385 |
|
| 2386 |
assert( atok->nsamples==btok->nsamples ); |
| 2387 |
|
| 2388 |
for (i=0; i<rtok->nsamples; i++) |
| 2389 |
{ |
| 2390 |
if ( !rtok->usmpl[i] ) continue; |
| 2391 |
rtok->pass_samples[i] = atok->pass_samples[i] | btok->pass_samples[i]; |
| 2392 |
} |
| 2393 |
return 2; |
| 2394 |
} |
| 2395 |
static int vector_logic_and(filter_t *filter, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 2396 |
{ |
| 2397 |
if ( nstack < 2 ) error("Error occurred while processing the filter \"%s\". (nstack=%d)\n", filter->str,nstack); |
| 2398 |
|
| 2399 |
token_t *atok = stack[nstack-2]; |
| 2400 |
token_t *btok = stack[nstack-1]; |
| 2401 |
tok_init_samples(atok, btok, rtok); |
| 2402 |
|
| 2403 |
if ( !atok->pass_site || !btok->pass_site ) return 2; |
| 2404 |
if ( !atok->nsamples && !btok->nsamples ) { rtok->pass_site = 1; return 2; } |
| 2405 |
|
| 2406 |
int i; |
| 2407 |
if ( !atok->nsamples || !btok->nsamples ) |
| 2408 |
{ |
| 2409 |
token_t *tok = atok->nsamples ? atok : btok; |
| 2410 |
for (i=0; i<rtok->nsamples; i++) |
| 2411 |
{ |
| 2412 |
if ( !rtok->usmpl[i] ) continue; |
| 2413 |
rtok->pass_samples[i] = tok->pass_samples[i]; |
| 2414 |
} |
| 2415 |
rtok->pass_site = 1; |
| 2416 |
return 2; |
| 2417 |
} |
| 2418 |
|
| 2419 |
assert( atok->nsamples==btok->nsamples ); |
| 2420 |
if ( rtok->tok_type==TOK_AND_VEC ) |
| 2421 |
{ |
| 2422 |
for (i=0; i<rtok->nsamples; i++) |
| 2423 |
{ |
| 2424 |
if ( !rtok->usmpl[i] ) continue; |
| 2425 |
rtok->pass_samples[i] = atok->pass_samples[i] | btok->pass_samples[i]; |
| 2426 |
} |
| 2427 |
rtok->pass_site = 1; |
| 2428 |
} |
| 2429 |
else |
| 2430 |
{ |
| 2431 |
for (i=0; i<rtok->nsamples; i++) |
| 2432 |
{ |
| 2433 |
if ( !rtok->usmpl[i] ) continue; |
| 2434 |
rtok->pass_samples[i] = atok->pass_samples[i] & btok->pass_samples[i]; |
| 2435 |
if ( rtok->pass_samples[i] ) rtok->pass_site = 1; |
| 2436 |
} |
| 2437 |
} |
| 2438 |
return 2; |
| 2439 |
} |
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
#define CMP_VECTORS(atok,btok,_rtok,CMP_OP,missing_logic) \ |
| 2454 |
{ \ |
| 2455 |
token_t *rtok = _rtok; \ |
| 2456 |
int i, j, k; \ |
| 2457 |
tok_init_samples(atok, btok, rtok); \ |
| 2458 |
if ( !atok->nsamples && !btok->nsamples ) \ |
| 2459 |
{ \ |
| 2460 |
if ( !atok->nvalues && !btok->nvalues ) { rtok->pass_site = missing_logic[2]; } \ |
| 2461 |
else if ( !atok->nvalues || !btok->nvalues ) \ |
| 2462 |
{ \ |
| 2463 |
token_t *tok = atok->nvalues ? atok : btok; \ |
| 2464 |
for (j=0; j<tok->nvalues; j++) \ |
| 2465 |
{ \ |
| 2466 |
if ( bcf_double_is_missing_or_vector_end(tok->values[j]) ) \ |
| 2467 |
{ \ |
| 2468 |
if ( missing_logic[2] ) { rtok->pass_site = 1; break; } \ |
| 2469 |
} \ |
| 2470 |
else if ( missing_logic[1] ) { rtok->pass_site = 1; break; } \ |
| 2471 |
} \ |
| 2472 |
} \ |
| 2473 |
else \ |
| 2474 |
{ \ |
| 2475 |
for (i=0; i<atok->nvalues; i++) \ |
| 2476 |
{ \ |
| 2477 |
int amiss = bcf_double_is_missing_or_vector_end(atok->values[i]) ? 1 : 0; \ |
| 2478 |
for (j=0; j<btok->nvalues; j++) \ |
| 2479 |
{ \ |
| 2480 |
int nmiss = amiss + (bcf_double_is_missing_or_vector_end(btok->values[j]) ? 1 : 0); \ |
| 2481 |
if ( nmiss ) \ |
| 2482 |
{ \ |
| 2483 |
if ( missing_logic[nmiss] ) { rtok->pass_site = 1; i = atok->nvalues; break; } \ |
| 2484 |
} \ |
| 2485 |
else if ( atok->values[i] > 16777216 || btok->values[j] > 16777216 ) \ |
| 2486 |
{ \ |
| 2487 |
if ( atok->values[i] CMP_OP btok->values[j] ) { rtok->pass_site = 1; i = atok->nvalues; break; } \ |
| 2488 |
} \ |
| 2489 |
else if ( (float)atok->values[i] CMP_OP (float)btok->values[j] ) { rtok->pass_site = 1; i = atok->nvalues; break; } \ |
| 2490 |
} \ |
| 2491 |
} \ |
| 2492 |
} \ |
| 2493 |
} \ |
| 2494 |
else \ |
| 2495 |
{ \ |
| 2496 |
if ( !atok->nvalues && !btok->nvalues ) \ |
| 2497 |
{ \ |
| 2498 |
if ( missing_logic[2] ) \ |
| 2499 |
{ \ |
| 2500 |
for (i=0; i<rtok->nsamples; i++) \ |
| 2501 |
if ( rtok->usmpl[i] ) { rtok->pass_samples[i] = missing_logic[2]; rtok->pass_site = 1; } \ |
| 2502 |
} \ |
| 2503 |
} \ |
| 2504 |
else if ( !atok->nvalues || !btok->nvalues ) \ |
| 2505 |
{ \ |
| 2506 |
token_t *tok = atok->nvalues ? atok : btok; \ |
| 2507 |
if ( !tok->nsamples ) \ |
| 2508 |
{ \ |
| 2509 |
int miss = 0; \ |
| 2510 |
for (j=0; j<tok->nvalues; j++) \ |
| 2511 |
miss |= bcf_double_is_missing_or_vector_end(tok->values[j]) ? 1 : 0; \ |
| 2512 |
if ( missing_logic[++miss] ) \ |
| 2513 |
{ \ |
| 2514 |
for (i=0; i<rtok->nsamples; i++) \ |
| 2515 |
if ( rtok->usmpl[i] ) { rtok->pass_samples[i] = missing_logic[miss]; rtok->pass_site = 1; } \ |
| 2516 |
} \ |
| 2517 |
} \ |
| 2518 |
else \ |
| 2519 |
for (i=0; i<tok->nsamples; i++) \ |
| 2520 |
{ \ |
| 2521 |
if ( !rtok->usmpl[i] ) continue; \ |
| 2522 |
double *ptr = tok->values + i*tok->nval1; \ |
| 2523 |
int miss = 0; \ |
| 2524 |
for (j=0; j<tok->nval1; j++) \ |
| 2525 |
miss |= bcf_double_is_missing_or_vector_end(ptr[j]) ? 1 : 0; \ |
| 2526 |
if ( missing_logic[++miss] ) { rtok->pass_samples[i] = missing_logic[miss]; rtok->pass_site = 1; } \ |
| 2527 |
} \ |
| 2528 |
} \ |
| 2529 |
else if ( atok->nsamples && btok->nsamples ) \ |
| 2530 |
{ \ |
| 2531 |
if ( atok->nval1!=btok->nval1 ) error("Incompatible number of per-sample values in comparison: %d vs %d\n",atok->nval1,btok->nval1); \ |
| 2532 |
if ( atok->nsamples!=btok->nsamples ) error("Incompatible number samples in comparison: %d vs %d\n",atok->nsamples,btok->nsamples); \ |
| 2533 |
for (i=0; i<atok->nsamples; i++) \ |
| 2534 |
{ \ |
| 2535 |
if ( !atok->usmpl[i] || !btok->usmpl[i] ) { rtok->usmpl[i] = 0; continue; } \ |
| 2536 |
double *aptr = atok->values + i*atok->nval1; \ |
| 2537 |
double *bptr = btok->values + i*btok->nval1; \ |
| 2538 |
for (j=0; j<atok->nval1; j++) \ |
| 2539 |
{ \ |
| 2540 |
if ( atok->idx==-3 && bcf_double_is_vector_end(aptr[j]) ) break; \ |
| 2541 |
if ( btok->idx==-3 && bcf_double_is_vector_end(bptr[j]) ) break; \ |
| 2542 |
int nmiss = bcf_double_is_missing_or_vector_end(aptr[j]) ? 1 : 0; \ |
| 2543 |
if ( nmiss && !missing_logic[0] ) continue; \ |
| 2544 |
nmiss += (bcf_double_is_missing_or_vector_end(bptr[j]) ? 1 : 0); \ |
| 2545 |
if ( nmiss ) \ |
| 2546 |
{ \ |
| 2547 |
if ( missing_logic[nmiss] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; break; } \ |
| 2548 |
} \ |
| 2549 |
else if ( aptr[j] > 16777216 || bptr[j] > 16777216 ) \ |
| 2550 |
{ \ |
| 2551 |
if ( aptr[j] CMP_OP bptr[j] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; break; } \ |
| 2552 |
} \ |
| 2553 |
else if ( (float)aptr[j] CMP_OP (float)bptr[j] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; break; } \ |
| 2554 |
} \ |
| 2555 |
} \ |
| 2556 |
} \ |
| 2557 |
else if ( atok->nsamples )\ |
| 2558 |
{ \ |
| 2559 |
for (i=0; i<atok->nsamples; i++) \ |
| 2560 |
{ \ |
| 2561 |
if ( !rtok->usmpl[i] ) continue; \ |
| 2562 |
double *aptr = atok->values + i*atok->nval1; \ |
| 2563 |
double *bptr = btok->values; \ |
| 2564 |
for (j=0; j<atok->nval1; j++) \ |
| 2565 |
{ \ |
| 2566 |
if ( atok->idx==-3 && bcf_double_is_vector_end(aptr[j]) ) break; \ |
| 2567 |
int miss = bcf_double_is_missing_or_vector_end(aptr[j]) ? 1 : 0; \ |
| 2568 |
if ( miss && !missing_logic[0] ) continue; \ |
| 2569 |
for (k=0; k<btok->nvalues; k++) \ |
| 2570 |
{ \ |
| 2571 |
int nmiss = miss + (bcf_double_is_missing_or_vector_end(bptr[k]) ? 1 : 0); \ |
| 2572 |
if ( nmiss ) \ |
| 2573 |
{ \ |
| 2574 |
if ( missing_logic[nmiss] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; j = atok->nval1; break; } \ |
| 2575 |
} \ |
| 2576 |
else if ( aptr[j] > 16777216 || bptr[k] > 16777216 ) \ |
| 2577 |
{ \ |
| 2578 |
if ( aptr[j] CMP_OP bptr[k] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; j = atok->nval1; break; } \ |
| 2579 |
} \ |
| 2580 |
else if ( (float)aptr[j] CMP_OP (float)bptr[k] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; j = atok->nval1; break; } \ |
| 2581 |
} \ |
| 2582 |
} \ |
| 2583 |
} \ |
| 2584 |
} \ |
| 2585 |
else \ |
| 2586 |
{ \ |
| 2587 |
for (i=0; i<btok->nsamples; i++) \ |
| 2588 |
{ \ |
| 2589 |
if ( !rtok->usmpl[i] ) continue; \ |
| 2590 |
double *aptr = atok->values; \ |
| 2591 |
double *bptr = btok->values + i*btok->nval1; \ |
| 2592 |
for (j=0; j<btok->nval1; j++) \ |
| 2593 |
{ \ |
| 2594 |
if ( atok->idx==-3 && bcf_double_is_vector_end(bptr[j]) ) break; \ |
| 2595 |
int miss = bcf_double_is_missing_or_vector_end(bptr[j]) ? 1 : 0; \ |
| 2596 |
if ( miss && !missing_logic[0] ) continue; \ |
| 2597 |
for (k=0; k<atok->nvalues; k++) \ |
| 2598 |
{ \ |
| 2599 |
int nmiss = miss + (bcf_double_is_missing_or_vector_end(aptr[k]) ? 1 : 0); \ |
| 2600 |
if ( nmiss ) \ |
| 2601 |
{ \ |
| 2602 |
if ( missing_logic[nmiss] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; j = btok->nval1; break; } \ |
| 2603 |
} \ |
| 2604 |
else if ( bptr[j] > 16777216 || aptr[k] > 16777216 ) \ |
| 2605 |
{ \ |
| 2606 |
if ( aptr[k] CMP_OP bptr[j] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; j = btok->nval1; break; } \ |
| 2607 |
} \ |
| 2608 |
else if ( (float)aptr[k] CMP_OP (float)bptr[j] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; j = btok->nval1; break; } \ |
| 2609 |
} \ |
| 2610 |
} \ |
| 2611 |
} \ |
| 2612 |
} \ |
| 2613 |
} \ |
| 2614 |
} |
| 2615 |
static int _regex_vector_strings(regex_t *regex, char *str, size_t len, int logic, int *missing_logic) |
| 2616 |
{ |
| 2617 |
char *end = str + len; |
| 2618 |
while ( str < end && *str ) |
| 2619 |
{ |
| 2620 |
char *mid = str; |
| 2621 |
while ( mid < end && *mid && *mid!=',' ) mid++; |
| 2622 |
int miss = mid - str == 1 && str[0]=='.' ? 1 : 0; |
| 2623 |
if ( miss && missing_logic[miss] ) return 1; |
| 2624 |
char tmp = *mid; *mid = 0; |
| 2625 |
int match = regexec(regex, str, 0,NULL,0) ? 0 : 1; |
| 2626 |
*mid = tmp; |
| 2627 |
if ( logic==TOK_NLIKE ) match = match ? 0 : 1; |
| 2628 |
if ( match ) return 1; |
| 2629 |
if ( !*mid ) break; |
| 2630 |
str = mid + 1; |
| 2631 |
} |
| 2632 |
return 0; |
| 2633 |
} |
| 2634 |
static inline int _has_missing_string(char *beg) |
| 2635 |
{ |
| 2636 |
while ( *beg ) |
| 2637 |
{ |
| 2638 |
char *end = beg; |
| 2639 |
while ( *end && *end!=',' ) end++; |
| 2640 |
if ( end-beg==1 && beg[0]=='.' ) return 1; |
| 2641 |
if ( !*end ) break; |
| 2642 |
beg = end + 1; |
| 2643 |
} |
| 2644 |
return 0; |
| 2645 |
} |
| 2646 |
|
| 2647 |
|
| 2648 |
|
| 2649 |
static inline int _match_vector_strings(char *abeg, size_t alen, char *bstr, size_t blen, int logic, int *missing_logic) |
| 2650 |
{ |
| 2651 |
char *aend = abeg + alen; |
| 2652 |
char *bend = bstr + blen; |
| 2653 |
while ( abeg < aend && *abeg ) |
| 2654 |
{ |
| 2655 |
char *amid = abeg; |
| 2656 |
while ( amid < aend && *amid && *amid!=',' ) amid++; |
| 2657 |
int miss = amid - abeg == 1 && abeg[0]=='.' ? 1 : 0; |
| 2658 |
char *bbeg = bstr; |
| 2659 |
while ( bbeg < bend && *bbeg ) |
| 2660 |
{ |
| 2661 |
char *bmid = bbeg; |
| 2662 |
while ( bmid < bend && *bmid && *bmid!=',' ) bmid++; |
| 2663 |
int nmiss = miss + (bmid - bbeg == 1 && bbeg[0]=='.' ? 1 : 0); |
| 2664 |
if ( nmiss ) |
| 2665 |
{ |
| 2666 |
if ( missing_logic[nmiss] ) return 1; |
| 2667 |
} |
| 2668 |
else |
| 2669 |
{ |
| 2670 |
int match = amid-abeg==bmid-bbeg && !strncmp(abeg,bbeg,amid-abeg) ? 1 : 0; |
| 2671 |
if ( logic==TOK_NE ) match = match==1 ? 0 : 1; |
| 2672 |
if ( match ) return 1; |
| 2673 |
} |
| 2674 |
if ( !*bmid ) break; |
| 2675 |
bbeg = bmid + 1; |
| 2676 |
} |
| 2677 |
if ( !*amid ) break; |
| 2678 |
abeg = amid + 1; |
| 2679 |
} |
| 2680 |
return 0; |
| 2681 |
} |
| 2682 |
static void cmp_vector_strings(token_t *atok, token_t *btok, token_t *rtok) |
| 2683 |
{ |
| 2684 |
tok_init_samples(atok, btok, rtok); |
| 2685 |
|
| 2686 |
int i, logic = rtok->tok_type; |
| 2687 |
regex_t *regex = atok->regex ? atok->regex : (btok->regex ? btok->regex : NULL); |
| 2688 |
|
| 2689 |
assert( atok->nvalues==atok->str_value.l && btok->nvalues==btok->str_value.l ); |
| 2690 |
assert( !atok->nsamples || !btok->nsamples ); |
| 2691 |
assert( (!regex && (logic==TOK_EQ || logic==TOK_NE)) || (regex && (logic==TOK_LIKE || logic==TOK_NLIKE)) ); |
| 2692 |
|
| 2693 |
int missing_logic[] = {0,0,0}; |
| 2694 |
if ( logic==TOK_EQ || logic==TOK_LIKE ) missing_logic[0] = missing_logic[2] = 1; |
| 2695 |
else if ( logic==TOK_NE || logic==TOK_NLIKE ) missing_logic[0] = missing_logic[1] = 1; |
| 2696 |
|
| 2697 |
if ( !atok->nsamples && !btok->nsamples ) |
| 2698 |
{ |
| 2699 |
if ( !atok->nvalues && !btok->nvalues ) { rtok->pass_site = missing_logic[2]; return; } |
| 2700 |
if ( !atok->nvalues || !btok->nvalues ) |
| 2701 |
{ |
| 2702 |
int miss = _has_missing_string(atok->nvalues ? atok->str_value.s : btok->str_value.s); |
| 2703 |
if ( missing_logic[miss+1] ) rtok->pass_site = 1; |
| 2704 |
return; |
| 2705 |
} |
| 2706 |
if ( !regex ) |
| 2707 |
rtok->pass_site = _match_vector_strings(atok->str_value.s, atok->str_value.l, btok->str_value.s, btok->str_value.l, logic, missing_logic); |
| 2708 |
else |
| 2709 |
{ |
| 2710 |
token_t *tok = atok->regex ? btok : atok; |
| 2711 |
rtok->pass_site = _regex_vector_strings(regex, tok->str_value.s, tok->str_value.l, logic, missing_logic); |
| 2712 |
} |
| 2713 |
return; |
| 2714 |
} |
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
if ( !atok->nvalues && !btok->nvalues ) |
| 2719 |
{ |
| 2720 |
if ( missing_logic[2] ) |
| 2721 |
{ |
| 2722 |
for (i=0; i<rtok->nsamples; i++) |
| 2723 |
if ( rtok->usmpl[i] ) { rtok->pass_samples[i] = missing_logic[2]; rtok->pass_site = 1; } |
| 2724 |
} |
| 2725 |
return; |
| 2726 |
} |
| 2727 |
if ( !atok->nvalues || !btok->nvalues ) |
| 2728 |
{ |
| 2729 |
token_t *tok = atok->nvalues ? atok : btok; |
| 2730 |
if ( !tok->nsamples ) |
| 2731 |
{ |
| 2732 |
int miss = _has_missing_string(tok->str_value.s); |
| 2733 |
if ( !missing_logic[miss+1] ) return; |
| 2734 |
for (i=0; i<rtok->nsamples; i++) |
| 2735 |
if ( rtok->usmpl[i] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; } |
| 2736 |
} |
| 2737 |
else |
| 2738 |
for (i=0; i<tok->nsamples; i++) |
| 2739 |
{ |
| 2740 |
if ( !rtok->usmpl[i] ) continue; |
| 2741 |
int miss = _has_missing_string(tok->str_value.s + i*tok->nval1); |
| 2742 |
if ( missing_logic[miss+1] ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; } |
| 2743 |
} |
| 2744 |
return; |
| 2745 |
} |
| 2746 |
|
| 2747 |
|
| 2748 |
token_t *xtok = atok->nsamples ? atok : btok; |
| 2749 |
token_t *ytok = atok->nsamples ? btok : atok; |
| 2750 |
assert( regex==ytok->regex ); |
| 2751 |
for (i=0; i<xtok->nsamples; i++) |
| 2752 |
{ |
| 2753 |
if ( !rtok->usmpl[i] ) continue; |
| 2754 |
int match; |
| 2755 |
if ( regex ) |
| 2756 |
match = _regex_vector_strings(regex, xtok->str_value.s + i*xtok->nval1, xtok->nval1, logic, missing_logic); |
| 2757 |
else |
| 2758 |
match = _match_vector_strings(xtok->str_value.s + i*xtok->nval1, xtok->nval1, ytok->str_value.s, ytok->str_value.l, logic, missing_logic); |
| 2759 |
if ( match ) { rtok->pass_samples[i] = 1; rtok->pass_site = 1; } |
| 2760 |
} |
| 2761 |
} |
| 2762 |
|
| 2763 |
static int parse_idxs(char *tag_idx, int **idxs, int *nidxs, int *idx) |
| 2764 |
{ |
| 2765 |
|
| 2766 |
if ( *tag_idx==0 || !strcmp("*", tag_idx) ) |
| 2767 |
{ |
| 2768 |
*idxs = (int*) malloc(sizeof(int)); |
| 2769 |
(*idxs)[0] = -1; |
| 2770 |
*nidxs = 1; |
| 2771 |
*idx = -2; |
| 2772 |
return 0; |
| 2773 |
} |
| 2774 |
if ( !strcmp("GT", tag_idx) ) |
| 2775 |
{ |
| 2776 |
*idxs = (int*) malloc(sizeof(int)); |
| 2777 |
(*idxs)[0] = -1; |
| 2778 |
*nidxs = 1; |
| 2779 |
*idx = -3; |
| 2780 |
return 0; |
| 2781 |
} |
| 2782 |
|
| 2783 |
|
| 2784 |
char *end, *beg = tag_idx; |
| 2785 |
*idx = strtol(tag_idx, &end, 10); |
| 2786 |
if ( *idx >= 0 && *end==0 ) return 0; |
| 2787 |
|
| 2788 |
|
| 2789 |
int i, ibeg = -1; |
| 2790 |
while ( *beg ) |
| 2791 |
{ |
| 2792 |
int num = strtol(beg, &end, 10); |
| 2793 |
if ( end[0]==',' ) beg = end + 1; |
| 2794 |
else if ( end[0]==0 ) beg = end; |
| 2795 |
else if ( end[0]=='-' ) { beg = end + 1; ibeg = num; continue; } |
| 2796 |
else return -1; |
| 2797 |
if ( num >= *nidxs ) |
| 2798 |
{ |
| 2799 |
*idxs = (int*) realloc(*idxs, sizeof(int)*(num+1)); |
| 2800 |
memset(*idxs + *nidxs, 0, sizeof(int)*(num - *nidxs + 1)); |
| 2801 |
*nidxs = num + 1; |
| 2802 |
} |
| 2803 |
if ( ibeg>=0 ) |
| 2804 |
{ |
| 2805 |
for (i=ibeg; i<=num; i++) (*idxs)[i] = 1; |
| 2806 |
ibeg = -1; |
| 2807 |
} |
| 2808 |
(*idxs)[num] = 1; |
| 2809 |
} |
| 2810 |
if ( ibeg >=0 ) |
| 2811 |
{ |
| 2812 |
if ( ibeg >= *nidxs ) |
| 2813 |
{ |
| 2814 |
*idxs = (int*) realloc(*idxs, sizeof(int)*(ibeg+1)); |
| 2815 |
memset(*idxs + *nidxs, 0, sizeof(int)*(ibeg - *nidxs + 1)); |
| 2816 |
*nidxs = ibeg + 1; |
| 2817 |
} |
| 2818 |
(*idxs)[ibeg] = -1; |
| 2819 |
} |
| 2820 |
*idx = -2; |
| 2821 |
return 0; |
| 2822 |
} |
| 2823 |
|
| 2824 |
static void parse_tag_idx(bcf_hdr_t *hdr, int is_fmt, char *tag, char *tag_idx, token_t *tok) |
| 2825 |
{ |
| 2826 |
int i, len = strlen(tag_idx); |
| 2827 |
if ( tag_idx[len-1] == ']' ) tag_idx[len-1] = 0; |
| 2828 |
char *ori = strdup(tag_idx); |
| 2829 |
|
| 2830 |
assert( !tok->idxs && !tok->usmpl ); |
| 2831 |
int *idxs1 = NULL, nidxs1 = 0, idx1 = 0; |
| 2832 |
int *idxs2 = NULL, nidxs2 = 0, idx2 = 0; |
| 2833 |
|
| 2834 |
int set_samples = 0; |
| 2835 |
char *colon = strrchr(tag_idx, ':'); |
| 2836 |
if ( tag_idx[0]=='@' ) |
| 2837 |
{ |
| 2838 |
if ( !is_fmt ) error("Could not parse \"%s\". (Not a FORMAT tag yet a sample list provided.)\n", ori); |
| 2839 |
char *fname = expand_path(tag_idx+1); |
| 2840 |
#ifdef _WIN32 |
| 2841 |
if (fname && strlen(fname) > 2 && fname[1] == ':') |
| 2842 |
colon = strrchr(fname+2, ':'); |
| 2843 |
#endif |
| 2844 |
int nsmpl; |
| 2845 |
char **list = hts_readlist(fname, 1, &nsmpl); |
| 2846 |
if ( !list && colon ) |
| 2847 |
{ |
| 2848 |
if ( parse_idxs(colon+1, &idxs2, &nidxs2, &idx2) != 0 ) error("Could not parse the index: %s\n", ori); |
| 2849 |
tok->idxs = idxs2; |
| 2850 |
tok->nidxs = nidxs2; |
| 2851 |
tok->idx = idx2; |
| 2852 |
colon = strrchr(fname, ':'); |
| 2853 |
*colon = 0; |
| 2854 |
list = hts_readlist(fname, 1, &nsmpl); |
| 2855 |
} |
| 2856 |
if ( !list ) error("Could not read: %s\n", fname); |
| 2857 |
free(fname); |
| 2858 |
tok->nsamples = bcf_hdr_nsamples(hdr); |
| 2859 |
tok->usmpl = (uint8_t*) calloc(tok->nsamples,1); |
| 2860 |
for (i=0; i<nsmpl; i++) |
| 2861 |
{ |
| 2862 |
int ismpl = bcf_hdr_id2int(hdr,BCF_DT_SAMPLE,list[i]); |
| 2863 |
if ( ismpl<0 ) error("No such sample in the VCF: \"%s\"\n", list[i]); |
| 2864 |
free(list[i]); |
| 2865 |
tok->usmpl[ismpl] = 1; |
| 2866 |
} |
| 2867 |
free(list); |
| 2868 |
if ( !colon ) |
| 2869 |
{ |
| 2870 |
tok->idxs = (int*) malloc(sizeof(int)); |
| 2871 |
tok->idxs[0] = -1; |
| 2872 |
tok->nidxs = 1; |
| 2873 |
tok->idx = -2; |
| 2874 |
} |
| 2875 |
} |
| 2876 |
else if ( colon ) |
| 2877 |
{ |
| 2878 |
if ( !is_fmt ) error("Could not parse the index \"%s\". (Not a FORMAT tag yet sample index implied.)\n", ori); |
| 2879 |
*colon = 0; |
| 2880 |
if ( parse_idxs(tag_idx, &idxs1, &nidxs1, &idx1) != 0 ) error("Could not parse the index: %s\n", ori); |
| 2881 |
if ( parse_idxs(colon+1, &idxs2, &nidxs2, &idx2) != 0 ) error("Could not parse the index: %s\n", ori); |
| 2882 |
tok->idxs = idxs2; |
| 2883 |
tok->nidxs = nidxs2; |
| 2884 |
tok->idx = idx2; |
| 2885 |
set_samples = 1; |
| 2886 |
} |
| 2887 |
else |
| 2888 |
{ |
| 2889 |
if ( parse_idxs(tag_idx, &idxs1, &nidxs1, &idx1) != 0 ) error("Could not parse the index: %s\n", ori); |
| 2890 |
if ( is_fmt ) |
| 2891 |
{ |
| 2892 |
if ( nidxs1==1 && idxs1[0]==-1 ) |
| 2893 |
{ |
| 2894 |
tok->idxs = (int*) malloc(sizeof(int)); |
| 2895 |
tok->idxs[0] = -1; |
| 2896 |
tok->nidxs = 1; |
| 2897 |
tok->idx = idx1; |
| 2898 |
} |
| 2899 |
else if ( bcf_hdr_id2number(hdr,BCF_HL_FMT,tok->hdr_id)!=1 ) |
| 2900 |
error("The FORMAT tag %s can have multiple subfields, run as %s[sample:subfield]\n", tag,tag); |
| 2901 |
else |
| 2902 |
tok->idx = 0; |
| 2903 |
set_samples = 1; |
| 2904 |
} |
| 2905 |
else |
| 2906 |
{ |
| 2907 |
tok->idxs = idxs1; |
| 2908 |
tok->nidxs = nidxs1; |
| 2909 |
tok->idx = idx1; |
| 2910 |
} |
| 2911 |
} |
| 2912 |
|
| 2913 |
if ( set_samples ) |
| 2914 |
{ |
| 2915 |
tok->nsamples = bcf_hdr_nsamples(hdr); |
| 2916 |
tok->usmpl = (uint8_t*) calloc(tok->nsamples,1); |
| 2917 |
if ( idx1>=0 ) |
| 2918 |
{ |
| 2919 |
if ( idx1 >= bcf_hdr_nsamples(hdr) ) error("The sample index is too large: %s\n", ori); |
| 2920 |
tok->usmpl[idx1] = 1; |
| 2921 |
} |
| 2922 |
else if ( idx1==-2 || idx1==-3 ) |
| 2923 |
{ |
| 2924 |
for (i=0; i<nidxs1; i++) |
| 2925 |
{ |
| 2926 |
if ( idxs1[i]==0 ) continue; |
| 2927 |
if ( idxs1[i]==-1 ) break; |
| 2928 |
if ( i >= bcf_hdr_nsamples(hdr) ) error("The sample index is too large: %s\n", ori); |
| 2929 |
tok->usmpl[i] = 1; |
| 2930 |
} |
| 2931 |
if ( nidxs1 && idxs1[nidxs1-1]==-1 ) |
| 2932 |
{ |
| 2933 |
for (; i<tok->nsamples; i++) tok->usmpl[i] = 1; |
| 2934 |
} |
| 2935 |
} |
| 2936 |
else error("todo: %s:%d .. %d\n", __FILE__,__LINE__, idx2); |
| 2937 |
free(idxs1); |
| 2938 |
} |
| 2939 |
free(ori); |
| 2940 |
|
| 2941 |
if ( tok->nidxs && tok->idxs[tok->nidxs-1]!=-1 ) |
| 2942 |
{ |
| 2943 |
for (i=0; i<tok->nidxs; i++) if ( tok->idxs[i] ) tok->nuidxs++; |
| 2944 |
} |
| 2945 |
} |
| 2946 |
static int max_ac_an_unpack(bcf_hdr_t *hdr) |
| 2947 |
{ |
| 2948 |
int hdr_id = bcf_hdr_id2int(hdr,BCF_DT_ID,"AC"); |
| 2949 |
if ( hdr_id<0 ) return BCF_UN_FMT; |
| 2950 |
if ( !bcf_hdr_idinfo_exists(hdr,BCF_HL_INFO,hdr_id) ) return BCF_UN_FMT; |
| 2951 |
|
| 2952 |
hdr_id = bcf_hdr_id2int(hdr,BCF_DT_ID,"AN"); |
| 2953 |
if ( hdr_id<0 ) return BCF_UN_FMT; |
| 2954 |
if ( !bcf_hdr_idinfo_exists(hdr,BCF_HL_INFO,hdr_id) ) return BCF_UN_FMT; |
| 2955 |
|
| 2956 |
return BCF_UN_INFO; |
| 2957 |
} |
| 2958 |
static int filters_init1_ext(filter_t *filter, char *str, int len, token_t *tok) |
| 2959 |
{ |
| 2960 |
tok->hl_type = -1; |
| 2961 |
tok->ht_type = -1; |
| 2962 |
tok->tok_type = TOK_VAL; |
| 2963 |
tok->hdr_id = -1; |
| 2964 |
tok->pass_site = -1; |
| 2965 |
tok->idx = 0; |
| 2966 |
tok->iext = ++filter->n_ext; |
| 2967 |
filter->ext = realloc(filter->ext,sizeof(*filter->ext)*filter->n_ext); |
| 2968 |
if ( !strncasecmp(str,"{str}",len) ) { tok->ht_type = BCF_HT_STR; tok->is_str = 1; } |
| 2969 |
else if ( !strncasecmp(str,"{int}",len) ) tok->ht_type = BCF_HT_INT; |
| 2970 |
else if ( !strncasecmp(str,"{float}",len) ) tok->ht_type = BCF_HT_REAL; |
| 2971 |
filter->ext[filter->n_ext-1] = tok->ht_type; |
| 2972 |
return 0; |
| 2973 |
} |
| 2974 |
static int filters_init1(filter_t *filter, char *str, int len, token_t *tok) |
| 2975 |
{ |
| 2976 |
tok->ht_type = -1; |
| 2977 |
tok->hl_type = -1; |
| 2978 |
tok->tok_type = TOK_VAL; |
| 2979 |
tok->hdr_id = -1; |
| 2980 |
tok->pass_site = -1; |
| 2981 |
tok->idx = 0; |
| 2982 |
|
| 2983 |
|
| 2984 |
if ( str[0]=='"' || str[0]=='\'' ) |
| 2985 |
{ |
| 2986 |
int quote = str[0]; |
| 2987 |
if ( str[len-1] != quote ) error("TODO: [%s]\n", filter->str); |
| 2988 |
tok->key = (char*) calloc(len-1,sizeof(char)); |
| 2989 |
hts_expand(double,1,tok->mvalues,tok->values); |
| 2990 |
tok->values[0] = len-2; |
| 2991 |
memcpy(tok->key,str+1,len-2); |
| 2992 |
tok->key[len-2] = 0; |
| 2993 |
tok->is_str = 1; |
| 2994 |
tok->ht_type = BCF_HT_STR; |
| 2995 |
tok->nvalues = len-2; |
| 2996 |
if ( !strcmp(".",tok->key) ) tok->is_missing = 1; |
| 2997 |
return 0; |
| 2998 |
} |
| 2999 |
|
| 3000 |
|
| 3001 |
if ( str[0]=='@' ) |
| 3002 |
{ |
| 3003 |
tok->tag = (char*) calloc(len+1,sizeof(char)); |
| 3004 |
memcpy(tok->tag,str,len); |
| 3005 |
tok->tag[len] = 0; |
| 3006 |
char *fname = expand_path(tok->tag+1); |
| 3007 |
int i, n; |
| 3008 |
char **list = hts_readlist(fname, 1, &n); |
| 3009 |
if ( !list ) error("Could not read: %s\n", fname); |
| 3010 |
free(fname); |
| 3011 |
tok->hash = khash_str2int_init(); |
| 3012 |
for (i=0; i<n; i++) |
| 3013 |
{ |
| 3014 |
char *se = list[i]; |
| 3015 |
while ( *se && !isspace(*se) ) se++; |
| 3016 |
*se = 0; |
| 3017 |
if ( !khash_str2int_has_key(tok->hash,list[i]) ) |
| 3018 |
khash_str2int_inc(tok->hash,list[i]); |
| 3019 |
else |
| 3020 |
free(list[i]); |
| 3021 |
} |
| 3022 |
free(list); |
| 3023 |
return 0; |
| 3024 |
} |
| 3025 |
|
| 3026 |
int is_fmt = -1; |
| 3027 |
if ( !strncasecmp(str,"FMT/",4) ) { str += 4; len -= 4; is_fmt = 1; } |
| 3028 |
else if ( !strncasecmp(str,"FORMAT/",7) ) { str += 7; len -= 7; is_fmt = 1; } |
| 3029 |
else |
| 3030 |
{ |
| 3031 |
if ( !strncasecmp(str,"INFO/",5) ) { is_fmt = 0; str += 5; len -= 5; } |
| 3032 |
else if ( !strncasecmp(str,"QUAL",len) || !strncmp(str,"%QUAL",len) ) |
| 3033 |
{ |
| 3034 |
tok->setter = filters_set_qual; |
| 3035 |
tok->tag = strdup("QUAL"); |
| 3036 |
tok->ht_type = BCF_HT_REAL; |
| 3037 |
filter_add_used_tag(filter,NULL,tok->tag); |
| 3038 |
return 0; |
| 3039 |
} |
| 3040 |
else if ( !strncasecmp(str,"TYPE",len) || !strncmp(str,"%TYPE",len) ) |
| 3041 |
{ |
| 3042 |
tok->setter = filters_set_type; |
| 3043 |
tok->tag = strdup("TYPE"); |
| 3044 |
tok->ht_type = BCF_HT_STR; |
| 3045 |
return 0; |
| 3046 |
} |
| 3047 |
else if ( !strncasecmp(str,"FILTER",len) || !strncmp(str,"%FILTER",len) ) |
| 3048 |
{ |
| 3049 |
tok->comparator = filters_cmp_filter; |
| 3050 |
tok->tag = strdup("FILTER"); |
| 3051 |
filter->max_unpack |= BCF_UN_FLT; |
| 3052 |
tok->hl_type = BCF_HL_FLT; |
| 3053 |
tok->ht_type = BCF_HT_STR; |
| 3054 |
filter_add_used_tag(filter,NULL,tok->tag); |
| 3055 |
return 0; |
| 3056 |
} |
| 3057 |
else if ( !strncasecmp(str,"ID",len) || !strncasecmp(str,"%ID",len) ) |
| 3058 |
{ |
| 3059 |
tok->comparator = filters_cmp_id; |
| 3060 |
tok->tag = strdup("ID"); |
| 3061 |
tok->ht_type = BCF_HT_STR; |
| 3062 |
filter_add_used_tag(filter,NULL,tok->tag); |
| 3063 |
return 0; |
| 3064 |
} |
| 3065 |
else if ( !strncasecmp(str,"CHROM",len) ) |
| 3066 |
{ |
| 3067 |
tok->setter = &filters_set_chrom; |
| 3068 |
tok->tag = strdup("CHROM"); |
| 3069 |
tok->ht_type = BCF_HT_STR; |
| 3070 |
filter_add_used_tag(filter,NULL,tok->tag); |
| 3071 |
return 0; |
| 3072 |
} |
| 3073 |
else if ( !strncasecmp(str,"POS",len) ) |
| 3074 |
{ |
| 3075 |
tok->setter = &filters_set_pos; |
| 3076 |
tok->tag = strdup("POS"); |
| 3077 |
tok->ht_type = BCF_HT_INT; |
| 3078 |
filter_add_used_tag(filter,NULL,tok->tag); |
| 3079 |
return 0; |
| 3080 |
} |
| 3081 |
else if ( !strncasecmp(str,"REF",len) ) |
| 3082 |
{ |
| 3083 |
tok->setter = &filters_set_ref_string; |
| 3084 |
tok->is_str = 1; |
| 3085 |
tok->tag = strdup("REF"); |
| 3086 |
tok->ht_type = BCF_HT_STR; |
| 3087 |
filter_add_used_tag(filter,NULL,tok->tag); |
| 3088 |
return 0; |
| 3089 |
} |
| 3090 |
else if ( !strncasecmp(str,"ALT",len) ) |
| 3091 |
{ |
| 3092 |
tok->setter = &filters_set_alt_string; |
| 3093 |
tok->is_str = 1; |
| 3094 |
tok->tag = strdup("ALT"); |
| 3095 |
tok->ht_type = BCF_HT_STR; |
| 3096 |
tok->idxs = (int*) malloc(sizeof(int)); |
| 3097 |
tok->idxs[0] = -1; |
| 3098 |
tok->nidxs = 1; |
| 3099 |
tok->idx = -2; |
| 3100 |
filter_add_used_tag(filter,NULL,tok->tag); |
| 3101 |
return 0; |
| 3102 |
} |
| 3103 |
else if ( !strncasecmp(str,"N_ALT",len) ) |
| 3104 |
{ |
| 3105 |
tok->setter = &filters_set_nalt; |
| 3106 |
tok->tag = strdup("N_ALT"); |
| 3107 |
tok->ht_type = BCF_HT_INT; |
| 3108 |
return 0; |
| 3109 |
} |
| 3110 |
else if ( !strncasecmp(str,"N_SAMPLES",len) ) |
| 3111 |
{ |
| 3112 |
tok->tok_type = TOK_VAL; |
| 3113 |
tok->threshold = bcf_hdr_nsamples(filter->hdr); |
| 3114 |
tok->is_constant = 1; |
| 3115 |
tok->ht_type = BCF_HT_INT; |
| 3116 |
return 0; |
| 3117 |
} |
| 3118 |
else if ( !strncasecmp(str,"N_MISSING",len) ) |
| 3119 |
{ |
| 3120 |
filter->max_unpack |= BCF_UN_FMT; |
| 3121 |
tok->setter = &filters_set_nmissing; |
| 3122 |
tok->tag = strdup("N_MISSING"); |
| 3123 |
tok->ht_type = BCF_HT_INT; |
| 3124 |
return 0; |
| 3125 |
} |
| 3126 |
else if ( !strncasecmp(str,"F_MISSING",len) ) |
| 3127 |
{ |
| 3128 |
filter->max_unpack |= BCF_UN_FMT; |
| 3129 |
tok->setter = &filters_set_nmissing; |
| 3130 |
tok->tag = strdup("F_MISSING"); |
| 3131 |
tok->ht_type = BCF_HT_REAL; |
| 3132 |
return 0; |
| 3133 |
} |
| 3134 |
} |
| 3135 |
|
| 3136 |
|
| 3137 |
int is_array = 0; |
| 3138 |
kstring_t tmp = {0,0,0}; |
| 3139 |
kputsn(str, len, &tmp); |
| 3140 |
if ( tmp.s[tmp.l-1] == ']' ) |
| 3141 |
{ |
| 3142 |
int i; |
| 3143 |
for (i=0; i<tmp.l; i++) |
| 3144 |
if ( tmp.s[i]=='[' ) { tmp.s[i] = 0; is_array = i+1; break; } |
| 3145 |
} |
| 3146 |
tok->hdr_id = bcf_hdr_id2int(filter->hdr,BCF_DT_ID,tmp.s); |
| 3147 |
if ( is_fmt==-1 ) |
| 3148 |
{ |
| 3149 |
if ( tok->hdr_id >=0 ) |
| 3150 |
{ |
| 3151 |
int is_info = bcf_hdr_idinfo_exists(filter->hdr,BCF_HL_INFO,tok->hdr_id) ? 1 : 0; |
| 3152 |
is_fmt = bcf_hdr_idinfo_exists(filter->hdr,BCF_HL_FMT,tok->hdr_id) ? 1 : 0; |
| 3153 |
if ( is_info && is_fmt ) |
| 3154 |
error("Error: ambiguous filtering expression, both INFO/%s and FORMAT/%s are defined in the VCF header.\n" , tmp.s,tmp.s); |
| 3155 |
} |
| 3156 |
if ( is_fmt==-1 ) is_fmt = 0; |
| 3157 |
} |
| 3158 |
if ( is_array ) |
| 3159 |
{ |
| 3160 |
parse_tag_idx(filter->hdr, is_fmt, tmp.s, tmp.s+is_array, tok); |
| 3161 |
if ( tok->idx==-3 && bcf_hdr_id2length(filter->hdr,BCF_HL_FMT,tok->hdr_id)!=BCF_VL_R ) |
| 3162 |
error("Error: GT subscripts can be used only with Number=R tags\n"); |
| 3163 |
} |
| 3164 |
else if ( is_fmt && !tok->nsamples ) |
| 3165 |
{ |
| 3166 |
int i; |
| 3167 |
tok->nsamples = bcf_hdr_nsamples(filter->hdr); |
| 3168 |
tok->usmpl = (uint8_t*) malloc(tok->nsamples); |
| 3169 |
for (i=0; i<tok->nsamples; i++) tok->usmpl[i] = 1; |
| 3170 |
} |
| 3171 |
|
| 3172 |
tok->hl_type = is_fmt ? BCF_HL_FMT : BCF_HL_INFO; |
| 3173 |
if ( is_fmt ) filter->max_unpack |= BCF_UN_FMT; |
| 3174 |
if ( tok->hdr_id>=0 ) |
| 3175 |
{ |
| 3176 |
if ( is_fmt && !strcmp("GT",tmp.s) ) |
| 3177 |
{ |
| 3178 |
tok->setter = &filters_set_genotype_string; tok->is_str = 1; |
| 3179 |
tok->ht_type = BCF_HT_STR; |
| 3180 |
} |
| 3181 |
else if ( is_fmt ) |
| 3182 |
{ |
| 3183 |
if ( !bcf_hdr_idinfo_exists(filter->hdr,BCF_HL_FMT,tok->hdr_id) ) |
| 3184 |
error("No such FORMAT field: %s\n", tmp.s); |
| 3185 |
if ( bcf_hdr_id2number(filter->hdr,BCF_HL_FMT,tok->hdr_id)!=1 && !is_array ) |
| 3186 |
{ |
| 3187 |
tok->idxs = (int*) malloc(sizeof(int)); |
| 3188 |
tok->idxs[0] = -1; |
| 3189 |
tok->nidxs = 1; |
| 3190 |
tok->idx = -2; |
| 3191 |
} |
| 3192 |
switch ( bcf_hdr_id2type(filter->hdr,BCF_HL_FMT,tok->hdr_id) ) |
| 3193 |
{ |
| 3194 |
case BCF_HT_INT: tok->setter = &filters_set_format_int; tok->ht_type = BCF_HT_INT; break; |
| 3195 |
case BCF_HT_REAL: tok->setter = &filters_set_format_float; tok->ht_type = BCF_HT_REAL; break; |
| 3196 |
case BCF_HT_STR: tok->setter = &filters_set_format_string; tok->ht_type = BCF_HT_STR; tok->is_str = 1; break; |
| 3197 |
default: error("[%s:%d %s] FIXME\n", __FILE__,__LINE__,__FUNCTION__); |
| 3198 |
} |
| 3199 |
} |
| 3200 |
else if ( !bcf_hdr_idinfo_exists(filter->hdr,BCF_HL_INFO,tok->hdr_id) ) |
| 3201 |
error("No such INFO field: %s\n", tmp.s); |
| 3202 |
else |
| 3203 |
{ |
| 3204 |
if ( bcf_hdr_id2type(filter->hdr,BCF_HL_INFO,tok->hdr_id) == BCF_HT_FLAG ) |
| 3205 |
{ |
| 3206 |
tok->setter = filters_set_info_flag; |
| 3207 |
tok->ht_type = BCF_HT_INT; |
| 3208 |
} |
| 3209 |
else |
| 3210 |
{ |
| 3211 |
tok->ht_type = bcf_hdr_id2type(filter->hdr,BCF_HL_INFO,tok->hdr_id); |
| 3212 |
if ( tok->ht_type == BCF_HT_STR ) tok->is_str = 1; |
| 3213 |
if ( bcf_hdr_id2number(filter->hdr,BCF_HL_INFO,tok->hdr_id)==1 ) |
| 3214 |
tok->setter = filters_set_info; |
| 3215 |
else |
| 3216 |
{ |
| 3217 |
switch ( bcf_hdr_id2type(filter->hdr,BCF_HL_INFO,tok->hdr_id) ) |
| 3218 |
{ |
| 3219 |
case BCF_HT_INT: tok->setter = &filters_set_info_int; break; |
| 3220 |
case BCF_HT_REAL: tok->setter = &filters_set_info_float; break; |
| 3221 |
case BCF_HT_STR: tok->setter = &filters_set_info_string; tok->is_str = 1; break; |
| 3222 |
default: error("[%s:%d %s] FIXME\n", __FILE__,__LINE__,__FUNCTION__); |
| 3223 |
} |
| 3224 |
if (!is_array) |
| 3225 |
{ |
| 3226 |
tok->idx = -2; |
| 3227 |
tok->idxs = (int*) malloc(sizeof(int)); |
| 3228 |
tok->idxs[0] = -1; |
| 3229 |
tok->nidxs = 1; |
| 3230 |
} |
| 3231 |
} |
| 3232 |
} |
| 3233 |
filter->max_unpack |= BCF_UN_INFO; |
| 3234 |
} |
| 3235 |
tok->tag = strdup(tmp.s); |
| 3236 |
if ( tmp.s ) free(tmp.s); |
| 3237 |
filter_add_used_tag(filter,is_fmt ? "FORMAT/" : "INFO/",tok->tag); |
| 3238 |
return 0; |
| 3239 |
} |
| 3240 |
else if ( !strcasecmp(tmp.s,"ALT") ) |
| 3241 |
{ |
| 3242 |
tok->setter = &filters_set_alt_string; |
| 3243 |
tok->is_str = 1; |
| 3244 |
tok->ht_type = BCF_HT_STR; |
| 3245 |
tok->tag = strdup(tmp.s); |
| 3246 |
free(tmp.s); |
| 3247 |
filter_add_used_tag(filter,NULL,tok->tag); |
| 3248 |
return 0; |
| 3249 |
} |
| 3250 |
else if ( !strcasecmp(tmp.s,"AN") ) |
| 3251 |
{ |
| 3252 |
filter->max_unpack |= BCF_UN_FMT; |
| 3253 |
tok->setter = &filters_set_an; |
| 3254 |
tok->tag = strdup("AN"); |
| 3255 |
tok->ht_type = BCF_HT_INT; |
| 3256 |
free(tmp.s); |
| 3257 |
return 0; |
| 3258 |
} |
| 3259 |
else if ( !strcasecmp(tmp.s,"AC") ) |
| 3260 |
{ |
| 3261 |
filter->max_unpack |= BCF_UN_FMT; |
| 3262 |
tok->setter = &filters_set_ac; |
| 3263 |
tok->tag = strdup("AC"); |
| 3264 |
tok->ht_type = BCF_HT_INT; |
| 3265 |
free(tmp.s); |
| 3266 |
return 0; |
| 3267 |
} |
| 3268 |
else if ( !strcasecmp(tmp.s,"MAC") ) |
| 3269 |
{ |
| 3270 |
filter->max_unpack |= max_ac_an_unpack(filter->hdr); |
| 3271 |
tok->setter = &filters_set_mac; |
| 3272 |
tok->tag = strdup("MAC"); |
| 3273 |
tok->ht_type = BCF_HT_INT; |
| 3274 |
free(tmp.s); |
| 3275 |
return 0; |
| 3276 |
} |
| 3277 |
else if ( !strcasecmp(tmp.s,"AF") ) |
| 3278 |
{ |
| 3279 |
filter->max_unpack |= max_ac_an_unpack(filter->hdr); |
| 3280 |
tok->setter = &filters_set_af; |
| 3281 |
tok->tag = strdup("AF"); |
| 3282 |
tok->ht_type = BCF_HT_REAL; |
| 3283 |
free(tmp.s); |
| 3284 |
return 0; |
| 3285 |
} |
| 3286 |
else if ( !strcasecmp(tmp.s,"MAF") ) |
| 3287 |
{ |
| 3288 |
filter->max_unpack |= max_ac_an_unpack(filter->hdr); |
| 3289 |
tok->setter = &filters_set_maf; |
| 3290 |
tok->tag = strdup("MAF"); |
| 3291 |
tok->ht_type = BCF_HT_REAL; |
| 3292 |
free(tmp.s); |
| 3293 |
return 0; |
| 3294 |
} |
| 3295 |
else if ( !strcasecmp(tmp.s,"ILEN") || !strcasecmp(tmp.s,"%ILEN") ) |
| 3296 |
{ |
| 3297 |
filter->max_unpack |= BCF_UN_STR; |
| 3298 |
tok->setter = &filters_set_ilen; |
| 3299 |
tok->tag = strdup("ILEN"); |
| 3300 |
tok->ht_type = BCF_HT_INT; |
| 3301 |
free(tmp.s); |
| 3302 |
return 0; |
| 3303 |
} |
| 3304 |
|
| 3305 |
|
| 3306 |
|
| 3307 |
|
| 3308 |
|
| 3309 |
|
| 3310 |
char *end; |
| 3311 |
tok->threshold = strtol(tmp.s, &end, 10); |
| 3312 |
if ( end - tmp.s != strlen(tmp.s) ) |
| 3313 |
{ |
| 3314 |
errno = 0; |
| 3315 |
tok->threshold = strtod(tmp.s, &end); |
| 3316 |
if ( errno!=0 || end!=tmp.s+len ) |
| 3317 |
{ |
| 3318 |
if ( filter->exit_on_error ) |
| 3319 |
error("[%s:%d %s] Error: the tag \"%s\" is not defined in the VCF header\n", __FILE__,__LINE__,__FUNCTION__,tmp.s); |
| 3320 |
filter->status |= FILTER_ERR_UNKN_TAGS; |
| 3321 |
filter_add_undef_tag(filter,tmp.s); |
| 3322 |
} |
| 3323 |
tok->ht_type = BCF_HT_REAL; |
| 3324 |
} |
| 3325 |
else |
| 3326 |
tok->ht_type = BCF_HT_INT; |
| 3327 |
tok->is_constant = 1; |
| 3328 |
|
| 3329 |
if ( tmp.s ) free(tmp.s); |
| 3330 |
return 0; |
| 3331 |
} |
| 3332 |
|
| 3333 |
static void filter_debug_print(token_t *toks, token_t **tok_ptrs, int ntoks) |
| 3334 |
{ |
| 3335 |
int i; |
| 3336 |
for (i=0; i<ntoks; i++) |
| 3337 |
{ |
| 3338 |
token_t *tok = toks ? &toks[i] : tok_ptrs[i]; |
| 3339 |
if ( tok->tok_type==TOK_VAL ) |
| 3340 |
{ |
| 3341 |
if ( tok->key ) |
| 3342 |
fprintf(bcftools_stderr,"%s", tok->key); |
| 3343 |
else if ( tok->tag ) |
| 3344 |
fprintf(bcftools_stderr,"%s", tok->tag); |
| 3345 |
else |
| 3346 |
fprintf(bcftools_stderr,"%e", tok->threshold); |
| 3347 |
} |
| 3348 |
else |
| 3349 |
fprintf(bcftools_stderr,"%c", TOKEN_STRING[tok->tok_type]); |
| 3350 |
if ( tok->setter ) fprintf(bcftools_stderr,"\t[setter %p]", tok->setter); |
| 3351 |
fprintf(bcftools_stderr,"\n"); |
| 3352 |
} |
| 3353 |
} |
| 3354 |
|
| 3355 |
static void str_to_lower(char *str) |
| 3356 |
{ |
| 3357 |
while ( *str ) { *str = tolower(*str); str++; } |
| 3358 |
} |
| 3359 |
static int perl_exec(filter_t *flt, bcf1_t *line, token_t *rtok, token_t **stack, int nstack) |
| 3360 |
{ |
| 3361 |
#if ENABLE_PERL_FILTERS |
| 3362 |
|
| 3363 |
PerlInterpreter *perl = flt->perl; |
| 3364 |
if ( !perl ) error("Error: perl expression without a perl script name\n"); |
| 3365 |
|
| 3366 |
dSP; |
| 3367 |
ENTER; |
| 3368 |
SAVETMPS; |
| 3369 |
|
| 3370 |
PUSHMARK(SP); |
| 3371 |
int i,j, istack = nstack - rtok->nargs; |
| 3372 |
for (i=istack+1; i<nstack; i++) |
| 3373 |
{ |
| 3374 |
token_t *tok = stack[i]; |
| 3375 |
if ( tok->is_str ) |
| 3376 |
XPUSHs(sv_2mortal(newSVpvn(tok->str_value.s,tok->str_value.l))); |
| 3377 |
else if ( tok->nvalues==1 ) |
| 3378 |
XPUSHs(sv_2mortal(newSVnv(tok->values[0]))); |
| 3379 |
else if ( tok->nvalues>1 ) |
| 3380 |
{ |
| 3381 |
AV *av = newAV(); |
| 3382 |
for (j=0; j<tok->nvalues; j++) av_push(av, newSVnv(tok->values[j])); |
| 3383 |
SV *rv = newRV_inc((SV*)av); |
| 3384 |
XPUSHs(rv); |
| 3385 |
} |
| 3386 |
else |
| 3387 |
{ |
| 3388 |
bcf_double_set_missing(tok->values[0]); |
| 3389 |
XPUSHs(sv_2mortal(newSVnv(tok->values[0]))); |
| 3390 |
} |
| 3391 |
} |
| 3392 |
PUTBACK; |
| 3393 |
|
| 3394 |
|
| 3395 |
|
| 3396 |
|
| 3397 |
|
| 3398 |
int nret = call_pv(stack[istack]->str_value.s, G_ARRAY); |
| 3399 |
|
| 3400 |
SPAGAIN; |
| 3401 |
|
| 3402 |
rtok->nvalues = nret; |
| 3403 |
hts_expand(double, rtok->nvalues, rtok->mvalues, rtok->values); |
| 3404 |
for (i=nret; i>0; i--) |
| 3405 |
{ |
| 3406 |
rtok->values[i-1] = (double) POPn; |
| 3407 |
if ( isnan(rtok->values[i-1]) ) bcf_double_set_missing(rtok->values[i-1]); |
| 3408 |
} |
| 3409 |
|
| 3410 |
PUTBACK; |
| 3411 |
FREETMPS; |
| 3412 |
LEAVE; |
| 3413 |
|
| 3414 |
#else |
| 3415 |
error("\nPerl filtering requires running `configure --enable-perl-filters` at compile time.\n\n"); |
| 3416 |
#endif |
| 3417 |
return rtok->nargs; |
| 3418 |
} |
| 3419 |
static void perl_init(filter_t *filter, char **str) |
| 3420 |
{ |
| 3421 |
char *beg = *str; |
| 3422 |
while ( *beg && isspace(*beg) ) beg++; |
| 3423 |
if ( !*beg ) return; |
| 3424 |
if ( strncasecmp("perl:", beg, 5) ) return; |
| 3425 |
#if ENABLE_PERL_FILTERS |
| 3426 |
beg += 5; |
| 3427 |
char *end = beg; |
| 3428 |
while ( *end && *end!=';' ) end++; |
| 3429 |
*str = end+1; |
| 3430 |
|
| 3431 |
if ( ++filter_ninit == 1 ) |
| 3432 |
{ |
| 3433 |
|
| 3434 |
int argc = 0; |
| 3435 |
char **argv = NULL; |
| 3436 |
char **env = NULL; |
| 3437 |
PERL_SYS_INIT3(&argc, &argv, &env); |
| 3438 |
} |
| 3439 |
|
| 3440 |
filter->perl = perl_alloc(); |
| 3441 |
PerlInterpreter *perl = filter->perl; |
| 3442 |
|
| 3443 |
if ( !perl ) error("perl_alloc failed\n"); |
| 3444 |
perl_construct(perl); |
| 3445 |
|
| 3446 |
|
| 3447 |
char *rmme = (char*) calloc(end - beg + 1,1); |
| 3448 |
memcpy(rmme, beg, end - beg); |
| 3449 |
char *argv[] = { "", "" }; |
| 3450 |
argv[1] = expand_path(rmme); |
| 3451 |
free(rmme); |
| 3452 |
|
| 3453 |
PL_origalen = 1; |
| 3454 |
int ret = perl_parse(filter->perl, NULL, 2, argv, NULL); |
| 3455 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
| 3456 |
if ( ret ) error("Failed to parse: %s\n", argv[1]); |
| 3457 |
free(argv[1]); |
| 3458 |
|
| 3459 |
perl_run(perl); |
| 3460 |
#else |
| 3461 |
error("\nPerl filtering requires running `configure --enable-perl-filters` at compile time.\n\n"); |
| 3462 |
#endif |
| 3463 |
} |
| 3464 |
static void perl_destroy(filter_t *filter) |
| 3465 |
{ |
| 3466 |
#if ENABLE_PERL_FILTERS |
| 3467 |
if ( !filter->perl ) return; |
| 3468 |
|
| 3469 |
PerlInterpreter *perl = filter->perl; |
| 3470 |
perl_destruct(perl); |
| 3471 |
perl_free(perl); |
| 3472 |
if ( --filter_ninit <= 0 ) |
| 3473 |
{ |
| 3474 |
|
| 3475 |
PERL_SYS_TERM(); |
| 3476 |
} |
| 3477 |
#endif |
| 3478 |
} |
| 3479 |
|
| 3480 |
|
| 3481 |
|
| 3482 |
static void determine_ext_types(filter_t *filter, int ntok, token_t *tok) |
| 3483 |
{ |
| 3484 |
int i; |
| 3485 |
for (i=0; i<ntok; i++) |
| 3486 |
{ |
| 3487 |
if ( !tok[i].iext || tok[i].ht_type!=-1 ) continue; |
| 3488 |
if ( !i || i+1==ntok ) break; |
| 3489 |
if ( tok[i-1].ht_type==-1 ) break; |
| 3490 |
|
| 3491 |
|
| 3492 |
tok[i].ht_type = tok[i-1].ht_type; |
| 3493 |
} |
| 3494 |
if ( i!=ntok ) |
| 3495 |
error("[%s:%d %s] Error: unable to determine the type, use explicit notation: %s\n",__FILE__,__LINE__,__FUNCTION__,filter->str); |
| 3496 |
for (i=0; i<ntok; i++) |
| 3497 |
{ |
| 3498 |
int j = tok[i].iext - 1; |
| 3499 |
if ( j<0 ) continue; |
| 3500 |
if ( filter->ext[j]!=-1 && filter->ext[j]!=tok[i].ht_type ) |
| 3501 |
error("[%s:%d %s] FIXME: this should not happen %d vs %d, iext=%d\n",__FILE__,__LINE__,__FUNCTION__,filter->ext[j],tok[i].ht_type,j); |
| 3502 |
filter->ext[j] = tok[i].ht_type; |
| 3503 |
if ( tok[i].ht_type==BCF_HT_STR ) tok[i].is_str = 1; |
| 3504 |
} |
| 3505 |
} |
| 3506 |
|
| 3507 |
|
| 3508 |
|
| 3509 |
static filter_t *filter_init_(bcf_hdr_t *hdr, const char *str, int exit_on_error) |
| 3510 |
{ |
| 3511 |
filter_t *filter = (filter_t *) calloc(1,sizeof(filter_t)); |
| 3512 |
filter->str = strdup(str); |
| 3513 |
filter->hdr = hdr; |
| 3514 |
filter->max_unpack |= BCF_UN_STR; |
| 3515 |
filter->exit_on_error = exit_on_error; |
| 3516 |
|
| 3517 |
int nops = 0, mops = 0; |
| 3518 |
int nout = 0, mout = 0; |
| 3519 |
token_t *out = NULL; |
| 3520 |
token_t *ops = NULL; |
| 3521 |
char *tmp = filter->str; |
| 3522 |
perl_init(filter, &tmp); |
| 3523 |
|
| 3524 |
int last_op = -1; |
| 3525 |
while ( *tmp ) |
| 3526 |
{ |
| 3527 |
int len, ret; |
| 3528 |
ret = filters_next_token(&tmp, &len); |
| 3529 |
if ( ret==-1 ) error("Missing quotes in: %s\n", str); |
| 3530 |
|
| 3531 |
|
| 3532 |
|
| 3533 |
|
| 3534 |
if ( ret==TOK_LFT ) |
| 3535 |
{ |
| 3536 |
nops++; |
| 3537 |
hts_expand0(token_t, nops, mops, ops); |
| 3538 |
ops[nops-1].tok_type = ret; |
| 3539 |
} |
| 3540 |
else if ( ret==TOK_RGT ) |
| 3541 |
{ |
| 3542 |
while ( nops>0 && ops[nops-1].tok_type!=TOK_LFT ) |
| 3543 |
{ |
| 3544 |
nout++; |
| 3545 |
hts_expand0(token_t, nout, mout, out); |
| 3546 |
out[nout-1] = ops[nops-1]; |
| 3547 |
memset(&ops[nops-1],0,sizeof(token_t)); |
| 3548 |
nops--; |
| 3549 |
} |
| 3550 |
if ( nops<=0 ) error("Could not parse: %s\n", str); |
| 3551 |
memset(&ops[nops-1],0,sizeof(token_t)); |
| 3552 |
nops--; |
| 3553 |
} |
| 3554 |
else if ( ret==TOK_EXT ) |
| 3555 |
{ |
| 3556 |
nout++; |
| 3557 |
hts_expand0(token_t, nout, mout, out); |
| 3558 |
filters_init1_ext(filter, tmp, len, &out[nout-1]); |
| 3559 |
tmp += len; |
| 3560 |
} |
| 3561 |
else if ( ret!=TOK_VAL ) |
| 3562 |
{ |
| 3563 |
|
| 3564 |
if ( ret==TOK_SUB && last_op!=TOK_VAL && last_op!=TOK_RGT ) |
| 3565 |
{ |
| 3566 |
nout++; |
| 3567 |
hts_expand0(token_t, nout, mout, out); |
| 3568 |
token_t *tok = &out[nout-1]; |
| 3569 |
tok->tok_type = TOK_VAL; |
| 3570 |
tok->hdr_id = -1; |
| 3571 |
tok->pass_site = -1; |
| 3572 |
tok->threshold = -1.0; |
| 3573 |
tok->is_constant = 1; |
| 3574 |
ret = TOK_MULT; |
| 3575 |
} |
| 3576 |
else if ( ret == -TOK_FUNC ) |
| 3577 |
{ |
| 3578 |
|
| 3579 |
|
| 3580 |
nops++; |
| 3581 |
hts_expand0(token_t, nops, mops, ops); |
| 3582 |
token_t *tok = &ops[nops-1]; |
| 3583 |
tok->tok_type = -ret; |
| 3584 |
tok->hdr_id = -1; |
| 3585 |
tok->pass_site = -1; |
| 3586 |
tok->threshold = -1.0; |
| 3587 |
if ( !strncasecmp(tmp-len,"N_PASS",6) ) |
| 3588 |
{ |
| 3589 |
filter->max_unpack |= BCF_UN_FMT; |
| 3590 |
tok->func = func_npass; |
| 3591 |
tok->tag = strdup("N_PASS"); |
| 3592 |
} |
| 3593 |
else if ( !strncasecmp(tmp-len,"F_PASS",6) ) |
| 3594 |
{ |
| 3595 |
filter->max_unpack |= BCF_UN_FMT; |
| 3596 |
tok->func = func_npass; |
| 3597 |
tok->tag = strdup("F_PASS"); |
| 3598 |
} |
| 3599 |
else error("The function \"%s\" is not supported\n", tmp-len); |
| 3600 |
continue; |
| 3601 |
} |
| 3602 |
else if ( ret < 0 ) |
| 3603 |
{ |
| 3604 |
ret = -ret; |
| 3605 |
|
| 3606 |
tmp += len; |
| 3607 |
char *beg = tmp; |
| 3608 |
kstring_t rmme = {0,0,0}; |
| 3609 |
int i, margs, nargs = 0; |
| 3610 |
|
| 3611 |
if ( ret == TOK_PERLSUB ) |
| 3612 |
{ |
| 3613 |
while ( *beg && ((isalnum(*beg) && !ispunct(*beg)) || *beg=='_') ) beg++; |
| 3614 |
if ( *beg!='(' ) error("Could not parse the expression: %s\n", str); |
| 3615 |
|
| 3616 |
|
| 3617 |
kputc('"', &rmme); |
| 3618 |
kputsn(tmp, beg-tmp, &rmme); |
| 3619 |
kputc('"', &rmme); |
| 3620 |
nout++; |
| 3621 |
hts_expand0(token_t, nout, mout, out); |
| 3622 |
filters_init1(filter, rmme.s, rmme.l, &out[nout-1]); |
| 3623 |
nargs++; |
| 3624 |
} |
| 3625 |
char *end = beg; |
| 3626 |
while ( *end && *end!=')' ) end++; |
| 3627 |
if ( !*end ) error("Could not parse the expression: %s\n", str); |
| 3628 |
|
| 3629 |
|
| 3630 |
rmme.l = 0; |
| 3631 |
kputsn(beg+1, end-beg-1, &rmme); |
| 3632 |
char **rmme_list = hts_readlist(rmme.s, 0, &margs); |
| 3633 |
for (i=0; i<margs; i++) |
| 3634 |
{ |
| 3635 |
nargs++; |
| 3636 |
nout++; |
| 3637 |
hts_expand0(token_t, nout, mout, out); |
| 3638 |
filters_init1(filter, rmme_list[i], strlen(rmme_list[i]), &out[nout-1]); |
| 3639 |
free(rmme_list[i]); |
| 3640 |
} |
| 3641 |
free(rmme_list); |
| 3642 |
free(rmme.s); |
| 3643 |
|
| 3644 |
nout++; |
| 3645 |
hts_expand0(token_t, nout, mout, out); |
| 3646 |
token_t *tok = &out[nout-1]; |
| 3647 |
tok->tok_type = ret; |
| 3648 |
tok->nargs = nargs; |
| 3649 |
tok->hdr_id = -1; |
| 3650 |
tok->pass_site = -1; |
| 3651 |
tok->threshold = -1.0; |
| 3652 |
|
| 3653 |
tmp = end + 1; |
| 3654 |
continue; |
| 3655 |
} |
| 3656 |
else |
| 3657 |
{ |
| 3658 |
while ( nops>0 && op_prec[ret] < op_prec[ops[nops-1].tok_type] ) |
| 3659 |
{ |
| 3660 |
nout++; |
| 3661 |
hts_expand0(token_t, nout, mout, out); |
| 3662 |
out[nout-1] = ops[nops-1]; |
| 3663 |
memset(&ops[nops-1],0,sizeof(token_t)); |
| 3664 |
nops--; |
| 3665 |
} |
| 3666 |
} |
| 3667 |
nops++; |
| 3668 |
hts_expand0(token_t, nops, mops, ops); |
| 3669 |
ops[nops-1].tok_type = ret; |
| 3670 |
} |
| 3671 |
else if ( !len ) |
| 3672 |
{ |
| 3673 |
if ( *tmp && !isspace(*tmp) ) error("Could not parse the expression: [%s]\n", str); |
| 3674 |
break; |
| 3675 |
} |
| 3676 |
else |
| 3677 |
{ |
| 3678 |
nout++; |
| 3679 |
hts_expand0(token_t, nout, mout, out); |
| 3680 |
if ( tmp[len-1]==',' ) |
| 3681 |
filters_init1(filter, tmp, len-1, &out[nout-1]); |
| 3682 |
else |
| 3683 |
filters_init1(filter, tmp, len, &out[nout-1]); |
| 3684 |
tmp += len; |
| 3685 |
} |
| 3686 |
last_op = ret; |
| 3687 |
} |
| 3688 |
while ( nops>0 ) |
| 3689 |
{ |
| 3690 |
if ( ops[nops-1].tok_type==TOK_LFT || ops[nops-1].tok_type==TOK_RGT ) error("Could not parse the expression: [%s]\n", filter->str); |
| 3691 |
nout++; |
| 3692 |
hts_expand0(token_t, nout, mout, out); |
| 3693 |
out[nout-1] = ops[nops-1]; |
| 3694 |
memset(&ops[nops-1],0,sizeof(token_t)); |
| 3695 |
nops--; |
| 3696 |
} |
| 3697 |
|
| 3698 |
if ( filter->status != FILTER_OK ) |
| 3699 |
{ |
| 3700 |
if ( mops ) free(ops); |
| 3701 |
filter->filters = out; |
| 3702 |
filter->nfilters = nout; |
| 3703 |
return filter; |
| 3704 |
} |
| 3705 |
|
| 3706 |
|
| 3707 |
determine_ext_types(filter,nout,out); |
| 3708 |
|
| 3709 |
|
| 3710 |
|
| 3711 |
|
| 3712 |
|
| 3713 |
|
| 3714 |
|
| 3715 |
|
| 3716 |
static int comma_separator_warned = 0; |
| 3717 |
int i; |
| 3718 |
for (i=0; i<nout; i++) |
| 3719 |
{ |
| 3720 |
if ( i+1<nout && (out[i].tok_type==TOK_LT || out[i].tok_type==TOK_BT) && out[i+1].tok_type==TOK_EQ ) |
| 3721 |
error("Error parsing the expression: \"%s\"\n", filter->str); |
| 3722 |
|
| 3723 |
if ( out[i].hash ) |
| 3724 |
{ |
| 3725 |
int j = out[i+1].tok_type==TOK_VAL ? i+1 : i-1; |
| 3726 |
if ( out[j].comparator!=filters_cmp_id ) |
| 3727 |
{ |
| 3728 |
if ( out[j].comparator ) error("Error: could not parse the expression with \"@file_name\" syntax (possible todo)\n"); |
| 3729 |
out[j].comparator = filters_cmp_string_hash; |
| 3730 |
} |
| 3731 |
} |
| 3732 |
if ( out[i].tok_type==TOK_OR || out[i].tok_type==TOK_OR_VEC ) |
| 3733 |
out[i].func = vector_logic_or; |
| 3734 |
if ( out[i].tok_type==TOK_AND || out[i].tok_type==TOK_AND_VEC ) |
| 3735 |
out[i].func = vector_logic_and; |
| 3736 |
if ( out[i].tok_type==TOK_EQ || out[i].tok_type==TOK_NE ) |
| 3737 |
{ |
| 3738 |
|
| 3739 |
int j = i-1, k = i-2; |
| 3740 |
if ( !out[j].is_str ) { k = i-1, j = i-2; } |
| 3741 |
if ( out[j].is_str && out[j].key && !strcmp(".",out[j].key) ) |
| 3742 |
{ |
| 3743 |
int set_missing = 0; |
| 3744 |
if ( out[k].hdr_id>0 ) |
| 3745 |
{ |
| 3746 |
int type = bcf_hdr_id2type(filter->hdr,out[k].hl_type,out[k].hdr_id); |
| 3747 |
if ( type==BCF_HT_INT ) set_missing = 1; |
| 3748 |
else if ( type==BCF_HT_REAL ) set_missing = 1; |
| 3749 |
} |
| 3750 |
else if ( !strcmp("QUAL",out[k].tag) ) set_missing = 1; |
| 3751 |
if ( set_missing ) { out[j].is_str = 0; out[j].is_missing = 1; bcf_double_set_missing(out[j].values[0]); } |
| 3752 |
} |
| 3753 |
} |
| 3754 |
if ( out[i].tok_type==TOK_LIKE || out[i].tok_type==TOK_NLIKE ) |
| 3755 |
{ |
| 3756 |
int j = i-1; |
| 3757 |
if ( !out[j].key ) |
| 3758 |
error("Could not parse the expression, wrong value for regex operator: %s\n", filter->str); |
| 3759 |
out[j].regex = (regex_t *) malloc(sizeof(regex_t)); |
| 3760 |
int cflags = REG_NOSUB; |
| 3761 |
int len = strlen(out[j].key); |
| 3762 |
if ( len>2 && out[j].key[len-1]=='i' && out[j].key[len-2]=='/' && out[j].key[len-3]!='\\' ) |
| 3763 |
{ |
| 3764 |
out[j].key[len-2] = 0; |
| 3765 |
cflags |= REG_ICASE; |
| 3766 |
} |
| 3767 |
if ( regcomp(out[j].regex, out[j].key, cflags) ) |
| 3768 |
error("Could not compile the regex expression \"%s\": %s\n", out[j].key,filter->str); |
| 3769 |
} |
| 3770 |
if ( out[i].is_str && out[i].tok_type==TOK_VAL && out[i].key && strchr(out[i].key,',') ) |
| 3771 |
{ |
| 3772 |
int print_note = 0; |
| 3773 |
if ( out[i+1].tok_type==TOK_EQ || (out[i+1].is_str && out[i+2].tok_type==TOK_EQ) ) print_note = 1; |
| 3774 |
else if ( out[i+1].tok_type==TOK_NE || (out[i+1].is_str && out[i+2].tok_type==TOK_NE) ) print_note = 1; |
| 3775 |
if ( print_note && !comma_separator_warned ) |
| 3776 |
{ |
| 3777 |
comma_separator_warned = 1; |
| 3778 |
fprintf(bcftools_stderr, |
| 3779 |
"Warning: comma is interpreted as a separator and OR logic is used in string comparisons.\n" |
| 3780 |
" (Search the manual for \"Comma in strings\" to learn more.)\n"); |
| 3781 |
} |
| 3782 |
} |
| 3783 |
if ( out[i].tok_type!=TOK_VAL ) continue; |
| 3784 |
if ( !out[i].tag ) continue; |
| 3785 |
if ( out[i].setter==filters_set_type ) |
| 3786 |
{ |
| 3787 |
if ( i+1==nout || !out[i+1].key ) |
| 3788 |
error("Could not parse the expression: %s\n", filter->str); |
| 3789 |
int itok, ival; |
| 3790 |
if ( out[i+1].tok_type==TOK_EQ || out[i+1].tok_type==TOK_NE ) ival = i - 1, itok = i + 1; |
| 3791 |
else if ( out[i+1].tok_type==TOK_LIKE || out[i+1].tok_type==TOK_NLIKE ) ival = i - 1, itok = i + 1; |
| 3792 |
else if ( out[i+2].tok_type==TOK_EQ || out[i+2].tok_type==TOK_NE ) itok = i + 2, ival = i + 1; |
| 3793 |
else if ( out[i+2].tok_type==TOK_LIKE || out[i+2].tok_type==TOK_NLIKE ) itok = i + 2, ival = i + 1; |
| 3794 |
else error("[%s:%d %s] Could not parse the expression: %s\n", __FILE__,__LINE__,__FUNCTION__, filter->str); |
| 3795 |
if ( !strcasecmp(out[ival].key,"snp") || !strcasecmp(out[ival].key,"snps") ) { out[ival].threshold = VCF_SNP<<1; out[ival].is_str = 0; } |
| 3796 |
else if ( !strcasecmp(out[ival].key,"indel") || !strcasecmp(out[ival].key,"indels") ) { out[ival].threshold = VCF_INDEL<<1; out[ival].is_str = 0; } |
| 3797 |
else if ( !strcasecmp(out[ival].key,"mnp") || !strcasecmp(out[ival].key,"mnps") ) { out[ival].threshold = VCF_MNP<<1; out[ival].is_str = 0; } |
| 3798 |
else if ( !strcasecmp(out[ival].key,"other") ) { out[ival].threshold = VCF_OTHER<<1; out[ival].is_str = 0; } |
| 3799 |
else if ( !strcasecmp(out[ival].key,"bnd") ) { out[ival].threshold = VCF_BND<<1; out[ival].is_str = 0; } |
| 3800 |
else if ( !strcasecmp(out[ival].key,"overlap") ) { out[ival].threshold = VCF_OVERLAP<<1; out[ival].is_str = 0; } |
| 3801 |
else if ( !strcasecmp(out[ival].key,"ref") ) { out[ival].threshold = 1; out[ival].is_str = 0; } |
| 3802 |
else error("The type \"%s\" not recognised: %s\n", out[ival].key, filter->str); |
| 3803 |
out[ival].is_constant = 1; |
| 3804 |
if ( out[itok].tok_type==TOK_LIKE || out[itok].tok_type==TOK_NLIKE ) out[itok].comparator = filters_cmp_bit_and; |
| 3805 |
out[ival].tag = out[ival].key; out[ival].key = NULL; |
| 3806 |
i = itok; |
| 3807 |
continue; |
| 3808 |
} |
| 3809 |
if ( !strcmp(out[i].tag,"GT") ) |
| 3810 |
{ |
| 3811 |
if ( i+1==nout ) error("Could not parse the expression: %s\n", filter->str); |
| 3812 |
int ival; |
| 3813 |
if ( out[i+1].tok_type==TOK_EQ || out[i+1].tok_type==TOK_NE ) ival = i - 1; |
| 3814 |
else if ( out[i+1].tok_type==TOK_LIKE || out[i+1].tok_type==TOK_NLIKE ) ival = i - 1; |
| 3815 |
else if ( out[i+2].tok_type==TOK_EQ || out[i+2].tok_type==TOK_NE ) ival = i + 1; |
| 3816 |
else if ( out[i+2].tok_type==TOK_LIKE || out[i+2].tok_type==TOK_NLIKE ) ival = i + 1; |
| 3817 |
else error("[%s:%d %s] Could not parse the expression: %s\n", __FILE__,__LINE__,__FUNCTION__, filter->str); |
| 3818 |
|
| 3819 |
if ( !out[ival].key ) error("Comparison between samples is not supported, sorry!\n"); |
| 3820 |
|
| 3821 |
|
| 3822 |
if ( !strcasecmp(out[ival].key,"hom") ) { out[i].setter = filters_set_genotype3; str_to_lower(out[ival].key); } |
| 3823 |
else if ( !strcasecmp(out[ival].key,"het") ) { out[i].setter = filters_set_genotype3; str_to_lower(out[ival].key); } |
| 3824 |
else if ( !strcasecmp(out[ival].key,"hap") ) { out[i].setter = filters_set_genotype3; str_to_lower(out[ival].key); } |
| 3825 |
else if ( !strcasecmp(out[ival].key,"mis") ) { out[i].setter = filters_set_genotype4; str_to_lower(out[ival].key); } |
| 3826 |
else if ( !strcasecmp(out[ival].key,"ref") ) { out[i].setter = filters_set_genotype4; str_to_lower(out[ival].key); } |
| 3827 |
else if ( !strcasecmp(out[ival].key,"alt") ) { out[i].setter = filters_set_genotype4; str_to_lower(out[ival].key); } |
| 3828 |
else if ( !strcasecmp(out[ival].key,"rr") ) { out[i].setter = filters_set_genotype2; str_to_lower(out[ival].key); } |
| 3829 |
else if ( !strcasecmp(out[ival].key,"ra") || !strcasecmp(out[ival].key,"ar") ) { out[i].setter = filters_set_genotype2; out[ival].key[0]='r'; out[ival].key[1]='a'; } |
| 3830 |
else if ( !strcmp(out[ival].key,"aA") || !strcmp(out[ival].key,"Aa") ) { out[i].setter = filters_set_genotype2; out[ival].key[0]='a'; out[ival].key[1]='A'; } |
| 3831 |
else if ( !strcasecmp(out[ival].key,"aa") ) { out[i].setter = filters_set_genotype2; out[ival].key[0]='a'; out[ival].key[1]='a'; } |
| 3832 |
else if ( !strcasecmp(out[ival].key,"a") ) { out[i].setter = filters_set_genotype2; out[ival].key[0]='a'; out[ival].key[1]=0; } |
| 3833 |
else if ( !strcasecmp(out[ival].key,"r") ) { out[i].setter = filters_set_genotype2; out[ival].key[0]='r'; out[ival].key[1]=0; } |
| 3834 |
continue; |
| 3835 |
} |
| 3836 |
if ( out[i].hl_type==BCF_HL_FLT ) |
| 3837 |
{ |
| 3838 |
if ( i+1==nout ) error("Could not parse the expression: %s\n", filter->str); |
| 3839 |
int itok = i, ival; |
| 3840 |
if ( out[i+1].tok_type==TOK_EQ || out[i+1].tok_type==TOK_NE ) ival = i - 1; |
| 3841 |
else if ( out[i+1].tok_type==TOK_LIKE ) out[i+1].tok_type = TOK_IN, ival = i - 1; |
| 3842 |
else if ( out[i+1].tok_type==TOK_NLIKE ) out[i+1].tok_type = TOK_NOT_IN, ival = i - 1; |
| 3843 |
else if ( out[i+2].tok_type==TOK_EQ || out[i+2].tok_type==TOK_NE ) ival = ++i; |
| 3844 |
else if ( out[i+2].tok_type==TOK_LIKE ) out[i+2].tok_type = TOK_IN, ival = ++i; |
| 3845 |
else if ( out[i+2].tok_type==TOK_NLIKE ) out[i+2].tok_type = TOK_NOT_IN, ival = ++i; |
| 3846 |
else error("[%s:%d %s] Could not parse the expression: %s\n", __FILE__,__LINE__,__FUNCTION__, filter->str); |
| 3847 |
if ( out[ival].tok_type!=TOK_VAL || !out[ival].key ) |
| 3848 |
error("[%s:%d %s] Could not parse the expression, an unquoted string value perhaps? %s\n", __FILE__,__LINE__,__FUNCTION__, filter->str); |
| 3849 |
token_t *tok = &out[ival]; |
| 3850 |
char *bp = tok->key; |
| 3851 |
tok->nvalues = 0; |
| 3852 |
int has_missing = 0; |
| 3853 |
while ( *bp ) |
| 3854 |
{ |
| 3855 |
char tmp, *ep = bp; |
| 3856 |
while ( *ep && *ep!=';' ) ep++; |
| 3857 |
tmp = *ep; |
| 3858 |
*ep = 0; |
| 3859 |
if ( !strcmp(".",bp) ) has_missing = 1; |
| 3860 |
else |
| 3861 |
{ |
| 3862 |
tok->nvalues++; |
| 3863 |
hts_expand(double,tok->nvalues,tok->mvalues,tok->values); |
| 3864 |
int id = bcf_hdr_id2int(filter->hdr, BCF_DT_ID, bp); |
| 3865 |
if ( !bcf_hdr_idinfo_exists(filter->hdr,BCF_HL_FLT,id) ) |
| 3866 |
error("The filter \"%s\" not present in the VCF header\n", bp); |
| 3867 |
tok->values[tok->nvalues-1] = id; |
| 3868 |
} |
| 3869 |
*ep = tmp; |
| 3870 |
if ( !tmp ) break; |
| 3871 |
bp = ep + 1; |
| 3872 |
} |
| 3873 |
if ( has_missing && tok->nvalues ) error("The FILTER expression cannot contain missing value AND filters: \"%s\" (%d)\n",tok->key,tok->nvalues); |
| 3874 |
out[ival].tag = tok->key; |
| 3875 |
tok->key = NULL; |
| 3876 |
out[itok].hdr_id = tok->hdr_id; |
| 3877 |
continue; |
| 3878 |
} |
| 3879 |
} |
| 3880 |
filter->nsamples = filter->max_unpack&BCF_UN_FMT ? bcf_hdr_nsamples(filter->hdr) : 0; |
| 3881 |
for (i=0; i<nout; i++) |
| 3882 |
{ |
| 3883 |
if ( out[i].tok_type==TOK_MAX ) { out[i].func = func_max; out[i].tok_type = TOK_FUNC; } |
| 3884 |
else if ( out[i].tok_type==TOK_MIN ) { out[i].func = func_min; out[i].tok_type = TOK_FUNC; } |
| 3885 |
else if ( out[i].tok_type==TOK_AVG ) { out[i].func = func_avg; out[i].tok_type = TOK_FUNC; } |
| 3886 |
else if ( out[i].tok_type==TOK_MEDIAN ) { out[i].func = func_median; out[i].tok_type = TOK_FUNC; } |
| 3887 |
else if ( out[i].tok_type==TOK_AVG ) { out[i].func = func_avg; out[i].tok_type = TOK_FUNC; } |
| 3888 |
else if ( out[i].tok_type==TOK_STDEV ) { out[i].func = func_stddev; out[i].tok_type = TOK_FUNC; } |
| 3889 |
else if ( out[i].tok_type==TOK_SUM ) { out[i].func = func_sum; out[i].tok_type = TOK_FUNC; } |
| 3890 |
else if ( out[i].tok_type==TOK_ABS ) { out[i].func = func_abs; out[i].tok_type = TOK_FUNC; } |
| 3891 |
else if ( out[i].tok_type==TOK_CNT ) { out[i].func = func_count; out[i].tok_type = TOK_FUNC; } |
| 3892 |
else if ( out[i].tok_type==TOK_LEN ) { out[i].func = func_strlen; out[i].tok_type = TOK_FUNC; } |
| 3893 |
else if ( out[i].tok_type==TOK_PHRED ) { out[i].func = func_phred; out[i].tok_type = TOK_FUNC; } |
| 3894 |
else if ( out[i].tok_type==TOK_BINOM ) { out[i].func = func_binom; out[i].tok_type = TOK_FUNC; } |
| 3895 |
else if ( out[i].tok_type==TOK_PERLSUB ) { out[i].func = perl_exec; out[i].tok_type = TOK_FUNC; } |
| 3896 |
else if ( out[i].tok_type==TOK_sMAX ) { out[i].func = func_smpl_max; out[i].tok_type = TOK_FUNC; } |
| 3897 |
else if ( out[i].tok_type==TOK_sMIN ) { out[i].func = func_smpl_min; out[i].tok_type = TOK_FUNC; } |
| 3898 |
else if ( out[i].tok_type==TOK_sAVG ) { out[i].func = func_smpl_avg; out[i].tok_type = TOK_FUNC; } |
| 3899 |
else if ( out[i].tok_type==TOK_sMEDIAN ) { out[i].func = func_smpl_median; out[i].tok_type = TOK_FUNC; } |
| 3900 |
else if ( out[i].tok_type==TOK_sSTDEV ) { out[i].func = func_smpl_stddev; out[i].tok_type = TOK_FUNC; } |
| 3901 |
else if ( out[i].tok_type==TOK_sSUM ) { out[i].func = func_smpl_sum; out[i].tok_type = TOK_FUNC; } |
| 3902 |
hts_expand0(double,1,out[i].mvalues,out[i].values); |
| 3903 |
if ( filter->nsamples ) |
| 3904 |
{ |
| 3905 |
out[i].pass_samples = (uint8_t*)malloc(filter->nsamples); |
| 3906 |
int j; |
| 3907 |
for (j=0; j<filter->nsamples; j++) out[i].pass_samples[j] = 0; |
| 3908 |
} |
| 3909 |
} |
| 3910 |
|
| 3911 |
if (0) filter_debug_print(out, NULL, nout); |
| 3912 |
|
| 3913 |
if ( mops ) free(ops); |
| 3914 |
filter->filters = out; |
| 3915 |
filter->nfilters = nout; |
| 3916 |
filter->flt_stack = (token_t **)malloc(sizeof(token_t*)*nout); |
| 3917 |
return filter; |
| 3918 |
} |
| 3919 |
filter_t *filter_parse(bcf_hdr_t *hdr, const char *str) |
| 3920 |
{ |
| 3921 |
return filter_init_(hdr, str, 0); |
| 3922 |
} |
| 3923 |
filter_t *filter_init(bcf_hdr_t *hdr, const char *str) |
| 3924 |
{ |
| 3925 |
return filter_init_(hdr, str, 1); |
| 3926 |
} |
| 3927 |
|
| 3928 |
void filter_destroy(filter_t *filter) |
| 3929 |
{ |
| 3930 |
perl_destroy(filter); |
| 3931 |
int i; |
| 3932 |
for (i=0; i<filter->nfilters; i++) |
| 3933 |
{ |
| 3934 |
if ( filter->filters[i].key ) free(filter->filters[i].key); |
| 3935 |
free(filter->filters[i].str_value.s); |
| 3936 |
free(filter->filters[i].tag); |
| 3937 |
free(filter->filters[i].idxs); |
| 3938 |
free(filter->filters[i].usmpl); |
| 3939 |
free(filter->filters[i].values); |
| 3940 |
free(filter->filters[i].pass_samples); |
| 3941 |
if (filter->filters[i].hash) khash_str2int_destroy_free(filter->filters[i].hash); |
| 3942 |
if (filter->filters[i].regex) |
| 3943 |
{ |
| 3944 |
regfree(filter->filters[i].regex); |
| 3945 |
free(filter->filters[i].regex); |
| 3946 |
} |
| 3947 |
} |
| 3948 |
for (i=0; i<filter->nundef_tag; i++) free(filter->undef_tag[i]); |
| 3949 |
for (i=0; i<filter->nused_tag; i++) free(filter->used_tag[i]); |
| 3950 |
free(filter->ext); |
| 3951 |
free(filter->undef_tag); |
| 3952 |
free(filter->used_tag); |
| 3953 |
free(filter->cached_GT.buf); |
| 3954 |
free(filter->cached_GT.mask); |
| 3955 |
free(filter->filters); |
| 3956 |
free(filter->flt_stack); |
| 3957 |
free(filter->str); |
| 3958 |
free(filter->tmpi); |
| 3959 |
free(filter->tmpf); |
| 3960 |
free(filter->tmps.s); |
| 3961 |
free(filter); |
| 3962 |
} |
| 3963 |
|
| 3964 |
int filter_test_ext(filter_t *filter, bcf1_t *rec, const uint8_t **samples, const void **ext) |
| 3965 |
{ |
| 3966 |
if ( !filter->n_ext ) |
| 3967 |
return filter_test(filter,rec,samples); |
| 3968 |
|
| 3969 |
int i; |
| 3970 |
for (i=0; i<filter->nfilters; i++) |
| 3971 |
{ |
| 3972 |
token_t *tok = &filter->filters[i]; |
| 3973 |
if ( !tok->iext ) continue; |
| 3974 |
if ( !ext[tok->iext-1] ) |
| 3975 |
{ |
| 3976 |
tok->is_missing = 1; |
| 3977 |
tok->nvalues = 0; |
| 3978 |
if ( filter->ext[tok->iext-1]==BCF_HT_STR ) tok->str_value.l = 0; |
| 3979 |
continue; |
| 3980 |
} |
| 3981 |
tok->is_missing = 0; |
| 3982 |
tok->nvalues = 1; |
| 3983 |
if ( filter->ext[tok->iext-1]==BCF_HT_STR ) |
| 3984 |
{ |
| 3985 |
tok->str_value.l = 0; |
| 3986 |
kputs((const char*)ext[tok->iext-1],&tok->str_value); |
| 3987 |
tok->nvalues = tok->str_value.l; |
| 3988 |
} |
| 3989 |
else if ( filter->ext[tok->iext-1]==BCF_HT_INT ) tok->values[0] = *((const int*)ext[tok->iext-1]); |
| 3990 |
else if ( filter->ext[tok->iext-1]==BCF_HT_REAL ) tok->values[0] = *((const float*)ext[tok->iext-1]); |
| 3991 |
} |
| 3992 |
return filter_test(filter,rec,samples); |
| 3993 |
} |
| 3994 |
|
| 3995 |
int filter_test(filter_t *filter, bcf1_t *line, const uint8_t **samples) |
| 3996 |
{ |
| 3997 |
if ( filter->status != FILTER_OK ) error("Error: the caller did not check the filter status\n"); |
| 3998 |
bcf_unpack(line, filter->max_unpack); |
| 3999 |
|
| 4000 |
int i, nstack = 0; |
| 4001 |
for (i=0; i<filter->nfilters; i++) |
| 4002 |
{ |
| 4003 |
filter->filters[i].pass_site = 0; |
| 4004 |
if ( filter->filters[i].tok_type == TOK_VAL ) |
| 4005 |
{ |
| 4006 |
if ( filter->filters[i].setter ) |
| 4007 |
filter->filters[i].setter(filter, line, &filter->filters[i]); |
| 4008 |
else if ( filter->filters[i].key ) |
| 4009 |
{ |
| 4010 |
filter->filters[i].str_value.l = 0; |
| 4011 |
kputs(filter->filters[i].key, &filter->filters[i].str_value); |
| 4012 |
filter->filters[i].nvalues = filter->filters[i].str_value.l; |
| 4013 |
} |
| 4014 |
else if ( filter->filters[i].is_constant ) |
| 4015 |
{ |
| 4016 |
filter->filters[i].values[0] = filter->filters[i].threshold; |
| 4017 |
filter->filters[i].nvalues = 1; |
| 4018 |
} |
| 4019 |
filter->flt_stack[nstack++] = &filter->filters[i]; |
| 4020 |
continue; |
| 4021 |
} |
| 4022 |
else if ( filter->filters[i].func ) |
| 4023 |
{ |
| 4024 |
int nargs = filter->filters[i].func(filter, line, &filter->filters[i], filter->flt_stack, nstack); |
| 4025 |
filter->flt_stack[nstack-nargs] = &filter->filters[i]; |
| 4026 |
if ( --nargs > 0 ) nstack -= nargs; |
| 4027 |
continue; |
| 4028 |
} |
| 4029 |
if ( nstack<2 ) |
| 4030 |
error("Error occurred while processing the filter \"%s\" (1:%d)\n", filter->str,nstack); |
| 4031 |
|
| 4032 |
int is_str = filter->flt_stack[nstack-1]->is_str + filter->flt_stack[nstack-2]->is_str; |
| 4033 |
|
| 4034 |
if ( filter->filters[i].tok_type == TOK_ADD ) |
| 4035 |
{ |
| 4036 |
VECTOR_ARITHMETICS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],+,(double)); |
| 4037 |
filter->flt_stack[nstack-2] = &filter->filters[i]; |
| 4038 |
nstack--; |
| 4039 |
continue; |
| 4040 |
} |
| 4041 |
else if ( filter->filters[i].tok_type == TOK_SUB ) |
| 4042 |
{ |
| 4043 |
VECTOR_ARITHMETICS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],-,(double)); |
| 4044 |
filter->flt_stack[nstack-2] = &filter->filters[i]; |
| 4045 |
nstack--; |
| 4046 |
continue; |
| 4047 |
} |
| 4048 |
else if ( filter->filters[i].tok_type == TOK_MULT ) |
| 4049 |
{ |
| 4050 |
VECTOR_ARITHMETICS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],*,(double)); |
| 4051 |
filter->flt_stack[nstack-2] = &filter->filters[i]; |
| 4052 |
nstack--; |
| 4053 |
continue; |
| 4054 |
} |
| 4055 |
else if ( filter->filters[i].tok_type == TOK_DIV ) |
| 4056 |
{ |
| 4057 |
VECTOR_ARITHMETICS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],/,(double)); |
| 4058 |
filter->flt_stack[nstack-2] = &filter->filters[i]; |
| 4059 |
nstack--; |
| 4060 |
continue; |
| 4061 |
} |
| 4062 |
else if ( filter->filters[i].tok_type == TOK_MODULO ) |
| 4063 |
{ |
| 4064 |
VECTOR_ARITHMETICS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],%,(int)); |
| 4065 |
filter->flt_stack[nstack-2] = &filter->filters[i]; |
| 4066 |
nstack--; |
| 4067 |
continue; |
| 4068 |
} |
| 4069 |
|
| 4070 |
|
| 4071 |
if ( filter->filters[i].comparator ) |
| 4072 |
{ |
| 4073 |
filter->filters[i].comparator(filter->flt_stack[nstack-1],filter->flt_stack[nstack-2],&filter->filters[i],line); |
| 4074 |
} |
| 4075 |
else if ( filter->flt_stack[nstack-1]->comparator ) |
| 4076 |
{ |
| 4077 |
filter->flt_stack[nstack-1]->comparator(filter->flt_stack[nstack-1],filter->flt_stack[nstack-2],&filter->filters[i],line); |
| 4078 |
} |
| 4079 |
else if ( filter->flt_stack[nstack-2]->comparator ) |
| 4080 |
{ |
| 4081 |
filter->flt_stack[nstack-2]->comparator(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],line); |
| 4082 |
} |
| 4083 |
else if ( is_str==2 ) |
| 4084 |
{ |
| 4085 |
cmp_vector_strings(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i]); |
| 4086 |
} |
| 4087 |
else |
| 4088 |
{ |
| 4089 |
if ( is_str==1 ) error("Error: cannot use arithmetic operators to compare strings and numbers\n"); |
| 4090 |
|
| 4091 |
|
| 4092 |
int missing_logic[] = {0,0,0}; |
| 4093 |
if ( filter->filters[i].tok_type == TOK_EQ ) { missing_logic[0] = missing_logic[2] = 1; } |
| 4094 |
if ( filter->filters[i].tok_type == TOK_NE ) { missing_logic[0] = missing_logic[1] = 1; } |
| 4095 |
|
| 4096 |
if ( filter->filters[i].tok_type == TOK_EQ ) |
| 4097 |
CMP_VECTORS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],==,missing_logic) |
| 4098 |
else if ( filter->filters[i].tok_type == TOK_NE ) |
| 4099 |
CMP_VECTORS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],!=,missing_logic) |
| 4100 |
else if ( filter->filters[i].tok_type == TOK_LE ) |
| 4101 |
CMP_VECTORS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],<=,missing_logic) |
| 4102 |
else if ( filter->filters[i].tok_type == TOK_LT ) |
| 4103 |
CMP_VECTORS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],<,missing_logic) |
| 4104 |
else if ( filter->filters[i].tok_type == TOK_BT ) |
| 4105 |
CMP_VECTORS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],>,missing_logic) |
| 4106 |
else if ( filter->filters[i].tok_type == TOK_BE ) |
| 4107 |
CMP_VECTORS(filter->flt_stack[nstack-2],filter->flt_stack[nstack-1],&filter->filters[i],>=,missing_logic) |
| 4108 |
else |
| 4109 |
error("todo: %s:%d .. type=%d\n", __FILE__,__LINE__,filter->filters[i].tok_type); |
| 4110 |
} |
| 4111 |
filter->flt_stack[nstack-2] = &filter->filters[i]; |
| 4112 |
nstack--; |
| 4113 |
} |
| 4114 |
if ( nstack>1 ) error("Error occurred while processing the filter \"%s\" (2:%d)\n", filter->str,nstack); |
| 4115 |
if ( samples ) |
| 4116 |
{ |
| 4117 |
*samples = filter->max_unpack&BCF_UN_FMT ? filter->flt_stack[0]->pass_samples : NULL; |
| 4118 |
if ( *samples && !filter->flt_stack[0]->nsamples ) |
| 4119 |
{ |
| 4120 |
for (i=0; i<filter->nsamples; i++) |
| 4121 |
filter->flt_stack[0]->pass_samples[i] = filter->flt_stack[0]->pass_site; |
| 4122 |
} |
| 4123 |
} |
| 4124 |
return filter->flt_stack[0]->pass_site; |
| 4125 |
} |
| 4126 |
|
| 4127 |
int filter_max_unpack(filter_t *flt) |
| 4128 |
{ |
| 4129 |
return flt->max_unpack; |
| 4130 |
} |
| 4131 |
const int *filter_ext_types(filter_t *filter, int *n_ext) |
| 4132 |
{ |
| 4133 |
*n_ext = filter->n_ext; |
| 4134 |
return filter->ext; |
| 4135 |
} |
| 4136 |
const double *filter_get_doubles(filter_t *filter, int *nval, int *nval1) |
| 4137 |
{ |
| 4138 |
token_t *tok = filter->flt_stack[0]; |
| 4139 |
if ( tok->nvalues ) |
| 4140 |
{ |
| 4141 |
*nval = tok->nvalues; |
| 4142 |
*nval1 = tok->nval1; |
| 4143 |
} |
| 4144 |
else |
| 4145 |
{ |
| 4146 |
if ( !tok->values ) error("fixme in filter_get_doubles(): %s\n", filter->str); |
| 4147 |
*nval = 1; |
| 4148 |
*nval1 = 1; |
| 4149 |
tok->values[0] = filter->flt_stack[0]->pass_site; |
| 4150 |
} |
| 4151 |
return tok->values; |
| 4152 |
} |
| 4153 |
|
| 4154 |
void filter_set_samples(filter_t *filter, const uint8_t *samples) |
| 4155 |
{ |
| 4156 |
int i,j; |
| 4157 |
for (i=0; i<filter->nfilters; i++) |
| 4158 |
{ |
| 4159 |
if ( !filter->filters[i].nsamples ) continue; |
| 4160 |
for (j=0; j<filter->filters[i].nsamples; j++) filter->filters[i].usmpl[j] = samples[j]; |
| 4161 |
} |
| 4162 |
} |
| 4163 |
|
| 4164 |
int filter_status(filter_t *filter) |
| 4165 |
{ |
| 4166 |
return filter->status; |
| 4167 |
} |
| 4168 |
|