Debian Codegraph @

samtools/bam_lpileup.c.pysam.c

raw symbols SCIP
1 #include "samtools.pysam.h"
2
3 /* bam_lpileup.c -- lplbuf routines.
4
5 Copyright (C) 2008, 2009, 2013 Genome Research Ltd.
6
7 Author: Heng Li <[email protected]>
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE. */
26
27 #include <config.h>
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include "bam_plbuf.h"
33 #include "bam_lpileup.h"
34 #include "splaysort.h"
35
36 #define TV_GAP 2
37
38 typedef struct __freenode_t {
39 uint32_t level:28, cnt:4;
40 struct __freenode_t *next;
41 } freenode_t, *freenode_p;
42
43 #define freenode_lt(a,b) ((a)->cnt < (b)->cnt || ((a)->cnt == (b)->cnt && (a)->level < (b)->level))
44 SPLAYSORT_INIT(node, freenode_p, freenode_lt)
45
46 /* Memory pool, similar to the one in bam_pileup.c */
47 typedef struct {
48 int cnt, n, max;
49 freenode_t **buf;
50 } mempool_t;
51
52 static mempool_t *mp_init(void)
53 {
54 return (mempool_t*)calloc(1, sizeof(mempool_t));
55 }
56 static void mp_destroy(mempool_t *mp)
57 {
58 int k;
59 for (k = 0; k < mp->n; ++k) free(mp->buf[k]);
60 free(mp->buf); free(mp);
61 }
62 static inline freenode_t *mp_alloc(mempool_t *mp)
63 {
64 ++mp->cnt;
65 if (mp->n == 0) return (freenode_t*)calloc(1, sizeof(freenode_t));
66 else return mp->buf[--mp->n];
67 }
68 static inline void mp_free(mempool_t *mp, freenode_t *p)
69 {
70 --mp->cnt; p->next = 0; p->cnt = TV_GAP;
71 if (mp->n == mp->max) {
72 mp->max = mp->max? mp->max<<1 : 256;
73 mp->buf = (freenode_t**)realloc(mp->buf, sizeof(freenode_t*) * mp->max);
74 }
75 mp->buf[mp->n++] = p;
76 }
77
78 /* core part */
79 struct __bam_lplbuf_t {
80 int max, n_cur, n_pre;
81 int max_level, *cur_level, *pre_level;
82 mempool_t *mp;
83 freenode_t **aux, *head, *tail;
84 int n_nodes, m_aux;
85 bam_pileup_f func;
86 void *user_data;
87 bam_plbuf_t *plbuf;
88 };
89
90 void bam_lplbuf_reset(bam_lplbuf_t *buf)
91 {
92 freenode_t *p, *q;
93 bam_plbuf_reset(buf->plbuf);
94 for (p = buf->head; p->next;) {
95 q = p->next;
96 mp_free(buf->mp, p);
97 p = q;
98 }
99 buf->head = buf->tail;
100 buf->max_level = 0;
101 buf->n_cur = buf->n_pre = 0;
102 buf->n_nodes = 0;
103 }
104
105 static int tview_func(uint32_t tid, hts_pos_t pos, int n, const bam_pileup1_t *pl, void *data)
106 {
107 bam_lplbuf_t *tv = (bam_lplbuf_t*)data;
108 freenode_t *p;
109 int i, l, max_level;
110 // allocate memory if necessary
111 if (tv->max < n) { // enlarge
112 tv->max = n;
113 kroundup32(tv->max);
114 tv->cur_level = (int*)realloc(tv->cur_level, sizeof(int) * tv->max);
115 tv->pre_level = (int*)realloc(tv->pre_level, sizeof(int) * tv->max);
116 }
117 tv->n_cur = n;
118 // update cnt
119 for (p = tv->head; p->next; p = p->next)
120 if (p->cnt > 0) --p->cnt;
121 // calculate cur_level[]
122 max_level = 0;
123 for (i = l = 0; i < n; ++i) {
124 const bam_pileup1_t *p = pl + i;
125 if (p->is_head) {
126 if (tv->head->next && tv->head->cnt == 0) { // then take a free slot
127 freenode_t *p = tv->head->next;
128 tv->cur_level[i] = tv->head->level;
129 mp_free(tv->mp, tv->head);
130 tv->head = p;
131 --tv->n_nodes;
132 } else tv->cur_level[i] = ++tv->max_level;
133 } else {
134 tv->cur_level[i] = tv->pre_level[l++];
135 if (p->is_tail) { // then return a free slot
136 tv->tail->level = tv->cur_level[i];
137 tv->tail->next = mp_alloc(tv->mp);
138 tv->tail = tv->tail->next;
139 ++tv->n_nodes;
140 }
141 }
142 if (tv->cur_level[i] > max_level) max_level = tv->cur_level[i];
143 ((bam_pileup1_t*)p)->level = tv->cur_level[i];
144 }
145 assert(l == tv->n_pre);
146 tv->func(tid, pos, n, pl, tv->user_data);
147 // sort the linked list
148 if (tv->n_nodes) {
149 freenode_t *q;
150 if (tv->n_nodes + 1 > tv->m_aux) { // enlarge
151 tv->m_aux = tv->n_nodes + 1;
152 kroundup32(tv->m_aux);
153 tv->aux = (freenode_t**)realloc(tv->aux, sizeof(freenode_t*) * tv->m_aux);
154 }
155 for (p = tv->head, i = l = 0; p->next;) {
156 if (p->level > max_level) { // then discard this entry
157 q = p->next;
158 mp_free(tv->mp, p);
159 p = q;
160 } else {
161 tv->aux[i++] = p;
162 p = p->next;
163 }
164 }
165 tv->aux[i] = tv->tail; // add a proper tail for the loop below
166 tv->n_nodes = i;
167 if (tv->n_nodes) {
168 splaysort(node, tv->n_nodes, tv->aux);
169 for (i = 0; i < tv->n_nodes; ++i) tv->aux[i]->next = tv->aux[i+1];
170 tv->head = tv->aux[0];
171 } else tv->head = tv->tail;
172 }
173 // clean up
174 tv->max_level = max_level;
175 memcpy(tv->pre_level, tv->cur_level, tv->n_cur * 4);
176 // squeeze out terminated levels
177 for (i = l = 0; i < n; ++i) {
178 const bam_pileup1_t *p = pl + i;
179 if (!p->is_tail)
180 tv->pre_level[l++] = tv->pre_level[i];
181 }
182 tv->n_pre = l;
183 /*
184 fprintf(samtools_stderr, "%d\t", pos+1);
185 for (i = 0; i < n; ++i) {
186 const bam_pileup1_t *p = pl + i;
187 if (p->is_head) fprintf(samtools_stderr, "^");
188 if (p->is_tail) fprintf(samtools_stderr, "$");
189 fprintf(samtools_stderr, "%d,", p->level);
190 }
191 fprintf(samtools_stderr, "\n");
192 */
193 return 0;
194 }
195
196 bam_lplbuf_t *bam_lplbuf_init(bam_pileup_f func, void *data)
197 {
198 bam_lplbuf_t *tv;
199 tv = (bam_lplbuf_t*)calloc(1, sizeof(bam_lplbuf_t));
200 tv->mp = mp_init();
201 tv->head = tv->tail = mp_alloc(tv->mp);
202 tv->func = func;
203 tv->user_data = data;
204 tv->plbuf = bam_plbuf_init(tview_func, tv);
205 return (bam_lplbuf_t*)tv;
206 }
207
208 void bam_lplbuf_destroy(bam_lplbuf_t *tv)
209 {
210 freenode_t *p, *q;
211 free(tv->cur_level); free(tv->pre_level);
212 bam_plbuf_destroy(tv->plbuf);
213 free(tv->aux);
214 for (p = tv->head; p->next;) {
215 q = p->next;
216 mp_free(tv->mp, p); p = q;
217 }
218 mp_free(tv->mp, p);
219 assert(tv->mp->cnt == 0);
220 mp_destroy(tv->mp);
221 free(tv);
222 }
223
224 int bam_lplbuf_push(const bam1_t *b, bam_lplbuf_t *tv)
225 {
226 return bam_plbuf_push(b, tv->plbuf);
227 }