comparison SNV/SNVMix2_source/SNVMix2-v0.12.1-rc1/samtools-0.1.6/bam_pileup.c @ 0:74f5ea818cea

Uploaded
author ryanmorin
date Wed, 12 Oct 2011 19:50:38 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:74f5ea818cea
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <assert.h>
5 #include "sam.h"
6
7 typedef struct __linkbuf_t {
8 bam1_t b;
9 uint32_t beg, end;
10 struct __linkbuf_t *next;
11 } lbnode_t;
12
13 /* --- BEGIN: Memory pool */
14
15 typedef struct {
16 int cnt, n, max;
17 lbnode_t **buf;
18 } mempool_t;
19
20 static mempool_t *mp_init()
21 {
22 mempool_t *mp;
23 mp = (mempool_t*)calloc(1, sizeof(mempool_t));
24 return mp;
25 }
26 static void mp_destroy(mempool_t *mp)
27 {
28 int k;
29 for (k = 0; k < mp->n; ++k) {
30 free(mp->buf[k]->b.data);
31 free(mp->buf[k]);
32 }
33 free(mp->buf);
34 free(mp);
35 }
36 static inline lbnode_t *mp_alloc(mempool_t *mp)
37 {
38 ++mp->cnt;
39 if (mp->n == 0) return (lbnode_t*)calloc(1, sizeof(lbnode_t));
40 else return mp->buf[--mp->n];
41 }
42 static inline void mp_free(mempool_t *mp, lbnode_t *p)
43 {
44 --mp->cnt; p->next = 0; // clear lbnode_t::next here
45 if (mp->n == mp->max) {
46 mp->max = mp->max? mp->max<<1 : 256;
47 mp->buf = (lbnode_t**)realloc(mp->buf, sizeof(lbnode_t*) * mp->max);
48 }
49 mp->buf[mp->n++] = p;
50 }
51
52 /* --- END: Memory pool */
53
54 /* --- BEGIN: Auxiliary functions */
55
56 static inline int resolve_cigar(bam_pileup1_t *p, uint32_t pos)
57 {
58 unsigned k;
59 bam1_t *b = p->b;
60 bam1_core_t *c = &b->core;
61 uint32_t x = c->pos, y = 0;
62 int ret = 1, is_restart = 1;
63
64 if (c->flag&BAM_FUNMAP) return 0; // unmapped read
65 assert(x <= pos); // otherwise a bug
66 p->qpos = -1; p->indel = 0; p->is_del = p->is_head = p->is_tail = 0;
67 for (k = 0; k < c->n_cigar; ++k) {
68 int op = bam1_cigar(b)[k] & BAM_CIGAR_MASK; // operation
69 int l = bam1_cigar(b)[k] >> BAM_CIGAR_SHIFT; // length
70 if (op == BAM_CMATCH) { // NOTE: this assumes the first and the last operation MUST BE a match or a clip
71 if (x + l > pos) { // overlap with pos
72 p->indel = p->is_del = 0;
73 p->qpos = y + (pos - x);
74 if (x == pos && is_restart) p->is_head = 1;
75 if (x + l - 1 == pos) { // come to the end of a match
76 if (k < c->n_cigar - 1) { // there are additional operation(s)
77 uint32_t cigar = bam1_cigar(b)[k+1]; // next CIGAR
78 int op_next = cigar&BAM_CIGAR_MASK; // next CIGAR operation
79 if (op_next == BAM_CDEL) p->indel = -(int32_t)(cigar>>BAM_CIGAR_SHIFT); // del
80 else if (op_next == BAM_CINS) p->indel = cigar>>BAM_CIGAR_SHIFT; // ins
81 if (op_next == BAM_CDEL || op_next == BAM_CINS) {
82 if (k + 2 < c->n_cigar) op_next = bam1_cigar(b)[k+2]&BAM_CIGAR_MASK;
83 else p->is_tail = 1;
84 }
85 if (op_next == BAM_CSOFT_CLIP || op_next == BAM_CREF_SKIP || op_next == BAM_CHARD_CLIP)
86 p->is_tail = 1; // tail
87 } else p->is_tail = 1; // this is the last operation; set tail
88 }
89 }
90 x += l; y += l;
91 } else if (op == BAM_CDEL) { // then set ->is_del
92 if (x + l > pos) {
93 p->indel = 0; p->is_del = 1;
94 p->qpos = y + (pos - x);
95 }
96 x += l;
97 } else if (op == BAM_CREF_SKIP) x += l;
98 else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
99 is_restart = (op == BAM_CREF_SKIP || op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP);
100 if (x > pos) {
101 if (op == BAM_CREF_SKIP) ret = 0; // then do not put it into pileup at all
102 break;
103 }
104 }
105 assert(x > pos); // otherwise a bug
106 return ret;
107 }
108
109 /* --- END: Auxiliary functions */
110
111 struct __bam_plbuf_t {
112 mempool_t *mp;
113 lbnode_t *head, *tail, *dummy;
114 bam_pileup_f func;
115 void *func_data;
116 int32_t tid, pos, max_tid, max_pos;
117 int max_pu, is_eof;
118 bam_pileup1_t *pu;
119 int flag_mask;
120 };
121
122 void bam_plbuf_reset(bam_plbuf_t *buf)
123 {
124 lbnode_t *p, *q;
125 buf->max_tid = buf->max_pos = -1;
126 buf->tid = buf->pos = 0;
127 buf->is_eof = 0;
128 for (p = buf->head; p->next;) {
129 q = p->next;
130 mp_free(buf->mp, p);
131 p = q;
132 }
133 buf->head = buf->tail;
134 }
135
136 void bam_plbuf_set_mask(bam_plbuf_t *buf, int mask)
137 {
138 if (mask < 0) buf->flag_mask = BAM_DEF_MASK;
139 else buf->flag_mask = BAM_FUNMAP | mask;
140 }
141
142 bam_plbuf_t *bam_plbuf_init(bam_pileup_f func, void *data)
143 {
144 bam_plbuf_t *buf;
145 buf = (bam_plbuf_t*)calloc(1, sizeof(bam_plbuf_t));
146 buf->func = func; buf->func_data = data;
147 buf->mp = mp_init();
148 buf->head = buf->tail = mp_alloc(buf->mp);
149 buf->dummy = mp_alloc(buf->mp);
150 buf->max_tid = buf->max_pos = -1;
151 buf->flag_mask = BAM_DEF_MASK;
152 return buf;
153 }
154
155 void bam_plbuf_destroy(bam_plbuf_t *buf)
156 {
157 mp_free(buf->mp, buf->dummy);
158 mp_free(buf->mp, buf->head);
159 if (buf->mp->cnt != 0)
160 fprintf(stderr, "[bam_plbuf_destroy] memory leak: %d. Continue anyway.\n", buf->mp->cnt);
161 mp_destroy(buf->mp);
162 free(buf->pu);
163 free(buf);
164 }
165
166 int bam_plbuf_push(const bam1_t *b, bam_plbuf_t *buf)
167 {
168 if (b) { // fill buffer
169 if (b->core.tid < 0) return 0;
170 if (b->core.flag & buf->flag_mask) return 0;
171 bam_copy1(&buf->tail->b, b);
172 buf->tail->beg = b->core.pos; buf->tail->end = bam_calend(&b->core, bam1_cigar(b));
173 if (b->core.tid < buf->max_tid) {
174 fprintf(stderr, "[bam_pileup_core] the input is not sorted (chromosomes out of order)\n");
175 return -1;
176 }
177 if ((b->core.tid == buf->max_tid) && (buf->tail->beg < buf->max_pos)) {
178 fprintf(stderr, "[bam_pileup_core] the input is not sorted (reads out of order)\n");
179 return -1;
180 }
181 buf->max_tid = b->core.tid; buf->max_pos = buf->tail->beg;
182 if (buf->tail->end > buf->pos || buf->tail->b.core.tid > buf->tid) {
183 buf->tail->next = mp_alloc(buf->mp);
184 buf->tail = buf->tail->next;
185 }
186 } else buf->is_eof = 1;
187 while (buf->is_eof || buf->max_tid > buf->tid || (buf->max_tid == buf->tid && buf->max_pos > buf->pos)) {
188 int n_pu = 0;
189 lbnode_t *p, *q;
190 buf->dummy->next = buf->head;
191 for (p = buf->head, q = buf->dummy; p->next; q = p, p = p->next) {
192 if (p->b.core.tid < buf->tid || (p->b.core.tid == buf->tid && p->end <= buf->pos)) { // then remove from the list
193 q->next = p->next; mp_free(buf->mp, p); p = q;
194 } else if (p->b.core.tid == buf->tid && p->beg <= buf->pos) { // here: p->end > pos; then add to pileup
195 if (n_pu == buf->max_pu) { // then double the capacity
196 buf->max_pu = buf->max_pu? buf->max_pu<<1 : 256;
197 buf->pu = (bam_pileup1_t*)realloc(buf->pu, sizeof(bam_pileup1_t) * buf->max_pu);
198 }
199 buf->pu[n_pu].b = &p->b;
200 if (resolve_cigar(buf->pu + n_pu, buf->pos)) ++n_pu; // skip the read if we are looking at BAM_CREF_SKIP
201 }
202 }
203 buf->head = buf->dummy->next; // dummy->next may be changed
204 if (n_pu) { // then call user defined function
205 buf->func(buf->tid, buf->pos, n_pu, buf->pu, buf->func_data);
206 }
207 // update tid and pos
208 if (buf->head->next) {
209 if (buf->tid > buf->head->b.core.tid) {
210 fprintf(stderr, "[bam_plbuf_push] unsorted input. Pileup aborts.\n");
211 return 1;
212 }
213 }
214 if (buf->tid < buf->head->b.core.tid) { // come to a new reference sequence
215 buf->tid = buf->head->b.core.tid; buf->pos = buf->head->beg; // jump to the next reference
216 } else if (buf->pos < buf->head->beg) { // here: tid == head->b.core.tid
217 buf->pos = buf->head->beg; // jump to the next position
218 } else ++buf->pos; // scan contiguously
219 if (buf->is_eof && buf->head->next == 0) break;
220 }
221 return 0;
222 }
223
224 int bam_pileup_file(bamFile fp, int mask, bam_pileup_f func, void *func_data)
225 {
226 bam_plbuf_t *buf;
227 int ret;
228 bam1_t *b;
229 b = bam_init1();
230 buf = bam_plbuf_init(func, func_data);
231 bam_plbuf_set_mask(buf, mask);
232 while ((ret = bam_read1(fp, b)) >= 0)
233 bam_plbuf_push(b, buf);
234 bam_plbuf_push(0, buf);
235 bam_plbuf_destroy(buf);
236 bam_destroy1(b);
237 return 0;
238 }