| 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 <stdio.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <getopt.h> |
| 30 |
#include <assert.h> |
| 31 |
#include <ctype.h> |
| 32 |
#include <string.h> |
| 33 |
#include <strings.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include <sys/types.h> |
| 37 |
#include <math.h> |
| 38 |
#include <htslib/vcf.h> |
| 39 |
#include <htslib/synced_bcf_reader.h> |
| 40 |
#include <htslib/vcfutils.h> |
| 41 |
#include "bcftools.h" |
| 42 |
#include "filter.h" |
| 43 |
#include "rbuf.h" |
| 44 |
#include "regidx.h" |
| 45 |
|
| 46 |
|
| 47 |
#define FLT_INCLUDE 1 |
| 48 |
#define FLT_EXCLUDE 2 |
| 49 |
|
| 50 |
|
| 51 |
#define ANNOT_ADD 1 |
| 52 |
#define ANNOT_RESET 2 |
| 53 |
|
| 54 |
|
| 55 |
#define SET_GTS_MISSING 1 |
| 56 |
#define SET_GTS_REF 2 |
| 57 |
|
| 58 |
typedef struct _args_t |
| 59 |
{ |
| 60 |
filter_t *filter; |
| 61 |
char *filter_str; |
| 62 |
int filter_logic; |
| 63 |
const uint8_t *smpl_pass; |
| 64 |
int set_gts; |
| 65 |
char *soft_filter; |
| 66 |
int annot_mode; |
| 67 |
int flt_fail, flt_pass; |
| 68 |
int snp_gap, snp_gap_type, indel_gap, IndelGap_id, SnpGap_id; |
| 69 |
char *snp_gap_str; |
| 70 |
int32_t ntmpi, *tmpi, ntmp_ac, *tmp_ac; |
| 71 |
rbuf_t rbuf; |
| 72 |
bcf1_t **rbuf_lines; |
| 73 |
|
| 74 |
bcf_srs_t *files; |
| 75 |
bcf_hdr_t *hdr; |
| 76 |
htsFile *out_fh; |
| 77 |
int output_type, n_threads, clevel; |
| 78 |
|
| 79 |
char **argv, *output_fname, *targets_list, *regions_list, *mask_list; |
| 80 |
int argc, record_cmd_line, mask_is_file, mask_overlap, mask_negate; |
| 81 |
regidx_t *mask; |
| 82 |
char *index_fn; |
| 83 |
int write_index; |
| 84 |
} |
| 85 |
args_t; |
| 86 |
|
| 87 |
static void init_data(args_t *args) |
| 88 |
{ |
| 89 |
char wmode[8]; |
| 90 |
set_wmode(wmode,args->output_type,args->output_fname,args->clevel); |
| 91 |
args->out_fh = hts_open(args->output_fname ? args->output_fname : "-", wmode); |
| 92 |
if ( args->out_fh == NULL ) error("Can't write to \"%s\": %s\n", args->output_fname, strerror(errno)); |
| 93 |
if ( args->n_threads ) hts_set_threads(args->out_fh, args->n_threads); |
| 94 |
|
| 95 |
if ( args->mask_list ) |
| 96 |
{ |
| 97 |
if ( args->mask_list[0]=='^' ) args->mask_negate = 1; |
| 98 |
if ( args->mask_is_file ) |
| 99 |
args->mask = regidx_init(args->mask_negate?args->mask_list+1:args->mask_list,NULL,NULL,0,NULL); |
| 100 |
else |
| 101 |
{ |
| 102 |
char *rmme = strdup(args->mask_negate?args->mask_list+1:args->mask_list), *tmp = rmme; |
| 103 |
while ( *tmp ) |
| 104 |
{ |
| 105 |
if ( *tmp==',' ) *tmp = '\n'; |
| 106 |
tmp++; |
| 107 |
} |
| 108 |
args->mask = regidx_init_string(rmme, regidx_parse_reg, NULL, 0, NULL); |
| 109 |
free(rmme); |
| 110 |
} |
| 111 |
if ( !args->mask ) |
| 112 |
error("Could not initialize the mask: %s\n",args->mask_list); |
| 113 |
} |
| 114 |
|
| 115 |
args->hdr = args->files->readers[0].header; |
| 116 |
args->flt_pass = bcf_hdr_id2int(args->hdr,BCF_DT_ID,"PASS"); assert( !args->flt_pass ); |
| 117 |
|
| 118 |
if ( args->soft_filter && (args->filter_logic || args->mask_list) ) |
| 119 |
{ |
| 120 |
kstring_t flt_name = {0,0,0}; |
| 121 |
if ( strcmp(args->soft_filter,"+") ) |
| 122 |
kputs(args->soft_filter, &flt_name); |
| 123 |
else |
| 124 |
{ |
| 125 |
|
| 126 |
int i = 0, id = -1; |
| 127 |
do |
| 128 |
{ |
| 129 |
ksprintf(&flt_name,"Filter%d", ++i); |
| 130 |
id = bcf_hdr_id2int(args->hdr,BCF_DT_ID,flt_name.s); |
| 131 |
} |
| 132 |
while ( bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FLT,id) ); |
| 133 |
} |
| 134 |
|
| 135 |
kstring_t tmp = {0,0,0}; |
| 136 |
if ( args->filter_logic ) |
| 137 |
{ |
| 138 |
|
| 139 |
ksprintf(&tmp,"Set if %s: ",args->filter_logic & FLT_INCLUDE ? "not true" : "true"); |
| 140 |
|
| 141 |
|
| 142 |
char *t = args->filter_str; |
| 143 |
while ( *t ) |
| 144 |
{ |
| 145 |
if ( *t=='"' ) kputc('\\',&tmp); |
| 146 |
kputc(*t,&tmp); |
| 147 |
t++; |
| 148 |
} |
| 149 |
} |
| 150 |
else if ( args->mask_list ) |
| 151 |
ksprintf(&tmp,"Record masked by region"); |
| 152 |
|
| 153 |
int ret = bcf_hdr_printf(args->hdr, "##FILTER=<ID=%s,Description=\"%s\">", flt_name.s,tmp.s); |
| 154 |
if ( ret!=0 ) |
| 155 |
error("Failed to append header line: ##FILTER=<ID=%s,Description=\"%s\">\n", flt_name.s,tmp.s); |
| 156 |
args->flt_fail = bcf_hdr_id2int(args->hdr,BCF_DT_ID,flt_name.s); assert( args->flt_fail>=0 ); |
| 157 |
free(flt_name.s); |
| 158 |
free(tmp.s); |
| 159 |
} |
| 160 |
|
| 161 |
if ( args->snp_gap || args->indel_gap ) |
| 162 |
{ |
| 163 |
if ( !args->filter_logic && args->soft_filter && strcmp(args->soft_filter,"+") ) |
| 164 |
{ |
| 165 |
kstring_t tmp = {0,0,0}; |
| 166 |
if ( args->snp_gap ) kputs("\"SnpGap\"", &tmp); |
| 167 |
if ( args->indel_gap ) |
| 168 |
{ |
| 169 |
if ( tmp.s ) kputs(" and ", &tmp); |
| 170 |
kputs("\"IndelGap\"", &tmp); |
| 171 |
} |
| 172 |
if ( strncmp(tmp.s+1,args->soft_filter,tmp.l-2) ) |
| 173 |
fprintf(bcftools_stderr,"Warning: using %s filter name instead of \"%s\"\n", tmp.s,args->soft_filter); |
| 174 |
free(tmp.s); |
| 175 |
} |
| 176 |
|
| 177 |
rbuf_init(&args->rbuf, 64); |
| 178 |
args->rbuf_lines = (bcf1_t**) calloc(args->rbuf.m, sizeof(bcf1_t*)); |
| 179 |
if ( args->snp_gap ) |
| 180 |
{ |
| 181 |
bcf_hdr_printf(args->hdr, "##FILTER=<ID=SnpGap,Description=\"SNP within %d bp of %s\">", args->snp_gap,args->snp_gap_str); |
| 182 |
args->SnpGap_id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, "SnpGap"); |
| 183 |
assert( args->SnpGap_id>=0 ); |
| 184 |
} |
| 185 |
if ( args->indel_gap ) |
| 186 |
{ |
| 187 |
bcf_hdr_printf(args->hdr, "##FILTER=<ID=IndelGap,Description=\"Indel within %d bp of an indel\">", args->indel_gap); |
| 188 |
args->IndelGap_id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, "IndelGap"); |
| 189 |
assert( args->IndelGap_id>=0 ); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if (args->record_cmd_line) bcf_hdr_append_version(args->hdr, args->argc, args->argv, "bcftools_filter"); |
| 194 |
|
| 195 |
if ( args->filter_str ) |
| 196 |
args->filter = filter_init(args->hdr, args->filter_str); |
| 197 |
} |
| 198 |
|
| 199 |
static void destroy_data(args_t *args) |
| 200 |
{ |
| 201 |
if ( args->rbuf_lines ) |
| 202 |
{ |
| 203 |
int i; |
| 204 |
for (i=0; i<args->rbuf.m; i++) |
| 205 |
if ( args->rbuf_lines[i] ) bcf_destroy1(args->rbuf_lines[i]); |
| 206 |
free(args->rbuf_lines); |
| 207 |
} |
| 208 |
if ( args->filter ) |
| 209 |
filter_destroy(args->filter); |
| 210 |
free(args->tmpi); |
| 211 |
free(args->tmp_ac); |
| 212 |
if ( args->mask ) regidx_destroy(args->mask); |
| 213 |
} |
| 214 |
|
| 215 |
static void flush_buffer(args_t *args, int n) |
| 216 |
{ |
| 217 |
int i, j; |
| 218 |
for (i=0; i<n; i++) |
| 219 |
{ |
| 220 |
int k = rbuf_shift(&args->rbuf); |
| 221 |
bcf1_t *rec = args->rbuf_lines[k]; |
| 222 |
|
| 223 |
int pass = 1; |
| 224 |
if ( !args->soft_filter ) |
| 225 |
{ |
| 226 |
for (j=0; j<rec->d.n_flt; j++) |
| 227 |
{ |
| 228 |
if ( args->indel_gap && rec->d.flt[j]==args->IndelGap_id ) { pass = 0; break; } |
| 229 |
if ( args->snp_gap && rec->d.flt[j]==args->SnpGap_id ) { pass = 0; break; } |
| 230 |
} |
| 231 |
} |
| 232 |
if ( pass && bcf_write1(args->out_fh, args->hdr, rec)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
#define SWAP(type_t, a, b) { type_t t = a; a = b; b = t; } |
| 237 |
static void buffered_filters(args_t *args, bcf1_t *line) |
| 238 |
{ |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
const int SnpGap_set = 1 << (8*sizeof(int)/2); |
| 262 |
const int IndelGap_set = 1 << (8*sizeof(int)/2-1); |
| 263 |
const int IndelGap_flush = 1 << (8*sizeof(int)/2-2); |
| 264 |
|
| 265 |
int var_type = 0, i; |
| 266 |
if ( line ) |
| 267 |
{ |
| 268 |
|
| 269 |
int ilast = rbuf_last(&args->rbuf); |
| 270 |
if ( ilast>=0 && line->rid != args->rbuf_lines[ilast]->rid ) |
| 271 |
flush_buffer(args, args->rbuf.n); |
| 272 |
|
| 273 |
rbuf_expand0(&args->rbuf,bcf1_t*,args->rbuf.n,args->rbuf_lines); |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
ilast = rbuf_append(&args->rbuf); |
| 279 |
if ( !args->rbuf_lines[ilast] ) args->rbuf_lines[ilast] = bcf_init1(); |
| 280 |
SWAP(bcf1_t*, args->files->readers[0].buffer[0], args->rbuf_lines[ilast]); |
| 281 |
|
| 282 |
var_type = bcf_get_variant_types(line); |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
line->d.var[0].n = line->rlen; |
| 291 |
} |
| 292 |
|
| 293 |
int k_flush = 1; |
| 294 |
if ( args->indel_gap ) |
| 295 |
{ |
| 296 |
k_flush = 0; |
| 297 |
|
| 298 |
int last_to = -1; |
| 299 |
for (i=-1; rbuf_next(&args->rbuf,&i); ) |
| 300 |
{ |
| 301 |
bcf1_t *rec = args->rbuf_lines[i]; |
| 302 |
int rec_from = rec->pos; |
| 303 |
if ( last_to!=-1 && last_to < rec_from ) break; |
| 304 |
|
| 305 |
k_flush++; |
| 306 |
if ( !(rec->d.var_type & VCF_INDEL) ) continue; |
| 307 |
|
| 308 |
rec->d.var_type |= IndelGap_set; |
| 309 |
last_to = args->indel_gap + rec->pos + rec->d.var[0].n - 1; |
| 310 |
} |
| 311 |
if ( i==args->rbuf.f && line && last_to!=-1 ) k_flush = 0; |
| 312 |
if ( k_flush || !line ) |
| 313 |
{ |
| 314 |
|
| 315 |
int k = 0, max_ac = -1, imax_ac = -1, max_qual = -1, imax_qual = -1; |
| 316 |
for (i=-1; rbuf_next(&args->rbuf,&i) && k<k_flush; ) |
| 317 |
{ |
| 318 |
k++; |
| 319 |
bcf1_t *rec = args->rbuf_lines[i]; |
| 320 |
if ( !(rec->d.var_type & IndelGap_set) ) continue; |
| 321 |
hts_expand(int, rec->n_allele, args->ntmpi, args->tmpi); |
| 322 |
int ret = bcf_calc_ac(args->hdr, rec, args->tmpi, BCF_UN_ALL); |
| 323 |
if ( imax_ac==-1 || (ret && max_ac < args->tmpi[1]) ) { max_ac = args->tmpi[1]; imax_ac = i; } |
| 324 |
if ( imax_qual==-1 || max_qual < rec->qual ) { max_qual = rec->qual; imax_qual = i; } |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
k = 0; |
| 329 |
for (i=-1; rbuf_next(&args->rbuf,&i) && k<k_flush; ) |
| 330 |
{ |
| 331 |
k++; |
| 332 |
bcf1_t *rec = args->rbuf_lines[i]; |
| 333 |
if ( !(rec->d.var_type & IndelGap_set) ) continue; |
| 334 |
rec->d.var_type |= IndelGap_flush; |
| 335 |
|
| 336 |
int do_filter = 0; |
| 337 |
if ( max_qual>0 ) |
| 338 |
{ |
| 339 |
if ( i!=imax_qual ) do_filter = 1; |
| 340 |
} |
| 341 |
else if ( i!=imax_ac ) do_filter = 1; |
| 342 |
if ( do_filter ) bcf_add_filter(args->hdr, args->rbuf_lines[i], args->IndelGap_id); |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
if ( !line ) |
| 348 |
{ |
| 349 |
|
| 350 |
flush_buffer(args, args->rbuf.n); |
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
int j_flush = 1; |
| 355 |
if ( args->snp_gap ) |
| 356 |
{ |
| 357 |
j_flush = 0; |
| 358 |
int last_from = line->pos; |
| 359 |
for (i=-1; rbuf_next(&args->rbuf,&i); ) |
| 360 |
{ |
| 361 |
bcf1_t *rec = args->rbuf_lines[i]; |
| 362 |
int rec_to = rec->pos + rec->d.var[0].n - 1; |
| 363 |
if ( rec_to + args->snp_gap < last_from ) |
| 364 |
j_flush++; |
| 365 |
else if ( (var_type & args->snp_gap_type) && (rec->d.var_type & VCF_SNP) && !(rec->d.var_type & SnpGap_set) ) |
| 366 |
{ |
| 367 |
|
| 368 |
rec->d.var_type |= SnpGap_set; |
| 369 |
bcf_add_filter(args->hdr, rec, args->SnpGap_id); |
| 370 |
} |
| 371 |
else if ( (var_type & VCF_SNP) && (rec->d.var_type & args->snp_gap_type) ) |
| 372 |
{ |
| 373 |
|
| 374 |
line->d.var_type |= SnpGap_set; |
| 375 |
bcf_add_filter(args->hdr, line, args->SnpGap_id); |
| 376 |
break; |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
flush_buffer(args, j_flush < k_flush ? j_flush : k_flush); |
| 381 |
} |
| 382 |
|
| 383 |
static void set_genotypes(args_t *args, bcf1_t *line, int pass_site) |
| 384 |
{ |
| 385 |
int i,j; |
| 386 |
if ( !bcf_hdr_nsamples(args->hdr) ) return; |
| 387 |
if ( args->smpl_pass ) |
| 388 |
{ |
| 389 |
int npass = 0; |
| 390 |
for (i=0; i<bcf_hdr_nsamples(args->hdr); i++) npass += args->smpl_pass[i]; |
| 391 |
|
| 392 |
|
| 393 |
if ( npass==bcf_hdr_nsamples(args->hdr) && (args->filter_logic & FLT_INCLUDE) ) return; |
| 394 |
if ( npass==0 && (args->filter_logic & FLT_EXCLUDE) ) return; |
| 395 |
} |
| 396 |
else if ( pass_site ) return; |
| 397 |
|
| 398 |
int an = 0, has_an = bcf_get_info_int32(args->hdr, line, "AN", &args->tmp_ac, &args->ntmp_ac); |
| 399 |
if ( has_an==1 ) an = args->tmp_ac[0]; |
| 400 |
else has_an = 0; |
| 401 |
|
| 402 |
int has_ac = bcf_get_info_int32(args->hdr, line, "AC", &args->tmp_ac, &args->ntmp_ac); |
| 403 |
has_ac = has_ac==line->n_allele-1 ? 1 : 0; |
| 404 |
|
| 405 |
int new_gt = 0, ngts = bcf_get_format_int32(args->hdr, line, "GT", &args->tmpi, &args->ntmpi); |
| 406 |
ngts /= bcf_hdr_nsamples(args->hdr); |
| 407 |
if ( args->set_gts==SET_GTS_MISSING ) new_gt = bcf_gt_missing; |
| 408 |
else if ( args->set_gts==SET_GTS_REF ) new_gt = bcf_gt_unphased(0); |
| 409 |
else error("todo: set_gts=%d\n", args->set_gts); |
| 410 |
for (i=0; i<bcf_hdr_nsamples(args->hdr); i++) |
| 411 |
{ |
| 412 |
if ( args->smpl_pass ) |
| 413 |
{ |
| 414 |
int pass = args->smpl_pass[i]; |
| 415 |
if ( args->filter_logic & FLT_EXCLUDE ) pass = pass ? 0 : 1; |
| 416 |
if ( pass ) continue; |
| 417 |
} |
| 418 |
int32_t *gts = args->tmpi + ngts*i; |
| 419 |
for (j=0; j<ngts; j++) |
| 420 |
{ |
| 421 |
if ( gts[j]==bcf_int32_vector_end ) break; |
| 422 |
if ( args->set_gts==SET_GTS_MISSING && !bcf_gt_is_missing(gts[j]) ) |
| 423 |
{ |
| 424 |
int ial = bcf_gt_allele(gts[j]); |
| 425 |
if ( has_ac && ial>0 && ial<=line->n_allele ) args->tmp_ac[ ial-1 ]--; |
| 426 |
an--; |
| 427 |
} |
| 428 |
else if ( args->set_gts==SET_GTS_REF ) |
| 429 |
{ |
| 430 |
int ial = bcf_gt_allele(gts[j]); |
| 431 |
if ( bcf_gt_is_missing(gts[j]) ) an++; |
| 432 |
else if ( has_ac && ial>0 && ial<=line->n_allele ) args->tmp_ac[ ial-1 ]--; |
| 433 |
} |
| 434 |
gts[j] = new_gt; |
| 435 |
} |
| 436 |
} |
| 437 |
bcf_update_genotypes(args->hdr,line,args->tmpi,ngts*bcf_hdr_nsamples(args->hdr)); |
| 438 |
if ( has_an ) bcf_update_info_int32(args->hdr,line,"AN",&an,1); |
| 439 |
if ( has_ac ) bcf_update_info_int32(args->hdr,line,"AC",args->tmp_ac,line->n_allele-1); |
| 440 |
} |
| 441 |
|
| 442 |
static void _set_variant_boundaries(bcf1_t *rec, hts_pos_t *beg, hts_pos_t *end) |
| 443 |
{ |
| 444 |
hts_pos_t off; |
| 445 |
if ( rec->n_allele ) |
| 446 |
{ |
| 447 |
off = rec->rlen; |
| 448 |
bcf_unpack(rec, BCF_UN_STR); |
| 449 |
int i; |
| 450 |
for (i=1; i<rec->n_allele; i++) |
| 451 |
{ |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
int j = 0; |
| 457 |
char *ref = rec->d.allele[0]; |
| 458 |
char *alt = rec->d.allele[i]; |
| 459 |
while ( ref[j] && alt[j] && ref[j]==alt[j] ) j++; |
| 460 |
if ( off > j ) off = j; |
| 461 |
if ( !off ) break; |
| 462 |
} |
| 463 |
} |
| 464 |
else |
| 465 |
off = 0; |
| 466 |
|
| 467 |
*beg = rec->pos + off; |
| 468 |
*end = rec->pos + rec->rlen - 1; |
| 469 |
} |
| 470 |
|
| 471 |
static void usage(args_t *args) |
| 472 |
{ |
| 473 |
fprintf(bcftools_stderr, "\n"); |
| 474 |
fprintf(bcftools_stderr, "About: Apply fixed-threshold filters.\n"); |
| 475 |
fprintf(bcftools_stderr, "Usage: bcftools filter [options] <in.vcf.gz>\n"); |
| 476 |
fprintf(bcftools_stderr, "\n"); |
| 477 |
fprintf(bcftools_stderr, "Options:\n"); |
| 478 |
fprintf(bcftools_stderr, " -e, --exclude EXPR Exclude sites for which the expression is true (see man page for details)\n"); |
| 479 |
fprintf(bcftools_stderr, " -g, --SnpGap INT[:TYPE] Filter SNPs within <int> base pairs of an indel (the default) or any combination of indel,mnp,bnd,other,overlap\n"); |
| 480 |
fprintf(bcftools_stderr, " -G, --IndelGap INT Filter clusters of indels separated by <int> or fewer base pairs allowing only one to pass\n"); |
| 481 |
fprintf(bcftools_stderr, " -i, --include EXPR Include only sites for which the expression is true (see man page for details\n"); |
| 482 |
fprintf(bcftools_stderr, " --mask [^]REGION Soft filter regions, \"^\" to negate\n"); |
| 483 |
fprintf(bcftools_stderr, " -M, --mask-file [^]FILE Soft filter regions listed in a file, \"^\" to negate\n"); |
| 484 |
fprintf(bcftools_stderr, " --mask-overlap 0|1|2 Mask if POS in the region (0), record overlaps (1), variant overlaps (2) [1]\n"); |
| 485 |
fprintf(bcftools_stderr, " -m, --mode [+x] \"+\": do not replace but add to existing FILTER; \"x\": reset filters at sites which pass\n"); |
| 486 |
fprintf(bcftools_stderr, " --no-version Do not append version and command line to the header\n"); |
| 487 |
fprintf(bcftools_stderr, " -o, --output FILE Write output to a file [standard output]\n"); |
| 488 |
fprintf(bcftools_stderr, " -O, --output-type u|b|v|z[0-9] u/b: un/compressed BCF, v/z: un/compressed VCF, 0-9: compression level [v]\n"); |
| 489 |
fprintf(bcftools_stderr, " -r, --regions REGION Restrict to comma-separated list of regions\n"); |
| 490 |
fprintf(bcftools_stderr, " -R, --regions-file FILE Restrict to regions listed in a file\n"); |
| 491 |
fprintf(bcftools_stderr, " --regions-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [1]\n"); |
| 492 |
fprintf(bcftools_stderr, " -s, --soft-filter STRING Annotate FILTER column with <string> or unique filter name (\"Filter%%d\") made up by the program (\"+\")\n"); |
| 493 |
fprintf(bcftools_stderr, " -S, --set-GTs .|0 Set genotypes of failed samples to missing (.) or ref (0)\n"); |
| 494 |
fprintf(bcftools_stderr, " -t, --targets REGION Similar to -r but streams rather than index-jumps\n"); |
| 495 |
fprintf(bcftools_stderr, " -T, --targets-file FILE Similar to -R but streams rather than index-jumps\n"); |
| 496 |
fprintf(bcftools_stderr, " --targets-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [0]\n"); |
| 497 |
fprintf(bcftools_stderr, " --threads INT Use multithreading with <int> worker threads [0]\n"); |
| 498 |
fprintf(bcftools_stderr, " -W, --write-index[=FMT] Automatically index the output files [off]\n"); |
| 499 |
fprintf(bcftools_stderr, "\n"); |
| 500 |
bcftools_exit(1); |
| 501 |
} |
| 502 |
|
| 503 |
int main_vcffilter(int argc, char *argv[]) |
| 504 |
{ |
| 505 |
int c; |
| 506 |
args_t *args = (args_t*) calloc(1,sizeof(args_t)); |
| 507 |
args->argc = argc; args->argv = argv; |
| 508 |
args->files = bcf_sr_init(); |
| 509 |
args->output_fname = "-"; |
| 510 |
args->output_type = FT_VCF; |
| 511 |
args->n_threads = 0; |
| 512 |
args->record_cmd_line = 1; |
| 513 |
args->clevel = -1; |
| 514 |
int regions_is_file = 0, targets_is_file = 0; |
| 515 |
int regions_overlap = 1; |
| 516 |
int targets_overlap = 0; |
| 517 |
args->mask_overlap = 1; |
| 518 |
|
| 519 |
static struct option loptions[] = |
| 520 |
{ |
| 521 |
{"set-GTs",required_argument,NULL,'S'}, |
| 522 |
{"mode",required_argument,NULL,'m'}, |
| 523 |
{"mask",required_argument,NULL,10}, |
| 524 |
{"mask-file",required_argument,NULL,'M'}, |
| 525 |
{"mask-overlap",required_argument,NULL,11}, |
| 526 |
{"soft-filter",required_argument,NULL,'s'}, |
| 527 |
{"exclude",required_argument,NULL,'e'}, |
| 528 |
{"include",required_argument,NULL,'i'}, |
| 529 |
{"targets",required_argument,NULL,'t'}, |
| 530 |
{"targets-file",required_argument,NULL,'T'}, |
| 531 |
{"targets-overlap",required_argument,NULL,4}, |
| 532 |
{"regions",required_argument,NULL,'r'}, |
| 533 |
{"regions-file",required_argument,NULL,'R'}, |
| 534 |
{"regions-overlap",required_argument,NULL,3}, |
| 535 |
{"output",required_argument,NULL,'o'}, |
| 536 |
{"output-type",required_argument,NULL,'O'}, |
| 537 |
{"threads",required_argument,NULL,9}, |
| 538 |
{"SnpGap",required_argument,NULL,'g'}, |
| 539 |
{"IndelGap",required_argument,NULL,'G'}, |
| 540 |
{"no-version",no_argument,NULL,8}, |
| 541 |
{"write-index",optional_argument,NULL,'W'}, |
| 542 |
{NULL,0,NULL,0} |
| 543 |
}; |
| 544 |
char *tmp; |
| 545 |
while ((c = getopt_long(argc, argv, "e:i:t:T:r:R:h?s:m:M:o:O:g:G:S:W::",loptions,NULL)) >= 0) { |
| 546 |
switch (c) { |
| 547 |
case 'g': |
| 548 |
args->snp_gap = strtol(optarg,&tmp,10); |
| 549 |
if ( *tmp && *tmp!=':' ) error("Could not parse argument: --SnpGap %s\n", optarg); |
| 550 |
if ( *tmp==':' ) |
| 551 |
{ |
| 552 |
args->snp_gap_str = tmp+1; |
| 553 |
int i,n; |
| 554 |
char **keys = hts_readlist(tmp+1,0,&n); |
| 555 |
for(i=0; i<n; i++) |
| 556 |
{ |
| 557 |
if ( !strcasecmp(keys[i],"indel") ) args->snp_gap_type |= VCF_INDEL; |
| 558 |
else if ( !strcasecmp(keys[i],"mnp") ) args->snp_gap_type |= VCF_MNP; |
| 559 |
else if ( !strcasecmp(keys[i],"bnd") ) args->snp_gap_type |= VCF_BND; |
| 560 |
else if ( !strcasecmp(keys[i],"other") ) args->snp_gap_type |= VCF_OTHER; |
| 561 |
else if ( !strcasecmp(keys[i],"overlap") ) args->snp_gap_type |= VCF_OVERLAP; |
| 562 |
else error("Could not parse \"%s\" in \"--SnpGap %s\"\n", keys[i], optarg); |
| 563 |
free(keys[i]); |
| 564 |
} |
| 565 |
if ( n ) free(keys); |
| 566 |
} |
| 567 |
else |
| 568 |
{ |
| 569 |
args->snp_gap_type = VCF_INDEL; |
| 570 |
args->snp_gap_str = "indel"; |
| 571 |
} |
| 572 |
break; |
| 573 |
case 'G': |
| 574 |
args->indel_gap = strtol(optarg,&tmp,10); |
| 575 |
if ( *tmp ) error("Could not parse argument: --IndelGap %s\n", optarg); |
| 576 |
break; |
| 577 |
case 'o': args->output_fname = optarg; break; |
| 578 |
case 'O': |
| 579 |
switch (optarg[0]) { |
| 580 |
case 'b': args->output_type = FT_BCF_GZ; break; |
| 581 |
case 'u': args->output_type = FT_BCF; break; |
| 582 |
case 'z': args->output_type = FT_VCF_GZ; break; |
| 583 |
case 'v': args->output_type = FT_VCF; break; |
| 584 |
default: |
| 585 |
{ |
| 586 |
args->clevel = strtol(optarg,&tmp,10); |
| 587 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("The output type \"%s\" not recognised\n", optarg); |
| 588 |
} |
| 589 |
} |
| 590 |
if ( optarg[1] ) |
| 591 |
{ |
| 592 |
args->clevel = strtol(optarg+1,&tmp,10); |
| 593 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("Could not parse argument: --compression-level %s\n", optarg+1); |
| 594 |
} |
| 595 |
break; |
| 596 |
case 's': args->soft_filter = optarg; break; |
| 597 |
case 'm': |
| 598 |
if ( strchr(optarg,'x') ) args->annot_mode |= ANNOT_RESET; |
| 599 |
if ( strchr(optarg,'+') ) args->annot_mode |= ANNOT_ADD; |
| 600 |
break; |
| 601 |
case 't': args->targets_list = optarg; break; |
| 602 |
case 'T': args->targets_list = optarg; targets_is_file = 1; break; |
| 603 |
case 'r': args->regions_list = optarg; break; |
| 604 |
case 'R': args->regions_list = optarg; regions_is_file = 1; break; |
| 605 |
case 'e': |
| 606 |
if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n"); |
| 607 |
args->filter_str = optarg; args->filter_logic |= FLT_EXCLUDE; break; |
| 608 |
case 'i': |
| 609 |
if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n"); |
| 610 |
args->filter_str = optarg; args->filter_logic |= FLT_INCLUDE; break; |
| 611 |
case 'S': |
| 612 |
if ( !strcmp(".",optarg) ) args->set_gts = SET_GTS_MISSING; |
| 613 |
else if ( !strcmp("0",optarg) ) args->set_gts = SET_GTS_REF; |
| 614 |
else error("The argument to -S not recognised: %s\n", optarg); |
| 615 |
break; |
| 616 |
case 9 : args->n_threads = strtol(optarg, 0, 0); break; |
| 617 |
case 8 : args->record_cmd_line = 0; break; |
| 618 |
case 3 : |
| 619 |
regions_overlap = parse_overlap_option(optarg); |
| 620 |
if ( regions_overlap < 0 ) error("Could not parse: --regions-overlap %s\n",optarg); |
| 621 |
break; |
| 622 |
case 4 : |
| 623 |
targets_overlap = parse_overlap_option(optarg); |
| 624 |
if ( targets_overlap < 0 ) error("Could not parse: --targets-overlap %s\n",optarg); |
| 625 |
break; |
| 626 |
case 10 : args->mask_list = optarg; break; |
| 627 |
case 'M' : args->mask_list = optarg; args->mask_is_file = 1; break; |
| 628 |
case 11 : |
| 629 |
if ( !strcasecmp(optarg,"0") ) args->mask_overlap = 0; |
| 630 |
else if ( !strcasecmp(optarg,"1") ) args->mask_overlap = 1; |
| 631 |
else if ( !strcasecmp(optarg,"2") ) args->mask_overlap = 2; |
| 632 |
else error("Could not parse: --mask-overlap %s\n",optarg); |
| 633 |
break; |
| 634 |
case 'W': |
| 635 |
if (!(args->write_index = write_index_parse(optarg))) |
| 636 |
error("Unsupported index format '%s'\n", optarg); |
| 637 |
break; |
| 638 |
case 'h': |
| 639 |
case '?': usage(args); break; |
| 640 |
default: error("Unknown argument: %s\n", optarg); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
if ( args->filter_logic == (FLT_EXCLUDE|FLT_INCLUDE) ) error("Only one of -i or -e can be given.\n"); |
| 645 |
char *fname = NULL; |
| 646 |
if ( optind>=argc ) |
| 647 |
{ |
| 648 |
if ( !isatty(fileno((FILE *)stdin)) ) fname = "-"; |
| 649 |
else usage(args); |
| 650 |
} |
| 651 |
else fname = argv[optind]; |
| 652 |
|
| 653 |
if ( args->mask_list && !args->soft_filter ) error("The option --soft-filter is required with --mask and --mask-file options\n"); |
| 654 |
|
| 655 |
|
| 656 |
if ( args->regions_list ) |
| 657 |
{ |
| 658 |
args->files->require_index = 1; |
| 659 |
bcf_sr_set_opt(args->files,BCF_SR_REGIONS_OVERLAP,regions_overlap); |
| 660 |
if ( bcf_sr_set_regions(args->files, args->regions_list, regions_is_file)<0 ) |
| 661 |
error("Failed to read the regions: %s\n", args->regions_list); |
| 662 |
} |
| 663 |
else if ( optind+1 < argc ) |
| 664 |
{ |
| 665 |
int i; |
| 666 |
kstring_t tmp = {0,0,0}; |
| 667 |
kputs(argv[optind+1],&tmp); |
| 668 |
for (i=optind+2; i<argc; i++) { kputc(',',&tmp); kputs(argv[i],&tmp); } |
| 669 |
args->files->require_index = 1; |
| 670 |
bcf_sr_set_opt(args->files,BCF_SR_REGIONS_OVERLAP,regions_overlap); |
| 671 |
if ( bcf_sr_set_regions(args->files, tmp.s, regions_is_file)<0 ) |
| 672 |
error("Failed to read the regions: %s\n", args->regions_list); |
| 673 |
free(tmp.s); |
| 674 |
} |
| 675 |
if ( args->targets_list ) |
| 676 |
{ |
| 677 |
bcf_sr_set_opt(args->files,BCF_SR_TARGETS_OVERLAP,targets_overlap); |
| 678 |
if ( bcf_sr_set_targets(args->files, args->targets_list,targets_is_file, 0)<0 ) |
| 679 |
error("Failed to read the targets: %s\n", args->targets_list); |
| 680 |
} |
| 681 |
if ( !bcf_sr_add_reader(args->files, fname) ) error("Failed to read from %s: %s\n", !strcmp("-",fname)?"standard input":fname,bcf_sr_strerror(args->files->errnum)); |
| 682 |
|
| 683 |
init_data(args); |
| 684 |
if ( bcf_hdr_write(args->out_fh, args->hdr)!=0 ) error("[%s] Error: cannot write the header to %s\n", __func__,args->output_fname); |
| 685 |
if ( init_index2(args->out_fh,args->hdr,args->output_fname,&args->index_fn, |
| 686 |
args->write_index)<0 ) |
| 687 |
error("Error: failed to initialise index for %s\n",args->output_fname); |
| 688 |
while ( bcf_sr_next_line(args->files) ) |
| 689 |
{ |
| 690 |
bcf1_t *line = bcf_sr_get_line(args->files, 0); |
| 691 |
int pass = 1; |
| 692 |
if ( args->filter ) |
| 693 |
{ |
| 694 |
pass = filter_test(args->filter, line, &args->smpl_pass); |
| 695 |
if ( args->filter_logic & FLT_EXCLUDE ) pass = pass ? 0 : 1; |
| 696 |
} |
| 697 |
if ( args->mask ) |
| 698 |
{ |
| 699 |
hts_pos_t beg, end; |
| 700 |
if ( args->mask_overlap==0 ) beg = end = line->pos; |
| 701 |
else if ( args->mask_overlap==1 ) beg = line->pos, end = line->pos + line->rlen - 1; |
| 702 |
else _set_variant_boundaries(line,&beg,&end); |
| 703 |
int mpass = regidx_overlap(args->mask,bcf_seqname(args->hdr,line),beg,end,NULL) ? 0 : 1; |
| 704 |
if ( args->mask_negate ) mpass = mpass ? 0 : 1; |
| 705 |
pass &= mpass; |
| 706 |
} |
| 707 |
if ( args->soft_filter || args->set_gts || pass ) |
| 708 |
{ |
| 709 |
if ( pass ) |
| 710 |
{ |
| 711 |
bcf_unpack(line,BCF_UN_FLT); |
| 712 |
if ( args->annot_mode & ANNOT_RESET || !line->d.n_flt ) bcf_add_filter(args->hdr, line, args->flt_pass); |
| 713 |
} |
| 714 |
else if ( args->soft_filter ) |
| 715 |
{ |
| 716 |
if ( (args->annot_mode & ANNOT_ADD) ) bcf_add_filter(args->hdr, line, args->flt_fail); |
| 717 |
else bcf_update_filter(args->hdr, line, &args->flt_fail, 1); |
| 718 |
} |
| 719 |
if ( args->set_gts ) set_genotypes(args, line, pass); |
| 720 |
if ( !args->rbuf_lines ) |
| 721 |
{ |
| 722 |
if ( bcf_write1(args->out_fh, args->hdr, line)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname); |
| 723 |
} |
| 724 |
else |
| 725 |
buffered_filters(args, line); |
| 726 |
} |
| 727 |
} |
| 728 |
buffered_filters(args, NULL); |
| 729 |
if ( args->write_index ) |
| 730 |
{ |
| 731 |
if ( bcf_idx_save(args->out_fh)<0 ) |
| 732 |
{ |
| 733 |
if ( hts_close(args->out_fh)!=0 ) error("Error: close failed .. %s\n", args->output_fname?args->output_fname:"bcftools_stdout"); |
| 734 |
error("Error: cannot write to index %s\n", args->index_fn); |
| 735 |
} |
| 736 |
free(args->index_fn); |
| 737 |
} |
| 738 |
if ( hts_close(args->out_fh)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->output_fname); |
| 739 |
destroy_data(args); |
| 740 |
bcf_sr_destroy(args->files); |
| 741 |
free(args); |
| 742 |
return 0; |
| 743 |
} |