modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / htslib / test / test-vcf-sweep.c
blob44c2fb1405f6b2440288488c3f46f4d1d57a7bd9
1 /* test/test-vcf-sweep.c -- VCF test harness.
3 Copyright (C) 2013 Genome Research Ltd.
5 Author: Petr Danecek <pd3@sanger.ac.uk>
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE. */
25 #include <config.h>
27 #include <stdio.h>
28 #include <htslib/vcf_sweep.h>
30 int main(int argc, char **argv)
32 if ( argc!=2 )
34 fprintf(stderr,"Usage: test-vcf-sweep <file.bcf|file.vcf>\n");
35 return 1;
38 // Init variables. The checksum is just for this test program to output
39 // something and verify that all sites are read in both passes - fwd and
40 // bwd.
41 bcf_sweep_t *sw = bcf_sweep_init(argv[1]);
42 bcf_hdr_t *hdr = bcf_sweep_hdr(sw);
43 int chksum = 0;
45 // First we must sweep forward and read the whole file to build an index.
46 // If this is undesirable, we can require the presence of a .gzi index
47 // which can be created with `bgzip -r` from the samtools/htslib package
48 bcf1_t *rec;
49 while ( (rec = bcf_sweep_fwd(sw)) ) chksum += rec->pos+1;
50 printf("fwd position chksum: %d\n", chksum);
52 // Now sweep backward.
53 chksum = 0;
54 while ( (rec = bcf_sweep_bwd(sw)) ) chksum += rec->pos+1;
55 printf("bwd position chksum: %d\n", chksum);
57 // And forward and backward again, this time summing the PL vectors
58 int i,j, mPLs = 0, nPLs;
59 int32_t *PLs = NULL;
60 chksum = 0;
61 while ( (rec = bcf_sweep_fwd(sw)) )
63 // get copy of the PL vectors
64 nPLs = bcf_get_format_int32(hdr, rec, "PL", &PLs, &mPLs);
65 if ( !nPLs ) continue; // PL not present
67 // how many values are there per sample
68 int nvals = nPLs / bcf_hdr_nsamples(hdr);
70 int32_t *ptr = PLs;
71 for (i=0; i<bcf_hdr_nsamples(hdr); i++)
73 for (j=0; j<nvals; j++)
75 // check for shorter vectors (haploid genotypes amongst diploids)
76 if ( ptr[j]==bcf_int32_vector_end ) break;
78 // skip missing values
79 if ( ptr[j]==bcf_int32_missing ) continue;
81 chksum += ptr[j];
83 ptr += nvals;
86 printf("fwd PL chksum: %d\n", chksum);
88 // And the same backwards..
89 chksum = 0;
90 while ( (rec = bcf_sweep_bwd(sw)) )
92 nPLs = bcf_get_format_int32(hdr, rec, "PL", &PLs, &mPLs);
93 if ( !nPLs ) continue;
94 int nvals = nPLs / bcf_hdr_nsamples(hdr);
95 int32_t *ptr = PLs;
96 for (i=0; i<bcf_hdr_nsamples(hdr); i++)
98 for (j=0; j<nvals; j++)
100 if ( ptr[j]==bcf_int32_vector_end ) break;
101 if ( ptr[j]==bcf_int32_missing ) continue;
102 chksum += ptr[j];
104 ptr += nvals;
107 printf("bwd PL chksum: %d\n", chksum);
109 // Clean up
110 bcf_sweep_destroy(sw);
111 return 0;