forked from ruanjue/wtdbg2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kbmpoa.h
409 lines (398 loc) · 12 KB
/
kbmpoa.h
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
*
* Copyright (c) 2018, Jue Ruan <[email protected]>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KMER_BIN_MAP_PO_MSA_CNS_RJ_H
#define KMER_BIN_MAP_PO_MSA_CNS_RJ_H
#include "kbm.h"
#include "wtpoa.h"
typedef struct {
u4i chridx, cidx, bidx;
String *rdtag;
BaseBank *rdseq;
u8i rdoff;
u4i rdlen;
KBMAux *aux;
layseqr *seqs;
u4v *heap;
u2i bsize, bstep; // block size, and slide steps
u4i sidx, lidx, hidx;
} KBMBlock;
static inline KBMBlock* init_kbmblock(u2i bsize, u2i bstep){
KBMBlock *kb;
kb = malloc(sizeof(KBMBlock));
kb->rdtag = init_string(1024);
kb->rdseq = NULL;
kb->rdoff = 0;
kb->rdlen = 0;
kb->aux = NULL;
kb->seqs = init_layseqr(32);
kb->heap = init_u4v(32);
bsize = ((bsize + KBM_BIN_SIZE - 1) / KBM_BIN_SIZE) * KBM_BIN_SIZE;
if(bsize == 0) bsize = KBM_BIN_SIZE;
bstep = ((bstep + KBM_BIN_SIZE - 1) / KBM_BIN_SIZE) * KBM_BIN_SIZE;
if(bstep == 0) bstep = KBM_BIN_SIZE;
kb->bsize = bsize;
kb->bstep = bstep;
kb->chridx = 0;
kb->cidx = 0;
kb->bidx = 0;
kb->sidx = MAX_U4;
kb->lidx = MAX_U4;
kb->hidx = 0;
return kb;
}
static inline void free_kbmblock(KBMBlock *kb){
free_string(kb->rdtag);
free_layseqr(kb->seqs);
free(kb);
}
static inline void reset_kbmblock(KBMBlock *kb, char *rdtag, u4i chridx, BaseBank *rdseq, u8i rdoff, u4i rdlen, KBMAux *aux){
lay_seq_t *stop;
kb->chridx = chridx;
kb->cidx = chridx;
kb->bidx = 0;
clear_string(kb->rdtag);
if(rdtag){
append_string(kb->rdtag, rdtag, strlen(rdtag));
} else {
append_string(kb->rdtag, "anonymous", strlen("anonymous"));
}
kb->rdseq = rdseq;
kb->rdoff = rdoff;
kb->rdlen = rdlen;
kb->aux = aux;
recyc_all_layseqr(kb->seqs);
clear_u4v(kb->heap);
kb->sidx = MAX_U4;
kb->lidx = MAX_U4;
sort_array(aux->hits->buffer, aux->hits->size, kbm_map_t, num_cmpgt(a.qb, b.qb));
kb->hidx = 0;
stop = pop_layseqr(kb->seqs);
stop->chridx = chridx + 1;
stop->bidx = 0;
}
static inline lay_seq_t* _push_padding_ref_kbmblock(KBMBlock *kb, lay_seq_t *sq){
lay_seq_t *st;
u8i off;
u4i sqidx, len;
sqidx = offset_layseqr(kb->seqs, sq);
while(kb->chridx == sq->chridx && kb->bidx <= sq->bidx){
st = pop_layseqr(kb->seqs);
st->chridx = kb->cidx;
st->bidx = kb->bidx;
st->rdidx = 0;
st->rddir = 0;
st->rdoff = st->bidx * kb->bstep;
st->rbeg = st->rdoff;
off = kb->rdoff;
len = kb->rdlen;
st->rend = num_min(st->rbeg + kb->bsize, len);
st->rdtag = kb->rdtag->string;
clear_basebank(st->seq);
fast_fwdbits2basebank(st->seq, kb->rdseq->bits, off + st->rdoff, st->rend - st->rbeg);
array_heap_push(kb->heap->buffer, kb->heap->size, kb->heap->cap, u4i, offset_layseqr(kb->seqs, st),
num_cmpx(ref_layseqr(kb->seqs, a)->bidx, ref_layseqr(kb->seqs, b)->bidx, ref_layseqr(kb->seqs, a)->rdidx, ref_layseqr(kb->seqs, b)->rdidx));
if(st->rend >= len){
kb->chridx = MAX_U4;
kb->bidx = 0;
break;
} else {
kb->bidx ++;
}
sq = ref_layseqr(kb->seqs, sqidx);
}
sq = ref_layseqr(kb->seqs, sqidx);
return sq;
}
static inline lay_seq_t* iter_kbmblock(void *obj){
KBMBlock *kb;
kbm_map_t *hit;
lay_seq_t *sc, *sl;
u8i coff, tsoff;
u4i off, rdoff, rdlen, nxt, val, len, clen, bt;
kb = (KBMBlock*)obj;
if(kb->sidx != MAX_U4){
recyc_layseqr(kb->seqs, kb->sidx);
kb->sidx = MAX_U4;
}
sc = NULL;
sl = NULL;
do {
if(kb->heap->size == 0){
kb->lidx = MAX_U4;
}
while(kb->lidx == MAX_U4){
while(kb->hidx < kb->aux->hits->size){
if(kb->aux->hits->buffer[kb->hidx].mat == 0) kb->hidx ++;
else break;
}
if(kb->hidx >= kb->aux->hits->size){
sc = ref_layseqr(kb->seqs, 0);
sc = _push_padding_ref_kbmblock(kb, sc);
break;
}
hit = ref_kbmmapv(kb->aux->hits, kb->hidx ++);
tsoff = kb->aux->kbm->reads->buffer[hit->tidx].rdoff;
rdlen = kb->aux->kbm->reads->buffer[hit->tidx].rdlen;
off = hit->qb;
rdoff = hit->qdir? Int(rdlen - hit->te) : hit->tb;
{
encap_layseqr(kb->seqs, 2);
sc = pop_layseqr(kb->seqs);
sc->chridx = kb->cidx;
sc->bidx = (off / kb->bstep);
sc->rdidx = hit->tidx;
sc->rddir = hit->qdir;
sc->rdoff = rdoff;
sc->rbeg = off;
sc->rend = 0;
sc->rdtag = kb->aux->kbm->reads->buffer[hit->tidx].tag;
clear_basebank(sc->seq);
}
sl = NULL;
{
nxt = (off / kb->bstep) * kb->bstep;
if(nxt && off - nxt < UInt(kb->bsize - kb->bstep)){
if(sc->bidx){
sl = pop_layseqr(kb->seqs);
sl->chridx = kb->cidx;
sl->bidx = sc->bidx - 1;
sl->rdidx = hit->tidx;
sl->rddir = hit->qdir;
sl->rdoff = rdoff;
sl->rbeg = off;
sl->rend = 0;
sl->rdtag = kb->aux->kbm->reads->buffer[hit->tidx].tag;
clear_basebank(sl->seq);
}
nxt += kb->bsize - kb->bstep;
} else {
nxt += kb->bstep;
}
}
coff = hit->cgoff;
clen = hit->cglen;
while(clen){
bt = get_bitsvec(kb->aux->cigars, coff + clen - 1);
clen --;
val = KBM_BIN_SIZE;
bt = (bt & 0x03)? (bt & 0x03) : 3;
while(val){
if(bt & 0b001){
len = num_min(val, nxt - off);
off += len;
} else {
len = val;
}
val -= len;
if(bt & 0b010){
{
if(sc){
if(sc->seq->size == 0){
sc->rbeg = (bt & 0b001)? off - len : off;
}
if(hit->qdir){
revbits2basebank(sc->seq, kb->aux->kbm->rdseqs->bits, tsoff + rdlen - (rdoff + len), len);
} else {
fwdbits2basebank(sc->seq, kb->aux->kbm->rdseqs->bits, tsoff + rdoff, len);
}
sc->rend = off;
}
if(sl){
if(sl->seq->size == 0){
sl->rbeg = (bt & 0b001)? off - len : off;
}
if(hit->qdir){
revbits2basebank(sl->seq, kb->aux->kbm->rdseqs->bits, tsoff + rdlen - (rdoff + len), len);
} else {
fwdbits2basebank(sl->seq, kb->aux->kbm->rdseqs->bits, tsoff + rdoff, len);
}
sl->rend = off;
}
}
rdoff += len;
}
if(off == nxt){
if(sl){
if(sl->rend > sl->rbeg){
u4i scidx;
scidx = offset_layseqr(kb->seqs, sc);
sl = _push_padding_ref_kbmblock(kb, sl);
sc = ref_layseqr(kb->seqs, scidx);
if(kb->lidx == MAX_U4){
kb->lidx = offset_layseqr(kb->seqs, sl);
}
array_heap_push(kb->heap->buffer, kb->heap->size, kb->heap->cap, u4i, offset_layseqr(kb->seqs, sl),
num_cmpx(ref_layseqr(kb->seqs, a)->bidx, ref_layseqr(kb->seqs, b)->bidx, ref_layseqr(kb->seqs, a)->rdidx, ref_layseqr(kb->seqs, b)->rdidx));
}
sl = NULL;
nxt += 2 * kb->bstep - kb->bsize;
} else {
u4i scidx;
scidx = offset_layseqr(kb->seqs, sc);
encap_layseqr(kb->seqs, 1);
sl = ref_layseqr(kb->seqs, scidx);
sc = pop_layseqr(kb->seqs);
sc->chridx = kb->cidx;
sc->bidx = sl->bidx + 1;
sc->rdidx = hit->tidx;
sc->rddir = hit->qdir;
sc->rdoff = rdoff;
sc->rbeg = off;
sc->rend = 0;
sc->rdtag = kb->aux->kbm->reads->buffer[hit->tidx].tag;
clear_basebank(sc->seq);
nxt += kb->bsize - kb->bstep;
}
}
}
}
if(sl && sl->rend > sl->rbeg){
u4i scidx;
scidx = offset_layseqr(kb->seqs, sc);
sl = _push_padding_ref_kbmblock(kb, sl);
sc = ref_layseqr(kb->seqs, scidx);
if(kb->lidx == MAX_U4){
kb->lidx = offset_layseqr(kb->seqs, sl);
}
array_heap_push(kb->heap->buffer, kb->heap->size, kb->heap->cap, u4i, offset_layseqr(kb->seqs, sl),
num_cmpx(ref_layseqr(kb->seqs, a)->bidx, ref_layseqr(kb->seqs, b)->bidx, ref_layseqr(kb->seqs, a)->rdidx, ref_layseqr(kb->seqs, b)->rdidx));
}
if(sc && sc->rend > sc->rbeg){
u4i scidx;
scidx = offset_layseqr(kb->seqs, sl);
sc = _push_padding_ref_kbmblock(kb, sc);
sl = ref_layseqr(kb->seqs, scidx);
if(kb->lidx == MAX_U4){
kb->lidx = offset_layseqr(kb->seqs, sc);
}
array_heap_push(kb->heap->buffer, kb->heap->size, kb->heap->cap, u4i, offset_layseqr(kb->seqs, sc),
num_cmpx(ref_layseqr(kb->seqs, a)->bidx, ref_layseqr(kb->seqs, b)->bidx, ref_layseqr(kb->seqs, a)->rdidx, ref_layseqr(kb->seqs, b)->rdidx));
}
}
if(kb->heap->size == 0) break;
if(kb->lidx != MAX_U4){
sl = ref_layseqr(kb->seqs, kb->lidx);
kb->sidx = kb->heap->buffer[0];
sc = ref_layseqr(kb->seqs, kb->sidx);
if(sc->chridx == sl->chridx && sc->bidx + 1 >= sl->bidx){
kb->lidx = MAX_U4;
continue;
}
array_heap_remove(kb->heap->buffer, kb->heap->size, kb->heap->cap, u4i, 0,
num_cmpx(ref_layseqr(kb->seqs, a)->bidx, ref_layseqr(kb->seqs, b)->bidx, ref_layseqr(kb->seqs, a)->rdidx, ref_layseqr(kb->seqs, b)->rdidx));
sc->rbeg -= sc->bidx * kb->bstep;
sc->rend -= sc->bidx * kb->bstep;
} else {
kb->sidx = kb->heap->buffer[0];
array_heap_remove(kb->heap->buffer, kb->heap->size, kb->heap->cap, u4i, 0,
num_cmpx(ref_layseqr(kb->seqs, a)->bidx, ref_layseqr(kb->seqs, b)->bidx, ref_layseqr(kb->seqs, a)->rdidx, ref_layseqr(kb->seqs, b)->rdidx));
sc = ref_layseqr(kb->seqs, kb->sidx);
sc->rbeg -= sc->bidx * kb->bstep;
sc->rend -= sc->bidx * kb->bstep;
}
return sc;
} while(1);
kb->sidx = MAX_U4;
return NULL;
}
static inline void info_kbmblock(void *obj, lay_seq_t *sq, lay_blk_t *bk){
KBMBlock *kb;
kb = (KBMBlock*)obj;
if(sq == NULL) return;
bk->node1 = sq->bidx;
bk->node2 = sq->bidx + 1;
bk->reftag = kb->rdtag->string;
bk->reflen = kb->rdlen;
bk->refoff = sq->bidx * kb->bstep;
}
static inline int map_kbmpoa(CTGCNS *cc, KBMAux *aux, char *rdtag, u4i qidx, BaseBank *rdseq, u8i seqoff, u4i seqlen, u4i corr_min, u4i corr_max, float corr_cov, FILE *layf){
KBMBlock *kb;
u4i i;
int self_aln, max_hit, min_aln, min_mat;
kb = (KBMBlock*)cc->obj;
reset_ctgcns(cc, kb, iter_kbmblock, info_kbmblock);
seqlen = cvt_kbm_read_length(seqlen);
if(seqlen < 4 * KBM_BIN_SIZE + UInt(aux->par->min_aln)) return 0;
if(rdseq && rdseq != aux->kbm->rdseqs){
self_aln = 0;
} else {
self_aln = 1;
rdseq = aux->kbm->rdseqs;
}
max_hit = aux->par->max_hit;
min_aln = aux->par->min_aln;
min_mat = aux->par->min_mat;
aux->par->self_aln = 0;
aux->par->max_hit = corr_max;
aux->par->min_aln = num_max(seqlen * corr_cov, min_aln);
aux->par->min_mat = num_max(aux->par->min_aln * aux->par->min_sim, min_mat);
query_index_kbm(aux, rdtag, qidx, rdseq, seqoff, seqlen);
map_kbm(aux);
aux->par->self_aln = 0;
aux->par->max_hit = max_hit;
aux->par->min_aln = min_aln;
aux->par->min_mat = min_mat;
if(KBM_LOG){
fprintf(KBM_LOGF, ">>> Contained alignments\n"); fflush(KBM_LOGF);
for(i=0;i<aux->hits->size;i++){
fprint_hit_kbm(aux, i, KBM_LOGF);
}
}
if(aux->hits->size < corr_min){
return 0;
}
if(self_aln){
for(i=0;i<aux->hits->size;i++){
if(aux->hits->buffer[i].tidx == qidx){
aux->hits->buffer[i].mat = 0;
}
}
}
reset_kbmblock(kb, rdtag, qidx, rdseq, seqoff, seqlen, aux);
if(layf){
print_lays_ctgcns(cc, layf);
fflush(layf);
reset_kbmblock(kb, rdtag, qidx, rdseq, seqoff, seqlen, aux);
reset_ctgcns(cc, kb, iter_kbmblock, info_kbmblock);
}
if(iter_ctgcns(cc)){
if(KBM_LOG){
fprintf(KBM_LOGF, ">%s corrected\n", cc->tag->string);
print_lines_basebank(cc->cns, 0, cc->cns->size, KBM_LOGF, 100);
fflush(KBM_LOGF);
}
} else {
return 0;
}
clear_kbmmapv(aux->hits);
if(cc->cns->size > seqlen){
cc->cns->size = seqlen;
normalize_basebank(cc->cns);
} else if(cc->cns->size < seqlen){
cc->cns->size = cvt_kbm_read_length(cc->cns->size);
normalize_basebank(cc->cns);
}
if(cc->cns->size == 0){
return 0;
}
query_index_kbm(aux, rdtag, qidx, cc->cns, 0, cc->cns->size);
map_kbm(aux);
return 1;
}
#endif