Source code

Download source archive ↓

upstream-dependencies/bcftools/plugins/missing2ref.c

1/*  plugins/missing2ref.c -- sets missing genotypes to reference allele.
2
3    Copyright (C) 2014-2021 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 <inttypes.h>
30#include <getopt.h>
31
32bcf_hdr_t *in_hdr, *out_hdr;
33int32_t *gts = NULL, mgts = 0;
34int *arr = NULL, marr = 0;
35uint64_t nchanged = 0;
36int new_gt = bcf_gt_unphased(0);
37int use_major = 0;
38
39const char *about(void)
40{
41    return "Set missing genotypes (\"./.\") to ref or major allele (\"0/0\" or \"0|0\").\n";
42}
43
44const char *usage(void)
45{
46    return 
47        "\n"
48        "About: Set missing genotypes\n"
49        "Usage: bcftools +missing2ref [General Options] -- [Plugin Options]\n"
50        "Options:\n"
51        "   run \"bcftools plugin\" for a list of common options\n"
52        "\n"
53        "Plugin options:\n"
54        "   -p, --phased       Set to \"0|0\" \n"
55        "   -m, --major        Set to major allele \n"
56        "\n"
57        "Example:\n"
58        "   bcftools +missing2ref in.vcf -- -p\n"
59        "   bcftools +missing2ref in.vcf -- -p -m\n"
60        "\n";
61}
62
63
64int init(int argc, char **argv, bcf_hdr_t *in, bcf_hdr_t *out)
65{
66    int c;
67    static struct option loptions[] =
68    {
69        {"phased",0,0,'p'},
70        {"major",0,0,'m'},
71        {0,0,0,0}
72    };
73    while ((c = getopt_long(argc, argv, "mp?h",loptions,NULL)) >= 0)
74    {
75        switch (c) 
76        {
77            case 'p': new_gt = bcf_gt_phased(0); break;
78            case 'm': use_major = 1; break;
79            case 'h':
80            case '?':
81            default: fprintf(stderr,"%s", usage()); exit(1); break;
82        }
83    }
84    in_hdr  = in;
85    out_hdr = out;
86    return 0;
87}
88
89bcf1_t *process(bcf1_t *rec)
90{
91    int ngts = bcf_get_genotypes(in_hdr, rec, &gts, &mgts);
92    int i, changed = 0;
93    
94    // Calculating allele frequency for each allele and determining major allele
95    // only do this if use_major is true
96    int majorAllele = -1;
97    int maxAC = -1;
98    if(use_major == 1){
99        hts_expand(int,rec->n_allele,marr,arr);
100        int ret = bcf_calc_ac(in_hdr,rec,arr,BCF_UN_FMT);
101        if(ret > 0){
102            for(i=0; i < rec->n_allele; ++i){
103                if(*(arr+i) > maxAC){
104                    maxAC = *(arr+i);
105                    majorAllele = i;
106                }
107            }
108        }
109        else{
110            fprintf(stderr,"Warning: Could not calculate allele count at position %"PRId64"\n", (int64_t) rec->pos);
111            exit(1);
112        }
113
114        // replacing new_gt by major allele
115        if(bcf_gt_is_phased(new_gt))
116            new_gt = bcf_gt_phased(majorAllele);
117        else
118            new_gt = bcf_gt_unphased(majorAllele);
119    }
120
121    // replace gts
122    for (i=0; i<ngts; i++)
123    {
124        if ( gts[i]==bcf_gt_missing )
125        {
126            gts[i] = new_gt;
127            changed++;
128        }
129    }
130    nchanged += changed;
131    if ( changed ) bcf_update_genotypes(out_hdr, rec, gts, ngts);
132    return rec;
133}
134
135void destroy(void)
136{
137    free(arr);
138    fprintf(stderr,"Filled %"PRId64" REF alleles\n", nchanged);
139    free(gts);
140}
141
142