-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwtio.c
77 lines (67 loc) · 1.93 KB
/
bwtio.c
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "bwt.h"
#include "utils.h"
void bwt_dump_bwt(const char *fn, const bwt_t *bwt)
{
FILE *fp;
fp = xopen(fn, "wb");
fwrite(&bwt->primary, sizeof(bwtint_t), 1, fp);
fwrite(bwt->L2+1, sizeof(bwtint_t), 4, fp);
fwrite(bwt->bwt, 4, bwt->bwt_size, fp);
fclose(fp);
}
void bwt_dump_sa(const char *fn, const bwt_t *bwt)
{
FILE *fp;
fp = xopen(fn, "wb");
fwrite(&bwt->primary, sizeof(bwtint_t), 1, fp);
fwrite(bwt->L2+1, sizeof(bwtint_t), 4, fp);
fwrite(&bwt->sa_intv, sizeof(bwtint_t), 1, fp);
fwrite(&bwt->seq_len, sizeof(bwtint_t), 1, fp);
fwrite(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp);
fclose(fp);
}
void bwt_restore_sa(const char *fn, bwt_t *bwt)
{
char skipped[256];
FILE *fp;
bwtint_t primary;
fp = xopen(fn, "rb");
fread(&primary, sizeof(bwtint_t), 1, fp);
xassert(primary == bwt->primary, "SA-BWT inconsistency: primary is not the same.");
fread(skipped, sizeof(bwtint_t), 4, fp); // skip
fread(&bwt->sa_intv, sizeof(bwtint_t), 1, fp);
fread(&primary, sizeof(bwtint_t), 1, fp);
xassert(primary == bwt->seq_len, "SA-BWT inconsistency: seq_len is not the same.");
bwt->n_sa = (bwt->seq_len + bwt->sa_intv) / bwt->sa_intv;
bwt->sa = (bwtint_t*)calloc(bwt->n_sa, sizeof(bwtint_t));
bwt->sa[0] = -1;
fread(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp);
fclose(fp);
}
bwt_t *bwt_restore_bwt(const char *fn)
{
bwt_t *bwt;
FILE *fp;
bwt = (bwt_t*)calloc(1, sizeof(bwt_t));
fp = xopen(fn, "rb");
fseek(fp, 0, SEEK_END);
bwt->bwt_size = (ftell(fp) - sizeof(bwtint_t) * 5) >> 2;
bwt->bwt = (uint32_t*)calloc(bwt->bwt_size, 4);
fseek(fp, 0, SEEK_SET);
fread(&bwt->primary, sizeof(bwtint_t), 1, fp);
fread(bwt->L2+1, sizeof(bwtint_t), 4, fp);
fread(bwt->bwt, 4, bwt->bwt_size, fp);
bwt->seq_len = bwt->L2[4];
fclose(fp);
bwt_gen_cnt_table(bwt);
return bwt;
}
void bwt_destroy(bwt_t *bwt)
{
if (bwt == 0) return;
free(bwt->sa); free(bwt->bwt);
free(bwt);
}