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