annotate pyPRADA_1.2/tools/samtools-0.1.16/bam_index.c @ 3:f17965495ec9 draft default tip

Uploaded
author siyuan
date Tue, 11 Mar 2014 12:14:01 -0400
parents acc2ca1a3ba4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #include <ctype.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include <assert.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #include "bam.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include "khash.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 #include "ksort.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 #include "bam_endian.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 #include "knetfile.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 /*!
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 @header
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 Alignment indexing. Before indexing, BAM must be sorted based on the
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 leftmost coordinate of alignments. In indexing, BAM uses two indices:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 a UCSC binning index and a simple linear index. The binning index is
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 efficient for alignments spanning long distance, while the auxiliary
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 linear index helps to reduce unnecessary seek calls especially for
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 short alignments.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 The UCSC binning scheme was suggested by Richard Durbin and Lincoln
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 Stein and is explained by Kent et al. (2002). In this scheme, each bin
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 represents a contiguous genomic region which can be fully contained in
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 another bin; each alignment is associated with a bin which represents
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 the smallest region containing the entire alignment. The binning
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 scheme is essentially another representation of R-tree. A distinct bin
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 uniquely corresponds to a distinct internal node in a R-tree. Bin A is
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 a child of Bin B if region A is contained in B.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 In BAM, each bin may span 2^29, 2^26, 2^23, 2^20, 2^17 or 2^14 bp. Bin
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 0 spans a 512Mbp region, bins 1-8 span 64Mbp, 9-72 8Mbp, 73-584 1Mbp,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 585-4680 128Kbp and bins 4681-37449 span 16Kbp regions. If we want to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 find the alignments overlapped with a region [rbeg,rend), we need to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 calculate the list of bins that may be overlapped the region and test
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 the alignments in the bins to confirm the overlaps. If the specified
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 region is short, typically only a few alignments in six bins need to
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 be retrieved. The overlapping alignments can be quickly fetched.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 #define BAM_MIN_CHUNK_GAP 32768
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 // 1<<14 is the size of minimum bin.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 #define BAM_LIDX_SHIFT 14
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 #define BAM_MAX_BIN 37450 // =(8^6-1)/7+1
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 typedef struct {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 uint64_t u, v;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 } pair64_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 #define pair64_lt(a,b) ((a).u < (b).u)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 KSORT_INIT(off, pair64_t, pair64_lt)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 typedef struct {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 uint32_t m, n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 pair64_t *list;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 } bam_binlist_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 typedef struct {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 int32_t n, m;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 uint64_t *offset;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 } bam_lidx_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 KHASH_MAP_INIT_INT(i, bam_binlist_t)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 struct __bam_index_t {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 int32_t n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 uint64_t n_no_coor; // unmapped reads without coordinate
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 khash_t(i) **index;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 bam_lidx_t *index2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 };
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 // requirement: len <= LEN_MASK
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 static inline void insert_offset(khash_t(i) *h, int bin, uint64_t beg, uint64_t end)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 bam_binlist_t *l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 int ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 k = kh_put(i, h, bin, &ret);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 l = &kh_value(h, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 if (ret) { // not present
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 l->m = 1; l->n = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 l->list = (pair64_t*)calloc(l->m, 16);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 if (l->n == l->m) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 l->m <<= 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 l->list = (pair64_t*)realloc(l->list, l->m * 16);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 l->list[l->n].u = beg; l->list[l->n++].v = end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 static inline void insert_offset2(bam_lidx_t *index2, bam1_t *b, uint64_t offset)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 int i, beg, end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 beg = b->core.pos >> BAM_LIDX_SHIFT;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 end = (bam_calend(&b->core, bam1_cigar(b)) - 1) >> BAM_LIDX_SHIFT;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 if (index2->m < end + 1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 int old_m = index2->m;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 index2->m = end + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 kroundup32(index2->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 index2->offset = (uint64_t*)realloc(index2->offset, index2->m * 8);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 memset(index2->offset + old_m, 0, 8 * (index2->m - old_m));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 if (beg == end) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 if (index2->offset[beg] == 0) index2->offset[beg] = offset;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 for (i = beg; i <= end; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 if (index2->offset[i] == 0) index2->offset[i] = offset;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 index2->n = end + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 static void merge_chunks(bam_index_t *idx)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 #if defined(BAM_TRUE_OFFSET) || defined(BAM_VIRTUAL_OFFSET16)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 khash_t(i) *index;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 int i, l, m;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 for (i = 0; i < idx->n; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 index = idx->index[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 for (k = kh_begin(index); k != kh_end(index); ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 bam_binlist_t *p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 if (!kh_exist(index, k) || kh_key(index, k) == BAM_MAX_BIN) continue;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 p = &kh_value(index, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 m = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 for (l = 1; l < p->n; ++l) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 #ifdef BAM_TRUE_OFFSET
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 if (p->list[m].v + BAM_MIN_CHUNK_GAP > p->list[l].u) p->list[m].v = p->list[l].v;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 if (p->list[m].v>>16 == p->list[l].u>>16) p->list[m].v = p->list[l].v;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 else p->list[++m] = p->list[l];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 } // ~for(l)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 p->n = m + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 } // ~for(k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 } // ~for(i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 #endif // defined(BAM_TRUE_OFFSET) || defined(BAM_BGZF)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 static void fill_missing(bam_index_t *idx)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 int i, j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 for (i = 0; i < idx->n; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 bam_lidx_t *idx2 = &idx->index2[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 for (j = 1; j < idx2->n; ++j)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 if (idx2->offset[j] == 0)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 idx2->offset[j] = idx2->offset[j-1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 bam_index_t *bam_index_core(bamFile fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 bam1_t *b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 bam_header_t *h;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 int i, ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 bam_index_t *idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 uint32_t last_bin, save_bin;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 int32_t last_coor, last_tid, save_tid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 bam1_core_t *c;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 uint64_t save_off, last_off, n_mapped, n_unmapped, off_beg, off_end, n_no_coor;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 idx = (bam_index_t*)calloc(1, sizeof(bam_index_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 b = (bam1_t*)calloc(1, sizeof(bam1_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 h = bam_header_read(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 c = &b->core;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 idx->n = h->n_targets;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 bam_header_destroy(h);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 for (i = 0; i < idx->n; ++i) idx->index[i] = kh_init(i);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 save_bin = save_tid = last_tid = last_bin = 0xffffffffu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174 save_off = last_off = bam_tell(fp); last_coor = 0xffffffffu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 n_mapped = n_unmapped = n_no_coor = off_end = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 off_beg = off_end = bam_tell(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 while ((ret = bam_read1(fp, b)) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 if (c->tid < 0) ++n_no_coor;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 if (last_tid != c->tid) { // change of chromosomes
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
180 last_tid = c->tid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
181 last_bin = 0xffffffffu;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
182 } else if (last_coor > c->pos) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
183 fprintf(stderr, "[bam_index_core] the alignment is not sorted (%s): %u > %u in %d-th chr\n",
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
184 bam1_qname(b), last_coor, c->pos, c->tid+1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
185 exit(1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
186 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
187 if (c->tid >= 0) insert_offset2(&idx->index2[b->core.tid], b, last_off);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
188 if (c->bin != last_bin) { // then possibly write the binning index
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
189 if (save_bin != 0xffffffffu) // save_bin==0xffffffffu only happens to the first record
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
190 insert_offset(idx->index[save_tid], save_bin, save_off, last_off);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
191 if (last_bin == 0xffffffffu && save_tid != 0xffffffffu) { // write the meta element
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
192 off_end = last_off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
193 insert_offset(idx->index[save_tid], BAM_MAX_BIN, off_beg, off_end);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
194 insert_offset(idx->index[save_tid], BAM_MAX_BIN, n_mapped, n_unmapped);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
195 n_mapped = n_unmapped = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
196 off_beg = off_end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
197 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
198 save_off = last_off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
199 save_bin = last_bin = c->bin;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
200 save_tid = c->tid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
201 if (save_tid < 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
202 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
203 if (bam_tell(fp) <= last_off) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
204 fprintf(stderr, "[bam_index_core] bug in BGZF/RAZF: %llx < %llx\n",
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
205 (unsigned long long)bam_tell(fp), (unsigned long long)last_off);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
206 exit(1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
207 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
208 if (c->flag & BAM_FUNMAP) ++n_unmapped;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
209 else ++n_mapped;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
210 last_off = bam_tell(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
211 last_coor = b->core.pos;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
212 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
213 if (save_tid >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
214 insert_offset(idx->index[save_tid], save_bin, save_off, bam_tell(fp));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
215 insert_offset(idx->index[save_tid], BAM_MAX_BIN, off_beg, bam_tell(fp));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
216 insert_offset(idx->index[save_tid], BAM_MAX_BIN, n_mapped, n_unmapped);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
217 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
218 merge_chunks(idx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
219 fill_missing(idx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
220 if (ret >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
221 while ((ret = bam_read1(fp, b)) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
222 ++n_no_coor;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
223 if (c->tid >= 0 && n_no_coor) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
224 fprintf(stderr, "[bam_index_core] the alignment is not sorted: reads without coordinates prior to reads with coordinates.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
225 exit(1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
226 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
227 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
228 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
229 if (ret < -1) fprintf(stderr, "[bam_index_core] truncated file? Continue anyway. (%d)\n", ret);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
230 free(b->data); free(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
231 idx->n_no_coor = n_no_coor;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
232 return idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
233 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
234
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
235 void bam_index_destroy(bam_index_t *idx)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
236 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
237 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
238 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
239 if (idx == 0) return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
240 for (i = 0; i < idx->n; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
241 khash_t(i) *index = idx->index[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
242 bam_lidx_t *index2 = idx->index2 + i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
243 for (k = kh_begin(index); k != kh_end(index); ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
244 if (kh_exist(index, k))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
245 free(kh_value(index, k).list);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
246 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
247 kh_destroy(i, index);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
248 free(index2->offset);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
249 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
250 free(idx->index); free(idx->index2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
251 free(idx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
252 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
253
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
254 void bam_index_save(const bam_index_t *idx, FILE *fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
255 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
256 int32_t i, size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
257 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
258 fwrite("BAI\1", 1, 4, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
259 if (bam_is_be) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
260 uint32_t x = idx->n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
261 fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
262 } else fwrite(&idx->n, 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
263 for (i = 0; i < idx->n; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
264 khash_t(i) *index = idx->index[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
265 bam_lidx_t *index2 = idx->index2 + i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
266 // write binning index
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
267 size = kh_size(index);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
268 if (bam_is_be) { // big endian
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
269 uint32_t x = size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
270 fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
271 } else fwrite(&size, 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
272 for (k = kh_begin(index); k != kh_end(index); ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
273 if (kh_exist(index, k)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
274 bam_binlist_t *p = &kh_value(index, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
275 if (bam_is_be) { // big endian
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
276 uint32_t x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
277 x = kh_key(index, k); fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
278 x = p->n; fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
279 for (x = 0; (int)x < p->n; ++x) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
280 bam_swap_endian_8p(&p->list[x].u);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
281 bam_swap_endian_8p(&p->list[x].v);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
282 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
283 fwrite(p->list, 16, p->n, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
284 for (x = 0; (int)x < p->n; ++x) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
285 bam_swap_endian_8p(&p->list[x].u);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
286 bam_swap_endian_8p(&p->list[x].v);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
287 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
288 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
289 fwrite(&kh_key(index, k), 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
290 fwrite(&p->n, 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
291 fwrite(p->list, 16, p->n, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
292 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
293 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
294 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
295 // write linear index (index2)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
296 if (bam_is_be) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
297 int x = index2->n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
298 fwrite(bam_swap_endian_4p(&x), 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
299 } else fwrite(&index2->n, 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
300 if (bam_is_be) { // big endian
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
301 int x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
302 for (x = 0; (int)x < index2->n; ++x)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
303 bam_swap_endian_8p(&index2->offset[x]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
304 fwrite(index2->offset, 8, index2->n, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
305 for (x = 0; (int)x < index2->n; ++x)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
306 bam_swap_endian_8p(&index2->offset[x]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
307 } else fwrite(index2->offset, 8, index2->n, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
308 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
309 { // write the number of reads coor-less records.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
310 uint64_t x = idx->n_no_coor;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
311 if (bam_is_be) bam_swap_endian_8p(&x);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
312 fwrite(&x, 8, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
313 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
314 fflush(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
315 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
316
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
317 static bam_index_t *bam_index_load_core(FILE *fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
318 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
319 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
320 char magic[4];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
321 bam_index_t *idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
322 if (fp == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
323 fprintf(stderr, "[bam_index_load_core] fail to load index.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
324 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
325 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
326 fread(magic, 1, 4, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
327 if (strncmp(magic, "BAI\1", 4)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
328 fprintf(stderr, "[bam_index_load] wrong magic number.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
329 fclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
330 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
331 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
332 idx = (bam_index_t*)calloc(1, sizeof(bam_index_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
333 fread(&idx->n, 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
334 if (bam_is_be) bam_swap_endian_4p(&idx->n);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
335 idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
336 idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
337 for (i = 0; i < idx->n; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
338 khash_t(i) *index;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
339 bam_lidx_t *index2 = idx->index2 + i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
340 uint32_t key, size;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
341 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
342 int j, ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
343 bam_binlist_t *p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
344 index = idx->index[i] = kh_init(i);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
345 // load binning index
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
346 fread(&size, 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
347 if (bam_is_be) bam_swap_endian_4p(&size);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
348 for (j = 0; j < (int)size; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
349 fread(&key, 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
350 if (bam_is_be) bam_swap_endian_4p(&key);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
351 k = kh_put(i, index, key, &ret);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
352 p = &kh_value(index, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
353 fread(&p->n, 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
354 if (bam_is_be) bam_swap_endian_4p(&p->n);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
355 p->m = p->n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
356 p->list = (pair64_t*)malloc(p->m * 16);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
357 fread(p->list, 16, p->n, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
358 if (bam_is_be) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
359 int x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
360 for (x = 0; x < p->n; ++x) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
361 bam_swap_endian_8p(&p->list[x].u);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
362 bam_swap_endian_8p(&p->list[x].v);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
363 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
364 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
365 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
366 // load linear index
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
367 fread(&index2->n, 4, 1, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
368 if (bam_is_be) bam_swap_endian_4p(&index2->n);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
369 index2->m = index2->n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
370 index2->offset = (uint64_t*)calloc(index2->m, 8);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
371 fread(index2->offset, index2->n, 8, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
372 if (bam_is_be)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
373 for (j = 0; j < index2->n; ++j) bam_swap_endian_8p(&index2->offset[j]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
374 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
375 if (fread(&idx->n_no_coor, 8, 1, fp) == 0) idx->n_no_coor = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
376 if (bam_is_be) bam_swap_endian_8p(&idx->n_no_coor);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
377 return idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
378 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
379
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
380 bam_index_t *bam_index_load_local(const char *_fn)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
381 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
382 FILE *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
383 char *fnidx, *fn;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
384
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
385 if (strstr(_fn, "ftp://") == _fn || strstr(_fn, "http://") == _fn) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
386 const char *p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
387 int l = strlen(_fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
388 for (p = _fn + l - 1; p >= _fn; --p)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
389 if (*p == '/') break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
390 fn = strdup(p + 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
391 } else fn = strdup(_fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
392 fnidx = (char*)calloc(strlen(fn) + 5, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
393 strcpy(fnidx, fn); strcat(fnidx, ".bai");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
394 fp = fopen(fnidx, "rb");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
395 if (fp == 0) { // try "{base}.bai"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
396 char *s = strstr(fn, "bam");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
397 if (s == fn + strlen(fn) - 3) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
398 strcpy(fnidx, fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
399 fnidx[strlen(fn)-1] = 'i';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
400 fp = fopen(fnidx, "rb");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
401 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
402 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
403 free(fnidx); free(fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
404 if (fp) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
405 bam_index_t *idx = bam_index_load_core(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
406 fclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
407 return idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
408 } else return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
409 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
410
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
411 #ifdef _USE_KNETFILE
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
412 static void download_from_remote(const char *url)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
413 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
414 const int buf_size = 1 * 1024 * 1024;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
415 char *fn;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
416 FILE *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
417 uint8_t *buf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
418 knetFile *fp_remote;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
419 int l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
420 if (strstr(url, "ftp://") != url && strstr(url, "http://") != url) return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
421 l = strlen(url);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
422 for (fn = (char*)url + l - 1; fn >= url; --fn)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
423 if (*fn == '/') break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
424 ++fn; // fn now points to the file name
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
425 fp_remote = knet_open(url, "r");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
426 if (fp_remote == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
427 fprintf(stderr, "[download_from_remote] fail to open remote file.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
428 return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
429 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
430 if ((fp = fopen(fn, "wb")) == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
431 fprintf(stderr, "[download_from_remote] fail to create file in the working directory.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
432 knet_close(fp_remote);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
433 return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
434 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
435 buf = (uint8_t*)calloc(buf_size, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
436 while ((l = knet_read(fp_remote, buf, buf_size)) != 0)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
437 fwrite(buf, 1, l, fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
438 free(buf);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
439 fclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
440 knet_close(fp_remote);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
441 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
442 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
443 static void download_from_remote(const char *url)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
444 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
445 return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
446 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
447 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
448
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
449 bam_index_t *bam_index_load(const char *fn)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
450 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
451 bam_index_t *idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
452 idx = bam_index_load_local(fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
453 if (idx == 0 && (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
454 char *fnidx = calloc(strlen(fn) + 5, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
455 strcat(strcpy(fnidx, fn), ".bai");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
456 fprintf(stderr, "[bam_index_load] attempting to download the remote index file.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
457 download_from_remote(fnidx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
458 idx = bam_index_load_local(fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
459 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
460 if (idx == 0) fprintf(stderr, "[bam_index_load] fail to load BAM index.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
461 return idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
462 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
463
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
464 int bam_index_build2(const char *fn, const char *_fnidx)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
465 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
466 char *fnidx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
467 FILE *fpidx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
468 bamFile fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
469 bam_index_t *idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
470 if ((fp = bam_open(fn, "r")) == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
471 fprintf(stderr, "[bam_index_build2] fail to open the BAM file.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
472 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
473 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
474 idx = bam_index_core(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
475 bam_close(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
476 if (_fnidx == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
477 fnidx = (char*)calloc(strlen(fn) + 5, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
478 strcpy(fnidx, fn); strcat(fnidx, ".bai");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
479 } else fnidx = strdup(_fnidx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
480 fpidx = fopen(fnidx, "wb");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
481 if (fpidx == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
482 fprintf(stderr, "[bam_index_build2] fail to create the index file.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
483 free(fnidx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
484 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
485 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
486 bam_index_save(idx, fpidx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
487 bam_index_destroy(idx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
488 fclose(fpidx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
489 free(fnidx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
490 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
491 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
492
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
493 int bam_index_build(const char *fn)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
494 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
495 return bam_index_build2(fn, 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
496 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
497
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
498 int bam_index(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
499 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
500 if (argc < 2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
501 fprintf(stderr, "Usage: samtools index <in.bam> [out.index]\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
502 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
503 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
504 if (argc >= 3) bam_index_build2(argv[1], argv[2]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
505 else bam_index_build(argv[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
506 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
507 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
508
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
509 int bam_idxstats(int argc, char *argv[])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
510 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
511 bam_index_t *idx;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
512 bam_header_t *header;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
513 bamFile fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
514 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
515 if (argc < 2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
516 fprintf(stderr, "Usage: samtools idxstats <in.bam>\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
517 return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
518 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
519 fp = bam_open(argv[1], "r");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
520 if (fp == 0) { fprintf(stderr, "[%s] fail to open BAM.\n", __func__); return 1; }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
521 header = bam_header_read(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
522 bam_close(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
523 idx = bam_index_load(argv[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
524 if (idx == 0) { fprintf(stderr, "[%s] fail to load the index.\n", __func__); return 1; }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
525 for (i = 0; i < idx->n; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
526 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
527 khash_t(i) *h = idx->index[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
528 printf("%s\t%d", header->target_name[i], header->target_len[i]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
529 k = kh_get(i, h, BAM_MAX_BIN);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
530 if (k != kh_end(h))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
531 printf("\t%llu\t%llu", (long long)kh_val(h, k).list[1].u, (long long)kh_val(h, k).list[1].v);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
532 else printf("\t0\t0");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
533 putchar('\n');
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
534 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
535 printf("*\t0\t0\t%llu\n", (long long)idx->n_no_coor);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
536 bam_header_destroy(header);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
537 bam_index_destroy(idx);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
538 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
539 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
540
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
541 static inline int reg2bins(uint32_t beg, uint32_t end, uint16_t list[BAM_MAX_BIN])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
542 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
543 int i = 0, k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
544 if (beg >= end) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
545 if (end >= 1u<<29) end = 1u<<29;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
546 --end;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
547 list[i++] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
548 for (k = 1 + (beg>>26); k <= 1 + (end>>26); ++k) list[i++] = k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
549 for (k = 9 + (beg>>23); k <= 9 + (end>>23); ++k) list[i++] = k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
550 for (k = 73 + (beg>>20); k <= 73 + (end>>20); ++k) list[i++] = k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
551 for (k = 585 + (beg>>17); k <= 585 + (end>>17); ++k) list[i++] = k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
552 for (k = 4681 + (beg>>14); k <= 4681 + (end>>14); ++k) list[i++] = k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
553 return i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
554 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
555
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
556 static inline int is_overlap(uint32_t beg, uint32_t end, const bam1_t *b)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
557 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
558 uint32_t rbeg = b->core.pos;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
559 uint32_t rend = b->core.n_cigar? bam_calend(&b->core, bam1_cigar(b)) : b->core.pos + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
560 return (rend > beg && rbeg < end);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
561 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
562
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
563 struct __bam_iter_t {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
564 int from_first; // read from the first record; no random access
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
565 int tid, beg, end, n_off, i, finished;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
566 uint64_t curr_off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
567 pair64_t *off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
568 };
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
569
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
570 // bam_fetch helper function retrieves
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
571 bam_iter_t bam_iter_query(const bam_index_t *idx, int tid, int beg, int end)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
572 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
573 uint16_t *bins;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
574 int i, n_bins, n_off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
575 pair64_t *off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
576 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
577 khash_t(i) *index;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
578 uint64_t min_off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
579 bam_iter_t iter = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
580
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
581 if (beg < 0) beg = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
582 if (end < beg) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
583 // initialize iter
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
584 iter = calloc(1, sizeof(struct __bam_iter_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
585 iter->tid = tid, iter->beg = beg, iter->end = end; iter->i = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
586 //
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
587 bins = (uint16_t*)calloc(BAM_MAX_BIN, 2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
588 n_bins = reg2bins(beg, end, bins);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
589 index = idx->index[tid];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
590 if (idx->index2[tid].n > 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
591 min_off = (beg>>BAM_LIDX_SHIFT >= idx->index2[tid].n)? idx->index2[tid].offset[idx->index2[tid].n-1]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
592 : idx->index2[tid].offset[beg>>BAM_LIDX_SHIFT];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
593 if (min_off == 0) { // improvement for index files built by tabix prior to 0.1.4
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
594 int n = beg>>BAM_LIDX_SHIFT;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
595 if (n > idx->index2[tid].n) n = idx->index2[tid].n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
596 for (i = n - 1; i >= 0; --i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
597 if (idx->index2[tid].offset[i] != 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
598 if (i >= 0) min_off = idx->index2[tid].offset[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
599 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
600 } else min_off = 0; // tabix 0.1.2 may produce such index files
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
601 for (i = n_off = 0; i < n_bins; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
602 if ((k = kh_get(i, index, bins[i])) != kh_end(index))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
603 n_off += kh_value(index, k).n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
604 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
605 if (n_off == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
606 free(bins); return iter;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
607 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
608 off = (pair64_t*)calloc(n_off, 16);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
609 for (i = n_off = 0; i < n_bins; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
610 if ((k = kh_get(i, index, bins[i])) != kh_end(index)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
611 int j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
612 bam_binlist_t *p = &kh_value(index, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
613 for (j = 0; j < p->n; ++j)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
614 if (p->list[j].v > min_off) off[n_off++] = p->list[j];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
615 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
616 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
617 free(bins);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
618 if (n_off == 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
619 free(off); return iter;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
620 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
621 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
622 bam1_t *b = (bam1_t*)calloc(1, sizeof(bam1_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
623 int l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
624 ks_introsort(off, n_off, off);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
625 // resolve completely contained adjacent blocks
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
626 for (i = 1, l = 0; i < n_off; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
627 if (off[l].v < off[i].v)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
628 off[++l] = off[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
629 n_off = l + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
630 // resolve overlaps between adjacent blocks; this may happen due to the merge in indexing
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
631 for (i = 1; i < n_off; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
632 if (off[i-1].v >= off[i].u) off[i-1].v = off[i].u;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
633 { // merge adjacent blocks
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
634 #if defined(BAM_TRUE_OFFSET) || defined(BAM_VIRTUAL_OFFSET16)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
635 for (i = 1, l = 0; i < n_off; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
636 #ifdef BAM_TRUE_OFFSET
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
637 if (off[l].v + BAM_MIN_CHUNK_GAP > off[i].u) off[l].v = off[i].v;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
638 #else
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
639 if (off[l].v>>16 == off[i].u>>16) off[l].v = off[i].v;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
640 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
641 else off[++l] = off[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
642 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
643 n_off = l + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
644 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
645 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
646 bam_destroy1(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
647 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
648 iter->n_off = n_off; iter->off = off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
649 return iter;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
650 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
651
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
652 pair64_t *get_chunk_coordinates(const bam_index_t *idx, int tid, int beg, int end, int *cnt_off)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
653 { // for pysam compatibility
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
654 bam_iter_t iter;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
655 pair64_t *off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
656 iter = bam_iter_query(idx, tid, beg, end);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
657 off = iter->off; *cnt_off = iter->n_off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
658 free(iter);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
659 return off;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
660 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
661
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
662 void bam_iter_destroy(bam_iter_t iter)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
663 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
664 if (iter) { free(iter->off); free(iter); }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
665 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
666
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
667 int bam_iter_read(bamFile fp, bam_iter_t iter, bam1_t *b)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
668 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
669 int ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
670 if (iter && iter->finished) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
671 if (iter == 0 || iter->from_first) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
672 ret = bam_read1(fp, b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
673 if (ret < 0 && iter) iter->finished = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
674 return ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
675 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
676 if (iter->off == 0) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
677 for (;;) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
678 if (iter->curr_off == 0 || iter->curr_off >= iter->off[iter->i].v) { // then jump to the next chunk
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
679 if (iter->i == iter->n_off - 1) { ret = -1; break; } // no more chunks
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
680 if (iter->i >= 0) assert(iter->curr_off == iter->off[iter->i].v); // otherwise bug
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
681 if (iter->i < 0 || iter->off[iter->i].v != iter->off[iter->i+1].u) { // not adjacent chunks; then seek
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
682 bam_seek(fp, iter->off[iter->i+1].u, SEEK_SET);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
683 iter->curr_off = bam_tell(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
684 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
685 ++iter->i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
686 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
687 if ((ret = bam_read1(fp, b)) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
688 iter->curr_off = bam_tell(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
689 if (b->core.tid != iter->tid || b->core.pos >= iter->end) { // no need to proceed
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
690 ret = bam_validate1(NULL, b)? -1 : -5; // determine whether end of region or error
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
691 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
692 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
693 else if (is_overlap(iter->beg, iter->end, b)) return ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
694 } else break; // end of file or error
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
695 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
696 iter->finished = 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
697 return ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
698 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
699
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
700 int bam_fetch(bamFile fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
701 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
702 int ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
703 bam_iter_t iter;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
704 bam1_t *b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
705 b = bam_init1();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
706 iter = bam_iter_query(idx, tid, beg, end);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
707 while ((ret = bam_iter_read(fp, iter, b)) >= 0) func(b, data);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
708 bam_iter_destroy(iter);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
709 bam_destroy1(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
710 return (ret == -1)? 0 : ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
711 }