Source code

Download source archive ↓

upstream-dependencies/bcftools/plugins/setGT.c

1/*  plugins/setGT.c -- set gentoypes to given values
2
3    Copyright (C) 2015-2024 Genome Research Ltd.
4
5    Author: Petr Danecek <pd3@sanger.ac.uk>
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23DEALINGS IN THE SOFTWARE.  */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <htslib/vcf.h>
28#include <htslib/vcfutils.h>
29#include <htslib/kfunc.h>
30#include <htslib/hts_os.h>
31#include <inttypes.h>
32#include <getopt.h>
33#include <ctype.h>
34#include "bcftools.h"
35#include "filter.h"
36
37// Logic of the filters: include or exclude sites which match the filters?
38#define FLT_INCLUDE 1
39#define FLT_EXCLUDE 2
40
41typedef int (*cmp_f)(double a, double b);
42
43static int cmp_eq(double a, double b) { return a==b ? 1 : 0; }
44static int cmp_le(double a, double b) { return a<=b ? 1 : 0; }
45static int cmp_ge(double a, double b) { return a>=b ? 1 : 0; }
46static int cmp_lt(double a, double b) { return a<b ? 1 : 0; }
47static int cmp_gt(double a, double b) { return a>b ? 1 : 0; }
48
49typedef struct
50{
51    bcf_hdr_t *in_hdr, *out_hdr;
52    int32_t *gts, mgts, *iarr, miarr, *xarr, mxarr;
53    int *arr, marr;
54    uint64_t nchanged;
55    int tgt_mask, new_mask, new_gt;
56    filter_t *filter;
57    char *filter_str;
58    struct {
59        int m_allele, M_allele, x_vaf_allele, *gt, *phased, ploidy;
60        char *gt_str;
61    } custom;
62    int filter_logic, rand_seed;
63    uint8_t *smpl_pass;
64    double binom_val, rand_frac;
65    char *binom_tag;
66    cmp_f binom_cmp;
67}
68args_t;
69
70args_t *args = NULL;
71
72#define GT_MISSING   1
73#define GT_PARTIAL  (1<<1)
74#define GT_REF      (1<<2)
75#define GT_MAJOR    (1<<3)
76#define GT_PHASED   (1<<4)
77#define GT_UNPHASED (1<<5)
78#define GT_ALL      (1<<6)
79#define GT_QUERY    (1<<7)
80#define GT_BINOM    (1<<8)
81#define GT_MINOR    (1<<9)
82#define GT_CUSTOM   (1<<10)
83#define GT_X_VAF    (1<<11)
84#define GT_RAND     (1<<12)
85
86#define MINOR_ALLELE -1
87#define MAJOR_ALLELE -2
88#define X_VAF_ALLELE -3
89
90const char *about(void)
91{
92    return "Set genotypes: partially missing to missing, missing to ref/major allele, etc.\n";
93}
94
95const char *usage(void)
96{
97    return
98        "About: Sets genotypes. The target genotypes can be specified as:\n"
99        "           ./.     .. completely missing (\".\" or \"./.\", depending on ploidy)\n"
100        "           ./x     .. partially missing (e.g., \"./0\" or \".|1\" but not \"./.\")\n"
101        "           .       .. partially or completely missing\n"
102        "           a       .. all genotypes\n"
103        "           b       .. heterozygous genotypes failing two-tailed binomial test (example below)\n"
104        "           q       .. select genotypes using -i/-e options\n"
105        "           r:FLOAT .. select randomly a proportion of FLOAT genotypes (can be combined with other modes)\n"
106        "       and the new genotype can be one of:\n"
107        "           .       .. missing (\".\" or \"./.\", keeps ploidy)\n"
108        "           0       .. reference allele (e.g. 0/0 or 0, keeps ploidy)\n"
109        "           c:GT    .. custom genotype (e.g. 0/0, 0, 0/1, m/M, 0/X overrides ploidy)\n"
110        "           m       .. minor (the second most common) allele as determined from INFO/AC or FMT/GT (e.g. 1/1 or 1, keeps ploidy)\n"
111        "           M       .. major allele as determined from INFO/AC or FMT/GT (e.g. 1/1 or 1, keeps ploidy)\n"
112        "           X       .. allele with bigger read depth as determined from FMT/AD\n"
113        "           p       .. phase genotype (0/1 becomes 0|1)\n"
114        "           u       .. unphase genotype and sort by allele (1|0 becomes 0/1)\n"
115        "Usage: bcftools +setGT [General Options] -- [Plugin Options]\n"
116        "Options:\n"
117        "   run \"bcftools plugin\" for a list of common options\n"
118        "\n"
119        "Plugin options:\n"
120        "   -e, --exclude EXPR        Exclude a genotype if true (requires -t q)\n"
121        "   -i, --include EXPR        include a genotype if true (requires -t q)\n"
122        "   -n, --new-gt TYPE         Genotypes to set, see above\n"
123        "   -s, --seed INT            Random seed to use with -t r [0]\n"
124        "   -t, --target-gt TYPE      Genotypes to change, see above\n"
125        "\n"
126        "Example:\n"
127        "   # set missing genotypes (\"./.\") to phased ref genotypes (\"0|0\")\n"
128        "   bcftools +setGT in.vcf -- -t . -n 0p\n"
129        "\n"
130        "   # set missing genotypes with DP>0 and GQ>20 to ref genotypes (\"0/0\")\n"
131        "   bcftools +setGT in.vcf -- -t q -n 0 -i 'GT=\".\" && FMT/DP>0 && GQ>20'\n"
132        "\n"
133        "   # set partially missing genotypes to completely missing\n"
134        "   bcftools +setGT in.vcf -- -t ./x -n .\n"
135        "\n"
136        "   # set heterozygous genotypes to 0/0 if binom.test(nAlt,nRef+nAlt,0.5)<1e-3\n"
137        "   bcftools +setGT in.vcf -- -t \"b:AD<1e-3\" -n 0\n"
138        "\n"
139        "   # force unphased heterozygous genotype if binom.test(nAlt,nRef+nAlt,0.5)>0.1\n"
140        "   bcftools +setGT in.vcf -- -t ./x -n c:'m/M'\n"
141        "\n";
142}
143
144static void _parse_binom_expr_error(char *str)
145{
146    error(
147            "Error parsing the expression: %s\n"
148            "Expected TAG CMP VAL, where\n"
149            "   TAG .. one of the format tags\n"
150            "   CMP .. operator, one of <, <=, >, >=\n"
151            "   VAL .. value\n"
152            "For example:\n"
153            "   bcftools +setGT in.vcf -- -t \"b:AD>1e-3\" -n 0\n"
154            "\n", str
155         );
156}
157void parse_binom_expr(args_t *args, char *str)
158{
159    if ( str[1]!=':' ) _parse_binom_expr_error(str);
160
161    char *beg = str+2;
162    while ( *beg && isspace(*beg) ) beg++;
163    if ( !*beg ) _parse_binom_expr_error(str);
164    char *end = beg;
165    while ( *end )
166    {
167        if ( isspace(*end) || *end=='<' || *end=='=' || *end=='>' ) break;
168        end++;
169    }
170    if ( !*end ) _parse_binom_expr_error(str);
171    args->binom_tag = (char*) calloc(1,end-beg+1);
172    memcpy(args->binom_tag,beg,end-beg);
173    int tag_id = bcf_hdr_id2int(args->in_hdr,BCF_DT_ID,args->binom_tag);
174    if ( !bcf_hdr_idinfo_exists(args->in_hdr,BCF_HL_FMT,tag_id) ) error("The FORMAT tag \"%s\" is not present in the VCF\n", args->binom_tag);
175
176    while ( *end && isspace(*end) ) end++;
177    if ( !*end ) _parse_binom_expr_error(str);
178
179    if ( !strncmp(end,"<=",2) ) { args->binom_cmp = cmp_le; beg = end+2; }
180    else if ( !strncmp(end,">=",2) ) { args->binom_cmp = cmp_ge; beg = end+2; }
181    else if ( !strncmp(end,"==",2) ) { args->binom_cmp = cmp_eq; beg = end+2; }
182    else if ( !strncmp(end,"<",1) ) { args->binom_cmp = cmp_lt; beg = end+1; }
183    else if ( !strncmp(end,">",1) ) { args->binom_cmp = cmp_gt; beg = end+1; }
184    else if ( !strncmp(end,"=",1) ) { args->binom_cmp = cmp_eq; beg = end+1; }
185    else _parse_binom_expr_error(str);
186
187    while ( *beg && isspace(*beg) ) beg++;
188    if ( !*beg ) _parse_binom_expr_error(str);
189
190    args->binom_val = strtod(beg, &end);
191    while ( *end && isspace(*end) ) end++;
192    if ( *end ) _parse_binom_expr_error(str);
193
194    args->tgt_mask |= GT_BINOM;
195    return;
196}
197
198int init(int argc, char **argv, bcf_hdr_t *in, bcf_hdr_t *out)
199{
200    args = (args_t*) calloc(1,sizeof(args_t));
201    args->in_hdr  = in;
202    args->out_hdr = out;
203
204    char *tmp;
205    int c;
206    static struct option loptions[] =
207    {
208        {"include",required_argument,NULL,'i'},
209        {"exclude",required_argument,NULL,'e'},
210        {"new-gt",required_argument,NULL,'n'},
211        {"target-gt",required_argument,NULL,'t'},
212        {"seed",required_argument,NULL,'s'},
213        {NULL,0,NULL,0}
214    };
215    while ((c = getopt_long(argc, argv, "?hn:t:i:e:s:",loptions,NULL)) >= 0)
216    {
217        switch (c)
218        {
219            case 's':
220                args->rand_seed = strtol(optarg,&tmp,10);
221                if ( *tmp ) error("Could not parse: -s %s\n",optarg);
222                break;
223            case 'e':
224                if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n");
225                args->filter_str = optarg; args->filter_logic |= FLT_EXCLUDE; break;
226            case 'i':
227                if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n");
228                args->filter_str = optarg; args->filter_logic |= FLT_INCLUDE; break;
229            case 'n': args->new_mask = 0;
230                if ( strchr(optarg,'.') ) args->new_mask |= GT_MISSING;
231                if ( strchr(optarg,'0') ) args->new_mask |= GT_REF;
232                if ( strchr(optarg,'m') ) args->new_mask |= GT_MINOR;
233                if ( strchr(optarg,'M') ) args->new_mask |= GT_MAJOR;
234                if ( strchr(optarg,'X') ) args->new_mask |= GT_X_VAF;
235                if ( strchr(optarg,'p') ) args->new_mask |= GT_PHASED;
236                if ( strchr(optarg,'u') ) args->new_mask |= GT_UNPHASED;
237                if ( !strncmp(optarg,"c:",2) ) { args->new_mask |= GT_CUSTOM; args->custom.gt_str = optarg; }
238                if ( args->new_mask==0 ) error("Unknown parameter to --new-gt: %s\n", optarg);
239                break;
240            case 't':
241                if ( !strcmp(optarg,".") ) args->tgt_mask |= GT_MISSING|GT_PARTIAL;
242                if ( !strcmp(optarg,"./x") ) args->tgt_mask |= GT_PARTIAL;
243                if ( !strcmp(optarg,"./.") ) args->tgt_mask |= GT_MISSING;
244                if ( !strcmp(optarg,"a") ) args->tgt_mask |= GT_ALL;
245                if ( !strcmp(optarg,"q") ) args->tgt_mask |= GT_QUERY;
246                if ( !strcmp(optarg,"?") ) args->tgt_mask |= GT_QUERY;        // for backward compatibility
247                if ( !strncmp(optarg,"r:",2) )
248                {
249                    args->rand_frac = strtod(optarg+2,&tmp);
250                    if ( *tmp ) error("Could not parse: -t %s\n", optarg);
251                    if ( args->rand_frac<=0 || args->rand_frac>=1 ) error("Expected value between 0 and 1 with -t\n");
252                    args->tgt_mask |= GT_RAND;
253                }
254                if ( strchr(optarg,'b') ) parse_binom_expr(args, strchr(optarg,'b'));
255                if ( args->tgt_mask==0 ) error("Unknown parameter to --target-gt: %s\n", optarg);
256                break;
257            case 'h':
258            case '?':
259            default: fprintf(stderr,"%s", usage()); exit(1); break;
260        }
261    }
262
263    if ( !args->new_mask ) error("Expected -n option\n");
264    if ( !args->tgt_mask ) error("Expected -t option\n");
265    if ( args->tgt_mask & GT_RAND )
266    {
267        if ( args->tgt_mask==GT_RAND ) args->tgt_mask |= GT_ALL;
268        hts_srand48(args->rand_seed);
269    }
270
271    if ( args->new_mask & GT_MISSING ) args->new_gt = bcf_gt_missing;
272    if ( args->new_mask & GT_REF ) args->new_gt = args->new_mask&GT_PHASED ? bcf_gt_phased(0) : bcf_gt_unphased(0);
273    if ( args->new_mask & GT_CUSTOM )
274    {
275        char *ptr = args->custom.gt_str + 2;
276        while ( *ptr )
277        {
278            int allele;
279            if ( *ptr=='m' ) { allele = MINOR_ALLELE; args->new_mask |= GT_MINOR; }
280            else if ( *ptr=='M' ) { allele = MAJOR_ALLELE; args->new_mask |= GT_MAJOR; }
281            else if ( *ptr=='X' ) { allele = X_VAF_ALLELE; args->new_mask |= GT_X_VAF; }
282            else if ( *ptr=='/' || *ptr=='|' )
283            {
284                if ( !args->custom.ploidy ) error("Could not parse the genotype: %s\n",args->custom.gt_str);
285                args->custom.phased[args->custom.ploidy-1] = *ptr=='|' ? 1 : 0;
286                ptr++;
287                continue;
288            }
289            else
290            {
291                char *end;
292                allele = strtol(ptr,&end,10);
293                if ( end==ptr || (*end && *end!='/' && *end!='|') ) error("Could not parse the genotype: %s\n",args->custom.gt_str);
294                ptr = end - 1;
295            }
296            args->custom.ploidy++;
297            args->custom.gt = realloc(args->custom.gt,sizeof(*args->custom.gt)*args->custom.ploidy);
298            args->custom.phased = realloc(args->custom.phased,sizeof(*args->custom.phased)*args->custom.ploidy);
299            args->custom.gt[args->custom.ploidy-1] = allele;
300            args->custom.phased[args->custom.ploidy-1] = 0;
301            ptr++;
302        }
303        // the phasing sign comes before the allele, move to the correct one
304        int i;
305        for (i=args->custom.ploidy-1; i>0; i--)
306            args->custom.phased[i] = args->custom.phased[i-1];
307    }
308
309    if ( args->filter_str  && !(args->tgt_mask&GT_QUERY) ) error("Expected -tq with -i/-e\n");
310    if ( !args->filter_str && args->tgt_mask&GT_QUERY ) error("Expected -i/-e with -tq\n");
311    if ( args->filter_str ) args->filter = filter_init(in,args->filter_str);
312
313    // Check the existence of FORMAT/GT tag, add it if not present
314    int id = bcf_hdr_id2int(args->in_hdr,BCF_DT_ID,"GT");
315    if ( !bcf_hdr_idinfo_exists(args->in_hdr,BCF_HL_FMT,id) )
316        bcf_hdr_printf(args->out_hdr, "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">");
317    if ( (args->new_mask & GT_X_VAF) && !bcf_hdr_idinfo_exists(args->in_hdr,BCF_HL_FMT,bcf_hdr_id2int(args->in_hdr,BCF_DT_ID,"AD")) )
318        error("Error: the FORMAT/AD annotation does exist, cannot run with --new-gt %s\n",args->custom.gt_str);
319
320    return 0;
321}
322
323// sets GT phase for a single sample, ngts is the ploidy
324static inline int phase_gt(int32_t *ptr, int ngts)
325{
326    int j, changed = 0;
327    for (j=0; j<ngts; j++)
328    {
329        if ( ptr[j]==bcf_int32_vector_end ) break;
330        if ( bcf_gt_is_phased(ptr[j]) ) continue;
331        ptr[j] = bcf_gt_phased(bcf_gt_allele(ptr[j]));    // add phasing; this may need a fix, I think the flag should be set for one allele only?
332        changed++;
333    }
334    return changed;
335}
336
337// unphase GT for a single sample, ngts is the ploidy
338static inline int unphase_gt(int32_t *ptr, int ngts)
339{
340    int j, changed = 0;
341    for (j=0; j<ngts; j++)
342    {
343        if ( ptr[j]==bcf_int32_vector_end ) break;
344        if ( !bcf_gt_is_phased(ptr[j]) ) continue;
345        ptr[j] = bcf_gt_unphased(bcf_gt_allele(ptr[j]));    // remove phasing
346        changed++;
347    }
348
349    // insertion sort
350    int k, l;
351    for (k=1; k<j; k++)
352    {
353        int32_t x = ptr[k];
354        l = k;
355        while ( l>0 && ptr[l-1]>x )
356        {
357            ptr[l] = ptr[l-1];
358            l--;
359        }
360        ptr[l] = x;
361    }
362    return changed;
363}
364
365// sets GT for a single sample, ngts is the ploidy, allele
366static inline int set_gt(int32_t *ptr, int ngts, int allele)
367{
368    int j, changed = 0;
369    for (j=0; j<ngts; j++)
370    {
371        if ( ptr[j]==bcf_int32_vector_end ) break;
372        if ( ptr[j] != allele ) changed++;
373        ptr[j] = allele;
374    }
375    return changed;
376}
377
378// sets GT for a single sample using custom.[mMX]_allele; ngts is the ploidy, nals is the number of REF+ALT alleles
379static inline int set_gt_custom(args_t *args, int32_t *ptr, int ngts, int nals)
380{
381    int i, changed = 0, new_allele = 0;
382    for (i=0; i<args->custom.ploidy; i++)
383    {
384        if ( args->custom.gt[i]==MINOR_ALLELE ) new_allele = args->custom.m_allele;
385        else if ( args->custom.gt[i]==MAJOR_ALLELE ) new_allele = args->custom.M_allele;
386        else if ( args->custom.gt[i]==X_VAF_ALLELE ) new_allele = args->custom.x_vaf_allele==bcf_gt_missing ? nals : bcf_gt_allele(args->custom.x_vaf_allele);
387        else new_allele = args->custom.gt[i];
388        if ( new_allele >= nals ) // cannot set, the requested index is bigger than there are alleles in ALT
389            new_allele = bcf_gt_missing;
390        else
391            new_allele = args->custom.phased[i] ? bcf_gt_phased(new_allele) : bcf_gt_unphased(new_allele);
392        if ( ptr[i]==new_allele ) continue;
393        ptr[i] = new_allele;
394        changed = 1;
395    }
396    while ( i<ngts )
397    {
398        if ( !changed && ptr[i]!=bcf_int32_vector_end ) changed = 1;
399        ptr[i++] = bcf_int32_vector_end;
400    }
401    return changed;
402}
403
404static inline int random_draw(args_t *args)
405{
406    return hts_drand48() > args->rand_frac ? 1 : 0; // reversed random draw
407}
408
409bcf1_t *process(bcf1_t *rec)
410{
411    if ( !rec->n_sample ) return rec;
412
413    int ngts = bcf_get_genotypes(args->in_hdr, rec, &args->gts, &args->mgts);
414    ngts /= rec->n_sample;
415    int i, j, changed = 0;
416
417    if ( args->new_mask & GT_CUSTOM )
418    {
419        if ( args->custom.ploidy > ngts ) // increased ploidy, expand the array
420        {
421            if ( args->mgts < args->custom.ploidy * rec->n_sample )
422            {
423                args->mgts = args->custom.ploidy * rec->n_sample;
424                args->gts  = (int32_t*)realloc(args->gts,args->mgts*sizeof(*args->gts));
425            }
426            for (i=0; i<rec->n_sample; i++)
427            {
428                int32_t *src = args->gts + (rec->n_sample-i-1)*ngts;
429                int32_t *dst = args->gts + (rec->n_sample-i-1)*args->custom.ploidy;
430                for (j=0; j<ngts; j++) dst[j] = src[j];
431                for (; j<args->custom.ploidy; j++) dst[j] = bcf_int32_vector_end;
432            }
433            ngts = args->custom.ploidy;
434        }
435    }
436
437    // Calculating allele frequency for each allele and determining major allele
438    // only do this if use_major is true
439    if ( args->new_mask & GT_MAJOR )
440    {
441        int maxAC = -1, majorAllele = -1;
442        hts_expand(int,rec->n_allele,args->marr,args->arr);
443        int ret = bcf_calc_ac(args->in_hdr,rec,args->arr,BCF_UN_FMT);
444        if ( ret<= 0 )
445            error("Could not calculate allele count at %s:%"PRId64"\n", bcf_seqname(args->in_hdr,rec),(int64_t) rec->pos+1);
446
447        for (i=0; i < rec->n_allele; ++i)
448        {
449            if (args->arr[i] > maxAC)
450            {
451                maxAC = args->arr[i];
452                majorAllele = i;
453            }
454        }
455
456        // replacing new_gt by major allele
457        args->new_gt = args->new_mask & GT_PHASED ?  bcf_gt_phased(majorAllele) : bcf_gt_unphased(majorAllele);
458        if ( args->new_mask & GT_CUSTOM ) args->custom.M_allele = majorAllele;
459    }
460    if ( args->new_mask & GT_MINOR )
461    {
462        hts_expand(int,rec->n_allele,args->marr,args->arr);
463        int ret = bcf_calc_ac(args->in_hdr,rec,args->arr,BCF_UN_FMT);
464        if ( ret<= 0 )
465            error("Could not calculate allele count at %s:%"PRId64"\n", bcf_seqname(args->in_hdr,rec),(int64_t) rec->pos+1);
466
467        int imax = 0;
468        for (i=1; i<rec->n_allele; i++)
469            if ( args->arr[imax] < args->arr[i] ) imax = i;
470        int imax2 = imax>0 ? 0 : (rec->n_allele>1 ? 1 : 0);
471        for (i=0; i<rec->n_allele; i++)
472            if ( i!=imax && args->arr[imax2] < args->arr[i] ) imax2 = i;
473
474        // replacing new_gt by major allele
475        args->new_gt = args->new_mask & GT_PHASED ?  bcf_gt_phased(imax2) : bcf_gt_unphased(imax2);
476        if ( args->new_mask & GT_CUSTOM ) args->custom.m_allele = imax2;
477    }
478    if ( args->new_mask & GT_X_VAF )
479    {
480        if ( bcf_get_format_int32(args->in_hdr,rec,"AD",&args->xarr,&args->mxarr)==rec->n_allele*rec->n_sample )
481        {
482            int32_t *src = args->xarr, *dst = args->xarr;
483            for (i=0; i<rec->n_sample; i++)
484            {
485                int jmax = -1;
486                for (j=0; j<rec->n_allele; j++)
487                {
488                    if ( src[j]==bcf_int32_vector_end ) break;
489                    if ( src[j]==bcf_int32_missing  ) continue;
490                    if ( j==0 || src[jmax] < src[j] ) jmax = j;
491                }
492                *dst = jmax==-1 ? bcf_gt_missing : bcf_gt_unphased(jmax);
493                src += rec->n_allele;
494                dst++;
495            }
496        }
497        else return rec;
498    }
499
500    int nbinom = 0;
501    if ( args->tgt_mask & GT_BINOM )
502    {
503        nbinom = bcf_get_format_int32(args->in_hdr, rec, args->binom_tag, &args->iarr, &args->miarr);
504        if ( nbinom<0 ) nbinom = 0;
505        nbinom /= rec->n_sample;
506    }
507
508    // replace gts
509    if ( nbinom && ngts>=2 )    // only diploid genotypes are considered: higher ploidy ignored further, haploid here
510    {
511        if ( args->filter ) filter_test(args->filter,rec,(const uint8_t **)&args->smpl_pass);
512        for (i=0; i<rec->n_sample; i++)
513        {
514            if ( args->smpl_pass )
515            {
516                if ( !args->smpl_pass[i] && args->filter_logic==FLT_INCLUDE ) continue;
517                if (  args->smpl_pass[i] && args->filter_logic==FLT_EXCLUDE ) continue;
518            }
519            int32_t *ptr = args->gts + i*ngts;
520            if ( bcf_gt_is_missing(ptr[0]) || bcf_gt_is_missing(ptr[1]) || ptr[1]==bcf_int32_vector_end ) continue;
521            if ( ptr[0]==ptr[1] ) continue; // a hom
522            int ia = bcf_gt_allele(ptr[0]);
523            int ib = bcf_gt_allele(ptr[1]);
524            if ( ia>=nbinom || ib>=nbinom )
525                error("The sample %s has incorrect number of %s fields at %s:%"PRId64"\n",
526                        args->in_hdr->samples[i],args->binom_tag,bcf_seqname(args->in_hdr,rec),(int64_t) rec->pos+1);
527
528            double prob = calc_binom_two_sided(args->iarr[i*nbinom+ia],args->iarr[i*nbinom+ib],0.5);
529            if ( !args->binom_cmp(prob,args->binom_val) ) continue;
530            if ( args->tgt_mask&GT_RAND && random_draw(args) ) continue;
531
532            if ( args->new_mask&GT_UNPHASED )
533                changed += unphase_gt(ptr, ngts);
534            else if ( args->new_mask==GT_PHASED )
535                changed += phase_gt(ptr, ngts);
536            else if ( args->new_mask & GT_CUSTOM )
537            {
538                if ( args->new_mask & GT_X_VAF ) args->custom.x_vaf_allele = args->xarr[i];
539                changed += set_gt_custom(args, ptr, ngts, rec->n_allele);
540            }
541            else if ( args->new_mask & GT_X_VAF )
542                changed += set_gt(ptr, ngts, args->xarr[i]);
543            else
544                changed += set_gt(ptr, ngts, args->new_gt);
545        }
546    }
547    else if ( args->tgt_mask&GT_QUERY )
548    {
549        int pass_site = filter_test(args->filter,rec,(const uint8_t **)&args->smpl_pass);
550        if ( args->filter_logic==FLT_EXCLUDE )
551        {
552            // -i can include a site but exclude a sample, -e exclude a site but include a sample
553            if ( pass_site )
554            {
555                if ( !args->smpl_pass ) return rec;
556                pass_site = 0;
557                for (i=0; i<rec->n_sample; i++)
558                {
559                    if ( args->smpl_pass[i] ) args->smpl_pass[i] = 0;
560                    else { args->smpl_pass[i] = 1; pass_site = 1; }
561                }
562                if ( !pass_site ) return rec;
563            }
564            else if ( args->smpl_pass )
565                for (i=0; i<rec->n_sample; i++) args->smpl_pass[i] = 1;
566        }
567        else if ( !pass_site ) return rec;
568
569        for (i=0; i<rec->n_sample; i++)
570        {
571            if ( args->smpl_pass && !args->smpl_pass[i] ) continue;
572            if ( args->tgt_mask&GT_RAND && random_draw(args) ) continue;
573            if ( args->new_mask&GT_UNPHASED )
574                changed += unphase_gt(args->gts + i*ngts, ngts);
575            else if ( args->new_mask==GT_PHASED )
576                changed += phase_gt(args->gts + i*ngts, ngts);
577            else if ( args->new_mask & GT_CUSTOM )
578            {
579                if ( args->new_mask & GT_X_VAF ) args->custom.x_vaf_allele = args->xarr[i];
580                changed += set_gt_custom(args, args->gts + i*ngts, ngts, rec->n_allele);
581            }
582            else if ( args->new_mask & GT_X_VAF )
583                changed += set_gt(args->gts + i*ngts, ngts, args->xarr[i]);
584            else
585                changed += set_gt(args->gts + i*ngts, ngts, args->new_gt);
586        }
587    }
588    else
589    {
590        for (i=0; i<rec->n_sample; i++)
591        {
592            int ploidy = 0, nmiss = 0;
593            int32_t *ptr = args->gts + i*ngts;
594            for (j=0; j<ngts; j++)
595            {
596                if ( ptr[j]==bcf_int32_vector_end ) break;
597                ploidy++;
598                if ( bcf_gt_is_missing(ptr[j]) ) nmiss++;
599            }
600
601            int do_set = 0;
602            if ( args->tgt_mask&GT_ALL ) do_set = 1;
603            else if ( args->tgt_mask&GT_PARTIAL && nmiss ) do_set = 1;
604            else if ( args->tgt_mask&GT_MISSING && ploidy==nmiss ) do_set = 1;
605
606            if ( !do_set ) continue;
607            if ( args->tgt_mask&GT_RAND && random_draw(args) ) continue;
608
609            if ( args->new_mask&GT_UNPHASED )
610                changed += unphase_gt(ptr, ngts);
611            else if ( args->new_mask==GT_PHASED )
612                changed += phase_gt(ptr, ngts);
613            else if ( args->new_mask & GT_CUSTOM )
614            {
615                if ( args->new_mask & GT_X_VAF ) args->custom.x_vaf_allele = args->xarr[i];
616                changed += set_gt_custom(args, args->gts + i*ngts, ngts, rec->n_allele);
617            }
618            else if ( args->new_mask & GT_X_VAF )
619                changed += set_gt(ptr, ngts, args->xarr[i]);
620            else
621                changed += set_gt(ptr, ngts, args->new_gt);
622        }
623    }
624    args->nchanged += changed;
625    if ( changed )
626    {
627        int ret = bcf_update_genotypes(args->out_hdr, rec, args->gts, ngts*rec->n_sample);
628        if ( ret!=0 ) error("Error: failed to update genotypes at %s:%"PRIhts_pos"\n",bcf_seqname(args->in_hdr,rec),rec->pos+1);
629    }
630    return rec;
631}
632
633void destroy(void)
634{
635    fprintf(stderr,"Filled %"PRId64" alleles\n", args->nchanged);
636    free(args->custom.gt);
637    free(args->custom.phased);
638    free(args->binom_tag);
639    if ( args->filter ) filter_destroy(args->filter);
640    free(args->arr);
641    free(args->iarr);
642    free(args->xarr);
643    free(args->gts);
644    free(args);
645}
646
647