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