annotate pyPRADA_1.2/tools/bwa-0.5.7-mh/bwt_lite.c @ 3:f17965495ec9 draft default tip

Uploaded
author siyuan
date Tue, 11 Mar 2014 12:14:01 -0400
parents acc2ca1a3ba4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #include <stdio.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include "bwt_lite.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 int is_sa(const uint8_t *T, uint32_t *SA, int n);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 int is_bwt(uint8_t *T, int n);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 bwtl_t *bwtl_seq2bwtl(int len, const uint8_t *seq)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 bwtl_t *b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 b = (bwtl_t*)calloc(1, sizeof(bwtl_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 b->seq_len = len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 { // calculate b->bwt
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 uint8_t *s;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 b->sa = (uint32_t*)calloc(len + 1, 4);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 is_sa(seq, b->sa, len);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 s = (uint8_t*)calloc(len + 1, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 for (i = 0; i <= len; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 if (b->sa[i] == 0) b->primary = i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 else s[i] = seq[b->sa[i] - 1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 for (i = b->primary; i < len; ++i) s[i] = s[i + 1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 b->bwt_size = (len + 15) / 16;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 b->bwt = (uint32_t*)calloc(b->bwt_size, 4);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 for (i = 0; i < len; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 b->bwt[i>>4] |= s[i] << ((15 - (i&15)) << 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 free(s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 { // calculate b->occ
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 uint32_t c[4];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 b->n_occ = (len + 15) / 16 * 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 b->occ = (uint32_t*)calloc(b->n_occ, 4);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 memset(c, 0, 16);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 for (i = 0; i < len; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 if (i % 16 == 0)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 memcpy(b->occ + (i/16) * 4, c, 16);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 ++c[bwtl_B0(b, i)];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 memcpy(b->L2+1, c, 16);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 for (i = 2; i < 5; ++i) b->L2[i] += b->L2[i-1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 { // generate cnt_table
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 for (i = 0; i != 256; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 u_int32_t j, x = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 for (j = 0; j != 4; ++j)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 x |= (((i&3) == j) + ((i>>2&3) == j) + ((i>>4&3) == j) + (i>>6 == j)) << (j<<3);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 b->cnt_table[i] = x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 return b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 inline uint32_t bwtl_occ(const bwtl_t *bwt, uint32_t k, uint8_t c)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 uint32_t n, b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 if (k == bwt->seq_len) return bwt->L2[c+1] - bwt->L2[c];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 if (k == (uint32_t)(-1)) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 if (k >= bwt->primary) --k; // because $ is not in bwt
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 n = bwt->occ[k/16<<2|c];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 b = bwt->bwt[k/16] & ~((1U<<((15-(k&15))<<1)) - 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 n += (bwt->cnt_table[b&0xff] + bwt->cnt_table[b>>8&0xff]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 + bwt->cnt_table[b>>16&0xff] + bwt->cnt_table[b>>24]) >> (c<<3) & 0xff;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 if (c == 0) n -= 15 - (k&15); // corrected for the masked bits
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 return n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 inline void bwtl_occ4(const bwtl_t *bwt, uint32_t k, uint32_t cnt[4])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 uint32_t x, b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 if (k == (uint32_t)(-1)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 memset(cnt, 0, 16);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 if (k >= bwt->primary) --k; // because $ is not in bwt
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 memcpy(cnt, bwt->occ + (k>>4<<2), 16);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 b = bwt->bwt[k>>4] & ~((1U<<((~k&15)<<1)) - 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 x = bwt->cnt_table[b&0xff] + bwt->cnt_table[b>>8&0xff]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 + bwt->cnt_table[b>>16&0xff] + bwt->cnt_table[b>>24];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 x -= 15 - (k&15);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 cnt[0] += x&0xff; cnt[1] += x>>8&0xff; cnt[2] += x>>16&0xff; cnt[3] += x>>24;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 inline void bwtl_2occ4(const bwtl_t *bwt, uint32_t k, uint32_t l, uint32_t cntk[4], uint32_t cntl[4])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 bwtl_occ4(bwt, k, cntk);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 bwtl_occ4(bwt, l, cntl);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 void bwtl_destroy(bwtl_t *bwt)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 if (bwt) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 free(bwt->occ); free(bwt->bwt); free(bwt->sa);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 free(bwt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 }