diff --git a/NEWS b/NEWS index 46ec574f1..f2993366b 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ ## Release a.b +* New plugin `bcftools +variant-distance` to annotate records with distance to the + nearest variant (#1690) + * bcftools annotate - New `-H, --header-line` convenience option to pass a header line on command line, diff --git a/doc/bcftools.txt b/doc/bcftools.txt index 5e2fbf4c6..636c1a995 100644 --- a/doc/bcftools.txt +++ b/doc/bcftools.txt @@ -2622,6 +2622,9 @@ By default, appropriate system directories are searched for installed plugins. *trio-switch-rate*:: calculate phase switch rate in trio samples, children samples must have phased GTs +*variant-distance*:: + annotate sites with the distance to the nearest variant + *variantkey-hex*:: generate unsorted VariantKey-RSid index files in hexadecimal format diff --git a/plugins/variant-distance.c b/plugins/variant-distance.c new file mode 100644 index 000000000..433092c2e --- /dev/null +++ b/plugins/variant-distance.c @@ -0,0 +1,278 @@ +/* + Copyright (C) 2022 Genome Research Ltd. + + Author: Petr Danecek + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +/* + Add custom INFO field with the distance to the nearest variant + +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "bcftools.h" +#include "filter.h" +#include "vcfbuf.h" + +#define FLT_INCLUDE 1 +#define FLT_EXCLUDE 2 + +typedef struct +{ + char *tag; + int argc, region_is_file, target_is_file, output_type, clevel; + int regions_overlap, targets_overlap; + int32_t prev_dist; + char **argv, *region, *target, *fname, *output_fname; + char *filter_str; + filter_t *filter; + int filter_logic; // one of FLT_INCLUDE/FLT_EXCLUDE (-i or -e) + htsFile *out_fh; + bcf_hdr_t *hdr; + bcf_srs_t *sr; + vcfbuf_t *buf; +} +args_t; + +const char *about(void) +{ + return "Annotate sites with distance to the nearest variant\n"; +} + +static const char *usage_text(void) +{ + return + "\n" + "About: Annotate sites with distance to the nearest variant\n" + "Usage: bcftools +variant-distance [Options]\n" + "Options:\n" + " -n, --tag-name STR The tag name to be added [DIST]\n" + "Common options:\n" + " -e, --exclude EXPR Exclude sites for which the expression is true\n" + " -i, --include EXPR Include only sites for which the expression is true\n" + " -o, --output FILE Write output to the FILE [standard output]\n" + " -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" + " -r, --regions REGION Restrict to comma-separated list of regions\n" + " -R, --regions-file FILE Restrict to regions listed in a file\n" + " --regions-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [1]\n" + " -t, --targets REGION Similar to -r but streams rather than index-jumps\n" + " -T, --targets-file FILE Similar to -R but streams rather than index-jumps\n" + " --targets-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [0]\n" + "Examples:\n" + " bcftools +variant-distance input.bcf -Ob -o output.bcf\n" + "\n"; +} + +static void init_data(args_t *args) +{ + args->sr = bcf_sr_init(); + if ( args->region ) + { + args->sr->require_index = 1; + if ( bcf_sr_set_regions(args->sr, args->region, args->region_is_file)<0 ) error("Failed to read the regions: %s\n",args->region); + } + if ( args->target && bcf_sr_set_targets(args->sr, args->target, args->target_is_file, 0)<0 ) error("Failed to read the targets: %s\n",args->target); + if ( !bcf_sr_add_reader(args->sr,args->fname) ) error("Error: %s\n", bcf_sr_strerror(args->sr->errnum)); + args->hdr = bcf_sr_get_header(args->sr,0); + + if ( args->filter_str ) + args->filter = filter_init(args->hdr, args->filter_str); + + if ( !args->tag ) args->tag = strdup("DIST"); + bcf_hdr_printf(args->hdr,"##INFO=",args->tag); + + char wmode[8]; + set_wmode(wmode,args->output_type,args->output_fname,args->clevel); + args->out_fh = hts_open(args->output_fname ? args->output_fname : "-", wmode); + if ( args->out_fh == NULL ) error("Can't write to \"%s\": %s\n", args->output_fname, strerror(errno)); + + if ( bcf_hdr_write(args->out_fh, args->hdr)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname); + + args->buf = vcfbuf_init(args->hdr, 0); +} +static void destroy_data(args_t *args) +{ + if ( args->filter ) + filter_destroy(args->filter); + if ( hts_close(args->out_fh)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->output_fname); + bcf_sr_destroy(args->sr); + vcfbuf_destroy(args->buf); + free(args->tag); + free(args); +} +static void flush(args_t *args, int n) +{ + int i; + for (i=0; ibuf, 1); + if ( args->prev_dist ) + { + if ( bcf_update_info_int32(args->hdr,rec,args->tag,&args->prev_dist,1)!=0 ) + error("[%s] Error: failed to update INFO/%s=%d\n",__func__,args->tag,args->prev_dist); + } + if ( bcf_write(args->out_fh,args->hdr,rec)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname); + } +} +static void process(args_t *args, int done) +{ + if ( done ) + { + // If the buffer is not empty, all records must be duplicate positions and + // the previous site may or may not be set + flush(args, vcfbuf_nsites(args->buf)); + return; + } + + // Do filtering when requested + bcf1_t *rec = bcf_sr_get_line(args->sr,0); + if ( args->filter ) + { + int pass = filter_test(args->filter, rec, NULL); + if ( args->filter_logic==FLT_EXCLUDE ) pass = pass ? 0 : 1; + if ( !pass ) return; + } + bcf_sr_t *sr = bcf_sr_get_reader(args->sr,0); + sr->buffer[0] = vcfbuf_push(args->buf, rec); + + // Find the block of duplicate records (the same chr+position) + int i, n = vcfbuf_nsites(args->buf); + bcf1_t *rec0 = vcfbuf_peek(args->buf, 0); + for (i=1; ibuf, i); + if ( rec->rid != rec0->rid ) break; + if ( rec->pos != rec0->pos ) break; + } + if ( i==n ) return; // cannot flush yet, either a single record or all duplicates + if ( rec->rid != rec0->rid ) // the new record is a different chr + { + flush(args, i); + args->prev_dist = 0; + return; + } + + // the new record is a different pos + if ( !args->prev_dist || args->prev_dist > rec->pos - rec0->pos ) args->prev_dist = rec->pos - rec0->pos; + flush(args, i); + args->prev_dist = rec->pos - rec0->pos; +} + +int run(int argc, char **argv) +{ + args_t *args = (args_t*) calloc(1,sizeof(args_t)); + args->argc = argc; args->argv = argv; + args->output_type = FT_VCF; + args->output_fname = "-"; + args->clevel = -1; + static struct option loptions[] = + { + {"tag-name",required_argument,NULL,'n'}, + {"exclude",required_argument,NULL,'e'}, + {"include",required_argument,NULL,'i'}, + {"regions",required_argument,NULL,'r'}, + {"regions-file",required_argument,NULL,'R'}, + {"regions-overlap",required_argument,NULL,3}, + {"targets",required_argument,NULL,'t'}, + {"targets-file",required_argument,NULL,'T'}, + {"targets-overlap",required_argument,NULL,2}, + {"output",required_argument,NULL,'o'}, + {"output-type",required_argument,NULL,'O'}, + {NULL,0,NULL,0} + }; + int c; + char *tmp; + while ((c = getopt_long(argc, argv, "r:R:t:T:o:O:n:",loptions,NULL)) >= 0) + { + switch (c) + { + case 'n': args->tag = strdup(optarg); break; + case 'e': + if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n"); + args->filter_str = optarg; args->filter_logic |= FLT_EXCLUDE; break; + case 'i': + if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n"); + args->filter_str = optarg; args->filter_logic |= FLT_INCLUDE; break; + case 'T': args->target_is_file = 1; // fall-through + case 't': args->target = optarg; break; + case 'R': args->region_is_file = 1; // fall-through + case 'r': args->region = optarg; break; + case 2 : + args->targets_overlap = parse_overlap_option(optarg); + if ( args->targets_overlap < 0 ) error("Could not parse: --targets-overlap %s\n",optarg); + break; + case 3 : + args->regions_overlap = parse_overlap_option(optarg); + if ( args->regions_overlap < 0 ) error("Could not parse: --regions-overlap %s\n",optarg); + break; + case 'o': args->output_fname = optarg; break; + case 'O': + switch (optarg[0]) { + case 'b': args->output_type = FT_BCF_GZ; break; + case 'u': args->output_type = FT_BCF; break; + case 'z': args->output_type = FT_VCF_GZ; break; + case 'v': args->output_type = FT_VCF; break; + default: + { + args->clevel = strtol(optarg,&tmp,10); + if ( *tmp || args->clevel<0 || args->clevel>9 ) error("The output type \"%s\" not recognised\n", optarg); + } + } + if ( optarg[1] ) + { + args->clevel = strtol(optarg+1,&tmp,10); + if ( *tmp || args->clevel<0 || args->clevel>9 ) error("Could not parse argument: --compression-level %s\n", optarg+1); + } + break; + case 'h': + case '?': + default: error("%s", usage_text()); break; + } + } + if ( args->filter_logic == (FLT_EXCLUDE|FLT_INCLUDE) ) error("Only one of -i or -e can be given.\n"); + + if ( optind==argc ) + { + if ( !isatty(fileno((FILE *)stdin)) ) args->fname = "-"; // reading from stdin + else { error("%s",usage_text()); } + } + else if ( optind+1!=argc ) error("%s",usage_text()); + else args->fname = argv[optind]; + + init_data(args); + + while ( bcf_sr_next_line(args->sr) ) process(args,0); + process(args,1); + + destroy_data(args); + return 0; +} + + diff --git a/test/test.pl b/test/test.pl index 2e1d08198..c8ad114ff 100755 --- a/test/test.pl +++ b/test/test.pl @@ -629,6 +629,7 @@ test_vcf_plugin($opts,in=>'prune.1',out=>'prune.1.5.out',cmd=>q[+prune -w 2bp -n 1 --AF-tag AF -i 'GT="alt"']); # same as above but first discard REF-only sites test_vcf_plugin($opts,in=>'prune.1',out=>'prune.1.6.out',cmd=>'+prune -w 2bp -n 1 -N 1st'); test_vcf_plugin($opts,in=>'prune.1',out=>'prune.1.7.out',cmd=>'+prune -w 2bp -n 1 -N rand --random-seed 1'); +test_vcf_plugin($opts,in=>'variant-distance',out=>'variant-distance.1.out',cmd=>'+variant-distance'); test_plugin_split($opts,in=>'split.1',out=>'split.1.1.out',tmp=>'split.1.1'); test_plugin_split($opts,in=>'split.1',out=>'split.1.2.out',tmp=>'split.1.2',args=>'-S {PATH}/split.smpl.1.2.txt'); test_plugin_split($opts,in=>'split.1',out=>'split.1.3.out',tmp=>'split.1.3',args=>'-S {PATH}/split.smpl.1.3.txt'); diff --git a/test/variant-distance.1.out b/test/variant-distance.1.out new file mode 100644 index 000000000..2b3c23ad3 --- /dev/null +++ b/test/variant-distance.1.out @@ -0,0 +1,13 @@ +##fileformat=VCFv4.2 +##FILTER= +##reference=file://some/path/human_g1k_v37.fasta +##contig= +##INFO= +#CHROM POS ID REF ALT QUAL FILTER INFO +1 101 . T A . . DIST=3 +1 101 . T C . . DIST=3 +1 104 . T A . . DIST=3 +1 104 . T C . . DIST=3 +1 108 . T A . . DIST=4 +1 108 . T C . . DIST=4 +1 115 . T A . . DIST=7 diff --git a/test/variant-distance.vcf b/test/variant-distance.vcf new file mode 100644 index 000000000..2f7eef1e0 --- /dev/null +++ b/test/variant-distance.vcf @@ -0,0 +1,11 @@ +##fileformat=VCFv4.2 +##reference=file://some/path/human_g1k_v37.fasta +##contig= +#CHROM POS ID REF ALT QUAL FILTER INFO +1 101 . T A . . . +1 101 . T C . . . +1 104 . T A . . . +1 104 . T C . . . +1 108 . T A . . . +1 108 . T C . . . +1 115 . T A . . . diff --git a/vcfbuf.c b/vcfbuf.c index 71916bb6b..30f6a13b7 100644 --- a/vcfbuf.c +++ b/vcfbuf.c @@ -1,19 +1,19 @@ /* The MIT License - Copyright (c) 2016-2021 Genome Research Ltd. + Copyright (c) 2016-2022 Genome Research Ltd. Author: Petr Danecek - + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -135,6 +135,7 @@ void vcfbuf_set(vcfbuf_t *buf, vcfbuf_opt_t key, void *value) else if ( !strcasecmp(mode,"1st") ) buf->prune.mode = PRUNE_MODE_1ST; else if ( !strcasecmp(mode,"rand") ) buf->prune.mode = PRUNE_MODE_RAND; else error("The mode \"%s\" is not recognised\n",mode); + return; } } @@ -149,7 +150,7 @@ bcf1_t *vcfbuf_push(vcfbuf_t *buf, bcf1_t *rec) int i = rbuf_append(&buf->rbuf); if ( !buf->vcf[i].rec ) buf->vcf[i].rec = bcf_init1(); - + bcf1_t *ret = buf->vcf[i].rec; buf->vcf[i].rec = rec; buf->vcf[i].af_set = 0; @@ -226,7 +227,7 @@ static void _prune_sites(vcfbuf_t *buf, int flush_all) for (i=-1; rbuf_next(&buf->rbuf,&i) && irecvcf[i].rec; - if ( line->n_allele > buf->prune.mac ) + if ( line->n_allele > buf->prune.mac ) { buf->prune.ac = (int*) realloc(buf->prune.ac, line->n_allele*sizeof(*buf->prune.ac)); buf->prune.mac = line->n_allele; @@ -240,7 +241,7 @@ static void _prune_sites(vcfbuf_t *buf, int flush_all) } else if ( bcf_calc_ac(buf->hdr, line, buf->prune.ac, BCF_UN_INFO|BCF_UN_FMT) ) { - int ntot = buf->prune.ac[0], nalt = 0; + int ntot = buf->prune.ac[0], nalt = 0; for (k=1; kn_allele; k++) nalt += buf->prune.ac[k]; buf->vcf[i].af = ntot ? (float)nalt/ntot : 0; } @@ -315,7 +316,7 @@ static int _overlap_can_flush(vcfbuf_t *buf, int flush_all) { buf->overlap.rid = last->rec->rid; buf->overlap.end = end_pos; - return 0; + return 0; } if ( beg_pos <= buf->overlap.end ) { @@ -330,7 +331,7 @@ bcf1_t *vcfbuf_flush(vcfbuf_t *buf, int flush_all) int i,j; if ( buf->rbuf.n==0 ) return NULL; - if ( flush_all ) goto ret; + if ( flush_all || buf->win==0 ) goto ret; i = rbuf_kth(&buf->rbuf, 0); // first j = rbuf_last(&buf->rbuf); // last @@ -347,9 +348,9 @@ bcf1_t *vcfbuf_flush(vcfbuf_t *buf, int flush_all) else if ( buf->win < 0 ) { if ( buf->vcf[i].rec->pos - buf->vcf[j].rec->pos > buf->win ) return NULL; + goto ret; } - else return NULL; - + ret: if ( buf->prune.max_sites && buf->prune.max_sites < buf->rbuf.n ) _prune_sites(buf, flush_all); @@ -380,7 +381,7 @@ static double _estimate_af(int8_t *ptr, int size, int nvals, int nsamples) D =~ (GT correlation) * sqrt(Pa*(1-Pa)*Pb*(1-Pb)) and `hd` as proposed in Ragsdale, A. P., & Gravel, S. (2019). Unbiased estimation of linkage - disequilibrium from unphased data. Molecular Biology and Evolution. doi:10.1093/molbev/msz265 + disequilibrium from unphased data. Molecular Biology and Evolution. doi:10.1093/molbev/msz265 \hat{D} = 1/[n*(n+1)]*[ (n1 + n2/2 + n4/2 + n5/4)*(n5/4 + n6/2 + n8/2 + n9) @@ -423,7 +424,7 @@ static int _calc_r2_ld(vcfbuf_t *buf, bcf1_t *arec, bcf1_t *brec, vcfbuf_ld_t *l double nhd[] = {0,0,0,0,0,0,0,0,0}; double ab = 0, aa = 0, bb = 0, a = 0, b = 0; int nab = 0, ndiff = 0; - int an_tot = 0, bn_tot = 0; + int an_tot = 0, bn_tot = 0; for (i=0; in_sample; i++) { int8_t *aptr = (int8_t*) (afmt->p + i*afmt->size); @@ -508,7 +509,7 @@ static int _calc_r2_ld(vcfbuf_t *buf, bcf1_t *arec, bcf1_t *brec, vcfbuf_ld_t *l ld->val[VCFBUF_LD_IDX_LD] = fabs(ld->val[VCFBUF_LD_IDX_LD]); // avoid "-0" on output ld->val[VCFBUF_LD_IDX_HD] = - (nhd[0] + nhd[1]/2. + nhd[3]/2. + nhd[4]/4.)*(nhd[4]/4. + nhd[5]/2. + nhd[7]/2. + nhd[8]) + (nhd[0] + nhd[1]/2. + nhd[3]/2. + nhd[4]/4.)*(nhd[4]/4. + nhd[5]/2. + nhd[7]/2. + nhd[8]) - (nhd[1]/2. + nhd[2] + nhd[4]/4. + nhd[5]/2.)*(nhd[3]/2. + nhd[4]/4. + nhd[6] + nhd[7]/2.); ld->val[VCFBUF_LD_IDX_HD] /= nab; ld->val[VCFBUF_LD_IDX_HD] /= nab+1; @@ -535,7 +536,7 @@ int vcfbuf_ld(vcfbuf_t *buf, bcf1_t *rec, vcfbuf_ld_t *ld) } for (i=-1; rbuf_next(&buf->rbuf,&i); ) - { + { if ( buf->vcf[i].filter ) continue; if ( _calc_r2_ld(buf, buf->vcf[i].rec, rec, &tmp) < 0 ) continue; // missing genotypes diff --git a/vcfbuf.h b/vcfbuf.h index d3be6c53c..95c3b2f40 100644 --- a/vcfbuf.h +++ b/vcfbuf.h @@ -1,19 +1,19 @@ /* The MIT License - Copyright (c) 2017-2021 Genome Research Ltd. + Copyright (c) 2017-2022 Genome Research Ltd. Author: Petr Danecek - + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -49,7 +49,7 @@ typedef enum LD_FILTER1, // exclude the next record inserted by vcfbuf_push() from LD analysis LD_MAX_R2, // If set, vcfbuf_ld() will stop at the first record that exceeds the R2, LD_MAX_LD, // LD, or HD threshold. When multiple are set, the OR logic is applied - LD_MAX_HD, // + LD_MAX_HD, // } vcfbuf_opt_t; @@ -59,7 +59,8 @@ void vcfbuf_set(vcfbuf_t *buf, vcfbuf_opt_t key, void *value); /* * vcfbuf_init() - init buffer - * @win: number of sites (>0) or bp (<0) + * @win: number of sites (>0), bp (<0), or 0 in the dummy mode which is controlled manually by the caller + * with explicit push/peak/flush calls */ vcfbuf_t *vcfbuf_init(bcf_hdr_t *hdr, int win); void vcfbuf_destroy(vcfbuf_t *buf); @@ -81,6 +82,10 @@ bcf1_t *vcfbuf_peek(vcfbuf_t *buf, int idx); */ bcf1_t *vcfbuf_remove(vcfbuf_t *buf, int idx); +/* + * vcfbuf_flush() - returns the next record or NULL, depending on the mode of operation and + * the content of the buffer + */ bcf1_t *vcfbuf_flush(vcfbuf_t *buf, int flush_all); /*