minibwa 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +119 -0
- data/ext/minibwa/extconf.rb +105 -0
- data/ext/minibwa/mb_buffer.c +113 -0
- data/ext/minibwa/mb_hit.c +192 -0
- data/ext/minibwa/mb_index.c +462 -0
- data/ext/minibwa/mb_index_build.c +174 -0
- data/ext/minibwa/mb_options.c +301 -0
- data/ext/minibwa/minibwa/LICENSE.txt +37 -0
- data/ext/minibwa/minibwa/align.c +930 -0
- data/ext/minibwa/minibwa/bseq.h +45 -0
- data/ext/minibwa/minibwa/bwt.c +715 -0
- data/ext/minibwa/minibwa/bwt.h +86 -0
- data/ext/minibwa/minibwa/cs.c +161 -0
- data/ext/minibwa/minibwa/format.c +356 -0
- data/ext/minibwa/minibwa/index.c +342 -0
- data/ext/minibwa/minibwa/kalloc.c +224 -0
- data/ext/minibwa/minibwa/kalloc.h +54 -0
- data/ext/minibwa/minibwa/ketopt.h +123 -0
- data/ext/minibwa/minibwa/kommon.c +374 -0
- data/ext/minibwa/minibwa/kommon.h +85 -0
- data/ext/minibwa/minibwa/kseq.h +256 -0
- data/ext/minibwa/minibwa/ksort.h +163 -0
- data/ext/minibwa/minibwa/ksw2.h +220 -0
- data/ext/minibwa/minibwa/ksw2_extd2_sse.c +403 -0
- data/ext/minibwa/minibwa/ksw2_extz2_sse.c +296 -0
- data/ext/minibwa/minibwa/ksw2_ll_sse.c +341 -0
- data/ext/minibwa/minibwa/kthread.h +15 -0
- data/ext/minibwa/minibwa/l2bit.c +479 -0
- data/ext/minibwa/minibwa/l2bit.h +72 -0
- data/ext/minibwa/minibwa/lchain.c +231 -0
- data/ext/minibwa/minibwa/libsais.c +6985 -0
- data/ext/minibwa/minibwa/libsais.h +106 -0
- data/ext/minibwa/minibwa/libsais64.c +7064 -0
- data/ext/minibwa/minibwa/libsais64.h +81 -0
- data/ext/minibwa/minibwa/map-algo.c +769 -0
- data/ext/minibwa/minibwa/mbpriv.h +148 -0
- data/ext/minibwa/minibwa/minibwa.h +176 -0
- data/ext/minibwa/minibwa/options.c +116 -0
- data/ext/minibwa/minibwa/pe.c +559 -0
- data/ext/minibwa/minibwa/s2n-lite.h +59 -0
- data/ext/minibwa/minibwa/seed.c +354 -0
- data/ext/minibwa/minibwa.c +67 -0
- data/ext/minibwa/minibwa.h +51 -0
- data/lib/minibwa/hit.rb +110 -0
- data/lib/minibwa/index.rb +77 -0
- data/lib/minibwa/options.rb +235 -0
- data/lib/minibwa/sam.rb +85 -0
- data/lib/minibwa/version.rb +6 -0
- data/lib/minibwa.rb +11 -0
- metadata +88 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
#include <stdlib.h>
|
|
2
|
+
#include <assert.h>
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include "libsais.h"
|
|
5
|
+
#include "libsais64.h"
|
|
6
|
+
#include "kommon.h"
|
|
7
|
+
#include "ketopt.h"
|
|
8
|
+
#include "mbpriv.h"
|
|
9
|
+
|
|
10
|
+
void mb_bwtgen(const char *fn_pac, const char *fn_bwt, int block_size);
|
|
11
|
+
|
|
12
|
+
static ko_longopt_t long_opts[] = { // common long options shared across all index-related functions
|
|
13
|
+
{ "help", ko_no_argument, 901 },
|
|
14
|
+
{ "meth", ko_no_argument, 902 },
|
|
15
|
+
{ 0, 0, 0 }
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
static inline uint8_t l2b_c2t(uint8_t b) { return b == 1? 3 : b; } // C(1) -> T(3)
|
|
19
|
+
static inline uint8_t l2b_g2a(uint8_t b) { return b == 2? 0 : b; } // G(2) -> A(0)
|
|
20
|
+
|
|
21
|
+
// invert the suffix array a[] (32- or 64-bit) to the BWT in seq[], sample the SSA and drop the primary ($); return the primary
|
|
22
|
+
static int64_t sa_to_bwt(void *a, int use_int32, uint8_t *seq, int64_t len, int sa_bit, uint64_t *ssa)
|
|
23
|
+
{
|
|
24
|
+
int32_t *a32 = a;
|
|
25
|
+
int64_t *a64 = a, i, primary = -1;
|
|
26
|
+
uint64_t mask = (1ULL<<sa_bit) - 1;
|
|
27
|
+
for (i = 0; i <= len; ++i) {
|
|
28
|
+
int64_t v = use_int32? a32[i] : a64[i];
|
|
29
|
+
if ((i & mask) == 0) ssa[i>>sa_bit] = v;
|
|
30
|
+
if (v == 0) primary = i;
|
|
31
|
+
else if (use_int32) a32[i] = seq[v - 1];
|
|
32
|
+
else a64[i] = seq[v - 1];
|
|
33
|
+
}
|
|
34
|
+
ssa[0] = (uint64_t)-1;
|
|
35
|
+
for (i = 0; i < primary; ++i) seq[i] = use_int32? a32[i] : a64[i];
|
|
36
|
+
for (; i < len; ++i) seq[i] = use_int32? a32[i+1] : a64[i+1];
|
|
37
|
+
return primary;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static mb_bwt_t *mb_bwt_libsais(const l2b_t *l2b, int sa_bit, int both_strand, int is_meth, int n_thread)
|
|
41
|
+
{
|
|
42
|
+
const int fs = 10000;
|
|
43
|
+
uint8_t *seq;
|
|
44
|
+
int64_t i, j, primary, len;
|
|
45
|
+
mb_bwt_t *bwt;
|
|
46
|
+
uint64_t *ssa, n_ssa;
|
|
47
|
+
void *a;
|
|
48
|
+
int use_int32;
|
|
49
|
+
|
|
50
|
+
len = l2b->tot_len * (is_meth? 2 : 1) * (both_strand? 2 : 1);
|
|
51
|
+
// use a 32-bit suffix array (half the memory) when the concatenated length fits in int32_t
|
|
52
|
+
use_int32 = (len + fs + 1 <= INT32_MAX);
|
|
53
|
+
seq = kom_malloc(uint8_t, len);
|
|
54
|
+
if (use_int32) a = kom_malloc(int32_t, len + fs + 1);
|
|
55
|
+
else a = kom_malloc(int64_t, len + fs + 1);
|
|
56
|
+
if (is_meth) {
|
|
57
|
+
// c2t forward
|
|
58
|
+
for (i = 0, j = 0; i < l2b->tot_len; ++i, ++j)
|
|
59
|
+
seq[j] = l2b_c2t(l2b_get0(l2b, i));
|
|
60
|
+
// g2a forward
|
|
61
|
+
for (i = 0; i < l2b->tot_len; ++i, ++j)
|
|
62
|
+
seq[j] = l2b_g2a(l2b_get0(l2b, i));
|
|
63
|
+
if (both_strand) {
|
|
64
|
+
// g2a reverse (reverse complement of g2a converted)
|
|
65
|
+
for (i = l2b->tot_len - 1; i >= 0; --i, ++j)
|
|
66
|
+
seq[j] = 3 - l2b_g2a(l2b_get0(l2b, i));
|
|
67
|
+
// c2t reverse (reverse complement of c2t converted)
|
|
68
|
+
for (i = l2b->tot_len - 1; i >= 0; --i, ++j)
|
|
69
|
+
seq[j] = 3 - l2b_c2t(l2b_get0(l2b, i));
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
for (i = 0, j = 0; i < l2b->tot_len; ++i, ++j)
|
|
73
|
+
seq[j] = l2b_get0(l2b, i);
|
|
74
|
+
if (both_strand)
|
|
75
|
+
for (i = l2b->tot_len - 1; i >= 0; --i, ++j)
|
|
76
|
+
seq[j] = 3 - l2b_get0(l2b, i);
|
|
77
|
+
}
|
|
78
|
+
if (use_int32) {
|
|
79
|
+
int32_t *a32 = a;
|
|
80
|
+
#ifdef LIBSAIS_OPENMP
|
|
81
|
+
libsais_omp(seq, a32 + 1, (int32_t)len, fs, 0, n_thread);
|
|
82
|
+
#else
|
|
83
|
+
libsais(seq, a32 + 1, (int32_t)len, fs, 0);
|
|
84
|
+
#endif
|
|
85
|
+
a32[0] = (int32_t)len; // libsais doesn't write a[0], which always equals to len
|
|
86
|
+
} else {
|
|
87
|
+
int64_t *a64 = a;
|
|
88
|
+
#ifdef LIBSAIS_OPENMP
|
|
89
|
+
libsais64_omp(seq, a64 + 1, len, fs, 0, n_thread);
|
|
90
|
+
#else
|
|
91
|
+
libsais64(seq, a64 + 1, len, fs, 0);
|
|
92
|
+
#endif
|
|
93
|
+
a64[0] = len;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
n_ssa = (len + (1<<sa_bit)) >> sa_bit;
|
|
97
|
+
ssa = kom_calloc(uint64_t, n_ssa);
|
|
98
|
+
primary = sa_to_bwt(a, use_int32, seq, len, sa_bit, ssa);
|
|
99
|
+
assert(primary != -1);
|
|
100
|
+
free(a);
|
|
101
|
+
bwt = mb_bwt_init_from_raw(1, seq, len, primary);
|
|
102
|
+
bwt->sa_bit = sa_bit, bwt->n_sa = n_ssa, bwt->sa = ssa;
|
|
103
|
+
free(seq);
|
|
104
|
+
return bwt;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static int usage_fa2bit(FILE *fp, uint64_t seed)
|
|
108
|
+
{
|
|
109
|
+
fprintf(fp, "Usage: minibwa fa2bit [options] <in.fa> <out.l2b>\n");
|
|
110
|
+
fprintf(fp, "Options:\n");
|
|
111
|
+
fprintf(fp, " -s INT random seed [%lu]\n", (unsigned long)seed);
|
|
112
|
+
fprintf(fp, " -p output the BWA pac format\n");
|
|
113
|
+
fprintf(fp, " -2 output both strands (effective with -p)\n");
|
|
114
|
+
fprintf(fp, " --help print this help message\n");
|
|
115
|
+
return fp == stdout? 0 : 1;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
int main_fa2bit(int argc, char *argv[])
|
|
119
|
+
{
|
|
120
|
+
l2b_t *l2b;
|
|
121
|
+
int out_pac = 0, both_strand = 0;
|
|
122
|
+
uint64_t seed = 11;
|
|
123
|
+
ketopt_t o = KETOPT_INIT;
|
|
124
|
+
int c;
|
|
125
|
+
while ((c = ketopt(&o, argc, argv, 1, "s:p2", long_opts)) >= 0) {
|
|
126
|
+
if (c == 's') seed = atol(o.arg);
|
|
127
|
+
else if (c == 'p') out_pac = 1;
|
|
128
|
+
else if (c == '2') both_strand = 1;
|
|
129
|
+
else if (c == 901) return usage_fa2bit(stdout, seed);
|
|
130
|
+
}
|
|
131
|
+
if (argc - o.ind < 2) return usage_fa2bit(stderr, seed);
|
|
132
|
+
l2b = l2b_import(argv[o.ind], seed);
|
|
133
|
+
if (out_pac)
|
|
134
|
+
l2b_save_pac(argv[o.ind+1], l2b, both_strand);
|
|
135
|
+
else
|
|
136
|
+
l2b_save(argv[o.ind+1], l2b);
|
|
137
|
+
l2b_destroy(l2b);
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#ifdef USE_GPL
|
|
142
|
+
static int usage_genraw(FILE *fp)
|
|
143
|
+
{
|
|
144
|
+
fprintf(fp, "Usage: minibwa genraw [options] <in.pac> <out.raw-bwt>\n");
|
|
145
|
+
fprintf(fp, "Options:\n");
|
|
146
|
+
fprintf(fp, " -b NUM block size [10m]\n");
|
|
147
|
+
fprintf(fp, " --help print this help message\n");
|
|
148
|
+
return fp == stdout? 0 : 1;
|
|
149
|
+
}
|
|
150
|
+
#endif
|
|
151
|
+
|
|
152
|
+
int main_genraw(int argc, char *argv[])
|
|
153
|
+
{
|
|
154
|
+
#ifdef USE_GPL
|
|
155
|
+
ketopt_t o = KETOPT_INIT;
|
|
156
|
+
int c, block_size = 10000000;
|
|
157
|
+
while ((c = ketopt(&o, argc, argv, 1, "b:", long_opts)) >= 0) {
|
|
158
|
+
if (c == 'b') block_size = kom_parse_num(o.arg, 0);
|
|
159
|
+
else if (c == 901) return usage_genraw(stdout);
|
|
160
|
+
}
|
|
161
|
+
if (argc - o.ind < 2) return usage_genraw(stderr);
|
|
162
|
+
mb_bwtgen(argv[o.ind], argv[o.ind+1], block_size);
|
|
163
|
+
return 0;
|
|
164
|
+
#else
|
|
165
|
+
(void)argc; (void)argv;
|
|
166
|
+
if (kom_verbose >= 1) fprintf(stderr, "ERROR: genraw not compiled as it depends on GPL'd code\n");
|
|
167
|
+
return 1;
|
|
168
|
+
#endif
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static int usage_raw2bwt(FILE *fp)
|
|
172
|
+
{
|
|
173
|
+
fprintf(fp, "Usage: minibwa raw2bwt <raw.bwt> <recode.bwt>\n");
|
|
174
|
+
fprintf(fp, "Options:\n");
|
|
175
|
+
fprintf(fp, " --help print this help message\n");
|
|
176
|
+
return fp == stdout? 0 : 1;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
int main_raw2bwt(int argc, char *argv[])
|
|
180
|
+
{
|
|
181
|
+
mb_bwt_t *bwt;
|
|
182
|
+
int i;
|
|
183
|
+
for (i = 1; i < argc; ++i)
|
|
184
|
+
if (strcmp(argv[i], "--help") == 0) return usage_raw2bwt(stdout);
|
|
185
|
+
if (argc < 3) return usage_raw2bwt(stderr);
|
|
186
|
+
bwt = mb_bwt_load_raw(argv[1]);
|
|
187
|
+
mb_bwt_save(argv[2], bwt);
|
|
188
|
+
mb_bwt_destroy(bwt);
|
|
189
|
+
return 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static int usage_genbwt(FILE *fp, int sa_bit, int n_thread)
|
|
193
|
+
{
|
|
194
|
+
(void)n_thread;
|
|
195
|
+
fprintf(fp, "Usage: minibwa genbwt [options] <in.l2b> <out.bwt>\n");
|
|
196
|
+
fprintf(fp, "Options:\n");
|
|
197
|
+
fprintf(fp, " -u INT SA sample rate at 1/(1<<INT) [%d]\n", sa_bit);
|
|
198
|
+
fprintf(fp, " -1 forward strand only\n");
|
|
199
|
+
#ifdef LIBSAIS_OPENMP
|
|
200
|
+
fprintf(fp, " -t INT number of threads [%d]\n", n_thread);
|
|
201
|
+
#endif
|
|
202
|
+
fprintf(fp, " --help print this help message\n");
|
|
203
|
+
return fp == stdout? 0 : 1;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
int main_genbwt(int argc, char *argv[])
|
|
207
|
+
{
|
|
208
|
+
ketopt_t o = KETOPT_INIT;
|
|
209
|
+
int c, n_thread = 4, both_strand = 1, sa_bit = 4;
|
|
210
|
+
mb_bwt_t *bwt;
|
|
211
|
+
l2b_t *l2b;
|
|
212
|
+
while ((c = ketopt(&o, argc, argv, 1, "1u:t:", long_opts)) >= 0) {
|
|
213
|
+
if (c == 't') n_thread = atoi(o.arg);
|
|
214
|
+
else if (c == '1') both_strand = 0;
|
|
215
|
+
else if (c == 'u') sa_bit = atoi(o.arg);
|
|
216
|
+
else if (c == 901) return usage_genbwt(stdout, sa_bit, n_thread);
|
|
217
|
+
}
|
|
218
|
+
if (argc - o.ind < 2) return usage_genbwt(stderr, sa_bit, n_thread);
|
|
219
|
+
l2b = l2b_load(argv[o.ind]);
|
|
220
|
+
kom_assert(l2b, "failed to open the input file.");
|
|
221
|
+
bwt = mb_bwt_libsais(l2b, sa_bit, both_strand, 0, n_thread);
|
|
222
|
+
l2b_destroy(l2b);
|
|
223
|
+
mb_bwt_save(argv[o.ind+1], bwt);
|
|
224
|
+
mb_bwt_destroy(bwt);
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
static int usage_gensa(FILE *fp, int sa_bit)
|
|
229
|
+
{
|
|
230
|
+
fprintf(fp, "Usage: minibwa gensa [options] <in.bwt> <out.bwt>\n");
|
|
231
|
+
fprintf(fp, "Options:\n");
|
|
232
|
+
fprintf(fp, " -u INT sample rate at 1/(1<<INT) [%d]\n", sa_bit);
|
|
233
|
+
fprintf(fp, " -r input BWT in the raw BWA format\n");
|
|
234
|
+
fprintf(fp, " --help print this help message\n");
|
|
235
|
+
return fp == stdout? 0 : 1;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
int main_gensa(int argc, char *argv[])
|
|
239
|
+
{
|
|
240
|
+
mb_bwt_t *bwt;
|
|
241
|
+
int c, sa_bit = 4, is_raw = 0;
|
|
242
|
+
ketopt_t o = KETOPT_INIT;
|
|
243
|
+
while ((c = ketopt(&o, argc, argv, 1, "ru:", long_opts)) >= 0) {
|
|
244
|
+
if (c == 'u') sa_bit = atoi(o.arg);
|
|
245
|
+
else if (c == 'r') is_raw = 1;
|
|
246
|
+
else if (c == 901) return usage_gensa(stdout, sa_bit);
|
|
247
|
+
}
|
|
248
|
+
if (argc - o.ind < 2) return usage_gensa(stderr, sa_bit);
|
|
249
|
+
bwt = is_raw? mb_bwt_load_raw(argv[o.ind]) : mb_bwt_load(argv[o.ind]);
|
|
250
|
+
mb_bwt_gen_sa(bwt, sa_bit);
|
|
251
|
+
mb_bwt_save(argv[o.ind+1], bwt);
|
|
252
|
+
mb_bwt_destroy(bwt);
|
|
253
|
+
return 0;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
static int usage_index(FILE *fp, uint64_t seed, int sa_bit, int n_thread)
|
|
257
|
+
{
|
|
258
|
+
(void)n_thread;
|
|
259
|
+
fprintf(fp, "Usage: minibwa index [options] <in.fasta> [out.prefix]\n");
|
|
260
|
+
fprintf(fp, "Options:\n");
|
|
261
|
+
fprintf(fp, " -s INT random seed for amibiguous bases [%ld]\n", (unsigned long)seed);
|
|
262
|
+
fprintf(fp, " -u INT SA sample rate at 1/(1<<INT) [%d]\n", sa_bit);
|
|
263
|
+
fprintf(fp, " -l low-memory GPL'd algorithm for BWT construction\n");
|
|
264
|
+
fprintf(fp, " -b NUM block size (effective with -l) [10m]\n");
|
|
265
|
+
#ifdef LIBSAIS_OPENMP
|
|
266
|
+
fprintf(fp, " -t INT number of threads (effective w/o -l) [%d]\n", n_thread);
|
|
267
|
+
#endif
|
|
268
|
+
fprintf(fp, " --meth build FM-index for BS-seq mapping\n");
|
|
269
|
+
fprintf(fp, " --help print this help message\n");
|
|
270
|
+
return fp == stdout? 0 : 1;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
int main_index(int argc, char *argv[])
|
|
274
|
+
{
|
|
275
|
+
ketopt_t o = KETOPT_INIT;
|
|
276
|
+
int c, low_mem = 0, n_thread = 4, sa_bit = 4, is_meth = 0;
|
|
277
|
+
int64_t block_size = 10000000;
|
|
278
|
+
uint64_t seed = 11;
|
|
279
|
+
char *prefix, *fn_l2b, *fn_bwt, *fn_meth_bwt = 0;
|
|
280
|
+
l2b_t *l2b;
|
|
281
|
+
mb_bwt_t *bwt;
|
|
282
|
+
|
|
283
|
+
while ((c = ketopt(&o, argc, argv, 1, "ls:u:b:t:", long_opts)) >= 0) {
|
|
284
|
+
if (c == 't') n_thread = atoi(o.arg);
|
|
285
|
+
else if (c == 'l') low_mem = 1;
|
|
286
|
+
else if (c == 'b') block_size = kom_parse_num(o.arg, 0);
|
|
287
|
+
else if (c == 'u') sa_bit = atoi(o.arg);
|
|
288
|
+
else if (c == 's') seed = atol(o.arg);
|
|
289
|
+
else if (c == 901) return usage_index(stdout, seed, sa_bit, n_thread);
|
|
290
|
+
else if (c == 902) is_meth = 1;
|
|
291
|
+
}
|
|
292
|
+
if (argc - o.ind == 0) return usage_index(stderr, seed, sa_bit, n_thread);
|
|
293
|
+
|
|
294
|
+
prefix = o.ind + 1 < argc? argv[o.ind+1] : argv[o.ind];
|
|
295
|
+
fn_l2b = kom_calloc(char, strlen(prefix) + 10);
|
|
296
|
+
strcat(strcpy(fn_l2b, prefix), ".l2b");
|
|
297
|
+
fn_bwt = kom_calloc(char, strlen(prefix) + 10);
|
|
298
|
+
strcat(strcpy(fn_bwt, prefix), ".mbw");
|
|
299
|
+
if (is_meth) {
|
|
300
|
+
fn_meth_bwt = kom_calloc(char, strlen(prefix) + 10);
|
|
301
|
+
strcat(strcpy(fn_meth_bwt, prefix), ".meth.mbw");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
l2b = l2b_import(argv[o.ind], seed);
|
|
305
|
+
kom_assert(l2b, "failed to read the genome FASTA.");
|
|
306
|
+
if (low_mem) {
|
|
307
|
+
#ifdef USE_GPL
|
|
308
|
+
l2b_save_pac(fn_l2b, l2b, 1);
|
|
309
|
+
mb_bwtgen(fn_l2b, fn_bwt, block_size);
|
|
310
|
+
l2b_save(fn_l2b, l2b);
|
|
311
|
+
bwt = mb_bwt_load_raw(fn_bwt);
|
|
312
|
+
mb_bwt_gen_sa(bwt, sa_bit);
|
|
313
|
+
mb_bwt_save(fn_bwt, bwt);
|
|
314
|
+
mb_bwt_destroy(bwt);
|
|
315
|
+
if (is_meth) {
|
|
316
|
+
l2b_save_pac_meth(fn_l2b, l2b, 1);
|
|
317
|
+
mb_bwtgen(fn_l2b, fn_meth_bwt, block_size);
|
|
318
|
+
l2b_save(fn_l2b, l2b); // restore the real .l2b; the meth pac above overwrote it (cf. the regular pass)
|
|
319
|
+
bwt = mb_bwt_load_raw(fn_meth_bwt);
|
|
320
|
+
mb_bwt_gen_sa(bwt, sa_bit);
|
|
321
|
+
mb_bwt_save(fn_meth_bwt, bwt);
|
|
322
|
+
mb_bwt_destroy(bwt);
|
|
323
|
+
}
|
|
324
|
+
#else
|
|
325
|
+
if (kom_verbose >= 1) fprintf(stderr, "ERROR: option -l not compiled as it depends on GPL'd code\n");
|
|
326
|
+
abort();
|
|
327
|
+
#endif
|
|
328
|
+
} else {
|
|
329
|
+
l2b_save(fn_l2b, l2b);
|
|
330
|
+
bwt = mb_bwt_libsais(l2b, sa_bit, 1, 0, n_thread);
|
|
331
|
+
mb_bwt_save(fn_bwt, bwt);
|
|
332
|
+
mb_bwt_destroy(bwt);
|
|
333
|
+
if (is_meth) {
|
|
334
|
+
bwt = mb_bwt_libsais(l2b, sa_bit, 1, 1, n_thread);
|
|
335
|
+
mb_bwt_save(fn_meth_bwt, bwt);
|
|
336
|
+
mb_bwt_destroy(bwt);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
l2b_destroy(l2b);
|
|
340
|
+
free(fn_meth_bwt); free(fn_bwt); free(fn_l2b);
|
|
341
|
+
return 0;
|
|
342
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
#include <stdio.h>
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <string.h>
|
|
4
|
+
#include "kalloc.h"
|
|
5
|
+
|
|
6
|
+
/* In kalloc, a *core* is a large chunk of contiguous memory. Each core is
|
|
7
|
+
* associated with a master header, which keeps the size of the current core
|
|
8
|
+
* and the pointer to next core. Kalloc allocates small *blocks* of memory from
|
|
9
|
+
* the cores and organizes free memory blocks in a circular single-linked list.
|
|
10
|
+
*
|
|
11
|
+
* In the following diagram, "@" stands for the header of a free block (of type
|
|
12
|
+
* header_t), "#" for the header of an allocated block (of type size_t), "-"
|
|
13
|
+
* for free memory, and "+" for allocated memory.
|
|
14
|
+
*
|
|
15
|
+
* master This region is core 1. master This region is core 2.
|
|
16
|
+
* | |
|
|
17
|
+
* *@-------#++++++#++++++++++++@-------- *@----------#++++++++++++#+++++++@------------
|
|
18
|
+
* | | | |
|
|
19
|
+
* p=p->ptr->ptr->ptr->ptr p->ptr p->ptr->ptr p->ptr->ptr->ptr
|
|
20
|
+
*/
|
|
21
|
+
typedef struct header_t {
|
|
22
|
+
size_t size;
|
|
23
|
+
struct header_t *ptr;
|
|
24
|
+
} header_t;
|
|
25
|
+
|
|
26
|
+
typedef struct {
|
|
27
|
+
void *par;
|
|
28
|
+
size_t min_core_size;
|
|
29
|
+
header_t base, *loop_head, *core_head; /* base is a zero-sized block always kept in the loop */
|
|
30
|
+
} kmem_t;
|
|
31
|
+
|
|
32
|
+
static void panic(const char *s)
|
|
33
|
+
{
|
|
34
|
+
fprintf(stderr, "%s\n", s);
|
|
35
|
+
abort();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
void *km_init2(void *km_par, size_t min_core_size)
|
|
39
|
+
{
|
|
40
|
+
kmem_t *km;
|
|
41
|
+
km = (kmem_t*)kcalloc(km_par, 1, sizeof(kmem_t));
|
|
42
|
+
km->par = km_par;
|
|
43
|
+
if (km_par) km->min_core_size = min_core_size > 0? min_core_size : ((kmem_t*)km_par)->min_core_size - 2;
|
|
44
|
+
else km->min_core_size = min_core_size > 0? min_core_size : 0x80000;
|
|
45
|
+
return (void*)km;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
void *km_init(void) { return km_init2(0, 0); }
|
|
49
|
+
|
|
50
|
+
void km_destroy(void *_km)
|
|
51
|
+
{
|
|
52
|
+
kmem_t *km = (kmem_t*)_km;
|
|
53
|
+
void *km_par;
|
|
54
|
+
header_t *p, *q;
|
|
55
|
+
if (km == NULL) return;
|
|
56
|
+
km_par = km->par;
|
|
57
|
+
for (p = km->core_head; p != NULL;) {
|
|
58
|
+
q = p->ptr;
|
|
59
|
+
kfree(km_par, p);
|
|
60
|
+
p = q;
|
|
61
|
+
}
|
|
62
|
+
kfree(km_par, km);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static header_t *morecore(kmem_t *km, size_t nu)
|
|
66
|
+
{
|
|
67
|
+
header_t *q;
|
|
68
|
+
size_t bytes, *p;
|
|
69
|
+
nu = (nu + 1 + (km->min_core_size - 1)) / km->min_core_size * km->min_core_size; /* the first +1 for core header */
|
|
70
|
+
bytes = nu * sizeof(header_t);
|
|
71
|
+
q = (header_t*)kmalloc(km->par, bytes);
|
|
72
|
+
if (!q) panic("[morecore] insufficient memory");
|
|
73
|
+
q->ptr = km->core_head, q->size = nu, km->core_head = q;
|
|
74
|
+
p = (size_t*)(q + 1);
|
|
75
|
+
*p = nu - 1; /* the size of the free block; -1 because the first unit is used for the core header */
|
|
76
|
+
kfree(km, p + 1); /* initialize the new "core"; NB: the core header is not looped. */
|
|
77
|
+
return km->loop_head;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
void kfree(void *_km, void *ap) /* kfree() also adds a new core to the circular list */
|
|
81
|
+
{
|
|
82
|
+
header_t *p, *q;
|
|
83
|
+
kmem_t *km = (kmem_t*)_km;
|
|
84
|
+
|
|
85
|
+
if (!ap) return;
|
|
86
|
+
if (km == NULL) {
|
|
87
|
+
free(ap);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
p = (header_t*)((size_t*)ap - 1);
|
|
91
|
+
p->size = *((size_t*)ap - 1);
|
|
92
|
+
/* Find the pointer that points to the block to be freed. The following loop can stop on two conditions:
|
|
93
|
+
*
|
|
94
|
+
* a) "p>q && p<q->ptr": @------#++++++++#+++++++@------- @---------------#+++++++@-------
|
|
95
|
+
* (can also be in | | | -> | |
|
|
96
|
+
* two cores) q p q->ptr q q->ptr
|
|
97
|
+
*
|
|
98
|
+
* @-------- #+++++++++@-------- @-------- @------------------
|
|
99
|
+
* | | | -> | |
|
|
100
|
+
* q p q->ptr q q->ptr
|
|
101
|
+
*
|
|
102
|
+
* b) "q>=q->ptr && (p>q || p<q->ptr)": @-------#+++++ @--------#+++++++ @-------#+++++ @----------------
|
|
103
|
+
* | | | -> | |
|
|
104
|
+
* q->ptr q p q->ptr q
|
|
105
|
+
*
|
|
106
|
+
* #+++++++@----- #++++++++@------- @------------- #++++++++@-------
|
|
107
|
+
* | | | -> | |
|
|
108
|
+
* p q->ptr q q->ptr q
|
|
109
|
+
*/
|
|
110
|
+
for (q = km->loop_head; !(p > q && p < q->ptr); q = q->ptr)
|
|
111
|
+
if (q >= q->ptr && (p > q || p < q->ptr)) break;
|
|
112
|
+
if (p + p->size == q->ptr) { /* two adjacent blocks, merge p and q->ptr (the 2nd and 4th cases) */
|
|
113
|
+
p->size += q->ptr->size;
|
|
114
|
+
p->ptr = q->ptr->ptr;
|
|
115
|
+
} else if (p + p->size > q->ptr && q->ptr >= p) {
|
|
116
|
+
panic("[kfree] The end of the allocated block enters a free block.");
|
|
117
|
+
} else p->ptr = q->ptr; /* backup q->ptr */
|
|
118
|
+
|
|
119
|
+
if (q + q->size == p) { /* two adjacent blocks, merge q and p (the other two cases) */
|
|
120
|
+
q->size += p->size;
|
|
121
|
+
q->ptr = p->ptr;
|
|
122
|
+
km->loop_head = q;
|
|
123
|
+
} else if (q + q->size > p && p >= q) {
|
|
124
|
+
panic("[kfree] The end of a free block enters the allocated block.");
|
|
125
|
+
} else km->loop_head = p, q->ptr = p; /* in two cores, cannot be merged; create a new block in the list */
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
void *kmalloc(void *_km, size_t n_bytes)
|
|
129
|
+
{
|
|
130
|
+
kmem_t *km = (kmem_t*)_km;
|
|
131
|
+
size_t n_units;
|
|
132
|
+
header_t *p, *q;
|
|
133
|
+
|
|
134
|
+
if (n_bytes == 0) return 0;
|
|
135
|
+
if (km == NULL) return malloc(n_bytes);
|
|
136
|
+
n_units = (n_bytes + sizeof(size_t) + sizeof(header_t) - 1) / sizeof(header_t); /* header+n_bytes requires at least this number of units */
|
|
137
|
+
|
|
138
|
+
if (!(q = km->loop_head)) /* the first time when kmalloc() is called, intialize it */
|
|
139
|
+
q = km->loop_head = km->base.ptr = &km->base;
|
|
140
|
+
for (p = q->ptr;; q = p, p = p->ptr) { /* search for a suitable block */
|
|
141
|
+
if (p->size >= n_units) { /* p->size if the size of current block. This line means the current block is large enough. */
|
|
142
|
+
if (p->size == n_units) q->ptr = p->ptr; /* no need to split the block */
|
|
143
|
+
else { /* split the block. NB: memory is allocated at the end of the block! */
|
|
144
|
+
p->size -= n_units; /* reduce the size of the free block */
|
|
145
|
+
p += p->size; /* p points to the allocated block */
|
|
146
|
+
*(size_t*)p = n_units; /* set the size */
|
|
147
|
+
}
|
|
148
|
+
km->loop_head = q; /* set the end of chain */
|
|
149
|
+
return (size_t*)p + 1;
|
|
150
|
+
}
|
|
151
|
+
if (p == km->loop_head) { /* then ask for more "cores" */
|
|
152
|
+
if ((p = morecore(km, n_units)) == 0) return 0;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
void *kcalloc(void *_km, size_t count, size_t size)
|
|
158
|
+
{
|
|
159
|
+
kmem_t *km = (kmem_t*)_km;
|
|
160
|
+
void *p;
|
|
161
|
+
if (size == 0 || count == 0) return 0;
|
|
162
|
+
if (km == NULL) return calloc(count, size);
|
|
163
|
+
p = kmalloc(km, count * size);
|
|
164
|
+
memset(p, 0, count * size);
|
|
165
|
+
return p;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
void *krealloc(void *_km, void *ap, size_t n_bytes) // TODO: this can be made more efficient in principle
|
|
169
|
+
{
|
|
170
|
+
kmem_t *km = (kmem_t*)_km;
|
|
171
|
+
size_t cap, *p, *q;
|
|
172
|
+
|
|
173
|
+
if (n_bytes == 0) {
|
|
174
|
+
kfree(km, ap); return 0;
|
|
175
|
+
}
|
|
176
|
+
if (km == NULL) return realloc(ap, n_bytes);
|
|
177
|
+
if (ap == NULL) return kmalloc(km, n_bytes);
|
|
178
|
+
p = (size_t*)ap - 1;
|
|
179
|
+
cap = (*p) * sizeof(header_t) - sizeof(size_t);
|
|
180
|
+
if (cap >= n_bytes) return ap; /* TODO: this prevents shrinking */
|
|
181
|
+
q = (size_t*)kmalloc(km, n_bytes);
|
|
182
|
+
memcpy(q, ap, cap);
|
|
183
|
+
kfree(km, ap);
|
|
184
|
+
return q;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
void *krelocate(void *km, void *ap, size_t n_bytes)
|
|
188
|
+
{
|
|
189
|
+
void *p;
|
|
190
|
+
if (km == 0 || ap == 0) return ap;
|
|
191
|
+
p = kmalloc(km, n_bytes);
|
|
192
|
+
memcpy(p, ap, n_bytes);
|
|
193
|
+
kfree(km, ap);
|
|
194
|
+
return p;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
void km_stat(const void *_km, km_stat_t *s)
|
|
198
|
+
{
|
|
199
|
+
kmem_t *km = (kmem_t*)_km;
|
|
200
|
+
header_t *p;
|
|
201
|
+
memset(s, 0, sizeof(km_stat_t));
|
|
202
|
+
if (km == NULL || km->loop_head == NULL) return;
|
|
203
|
+
for (p = km->loop_head;; p = p->ptr) {
|
|
204
|
+
s->available += p->size * sizeof(header_t);
|
|
205
|
+
if (p->size != 0) ++s->n_blocks; /* &kmem_t::base is always one of the cores. It is zero-sized. */
|
|
206
|
+
if (p->ptr > p && p + p->size > p->ptr)
|
|
207
|
+
panic("[km_stat] The end of a free block enters another free block.");
|
|
208
|
+
if (p->ptr == km->loop_head) break;
|
|
209
|
+
}
|
|
210
|
+
for (p = km->core_head; p != NULL; p = p->ptr) {
|
|
211
|
+
size_t size = p->size * sizeof(header_t);
|
|
212
|
+
++s->n_cores;
|
|
213
|
+
s->capacity += size;
|
|
214
|
+
s->largest = s->largest > size? s->largest : size;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
void km_stat_print(const void *km)
|
|
219
|
+
{
|
|
220
|
+
km_stat_t st;
|
|
221
|
+
km_stat(km, &st);
|
|
222
|
+
fprintf(stderr, "[km_stat] cap=%ld, avail=%ld, largest=%ld, n_core=%ld, n_block=%ld\n",
|
|
223
|
+
st.capacity, st.available, st.largest, st.n_blocks, st.n_cores);
|
|
224
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#ifndef _KALLOC_H_
|
|
2
|
+
#define _KALLOC_H_
|
|
3
|
+
|
|
4
|
+
#include <stddef.h> /* for size_t */
|
|
5
|
+
|
|
6
|
+
#ifdef __cplusplus
|
|
7
|
+
extern "C" {
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
typedef struct {
|
|
11
|
+
size_t capacity, available, n_blocks, n_cores, largest;
|
|
12
|
+
} km_stat_t;
|
|
13
|
+
|
|
14
|
+
void *kmalloc(void *km, size_t size);
|
|
15
|
+
void *krealloc(void *km, void *ptr, size_t size);
|
|
16
|
+
void *krelocate(void *km, void *ap, size_t n_bytes);
|
|
17
|
+
void *kcalloc(void *km, size_t count, size_t size);
|
|
18
|
+
void kfree(void *km, void *ptr);
|
|
19
|
+
|
|
20
|
+
void *km_init(void);
|
|
21
|
+
void *km_init2(void *km_par, size_t min_core_size);
|
|
22
|
+
void km_destroy(void *km);
|
|
23
|
+
void km_stat(const void *_km, km_stat_t *s);
|
|
24
|
+
void km_stat_print(const void *km);
|
|
25
|
+
|
|
26
|
+
#ifdef __cplusplus
|
|
27
|
+
}
|
|
28
|
+
#endif
|
|
29
|
+
|
|
30
|
+
#define Kmalloc(km, type, cnt) ((type*)kmalloc((km), (cnt) * sizeof(type)))
|
|
31
|
+
#define Kcalloc(km, type, cnt) ((type*)kcalloc((km), (cnt), sizeof(type)))
|
|
32
|
+
#define Krealloc(km, type, ptr, cnt) ((type*)krealloc((km), (ptr), (cnt) * sizeof(type)))
|
|
33
|
+
|
|
34
|
+
#define Kgrow(km, type, ptr, __i, __m) do { \
|
|
35
|
+
if ((__i) >= (__m)) { \
|
|
36
|
+
(__m) = (__i) + 1; \
|
|
37
|
+
(__m) += ((__m)>>1) + 16; \
|
|
38
|
+
(ptr) = Krealloc(km, type, ptr, (__m)); \
|
|
39
|
+
} \
|
|
40
|
+
} while (0)
|
|
41
|
+
|
|
42
|
+
#define Kexpand(km, type, a, m) do { \
|
|
43
|
+
(m) = (m) >= 4? (m) + ((m)>>1) : 16; \
|
|
44
|
+
(a) = Krealloc(km, type, (a), (m)); \
|
|
45
|
+
} while (0)
|
|
46
|
+
|
|
47
|
+
#ifndef klib_unused
|
|
48
|
+
#if (defined __clang__ && __clang_major__ >= 3) || (defined __GNUC__ && __GNUC__ >= 3)
|
|
49
|
+
#define klib_unused __attribute__ ((__unused__))
|
|
50
|
+
#else
|
|
51
|
+
#define klib_unused
|
|
52
|
+
#endif
|
|
53
|
+
#endif /* klib_unused */
|
|
54
|
+
#endif
|