File: trio-switch-rate.c.pysam.c

package info (click to toggle)
python-pysam 0.15.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,604 kB
  • sloc: ansic: 125,787; python: 7,782; sh: 284; makefile: 222; perl: 41
file content (275 lines) | stat: -rw-r--r-- 9,273 bytes parent folder | download | duplicates (2)
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "bcftools.pysam.h"

/* The MIT License

   Copyright (c) 2016 Genome Research Ltd.

   Author: Petr Danecek <pd3@sanger.ac.uk>
   
   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.

 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <math.h>
#include <htslib/hts.h>
#include <htslib/vcf.h>
#include <htslib/kstring.h>
#include <htslib/kseq.h>
#include <htslib/khash_str2int.h>
#include "bcftools.h"

typedef struct
{
    int father, mother, child;      // VCF sample index
    int prev, ipop;
    uint32_t err, nswitch, ntest;
}
trio_t;

typedef struct
{
    char *name;
    uint32_t err, nswitch, ntest, ntrio;
    float pswitch;
}
pop_t;

typedef struct
{
    int argc;
    char **argv;
    bcf_hdr_t *hdr;
    trio_t *trio;
    int ntrio, mtrio;
    int32_t *gt_arr;
    int npop;
    pop_t *pop;
    int mgt_arr, prev_rid;
}
args_t;

args_t args;

const char *about(void)
{
    return "Calculate phase switch rate in trio samples, children samples must have phased GTs.\n";
}

const char *usage(void)
{
    return 
        "\n"
        "About: Calculate phase switch rate in trio children.\n"
        "Usage: bcftools +trio-swich-rate [General Options] -- [Plugin Options]\n"
        "Options:\n"
        "   run \"bcftools plugin\" for a list of common options\n"
        "\n"
        "Plugin options:\n"
        "   -p, --ped <file>        PED file with optional 7th column to group\n"
        "                           results by population\n"
        "\n"
        "Example:\n"
        "   bcftools +trio-switch-rate file.bcf -- -p file.ped\n"
        "\n";
}

void parse_ped(args_t *args, char *fname)
{
    htsFile *fp = hts_open(fname, "r");
    if ( !fp ) error("Could not read: %s\n", fname);

    kstring_t str = {0,0,0};
    if ( hts_getline(fp, KS_SEP_LINE, &str) <= 0 ) error("Empty file: %s\n", fname);

    void *pop2i = khash_str2int_init();

    int moff = 0, *off = NULL;
    do
    {
        // familyID    sampleID paternalID maternalID sex   phenotype   population relationship   siblings   secondOrder   thirdOrder   children    comment
        // BB03    HG01884 HG01885 HG01956 2   0   ACB child   0   0   0   0
        int ncols = ksplit_core(str.s,0,&moff,&off);
        if ( ncols<4 ) error("Could not parse the ped file: %s\n", str.s);

        int father = bcf_hdr_id2int(args->hdr,BCF_DT_SAMPLE,&str.s[off[2]]);
        if ( father<0 ) continue;
        int mother = bcf_hdr_id2int(args->hdr,BCF_DT_SAMPLE,&str.s[off[3]]);
        if ( mother<0 ) continue;
        int child = bcf_hdr_id2int(args->hdr,BCF_DT_SAMPLE,&str.s[off[1]]);
        if ( child<0 ) continue;

        args->ntrio++;
        hts_expand0(trio_t,args->ntrio,args->mtrio,args->trio);
        trio_t *trio = &args->trio[args->ntrio-1];
        trio->father = father;
        trio->mother = mother;
        trio->child  = child;

        if (ncols>6) {
            char *pop_name = &str.s[off[6]];
            if ( !khash_str2int_has_key(pop2i,pop_name) )
            {
                pop_name = strdup(&str.s[off[6]]);
                khash_str2int_set(pop2i,pop_name,args->npop);
                args->npop++;
                args->pop = (pop_t*) realloc(args->pop,args->npop*sizeof(*args->pop));
                memset(args->pop+args->npop-1,0,sizeof(*args->pop));
                args->pop[args->npop-1].name = pop_name;
            }
            khash_str2int_get(pop2i,pop_name,&trio->ipop);
            args->pop[trio->ipop].ntrio++;
        }
    } while ( hts_getline(fp, KS_SEP_LINE, &str)>=0 );

    khash_str2int_destroy(pop2i);
    free(str.s);
    free(off);
    hts_close(fp);
}

int init(int argc, char **argv, bcf_hdr_t *in, bcf_hdr_t *out)
{
    memset(&args,0,sizeof(args_t));
    args.argc   = argc; args.argv = argv;
    args.prev_rid = -1;
    args.hdr = in;
    char *ped_fname = NULL;
    static struct option loptions[] =
    {
        {"ped",required_argument,NULL,'p'},
        {0,0,0,0}
    };
    int c;
    while ((c = getopt_long(argc, argv, "?hp:",loptions,NULL)) >= 0)
    {
        switch (c) 
        {
            case 'p': ped_fname = optarg; break;
            case 'h':
            case '?':
            default: error("%s", usage()); break;
        }
    }
    if ( !ped_fname ) error("Expected the -p option\n");
    parse_ped(&args, ped_fname);
    return 1;
}

typedef struct
{
    int a, b, phased;
}
gt_t;

int parse_genotype(gt_t *gt, int32_t *ptr);

inline int parse_genotype(gt_t *gt, int32_t *ptr)
{
    if ( ptr[0]==bcf_gt_missing ) return 0;
    if ( ptr[1]==bcf_gt_missing ) return 0;
    if ( ptr[1]==bcf_int32_vector_end ) return 0;
    gt->phased = bcf_gt_is_phased(ptr[1]) ? 1 : 0;
    gt->a = bcf_gt_allele(ptr[0]); if ( gt->a > 1 ) return 0;  // consider only the first two alleles at biallelic sites
    gt->b = bcf_gt_allele(ptr[1]); if ( gt->b > 1 ) return 0;
    return 1;
}

bcf1_t *process(bcf1_t *rec)
{
    int ngt = bcf_get_genotypes(args.hdr, rec, &args.gt_arr, &args.mgt_arr);
    if ( ngt<0 ) return NULL;
    ngt /= bcf_hdr_nsamples(args.hdr);
    if ( ngt!=2 ) return NULL;

    int i;
    if ( rec->rid!=args.prev_rid )
    {
        args.prev_rid = rec->rid;
        for (i=0; i<args.ntrio; i++) args.trio[i].prev = 0;
    }

    gt_t child, father, mother;
    for (i=0; i<args.ntrio; i++)
    {
        trio_t *trio = &args.trio[i];

        if ( !parse_genotype(&child, args.gt_arr + ngt*trio->child) ) continue;
        if ( !child.phased ) continue;
        if ( child.a+child.b != 1 ) continue;       // child is not a het

        if ( !parse_genotype(&father, args.gt_arr + ngt*trio->father) ) continue;
        if ( !parse_genotype(&mother, args.gt_arr + ngt*trio->mother) ) continue;
        if ( father.a+father.b == 1 && mother.a+mother.b == 1 ) continue;     // both parents are hets
        if ( father.a+father.b == mother.a+mother.b ) { trio->err++; continue; }    // mendelian error

        int test_phase = 0; 
        if ( father.a==father.b ) test_phase = 1 + (child.a==father.a);
        else if ( mother.a==mother.b ) test_phase = 1 + (child.b==mother.a);
        if ( trio->prev > 0 )
        {
            if ( trio->prev!=test_phase ) trio->nswitch++;
        }
        trio->ntest++;
        trio->prev = test_phase;
    }
    return NULL;
}

void destroy(void)
{
    int i;
    fprintf(bcftools_stdout, "# This file was produced by: bcftools +trio-switch-rate(%s+htslib-%s)\n", bcftools_version(),hts_version());
    fprintf(bcftools_stdout, "# The command line was:\tbcftools +trio-switch-rate %s", args.argv[0]);
    for (i=1; i<args.argc; i++) fprintf(bcftools_stdout, " %s",args.argv[i]);
    fprintf(bcftools_stdout, "\n#\n");
    fprintf(bcftools_stdout, "# TRIO\t[2]Father\t[3]Mother\t[4]Child\t[5]nTested\t[6]nMendelian Errors\t[7]nSwitch\t[8]nSwitch (%%)\n");
    for (i=0; i<args.ntrio; i++)
    {
        trio_t *trio = &args.trio[i];
        fprintf(bcftools_stdout, "TRIO\t%s\t%s\t%s\t%d\t%d\t%d\t%.2f\n",
            bcf_hdr_int2id(args.hdr,BCF_DT_SAMPLE,trio->father),
            bcf_hdr_int2id(args.hdr,BCF_DT_SAMPLE,trio->mother),
            bcf_hdr_int2id(args.hdr,BCF_DT_SAMPLE,trio->child),
            trio->ntest, trio->err, trio->nswitch, trio->ntest ? trio->nswitch*100./trio->ntest : 0
        );
        if (args.npop) {
            pop_t *pop = &args.pop[trio->ipop];
            pop->err     += trio->err;
            pop->nswitch += trio->nswitch;
            pop->ntest   += trio->ntest;
            pop->pswitch += trio->ntest ? trio->nswitch*100./trio->ntest : 0;
        }
    }
    fprintf(bcftools_stdout, "# POP\tpopulation or other grouping defined by an optional 7-th column of the PED file\n");
    fprintf(bcftools_stdout, "# POP\t[2]Name\t[3]Number of trios\t[4]avgTested\t[5]avgMendelian Errors\t[6]avgSwitch\t[7]avgSwitch (%%)\n");
    for (i=0; i<args.npop; i++)
    {
        pop_t *pop = &args.pop[i];
        fprintf(bcftools_stdout, "POP\t%s\t%d\t%.0f\t%.0f\t%.0f\t%.2f\n", pop->name,pop->ntrio,
            (float)pop->ntest/pop->ntrio,(float)pop->err/pop->ntrio,(float)pop->nswitch/pop->ntrio,
            pop->pswitch/pop->ntrio);
    }
    for (i=0; i<args.npop; i++) free(args.pop[i].name);
    free(args.pop);
    free(args.trio);
    free(args.gt_arr);
}