annotate PsiCLASS-1.0.2/samtools-0.1.19/bcftools/index.c @ 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 #include <assert.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <ctype.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <sys/stat.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include "bam_endian.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include "kstring.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include "bcf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include "knetfile.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 #define TAD_LIDX_SHIFT 13
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 int32_t n, m;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 uint64_t *offset;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 } bcf_lidx_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 struct __bcf_idx_t {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 int32_t n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 bcf_lidx_t *index2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 /************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 * indexing *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 ************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 static inline void insert_offset2(bcf_lidx_t *index2, int _beg, int _end, uint64_t offset)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 int i, beg, end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 beg = _beg >> TAD_LIDX_SHIFT;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 end = (_end - 1) >> TAD_LIDX_SHIFT;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 if (index2->m < end + 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 int old_m = index2->m;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 index2->m = end + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 kroundup32(index2->m);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 index2->offset = (uint64_t*)realloc(index2->offset, index2->m * 8);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 memset(index2->offset + old_m, 0, 8 * (index2->m - old_m));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 if (beg == end) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 if (index2->offset[beg] == 0) index2->offset[beg] = offset;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 for (i = beg; i <= end; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 if (index2->offset[i] == 0) index2->offset[i] = offset;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 if (index2->n < end + 1) index2->n = end + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 bcf_idx_t *bcf_idx_core(bcf_t *bp, bcf_hdr_t *h)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 bcf_idx_t *idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 int32_t last_coor, last_tid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 uint64_t last_off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 kstring_t *str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 BGZF *fp = bp->fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 bcf1_t *b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 int ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 b = calloc(1, sizeof(bcf1_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 str = calloc(1, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 idx = (bcf_idx_t*)calloc(1, sizeof(bcf_idx_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 idx->n = h->n_ref;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 idx->index2 = calloc(h->n_ref, sizeof(bcf_lidx_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 last_tid = 0xffffffffu;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 last_off = bgzf_tell(fp); last_coor = 0xffffffffu;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 while ((ret = bcf_read(bp, h, b)) > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 int end, tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 if (last_tid != b->tid) { // change of chromosomes
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 last_tid = b->tid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 } else if (last_coor > b->pos) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 fprintf(stderr, "[bcf_idx_core] the input is out of order\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 free(str->s); free(str); free(idx); bcf_destroy(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 tmp = strlen(b->ref);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 end = b->pos + (tmp > 0? tmp : 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 insert_offset2(&idx->index2[b->tid], b->pos, end, last_off);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 last_off = bgzf_tell(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 last_coor = b->pos;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 free(str->s); free(str); bcf_destroy(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 return idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 void bcf_idx_destroy(bcf_idx_t *idx)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 if (idx == 0) return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 for (i = 0; i < idx->n; ++i) free(idx->index2[i].offset);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 free(idx->index2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 free(idx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 /******************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 * index file I/O *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 ******************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 void bcf_idx_save(const bcf_idx_t *idx, BGZF *fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 int32_t i, ti_is_be;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 ti_is_be = bam_is_big_endian();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 bgzf_write(fp, "BCI\4", 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 if (ti_is_be) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 uint32_t x = idx->n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 bgzf_write(fp, bam_swap_endian_4p(&x), 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 } else bgzf_write(fp, &idx->n, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 for (i = 0; i < idx->n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 bcf_lidx_t *index2 = idx->index2 + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 // write linear index (index2)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 if (ti_is_be) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 int x = index2->n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 bgzf_write(fp, bam_swap_endian_4p(&x), 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 } else bgzf_write(fp, &index2->n, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 if (ti_is_be) { // big endian
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 int x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 for (x = 0; (int)x < index2->n; ++x)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 bam_swap_endian_8p(&index2->offset[x]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 bgzf_write(fp, index2->offset, 8 * index2->n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 for (x = 0; (int)x < index2->n; ++x)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 bam_swap_endian_8p(&index2->offset[x]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 } else bgzf_write(fp, index2->offset, 8 * index2->n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 static bcf_idx_t *bcf_idx_load_core(BGZF *fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 int i, ti_is_be;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 char magic[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 bcf_idx_t *idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 ti_is_be = bam_is_big_endian();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 if (fp == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 fprintf(stderr, "[%s] fail to load index.\n", __func__);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 bgzf_read(fp, magic, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 if (strncmp(magic, "BCI\4", 4)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 fprintf(stderr, "[%s] wrong magic number.\n", __func__);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 idx = (bcf_idx_t*)calloc(1, sizeof(bcf_idx_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 bgzf_read(fp, &idx->n, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 if (ti_is_be) bam_swap_endian_4p(&idx->n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 idx->index2 = (bcf_lidx_t*)calloc(idx->n, sizeof(bcf_lidx_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 for (i = 0; i < idx->n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 bcf_lidx_t *index2 = idx->index2 + i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 int j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 bgzf_read(fp, &index2->n, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 if (ti_is_be) bam_swap_endian_4p(&index2->n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 index2->m = index2->n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 index2->offset = (uint64_t*)calloc(index2->m, 8);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 bgzf_read(fp, index2->offset, index2->n * 8);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 if (ti_is_be)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 for (j = 0; j < index2->n; ++j) bam_swap_endian_8p(&index2->offset[j]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 return idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 bcf_idx_t *bcf_idx_load_local(const char *fnidx)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 BGZF *fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 fp = bgzf_open(fnidx, "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 if (fp) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 bcf_idx_t *idx = bcf_idx_load_core(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 bgzf_close(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 return idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 } else return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 static void download_from_remote(const char *url)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 const int buf_size = 1 * 1024 * 1024;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 char *fn;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 FILE *fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 uint8_t *buf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 knetFile *fp_remote;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 int l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 if (strstr(url, "ftp://") != url && strstr(url, "http://") != url) return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 l = strlen(url);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 for (fn = (char*)url + l - 1; fn >= url; --fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 if (*fn == '/') break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 ++fn; // fn now points to the file name
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 fp_remote = knet_open(url, "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 if (fp_remote == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 fprintf(stderr, "[download_from_remote] fail to open remote file.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 if ((fp = fopen(fn, "w")) == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 fprintf(stderr, "[download_from_remote] fail to create file in the working directory.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 knet_close(fp_remote);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 buf = (uint8_t*)calloc(buf_size, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 while ((l = knet_read(fp_remote, buf, buf_size)) != 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 fwrite(buf, 1, l, fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 free(buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 fclose(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 knet_close(fp_remote);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 static void download_from_remote(const char *url)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 static char *get_local_version(const char *fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 struct stat sbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 char *fnidx = (char*)calloc(strlen(fn) + 5, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 strcat(strcpy(fnidx, fn), ".bci");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 if ((strstr(fnidx, "ftp://") == fnidx || strstr(fnidx, "http://") == fnidx)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 char *p, *url;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 int l = strlen(fnidx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 for (p = fnidx + l - 1; p >= fnidx; --p)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 if (*p == '/') break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 url = fnidx; fnidx = strdup(p + 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 if (stat(fnidx, &sbuf) == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 free(url);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 return fnidx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 fprintf(stderr, "[%s] downloading the index file...\n", __func__);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 download_from_remote(url);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 free(url);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 if (stat(fnidx, &sbuf) == 0) return fnidx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 free(fnidx); return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 bcf_idx_t *bcf_idx_load(const char *fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 bcf_idx_t *idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 char *fname = get_local_version(fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 if (fname == 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 idx = bcf_idx_load_local(fname);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 free(fname);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 return idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 int bcf_idx_build2(const char *fn, const char *_fnidx)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 char *fnidx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 BGZF *fpidx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 bcf_t *bp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 bcf_idx_t *idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 bcf_hdr_t *h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 if ((bp = bcf_open(fn, "r")) == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 fprintf(stderr, "[bcf_idx_build2] fail to open the BAM file.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 h = bcf_hdr_read(bp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 idx = bcf_idx_core(bp, h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 bcf_close(bp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 if (_fnidx == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 fnidx = (char*)calloc(strlen(fn) + 5, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 strcpy(fnidx, fn); strcat(fnidx, ".bci");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 } else fnidx = strdup(_fnidx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 fpidx = bgzf_open(fnidx, "w");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 if (fpidx == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 fprintf(stderr, "[bcf_idx_build2] fail to create the index file.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 free(fnidx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 bcf_idx_destroy(idx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 bcf_idx_save(idx, fpidx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 bcf_idx_destroy(idx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 bgzf_close(fpidx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 free(fnidx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 int bcf_idx_build(const char *fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 return bcf_idx_build2(fn, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 /********************************************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 * parse a region in the format chr:beg-end *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 ********************************************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 int bcf_parse_region(void *str2id, const char *str, int *tid, int *begin, int *end)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 char *s, *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 int i, l, k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 l = strlen(str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 p = s = (char*)malloc(l+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 /* squeeze out "," */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 for (i = k = 0; i != l; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 if (str[i] != ',' && !isspace(str[i])) s[k++] = str[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 s[k] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 for (i = 0; i != k; ++i) if (s[i] == ':') break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 s[i] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 if ((*tid = bcf_str2id(str2id, s)) < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 free(s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 if (i == k) { /* dump the whole sequence */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 *begin = 0; *end = 1<<29; free(s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 for (p = s + i + 1; i != k; ++i) if (s[i] == '-') break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 *begin = atoi(p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 if (i < k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 p = s + i + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 *end = atoi(p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 } else *end = 1<<29;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 if (*begin > 0) --*begin;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 free(s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 if (*begin > *end) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 /*******************************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 * retrieve a specified region *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 *******************************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 uint64_t bcf_idx_query(const bcf_idx_t *idx, int tid, int beg)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 uint64_t min_off, *offset;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 if (beg < 0) beg = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 offset = idx->index2[tid].offset;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 for (i = beg>>TAD_LIDX_SHIFT; i < idx->index2[tid].n && offset[i] == 0; ++i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 min_off = (i == idx->index2[tid].n)? offset[idx->index2[tid].n-1] : offset[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 return min_off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 int bcf_main_index(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 if (argc == 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 fprintf(stderr, "Usage: bcftools index <in.bcf>\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 bcf_idx_build(argv[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 }