forked from samtools/samtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bam_lpileup.c
199 lines (188 loc) · 4.91 KB
/
bam_lpileup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "bam_plbuf.h"
#include "bam_lpileup.h"
#include <htslib/ksort.h>
#define TV_GAP 2
typedef struct __freenode_t {
uint32_t level:28, cnt:4;
struct __freenode_t *next;
} freenode_t, *freenode_p;
#define freenode_lt(a,b) ((a)->cnt < (b)->cnt || ((a)->cnt == (b)->cnt && (a)->level < (b)->level))
KSORT_INIT(node, freenode_p, freenode_lt)
/* Memory pool, similar to the one in bam_pileup.c */
typedef struct {
int cnt, n, max;
freenode_t **buf;
} mempool_t;
static mempool_t *mp_init(void)
{
return (mempool_t*)calloc(1, sizeof(mempool_t));
}
static void mp_destroy(mempool_t *mp)
{
int k;
for (k = 0; k < mp->n; ++k) free(mp->buf[k]);
free(mp->buf); free(mp);
}
static inline freenode_t *mp_alloc(mempool_t *mp)
{
++mp->cnt;
if (mp->n == 0) return (freenode_t*)calloc(1, sizeof(freenode_t));
else return mp->buf[--mp->n];
}
static inline void mp_free(mempool_t *mp, freenode_t *p)
{
--mp->cnt; p->next = 0; p->cnt = TV_GAP;
if (mp->n == mp->max) {
mp->max = mp->max? mp->max<<1 : 256;
mp->buf = (freenode_t**)realloc(mp->buf, sizeof(freenode_t*) * mp->max);
}
mp->buf[mp->n++] = p;
}
/* core part */
struct __bam_lplbuf_t {
int max, n_cur, n_pre;
int max_level, *cur_level, *pre_level;
mempool_t *mp;
freenode_t **aux, *head, *tail;
int n_nodes, m_aux;
bam_pileup_f func;
void *user_data;
bam_plbuf_t *plbuf;
};
void bam_lplbuf_reset(bam_lplbuf_t *buf)
{
freenode_t *p, *q;
bam_plbuf_reset(buf->plbuf);
for (p = buf->head; p->next;) {
q = p->next;
mp_free(buf->mp, p);
p = q;
}
buf->head = buf->tail;
buf->max_level = 0;
buf->n_cur = buf->n_pre = 0;
buf->n_nodes = 0;
}
static int tview_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl, void *data)
{
bam_lplbuf_t *tv = (bam_lplbuf_t*)data;
freenode_t *p;
int i, l, max_level;
// allocate memory if necessary
if (tv->max < n) { // enlarge
tv->max = n;
kroundup32(tv->max);
tv->cur_level = (int*)realloc(tv->cur_level, sizeof(int) * tv->max);
tv->pre_level = (int*)realloc(tv->pre_level, sizeof(int) * tv->max);
}
tv->n_cur = n;
// update cnt
for (p = tv->head; p->next; p = p->next)
if (p->cnt > 0) --p->cnt;
// calculate cur_level[]
max_level = 0;
for (i = l = 0; i < n; ++i) {
const bam_pileup1_t *p = pl + i;
if (p->is_head) {
if (tv->head->next && tv->head->cnt == 0) { // then take a free slot
freenode_t *p = tv->head->next;
tv->cur_level[i] = tv->head->level;
mp_free(tv->mp, tv->head);
tv->head = p;
--tv->n_nodes;
} else tv->cur_level[i] = ++tv->max_level;
} else {
tv->cur_level[i] = tv->pre_level[l++];
if (p->is_tail) { // then return a free slot
tv->tail->level = tv->cur_level[i];
tv->tail->next = mp_alloc(tv->mp);
tv->tail = tv->tail->next;
++tv->n_nodes;
}
}
if (tv->cur_level[i] > max_level) max_level = tv->cur_level[i];
((bam_pileup1_t*)p)->level = tv->cur_level[i];
}
assert(l == tv->n_pre);
tv->func(tid, pos, n, pl, tv->user_data);
// sort the linked list
if (tv->n_nodes) {
freenode_t *q;
if (tv->n_nodes + 1 > tv->m_aux) { // enlarge
tv->m_aux = tv->n_nodes + 1;
kroundup32(tv->m_aux);
tv->aux = (freenode_t**)realloc(tv->aux, sizeof(freenode_t*) * tv->m_aux);
}
for (p = tv->head, i = l = 0; p->next;) {
if (p->level > max_level) { // then discard this entry
q = p->next;
mp_free(tv->mp, p);
p = q;
} else {
tv->aux[i++] = p;
p = p->next;
}
}
tv->aux[i] = tv->tail; // add a proper tail for the loop below
tv->n_nodes = i;
if (tv->n_nodes) {
ks_introsort(node, tv->n_nodes, tv->aux);
for (i = 0; i < tv->n_nodes; ++i) tv->aux[i]->next = tv->aux[i+1];
tv->head = tv->aux[0];
} else tv->head = tv->tail;
}
// clean up
tv->max_level = max_level;
memcpy(tv->pre_level, tv->cur_level, tv->n_cur * 4);
// squeeze out terminated levels
for (i = l = 0; i < n; ++i) {
const bam_pileup1_t *p = pl + i;
if (!p->is_tail)
tv->pre_level[l++] = tv->pre_level[i];
}
tv->n_pre = l;
/*
fprintf(stderr, "%d\t", pos+1);
for (i = 0; i < n; ++i) {
const bam_pileup1_t *p = pl + i;
if (p->is_head) fprintf(stderr, "^");
if (p->is_tail) fprintf(stderr, "$");
fprintf(stderr, "%d,", p->level);
}
fprintf(stderr, "\n");
*/
return 0;
}
bam_lplbuf_t *bam_lplbuf_init(bam_pileup_f func, void *data)
{
bam_lplbuf_t *tv;
tv = (bam_lplbuf_t*)calloc(1, sizeof(bam_lplbuf_t));
tv->mp = mp_init();
tv->head = tv->tail = mp_alloc(tv->mp);
tv->func = func;
tv->user_data = data;
tv->plbuf = bam_plbuf_init(tview_func, tv);
return (bam_lplbuf_t*)tv;
}
void bam_lplbuf_destroy(bam_lplbuf_t *tv)
{
freenode_t *p, *q;
free(tv->cur_level); free(tv->pre_level);
bam_plbuf_destroy(tv->plbuf);
free(tv->aux);
for (p = tv->head; p->next;) {
q = p->next;
mp_free(tv->mp, p); p = q;
}
mp_free(tv->mp, p);
assert(tv->mp->cnt == 0);
mp_destroy(tv->mp);
free(tv);
}
int bam_lplbuf_push(const bam1_t *b, bam_lplbuf_t *tv)
{
return bam_plbuf_push(b, tv->plbuf);
}