| 1 |
#include <zlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <limits.h> |
| 5 |
#include <string.h> |
| 6 |
#include <unistd.h> |
| 7 |
#include "bwa.h" |
| 8 |
#include "bwamem.h" |
| 9 |
#include "kseq.h" |
| 10 |
KSEQ_DECLARE(gzFile) |
| 11 |
|
| 12 |
int main_maxk(int argc, char *argv[]) |
| 13 |
{ |
| 14 |
int i, c, self = 0, max_len = 0; |
| 15 |
uint8_t *cnt = 0; |
| 16 |
uint64_t hist[256]; |
| 17 |
bwt_t *bwt; |
| 18 |
kseq_t *ks; |
| 19 |
smem_i *itr; |
| 20 |
gzFile fp; |
| 21 |
|
| 22 |
while ((c = getopt(argc, argv, "s")) >= 0) { |
| 23 |
if (c == 's') self = 1; |
| 24 |
} |
| 25 |
if (optind + 2 > argc) { |
| 26 |
fprintf(stderr, "Usage: bwa maxk [-s] <index.prefix> <seq.fa>\n"); |
| 27 |
return 1; |
| 28 |
} |
| 29 |
fp = strcmp(argv[optind+1], "-")? gzopen(argv[optind+1], "rb") : gzdopen(fileno(stdin), "rb"); |
| 30 |
ks = kseq_init(fp); |
| 31 |
bwt = bwt_restore_bwt(argv[optind]); |
| 32 |
itr = smem_itr_init(bwt); |
| 33 |
if (self) smem_config(itr, 2, INT_MAX, 0); |
| 34 |
memset(hist, 0, 8 * 256); |
| 35 |
|
| 36 |
while (kseq_read(ks) >= 0) { |
| 37 |
const bwtintv_v *a; |
| 38 |
if (ks->seq.l > max_len) { |
| 39 |
max_len = ks->seq.l; |
| 40 |
kroundup32(max_len); |
| 41 |
cnt = realloc(cnt, max_len); |
| 42 |
} |
| 43 |
memset(cnt, 0, ks->seq.l); |
| 44 |
for (i = 0; i < ks->seq.l; ++i) |
| 45 |
ks->seq.s[i] = nst_nt4_table[(int)ks->seq.s[i]]; |
| 46 |
smem_set_query(itr, ks->seq.l, (uint8_t*)ks->seq.s); |
| 47 |
while ((a = smem_next(itr)) != 0) { |
| 48 |
for (i = 0; i < a->n; ++i) { |
| 49 |
bwtintv_t *p = &a->a[i]; |
| 50 |
int j, l, start = p->info>>32, end = (uint32_t)p->info; |
| 51 |
l = end - start < 255? end - start : 255; |
| 52 |
for (j = start; j < end; ++j) |
| 53 |
cnt[j] = cnt[j] > l? cnt[j] : l; |
| 54 |
} |
| 55 |
} |
| 56 |
for (i = 0; i < ks->seq.l; ++i) ++hist[cnt[i]]; |
| 57 |
} |
| 58 |
for (i = 0; i < 256; ++i) |
| 59 |
printf("%d\t%lld\n", i, (long long)hist[i]); |
| 60 |
free(cnt); |
| 61 |
|
| 62 |
smem_itr_destroy(itr); |
| 63 |
bwt_destroy(bwt); |
| 64 |
kseq_destroy(ks); |
| 65 |
gzclose(fp); |
| 66 |
return 0; |
| 67 |
} |