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.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +119 -0
  4. data/ext/minibwa/extconf.rb +105 -0
  5. data/ext/minibwa/mb_buffer.c +113 -0
  6. data/ext/minibwa/mb_hit.c +192 -0
  7. data/ext/minibwa/mb_index.c +462 -0
  8. data/ext/minibwa/mb_index_build.c +174 -0
  9. data/ext/minibwa/mb_options.c +301 -0
  10. data/ext/minibwa/minibwa/LICENSE.txt +37 -0
  11. data/ext/minibwa/minibwa/align.c +930 -0
  12. data/ext/minibwa/minibwa/bseq.h +45 -0
  13. data/ext/minibwa/minibwa/bwt.c +715 -0
  14. data/ext/minibwa/minibwa/bwt.h +86 -0
  15. data/ext/minibwa/minibwa/cs.c +161 -0
  16. data/ext/minibwa/minibwa/format.c +356 -0
  17. data/ext/minibwa/minibwa/index.c +342 -0
  18. data/ext/minibwa/minibwa/kalloc.c +224 -0
  19. data/ext/minibwa/minibwa/kalloc.h +54 -0
  20. data/ext/minibwa/minibwa/ketopt.h +123 -0
  21. data/ext/minibwa/minibwa/kommon.c +374 -0
  22. data/ext/minibwa/minibwa/kommon.h +85 -0
  23. data/ext/minibwa/minibwa/kseq.h +256 -0
  24. data/ext/minibwa/minibwa/ksort.h +163 -0
  25. data/ext/minibwa/minibwa/ksw2.h +220 -0
  26. data/ext/minibwa/minibwa/ksw2_extd2_sse.c +403 -0
  27. data/ext/minibwa/minibwa/ksw2_extz2_sse.c +296 -0
  28. data/ext/minibwa/minibwa/ksw2_ll_sse.c +341 -0
  29. data/ext/minibwa/minibwa/kthread.h +15 -0
  30. data/ext/minibwa/minibwa/l2bit.c +479 -0
  31. data/ext/minibwa/minibwa/l2bit.h +72 -0
  32. data/ext/minibwa/minibwa/lchain.c +231 -0
  33. data/ext/minibwa/minibwa/libsais.c +6985 -0
  34. data/ext/minibwa/minibwa/libsais.h +106 -0
  35. data/ext/minibwa/minibwa/libsais64.c +7064 -0
  36. data/ext/minibwa/minibwa/libsais64.h +81 -0
  37. data/ext/minibwa/minibwa/map-algo.c +769 -0
  38. data/ext/minibwa/minibwa/mbpriv.h +148 -0
  39. data/ext/minibwa/minibwa/minibwa.h +176 -0
  40. data/ext/minibwa/minibwa/options.c +116 -0
  41. data/ext/minibwa/minibwa/pe.c +559 -0
  42. data/ext/minibwa/minibwa/s2n-lite.h +59 -0
  43. data/ext/minibwa/minibwa/seed.c +354 -0
  44. data/ext/minibwa/minibwa.c +67 -0
  45. data/ext/minibwa/minibwa.h +51 -0
  46. data/lib/minibwa/hit.rb +110 -0
  47. data/lib/minibwa/index.rb +77 -0
  48. data/lib/minibwa/options.rb +235 -0
  49. data/lib/minibwa/sam.rb +85 -0
  50. data/lib/minibwa/version.rb +6 -0
  51. data/lib/minibwa.rb +11 -0
  52. metadata +88 -0
@@ -0,0 +1,123 @@
1
+ #ifndef KETOPT_H
2
+ #define KETOPT_H
3
+
4
+ #include <string.h> /* for strchr() and strncmp() */
5
+
6
+ #define ko_no_argument 0
7
+ #define ko_required_argument 1
8
+ #define ko_optional_argument 2
9
+
10
+ typedef struct {
11
+ int ind; /* equivalent to optind */
12
+ int opt; /* equivalent to optopt */
13
+ char *arg; /* equivalent to optarg */
14
+ int longidx; /* index of a long option; or -1 if short */
15
+ /* private variables not intended for external uses */
16
+ int i, pos, n_args;
17
+ } ketopt_t;
18
+
19
+ typedef struct {
20
+ char *name;
21
+ int has_arg;
22
+ int val;
23
+ } ko_longopt_t;
24
+
25
+ static ketopt_t KETOPT_INIT = { 1, 0, 0, -1, 1, 0, 0 };
26
+
27
+ static void ketopt_permute(char *argv[], int j, int n) /* move argv[j] over n elements to the left */
28
+ {
29
+ int k;
30
+ char *p = argv[j];
31
+ for (k = 0; k < n; ++k)
32
+ argv[j - k] = argv[j - k - 1];
33
+ argv[j - k] = p;
34
+ }
35
+
36
+ /**
37
+ * Parse command-line options and arguments
38
+ *
39
+ * This fuction has a similar interface to GNU's getopt_long(). Each call
40
+ * parses one option and returns the option name. s->arg points to the option
41
+ * argument if present. The function returns -1 when all command-line arguments
42
+ * are parsed. In this case, s->ind is the index of the first non-option
43
+ * argument.
44
+ *
45
+ * @param s status; shall be initialized to KETOPT_INIT on the first call
46
+ * @param argc length of argv[]
47
+ * @param argv list of command-line arguments; argv[0] is ignored
48
+ * @param permute non-zero to move options ahead of non-option arguments
49
+ * @param ostr option string
50
+ * @param longopts long options
51
+ *
52
+ * @return ASCII for a short option; ko_longopt_t::val for a long option; -1 if
53
+ * argv[] is fully processed; '?' for an unknown option or an ambiguous
54
+ * long option; ':' if an option argument is missing
55
+ */
56
+ static int ketopt(ketopt_t *s, int argc, char *argv[], int permute, const char *ostr, const ko_longopt_t *longopts)
57
+ {
58
+ int opt = -1, i0, j;
59
+ if (permute) {
60
+ while (s->i < argc && (argv[s->i][0] != '-' || argv[s->i][1] == '\0'))
61
+ ++s->i, ++s->n_args;
62
+ }
63
+ s->arg = 0, s->longidx = -1, i0 = s->i;
64
+ if (s->i >= argc || argv[s->i][0] != '-' || argv[s->i][1] == '\0') {
65
+ s->ind = s->i - s->n_args;
66
+ return -1;
67
+ }
68
+ if (argv[s->i][0] == '-' && argv[s->i][1] == '-') { /* "--" or a long option */
69
+ if (argv[s->i][2] == '\0') { /* a bare "--" */
70
+ ketopt_permute(argv, s->i, s->n_args);
71
+ ++s->i, s->ind = s->i - s->n_args;
72
+ return -1;
73
+ }
74
+ s->opt = 0, opt = '?', s->pos = -1;
75
+ if (longopts) { /* parse long options */
76
+ int k, n_exact = 0, n_partial = 0;
77
+ const ko_longopt_t *o = 0, *o_exact = 0, *o_partial = 0;
78
+ for (j = 2; argv[s->i][j] != '\0' && argv[s->i][j] != '='; ++j) {} /* find the end of the option name */
79
+ for (k = 0; longopts[k].name != 0; ++k)
80
+ if (strncmp(&argv[s->i][2], longopts[k].name, j - 2) == 0) {
81
+ if (longopts[k].name[j - 2] == 0) ++n_exact, o_exact = &longopts[k];
82
+ else ++n_partial, o_partial = &longopts[k];
83
+ }
84
+ if (n_exact > 1 || (n_exact == 0 && n_partial > 1)) {
85
+ s->i++;
86
+ return '?';
87
+ }
88
+ o = n_exact == 1? o_exact : n_partial == 1? o_partial : 0;
89
+ if (o) {
90
+ s->opt = opt = o->val, s->longidx = o - longopts;
91
+ if (argv[s->i][j] == '=') s->arg = &argv[s->i][j + 1];
92
+ if (o->has_arg == 1 && argv[s->i][j] == '\0') {
93
+ if (s->i < argc - 1) s->arg = argv[++s->i];
94
+ else opt = ':'; /* missing option argument */
95
+ }
96
+ }
97
+ }
98
+ } else { /* a short option */
99
+ char *p;
100
+ if (s->pos == 0) s->pos = 1;
101
+ opt = s->opt = argv[s->i][s->pos++];
102
+ p = strchr((char*)ostr, opt);
103
+ if (p == 0) {
104
+ opt = '?'; /* unknown option */
105
+ } else if (p[1] == ':') {
106
+ if (argv[s->i][s->pos] == 0) {
107
+ if (s->i < argc - 1) s->arg = argv[++s->i];
108
+ else opt = ':'; /* missing option argument */
109
+ } else s->arg = &argv[s->i][s->pos];
110
+ s->pos = -1;
111
+ }
112
+ }
113
+ if (s->pos < 0 || argv[s->i][s->pos] == 0) {
114
+ ++s->i, s->pos = 0;
115
+ if (s->n_args > 0) /* permute */
116
+ for (j = i0; j < s->i; ++j)
117
+ ketopt_permute(argv, j, s->n_args);
118
+ }
119
+ s->ind = s->i - s->n_args;
120
+ return opt;
121
+ }
122
+
123
+ #endif
@@ -0,0 +1,374 @@
1
+ #define _DEFAULT_SOURCE // expose MADV_RANDOM etc. under -std=c99 (glibc >= 2.19)
2
+ #define _BSD_SOURCE // ditto on older glibc (e.g. CentOS 7 / glibc 2.17)
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+ #include <stdarg.h>
6
+ #include <stdio.h>
7
+ #include "kommon.h"
8
+
9
+ int kom_verbose = 3, kom_dbg = 0, kom_dbg_flag = 0;
10
+
11
+ char *kom_strdup(const char *src) // strdup() doesn't conform to C99
12
+ {
13
+ size_t len;
14
+ char *dst;
15
+ len = strlen(src);
16
+ dst = kom_malloc(char, len + 1);
17
+ memcpy(dst, src, len + 1);
18
+ return dst;
19
+ }
20
+
21
+ int64_t kom_parse_num(const char *str, char **q)
22
+ {
23
+ double x;
24
+ char *p;
25
+ x = strtod(str, &p);
26
+ if (*p == 'G' || *p == 'g') x *= 1e9, ++p;
27
+ else if (*p == 'M' || *p == 'm') x *= 1e6, ++p;
28
+ else if (*p == 'K' || *p == 'k') x *= 1e3, ++p;
29
+ if (q) *q = p;
30
+ return (int64_t)(x + .499);
31
+ }
32
+
33
+ void kom_panic(const char *func, const char *msg)
34
+ {
35
+ fprintf(stderr, "[E::%s] %s ABORT!\n", func, msg);
36
+ abort();
37
+ }
38
+
39
+ /****************
40
+ * Fast sprintf *
41
+ ****************/
42
+
43
+ #ifdef HAVE_KALLOC
44
+ #include "kalloc.h"
45
+ #endif
46
+
47
+ static inline void str_enlarge(void *km, kstring_t *s, int l)
48
+ {
49
+ if (s->l + l + 1 > s->m) {
50
+ s->m = s->l + l + 1;
51
+ kom_roundup64(s->m);
52
+ #ifdef HAVE_KALLOC
53
+ s->s = Krealloc(km, char, s->s, s->m);
54
+ #else
55
+ s->s = kom_realloc(char, s->s, s->m);
56
+ #endif
57
+ }
58
+ }
59
+
60
+ static inline void str_copy(void *km, kstring_t *s, const char *st, const char *en)
61
+ {
62
+ str_enlarge(km, s, en - st);
63
+ memcpy(&s->s[s->l], st, en - st);
64
+ s->l += en - st;
65
+ }
66
+
67
+ int64_t kom_sprintf_lite_core(void *km, kstring_t *s, const char *fmt, va_list ap)
68
+ {
69
+ char buf[32]; // for integer to string conversion
70
+ const char *p, *q;
71
+ int64_t len = 0;
72
+ for (q = p = fmt; *p; ++p) {
73
+ if (*p == '%') {
74
+ if (p > q) {
75
+ len += p - q;
76
+ if (s) str_copy(km, s, q, p);
77
+ }
78
+ ++p;
79
+ if (*p == 'd') {
80
+ int c, i, l = 0;
81
+ unsigned int x;
82
+ c = va_arg(ap, int);
83
+ x = c >= 0? c : -c;
84
+ do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
85
+ if (c < 0) buf[l++] = '-';
86
+ len += l;
87
+ if (s) {
88
+ str_enlarge(km, s, l);
89
+ for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
90
+ }
91
+ } else if (*p == 'l' && *(p+1) == 'd') {
92
+ int i, l = 0;
93
+ long c;
94
+ unsigned long x;
95
+ c = va_arg(ap, long);
96
+ x = c >= 0? c : -c;
97
+ do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
98
+ if (c < 0) buf[l++] = '-';
99
+ len += l;
100
+ if (s) {
101
+ str_enlarge(km, s, l);
102
+ for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
103
+ }
104
+ ++p;
105
+ } else if (*p == 'u') {
106
+ int i, l = 0;
107
+ uint32_t x;
108
+ x = va_arg(ap, uint32_t);
109
+ do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0);
110
+ len += l;
111
+ if (s) {
112
+ str_enlarge(km, s, l);
113
+ for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
114
+ }
115
+ } else if (*p == 's') {
116
+ char *r = va_arg(ap, char*);
117
+ int l;
118
+ l = strlen(r);
119
+ len += l;
120
+ if (s) str_copy(km, s, r, r + l);
121
+ } else if (*p == 'c') {
122
+ ++len;
123
+ if (s) {
124
+ str_enlarge(km, s, 1);
125
+ s->s[s->l++] = va_arg(ap, int);
126
+ }
127
+ } else {
128
+ fprintf(stderr, "ERROR: unrecognized type '%%%c'\n", *p);
129
+ abort();
130
+ }
131
+ q = p + 1;
132
+ }
133
+ }
134
+ if (p > q) {
135
+ len += p - q;
136
+ if (s) str_copy(km, s, q, p);
137
+ }
138
+ if (s) s->s[s->l] = 0;
139
+ return len;
140
+ }
141
+
142
+ int64_t kom_sprintf_lite(kstring_t *s, const char *fmt, ...)
143
+ {
144
+ int64_t ret;
145
+ va_list ap;
146
+ va_start(ap, fmt); ret = kom_sprintf_lite_core(0, s, fmt, ap); va_end(ap);
147
+ return ret;
148
+ }
149
+
150
+ int64_t km_sprintf_lite(void *km, kstring_t *s, const char *fmt, ...)
151
+ {
152
+ int64_t ret;
153
+ va_list ap;
154
+ va_start(ap, fmt); ret = kom_sprintf_lite_core(km, s, fmt, ap); va_end(ap);
155
+ return ret;
156
+ }
157
+
158
+ /****************
159
+ * DNA sequence *
160
+ ****************/
161
+
162
+ uint8_t kom_nt4_table[256] = {
163
+ 0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
164
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
165
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
166
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
167
+ 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
168
+ 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
169
+ 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
170
+ 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
171
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
172
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
173
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
174
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
175
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
176
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
177
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
178
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
179
+ };
180
+
181
+ uint8_t kom_comp_table[256] = {
182
+ 3, 2, 1, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
183
+ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
184
+ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
185
+ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
186
+ 64, 'T', 'V', 'G', 'H', 'E', 'F', 'C', 'D', 'I', 'J', 'M', 'L', 'K', 'N', 'O',
187
+ 'P', 'Q', 'Y', 'S', 'A', 'A', 'B', 'W', 'X', 'R', 'Z', 91, 92, 93, 94, 95,
188
+ 96, 't', 'v', 'g', 'h', 'e', 'f', 'c', 'd', 'i', 'j', 'm', 'l', 'k', 'n', 'o',
189
+ 'p', 'q', 'y', 's', 'a', 'a', 'b', 'w', 'x', 'r', 'z', 123, 124, 125, 126, 127,
190
+ 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
191
+ 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
192
+ 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
193
+ 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
194
+ 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
195
+ 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
196
+ 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
197
+ 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
198
+ };
199
+
200
+ void kom_revcomp(uint64_t len, char *seq) // reverse complement in place
201
+ {
202
+ uint64_t i;
203
+ for (i = 0; i < len>>1; ++i) {
204
+ uint8_t t = seq[len - i - 1];
205
+ seq[len - i - 1] = kom_comp_table[(uint8_t)seq[i]];
206
+ seq[i] = kom_comp_table[t];
207
+ }
208
+ if (len&1) seq[len>>1] = kom_comp_table[(uint8_t)seq[len>>1]];
209
+ }
210
+
211
+ /***********************
212
+ * Timing and peak RSS *
213
+ ***********************/
214
+
215
+ #if defined(WIN32) || defined(_WIN32)
216
+ #include <windows.h>
217
+
218
+ struct timezone
219
+ {
220
+ __int32 tz_minuteswest; /* minutes W of Greenwich */
221
+ int tz_dsttime; /* type of dst correction */
222
+ };
223
+
224
+ /*
225
+ * gettimeofday.c
226
+ * Win32 gettimeofday() replacement
227
+ * taken from PostgreSQL, according to
228
+ * https://stackoverflow.com/questions/1676036/what-should-i-use-to-replace-gettimeofday-on-windows
229
+ *
230
+ * src/port/gettimeofday.c
231
+ *
232
+ * Copyright (c) 2003 SRA, Inc.
233
+ * Copyright (c) 2003 SKC, Inc.
234
+ *
235
+ * Permission to use, copy, modify, and distribute this software and
236
+ * its documentation for any purpose, without fee, and without a
237
+ * written agreement is hereby granted, provided that the above
238
+ * copyright notice and this paragraph and the following two
239
+ * paragraphs appear in all copies.
240
+ *
241
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
242
+ * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
243
+ * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
244
+ * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
245
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
246
+ *
247
+ * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
248
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
249
+ * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
250
+ * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
251
+ * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
252
+ */
253
+
254
+ /* FILETIME of Jan 1 1970 00:00:00. */
255
+ static const unsigned __int64 epoch = ((unsigned __int64) 116444736000000000ULL);
256
+
257
+ /*
258
+ * timezone information is stored outside the kernel so tzp isn't used anymore.
259
+ *
260
+ * Note: this function is not for Win32 high precision timing purpose. See
261
+ * elapsed_time().
262
+ */
263
+ int gettimeofday(struct timeval * tp, struct timezone *tzp)
264
+ {
265
+ FILETIME file_time;
266
+ SYSTEMTIME system_time;
267
+ ULARGE_INTEGER ularge;
268
+
269
+ GetSystemTime(&system_time);
270
+ SystemTimeToFileTime(&system_time, &file_time);
271
+ ularge.LowPart = file_time.dwLowDateTime;
272
+ ularge.HighPart = file_time.dwHighDateTime;
273
+
274
+ tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L);
275
+ tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
276
+
277
+ return 0;
278
+ }
279
+
280
+ // taken from https://stackoverflow.com/questions/5272470/c-get-cpu-usage-on-linux-and-windows
281
+ double kom_cputime()
282
+ {
283
+ HANDLE hProcess = GetCurrentProcess();
284
+ FILETIME ftCreation, ftExit, ftKernel, ftUser;
285
+ SYSTEMTIME stKernel;
286
+ SYSTEMTIME stUser;
287
+
288
+ GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);
289
+ FileTimeToSystemTime(&ftKernel, &stKernel);
290
+ FileTimeToSystemTime(&ftUser, &stUser);
291
+
292
+ double kernelModeTime = ((stKernel.wHour * 60.) + stKernel.wMinute * 60.) + stKernel.wSecond * 1. + stKernel.wMilliseconds / 1000.;
293
+ double userModeTime = ((stUser.wHour * 60.) + stUser.wMinute * 60.) + stUser.wSecond * 1. + stUser.wMilliseconds / 1000.;
294
+
295
+ return kernelModeTime + userModeTime;
296
+ }
297
+
298
+ long kom_peakrss(void) { return 0; }
299
+ #else
300
+ #include <sys/resource.h>
301
+ #include <sys/time.h>
302
+
303
+ double kom_cputime(void)
304
+ {
305
+ struct rusage r;
306
+ getrusage(RUSAGE_SELF, &r);
307
+ return r.ru_utime.tv_sec + r.ru_stime.tv_sec + 1e-6 * (r.ru_utime.tv_usec + r.ru_stime.tv_usec);
308
+ }
309
+
310
+ long kom_peakrss(void)
311
+ {
312
+ struct rusage r;
313
+ getrusage(RUSAGE_SELF, &r);
314
+ #ifdef __linux__
315
+ return r.ru_maxrss * 1024;
316
+ #else
317
+ return r.ru_maxrss;
318
+ #endif
319
+ }
320
+
321
+ #endif /* WIN32 || _WIN32 */
322
+
323
+ double kom_realtime(void)
324
+ {
325
+ static double realtime0 = -1.0;
326
+ struct timeval tp;
327
+ double t;
328
+ gettimeofday(&tp, NULL);
329
+ t = tp.tv_sec + tp.tv_usec * 1e-6;
330
+ if (realtime0 < 0.0) realtime0 = t;
331
+ return t - realtime0;
332
+ }
333
+
334
+ double kom_percent_cpu(void)
335
+ {
336
+ return (kom_cputime() + 1e-6) / (kom_realtime() + 1e-6);
337
+ }
338
+
339
+ /***************
340
+ * mmap loader *
341
+ ***************/
342
+
343
+ #include <fcntl.h>
344
+ #include <unistd.h>
345
+ #include <sys/mman.h>
346
+ #include <sys/stat.h>
347
+
348
+ void *kom_mmap_file(const char *fn, size_t *len, int preload)
349
+ {
350
+ int fd, mmap_flags = MAP_SHARED;
351
+ struct stat st;
352
+ uint8_t *base;
353
+
354
+ *len = 0;
355
+ fd = open(fn, O_RDONLY);
356
+ if (fd < 0) return 0;
357
+ if (fstat(fd, &st) < 0 || st.st_size == 0) { close(fd); return 0; }
358
+ *len = st.st_size;
359
+ #ifdef MAP_POPULATE // this is Linux only
360
+ if (preload) mmap_flags |= MAP_POPULATE;
361
+ #endif
362
+ base = (uint8_t*)mmap(0, *len, PROT_READ, mmap_flags, fd, 0);
363
+ close(fd);
364
+ if (base == MAP_FAILED) return 0;
365
+ #ifdef MADV_RANDOM
366
+ madvise(base, *len, MADV_RANDOM);
367
+ #endif
368
+ return base;
369
+ }
370
+
371
+ int kom_munmap(void *base, size_t map_len) // a simple wrapper
372
+ {
373
+ return munmap(base, map_len);
374
+ }
@@ -0,0 +1,85 @@
1
+ #ifndef HL_HEADER
2
+ #define HL_HEADER
3
+
4
+ #include <stdint.h>
5
+
6
+ #define kom_malloc(type, cnt) ((type*)malloc((cnt) * sizeof(type)))
7
+ #define kom_calloc(type, cnt) ((type*)calloc((cnt), sizeof(type)))
8
+ #define kom_realloc(type, ptr, cnt) ((type*)realloc((ptr), (cnt) * sizeof(type)))
9
+
10
+ // make enough room to write ptr[i]
11
+ #define kom_grow(type, ptr, __i, __m) do { \
12
+ if ((__i) >= (__m)) { \
13
+ (__m) = (__i) + 1; \
14
+ (__m) += ((__m)>>1) + 16; \
15
+ (ptr) = kom_realloc(type, (ptr), (__m)); \
16
+ } \
17
+ } while (0)
18
+
19
+ #define kom_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
20
+ #define kom_roundup64(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, (x)|=(x)>>32, ++(x))
21
+
22
+ #define kom_reverse(type, n, a) do { \
23
+ size_t i; \
24
+ for (i = 0; i < (n)>>1; ++i) { \
25
+ type t = a[(n) - 1 - i]; \
26
+ a[(n) - 1 - i] = a[i], a[i] = t; \
27
+ } \
28
+ } while (0)
29
+
30
+ #define kom_assert(cond, msg) if ((cond) == 0) kom_panic(__func__, (msg))
31
+
32
+ #ifndef KSTRING_T
33
+ #define KSTRING_T kstring_t
34
+ typedef struct {
35
+ size_t l, m;
36
+ char *s;
37
+ } kstring_t;
38
+ #endif
39
+
40
+ typedef struct { uint64_t x, y; } kom128_t;
41
+
42
+ #ifdef __cplusplus
43
+ extern "C" {
44
+ #endif
45
+
46
+ extern int kom_verbose, kom_dbg, kom_dbg_flag;
47
+
48
+ char *kom_strdup(const char *src);
49
+ int64_t kom_parse_num(const char *str, char **q);
50
+ void kom_panic(const char *func, const char *msg);
51
+
52
+ int64_t kom_sprintf_lite(kstring_t *s, const char *fmt, ...);
53
+ int64_t km_sprintf_lite(void *km, kstring_t *s, const char *fmt, ...);
54
+
55
+ double kom_cputime(void);
56
+ double kom_realtime(void); // call at the beginning to reset the timer
57
+ long kom_peakrss(void);
58
+ double kom_percent_cpu(void);
59
+
60
+ void *kom_mmap_file(const char *fn, size_t *len, int preload);
61
+ int kom_munmap(void *base, size_t map_len);
62
+
63
+ extern uint8_t kom_nt4_table[256], kom_comp_table[256];
64
+ void kom_revcomp(uint64_t len, char *seq);
65
+
66
+ static inline uint64_t kom_splitmix64(uint64_t *x)
67
+ {
68
+ uint64_t z = ((*x) += 0x9e3779b97f4a7c15ULL);
69
+ z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
70
+ z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
71
+ return z ^ (z >> 31);
72
+ }
73
+
74
+ static inline double kom_u64todbl(uint64_t x)
75
+ {
76
+ union { uint64_t i; double d; } u;
77
+ u.i = 0x3FFULL << 52 | x >> 12;
78
+ return u.d - 1.0;
79
+ }
80
+
81
+ #ifdef __cplusplus
82
+ }
83
+ #endif
84
+
85
+ #endif // ~HL_HEADER