Debian Codegraph @

bcftools/bcftools.h

raw symbols SCIP
1 /* bcftools.h -- utility function declarations.
2
3 Copyright (C) 2013-2024 Genome Research Ltd.
4
5 Author: Petr Danecek <[email protected]>
6
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:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
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 THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE. */
24
25 #ifndef BCFTOOLS_H
26 #define BCFTOOLS_H
27
28 #include <stdarg.h>
29 #include <htslib/hts_defs.h>
30 #include <htslib/vcf.h>
31 #include <htslib/synced_bcf_reader.h>
32 #include <htslib/kfunc.h>
33 #include <math.h>
34
35 #define FT_TAB_TEXT 0 // custom tab-delimited text file
36 #define FT_GZ 1
37 #define FT_VCF 2
38 #define FT_VCF_GZ (FT_GZ|FT_VCF)
39 #define FT_BCF (1<<2)
40 #define FT_BCF_GZ (FT_GZ|FT_BCF)
41 #define FT_STDIN (1<<3)
42
43 char *bcftools_version(void);
44
45 /// Report an error and exit -1
46 void error(const char *format, ...) HTS_NORETURN HTS_FORMAT(HTS_PRINTF_FMT, 1, 2);
47
48 /// Report an error and exit -1. If errno != 0, appends strerror(errno).
49 // Note: unlike error() above, the message should not end with "\n" as a
50 // newline will be added by the function.
51 void error_errno(const char *format, ...) HTS_NORETURN HTS_FORMAT(HTS_PRINTF_FMT, 1, 2);
52
53 // For on the fly index creation with --write-index
54 int init_index2(htsFile *fh, bcf_hdr_t *hdr, const char *fname, char **idx_fname, int idx_fmt);
55 int init_index(htsFile *fh, bcf_hdr_t *hdr, const char *fname, char **idx_fname);
56
57 // Used to set args->write_index in CLI.
58 // It will be true if set correctly.
59 // Note due to HTS_FMT_CSI being zero we have to use an additional bit.
60 int write_index_parse(char *arg);
61
62 void bcf_hdr_append_version(bcf_hdr_t *hdr, int argc, char **argv, const char *cmd);
63 const char *hts_bcf_wmode(int file_type);
64 const char *hts_bcf_wmode2(int file_type, const char *fname);
65 void set_wmode(char dst[8], int file_type, const char *fname, int compression_level); // clevel: 0-9 with or zb type, -1 unset
66 char *init_tmp_prefix(const char *prefix);
67 int read_AF(bcf_sr_regions_t *tgt, bcf1_t *line, double *alt_freq);
68 int parse_overlap_option(const char *arg);
69
70 // Default sort order: chr,pos,alleles
71 int cmp_bcf_pos(const void *aptr, const void *bptr);
72 int cmp_bcf_pos_ref_alt(const void *aptr, const void *bptr);
73
74 static inline int iupac2bitmask(char iupac)
75 {
76 const int A = 1;
77 const int C = 2;
78 const int G = 4;
79 const int T = 8;
80 if ( iupac >= 97 ) iupac -= 32;
81 if ( iupac == 'A' ) return A;
82 if ( iupac == 'C' ) return C;
83 if ( iupac == 'G' ) return G;
84 if ( iupac == 'T' ) return T;
85 if ( iupac == 'M' ) return A|C;
86 if ( iupac == 'R' ) return A|G;
87 if ( iupac == 'W' ) return A|T;
88 if ( iupac == 'S' ) return C|G;
89 if ( iupac == 'Y' ) return C|T;
90 if ( iupac == 'K' ) return G|T;
91 if ( iupac == 'V' ) return A|C|G;
92 if ( iupac == 'H' ) return A|C|T;
93 if ( iupac == 'D' ) return A|G|T;
94 if ( iupac == 'B' ) return C|G|T;
95 if ( iupac == 'N' ) return A|C|G|T;
96 return -1;
97 }
98 static inline char bitmask2iupac(int bitmask)
99 {
100 const char iupac[16] = {'.','A','C','M','G','R','S','V','T','W','Y','H','K','D','B','N'};
101 if ( bitmask <= 0 || bitmask > 15 ) return 0;
102 return iupac[bitmask];
103 }
104
105 static inline int iupac_consistent(char iupac, char nt)
106 {
107 static const char iupac_mask[90] = {
108 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
109 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
110 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,14,2,
111 13,0,0,4,11,0,0,12,0,3,15,0,0,0,5,6,8,0,7,9,0,10
112 };
113 if ( iupac > 89 ) return 0;
114 if ( nt > 90 ) nt -= 32; // lowercase
115 if ( nt=='A' ) nt = 1;
116 else if ( nt=='C' ) nt = 2;
117 else if ( nt=='G' ) nt = 4;
118 else if ( nt=='T' ) nt = 8;
119 return iupac_mask[(int)iupac] & nt ? 1 : 0;
120 }
121
122 static inline char nt_to_upper(char nt)
123 {
124 if ( nt < 97 ) return nt;
125 return nt - 32;
126 }
127
128 static inline double phred_score(double prob)
129 {
130 if ( prob==0 ) return 99;
131 prob = -4.3429*log(prob);
132 return prob>99 ? 99 : prob;
133 }
134
135 static inline double calc_binom_two_sided(int na, int nb, double aprob)
136 {
137 if ( !na && !nb ) return -1;
138 if ( na==nb ) return 1;
139
140 // kfunc.h implements kf_betai, which is the regularized beta function P(X<=k/N;p) = I_{1-p}(N-k,k+1)
141
142 double prob = na > nb ? 2 * kf_betai(na, nb+1, aprob) : 2 * kf_betai(nb, na+1, aprob);
143
144 if ( prob > 1 ) prob = 1; // this can happen, machine precision error, eg. kf_betai(1,0,0.5)
145 return prob;
146 }
147 static inline double calc_binom_one_sided(int na, int nb, double aprob, int ge)
148 {
149 return ge ? kf_betai(na, nb + 1, aprob) : kf_betai(nb, na + 1, 1 - aprob);
150 }
151
152 static const uint64_t bcf_double_missing = 0x7ff0000000000001;
153 static const uint64_t bcf_double_vector_end = 0x7ff0000000000002;
154 static inline void bcf_double_set(double *ptr, uint64_t value)
155 {
156 union { uint64_t i; double d; } u;
157 u.i = value;
158 *ptr = u.d;
159 }
160 static inline int bcf_double_test(double d, uint64_t value)
161 {
162 union { uint64_t i; double d; } u;
163 u.d = d;
164 return u.i==value ? 1 : 0;
165 }
166 #define bcf_double_set_vector_end(x) bcf_double_set(&(x),bcf_double_vector_end)
167 #define bcf_double_set_missing(x) bcf_double_set(&(x),bcf_double_missing)
168 #define bcf_double_is_vector_end(x) bcf_double_test((x),bcf_double_vector_end)
169 #define bcf_double_is_missing(x) bcf_double_test((x),bcf_double_missing)
170 #define bcf_double_is_missing_or_vector_end(x) (bcf_double_test((x),bcf_double_missing) || bcf_double_test((x),bcf_double_vector_end))
171
172 static inline int get_unseen_allele(bcf1_t *line)
173 {
174 int i;
175 for (i=1; i<line->n_allele; i++)
176 {
177 if ( !strcmp(line->d.allele[i],"<*>") ) return i;
178 if ( !strcmp(line->d.allele[i],"<NON_REF>") ) return i;
179 if ( !strcmp(line->d.allele[i],"<X>") ) return i;
180 }
181 return 0;
182 }
183
184 #endif