annotate PsiCLASS-1.0.2/samtools-0.1.19/bcftools/bcf.h @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 /* The MIT License
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 Copyright (c) 2010 Broad Institute
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 a copy of this software and associated documentation files (the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 "Software"), to deal in the Software without restriction, including
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 without limitation the rights to use, copy, modify, merge, publish,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 distribute, sublicense, and/or sell copies of the Software, and to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 permit persons to whom the Software is furnished to do so, subject to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 the following conditions:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 The above copyright notice and this permission notice shall be
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 included in all copies or substantial portions of the Software.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 SOFTWARE.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 /* Contact: Heng Li <lh3@live.co.uk> */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 #ifndef BCF_H
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 #define BCF_H
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 #define BCF_VERSION "0.1.19-44428cd"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 #include <stdint.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 #include <zlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 #ifndef BCF_LITE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 #include "bgzf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 typedef BGZF *bcfFile;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 typedef gzFile bcfFile;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 #define bgzf_open(fn, mode) gzopen(fn, mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 #define bgzf_fdopen(fd, mode) gzdopen(fd, mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 #define bgzf_close(fp) gzclose(fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 #define bgzf_read(fp, buf, len) gzread(fp, buf, len)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 #define bgzf_write(fp, buf, len)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 #define bgzf_flush(fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 A member in the structs below is said to "primary" if its content
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 cannot be inferred from other members in any of structs below; a
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 member is said to be "derived" if its content can be derived from
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 other members. For example, bcf1_t::str is primary as this comes from
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 the input data, while bcf1_t::info is derived as it can always be
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 correctly set if we know bcf1_t::str. Derived members are for quick
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 access to the content and must be synchronized with the primary data.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 uint32_t fmt; // format of the block, set by bcf_str2int().
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 int len; // length of data for each individual
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 void *data; // concatenated data
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 // derived info: fmt, len (<-bcf1_t::fmt)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 } bcf_ginfo_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 int32_t tid, pos; // refID and 0-based position
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 int32_t l_str, m_str; // length and the allocated size of ->str
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 float qual; // SNP quality
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 char *str; // concatenated string of variable length strings in VCF (from col.2 to col.7)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 char *ref, *alt, *flt, *info, *fmt; // they all point to ->str; no memory allocation
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 int n_gi, m_gi; // number and the allocated size of geno fields
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 bcf_ginfo_t *gi; // array of geno fields
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 int n_alleles, n_smpl; // number of alleles and samples
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 // derived info: ref, alt, flt, info, fmt (<-str), n_gi (<-fmt), n_alleles (<-alt), n_smpl (<-bcf_hdr_t::n_smpl)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 uint8_t *ploidy; // ploidy of all samples; if NULL, ploidy of 2 is assumed.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 } bcf1_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 int32_t n_ref, n_smpl; // number of reference sequences and samples
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 int32_t l_nm; // length of concatenated sequence names; 0 padded
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 int32_t l_smpl; // length of concatenated sample names; 0 padded
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 int32_t l_txt; // length of header text (lines started with ##)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 char *name, *sname, *txt; // concatenated sequence names, sample names and header text
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 char **ns, **sns; // array of sequence and sample names; point to name and sname, respectively
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 // derived info: n_ref (<-name), n_smpl (<-sname), ns (<-name), sns (<-sname)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 } bcf_hdr_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 int is_vcf; // if the file in operation is a VCF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 void *v; // auxillary data structure for VCF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 bcfFile fp; // file handler for BCF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 } bcf_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 struct __bcf_idx_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 typedef struct __bcf_idx_t bcf_idx_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 #ifdef __cplusplus
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 extern "C" {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 // open a BCF file; for BCF file only
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 bcf_t *bcf_open(const char *fn, const char *mode);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 // close file
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 int bcf_close(bcf_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 // read one record from BCF; return -1 on end-of-file, and <-1 for errors
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 int bcf_read(bcf_t *bp, const bcf_hdr_t *h, bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 // call this function if b->str is changed
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 int bcf_sync(bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 // write a BCF record
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 int bcf_write(bcf_t *bp, const bcf_hdr_t *h, const bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 // read the BCF header; BCF only
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 bcf_hdr_t *bcf_hdr_read(bcf_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 // write the BCF header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 int bcf_hdr_write(bcf_t *b, const bcf_hdr_t *h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 // set bcf_hdr_t::ns and bcf_hdr_t::sns
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 int bcf_hdr_sync(bcf_hdr_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 // destroy the header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 void bcf_hdr_destroy(bcf_hdr_t *h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 // destroy a record
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 int bcf_destroy(bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 // BCF->VCF conversion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 char *bcf_fmt(const bcf_hdr_t *h, bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 // append more info
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 int bcf_append_info(bcf1_t *b, const char *info, int l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 // remove tag
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 int remove_tag(char *string, const char *tag, char delim);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 // remove info tag, string is the kstring holder of bcf1_t.str
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 void rm_info(kstring_t *string, const char *key);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 // copy
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 int bcf_cpy(bcf1_t *r, const bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 // open a VCF or BCF file if "b" is set in "mode"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 bcf_t *vcf_open(const char *fn, const char *mode);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 // close a VCF/BCF file
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 int vcf_close(bcf_t *bp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 // read the VCF/BCF header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 bcf_hdr_t *vcf_hdr_read(bcf_t *bp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 // read the sequence dictionary from a separate file; required for VCF->BCF conversion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 int vcf_dictread(bcf_t *bp, bcf_hdr_t *h, const char *fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 // read a VCF/BCF record; return -1 on end-of-file and <-1 for errors
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 int vcf_read(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 // write the VCF header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 int vcf_hdr_write(bcf_t *bp, const bcf_hdr_t *h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 // write a VCF record
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 int vcf_write(bcf_t *bp, bcf_hdr_t *h, bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 // keep the first n alleles and discard the rest
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 int bcf_shrink_alt(bcf1_t *b, int n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 // keep the masked alleles and discard the rest
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 void bcf_fit_alt(bcf1_t *b, int mask);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 // convert GL to PL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 int bcf_gl2pl(bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 // if the site is an indel
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 int bcf_is_indel(const bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 bcf_hdr_t *bcf_hdr_subsam(const bcf_hdr_t *h0, int n, char *const* samples, int *list);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 int bcf_subsam(int n_smpl, int *list, bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 // move GT to the first FORMAT field
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 int bcf_fix_gt(bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 // update PL generated by old samtools
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 int bcf_fix_pl(bcf1_t *b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 // convert PL to GLF-like 10-likelihood GL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 int bcf_gl10(const bcf1_t *b, uint8_t *gl);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 // convert up to 4 INDEL alleles to GLF-like 10-likelihood GL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 int bcf_gl10_indel(const bcf1_t *b, uint8_t *gl);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 // string hash table
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 void *bcf_build_refhash(bcf_hdr_t *h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 void bcf_str2id_destroy(void *_hash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 void bcf_str2id_thorough_destroy(void *_hash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 int bcf_str2id_add(void *_hash, const char *str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 int bcf_str2id(void *_hash, const char *str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 void *bcf_str2id_init();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 // indexing related functions
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 int bcf_idx_build(const char *fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 uint64_t bcf_idx_query(const bcf_idx_t *idx, int tid, int beg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 int bcf_parse_region(void *str2id, const char *str, int *tid, int *begin, int *end);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 bcf_idx_t *bcf_idx_load(const char *fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 void bcf_idx_destroy(bcf_idx_t *idx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 #ifdef __cplusplus
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 static inline uint32_t bcf_str2int(const char *str, int l)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 uint32_t x = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 for (i = 0; i < l && i < 4; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 if (str[i] == 0) return x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 x = x<<8 | str[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 return x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 #endif